feat(packages): Add selective pi-ai base entrypoints (#5348)
This commit is contained in:
@@ -5,6 +5,7 @@ import { describe, expect, it } from "vitest";
|
||||
|
||||
const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
||||
const aiEntryUrl = new URL("../src/index.ts", import.meta.url).href;
|
||||
const baseEntryUrl = new URL("../src/base.ts", import.meta.url).href;
|
||||
|
||||
const SDK_SPECIFIERS = [
|
||||
"@anthropic-ai/sdk",
|
||||
@@ -16,9 +17,10 @@ const SDK_SPECIFIERS = [
|
||||
|
||||
type ProbeResult = {
|
||||
loadedSpecifiers: string[];
|
||||
value?: unknown;
|
||||
};
|
||||
|
||||
function runProbe(action: string): ProbeResult {
|
||||
function runProbe(action: string, entryUrl = aiEntryUrl): ProbeResult {
|
||||
const script = `
|
||||
import { registerHooks } from "node:module";
|
||||
|
||||
@@ -34,9 +36,11 @@ function runProbe(action: string): ProbeResult {
|
||||
},
|
||||
});
|
||||
|
||||
const mod = await import(${JSON.stringify(aiEntryUrl)});
|
||||
${action}
|
||||
console.log(JSON.stringify({ loadedSpecifiers: [...new Set(loaded)] }));
|
||||
const mod = await import(${JSON.stringify(entryUrl)});
|
||||
const value = await (async () => {
|
||||
${action}
|
||||
})();
|
||||
console.log(JSON.stringify({ loadedSpecifiers: [...new Set(loaded)], value }));
|
||||
`;
|
||||
|
||||
const result = spawnSync(process.execPath, ["--input-type=module", "--eval", script], {
|
||||
@@ -66,6 +70,33 @@ describe("lazy provider module loading", () => {
|
||||
expect(result.loadedSpecifiers).toEqual([]);
|
||||
});
|
||||
|
||||
it("registers built-in transports when importing the root barrel", () => {
|
||||
const result = runProbe(`return mod.getApiProviders().map((provider) => provider.api).sort();`);
|
||||
expect(result.value).toEqual([
|
||||
"anthropic-messages",
|
||||
"azure-openai-responses",
|
||||
"bedrock-converse-stream",
|
||||
"google-generative-ai",
|
||||
"google-vertex",
|
||||
"mistral-conversations",
|
||||
"openai-codex-responses",
|
||||
"openai-completions",
|
||||
"openai-responses",
|
||||
]);
|
||||
});
|
||||
|
||||
it("registers built-in image transports when importing the root barrel", () => {
|
||||
const result = runProbe(`return mod.getImagesApiProvider("openrouter-images")?.api;`);
|
||||
expect(result.loadedSpecifiers).toEqual([]);
|
||||
expect(result.value).toBe("openrouter-images");
|
||||
});
|
||||
|
||||
it("does not load provider SDKs or register transports when importing the base barrel", () => {
|
||||
const result = runProbe(`return mod.getApiProviders().map((provider) => provider.api);`, baseEntryUrl);
|
||||
expect(result.loadedSpecifiers).toEqual([]);
|
||||
expect(result.value).toEqual([]);
|
||||
});
|
||||
|
||||
it("loads only the Anthropic SDK when calling the root lazy wrapper", () => {
|
||||
const result = runProbe(`
|
||||
const model = {
|
||||
@@ -96,4 +127,17 @@ describe("lazy provider module loading", () => {
|
||||
|
||||
expect(result.loadedSpecifiers).toEqual(["@anthropic-ai/sdk"]);
|
||||
});
|
||||
|
||||
it("dispatches through a lazy wrapper again after resetting providers", () => {
|
||||
const result = runProbe(`
|
||||
const model = mod.getModel("anthropic", "claude-sonnet-4-6");
|
||||
const context = { messages: [{ role: "user", content: "hi" }] };
|
||||
await mod.streamSimple(model, context).result();
|
||||
mod.resetApiProviders();
|
||||
return (await mod.streamSimple(model, context).result()).stopReason;
|
||||
`);
|
||||
|
||||
expect(result.loadedSpecifiers).toEqual(["@anthropic-ai/sdk"]);
|
||||
expect(result.value).toBe("error");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user