mirror of
https://github.com/hansjone/oclaw.git
synced 2026-10-17 09:44:01 +08:00
重构主控编排与运行时预热链路,统一工作区提示词/专家调度协议并补齐 wiki 记忆注入与写回闭环。
同时收敛启动与运维脚本默认行为(含 wiki worker)、更新 Admin 可观测性与相关测试,降低首轮时延并提高运行稳定性。 Made-with: Cursor
This commit is contained in:
parent
4a23b715a2
commit
dbbe3add6a
14438 changed files with 2693620 additions and 2546 deletions
218
openclaw/extensions/anthropic/stream-wrappers.test.ts
Normal file
218
openclaw/extensions/anthropic/stream-wrappers.test.ts
Normal file
|
|
@ -0,0 +1,218 @@
|
|||
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]);
|
||||
void 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);
|
||||
|
||||
void 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);
|
||||
|
||||
void 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);
|
||||
void 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");
|
||||
void 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();
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue