This commit is contained in:
2026-07-26 14:02:37 +07:00
parent bc56546b49
commit 367ebc1c7f
171 changed files with 4617 additions and 10402 deletions
@@ -5,13 +5,17 @@ import type { Api, Model } from "../src/types.ts";
const EXPECTED_CURRENT_ADAPTIVE_THINKING_MODELS = [
"anthropic/claude-fable-5",
"anthropic/claude-opus-4-8",
"anthropic/claude-opus-5",
"anthropic/claude-sonnet-5",
"cloudflare-ai-gateway/claude-fable-5",
"kimi-coding/kimi-for-coding",
"kimi-coding/k3",
"kimi-coding/kimi-for-coding-highspeed",
"opencode/claude-opus-4-8",
"opencode/claude-opus-5",
"vercel-ai-gateway/anthropic/claude-opus-4.8",
"vercel-ai-gateway/anthropic/claude-opus-5",
"vercel-ai-gateway/anthropic/claude-opus-5-fast",
"vercel-ai-gateway/anthropic/claude-sonnet-5",
];
@@ -30,7 +34,7 @@ describe("Anthropic adaptive thinking model metadata", () => {
expect(flaggedModels).toEqual(expect.arrayContaining([...EXPECTED_CURRENT_ADAPTIVE_THINKING_MODELS].sort()));
expect(flaggedModels).toEqual(
flaggedModels.filter((modelId) =>
/(opus[-.]4[-.][678]|sonnet[-.]4[-.]6|sonnet[-.]5|fable[-.]5|kimi-coding\/)/.test(modelId),
/(opus[-.](4[-.][678]|5)|sonnet[-.]4[-.]6|sonnet[-.]5|fable[-.]5|kimi-coding\/)/.test(modelId),
),
);
});
@@ -0,0 +1,183 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { stream as streamAnthropic } from "../src/api/anthropic-messages.ts";
import { ANTHROPIC_AUTH_TOKEN_ENV, ANTHROPIC_OAUTH_TOKEN_ENV } from "../src/env-api-keys.ts";
import { createModels } from "../src/models.ts";
import { anthropicProvider } from "../src/providers/anthropic.ts";
import type { Context, Model } from "../src/types.ts";
const mockState = vi.hoisted(() => ({
constructorOpts: undefined as Record<string, unknown> | undefined,
createParams: undefined as Record<string, unknown> | undefined,
}));
vi.mock("@anthropic-ai/sdk", () => {
function createSseResponse(): Response {
const body = [
`event: message_start\ndata: ${JSON.stringify({
type: "message_start",
message: {
id: "msg_test",
usage: { input_tokens: 1, output_tokens: 0 },
},
})}\n`,
`event: message_delta\ndata: ${JSON.stringify({
type: "message_delta",
delta: { stop_reason: "end_turn" },
usage: { output_tokens: 1 },
})}\n`,
`event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n`,
].join("\n");
return new Response(body, {
status: 200,
headers: { "content-type": "text/event-stream" },
});
}
class FakeAnthropic {
constructor(opts: Record<string, unknown>) {
mockState.constructorOpts = opts;
}
messages = {
create: (params: Record<string, unknown>) => {
mockState.createParams = params;
return {
asResponse: async () => createSseResponse(),
};
},
};
}
return { default: FakeAnthropic };
});
const context: Context = {
systemPrompt: "System prompt.",
messages: [{ role: "user", content: "Hello", timestamp: Date.now() }],
};
const anthropicModel: Model<"anthropic-messages"> = {
id: "claude-test",
name: "Claude Test",
api: "anthropic-messages",
provider: "anthropic",
baseUrl: "https://api.anthropic.com",
reasoning: false,
input: ["text"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 100000,
maxTokens: 4096,
};
afterEach(() => {
mockState.constructorOpts = undefined;
mockState.createParams = undefined;
});
describe("Anthropic auth token env", () => {
it("resolves ANTHROPIC_AUTH_TOKEN as a bearer Authorization header", async () => {
const provider = anthropicProvider();
const auth = await provider.auth.apiKey?.resolve({
ctx: {
env: async (name) =>
({
ANTHROPIC_AUTH_TOKEN: "auth-token",
ANTHROPIC_OAUTH_TOKEN: "oauth-token",
ANTHROPIC_API_KEY: "api-key",
})[name],
fileExists: async () => false,
},
});
expect(auth).toEqual({
auth: { headers: { Authorization: "Bearer auth-token" } },
source: ANTHROPIC_AUTH_TOKEN_ENV,
});
});
it("preserves ANTHROPIC_OAUTH_TOKEN as OAuth-shaped API auth", async () => {
const provider = anthropicProvider();
const auth = await provider.auth.apiKey?.resolve({
ctx: {
env: async (name) =>
({
ANTHROPIC_OAUTH_TOKEN: "oauth-token",
ANTHROPIC_API_KEY: "api-key",
})[name],
fileExists: async () => false,
},
});
expect(auth).toEqual({
auth: { apiKey: "oauth-token" },
source: ANTHROPIC_OAUTH_TOKEN_ENV,
});
});
it("uses Authorization headers without OAuth-mode request shaping", async () => {
const stream = streamAnthropic(anthropicModel, context, {
headers: { Authorization: "Bearer gateway-token" },
});
await stream.result();
expect(mockState.constructorOpts?.apiKey).toBeNull();
expect(mockState.constructorOpts?.authToken).toBeNull();
const headers = mockState.constructorOpts?.defaultHeaders as Record<string, string | null>;
expect(headers.Authorization).toBe("Bearer gateway-token");
expect(headers["anthropic-beta"] ?? "").not.toContain("oauth-2025-04-20");
expect(mockState.createParams?.system).toEqual([expect.objectContaining({ text: "System prompt." })]);
});
it("threads authContext ANTHROPIC_AUTH_TOKEN through request headers", async () => {
const models = createModels({
authContext: {
env: async (name) => (name === "ANTHROPIC_AUTH_TOKEN" ? "ctx-token" : undefined),
fileExists: async () => false,
},
});
models.setProvider(anthropicProvider());
await models.streamSimple(anthropicModel, context).result();
expect(mockState.constructorOpts?.apiKey).toBeNull();
expect(mockState.constructorOpts?.authToken).toBeNull();
const headers = mockState.constructorOpts?.defaultHeaders as Record<string, string>;
expect(headers.Authorization).toBe("Bearer ctx-token");
expect(headers["anthropic-beta"] ?? "").not.toContain("oauth-2025-04-20");
expect(mockState.createParams?.system).toEqual([expect.objectContaining({ text: "System prompt." })]);
});
it("preserves OAuth request shaping for ANTHROPIC_OAUTH_TOKEN", async () => {
const models = createModels({
authContext: {
env: async (name) => (name === "ANTHROPIC_OAUTH_TOKEN" ? "sk-ant-oat-test" : undefined),
fileExists: async () => false,
},
});
models.setProvider(anthropicProvider());
await models.streamSimple(anthropicModel, context).result();
expect(mockState.constructorOpts?.apiKey).toBeNull();
expect(mockState.constructorOpts?.authToken).toBe("sk-ant-oat-test");
const headers = mockState.constructorOpts?.defaultHeaders as Record<string, string>;
expect(headers["anthropic-beta"]).toContain("oauth-2025-04-20");
});
it("lets explicit request headers override ANTHROPIC_AUTH_TOKEN", async () => {
const models = createModels({
authContext: {
env: async (name) => (name === "ANTHROPIC_AUTH_TOKEN" ? "ctx-token" : undefined),
fileExists: async () => false,
},
});
models.setProvider(anthropicProvider());
await models
.streamSimple(anthropicModel, context, { headers: { Authorization: "Bearer explicit-token" } })
.result();
const headers = mockState.constructorOpts?.defaultHeaders as Record<string, string>;
expect(headers.Authorization).toBe("Bearer explicit-token");
});
});
@@ -32,6 +32,17 @@ const tool: Tool = {
parameters: Type.Object({ value: Type.String() }),
};
const schemaCompatibilityTool: Tool = {
...tool,
parameters: Type.Object({ value: Type.String() }, { additionalProperties: false, title: "LookupInput" }),
};
const strictTool: Tool = {
...tool,
parameters: Type.Object({ value: Type.String() }, { additionalProperties: false, title: "StrictLookupInput" }),
constrainedSampling: { type: "json_schema", strict: "prefer" },
};
function createContext(tools: Tool[] = [tool]): Context {
return {
messages: [{ role: "user", content: "Use the tool", timestamp: Date.now() }],
@@ -98,6 +109,14 @@ function getFirstTool(body: Record<string, unknown>): Record<string, unknown> {
return tools[0] as Record<string, unknown>;
}
function getFirstToolInputSchema(body: Record<string, unknown>): Record<string, unknown> {
const inputSchema = getFirstTool(body).input_schema;
if (typeof inputSchema !== "object" || inputSchema === null || Array.isArray(inputSchema)) {
throw new Error("Expected first tool input schema in request body");
}
return inputSchema as Record<string, unknown>;
}
describe("Anthropic eager tool input streaming compatibility", () => {
it("sends per-tool eager_input_streaming by default", async () => {
const request = await captureAnthropicRequest(undefined, createContext());
@@ -119,4 +138,24 @@ describe("Anthropic eager tool input streaming compatibility", () => {
expect(request.body.tools).toBeUndefined();
expect(request.headers["anthropic-beta"]).toBeUndefined();
});
it("only sends the full input schema for strict JSON-schema tools", async () => {
const legacyRequest = await captureAnthropicRequest(
{ supportsStrictTools: true },
createContext([schemaCompatibilityTool]),
);
const parameters = schemaCompatibilityTool.parameters as { properties?: unknown; required?: unknown };
expect(getFirstToolInputSchema(legacyRequest.body)).toEqual({
type: "object",
properties: parameters.properties,
required: parameters.required,
});
const strictRequest = await captureAnthropicRequest({ supportsStrictTools: true }, createContext([strictTool]));
expect(getFirstTool(strictRequest.body).strict).toBe(true);
expect(getFirstToolInputSchema(strictRequest.body)).toMatchObject({
additionalProperties: false,
title: "StrictLookupInput",
});
});
});
+29 -1
View File
@@ -1,7 +1,8 @@
import { Type } from "typebox";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { stream as streamAzureOpenAIResponses } from "../src/api/azure-openai-responses.ts";
import { getModel } from "../src/compat.ts";
import type { Context } from "../src/types.ts";
import type { Context, Model } from "../src/types.ts";
interface CapturedAzureClientOptions {
apiKey: string;
@@ -14,6 +15,7 @@ interface CapturedAzureClientOptions {
interface CapturedAzureResponsesPayload {
prompt_cache_key?: string;
store?: boolean;
tools?: Array<{ strict?: boolean }>;
}
const azureMock = vi.hoisted(() => ({
@@ -165,6 +167,32 @@ describe("azure-openai-responses base URL normalization", () => {
expect(azureMock.lastParams?.store).toBe(false);
});
it("honors supportsStrictMode: false", async () => {
const baseModel = getModel("azure-openai-responses", "gpt-4o-mini");
const model: Model<"azure-openai-responses"> = {
...baseModel,
compat: { ...baseModel.compat, supportsStrictMode: false },
};
await streamAzureOpenAIResponses(
model,
{
...context,
tools: [
{
name: "preferred",
description: "Preferred constrained tool",
parameters: Type.Object({ value: Type.String() }),
constrainedSampling: { type: "json_schema", strict: "prefer" },
},
],
},
{ apiKey: "test-api-key", azureBaseUrl: "https://my-resource.openai.azure.com" },
).result();
expect(azureMock.lastParams?.tools?.[0]).not.toHaveProperty("strict");
});
it("builds correct default URL from AZURE_OPENAI_RESOURCE_NAME", async () => {
process.env.AZURE_OPENAI_RESOURCE_NAME = "my-resource";
const model = getModel("azure-openai-responses", "gpt-4o-mini");
@@ -1,3 +1,4 @@
import { Type } from "typebox";
import { describe, expect, it, vi } from "vitest";
const bedrockMock = vi.hoisted(() => ({
@@ -50,9 +51,9 @@ import type { Context, Message } from "../src/types.ts";
const baseModel = getModel("amazon-bedrock", "us.anthropic.claude-sonnet-4-5-20250929-v1:0");
async function capturePayload(context: Context): Promise<unknown> {
async function capturePayload(context: Context, model = baseModel): Promise<unknown> {
let capturedPayload: unknown;
const s = streamBedrock(baseModel, context, {
const s = streamBedrock(model, context, {
cacheRetention: "none",
signal: AbortSignal.abort(),
onPayload: (payload) => {
@@ -66,6 +67,34 @@ async function capturePayload(context: Context): Promise<unknown> {
return capturedPayload;
}
describe("Bedrock constrained sampling", () => {
it("gates native strict tool use by model capability", async () => {
const context: Context = {
messages: [{ role: "user", content: "Use the tool", timestamp: Date.now() }],
tools: [
{
name: "lookup",
description: "Look up a value",
parameters: Type.Object({ value: Type.String() }),
constrainedSampling: { type: "json_schema", strict: "require" },
},
],
};
const payload = await capturePayload(context);
const toolConfig = (payload as { toolConfig: { tools: Array<{ toolSpec: { strict?: boolean } }> } }).toolConfig;
expect(toolConfig.tools[0].toolSpec.strict).toBe(true);
context.tools![0].constrainedSampling = { type: "json_schema", strict: "prefer" };
const novaPayload = await capturePayload(context, getModel("amazon-bedrock", "amazon.nova-lite-v1:0"));
const novaToolConfig = (
novaPayload as {
toolConfig: { tools: Array<{ toolSpec: { strict?: boolean } }> };
}
).toolConfig;
expect(novaToolConfig.tools[0].toolSpec.strict).toBeUndefined();
});
});
describe("bedrock convertMessages skips unknown content types", () => {
it("skips unknown user content blocks instead of throwing", async () => {
const messages: Message[] = [
+5
View File
@@ -29,6 +29,11 @@ describe("Amazon Bedrock Models", () => {
console.log(`Found ${models.length} Bedrock models`);
});
it("exposes Claude Opus 5 through an inference profile only", () => {
expect(models.some((model) => model.id === "global.anthropic.claude-opus-5")).toBe(true);
expect(models.some((model) => model.id === "anthropic.claude-opus-5")).toBe(false);
});
if (hasBedrockCredentials() && process.env.BEDROCK_EXTENSIVE_MODEL_TEST) {
for (const model of models) {
it(`should make a simple request with ${model.id}`, { timeout: 10_000 }, async () => {
@@ -103,6 +103,26 @@ describe("Bedrock thinking payload", () => {
expect(payload.additionalModelRequestFields?.anthropic_beta).toBeUndefined();
});
it("uses adaptive thinking for Claude Opus 5 when reasoning is enabled", async () => {
const model = getModel("amazon-bedrock", "global.anthropic.claude-opus-5");
const payload = await capturePayload(model);
expect(payload.additionalModelRequestFields?.thinking).toEqual({ type: "adaptive", display: "summarized" });
expect(payload.additionalModelRequestFields?.output_config).toEqual({ effort: "high" });
expect(payload.additionalModelRequestFields?.anthropic_beta).toBeUndefined();
});
it("maps xhigh reasoning to effort=xhigh for Claude Opus 5", async () => {
const model = getModel("amazon-bedrock", "global.anthropic.claude-opus-5");
const payload = await capturePayload(model, { reasoning: "xhigh" });
expect(payload.additionalModelRequestFields?.thinking).toEqual({ type: "adaptive", display: "summarized" });
expect(payload.additionalModelRequestFields?.output_config).toEqual({ effort: "xhigh" });
expect(payload.additionalModelRequestFields?.anthropic_beta).toBeUndefined();
});
it("maps xhigh reasoning to effort=xhigh for Claude Fable 5", async () => {
const model = getModel("amazon-bedrock", "global.anthropic.claude-fable-5");
+38 -7
View File
@@ -18,6 +18,10 @@ interface OpenAICompletionsCachePayload {
prompt_cache_retention?: string;
}
interface OpenAIResponsesCachePayload extends OpenAICompletionsCachePayload {
prompt_cache_options?: { mode: "explicit" };
}
function stopAfterPayload<TPayload>(capture: (payload: TPayload) => void): (payload: unknown) => never {
return (payload: unknown): never => {
capture(payload as TPayload);
@@ -341,16 +345,16 @@ describe("Cache Retention (PI_CACHE_RETENTION)", () => {
expect(capturedPayload.prompt_cache_retention).toBeUndefined();
});
it("should omit prompt_cache_key when cacheRetention is none", async () => {
const model = getModel("openai", "gpt-4o-mini");
let capturedPayload: any = null;
it("should omit prompt_cache_key and disable implicit writes when cacheRetention is none", async () => {
const model = getModel("openai", "gpt-5.6-sol");
let capturedPayload: OpenAIResponsesCachePayload | undefined;
try {
const s = streamOpenAIResponses(model, context, {
apiKey: "fake-key",
cacheRetention: "none",
sessionId: "session-1",
onPayload: stopAfterPayload((payload) => {
onPayload: stopAfterPayload<OpenAIResponsesCachePayload>((payload) => {
capturedPayload = payload;
}),
});
@@ -362,9 +366,36 @@ describe("Cache Retention (PI_CACHE_RETENTION)", () => {
// Expected to fail
}
expect(capturedPayload).not.toBeNull();
expect(capturedPayload.prompt_cache_key).toBeUndefined();
expect(capturedPayload.prompt_cache_retention).toBeUndefined();
expect(capturedPayload).toBeDefined();
expect(capturedPayload?.prompt_cache_key).toBeUndefined();
expect(capturedPayload?.prompt_cache_retention).toBeUndefined();
expect(capturedPayload?.prompt_cache_options).toEqual({ mode: "explicit" });
});
it("should omit prompt_cache_options for models that reject it", async () => {
const model = getModel("openai", "gpt-4o-mini");
let capturedPayload: OpenAIResponsesCachePayload | undefined;
try {
const s = streamOpenAIResponses(model, context, {
apiKey: "fake-key",
cacheRetention: "none",
sessionId: "session-1",
onPayload: stopAfterPayload<OpenAIResponsesCachePayload>((payload) => {
capturedPayload = payload;
}),
});
for await (const event of s) {
if (event.type === "error") break;
}
} catch {
// Expected to fail
}
expect(capturedPayload).toBeDefined();
expect(capturedPayload?.prompt_cache_key).toBeUndefined();
expect(capturedPayload?.prompt_cache_options).toBeUndefined();
});
it("should set prompt_cache_retention when cacheRetention is long", async () => {
@@ -0,0 +1,229 @@
import type { ResponseStreamEvent } from "openai/resources/responses/responses.js";
import { Type } from "typebox";
import { describe, expect, it } from "vitest";
import { appendGrammarToolInputJsonDelta } from "../src/api/constrained-sampling.ts";
import {
convertResponsesMessages,
convertResponsesTools,
processResponsesStream,
} from "../src/api/openai-responses-shared.ts";
import type { AssistantMessage, Context, Model, Tool, ToolCall } from "../src/types.ts";
import { AssistantMessageEventStream } from "../src/utils/event-stream.ts";
function makeModel(): Model<"openai-responses"> {
return {
id: "gpt-test",
name: "GPT Test",
api: "openai-responses",
provider: "openai",
baseUrl: "https://api.openai.com/v1",
reasoning: false,
input: ["text", "image"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 128000,
maxTokens: 4096,
};
}
function makeUsage(): AssistantMessage["usage"] {
return {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
totalTokens: 0,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
};
}
function makeOutput(): AssistantMessage {
return {
role: "assistant",
content: [],
api: "openai-responses",
provider: "openai",
model: "gpt-test",
usage: makeUsage(),
stopReason: "stop",
timestamp: Date.now(),
};
}
async function* iterateEvents(events: ResponseStreamEvent[]): AsyncGenerator<ResponseStreamEvent> {
yield* events;
}
function makeTool(overrides: Partial<Tool> = {}): Tool {
return {
name: "sample_tool",
description: "Sample tool",
parameters: Type.Object({ payload: Type.String() }, { additionalProperties: false }),
...overrides,
};
}
function captureToolCallDeltas(stream: AssistantMessageEventStream): string[] {
const deltas: string[] = [];
const originalPush = stream.push.bind(stream);
stream.push = (event) => {
if (event.type === "toolcall_delta") {
deltas.push(event.delta);
}
originalPush(event);
};
return deltas;
}
describe("constrained tool sampling", () => {
it("converts supported constraints and falls back when unsupported", () => {
expect(
convertResponsesTools([makeTool({ constrainedSampling: { type: "json_schema", strict: "prefer" } })])[0],
).toMatchObject({ type: "function", name: "sample_tool", strict: true });
expect(() =>
convertResponsesTools([makeTool({ constrainedSampling: { type: "json_schema", strict: "require" } })], {
supportsStrictMode: false,
}),
).toThrow('Tool "sample_tool" requires JSON-schema constrained sampling');
const grammarTool = makeTool({
constrainedSampling: { type: "grammar", variants: { openai_lark: "start: /[a-z]+/" } },
});
expect(convertResponsesTools([grammarTool], { supportsOpenAIGrammarTools: true })[0]).toMatchObject({
type: "custom",
name: "sample_tool",
format: { type: "grammar", syntax: "lark", definition: "start: /[a-z]+/" },
});
expect(() =>
convertResponsesTools([makeTool({ constrainedSampling: { type: "grammar", variants: {} } })], {
supportsOpenAIGrammarTools: true,
}),
).toThrow(
'Tool "sample_tool" cannot use grammar constrained sampling: no supported grammar variant was provided',
);
const fallback = convertResponsesTools([grammarTool], {
supportsOpenAIGrammarTools: false,
supportsStrictMode: false,
})[0];
expect(fallback).toMatchObject({ type: "function", name: "sample_tool" });
expect("strict" in (fallback as object)).toBe(false);
expect(convertResponsesTools([makeTool({ constrainedSampling: false })])).toEqual(
convertResponsesTools([makeTool()]),
);
});
it("replays grammar calls as custom Responses items", () => {
const replayedToolCall: ToolCall = {
type: "toolCall",
id: "call_1|ctc_1",
name: "sample_tool",
arguments: { payload: "abc" },
};
const context: Context = {
messages: [
{
role: "assistant",
api: "openai-responses",
provider: "openai",
model: "gpt-test",
content: [replayedToolCall],
usage: makeUsage(),
stopReason: "toolUse",
timestamp: Date.now(),
},
{
role: "toolResult",
toolCallId: "call_1|ctc_1",
toolName: "sample_tool",
content: [{ type: "text", text: "done" }],
isError: false,
timestamp: Date.now(),
},
],
};
for (const invalidArguments of [{}, { payload: 42 }]) {
replayedToolCall.arguments = invalidArguments;
expect(() =>
convertResponsesMessages(makeModel(), context, new Set(["openai"]), {
grammarToolInputProperties: new Map([["sample_tool", "payload"]]),
}),
).toThrow('Grammar tool call "sample_tool" requires argument "payload" to be a string');
}
replayedToolCall.arguments = { payload: "abc" };
const messages = convertResponsesMessages(makeModel(), context, new Set(["openai"]), {
grammarToolInputProperties: new Map([["sample_tool", "payload"]]),
});
expect(messages).toContainEqual({
type: "custom_tool_call",
id: "ctc_1",
call_id: "call_1",
name: "sample_tool",
input: "abc",
});
expect(messages).toContainEqual({
type: "custom_tool_call_output",
call_id: "call_1",
output: "done",
});
});
it("keeps grammar input JSON deltas append-only", () => {
const buffer = { input: "", started: false, closed: false };
const first = appendGrammarToolInputJsonDelta(buffer, "payload", 'a"', false);
const second = appendGrammarToolInputJsonDelta(buffer, "payload", 'a"\nb', true);
expect(JSON.parse(`${first}${second}`)).toEqual({ payload: 'a"\nb' });
expect(appendGrammarToolInputJsonDelta(buffer, "payload", 'a"\nb', true)).toBeUndefined();
expect(() => appendGrammarToolInputJsonDelta(buffer, "payload", "changed", true)).toThrow(
'grammar tool input for property "payload" changed after it was closed',
);
});
it("streams custom Responses tool calls as string arguments", async () => {
const output = makeOutput();
const stream = new AssistantMessageEventStream();
const deltas = captureToolCallDeltas(stream);
const events = [
{
type: "response.output_item.added",
output_index: 0,
item: { type: "custom_tool_call", call_id: "call_1", id: "ctc_1", name: "sample_tool", input: "" },
},
{
type: "response.custom_tool_call_input.delta",
output_index: 0,
item_id: "ctc_1",
delta: "ab",
},
{
type: "response.custom_tool_call_input.done",
output_index: 0,
item_id: "ctc_1",
input: "abc",
},
{
type: "response.output_item.done",
output_index: 0,
item: { type: "custom_tool_call", call_id: "call_1", id: "ctc_1", name: "sample_tool", input: "abc" },
},
{
type: "response.completed",
response: { status: "completed", usage: { input_tokens: 1, output_tokens: 1, total_tokens: 2 } },
},
] as ResponseStreamEvent[];
await processResponsesStream(iterateEvents(events), output, stream, makeModel(), {
grammarToolInputProperties: new Map([["sample_tool", "payload"]]),
});
expect(output.stopReason).toBe("toolUse");
expect(output.content).toEqual([
{ type: "toolCall", id: "call_1|ctc_1", name: "sample_tool", arguments: { payload: "abc" } },
]);
expect(JSON.parse(deltas.join(""))).toEqual({ payload: "abc" });
});
});
+1
View File
@@ -369,6 +369,7 @@ describe("deferred tools", () => {
chatTemplateKwargs: {},
zaiToolStream: false,
supportsStrictMode: false,
supportsOpenAIGrammarTools: false,
cacheControlFormat: undefined,
sendSessionAffinityHeaders: false,
deferredToolsMode: "kimi",
+56
View File
@@ -5,6 +5,9 @@ const originalCopilotGitHubToken = process.env.COPILOT_GITHUB_TOKEN;
const originalGhToken = process.env.GH_TOKEN;
const originalGitHubToken = process.env.GITHUB_TOKEN;
const originalZaiCodingCnApiKey = process.env.ZAI_CODING_CN_API_KEY;
const originalAnthropicAuthToken = process.env.ANTHROPIC_AUTH_TOKEN;
const originalAnthropicOauthToken = process.env.ANTHROPIC_OAUTH_TOKEN;
const originalAnthropicApiKey = process.env.ANTHROPIC_API_KEY;
afterEach(() => {
if (originalCopilotGitHubToken === undefined) {
@@ -30,6 +33,24 @@ afterEach(() => {
} else {
process.env.ZAI_CODING_CN_API_KEY = originalZaiCodingCnApiKey;
}
if (originalAnthropicAuthToken === undefined) {
delete process.env.ANTHROPIC_AUTH_TOKEN;
} else {
process.env.ANTHROPIC_AUTH_TOKEN = originalAnthropicAuthToken;
}
if (originalAnthropicOauthToken === undefined) {
delete process.env.ANTHROPIC_OAUTH_TOKEN;
} else {
process.env.ANTHROPIC_OAUTH_TOKEN = originalAnthropicOauthToken;
}
if (originalAnthropicApiKey === undefined) {
delete process.env.ANTHROPIC_API_KEY;
} else {
process.env.ANTHROPIC_API_KEY = originalAnthropicApiKey;
}
});
describe("environment API keys", () => {
@@ -57,4 +78,39 @@ describe("environment API keys", () => {
expect(findEnvKeys("zai-coding-cn")).toEqual(["ZAI_CODING_CN_API_KEY"]);
expect(getEnvApiKey("zai-coding-cn")).toBe("zai-coding-cn-token");
});
it("reports ANTHROPIC_AUTH_TOKEN but preserves OAuth token API key lookup", () => {
process.env.ANTHROPIC_AUTH_TOKEN = "auth-token";
process.env.ANTHROPIC_OAUTH_TOKEN = "oauth-token";
process.env.ANTHROPIC_API_KEY = "api-key";
expect(findEnvKeys("anthropic")).toEqual(["ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_OAUTH_TOKEN", "ANTHROPIC_API_KEY"]);
expect(getEnvApiKey("anthropic")).toBe("oauth-token");
});
it("does not return ANTHROPIC_AUTH_TOKEN as an API key", () => {
process.env.ANTHROPIC_AUTH_TOKEN = "auth-token";
delete process.env.ANTHROPIC_OAUTH_TOKEN;
delete process.env.ANTHROPIC_API_KEY;
expect(findEnvKeys("anthropic")).toEqual(["ANTHROPIC_AUTH_TOKEN"]);
expect(getEnvApiKey("anthropic")).toBeUndefined();
});
it("preserves ANTHROPIC_OAUTH_TOKEN as an API key", () => {
delete process.env.ANTHROPIC_AUTH_TOKEN;
process.env.ANTHROPIC_OAUTH_TOKEN = "oauth-token";
delete process.env.ANTHROPIC_API_KEY;
expect(findEnvKeys("anthropic")).toEqual(["ANTHROPIC_OAUTH_TOKEN"]);
expect(getEnvApiKey("anthropic")).toBe("oauth-token");
});
it("falls back to ANTHROPIC_API_KEY for API key lookup", () => {
delete process.env.ANTHROPIC_AUTH_TOKEN;
delete process.env.ANTHROPIC_OAUTH_TOKEN;
process.env.ANTHROPIC_API_KEY = "api-key";
expect(getEnvApiKey("anthropic")).toBe("api-key");
});
});
+21
View File
@@ -64,6 +64,27 @@ describe("normalizeProviderError", () => {
expect(norm.messageCarriesBody).toBe(false);
});
it("ignores a Bedrock response stream instead of serializing its internals", () => {
const error = Object.assign(
new Error("Invocation of model ID anthropic.claude-opus-5 with on-demand throughput isn't supported."),
{
name: "ValidationException",
$metadata: { httpStatusCode: 400 },
$response: {
statusCode: 400,
body: { pipe: () => undefined, _events: { close: [null, null] } },
},
},
);
const norm = normalizeProviderError(error);
expect(norm.status).toBe(400);
expect(norm.body).toBeUndefined();
expect(norm.message).toContain("on-demand throughput isn't supported");
expect(norm.messageCarriesBody).toBe(true);
});
it("JSON-stringifies a non-Error thrown value", () => {
const norm = normalizeProviderError({ reason: "boom" });
@@ -1,5 +1,9 @@
import { describe, expect, it } from "vitest";
import { convertTools } from "../src/api/google-shared.ts";
import {
convertTools,
resolveGoogleFunctionCallingMode,
supportsGoogleStrictToolSampling,
} from "../src/api/google-shared.ts";
import type { Tool } from "../src/types.ts";
function makeTool(parameters: Record<string, unknown>): Tool {
@@ -180,6 +184,18 @@ describe("google-shared convertTools", () => {
});
});
it("uses validated function calling for strict tools on Gemini 3", () => {
const tool = makeTool({ type: "object", properties: {} });
tool.constrainedSampling = { type: "json_schema", strict: "require" };
expect(supportsGoogleStrictToolSampling("gemini-3.1-pro-preview")).toBe(true);
expect(supportsGoogleStrictToolSampling("gemini-2.5-pro")).toBe(false);
expect(resolveGoogleFunctionCallingMode([tool], undefined, true)).toBe("VALIDATED");
expect(() => resolveGoogleFunctionCallingMode([tool], undefined, false)).toThrow(
'Tool "test_tool" requires JSON-schema constrained sampling',
);
});
it("returns undefined for empty tool list", () => {
expect(convertTools([])).toBeUndefined();
expect(convertTools([], true)).toBeUndefined();
@@ -9,6 +9,7 @@ interface MistralToolPayload {
function: {
name: string;
parameters: Record<string, unknown>;
strict?: boolean;
};
}>;
}
@@ -31,6 +32,7 @@ describe("Mistral tool schema serialization", () => {
name: "inspect_schema",
description: "Inspect the schema",
parameters,
constrainedSampling: { type: "json_schema", strict: "require" },
},
],
};
@@ -45,6 +47,7 @@ describe("Mistral tool schema serialization", () => {
});
expect(capturedPayload?.tools).toHaveLength(1);
expect(capturedPayload?.tools?.[0]?.function.strict).toBe(true);
const payloadParameters = capturedPayload?.tools?.[0]?.function.parameters;
expect(payloadParameters).toBeDefined();
expect(Object.getOwnPropertySymbols(payloadParameters ?? {})).toHaveLength(0);
+12 -2
View File
@@ -11,6 +11,7 @@ import {
validateModelDataDirectory,
} from "../scripts/model-data.ts";
const GENERATED_AT = "2026-07-23T10:00:00.000Z";
const temporaryRoots: string[] = [];
afterEach(() => {
@@ -70,7 +71,7 @@ function writeFixtureData(
const filename = "test-provider.json";
const content = `${JSON.stringify({ [apiGroup]: values })}\n`;
writeFileSync(join(dataDir, filename), content);
const manifest = createModelDataManifest(structure, { [filename]: content });
const manifest = createModelDataManifest(structure, { [filename]: content }, GENERATED_AT);
manifest.schemaVersion = manifestSchemaVersion;
writeFileSync(join(dataDir, MODEL_DATA_MANIFEST_FILE), `${JSON.stringify(manifest)}\n`);
}
@@ -120,7 +121,7 @@ describe("generated model data validation", () => {
"anthropic-messages": fixture.values,
})}\n`;
writeFileSync(join(fixture.dataDir, filename), content);
const manifest = createModelDataManifest(fixture.structure, { [filename]: content });
const manifest = createModelDataManifest(fixture.structure, { [filename]: content }, GENERATED_AT);
writeFileSync(join(fixture.dataDir, MODEL_DATA_MANIFEST_FILE), `${JSON.stringify(manifest)}\n`);
expect(() => validateModelDataDirectory(fixture.structure, fixture.dataDir)).toThrow("more than one API group");
});
@@ -143,6 +144,15 @@ describe("generated model data validation", () => {
expect(() => validateModelDataDirectory(fixture.structure, fixture.dataDir)).toThrow("generation stamp");
});
it("rejects an invalid generation timestamp", () => {
const fixture = createFixture();
const manifestPath = join(fixture.dataDir, MODEL_DATA_MANIFEST_FILE);
const manifest = JSON.parse(readFileSync(manifestPath, "utf8")) as Record<string, unknown>;
manifest.generatedAt = "invalid";
writeFileSync(manifestPath, `${JSON.stringify(manifest)}\n`);
expect(() => validateModelDataDirectory(fixture.structure, fixture.dataDir)).toThrow("generation timestamp");
});
it("rejects missing provider shards imported by the aggregator", () => {
const { packageRoot } = createFixture();
writeFileSync(
+22
View File
@@ -567,6 +567,28 @@ describe("Models runtime", () => {
await expect(oauthModels.getAuth("p1")).rejects.toMatchObject({ code: "auth" });
});
it("keeps the underlying reason in wrapped oauth refresh errors", async () => {
const credentials = new InMemoryCredentialStore();
await credentials.modify("p1", async () => ({ type: "oauth", access: "old", refresh: "r", expires: 0 }));
const models = createModels({ credentials });
models.setProvider(
testProvider({
id: "p1",
auth: {
oauth: testOAuth({
refresh: async () => {
throw new Error("token refresh failed (400): invalid_grant");
},
}),
},
}),
);
await expect(models.getAuth("p1")).rejects.toThrow(
"OAuth refresh failed for p1: token refresh failed (400): invalid_grant",
);
});
it("wraps api-key auth failures in ModelsError", async () => {
const failing: ApiKeyAuth = {
name: "Failing",
+315 -36
View File
@@ -590,6 +590,57 @@ describe("openai-codex streaming", () => {
await streamResult.result();
});
it("omits SSE cache affinity when cacheRetention is none", async () => {
const token = mockToken();
const encoder = new TextEncoder();
let capturedHeaders: Headers | undefined;
let capturedBody: Record<string, unknown> | null = null;
vi.stubGlobal(
"fetch",
vi.fn(async (_input: string | URL, init?: RequestInit) => {
capturedHeaders = init?.headers instanceof Headers ? init.headers : undefined;
capturedBody = decodeCodexRequestBody(init?.body);
return new Response(
new ReadableStream<Uint8Array>({
start(controller) {
controller.enqueue(encoder.encode(buildSSEPayload({ status: "completed" })));
controller.close();
},
}),
{ status: 200, headers: { "content-type": "text/event-stream" } },
);
}),
);
const model: Model<"openai-codex-responses"> = {
id: "gpt-5.1-codex",
name: "GPT-5.1 Codex",
api: "openai-codex-responses",
provider: "openai-codex",
baseUrl: "https://chatgpt.com/backend-api",
reasoning: true,
input: ["text"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 400000,
maxTokens: 128000,
};
const context: Context = {
systemPrompt: "You are a helpful assistant.",
messages: [{ role: "user", content: "Say hello", timestamp: Date.now() }],
};
await streamOpenAICodexResponses(model, context, {
apiKey: token,
cacheRetention: "none",
sessionId: "one-off-summary",
transport: "sse",
}).result();
expect(capturedHeaders?.has("session-id")).toBe(false);
expect(capturedHeaders?.has("x-client-request-id")).toBe(false);
expect(capturedBody).not.toHaveProperty("prompt_cache_key");
});
it("clamps prompt_cache_key to OpenAI's 64-character limit", async () => {
const token = mockToken();
const sessionId = "x".repeat(67);
@@ -804,6 +855,75 @@ describe("openai-codex streaming", () => {
expect(requestedToolChoice).toBe("required");
});
it("sets Codex strict mode explicitly and honors constrained sampling", async () => {
const token = mockToken();
const encoder = new TextEncoder();
const sse = buildSSEPayload({ status: "completed" });
let requestedTools: Array<{ type?: string; name?: string; strict?: boolean | null }> | undefined;
vi.stubGlobal(
"fetch",
vi.fn(
async () =>
new Response(
new ReadableStream<Uint8Array>({
start(controller) {
controller.enqueue(encoder.encode(sse));
controller.close();
},
}),
{ status: 200, headers: { "content-type": "text/event-stream" } },
),
),
);
const model: Model<"openai-codex-responses"> = {
id: "gpt-5.5",
name: "GPT-5.5",
api: "openai-codex-responses",
provider: "openai-codex",
baseUrl: "https://chatgpt.com/backend-api",
reasoning: true,
input: ["text"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 400000,
maxTokens: 128000,
};
await streamOpenAICodexResponses(
model,
{
messages: [{ role: "user", content: "Use a tool", timestamp: Date.now() }],
tools: [
{
name: "optional",
description: "Optional constrained sampling",
parameters: Type.Object({ value: Type.String() }),
constrainedSampling: false,
},
{
name: "strict",
description: "Strict constrained sampling",
parameters: Type.Object({ value: Type.String() }, { additionalProperties: false }),
constrainedSampling: { type: "json_schema", strict: "prefer" },
},
],
},
{
apiKey: token,
transport: "sse",
onPayload: (payload) => {
requestedTools = (payload as { tools?: typeof requestedTools }).tools;
},
},
).result();
expect(requestedTools).toMatchObject([
{ type: "function", name: "optional", strict: null },
{ type: "function", name: "strict", strict: true },
]);
});
it.each(["gpt-5.3-codex", "gpt-5.4", "gpt-5.5"])("clamps %s minimal reasoning effort to low", async (modelId) => {
const tempDir = mkdtempSync(join(tmpdir(), "pi-codex-stream-"));
process.env.PI_CODING_AGENT_DIR = tempDir;
@@ -1214,6 +1334,100 @@ describe("openai-codex streaming", () => {
});
});
it("closes one-shot websockets when cacheRetention is none", async () => {
const token = mockToken();
const sentBodies: Array<{ prompt_cache_key?: string }> = [];
let connections = 0;
let closedConnections = 0;
class MockWebSocket {
private listeners = new Map<string, Set<(event: unknown) => void>>();
constructor() {
connections++;
queueMicrotask(() => this.dispatch("open", {}));
}
addEventListener(type: string, listener: (event: unknown) => void): void {
let listeners = this.listeners.get(type);
if (!listeners) {
listeners = new Set();
this.listeners.set(type, listeners);
}
listeners.add(listener);
}
removeEventListener(type: string, listener: (event: unknown) => void): void {
this.listeners.get(type)?.delete(listener);
}
send(data: string): void {
sentBodies.push(JSON.parse(data) as { prompt_cache_key?: string });
queueMicrotask(() => {
this.dispatch("message", {
data: JSON.stringify({
type: "response.completed",
response: {
id: `resp_${connections}`,
status: "completed",
usage: { input_tokens: 5, output_tokens: 3, total_tokens: 8 },
},
}),
});
});
}
close(): void {
closedConnections++;
}
private dispatch(type: string, event: unknown): void {
for (const listener of this.listeners.get(type) ?? []) {
listener(event);
}
}
}
vi.stubGlobal("WebSocket", MockWebSocket);
vi.stubGlobal(
"fetch",
vi.fn(async () => new Response("unexpected fetch", { status: 500 })),
);
const model: Model<"openai-codex-responses"> = {
id: "gpt-5.1-codex",
name: "GPT-5.1 Codex",
api: "openai-codex-responses",
provider: "openai-codex",
baseUrl: "https://chatgpt.com/backend-api",
reasoning: true,
input: ["text"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 400000,
maxTokens: 128000,
};
const context: Context = {
systemPrompt: "You are a helpful assistant.",
messages: [{ role: "user", content: "Say hello", timestamp: 1 }],
};
const options = {
apiKey: token,
cacheRetention: "none" as const,
sessionId: "one-off-summary",
transport: "auto" as const,
};
await streamOpenAICodexResponses(model, context, options).result();
await streamOpenAICodexResponses(model, context, options).result();
expect(connections).toBe(2);
expect(closedConnections).toBe(2);
expect(sentBodies).toHaveLength(2);
expect(sentBodies.every((body) => body.prompt_cache_key === undefined)).toBe(true);
expect(getOpenAICodexWebSocketDebugStats("one-off-summary")).toBeUndefined();
expect(global.fetch).not.toHaveBeenCalled();
});
it("falls back to SSE when websocket connect does not open before the connect timeout", async () => {
vi.useFakeTimers();
const token = mockToken();
@@ -1658,10 +1872,6 @@ describe("openai-codex streaming", () => {
it("sends only response input deltas in websocket-cached mode", async () => {
const token = mockToken();
const sentBodies: unknown[] = [];
const responses = [
{ responseId: "resp_1", messageId: "msg_1", text: "Hello" },
{ responseId: "resp_2", messageId: "msg_2", text: "Done" },
];
class MockWebSocket {
static OPEN = 1;
@@ -1687,36 +1897,41 @@ describe("openai-codex streaming", () => {
send(data: string): void {
sentBodies.push(JSON.parse(data));
const response = responses.shift();
if (!response) throw new Error("unexpected websocket request");
const responseId = `resp_${sentBodies.length}`;
const outputEvents =
sentBodies.length === 1
? [
{
type: "response.output_item.added",
item: {
type: "custom_tool_call",
id: "ctc_1",
call_id: "call_1",
name: "sample_tool",
input: "",
},
},
{ type: "response.custom_tool_call_input.delta", item_id: "ctc_1", delta: "abc" },
{ type: "response.custom_tool_call_input.done", item_id: "ctc_1", input: "abc" },
{
type: "response.output_item.done",
item: {
type: "custom_tool_call",
id: "ctc_1",
call_id: "call_1",
name: "sample_tool",
input: "abc",
},
},
]
: [];
const events = [
{ type: "response.created", response: { id: response.responseId } },
{
type: "response.output_item.added",
item: {
type: "message",
id: response.messageId,
role: "assistant",
status: "in_progress",
content: [],
},
},
{ type: "response.content_part.added", part: { type: "output_text", text: "" } },
{ type: "response.output_text.delta", delta: response.text },
{
type: "response.output_item.done",
item: {
type: "message",
id: response.messageId,
role: "assistant",
status: "completed",
content: [{ type: "output_text", text: response.text }],
},
},
{ type: "response.created", response: { id: responseId } },
...outputEvents,
{
type: "response.completed",
response: {
id: response.responseId,
id: responseId,
status: "completed",
usage: {
input_tokens: 5,
@@ -1758,10 +1973,19 @@ describe("openai-codex streaming", () => {
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 400000,
maxTokens: 128000,
compat: { supportsOpenAIGrammarTools: true },
};
const firstContext: Context = {
systemPrompt: "You are a helpful assistant.",
messages: [{ role: "user", content: "Say hello", timestamp: 1 }],
messages: [{ role: "user", content: "Use the tool", timestamp: 1 }],
tools: [
{
name: "sample_tool",
description: "Sample tool",
parameters: Type.Object({ payload: Type.String() }),
constrainedSampling: { type: "grammar", variants: { openai_lark: "start: /[a-z]+/" } },
},
],
};
const first = await streamOpenAICodexResponses(model, firstContext, {
@@ -1771,8 +1995,20 @@ describe("openai-codex streaming", () => {
}).result();
const secondContext: Context = {
systemPrompt: "You are a helpful assistant.",
messages: [...firstContext.messages, first, { role: "user", content: "Now finish", timestamp: 2 }],
...firstContext,
messages: [
...firstContext.messages,
first,
{
role: "toolResult",
toolCallId: "call_1|ctc_1",
toolName: "sample_tool",
content: [{ type: "text", text: "real result" }],
isError: false,
timestamp: 2,
},
{ role: "user", content: "Now finish", timestamp: 3 },
],
};
await streamOpenAICodexResponses(model, secondContext, {
apiKey: token,
@@ -1785,10 +2021,13 @@ describe("openai-codex streaming", () => {
const secondBody = sentBodies[1] as { input: unknown[]; previous_response_id?: string; store?: boolean };
expect(firstBody.store).toBe(false);
expect(firstBody.previous_response_id).toBeUndefined();
expect(firstBody.input).toEqual([{ role: "user", content: [{ type: "input_text", text: "Say hello" }] }]);
expect(firstBody.input).toEqual([{ role: "user", content: [{ type: "input_text", text: "Use the tool" }] }]);
expect(secondBody.store).toBe(false);
expect(secondBody.previous_response_id).toBe("resp_1");
expect(secondBody.input).toEqual([{ role: "user", content: [{ type: "input_text", text: "Now finish" }] }]);
expect(secondBody.input).toEqual([
{ type: "custom_tool_call_output", call_id: "call_1", output: "real result" },
{ role: "user", content: [{ type: "input_text", text: "Now finish" }] },
]);
expect(getOpenAICodexWebSocketDebugStats("session-1")).toMatchObject({
requests: 2,
connectionsCreated: 1,
@@ -1797,7 +2036,7 @@ describe("openai-codex streaming", () => {
storeTrueRequests: 0,
fullContextRequests: 1,
deltaRequests: 1,
lastDeltaInputItems: 1,
lastDeltaInputItems: 2,
lastPreviousResponseId: "resp_1",
});
});
@@ -2094,6 +2333,46 @@ describe("openai-codex streaming", () => {
expect(codexRequests).toBe(2);
});
it.each([429, 503])("fails immediately when a %i retry delay exceeds the limit", async (status) => {
const token = mockToken();
const fetchMock = vi.fn(
async () =>
new Response(JSON.stringify({ error: { code: "temporarily_unavailable", message: "retry later" } }), {
status,
headers: { "content-type": "application/json", "retry-after": "2" },
}),
);
vi.stubGlobal("fetch", fetchMock);
const model: Model<"openai-codex-responses"> = {
id: "gpt-5.1-codex",
name: "GPT-5.1 Codex",
api: "openai-codex-responses",
provider: "openai-codex",
baseUrl: "https://chatgpt.com/backend-api",
reasoning: true,
input: ["text"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 400000,
maxTokens: 128000,
};
const context: Context = {
systemPrompt: "You are a helpful assistant.",
messages: [{ role: "user", content: "Say hello", timestamp: Date.now() }],
};
const result = await streamOpenAICodexResponses(model, context, {
apiKey: token,
transport: "sse",
maxRetries: 3,
maxRetryDelayMs: 1000,
}).result();
expect(result.stopReason).toBe("error");
expect(result.errorMessage).toBe("Server requested 2s retry delay (max: 1s)");
expect(fetchMock).toHaveBeenCalledTimes(1);
});
it("zstd-compresses SSE request bodies", async () => {
const token = mockToken();
const encoder = new TextEncoder();
@@ -1,9 +1,10 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { stream as streamOpenAICompletions } from "../src/api/openai-completions.ts";
import type { Context, Model } from "../src/types.ts";
const mockState = vi.hoisted(() => ({
requestOptions: [] as unknown[],
requestErrors: [] as Error[],
}));
vi.mock("openai", () => {
@@ -30,10 +31,14 @@ vi.mock("openai", () => {
response: { status: number; headers: Headers };
}>;
};
promise.withResponse = async () => ({
data: stream,
response: { status: 200, headers: new Headers() },
});
promise.withResponse = async () => {
const error = mockState.requestErrors.shift();
if (error) throw error;
return {
data: stream,
response: { status: 200, headers: new Headers() },
};
};
return promise;
},
},
@@ -61,7 +66,7 @@ const context: Context = {
tools: [],
};
async function consume(options?: { maxRetries?: number }) {
async function consume(options?: { maxRetries?: number; maxRetryDelayMs?: number }) {
const stream = streamOpenAICompletions(model, context, { apiKey: "test", ...options });
for await (const _event of stream) {
void _event;
@@ -72,6 +77,11 @@ async function consume(options?: { maxRetries?: number }) {
describe("openai-completions provider retries", () => {
beforeEach(() => {
mockState.requestOptions = [];
mockState.requestErrors = [];
});
afterEach(() => {
vi.useRealTimers();
});
it("disables SDK retries by default", async () => {
@@ -79,8 +89,51 @@ describe("openai-completions provider retries", () => {
expect(mockState.requestOptions).toEqual([expect.objectContaining({ maxRetries: 0 })]);
});
it("honors explicit provider retry settings", async () => {
await consume({ maxRetries: 2 });
expect(mockState.requestOptions).toEqual([expect.objectContaining({ maxRetries: 2 })]);
it("honors provider retries while keeping SDK retries disabled", async () => {
vi.useFakeTimers();
mockState.requestErrors = [
Object.assign(new Error("rate limited"), {
status: 429,
headers: new Headers({ "retry-after-ms": "100" }),
}),
Object.assign(new Error("server error"), {
status: 500,
headers: new Headers({ "retry-after-ms": "100" }),
}),
];
const result = consume({ maxRetries: 2, maxRetryDelayMs: 100 });
await vi.advanceTimersByTimeAsync(0);
expect(mockState.requestOptions).toHaveLength(1);
await vi.advanceTimersByTimeAsync(99);
expect(mockState.requestOptions).toHaveLength(1);
await vi.advanceTimersByTimeAsync(1);
expect(mockState.requestOptions).toHaveLength(2);
await vi.advanceTimersByTimeAsync(99);
expect(mockState.requestOptions).toHaveLength(2);
await vi.advanceTimersByTimeAsync(1);
await result;
expect(mockState.requestOptions).toEqual([
expect.objectContaining({ maxRetries: 0 }),
expect.objectContaining({ maxRetries: 0 }),
expect.objectContaining({ maxRetries: 0 }),
]);
});
it("fails immediately when a provider-requested retry delay exceeds the limit", async () => {
mockState.requestErrors = [
Object.assign(new Error("rate limited"), {
status: 429,
headers: new Headers({ "retry-after": "277403" }),
}),
];
const result = await consume({ maxRetries: 2, maxRetryDelayMs: 1000 });
expect(result.stopReason).toBe("error");
expect(result.errorMessage).toContain("Server requested 277403s retry delay (max: 1s)");
expect(result.errorMessage).toContain("rate limited");
expect(mockState.requestOptions).toEqual([expect.objectContaining({ maxRetries: 0 })]);
});
});
@@ -37,6 +37,7 @@ const compat = {
chatTemplateKwargs: {},
zaiToolStream: false,
supportsStrictMode: true,
supportsOpenAIGrammarTools: false,
cacheControlFormat: undefined,
sendSessionAffinityHeaders: false,
sessionAffinityFormat: "openai",
@@ -1258,6 +1258,7 @@ describe("openai-completions tool_choice", () => {
chatTemplateKwargs: {},
zaiToolStream: false,
supportsStrictMode: true,
supportsOpenAIGrammarTools: false,
sendSessionAffinityHeaders: false,
sessionAffinityFormat: "openai",
supportsLongCacheRetention: true,
@@ -37,6 +37,7 @@ const compat: Omit<Required<OpenAICompletionsCompat>, "deferredToolsMode"> & {
chatTemplateKwargs: {},
zaiToolStream: false,
supportsStrictMode: true,
supportsOpenAIGrammarTools: false,
cacheControlFormat: "anthropic",
sendSessionAffinityHeaders: false,
sessionAffinityFormat: "openai",
@@ -90,7 +90,7 @@ describe.skipIf(!process.env.OPENAI_API_KEY || !process.env.ANTHROPIC_API_KEY)(
// 6. With fix: tool calls/results converted to text, conversation continues
const modelA = getModel("openai", "gpt-5-mini");
const modelB = getModel("openai", "gpt-5.2-codex");
const modelB = getModel("openai", "gpt-5.5");
const apiKey = getEnvApiKey("openai");
if (!apiKey) {
@@ -189,7 +189,7 @@ describe.skipIf(!process.env.OPENAI_API_KEY || !process.env.ANTHROPIC_API_KEY)(
// 5. Should work because foreign IDs have no pairing expectation
const anthropicModel = getModel("anthropic", "claude-sonnet-4-5");
const codexModel = getModel("openai", "gpt-5.2-codex");
const codexModel = getModel("openai", "gpt-5.5");
const anthropicApiKey = getEnvApiKey("anthropic");
const openaiApiKey = getEnvApiKey("openai");
@@ -186,4 +186,28 @@ describe("provider error body passthrough (per-tier regression)", () => {
expect(output.errorMessage).toContain("blocked by gateway WAF");
expect(output.errorMessage).not.toContain("Unknown: UnknownError");
});
it("bedrock preserves the SDK validation message when the response body is a stream", async () => {
bedrockMock.sendError = Object.assign(
new Error(
"Invocation of model ID anthropic.claude-opus-5 with on-demand throughput isn't supported. Retry with an inference profile.",
),
{
name: "ValidationException",
$metadata: { httpStatusCode: 400 },
$response: {
statusCode: 400,
body: { pipe: () => undefined, _readableState: { buffer: [], length: 0 } },
},
},
);
const model = getModel("amazon-bedrock", "global.anthropic.claude-opus-5");
const output = await drainResult(streamSimpleBedrock(model, { messages: context.messages }, {}));
expect(output.stopReason).toBe("error");
expect(output.errorMessage).toContain("on-demand throughput isn't supported");
expect(output.errorMessage).toContain("inference profile");
expect(output.errorMessage).not.toContain("_readableState");
});
});
+81
View File
@@ -0,0 +1,81 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { retryProviderRequest } from "../src/utils/provider-retry.ts";
function providerError(status: number | undefined, headers?: Record<string, string>): Error {
return Object.assign(new Error(`Provider error: ${status}`), {
status,
headers: new Headers(headers),
});
}
describe("provider request retries", () => {
afterEach(() => {
vi.useRealTimers();
});
it("retries retryable provider errors", async () => {
vi.useFakeTimers();
const request = vi
.fn<() => Promise<string>>()
.mockRejectedValueOnce(providerError(429, { "retry-after-ms": "1000" }))
.mockResolvedValue("ok");
const result = retryProviderRequest(request, { maxRetries: 1 });
await vi.advanceTimersByTimeAsync(999);
expect(request).toHaveBeenCalledTimes(1);
await vi.advanceTimersByTimeAsync(1);
await expect(result).resolves.toBe("ok");
expect(request).toHaveBeenCalledTimes(2);
});
it("does not retry errors the provider marks as non-retryable", async () => {
const error = providerError(429, { "x-should-retry": "false" });
const request = vi.fn<() => Promise<string>>().mockRejectedValue(error);
await expect(retryProviderRequest(request, { maxRetries: 2 })).rejects.toBe(error);
expect(request).toHaveBeenCalledTimes(1);
});
it("rejects a provider-requested retry delay above the limit", async () => {
const request = vi.fn<() => Promise<string>>().mockRejectedValue(providerError(429, { "retry-after": "277403" }));
await expect(retryProviderRequest(request, { maxRetries: 1, maxRetryDelayMs: 1000 })).rejects.toThrow(
"Server requested 277403s retry delay (max: 1s)",
);
expect(request).toHaveBeenCalledTimes(1);
});
it("allows disabling the provider-requested retry delay cap", async () => {
vi.useFakeTimers();
const request = vi
.fn<() => Promise<string>>()
.mockRejectedValueOnce(providerError(429, { "retry-after": "2" }))
.mockResolvedValue("ok");
const result = retryProviderRequest(request, { maxRetries: 1, maxRetryDelayMs: 0 });
await vi.advanceTimersByTimeAsync(1999);
expect(request).toHaveBeenCalledTimes(1);
await vi.advanceTimersByTimeAsync(1);
await expect(result).resolves.toBe("ok");
expect(request).toHaveBeenCalledTimes(2);
});
it("aborts a provider-requested retry delay", async () => {
vi.useFakeTimers();
const controller = new AbortController();
const request = vi.fn<() => Promise<string>>().mockRejectedValue(providerError(429, { "retry-after": "277403" }));
const result = retryProviderRequest(request, { maxRetries: 2, maxRetryDelayMs: 0, signal: controller.signal });
await vi.advanceTimersByTimeAsync(0);
expect(request).toHaveBeenCalledTimes(1);
expect(vi.getTimerCount()).toBe(1);
controller.abort();
await expect(result).rejects.toMatchObject({ name: "AbortError" });
expect(request).toHaveBeenCalledTimes(1);
expect(vi.getTimerCount()).toBe(0);
});
});
+30 -4
View File
@@ -3,7 +3,7 @@ import { envApiKeyAuth } from "../src/auth/helpers.ts";
import type { AuthContext, AuthEvent } from "../src/auth/types.ts";
import { createModels, createProvider } from "../src/models.ts";
import { InMemoryModelsStore, type ModelsStoreEntry } from "../src/models-store.ts";
import { builtinModels, builtinProviders } from "../src/providers/all.ts";
import { builtinModels, builtinProviders, getBuiltinModel } from "../src/providers/all.ts";
import { amazonBedrockProvider } from "../src/providers/amazon-bedrock.ts";
import { anthropicProvider } from "../src/providers/anthropic.ts";
import { cloudflareAIGatewayProvider } from "../src/providers/cloudflare-ai-gateway.ts";
@@ -44,6 +44,17 @@ describe("builtin providers", () => {
}
});
it("stores native constrained-sampling capabilities in model metadata", () => {
const gpt4o = getBuiltinModel("openai", "gpt-4o");
expect(gpt4o.compat?.supportsStrictMode).toBe(true);
expect(gpt4o.compat?.supportsOpenAIGrammarTools).toBeUndefined();
expect(getBuiltinModel("openai", "gpt-5.4").compat).toMatchObject({
supportsStrictMode: true,
supportsOpenAIGrammarTools: true,
});
expect(getBuiltinModel("anthropic", "claude-haiku-4-5").compat?.supportsStrictTools).toBe(true);
});
it("uses official Kimi K3 pricing for Moonshot providers", () => {
const models = builtinModels();
for (const provider of ["moonshotai", "moonshotai-cn"]) {
@@ -68,14 +79,29 @@ describe("builtin providers", () => {
}
});
it("resolves anthropic auth from env with OAuth token precedence", async () => {
it("resolves Anthropic bearer auth from env with auth token precedence", async () => {
const models = createModels({
authContext: fakeAuthContext({
ANTHROPIC_AUTH_TOKEN: "auth-token",
ANTHROPIC_OAUTH_TOKEN: "oauth-token",
ANTHROPIC_API_KEY: "api-key",
}),
});
models.setProvider(anthropicProvider());
expect(await models.getAuth("anthropic")).toEqual({
auth: { headers: { Authorization: "Bearer auth-token" } },
source: "ANTHROPIC_AUTH_TOKEN",
});
});
it("preserves Anthropic OAuth token precedence over the API key", async () => {
const models = createModels({
authContext: fakeAuthContext({ ANTHROPIC_API_KEY: "key", ANTHROPIC_OAUTH_TOKEN: "oauth-token" }),
});
models.setProvider(anthropicProvider());
const model = models.getModel("anthropic", "claude-haiku-4-5")!;
const result = await models.getAuth(model.provider);
const result = await models.getAuth("anthropic");
expect(result?.auth.apiKey).toBe("oauth-token");
expect(result?.source).toBe("ANTHROPIC_OAUTH_TOKEN");
});
+125
View File
@@ -0,0 +1,125 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { createRadiusOAuth } from "../src/auth/oauth/radius.ts";
import type { AuthEvent, AuthInteraction } from "../src/auth/types.ts";
const GATEWAY = "https://radius.example";
function jsonResponse(body: unknown, status = 200): Response {
return new Response(JSON.stringify(body), {
status,
headers: { "content-type": "application/json" },
});
}
function requestUrl(input: unknown): string {
if (typeof input === "string") return input;
if (input instanceof URL) return input.toString();
if (input instanceof Request) return input.url;
throw new Error(`Unsupported request input: ${String(input)}`);
}
function interaction(loginMethod: "browser" | "device-code", events: AuthEvent[] = []): AuthInteraction {
return {
prompt: async () => loginMethod,
notify: (event) => events.push(event),
};
}
describe("Radius OAuth", () => {
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
vi.useRealTimers();
});
it("uses gateway endpoints directly for device login", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-07-24T00:00:00Z"));
const events: AuthEvent[] = [];
const urls: string[] = [];
vi.stubGlobal(
"fetch",
vi.fn(async (input: unknown, init?: RequestInit) => {
const url = requestUrl(input);
urls.push(url);
const form = new URLSearchParams(String(init?.body));
if (url === `${GATEWAY}/v1/oauth/device`) {
expect(form.get("client_id")).toBe("pi-gateway");
expect(form.get("scope")).toBe("gateway offline_access");
return jsonResponse({
device_code: "device-code",
user_code: "ABCD-1234",
verification_uri: "https://radius-ui.example/pair",
expires_in: 600,
interval: 5,
});
}
if (url === `${GATEWAY}/v1/oauth/token`) {
expect(form.get("grant_type")).toBe("urn:ietf:params:oauth:grant-type:device_code");
expect(form.get("client_id")).toBe("pi-gateway");
expect(form.get("device_code")).toBe("device-code");
return jsonResponse({
access_token: "access-token",
refresh_token: "refresh-token",
expires_in: 3600,
scope: "gateway offline_access",
});
}
throw new Error(`Unexpected request: ${url}`);
}),
);
const oauth = createRadiusOAuth({ name: "Radius", gateway: GATEWAY });
await expect(oauth.login(interaction("device-code", events))).resolves.toEqual({
type: "oauth",
access: "access-token",
refresh: "refresh-token",
expires: Date.now() + 3600 * 1000 - 60_000,
scope: "gateway offline_access",
});
expect(events).toEqual([
{
type: "device_code",
userCode: "ABCD-1234",
verificationUri: "https://radius-ui.example/pair",
intervalSeconds: 5,
expiresInSeconds: 600,
},
]);
expect(urls).toEqual([`${GATEWAY}/v1/oauth/device`, `${GATEWAY}/v1/oauth/token`]);
});
it("refreshes directly through the gateway without discovery", async () => {
const fetchMock = vi.fn(async (input: unknown, init?: RequestInit) => {
expect(requestUrl(input)).toBe(`${GATEWAY}/v1/oauth/token`);
const form = new URLSearchParams(String(init?.body));
expect(form.get("grant_type")).toBe("refresh_token");
expect(form.get("client_id")).toBe("pi-gateway");
expect(form.get("refresh_token")).toBe("old-refresh");
return jsonResponse({
access_token: "new-access",
refresh_token: "new-refresh",
expires_in: 3600,
});
});
vi.stubGlobal("fetch", fetchMock);
const oauth = createRadiusOAuth({ name: "Radius", gateway: GATEWAY });
await expect(
oauth.refresh({ type: "oauth", access: "old-access", refresh: "old-refresh", expires: 0 }),
).resolves.toMatchObject({ access: "new-access", refresh: "new-refresh" });
expect(fetchMock).toHaveBeenCalledOnce();
});
it("discovers only the interactive browser authorization endpoint", async () => {
const fetchMock = vi.fn(async (input: unknown) => {
expect(requestUrl(input)).toBe(`${GATEWAY}/v1/oauth`);
return jsonResponse({ issuer: "https://radius-ui.example" });
});
vi.stubGlobal("fetch", fetchMock);
const oauth = createRadiusOAuth({ name: "Radius", gateway: GATEWAY });
await expect(oauth.login(interaction("browser"))).rejects.toThrow(`Invalid Radius OAuth config from ${GATEWAY}`);
expect(fetchMock).toHaveBeenCalledOnce();
});
});
+14
View File
@@ -16,6 +16,13 @@ describe("getSupportedThinkingLevels", () => {
expect(getSupportedThinkingLevels(model!)).toContain("max");
});
it("includes xhigh and max for Anthropic Opus 5 on anthropic-messages API", () => {
const model = getModel("anthropic", "claude-opus-5");
expect(model).toBeDefined();
expect(getSupportedThinkingLevels(model!)).toContain("xhigh");
expect(getSupportedThinkingLevels(model!)).toContain("max");
});
it("includes max but not xhigh for Anthropic Sonnet 4.6 on anthropic-messages API", () => {
const model = getModel("anthropic", "claude-sonnet-4-6");
expect(model).toBeDefined();
@@ -133,6 +140,13 @@ describe("getSupportedThinkingLevels", () => {
expect(getSupportedThinkingLevels(model!)).not.toContain("xhigh");
});
it("includes xhigh and max for Bedrock Claude Opus 5", () => {
const model = getModel("amazon-bedrock", "global.anthropic.claude-opus-5");
expect(model).toBeDefined();
expect(getSupportedThinkingLevels(model!)).toContain("xhigh");
expect(getSupportedThinkingLevels(model!)).toContain("max");
});
it("includes xhigh and max but not off for Bedrock Claude Fable 5", () => {
const model = getModel("amazon-bedrock", "global.anthropic.claude-fable-5");
expect(model).toBeDefined();
+2 -2
View File
@@ -15,10 +15,10 @@ function makeContext(): Context {
}
describe.skipIf(!process.env.OPENAI_API_KEY)("xhigh reasoning", () => {
describe("codex-max (supports xhigh)", () => {
describe("gpt 5.5 (supports xhigh)", () => {
// Note: codex models only support the responses API, not chat completions
it("should work with openai-responses", async () => {
const model = getModel("openai", "gpt-5.1-codex-max");
const model = getModel("openai", "gpt-5.5");
const s = stream(model, makeContext(), { reasoningEffort: "xhigh" });
let hasThinking = false;