@sleep2agi/commhub-server 0.9.0-preview.271 → 0.9.0-preview.273
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build-info.json +2 -2
- package/package.json +1 -1
- package/src/node-token-usage.test.ts +9 -0
- package/src/node-token-usage.ts +21 -7
- package/src/server.ts +20 -18
- package/src/tools.ts +3 -1
package/build-info.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sleep2agi/commhub-server",
|
|
3
|
-
"version": "0.9.0-preview.
|
|
3
|
+
"version": "0.9.0-preview.273",
|
|
4
4
|
"description": "CommHub Server — AI Agent communication hub with MCP protocol, multi-network isolation, user auth, and MCP tools (17 collaboration-core + node/provider ops tools; authoritative list: docs-site/docs/api/mcp-tools.md).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.ts",
|
|
@@ -64,6 +64,15 @@ describe("#3952 节点 Token 审计", () => {
|
|
|
64
64
|
expect(rows[0].total_tokens).toBe(2);
|
|
65
65
|
});
|
|
66
66
|
|
|
67
|
+
test("cache 单列:报进去不进 total(它是 input 的几百倍,混算会把排行变成「谁最老」)", () => {
|
|
68
|
+
cleanup();
|
|
69
|
+
const hour = currentHourUtc();
|
|
70
|
+
reportTokenUsage({ networkId: net, nodeId: nodeA, hour, model: 'm', inputTokens: 100, outputTokens: 10, cacheReadTokens: 50_000 });
|
|
71
|
+
const rows = readTokenUsage(net, 24);
|
|
72
|
+
expect(rows[0].total_tokens).toBe(110);
|
|
73
|
+
expect(rows[0].cache_read_tokens).toBe(50_000);
|
|
74
|
+
});
|
|
75
|
+
|
|
67
76
|
test("④ 形状:表里没有任何内容列(prompt / message / credential 一律不存在)", () => {
|
|
68
77
|
const shape = [...NODE_TOKEN_USAGE_COLUMNS].join(",");
|
|
69
78
|
expect(shape).not.toMatch(/prompt|message|content|credential|secret|token_value/i);
|
package/src/node-token-usage.ts
CHANGED
|
@@ -29,6 +29,7 @@ db.exec(`
|
|
|
29
29
|
model TEXT NOT NULL DEFAULT 'unknown',
|
|
30
30
|
input_tokens INTEGER NOT NULL DEFAULT 0,
|
|
31
31
|
output_tokens INTEGER NOT NULL DEFAULT 0,
|
|
32
|
+
cache_read_tokens INTEGER NOT NULL DEFAULT 0,
|
|
32
33
|
turns INTEGER NOT NULL DEFAULT 0,
|
|
33
34
|
cost_usd REAL NOT NULL DEFAULT 0,
|
|
34
35
|
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
@@ -36,6 +37,8 @@ db.exec(`
|
|
|
36
37
|
);
|
|
37
38
|
CREATE INDEX IF NOT EXISTS idx_node_token_usage_hour ON node_token_usage(network_id, hour);
|
|
38
39
|
`);
|
|
40
|
+
// 已存在的库补列(SQLite 没有 IF NOT EXISTS for ADD COLUMN)
|
|
41
|
+
try { db.exec("ALTER TABLE node_token_usage ADD COLUMN cache_read_tokens INTEGER NOT NULL DEFAULT 0"); } catch {}
|
|
39
42
|
|
|
40
43
|
export interface TokenUsageInput {
|
|
41
44
|
networkId: string;
|
|
@@ -46,6 +49,9 @@ export interface TokenUsageInput {
|
|
|
46
49
|
model?: string | null;
|
|
47
50
|
inputTokens?: number | null;
|
|
48
51
|
outputTokens?: number | null;
|
|
52
|
+
/** 缓存命中的读入量。**必须单列**:opencode 实测 cache.read 是 input+output 的 289 倍,
|
|
53
|
+
* 混进去算会让「谁用得多」变成「谁用得久」。 */
|
|
54
|
+
cacheReadTokens?: number | null;
|
|
49
55
|
/** 有的运行时只给总轮数/费用;没有就留空,**不填 0**(0 与"不知道"是两件事) */
|
|
50
56
|
turns?: number | null;
|
|
51
57
|
costUsd?: number | null;
|
|
@@ -67,7 +73,7 @@ function nonNegativeInt(value: unknown): number | null {
|
|
|
67
73
|
/** 校验 + 归一。返回 null 表示「这一份上报不合规」——调用方负责拒绝,不静默丢。 */
|
|
68
74
|
export function normalizeUsage(input: TokenUsageInput): {
|
|
69
75
|
hour: string; model: string;
|
|
70
|
-
inputTokens: number; outputTokens: number;
|
|
76
|
+
inputTokens: number; outputTokens: number; cacheReadTokens: number;
|
|
71
77
|
turns: number | null; costUsd: number | null;
|
|
72
78
|
} | null {
|
|
73
79
|
const hour = typeof input.hour === 'string' && input.hour ? input.hour : currentHourUtc();
|
|
@@ -77,14 +83,15 @@ export function normalizeUsage(input: TokenUsageInput): {
|
|
|
77
83
|
const model = rawModel || 'unknown';
|
|
78
84
|
const inputTokens = nonNegativeInt(input.inputTokens) ?? 0;
|
|
79
85
|
const outputTokens = nonNegativeInt(input.outputTokens) ?? 0;
|
|
80
|
-
|
|
86
|
+
const cacheReadTokens = nonNegativeInt(input.cacheReadTokens) ?? 0;
|
|
87
|
+
if (inputTokens === 0 && outputTokens === 0 && cacheReadTokens === 0 && input.turns == null && input.costUsd == null) {
|
|
81
88
|
// 什么都没报:不写一行全 0 的记录(那会让"没有用量"和"没上报"在报表上同形)。
|
|
82
89
|
return null;
|
|
83
90
|
}
|
|
84
91
|
const turns = nonNegativeInt(input.turns);
|
|
85
92
|
const costRaw = input.costUsd;
|
|
86
93
|
const costUsd = typeof costRaw === 'number' && Number.isFinite(costRaw) && costRaw >= 0 ? costRaw : null;
|
|
87
|
-
return { hour, model, inputTokens, outputTokens, turns, costUsd };
|
|
94
|
+
return { hour, model, inputTokens, outputTokens, cacheReadTokens, turns, costUsd };
|
|
88
95
|
}
|
|
89
96
|
|
|
90
97
|
/** 记账:同一 (network, node, hour, model) 累加。turns/cost 可空,空则保持原值。 */
|
|
@@ -92,17 +99,19 @@ export function reportTokenUsage(input: TokenUsageInput): { ok: true; hour: stri
|
|
|
92
99
|
const norm = normalizeUsage(input);
|
|
93
100
|
if (!norm) return { ok: false, error: 'invalid_token_usage' };
|
|
94
101
|
db.run(
|
|
95
|
-
`INSERT INTO node_token_usage(network_id, node_id, hour, model, input_tokens, output_tokens, turns, cost_usd, updated_at)
|
|
96
|
-
VALUES(?1, ?2, ?3, ?4, ?5, ?6, COALESCE(?
|
|
102
|
+
`INSERT INTO node_token_usage(network_id, node_id, hour, model, input_tokens, output_tokens, cache_read_tokens, turns, cost_usd, updated_at)
|
|
103
|
+
VALUES(?1, ?2, ?3, ?4, ?5, ?6, ?7, COALESCE(?8, 0), COALESCE(?9, 0), datetime('now'))
|
|
97
104
|
ON CONFLICT(network_id, node_id, hour, model) DO UPDATE SET
|
|
98
105
|
input_tokens = input_tokens + excluded.input_tokens,
|
|
99
106
|
output_tokens = output_tokens + excluded.output_tokens,
|
|
107
|
+
cache_read_tokens = cache_read_tokens + excluded.cache_read_tokens,
|
|
100
108
|
turns = COALESCE(turns, 0) + COALESCE(excluded.turns, 0),
|
|
101
109
|
cost_usd = COALESCE(cost_usd, 0) + COALESCE(excluded.cost_usd, 0),
|
|
102
110
|
updated_at = datetime('now')`,
|
|
103
111
|
// turns / cost_usd 这里存 0 = 「没有这一项」。**存储层不区分「0 轮」与「没报轮数」**,
|
|
104
112
|
// 这是已知偏差(见文件头「已知边界」);要区分得加一列 has_turns,成本不值。
|
|
105
|
-
[input.networkId, input.nodeId, norm.hour, norm.model, norm.inputTokens, norm.outputTokens,
|
|
113
|
+
[input.networkId, input.nodeId, norm.hour, norm.model, norm.inputTokens, norm.outputTokens,
|
|
114
|
+
norm.cacheReadTokens, norm.turns, norm.costUsd],
|
|
106
115
|
);
|
|
107
116
|
return { ok: true, hour: norm.hour, model: norm.model };
|
|
108
117
|
}
|
|
@@ -112,7 +121,10 @@ export interface NodeTokenUsageRow {
|
|
|
112
121
|
alias: string | null;
|
|
113
122
|
input_tokens: number;
|
|
114
123
|
output_tokens: number;
|
|
124
|
+
/** input+output(**不含 cache**)—— 报表默认看这个 */
|
|
115
125
|
total_tokens: number;
|
|
126
|
+
/** 单列;opencode 上它可能是 total 的几百倍 */
|
|
127
|
+
cache_read_tokens: number;
|
|
116
128
|
turns: number;
|
|
117
129
|
cost_usd: number;
|
|
118
130
|
models: number;
|
|
@@ -138,6 +150,7 @@ export function readTokenUsage(networkId: string, sinceHours = 24): NodeTokenUsa
|
|
|
138
150
|
COALESCE(n.alias, s.alias) AS alias,
|
|
139
151
|
SUM(u.input_tokens) AS input_tokens,
|
|
140
152
|
SUM(u.output_tokens) AS output_tokens,
|
|
153
|
+
SUM(u.cache_read_tokens) AS cache_read_tokens,
|
|
141
154
|
SUM(u.turns) AS turns,
|
|
142
155
|
SUM(u.cost_usd) AS cost_usd,
|
|
143
156
|
COUNT(DISTINCT u.model) AS models,
|
|
@@ -153,6 +166,7 @@ export function readTokenUsage(networkId: string, sinceHours = 24): NodeTokenUsa
|
|
|
153
166
|
);
|
|
154
167
|
return rows.map((r) => ({
|
|
155
168
|
...r,
|
|
169
|
+
cache_read_tokens: r.cache_read_tokens ?? 0,
|
|
156
170
|
total_tokens: (r.input_tokens ?? 0) + (r.output_tokens ?? 0),
|
|
157
171
|
}));
|
|
158
172
|
}
|
|
@@ -160,5 +174,5 @@ export function readTokenUsage(networkId: string, sinceHours = 24): NodeTokenUsa
|
|
|
160
174
|
/** 记账表与读账口径都只认这几个列 —— 形状就是「不收内容」的保证。 */
|
|
161
175
|
export const NODE_TOKEN_USAGE_COLUMNS = [
|
|
162
176
|
'network_id', 'node_id', 'hour', 'model',
|
|
163
|
-
'input_tokens', 'output_tokens', 'turns', 'cost_usd', 'updated_at',
|
|
177
|
+
'input_tokens', 'output_tokens', 'cache_read_tokens', 'turns', 'cost_usd', 'updated_at',
|
|
164
178
|
] as const;
|
package/src/server.ts
CHANGED
|
@@ -6394,22 +6394,7 @@ return Bun.serve({
|
|
|
6394
6394
|
// {model, flags, config_revision} the node last posted via
|
|
6395
6395
|
// report_status (B6). NEVER reads per-node files. Network-scoped
|
|
6396
6396
|
// via addNetworkScope so a netA viewer can't read a netB node.
|
|
6397
|
-
|
|
6398
|
-
if (nodeConfigMatch && req.method === "GET") {
|
|
6399
|
-
const nodeId = decodeURIComponent(nodeConfigMatch[1] ?? "");
|
|
6400
|
-
const params: any[] = [nodeId];
|
|
6401
|
-
let sql = "SELECT node_id, alias, network_id, config_revision, config_snapshot FROM nodes WHERE node_id = ?1";
|
|
6402
|
-
sql = addNetworkScope(sql, params, restScope);
|
|
6403
|
-
sql += " LIMIT 1";
|
|
6404
|
-
const node = db.get<any>(sql, ...params);
|
|
6405
|
-
if (!node) {
|
|
6406
|
-
return withCors(req, Response.json({ ok: false, error: "node_not_found", node_id: nodeId }, { status: 404 }));
|
|
6407
|
-
}
|
|
6408
|
-
let snapshot: any = {};
|
|
6409
|
-
if (node.config_snapshot) {
|
|
6410
|
-
try { snapshot = JSON.parse(node.config_snapshot); } catch { snapshot = {}; }
|
|
6411
|
-
}
|
|
6412
|
-
// ── #3952 Token 审计(admin 2026-11-05)──
|
|
6397
|
+
// ── #3952 Token 审计(admin 2026-11-05)──
|
|
6413
6398
|
// GET /api/token-usage?network_id=…&since_hours=24
|
|
6414
6399
|
// 人(成员/owner/admin)读本网络各节点的 LLM token 计数聚合;节点令牌不给(user_token_required)。
|
|
6415
6400
|
// 为什么走 REST 而不是 MCP 工具:tools/list 的 user 预算只剩 ~500 B,
|
|
@@ -6430,9 +6415,11 @@ return Bun.serve({
|
|
|
6430
6415
|
const totals = nodes.reduce((acc, r) => ({
|
|
6431
6416
|
input_tokens: acc.input_tokens + r.input_tokens,
|
|
6432
6417
|
output_tokens: acc.output_tokens + r.output_tokens,
|
|
6418
|
+
// cache 单列:它可能是 total 的几百倍,绝不能加进 total
|
|
6419
|
+
cache_read_tokens: acc.cache_read_tokens + r.cache_read_tokens,
|
|
6433
6420
|
total_tokens: acc.total_tokens + r.total_tokens,
|
|
6434
6421
|
cost_usd: Number((acc.cost_usd + r.cost_usd).toFixed(6)),
|
|
6435
|
-
}), { input_tokens: 0, output_tokens: 0, total_tokens: 0, cost_usd: 0 });
|
|
6422
|
+
}), { input_tokens: 0, output_tokens: 0, cache_read_tokens: 0, total_tokens: 0, cost_usd: 0 });
|
|
6436
6423
|
return withCors(req, Response.json({
|
|
6437
6424
|
ok: true, network_id: netId, since_hours: sinceHours,
|
|
6438
6425
|
// 空表也要说清口径:0 行可能因为「没人上报」,也可能因为「窗口里没用量」。
|
|
@@ -6441,7 +6428,22 @@ return Bun.serve({
|
|
|
6441
6428
|
}));
|
|
6442
6429
|
}
|
|
6443
6430
|
|
|
6444
|
-
|
|
6431
|
+
const nodeConfigMatch = url.pathname.match(/^\/api\/nodes\/([^/]+)\/config$/);
|
|
6432
|
+
if (nodeConfigMatch && req.method === "GET") {
|
|
6433
|
+
const nodeId = decodeURIComponent(nodeConfigMatch[1] ?? "");
|
|
6434
|
+
const params: any[] = [nodeId];
|
|
6435
|
+
let sql = "SELECT node_id, alias, network_id, config_revision, config_snapshot FROM nodes WHERE node_id = ?1";
|
|
6436
|
+
sql = addNetworkScope(sql, params, restScope);
|
|
6437
|
+
sql += " LIMIT 1";
|
|
6438
|
+
const node = db.get<any>(sql, ...params);
|
|
6439
|
+
if (!node) {
|
|
6440
|
+
return withCors(req, Response.json({ ok: false, error: "node_not_found", node_id: nodeId }, { status: 404 }));
|
|
6441
|
+
}
|
|
6442
|
+
let snapshot: any = {};
|
|
6443
|
+
if (node.config_snapshot) {
|
|
6444
|
+
try { snapshot = JSON.parse(node.config_snapshot); } catch { snapshot = {}; }
|
|
6445
|
+
}
|
|
6446
|
+
// #3146 —— 上一次下发的**结局**。没有这一格时,客户端只能靠
|
|
6445
6447
|
// 「config_revision 有没有涨」推断,涨不了就一律显示成 timeout(「90s 内没回来,去看日志」),
|
|
6446
6448
|
// 而生产上真实结局是 rejected,原因(OpenCode V2 session is not running)就写在本库里。
|
|
6447
6449
|
// ⇒ 读者拿不到答案,于是被指去机器上看日志 —— 一句话把人送错了层。
|
package/src/tools.ts
CHANGED
|
@@ -3308,10 +3308,11 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
|
|
|
3308
3308
|
model: z.string().max(100).optional(),
|
|
3309
3309
|
input_tokens: z.number().int().min(0).optional(),
|
|
3310
3310
|
output_tokens: z.number().int().min(0).optional(),
|
|
3311
|
+
cache_read_tokens: z.number().int().min(0).optional(),
|
|
3311
3312
|
turns: z.number().int().min(0).optional(),
|
|
3312
3313
|
cost_usd: z.number().min(0).optional(),
|
|
3313
3314
|
},
|
|
3314
|
-
async ({ hour, model, input_tokens, output_tokens, turns, cost_usd }) => {
|
|
3315
|
+
async ({ hour, model, input_tokens, output_tokens, cache_read_tokens, turns, cost_usd }) => {
|
|
3315
3316
|
if (!callerTokenIsNetwork || !enforceNetworkId || !callerAlias) {
|
|
3316
3317
|
return { content: [{ type: "text" as const, text: JSON.stringify({ ok: false, error: "network_token_required" }) }] };
|
|
3317
3318
|
}
|
|
@@ -3321,6 +3322,7 @@ export function registerTools(server: McpServer, clientIP?: string, enforceNetwo
|
|
|
3321
3322
|
networkId: enforceNetworkId, nodeId: node.node_id,
|
|
3322
3323
|
hour: hour ?? null, model: model ?? null,
|
|
3323
3324
|
inputTokens: input_tokens ?? null, outputTokens: output_tokens ?? null,
|
|
3325
|
+
cacheReadTokens: cache_read_tokens ?? null,
|
|
3324
3326
|
turns: turns ?? null, costUsd: cost_usd ?? null,
|
|
3325
3327
|
});
|
|
3326
3328
|
return { content: [{ type: "text" as const, text: JSON.stringify(r) }] };
|