feat(packages): Add selective pi-ai base entrypoints (#5348)

This commit is contained in:
Fred K. Schott
2026-06-18 15:41:49 -07:00
committed by GitHub
parent 1a418ad223
commit 0d89a33373
78 changed files with 671 additions and 495 deletions

View File

@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { complete, stream } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { complete, stream } from "../src/stream.ts";
import type { Api, Context, Model, StreamOptions } from "../src/types.ts";
type StreamOptionsWithExtras = StreamOptions & Record<string, unknown>;

View File

@@ -1,8 +1,8 @@
import { Type } from "typebox";
import { describe, expect, it } from "vitest";
import { getEnvApiKey } from "../src/env-api-keys.ts";
import { complete } from "../src/index.ts";
import { getModels, getProviders } from "../src/models.ts";
import { complete } from "../src/stream.ts";
import type { Api, KnownProvider, Model, ProviderStreamOptions, Tool } from "../src/types.ts";
import { resolveApiKey } from "./oauth.ts";

View File

@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { streamSimple } from "../src/stream.ts";
import { streamSimple } from "../src/index.ts";
import type { AssistantMessage, Context, Model } from "../src/types.ts";
interface AnthropicPayload {

View File

@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { streamSimple } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { streamSimple } from "../src/stream.ts";
import type { Context, Model, SimpleStreamOptions } from "../src/types.ts";
interface AnthropicThinkingPayload {

View File

@@ -1,7 +1,7 @@
import { describe, expect, it } from "vitest";
import { getEnvApiKey } from "../src/env-api-keys.ts";
import { complete } from "../src/index.ts";
import { getModels, getProviders } from "../src/models.ts";
import { complete } from "../src/stream.ts";
import type { Api, KnownProvider, Model, ProviderStreamOptions } from "../src/types.ts";
import { resolveApiKey } from "./oauth.ts";

View File

@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { streamSimple } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { streamSimple } from "../src/stream.ts";
import type { Context } from "../src/types.ts";
interface AnthropicThinkingPayload {

View File

@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { streamSimple } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { streamSimple } from "../src/stream.ts";
import type { Context, Model, SimpleStreamOptions } from "../src/types.ts";
interface AnthropicTemperaturePayload {

View File

@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { streamSimple } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { streamSimple } from "../src/stream.ts";
import type { Context, Model, SimpleStreamOptions } from "../src/types.ts";
interface AnthropicThinkingPayload {

View File

@@ -1,7 +1,7 @@
import { Type } from "typebox";
import { describe, expect, it } from "vitest";
import { stream } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { stream } from "../src/stream.ts";
import type { Context, Tool } from "../src/types.ts";
import { resolveApiKey } from "./oauth.ts";

View File

@@ -0,0 +1,68 @@
import { afterEach, describe, expect, it } from "vitest";
import { clearApiProviders, complete, getApiProvider, getApiProviders } from "../src/base.ts";
import { register as registerAmazonBedrock } from "../src/providers/amazon-bedrock.ts";
import { register as registerAnthropic } from "../src/providers/anthropic.ts";
import { register as registerAzureOpenAIResponses } from "../src/providers/azure-openai-responses.ts";
import { fauxAssistantMessage, registerFauxProvider } from "../src/providers/faux.ts";
import { register as registerGoogle } from "../src/providers/google.ts";
import { register as registerGoogleVertex } from "../src/providers/google-vertex.ts";
import { register as registerMistral } from "../src/providers/mistral.ts";
import { register as registerOpenAICodexResponses } from "../src/providers/openai-codex-responses.ts";
import { register as registerOpenAICompletions } from "../src/providers/openai-completions.ts";
import { register as registerOpenAIResponses } from "../src/providers/openai-responses.ts";
const registrations = [
["bedrock-converse-stream", registerAmazonBedrock],
["anthropic-messages", registerAnthropic],
["azure-openai-responses", registerAzureOpenAIResponses],
["google-generative-ai", registerGoogle],
["google-vertex", registerGoogleVertex],
["mistral-conversations", registerMistral],
["openai-codex-responses", registerOpenAICodexResponses],
["openai-completions", registerOpenAICompletions],
["openai-responses", registerOpenAIResponses],
] as const;
afterEach(() => {
clearApiProviders();
});
describe("base entrypoint", () => {
it("starts without built-in provider registrations", () => {
expect(getApiProviders()).toEqual([]);
});
it.each(registrations)("registers the %s transport explicitly", (api, register) => {
register();
expect(getApiProvider(api)?.api).toBe(api);
});
it("dispatches custom providers", async () => {
const registration = registerFauxProvider();
registration.setResponses([fauxAssistantMessage("hello")]);
const response = await complete(registration.getModel(), {
messages: [{ role: "user", content: "hi", timestamp: Date.now() }],
});
expect(response.content).toEqual([{ type: "text", text: "hello" }]);
});
it("fails clearly when no transport is registered", async () => {
await expect(
complete(
{
id: "missing-model",
name: "Missing Model",
api: "missing-api",
provider: "missing-provider",
baseUrl: "https://example.com",
reasoning: false,
input: ["text"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 1,
maxTokens: 1,
},
{ messages: [] },
),
).rejects.toThrow("No API provider registered for api: missing-api");
});
});

View File

@@ -17,8 +17,8 @@
*/
import { describe, expect, it } from "vitest";
import { complete } from "../src/index.ts";
import { getModels } from "../src/models.ts";
import { complete } from "../src/stream.ts";
import type { Context } from "../src/types.ts";
import { hasBedrockCredentials } from "./bedrock-utils.ts";

View File

@@ -1,10 +1,10 @@
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { stream } from "../src/index.ts";
import { MODELS } from "../src/models.generated.ts";
import { getModel } from "../src/models.ts";
import { streamAnthropic } from "../src/providers/anthropic.ts";
import { streamOpenAICompletions } from "../src/providers/openai-completions.ts";
import { streamOpenAIResponses } from "../src/providers/openai-responses.ts";
import { stream } from "../src/stream.ts";
import type { Context, Model } from "../src/types.ts";
class PayloadCaptured extends Error {

View File

@@ -14,8 +14,8 @@
import type { ChildProcess } from "child_process";
import { execSync, spawn } from "child_process";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { complete } from "../src/index.ts";
import { getModel, getModels } from "../src/models.ts";
import { complete } from "../src/stream.ts";
import type { AssistantMessage, Context, Model, Usage } from "../src/types.ts";
import { isContextOverflow } from "../src/utils/overflow.ts";
import { hasAzureOpenAICredentials } from "./azure-utils.ts";

View File

@@ -25,8 +25,8 @@
import { writeFileSync } from "fs";
import { Type } from "typebox";
import { beforeAll, describe, expect, it } from "vitest";
import { completeSimple, getEnvApiKey } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { completeSimple, getEnvApiKey } from "../src/stream.ts";
import type { Api, AssistantMessage, Message, Model, Tool, ToolResultMessage } from "../src/types.ts";
import { hasAzureOpenAICredentials } from "./azure-utils.ts";
import { hasCloudflareAiGatewayCredentials, hasCloudflareWorkersAICredentials } from "./cloudflare-utils.ts";

View File

@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { complete } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { complete } from "../src/stream.ts";
import type { Api, AssistantMessage, Context, Model, StreamOptions, UserMessage } from "../src/types.ts";
type StreamOptionsWithExtras = StreamOptions & Record<string, unknown>;

View File

@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { streamSimple } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { streamSimple } from "../src/stream.ts";
import type { Api, Context, Model, SimpleStreamOptions } from "../src/types.ts";
type SimpleOptionsWithExtras = SimpleStreamOptions & Record<string, unknown>;

View File

@@ -1,8 +1,8 @@
import { Type } from "typebox";
import { describe, expect, it } from "vitest";
import { getEnvApiKey } from "../src/env-api-keys.ts";
import { completeSimple } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { completeSimple } from "../src/stream.ts";
import type { Api, Context, Model, StopReason, Tool, ToolCall, ToolResultMessage } from "../src/types.ts";
import { StringEnum } from "../src/utils/typebox-helpers.ts";
import { hasBedrockCredentials } from "./bedrock-utils.ts";

View File

@@ -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");
});
});

View File

@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { streamSimple } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { streamSimple } from "../src/stream.ts";
import type { Context, Model, SimpleStreamOptions } from "../src/types.ts";
interface MistralPayload {

View File

@@ -1,7 +1,7 @@
import { Type } from "typebox";
import { describe, expect, it } from "vitest";
import { complete } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { complete } from "../src/stream.ts";
import type { Context, Model } from "../src/types.ts";
interface MistralToolPayload {

View File

@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { complete } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { complete } from "../src/stream.ts";
import type { Context } from "../src/types.ts";
import { resolveApiKey } from "./oauth.ts";

View File

@@ -1,6 +1,6 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { streamSimple } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { streamSimple } from "../src/stream.ts";
// Empty tools arrays must NOT be serialized as `tools: []` — some OpenAI-compatible
// backends (e.g. DashScope / Aliyun Qwen via compatible-mode) reject the request with

View File

@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { complete } from "../src/stream.ts";
import { complete } from "../src/index.ts";
import type { Model } from "../src/types.ts";
// Router/virtual ids (e.g. OpenRouter `auto`) keep `model` pinned to the

View File

@@ -1,8 +1,8 @@
import { Type } from "typebox";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { stream, streamSimple } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { convertMessages } from "../src/providers/openai-completions.ts";
import { stream, streamSimple } from "../src/stream.ts";
import type { AssistantMessage, Model, Tool, ToolResultMessage } from "../src/types.ts";
const mockState = vi.hoisted(() => ({

View File

@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { complete } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { complete } from "../src/stream.ts";
import type { Context } from "../src/types.ts";
describe.skipIf(!process.env.OPENAI_API_KEY)("openai responses cache affinity e2e", () => {

View File

@@ -1,7 +1,7 @@
import { Type } from "typebox";
import { describe, expect, it } from "vitest";
import { complete, getEnvApiKey } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { complete, getEnvApiKey } from "../src/stream.ts";
import type { AssistantMessage, Context, Message, Tool, ToolCall } from "../src/types.ts";
const testToolSchema = Type.Object({

View File

@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { completeSimple } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { completeSimple } from "../src/stream.ts";
function createLongSystemPrompt(): string {
const nonce = `${Date.now()}-${Math.random()}`;

View File

@@ -1,5 +1,8 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { generateImages } from "../src/images.ts";
import { clearImagesApiProviders, getImagesApiProvider } from "../src/images-api-registry.ts";
import { register as registerOpenRouterImages } from "../src/providers/images/openrouter.ts";
import { registerBuiltInImagesApiProviders } from "../src/providers/register-builtins.ts";
import type { ImagesContext, ImagesModel } from "../src/types.ts";
const mockState = vi.hoisted(() => ({
@@ -62,6 +65,15 @@ describe("openrouter images", () => {
beforeEach(() => {
mockState.lastParams = undefined;
mockState.lastRequestOptions = undefined;
clearImagesApiProviders();
registerBuiltInImagesApiProviders();
});
it("registers the direct OpenRouter images transport explicitly", () => {
clearImagesApiProviders();
expect(getImagesApiProvider("openrouter-images")).toBeUndefined();
registerOpenRouterImages();
expect(getImagesApiProvider("openrouter-images")?.api).toBe("openrouter-images");
});
it("returns text plus images in final output", async () => {

View File

@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { complete } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { complete } from "../src/stream.ts";
import type { Api, Context, Model, StreamOptions } from "../src/types.ts";
import { hasAzureOpenAICredentials, resolveAzureDeploymentName } from "./azure-utils.ts";
import { resolveApiKey } from "./oauth.ts";

View File

@@ -4,8 +4,8 @@ import { dirname, join } from "path";
import { Type } from "typebox";
import { fileURLToPath } from "url";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { complete, stream } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { complete, stream } from "../src/stream.ts";
import type { Api, Context, ImageContent, Model, StreamOptions, Tool, ToolResultMessage } from "../src/types.ts";
type StreamOptionsWithExtras = StreamOptions & Record<string, unknown>;

View File

@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { stream } from "../src/index.ts";
import { getModel, getModels } from "../src/models.ts";
import { stream } from "../src/stream.ts";
import type { Api, Context, Model, StreamOptions } from "../src/types.ts";
type StreamOptionsWithExtras = StreamOptions & Record<string, unknown>;

View File

@@ -12,8 +12,8 @@
import { Type } from "typebox";
import { describe, expect, it } from "vitest";
import { completeSimple, getEnvApiKey } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { completeSimple, getEnvApiKey } from "../src/stream.ts";
import type { AssistantMessage, Message, Tool, ToolResultMessage } from "../src/types.ts";
import { resolveApiKey } from "./oauth.ts";

View File

@@ -1,7 +1,7 @@
import { Type } from "typebox";
import { describe, expect, it } from "vitest";
import { complete } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { complete } from "../src/stream.ts";
import type { Api, Context, Model, StreamOptions, Tool } from "../src/types.ts";
type StreamOptionsWithExtras = StreamOptions & Record<string, unknown>;

View File

@@ -13,8 +13,8 @@
*/
import { describe, expect, it } from "vitest";
import { complete } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { complete } from "../src/stream.ts";
import type { Api, Context, Model, StreamOptions, Usage } from "../src/types.ts";
type StreamOptionsWithExtras = StreamOptions & Record<string, unknown>;

View File

@@ -1,7 +1,7 @@
import { Type } from "typebox";
import { describe, expect, it } from "vitest";
import { complete } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { complete } from "../src/stream.ts";
import type { Api, Context, Model, StreamOptions, ToolResultMessage } from "../src/types.ts";
type StreamOptionsWithExtras = StreamOptions & Record<string, unknown>;

View File

@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { stream } from "../src/index.ts";
import { getModel } from "../src/models.ts";
import { stream } from "../src/stream.ts";
import type { Context, Model } from "../src/types.ts";
function makeContext(): Context {

View File

@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { completeSimple, getEnvApiKey, streamSimple } from "../src/stream.ts";
import { completeSimple, getEnvApiKey, streamSimple } from "../src/index.ts";
import type { AssistantMessage, Context, Model } from "../src/types.ts";
const provider = "xiaomi-token-plan-ams";

View File

@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { complete } from "../src/index.ts";
import { MODELS } from "../src/models.generated.ts";
import { complete } from "../src/stream.ts";
import type { Model } from "../src/types.ts";
describe.skipIf(!process.env.OPENCODE_API_KEY)("OpenCode Models Smoke Test", () => {