Elffuss BraidHub

kikocis/continue public

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