dsh-ws-recency 0.10.0 → 0.10.1

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 (4) hide show
  1. package/README.md +3 -1
  2. package/client.js +55 -17
  3. package/index.js +101 -83
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -73,6 +73,8 @@ DSH 原生的置顶只把会话顶到**它所属工作区分组的第一行**,
73
73
 
74
74
  口径说明:prompt 的 token 是「整段上下文每一步都计费」,没法精确归属到某一条消息,所以「主 agent / 工具」的拆分是按内容长度估算(中文≈1 token/字,英文≈4 字符/token),用来比较占比;**总量是真的**。数据从会话日志算,跨重启也能看。大会话第一次打开约 1–2 秒(29MB 日志实测 1.2 秒),10 秒内重复打开走缓存。
75
75
 
76
+ 宿主半(`index.js`)把统计与删除的路由挂在 webServer 上;如果面板报“宿主半未加载”,说明应用还没重启加载新版本。0.10.1 起面板失败时会带上 HTTP 状态和 `/ws-recency/health` 的自检结果,便于定位。
77
+
76
78
  ## 设置
77
79
 
78
80
  **设置 → 通用**:
@@ -108,7 +110,7 @@ dsh plugin --profile <profile> add github:chyxhtc/dsh-ws-recency
108
110
  或固定版本的预构建包:
109
111
 
110
112
  ```powershell
111
- dsh plugin --profile <profile> add https://github.com/chyxhtc/dsh-ws-recency/releases/download/v0.10.0/dsh-ws-recency-0.10.0.tgz
113
+ dsh plugin --profile <profile> add https://github.com/chyxhtc/dsh-ws-recency/releases/download/v0.10.1/dsh-ws-recency-0.10.1.tgz
112
114
  ```
113
115
 
114
116
  装完把 `dsh-ws-recency` 加进 `~/.hypers-work/profiles/<profile>/package.json` 的 `dsh.profile.bundles`,重启应用。
package/client.js CHANGED
@@ -179,6 +179,8 @@
179
179
  "stats.col.share": "\u5360\u6bd4",
180
180
  "stats.note": "\u5408\u8ba1\u6765\u81ea\u6bcf\u6b65\u8bb0\u5f55\u7684\u771f\u5b9e usage\uff08\u7f13\u5b58\u547d\u4e2d + \u8f93\u5165 + \u8f93\u51fa\uff09\uff1b\u672c\u8868\u6309\u6765\u6e90\u7684\u62c6\u5206\u662f\u6309\u5185\u5bb9\u957f\u5ea6\u4f30\u7b97\uff08\u4e2d\u6587\u7ea6 1 token/\u5b57\uff0c\u82f1\u6587\u7ea6 4 \u5b57\u7b26/token\uff09\uff0c\u7528\u4e8e\u6bd4\u8f83\u5360\u6bd4\uff0c\u4e0d\u662f\u8ba1\u8d39\u53e3\u5f84\u3002",
181
181
  "stats.agentDetail": "主 agent 构成:思考 {reasoning} · 回复 {text} · 用户消息 {user} · 系统提示 {system}",
182
+ "stats.diagnose.unmounted": "\n\n(宿主半已加载,但统计路由没挂上——重启应用再试)",
183
+ "stats.diagnose.noHost": "\n\n(宿主半未加载:需要重启应用加载新版本)",
182
184
  "trash.confirm": "\u5220\u9664\u8fd9\u4e2a\u4f1a\u8bdd\uff1f\n\n\u4f1a\u5148\u505c\u6b62\u5b83\u6b63\u5728\u8fdb\u884c\u7684\u5de5\u4f5c\uff0c\u518d\u628a\u65e5\u5fd7\u76ee\u5f55\u79fb\u5165\u56de\u6536\u7ad9\uff08\u4e0d\u662f\u7269\u7406\u5220\u9664\uff0c\u53ef\u4ee5\u624b\u52a8\u6062\u590d\uff09\u3002",
183
185
  "trash.failed": "\u5220\u9664\u5931\u8d25\uff1a",
184
186
  };
@@ -217,6 +219,8 @@
217
219
  "stats.col.share": "share",
218
220
  "stats.note": "Totals come from the recorded per-step usage (cache read + input + output). The per-source split is estimated from content length (about 1 token per CJK character, 4 characters per token otherwise) so the shares are comparable, not billable.",
219
221
  "stats.agentDetail": "Agent composition: reasoning {reasoning} \u00b7 reply {text} \u00b7 user messages {user} \u00b7 system prompt {system}",
