dsh-snowbreak-heimdall 3.9.8 → 3.9.9

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/SHA256SUMS.txt CHANGED
@@ -29,8 +29,8 @@ b5f2ff21b67a6a0c178d251af49331319eb42e9e140e279e391edda30112bf47 *assets\preview
29
29
  3ebaa8c085365385745ec2ceac437970a146581487c89ae4e4b4e4e15a5c5ec5 *assets\source\brand-client.js
30
30
  12ea3ea272ae13b66f2875c42d466053516588057bb90775499e0fdb575fe624 *assets\source\client-template.js
31
31
  54fc8e9c7394fc84dad497500021784d639d835bfa7e10abe39f04ab7f21f584 *assets\source\confirm-mark.png
32
- 3ae8046a945677788b289d8db464ae947cebaa9cc3f23ecdb8c5ff38bd8f02f8 *assets\source\extras.css
33
- 97f61a89a8f1879cfd563bc1dee158f8665ebbc0742eb2fae0032671369da5a2 *assets\source\extras.js
32
+ 59ffbf2123896606757c7fc1520c26345603a28e29748dbbcf656b13d501c094 *assets\source\extras.css
33
+ 8c0e66cc1e814bd17eeaeecb30a0fe518c9cbedc2c2e1f75175e35b206f0586c *assets\source\extras.js
34
34
  f97fb8cb96c579159c4b5d6d1b0f1a767ead9895b71cfaa5aaa7c4248a3938ad *assets\source\hm-mark-blue.png
35
35
  aef34c1fed4a009a78955f5316b999bbd157fd082d7441d34852d32f0a481c28 *assets\source\hm-mark-ink.png
36
36
  1591f7b426d79f71440d4d9c93518de14c88d5b69610beea03a2ecac5c3ac958 *assets\source\hm-mark-light.png
@@ -52,12 +52,11 @@ aef34c1fed4a009a78955f5316b999bbd157fd082d7441d34852d32f0a481c28 *assets\source\
52
52
  2a0b4b9c68d5f4b87bb8fbb768a1933c40394fe4e6f23c144fae88052ebe4a02 *cordis.patch.yml
53
53
  065af22a9be00dc76ce08bc647814d33da2779e937db6c5fa4ea0dcb8c65caa9 *icon.svg
54
54
  bcb1d6e0762ab2b3bca86cb7cc17c194fdf76c2c3e44c4bcba81869bc5c4b42f *lib\client.js
55
- 349e498371c9fb55dfd30b6d27b62fb06568e082a6ecf3939f54ed06db377681 *lib\index.js
56
- b7a3b5add20b59e19942ecdb1054238ab26ca6277d3f2a9456e34229f2eba0f2 *lib\turns.js
55
+ 70fc214943a863b9f3694b06440dbb2b9d156c0791eb1124dad7deb76095ae63 *lib\index.js
57
56
  0794a24c333ef529e56c6eb7b1dee62f4f5b8da1c56fe001885824ce2a3f8b2f *LICENSE
58
57
  de949631618eb48bf5cd6c06698a28214c8e9cb4bca23d298cd89462f4fb260e *locale\en.json
59
58
  e931cfb8c9b64aa4e01363e603537afee392e8309fe07daaff3e07e028ab8010 *locale\zh.json
60
- 896c3cfbf5aa7efcc4d82277af964b4134e8510aea900df161f8ab4d380f10fb *package.json
59
+ 63ad0386682d7818b634f658c51b8f3bfa26806ed3b835e4f96baf59d04802c4 *package.json
61
60
  b39651c3299516bbae0a6844290425c2310085f2a7691edfbc04c4b832eb9e70 *patch\__pycache__\asar.cpython-314.pyc
62
61
  46e365afb82f22ca4071b2571ad7981be7f308f9196d8b4ffcefc0f15dba869c *patch\__pycache__\patcher.cpython-314.pyc
63
62
  8fa6aff9fa2ab7eb11cc813c7ed9cda2b9ae7757c039aad8d71d5b56b5b683d9 *patch\asar.py
@@ -152,19 +152,6 @@ html[data-hm-night] #hm-ui-chrome .hm-ui-scan{opacity:.55;animation-duration:11s
152
152
  html[data-hm-night] #hm-extras .hm-hud__link b{color:#D97745}
153
153
  html[data-hm-night] #hm-extras .hm-hud__link i{background:#D97745}
154
154
 
155
- /* ----------------------- 10 轨迹车道图:圆环线框(覆盖原图,不改数据) */
156
- #hm-torus{position:absolute;inset:0;z-index:5;display:flex;flex-direction:column;
157
- background:#F7F9FB;border-bottom:1px solid rgba(47,106,142,.14);
158
- font-family:"Bahnschrift","Noto Sans SC","Segoe UI",sans-serif;user-select:none}
159
- #hm-torus[hidden]{display:none}
160
- #hm-torus .hm-torus__head{display:flex;align-items:baseline;gap:10px;padding:3px 6px 2px;
161
- font-size:8px;letter-spacing:.14em;color:#7C8B99}
162
- #hm-torus .hm-torus__head em{margin-left:auto;font-style:normal;
163
- font-family:"Consolas",monospace;font-size:8px;letter-spacing:.06em;color:#8A98A6}
164
- #hm-torus .hm-torus__scene{position:relative;flex:1;min-height:0;overflow:hidden}
165
- #hm-torus canvas{display:block;width:100%;height:100%}
166
- body[data-ds-dark-theme] #hm-torus{background:#0A0E13;border-bottom-color:rgba(127,178,208,.16)}
167
-
168
155
  /* ------------------------------------------------------- 10 启动层进度条 */
169
156
  .hm-boot-progress{
170
157
  position:absolute;left:48px;right:48px;bottom:44px;z-index:3;
@@ -715,263 +715,6 @@
715
715
  setTimeout(function () { clearInterval(timer); }, 60000);
716
716
  }
717
717
 
