@miles-huang/cove 0.1.4 → 0.1.5
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/bin/cove.js +220 -9
- package/package.json +1 -1
- package/server/agent/messages.js +213 -4
- package/server/agent/session-hub.js +305 -22
- package/server/app.js +16 -1
- package/server/cli.js +47 -25
- package/server/index.js +3 -0
- package/server/routes/meta.js +4 -2
- package/server/routes/sessions.js +30 -0
- package/server/routes/update.js +36 -0
- package/server/routes/uploads.js +100 -0
- package/server/routes/voice.js +95 -0
- package/server/server.js +46 -11
- package/server/updater.js +484 -0
- package/server/voice.js +618 -0
- package/shared/protocol.js +8 -0
- package/web/assets/{abnfDiagram-VCTEODGH-BvfDeUDt.js → abnfDiagram-VCTEODGH-CzbWgcsF.js} +1 -1
- package/web/assets/{architectureDiagram-5GKGNRK7-B6x-Vdta.js → architectureDiagram-5GKGNRK7-jpqUHglp.js} +1 -1
- package/web/assets/{blockDiagram-I7D4REHJ-DSdow19J.js → blockDiagram-I7D4REHJ-CaDKtZbM.js} +1 -1
- package/web/assets/{c4Diagram-7LVT6UL2-DNANA_BY.js → c4Diagram-7LVT6UL2-DSzOjXhU.js} +1 -1
- package/web/assets/channel-CyiORw9T.js +1 -0
- package/web/assets/{chunk-2Q5K7J3B-DQhKxJim.js → chunk-2Q5K7J3B-jC1KoHVE.js} +1 -1
- package/web/assets/{chunk-5VM5RSS4-CxYuf8nJ.js → chunk-5VM5RSS4-D6X5RslJ.js} +1 -1
- package/web/assets/{chunk-F27PBJKO-pm7TdZp3.js → chunk-F27PBJKO-BbRLxBSh.js} +1 -1
- package/web/assets/{chunk-IMKFNOWR-BOUFtEzN.js → chunk-IMKFNOWR-y223YRpm.js} +1 -1
- package/web/assets/{chunk-JWPE2WC7-AmPhkmIF.js → chunk-JWPE2WC7-s-_Lm4OD.js} +1 -1
- package/web/assets/{chunk-POPQ4Y6H-DAjm7eYi.js → chunk-POPQ4Y6H-FAjR47qb.js} +1 -1
- package/web/assets/{chunk-SVP7TREG-CPIhAIng.js → chunk-SVP7TREG-V3BsTVSS.js} +1 -1
- package/web/assets/{chunk-TICWLB2K-zdUNYXIa.js → chunk-TICWLB2K-BHXaSfO0.js} +1 -1
- package/web/assets/{chunk-XXDRQBXY-HxMWsKyb.js → chunk-XXDRQBXY-uzA700bf.js} +1 -1
- package/web/assets/classDiagram-ZZMXUADV-gyexRBgY.js +1 -0
- package/web/assets/classDiagram-v2-VYDZK3BY-Bc9eynfM.js +1 -0
- package/web/assets/{cose-bilkent-JH36ORCC-lYmJLW2h.js → cose-bilkent-JH36ORCC-B6XhcmxK.js} +1 -1
- package/web/assets/{cynefin-OW5HDTMX-C8UcJePz.js → cynefin-OW5HDTMX-CCabvWFn.js} +1 -1
- package/web/assets/{cynefinDiagram-5FMLGOSQ-Dt1tZqyf.js → cynefinDiagram-5FMLGOSQ-D3FXPxq1.js} +1 -1
- package/web/assets/{dagre-GXQ25YYZ-Y0qiXkMO.js → dagre-GXQ25YYZ-eitZh1b1.js} +1 -1
- package/web/assets/{diagram-S7CK7UJ4-BtcdW_SA.js → diagram-S7CK7UJ4-B_523Ard.js} +1 -1
- package/web/assets/{diagram-UQ7AKVKN-DMwC8JNI.js → diagram-UQ7AKVKN-DLK2D1Lf.js} +1 -1
- package/web/assets/{diagram-VSXAHHWV-BVU0Bm6u.js → diagram-VSXAHHWV-IqWaTga0.js} +1 -1
- package/web/assets/{diagram-VX7I27RA-BYlx-nH2.js → diagram-VX7I27RA-D42Xhhqj.js} +1 -1
- package/web/assets/{diagram-Z3DM3KII-C_oAUIPq.js → diagram-Z3DM3KII-B38ZR6s3.js} +1 -1
- package/web/assets/{ebnfDiagram-PWID7BFC-D9eTa0_d.js → ebnfDiagram-PWID7BFC-Bto2hjgy.js} +1 -1
- package/web/assets/{erDiagram-RLTQ6QDP-Cn00lD_I.js → erDiagram-RLTQ6QDP-BbrcgCtr.js} +1 -1
- package/web/assets/{flowDiagram-HODETNUW-CBjFObVm.js → flowDiagram-HODETNUW-BccX2IC1.js} +1 -1
- package/web/assets/{ganttDiagram-EL5Y4UJY-BrJFpYUI.js → ganttDiagram-EL5Y4UJY-CIVhLWAs.js} +1 -1
- package/web/assets/{gitGraphDiagram-WWUBYQGX-CgDCGY4x.js → gitGraphDiagram-WWUBYQGX-CiQgydKF.js} +1 -1
- package/web/assets/{highlighted-body-6RAV4W6J-BaouqEZW.js → highlighted-body-6RAV4W6J-eKTYB7cG.js} +1 -1
- package/web/assets/index-8rrMUwb2.css +1 -0
- package/web/assets/index-DFKPpT1o.js +1289 -0
- package/web/assets/{infoDiagram-27XIBGKW-4GAuy2r7.js → infoDiagram-27XIBGKW-C3mfF7LW.js} +1 -1
- package/web/assets/{ishikawaDiagram-5VMMS53U-CDZ9dPA6.js → ishikawaDiagram-5VMMS53U-XWdz8N0o.js} +1 -1
- package/web/assets/{journeyDiagram-3NMN7TZE-D0uXeMh-.js → journeyDiagram-3NMN7TZE-CtvvC10N.js} +1 -1
- package/web/assets/{kanban-definition-UXKFOSKX-C4H7MD6w.js → kanban-definition-UXKFOSKX-uhxTqXnW.js} +1 -1
- package/web/assets/{layout-BFfV8fTs.js → layout-BQ419s8Q.js} +1 -1
- package/web/assets/{mindmap-definition-YA3MSWOX-Dd7Obl6F.js → mindmap-definition-YA3MSWOX-Cg7HolUO.js} +1 -1
- package/web/assets/{pegDiagram-XKGWAZYB-nDep-Xjv.js → pegDiagram-XKGWAZYB-Ca_T_k1c.js} +1 -1
- package/web/assets/{pieDiagram-E7YTZNPT-C8783QQe.js → pieDiagram-E7YTZNPT-Cqu0K07s.js} +1 -1
- package/web/assets/{quadrantDiagram-AXDQQJYC-CH5o8mIt.js → quadrantDiagram-AXDQQJYC-wK7kSaZg.js} +1 -1
- package/web/assets/{railroadDiagram-O6MQD6OU-QPxQi08o.js → railroadDiagram-O6MQD6OU-Yt2QDvBQ.js} +1 -1
- package/web/assets/{requirementDiagram-BXWQKSXE-DVr0s9y9.js → requirementDiagram-BXWQKSXE-BnUkaWq7.js} +1 -1
- package/web/assets/{sankeyDiagram-P5KCCOFB-Bq3TJeq8.js → sankeyDiagram-P5KCCOFB-M_ig6yCr.js} +1 -1
- package/web/assets/{sequenceDiagram-WJ2MYXX4-VWpvqhZf.js → sequenceDiagram-WJ2MYXX4-BZsd1fkB.js} +1 -1
- package/web/assets/{sizeCapture-INFHLROL-1n-6h_CN.js → sizeCapture-INFHLROL-QGHTzTuy.js} +1 -1
- package/web/assets/{stateDiagram-D77RDMKH-cmZfkPUb.js → stateDiagram-D77RDMKH-B-fkTedS.js} +1 -1
- package/web/assets/stateDiagram-v2-MP3YSRHH-BUXoRLIu.js +1 -0
- package/web/assets/{swimlanes-42K2YHIH-BqTB27WF.js → swimlanes-42K2YHIH-BP-raAm4.js} +1 -1
- package/web/assets/swimlanesDiagram-VR7AAH4N-ClyXkjH5.js +8 -0
- package/web/assets/{timeline-definition-24CTP7MA--BCgTKxF.js → timeline-definition-24CTP7MA-CDTB4Ri6.js} +1 -1
- package/web/assets/{vennDiagram-4TSXK5OY-BJaPku01.js → vennDiagram-4TSXK5OY-DFmr7Hbu.js} +1 -1
- package/web/assets/{wardleyDiagram-VM6X3IG4-n2YbPRWQ.js → wardleyDiagram-VM6X3IG4-DrhRjyVy.js} +1 -1
- package/web/assets/{xychartDiagram-S5SC5T6Z-CQpktgwa.js → xychartDiagram-S5SC5T6Z-CvH_0Dcr.js} +1 -1
- package/web/index.html +2 -2
- package/web/assets/channel-CyEPAFBz.js +0 -1
- package/web/assets/classDiagram-ZZMXUADV-DqTY3X0j.js +0 -1
- package/web/assets/classDiagram-v2-VYDZK3BY-BSxiMdF_.js +0 -1
- package/web/assets/index-4qPcqjHt.js +0 -1259
- package/web/assets/index-5AGKaJhL.css +0 -1
- package/web/assets/stateDiagram-v2-MP3YSRHH-wEaM0KsS.js +0 -1
- package/web/assets/swimlanesDiagram-VR7AAH4N-CjIKX5n2.js +0 -8
package/bin/cove.js
CHANGED
|
@@ -1,15 +1,226 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { readFileSync } from "node:fs";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
5
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
6
|
+
import { parseEnv } from "node:util";
|
|
5
7
|
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
+
const SEMVER_RE = /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/;
|
|
9
|
+
const RELEASE_DIR_RE = /^v\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/;
|
|
10
|
+
const IGNORED_ENV_ERRORS = new Set(["ENOENT", "EACCES", "EPERM", "ENOTDIR"]);
|
|
8
11
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
function parseSemver(value) {
|
|
13
|
+
if (typeof value !== "string") return null;
|
|
14
|
+
const match = SEMVER_RE.exec(value.trim());
|
|
15
|
+
if (!match) return null;
|
|
16
|
+
return {
|
|
17
|
+
major: Number(match[1]),
|
|
18
|
+
minor: Number(match[2]),
|
|
19
|
+
patch: Number(match[3]),
|
|
20
|
+
prerelease: match[4] ? match[4].split(".") : [],
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function compareSemver(a, b) {
|
|
25
|
+
const pa = parseSemver(a);
|
|
26
|
+
const pb = parseSemver(b);
|
|
27
|
+
if (!pa && !pb) return a === b ? 0 : a < b ? -1 : 1;
|
|
28
|
+
if (!pa) return -1;
|
|
29
|
+
if (!pb) return 1;
|
|
30
|
+
if (pa.major !== pb.major) return pa.major < pb.major ? -1 : 1;
|
|
31
|
+
if (pa.minor !== pb.minor) return pa.minor < pb.minor ? -1 : 1;
|
|
32
|
+
if (pa.patch !== pb.patch) return pa.patch < pb.patch ? -1 : 1;
|
|
33
|
+
if (pa.prerelease.length === 0 && pb.prerelease.length === 0) return 0;
|
|
34
|
+
if (pa.prerelease.length === 0) return 1;
|
|
35
|
+
if (pb.prerelease.length === 0) return -1;
|
|
36
|
+
const maxLen = Math.max(pa.prerelease.length, pb.prerelease.length);
|
|
37
|
+
for (let i = 0; i < maxLen; i += 1) {
|
|
38
|
+
const idA = pa.prerelease[i];
|
|
39
|
+
const idB = pb.prerelease[i];
|
|
40
|
+
if (idA === undefined) return -1;
|
|
41
|
+
if (idB === undefined) return 1;
|
|
42
|
+
if (idA === idB) continue;
|
|
43
|
+
const numA = /^\d+$/.test(idA);
|
|
44
|
+
const numB = /^\d+$/.test(idB);
|
|
45
|
+
if (numA && numB) return Number(idA) < Number(idB) ? -1 : 1;
|
|
46
|
+
if (numA) return -1;
|
|
47
|
+
if (numB) return 1;
|
|
48
|
+
return idA < idB ? -1 : 1;
|
|
49
|
+
}
|
|
50
|
+
return 0;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function readEnvFile(filePath, optional) {
|
|
54
|
+
try {
|
|
55
|
+
const parsed = parseEnv(readFileSync(filePath, "utf8"));
|
|
56
|
+
return parsed && typeof parsed.DATA_DIR === "string" ? parsed.DATA_DIR.trim() : undefined;
|
|
57
|
+
} catch (error) {
|
|
58
|
+
if (optional && error && IGNORED_ENV_ERRORS.has(error.code)) {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function extractBootstrapArgs(argv) {
|
|
66
|
+
let dataDir;
|
|
67
|
+
let envFile;
|
|
68
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
69
|
+
const arg = argv[i];
|
|
70
|
+
if (arg === "--") break;
|
|
71
|
+
if (arg === "-d" || arg === "--data-dir") {
|
|
72
|
+
if (i + 1 < argv.length) dataDir = argv[++i];
|
|
73
|
+
} else if (arg.startsWith("--data-dir=")) {
|
|
74
|
+
dataDir = arg.slice("--data-dir=".length);
|
|
75
|
+
} else if (arg === "-e" || arg === "--env-file") {
|
|
76
|
+
if (i + 1 < argv.length) envFile = argv[++i];
|
|
77
|
+
} else if (arg.startsWith("--env-file=")) {
|
|
78
|
+
envFile = arg.slice("--env-file=".length);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return { dataDir, envFile };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function resolveBootstrapDataDir(argv) {
|
|
85
|
+
const cwd = process.cwd();
|
|
86
|
+
const home = homedir();
|
|
87
|
+
const { dataDir: cliDataDir, envFile: cliEnvFile } = extractBootstrapArgs(argv);
|
|
88
|
+
if (cliDataDir && cliDataDir.trim()) {
|
|
89
|
+
return resolve(cwd, cliDataDir.trim());
|
|
90
|
+
}
|
|
91
|
+
if (process.env.DATA_DIR && process.env.DATA_DIR.trim()) {
|
|
92
|
+
return resolve(cwd, process.env.DATA_DIR.trim());
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
let envDataDir;
|
|
96
|
+
if (cliEnvFile !== undefined) {
|
|
97
|
+
envDataDir = readEnvFile(resolve(cwd, cliEnvFile), true);
|
|
98
|
+
} else {
|
|
99
|
+
const homeEnvPath = join(home, ".cove", ".env");
|
|
100
|
+
const homeVal = readEnvFile(homeEnvPath, true);
|
|
101
|
+
if (homeVal) envDataDir = homeVal;
|
|
102
|
+
const cwdEnvPath = resolve(cwd, ".env");
|
|
103
|
+
if (resolve(homeEnvPath) !== cwdEnvPath) {
|
|
104
|
+
const cwdVal = readEnvFile(cwdEnvPath, true);
|
|
105
|
+
if (cwdVal) envDataDir = cwdVal;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (envDataDir) {
|
|
109
|
+
return resolve(cwd, envDataDir);
|
|
110
|
+
}
|
|
111
|
+
return join(home, ".cove", "data");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function isContainedIn(root, target) {
|
|
115
|
+
const rel = relative(root, target);
|
|
116
|
+
return rel !== "" && rel !== ".." && !rel.startsWith(".." + sep) && !isAbsolute(rel);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function resolveOverlay(argv, bundledPkg) {
|
|
120
|
+
try {
|
|
121
|
+
const dataDir = resolveBootstrapDataDir(argv);
|
|
122
|
+
if (!existsSync(dataDir)) return null;
|
|
123
|
+
const realDataDir = realpathSync(dataDir);
|
|
124
|
+
const activePath = join(dataDir, "runtime", "active.json");
|
|
125
|
+
if (!existsSync(activePath)) return null;
|
|
126
|
+
const realActive = realpathSync(activePath);
|
|
127
|
+
if (!isContainedIn(realDataDir, realActive)) return null;
|
|
128
|
+
|
|
129
|
+
const active = JSON.parse(readFileSync(realActive, "utf8"));
|
|
130
|
+
if (
|
|
131
|
+
!active ||
|
|
132
|
+
typeof active.version !== "string" ||
|
|
133
|
+
!parseSemver(active.version) ||
|
|
134
|
+
typeof active.releaseDir !== "string" ||
|
|
135
|
+
!RELEASE_DIR_RE.test(active.releaseDir)
|
|
136
|
+
) {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
if (compareSemver(active.version, bundledPkg.version) <= 0) {
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const pkgParts = String(bundledPkg.name || "@miles-huang/cove").split("/");
|
|
144
|
+
const pkgRoot = join(dataDir, "runtime", "releases", active.releaseDir, "node_modules", ...pkgParts);
|
|
145
|
+
const overlayPkgPath = join(pkgRoot, "package.json");
|
|
146
|
+
const overlayCliPath = join(pkgRoot, "server", "cli.js");
|
|
147
|
+
const overlayWebDir = join(pkgRoot, "web");
|
|
148
|
+
const overlayWebIndex = join(overlayWebDir, "index.html");
|
|
149
|
+
|
|
150
|
+
if (!existsSync(overlayPkgPath) || !existsSync(overlayCliPath) || !existsSync(overlayWebIndex)) {
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const realPkgPath = realpathSync(overlayPkgPath);
|
|
155
|
+
const realCliPath = realpathSync(overlayCliPath);
|
|
156
|
+
const realWebDir = realpathSync(overlayWebDir);
|
|
157
|
+
const realWebIndex = realpathSync(overlayWebIndex);
|
|
158
|
+
if (
|
|
159
|
+
!isContainedIn(realDataDir, realPkgPath) ||
|
|
160
|
+
!isContainedIn(realDataDir, realCliPath) ||
|
|
161
|
+
!isContainedIn(realDataDir, realWebDir) ||
|
|
162
|
+
!isContainedIn(realDataDir, realWebIndex)
|
|
163
|
+
) {
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const overlayPkg = JSON.parse(readFileSync(realPkgPath, "utf8"));
|
|
168
|
+
if (overlayPkg.version !== active.version) {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
return {
|
|
172
|
+
version: overlayPkg.version,
|
|
173
|
+
cliPath: realCliPath,
|
|
174
|
+
webDir: realWebDir,
|
|
175
|
+
};
|
|
176
|
+
} catch {
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async function bootstrap() {
|
|
182
|
+
const argv = process.argv.slice(2);
|
|
183
|
+
const bundledPkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
184
|
+
const candidate = resolveOverlay(argv, bundledPkg);
|
|
185
|
+
|
|
186
|
+
let loadedOverlay = null;
|
|
187
|
+
if (candidate) {
|
|
188
|
+
try {
|
|
189
|
+
const mod = await import(pathToFileURL(candidate.cliPath).href);
|
|
190
|
+
if (typeof mod.runCli !== "function") {
|
|
191
|
+
throw new Error("overlay server/cli.js 缺少 runCli 导出");
|
|
192
|
+
}
|
|
193
|
+
loadedOverlay = {
|
|
194
|
+
runCli: mod.runCli,
|
|
195
|
+
version: candidate.version,
|
|
196
|
+
webDistDir: candidate.webDir,
|
|
197
|
+
};
|
|
198
|
+
} catch (error) {
|
|
199
|
+
console.error(
|
|
200
|
+
"加载隔离更新版本 v" + candidate.version + " 失败,回退至内置版本 v" + bundledPkg.version + ":",
|
|
201
|
+
error instanceof Error ? error.message : error,
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (loadedOverlay) {
|
|
207
|
+
await loadedOverlay.runCli(argv, {
|
|
208
|
+
version: loadedOverlay.version,
|
|
209
|
+
webDistDir: loadedOverlay.webDistDir,
|
|
210
|
+
runtimeMode: "overlay",
|
|
211
|
+
});
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const { runCli } = await import("../server/cli.js");
|
|
216
|
+
await runCli(argv, {
|
|
217
|
+
version: bundledPkg.version,
|
|
218
|
+
webDistDir: fileURLToPath(new URL("../web", import.meta.url)),
|
|
219
|
+
runtimeMode: "bundled",
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
bootstrap().catch((error) => {
|
|
13
224
|
console.error("启动失败:", error instanceof Error ? error.message : error);
|
|
14
225
|
process.exitCode = 1;
|
|
15
226
|
});
|
package/package.json
CHANGED
package/server/agent/messages.js
CHANGED
|
@@ -1,9 +1,21 @@
|
|
|
1
|
+
import { calculateContextTokens, estimateTokens, } from "@earendil-works/pi-coding-agent";
|
|
1
2
|
import { clampThinkingLevelForDescriptor, DEFAULT_THINKING_LEVEL, isThinkingLevel, } from "../../shared/protocol.js";
|
|
2
3
|
export function contentText(content) {
|
|
3
4
|
return typeof content === "string"
|
|
4
5
|
? content
|
|
5
6
|
: content.filter((part) => part.type === "text").map((part) => part.text ?? "").join("\n");
|
|
6
7
|
}
|
|
8
|
+
function extractUserImages(content) {
|
|
9
|
+
if (typeof content === "string")
|
|
10
|
+
return [];
|
|
11
|
+
const images = [];
|
|
12
|
+
for (const part of content) {
|
|
13
|
+
if (part.type === "image" && typeof part.data === "string" && typeof part.mimeType === "string") {
|
|
14
|
+
images.push({ data: part.data, mimeType: part.mimeType });
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return images;
|
|
18
|
+
}
|
|
7
19
|
export function summarizeTool(toolName, args) {
|
|
8
20
|
const action = { read: "读取", write: "写入", edit: "编辑", bash: "执行" };
|
|
9
21
|
if (!Object.hasOwn(action, toolName))
|
|
@@ -18,6 +30,7 @@ export function summarizeTool(toolName, args) {
|
|
|
18
30
|
return `${label} ${line.length > 100 ? `${line.slice(0, 97)}…` : line}`;
|
|
19
31
|
}
|
|
20
32
|
export const TURN_TIMING_CUSTOM_TYPE = "cpa:turn_timing";
|
|
33
|
+
export const USER_ATTACHMENTS_CUSTOM_TYPE = "cpa:user_attachments";
|
|
21
34
|
function collectTurnTimings(entries) {
|
|
22
35
|
const timings = new Map();
|
|
23
36
|
for (const entry of entries) {
|
|
@@ -32,17 +45,66 @@ function collectTurnTimings(entries) {
|
|
|
32
45
|
const durationMs = typeof candidate.durationMs === "number" && Number.isFinite(candidate.durationMs) && candidate.durationMs >= 0
|
|
33
46
|
? Math.round(candidate.durationMs)
|
|
34
47
|
: undefined;
|
|
48
|
+
const generationDurationMs = typeof candidate.generationDurationMs === "number" &&
|
|
49
|
+
Number.isFinite(candidate.generationDurationMs) &&
|
|
50
|
+
candidate.generationDurationMs >= 0
|
|
51
|
+
? Math.round(candidate.generationDurationMs)
|
|
52
|
+
: undefined;
|
|
35
53
|
const thinkingDurations = Array.isArray(candidate.thinkingDurations)
|
|
36
|
-
? candidate.thinkingDurations
|
|
54
|
+
? candidate.thinkingDurations
|
|
55
|
+
.filter((v) => typeof v === "number" && Number.isFinite(v) && v >= 0)
|
|
56
|
+
.map((v) => Math.round(v))
|
|
37
57
|
: undefined;
|
|
38
58
|
timings.set(candidate.entryId, {
|
|
39
59
|
entryId: candidate.entryId,
|
|
40
60
|
...(durationMs !== undefined ? { durationMs } : {}),
|
|
61
|
+
...(generationDurationMs !== undefined ? { generationDurationMs } : {}),
|
|
41
62
|
...(thinkingDurations && thinkingDurations.length > 0 ? { thinkingDurations } : {}),
|
|
42
63
|
});
|
|
43
64
|
}
|
|
44
65
|
return timings;
|
|
45
66
|
}
|
|
67
|
+
function collectUserAttachments(entries) {
|
|
68
|
+
const result = new Map();
|
|
69
|
+
for (const entry of entries) {
|
|
70
|
+
if (entry.type !== "custom" || entry.customType !== USER_ATTACHMENTS_CUSTOM_TYPE)
|
|
71
|
+
continue;
|
|
72
|
+
const raw = entry.data;
|
|
73
|
+
if (!raw || typeof raw !== "object")
|
|
74
|
+
continue;
|
|
75
|
+
const candidate = raw;
|
|
76
|
+
if (typeof candidate.entryId !== "string" || !candidate.entryId)
|
|
77
|
+
continue;
|
|
78
|
+
if (typeof candidate.displayContent !== "string")
|
|
79
|
+
continue;
|
|
80
|
+
const rawAttachments = Array.isArray(candidate.attachments) ? candidate.attachments : [];
|
|
81
|
+
const attachments = [];
|
|
82
|
+
for (const item of rawAttachments) {
|
|
83
|
+
if (!item || typeof item !== "object")
|
|
84
|
+
continue;
|
|
85
|
+
const a = item;
|
|
86
|
+
if (typeof a.id === "string" &&
|
|
87
|
+
typeof a.name === "string" &&
|
|
88
|
+
typeof a.mimeType === "string" &&
|
|
89
|
+
typeof a.size === "number" &&
|
|
90
|
+
typeof a.workspacePath === "string") {
|
|
91
|
+
attachments.push({
|
|
92
|
+
id: a.id,
|
|
93
|
+
name: a.name,
|
|
94
|
+
mimeType: a.mimeType,
|
|
95
|
+
size: a.size,
|
|
96
|
+
workspacePath: a.workspacePath,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
result.set(candidate.entryId, {
|
|
101
|
+
entryId: candidate.entryId,
|
|
102
|
+
displayContent: candidate.displayContent,
|
|
103
|
+
attachments,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
46
108
|
export function projectSessionMessages(entries, defaults = {
|
|
47
109
|
provider: "",
|
|
48
110
|
model: "",
|
|
@@ -51,6 +113,7 @@ export function projectSessionMessages(entries, defaults = {
|
|
|
51
113
|
const messages = [];
|
|
52
114
|
const tools = new Map();
|
|
53
115
|
const timings = collectTurnTimings(entries);
|
|
116
|
+
const userAttachments = collectUserAttachments(entries);
|
|
54
117
|
let { provider, model, thinkingLevel } = defaults;
|
|
55
118
|
for (const entry of entries) {
|
|
56
119
|
if (entry.type === "model_change" && entry.provider && entry.modelId) {
|
|
@@ -60,20 +123,44 @@ export function projectSessionMessages(entries, defaults = {
|
|
|
60
123
|
if (entry.type === "thinking_level_change" && isThinkingLevel(entry.thinkingLevel)) {
|
|
61
124
|
thinkingLevel = entry.thinkingLevel;
|
|
62
125
|
}
|
|
126
|
+
if (entry.type === "compaction") {
|
|
127
|
+
tools.clear();
|
|
128
|
+
messages.push({
|
|
129
|
+
id: entry.id,
|
|
130
|
+
role: "compaction",
|
|
131
|
+
createdAt: entry.timestamp,
|
|
132
|
+
compaction: {
|
|
133
|
+
summary: entry.summary,
|
|
134
|
+
tokensBefore: entry.tokensBefore,
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
63
139
|
if (entry.type !== "message")
|
|
64
140
|
continue;
|
|
65
141
|
const { message } = entry;
|
|
66
142
|
const createdAt = new Date(message.timestamp).toISOString();
|
|
67
143
|
if (message.role === "user") {
|
|
68
144
|
tools.clear();
|
|
69
|
-
|
|
145
|
+
const images = extractUserImages(message.content);
|
|
146
|
+
const attachmentMeta = userAttachments.get(entry.id);
|
|
147
|
+
const content = attachmentMeta ? attachmentMeta.displayContent : contentText(message.content);
|
|
148
|
+
messages.push({
|
|
149
|
+
id: entry.id,
|
|
150
|
+
role: "user",
|
|
151
|
+
createdAt,
|
|
152
|
+
content,
|
|
153
|
+
...(images.length > 0 ? { images } : {}),
|
|
154
|
+
...(attachmentMeta && attachmentMeta.attachments.length > 0
|
|
155
|
+
? { attachments: attachmentMeta.attachments }
|
|
156
|
+
: {}),
|
|
157
|
+
});
|
|
70
158
|
}
|
|
71
159
|
else if (message.role === "assistant") {
|
|
72
160
|
tools.clear();
|
|
73
161
|
const messageProvider = message.provider || provider;
|
|
74
162
|
const messageModel = message.model || model;
|
|
75
|
-
const messageThinking = message.thinkingLevel && isThinkingLevel(message.thinkingLevel)
|
|
76
|
-
? message.thinkingLevel : thinkingLevel;
|
|
163
|
+
const messageThinking = message.thinkingLevel && isThinkingLevel(message.thinkingLevel) ? message.thinkingLevel : thinkingLevel;
|
|
77
164
|
const descriptor = resolveDescriptor?.(messageProvider, messageModel);
|
|
78
165
|
const timing = timings.get(entry.id);
|
|
79
166
|
let thinkingIndex = 0;
|
|
@@ -124,3 +211,125 @@ export function projectSessionMessages(entries, defaults = {
|
|
|
124
211
|
}
|
|
125
212
|
return messages;
|
|
126
213
|
}
|
|
214
|
+
function estimateProjectedContextTokensCompat(manager, branch) {
|
|
215
|
+
const projection = manager.buildSessionProjection();
|
|
216
|
+
let lastUsageIndex = null;
|
|
217
|
+
let lastUsageTokens = 0;
|
|
218
|
+
for (let i = projection.messages.length - 1; i >= 0; i -= 1) {
|
|
219
|
+
const msg = projection.messages[i];
|
|
220
|
+
if (msg?.role === "assistant" && msg.stopReason !== "aborted" && msg.stopReason !== "error" && msg.usage) {
|
|
221
|
+
const tokens = calculateContextTokens(msg.usage);
|
|
222
|
+
if (tokens > 0) {
|
|
223
|
+
lastUsageIndex = i;
|
|
224
|
+
lastUsageTokens = tokens;
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (lastUsageIndex !== null) {
|
|
230
|
+
let projectedMessageIndex = 0;
|
|
231
|
+
let usageEntryId;
|
|
232
|
+
for (const entry of projection.entries) {
|
|
233
|
+
const nextMessageIndex = projectedMessageIndex + entry.messages.length;
|
|
234
|
+
if (lastUsageIndex < nextMessageIndex) {
|
|
235
|
+
usageEntryId = entry.sourceEntry.id;
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
projectedMessageIndex = nextMessageIndex;
|
|
239
|
+
}
|
|
240
|
+
const usageEntryIndex = usageEntryId ? branch.findIndex((e) => e.id === usageEntryId) : -1;
|
|
241
|
+
let latestInvalidatingIndex = -1;
|
|
242
|
+
for (let i = branch.length - 1; i >= 0; i -= 1) {
|
|
243
|
+
const e = branch[i];
|
|
244
|
+
if (e && (e.type === "context_edit" || e.type === "compaction")) {
|
|
245
|
+
latestInvalidatingIndex = i;
|
|
246
|
+
break;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
if (usageEntryIndex > latestInvalidatingIndex) {
|
|
250
|
+
let trailingTokens = 0;
|
|
251
|
+
for (let i = lastUsageIndex + 1; i < projection.messages.length; i += 1) {
|
|
252
|
+
trailingTokens += estimateTokens(projection.messages[i]);
|
|
253
|
+
}
|
|
254
|
+
return lastUsageTokens + trailingTokens;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
let latestSystemMessage;
|
|
258
|
+
let tokens = 0;
|
|
259
|
+
for (const message of projection.messages) {
|
|
260
|
+
if (message.role === "system") {
|
|
261
|
+
latestSystemMessage = message;
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
tokens += estimateTokens(message);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
if (latestSystemMessage) {
|
|
268
|
+
tokens += estimateTokens(latestSystemMessage);
|
|
269
|
+
}
|
|
270
|
+
return tokens;
|
|
271
|
+
}
|
|
272
|
+
export function computeSessionStats(manager, contextWindow) {
|
|
273
|
+
const branch = manager.getBranch();
|
|
274
|
+
const timings = collectTurnTimings(branch);
|
|
275
|
+
let turns = 0;
|
|
276
|
+
let steps = 0;
|
|
277
|
+
let inputTokens = 0;
|
|
278
|
+
let outputTokens = 0;
|
|
279
|
+
let cacheReadTokens = 0;
|
|
280
|
+
let cacheWriteTokens = 0;
|
|
281
|
+
let totalTokens = 0;
|
|
282
|
+
let lastAssistantOutput = 0;
|
|
283
|
+
let lastAssistantDurationMs;
|
|
284
|
+
for (const entry of branch) {
|
|
285
|
+
if (entry.type === "message") {
|
|
286
|
+
if (entry.message.role === "user") {
|
|
287
|
+
turns += 1;
|
|
288
|
+
}
|
|
289
|
+
else if (entry.message.role === "assistant") {
|
|
290
|
+
steps += 1;
|
|
291
|
+
const { usage, stopReason, durationMs } = entry.message;
|
|
292
|
+
if (usage) {
|
|
293
|
+
inputTokens += usage.input;
|
|
294
|
+
outputTokens += usage.output;
|
|
295
|
+
cacheReadTokens += usage.cacheRead;
|
|
296
|
+
cacheWriteTokens += usage.cacheWrite;
|
|
297
|
+
totalTokens += calculateContextTokens(usage);
|
|
298
|
+
if (stopReason !== "error" && stopReason !== "aborted" && usage.output > 0) {
|
|
299
|
+
lastAssistantOutput = usage.output;
|
|
300
|
+
const timing = timings.get(entry.id);
|
|
301
|
+
lastAssistantDurationMs = durationMs ?? timing?.generationDurationMs ?? timing?.durationMs;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
else if ((entry.type === "compaction" || entry.type === "usage") && entry.usage) {
|
|
307
|
+
inputTokens += entry.usage.input;
|
|
308
|
+
outputTokens += entry.usage.output;
|
|
309
|
+
cacheReadTokens += entry.usage.cacheRead;
|
|
310
|
+
cacheWriteTokens += entry.usage.cacheWrite;
|
|
311
|
+
totalTokens += calculateContextTokens(entry.usage);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
const promptTotal = inputTokens + cacheReadTokens + cacheWriteTokens;
|
|
315
|
+
const cacheHitRate = promptTotal > 0 ? Math.round((cacheReadTokens / promptTotal) * 100) : null;
|
|
316
|
+
const tokensPerSecond = lastAssistantOutput > 0 && lastAssistantDurationMs && lastAssistantDurationMs > 0
|
|
317
|
+
? Math.max(1, Math.round((lastAssistantOutput * 1000) / lastAssistantDurationMs))
|
|
318
|
+
: null;
|
|
319
|
+
const contextTokens = turns === 0 && steps === 0 ? 0 : estimateProjectedContextTokensCompat(manager, branch);
|
|
320
|
+
const contextPercent = contextWindow > 0 ? Math.min(100, Math.round((contextTokens / contextWindow) * 1000) / 10) : null;
|
|
321
|
+
return {
|
|
322
|
+
turns,
|
|
323
|
+
steps,
|
|
324
|
+
totalTokens,
|
|
325
|
+
inputTokens,
|
|
326
|
+
outputTokens,
|
|
327
|
+
cacheReadTokens,
|
|
328
|
+
cacheWriteTokens,
|
|
329
|
+
cacheHitRate,
|
|
330
|
+
tokensPerSecond,
|
|
331
|
+
contextTokens,
|
|
332
|
+
contextWindow,
|
|
333
|
+
contextPercent,
|
|
334
|
+
};
|
|
335
|
+
}
|