diff --git a/core/__mocks__/@continuedev/fetch/index.ts b/core/__mocks__/@continuedev/fetch/index.ts new file mode 100644 index 000000000..526cbe2ea --- /dev/null +++ b/core/__mocks__/@continuedev/fetch/index.ts @@ -0,0 +1,16 @@ +import { vi } from "vitest"; + +export const fetchwithRequestOptions = vi.fn( + async (url, options, requestOptions) => { + console.log("Mocked fetch called with:", url, options, requestOptions); + return { + ok: true, + status: 200, + statusText: "OK", + }; + }, +); + +export const streamSse = vi.fn(function* () { + yield ""; +}); diff --git a/core/llm/llm-pre-fetch.vitest.ts b/core/llm/llm-pre-fetch.vitest.ts new file mode 100644 index 000000000..3db39fc3e --- /dev/null +++ b/core/llm/llm-pre-fetch.vitest.ts @@ -0,0 +1,113 @@ +import { fetchwithRequestOptions } from "@continuedev/fetch"; +import * as openAiAdapters from "@continuedev/openai-adapters"; +import * as dotenv from "dotenv"; +import { beforeEach, describe, expect, test, vi } from "vitest"; +import { ChatMessage, ILLM } from ".."; +import Anthropic from "./llms/Anthropic"; +import Gemini from "./llms/Gemini"; +import OpenAI from "./llms/OpenAI"; + +dotenv.config(); + +vi.mock("@continuedev/fetch"); +vi.mock("@continuedev/openai-adapters"); + +async function dudLLMCall(llm: ILLM, messages: ChatMessage[]) { + try { + const abortController = new AbortController(); + const gen = llm.streamChat(messages, abortController.signal, {}); + await gen.next(); + await gen.return({ + completion: "", + completionOptions: { + model: "", + }, + modelTitle: "", + prompt: "", + }); + abortController.abort(); + } catch (e) { + console.error("Expected error", e); + } +} + +const invalidToolCallArg = '{"name": "Ali'; +const messagesWithInvalidToolCallArgs: ChatMessage[] = [ + { + role: "user", + content: "Call the say_hello tool", + }, + { + role: "assistant", + content: "", + toolCalls: [ + { + id: "tool_call_1", + type: "function", + function: { + name: "say_name", + arguments: invalidToolCallArg, + }, + }, + ], + }, + { + role: "user", + content: "This is my response", + }, +]; + +describe("LLM Pre-fetch", () => { + beforeEach(() => { + vi.resetAllMocks(); + // Log to verify the mock is properly set up + console.log("Mock setup:", openAiAdapters); + }); + + test("Invalid tool call args are ignored", async () => { + const anthropic = new Anthropic({ + model: "not-important", + apiKey: "invalid", + }); + await dudLLMCall(anthropic, messagesWithInvalidToolCallArgs); + expect(fetchwithRequestOptions).toHaveBeenCalledWith( + expect.any(URL), + { + method: "POST", + headers: expect.any(Object), + signal: expect.any(AbortSignal), + body: expect.stringContaining('"name":"say_name","input":{}'), + }, + expect.any(Object), + ); + + vi.clearAllMocks(); + const gemini = new Gemini({ model: "gemini-something", apiKey: "invalid" }); + await dudLLMCall(gemini, messagesWithInvalidToolCallArgs); + expect(fetchwithRequestOptions).toHaveBeenCalledWith( + expect.any(URL), + { + method: "POST", + // headers: expect.any(Object), + signal: expect.any(AbortSignal), + body: expect.stringContaining('"name":"say_name","args":{}'), + }, + expect.any(Object), + ); + + // OPENAI DOES NOT NEED TO CLEAR INVALID TOOL CALL ARGS BECAUSE IT STORES THEM IN STRINGS + vi.clearAllMocks(); + const openai = new OpenAI({ model: "gpt-something", apiKey: "invalid" }); + await dudLLMCall(openai, messagesWithInvalidToolCallArgs); + expect(fetchwithRequestOptions).toHaveBeenCalledWith( + expect.any(URL), + { + method: "POST", + headers: expect.any(Object), + signal: expect.any(AbortSignal), + body: expect.stringContaining(JSON.stringify(invalidToolCallArg)), + }, + expect.any(Object), + ); + }); +}); diff --git a/core/tools/parseArgs.ts b/core/tools/parseArgs.ts index 9e43ec48f..36d75e020 100644 --- a/core/tools/parseArgs.ts +++ b/core/tools/parseArgs.ts @@ -1,26 +1,14 @@ import { ToolCallDelta } from ".."; -function safeParseArgs( - args: string | undefined, - errorId?: string, -): Record { - try { - return JSON.parse(args ?? "{}"); - } catch (e) { - const identifier = errorId ? `Call: ${errorId}\nArgs:${args}\n` : ""; - console.error( - `Failed to parse tool call arguments\n${identifier}Error:`, - e, - ); - return {}; - } -} - export function safeParseToolCallArgs( toolCall: ToolCallDelta, ): Record { - return safeParseArgs( - toolCall.function?.arguments, - `${toolCall.function?.name} ${toolCall.id}`, - ); + try { + return JSON.parse(toolCall.function?.arguments ?? "{}"); + } catch (e) { + console.error( + `Failed to parse tool call arguments:\nTool call: ${toolCall.function?.name + " " + toolCall.id}\nArgs:${toolCall.function?.arguments}\n`, + ); + return {}; + } }