feat: add ask_chatgpt tool handler

This commit is contained in:
2026-06-12 07:43:20 +01:00
parent 3f6eec38e2
commit 3eedcfa559
5 changed files with 486 additions and 3 deletions
+78 -1
View File
@@ -1 +1,78 @@
// ask_chatgpt tool handler.
// Tool handler for the ask_chatgpt MCP tool.
import { validateToolInput } from "./schemas.js";
import { checkContextBudget } from "../utils/context-budget.js";
import { buildAskChatGptPrompt } from "../prompts/ask-chatgpt.js";
/**
* Handle the ask_chatgpt MCP tool.
*
* Orchestration order: validate -> config -> budget -> prompt -> client -> response.
* All external dependencies injected via deps. Internal utilities imported directly.
* No throws escape — all paths return structured results.
*
* @param {unknown} input
* Raw tool input per ARCHITECTURE.md §7 schema.
* @param {{
* loadConfig: () => object,
* createOpenAIClient: (config: object) => any,
* sendOpenAIResponse: (client: any, params: object) => Promise<any>
* }} deps
* Injected external dependencies.
* @returns {Promise<{ ok: true, answer: string, warnings: string[] } | { ok: false, error: string, warnings: string[] }>}
*/
export async function handleAskChatGpt(input, deps) {
// --- 1. Validate input (before anything else) ---
const validation = validateToolInput(input);
if (!validation.ok) {
return { ok: false, error: validation.errors.join(" | "), warnings: [] };
}
// --- 2. Load config ---
let config;
try {
config = deps.loadConfig();
} catch (err) {
return { ok: false, error: String(err), warnings: [] };
}
// --- 3. Context budget check ---
const budget = checkContextBudget(validation.data, config);
if (!budget.ok) {
return { ok: false, error: budget.error, warnings: budget.warnings };
}
// --- 4. Build prompt ---
const promptMessages = buildAskChatGptPrompt(budget.input);
// --- 5. Create OpenAI client ---
let client;
try {
client = deps.createOpenAIClient(config);
} catch (err) {
return { ok: false, error: String(err), warnings: [] };
}
// --- 6. Send to OpenAI ---
let aiResult;
try {
aiResult = await deps.sendOpenAIResponse(client, {
input: [{ role: "system", content: promptMessages }],
model: config.openaiModel,
temperature: config.temperature,
maxOutputTokens: config.maxOutputTokens,
});
} catch (err) {
return { ok: false, error: String(err), warnings: [] };
}
// --- 7. Success ---
return { ok: true, answer: aiResult.content, warnings: budget.warnings };
}