mirror of
https://github.com/BinaryBeastMaster/chat-relay.git
synced 2026-04-30 04:39:28 +00:00
86 lines
3 KiB
TypeScript
86 lines
3 KiB
TypeScript
/*
|
|
* Chat Relay: Relay for AI Chat Interfaces
|
|
* Copyright (C) 2025 Jamison Moore
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
* License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see https://www.gnu.org/licenses/.
|
|
*/
|
|
import { MCPTool } from "mcp-framework";
|
|
import { z } from "zod";
|
|
import fs from "fs/promises";
|
|
import path from "path";
|
|
import fetch from "node-fetch";
|
|
|
|
interface ReadFileInput {
|
|
path: string;
|
|
}
|
|
|
|
class ReadFileTool extends MCPTool<ReadFileInput> {
|
|
name = "read_file";
|
|
description = "Reads a file and sends its content through the API relay server for processing";
|
|
|
|
schema = {
|
|
path: {
|
|
type: z.string(),
|
|
description: "Path to the file to read",
|
|
},
|
|
};
|
|
|
|
async execute(input: ReadFileInput) {
|
|
try {
|
|
// Read the file
|
|
const filePath = path.resolve(input.path);
|
|
const content = await fs.readFile(filePath, "utf-8");
|
|
|
|
// Send the file content to the API relay server
|
|
const response = await fetch('http://localhost:3003/v1/chat/completions', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
model: "chatgpt",
|
|
messages: [
|
|
{
|
|
role: "user",
|
|
content: `The following is the content of the file at path ${input.path}:\n\n${content}\n\nPlease analyze this file content and provide any insights or suggestions.`
|
|
}
|
|
],
|
|
temperature: 0.7,
|
|
max_tokens: 100
|
|
})
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorText = await response.text();
|
|
console.error(`MCP Tool: Error from API relay server: ${response.status} ${response.statusText}`, errorText);
|
|
return `Error from API relay server: ${response.status} ${response.statusText}`;
|
|
}
|
|
|
|
const data = await response.json();
|
|
console.log(`MCP Tool: Received response from API relay server:`, data);
|
|
|
|
// Extract the assistant's message from the response
|
|
const responseData = data as any; // Type assertion
|
|
const assistantMessage = responseData.choices[0].message.content;
|
|
|
|
// Return both the file content and the analysis
|
|
return `File content:\n\n${content}\n\nAnalysis: ${assistantMessage}`;
|
|
} catch (error: any) {
|
|
console.error("MCP Tool: Error reading file or sending to API relay server:", error);
|
|
return `Error: ${error.message || 'Unknown error'}`;
|
|
}
|
|
}
|
|
}
|
|
|
|
export default ReadFileTool;
|