mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-16 19:51:11 +00:00
Merged via squash.
Prepared head SHA: 7d58befec8
Co-authored-by: openperf <80630709+openperf@users.noreply.github.com>
Co-authored-by: jalehman <550978+jalehman@users.noreply.github.com>
Reviewed-by: @jalehman
219 lines
7.4 KiB
TypeScript
219 lines
7.4 KiB
TypeScript
import type { StreamFn } from "@mariozechner/pi-agent-core";
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import {
|
|
__testing,
|
|
createAnthropicBetaHeadersWrapper,
|
|
createAnthropicFastModeWrapper,
|
|
createAnthropicServiceTierWrapper,
|
|
wrapAnthropicProviderStream,
|
|
} from "./stream-wrappers.js";
|
|
|
|
const CONTEXT_1M_BETA = "context-1m-2025-08-07";
|
|
const OAUTH_BETA = "oauth-2025-04-20";
|
|
|
|
function runWrapper(apiKey: string | undefined): Record<string, string> | undefined {
|
|
const captured: { headers?: Record<string, string> } = {};
|
|
const base: StreamFn = (_model, _context, options) => {
|
|
captured.headers = options?.headers;
|
|
return {} as never;
|
|
};
|
|
const wrapper = createAnthropicBetaHeadersWrapper(base, [CONTEXT_1M_BETA]);
|
|
wrapper(
|
|
{ provider: "anthropic", id: "claude-opus-4-6" } as never,
|
|
{} as never,
|
|
{ apiKey } as never,
|
|
);
|
|
return captured.headers;
|
|
}
|
|
|
|
describe("anthropic stream wrappers", () => {
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
it("strips context-1m for Claude CLI or legacy token auth and warns", () => {
|
|
const warn = vi.spyOn(__testing.log, "warn").mockImplementation(() => undefined);
|
|
const headers = runWrapper("sk-ant-oat01-123");
|
|
expect(headers?.["anthropic-beta"]).toBeDefined();
|
|
expect(headers?.["anthropic-beta"]).toContain(OAUTH_BETA);
|
|
expect(headers?.["anthropic-beta"]).not.toContain(CONTEXT_1M_BETA);
|
|
expect(warn).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("keeps context-1m for API key auth", () => {
|
|
const warn = vi.spyOn(__testing.log, "warn").mockImplementation(() => undefined);
|
|
const headers = runWrapper("sk-ant-api-123");
|
|
expect(headers?.["anthropic-beta"]).toBeDefined();
|
|
expect(headers?.["anthropic-beta"]).toContain(CONTEXT_1M_BETA);
|
|
expect(warn).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("skips service_tier for OAuth token in composed stream chain", () => {
|
|
const captured: { headers?: Record<string, string>; payload?: Record<string, unknown> } = {};
|
|
const base: StreamFn = (model, _context, options) => {
|
|
captured.headers = options?.headers;
|
|
const payload = {} as Record<string, unknown>;
|
|
options?.onPayload?.(payload as never, model as never);
|
|
captured.payload = payload;
|
|
return {} as never;
|
|
};
|
|
|
|
const wrapped = wrapAnthropicProviderStream({
|
|
streamFn: base,
|
|
modelId: "claude-sonnet-4-6",
|
|
extraParams: { context1m: true, serviceTier: "auto" },
|
|
} as never);
|
|
|
|
wrapped?.(
|
|
{ provider: "anthropic", api: "anthropic-messages", id: "claude-sonnet-4-6" } as never,
|
|
{} as never,
|
|
{ apiKey: "sk-ant-oat01-oauth-token" } as never,
|
|
);
|
|
|
|
expect(captured.headers?.["anthropic-beta"]).toContain(OAUTH_BETA);
|
|
expect(captured.headers?.["anthropic-beta"]).not.toContain(CONTEXT_1M_BETA);
|
|
expect(captured.payload?.service_tier).toBeUndefined();
|
|
});
|
|
|
|
it("composes the anthropic provider stream chain from extra params", () => {
|
|
const captured: { headers?: Record<string, string>; payload?: Record<string, unknown> } = {};
|
|
const base: StreamFn = (model, _context, options) => {
|
|
captured.headers = options?.headers;
|
|
const payload = {} as Record<string, unknown>;
|
|
options?.onPayload?.(payload as never, model as never);
|
|
captured.payload = payload;
|
|
return {} as never;
|
|
};
|
|
|
|
const wrapped = wrapAnthropicProviderStream({
|
|
streamFn: base,
|
|
modelId: "claude-sonnet-4-6",
|
|
extraParams: { context1m: true, serviceTier: "auto" },
|
|
} as never);
|
|
|
|
wrapped?.(
|
|
{ provider: "anthropic", api: "anthropic-messages", id: "claude-sonnet-4-6" } as never,
|
|
{} as never,
|
|
{ apiKey: "sk-ant-api-123" } as never,
|
|
);
|
|
|
|
expect(captured.headers?.["anthropic-beta"]).toContain(CONTEXT_1M_BETA);
|
|
expect(captured.payload).toMatchObject({ service_tier: "auto" });
|
|
});
|
|
});
|
|
|
|
describe("createAnthropicFastModeWrapper", () => {
|
|
function runFastModeWrapper(params: {
|
|
apiKey?: string;
|
|
provider?: string;
|
|
api?: string;
|
|
baseUrl?: string;
|
|
enabled?: boolean;
|
|
}): Record<string, unknown> | undefined {
|
|
const captured: { payload?: Record<string, unknown> } = {};
|
|
const base: StreamFn = (_model, _context, options) => {
|
|
if (options?.onPayload) {
|
|
const payload: Record<string, unknown> = {};
|
|
options.onPayload(payload, _model);
|
|
captured.payload = payload;
|
|
}
|
|
return {} as never;
|
|
};
|
|
|
|
const wrapper = createAnthropicFastModeWrapper(base, params.enabled ?? true);
|
|
wrapper(
|
|
{
|
|
provider: params.provider ?? "anthropic",
|
|
api: params.api ?? "anthropic-messages",
|
|
baseUrl: params.baseUrl,
|
|
id: "claude-sonnet-4-6",
|
|
} as never,
|
|
{} as never,
|
|
{ apiKey: params.apiKey } as never,
|
|
);
|
|
return captured.payload;
|
|
}
|
|
|
|
it("does not inject service_tier for OAuth token", () => {
|
|
const payload = runFastModeWrapper({ apiKey: "sk-ant-oat01-test-token" });
|
|
expect(payload?.service_tier).toBeUndefined();
|
|
});
|
|
|
|
it("injects service_tier for regular API keys", () => {
|
|
const payload = runFastModeWrapper({ apiKey: "sk-ant-api03-test-key" });
|
|
expect(payload?.service_tier).toBe("auto");
|
|
});
|
|
|
|
it("injects service_tier=standard_only when disabled for API keys", () => {
|
|
const payload = runFastModeWrapper({ apiKey: "sk-ant-api03-test-key", enabled: false });
|
|
expect(payload?.service_tier).toBe("standard_only");
|
|
});
|
|
|
|
it("does not inject service_tier for non-anthropic provider", () => {
|
|
const payload = runFastModeWrapper({
|
|
apiKey: "sk-ant-api03-test-key",
|
|
provider: "openai",
|
|
api: "openai-completions",
|
|
});
|
|
expect(payload?.service_tier).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe("createAnthropicServiceTierWrapper", () => {
|
|
function runServiceTierWrapper(params: {
|
|
apiKey?: string;
|
|
provider?: string;
|
|
api?: string;
|
|
serviceTier?: "auto" | "standard_only";
|
|
}): Record<string, unknown> | undefined {
|
|
const captured: { payload?: Record<string, unknown> } = {};
|
|
const base: StreamFn = (_model, _context, options) => {
|
|
if (options?.onPayload) {
|
|
const payload: Record<string, unknown> = {};
|
|
options.onPayload(payload, _model);
|
|
captured.payload = payload;
|
|
}
|
|
return {} as never;
|
|
};
|
|
|
|
const wrapper = createAnthropicServiceTierWrapper(base, params.serviceTier ?? "auto");
|
|
wrapper(
|
|
{
|
|
provider: params.provider ?? "anthropic",
|
|
api: params.api ?? "anthropic-messages",
|
|
id: "claude-sonnet-4-6",
|
|
} as never,
|
|
{} as never,
|
|
{ apiKey: params.apiKey } as never,
|
|
);
|
|
return captured.payload;
|
|
}
|
|
|
|
it("does not inject service_tier for OAuth token", () => {
|
|
const payload = runServiceTierWrapper({ apiKey: "sk-ant-oat01-test-token" });
|
|
expect(payload?.service_tier).toBeUndefined();
|
|
});
|
|
|
|
it("injects service_tier for regular API keys", () => {
|
|
const payload = runServiceTierWrapper({ apiKey: "sk-ant-api03-test-key" });
|
|
expect(payload?.service_tier).toBe("auto");
|
|
});
|
|
|
|
it("injects service_tier=standard_only for regular API keys", () => {
|
|
const payload = runServiceTierWrapper({
|
|
apiKey: "sk-ant-api03-test-key",
|
|
serviceTier: "standard_only",
|
|
});
|
|
expect(payload?.service_tier).toBe("standard_only");
|
|
});
|
|
|
|
it("does not inject service_tier for non-anthropic provider", () => {
|
|
const payload = runServiceTierWrapper({
|
|
apiKey: "sk-ant-api03-test-key",
|
|
provider: "openai",
|
|
api: "openai-completions",
|
|
});
|
|
expect(payload?.service_tier).toBeUndefined();
|
|
});
|
|
});
|