dsh-ws-recency 0.30.1 → 0.31.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 +5 -2
- package/client.js +1017 -242
- package/index.js +259 -14
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -32,7 +32,7 @@ import { dirname, join } from "node:path";
|
|
|
32
32
|
export const name = "ws-recency";
|
|
33
33
|
|
|
34
34
|
/** Version marker reported by the health route. */
|
|
35
|
-
export const HOST_VERSION = "0.
|
|
35
|
+
export const HOST_VERSION = "0.31.0";
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
38
|
* Exact routes the client half calls. Registered on the bare web server, so they
|
|
@@ -551,6 +551,35 @@ export function aggregateSession(events) {
|
|
|
551
551
|
}
|
|
552
552
|
}
|
|
553
553
|
const attributed = [...byKind.values()].reduce((sum, entry) => sum + entry.amplified, 0);
|
|
554
|
+
/* The same file read twice shows up as two entries, so a per-target total
|
|
555
|
+
answers "what content is actually costing me" better than a per-read list
|
|
556
|
+
does. Only repeats are interesting here: a single big read is already in
|
|
557
|
+
the ranking below. */
|
|
558
|
+
const byTarget = new Map();
|
|
559
|
+
for (const block of blocks) {
|
|
560
|
+
if (!(block.amplified > 0)) continue;
|
|
561
|
+
const detail = typeof block.detail === "string" ? block.detail.trim() : "";
|
|
562
|
+
if (detail === "") continue;
|
|
563
|
+
const key = block.kind + "\u0000" + block.name + "\u0000" + detail;
|
|
564
|
+
let entry = byTarget.get(key);
|
|
565
|
+
if (entry === undefined) {
|
|
566
|
+
entry = { kind: block.kind, name: block.name, detail, count: 0, tokens: 0, amplified: 0 };
|
|
567
|
+
byTarget.set(key, entry);
|
|
568
|
+
}
|
|
569
|
+
entry.count += 1;
|
|
570
|
+
entry.tokens += block.tokens;
|
|
571
|
+
entry.amplified += block.amplified;
|
|
572
|
+
}
|
|
573
|
+
const repeated = [...byTarget.values()]
|
|
574
|
+
.filter((entry) => entry.count > 1)
|
|
575
|
+
.sort((a, b) => b.amplified - a.amplified)
|
|
576
|
+
.slice(0, 10)
|
|
577
|
+
.map((entry) => ({
|
|
578
|
+
...entry,
|
|
579
|
+
share: bill > 0 ? entry.amplified / bill : 0,
|
|
580
|
+
saving: Math.round(entry.amplified * 0.8),
|
|
581
|
+
}));
|
|
582
|
+
|
|
554
583
|
const ranked = blocks
|
|
555
584
|
.filter((block) => block.amplified > 0)
|
|
556
585
|
.sort((a, b) => b.amplified - a.amplified)
|
|
@@ -611,6 +640,7 @@ export function aggregateSession(events) {
|
|
|
611
640
|
/* Steps after which the window was rewritten. The curve restarts there, so the
|
|
612
641
|
client reads each segment on its own instead of comparing across a rewrite. */
|
|
613
642
|
compactions: [...compactions],
|
|
643
|
+
repeated,
|
|
614
644
|
blocks: ranked,
|
|
615
645
|
byKind: [...byKind.values()]
|
|
616
646
|
.map((entry) => ({ ...entry, share: bill > 0 ? entry.amplified / bill : 0 }))
|
|
@@ -663,7 +693,7 @@ async function readSessionEvents(ctx, sessionId) {
|
|
|
663
693
|
* pending, which is what the panel shows as "still indexing".
|
|
664
694
|
* ------------------------------------------------------------------------- */
|
|
665
695
|
|
|
666
|
-
const SEARCH_INDEX_VERSION =
|
|
696
|
+
const SEARCH_INDEX_VERSION = 4;
|
|
667
697
|
/** Longer messages are cut for the cache: snippets never need the tail. */
|
|
668
698
|
const SEARCH_MESSAGE_CAP = 200000;
|
|
669
699
|
const SEARCH_PER_SESSION = 3;
|
|
@@ -752,8 +782,12 @@ function extractSearchMessages(events) {
|
|
|
752
782
|
if (text === "") continue;
|
|
753
783
|
if (text.length > SEARCH_MESSAGE_CAP) text = text.slice(0, SEARCH_MESSAGE_CAP);
|
|
754
784
|
const declared = Number(data.turn);
|
|
785
|
+
const declaredStep = Number(data.step);
|
|
755
786
|
messages.push({
|
|
756
787
|
turn: Number.isSafeInteger(declared) ? declared : turn,
|
|
788
|
+
/* Assistant messages carry their step inside the turn; user messages do not
|
|
789
|
+
(they arrive before the turn's steps), so the step stays null there. */
|
|
790
|
+
step: Number.isSafeInteger(declaredStep) ? declaredStep : null,
|
|
757
791
|
seq: Number.isSafeInteger(Number(event.seq)) ? Number(event.seq) : 0,
|
|
758
792
|
at: Number.isFinite(Number(event.time)) ? Number(event.time) : 0,
|
|
759
793
|
role: event.type === "user/message" ? "user" : "assistant",
|
|
@@ -763,6 +797,93 @@ function extractSearchMessages(events) {
|
|
|
763
797
|
return messages;
|
|
764
798
|
}
|
|
765
799
|
|
|
800
|
+
/**
|
|
801
|
+
* The session's own title, read from the events already in hand.
|
|
802
|
+
*
|
|
803
|
+
* A session writes the title twice: a fallback that is just the first prompt's
|
|
804
|
+
* prefix, then the real one from the title provider. The first one is not the
|
|
805
|
+
* name shown in the sidebar, so the provider title wins and the fallback is only
|
|
806
|
+
* used when the provider never answered.
|
|
807
|
+
*/
|
|
808
|
+
function titleFromEvents(events) {
|
|
809
|
+
let provider = "";
|
|
810
|
+
let fallback = "";
|
|
811
|
+
let firstPrompt = "";
|
|
812
|
+
for (const event of events) {
|
|
813
|
+
if (event === null || typeof event !== "object") continue;
|
|
814
|
+
if (event.type === "session/title") {
|
|
815
|
+
const raw = typeof event.data === "string" ? event.data : event.data?.title;
|
|
816
|
+
const value = typeof raw === "string" ? raw.trim() : "";
|
|
817
|
+
if (value === "") continue;
|
|
818
|
+
if (event.data?.source?.kind === "fallback") fallback = value;
|
|
819
|
+
else provider = value;
|
|
820
|
+
continue;
|
|
821
|
+
}
|
|
822
|
+
if (firstPrompt === "" && event.type === "user/message") {
|
|
823
|
+
const text = conversationText(event.data?.message?.content ?? event.data?.content).trim();
|
|
824
|
+
const first = text.split("\n")[0].trim();
|
|
825
|
+
if (first !== "") firstPrompt = first.slice(0, 120);
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
if (provider !== "") return provider;
|
|
829
|
+
if (fallback !== "") return fallback;
|
|
830
|
+
return firstPrompt;
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
/**
|
|
834
|
+
* Fragments of the message to find again in the rendered transcript.
|
|
835
|
+
*
|
|
836
|
+
* The conversation view exposes no per-turn DOM anchor, so a jump locates what
|
|
837
|
+
* to scroll to by its text - and the log's text is markdown while the DOM's is
|
|
838
|
+
* not. `**bold**`, backticks, table pipes and heading marks all disappear when
|
|
839
|
+
* the message renders, so the raw window would never match. Several candidates
|
|
840
|
+
* are returned because a match can sit inside bold, inside a table cell, or at
|
|
841
|
+
* the start of a line the renderer rewrites.
|
|
842
|
+
*/
|
|
843
|
+
function searchSeeks(text, needle) {
|
|
844
|
+
const stripped = text
|
|
845
|
+
.replace(/```[\s\S]*?```/g, " ")
|
|
846
|
+
.replace(/^\s{0,6}#{1,6}\s+/gm, "")
|
|
847
|
+
.replace(/^\s{0,6}>\s?/gm, "")
|
|
848
|
+
.replace(/^\s{0,6}(?:[-*+]|\d+\.)\s+/gm, "")
|
|
849
|
+
.replace(/\*\*|__|`/g, "")
|
|
850
|
+
.replace(/\|/g, " ")
|
|
851
|
+
.replace(/\s+/g, " ")
|
|
852
|
+
.trim();
|
|
853
|
+
const index = needle === "" ? -1 : stripped.toLowerCase().indexOf(needle);
|
|
854
|
+
const out = [];
|
|
855
|
+
const push = (from) => {
|
|
856
|
+
const body = stripped.slice(Math.max(0, from), Math.max(0, from) + 70).trim();
|
|
857
|
+
if (body.length >= 8 && out.indexOf(body) === -1) out.push(body);
|
|
858
|
+
};
|
|
859
|
+
if (index >= 0) {
|
|
860
|
+
push(index);
|
|
861
|
+
push(index - 30);
|
|
862
|
+
}
|
|
863
|
+
push(0);
|
|
864
|
+
return out.slice(0, 3);
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
/**
|
|
868
|
+
* A window of the message around the match.
|
|
869
|
+
*
|
|
870
|
+
* Jumping into a long session makes the chat mount a large page of history, and
|
|
871
|
+
* that is slow; the panel can show the neighbourhood of a hit on its own. The
|
|
872
|
+
* window is collapsed to single spaces and capped because it crosses the IPC
|
|
873
|
+
* boundary for every hit.
|
|
874
|
+
*/
|
|
875
|
+
function searchContext(text, needle, radius) {
|
|
876
|
+
const span = Number.isFinite(radius) ? radius : 420;
|
|
877
|
+
const flat = text.replace(/\s+/g, " ");
|
|
878
|
+
const index = needle === "" ? -1 : flat.toLowerCase().indexOf(needle);
|
|
879
|
+
if (index < 0) return flat.slice(0, span * 2).trim();
|
|
880
|
+
const from = Math.max(0, index - span);
|
|
881
|
+
const to = Math.min(flat.length, index + needle.length + span);
|
|
882
|
+
const head = from > 0 ? "\u2026" : "";
|
|
883
|
+
const tail = to < flat.length ? "\u2026" : "";
|
|
884
|
+
return head + flat.slice(from, to).trim() + tail;
|
|
885
|
+
}
|
|
886
|
+
|
|
766
887
|
function searchSnippet(text, needle) {
|
|
767
888
|
const haystack = text.toLowerCase();
|
|
768
889
|
const index = needle === "" ? -1 : haystack.indexOf(needle);
|
|
@@ -779,10 +900,78 @@ function searchSnippet(text, needle) {
|
|
|
779
900
|
* `terms` are the whitespace-separated pieces of the query; all of them have to
|
|
780
901
|
* appear in the same message, so a two-word query narrows instead of widening.
|
|
781
902
|
*/
|
|
903
|
+
/**
|
|
904
|
+
* Turn a typed query into groups of alternatives plus filters.
|
|
905
|
+
*
|
|
906
|
+
* Plain words are ANDed, `or` (or `|`) joins alternatives inside one group, a
|
|
907
|
+
* quoted phrase stays whole, a leading `-` excludes, and `ws:` / `in:` / `role:`
|
|
908
|
+
* narrow the sessions rather than the text. Parsing lives on the host because
|
|
909
|
+
* that is where the transcript is matched; the client only shows the result.
|
|
910
|
+
*/
|
|
911
|
+
function parseSearchQuery(raw) {
|
|
912
|
+
const filters = { workspace: "", session: "", role: "" };
|
|
913
|
+
const groups = [[]];
|
|
914
|
+
const exclude = [];
|
|
915
|
+
const positive = [];
|
|
916
|
+
const tokens = String(raw ?? "").match(/"[^"]*"|\S+/g) || [];
|
|
917
|
+
let joinNext = false;
|
|
918
|
+
const closeGroup = () => {
|
|
919
|
+
if (groups[groups.length - 1].length > 0) groups.push([]);
|
|
920
|
+
};
|
|
921
|
+
for (const token of tokens) {
|
|
922
|
+
const quoted = token.length > 1 && token.startsWith('"') && token.endsWith('"');
|
|
923
|
+
const text = quoted ? token.slice(1, -1) : token;
|
|
924
|
+
if (text === "") continue;
|
|
925
|
+
const lower = text.toLowerCase();
|
|
926
|
+
const filter = lower.match(/^(ws|in|role|工作区|会话|角色):(.*)$/);
|
|
927
|
+
if (filter !== null && filter[2] !== "") {
|
|
928
|
+
const value = filter[2];
|
|
929
|
+
if (filter[1] === "ws" || filter[1] === "工作区") filters.workspace = value.toLowerCase();
|
|
930
|
+
else if (filter[1] === "in" || filter[1] === "会话") filters.session = value.toLowerCase();
|
|
931
|
+
else filters.role = value.toLowerCase();
|
|
932
|
+
continue;
|
|
933
|
+
}
|
|
934
|
+
if (!quoted && (lower === "or" || text === "|")) {
|
|
935
|
+
/* Reopen the group the previous term closed: `a or b` is one
|
|
936
|
+
requirement satisfied by either, not two requirements. */
|
|
937
|
+
if (groups.length > 0 && groups[groups.length - 1].length === 0) groups.pop();
|
|
938
|
+
joinNext = true;
|
|
939
|
+
continue;
|
|
940
|
+
}
|
|
941
|
+
if (!quoted && text.length > 1 && text.startsWith("-")) {
|
|
942
|
+
exclude.push(text.slice(1).toLowerCase());
|
|
943
|
+
continue;
|
|
944
|
+
}
|
|
945
|
+
const term = text.toLowerCase();
|
|
946
|
+
groups[groups.length - 1].push(term);
|
|
947
|
+
if (positive.indexOf(term) === -1) positive.push(term);
|
|
948
|
+
if (!joinNext) closeGroup();
|
|
949
|
+
joinNext = false;
|
|
950
|
+
}
|
|
951
|
+
const cleaned = groups.filter((group) => group.length > 0);
|
|
952
|
+
return { groups: cleaned, filters, exclude, terms: positive };
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
/** Whether a message satisfies the parsed query. */
|
|
956
|
+
function matchesQuery(parsed, haystack) {
|
|
957
|
+
for (const group of parsed.groups) {
|
|
958
|
+
if (!group.some((term) => haystack.indexOf(term) !== -1)) return false;
|
|
959
|
+
}
|
|
960
|
+
for (const term of parsed.exclude) {
|
|
961
|
+
if (haystack.indexOf(term) !== -1) return false;
|
|
962
|
+
}
|
|
963
|
+
return true;
|
|
964
|
+
}
|
|
965
|
+
|
|
782
966
|
export async function searchSessions(ctx, query, options = {}) {
|
|
783
967
|
const started = Date.now();
|
|
784
|
-
const
|
|
785
|
-
|
|
968
|
+
const parsed = parseSearchQuery(query);
|
|
969
|
+
const terms = parsed.terms;
|
|
970
|
+
const hasFilter = parsed.filters.workspace !== "" || parsed.filters.session !== ""
|
|
971
|
+
|| parsed.filters.role !== "";
|
|
972
|
+
if (terms.length === 0 && parsed.groups.length === 0 && !hasFilter) {
|
|
973
|
+
return { ok: true, query: "", terms: [], hits: [], scanned: 0, total: 0, pending: 0, took: 0 };
|
|
974
|
+
}
|
|
786
975
|
|
|
787
976
|
const directory = await searchIndexDirectory();
|
|
788
977
|
const workspaces = typeof ctx.workspaceRegistry?.list === "function" ? ctx.workspaceRegistry.list() : [];
|
|
@@ -831,33 +1020,80 @@ export async function searchSessions(ctx, query, options = {}) {
|
|
|
831
1020
|
}
|
|
832
1021
|
decoded += 1;
|
|
833
1022
|
let messages = [];
|
|
1023
|
+
let title = "";
|
|
834
1024
|
try {
|
|
835
|
-
|
|
1025
|
+
const events = await readSessionEvents(ctx, session.id);
|
|
1026
|
+
messages = extractSearchMessages(events);
|
|
1027
|
+
title = titleFromEvents(events);
|
|
836
1028
|
} catch {
|
|
837
1029
|
messages = [];
|
|
838
1030
|
}
|
|
839
|
-
await writeSearchCache(directory, session.id, { v: SEARCH_INDEX_VERSION, key, messages });
|
|
840
|
-
cached = { key, messages };
|
|
1031
|
+
await writeSearchCache(directory, session.id, { v: SEARCH_INDEX_VERSION, key, messages, title });
|
|
1032
|
+
cached = { key, messages, title };
|
|
841
1033
|
}
|
|
842
1034
|
|
|
1035
|
+
/* Filters narrow the sessions, so a filtered-out one costs nothing beyond
|
|
1036
|
+
the snapshot it was listed in. */
|
|
1037
|
+
if (parsed.filters.workspace !== "") {
|
|
1038
|
+
const cwd = normalizedPath(session.header.cwd);
|
|
1039
|
+
const name = String(workspaceByPath.get(cwd) ?? "").toLowerCase();
|
|
1040
|
+
if (name.indexOf(parsed.filters.workspace) === -1) continue;
|
|
1041
|
+
}
|
|
1042
|
+
if (parsed.filters.session !== "") {
|
|
1043
|
+
const headerTitle = typeof session.header.title === "string" ? session.header.title : "";
|
|
1044
|
+
const cachedTitle = typeof cached.title === "string" ? cached.title : "";
|
|
1045
|
+
const title = (headerTitle + " " + cachedTitle).toLowerCase();
|
|
1046
|
+
if (title.indexOf(parsed.filters.session) === -1) continue;
|
|
1047
|
+
}
|
|
1048
|
+
scanned += 0;
|
|
843
1049
|
let found = 0;
|
|
1050
|
+
/* How far down the transcript the last turn sits: the client uses it to jump
|
|
1051
|
+
to an approximate scroll position before it looks for the text. */
|
|
1052
|
+
let maxTurn = 0;
|
|
1053
|
+
for (const message of cached.messages) {
|
|
1054
|
+
if (message !== null && typeof message === "object" && Number.isFinite(Number(message.turn))) {
|
|
1055
|
+
if (Number(message.turn) > maxTurn) maxTurn = Number(message.turn);
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
844
1058
|
for (const message of cached.messages) {
|
|
845
1059
|
if (message === null || typeof message !== "object" || typeof message.text !== "string") continue;
|
|
846
1060
|
const haystack = message.text.toLowerCase();
|
|
847
|
-
if (!
|
|
848
|
-
|
|
849
|
-
|
|
1061
|
+
if (!matchesQuery(parsed, haystack)) continue;
|
|
1062
|
+
if (parsed.filters.role !== "") {
|
|
1063
|
+
const role = String(message.role || "").toLowerCase();
|
|
1064
|
+
const wanted = parsed.filters.role;
|
|
1065
|
+
const userish = wanted === "user" || wanted === "me" || wanted === "我";
|
|
1066
|
+
const agentish = wanted === "assistant" || wanted === "agent" || wanted === "助手";
|
|
1067
|
+
if (userish && role !== "user") continue;
|
|
1068
|
+
if (agentish && role === "user") continue;
|
|
1069
|
+
}
|
|
1070
|
+
const needle = terms.length > 0 ? terms[0] : "";
|
|
1071
|
+
const { snippet, lead, tail } = searchSnippet(message.text, needle);
|
|
1072
|
+
const seeks = searchSeeks(message.text, needle);
|
|
1073
|
+
const context = searchContext(message.text, needle);
|
|
1074
|
+
const headerTitle = typeof session.header.title === "string" ? session.header.title.trim() : "";
|
|
1075
|
+
const cachedTitle = typeof cached.title === "string" ? cached.title.trim() : "";
|
|
850
1076
|
const cwd = normalizedPath(session.header.cwd);
|
|
851
1077
|
hits.push({
|
|
852
1078
|
sessionId: session.id,
|
|
853
|
-
title:
|
|
1079
|
+
title: headerTitle !== "" ? headerTitle : (cachedTitle !== "" ? cachedTitle : titleFromMessages(cached.messages, session.id)),
|
|
854
1080
|
workspace: workspaceByPath.get(cwd) ?? "",
|
|
855
1081
|
cwd: typeof session.header.cwd === "string" ? session.header.cwd : "",
|
|
856
1082
|
turn: message.turn,
|
|
1083
|
+
step: message.step === undefined ? null : message.step,
|
|
857
1084
|
seq: message.seq,
|
|
858
1085
|
at: message.at,
|
|
859
1086
|
role: message.role,
|
|
860
1087
|
snippet,
|
|
1088
|
+
/* A piece of the message to find again in the rendered transcript: the
|
|
1089
|
+
chat exposes no per-turn DOM anchor, so the client locates the text. */
|
|
1090
|
+
seek: seeks.length > 0 ? seeks[0] : "",
|
|
1091
|
+
seeks,
|
|
1092
|
+
/* The matched neighbourhood, so reading a hit never needs the chat. */
|
|
1093
|
+
context,
|
|
1094
|
+
/* How many turns the session has, so the client can scroll to a rough
|
|
1095
|
+
position before looking for the text. */
|
|
1096
|
+
turns: maxTurn,
|
|
861
1097
|
lead: lead === true,
|
|
862
1098
|
tail: tail === true,
|
|
863
1099
|
});
|
|
@@ -1657,6 +1893,8 @@ function checkpointValue(record, key) {
|
|
|
1657
1893
|
*
|
|
1658
1894
|
* A title is written right after the first prompt, so the first page of the log
|
|
1659
1895
|
* is enough - and reading one page is far cheaper than folding the whole Log.
|
|
1896
|
+
* The page holds both the prefix fallback and the provider's title; the provider
|
|
1897
|
+
* one is the name the sidebar shows, so it wins.
|
|
1660
1898
|
*/
|
|
1661
1899
|
async function titleFromLog(ctx, sessionId) {
|
|
1662
1900
|
let handle;
|
|
@@ -1668,11 +1906,18 @@ async function titleFromLog(ctx, sessionId) {
|
|
|
1668
1906
|
try {
|
|
1669
1907
|
const slice = await handle.read(0, 60);
|
|
1670
1908
|
const events = slice !== null && slice !== undefined && Array.isArray(slice.events) ? slice.events : [];
|
|
1909
|
+
let provider = "";
|
|
1910
|
+
let fallback = "";
|
|
1671
1911
|
for (const event of events) {
|
|
1672
1912
|
if (event?.type !== "session/title") continue;
|
|
1673
|
-
const
|
|
1674
|
-
|
|
1675
|
-
|
|
1913
|
+
const raw = typeof event.data === "string" ? event.data : event.data?.title;
|
|
1914
|
+
const value = typeof raw === "string" ? raw.trim() : "";
|
|
1915
|
+
if (value === "") continue;
|
|
1916
|
+
if (event.data?.source?.kind === "fallback") fallback = value;
|
|
1917
|
+
else provider = value;
|
|
1918
|
+
}
|
|
1919
|
+
if (provider !== "") return provider;
|
|
1920
|
+
if (fallback !== "") return fallback;
|
|
1676
1921
|
for (const event of events) {
|
|
1677
1922
|
if (event?.type !== "user/message") continue;
|
|
1678
1923
|
const line = blockText(event.data?.content).split("\n")[0].trim();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-ws-recency",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.31.0",
|
|
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, 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, moving a session to another workspace, and a literal content search that jumps to the turn a word was said in.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "chyxhtc",
|