惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
博客园 - Franky
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Apple Machine Learning Research
Apple Machine Learning Research
云风的 BLOG
云风的 BLOG
人人都是产品经理
人人都是产品经理
博客园 - 【当耐特】
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
H
Help Net Security
爱范儿
爱范儿
罗磊的独立博客
博客园_首页
美团技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
量子位
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
V
Visual Studio Blog
T
Tailwind CSS Blog

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(gateway): fall back to lastCallUsage on /v1/chat/comp...
RenzoMXD · 2026-04-25 · via Recent Commits to openclaw:main
Original file line numberDiff line numberDiff line change

@@ -1,7 +1,12 @@

11

import { randomUUID } from "node:crypto";

22

import type { IncomingMessage, ServerResponse } from "node:http";

33

import type { ImageContent } from "../agents/command/types.js";

4-

import { normalizeUsage, toOpenAiChatCompletionsUsage } from "../agents/usage.js";

4+

import {

5+

hasNonzeroUsage,

6+

normalizeUsage,

7+

toOpenAiChatCompletionsUsage,

8+

type NormalizedUsage,

9+

} from "../agents/usage.js";

510

import { createDefaultDeps } from "../cli/deps.js";

611

import { agentCommandFromIngress } from "../commands/agent.js";

712

import type { GatewayHttpChatCompletionsConfig } from "../config/types.gateway.js";

@@ -369,6 +374,7 @@ async function resolveImagesForRequest(

369374

export const __testOnlyOpenAiHttp = {

370375

resolveImagesForRequest,

371376

resolveOpenAiChatCompletionsLimits,

377+

resolveChatCompletionUsage,

372378

};

373379
374380

function buildAgentPrompt(

@@ -466,24 +472,34 @@ type AgentUsageMeta = {

466472

total?: number;

467473

};

468474
469-

function resolveRawAgentUsage(result: unknown): AgentUsageMeta | undefined {

470-

return (

475+

function resolveAgentRunUsage(result: unknown): NormalizedUsage | undefined {

476+

const agentMeta = (

471477

result as {

472478

meta?: {

473479

agentMeta?: {

474480

usage?: AgentUsageMeta;

481+

lastCallUsage?: AgentUsageMeta;

475482

};

476483

};

477484

} | null

478-

)?.meta?.agentMeta?.usage;

485+

)?.meta?.agentMeta;

486+

const primary = normalizeUsage(agentMeta?.usage);

487+

if (hasNonzeroUsage(primary)) {

488+

return primary;

489+

}

490+

const fallback = normalizeUsage(agentMeta?.lastCallUsage);

491+

if (hasNonzeroUsage(fallback)) {

492+

return fallback;

493+

}

494+

return primary ?? fallback;

479495

}

480496
481497

function resolveChatCompletionUsage(result: unknown): {

482498

prompt_tokens: number;

483499

completion_tokens: number;

484500

total_tokens: number;

485501

} {

486-

return toOpenAiChatCompletionsUsage(normalizeUsage(resolveRawAgentUsage(result)));

502+

return toOpenAiChatCompletionsUsage(resolveAgentRunUsage(result));

487503

}

488504
489505

function resolveIncludeUsageForStreaming(payload: OpenAiChatCompletionRequest): boolean {

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,78 @@

1+

import { describe, expect, it } from "vitest";

2+

import { __testOnlyOpenAiHttp } from "./openai-http.js";

3+
4+

const { resolveChatCompletionUsage } = __testOnlyOpenAiHttp;

5+
6+

describe("resolveChatCompletionUsage", () => {

7+

it("maps agentMeta.usage to OpenAI prompt/completion/total fields", () => {

8+

const result = {

9+

meta: {

10+

agentMeta: {

11+

usage: { input: 120, output: 42, cacheRead: 10, total: 172 },

12+

},

13+

},

14+

};

15+
16+

expect(resolveChatCompletionUsage(result)).toEqual({

17+

prompt_tokens: 130,

18+

completion_tokens: 42,

19+

total_tokens: 172,

20+

});

21+

});

22+
23+

it("falls back to agentMeta.lastCallUsage when agentMeta.usage is missing", () => {

24+

const result = {

25+

meta: {

26+

agentMeta: {

27+

lastCallUsage: { input: 80, output: 20, total: 100 },

28+

},

29+

},

30+

};

31+
32+

expect(resolveChatCompletionUsage(result)).toEqual({

33+

prompt_tokens: 80,

34+

completion_tokens: 20,

35+

total_tokens: 100,

36+

});

37+

});

38+
39+

it("falls back to agentMeta.lastCallUsage when agentMeta.usage is all zero", () => {

40+

const result = {

41+

meta: {

42+

agentMeta: {

43+

usage: { input: 0, output: 0, total: 0 },

44+

lastCallUsage: { input: 55, output: 7, total: 62 },

45+

},

46+

},

47+

};

48+
49+

expect(resolveChatCompletionUsage(result)).toEqual({

50+

prompt_tokens: 55,

51+

completion_tokens: 7,

52+

total_tokens: 62,

53+

});

54+

});

55+
56+

it("returns zeros when both agentMeta.usage and lastCallUsage are absent", () => {

57+

const result = { meta: { agentMeta: {} } };

58+
59+

expect(resolveChatCompletionUsage(result)).toEqual({

60+

prompt_tokens: 0,

61+

completion_tokens: 0,

62+

total_tokens: 0,

63+

});

64+

});

65+
66+

it("returns zeros when the result has no meta at all", () => {

67+

expect(resolveChatCompletionUsage({})).toEqual({

68+

prompt_tokens: 0,

69+

completion_tokens: 0,

70+

total_tokens: 0,

71+

});

72+

expect(resolveChatCompletionUsage(null)).toEqual({

73+

prompt_tokens: 0,

74+

completion_tokens: 0,

75+

total_tokens: 0,

76+

});

77+

});

78+

});