| 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 | // Log to verify the mock is properly set up |
| 62 | console.log("Mock setup:", openAiAdapters); |
| 63 | }); |
| 64 | |
| 65 | test("Invalid tool call args are ignored", async () => { |
| 66 | const anthropic = new Anthropic({ |
| 67 | model: "not-important", |
| 68 | apiKey: "invalid", |
| 69 | }); |
| 70 | await dudLLMCall(anthropic, messagesWithInvalidToolCallArgs); |
| 71 | expect(fetchwithRequestOptions).toHaveBeenCalledWith( |
| 72 | expect.any(URL), |
| 73 | { |
| 74 | method: "POST", |
| 75 | headers: expect.any(Object), |
| 76 | signal: expect.any(AbortSignal), |
| 77 | body: expect.stringContaining('"name":"say_name","input":{}'), |
| 78 | }, |
| 79 | expect.any(Object), |
| 80 | ); |
| 81 | |
| 82 | vi.clearAllMocks(); |
| 83 | const gemini = new Gemini({ model: "gemini-something", apiKey: "invalid" }); |
| 84 | await dudLLMCall(gemini, messagesWithInvalidToolCallArgs); |
| 85 | expect(fetchwithRequestOptions).toHaveBeenCalledWith( |
| 86 | expect.any(URL), |
| 87 | { |
| 88 | method: "POST", |
| 89 | // headers: expect.any(Object), |
| 90 | signal: expect.any(AbortSignal), |
| 91 | body: expect.stringContaining('"name":"say_name","args":{}'), |
| 92 | }, |
| 93 | expect.any(Object), |
| 94 | ); |
| 95 | |
| 96 | // OPENAI DOES NOT NEED TO CLEAR INVALID TOOL CALL ARGS BECAUSE IT STORES THEM IN STRINGS |
| 97 | vi.clearAllMocks(); |
| 98 | const openai = new OpenAI({ model: "gpt-something", apiKey: "invalid" }); |
| 99 | await dudLLMCall(openai, messagesWithInvalidToolCallArgs); |
| 100 | expect(fetchwithRequestOptions).toHaveBeenCalledWith( |
| 101 | expect.any(URL), |
| 102 | { |
| 103 | method: "POST", |
| 104 | headers: expect.any(Object), |
| 105 | signal: expect.any(AbortSignal), |
| 106 | body: expect.stringContaining(JSON.stringify(invalidToolCallArg)), |
| 107 | }, |
| 108 | expect.any(Object), |
| 109 | ); |
| 110 | }); |
| 111 | }); |