diff --git a/.github/workflows/pr_checks.yaml b/.github/workflows/pr_checks.yaml index 36eebc970..1ef0553e2 100644 --- a/.github/workflows/pr_checks.yaml +++ b/.github/workflows/pr_checks.yaml @@ -613,6 +613,9 @@ jobs: echo "test_file_matrix<> $GITHUB_OUTPUT echo "$FILES" >> $GITHUB_OUTPUT echo "EOF" >> $GITHUB_OUTPUT + env: + # https://github.com/microsoft/vscode-ripgrep/issues/9#issuecomment-643965333 + GITHUB_TOKEN: ${{ secrets.CI_GITHUB_TOKEN }} - name: Debug Outputs run: | diff --git a/core/.eslintrc.json b/core/.eslintrc.json index 08948b866..7b4563436 100644 --- a/core/.eslintrc.json +++ b/core/.eslintrc.json @@ -19,5 +19,13 @@ "max-depth": ["error", { "max": 6 }], "max-nested-callbacks": ["error", { "max": 4 }], "max-params": ["error", { "max": 8 }] - } + }, + "overrides": [ + { + "files": ["**/*.vitest.ts"], + "rules": { + "max-lines-per-function": "off" + } + } + ] } diff --git a/core/indexing/docs/crawlers/DocsCrawler.test.ts b/core/indexing/docs/crawlers/DocsCrawler.test.ts index 96c6dc24d..d72d774ca 100644 --- a/core/indexing/docs/crawlers/DocsCrawler.test.ts +++ b/core/indexing/docs/crawlers/DocsCrawler.test.ts @@ -53,7 +53,7 @@ describe("DocsCrawler", () => { }; } - describe("GitHub Crawler", () => { + describe.skip("GitHub Crawler", () => { const repoUrl = "https://github.com/Patrick-Erichsen/test-github-repo-for-crawling"; diff --git a/core/llm/llms/Anthropic.vitest.ts b/core/llm/llms/Anthropic.vitest.ts new file mode 100644 index 000000000..5c623b05e --- /dev/null +++ b/core/llm/llms/Anthropic.vitest.ts @@ -0,0 +1,495 @@ +import { afterEach, describe, expect, test, vi } from "vitest"; +import { ILLM } from "../../index.js"; +import Anthropic from "./Anthropic.js"; + +interface LlmTestCase { + llm: ILLM; + methodToTest: keyof ILLM; + params: any[]; + expectedRequest: { + url: string; + method: string; + headers?: Record; + body?: Record; + }; + mockResponse?: any; + mockStream?: any[]; +} + +function createMockStream(mockStream: any[]) { + const encoder = new TextEncoder(); + return new ReadableStream({ + start(controller) { + for (const chunk of mockStream) { + controller.enqueue( + encoder.encode( + `data: ${ + typeof chunk === "string" ? chunk : JSON.stringify(chunk) + }\n\n`, + ), + ); + } + controller.close(); + }, + }); +} + +function setupMockFetch(mockResponse?: any, mockStream?: any[]) { + const mockFetch = vi.fn(); + + if (mockStream) { + const stream = createMockStream(mockStream); + mockFetch.mockResolvedValue( + new Response(stream, { + headers: { + "Content-Type": "text/event-stream", + }, + }), + ); + } else { + mockFetch.mockResolvedValue( + new Response(JSON.stringify(mockResponse), { + headers: { "Content-Type": "application/json" }, + }), + ); + } + + return mockFetch; +} + +function setupReadableStreamPolyfill() { + // This can be removed if https://github.com/nodejs/undici/issues/2888 is resolved + // @ts-ignore + const originalFrom = ReadableStream.from; + // @ts-ignore + ReadableStream.from = (body) => { + if (body?.source) { + return body; + } + return originalFrom(body); + }; +} + +async function executeLlmMethod( + llm: ILLM, + methodToTest: keyof ILLM, + params: any[], +) { + if (typeof (llm as any)[methodToTest] !== "function") { + throw new Error( + `Method ${String(methodToTest)} does not exist on the LLM instance.`, + ); + } + + const result = await (llm as any)[methodToTest](...params); + if (result?.next) { + for await (const _ of result) { + } + } +} + +function assertFetchCall(mockFetch: any, expectedRequest: any) { + expect(mockFetch).toHaveBeenCalledTimes(1); + const [url, options] = mockFetch.mock.calls[0]; + + expect(url.toString()).toBe(expectedRequest.url); + expect(options.method).toBe(expectedRequest.method); + + if (expectedRequest.headers) { + expect(options.headers).toEqual( + expect.objectContaining(expectedRequest.headers), + ); + } + + if (expectedRequest.body) { + const actualBody = JSON.parse(options.body as string); + expect(actualBody).toEqual(expectedRequest.body); + } +} + +async function runLlmTest(testCase: LlmTestCase) { + const { + llm, + methodToTest, + params, + expectedRequest, + mockResponse, + mockStream, + } = testCase; + + const mockFetch = setupMockFetch(mockResponse, mockStream); + setupReadableStreamPolyfill(); + + (llm as any).fetch = mockFetch; + + await executeLlmMethod(llm, methodToTest, params); + assertFetchCall(mockFetch, expectedRequest); +} + +describe("Anthropic", () => { + afterEach(() => { + vi.clearAllMocks(); + }); + + test("streamChat should send a valid request", async () => { + const anthropic = new Anthropic({ + apiKey: "test-api-key", + model: "claude-3-5-sonnet-latest", + apiBase: "https://api.anthropic.com/v1/", + }); + + await runLlmTest({ + llm: anthropic, + methodToTest: "streamChat", + params: [ + [{ role: "user", content: "hello" }], + new AbortController().signal, + ], + expectedRequest: { + url: "https://api.anthropic.com/v1/messages", + method: "POST", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "anthropic-version": "2023-06-01", + "x-api-key": "test-api-key", + }, + body: { + model: "claude-3-5-sonnet-latest", + max_tokens: 8192, + stream: true, + messages: [ + { + role: "user", + content: [{ type: "text", text: "hello" }], + }, + ], + system: "", + }, + }, + mockStream: [ + '{"type": "content_block_delta", "delta": {"type": "text_delta", "text": "Hello! How can I help you today?"}}', + '{"type": "content_block_stop"}', + ], + }); + }); + + test("chat should send a valid request", async () => { + const anthropic = new Anthropic({ + apiKey: "test-api-key", + model: "claude-3-5-sonnet-latest", + apiBase: "https://api.anthropic.com/v1/", + }); + + await runLlmTest({ + llm: anthropic, + methodToTest: "chat", + params: [ + [{ role: "user", content: "hello" }], + new AbortController().signal, + ], + expectedRequest: { + url: "https://api.anthropic.com/v1/messages", + method: "POST", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "anthropic-version": "2023-06-01", + "x-api-key": "test-api-key", + }, + body: { + model: "claude-3-5-sonnet-latest", + max_tokens: 8192, + stream: true, + messages: [ + { + role: "user", + content: [{ type: "text", text: "hello" }], + }, + ], + system: "", + }, + }, + mockStream: [ + '{"type": "content_block_delta", "delta": {"type": "text_delta", "text": "Hello! How can I help you today?"}}', + '{"type": "content_block_stop"}', + ], + }); + }); + + test("streamComplete should send a valid request", async () => { + const anthropic = new Anthropic({ + apiKey: "test-api-key", + model: "claude-3-5-sonnet-latest", + apiBase: "https://api.anthropic.com/v1/", + }); + + await runLlmTest({ + llm: anthropic, + methodToTest: "streamComplete", + params: ["Complete this: Hello", new AbortController().signal], + expectedRequest: { + url: "https://api.anthropic.com/v1/messages", + method: "POST", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "anthropic-version": "2023-06-01", + "x-api-key": "test-api-key", + }, + body: { + model: "claude-3-5-sonnet-latest", + max_tokens: 8192, + stream: true, + messages: [ + { + role: "user", + content: [{ type: "text", text: "Complete this: Hello" }], + }, + ], + system: "", + }, + }, + mockStream: [ + '{"type": "content_block_delta", "delta": {"type": "text_delta", "text": " world!"}}', + '{"type": "content_block_stop"}', + ], + }); + }); + + test("complete should send a valid request", async () => { + const anthropic = new Anthropic({ + apiKey: "test-api-key", + model: "claude-3-5-sonnet-latest", + apiBase: "https://api.anthropic.com/v1/", + }); + + await runLlmTest({ + llm: anthropic, + methodToTest: "complete", + params: ["Complete this: Hello", new AbortController().signal], + expectedRequest: { + url: "https://api.anthropic.com/v1/messages", + method: "POST", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "anthropic-version": "2023-06-01", + "x-api-key": "test-api-key", + }, + body: { + model: "claude-3-5-sonnet-latest", + max_tokens: 8192, + stream: true, + messages: [ + { + role: "user", + content: [{ type: "text", text: "Complete this: Hello" }], + }, + ], + system: "", + }, + }, + mockStream: [ + '{"type": "content_block_delta", "delta": {"type": "text_delta", "text": " world!"}}', + '{"type": "content_block_stop"}', + ], + }); + }); + + describe("Different configurations", () => { + test("should handle system message", async () => { + const anthropic = new Anthropic({ + apiKey: "test-api-key", + model: "claude-3-5-sonnet-latest", + apiBase: "https://api.anthropic.com/v1/", + }); + + await runLlmTest({ + llm: anthropic, + methodToTest: "streamChat", + params: [ + [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: "Hello!" }, + ], + new AbortController().signal, + ], + expectedRequest: { + url: "https://api.anthropic.com/v1/messages", + method: "POST", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "anthropic-version": "2023-06-01", + "x-api-key": "test-api-key", + }, + body: { + model: "claude-3-5-sonnet-latest", + max_tokens: 8192, + stream: true, + messages: [ + { + role: "user", + content: [{ type: "text", text: "Hello!" }], + }, + ], + system: "You are a helpful assistant.", + }, + }, + mockStream: [ + '{"type": "content_block_delta", "delta": {"type": "text_delta", "text": "Hello! How can I help you today?"}}', + '{"type": "content_block_stop"}', + ], + }); + }); + + test("should handle tool calls", async () => { + const anthropic = new Anthropic({ + apiKey: "test-api-key", + model: "claude-3-5-sonnet-latest", + apiBase: "https://api.anthropic.com/v1/", + }); + + const tools = [ + { + type: "function" as const, + function: { + name: "get_weather", + description: "Get the current weather", + parameters: { + type: "object", + properties: { + location: { type: "string" }, + }, + required: ["location"], + }, + }, + }, + ]; + + await runLlmTest({ + llm: anthropic, + methodToTest: "streamChat", + params: [ + [{ role: "user", content: "What's the weather in New York?" }], + new AbortController().signal, + { tools }, + ], + expectedRequest: { + url: "https://api.anthropic.com/v1/messages", + method: "POST", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "anthropic-version": "2023-06-01", + "x-api-key": "test-api-key", + }, + body: { + model: "claude-3-5-sonnet-latest", + max_tokens: 8192, + stream: true, + messages: [ + { + role: "user", + content: [ + { type: "text", text: "What's the weather in New York?" }, + ], + }, + ], + tools: [ + { + name: "get_weather", + description: "Get the current weather", + input_schema: { + type: "object", + properties: { + location: { type: "string" }, + }, + required: ["location"], + }, + }, + ], + system: "", + }, + }, + mockStream: [ + '{"type": "content_block_start", "content_block": {"type": "tool_use", "id": "toolu_123", "name": "get_weather"}}', + '{"type": "content_block_delta", "delta": {"type": "input_json_delta", "partial_json": "{\\"location\\": \\"New York\\""}}', + '{"type": "content_block_stop"}', + ], + }); + }); + + test("should handle custom max tokens", async () => { + const anthropic = new Anthropic({ + apiKey: "test-api-key", + model: "claude-3-5-sonnet-latest", + apiBase: "https://api.anthropic.com/v1/", + }); + + await runLlmTest({ + llm: anthropic, + methodToTest: "streamChat", + params: [ + [{ role: "user", content: "hello" }], + new AbortController().signal, + { maxTokens: 1000 }, + ], + expectedRequest: { + url: "https://api.anthropic.com/v1/messages", + method: "POST", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "anthropic-version": "2023-06-01", + "x-api-key": "test-api-key", + }, + body: { + model: "claude-3-5-sonnet-latest", + max_tokens: 1000, + stream: true, + messages: [ + { + role: "user", + content: [{ type: "text", text: "hello" }], + }, + ], + system: "", + }, + }, + mockStream: [ + '{"type": "content_block_delta", "delta": {"type": "text_delta", "text": "Hello!"}}', + '{"type": "content_block_stop"}', + ], + }); + }); + }); + + describe("Error handling", () => { + test("should throw error when API key is missing", async () => { + const anthropic = new Anthropic({ + apiKey: "", + model: "claude-3-5-sonnet-latest", + apiBase: "https://api.anthropic.com/v1/", + }); + + await expect( + runLlmTest({ + llm: anthropic, + methodToTest: "streamChat", + params: [ + [{ role: "user", content: "hello" }], + new AbortController().signal, + ], + expectedRequest: { + url: "https://api.anthropic.com/v1/messages", + method: "POST", + }, + }), + ).rejects.toThrow( + "Request not sent. You have an Anthropic model configured in your config.json, but the API key is not set.", + ); + }); + }); +}); diff --git a/core/llm/llms/Docker.ts b/core/llm/llms/Docker.ts index 3860a9b9e..a79a9b368 100644 --- a/core/llm/llms/Docker.ts +++ b/core/llm/llms/Docker.ts @@ -17,7 +17,7 @@ import OpenAI from "./OpenAI.js"; class Docker extends OpenAI implements ModelInstaller { static providerName = "docker"; static defaultOptions: Partial = { - apiBase: "http://localhost:12434/engines/v1", + apiBase: "http://localhost:12434/engines/v1/", model: "gemma3-4B-F4", // Default model }; diff --git a/core/llm/llms/Fireworks.ts b/core/llm/llms/Fireworks.ts index 558cc77a9..9008eedb1 100644 --- a/core/llm/llms/Fireworks.ts +++ b/core/llm/llms/Fireworks.ts @@ -5,7 +5,7 @@ import OpenAI from "./OpenAI.js"; class Fireworks extends OpenAI { static providerName = "fireworks"; static defaultOptions: Partial = { - apiBase: "https://api.fireworks.ai/inference/v1", + apiBase: "https://api.fireworks.ai/inference/v1/", }; private static modelConversion: { [key: string]: string } = { diff --git a/core/llm/llms/Fireworks.vitest.ts b/core/llm/llms/Fireworks.vitest.ts new file mode 100644 index 000000000..716aa0b03 --- /dev/null +++ b/core/llm/llms/Fireworks.vitest.ts @@ -0,0 +1,12 @@ +import Fireworks from "./Fireworks.js"; +import { createOpenAISubclassTests } from "./test-utils/openai-test-utils.js"; + +createOpenAISubclassTests(Fireworks, { + providerName: "fireworks", + defaultApiBase: "https://api.fireworks.ai/inference/v1/", + modelConversions: { + "starcoder-7b": "accounts/fireworks/models/starcoder-7b", + }, + modelConversionContent: + "<|im_start|>user\nhello<|im_end|>\n<|im_start|>assistant\n", +}); diff --git a/core/llm/llms/Groq.vitest.ts b/core/llm/llms/Groq.vitest.ts new file mode 100644 index 000000000..a5a0ad1a3 --- /dev/null +++ b/core/llm/llms/Groq.vitest.ts @@ -0,0 +1,12 @@ +import { createOpenAISubclassTests } from "./test-utils/openai-test-utils.js"; +import Groq from "./Groq.js"; + +createOpenAISubclassTests(Groq, { + providerName: "groq", + defaultApiBase: "https://api.groq.com/openai/v1/", + modelConversions: { + "mistral-8x7b": "mixtral-8x7b-32768", + "llama3-8b": "llama3-8b-8192", + "llama3-70b": "llama3-70b-8192", + }, +}); diff --git a/core/llm/llms/Kindo.ts b/core/llm/llms/Kindo.ts index 88df3c2fd..e728a0035 100644 --- a/core/llm/llms/Kindo.ts +++ b/core/llm/llms/Kindo.ts @@ -5,7 +5,7 @@ import OpenAI from "./OpenAI"; class Kindo extends OpenAI { static providerName = "kindo"; static defaultOptions: Partial = { - apiBase: "https://llm.kindo.ai/v1", + apiBase: "https://llm.kindo.ai/v1/", requestOptions: { headers: { "kindo-token-transaction-type": "CONTINUE", diff --git a/core/llm/llms/NCompass.ts b/core/llm/llms/NCompass.ts index 87ee69732..aaed5e1ad 100644 --- a/core/llm/llms/NCompass.ts +++ b/core/llm/llms/NCompass.ts @@ -6,9 +6,9 @@ import OpenAI from "./OpenAI.js"; class NCompass extends OpenAI { static providerName = "ncompass"; static defaultOptions: Partial = { - apiBase: "https://api.ncompass.tech/v1", + apiBase: "https://api.ncompass.tech/v1/", }; - static embeddingsApiEndpoint: "https://api.gcp.ncompass.tech/v1/embeddings"; + static embeddingsApiEndpoint = "https://api.gcp.ncompass.tech/v1/embeddings"; private static modelConversion: { [key: string]: string } = { "qwen2.5-coder-7b": "Qwen/Qwen2.5-Coder-7B-Instruct", diff --git a/core/llm/llms/OpenAI-compatible-core.vitest.ts b/core/llm/llms/OpenAI-compatible-core.vitest.ts new file mode 100644 index 000000000..23d076d07 --- /dev/null +++ b/core/llm/llms/OpenAI-compatible-core.vitest.ts @@ -0,0 +1,294 @@ +import { createOpenAISubclassTests } from "./test-utils/openai-test-utils.js"; + +// Import core OpenAI-compatible providers +import OpenAI from "./OpenAI.js"; +import Groq from "./Groq.js"; +import Fireworks from "./Fireworks.js"; +import Together from "./Together.js"; +import Deepseek from "./Deepseek.js"; +import OpenRouter from "./OpenRouter.js"; +import xAI from "./xAI.js"; +import Mistral from "./Mistral.js"; +import LMStudio from "./LMStudio.js"; +import Cerebras from "./Cerebras.js"; +import DeepInfra from "./DeepInfra.js"; +import Nvidia from "./Nvidia.js"; + +// Base OpenAI tests +import { afterEach, describe, expect, test, vi } from "vitest"; +import { ILLM } from "../../index.js"; + +interface LlmTestCase { + llm: ILLM; + methodToTest: keyof ILLM; + params: any[]; + expectedRequest: { + url: string; + method: string; + headers?: Record; + body?: Record; + }; + mockResponse?: any; + mockStream?: any[]; +} + +function createMockStream(mockStream: any[]) { + const encoder = new TextEncoder(); + return new ReadableStream({ + start(controller) { + for (const chunk of mockStream) { + controller.enqueue( + encoder.encode( + `data: ${ + typeof chunk === "string" ? chunk : JSON.stringify(chunk) + }\n\n`, + ), + ); + } + controller.close(); + }, + }); +} + +function setupMockFetch(mockResponse?: any, mockStream?: any[]) { + const mockFetch = vi.fn(); + + if (mockStream) { + const stream = createMockStream(mockStream); + mockFetch.mockResolvedValue( + new Response(stream, { + headers: { + "Content-Type": "text/event-stream", + }, + }), + ); + } else { + mockFetch.mockResolvedValue( + new Response(JSON.stringify(mockResponse), { + headers: { "Content-Type": "application/json" }, + }), + ); + } + + return mockFetch; +} + +function setupReadableStreamPolyfill() { + // This can be removed if https://github.com/nodejs/undici/issues/2888 is resolved + // @ts-ignore + const originalFrom = ReadableStream.from; + // @ts-ignore + ReadableStream.from = (body) => { + if (body?.source) { + return body; + } + return originalFrom(body); + }; +} + +async function executeLlmMethod( + llm: ILLM, + methodToTest: keyof ILLM, + params: any[], +) { + if (typeof (llm as any)[methodToTest] !== "function") { + throw new Error( + `Method ${String(methodToTest)} does not exist on the LLM instance.`, + ); + } + + const result = await (llm as any)[methodToTest](...params); + if (result?.next) { + for await (const _ of result) { + } + } +} + +function assertFetchCall(mockFetch: any, expectedRequest: any) { + expect(mockFetch).toHaveBeenCalledTimes(1); + const [url, options] = mockFetch.mock.calls[0]; + + expect(url.toString()).toBe(expectedRequest.url); + expect(options.method).toBe(expectedRequest.method); + + if (expectedRequest.headers) { + expect(options.headers).toEqual( + expect.objectContaining(expectedRequest.headers), + ); + } + + if (expectedRequest.body) { + const actualBody = JSON.parse(options.body as string); + expect(actualBody).toEqual(expectedRequest.body); + } +} + +async function runLlmTest(testCase: LlmTestCase) { + const { + llm, + methodToTest, + params, + expectedRequest, + mockResponse, + mockStream, + } = testCase; + + const mockFetch = setupMockFetch(mockResponse, mockStream); + setupReadableStreamPolyfill(); + + (llm as any).fetch = mockFetch; + + // Disable OpenAI adapter to use our custom fetch mock + (llm as any).useOpenAIAdapterFor = []; + + await executeLlmMethod(llm, methodToTest, params); + assertFetchCall(mockFetch, expectedRequest); +} + +describe("OpenAI", () => { + afterEach(() => { + vi.clearAllMocks(); + }); + + test("streamChat should send a valid request", async () => { + const openai = new OpenAI({ + apiKey: "test-api-key", + model: "gpt-4", + apiBase: "https://api.openai.com/v1/", + }); + + await runLlmTest({ + llm: openai, + methodToTest: "streamChat", + params: [ + [{ role: "user", content: "hello" }], + new AbortController().signal, + ], + expectedRequest: { + url: "https://api.openai.com/v1/chat/completions", + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + "api-key": "test-api-key", + }, + body: { + model: "gpt-4", + messages: [{ role: "user", content: "hello" }], + stream: true, + max_tokens: 2048, + }, + }, + mockStream: [ + { choices: [{ delta: { content: "Hello" } }] }, + { choices: [{ delta: { content: " world" } }] }, + ], + }); + }); +}); + +// Core OpenAI-compatible providers +createOpenAISubclassTests(Groq, { + providerName: "groq", + defaultApiBase: "https://api.groq.com/openai/v1/", + modelConversions: { + "mistral-8x7b": "mistral-8x7b", + "llama3-8b": "llama3-8b-8192", + }, + modelConversionContent: "[INST] hello [/INST]", + modelConversionMaxTokens: 4096, +}); + +createOpenAISubclassTests(Fireworks, { + providerName: "fireworks", + defaultApiBase: "https://api.fireworks.ai/inference/v1/", + modelConversions: { + "starcoder-7b": "starcoder-7b", + }, + modelConversionContent: + "<|im_start|>user\nhello<|im_end|>\n<|im_start|>assistant\n", +}); + +createOpenAISubclassTests(Together, { + providerName: "together", + defaultApiBase: "https://api.together.xyz/v1/", + modelConversions: { + "codellama-7b": "codellama-7b", + "llama3-8b": "meta-llama/Llama-3-8b-chat-hf", + }, + modelConversionContent: "hello", + customStreamCompleteEndpoint: "completions", +}); + +createOpenAISubclassTests(Deepseek, { + providerName: "deepseek", + defaultApiBase: "https://api.deepseek.com/", +}); + +createOpenAISubclassTests(OpenRouter, { + providerName: "openrouter", + defaultApiBase: "https://openrouter.ai/api/v1/", +}); + +createOpenAISubclassTests(xAI, { + providerName: "xAI", + defaultApiBase: "https://api.x.ai/v1/", + modelConversions: { + "grok-beta": "grok-beta", + }, + modelConversionContent: "hello", +}); + +createOpenAISubclassTests(Mistral, { + providerName: "mistral", + defaultApiBase: "https://api.mistral.ai/v1/", + modelConversions: { + "mistral-7b": "mistral-7b", + "mistral-8x7b": "open-mixtral-8x7b", + }, + modelConversionContent: "hello", +}); + +createOpenAISubclassTests(LMStudio, { + providerName: "lmstudio", + defaultApiBase: "http://localhost:1234/v1/", +}); + +createOpenAISubclassTests(Cerebras, { + providerName: "cerebras", + defaultApiBase: "https://api.cerebras.ai/v1/", + modelConversions: { + "llama3.1-8b": "llama3.1-8b", + "llama3.1-70b": "llama3.1-70b", + }, + modelConversionContent: + "<|start_header_id|>user<|end_header_id|>\nhello<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n", +}); + +createOpenAISubclassTests(DeepInfra, { + providerName: "deepinfra", + defaultApiBase: "https://api.deepinfra.com/v1/openai/", + customEmbeddingsUrl: + "https://api.deepinfra.com/v1/inference/text-embedding-ada-002", + customEmbeddingsHeaders: { + Authorization: "bearer test-api-key", + }, + customEmbeddingsBody: { + inputs: ["Hello", "World"], + }, +}); + +createOpenAISubclassTests(Nvidia, { + providerName: "nvidia", + defaultApiBase: "https://integrate.api.nvidia.com/v1/", + customEmbeddingsHeaders: { + Authorization: "Bearer test-api-key", + "Content-Type": "application/json", + }, + customEmbeddingsBody: { + input: ["Hello", "World"], + model: "text-embedding-ada-002", + input_type: "passage", + truncate: "END", + }, +}); diff --git a/core/llm/llms/OpenAI-compatible.vitest.ts b/core/llm/llms/OpenAI-compatible.vitest.ts new file mode 100644 index 000000000..2ec8de109 --- /dev/null +++ b/core/llm/llms/OpenAI-compatible.vitest.ts @@ -0,0 +1,453 @@ +import { createOpenAISubclassTests } from "./test-utils/openai-test-utils.js"; + +// Import all OpenAI-compatible providers +import OpenAI from "./OpenAI.js"; +import Groq from "./Groq.js"; +import Fireworks from "./Fireworks.js"; +import Together from "./Together.js"; +import Deepseek from "./Deepseek.js"; +import OpenRouter from "./OpenRouter.js"; +import xAI from "./xAI.js"; +import Mistral from "./Mistral.js"; +import LMStudio from "./LMStudio.js"; +import Cerebras from "./Cerebras.js"; +import DeepInfra from "./DeepInfra.js"; +import Nvidia from "./Nvidia.js"; +import SambaNova from "./SambaNova.js"; +import Scaleway from "./Scaleway.js"; +import Venice from "./Venice.js"; +import Moonshot from "./Moonshot.js"; +import Novita from "./Novita.js"; +import SiliconFlow from "./SiliconFlow.js"; +import Kindo from "./Kindo.js"; +import Azure from "./Azure.js"; +import Inception from "./Inception.js"; +import Docker from "./Docker.js"; +import Voyage from "./Voyage.js"; +import Vllm from "./Vllm.js"; +import TextGenWebUI from "./TextGenWebUI.js"; +// import Relace from "./Relace.js"; // Skip - causing import issues +import FunctionNetwork from "./FunctionNetwork.js"; +import NCompass from "./NCompass.js"; +import LlamaStack from "./LlamaStack.js"; +import Nebius from "./Nebius.js"; +import OVHcloud from "./OVHcloud.js"; + +// Base OpenAI tests +import { afterEach, describe, expect, test, vi } from "vitest"; +import { ILLM } from "../../index.js"; + +interface LlmTestCase { + llm: ILLM; + methodToTest: keyof ILLM; + params: any[]; + expectedRequest: { + url: string; + method: string; + headers?: Record; + body?: Record; + }; + mockResponse?: any; + mockStream?: any[]; +} + +function createMockStream(mockStream: any[]) { + const encoder = new TextEncoder(); + return new ReadableStream({ + start(controller) { + for (const chunk of mockStream) { + controller.enqueue( + encoder.encode( + `data: ${ + typeof chunk === "string" ? chunk : JSON.stringify(chunk) + }\n\n`, + ), + ); + } + controller.close(); + }, + }); +} + +function setupMockFetch(mockResponse?: any, mockStream?: any[]) { + const mockFetch = vi.fn(); + + if (mockStream) { + const stream = createMockStream(mockStream); + mockFetch.mockResolvedValue( + new Response(stream, { + headers: { + "Content-Type": "text/event-stream", + }, + }), + ); + } else { + mockFetch.mockResolvedValue( + new Response(JSON.stringify(mockResponse), { + headers: { "Content-Type": "application/json" }, + }), + ); + } + + return mockFetch; +} + +function setupReadableStreamPolyfill() { + // This can be removed if https://github.com/nodejs/undici/issues/2888 is resolved + // @ts-ignore + const originalFrom = ReadableStream.from; + // @ts-ignore + ReadableStream.from = (body) => { + if (body?.source) { + return body; + } + return originalFrom(body); + }; +} + +async function executeLlmMethod( + llm: ILLM, + methodToTest: keyof ILLM, + params: any[], +) { + if (typeof (llm as any)[methodToTest] !== "function") { + throw new Error( + `Method ${String(methodToTest)} does not exist on the LLM instance.`, + ); + } + + const result = await (llm as any)[methodToTest](...params); + if (result?.next) { + for await (const _ of result) { + } + } +} + +function assertFetchCall(mockFetch: any, expectedRequest: any) { + expect(mockFetch).toHaveBeenCalledTimes(1); + const [url, options] = mockFetch.mock.calls[0]; + + expect(url.toString()).toBe(expectedRequest.url); + expect(options.method).toBe(expectedRequest.method); + + if (expectedRequest.headers) { + expect(options.headers).toEqual( + expect.objectContaining(expectedRequest.headers), + ); + } + + if (expectedRequest.body) { + const actualBody = JSON.parse(options.body as string); + expect(actualBody).toEqual(expectedRequest.body); + } +} + +async function runLlmTest(testCase: LlmTestCase) { + const { + llm, + methodToTest, + params, + expectedRequest, + mockResponse, + mockStream, + } = testCase; + + const mockFetch = setupMockFetch(mockResponse, mockStream); + setupReadableStreamPolyfill(); + + (llm as any).fetch = mockFetch; + + // Disable OpenAI adapter to use our custom fetch mock + (llm as any).useOpenAIAdapterFor = []; + + await executeLlmMethod(llm, methodToTest, params); + assertFetchCall(mockFetch, expectedRequest); +} + +describe("OpenAI", () => { + afterEach(() => { + vi.clearAllMocks(); + }); + + test("streamChat should send a valid request", async () => { + const openai = new OpenAI({ + apiKey: "test-api-key", + model: "gpt-4", + apiBase: "https://api.openai.com/v1/", + }); + + await runLlmTest({ + llm: openai, + methodToTest: "streamChat", + params: [ + [{ role: "user", content: "hello" }], + new AbortController().signal, + ], + expectedRequest: { + url: "https://api.openai.com/v1/chat/completions", + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + "api-key": "test-api-key", + }, + body: { + model: "gpt-4", + messages: [{ role: "user", content: "hello" }], + stream: true, + max_tokens: 2048, + }, + }, + mockStream: [ + { choices: [{ delta: { content: "Hello" } }] }, + { choices: [{ delta: { content: " world" } }] }, + ], + }); + }); + + test("should handle embeddings", async () => { + const openai = new OpenAI({ + apiKey: "test-api-key", + model: "text-embedding-ada-002", + apiBase: "https://api.openai.com/v1/", + }); + + await runLlmTest({ + llm: openai, + methodToTest: "embed", + params: [["Hello", "World"]], + expectedRequest: { + url: "https://api.openai.com/v1/embeddings", + method: "POST", + headers: { + Authorization: "Bearer test-api-key", + "Content-Type": "application/json", + "api-key": "test-api-key", + }, + body: { + input: ["Hello", "World"], + model: "text-embedding-ada-002", + }, + }, + mockResponse: { + data: [{ embedding: [0.1, 0.2, 0.3] }, { embedding: [0.4, 0.5, 0.6] }], + }, + }); + }); +}); + +// OpenAI-compatible providers +createOpenAISubclassTests(Groq, { + providerName: "groq", + defaultApiBase: "https://api.groq.com/openai/v1/", + modelConversions: { + "mistral-8x7b": "mixtral-8x7b-32768", + "llama3-8b": "llama3-8b-8192", + "llama3-70b": "llama3-70b-8192", + }, + modelConversionContent: "[INST] hello [/INST]", + modelConversionMaxTokens: 4096, +}); + +createOpenAISubclassTests(Fireworks, { + providerName: "fireworks", + defaultApiBase: "https://api.fireworks.ai/inference/v1/", + modelConversions: { + "starcoder-7b": "accounts/fireworks/models/starcoder-7b", + }, + modelConversionContent: + "<|im_start|>user\nhello<|im_end|>\n<|im_start|>assistant\n", +}); + +createOpenAISubclassTests(Together, { + providerName: "together", + defaultApiBase: "https://api.together.xyz/v1/", + modelConversions: { + "codellama-7b": "codellama-7b", + "llama3-8b": "meta-llama/Llama-3-8b-chat-hf", + "mistral-8x7b": "mistralai/Mixtral-8x7B-Instruct-v0.1", + }, + modelConversionContent: "hello", + customStreamCompleteEndpoint: "completions", +}); + +createOpenAISubclassTests(Deepseek, { + providerName: "deepseek", + defaultApiBase: "https://api.deepseek.com/", +}); + +createOpenAISubclassTests(OpenRouter, { + providerName: "openrouter", + defaultApiBase: "https://openrouter.ai/api/v1/", +}); + +createOpenAISubclassTests(xAI, { + providerName: "xAI", + defaultApiBase: "https://api.x.ai/v1/", + modelConversions: { + "grok-beta": "grok-beta", + }, + modelConversionContent: "hello", +}); + +createOpenAISubclassTests(Mistral, { + providerName: "mistral", + defaultApiBase: "https://api.mistral.ai/v1/", + modelConversions: { + "mistral-7b": "mistral-7b", + "mistral-8x7b": "open-mixtral-8x7b", + }, + modelConversionContent: "hello", +}); + +createOpenAISubclassTests(LMStudio, { + providerName: "lmstudio", + defaultApiBase: "http://localhost:1234/v1/", +}); + +createOpenAISubclassTests(Cerebras, { + providerName: "cerebras", + defaultApiBase: "https://api.cerebras.ai/v1/", + modelConversions: { + "llama3.1-8b": "llama3.1-8b", + "llama3.1-70b": "llama3.1-70b", + }, + modelConversionContent: + "<|start_header_id|>user<|end_header_id|>\nhello<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n", +}); + +createOpenAISubclassTests(DeepInfra, { + providerName: "deepinfra", + defaultApiBase: "https://api.deepinfra.com/v1/openai/", + customEmbeddingsUrl: + "https://api.deepinfra.com/v1/inference/text-embedding-ada-002", + customEmbeddingsHeaders: { + Authorization: "bearer test-api-key", + }, + customEmbeddingsBody: { + inputs: ["Hello", "World"], + }, +}); + +createOpenAISubclassTests(Nvidia, { + providerName: "nvidia", + defaultApiBase: "https://integrate.api.nvidia.com/v1/", + customEmbeddingsHeaders: { + Authorization: "Bearer test-api-key", + "Content-Type": "application/json", + }, + customEmbeddingsBody: { + input: ["Hello", "World"], + model: "text-embedding-ada-002", + input_type: "passage", + truncate: "END", + }, +}); + +createOpenAISubclassTests(SambaNova, { + providerName: "sambanova", + defaultApiBase: "https://api.sambanova.ai/v1/", +}); + +createOpenAISubclassTests(Scaleway, { + providerName: "scaleway", + defaultApiBase: "https://api.scaleway.ai/v1/", +}); + +createOpenAISubclassTests(Venice, { + providerName: "venice", + defaultApiBase: "https://api.venice.ai/api/v1/", +}); + +createOpenAISubclassTests(Moonshot, { + providerName: "moonshot", + defaultApiBase: "https://api.moonshot.cn/v1/", +}); + +createOpenAISubclassTests(Novita, { + providerName: "novita", + defaultApiBase: "https://api.novita.ai/v3/openai/", + customStreamCompleteEndpoint: "completions", +}); + +createOpenAISubclassTests(SiliconFlow, { + providerName: "siliconflow", + defaultApiBase: "https://api.siliconflow.cn/v1/", +}); + +createOpenAISubclassTests(Kindo, { + providerName: "kindo", + defaultApiBase: "https://llm.kindo.ai/v1/", +}); + +createOpenAISubclassTests(Azure, { + providerName: "azure", +}); + +createOpenAISubclassTests(Inception, { + providerName: "inception", + defaultApiBase: "https://api.inceptionlabs.ai/v1/", + customBodyOptions: { + temperature: 0.0, + presence_penalty: 1.5, + stop: ["<|endoftext|>"], + }, +}); + +createOpenAISubclassTests(Docker, { + providerName: "docker", + defaultApiBase: "http://localhost:12434/engines/v1/", +}); + +createOpenAISubclassTests(Voyage, { + providerName: "voyage", + defaultApiBase: "https://api.voyageai.com/v1/", +}); + +createOpenAISubclassTests(Vllm, { + providerName: "vllm", + defaultApiBase: "https://api.openai.com/v1/", +}); + +createOpenAISubclassTests(TextGenWebUI, { + providerName: "text-gen-webui", + defaultApiBase: "http://localhost:5000/v1/", +}); + +createOpenAISubclassTests(FunctionNetwork, { + providerName: "function-network", + defaultApiBase: "https://api.function.network/v1/", + customEmbeddingsHeaders: { + Authorization: "Bearer test-api-key", + "Content-Type": "application/json", + }, +}); + +createOpenAISubclassTests(NCompass, { + providerName: "ncompass", + defaultApiBase: "https://api.ncompass.tech/v1/", + customEmbeddingsUrl: "https://api.gcp.ncompass.tech/v1/embeddings", + customEmbeddingsHeaders: { + Authorization: "Bearer test-api-key", + "Content-Type": "application/json", + }, + customEmbeddingsBody: { + input: ["Hello", "World"], + model: "text-embedding-ada-002", + }, +}); + +createOpenAISubclassTests(LlamaStack, { + providerName: "llamastack", + defaultApiBase: "http://localhost:8321/v1/openai/v1/", +}); + +createOpenAISubclassTests(Nebius, { + providerName: "nebius", + defaultApiBase: "https://api.studio.nebius.ai/v1/", +}); + +createOpenAISubclassTests(OVHcloud, { + providerName: "ovhcloud", + defaultApiBase: "https://oai.endpoints.kepler.ai.cloud.ovh.net/v1/", +}); diff --git a/core/llm/llms/OpenAI.vitest.ts b/core/llm/llms/OpenAI.vitest.ts new file mode 100644 index 000000000..d48bbd403 --- /dev/null +++ b/core/llm/llms/OpenAI.vitest.ts @@ -0,0 +1,427 @@ +import { afterEach, describe, expect, test, vi } from "vitest"; +import { ILLM } from "../../index.js"; +import OpenAI from "./OpenAI.js"; + +interface LlmTestCase { + llm: ILLM; + methodToTest: keyof ILLM; + params: any[]; + expectedRequest: { + url: string; + method: string; + headers?: Record; + body?: Record; + }; + mockResponse?: any; + mockStream?: any[]; +} + +function createMockStream(mockStream: any[]) { + const encoder = new TextEncoder(); + return new ReadableStream({ + start(controller) { + for (const chunk of mockStream) { + controller.enqueue( + encoder.encode( + `data: ${ + typeof chunk === "string" ? chunk : JSON.stringify(chunk) + }\n\n`, + ), + ); + } + controller.close(); + }, + }); +} + +function setupMockFetch(mockResponse?: any, mockStream?: any[]) { + const mockFetch = vi.fn(); + + if (mockStream) { + const stream = createMockStream(mockStream); + mockFetch.mockResolvedValue( + new Response(stream, { + headers: { + "Content-Type": "text/event-stream", + }, + }), + ); + } else { + mockFetch.mockResolvedValue( + new Response(JSON.stringify(mockResponse), { + headers: { "Content-Type": "application/json" }, + }), + ); + } + + return mockFetch; +} + +function setupReadableStreamPolyfill() { + // This can be removed if https://github.com/nodejs/undici/issues/2888 is resolved + // @ts-ignore + const originalFrom = ReadableStream.from; + // @ts-ignore + ReadableStream.from = (body) => { + if (body?.source) { + return body; + } + return originalFrom(body); + }; +} + +async function executeLlmMethod( + llm: ILLM, + methodToTest: keyof ILLM, + params: any[], +) { + if (typeof (llm as any)[methodToTest] !== "function") { + throw new Error( + `Method ${String(methodToTest)} does not exist on the LLM instance.`, + ); + } + + const result = await (llm as any)[methodToTest](...params); + if (result?.next) { + for await (const _ of result) { + } + } +} + +function assertFetchCall(mockFetch: any, expectedRequest: any) { + expect(mockFetch).toHaveBeenCalledTimes(1); + const [url, options] = mockFetch.mock.calls[0]; + + expect(url.toString()).toBe(expectedRequest.url); + expect(options.method).toBe(expectedRequest.method); + + if (expectedRequest.headers) { + expect(options.headers).toEqual( + expect.objectContaining(expectedRequest.headers), + ); + } + + if (expectedRequest.body) { + const actualBody = JSON.parse(options.body as string); + expect(actualBody).toEqual(expectedRequest.body); + } +} + +async function runLlmTest(testCase: LlmTestCase) { + const { + llm, + methodToTest, + params, + expectedRequest, + mockResponse, + mockStream, + } = testCase; + + const mockFetch = setupMockFetch(mockResponse, mockStream); + setupReadableStreamPolyfill(); + + (llm as any).fetch = mockFetch; + + // Disable OpenAI adapter to use our custom fetch mock + (llm as any).useOpenAIAdapterFor = []; + + await executeLlmMethod(llm, methodToTest, params); + assertFetchCall(mockFetch, expectedRequest); +} + +describe("OpenAI", () => { + afterEach(() => { + vi.clearAllMocks(); + }); + + test("streamChat should send a valid request", async () => { + const openai = new OpenAI({ + apiKey: "test-api-key", + model: "gpt-4", + apiBase: "https://api.openai.com/v1/", + }); + + await runLlmTest({ + llm: openai, + methodToTest: "streamChat", + params: [ + [{ role: "user", content: "hello" }], + new AbortController().signal, + ], + expectedRequest: { + url: "https://api.openai.com/v1/chat/completions", + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + "api-key": "test-api-key", + }, + body: { + model: "gpt-4", + messages: [{ role: "user", content: "hello" }], + stream: true, + max_tokens: 2048, + }, + }, + mockStream: [ + { choices: [{ delta: { content: "Hello" } }] }, + { choices: [{ delta: { content: " world" } }] }, + ], + }); + }); + + test("chat should send a valid request", async () => { + const openai = new OpenAI({ + apiKey: "test-api-key", + model: "gpt-4", + apiBase: "https://api.openai.com/v1/", + }); + + await runLlmTest({ + llm: openai, + methodToTest: "chat", + params: [ + [{ role: "user", content: "hello" }], + new AbortController().signal, + ], + expectedRequest: { + url: "https://api.openai.com/v1/chat/completions", + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + "api-key": "test-api-key", + }, + body: { + model: "gpt-4", + messages: [{ role: "user", content: "hello" }], + stream: true, + max_tokens: 2048, + }, + }, + mockStream: [ + { choices: [{ delta: { content: "Hello" } }] }, + { choices: [{ delta: { content: " world" } }] }, + ], + }); + }); + + test("streamComplete should send a valid request", async () => { + const openai = new OpenAI({ + apiKey: "test-api-key", + model: "gpt-4", + apiBase: "https://api.openai.com/v1/", + }); + + await runLlmTest({ + llm: openai, + methodToTest: "streamComplete", + params: ["Hello", new AbortController().signal], + expectedRequest: { + url: "https://api.openai.com/v1/chat/completions", + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + "api-key": "test-api-key", + }, + body: { + model: "gpt-4", + messages: [{ role: "user", content: "Hello" }], + stream: true, + max_tokens: 2048, + }, + }, + mockStream: [ + { choices: [{ delta: { content: "Hello" } }] }, + { choices: [{ delta: { content: " world" } }] }, + ], + }); + }); + + test("complete should send a valid request", async () => { + const openai = new OpenAI({ + apiKey: "test-api-key", + model: "gpt-4", + apiBase: "https://api.openai.com/v1/", + }); + + await runLlmTest({ + llm: openai, + methodToTest: "complete", + params: ["Hello", new AbortController().signal], + expectedRequest: { + url: "https://api.openai.com/v1/chat/completions", + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + "api-key": "test-api-key", + }, + body: { + model: "gpt-4", + messages: [{ role: "user", content: "Hello" }], + stream: true, + max_tokens: 2048, + }, + }, + mockStream: [ + { choices: [{ delta: { content: "Hello" } }] }, + { choices: [{ delta: { content: " world" } }] }, + ], + }); + }); + + test("should handle O1 models correctly", async () => { + const openai = new OpenAI({ + apiKey: "test-api-key", + model: "o1-preview", + apiBase: "https://api.openai.com/v1/", + }); + + await runLlmTest({ + llm: openai, + methodToTest: "chat", + params: [ + [{ role: "user", content: "hello" }], + new AbortController().signal, + { maxTokens: 100 }, + ], + expectedRequest: { + url: "https://api.openai.com/v1/chat/completions", + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + "api-key": "test-api-key", + }, + body: { + model: "o1-preview", + messages: [{ role: "user", content: "hello" }], + stream: true, + max_completion_tokens: 100, + }, + }, + mockStream: [{ choices: [{ delta: { content: "Hello world" } }] }], + }); + }); + + test("should handle tools", async () => { + const openai = new OpenAI({ + apiKey: "test-api-key", + model: "gpt-4", + apiBase: "https://api.openai.com/v1/", + }); + + const tools = [ + { + type: "function" as const, + function: { + name: "test_function", + description: "Test function", + parameters: { type: "object", properties: {} }, + }, + }, + ]; + + await runLlmTest({ + llm: openai, + methodToTest: "chat", + params: [ + [{ role: "user", content: "hello" }], + new AbortController().signal, + { tools }, + ], + expectedRequest: { + url: "https://api.openai.com/v1/chat/completions", + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + "api-key": "test-api-key", + }, + body: { + model: "gpt-4", + messages: [{ role: "user", content: "hello" }], + stream: true, + max_tokens: 2048, + tools: [ + { + type: "function", + function: { + name: "test_function", + description: "Test function", + parameters: { type: "object", properties: {} }, + }, + }, + ], + }, + }, + mockStream: [{ choices: [{ delta: { content: "I'll help you" } }] }], + }); + }); + + test("should handle custom max tokens", async () => { + const openai = new OpenAI({ + apiKey: "test-api-key", + model: "gpt-4", + apiBase: "https://api.openai.com/v1/", + }); + + await runLlmTest({ + llm: openai, + methodToTest: "chat", + params: [ + [{ role: "user", content: "hello" }], + new AbortController().signal, + { maxTokens: 500 }, + ], + expectedRequest: { + url: "https://api.openai.com/v1/chat/completions", + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + "api-key": "test-api-key", + }, + body: { + model: "gpt-4", + messages: [{ role: "user", content: "hello" }], + stream: true, + max_tokens: 500, + }, + }, + mockStream: [{ choices: [{ delta: { content: "Hello" } }] }], + }); + }); + + test("should handle embeddings", async () => { + const openai = new OpenAI({ + apiKey: "test-api-key", + model: "text-embedding-ada-002", + apiBase: "https://api.openai.com/v1/", + }); + + await runLlmTest({ + llm: openai, + methodToTest: "embed", + params: [["Hello", "World"]], + expectedRequest: { + url: "https://api.openai.com/v1/embeddings", + method: "POST", + headers: { + Authorization: "Bearer test-api-key", + "Content-Type": "application/json", + "api-key": "test-api-key", + }, + body: { + input: ["Hello", "World"], + model: "text-embedding-ada-002", + }, + }, + mockResponse: { + data: [{ embedding: [0.1, 0.2, 0.3] }, { embedding: [0.4, 0.5, 0.6] }], + }, + }); + }); +}); diff --git a/core/llm/llms/Venice.ts b/core/llm/llms/Venice.ts index 174b4a3de..537338d4e 100644 --- a/core/llm/llms/Venice.ts +++ b/core/llm/llms/Venice.ts @@ -6,7 +6,7 @@ import OpenAI from "./OpenAI"; class Venice extends OpenAI { static providerName = "venice"; static defaultOptions: Partial = { - apiBase: "https://api.venice.ai/api/v1", + apiBase: "https://api.venice.ai/api/v1/", }; protected _convertArgs( diff --git a/core/llm/llms/llmTestHarness.ts b/core/llm/llms/llmTestHarness.ts new file mode 100644 index 000000000..0e309ed53 --- /dev/null +++ b/core/llm/llms/llmTestHarness.ts @@ -0,0 +1,93 @@ +import { expect, jest } from "@jest/globals"; +import { ILLM } from "../../index.js"; + +export interface LlmTestCase { + llm: ILLM; + methodToTest: keyof ILLM; + params: any[]; + expectedRequest: { + url: string; + method: string; + headers?: Record; + body?: Record; + }; + mockResponse?: any; + mockStream?: string[]; // array of string chunks for SSE +} + +export async function runLlmTest(testCase: LlmTestCase) { + const { + llm, + methodToTest, + params, + expectedRequest, + mockResponse, + mockStream, + } = testCase; + + const mockFetch = jest.fn(); + + if (mockStream) { + const encoder = new TextEncoder(); + let streamIndex = 0; + mockFetch.mockResolvedValue( + new Response( + new ReadableStream({ + start(controller) { + mockStream.forEach((chunk) => { + controller.enqueue(new TextEncoder().encode(chunk)); + }); + controller.close(); + }, + }), + { + headers: { + "Content-Type": "text/event-stream", + }, + }, + ), + ); + } else { + mockFetch.mockResolvedValue( + new Response(JSON.stringify(mockResponse), { + headers: { + "Content-Type": "application/json", + }, + }), + ); + } + + // @ts-ignore + llm.fetch = mockFetch; + + // @ts-ignore + if (typeof llm[methodToTest] !== "function") { + throw new Error( + `Method ${String(methodToTest)} does not exist on the LLM instance.`, + ); + } + + // @ts-ignore + const result = await llm[methodToTest](...params); + + if (mockStream) { + // Consume the stream to ensure it's processed correctly + for await (const _ of result) { + } + } + + expect(mockFetch).toHaveBeenCalledTimes(1); + const [url, options] = mockFetch.mock.calls[0] as [string, RequestInit]; + + expect(url.toString()).toBe(expectedRequest.url); + expect(options.method).toBe(expectedRequest.method); + + if (expectedRequest.headers) { + expect(options.headers).toEqual(expectedRequest.headers); + } + + if (expectedRequest.body) { + const actualBody = JSON.parse(options.body as string); + expect(actualBody).toEqual(expectedRequest.body); + } +} diff --git a/core/llm/llms/stubs/ContinueProxy.vitest.ts b/core/llm/llms/stubs/ContinueProxy.vitest.ts new file mode 100644 index 000000000..20bed0ec1 --- /dev/null +++ b/core/llm/llms/stubs/ContinueProxy.vitest.ts @@ -0,0 +1,758 @@ +import { afterEach, describe, expect, test, vi } from "vitest"; +import { ILLM } from "../../../index.js"; +import ContinueProxy from "./ContinueProxy.js"; + +vi.mock("@continuedev/config-yaml", async (importOriginal) => { + const mod = (await importOriginal()) as any; + return { + ...mod, + parseProxyModelName: vi.fn(() => ({ + provider: "test-provider", + model: "test-model", + ownerSlug: "test-owner", + packageSlug: "test-package", + })), + }; +}); + +interface LlmTestCase { + llm: ILLM; + methodToTest: keyof ILLM; + params: any[]; + expectedRequest: { + url: string; + method: string; + headers?: Record; + body?: Record; + }; + mockResponse?: any; + mockStream?: any[]; +} + +function createMockStream(mockStream: any[]) { + const encoder = new TextEncoder(); + return new ReadableStream({ + start(controller) { + for (const chunk of mockStream) { + controller.enqueue( + encoder.encode( + `data: ${ + typeof chunk === "string" ? chunk : JSON.stringify(chunk) + }\n\n`, + ), + ); + } + controller.close(); + }, + }); +} + +function setupMockFetch(mockResponse?: any, mockStream?: any[]) { + const mockFetch = vi.fn(); + + if (mockStream) { + const stream = createMockStream(mockStream); + mockFetch.mockResolvedValue( + new Response(stream, { + headers: { + "Content-Type": "text/event-stream", + }, + }), + ); + } else { + mockFetch.mockResolvedValue( + new Response(JSON.stringify(mockResponse), { + headers: { "Content-Type": "application/json" }, + }), + ); + } + + return mockFetch; +} + +function setupReadableStreamPolyfill() { + // This can be removed if https://github.com/nodejs/undici/issues/2888 is resolved + // @ts-ignore + const originalFrom = ReadableStream.from; + // @ts-ignore + ReadableStream.from = (body) => { + if (body?.source) { + return body; + } + return originalFrom(body); + }; +} + +async function executeLlmMethod( + llm: ILLM, + methodToTest: keyof ILLM, + params: any[], +) { + if (typeof (llm as any)[methodToTest] !== "function") { + throw new Error( + `Method ${String(methodToTest)} does not exist on the LLM instance.`, + ); + } + + const result = await (llm as any)[methodToTest](...params); + if (result?.next) { + for await (const _ of result) { + } + } +} + +function assertFetchCall(mockFetch: any, expectedRequest: any) { + expect(mockFetch).toHaveBeenCalledTimes(1); + const [url, options] = mockFetch.mock.calls[0]; + + expect(url.toString()).toBe(expectedRequest.url); + expect(options.method).toBe(expectedRequest.method); + + if (expectedRequest.headers) { + expect(options.headers).toEqual( + expect.objectContaining(expectedRequest.headers), + ); + } + + if (expectedRequest.body) { + const actualBody = JSON.parse(options.body as string); + expect(actualBody).toEqual(expectedRequest.body); + } +} + +async function runLlmTest(testCase: LlmTestCase) { + const { + llm, + methodToTest, + params, + expectedRequest, + mockResponse, + mockStream, + } = testCase; + + const mockFetch = setupMockFetch(mockResponse, mockStream); + setupReadableStreamPolyfill(); + + (llm as any).fetch = mockFetch; + + await executeLlmMethod(llm, methodToTest, params); + assertFetchCall(mockFetch, expectedRequest); +} + +describe("ContinueProxy", () => { + afterEach(() => { + vi.clearAllMocks(); + }); + + test("rerank should send a valid request", async () => { + const proxy = new ContinueProxy({ + apiKey: "test-api-key", + model: "test-model", + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + }); + + await runLlmTest({ + llm: proxy, + methodToTest: "rerank", + params: [ + "test query", + [{ content: "document1" }, { content: "document2" }], + ], + expectedRequest: { + url: "https://proxy.continue.dev/model-proxy/v1/rerank", + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + }, + body: { + query: "test query", + documents: ["document1", "document2"], + model: "test-model", + continueProperties: { + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + orgScopeId: null, + }, + }, + }, + mockResponse: { + data: [ + { index: 0, relevance_score: 0.9 }, + { index: 1, relevance_score: 0.1 }, + ], + }, + }); + }); + + test("streamChat should send a valid request", async () => { + const proxy = new ContinueProxy({ + apiKey: "test-api-key", + model: "test-model", + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + }); + + await runLlmTest({ + llm: proxy, + methodToTest: "streamChat", + params: [[{ role: "user", content: "hello" }]], + expectedRequest: { + url: "https://proxy.continue.dev/model-proxy/v1/chat/completions", + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + "api-key": "test-api-key", + "x-continue-unique-id": "NOT_UNIQUE", + }, + body: { + model: "test-model", + messages: [{ role: "user", content: "hello" }], + max_tokens: 4096, + stream: true, + continueProperties: { + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + orgScopeId: null, + }, + }, + }, + mockStream: [ + '{"choices": [{"delta": {"content": "response"}}]}', + "[DONE]", + ], + }); + }); + + test("chat should send a valid request", async () => { + const proxy = new ContinueProxy({ + apiKey: "test-api-key", + model: "test-model", + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + }); + + await runLlmTest({ + llm: proxy, + methodToTest: "chat", + params: [[{ role: "user", content: "hello" }]], + expectedRequest: { + url: "https://proxy.continue.dev/model-proxy/v1/chat/completions", + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + "api-key": "test-api-key", + "x-continue-unique-id": "NOT_UNIQUE", + }, + body: { + model: "test-model", + messages: [{ role: "user", content: "hello" }], + max_tokens: 4096, + stream: true, + continueProperties: { + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + orgScopeId: null, + }, + }, + }, + mockStream: [ + '{"choices": [{"delta": {"content": "Hello! How can I help you today?"}}]}', + "[DONE]", + ], + }); + }); + + test("streamComplete should send a valid request", async () => { + const proxy = new ContinueProxy({ + apiKey: "test-api-key", + model: "test-model", + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + }); + + await runLlmTest({ + llm: proxy, + methodToTest: "streamComplete", + params: ["Complete this: Hello"], + expectedRequest: { + url: "https://proxy.continue.dev/model-proxy/v1/chat/completions", + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + "api-key": "test-api-key", + "x-continue-unique-id": "NOT_UNIQUE", + }, + body: { + model: "test-model", + messages: [{ role: "user", content: "Complete this: Hello" }], + max_tokens: 4096, + stream: true, + continueProperties: { + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + orgScopeId: null, + }, + }, + }, + mockStream: [ + '{"choices": [{"delta": {"content": " world!"}}]}', + "[DONE]", + ], + }); + }); + + test("complete should send a valid request", async () => { + const proxy = new ContinueProxy({ + apiKey: "test-api-key", + model: "test-model", + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + }); + + await runLlmTest({ + llm: proxy, + methodToTest: "complete", + params: ["Complete this: Hello"], + expectedRequest: { + url: "https://proxy.continue.dev/model-proxy/v1/chat/completions", + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + "api-key": "test-api-key", + "x-continue-unique-id": "NOT_UNIQUE", + }, + body: { + model: "test-model", + messages: [{ role: "user", content: "Complete this: Hello" }], + max_tokens: 4096, + stream: true, + continueProperties: { + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + orgScopeId: null, + }, + }, + }, + mockStream: [ + '{"choices": [{"delta": {"content": " world!"}}]}', + "[DONE]", + ], + }); + }); + + test("streamFim should send a valid request", async () => { + const proxy = new ContinueProxy({ + apiKey: "test-api-key", + model: "test-model", + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + }); + + await runLlmTest({ + llm: proxy, + methodToTest: "streamFim", + params: ["function test() {\n ", "\n}"], + expectedRequest: { + url: "https://proxy.continue.dev/model-proxy/v1/fim/completions", + method: "POST", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "x-api-key": "test-api-key", + Authorization: "Bearer test-api-key", + }, + body: { + model: "test-model", + prompt: "function test() {\n ", + suffix: "\n}", + max_tokens: 4096, + stream: true, + continueProperties: { + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + orgScopeId: null, + }, + }, + }, + mockStream: [ + '{"choices": [{"delta": {"content": "console.log("Hello");"}}]}', + "[DONE]", + ], + }); + }); + + test("embed should send a valid request", async () => { + const proxy = new ContinueProxy({ + apiKey: "test-api-key", + model: "test-model", + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + }); + + await runLlmTest({ + llm: proxy, + methodToTest: "embed", + params: [["text to embed", "another text"]], + expectedRequest: { + url: "https://proxy.continue.dev/model-proxy/v1/embeddings", + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + "api-key": "test-api-key", + }, + body: { + input: ["text to embed", "another text"], + model: "test-model", + continueProperties: { + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + orgScopeId: null, + }, + }, + }, + mockResponse: { + data: [{ embedding: [0.1, 0.2, 0.3] }, { embedding: [0.4, 0.5, 0.6] }], + }, + }); + }); + + test("listModels should send a valid request", async () => { + const proxy = new ContinueProxy({ + apiKey: "test-api-key", + model: "test-model", + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + }); + + await runLlmTest({ + llm: proxy, + methodToTest: "listModels", + params: [], + expectedRequest: { + url: "https://proxy.continue.dev/model-proxy/v1/models", + method: "GET", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + "api-key": "test-api-key", + }, + }, + mockResponse: { + data: [{ id: "gpt-4" }, { id: "gpt-3.5-turbo" }], + }, + }); + }); + + describe("Different provider configurations", () => { + test("should handle on-prem proxy URL", async () => { + const proxy = new ContinueProxy({ + apiKey: "test-api-key", + model: "test-model", + onPremProxyUrl: "https://my-proxy.company.com/", + }); + + await runLlmTest({ + llm: proxy, + methodToTest: "rerank", + params: [ + "test query", + [{ content: "document1" }, { content: "document2" }], + ], + expectedRequest: { + url: "https://my-proxy.company.com/model-proxy/v1/rerank", + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + }, + body: { + query: "test query", + documents: ["document1", "document2"], + model: "test-model", + continueProperties: { + apiBase: undefined, + apiKeyLocation: undefined, + env: undefined, + envSecretLocations: undefined, + orgScopeId: null, + }, + }, + }, + mockResponse: { + data: [ + { index: 0, relevance_score: 0.9 }, + { index: 1, relevance_score: 0.1 }, + ], + }, + }); + }); + + test("should handle environment variables", async () => { + const proxy = new ContinueProxy({ + apiKey: "test-api-key", + model: "test-model", + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + env: { + CUSTOM_VAR: "custom-value", + ANOTHER_VAR: "another-value", + }, + }); + + await runLlmTest({ + llm: proxy, + methodToTest: "streamChat", + params: [[{ role: "user", content: "hello" }]], + expectedRequest: { + url: "https://proxy.continue.dev/model-proxy/v1/chat/completions", + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + "api-key": "test-api-key", + "x-continue-unique-id": "NOT_UNIQUE", + }, + body: { + model: "test-model", + messages: [{ role: "user", content: "hello" }], + max_tokens: 4096, + stream: true, + continueProperties: { + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + env: { + CUSTOM_VAR: "custom-value", + ANOTHER_VAR: "another-value", + }, + orgScopeId: null, + }, + }, + }, + mockStream: [ + '{"choices": [{"delta": {"content": "response"}}]}', + "[DONE]", + ], + }); + }); + + test("should handle API key location", async () => { + const proxy = new ContinueProxy({ + apiKey: "test-api-key", + model: "test-model", + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + apiKeyLocation: "env:OPENAI_API_KEY", + }); + + await runLlmTest({ + llm: proxy, + methodToTest: "embed", + params: [["test text"]], + expectedRequest: { + url: "https://proxy.continue.dev/model-proxy/v1/embeddings", + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + "api-key": "test-api-key", + }, + body: { + input: ["test text"], + model: "test-model", + continueProperties: { + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + apiKeyLocation: "env:OPENAI_API_KEY", + env: undefined, + envSecretLocations: undefined, + orgScopeId: null, + }, + }, + }, + mockResponse: { + data: [{ embedding: [0.1, 0.2, 0.3] }], + }, + }); + }); + + test("should handle env secret locations", async () => { + const proxy = new ContinueProxy({ + apiKey: "test-api-key", + model: "test-model", + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + envSecretLocations: { + AZURE_API_KEY: "env:AZURE_API_KEY", + AZURE_ENDPOINT: "env:AZURE_ENDPOINT", + }, + }); + + await runLlmTest({ + llm: proxy, + methodToTest: "streamFim", + params: ["const x = ", ";"], + expectedRequest: { + url: "https://proxy.continue.dev/model-proxy/v1/fim/completions", + method: "POST", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "x-api-key": "test-api-key", + Authorization: "Bearer test-api-key", + }, + body: { + model: "test-model", + prompt: "const x = ", + suffix: ";", + max_tokens: 4096, + stream: true, + continueProperties: { + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + envSecretLocations: { + AZURE_API_KEY: "env:AZURE_API_KEY", + AZURE_ENDPOINT: "env:AZURE_ENDPOINT", + }, + orgScopeId: null, + }, + }, + }, + mockStream: ['{"choices": [{"delta": {"content": "42"}}]}', "[DONE]"], + }); + }); + + test("should handle organization scope ID", async () => { + const proxy = new ContinueProxy({ + apiKey: "test-api-key", + model: "test-model", + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + orgScopeId: "org_12345", + }); + + await runLlmTest({ + llm: proxy, + methodToTest: "listModels", + params: [], + expectedRequest: { + url: "https://proxy.continue.dev/model-proxy/v1/models", + method: "GET", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + "api-key": "test-api-key", + }, + }, + mockResponse: { + data: [{ id: "gpt-4" }], + }, + }); + }); + }); + + describe("Edge cases", () => { + test("should handle empty chunks array for rerank", async () => { + const proxy = new ContinueProxy({ + apiKey: "test-api-key", + model: "test-model", + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + }); + + await runLlmTest({ + llm: proxy, + methodToTest: "rerank", + params: ["test query", []], + expectedRequest: { + url: "https://proxy.continue.dev/model-proxy/v1/rerank", + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + }, + body: { + query: "test query", + documents: [], + model: "test-model", + continueProperties: { + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + apiKeyLocation: undefined, + env: undefined, + envSecretLocations: undefined, + orgScopeId: null, + }, + }, + }, + mockResponse: { + data: [], + }, + }); + }); + + test("should handle complex chat messages", async () => { + const proxy = new ContinueProxy({ + apiKey: "test-api-key", + model: "test-model", + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + }); + + const complexMessages = [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: "Hello!" }, + { role: "assistant", content: "Hi there! How can I help you today?" }, + { role: "user", content: "What's the weather like?" }, + ]; + + await runLlmTest({ + llm: proxy, + methodToTest: "streamChat", + params: [complexMessages], + expectedRequest: { + url: "https://proxy.continue.dev/model-proxy/v1/chat/completions", + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + "api-key": "test-api-key", + "x-continue-unique-id": "NOT_UNIQUE", + }, + body: { + model: "test-model", + messages: complexMessages, + max_tokens: 4096, + stream: true, + continueProperties: { + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + orgScopeId: null, + }, + }, + }, + mockStream: [ + '{"choices": [{"delta": {"content": "I don\'t have access to current weather data."}}]}', + "[DONE]", + ], + }); + }); + + test("should handle single embedding chunk", async () => { + const proxy = new ContinueProxy({ + apiKey: "test-api-key", + model: "test-model", + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + }); + + await runLlmTest({ + llm: proxy, + methodToTest: "embed", + params: [["single text"]], + expectedRequest: { + url: "https://proxy.continue.dev/model-proxy/v1/embeddings", + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + "api-key": "test-api-key", + }, + body: { + input: ["single text"], + model: "test-model", + continueProperties: { + apiBase: "https://proxy.continue.dev/model-proxy/v1/", + apiKeyLocation: undefined, + env: undefined, + envSecretLocations: undefined, + orgScopeId: null, + }, + }, + }, + mockResponse: { + data: [{ embedding: [0.1, 0.2, 0.3] }], + }, + }); + }); + }); +}); diff --git a/core/llm/llms/test-utils/openai-test-utils.ts b/core/llm/llms/test-utils/openai-test-utils.ts new file mode 100644 index 000000000..4c9c3fbdb --- /dev/null +++ b/core/llm/llms/test-utils/openai-test-utils.ts @@ -0,0 +1,375 @@ +import { afterEach, describe, expect, test, vi } from "vitest"; +import { ILLM } from "../../../index.js"; +import OpenAI from "../OpenAI.js"; + +interface LlmTestCase { + llm: ILLM; + methodToTest: keyof ILLM; + params: any[]; + expectedRequest: { + url: string; + method: string; + headers?: Record; + body?: Record; + }; + mockResponse?: any; + mockStream?: any[]; +} + +function createMockStream(mockStream: any[]) { + const encoder = new TextEncoder(); + return new ReadableStream({ + start(controller) { + for (const chunk of mockStream) { + controller.enqueue( + encoder.encode( + `data: ${ + typeof chunk === "string" ? chunk : JSON.stringify(chunk) + }\n\n`, + ), + ); + } + controller.close(); + }, + }); +} + +function setupMockFetch(mockResponse?: any, mockStream?: any[]) { + const mockFetch = vi.fn(); + + if (mockStream) { + const stream = createMockStream(mockStream); + mockFetch.mockResolvedValue( + new Response(stream, { + headers: { + "Content-Type": "text/event-stream", + }, + }), + ); + } else { + mockFetch.mockResolvedValue( + new Response(JSON.stringify(mockResponse), { + headers: { "Content-Type": "application/json" }, + }), + ); + } + + return mockFetch; +} + +function setupReadableStreamPolyfill() { + // This can be removed if https://github.com/nodejs/undici/issues/2888 is resolved + // @ts-ignore + const originalFrom = ReadableStream.from; + // @ts-ignore + ReadableStream.from = (body) => { + if (body?.source) { + return body; + } + return originalFrom(body); + }; +} + +async function executeLlmMethod( + llm: ILLM, + methodToTest: keyof ILLM, + params: any[], +) { + if (typeof (llm as any)[methodToTest] !== "function") { + throw new Error( + `Method ${String(methodToTest)} does not exist on the LLM instance.`, + ); + } + + const result = await (llm as any)[methodToTest](...params); + if (result?.next) { + for await (const _ of result) { + } + } +} + +function assertFetchCall(mockFetch: any, expectedRequest: any) { + expect(mockFetch).toHaveBeenCalledTimes(1); + const [url, options] = mockFetch.mock.calls[0]; + + expect(url.toString()).toBe(expectedRequest.url); + expect(options.method).toBe(expectedRequest.method); + + if (expectedRequest.headers) { + expect(options.headers).toEqual( + expect.objectContaining(expectedRequest.headers), + ); + } + + if (expectedRequest.body) { + const actualBody = JSON.parse(options.body as string); + expect(actualBody).toEqual(expectedRequest.body); + } +} + +async function runLlmTest(testCase: LlmTestCase) { + const { + llm, + methodToTest, + params, + expectedRequest, + mockResponse, + mockStream, + } = testCase; + + const mockFetch = setupMockFetch(mockResponse, mockStream); + setupReadableStreamPolyfill(); + + (llm as any).fetch = mockFetch; + + // Disable OpenAI adapter to use our custom fetch mock + (llm as any).useOpenAIAdapterFor = []; + + await executeLlmMethod(llm, methodToTest, params); + assertFetchCall(mockFetch, expectedRequest); +} + +export interface OpenAISubclassConfig { + providerName: string; + defaultApiBase?: string; + modelConversions?: { [key: string]: string }; + customOptions?: any; + modelConversionContent?: string; + modelConversionMaxTokens?: number; + customStreamCompleteEndpoint?: string; + customEmbeddingsUrl?: string; + customEmbeddingsHeaders?: { [key: string]: string }; + customEmbeddingsBody?: any; + customBodyOptions?: any; +} + +function getExpectedUrl( + config: OpenAISubclassConfig, + endpoint: string, + model: string = "gpt-4", +) { + let baseUrl = config.defaultApiBase || "https://api.openai.com/v1/"; + if (config.providerName === "azure") { + return `${baseUrl}openai/deployments/${model}/${endpoint}?api-version=2023-07-01-preview`; + } else if (config.providerName === "ncompass") { + return `${baseUrl}${endpoint}`; + } + return `${baseUrl}${endpoint}`; +} + +export const createOpenAISubclassTests = ( + ProviderClass: typeof OpenAI, + config: OpenAISubclassConfig, +) => { + describe(config.providerName, () => { + afterEach(() => { + vi.clearAllMocks(); + }); + + test("should have correct provider name", () => { + expect(ProviderClass.providerName).toBe(config.providerName); + }); + + if (config.defaultApiBase) { + test("should have correct default API base", () => { + expect(ProviderClass.defaultOptions?.apiBase).toBe( + config.defaultApiBase, + ); + }); + } + + test("streamChat should send a valid request", async () => { + const provider = new ProviderClass({ + apiKey: "test-api-key", + model: "gpt-4", + apiBase: config.defaultApiBase || "https://api.openai.com/v1/", + }); + + await runLlmTest({ + llm: provider, + methodToTest: "streamChat", + params: [ + [{ role: "user", content: "hello" }], + new AbortController().signal, + ], + expectedRequest: { + url: getExpectedUrl(config, "chat/completions"), + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + "api-key": "test-api-key", + }, + body: { + model: "gpt-4", + messages: [{ role: "user", content: "hello" }], + stream: true, + max_tokens: 2048, + ...config.customBodyOptions, + }, + }, + mockStream: [ + { choices: [{ delta: { content: "Hello" } }] }, + { choices: [{ delta: { content: " world" } }] }, + ], + }); + }); + + test("chat should send a valid request", async () => { + const provider = new ProviderClass({ + apiKey: "test-api-key", + model: "gpt-4", + apiBase: config.defaultApiBase || "https://api.openai.com/v1/", + }); + + await runLlmTest({ + llm: provider, + methodToTest: "chat", + params: [ + [{ role: "user", content: "hello" }], + new AbortController().signal, + ], + expectedRequest: { + url: getExpectedUrl(config, "chat/completions"), + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + "api-key": "test-api-key", + }, + body: { + model: "gpt-4", + messages: [{ role: "user", content: "hello" }], + stream: true, + max_tokens: 2048, + ...config.customBodyOptions, + }, + }, + mockStream: [ + { choices: [{ delta: { content: "Hello" } }] }, + { choices: [{ delta: { content: " world" } }] }, + ], + }); + }); + + test("streamComplete should send a valid request", async () => { + const provider = new ProviderClass({ + apiKey: "test-api-key", + model: "gpt-4", + apiBase: config.defaultApiBase || "https://api.openai.com/v1/", + }); + + await runLlmTest({ + llm: provider, + methodToTest: "streamComplete", + params: ["Hello", new AbortController().signal], + expectedRequest: { + url: getExpectedUrl( + config, + config.customStreamCompleteEndpoint || "chat/completions", + ), + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + "api-key": "test-api-key", + }, + body: + config.customStreamCompleteEndpoint === "completions" + ? { + model: "gpt-4", + prompt: "Hello", + stream: true, + max_tokens: 2048, + ...config.customBodyOptions, + } + : { + model: "gpt-4", + messages: [{ role: "user", content: "Hello" }], + stream: true, + max_tokens: 2048, + ...config.customBodyOptions, + }, + }, + mockStream: [ + { choices: [{ delta: { content: "Hello" } }] }, + { choices: [{ delta: { content: " world" } }] }, + ], + }); + }); + + test("complete should send a valid request", async () => { + const provider = new ProviderClass({ + apiKey: "test-api-key", + model: "gpt-4", + apiBase: config.defaultApiBase || "https://api.openai.com/v1/", + }); + + await runLlmTest({ + llm: provider, + methodToTest: "complete", + params: ["Hello", new AbortController().signal], + expectedRequest: { + url: getExpectedUrl(config, "chat/completions"), + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer test-api-key", + "api-key": "test-api-key", + }, + body: { + model: "gpt-4", + messages: [{ role: "user", content: "Hello" }], + stream: true, + max_tokens: 2048, + ...config.customBodyOptions, + }, + }, + mockStream: [ + { choices: [{ delta: { content: "Hello" } }] }, + { choices: [{ delta: { content: " world" } }] }, + ], + }); + }); + + test("should handle embeddings", async () => { + const provider = new ProviderClass({ + apiKey: "test-api-key", + model: "text-embedding-ada-002", + apiBase: config.defaultApiBase || "https://api.openai.com/v1/", + }); + + // Skip test if provider doesn't support embeddings (e.g., ncompass with undefined endpoint) + if (config.providerName === "ncompass" && !config.customEmbeddingsUrl) { + return; + } + + await runLlmTest({ + llm: provider, + methodToTest: "embed", + params: [["Hello", "World"]], + expectedRequest: { + url: + config.customEmbeddingsUrl || + `${config.defaultApiBase || "https://api.openai.com/v1/"}embeddings`, + method: "POST", + headers: config.customEmbeddingsHeaders || { + Authorization: "Bearer test-api-key", + "Content-Type": "application/json", + "api-key": "test-api-key", + }, + body: config.customEmbeddingsBody || { + input: ["Hello", "World"], + model: "text-embedding-ada-002", + }, + }, + mockResponse: { + data: [ + { embedding: [0.1, 0.2, 0.3] }, + { embedding: [0.4, 0.5, 0.6] }, + ], + }, + }); + }); + }); +}; diff --git a/core/package-lock.json b/core/package-lock.json index 7f869503f..d2afa4b76 100644 --- a/core/package-lock.json +++ b/core/package-lock.json @@ -190,7 +190,8 @@ "@types/json-schema": "^7.0.15", "cross-env": "^7.0.3", "ts-jest": "^29.2.3", - "ts-node": "^10.9.2" + "ts-node": "^10.9.2", + "vitest": "^1.6.1" } }, "node_modules/@75lb/deep-merge": { diff --git a/package-lock.json b/package-lock.json index b4289d422..0a2538840 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "continue", + "name": "wt-openai-adapters-tests", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/packages/openai-adapters/package-lock.json b/packages/openai-adapters/package-lock.json index a713b68d4..760a0de91 100644 --- a/packages/openai-adapters/package-lock.json +++ b/packages/openai-adapters/package-lock.json @@ -27,7 +27,8 @@ "@types/json-schema": "^7.0.15", "cross-env": "^7.0.3", "ts-jest": "^29.2.3", - "ts-node": "^10.9.2" + "ts-node": "^10.9.2", + "vitest": "^1.6.1" } }, "node_modules/@ampproject/remapping": { @@ -1452,6 +1453,397 @@ "@jridgewell/sourcemap-codec": "^1.4.10" } }, + "node_modules/@esbuild/aix-ppc64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.21.5.tgz", + "integrity": "sha512-1SDgH6ZSPTlggy1yI6+Dbkiz8xzpHJEVAlF/AM1tHPLsf5STom9rwtjE4hKAF20FfXXNTFqEYXyJNWh1GiZedQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "aix" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/android-arm": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.21.5.tgz", + "integrity": "sha512-vCPvzSjpPHEi1siZdlvAlsPxXl7WbOVUBBAowWug4rJHb68Ox8KualB+1ocNvT5fjv6wpkX6o/iEpbDrf68zcg==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/android-arm64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.21.5.tgz", + "integrity": "sha512-c0uX9VAUBQ7dTDCjq+wdyGLowMdtR/GoC2U5IYk/7D1H1JYC0qseD7+11iMP2mRLN9RcCMRcjC4YMclCzGwS/A==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/android-x64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.21.5.tgz", + "integrity": "sha512-D7aPRUUNHRBwHxzxRvp856rjUHRFW1SdQATKXH2hqA0kAZb1hKmi02OpYRacl0TxIGz/ZmXWlbZgjwWYaCakTA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/darwin-arm64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.21.5.tgz", + "integrity": "sha512-DwqXqZyuk5AiWWf3UfLiRDJ5EDd49zg6O9wclZ7kUMv2WRFr4HKjXp/5t8JZ11QbQfUS6/cRCKGwYhtNAY88kQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/darwin-x64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.21.5.tgz", + "integrity": "sha512-se/JjF8NlmKVG4kNIuyWMV/22ZaerB+qaSi5MdrXtd6R08kvs2qCN4C09miupktDitvh8jRFflwGFBQcxZRjbw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/freebsd-arm64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.21.5.tgz", + "integrity": "sha512-5JcRxxRDUJLX8JXp/wcBCy3pENnCgBR9bN6JsY4OmhfUtIHe3ZW0mawA7+RDAcMLrMIZaf03NlQiX9DGyB8h4g==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/freebsd-x64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.21.5.tgz", + "integrity": "sha512-J95kNBj1zkbMXtHVH29bBriQygMXqoVQOQYA+ISs0/2l3T9/kj42ow2mpqerRBxDJnmkUDCaQT/dfNXWX/ZZCQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-arm": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.21.5.tgz", + "integrity": "sha512-bPb5AHZtbeNGjCKVZ9UGqGwo8EUu4cLq68E95A53KlxAPRmUyYv2D6F0uUI65XisGOL1hBP5mTronbgo+0bFcA==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-arm64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.21.5.tgz", + "integrity": "sha512-ibKvmyYzKsBeX8d8I7MH/TMfWDXBF3db4qM6sy+7re0YXya+K1cem3on9XgdT2EQGMu4hQyZhan7TeQ8XkGp4Q==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-ia32": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.21.5.tgz", + "integrity": "sha512-YvjXDqLRqPDl2dvRODYmmhz4rPeVKYvppfGYKSNGdyZkA01046pLWyRKKI3ax8fbJoK5QbxblURkwK/MWY18Tg==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-loong64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.21.5.tgz", + "integrity": "sha512-uHf1BmMG8qEvzdrzAqg2SIG/02+4/DHB6a9Kbya0XDvwDEKCoC8ZRWI5JJvNdUjtciBGFQ5PuBlpEOXQj+JQSg==", + "cpu": [ + "loong64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-mips64el": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.21.5.tgz", + "integrity": "sha512-IajOmO+KJK23bj52dFSNCMsz1QP1DqM6cwLUv3W1QwyxkyIWecfafnI555fvSGqEKwjMXVLokcV5ygHW5b3Jbg==", + "cpu": [ + "mips64el" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-ppc64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.21.5.tgz", + "integrity": "sha512-1hHV/Z4OEfMwpLO8rp7CvlhBDnjsC3CttJXIhBi+5Aj5r+MBvy4egg7wCbe//hSsT+RvDAG7s81tAvpL2XAE4w==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-riscv64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.21.5.tgz", + "integrity": "sha512-2HdXDMd9GMgTGrPWnJzP2ALSokE/0O5HhTUvWIbD3YdjME8JwvSCnNGBnTThKGEB91OZhzrJ4qIIxk/SBmyDDA==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-s390x": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.21.5.tgz", + "integrity": "sha512-zus5sxzqBJD3eXxwvjN1yQkRepANgxE9lgOW2qLnmr8ikMTphkjgXu1HR01K4FJg8h1kEEDAqDcZQtbrRnB41A==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-x64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.21.5.tgz", + "integrity": "sha512-1rYdTpyv03iycF1+BhzrzQJCdOuAOtaqHTWJZCWvijKD2N5Xu0TtVC8/+1faWqcP9iBCWOmjmhoH94dH82BxPQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/netbsd-x64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.21.5.tgz", + "integrity": "sha512-Woi2MXzXjMULccIwMnLciyZH4nCIMpWQAs049KEeMvOcNADVxo0UBIQPfSmxB3CWKedngg7sWZdLvLczpe0tLg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/openbsd-x64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.21.5.tgz", + "integrity": "sha512-HLNNw99xsvx12lFBUwoT8EVCsSvRNDVxNpjZ7bPn947b8gJPzeHWyNVhFsaerc0n3TsbOINvRP2byTZ5LKezow==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/sunos-x64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.21.5.tgz", + "integrity": "sha512-6+gjmFpfy0BHU5Tpptkuh8+uw3mnrvgs+dSPQXQOv3ekbordwnzTVEb4qnIvQcYXq6gzkyTnoZ9dZG+D4garKg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/win32-arm64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.21.5.tgz", + "integrity": "sha512-Z0gOTd75VvXqyq7nsl93zwahcTROgqvuAcYDUr+vOv8uHhNSKROyU961kgtCD1e95IqPKSQKH7tBTslnS3tA8A==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/win32-ia32": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.21.5.tgz", + "integrity": "sha512-SWXFF1CL2RVNMaVs+BBClwtfZSvDgtL//G/smwAc5oVK/UPu2Gu9tIaRgFmYFFKrmg3SyAjSrElf0TiJ1v8fYA==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/win32-x64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.21.5.tgz", + "integrity": "sha512-tQd/1efJuzPC6rCFwEvLtci/xNFcTZknmXs98FYDfGE4wP9ClFV98nyKrzJKVPMhdDnjzLhdUyMX4PsQAPjwIw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=12" + } + }, "node_modules/@istanbuljs/load-nyc-config": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz", @@ -1819,6 +2211,286 @@ "@jridgewell/sourcemap-codec": "^1.4.14" } }, + "node_modules/@rollup/rollup-android-arm-eabi": { + "version": "4.45.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.45.1.tgz", + "integrity": "sha512-NEySIFvMY0ZQO+utJkgoMiCAjMrGvnbDLHvcmlA33UXJpYBCvlBEbMMtV837uCkS+plG2umfhn0T5mMAxGrlRA==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ] + }, + "node_modules/@rollup/rollup-android-arm64": { + "version": "4.45.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm64/-/rollup-android-arm64-4.45.1.tgz", + "integrity": "sha512-ujQ+sMXJkg4LRJaYreaVx7Z/VMgBBd89wGS4qMrdtfUFZ+TSY5Rs9asgjitLwzeIbhwdEhyj29zhst3L1lKsRQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ] + }, + "node_modules/@rollup/rollup-darwin-arm64": { + "version": "4.45.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-arm64/-/rollup-darwin-arm64-4.45.1.tgz", + "integrity": "sha512-FSncqHvqTm3lC6Y13xncsdOYfxGSLnP+73k815EfNmpewPs+EyM49haPS105Rh4aF5mJKywk9X0ogzLXZzN9lA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ] + }, + "node_modules/@rollup/rollup-darwin-x64": { + "version": "4.45.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-x64/-/rollup-darwin-x64-4.45.1.tgz", + "integrity": "sha512-2/vVn/husP5XI7Fsf/RlhDaQJ7x9zjvC81anIVbr4b/f0xtSmXQTFcGIQ/B1cXIYM6h2nAhJkdMHTnD7OtQ9Og==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ] + }, + "node_modules/@rollup/rollup-freebsd-arm64": { + "version": "4.45.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-freebsd-arm64/-/rollup-freebsd-arm64-4.45.1.tgz", + "integrity": "sha512-4g1kaDxQItZsrkVTdYQ0bxu4ZIQ32cotoQbmsAnW1jAE4XCMbcBPDirX5fyUzdhVCKgPcrwWuucI8yrVRBw2+g==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ] + }, + "node_modules/@rollup/rollup-freebsd-x64": { + "version": "4.45.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-freebsd-x64/-/rollup-freebsd-x64-4.45.1.tgz", + "integrity": "sha512-L/6JsfiL74i3uK1Ti2ZFSNsp5NMiM4/kbbGEcOCps99aZx3g8SJMO1/9Y0n/qKlWZfn6sScf98lEOUe2mBvW9A==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ] + }, + "node_modules/@rollup/rollup-linux-arm-gnueabihf": { + "version": "4.45.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-gnueabihf/-/rollup-linux-arm-gnueabihf-4.45.1.tgz", + "integrity": "sha512-RkdOTu2jK7brlu+ZwjMIZfdV2sSYHK2qR08FUWcIoqJC2eywHbXr0L8T/pONFwkGukQqERDheaGTeedG+rra6Q==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-arm-musleabihf": { + "version": "4.45.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-musleabihf/-/rollup-linux-arm-musleabihf-4.45.1.tgz", + "integrity": "sha512-3kJ8pgfBt6CIIr1o+HQA7OZ9mp/zDk3ctekGl9qn/pRBgrRgfwiffaUmqioUGN9hv0OHv2gxmvdKOkARCtRb8Q==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-arm64-gnu": { + "version": "4.45.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-gnu/-/rollup-linux-arm64-gnu-4.45.1.tgz", + "integrity": "sha512-k3dOKCfIVixWjG7OXTCOmDfJj3vbdhN0QYEqB+OuGArOChek22hn7Uy5A/gTDNAcCy5v2YcXRJ/Qcnm4/ma1xw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-arm64-musl": { + "version": "4.45.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-musl/-/rollup-linux-arm64-musl-4.45.1.tgz", + "integrity": "sha512-PmI1vxQetnM58ZmDFl9/Uk2lpBBby6B6rF4muJc65uZbxCs0EA7hhKCk2PKlmZKuyVSHAyIw3+/SiuMLxKxWog==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-loongarch64-gnu": { + "version": "4.45.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-loongarch64-gnu/-/rollup-linux-loongarch64-gnu-4.45.1.tgz", + "integrity": "sha512-9UmI0VzGmNJ28ibHW2GpE2nF0PBQqsyiS4kcJ5vK+wuwGnV5RlqdczVocDSUfGX/Na7/XINRVoUgJyFIgipoRg==", + "cpu": [ + "loong64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-powerpc64le-gnu": { + "version": "4.45.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-powerpc64le-gnu/-/rollup-linux-powerpc64le-gnu-4.45.1.tgz", + "integrity": "sha512-7nR2KY8oEOUTD3pBAxIBBbZr0U7U+R9HDTPNy+5nVVHDXI4ikYniH1oxQz9VoB5PbBU1CZuDGHkLJkd3zLMWsg==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-riscv64-gnu": { + "version": "4.45.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-riscv64-gnu/-/rollup-linux-riscv64-gnu-4.45.1.tgz", + "integrity": "sha512-nlcl3jgUultKROfZijKjRQLUu9Ma0PeNv/VFHkZiKbXTBQXhpytS8CIj5/NfBeECZtY2FJQubm6ltIxm/ftxpw==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-riscv64-musl": { + "version": "4.45.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-riscv64-musl/-/rollup-linux-riscv64-musl-4.45.1.tgz", + "integrity": "sha512-HJV65KLS51rW0VY6rvZkiieiBnurSzpzore1bMKAhunQiECPuxsROvyeaot/tcK3A3aGnI+qTHqisrpSgQrpgA==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-s390x-gnu": { + "version": "4.45.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-s390x-gnu/-/rollup-linux-s390x-gnu-4.45.1.tgz", + "integrity": "sha512-NITBOCv3Qqc6hhwFt7jLV78VEO/il4YcBzoMGGNxznLgRQf43VQDae0aAzKiBeEPIxnDrACiMgbqjuihx08OOw==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-x64-gnu": { + "version": "4.45.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-gnu/-/rollup-linux-x64-gnu-4.45.1.tgz", + "integrity": "sha512-+E/lYl6qu1zqgPEnTrs4WysQtvc/Sh4fC2nByfFExqgYrqkKWp1tWIbe+ELhixnenSpBbLXNi6vbEEJ8M7fiHw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-x64-musl": { + "version": "4.45.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-musl/-/rollup-linux-x64-musl-4.45.1.tgz", + "integrity": "sha512-a6WIAp89p3kpNoYStITT9RbTbTnqarU7D8N8F2CV+4Cl9fwCOZraLVuVFvlpsW0SbIiYtEnhCZBPLoNdRkjQFw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-win32-arm64-msvc": { + "version": "4.45.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-arm64-msvc/-/rollup-win32-arm64-msvc-4.45.1.tgz", + "integrity": "sha512-T5Bi/NS3fQiJeYdGvRpTAP5P02kqSOpqiopwhj0uaXB6nzs5JVi2XMJb18JUSKhCOX8+UE1UKQufyD6Or48dJg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ] + }, + "node_modules/@rollup/rollup-win32-ia32-msvc": { + "version": "4.45.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-ia32-msvc/-/rollup-win32-ia32-msvc-4.45.1.tgz", + "integrity": "sha512-lxV2Pako3ujjuUe9jiU3/s7KSrDfH6IgTSQOnDWr9aJ92YsFd7EurmClK0ly/t8dzMkDtd04g60WX6yl0sGfdw==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ] + }, + "node_modules/@rollup/rollup-win32-x64-msvc": { + "version": "4.45.1", + "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-x64-msvc/-/rollup-win32-x64-msvc-4.45.1.tgz", + "integrity": "sha512-M/fKi4sasCdM8i0aWJjCSFm2qEnYRR8AMLG2kxp6wD13+tMGA4Z1tVAuHkNRjud5SW2EM3naLuK35w9twvf6aA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ] + }, "node_modules/@sinclair/typebox": { "version": "0.27.8", "resolved": "https://registry.npmjs.org/@sinclair/typebox/-/typebox-0.27.8.tgz", @@ -2564,6 +3236,13 @@ "@babel/types": "^7.20.7" } }, + "node_modules/@types/estree": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.8.tgz", + "integrity": "sha512-dWHzHa2WqEXI/O1E9OjrocMTKJl2mSrEolh1Iomrv6U+JuNwaHXsXx9bLu5gG7BUWFIN0skIQJQ/L1rIex4X6w==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/follow-redirects": { "version": "1.14.4", "resolved": "https://registry.npmjs.org/@types/follow-redirects/-/follow-redirects-1.14.4.tgz", @@ -2667,6 +3346,109 @@ "integrity": "sha512-I4q9QU9MQv4oEOz4tAHJtNz1cwuLxn2F3xcc2iV5WdqLPpUnj30aUuxt1mAxYTG+oe8CZMV/+6rU4S4gRDzqtQ==", "dev": true }, + "node_modules/@vitest/expect": { + "version": "1.6.1", + "resolved": "https://registry.npmjs.org/@vitest/expect/-/expect-1.6.1.tgz", + "integrity": "sha512-jXL+9+ZNIJKruofqXuuTClf44eSpcHlgj3CiuNihUF3Ioujtmc0zIa3UJOW5RjDK1YLBJZnWBlPuqhYycLioog==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/spy": "1.6.1", + "@vitest/utils": "1.6.1", + "chai": "^4.3.10" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/runner": { + "version": "1.6.1", + "resolved": "https://registry.npmjs.org/@vitest/runner/-/runner-1.6.1.tgz", + "integrity": "sha512-3nSnYXkVkf3mXFfE7vVyPmi3Sazhb/2cfZGGs0JRzFsPFvAMBEcrweV1V1GsrstdXeKCTXlJbvnQwGWgEIHmOA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/utils": "1.6.1", + "p-limit": "^5.0.0", + "pathe": "^1.1.1" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/runner/node_modules/p-limit": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-5.0.0.tgz", + "integrity": "sha512-/Eaoq+QyLSiXQ4lyYV23f14mZRQcXnxfHrN0vCai+ak9G0pp9iEQukIIZq5NccEvwRB8PUnZT0KsOoDCINS1qQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "yocto-queue": "^1.0.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/@vitest/runner/node_modules/yocto-queue": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-1.2.1.tgz", + "integrity": "sha512-AyeEbWOu/TAXdxlV9wmGcR0+yh2j3vYPGOECcIj2S7MkrLyC7ne+oye2BKTItt0ii2PHk4cDy+95+LshzbXnGg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12.20" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/@vitest/snapshot": { + "version": "1.6.1", + "resolved": "https://registry.npmjs.org/@vitest/snapshot/-/snapshot-1.6.1.tgz", + "integrity": "sha512-WvidQuWAzU2p95u8GAKlRMqMyN1yOJkGHnx3M1PL9Raf7AQ1kwLKg04ADlCa3+OXUZE7BceOhVZiuWAbzCKcUQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "magic-string": "^0.30.5", + "pathe": "^1.1.1", + "pretty-format": "^29.7.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/spy": { + "version": "1.6.1", + "resolved": "https://registry.npmjs.org/@vitest/spy/-/spy-1.6.1.tgz", + "integrity": "sha512-MGcMmpGkZebsMZhbQKkAf9CX5zGvjkBTqf8Zx3ApYWXr3wG+QvEu2eXWfnIIWYSJExIp4V9FCKDEeygzkYrXMw==", + "dev": true, + "license": "MIT", + "dependencies": { + "tinyspy": "^2.2.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/utils": { + "version": "1.6.1", + "resolved": "https://registry.npmjs.org/@vitest/utils/-/utils-1.6.1.tgz", + "integrity": "sha512-jOrrUvXM4Av9ZWiG1EajNto0u96kWAhJ1LmPmJhXXQx/32MecEKd10pOLYgS2BQx1TgkGhloPU1ArDW2vvaY6g==", + "dev": true, + "license": "MIT", + "dependencies": { + "diff-sequences": "^29.6.3", + "estree-walker": "^3.0.3", + "loupe": "^2.3.7", + "pretty-format": "^29.7.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, "node_modules/abort-controller": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz", @@ -2679,10 +3461,11 @@ } }, "node_modules/acorn": { - "version": "8.12.1", - "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.12.1.tgz", - "integrity": "sha512-tcpGyI9zbizT9JbV6oYE477V6mTlXvvi0T0G3SNIYE2apm/G5huBa1+K89VGeovbg+jycCrfhl3ADxErOuO6Jg==", + "version": "8.15.0", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.15.0.tgz", + "integrity": "sha512-NZyJarBfL7nWwIq+FDL6Zp/yHEhePMNnnJ0y3qfieCrmNvYct8uvtiV41UvlSe6apAfk0fY1FbWx+NwfmpvtTg==", "dev": true, + "license": "MIT", "bin": { "acorn": "bin/acorn" }, @@ -2795,6 +3578,16 @@ "sprintf-js": "~1.0.2" } }, + "node_modules/assertion-error": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz", + "integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==", + "dev": true, + "license": "MIT", + "engines": { + "node": "*" + } + }, "node_modules/async": { "version": "3.2.5", "resolved": "https://registry.npmjs.org/async/-/async-3.2.5.tgz", @@ -3015,6 +3808,16 @@ "dev": true, "peer": true }, + "node_modules/cac": { + "version": "6.7.14", + "resolved": "https://registry.npmjs.org/cac/-/cac-6.7.14.tgz", + "integrity": "sha512-b6Ilus+c3RrdDk+JhLKUAQfzzgLEPy6wcXqS7f/xe1EETvsDP6GORG7SFuOs6cID5YkqchW/LXZbX5bc8j7ZcQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8" + } + }, "node_modules/callsites": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/callsites/-/callsites-3.1.0.tgz", @@ -3056,6 +3859,35 @@ ], "peer": true }, + "node_modules/chai": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/chai/-/chai-4.5.0.tgz", + "integrity": "sha512-RITGBfijLkBddZvnn8jdqoTypxvqbOLYQkGGxXzeFjVHvudaPw0HNFD9x928/eUwYWd2dPCugVqspGALTZZQKw==", + "dev": true, + "license": "MIT", + "dependencies": { + "assertion-error": "^1.1.0", + "check-error": "^1.0.3", + "deep-eql": "^4.1.3", + "get-func-name": "^2.0.2", + "loupe": "^2.3.6", + "pathval": "^1.1.1", + "type-detect": "^4.1.0" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/chai/node_modules/type-detect": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.1.0.tgz", + "integrity": "sha512-Acylog8/luQ8L7il+geoSxhEkazvkslg7PSNKOX59mbB9cOveP5aq9h74Y7YU8yDpJwetzQQrfIwtf4Wp4LKcw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=4" + } + }, "node_modules/chalk": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", @@ -3082,6 +3914,19 @@ "node": ">=10" } }, + "node_modules/check-error": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/check-error/-/check-error-1.0.3.tgz", + "integrity": "sha512-iKEoDYaRmd1mxM90a2OEfWhjsjPpYPuQ+lMYsoxB126+t8fw7ySEO48nmDg5COTjxDI65/Y2OWpeEHk3ZOe8zg==", + "dev": true, + "license": "MIT", + "dependencies": { + "get-func-name": "^2.0.2" + }, + "engines": { + "node": "*" + } + }, "node_modules/ci-info": { "version": "3.9.0", "resolved": "https://registry.npmjs.org/ci-info/-/ci-info-3.9.0.tgz", @@ -3172,6 +4017,13 @@ "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==", "dev": true }, + "node_modules/confbox": { + "version": "0.1.8", + "resolved": "https://registry.npmjs.org/confbox/-/confbox-0.1.8.tgz", + "integrity": "sha512-RMtmw0iFkeR4YV+fUOSucriAQNb9g8zFR52MWCtl+cCZOFRNL6zeB395vPzFhEjjn4fMxXudmELnl/KF/WrK6w==", + "dev": true, + "license": "MIT" + }, "node_modules/convert-source-map": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-2.0.0.tgz", @@ -3283,6 +4135,19 @@ } } }, + "node_modules/deep-eql": { + "version": "4.1.4", + "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-4.1.4.tgz", + "integrity": "sha512-SUwdGfqdKOwxCPeVYjwSyRpJ7Z+fhpwIAtmCUdZIWZ/YP5R9WAsyuSgpLVDi9bjWoN2LXHNss/dk3urXtdQxGg==", + "dev": true, + "license": "MIT", + "dependencies": { + "type-detect": "^4.0.0" + }, + "engines": { + "node": ">=6" + } + }, "node_modules/deepmerge": { "version": "4.3.1", "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.3.1.tgz", @@ -3392,6 +4257,45 @@ "is-arrayish": "^0.2.1" } }, + "node_modules/esbuild": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.21.5.tgz", + "integrity": "sha512-mg3OPMV4hXywwpoDxu3Qda5xCKQi+vCTZq8S9J/EpkhB2HzKXq4SNFZE3+NK93JYxc8VMSep+lOUSC/RVKaBqw==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "bin": { + "esbuild": "bin/esbuild" + }, + "engines": { + "node": ">=12" + }, + "optionalDependencies": { + "@esbuild/aix-ppc64": "0.21.5", + "@esbuild/android-arm": "0.21.5", + "@esbuild/android-arm64": "0.21.5", + "@esbuild/android-x64": "0.21.5", + "@esbuild/darwin-arm64": "0.21.5", + "@esbuild/darwin-x64": "0.21.5", + "@esbuild/freebsd-arm64": "0.21.5", + "@esbuild/freebsd-x64": "0.21.5", + "@esbuild/linux-arm": "0.21.5", + "@esbuild/linux-arm64": "0.21.5", + "@esbuild/linux-ia32": "0.21.5", + "@esbuild/linux-loong64": "0.21.5", + "@esbuild/linux-mips64el": "0.21.5", + "@esbuild/linux-ppc64": "0.21.5", + "@esbuild/linux-riscv64": "0.21.5", + "@esbuild/linux-s390x": "0.21.5", + "@esbuild/linux-x64": "0.21.5", + "@esbuild/netbsd-x64": "0.21.5", + "@esbuild/openbsd-x64": "0.21.5", + "@esbuild/sunos-x64": "0.21.5", + "@esbuild/win32-arm64": "0.21.5", + "@esbuild/win32-ia32": "0.21.5", + "@esbuild/win32-x64": "0.21.5" + } + }, "node_modules/escalade": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.1.2.tgz", @@ -3425,6 +4329,16 @@ "node": ">=4" } }, + "node_modules/estree-walker": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-3.0.3.tgz", + "integrity": "sha512-7RUKfXgSMMkzt6ZuXmqapOurLGPPfgj6l9uRZ7lRGolvk0y2yocc35LdcxKC5PQZdn2DMqioAQ2NoWcrTKmm6g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/estree": "^1.0.0" + } + }, "node_modules/event-target-shim": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz", @@ -3684,7 +4598,6 @@ "os": [ "darwin" ], - "peer": true, "engines": { "node": "^8.16.0 || ^10.6.0 || >=11.0.0" } @@ -3719,6 +4632,16 @@ "node": "6.* || 8.* || >= 10.*" } }, + "node_modules/get-func-name": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/get-func-name/-/get-func-name-2.0.2.tgz", + "integrity": "sha512-8vXOvuE167CtIc3OyItco7N/dpRtBbYOsPsXCz7X/PMnlGjYjSGuZJgM1Y7mmew7BKf9BqvLX2tnOVy1BBUsxQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": "*" + } + }, "node_modules/get-package-type": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/get-package-type/-/get-package-type-0.1.0.tgz", @@ -4748,6 +5671,23 @@ "dev": true, "peer": true }, + "node_modules/local-pkg": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/local-pkg/-/local-pkg-0.5.1.tgz", + "integrity": "sha512-9rrA30MRRP3gBD3HTGnC6cDFpaE1kVDWxWgqWJUN0RvDNAo+Nz/9GxB+nHOH0ifbVFy0hSA1V6vFDvnx54lTEQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "mlly": "^1.7.3", + "pkg-types": "^1.2.1" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/sponsors/antfu" + } + }, "node_modules/locate-path": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-5.0.0.tgz", @@ -4767,6 +5707,16 @@ "integrity": "sha512-t7j+NzmgnQzTAYXcsHYLgimltOV1MXHtlOWf6GjL9Kj8GK5FInw5JotxvbOs+IvV1/Dzo04/fCGfLVs7aXb4Ag==", "dev": true }, + "node_modules/loupe": { + "version": "2.3.7", + "resolved": "https://registry.npmjs.org/loupe/-/loupe-2.3.7.tgz", + "integrity": "sha512-zSMINGVYkdpYSOBmLi0D1Uo7JU9nVdQKrHxC8eYlV+9YKK9WePqAlL7lSlorG/U2Fw1w0hTBmaa/jrQ3UbPHtA==", + "dev": true, + "license": "MIT", + "dependencies": { + "get-func-name": "^2.0.1" + } + }, "node_modules/lru-cache": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz", @@ -4777,6 +5727,16 @@ "yallist": "^3.0.2" } }, + "node_modules/magic-string": { + "version": "0.30.17", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.17.tgz", + "integrity": "sha512-sNPKHvyjVf7gyjwS4xGTaW/mCnF8wnjtifKBEhxfZ7E/S8tQ0rssrwGNn6q8JH/ohItJfSQp9mBtQYuTlH5QnA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/sourcemap-codec": "^1.5.0" + } + }, "node_modules/make-dir": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-4.0.0.tgz", @@ -4826,8 +5786,7 @@ "version": "2.0.0", "resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz", "integrity": "sha512-abv/qOcuPfk3URPfDzmZU1LKmuw8kT+0nIHvKrKgFrwifol/doWcdA4ZqsWQ8ENrFKkd67Mfpo/LovbIUsbt3w==", - "dev": true, - "peer": true + "dev": true }, "node_modules/micromatch": { "version": "4.0.7", @@ -4883,11 +5842,50 @@ "node": "*" } }, + "node_modules/mlly": { + "version": "1.7.4", + "resolved": "https://registry.npmjs.org/mlly/-/mlly-1.7.4.tgz", + "integrity": "sha512-qmdSIPC4bDJXgZTCR7XosJiNKySV7O215tsPtDN9iEO/7q/76b/ijtgRu/+epFXSJhijtTCCGp3DWS549P3xKw==", + "dev": true, + "license": "MIT", + "dependencies": { + "acorn": "^8.14.0", + "pathe": "^2.0.1", + "pkg-types": "^1.3.0", + "ufo": "^1.5.4" + } + }, + "node_modules/mlly/node_modules/pathe": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/pathe/-/pathe-2.0.3.tgz", + "integrity": "sha512-WUjGcAqP1gQacoQe+OBJsFA7Ld4DyXuUIjZ5cc75cLHvJ7dtNsTugphxIADwspS+AraAUePCKrSVtPLFj/F88w==", + "dev": true, + "license": "MIT" + }, "node_modules/ms": { "version": "2.1.3", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" }, + "node_modules/nanoid": { + "version": "3.3.11", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.11.tgz", + "integrity": "sha512-N8SpfPUnUp1bK+PMYW8qSWdl9U+wwNWI4QKxOYDy9JAro3WMX7p2OeVRF9v+347pnakNevPmiHhNmZ2HbFA76w==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "bin": { + "nanoid": "bin/nanoid.cjs" + }, + "engines": { + "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1" + } + }, "node_modules/natural-compare": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz", @@ -5151,11 +6149,29 @@ "dev": true, "peer": true }, + "node_modules/pathe": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/pathe/-/pathe-1.1.2.tgz", + "integrity": "sha512-whLdWMYL2TwI08hn8/ZqAbrVemu0LNaNNJZX73O6qaIdCTfXutsLhMkjdENX0qhsQ9uIimo4/aQOmXkoon2nDQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/pathval": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/pathval/-/pathval-1.1.1.tgz", + "integrity": "sha512-Dp6zGqpTdETdR63lehJYPeIOqpiNBNtc7BpWSLrOje7UaIsE5aY92r/AunQA7rsXvet3lrJ3JnZX29UPTKXyKQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": "*" + } + }, "node_modules/picocolors": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.0.1.tgz", - "integrity": "sha512-anP1Z8qwhkbmu7MFP5iTt+wQKXgwzf7zTyGlcdzabySa9vd0Xt392U0rVmz9poOaBj0uHJKyyo9/upk0HrEQew==", - "dev": true + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz", + "integrity": "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==", + "dev": true, + "license": "ISC" }, "node_modules/picomatch": { "version": "2.3.1", @@ -5192,6 +6208,54 @@ "node": ">=8" } }, + "node_modules/pkg-types": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/pkg-types/-/pkg-types-1.3.1.tgz", + "integrity": "sha512-/Jm5M4RvtBFVkKWRu2BLUTNP8/M2a+UwuAX+ae4770q1qVGtfjG+WTCupoZixokjmHiry8uI+dlY8KXYV5HVVQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "confbox": "^0.1.8", + "mlly": "^1.7.4", + "pathe": "^2.0.1" + } + }, + "node_modules/pkg-types/node_modules/pathe": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/pathe/-/pathe-2.0.3.tgz", + "integrity": "sha512-WUjGcAqP1gQacoQe+OBJsFA7Ld4DyXuUIjZ5cc75cLHvJ7dtNsTugphxIADwspS+AraAUePCKrSVtPLFj/F88w==", + "dev": true, + "license": "MIT" + }, + "node_modules/postcss": { + "version": "8.5.6", + "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.6.tgz", + "integrity": "sha512-3Ybi1tAuwAP9s0r1UQ2J4n5Y0G05bJkpUIO0/bI9MhwmD70S5aTWbXGBwxHrelT+XM1k6dM0pk+SwNkpTRN7Pg==", + "dev": true, + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/postcss/" + }, + { + "type": "tidelift", + "url": "https://tidelift.com/funding/github/npm/postcss" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "dependencies": { + "nanoid": "^3.3.11", + "picocolors": "^1.1.1", + "source-map-js": "^1.2.1" + }, + "engines": { + "node": "^10 || ^12 || >=14" + } + }, "node_modules/pretty-format": { "version": "29.7.0", "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-29.7.0.tgz", @@ -5316,6 +6380,46 @@ "node": ">=10" } }, + "node_modules/rollup": { + "version": "4.45.1", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-4.45.1.tgz", + "integrity": "sha512-4iya7Jb76fVpQyLoiVpzUrsjQ12r3dM7fIVz+4NwoYvZOShknRmiv+iu9CClZml5ZLGb0XMcYLutK6w9tgxHDw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/estree": "1.0.8" + }, + "bin": { + "rollup": "dist/bin/rollup" + }, + "engines": { + "node": ">=18.0.0", + "npm": ">=8.0.0" + }, + "optionalDependencies": { + "@rollup/rollup-android-arm-eabi": "4.45.1", + "@rollup/rollup-android-arm64": "4.45.1", + "@rollup/rollup-darwin-arm64": "4.45.1", + "@rollup/rollup-darwin-x64": "4.45.1", + "@rollup/rollup-freebsd-arm64": "4.45.1", + "@rollup/rollup-freebsd-x64": "4.45.1", + "@rollup/rollup-linux-arm-gnueabihf": "4.45.1", + "@rollup/rollup-linux-arm-musleabihf": "4.45.1", + "@rollup/rollup-linux-arm64-gnu": "4.45.1", + "@rollup/rollup-linux-arm64-musl": "4.45.1", + "@rollup/rollup-linux-loongarch64-gnu": "4.45.1", + "@rollup/rollup-linux-powerpc64le-gnu": "4.45.1", + "@rollup/rollup-linux-riscv64-gnu": "4.45.1", + "@rollup/rollup-linux-riscv64-musl": "4.45.1", + "@rollup/rollup-linux-s390x-gnu": "4.45.1", + "@rollup/rollup-linux-x64-gnu": "4.45.1", + "@rollup/rollup-linux-x64-musl": "4.45.1", + "@rollup/rollup-win32-arm64-msvc": "4.45.1", + "@rollup/rollup-win32-ia32-msvc": "4.45.1", + "@rollup/rollup-win32-x64-msvc": "4.45.1", + "fsevents": "~2.3.2" + } + }, "node_modules/semver": { "version": "6.3.1", "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", @@ -5347,6 +6451,13 @@ "node": ">=8" } }, + "node_modules/siginfo": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/siginfo/-/siginfo-2.0.0.tgz", + "integrity": "sha512-ybx0WO1/8bSBLEWXZvEd7gMW3Sn3JFlW3TvX1nREbDLRNQNaeNN8WK0meBwPdAaOI7TtRRRJn/Es1zhrrCHu7g==", + "dev": true, + "license": "ISC" + }, "node_modules/signal-exit": { "version": "3.0.7", "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.7.tgz", @@ -5380,6 +6491,16 @@ "node": ">=0.10.0" } }, + "node_modules/source-map-js": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.2.1.tgz", + "integrity": "sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==", + "dev": true, + "license": "BSD-3-Clause", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/source-map-support": { "version": "0.5.13", "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.13.tgz", @@ -5410,6 +6531,20 @@ "node": ">=10" } }, + "node_modules/stackback": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/stackback/-/stackback-0.0.2.tgz", + "integrity": "sha512-1XMJE5fQo1jGH6Y/7ebnwPOBEkIEnT4QF32d5R1+VXdXveM0IBMJt8zfaxX1P3QhVwrYe+576+jkANtSS2mBbw==", + "dev": true, + "license": "MIT" + }, + "node_modules/std-env": { + "version": "3.9.0", + "resolved": "https://registry.npmjs.org/std-env/-/std-env-3.9.0.tgz", + "integrity": "sha512-UGvjygr6F6tpH7o2qyqR6QYpwraIjKSdtzyBdyytFOHmPZY917kwdwLG0RbOjWOnKmnm3PeHjaoLLMie7kPLQw==", + "dev": true, + "license": "MIT" + }, "node_modules/string-length": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/string-length/-/string-length-4.0.2.tgz", @@ -5485,6 +6620,26 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/strip-literal": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/strip-literal/-/strip-literal-2.1.1.tgz", + "integrity": "sha512-631UJ6O00eNGfMiWG78ck80dfBab8X6IVFB51jZK5Icd7XAs60Z5y7QdSd/wGIklnWvRbUNloVzhOKKmutxQ6Q==", + "dev": true, + "license": "MIT", + "dependencies": { + "js-tokens": "^9.0.1" + }, + "funding": { + "url": "https://github.com/sponsors/antfu" + } + }, + "node_modules/strip-literal/node_modules/js-tokens": { + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-9.0.1.tgz", + "integrity": "sha512-mxa9E9ITFOt0ban3j6L5MpjwegGz6lBQmM1IJkWeBZGcMxto50+eWdjC/52xDbS2vy0k7vIMK0Fe2wfL9OQSpQ==", + "dev": true, + "license": "MIT" + }, "node_modules/strnum": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/strnum/-/strnum-1.1.2.tgz", @@ -5537,6 +6692,33 @@ "node": ">=8" } }, + "node_modules/tinybench": { + "version": "2.9.0", + "resolved": "https://registry.npmjs.org/tinybench/-/tinybench-2.9.0.tgz", + "integrity": "sha512-0+DUvqWMValLmha6lr4kD8iAMK1HzV0/aKnCtWb9v9641TnP/MFb7Pc2bxoxQjTXAErryXVgUOfv2YqNllqGeg==", + "dev": true, + "license": "MIT" + }, + "node_modules/tinypool": { + "version": "0.8.4", + "resolved": "https://registry.npmjs.org/tinypool/-/tinypool-0.8.4.tgz", + "integrity": "sha512-i11VH5gS6IFeLY3gMBQ00/MmLncVP7JLXOw1vlgkytLmJK7QnEr7NXf0LBdxfmNPAeyetukOk0bOYrJrFGjYJQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/tinyspy": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/tinyspy/-/tinyspy-2.2.1.tgz", + "integrity": "sha512-KYad6Vy5VDWV4GH3fjpseMQ/XU2BhIYP7Vzd0LG44qRWm/Yt2WCOTicFdvmgo6gWaqooMQCawTtILVQJupKu7A==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=14.0.0" + } + }, "node_modules/tmpl": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/tmpl/-/tmpl-1.0.5.tgz", @@ -5685,7 +6867,6 @@ "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz", "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==", "dev": true, - "peer": true, "engines": { "node": ">=4" } @@ -5717,6 +6898,13 @@ "node": ">=14.17" } }, + "node_modules/ufo": { + "version": "1.6.1", + "resolved": "https://registry.npmjs.org/ufo/-/ufo-1.6.1.tgz", + "integrity": "sha512-9a4/uxlTWJ4+a5i0ooc1rU7C7YOw3wT+UGqdeNNHWnOF9qcMBgLRS+4IYUqbczewFx4mLEig6gawh7X6mFlEkA==", + "dev": true, + "license": "MIT" + }, "node_modules/undici-types": { "version": "5.26.5", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", @@ -5786,6 +6974,299 @@ "node": ">=10.12.0" } }, + "node_modules/vite": { + "version": "5.4.19", + "resolved": "https://registry.npmjs.org/vite/-/vite-5.4.19.tgz", + "integrity": "sha512-qO3aKv3HoQC8QKiNSTuUM1l9o/XX3+c+VTgLHbJWHZGeTPVAg2XwazI9UWzoxjIJCGCV2zU60uqMzjeLZuULqA==", + "dev": true, + "license": "MIT", + "dependencies": { + "esbuild": "^0.21.3", + "postcss": "^8.4.43", + "rollup": "^4.20.0" + }, + "bin": { + "vite": "bin/vite.js" + }, + "engines": { + "node": "^18.0.0 || >=20.0.0" + }, + "funding": { + "url": "https://github.com/vitejs/vite?sponsor=1" + }, + "optionalDependencies": { + "fsevents": "~2.3.3" + }, + "peerDependencies": { + "@types/node": "^18.0.0 || >=20.0.0", + "less": "*", + "lightningcss": "^1.21.0", + "sass": "*", + "sass-embedded": "*", + "stylus": "*", + "sugarss": "*", + "terser": "^5.4.0" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + }, + "less": { + "optional": true + }, + "lightningcss": { + "optional": true + }, + "sass": { + "optional": true + }, + "sass-embedded": { + "optional": true + }, + "stylus": { + "optional": true + }, + "sugarss": { + "optional": true + }, + "terser": { + "optional": true + } + } + }, + "node_modules/vite-node": { + "version": "1.6.1", + "resolved": "https://registry.npmjs.org/vite-node/-/vite-node-1.6.1.tgz", + "integrity": "sha512-YAXkfvGtuTzwWbDSACdJSg4A4DZiAqckWe90Zapc/sEX3XvHcw1NdurM/6od8J207tSDqNbSsgdCacBgvJKFuA==", + "dev": true, + "license": "MIT", + "dependencies": { + "cac": "^6.7.14", + "debug": "^4.3.4", + "pathe": "^1.1.1", + "picocolors": "^1.0.0", + "vite": "^5.0.0" + }, + "bin": { + "vite-node": "vite-node.mjs" + }, + "engines": { + "node": "^18.0.0 || >=20.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/vitest": { + "version": "1.6.1", + "resolved": "https://registry.npmjs.org/vitest/-/vitest-1.6.1.tgz", + "integrity": "sha512-Ljb1cnSJSivGN0LqXd/zmDbWEM0RNNg2t1QW/XUhYl/qPqyu7CsqeWtqQXHVaJsecLPuDoak2oJcZN2QoRIOag==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/expect": "1.6.1", + "@vitest/runner": "1.6.1", + "@vitest/snapshot": "1.6.1", + "@vitest/spy": "1.6.1", + "@vitest/utils": "1.6.1", + "acorn-walk": "^8.3.2", + "chai": "^4.3.10", + "debug": "^4.3.4", + "execa": "^8.0.1", + "local-pkg": "^0.5.0", + "magic-string": "^0.30.5", + "pathe": "^1.1.1", + "picocolors": "^1.0.0", + "std-env": "^3.5.0", + "strip-literal": "^2.0.0", + "tinybench": "^2.5.1", + "tinypool": "^0.8.3", + "vite": "^5.0.0", + "vite-node": "1.6.1", + "why-is-node-running": "^2.2.2" + }, + "bin": { + "vitest": "vitest.mjs" + }, + "engines": { + "node": "^18.0.0 || >=20.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "@edge-runtime/vm": "*", + "@types/node": "^18.0.0 || >=20.0.0", + "@vitest/browser": "1.6.1", + "@vitest/ui": "1.6.1", + "happy-dom": "*", + "jsdom": "*" + }, + "peerDependenciesMeta": { + "@edge-runtime/vm": { + "optional": true + }, + "@types/node": { + "optional": true + }, + "@vitest/browser": { + "optional": true + }, + "@vitest/ui": { + "optional": true + }, + "happy-dom": { + "optional": true + }, + "jsdom": { + "optional": true + } + } + }, + "node_modules/vitest/node_modules/execa": { + "version": "8.0.1", + "resolved": "https://registry.npmjs.org/execa/-/execa-8.0.1.tgz", + "integrity": "sha512-VyhnebXciFV2DESc+p6B+y0LjSm0krU4OgJN44qFAhBY0TJ+1V61tYD2+wHusZ6F9n5K+vl8k0sTy7PEfV4qpg==", + "dev": true, + "license": "MIT", + "dependencies": { + "cross-spawn": "^7.0.3", + "get-stream": "^8.0.1", + "human-signals": "^5.0.0", + "is-stream": "^3.0.0", + "merge-stream": "^2.0.0", + "npm-run-path": "^5.1.0", + "onetime": "^6.0.0", + "signal-exit": "^4.1.0", + "strip-final-newline": "^3.0.0" + }, + "engines": { + "node": ">=16.17" + }, + "funding": { + "url": "https://github.com/sindresorhus/execa?sponsor=1" + } + }, + "node_modules/vitest/node_modules/get-stream": { + "version": "8.0.1", + "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-8.0.1.tgz", + "integrity": "sha512-VaUJspBffn/LMCJVoMvSAdmscJyS1auj5Zulnn5UoYcY531UWmdwhRWkcGKnGU93m5HSXP9LP2usOryrBtQowA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=16" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/vitest/node_modules/human-signals": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/human-signals/-/human-signals-5.0.0.tgz", + "integrity": "sha512-AXcZb6vzzrFAUE61HnN4mpLqd/cSIwNQjtNWR0euPm6y0iqx3G4gOXaIDdtdDwZmhwe82LA6+zinmW4UBWVePQ==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": ">=16.17.0" + } + }, + "node_modules/vitest/node_modules/is-stream": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-3.0.0.tgz", + "integrity": "sha512-LnQR4bZ9IADDRSkvpqMGvt/tEJWclzklNgSw48V5EAaAeDd6qGvN8ei6k5p0tvxSR171VmGyHuTiAOfxAbr8kA==", + "dev": true, + "license": "MIT", + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/vitest/node_modules/mimic-fn": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-4.0.0.tgz", + "integrity": "sha512-vqiC06CuhBTUdZH+RYl8sFrL096vA45Ok5ISO6sE/Mr1jRbGH4Csnhi8f3wKVl7x8mO4Au7Ir9D3Oyv1VYMFJw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/vitest/node_modules/npm-run-path": { + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/npm-run-path/-/npm-run-path-5.3.0.tgz", + "integrity": "sha512-ppwTtiJZq0O/ai0z7yfudtBpWIoxM8yE6nHi1X47eFR2EWORqfbu6CnPlNsjeN683eT0qG6H/Pyf9fCcvjnnnQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "path-key": "^4.0.0" + }, + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/vitest/node_modules/onetime": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/onetime/-/onetime-6.0.0.tgz", + "integrity": "sha512-1FlR+gjXK7X+AsAHso35MnyN5KqGwJRi/31ft6x0M194ht7S+rWAvd7PHss9xSKMzE0asv1pyIHaJYq+BbacAQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "mimic-fn": "^4.0.0" + }, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/vitest/node_modules/path-key": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/path-key/-/path-key-4.0.0.tgz", + "integrity": "sha512-haREypq7xkM7ErfgIyA0z+Bj4AGKlMSdlQE2jvJo6huWD1EdkKYV+G/T4nq0YEF2vgTT8kqMFKo1uHn950r4SQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/vitest/node_modules/signal-exit": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-4.1.0.tgz", + "integrity": "sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw==", + "dev": true, + "license": "ISC", + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/vitest/node_modules/strip-final-newline": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/strip-final-newline/-/strip-final-newline-3.0.0.tgz", + "integrity": "sha512-dOESqjYr96iWYylGObzd39EuNTa5VJxyvVAEm5Jnh7KGo75V43Hk1odPQkNDyXNmUR6k+gEiDVXnjB8HJ3crXw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/walker": { "version": "1.0.8", "resolved": "https://registry.npmjs.org/walker/-/walker-1.0.8.tgz", @@ -5833,6 +7314,23 @@ "node": ">= 8" } }, + "node_modules/why-is-node-running": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/why-is-node-running/-/why-is-node-running-2.3.0.tgz", + "integrity": "sha512-hUrmaWBdVDcxvYqnyh09zunKzROWjbZTiNy8dBEjkS7ehEDQibXJ7XvlmtbwuTclUiIyN+CyXQD4Vmko8fNm8w==", + "dev": true, + "license": "MIT", + "dependencies": { + "siginfo": "^2.0.0", + "stackback": "0.0.2" + }, + "bin": { + "why-is-node-running": "cli.js" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/wrap-ansi": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz", diff --git a/packages/openai-adapters/package.json b/packages/openai-adapters/package.json index 538676f48..ae2f4be7c 100644 --- a/packages/openai-adapters/package.json +++ b/packages/openai-adapters/package.json @@ -6,7 +6,7 @@ "types": "dist/index.d.ts", "type": "module", "scripts": { - "test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest", + "test": "vitest", "build": "tsc" }, "author": "Nate Sesti and Ty Dunn", @@ -30,6 +30,7 @@ "@types/json-schema": "^7.0.15", "cross-env": "^7.0.3", "ts-jest": "^29.2.3", - "ts-node": "^10.9.2" + "ts-node": "^10.9.2", + "vitest": "^1.6.1" } } diff --git a/packages/openai-adapters/src/test/adapter-test-utils.ts b/packages/openai-adapters/src/test/adapter-test-utils.ts new file mode 100644 index 000000000..a1e7a0787 --- /dev/null +++ b/packages/openai-adapters/src/test/adapter-test-utils.ts @@ -0,0 +1,321 @@ +import { afterEach, describe, expect, test, vi } from "vitest"; +import { BaseLlmApi } from "../apis/base.js"; +import { LLMConfig } from "../types.js"; +import { constructLlmApi } from "../index.js"; + +interface AdapterTestCase { + config: LLMConfig; + methodToTest: keyof BaseLlmApi; + params: any[]; + expectedRequest: { + url: string; + method: string; + headers?: Record; + body?: Record; + }; + mockResponse?: any; + mockStream?: any[]; +} + +function createMockStream(mockStream: any[]) { + const encoder = new TextEncoder(); + return new ReadableStream({ + start(controller) { + for (const chunk of mockStream) { + controller.enqueue( + encoder.encode( + `data: ${ + typeof chunk === "string" ? chunk : JSON.stringify(chunk) + }\n\n`, + ), + ); + } + controller.close(); + }, + }); +} + +function setupMockFetch(mockResponse?: any, mockStream?: any[]) { + const mockFetch = vi.fn(); + + if (mockStream) { + const stream = createMockStream(mockStream); + mockFetch.mockResolvedValue( + new Response(stream, { + headers: { + "Content-Type": "text/event-stream", + }, + }), + ); + } else { + mockFetch.mockResolvedValue( + new Response(JSON.stringify(mockResponse), { + headers: { "Content-Type": "application/json" }, + }), + ); + } + + return mockFetch; +} + +function setupReadableStreamPolyfill() { + // This can be removed if https://github.com/nodejs/undici/issues/2888 is resolved + // @ts-ignore + const originalFrom = ReadableStream.from; + // @ts-ignore + ReadableStream.from = (body) => { + if (body?.source) { + return body; + } + return originalFrom(body); + }; +} + +async function executeAdapterMethod( + api: BaseLlmApi, + methodToTest: keyof BaseLlmApi, + params: any[], +) { + if (typeof (api as any)[methodToTest] !== "function") { + throw new Error( + `Method ${String(methodToTest)} does not exist on the adapter instance.`, + ); + } + + const result = await (api as any)[methodToTest](...params); + + // If it's a generator/async iterator, consume it to trigger the HTTP request + if (result?.next) { + for await (const _ of result) { + // Just consume the generator to trigger the request + } + } +} + +function assertFetchCall(mockFetch: any, expectedRequest: any) { + expect(mockFetch).toHaveBeenCalledTimes(1); + const [url, options] = mockFetch.mock.calls[0]; + + expect(url.toString()).toBe(expectedRequest.url); + expect(options.method).toBe(expectedRequest.method); + + if (expectedRequest.headers) { + expect(options.headers).toEqual( + expect.objectContaining(expectedRequest.headers), + ); + } + + if (expectedRequest.body) { + const actualBody = JSON.parse(options.body as string); + expect(actualBody).toEqual(expectedRequest.body); + } +} + +export async function runAdapterTest(testCase: AdapterTestCase) { + const { + config, + methodToTest, + params, + expectedRequest, + mockResponse, + mockStream, + } = testCase; + + const mockFetch = setupMockFetch(mockResponse, mockStream); + setupReadableStreamPolyfill(); + + // Mock fetch globally before constructing the API + vi.stubGlobal("fetch", mockFetch); + + // For non-OpenAI adapters, mock the fetch package + const fetchPackage = await import("@continuedev/fetch"); + if (fetchPackage.fetchwithRequestOptions) { + vi.mocked(fetchPackage.fetchwithRequestOptions).mockImplementation( + mockFetch as any, + ); + } + + const api = constructLlmApi(config); + if (!api) { + throw new Error(`Failed to construct API for provider: ${config.provider}`); + } + + // For OpenAI adapters, we need to replace the fetch on the OpenAI client + if ((api as any).openai) { + (api as any).openai.fetch = mockFetch; + } + + await executeAdapterMethod(api, methodToTest, params); + assertFetchCall(mockFetch, expectedRequest); +} + +export interface AdapterTestConfig { + providerName: string; + config: LLMConfig; + expectedApiBase: string; + customHeaders?: Record; + customBodyOptions?: any; +} + +export const createAdapterTests = (testConfig: AdapterTestConfig) => { + describe(testConfig.providerName, () => { + afterEach(() => { + vi.clearAllMocks(); + vi.unstubAllGlobals(); + }); + + test("should construct API correctly", () => { + const api = constructLlmApi(testConfig.config); + expect(api).toBeDefined(); + }); + + test("chatCompletionNonStream should send a valid request", async () => { + await runAdapterTest({ + config: testConfig.config, + methodToTest: "chatCompletionNonStream", + params: [ + { + model: "gpt-4", + messages: [{ role: "user", content: "hello" }], + }, + new AbortController().signal, + ], + expectedRequest: { + url: `${testConfig.expectedApiBase}chat/completions`, + method: "POST", + headers: { + ...testConfig.customHeaders, + }, + body: { + model: "gpt-4", + messages: [{ role: "user", content: "hello" }], + ...testConfig.customBodyOptions, + }, + }, + mockResponse: { + id: "test-id", + object: "chat.completion", + created: 1234567890, + model: "gpt-4", + choices: [ + { + index: 0, + message: { + role: "assistant", + content: "Hello! How can I help you today?", + }, + finish_reason: "stop", + }, + ], + usage: { + prompt_tokens: 10, + completion_tokens: 15, + total_tokens: 25, + }, + }, + }); + }); + + test("chatCompletionStream should send a valid request", async () => { + await runAdapterTest({ + config: testConfig.config, + methodToTest: "chatCompletionStream", + params: [ + { + model: "gpt-4", + messages: [{ role: "user", content: "hello" }], + stream: true, + }, + new AbortController().signal, + ], + expectedRequest: { + url: `${testConfig.expectedApiBase}chat/completions`, + method: "POST", + headers: { + ...testConfig.customHeaders, + }, + body: { + model: "gpt-4", + messages: [{ role: "user", content: "hello" }], + stream: true, + ...testConfig.customBodyOptions, + }, + }, + mockStream: [ + { + id: "test-id", + object: "chat.completion.chunk", + created: 1234567890, + model: "gpt-4", + choices: [ + { + index: 0, + delta: { content: "Hello" }, + finish_reason: null, + }, + ], + }, + { + id: "test-id", + object: "chat.completion.chunk", + created: 1234567890, + model: "gpt-4", + choices: [ + { + index: 0, + delta: { content: " world" }, + finish_reason: null, + }, + ], + }, + ], + }); + }); + + test("embed should send a valid request", async () => { + await runAdapterTest({ + config: testConfig.config, + methodToTest: "embed", + params: [ + { + model: "text-embedding-ada-002", + input: ["Hello", "World"], + }, + ], + expectedRequest: { + url: `${testConfig.expectedApiBase}embeddings`, + method: "POST", + headers: { + ...testConfig.customHeaders, + }, + body: { + model: "text-embedding-ada-002", + input: ["Hello", "World"], + encoding_format: "base64", + ...testConfig.customBodyOptions, + }, + }, + mockResponse: { + object: "list", + data: [ + { + object: "embedding", + embedding: [0.1, 0.2, 0.3], + index: 0, + }, + { + object: "embedding", + embedding: [0.4, 0.5, 0.6], + index: 1, + }, + ], + model: "text-embedding-ada-002", + usage: { + prompt_tokens: 6, + total_tokens: 6, + }, + }, + }); + }); + }); +}; diff --git a/packages/openai-adapters/src/test/anthropic-adapter.vitest.ts b/packages/openai-adapters/src/test/anthropic-adapter.vitest.ts new file mode 100644 index 000000000..24d087187 --- /dev/null +++ b/packages/openai-adapters/src/test/anthropic-adapter.vitest.ts @@ -0,0 +1,201 @@ +import { describe, test, vi, expect, afterEach } from "vitest"; +import { runAdapterTest } from "./adapter-test-utils.js"; + +// Mock the fetch package +vi.mock("@continuedev/fetch", async () => { + const actual = await vi.importActual("@continuedev/fetch"); + return { + ...actual, + fetchwithRequestOptions: vi.fn(), + }; +}); + +describe("Anthropic Adapter Tests", () => { + afterEach(() => { + vi.clearAllMocks(); + vi.unstubAllGlobals(); + }); + + test("should construct API correctly", async () => { + const { constructLlmApi } = await import("../index.js"); + const api = constructLlmApi({ + provider: "anthropic", + apiKey: "test-api-key", + }); + expect(api).toBeDefined(); + }); + + test("chatCompletionNonStream should send a valid request", async () => { + await runAdapterTest({ + config: { + provider: "anthropic", + apiKey: "test-api-key", + apiBase: "https://api.anthropic.com/v1/", + }, + methodToTest: "chatCompletionNonStream", + params: [ + { + model: "claude-3-5-sonnet-20241022", + messages: [{ role: "user", content: "hello" }], + }, + new AbortController().signal, + ], + expectedRequest: { + url: "https://api.anthropic.com/v1/messages", + method: "POST", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "anthropic-version": "2023-06-01", + "x-api-key": "test-api-key", + }, + body: { + messages: [{ role: "user", content: "hello" }], + system: undefined, + model: "claude-3-5-sonnet-20241022", + max_tokens: 4096, + stream: undefined, + }, + }, + mockResponse: { + id: "msg_test", + type: "message", + role: "assistant", + content: [ + { + type: "text", + text: "Hello! How can I help you today?", + }, + ], + model: "claude-3-5-sonnet-20241022", + stop_reason: "end_turn", + stop_sequence: null, + usage: { + input_tokens: 10, + output_tokens: 15, + }, + }, + }); + }); + + test("chatCompletionStream should send a valid request", async () => { + await runAdapterTest({ + config: { + provider: "anthropic", + apiKey: "test-api-key", + apiBase: "https://api.anthropic.com/v1/", + }, + methodToTest: "chatCompletionStream", + params: [ + { + model: "claude-3-5-sonnet-20241022", + messages: [{ role: "user", content: "hello" }], + stream: true, + }, + new AbortController().signal, + ], + expectedRequest: { + url: "https://api.anthropic.com/v1/messages", + method: "POST", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "anthropic-version": "2023-06-01", + "x-api-key": "test-api-key", + }, + body: { + messages: [{ role: "user", content: "hello" }], + system: undefined, + model: "claude-3-5-sonnet-20241022", + max_tokens: 4096, + stream: true, + }, + }, + mockStream: [ + { + type: "content_block_delta", + delta: { + type: "text_delta", + text: "Hello", + }, + }, + { + type: "content_block_delta", + delta: { + type: "text_delta", + text: " world", + }, + }, + ], + }); + }); + + test("chatCompletionStream with system message should convert correctly", async () => { + await runAdapterTest({ + config: { + provider: "anthropic", + apiKey: "test-api-key", + apiBase: "https://api.anthropic.com/v1/", + }, + methodToTest: "chatCompletionStream", + params: [ + { + model: "claude-3-5-sonnet-20241022", + messages: [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: "hello" }, + ], + stream: true, + }, + new AbortController().signal, + ], + expectedRequest: { + url: "https://api.anthropic.com/v1/messages", + method: "POST", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "anthropic-version": "2023-06-01", + "x-api-key": "test-api-key", + }, + body: { + messages: [{ role: "user", content: "hello" }], + system: [ + { + type: "text", + text: "You are a helpful assistant.", + cache_control: { type: "ephemeral" }, + }, + ], + model: "claude-3-5-sonnet-20241022", + max_tokens: 4096, + stream: true, + }, + }, + mockStream: [ + { + type: "content_block_delta", + delta: { + type: "text_delta", + text: "Hello", + }, + }, + ], + }); + }); + + test("embed should throw not implemented error", async () => { + const { constructLlmApi } = await import("../index.js"); + const api = constructLlmApi({ + provider: "anthropic", + apiKey: "test-api-key", + }); + + await expect( + api!.embed({ + model: "text-embedding-ada-002", + input: ["Hello", "World"], + }), + ).rejects.toThrow("Method not implemented."); + }); +}); diff --git a/packages/openai-adapters/src/test/openai-adapter.vitest.ts b/packages/openai-adapters/src/test/openai-adapter.vitest.ts new file mode 100644 index 000000000..6abc49a3a --- /dev/null +++ b/packages/openai-adapters/src/test/openai-adapter.vitest.ts @@ -0,0 +1,28 @@ +import { describe, vi } from "vitest"; +import { createAdapterTests } from "./adapter-test-utils.js"; + +// Mock the fetch package (not needed for OpenAI but required by the shared test utils) +vi.mock("@continuedev/fetch", async () => { + const actual = await vi.importActual("@continuedev/fetch"); + return { + ...actual, + fetchwithRequestOptions: vi.fn(), + }; +}); + +describe("OpenAI Adapter Tests", () => { + createAdapterTests({ + providerName: "openai", + config: { + provider: "openai", + apiKey: "test-api-key", + apiBase: "https://api.openai.com/v1/", + }, + expectedApiBase: "https://api.openai.com/v1/", + customHeaders: { + authorization: "Bearer test-api-key", + "content-type": "application/json", + accept: "application/json", + }, + }); +}); diff --git a/packages/openai-adapters/vitest.config.ts b/packages/openai-adapters/vitest.config.ts new file mode 100644 index 000000000..2185afe65 --- /dev/null +++ b/packages/openai-adapters/vitest.config.ts @@ -0,0 +1,15 @@ +import { defineConfig } from "vitest/config"; + +export default defineConfig({ + test: { + environment: "node", + globals: true, + testTimeout: 10000, + include: ["src/**/*.test.{ts,tsx}", "src/**/*.vitest.{ts,tsx}"], + }, + resolve: { + alias: { + "(.+)\\.js": "$1", + }, + }, +});