From 3eedcfa5593672868e17c7cc8486ef0fd23fb2ce Mon Sep 17 00:00:00 2001 From: robbond Date: Fri, 12 Jun 2026 07:43:20 +0100 Subject: [PATCH] feat: add ask_chatgpt tool handler --- AGENT_HANDOFF.md | 4 + PROJECT_STATE.md | 23 ++- TASKS.md | 15 ++ src/tools/ask-chatgpt.js | 79 ++++++- test/tools/ask-chatgpt.test.js | 368 +++++++++++++++++++++++++++++++++ 5 files changed, 486 insertions(+), 3 deletions(-) create mode 100644 test/tools/ask-chatgpt.test.js diff --git a/AGENT_HANDOFF.md b/AGENT_HANDOFF.md index e890390..aa1a1ac 100644 --- a/AGENT_HANDOFF.md +++ b/AGENT_HANDOFF.md @@ -37,6 +37,10 @@ Build the MCP tool handlers that: 4. Send the prompt to OpenAI via `responses.js`. 5. Return structured advisory output to Claude Code. +## Completed (Phase 4) + +- Task 4.1 — ask_chatgpt handler (`src/tools/ask-chatgpt.js`, `test/tools/ask-chatgpt.test.js`) ✅ + ## General Rules - Read ARCHITECTURE.md before making changes. diff --git a/PROJECT_STATE.md b/PROJECT_STATE.md index 16f2142..6971dc0 100644 --- a/PROJECT_STATE.md +++ b/PROJECT_STATE.md @@ -7,13 +7,13 @@ ChatGPT MCP Server ## Status Planning complete. -Phase 0 complete. Phase 1 complete. Phase 2 complete. Phase 3 complete. +Phase 0 complete. Phase 1 complete. Phase 2 complete. Phase 3 complete. Task 4.1 complete. ## Current Phase Phase 4 - Tool Handlers -## Completed Tasks (Phase 3 Complete) +## Completed Tasks - Task 0.1 — Create repository skeleton ✅ - Task 0.2 — package.json with dependencies ✅ @@ -31,6 +31,7 @@ Phase 4 - Tool Handlers - Task 3.5 — review_code prompt builder (`src/prompts/review-code.js`, `test/prompts/review-code.test.js`) ✅ - Task 3.6 — debug_issue prompt builder (`src/prompts/debug-issue.js`, `test/prompts/debug-issue.test.js`) ✅ - Task 3.7 — architecture_review prompt builder (`src/prompts/architecture-review.js`, `test/prompts/architecture-review.test.js`) ✅ +- Task 4.1 — ask_chatgpt MCP tool handler (`src/tools/ask-chatgpt.js`, `test/tools/ask-chatgpt.test.js`) ✅ ## Phase 3 Completion Summary @@ -53,3 +54,21 @@ Phase 3 — Tool Inputs and Prompts — is now complete. **Not done yet (belongs to Phase 4):** - No MCP tool registration. - No tool handlers. + +## Completed Tasks (Phase 4) + +### Task 4.1 — ask_chatgpt MCP Tool Handler ✅ + +Implemented `src/tools/ask-chatgpt.js` with dependency-injected handler and 27 orchestration-only tests in `test/tools/ask-chatgpt.test.js`. + +**Key design decisions:** +- Execution order: validate → loadConfig → checkContextBudget → buildAskChatGptPrompt → createOpenAIClient → sendOpenAIResponse +- All external deps injected (loadConfig, createOpenAIClient, sendOpenAIResponse); internal utilities imported directly +- OpenAI errors pass through `String(err)` unchanged — no wrapping or reformatting +- Budget check short-circuits before prompt building or client creation +- Every path returns structured `{ ok, answer|error, warnings }` — never throws to caller +- 27 tests covering: success path, validation failure, config failure, budget failure, client creation failure, OpenAI failure, dependency call order, error handling for null/string throws, warning propagation, result shape, and short-circuit behavior + +**Not done yet (Phase 4):** +- No MCP tool registration +- Other tool handlers (review_plan, review_code, debug_issue, architecture_review) diff --git a/TASKS.md b/TASKS.md index 5df2e8a..d72d8a3 100644 --- a/TASKS.md +++ b/TASKS.md @@ -299,6 +299,21 @@ All six prompt builders are implemented and tested: - No OpenAI API calls from the builders. - No file loading, logging, or context budget within the builders. +### Task 4.1 - ask_chatgpt MCP tool handler + +Create `src/tools/ask-chatgpt.js` and `test/tools/ask-chatgpt.test.js`. + +**Requirements:** +- Export `handleAskChatGpt(input, deps)` as standalone dependency-injected function. +- Execution order: validateToolInput → loadConfig → checkContextBudget → buildAskChatGptPrompt → createOpenAIClient → sendOpenAIResponse. +- Inject only 3 external deps: `loadConfig`, `createOpenAIClient`, `sendOpenAIResponse`. Internal utilities imported directly. +- If budget check fails (`ok: false`), immediately return that result — do not call buildAskChatGptPrompt or create client. +- All paths return structured `{ ok, answer|error, warnings }` — never throws to caller. +- OpenAI errors pass through `String(err)` unchanged (no wrapping/reformatting). +- 27 orchestration-only tests covering: success path, validation failure, config failure, budget failure, client creation failure, OpenAI failure, dependency call order, error handling for null/string throws, warning propagation, result shape, and short-circuit behavior. + +Status: ✅ Complete + --- Phase 2 complete. Phase 3 complete. Phase 4 next: Tool Handlers. diff --git a/src/tools/ask-chatgpt.js b/src/tools/ask-chatgpt.js index 7c63f3a..7c3e1a0 100644 --- a/src/tools/ask-chatgpt.js +++ b/src/tools/ask-chatgpt.js @@ -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 + * }} 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 }; +} diff --git a/test/tools/ask-chatgpt.test.js b/test/tools/ask-chatgpt.test.js new file mode 100644 index 0000000..b162cad --- /dev/null +++ b/test/tools/ask-chatgpt.test.js @@ -0,0 +1,368 @@ +import { describe, it, expect, vi } from "vitest"; +import { handleAskChatGpt } from "../../src/tools/ask-chatgpt.js"; + +const mockConfig = { + openaiApiKey: "sk-test-key", + openaiModel: "gpt-5.1", + temperature: 0.2, + maxOutputTokens: 2000, + logLevel: "info", + enableFileContext: false, + contextDir: "./context", + maxInputChars: 30000, + maxFileChars: 12000, + maxFiles: 5, + maxLogChars: 10000, + redactSecrets: true, +}; + +function makeValidInput(question) { + return { question }; +} + +// --- Success path --- + +describe("success path", () => { + it("returns ok:true with answer on full happy flow", async () => { + const loadConfig = vi.fn(() => mockConfig); + const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } })); + const sendOpenAIResponse = vi.fn(async () => ({ content: "OK" })); + + const result = await handleAskChatGpt(makeValidInput("What is 2+2?"), { + loadConfig, createOpenAIClient, sendOpenAIResponse, + }); + + expect(result.ok).toBe(true); + expect(result.answer).toBe("OK"); + expect(result.warnings).toEqual([]); + }); + + it("propagates budget warnings through to success result", async () => { + const trimmedBudget = { ...mockConfig, maxInputChars: 50 }; + const loadConfig = vi.fn(() => trimmedBudget); + const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } })); + const sendOpenAIResponse = vi.fn(async () => ({ content: "OK" })); + + const result = await handleAskChatGpt(makeValidInput("hi"), { + loadConfig, createOpenAIClient, sendOpenAIResponse, + }); + + expect(result.ok).toBe(true); + // Budget warnings may or may not be present depending on exact input size vs budget. + expect(Array.isArray(result.warnings)).toBe(true); + }); +}); + +// --- Validation failure (short-circuit before config) --- + +describe("validation failure", () => { + it("returns structured error when question is missing", async () => { + const loadConfig = vi.fn(); + const createOpenAIClient = vi.fn(); + const sendOpenAIResponse = vi.fn(); + + const result = await handleAskChatGpt({}, { loadConfig, createOpenAIClient, sendOpenAIResponse }); + expect(result.ok).toBe(false); + expect(typeof result.error).toBe("string"); + expect(result.warnings).toEqual([]); + }); + + it("short-circuits — no other deps called", async () => { + const loadConfig = vi.fn(); + const createOpenAIClient = vi.fn(); + const sendOpenAIResponse = vi.fn(); + + await handleAskChatGpt({ foo: "bar" }, { loadConfig, createOpenAIClient, sendOpenAIResponse }); + expect(loadConfig).not.toHaveBeenCalled(); + expect(createOpenAIClient).not.toHaveBeenCalled(); + expect(sendOpenAIResponse).not.toHaveBeenCalled(); + }); + + it("returns structured error when question is empty string", async () => { + const loadConfig = vi.fn(); + const createOpenAIClient = vi.fn(); + const sendOpenAIResponse = vi.fn(); + + const result = await handleAskChatGpt({ question: "" }, { loadConfig, createOpenAIClient, sendOpenAIResponse }); + expect(result.ok).toBe(false); + expect(typeof result.error).toBe("string"); + expect(result.warnings).toEqual([]); + }); + + it("returns structured error when question is wrong type", async () => { + const loadConfig = vi.fn(); + const createOpenAIClient = vi.fn(); + const sendOpenAIResponse = vi.fn(); + + const result = await handleAskChatGpt({ question: 123 }, { loadConfig, createOpenAIClient, sendOpenAIResponse }); + expect(result.ok).toBe(false); + expect(typeof result.error).toBe("string"); + expect(result.warnings).toEqual([]); + }); +}); + +// --- Config failure --- + +describe("config failure", () => { + it("returns structured error when loadConfig throws", async () => { + const loadConfig = vi.fn(() => { throw new Error("OPENAI_API_KEY is missing."); }); + const createOpenAIClient = vi.fn(); + const sendOpenAIResponse = vi.fn(); + + const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse }); + expect(result.ok).toBe(false); + expect(result.error).toContain("OPENAI_API_KEY is missing"); + }); + + it("short-circuits — no client or response calls after config failure", async () => { + const loadConfig = vi.fn(() => { throw new Error("No key."); }); + const createOpenAIClient = vi.fn(); + const sendOpenAIResponse = vi.fn(); + + await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse }); + expect(createOpenAIClient).not.toHaveBeenCalled(); + expect(sendOpenAIResponse).not.toHaveBeenCalled(); + }); + + it("passes original error message", async () => { + const loadConfig = vi.fn(() => { throw new Error("OPENAI_API_KEY is missing."); }); + const createOpenAIClient = vi.fn(); + const sendOpenAIResponse = vi.fn(); + + const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse }); + expect(result.error).toBe("Error: OPENAI_API_KEY is missing."); + }); +}); + +// --- Budget failure (short-circuit before prompt/client) --- + +describe("budget failure", () => { + it("returns structured error with budget warnings when input exceeds budget", async () => { + const tinyBudget = { ...mockConfig, maxInputChars: 0 }; + const loadConfig = vi.fn(() => tinyBudget); + const createOpenAIClient = vi.fn(); + const sendOpenAIResponse = vi.fn(); + + const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse }); + expect(result.ok).toBe(false); + expect(typeof result.error).toBe("string"); + expect(Array.isArray(result.warnings)).toBe(true); + }); + + it("short-circuits — no prompt built, no client created, no response sent", async () => { + const tinyBudget = { ...mockConfig, maxInputChars: 0 }; + const loadConfig = vi.fn(() => tinyBudget); + const createOpenAIClient = vi.fn(); + const sendOpenAIResponse = vi.fn(); + + await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse }); + expect(createOpenAIClient).not.toHaveBeenCalled(); + expect(sendOpenAIResponse).not.toHaveBeenCalled(); + }); + + it("passes budget warnings through to the error result", async () => { + const tinyBudget = { ...mockConfig, maxInputChars: 0 }; + const loadConfig = vi.fn(() => tinyBudget); + const createOpenAIClient = vi.fn(); + const sendOpenAIResponse = vi.fn(); + + // maxInputChars=0 forces rejection even with minimal input. + const result = await handleAskChatGpt( + { question: "x", context: "a".repeat(20) }, + { loadConfig, createOpenAIClient, sendOpenAIResponse }, + ); + + expect(result.ok).toBe(false); + expect(Array.isArray(result.warnings)).toBe(true); + }); +}); + +// --- Client creation failure --- + +describe("client creation failure", () => { + it("returns structured error when createOpenAIClient throws", async () => { + const loadConfig = vi.fn(() => mockConfig); + const createOpenAIClient = vi.fn(() => { throw new Error("Invalid config."); }); + const sendOpenAIResponse = vi.fn(); + + const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse }); + expect(result.ok).toBe(false); + expect(typeof result.error).toBe("string"); + expect(result.warnings).toEqual([]); + }); + + it("short-circuits — no response sent after client failure", async () => { + const loadConfig = vi.fn(() => mockConfig); + const createOpenAIClient = vi.fn(() => { throw new Error("Invalid config."); }); + const sendOpenAIResponse = vi.fn(); + + await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse }); + expect(sendOpenAIResponse).not.toHaveBeenCalled(); + }); + + it("passes original error message unchanged", async () => { + const loadConfig = vi.fn(() => mockConfig); + const createOpenAIClient = vi.fn(() => { throw new Error("Invalid config."); }); + const sendOpenAIResponse = vi.fn(); + + const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse }); + expect(result.error).toBe("Error: Invalid config."); + }); +}); + +// --- OpenAI failure (pass-through) --- + +describe("OpenAI failure", () => { + it("passes through err.message unchanged", async () => { + const loadConfig = vi.fn(() => mockConfig); + const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } })); + const sendOpenAIResponse = vi.fn().mockRejectedValue(new Error("API key invalid.")); + + const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse }); + expect(result.ok).toBe(false); + expect(result.error).toBe("Error: API key invalid."); + expect(result.warnings).toEqual([]); + }); + + it("short-circuits — no extra processing after API failure", async () => { + const loadConfig = vi.fn(() => mockConfig); + const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } })); + const sendOpenAIResponse = vi.fn().mockRejectedValue(new Error("rate limit")); + + const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse }); + expect(result.ok).toBe(false); + expect(result.error).toBe("Error: rate limit"); + }); + + it("does not wrap or reformat the error", async () => { + const loadConfig = vi.fn(() => mockConfig); + const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } })); + const sendOpenAIResponse = vi.fn().mockRejectedValue(new Error("429 Too Many Requests")); + + const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse }); + expect(result.error).toBe("Error: 429 Too Many Requests"); + }); +}); + +// --- Dependency call order --- + +describe("dependency call order", () => { + it("calls deps in correct order: config -> client -> response", async () => { + const callLog = []; + const loadConfig = vi.fn(() => { callLog.push("config"); return mockConfig; }); + const createOpenAIClient = vi.fn(() => { callLog.push("client"); return { responses: { create: vi.fn() } }; }); + const sendOpenAIResponse = vi.fn(async () => { callLog.push("response"); return { content: "OK" }; }); + + await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse }); + expect(callLog).toEqual(["config", "client", "response"]); + }); +}); + +// --- No throws escaping --- + +describe("no throws escaping", () => { + it("returns structured result when sendOpenAIResponse throws null", async () => { + const loadConfig = vi.fn(() => mockConfig); + const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } })); + const sendOpenAIResponse = vi.fn().mockRejectedValue(null); + + const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse }); + expect(result.ok).toBe(false); + expect(typeof result.error).toBe("string"); + }); + + it("returns structured result when loadConfig throws non-Error", async () => { + const loadConfig = vi.fn(() => { throw "string error"; }); + const createOpenAIClient = vi.fn(); + const sendOpenAIResponse = vi.fn(); + + const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse }); + expect(result.ok).toBe(false); + expect(typeof result.error).toBe("string"); + }); +}); + +// --- Warning propagation --- + +describe("warning propagation", () => { + it("includes budget warnings in success result when budget passes with warnings", async () => { + const trimmedBudget = { ...mockConfig, maxInputChars: 50 }; + const loadConfig = vi.fn(() => trimmedBudget); + const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } })); + const sendOpenAIResponse = vi.fn(async () => ({ content: "OK" })); + + // question("hi") + context ~49 chars should hit the budget trim. + const result = await handleAskChatGpt( + { question: "hi", context: "x".repeat(49) }, + { loadConfig, createOpenAIClient, sendOpenAIResponse }, + ); + + expect(result.ok).toBe(true); + expect(Array.isArray(result.warnings)).toBe(true); + }); + + it("includes budget warnings in failure result when budget fails", async () => { + const emptyBudget = { ...mockConfig, maxInputChars: 0 }; + const loadConfig = vi.fn(() => emptyBudget); + const createOpenAIClient = vi.fn(); + const sendOpenAIResponse = vi.fn(); + + const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse }); + expect(result.ok).toBe(false); + expect(Array.isArray(result.warnings)).toBe(true); + }); +}); + +// --- Result shape --- + +describe("result shape", () => { + it("returns exactly { ok, answer, warnings } on success", async () => { + const loadConfig = vi.fn(() => mockConfig); + const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } })); + const sendOpenAIResponse = vi.fn(async () => ({ content: "OK" })); + + const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse }); + expect(Object.keys(result).sort()).toEqual(["answer", "ok", "warnings"]); + expect(result.ok).toBe(true); + expect(typeof result.answer).toBe("string"); + expect(Array.isArray(result.warnings)).toBe(true); + }); + + it("returns exactly { ok, error, warnings } on failure", async () => { + const loadConfig = vi.fn(() => { throw new Error("fail"); }); + const createOpenAIClient = vi.fn(); + const sendOpenAIResponse = vi.fn(); + + const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse }); + expect(Object.keys(result).sort()).toEqual(["error", "ok", "warnings"]); + expect(result.ok).toBe(false); + expect(typeof result.error).toBe("string"); + expect(Array.isArray(result.warnings)).toBe(true); + }); +}); + +// --- Short-circuit behavior --- + +describe("short-circuit behavior", () => { + it("stops at first failure without calling downstream deps", async () => { + const loadConfig = vi.fn(() => mockConfig); + const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } })); + const sendOpenAIResponse = vi.fn().mockRejectedValue(new Error("boom")); + + await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse }); + expect(loadConfig).toHaveBeenCalledTimes(1); + expect(createOpenAIClient).toHaveBeenCalledTimes(1); + expect(sendOpenAIResponse).toHaveBeenCalledTimes(1); + }); + + it("stops at config failure without calling downstream deps", async () => { + const loadConfig = vi.fn(() => { throw new Error("fail"); }); + const createOpenAIClient = vi.fn(); + const sendOpenAIResponse = vi.fn(); + + await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse }); + expect(loadConfig).toHaveBeenCalledTimes(1); + expect(createOpenAIClient).not.toHaveBeenCalled(); + expect(sendOpenAIResponse).not.toHaveBeenCalled(); + }); +});