| 1 | import { fetchwithRequestOptions } from "@continuedev/fetch"; |
| 2 | import * as openAiAdapters from "@continuedev/openai-adapters"; |
| 3 | import * as dotenv from "dotenv"; |
| 4 | import { beforeEach, describe, expect, test, vi } from "vitest"; |
| 5 | import { ChatMessage, ILLM } from ".."; |
| 6 | import Anthropic from "./llms/Anthropic"; |
| 7 | import Gemini from "./llms/Gemini"; |
| 8 | import OpenAI from "./llms/OpenAI"; |
| 9 | |
| 10 | dotenv.config(); |
| 11 | |
| 12 | vi.mock("@continuedev/fetch"); |
| 13 | vi.mock("@continuedev/openai-adapters"); |
| 14 | |
| 15 | async function dudLLMCall(llm: ILLM, messages: ChatMessage[]) { |
| 16 | try { |
| 17 | const abortController = new AbortController(); |
| 18 | const gen = llm.streamChat(messages, abortController.signal, {}); |
| 19 | await gen.next(); |
| 20 | await gen.return({ |
| 21 | completion: "", |
| 22 | modelTitle: "", |
| 23 | modelProvider: "", |
| 24 | prompt: "", |
| 25 | }); |
| 26 | abortController.abort(); |
| 27 | } catch (e) { |
| 28 | console.error("Expected error", e); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | const invalidToolCallArg = '{"name": "Ali'; |
| 33 | const messagesWithInvalidToolCallArgs: ChatMessage[] = [ |
| 34 | { |
| 35 | role: "user", |
| 36 | content: "Call the say_hello tool", |
| 37 | }, |
| 38 | { |
| 39 | role: "assistant", |
| 40 | content: "", |
| 41 | toolCalls: [ |
| 42 | { |
| 43 | id: "tool_call_1", |
| 44 | type: "function", |
| 45 | function: { |
| 46 | name: "say_name", |
| 47 | arguments: invalidToolCallArg, |
| 48 | }, |
| 49 | }, |
| 50 | ], |
| 51 | }, |
| 52 | { |
| 53 | role: "user", |
| 54 | content: "This is my response", |
| 55 | }, |
| 56 | ]; |
| 57 | |
| 58 | describe("LLM Pre-fetch", () => { |
| 59 | beforeEach(() => { |
| 60 | vi.resetAllMocks(); |
| 61 | // Mock getAnthropicHeaders to return fake headers |
| 62 | vi.mocked(openAiAdapters.getAnthropicHeaders).mockReturnValue({ |
| 63 | fake: "headers", |
| 64 | }); |
| 65 | // Log to verify the mock is properly set up |
| 66 | console.log("Mock setup:", openAiAdapters); |
| 67 | }); |
| 68 | |
| 69 | test("Invalid tool call args are ignored", async () => { |
| 70 | const anthropic = new Anthropic({ |
| 71 | model: "not-important", |
| 72 | apiKey: "invalid", |
| 73 | }); |
| 74 | await dudLLMCall(anthropic, messagesWithInvalidToolCallArgs); |
| 75 | expect(fetchwithRequestOptions).toHaveBeenCalledWith( |
| 76 | expect.any(URL), |
| 77 | { |
| 78 | method: "POST", |
| 79 | headers: expect.any(Object), |
| 80 | signal: expect.any(AbortSignal), |
| 81 | body: expect.stringContaining('"name":"say_name","input":{}'), |
| 82 | }, |
| 83 | expect.any(Object), |
| 84 | ); |
| 85 | |
| 86 | vi.clearAllMocks(); |
| 87 | const gemini = new Gemini({ model: "gemini-something", apiKey: "invalid" }); |
| 88 | await dudLLMCall(gemini, messagesWithInvalidToolCallArgs); |
| 89 | expect(fetchwithRequestOptions).toHaveBeenCalledWith( |
| 90 | expect.any(URL), |
| 91 | { |
| 92 | method: "POST", |
| 93 | // headers: expect.any(Object), |
| 94 | signal: expect.any(AbortSignal), |
| 95 | body: expect.stringContaining('"name":"say_name","args":{}'), |
| 96 | }, |
| 97 | expect.any(Object), |
| 98 | ); |
| 99 | |
| 100 | // OPENAI DOES NOT NEED TO CLEAR INVALID TOOL CALL ARGS BECAUSE IT STORES THEM IN STRINGS |
| 101 | vi.clearAllMocks(); |
| 102 | const openai = new OpenAI({ model: "gpt-something", apiKey: "invalid" }); |
| 103 | await dudLLMCall(openai, messagesWithInvalidToolCallArgs); |
| 104 | expect(fetchwithRequestOptions).toHaveBeenCalledWith( |
| 105 | expect.any(URL), |
| 106 | { |
| 107 | method: "POST", |
| 108 | headers: expect.any(Object), |
| 109 | signal: expect.any(AbortSignal), |
| 110 | body: expect.stringContaining(JSON.stringify(invalidToolCallArg)), |
| 111 | }, |
| 112 | expect.any(Object), |
| 113 | ); |
| 114 | }); |
| 115 | }); |