@aiti-ai/ext-shared 0.0.0-stage → 0.1.11
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/LICENSE +21 -0
- package/README.md +36 -2
- package/agent-accounts.test.mjs +227 -0
- package/agent-accounts.ts +324 -0
- package/agent-capability-contract.test.mjs +129 -0
- package/agent-interaction.test.mjs +194 -0
- package/agent-interaction.ts +340 -0
- package/agent-launch.test.mjs +262 -0
- package/agent-spawn-env.test.mjs +169 -0
- package/agent-spawn-env.ts +548 -0
- package/aiti-editor-inject.mjs +49 -0
- package/backend-queue.test.mjs +336 -0
- package/backend-queue.ts +469 -0
- package/blob-save-route.ts +128 -0
- package/cbc-model-catalog.ts +86 -0
- package/check-client.mjs +273 -0
- package/check-ext-globals.mjs +144 -0
- package/controlled-login-dialog.mjs +270 -0
- package/controlled-login.mjs +398 -0
- package/controlled-login.test.mjs +466 -0
- package/convert-saveas.test.mjs +39 -0
- package/convert-saveas.ts +170 -0
- package/copy-client-bundle.mjs +39 -0
- package/cursor-cli-store.ts +377 -0
- package/dedupe-fetch.test.mjs +71 -0
- package/dedupe-fetch.ts +58 -0
- package/deploy-commit.mjs +52 -0
- package/dev-pack.mjs +246 -0
- package/dock-icons.ts +127 -0
- package/esbuild-bin.test.mjs +53 -0
- package/esbuild-bin.ts +66 -0
- package/exec-async.test.mjs +86 -0
- package/exec-async.ts +179 -0
- package/ext-display-names.ts +91 -0
- package/ext-entry-contracts.test.mjs +131 -0
- package/ext-float-dock-core.test.mjs +348 -0
- package/ext-float-dock-core.ts +783 -0
- package/ext-float-dock-lite.ts +52 -0
- package/ext-float-dock.test.mjs +286 -0
- package/ext-float-dock.ts +740 -0
- package/ext-icons-deploy.mjs +24 -0
- package/ext-lazy.test.mjs +186 -0
- package/ext-lazy.tsx +183 -0
- package/ext-model-state.test.mjs +272 -0
- package/ext-model-state.ts +324 -0
- package/ext-protocol.mjs +113 -0
- package/ext-protocol.test.mjs +88 -0
- package/ext-session-usage.test.mjs +219 -0
- package/ext-session-usage.ts +507 -0
- package/external-cli-governance.ts +153 -0
- package/git-ops-client/api.mjs +56 -0
- package/git-ops-client/branch-panel.tsx +987 -0
- package/git-ops-client/i18n.mjs +328 -0
- package/host-tools-allow.test.mjs +46 -0
- package/host-tools-allow.ts +84 -0
- package/icons/alert.svg +1 -0
- package/icons/bookmark.svg +1 -0
- package/icons/bot.svg +1 -0
- package/icons/chart-column.svg +1 -0
- package/icons/check-circle.svg +1 -0
- package/icons/clipboard-list.svg +1 -0
- package/icons/clock.svg +1 -0
- package/icons/coffee.svg +1 -0
- package/icons/edit.svg +1 -0
- package/icons/eye.svg +1 -0
- package/icons/git-branch.svg +1 -0
- package/icons/git-commit-horizontal.svg +1 -0
- package/icons/globe.svg +1 -0
- package/icons/history.svg +1 -0
- package/icons/import.svg +1 -0
- package/icons/inbox.svg +1 -0
- package/icons/key-round.svg +1 -0
- package/icons/layout-grid.svg +1 -0
- package/icons/mail.svg +1 -0
- package/icons/message-circle.svg +1 -0
- package/icons/message-square.svg +1 -0
- package/icons/meter-vertical.svg +1 -0
- package/icons/mic.svg +1 -0
- package/icons/monitor.svg +1 -0
- package/icons/pen-line.svg +1 -0
- package/icons/plug.svg +1 -0
- package/icons/radio-dot.svg +1 -0
- package/icons/radio.svg +1 -0
- package/icons/refresh.svg +1 -0
- package/icons/rocket.svg +1 -0
- package/icons/search.svg +1 -0
- package/icons/shield.svg +1 -0
- package/icons/sparkles.svg +1 -0
- package/icons/square-kanban.svg +1 -0
- package/icons/terminal.svg +1 -0
- package/icons/users.svg +1 -0
- package/icons/volume-2.svg +1 -0
- package/icons/wand.svg +1 -0
- package/icons/wrench.svg +1 -0
- package/icons/x-circle.svg +1 -0
- package/icons/x.svg +1 -0
- package/ignore-semantics.ts +76 -0
- package/lazy-loading-metrics.mjs +61 -0
- package/new-session-cwd.test.mjs +389 -0
- package/new-session-cwd.ts +253 -0
- package/open-viewer-window.ts +65 -0
- package/overlay-back-client.test.mjs +123 -0
- package/overlay-back-client.ts +87 -0
- package/package.json +65 -4
- package/pi-fork-rpc/README.md +12 -0
- package/pi-fork-rpc/cli-resolve.test.mjs +42 -0
- package/pi-fork-rpc/cli-resolve.ts +79 -0
- package/pi-fork-rpc/remote-queue.test.mjs +100 -0
- package/pi-fork-rpc/remote-queue.ts +140 -0
- package/pi-fork-rpc/spec.ts +106 -0
- package/pi-fork-rpc/wire.test.mjs +260 -0
- package/pi-fork-rpc/wire.ts +367 -0
- package/popup-anchor.ts +45 -0
- package/provider-priority-contract.test.mjs +55 -0
- package/reconcile-deps.mjs +41 -0
- package/reload-instances.mjs +60 -0
- package/seed-tool.test.mjs +95 -0
- package/seed-tool.ts +88 -0
- package/session-recycle.ts +109 -0
- package/shared-copy-sync.test.mjs +115 -0
- package/soffice-locate.ts +171 -0
- package/soffice-panel.ts +239 -0
- package/sync-dir.mjs +91 -0
- package/sync-dir.test.mjs +101 -0
- package/sync-gate.test.mjs +93 -0
- package/sync-mirror.test.mjs +85 -0
- package/task-list.test.mjs +91 -0
- package/task-list.ts +118 -0
- package/test-rm-rf.mjs +33 -0
- package/ui-dialogs.test.mjs +198 -0
- package/ui-dialogs.ts +310 -0
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ext-session-usage tests — fixture 钉死各外部 agent 会话的 usage wire 假设,
|
|
3
|
+
* 防上游格式漂移静默破坏(对齐 cursor-cli-store.test.mjs 范式)。
|
|
4
|
+
*
|
|
5
|
+
* 覆盖(本机 2026-09 实测样本形状,见 docs/aiti-ai-usage-ext-agent-token-ingest-plan.md §2.2):
|
|
6
|
+
* - claude-like(CC):assistant 行 message.usage{input_tokens,...} + message.model
|
|
7
|
+
* - claude-like(Qoder 0.1.24):usage 恒 0 且无 model/credits → 不产出行
|
|
8
|
+
* - claude-like(QoderCN 1.1.44):model + usage.credits(token 恒 0)→ 产出 model/credits 行
|
|
9
|
+
* - codebuddy:providerData.rawUsage(OpenAI 兼容字段名)+ providerData.model
|
|
10
|
+
* - zcode:SQLite message.data JSON 的 tokens{...}+modelID/providerID(3.16.3 起新键 modelId/providerId,双键兼容)+cost;timeline 噪音剔除
|
|
11
|
+
*
|
|
12
|
+
* Run: node --experimental-strip-types --test pi-web-ext/_shared/ext-session-usage.test.mjs
|
|
13
|
+
*/
|
|
14
|
+
import assert from "node:assert/strict";
|
|
15
|
+
import test from "node:test";
|
|
16
|
+
import { mkdtempSync, writeFileSync, rmSync, mkdirSync } from "node:fs";
|
|
17
|
+
import { tmpdir } from "node:os";
|
|
18
|
+
import { join } from "node:path";
|
|
19
|
+
import { DatabaseSync } from "node:sqlite";
|
|
20
|
+
|
|
21
|
+
const { parseClaudeLikeSession, parseCodebuddySession, parseZcodeSession, inferProvider } =
|
|
22
|
+
await import("./ext-session-usage.ts");
|
|
23
|
+
|
|
24
|
+
const ROOT = mkdtempSync(join(tmpdir(), "ext-usage-test-"));
|
|
25
|
+
|
|
26
|
+
test.after(() => rmSync(ROOT, { recursive: true, force: true }));
|
|
27
|
+
|
|
28
|
+
function writeSession(sub, name, lines) {
|
|
29
|
+
const dir = join(ROOT, sub);
|
|
30
|
+
mkdirSync(dir, { recursive: true });
|
|
31
|
+
const p = join(dir, name);
|
|
32
|
+
writeFileSync(p, lines.map((l) => JSON.stringify(l)).join("\n"));
|
|
33
|
+
return p;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// ── claude-like 家族 ───────────────────────────────────────────────────────
|
|
37
|
+
|
|
38
|
+
test("claude-like: CC assistant usage 全字段提取(含 cache 分类)", () => {
|
|
39
|
+
const f = writeSession("cc", "11111111-2222-3333-4444-555555555555.jsonl", [
|
|
40
|
+
{ type: "session", id: "11111111-2222-3333-4444-555555555555", cwd: "/tmp/x" },
|
|
41
|
+
{ type: "user", timestamp: "2026-09-01T10:00:00Z", message: { role: "user", content: "hi" } },
|
|
42
|
+
{
|
|
43
|
+
type: "assistant",
|
|
44
|
+
timestamp: "2026-09-01T10:00:01Z",
|
|
45
|
+
message: {
|
|
46
|
+
role: "assistant", model: "glm-5.3",
|
|
47
|
+
usage: { input_tokens: 44783, cache_creation_input_tokens: 0, cache_read_input_tokens: 1920, output_tokens: 108 },
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
type: "assistant",
|
|
52
|
+
timestamp: "2026-09-01T10:00:02Z",
|
|
53
|
+
message: {
|
|
54
|
+
role: "assistant", model: "claude-sonnet-4-5",
|
|
55
|
+
usage: { input_tokens: 100, cache_creation_input_tokens: 50, cache_read_input_tokens: 20, output_tokens: 30 },
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
]);
|
|
59
|
+
const rows = parseClaudeLikeSession("claude", f, "11111111-2222-3333-4444-555555555555");
|
|
60
|
+
assert.equal(rows.length, 2, "两条 assistant usage 行都产出");
|
|
61
|
+
const [r1, r2] = rows;
|
|
62
|
+
assert.deepEqual(
|
|
63
|
+
[r1.input, r1.output, r1.cacheRead, r1.cacheWrite, r1.totalTokens],
|
|
64
|
+
[44783, 108, 1920, 0, 44891],
|
|
65
|
+
"CC 字段映射 input/output/cacheRead/cacheWrite/total",
|
|
66
|
+
);
|
|
67
|
+
assert.equal(r1.model, "glm-5.3");
|
|
68
|
+
assert.equal(r1.provider, "zhipu", "glm-* → zhipu");
|
|
69
|
+
assert.equal(r1.ts, Date.parse("2026-09-01T10:00:01Z"));
|
|
70
|
+
assert.equal(r1.agent, "claude");
|
|
71
|
+
assert.equal(r2.provider, "anthropic", "claude-* → anthropic");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("claude-like: Qoder 0.1.24 空计费(usage 恒 0 且无 model/credits)→ 不产出", () => {
|
|
75
|
+
const f = writeSession("qoder-old", "22222222-2222-3333-4444-555555555555.jsonl", [
|
|
76
|
+
{ type: "assistant", timestamp: "2026-01-29T03:01:57.282Z",
|
|
77
|
+
message: { role: "assistant",
|
|
78
|
+
usage: { cache_creation_input_tokens: 0, cache_read_input_tokens: 0, input_tokens: 0, output_tokens: 0 } } },
|
|
79
|
+
]);
|
|
80
|
+
const rows = parseClaudeLikeSession("qoder", f, "22222222-2222-3333-4444-555555555555");
|
|
81
|
+
assert.equal(rows.length, 0, "Qoder 0.1.24 空计费行必须被过滤");
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("claude-like: QoderCN 1.1.44 model + credits 提取(token 恒 0)", () => {
|
|
85
|
+
const f = writeSession("qodercn", "33333333-2222-3333-4444-555555555555.jsonl", [
|
|
86
|
+
{ type: "assistant", timestamp: "2026-09-05T12:38:10.000Z",
|
|
87
|
+
message: { role: "assistant", model: "qmodel_38max",
|
|
88
|
+
usage: { input_tokens: 0, output_tokens: 0, cache_read_input_tokens: 0, cache_creation_input_tokens: 0,
|
|
89
|
+
credits: 11.14515204, original_credits: 11.14515204, billable: true, context_usage_ratio: 0.185 } } },
|
|
90
|
+
]);
|
|
91
|
+
const rows = parseClaudeLikeSession("qodercn", f, "33333333-2222-3333-4444-555555555555");
|
|
92
|
+
assert.equal(rows.length, 1);
|
|
93
|
+
assert.equal(rows[0].model, "qmodel_38max");
|
|
94
|
+
assert.equal(rows[0].credits, 11.14515204, "credits = Qoder 自有积分,独立字段");
|
|
95
|
+
assert.equal(rows[0].input, 0, "Qoder token 恒 0");
|
|
96
|
+
assert.equal(rows[0].provider, "qoder", "qmodel_* → qoder");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("claude-like: usage 缺失的行跳过;timestamp 缺失跳过", () => {
|
|
100
|
+
const f = writeSession("cc-edge", "44444444-2222-3333-4444-555555555555.jsonl", [
|
|
101
|
+
{ type: "assistant", message: { role: "assistant", model: "glm-4", content: [] } }, // 无 usage
|
|
102
|
+
{ type: "assistant", message: { role: "assistant", model: "glm-4",
|
|
103
|
+
usage: { input_tokens: 5, output_tokens: 5 } } }, // 无 timestamp
|
|
104
|
+
]);
|
|
105
|
+
const rows = parseClaudeLikeSession("claude", f, "44444444-2222-3333-4444-555555555555");
|
|
106
|
+
assert.equal(rows.length, 0, "两者都应被跳过");
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// ── CodeBuddy ──────────────────────────────────────────────────────────────
|
|
110
|
+
|
|
111
|
+
test("codebuddy: providerData.rawUsage 提取(OpenAI 兼容字段名 + reasoning)", () => {
|
|
112
|
+
const f = writeSession("cbc", "55555555-2222-3333-4444-555555555555.jsonl", [
|
|
113
|
+
{ type: "message", timestamp: 1789057352340, role: "assistant",
|
|
114
|
+
providerData: { model: "hy4-preview", requestModelId: "hy4-preview",
|
|
115
|
+
rawUsage: { prompt_tokens: 22869, completion_tokens: 109, total_tokens: 22978,
|
|
116
|
+
cache_read_input_tokens: 0, cache_creation_input_tokens: 0,
|
|
117
|
+
completion_tokens_details: { reasoning_tokens: 76 } } } },
|
|
118
|
+
{ type: "reasoning", timestamp: 1789057233974,
|
|
119
|
+
providerData: { model: "hy4-preview" }, content: [] }, // 非 message 行跳过
|
|
120
|
+
]);
|
|
121
|
+
const rows = parseCodebuddySession(f, "55555555-2222-3333-4444-555555555555");
|
|
122
|
+
assert.equal(rows.length, 1);
|
|
123
|
+
const r = rows[0];
|
|
124
|
+
assert.equal(r.input, 22869);
|
|
125
|
+
assert.equal(r.output, 109);
|
|
126
|
+
assert.equal(r.reasoning, 76);
|
|
127
|
+
assert.equal(r.totalTokens, 22978);
|
|
128
|
+
assert.equal(r.model, "hy4-preview");
|
|
129
|
+
assert.equal(r.provider, "codebuddy", "cbc 无显式 provider → agent 名");
|
|
130
|
+
assert.equal(r.ts, 1789057352340, "cbc timestamp 是数字 epoch 毫秒");
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
// ── ZCode ─────────────────────────────────────────────────────────────────
|
|
134
|
+
|
|
135
|
+
function buildZcodeDb(path, messages) {
|
|
136
|
+
const db = new DatabaseSync(path);
|
|
137
|
+
db.exec(`
|
|
138
|
+
CREATE TABLE IF NOT EXISTS session (id TEXT PRIMARY KEY, directory TEXT, title TEXT, time_created INTEGER, time_updated INTEGER);
|
|
139
|
+
CREATE TABLE IF NOT EXISTS message (id TEXT PRIMARY KEY, session_id TEXT, data TEXT, sequence INTEGER, time_created INTEGER);
|
|
140
|
+
`);
|
|
141
|
+
const ins = db.prepare("INSERT OR REPLACE INTO message (id, session_id, data, sequence) VALUES (?, ?, ?, ?)");
|
|
142
|
+
const seen = new Set();
|
|
143
|
+
for (const m of messages) {
|
|
144
|
+
if (seen.has(m.sid)) continue;
|
|
145
|
+
seen.add(m.sid);
|
|
146
|
+
db.prepare("INSERT OR REPLACE INTO session (id, directory, time_created, time_updated) VALUES (?, '/tmp/x', ?, ?)")
|
|
147
|
+
.run(m.sid, 1_788_000_000_000, 1_788_000_600_000);
|
|
148
|
+
}
|
|
149
|
+
messages.forEach((m, i) => ins.run(`msg-${i}`, m.sid, m.data, m.seq ?? i));
|
|
150
|
+
db.close();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
test("zcode: tokens 会话累积值差分(input/cacheRead 逐条累积)", () => {
|
|
154
|
+
const dbPath = join(ROOT, "zcode-test.sqlite");
|
|
155
|
+
const sid = "66666666-2222-3333-4444-555555555555";
|
|
156
|
+
buildZcodeDb(dbPath, [
|
|
157
|
+
// 第一条:无前值,input/cacheRead 直取
|
|
158
|
+
{ sid, seq: 0, data: JSON.stringify({
|
|
159
|
+
role: "assistant", modelID: "GLM-5.3-Flash", providerID: "builtin:bigmodel-coding-plan",
|
|
160
|
+
time: { created: 1788664736131 },
|
|
161
|
+
tokens: { total: 38889, input: 38562, output: 327, reasoning: 0, cache: { read: 16384, write: 0 } },
|
|
162
|
+
cost: 0 }) },
|
|
163
|
+
// 第二条:input 38562→39374(增 812)、cacheR 16384→38528(增 22144)、output 单轮直取
|
|
164
|
+
{ sid, seq: 1, data: JSON.stringify({
|
|
165
|
+
role: "assistant", modelID: "GLM-5.3-Flash", providerID: "bigmodel",
|
|
166
|
+
time: { created: 1788664736132 },
|
|
167
|
+
tokens: { total: 39456, input: 39374, output: 82, reasoning: 0, cache: { read: 38528, write: 0 } },
|
|
168
|
+
cost: 0 }) },
|
|
169
|
+
// timeline_event 噪音(tokens 全 0 + cost 0)→ 剔除
|
|
170
|
+
{ sid, seq: 2, data: JSON.stringify({
|
|
171
|
+
role: "assistant", modelID: "GLM-5.3-Flash", providerID: "bigmodel",
|
|
172
|
+
time: { created: 1788664736133 },
|
|
173
|
+
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
|
|
174
|
+
cost: 0, finish: "completed", semantics: { kind: "timeline_event" } }) },
|
|
175
|
+
// user 行跳过
|
|
176
|
+
{ sid, seq: 3, data: JSON.stringify({
|
|
177
|
+
role: "user", time: { created: 1788664736000 }, content: "hi" }) },
|
|
178
|
+
]);
|
|
179
|
+
const rows = parseZcodeSession(dbPath, sid);
|
|
180
|
+
assert.equal(rows.length, 2, "只产出 2 条真实 assistant;timeline 噪音与 user 行剔除");
|
|
181
|
+
const [r1, r2] = rows;
|
|
182
|
+
assert.equal(r1.input, 38562, "首条 input 直取");
|
|
183
|
+
assert.equal(r1.output, 327);
|
|
184
|
+
assert.equal(r1.cacheRead, 16384);
|
|
185
|
+
assert.equal(r1.totalTokens, 38889, "total 首条直取");
|
|
186
|
+
assert.equal(r2.input, 812, "第二条 input = 39374 − 38562(差分)");
|
|
187
|
+
assert.equal(r2.cacheRead, 22144, "第二条 cacheRead = 38528 − 16384(差分)");
|
|
188
|
+
assert.equal(r2.output, 82, "output 单轮直取");
|
|
189
|
+
assert.equal(r2.totalTokens, 39456 - 38889, "total 差分");
|
|
190
|
+
assert.equal(r2.provider, "bigmodel", "builtin:/-coding-plan 后缀归一");
|
|
191
|
+
assert.equal(r2.agent, "zcode");
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test("zcode: 3.16.3+ 新键名 modelId/providerId + account: provider 归一", () => {
|
|
195
|
+
const dbPath = join(ROOT, "zcode-test-v3163.sqlite");
|
|
196
|
+
const sid = "77777777-2222-3333-4444-555555555555";
|
|
197
|
+
buildZcodeDb(dbPath, [
|
|
198
|
+
{ sid, seq: 0, data: JSON.stringify({
|
|
199
|
+
role: "assistant", modelId: "GLM-5.3-Flash", providerId: "account:bigmodel-start-plan",
|
|
200
|
+
time: { created: 1788664740000 },
|
|
201
|
+
tokens: { total: 50000, input: 49500, output: 500, reasoning: 0, cache: { read: 30000, write: 0 } },
|
|
202
|
+
cost: 0 }) },
|
|
203
|
+
]);
|
|
204
|
+
const rows = parseZcodeSession(dbPath, sid);
|
|
205
|
+
assert.equal(rows.length, 1);
|
|
206
|
+
assert.equal(rows[0].model, "GLM-5.3-Flash", "新键 modelId 读取");
|
|
207
|
+
assert.equal(rows[0].provider, "bigmodel", "account: 前缀 + -start-plan 后缀归一");
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
// ── provider 推断 ─────────────────────────────────────────────────────────
|
|
211
|
+
|
|
212
|
+
test("inferProvider: 前缀推断表", () => {
|
|
213
|
+
assert.equal(inferProvider("claude", "glm-5.3"), "zhipu");
|
|
214
|
+
assert.equal(inferProvider("claude", "claude-sonnet-4-5"), "anthropic");
|
|
215
|
+
assert.equal(inferProvider("qodercn", "qmodel_38max"), "qoder");
|
|
216
|
+
assert.equal(inferProvider("codex", "nvidia/nemotron-3.5"), "nvidia");
|
|
217
|
+
assert.equal(inferProvider("codex", "openai/gpt-5"), "openai");
|
|
218
|
+
assert.equal(inferProvider("codebuddy", "hy4-preview"), "codebuddy");
|
|
219
|
+
});
|