222
+ "stats.diagnose.unmounted": "\n\n(the host half is loaded but the statistics route did not mount - restart the app and retry)",
223
+ "stats.diagnose.noHost": "\n\n(the host half is not loaded: restart the app to pick up the new version)",
220
224
  "trash.confirm": "Delete this session?\n\nIt stops whatever the session is running, then moves the log directory into the trash folder (a move, not an erase: you can restore it by hand).",
221
225
  "trash.failed": "Delete failed: ",
222
226
  };
@@ -885,16 +889,22 @@
885
889
  var question = t("trash.confirm");
886
890
  var confirmed = typeof window.confirm === "function" ? window.confirm(question) : false;
887
891
  if (!confirmed) return;
888
- fetch("/ws-recency/session-trash", {
892
+ fetch("/ws-recency/session-trash?sessionId=" + encodeURIComponent(props.sessionId), {
889
893
  method: "POST",
890
894
  headers: { "content-type": "application/json" },
891
895
  body: JSON.stringify({ sessionId: props.sessionId }),
892
896
  })
893
- .then(function (response) { return response.json(); })
897
+ .then(function (response) {
898
+ return response.text().then(function (text) {
899
+ var parsed = null;
900
+ try { parsed = JSON.parse(text); } catch (error) { parsed = null; }
901
+ return { status: response.status, text: text, json: parsed };
902
+ });
903
+ })
894
904
  .then(function (result) {
895
- if (result !== null && result.ok !== true && typeof window.alert === "function") {
896
- window.alert(t("trash.failed") + (result.error || "unknown"));
897
- }
905
+ if (result.json !== null && result.json.ok === true) return;
906
+ var reason = (result.json !== null && result.json.error) || (result.text || "").trim().slice(0, 120) || "(empty response)";
907
+ if (typeof window.alert === "function") window.alert(t("trash.failed") + "HTTP " + result.status + " \u00b7 " + reason);
898
908
  })
899
909
  .catch(function (error) {
900
910
  if (typeof window.alert === "function") window.alert(t("trash.failed") + (error && error.message ? error.message : error));
@@ -1054,22 +1064,50 @@
1054
1064
  statsModal = backdrop;
1055
1065
  document.addEventListener("keydown", onStatsKey);
1056
1066
 
1057
- fetch("/ws-recency/session-stats", {
1067
+ var controller = typeof AbortController === "function" ? new AbortController() : null;
1068
+ var timer = controller === null ? null : window.setTimeout(function () { controller.abort(); }, 180000);
1069
+ var request = fetch("/ws-recency/session-stats?sessionId=" + encodeURIComponent(sessionId), {
1058
1070
  method: "POST",
1059
1071
  headers: { "content-type": "application/json" },
1060
1072
  body: JSON.stringify({ sessionId: sessionId }),
1061
- })
1062
- .then(function (response) { return response.json(); })
1063
- .then(function (result) {
1064
- if (result === null || result.ok !== true) {
1065
- body.textContent = t("stats.failed") + (result && result.error ? result.error : "unknown");
1066
- return;
1067
- }
1068
- renderStats(body, result.stats);
1069
- })
1070
- .catch(function (error) {
1071
- body.textContent = t("stats.failed") + (error && error.message ? error.message : error);
1073
+ signal: controller === null ? undefined : controller.signal,
1074
+ }).then(function (response) {
1075
+ if (timer !== null) window.clearTimeout(timer);
1076
+ return response.text().then(function (text) {
1077
+ var parsed = null;
1078
+ try { parsed = JSON.parse(text); } catch (error) { parsed = null; }
1079
+ return { status: response.status, text: text, json: parsed };
1072
1080
  });
1081
+ }, function (error) {
1082
+ if (timer !== null) window.clearTimeout(timer);
1083
+ throw error;
1084
+ });
1085
+
1086
+ request.then(function (result) {
1087
+ if (result.status === 200 && result.json !== null && result.json.ok === true) {
1088
+ renderStats(body, result.json.stats);
1089
+ return;
1090
+ }
1091
+ var reason = (result.json !== null && result.json.error)
1092
+ || (result.text || "").trim().slice(0, 120)
1093
+ || "(empty response)";
1094
+ body.textContent = t("stats.failed") + "HTTP " + result.status + " \u00b7 " + reason;
1095
+ /* A 405 here is the SPA fallback answering an unknown POST: the host
1096
+ half never mounted. Ask the health route which part is missing. */
1097
+ return fetch("/ws-recency/health").then(function (response) {
1098
+ return response.text();
1099
+ }).then(function (text) {
1100
+ var health = null;
1101
+ try { health = JSON.parse(text); } catch (error) { health = null; }
1102
+ body.textContent += health !== null && health.ok === true
1103
+ ? t("stats.diagnose.unmounted")
1104
+ : t("stats.diagnose.noHost");
1105
+ }).catch(function () {
1106
+ body.textContent += t("stats.diagnose.noHost");
1107
+ });
1108
+ }).catch(function (error) {
1109
+ body.textContent = t("stats.failed") + (error && error.message ? error.message : error);
1110
+ });
1073
1111
  }
1074
1112
 
1075
1113
  /** Session "..." menu row: token statistics of that session. */
package/index.js CHANGED
@@ -19,24 +19,41 @@
19
19
  *
20
20
  * @module dsh-ws-recency
21
21
  */
22
- import { access, mkdir, readFile, readdir, rename, stat } from "node:fs/promises";
22
+ import { mkdir, readdir, rename } from "node:fs/promises";
23
23
  import { dirname, join } from "node:path";
24
- import { zstdDecompressSync } from "node:zlib";
25
24
 
26
25
  /** Cordis plugin name shown by the loader. */
27
26
  export const name = "ws-recency";
28
27
 
29
- /** Host services this module asks for; `webServer` is injected separately. */
30
- export const inject = ["sessionPersistence", "workspaceRegistry", "sessions"];
28
+ /** Version marker reported by the health route. */
29
+ export const HOST_VERSION = "0.10.1";
31
30
 
32
- /** Exact route the client half calls. */
31
+ /**
32
+ * Exact routes the client half calls. Registered on the bare web server, so they
33
+ * win over the SPA's fallback (which answers unknown POSTs with an empty 405).
34
+ */
33
35
  export const TRASH_SESSION_PATH = "/ws-recency/session-trash";
36
+ export const STATS_SESSION_PATH = "/ws-recency/session-stats";
37
+ export const HEALTH_PATH = "/ws-recency/health";
34
38
 
35
39
  const STOP_SETTLE_MS = 500;
36
40
  const MAX_BODY_BYTES = 16 * 1024;
37
41
  const RENAME_RETRY = 4;
38
42
  const RENAME_GAP_MS = 150;
39
43
 
44
+ /** `?sessionId=` first, then the JSON body: a middleware may have consumed the body. */
45
+ function sessionIdFrom(req, body) {
46
+ try {
47
+ const url = new URL(req.url ?? "/", "http://localhost");
48
+ const fromQuery = url.searchParams.get("sessionId");
49
+ if (typeof fromQuery === "string" && fromQuery.trim() !== "") return fromQuery.trim();
50
+ } catch {
51
+ /* fall through to the body */
52
+ }
53
+ return body !== null && body !== undefined && typeof body.sessionId === "string" ? body.sessionId.trim() : "";
54
+ }
55
+
56
+ /** Only the app itself may call these routes: a foreign origin is refused. */
40
57
  function loopbackOrigin(req) {
41
58
  const origin = req.headers.origin;
42
59
  if (origin === undefined) return true;
@@ -197,7 +214,7 @@ export async function handleTrashRequest(ctx, req, res) {
197
214
  }
198
215
  try {
199
216
  const body = await readJsonBody(req);
200
- const sessionId = body !== null && typeof body.sessionId === "string" ? body.sessionId.trim() : "";
217
+ const sessionId = sessionIdFrom(req, body);
201
218
  if (sessionId === "") {
202
219
  sendJson(res, 400, { ok: false, error: "sessionId required" });
203
220
  return;
@@ -224,9 +241,6 @@ export async function handleTrashRequest(ctx, req, res) {
224
241
  * decoding each frame is what makes the whole transcript readable.
225
242
  * ------------------------------------------------------------------------- */
226
243
 
227
- export const STATS_SESSION_PATH = "/ws-recency/session-stats";
228
-
229
- const ZSTD_MAGIC = Buffer.from([0x28, 0xb5, 0x2f, 0xfd]);
230
244
  const STATS_CACHE_MS = 10 * 1000;
231
245
  const statsCache = new Map();
232
246
 
@@ -362,86 +376,61 @@ export function aggregateSession(events) {
362
376
  return summary;
363
377
  }
364
378
 
365
- /** Decode every Zstandard frame of a session log and parse its JSONL records. */
366
- export async function readSessionEvents(path) {
367
- const bytes = await readFile(path);
368
- const offsets = [];
369
- let at = bytes.indexOf(ZSTD_MAGIC, 0);
370
- while (at !== -1) {
371
- offsets.push(at);
372
- at = bytes.indexOf(ZSTD_MAGIC, at + 4);
373
- }
374
- const parts = [];
375
- for (let index = 0; index < offsets.length; index += 1) {
376
- const end = index + 1 < offsets.length ? offsets[index + 1] : bytes.length;
377
- try {
378
- parts.push(zstdDecompressSync(bytes.subarray(offsets[index], end)));
379
- } catch {
380
- /* a torn tail: the rest of the log is still usable */
381
- }
382
- }
383
- const events = [];
384
- for (const line of Buffer.concat(parts).toString("utf8").split("\n")) {
385
- if (line === "") continue;
386
- try {
387
- events.push(JSON.parse(line));
388
- } catch {
389
- /* a torn final record */
390
- }
391
- }
392
- return events;
393
- }
394
-
395
- /** Absolute path of a session log, or undefined when it is gone. */
396
- async function sessionLogPath(ctx, sessionId) {
379
+ /**
380
+ * Read a session's events through the host's own persistence seam.
381
+ *
382
+ * Decoding the log by hand looked simpler, but it is a chain of independent
383
+ * Zstandard frames, and the host's Node build is not guaranteed to ship a zstd
384
+ * decoder. The persistence service owns both concerns (it carries its own frame
385
+ * decoder), so ask it instead of touching the file.
386
+ */
387
+ async function readSessionEvents(ctx, sessionId) {
388
+ const handle = await ctx.sessionPersistence.open(sessionId, "read");
389
+ const all = [];
397
390
  try {
398
- const stat = await ctx.sessionPersistence.stat(sessionId);
399
- const meta = stat !== undefined && stat !== null ? stat.header ?? stat : undefined;
400
- if (meta !== undefined && typeof ctx.sessionPersistence.locate === "function") {
401
- const located = ctx.sessionPersistence.locate(meta);
402
- if (located !== undefined && located !== null && typeof located.path === "string") return located.path;
391
+ /* Page through in slices: one huge read holds the whole transcript plus its
392
+ parse alive at the same time, which is a lot for a months-old session. */
393
+ let offset = 0;
394
+ const page = 5000;
395
+ for (;;) {
396
+ const slice = await handle.read(offset, page);
397
+ const events = slice !== null && slice !== undefined && Array.isArray(slice.events) ? slice.events : [];
398
+ for (const event of events) all.push(event);
399
+ if (events.length < page) break;
400
+ offset += events.length;
401
+ if (offset >= 400000) break;
403
402
  }
404
- } catch {
405
- /* fall back to the scan */
406
- }
407
- const home = await dshHome();
408
- const root = join(home, "sessions");
409
- let projects;
410
- try {
411
- projects = await readdir(root, { withFileTypes: true });
412
- } catch {
413
- return undefined;
414
- }
415
- for (const project of projects) {
416
- if (!project.isDirectory()) continue;
417
- const candidate = join(root, project.name, sessionId, "session.v4.jsonl.zstd");
403
+ } finally {
418
404
  try {
419
- await access(candidate);
420
- return candidate;
405
+ if (typeof handle.close === "function") await handle.close();
421
406
  } catch {
422
- /* keep scanning */
407
+ /* closing is best effort */
423
408
  }
424
409
  }
425
- return undefined;
410
+ return all;
426
411
  }
427
412
 
428
413
  /** Statistics for one session, cached briefly so re-opening the panel is cheap. */
429
414
  export async function sessionStatistics(ctx, sessionId) {
430
- const path = await sessionLogPath(ctx, sessionId);
431
- if (path === undefined) return { ok: false, error: "session log not found" };
432
- let info;
415
+ let stat;
433
416
  try {
434
- info = await stat(path);
417
+ stat = await ctx.sessionPersistence.stat(sessionId);
435
418
  } catch {
436
- return { ok: false, error: "session log not found" };
419
+ return { ok: false, error: "session not found" };
437
420
  }
438
- const cacheKey = `${info.size}:${info.mtimeMs}`;
421
+ if (stat === undefined || stat === null) return { ok: false, error: "session not found" };
422
+ const cacheKey = `${stat.sizeBytes ?? 0}:${stat.revision ?? ""}`;
439
423
  const cached = statsCache.get(sessionId);
440
424
  if (cached !== undefined && cached.cacheKey === cacheKey && Date.now() - cached.at < STATS_CACHE_MS) return { ok: true, stats: cached.stats };
441
425
  try {
442
- const events = await readSessionEvents(path);
426
+ const events = await readSessionEvents(ctx, sessionId);
443
427
  const stats = aggregateSession(events);
444
428
  if (stats.session.id === "") stats.session.id = sessionId;
429
+ const header = stat.header;
430
+ if (header !== undefined && header !== null) {
431
+ if (stats.session.createdAt === 0 && typeof header.createdAt === "number") stats.session.createdAt = header.createdAt;
432
+ if (stats.session.cwd === "" && typeof header.cwd === "string") stats.session.cwd = header.cwd;
433
+ }
445
434
  statsCache.set(sessionId, { cacheKey, stats, at: Date.now() });
446
435
  return { ok: true, stats };
447
436
  } catch (error) {
@@ -461,7 +450,7 @@ export async function handleStatsRequest(ctx, req, res) {
461
450
  }
462
451
  try {
463
452
  const body = await readJsonBody(req);
464
- const sessionId = body !== null && typeof body.sessionId === "string" ? body.sessionId.trim() : "";
453
+ const sessionId = sessionIdFrom(req, body);
465
454
  if (sessionId === "") {
466
455
  sendJson(res, 400, { ok: false, error: "sessionId required" });
467
456
  return;
@@ -473,24 +462,53 @@ export async function handleStatsRequest(ctx, req, res) {
473
462
  }
474
463
  }
475
464
 
476
- /** Cordis entry point: mount the routes when the host has a web server. */
465
+ /**
466
+ * Cordis entry point.
467
+ *
468
+ * `apply` itself must not wait for services: the Client half is served from this
469
+ * same package, and a plugin whose row never applies still shows its sidebar
470
+ * menu while none of its routes exist. So every capability is injected in its
471
+ * own nested scope, and the health route reports what actually mounted - the
472
+ * panel calls it to say "host plugin not loaded" instead of an empty 405.
473
+ */
477
474
  export function apply(ctx) {
478
475
  try {
479
- if (typeof ctx.inject === "function") {
480
- ctx.inject(["webServer"], (webCtx) => {
481
- webCtx.effect(() => webCtx.webServer.register({
482
- kind: "exact",
483
- path: TRASH_SESSION_PATH,
484
- handler: (req, res) => handleTrashRequest(ctx, req, res),
485
- }), "ws-recency: trash route");
486
- webCtx.effect(() => webCtx.webServer.register({
476
+ if (typeof ctx.inject !== "function") return;
477
+ ctx.inject(["webServer"], (webCtx) => {
478
+ const server = webCtx.webServer;
479
+ if (server === undefined || server === null) return;
480
+ webCtx.effect(() => server.register({
481
+ kind: "exact",
482
+ path: HEALTH_PATH,
483
+ handler: (req, res) => sendJson(res, 200, {
484
+ ok: true,
485
+ version: HOST_VERSION,
486
+ routes: { stats: statsMounted, trash: trashMounted },
487
+ }),
488
+ }), "ws-recency: health route");
489
+
490
+ webCtx.inject(["sessionPersistence"], (statsCtx) => {
491
+ statsMounted = true;
492
+ webCtx.effect(() => server.register({
487
493
  kind: "exact",
488
494
  path: STATS_SESSION_PATH,
489
- handler: (req, res) => handleStatsRequest(ctx, req, res),
495
+ handler: (req, res) => handleStatsRequest(statsCtx, req, res),
490
496
  }), "ws-recency: stats route");
491
497
  });
492
- }
498
+
499
+ webCtx.inject(["sessionPersistence", "workspaceRegistry", "sessions"], (trashCtx) => {
500
+ trashMounted = true;
501
+ webCtx.effect(() => server.register({
502
+ kind: "exact",
503
+ path: TRASH_SESSION_PATH,
504
+ handler: (req, res) => handleTrashRequest(trashCtx, req, res),
505
+ }), "ws-recency: trash route");
506
+ });
507
+ });
493
508
  } catch {
494
509
  /* a headless profile simply gets no routes */
495
510
  }
496
511
  }
512
+
513
+ let statsMounted = false;
514
+ let trashMounted = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-ws-recency",
3
- "version": "0.10.0",
3
+ "version": "0.10.1",
4
4
  "description": "Sidebar session management for DeepSeek Harness: workspace groups sorted by latest activity, stale history folded, a global pinned section, follow-up marks, session delete into a recoverable trash, and per-session token statistics split by tool.",
5
5
  "license": "MIT",
6
6
  "author": "chyxhtc",