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

推荐订阅源

MongoDB | Blog
MongoDB | Blog
B
Blog
Y
Y Combinator Blog
大猫的无限游戏
大猫的无限游戏
aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
博客园 - Franky
V
V2EX
IT之家
IT之家
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
J
Java Code Geeks
F
Fortinet All Blogs
I
InfoQ
云风的 BLOG
云风的 BLOG
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
Netflix TechBlog - Medium
宝玉的分享
宝玉的分享
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Proofpoint News Feed
Microsoft Security Blog
Microsoft Security 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
chore(deadcode): share chat token formatter · openclaw/op...
vincentkoc · 2026-06-21 · via Recent Commits to openclaw:main
Original file line numberDiff line numberDiff line change

@@ -2,6 +2,7 @@

22

import { html, nothing } from "lit";

33

import { icons } from "../icons.ts";

44

import type { GatewaySessionRow } from "../types.ts";

5+

import { formatCompactTokenCount } from "./token-format.ts";

56
67

const CONTEXT_NOTICE_RATIO = 0.85;

78

const CONTEXT_COMPACT_RATIO = 0.9;

@@ -77,7 +78,7 @@ export function getContextNoticeViewModel(

7778

if (!warning) {

7879

return {

7980

pct,

80-

detail: `${formatTokensCompact(used)} / ${formatTokensCompact(limit)}`,

81+

detail: `${formatCompactTokenCount(used)} / ${formatCompactTokenCount(limit)}`,

8182

color: "var(--muted)",

8283

bg: "color-mix(in srgb, var(--muted) 8%, transparent)",

8384

warning,

@@ -97,7 +98,7 @@ export function getContextNoticeViewModel(

9798

const bg = `rgba(${r}, ${g}, ${b}, ${bgOpacity})`;

9899

return {

99100

pct,

100-

detail: `${formatTokensCompact(used)} / ${formatTokensCompact(limit)}`,

101+

detail: `${formatCompactTokenCount(used)} / ${formatCompactTokenCount(limit)}`,

101102

color,

102103

bg,

103104

warning,

@@ -175,14 +176,3 @@ export function renderContextNotice(

175176

</div>

176177

`;

177178

}

178-
179-

/** Format token count compactly (e.g. 128000 -> "128k"). */

180-

function formatTokensCompact(n: number): string {

181-

if (n >= 1_000_000) {

182-

return `${(n / 1_000_000).toFixed(1).replace(/\.0$/, "")}M`;

183-

}

184-

if (n >= 1_000) {

185-

return `${(n / 1_000).toFixed(1).replace(/\.0$/, "")}k`;

186-

}

187-

return String(n);

188-

}

Original file line numberDiff line numberDiff line change

@@ -25,6 +25,7 @@ import { renderCopyAsMarkdownButton } from "./copy-as-markdown.ts";

2525

import { extractThinkingCached, formatReasoningMarkdown } from "./message-extract.ts";

2626

import { isToolResultMessage, normalizeMessage } from "./message-normalizer.ts";

2727

import { normalizeRoleForGrouping } from "./role-normalizer.ts";

28+

import { formatCompactTokenCount } from "./token-format.ts";

2829

import {

2930

extractToolCardsCached,

3031

formatCollapsedToolPreviewText,

@@ -683,17 +684,6 @@ function extractGroupMeta(group: MessageGroup, contextWindow: number | null): Gr

683684

return { input, output, cacheRead, cacheWrite, cost, model, contextPercent };

684685

}

685686
686-

/** Compact token count formatter (e.g. 128000 → "128k"). */

687-

function fmtTokens(n: number): string {

688-

if (n >= 1_000_000) {

689-

return `${(n / 1_000_000).toFixed(1).replace(/\.0$/, "")}M`;

690-

}

691-

if (n >= 1_000) {

692-

return `${(n / 1_000).toFixed(1).replace(/\.0$/, "")}k`;

693-

}

694-

return String(n);

695-

}

696-
697687

function renderMessageMeta(meta: GroupMeta | null) {

698688

if (!meta) {

699689

return nothing;

@@ -703,18 +693,24 @@ function renderMessageMeta(meta: GroupMeta | null) {

703693
704694

// Token counts: ↑input ↓output

705695

if (meta.input) {

706-

parts.push(html`<span class="msg-meta__tokens">${fmtTokens(meta.input)}</span>`);

696+

parts.push(html`<span class="msg-meta__tokens">${formatCompactTokenCount(meta.input)}</span>`);

707697

}

708698

if (meta.output) {

709-

parts.push(html`<span class="msg-meta__tokens">${fmtTokens(meta.output)}</span>`);

699+

parts.push(

700+

html`<span class="msg-meta__tokens">${formatCompactTokenCount(meta.output)}</span>`,

701+

);

710702

}

711703
712704

// Cache: R/W

713705

if (meta.cacheRead) {

714-

parts.push(html`<span class="msg-meta__cache">R${fmtTokens(meta.cacheRead)}</span>`);

706+

parts.push(

707+

html`<span class="msg-meta__cache">R${formatCompactTokenCount(meta.cacheRead)}</span>`,

708+

);

715709

}

716710

if (meta.cacheWrite) {

717-

parts.push(html`<span class="msg-meta__cache">W${fmtTokens(meta.cacheWrite)}</span>`);

711+

parts.push(

712+

html`<span class="msg-meta__cache">W${formatCompactTokenCount(meta.cacheWrite)}</span>`,

713+

);

718714

}

719715
720716

// Cost

Original file line numberDiff line numberDiff line change

@@ -30,6 +30,7 @@ import type {

3030

} from "../types.ts";

3131

import { generateUUID } from "../uuid.ts";

3232

import { SLASH_COMMANDS } from "./slash-commands.ts";

33+

import { formatCompactTokenCount } from "./token-format.ts";

3334
3435

export type SlashCommandResult = {

3536

/** Markdown-formatted result to display in chat. */

@@ -436,16 +437,16 @@ async function executeUsage(

436437

const totalDisplay =

437438

cumulativeTotal === null

438439

? "n/a"

439-

: `${totalTokensFresh ? "" : "~"}${fmtTokens(cumulativeTotal)}`;

440+

: `${totalTokensFresh ? "" : "~"}${formatCompactTokenCount(cumulativeTotal)}`;

440441
441442

const lines = [

442443

"**Session Usage**",

443-

`Input: **${fmtTokens(input)}** tokens`,

444-

`Output: **${fmtTokens(output)}** tokens`,

444+

`Input: **${formatCompactTokenCount(input)}** tokens`,

445+

`Output: **${formatCompactTokenCount(output)}** tokens`,

445446

`Total: **${totalDisplay}** tokens`,

446447

];

447448

if (pct !== null) {

448-

lines.push(`Context: **${pct}%** of ${fmtTokens(ctx)}`);

449+

lines.push(`Context: **${pct}%** of ${formatCompactTokenCount(ctx)}`);

449450

}

450451

if (session.model) {

451452

lines.push(`Model: \`${session.model}\``);

@@ -785,13 +786,3 @@ async function executeRedirect(

785786

return { content: `Failed to redirect: ${String(err)}` };

786787

}

787788

}

788-
789-

function fmtTokens(n: number): string {

790-

if (n >= 1_000_000) {

791-

return `${(n / 1_000_000).toFixed(1).replace(/\.0$/, "")}M`;

792-

}

793-

if (n >= 1_000) {

794-

return `${(n / 1_000).toFixed(1).replace(/\.0$/, "")}k`;

795-

}

796-

return String(n);

797-

}

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,10 @@

1+

// Chat surfaces share a one-decimal compact token label, e.g. 214500 -> "214.5k".

2+

export function formatCompactTokenCount(tokens: number): string {

3+

if (tokens >= 1_000_000) {

4+

return `${(tokens / 1_000_000).toFixed(1).replace(/\.0$/, "")}M`;

5+

}

6+

if (tokens >= 1_000) {

7+

return `${(tokens / 1_000).toFixed(1).replace(/\.0$/, "")}k`;

8+

}

9+

return String(tokens);

10+

}