@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.
Files changed (131) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +36 -2
  3. package/agent-accounts.test.mjs +227 -0
  4. package/agent-accounts.ts +324 -0
  5. package/agent-capability-contract.test.mjs +129 -0
  6. package/agent-interaction.test.mjs +194 -0
  7. package/agent-interaction.ts +340 -0
  8. package/agent-launch.test.mjs +262 -0
  9. package/agent-spawn-env.test.mjs +169 -0
  10. package/agent-spawn-env.ts +548 -0
  11. package/aiti-editor-inject.mjs +49 -0
  12. package/backend-queue.test.mjs +336 -0
  13. package/backend-queue.ts +469 -0
  14. package/blob-save-route.ts +128 -0
  15. package/cbc-model-catalog.ts +86 -0
  16. package/check-client.mjs +273 -0
  17. package/check-ext-globals.mjs +144 -0
  18. package/controlled-login-dialog.mjs +270 -0
  19. package/controlled-login.mjs +398 -0
  20. package/controlled-login.test.mjs +466 -0
  21. package/convert-saveas.test.mjs +39 -0
  22. package/convert-saveas.ts +170 -0
  23. package/copy-client-bundle.mjs +39 -0
  24. package/cursor-cli-store.ts +377 -0
  25. package/dedupe-fetch.test.mjs +71 -0
  26. package/dedupe-fetch.ts +58 -0
  27. package/deploy-commit.mjs +52 -0
  28. package/dev-pack.mjs +246 -0
  29. package/dock-icons.ts +127 -0
  30. package/esbuild-bin.test.mjs +53 -0
  31. package/esbuild-bin.ts +66 -0
  32. package/exec-async.test.mjs +86 -0
  33. package/exec-async.ts +179 -0
  34. package/ext-display-names.ts +91 -0
  35. package/ext-entry-contracts.test.mjs +131 -0
  36. package/ext-float-dock-core.test.mjs +348 -0
  37. package/ext-float-dock-core.ts +783 -0
  38. package/ext-float-dock-lite.ts +52 -0
  39. package/ext-float-dock.test.mjs +286 -0
  40. package/ext-float-dock.ts +740 -0
  41. package/ext-icons-deploy.mjs +24 -0
  42. package/ext-lazy.test.mjs +186 -0
  43. package/ext-lazy.tsx +183 -0
  44. package/ext-model-state.test.mjs +272 -0
  45. package/ext-model-state.ts +324 -0
  46. package/ext-protocol.mjs +113 -0
  47. package/ext-protocol.test.mjs +88 -0
  48. package/ext-session-usage.test.mjs +219 -0
  49. package/ext-session-usage.ts +507 -0
  50. package/external-cli-governance.ts +153 -0
  51. package/git-ops-client/api.mjs +56 -0
  52. package/git-ops-client/branch-panel.tsx +987 -0
  53. package/git-ops-client/i18n.mjs +328 -0
  54. package/host-tools-allow.test.mjs +46 -0
  55. package/host-tools-allow.ts +84 -0
  56. package/icons/alert.svg +1 -0
  57. package/icons/bookmark.svg +1 -0
  58. package/icons/bot.svg +1 -0
  59. package/icons/chart-column.svg +1 -0
  60. package/icons/check-circle.svg +1 -0
  61. package/icons/clipboard-list.svg +1 -0
  62. package/icons/clock.svg +1 -0
  63. package/icons/coffee.svg +1 -0
  64. package/icons/edit.svg +1 -0
  65. package/icons/eye.svg +1 -0
  66. package/icons/git-branch.svg +1 -0
  67. package/icons/git-commit-horizontal.svg +1 -0
  68. package/icons/globe.svg +1 -0
  69. package/icons/history.svg +1 -0
  70. package/icons/import.svg +1 -0
  71. package/icons/inbox.svg +1 -0
  72. package/icons/key-round.svg +1 -0
  73. package/icons/layout-grid.svg +1 -0
  74. package/icons/mail.svg +1 -0
  75. package/icons/message-circle.svg +1 -0
  76. package/icons/message-square.svg +1 -0
  77. package/icons/meter-vertical.svg +1 -0
  78. package/icons/mic.svg +1 -0
  79. package/icons/monitor.svg +1 -0
  80. package/icons/pen-line.svg +1 -0
  81. package/icons/plug.svg +1 -0
  82. package/icons/radio-dot.svg +1 -0
  83. package/icons/radio.svg +1 -0
  84. package/icons/refresh.svg +1 -0
  85. package/icons/rocket.svg +1 -0
  86. package/icons/search.svg +1 -0
  87. package/icons/shield.svg +1 -0
  88. package/icons/sparkles.svg +1 -0
  89. package/icons/square-kanban.svg +1 -0
  90. package/icons/terminal.svg +1 -0
  91. package/icons/users.svg +1 -0
  92. package/icons/volume-2.svg +1 -0
  93. package/icons/wand.svg +1 -0
  94. package/icons/wrench.svg +1 -0
  95. package/icons/x-circle.svg +1 -0
  96. package/icons/x.svg +1 -0
  97. package/ignore-semantics.ts +76 -0
  98. package/lazy-loading-metrics.mjs +61 -0
  99. package/new-session-cwd.test.mjs +389 -0
  100. package/new-session-cwd.ts +253 -0
  101. package/open-viewer-window.ts +65 -0
  102. package/overlay-back-client.test.mjs +123 -0
  103. package/overlay-back-client.ts +87 -0
  104. package/package.json +65 -4
  105. package/pi-fork-rpc/README.md +12 -0
  106. package/pi-fork-rpc/cli-resolve.test.mjs +42 -0
  107. package/pi-fork-rpc/cli-resolve.ts +79 -0
  108. package/pi-fork-rpc/remote-queue.test.mjs +100 -0
  109. package/pi-fork-rpc/remote-queue.ts +140 -0
  110. package/pi-fork-rpc/spec.ts +106 -0
  111. package/pi-fork-rpc/wire.test.mjs +260 -0
  112. package/pi-fork-rpc/wire.ts +367 -0
  113. package/popup-anchor.ts +45 -0
  114. package/provider-priority-contract.test.mjs +55 -0
  115. package/reconcile-deps.mjs +41 -0
  116. package/reload-instances.mjs +60 -0
  117. package/seed-tool.test.mjs +95 -0
  118. package/seed-tool.ts +88 -0
  119. package/session-recycle.ts +109 -0
  120. package/shared-copy-sync.test.mjs +115 -0
  121. package/soffice-locate.ts +171 -0
  122. package/soffice-panel.ts +239 -0
  123. package/sync-dir.mjs +91 -0
  124. package/sync-dir.test.mjs +101 -0
  125. package/sync-gate.test.mjs +93 -0
  126. package/sync-mirror.test.mjs +85 -0
  127. package/task-list.test.mjs +91 -0
  128. package/task-list.ts +118 -0
  129. package/test-rm-rf.mjs +33 -0
  130. package/ui-dialogs.test.mjs +198 -0
  131. 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
+ });