llm pre-fetch tests: nested dependency mocks for continue fetch
This commit is contained in:
parent
4137954fea
commit
68ce00ec5c
3 changed files with 137 additions and 20 deletions
16
core/__mocks__/@continuedev/fetch/index.ts
Normal file
16
core/__mocks__/@continuedev/fetch/index.ts
Normal file
|
|
@ -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 "";
|
||||
});
|
||||
113
core/llm/llm-pre-fetch.vitest.ts
Normal file
113
core/llm/llm-pre-fetch.vitest.ts
Normal file
|
|
@ -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),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
@ -1,26 +1,14 @@
|
|||
import { ToolCallDelta } from "..";
|
||||
|
||||
function safeParseArgs(
|
||||
args: string | undefined,
|
||||
errorId?: string,
|
||||
): Record<string, any> {
|
||||
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<string, any> {
|
||||
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 {};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue