dsh-ws-recency 0.15.0 → 0.17.0
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/README.md +8 -3
- package/client.js +2656 -2358
- package/index.js +114 -2
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -25,13 +25,14 @@
|
|
|
25
25
|
* @module dsh-ws-recency
|
|
26
26
|
*/
|
|
27
27
|
import { mkdir, readFile, readdir, rename, rm, stat as statPath, writeFile } from "node:fs/promises";
|
|
28
|
+
import zlib from "node:zlib";
|
|
28
29
|
import { dirname, join } from "node:path";
|
|
29
30
|
|
|
30
31
|
/** Cordis plugin name shown by the loader. */
|
|
31
32
|
export const name = "ws-recency";
|
|
32
33
|
|
|
33
34
|
/** Version marker reported by the health route. */
|
|
34
|
-
export const HOST_VERSION = "0.
|
|
35
|
+
export const HOST_VERSION = "0.17.0";
|
|
35
36
|
|
|
36
37
|
/**
|
|
37
38
|
* Exact routes the client half calls. Registered on the bare web server, so they
|
|
@@ -212,7 +213,16 @@ export async function trashSession(ctx, sessionId) {
|
|
|
212
213
|
try {
|
|
213
214
|
moved = await trashSessionLog(ctx, meta, trashRoot);
|
|
214
215
|
if (moved !== undefined) {
|
|
215
|
-
|
|
216
|
+
/* Card facts are captured now, while the log is still on this machine:
|
|
217
|
+
the list only has to read the sidecar afterwards. */
|
|
218
|
+
const card = await trashEntryMetadata(moved);
|
|
219
|
+
await writeTrashSidecar(moved, {
|
|
220
|
+
sessionId: meta.id,
|
|
221
|
+
cwd: typeof meta.cwd === "string" ? meta.cwd : null,
|
|
222
|
+
reason: "delete",
|
|
223
|
+
at: Date.now(),
|
|
224
|
+
...(card === null ? {} : { meta: card }),
|
|
225
|
+
});
|
|
216
226
|
}
|
|
217
227
|
} catch (error) {
|
|
218
228
|
return { ok: false, error: error instanceof Error ? error.message : String(error) };
|
|
@@ -826,6 +836,85 @@ async function readTrashSidecar(directory) {
|
|
|
826
836
|
}
|
|
827
837
|
}
|
|
828
838
|
|
|
839
|
+
/** The one log file a session directory holds. */
|
|
840
|
+
const SESSION_LOG = "session.v4.jsonl.zstd";
|
|
841
|
+
|
|
842
|
+
/** zstd frame magic: every append is its own frame, so a log must be split on it. */
|
|
843
|
+
const ZSTD_MAGIC = Buffer.from([0x28, 0xb5, 0x2f, 0xfd]);
|
|
844
|
+
|
|
845
|
+
/**
|
|
846
|
+
* Read a session log that is no longer reachable through the persistence service.
|
|
847
|
+
*
|
|
848
|
+
* A trashed log sits outside `sessions/`, so the Host's own reader cannot open
|
|
849
|
+
* it; the frames are decompressed directly with the bundled zstd support (Node
|
|
850
|
+
* 22.15 and newer). Without it the caller keeps the id-only card.
|
|
851
|
+
*/
|
|
852
|
+
async function readLogEvents(file) {
|
|
853
|
+
if (typeof zlib.zstdDecompressSync !== "function") return null;
|
|
854
|
+
const buffer = await readFile(file);
|
|
855
|
+
const offsets = [];
|
|
856
|
+
let index = buffer.indexOf(ZSTD_MAGIC, 0);
|
|
857
|
+
while (index !== -1) {
|
|
858
|
+
offsets.push(index);
|
|
859
|
+
index = buffer.indexOf(ZSTD_MAGIC, index + ZSTD_MAGIC.length);
|
|
860
|
+
}
|
|
861
|
+
if (offsets.length === 0) return null;
|
|
862
|
+
const parts = [];
|
|
863
|
+
for (let frame = 0; frame < offsets.length; frame += 1) {
|
|
864
|
+
const end = frame + 1 < offsets.length ? offsets[frame + 1] : buffer.length;
|
|
865
|
+
try {
|
|
866
|
+
parts.push(zlib.zstdDecompressSync(buffer.subarray(offsets[frame], end)));
|
|
867
|
+
} catch {
|
|
868
|
+
/* a torn tail frame is normal after a crash */
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
const text = Buffer.concat(parts).toString("utf8");
|
|
872
|
+
const events = [];
|
|
873
|
+
for (const line of text.split("\n")) {
|
|
874
|
+
if (line === "") continue;
|
|
875
|
+
try {
|
|
876
|
+
events.push(JSON.parse(line));
|
|
877
|
+
} catch {
|
|
878
|
+
/* skip a partial line */
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
return events;
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
/**
|
|
885
|
+
* The card facts for a trashed session: title, turns, steps and recorded tokens.
|
|
886
|
+
* @param directory - one entry of the trash root.
|
|
887
|
+
* @returns metadata, or null when the log cannot be read here.
|
|
888
|
+
*/
|
|
889
|
+
export async function trashEntryMetadata(directory) {
|
|
890
|
+
let events;
|
|
891
|
+
try {
|
|
892
|
+
events = await readLogEvents(join(directory, SESSION_LOG));
|
|
893
|
+
} catch {
|
|
894
|
+
return null;
|
|
895
|
+
}
|
|
896
|
+
if (events === null || events.length === 0) return null;
|
|
897
|
+
const summary = aggregateSession(events);
|
|
898
|
+
let turns = 0;
|
|
899
|
+
let messages = 0;
|
|
900
|
+
for (const event of events) {
|
|
901
|
+
if (event === null || typeof event !== "object") continue;
|
|
902
|
+
if (event.type === "turn/start") turns += 1;
|
|
903
|
+
if (event.type === "user/message") messages += 1;
|
|
904
|
+
}
|
|
905
|
+
if (turns === 0) turns = messages;
|
|
906
|
+
return {
|
|
907
|
+
title: typeof summary.session.title === "string" ? summary.session.title : "",
|
|
908
|
+
turns,
|
|
909
|
+
messages,
|
|
910
|
+
steps: summary.steps,
|
|
911
|
+
tokens: summary.totals.totalTokens,
|
|
912
|
+
inputTokens: summary.totals.inputTokens,
|
|
913
|
+
outputTokens: summary.totals.outputTokens,
|
|
914
|
+
lastAt: summary.session.lastAt,
|
|
915
|
+
};
|
|
916
|
+
}
|
|
917
|
+
|
|
829
918
|
/** Every entry in the plugin's trash, newest first. */
|
|
830
919
|
export async function listTrash() {
|
|
831
920
|
const root = await trashRoot();
|
|
@@ -853,9 +942,29 @@ export async function listTrash() {
|
|
|
853
942
|
} catch {
|
|
854
943
|
/* skip unreadable directories */
|
|
855
944
|
}
|
|
945
|
+
let meta = sidecar !== null && sidecar.meta !== null && typeof sidecar.meta === "object" ? sidecar.meta : null;
|
|
946
|
+
if (meta === null) {
|
|
947
|
+
/* One decode per entry, then cached in its sidecar so the list stays cheap. */
|
|
948
|
+
const computed = await trashEntryMetadata(directory);
|
|
949
|
+
if (computed !== null) {
|
|
950
|
+
meta = computed;
|
|
951
|
+
await writeTrashSidecar(directory, {
|
|
952
|
+
sessionId: sidecar !== null && typeof sidecar.sessionId === "string" ? sidecar.sessionId : null,
|
|
953
|
+
cwd: sidecar !== null && typeof sidecar.cwd === "string" ? sidecar.cwd : null,
|
|
954
|
+
reason: sidecar !== null && typeof sidecar.reason === "string" ? sidecar.reason : "unknown",
|
|
955
|
+
at: sidecar !== null && typeof sidecar.at === "number" ? sidecar.at : 0,
|
|
956
|
+
meta: computed,
|
|
957
|
+
});
|
|
958
|
+
}
|
|
959
|
+
}
|
|
856
960
|
results.push({
|
|
857
961
|
name: entry.name,
|
|
858
962
|
sizeBytes: size,
|
|
963
|
+
title: meta !== null && typeof meta.title === "string" ? meta.title : "",
|
|
964
|
+
turns: meta !== null && typeof meta.turns === "number" ? meta.turns : 0,
|
|
965
|
+
steps: meta !== null && typeof meta.steps === "number" ? meta.steps : 0,
|
|
966
|
+
tokens: meta !== null && typeof meta.tokens === "number" ? meta.tokens : 0,
|
|
967
|
+
lastAt: meta !== null && typeof meta.lastAt === "number" ? meta.lastAt : 0,
|
|
859
968
|
sessionId: sidecar !== null && typeof sidecar.sessionId === "string" ? sidecar.sessionId : null,
|
|
860
969
|
cwd: sidecar !== null && typeof sidecar.cwd === "string" ? sidecar.cwd : null,
|
|
861
970
|
reason: sidecar !== null && typeof sidecar.reason === "string" ? sidecar.reason : "unknown",
|
|
@@ -1074,6 +1183,8 @@ export async function listArchivedSessions(ctx) {
|
|
|
1074
1183
|
/* Last resort: the cache may have no record for this session at all. */
|
|
1075
1184
|
if (title === "") title = await titleFromLog(ctx, sessionId);
|
|
1076
1185
|
|
|
1186
|
+
const outlineTurns = checkpointValue(checkpoint, "turnOutline");
|
|
1187
|
+
const turns = Array.isArray(outlineTurns?.turns) ? outlineTurns.turns.length : 0;
|
|
1077
1188
|
const listMeta = checkpointValue(checkpoint, "sessionListMetadata");
|
|
1078
1189
|
let lastAt = typeof listMeta?.lastPromptAt === "number" ? listMeta.lastPromptAt : 0;
|
|
1079
1190
|
if (lastAt === 0 && typeof header?.updatedAt === "number") lastAt = header.updatedAt;
|
|
@@ -1086,6 +1197,7 @@ export async function listArchivedSessions(ctx) {
|
|
|
1086
1197
|
entries.push({
|
|
1087
1198
|
sessionId,
|
|
1088
1199
|
title,
|
|
1200
|
+
turns,
|
|
1089
1201
|
cwd,
|
|
1090
1202
|
createdAt: typeof header?.createdAt === "number" ? header.createdAt : 0,
|
|
1091
1203
|
lastAt,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-ws-recency",
|
|
3
|
-
"version": "0.
|
|
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, archived sessions managed from that same trash panel, per-session token statistics split
|
|
3
|
+
"version": "0.17.0",
|
|
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, archived sessions managed from that same trash panel, per-session token statistics with an argument/result split and an option to count tool arguments as the agent's writing, and moving a session to another workspace.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "chyxhtc",
|
|
7
7
|
"repository": {
|