Elffuss BraidHub

kikocis/continue public

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