718
- /* ---------------------------------------- 10 轨迹车道图:圆环(torus 线框) */
719
- // A real torus drawn on canvas: latitude/longitude grid, perspective
720
- // projection, depth-faded strokes and a continuous rotation. The current
721
- // turn is highlighted as a band of tubes at the near side.
722
- var TORUS_U = 56;
723
- var TORUS_V = 18;
724
- var TORUS_R = 1;
725
- var TORUS_r = 0.42;
726
-
727
- var torusEl = null;
728
- var torusCanvas = null;
729
- var torusCtx = null;
730
- var torusHost = null;
731
- var torusRaf = 0;
732
- var torusAngle = 0.4;
733
- var torusAngularSpeed = 0.45;
734
- var torusBusyUntil = 0;
735
- var torusTurns = null;
736
- var torusSelected = -1;
737
- var torusSpunAt = 0;
738
-
739
- function torusLeaf(text) {
740
- var nodes;
741
- try { nodes = doc.querySelectorAll("span, div"); } catch (_) { return null; }
742
- for (var i = 0; i < nodes.length; i += 1) {
743
- var el = nodes[i];
744
- if (!el.childElementCount && (el.textContent || "").trim() === text) return el;
745
- }
746
- return null;
747
- }
748
-
749
- function torusHostElement() {
750
- // DSH marks every timeline span itself; anchor on those instead of guessing
751
- // from label text. The band is the smallest ancestor that still holds most
752
- // of the spans and stays short.
753
- var spans;
754
- try { spans = doc.querySelectorAll("[data-timeline-span]"); } catch (_) { spans = []; }
755
- if (spans.length) {
756
- var total = spans.length;
757
- var node = spans[0].parentElement;
758
- for (var depth = 0; depth < 7 && node; depth += 1) {
759
- var rect = node.getBoundingClientRect();
760
- var held = node.querySelectorAll("[data-timeline-span]").length;
761
- if (rect.width > 240 && rect.height > 36 && rect.height < 340 && held >= total * 0.5) return node;
762
- node = node.parentElement;
763
- }
764
- }
765
- var first = torusLeaf("输入");
766
- if (!first) return null;
767
- var walker = first.parentElement;
768
- for (var step = 0; step < 6 && walker; step += 1) {
769
- var box = walker.getBoundingClientRect();
770
- var text = walker.textContent || "";
771
- if (box.width > 240 && box.height > 30 && box.height < 260 && text.indexOf("第 ") < 0) return walker;
772
- walker = walker.parentElement;
773
- }
774
- return null;
775
- }
776
-
777
- function torusTheme() {
778
- var dark = false;
779
- try { dark = doc.body.getAttribute("data-ds-dark-theme") !== null; } catch (_) {}
780
- return dark
781
- ? { ink: "200,220,236", accent: "217,119,69", grid: 0.34 }
782
- : { ink: "22,32,44", accent: "169,72,31", grid: 0.42 };
783
- }
784
-
785
- function torusBuild() {
786
- var wrap = doc.createElement("div");
787
- wrap.id = "hm-torus";
788
- wrap.innerHTML =
789
- '<div class="hm-torus__head"><span>回合环</span><em data-hm="torus-meta"></em></div>' +
790
- '<div class="hm-torus__scene"><canvas></canvas></div>';
791
- return wrap;
792
- }
793
-
794
- function torusResize() {
795
- if (!torusEl || !torusCanvas) return;
796
- var scene = torusEl.querySelector(".hm-torus__scene");
797
- if (!scene) return;
798
- var dpr = Math.min(2, window.devicePixelRatio || 1);
799
- var w = Math.max(40, scene.clientWidth);
800
- var h = Math.max(40, scene.clientHeight);
801
- if (torusCanvas.width !== Math.round(w * dpr) || torusCanvas.height !== Math.round(h * dpr)) {
802
- torusCanvas.width = Math.round(w * dpr);
803
- torusCanvas.height = Math.round(h * dpr);
804
- torusCanvas.style.width = w + "px";
805
- torusCanvas.style.height = h + "px";
806
- }
807
- torusCtx = torusCanvas.getContext("2d");
808
- torusCtx.setTransform(dpr, 0, 0, dpr, 0, 0);
809
- }
810
-
811
- function torusDraw(ts) {
812
- if (!torusCanvas || !torusCtx) return;
813
- var w = torusCanvas.width / (Math.min(2, window.devicePixelRatio || 1));
814
- var h = torusCanvas.height / (Math.min(2, window.devicePixelRatio || 1));
815
- var ctx = torusCtx;
816
- var theme = torusTheme();
817
- ctx.clearRect(0, 0, w, h);
818
-
819
- var cosA = Math.cos(torusAngle), sinA = Math.sin(torusAngle);
820
- var fov = 3.4;
821
- var size = Math.min(w * 0.34, h * 2.4) * 0.5;
822
- var cx = w / 2;
823
- var cy = h / 2;
824
- var tiltX = -0.92;
825
- var cosX = Math.cos(tiltX), sinX = Math.sin(tiltX);
826
-
827
- function project(theta, phi) {
828
- var rr = TORUS_R + TORUS_r * Math.cos(phi);
829
- var x = rr * Math.cos(theta);
830
- var y = rr * Math.sin(theta);
831
- var z = TORUS_r * Math.sin(phi);
832
- var x1 = x * cosA - y * sinA;
833
- var y1 = x * sinA + y * cosA;
834
- var y2 = y1 * cosX - z * sinX;
835
- var z2 = y1 * sinX + z * cosX;
836
- var depth = fov + z2;
837
- if (depth < 0.25) depth = 0.25;
838
- var s = size / depth;
839
- return { x: cx + x1 * s, y: cy + y2 * s, depth: depth };
840
- }
841
-
842
- var points = [];
843
- for (var u = 0; u <= TORUS_U; u += 1) {
844
- var theta = (u / TORUS_U) * Math.PI * 2;
845
- var row = [];
846
- for (var v = 0; v <= TORUS_V; v += 1) {
847
- row.push(project(theta, (v / TORUS_V) * Math.PI * 2));
848
- }
849
- points.push(row);
850
- }
851
-
852
- // depth range for alpha normalisation
853
- var minD = Infinity, maxD = 0;
854
- for (var i = 0; i < points.length; i += 1) {
855
- for (var j = 0; j < points[i].length; j += 1) {
856
- var d = points[i][j].depth;
857
- if (d < minD) minD = d;
858
- if (d > maxD) maxD = d;
859
- }
860
- }
861
- var span = Math.max(0.001, maxD - minD);
862
-
863
- function strokeLine(a, b) {
864
- var depth = (a.depth + b.depth) / 2;
865
- var near = 1 - (depth - minD) / span;
866
- var alpha = theme.grid * (0.35 + near * 0.9);
867
- ctx.strokeStyle = "rgba(" + theme.ink + "," + alpha.toFixed(3) + ")";
868
- ctx.lineWidth = 0.5 + near * 0.9;
869
- ctx.beginPath();
870
- ctx.moveTo(a.x, a.y);
871
- ctx.lineTo(b.x, b.y);
872
- ctx.stroke();
873
- }
874
-
875
- // longitude lines (along theta)
876
- for (var vIdx = 0; vIdx <= TORUS_V; vIdx += 1) {
877
- for (var uIdx = 0; uIdx < TORUS_U; uIdx += 1) {
878
- strokeLine(points[uIdx][vIdx], points[uIdx + 1][vIdx]);
879
- }
880
- }
881
- // latitude lines (along phi)
882
- for (var uL = 0; uL <= TORUS_U; uL += 2) {
883
- for (var vL = 0; vL < TORUS_V; vL += 1) {
884
- strokeLine(points[uL][vL], points[uL][vL + 1]);
885
- }
886
- }
887
-
888
- // highlight the selected turn as a saturated band of tubes
889
- if (torusTurns && torusTurns.length && torusSelected >= 0) {
890
- var share = (torusSelected / torusTurns.length) * Math.PI * 2;
891
- var center = Math.round((share / (Math.PI * 2)) * TORUS_U);
892
- ctx.strokeStyle = "rgba(" + theme.accent + ",0.9)";
893
- ctx.lineWidth = 1.8;
894
- for (var band = 0; band < 2; band += 1) {
895
- var uBand = ((center + band) % TORUS_U + TORUS_U) % TORUS_U;
896
- for (var hv = 0; hv < TORUS_V; hv += 1) {
897
- var a = points[uBand][hv];
898
- var b = points[uBand][hv + 1];
899
- ctx.beginPath();
900
- ctx.moveTo(a.x, a.y);
901
- ctx.lineTo(b.x, b.y);
902
- ctx.stroke();
903
- }
904
- }
905
- }
906
-
907
- var meta = torusEl && torusEl.querySelector('[data-hm="torus-meta"]');
908
- if (meta && torusTurns) {
909
- var current = torusTurns[torusSelected] || null;
910
- var text = torusTurns.length + " 回合 · T-" + String(current ? current.n : 0);
911
- if (meta.textContent !== text) meta.textContent = text;
912
- }
913
- }
914
-
915
- function torusLoop(ts) {
916
- var reduce = false;
917
- try { reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches; } catch (_) {}
918
- if (!reduce) {
919
- var dt = torusSpunAt ? Math.min(64, ts - torusSpunAt) : 16;
920
- torusSpunAt = ts;
921
- torusAngle += (torusAngularSpeed * dt) / 1000;
922
- }
923
- torusDraw(ts);
924
- torusRaf = window.requestAnimationFrame(torusLoop);
925
- }
926
-
927
- function ensureTorus() {
928
- if (Date.now() < torusBusyUntil) return;
929
- var host = torusHostElement();
930
- if (!host) {
931
- if (torusEl) torusEl.hidden = true;
932
- return;
933
- }
934
- torusBusyUntil = Date.now() + 60;
935
- if (!torusEl) {
936
- torusEl = torusBuild();
937
- torusCanvas = torusEl.querySelector("canvas");
938
- }
939
- if (torusEl.parentElement !== host) {
940
- try {
941
- host.style.position = "relative";
942
- host.style.minHeight = "150px";
943
- host.style.marginBottom = "8px";
944
- host.appendChild(torusEl);
945
- } catch (_) { return; }
946
- }
947
- torusHost = host;
948
- torusEl.hidden = false;
949
- torusResize();
950
- if (!torusRaf) torusLoop(0);
951
- }
952
-
953
- function fetchTorusTurns() {
954
- if (typeof window.fetch !== "function") return;
955
- window.fetch("/api/heimdall/turns", { cache: "no-store" })
956
- .then(function (response) { return response.ok ? response.json() : null; })
957
- .then(function (data) {
958
- if (!data || !data.turns || !data.turns.length) return;
959
- torusTurns = data.turns;
960
- if (torusSelected < 0 || torusSelected >= torusTurns.length) torusSelected = torusTurns.length - 1;
961
- ensureTorus();
962
- }, function () {});
963
- }
964
-
965
- doc.addEventListener("wheel", function (event) {
966
- var target = event.target;
967
- if (!target || !target.closest || !target.closest("#hm-torus")) return;
968
- event.preventDefault();
969
- if (!torusTurns || !torusTurns.length) return;
970
- torusSelected = Math.min(torusTurns.length - 1, Math.max(0, torusSelected + (event.deltaY > 0 ? 1 : -1)));
971
- torusAngularSpeed = 3.2;
972
- setTimeout(function () { torusAngularSpeed = 0.45; }, 320);
973
- }, { passive: false, capture: true });
974
-
975
718
  /* ------------------------------------------------------------------ tick */
