mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-02 15:41:03 +00:00
* refactor: extract agent core package Introduce packages/agent-core as the OpenClaw-owned home for reusable agent loop, harness, session, prompt, and runtime dependency contracts. * refactor: extract shared llm runtime Move provider model registries, stream wrappers, OAuth helpers, and LLM utilities into src/llm with plugin-sdk barrels instead of depending on the old embedded runtime layout. * refactor: remove pi runtime internals Rename remaining Pi-shaped agent surfaces to OpenClaw agent runtime names, delete obsolete Pi docs and package graph checks, and add the third-party notice for incorporated code. * refactor: tighten agent session runtime Make agent-core/runtime dependencies explicit, consolidate compaction and session transcript helpers, and move model/session helpers behind OpenClaw-owned contracts. * refactor: remove static model and pi auth paths Drop static model catalogs and Pi auth bridges, move model/provider facts to manifest-owned runtime contracts, and harden internal embedded-agent utilities. * refactor: remove legacy provider compat paths * docs: remove agent parity notes * fix: skip provider wildcard metadata parsing * refactor: share session extension sdk loading * refactor: inline acpx proxy error formatter * refactor: fold edit recovery into edit tool * fix: accept extension batch separator * test: align startup provider plugin expectations * fix: restore provider-scoped release discovery * test: align static asset packaging expectations * fix: run static provider catalogs during scoped discovery * fix: add provider entry catalogs for scoped live discovery * fix: load lightweight provider catalog entries * fix: refresh provider-scoped plugin metadata * fix: keep provider catalog entries on release live path * fix: keep static manifest models in release live checks * fix: harden release model discovery * fix: reduce OpenAI live cache probe reasoning * fix: disable OpenAI cache probe reasoning * ci: extend OpenAI gateway live timeout * fix: extend live gateway model budget * fix: stabilize release validation regressions * fix: honor provider aliases in model rows * fix: stabilize release validation lanes * fix: stabilize release memory qa * ci: stabilize release validation lanes * ci: prefer ipv4 for live docker node calls * fix: restore shared tool-call stream wrapper * ci: remove legacy pi test shard alias * fix: clean up embedded agent test drift * fix: stabilize runtime alias status * fix: clean up embedded agent ci drift * fix: restore release ci invariants * fix: clean up post-rebase runtime drift * fix: restore release ci checks * fix: restore release ci after rebase * fix: remove stale pi runtime path * test: align compaction runtime expectations * test: update plugin prerelease expectations * fix: handle claude live tool approvals * fix: stabilize release validation gates * fix: finish agent runtime import * test: finish post-rebase agent runtime mocks * fix: keep codex compaction native * fix: stabilize codex app-server hook tests * test: isolate codex diagnostic active run * test: remove codex diagnostic completion race # Conflicts: # extensions/codex/src/app-server/run-attempt.test.ts * ci: fix full release manifest performance run id * refactor: narrow llm plugin sdk boundary * chore: drop generated google boundary stamps * fix: repair rebase fallout * fix: clean up rebased runtime references * fix: decode codex jwt payloads as base64url * fix: preserve shipped pi runtime alias * fix: add scoped sdk virtual modules * fix: decode llm codex oauth jwt as base64url * fix: avoid stale vertex adc negative cache * fix: harden tool arg decoding and codeql path * fix: keep vertex adc negative checks live * refactor: consolidate codex jwt and edit helpers * fix: await codex oauth node runtime imports * fix: preserve sdk tool and notice contracts * fix: preserve shipped compat config boundaries * fix: align codex oauth callback host * fix: terminate agent-core loop streams on failure * fix: keep codex oauth callback alive during fallback * ci: include session tools in critical codeql scans * fix: keep Cloudflare Anthropic provider auth header * docs: redirect legacy pi runtime pages * fix: honor bundled web provider compat discovery * fix: protect session output spill files * fix: keep legacy agent dir env blocked * fix: contain auto-discovered skill symlinks * fix: harden agent core sdk proxy surfaces * fix: restore approval reaction sdk compat * fix: keep live docker runs bounded * fix: keep codex oauth redirect host aligned * fix: resolve post-rebase agent runtime drift * fix: redact anthropic oauth parse failures * fix: preserve responses strict tool shaping * fix: repair agent runtime rebase cleanup * docs: redirect retired parity pages * fix: bound auto-discovered resources to roots * fix: repair post-rebase agent test drift * fix: preserve bundled provider allowlist migration * fix: preserve manifest-owned provider aliases * fix: declare photon image dependency * fix: keep provider headers out of proxy body * fix: preserve shipped env aliases * fix: refresh control ui i18n generated state * fix: quote read fallback paths * fix: preview edits through configured backend * test: satisfy core test typecheck * fix: preserve ZAI usage auth fallback * test: repair codex diagnostic test * fix: repair agent runtime rebase drift * test: finish embedded runner import rename * fix: repair agent runtime rebase integrations * test: align compaction oauth fallback expectations * fix: allow sdk-auth session models * fix: update doctor tool schema import * fix: preserve bedrock plugin region * fix: stream harmony-like prose immediately * ci: include session runtime in codeql shards * fix: repair latest rebase integrations * fix: honor explicit codex websocket transport * fix: keep openai-compatible credentials provider-scoped * fix: refresh sdk api baseline after rebase * fix: route cli runtime aliases through openclaw harness * test: rename stale harness mock expectation * test: rename embedded agent overflow calls * test: clean embedded auth test wording * test: use openclaw stream types in deepinfra cache test * fix: refresh sdk api baseline on latest main * fix: honor bundled discovery compat allowlists * fix: refresh sdk api baseline after latest rebase * fix: remove stale rebase imports * test: rename stale model catalog mock * test: mock renamed doctor runtime modules * fix: map canonical kimi env auth * fix: use internal model registry in bench script * fix: migrate deepinfra provider catalog entry * fix: enforce builtin tool suppression * fix: route compaction auth and proxy payloads safely * refactor: prune unused llm registry leftovers * test: update codex hooks session import * test: fix model picker ci coverage * test: align model picker auth mock types
408 lines
10 KiB
TypeScript
408 lines
10 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
deriveContextPromptTokens,
|
|
derivePromptTokens,
|
|
deriveSessionTotalTokens,
|
|
hasNonzeroUsage,
|
|
normalizeUsage,
|
|
toOpenAiChatCompletionsUsage,
|
|
} from "./usage.js";
|
|
|
|
describe("normalizeUsage", () => {
|
|
it("normalizes cache fields from provider response", () => {
|
|
const usage = normalizeUsage({
|
|
input: 1000,
|
|
output: 500,
|
|
cacheRead: 2000,
|
|
cacheWrite: 300,
|
|
});
|
|
expect(usage).toEqual({
|
|
input: 1000,
|
|
output: 500,
|
|
cacheRead: 2000,
|
|
cacheWrite: 300,
|
|
total: undefined,
|
|
});
|
|
});
|
|
|
|
it("normalizes cache fields from alternate naming", () => {
|
|
const usage = normalizeUsage({
|
|
input_tokens: 1000,
|
|
output_tokens: 500,
|
|
cache_read_input_tokens: 2000,
|
|
cache_creation_input_tokens: 300,
|
|
});
|
|
expect(usage).toEqual({
|
|
input: 1000,
|
|
output: 500,
|
|
cacheRead: 2000,
|
|
cacheWrite: 300,
|
|
total: undefined,
|
|
});
|
|
});
|
|
|
|
it("handles cache_read and cache_write naming variants", () => {
|
|
const usage = normalizeUsage({
|
|
input: 1000,
|
|
cache_read: 1500,
|
|
cache_write: 200,
|
|
});
|
|
expect(usage).toEqual({
|
|
input: 1000,
|
|
output: undefined,
|
|
cacheRead: 1500,
|
|
cacheWrite: 200,
|
|
total: undefined,
|
|
});
|
|
});
|
|
|
|
it("handles Moonshot/Kimi cached_tokens field", () => {
|
|
// Moonshot v1 returns cached_tokens instead of cache_read_input_tokens
|
|
const usage = normalizeUsage({
|
|
prompt_tokens: 30,
|
|
completion_tokens: 9,
|
|
total_tokens: 39,
|
|
cached_tokens: 19,
|
|
});
|
|
expect(usage).toEqual({
|
|
input: 11,
|
|
output: 9,
|
|
cacheRead: 19,
|
|
cacheWrite: undefined,
|
|
total: 39,
|
|
});
|
|
});
|
|
|
|
it("handles Kimi K2 prompt_tokens_details.cached_tokens field", () => {
|
|
// Kimi K2 uses automatic prefix caching and returns cached_tokens in prompt_tokens_details
|
|
const usage = normalizeUsage({
|
|
prompt_tokens: 1113,
|
|
completion_tokens: 5,
|
|
total_tokens: 1118,
|
|
prompt_tokens_details: { cached_tokens: 1024 },
|
|
});
|
|
expect(usage).toEqual({
|
|
input: 89,
|
|
output: 5,
|
|
cacheRead: 1024,
|
|
cacheWrite: undefined,
|
|
total: 1118,
|
|
});
|
|
});
|
|
|
|
it("handles OpenAI Responses input_tokens_details.cached_tokens field", () => {
|
|
const usage = normalizeUsage({
|
|
input_tokens: 120,
|
|
output_tokens: 30,
|
|
total_tokens: 250,
|
|
input_tokens_details: { cached_tokens: 100 },
|
|
output_tokens_details: { reasoning_tokens: 17 },
|
|
});
|
|
expect(usage).toEqual({
|
|
input: 20,
|
|
output: 30,
|
|
cacheRead: 100,
|
|
cacheWrite: undefined,
|
|
reasoningTokens: 17,
|
|
total: 250,
|
|
});
|
|
});
|
|
|
|
it("handles OpenAI Chat Completions reasoning token details", () => {
|
|
const usage = normalizeUsage({
|
|
prompt_tokens: 120,
|
|
completion_tokens: 30,
|
|
total_tokens: 150,
|
|
completion_tokens_details: { reasoning_tokens: 11 },
|
|
});
|
|
expect(usage).toEqual({
|
|
input: 120,
|
|
output: 30,
|
|
cacheRead: undefined,
|
|
cacheWrite: undefined,
|
|
reasoningTokens: 11,
|
|
total: 150,
|
|
});
|
|
});
|
|
|
|
it("clamps negative input to zero (pre-subtracted cached_tokens > prompt_tokens)", () => {
|
|
// shared model runtime OpenAI-format providers subtract cached_tokens from prompt_tokens
|
|
// upstream. When cached_tokens exceeds prompt_tokens the result is negative.
|
|
const usage = normalizeUsage({
|
|
input: -4900,
|
|
output: 200,
|
|
cacheRead: 5000,
|
|
});
|
|
expect(usage).toEqual({
|
|
input: 0,
|
|
output: 200,
|
|
cacheRead: 5000,
|
|
cacheWrite: undefined,
|
|
total: undefined,
|
|
});
|
|
});
|
|
|
|
it("clamps negative prompt_tokens alias to zero", () => {
|
|
const usage = normalizeUsage({
|
|
prompt_tokens: -12,
|
|
completion_tokens: 4,
|
|
});
|
|
expect(usage).toEqual({
|
|
input: 0,
|
|
output: 4,
|
|
cacheRead: undefined,
|
|
cacheWrite: undefined,
|
|
total: undefined,
|
|
});
|
|
});
|
|
|
|
it("returns undefined when no valid fields are provided", () => {
|
|
const usage = normalizeUsage(null);
|
|
expect(usage).toBeUndefined();
|
|
});
|
|
|
|
it("handles undefined input", () => {
|
|
const usage = normalizeUsage(undefined);
|
|
expect(usage).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe("toOpenAiChatCompletionsUsage", () => {
|
|
it("uses max(component sum, aggregate total) when breakdown is partial", () => {
|
|
const usage = normalizeUsage({ output_tokens: 20, total_tokens: 100 });
|
|
expect(toOpenAiChatCompletionsUsage(usage)).toEqual({
|
|
prompt_tokens: 0,
|
|
completion_tokens: 20,
|
|
total_tokens: 100,
|
|
});
|
|
});
|
|
|
|
it("uses component sum when it exceeds aggregate total", () => {
|
|
expect(
|
|
toOpenAiChatCompletionsUsage({
|
|
input: 30,
|
|
output: 40,
|
|
total: 50,
|
|
}),
|
|
).toEqual({
|
|
prompt_tokens: 30,
|
|
completion_tokens: 40,
|
|
total_tokens: 70,
|
|
});
|
|
});
|
|
|
|
it("uses aggregate total when only total is present", () => {
|
|
const usage = normalizeUsage({ total_tokens: 42 });
|
|
expect(toOpenAiChatCompletionsUsage(usage)).toEqual({
|
|
prompt_tokens: 0,
|
|
completion_tokens: 0,
|
|
total_tokens: 42,
|
|
});
|
|
});
|
|
|
|
it("preserves reasoning token details", () => {
|
|
const usage = normalizeUsage({
|
|
prompt_tokens: 10,
|
|
completion_tokens: 8,
|
|
completion_tokens_details: { reasoning_tokens: 6 },
|
|
total_tokens: 18,
|
|
});
|
|
expect(toOpenAiChatCompletionsUsage(usage)).toEqual({
|
|
prompt_tokens: 10,
|
|
completion_tokens: 8,
|
|
completion_tokens_details: { reasoning_tokens: 6 },
|
|
total_tokens: 18,
|
|
});
|
|
});
|
|
|
|
it("returns zeros for undefined usage", () => {
|
|
expect(toOpenAiChatCompletionsUsage(undefined)).toEqual({
|
|
prompt_tokens: 0,
|
|
completion_tokens: 0,
|
|
total_tokens: 0,
|
|
});
|
|
});
|
|
|
|
it("raises total_tokens with aggregate when cache write is excluded from prompt sum", () => {
|
|
expect(
|
|
toOpenAiChatCompletionsUsage({
|
|
input: 10,
|
|
output: 5,
|
|
cacheWrite: 100,
|
|
total: 200,
|
|
}),
|
|
).toEqual({
|
|
prompt_tokens: 10,
|
|
completion_tokens: 5,
|
|
total_tokens: 200,
|
|
});
|
|
});
|
|
|
|
it("clamps negative completion before deriving total_tokens", () => {
|
|
expect(
|
|
toOpenAiChatCompletionsUsage({
|
|
input: 3,
|
|
output: -5,
|
|
}),
|
|
).toEqual({
|
|
prompt_tokens: 3,
|
|
completion_tokens: 0,
|
|
total_tokens: 3,
|
|
});
|
|
});
|
|
|
|
it("preserves aggregate total when components are partially negative", () => {
|
|
expect(
|
|
toOpenAiChatCompletionsUsage({
|
|
input: 3,
|
|
output: -5,
|
|
total: 7,
|
|
}),
|
|
).toEqual({
|
|
prompt_tokens: 3,
|
|
completion_tokens: 0,
|
|
total_tokens: 7,
|
|
});
|
|
});
|
|
|
|
it("forwards cached_tokens via prompt_tokens_details when cache was hit", () => {
|
|
expect(
|
|
toOpenAiChatCompletionsUsage({
|
|
input: 594,
|
|
output: 79,
|
|
cacheRead: 30848,
|
|
cacheWrite: 0,
|
|
total: 31521,
|
|
}),
|
|
).toEqual({
|
|
prompt_tokens: 31442,
|
|
completion_tokens: 79,
|
|
total_tokens: 31521,
|
|
prompt_tokens_details: { cached_tokens: 30848 },
|
|
});
|
|
});
|
|
|
|
it("omits prompt_tokens_details when no cache was read", () => {
|
|
const result = toOpenAiChatCompletionsUsage({
|
|
input: 1000,
|
|
output: 50,
|
|
cacheRead: 0,
|
|
cacheWrite: 0,
|
|
total: 1050,
|
|
});
|
|
expect(result).toEqual({
|
|
prompt_tokens: 1000,
|
|
completion_tokens: 50,
|
|
total_tokens: 1050,
|
|
});
|
|
expect("prompt_tokens_details" in result).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("hasNonzeroUsage", () => {
|
|
it("returns true when cache read is nonzero", () => {
|
|
const usage = { cacheRead: 100 };
|
|
expect(hasNonzeroUsage(usage)).toBe(true);
|
|
});
|
|
|
|
it("returns true when cache write is nonzero", () => {
|
|
const usage = { cacheWrite: 50 };
|
|
expect(hasNonzeroUsage(usage)).toBe(true);
|
|
});
|
|
|
|
it("returns true when both cache fields are nonzero", () => {
|
|
const usage = { cacheRead: 100, cacheWrite: 50 };
|
|
expect(hasNonzeroUsage(usage)).toBe(true);
|
|
});
|
|
|
|
it("returns false when cache fields are zero", () => {
|
|
const usage = { cacheRead: 0, cacheWrite: 0 };
|
|
expect(hasNonzeroUsage(usage)).toBe(false);
|
|
});
|
|
|
|
it("returns false for undefined usage", () => {
|
|
expect(hasNonzeroUsage(undefined)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("derivePromptTokens", () => {
|
|
it("includes cache tokens in prompt total", () => {
|
|
const usage = {
|
|
input: 1000,
|
|
cacheRead: 500,
|
|
cacheWrite: 200,
|
|
};
|
|
const promptTokens = derivePromptTokens(usage);
|
|
expect(promptTokens).toBe(1700); // 1000 + 500 + 200
|
|
});
|
|
|
|
it("handles missing cache fields", () => {
|
|
const usage = {
|
|
input: 1000,
|
|
};
|
|
const promptTokens = derivePromptTokens(usage);
|
|
expect(promptTokens).toBe(1000);
|
|
});
|
|
|
|
it("returns undefined for empty usage", () => {
|
|
const promptTokens = derivePromptTokens({});
|
|
expect(promptTokens).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe("deriveContextPromptTokens", () => {
|
|
it("prefers explicit prompt snapshot over provider usage", () => {
|
|
expect(
|
|
deriveContextPromptTokens({
|
|
promptTokens: 44_000,
|
|
lastCallUsage: { input: 55_000, cacheRead: 25_000 },
|
|
usage: { input: 75_000, cacheRead: 25_000, output: 5_000, total: 105_000 },
|
|
}),
|
|
).toBe(44_000);
|
|
});
|
|
|
|
it("falls back to last-call prompt usage before accumulated usage", () => {
|
|
expect(
|
|
deriveContextPromptTokens({
|
|
lastCallUsage: { input: 55_000, cacheRead: 25_000, cacheWrite: 1_000 },
|
|
usage: { input: 75_000, cacheRead: 25_000, output: 5_000, total: 105_000 },
|
|
}),
|
|
).toBe(81_000);
|
|
});
|
|
|
|
it("falls back to accumulated usage when no prompt snapshot exists", () => {
|
|
expect(
|
|
deriveContextPromptTokens({
|
|
usage: { input: 75_000, cacheRead: 25_000, output: 5_000, total: 105_000 },
|
|
}),
|
|
).toBe(100_000);
|
|
});
|
|
});
|
|
|
|
describe("deriveSessionTotalTokens", () => {
|
|
it("includes cache tokens in total calculation", () => {
|
|
const totalTokens = deriveSessionTotalTokens({
|
|
usage: {
|
|
input: 1000,
|
|
cacheRead: 500,
|
|
cacheWrite: 200,
|
|
},
|
|
contextTokens: 4000,
|
|
});
|
|
expect(totalTokens).toBe(1700); // 1000 + 500 + 200
|
|
});
|
|
|
|
it("prefers promptTokens override over derived total", () => {
|
|
const totalTokens = deriveSessionTotalTokens({
|
|
usage: {
|
|
input: 1000,
|
|
cacheRead: 500,
|
|
cacheWrite: 200,
|
|
},
|
|
contextTokens: 4000,
|
|
promptTokens: 2500, // Override
|
|
});
|
|
expect(totalTokens).toBe(2500);
|
|
});
|
|
});
|