976
719
  var pending = 0;
977
720
  function tick() {
@@ -987,7 +730,6 @@
987
730
  authCodes();
988
731
  rewriteErrors();
989
732
  syncModelNames();
990
- ensureTorus();
991
733
  } catch (_) {}
992
734
  }, 400);
993
735
  }
@@ -1002,7 +744,6 @@
1002
744
  syncModelNames();
1003
745
  trackUpdate();
1004
746
  fetchStatus();
1005
- fetchTorusTurns();
1006
747
  } catch (_) {}
1007
748
  // The meter re-renders on every turn: poll it rather than trust a one-shot read.
1008
749
  setInterval(function () {
@@ -1011,9 +752,6 @@
1011
752
  setInterval(function () {
1012
753
  try { fetchStatus(); } catch (_) {}
1013
754
  }, 60000);
1014
- setInterval(function () {
1015
- try { fetchTorusTurns(); } catch (_) {}
1016
- }, 30000);
1017
755
  // Execution state changes without a DOM mutation on our own subtree.
1018
756
  setInterval(function () {
1019
757
  try { frameHud(); } catch (_) {}
package/lib/index.js CHANGED
@@ -12,14 +12,13 @@ import os from 'node:os';
12
12
  import path from 'node:path';
13
13
  import { createHash } from 'node:crypto';
14
14
  import { createRequire } from 'node:module';
15
- import { readTurns } from './turns.js';
16
15
  import { spawn } from 'node:child_process';
17
16
  import { fileURLToPath } from 'node:url';
18
17
 
19
18
  const requireFromHere = createRequire(import.meta.url);
20
19
 
21
20
  const ROOT = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
22
- const VERSION = '3.9.8';
21
+ const VERSION = '3.9.9';
23
22
  let restartPrompted = false;
24
23
  const INSTALL = path.join(process.env.LOCALAPPDATA || '', 'Programs', 'DeepSeek Harness');
25
24
  const DEFAULT_RESOURCES = process.env.HEIMDALL_RESOURCES
@@ -155,68 +154,6 @@ function warmStatus() {
155
154
  statusSnapshot().catch(() => { /* warmed best effort */ });
156
155
  }
157
156
 
158
- const SESSIONS_ROOT = path.join(process.env.DSH_HOME || path.join(os.homedir(), '.dsh'), 'sessions');
159
- const turnsCache = new Map();
160
-
161
- function sessionLogPath(sessionId) {
162
- let newest = null;
163
- let buckets = [];
164
- try {
165
- buckets = fs.readdirSync(SESSIONS_ROOT, { withFileTypes: true }).filter((e) => e.isDirectory());
166
- } catch (_) {
167
- return null;
168
- }
169
- for (const bucket of buckets) {
170
- const bucketPath = path.join(SESSIONS_ROOT, bucket.name);
171
- let entries = [];
172
- try {
173
- entries = fs.readdirSync(bucketPath, { withFileTypes: true }).filter((e) => e.isDirectory());
174
- } catch (_) {
175
- continue;
176
- }
177
- for (const entry of entries) {
178
- if (!entry.name.startsWith('session-')) continue;
179
- const candidate = path.join(bucketPath, entry.name, 'session.v4.jsonl.zstd');
180
- try {
181
- if (!fs.existsSync(candidate)) continue;
182
- if (sessionId && !entry.name.slice('session-'.length).startsWith(sessionId.replace(/^session-/, ''))) continue;
183
- const stat = fs.statSync(candidate);
184
- if (!newest || stat.mtimeMs > newest.mtime) newest = { file: candidate, mtime: stat.mtimeMs, size: stat.size };
185
- } catch (_) { /* skip unreadable entry */ }
186
- }
187
- }
188
- return newest;
189
- }
190
-
191
- function handleTurns(req, res) {
192
- if (req.method !== 'GET' && req.method !== 'HEAD') {
193
- sendJson(res, 405, { error: 'method not allowed' });
194
- return;
195
- }
196
- let sessionId = '';
197
- try {
198
- const url = new URL(String(req.url || '/'), 'http://localhost');
199
- sessionId = url.searchParams.get('session') || '';
200
- } catch (_) { sessionId = ''; }
201
- const found = sessionLogPath(sessionId);
202
- if (!found) {
203
- sendJson(res, 404, { error: 'session log not found' });
204
- return;
205
- }
206
- try {
207
- const key = found.file + ':' + found.mtime + ':' + found.size;
208
- let data = turnsCache.get(key);
209
- if (!data) {
210
- data = readTurns(found.file);
211
- if (turnsCache.size > 8) turnsCache.clear();
212
- turnsCache.set(key, data);
213
- }
214
- sendJson(res, 200, { file: found.file, title: data.title, turns: data.turns, totals: data.totals });
215
- } catch (error) {
216
- sendJson(res, 500, { error: String(error && error.message ? error.message : error) });
217
- }
218
- }
219
-
220
157
  function sendJson(res, code, payload) {
221
158
  try {
222
159
  res.writeHead(code, {
@@ -278,7 +215,6 @@ function registerRoutes(ctx) {
278
215
  const disposers = [
279
216
  server.register({ kind: 'exact', path: '/api/heimdall/status', handler: handleStatus }),
280
217
  server.register({ kind: 'exact', path: '/api/heimdall/apply', handler: handleApply }),
281
- server.register({ kind: 'exact', path: '/api/heimdall/turns', handler: handleTurns }),
282
218
  server.register({ kind: 'exact', path: '/api/heimdall/restore', handler: handleRestore }),
283
219
  ];
284
220
  routesRegistered = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-snowbreak-heimdall",
3
- "version": "3.9.8",
3
+ "version": "3.9.9",
4
4
  "description": "《尘白禁区》海姆达尔终端 / 世界树公司源码级皮肤:加载画面、双主题、菜单/侧栏/图标,插件仅在加载后播放启动动画。Windows / DSH 0.2.2-rc.x。",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -21,7 +21,6 @@
21
21
  },
22
22
  "files": [
23
23
  "lib/index.js",
24
- "lib/turns.js",
25
24
  "lib/client.js",
26
25
  "cordis.patch.yml",
27
26
  "locale/*.json",
package/lib/turns.js DELETED
@@ -1,164 +0,0 @@
1
- /**
2
- * Turn index for the "行动记录" conversation view.
3
- *
4
- * DSH appends one zstd frame per write to `session.v4.jsonl.zstd`, so the file
5
- * is a concatenation of frames rather than a single stream. Splitting on the
6
- * zstd magic and decoding each frame individually recovers the whole JSONL log.
7
- */
8
- import fs from 'node:fs';
9
- import zlib from 'node:zlib';
10
-
11
- const MAGIC = Buffer.from([0x28, 0xb5, 0x2f, 0xfd]);
12
-
13
- /**
14
- * Decode every zstd frame in a session log.
15
- * @param {Buffer} buffer raw file contents
16
- * @returns {string} concatenated UTF-8 JSONL text
17
- */
18
- export function decodeFrames(buffer) {
19
- const offsets = [];
20
- let at = 0;
21
- for (;;) {
22
- const hit = buffer.indexOf(MAGIC, at);
23
- if (hit < 0) break;
24
- offsets.push(hit);
25
- at = hit + MAGIC.length;
26
- }
27
- if (offsets.length === 0) return '';
28
- const parts = [];
29
- for (let i = 0; i < offsets.length; i += 1) {
30
- const start = offsets[i];
31
- const end = i + 1 < offsets.length ? offsets[i + 1] : buffer.length;
32
- try {
33
- parts.push(zlib.zstdDecompressSync(buffer.subarray(start, end)));
34
- } catch {
35
- // A frame that the writer never finished: stop here rather than skip it.
36
- break;
37
- }
38
- }
39
- return Buffer.concat(parts).toString('utf8');
40
- }
41
-
42
- function textOf(content) {
43
- if (!Array.isArray(content)) return '';
44
- const out = [];
45
- for (const part of content) {
46
- if (part && part.type === 'text' && typeof part.text === 'string') out.push(part.text);
47
- }
48
- return out.join('\n').trim();
49
- }
50
-
51
- function firstLine(value, limit) {
52
- const line = String(value || '').replace(/\s+/g, ' ').trim();
53
- if (line.length <= limit) return line;
54
- return line.slice(0, limit - 1) + '…';
55
- }
56
-
57
- /**
58
- * Fold the log into one record per turn.
59
- * @param {string} jsonl decoded log text
60
- * @returns {{title: string, turns: Array<object>, totals: object}}
61
- */
62
- export function foldTurns(jsonl) {
63
- const turns = new Map();
64
- let title = '';
65
- const order = [];
66
-
67
- const get = (n) => {
68
- let entry = turns.get(n);
69
- if (!entry) {
70
- entry = {
71
- n, start: 0, end: 0, prompt: '', summary: '', tools: 0,
72
- retries: 0, aborted: false, steps: 0,
73
- };
74
- turns.set(n, entry);
75
- order.push(entry);
76
- }
77
- return entry;
78
- };
79
-
80
- for (const line of jsonl.split('\n')) {
81
- if (!line) continue;
82
- let event;
83
- try {
84
- event = JSON.parse(line);
85
- } catch {
86
- continue;
87
- }
88
- const type = event.type;
89
- const data = event.data || {};
90
- const time = typeof event.time === 'number' ? event.time : 0;
91
-
92
- if (type === 'session/title' && typeof data.title === 'string' && data.title.trim()) {
93
- title = data.title.trim();
94
- continue;
95
- }
96
- if (type === 'turn/start' && Number.isInteger(data.turn)) {
97
- const entry = get(data.turn);
98
- entry.start = time;
99
- continue;
100
- }
101
- if (type === 'turn/end' && Number.isInteger(data.turn)) {
102
- const entry = get(data.turn);
103
- entry.end = time;
104
- const reason = data.reason || {};
105
- if (reason.kind === 'aborted') entry.aborted = true;
106
- continue;
107
- }
108
- if (type === 'user/message' && Number.isInteger(data.turn)) {
109
- const entry = get(data.turn);
110
- const source = data.source && data.source.kind;
111
- const text = textOf(data.content);
112
- if (!entry.prompt && text && (source === 'user' || source === 'user-approval')) {
113
- entry.prompt = firstLine(text, 120);
114
- }
115
- continue;
116
- }
117
- if (type === 'assistant/message' && Number.isInteger(data.turn)) {
118
- const entry = get(data.turn);
119
- const text = textOf(data.message && data.message.content);
120
- if (!entry.summary && text) entry.summary = firstLine(text, 96);
121
- continue;
122
- }
123
- if (type === 'tool/call' && Number.isInteger(data.turn)) {
124
- get(data.turn).tools += 1;
125
- continue;
126
- }
127
- if (type === 'llm/retry' && Number.isInteger(data.turn)) {
128
- get(data.turn).retries += 1;
129
- }
130
- }
131
-
132
- const list = order
133
- .filter((entry) => entry.start > 0)
134
- .sort((a, b) => a.n - b.n)
135
- .map((entry) => ({
136
- n: entry.n,
137
- start: entry.start,
138
- durationMs: entry.end > entry.start ? entry.end - entry.start : 0,
139
- tools: entry.tools,
140
- retries: entry.retries,
141
- aborted: entry.aborted,
142
- prompt: entry.prompt,
143
- summary: entry.summary,
144
- }));
145
-
146
- const totals = {
147
- turns: list.length,
148
- durationMs: list.reduce((sum, t) => sum + t.durationMs, 0),
149
- tools: list.reduce((sum, t) => sum + t.tools, 0),
150
- retries: list.reduce((sum, t) => sum + t.retries, 0),
151
- aborted: list.filter((t) => t.aborted).length,
152
- };
153
- return { title, turns: list, totals };
154
- }
155
-
156
- /**
157
- * Read and fold one session log.
158
- * @param {string} file absolute path to session.v4.jsonl.zstd
159
- * @returns {{title: string, turns: Array<object>, totals: object}}
160
- */
161
- export function readTurns(file) {
162
- const buffer = fs.readFileSync(file);
163
- return foldTurns(decodeFrames(buffer));
164
- }