dsh-ws-recency 0.25.1 → 0.26.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.
- package/README.md +16 -2
- package/client.js +425 -3
- package/index.js +271 -4
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# dsh-ws-recency
|
|
2
2
|
|
|
3
|
-
DSH
|
|
3
|
+
DSH 侧栏的七件事:**工作区分组按最近活跃排序**、**过期历史自动折叠**、**运行中/未读/需跟进三色标记**、**所有置顶会话汇总到顶部一个区**、**删除会话到可恢复的回收站(归档会话也在同一个面板里管理)**、**按来源拆分、并将参数与结果分段的 token 消耗统计**、**把会话移动到另一个工作区**,外加**跨会话的内容搜索(点结果直接跳到说过那句话的那一轮)**。单独装上就有效,不依赖其它插件。
|
|
4
4
|
|
|
5
5
|
## 一、分组排序
|
|
6
6
|
|
|
@@ -46,6 +46,7 @@ DSH 原生的置顶只把会话顶到**它所属工作区分组的第一行**,
|
|
|
46
46
|
- 置顶区每行显示标题 + 所属工作区名,点行直接打开;hover 出现「取消置顶」;
|
|
47
47
|
- 置顶期间它在原工作区分组里的那一行**隐藏**(不重复出现),取消置顶后原行回到原位;
|
|
48
48
|
- 置顶行沿用同一套状态色(运行中蓝 / 完成未读绿 / 需要跟进琥珀),运行状态变化即时更新(0.25.1 修);
|
|
49
|
+
- 置顶行也带状态图标:运行中是转圈、完成未读是圆点、等你确认/回答时是同款琥珀点(0.26.1 起与分组里的行一致,之前只有颜色);
|
|
49
50
|
- 工作区分组、组内顺序、折叠、颜色全部照旧;
|
|
50
51
|
- 只在按工作区分组时出现;搜索、扁平视图、侧栏收起时自动隐藏。
|
|
51
52
|
|
|
@@ -146,6 +147,16 @@ DSH 原生的置顶只把会话顶到**它所属工作区分组的第一行**,
|
|
|
146
147
|
卡片只在**这一轮结束之后**才出现(0.24.2 修):轮询会比对回复的日志序号与「本轮是否还在跑」,只认点击之后产生、且已经收尾的那条回复——之前它读的是「全日志最后一条非空回复」,只要新轮先落一条工具步骤,就会把上一轮里的旧交接块当成答案,交接刚开始还在处理就弹卡;卡片也只挂在产生它的那一轮下面,不再在每个旧轮下面重复出现。
|
|
147
148
|
|
|
148
149
|
|
|
150
|
+
## 九、会话内容搜索
|
|
151
|
+
|
|
152
|
+
会话一长,靠滚动是找不回来的。侧栏的放大镜打开搜索页,输词回车:
|
|
153
|
+
|
|
154
|
+
- 搜的是**对话正文**:你的提问 + 助手的回复;不搜思维链、不搜工具参数和工具结果(那些会让命中被日志噪声淹没);
|
|
155
|
+
- 多个词用空格分隔,**要求同时出现在同一条消息里**(AND);大小写不敏感,纯字面匹配,不支持正则;
|
|
156
|
+
- 结果按新到旧排列,每条给出角色、会话标题、所属工作区、第几轮,以及带高亮的片段;
|
|
157
|
+
- 点一条结果:打开那个会话 → 滚到对应那一轮 → 闪一下蓝色描边。老会话的历史是懒加载的,插件会自己往上翻直到那一轮出现,最多等 8 秒;
|
|
158
|
+
- 第一次搜索要为每个会话建一次正文索引(存在 `~/.hypers-work/.wsr/search/<sessionId>.json`,按日志大小 + revision 决定是否失效)。面板顶部会显示「还有 N 个会话正在建立索引」,每轮补一批、结果自动刷新,之后同一份日志不再重复解码。
|
|
159
|
+
|
|
149
160
|
## 设置
|
|
150
161
|
|
|
151
162
|
**设置 → 通用**:
|
|
@@ -164,6 +175,9 @@ DSH 原生的置顶只把会话顶到**它所属工作区分组的第一行**,
|
|
|
164
175
|
- “未分组”桶永远排在真实工作区之后。
|
|
165
176
|
- 只在按工作区分组(含工作区树顶层)时生效。
|
|
166
177
|
- **可选互操作**:如果你另外装着 `dsh-plugin-busy-workspace`,本插件会尊重它写在分组上的 `data-busy-rank`,把它的紧急度当主键、档位内部再按活跃度排。没装就是纯最近活跃排序——本插件不依赖它。
|
|
178
|
+
- 搜索是**字面匹配**,不是全文检索:不支持正则、不排序相关性,结果只按时间倒序;正文外的内容(思维链、工具参数、工具结果、附件)不参与匹配。
|
|
179
|
+
- 搜索索引是插件自己的缓存(`~/.hypers-work/.wsr/search/`),删掉只会让下一次搜索重新建;宿主自带的 SQLite 全文索引默认关闭,本插件没有去改那个开关。
|
|
180
|
+
- 跳到某一轮靠宿主的 `data-chat-turn` 行和 `[data-conversation-scroll]` 滚动容器:宿主改这两个标识,跳转就会失效,搜索本身不受影响。
|
|
167
181
|
|
|
168
182
|
## 安装
|
|
169
183
|
|
|
@@ -182,7 +196,7 @@ dsh plugin --profile <profile> add github:chyxhtc/dsh-ws-recency
|
|
|
182
196
|
或固定版本的预构建包:
|
|
183
197
|
|
|
184
198
|
```powershell
|
|
185
|
-
dsh plugin --profile <profile> add https://github.com/chyxhtc/dsh-ws-recency/releases/download/v0.
|
|
199
|
+
dsh plugin --profile <profile> add https://github.com/chyxhtc/dsh-ws-recency/releases/download/v0.26.1/dsh-ws-recency-0.26.1.tgz
|
|
186
200
|
```
|
|
187
201
|
|
|
188
202
|
装完把 `dsh-ws-recency` 加进 `~/.hypers-work/profiles/<profile>/package.json` 的 `dsh.profile.bundles`,重启应用。
|
package/client.js
CHANGED
|
@@ -107,6 +107,15 @@
|
|
|
107
107
|
".wsr-pin-ws{flex:none;max-width:40%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-size:11px;color:var(--dsw-alias-label-tertiary)}",
|
|
108
108
|
".wsr-pin-unpin{flex:none;border:none;background:transparent;color:var(--dsw-alias-label-tertiary);cursor:pointer;font-size:11px;padding:2px 4px;border-radius:4px;visibility:hidden}",
|
|
109
109
|
".wsr-pin-row:hover .wsr-pin-unpin{visibility:visible}",
|
|
110
|
+
".wsr-pin-slot{flex:none;width:14px;height:14px;display:flex;align-items:center;justify-content:center}",
|
|
111
|
+
".wsr-pin-dot{display:block;width:10px;height:10px;border-radius:50%;background:var(--dsw-alias-label-tertiary,#8a8f98)}",
|
|
112
|
+
'.wsr-pin-dot[data-state="done"]{background:var(--dsw-alias-state-success-primary,#3fae6b)}',
|
|
113
|
+
'.wsr-pin-dot[data-state="warning"]{background:var(--dsw-alias-state-warning-primary,#d99a2b)}',
|
|
114
|
+
".wsr-pin-spin{animation:wsr-pin-spin 1s linear infinite;transform-origin:50% 50%}",
|
|
115
|
+
".wsr-pin-spin circle{fill:none;stroke-width:2}",
|
|
116
|
+
".wsr-pin-track{stroke:var(--dsw-alias-border-l3,rgba(255,255,255,.2))}",
|
|
117
|
+
".wsr-pin-arc{stroke:var(--dsw-alias-state-business-primary,#4f8ef7);stroke-linecap:round;stroke-dasharray:22 60}",
|
|
118
|
+
"@keyframes wsr-pin-spin{to{transform:rotate(360deg)}}",
|
|
110
119
|
".wsr-pin-row[" + STATE_ATTR + '="running"] .wsr-pin-title{color:#4f8ef7}',
|
|
111
120
|
".wsr-pin-row[" + STATE_ATTR + '="unread"] .wsr-pin-title{color:#3fae6b}',
|
|
112
121
|
".wsr-pin-row[" + STATE_ATTR + '="followup"] .wsr-pin-title{color:#d99a2b}',
|
|
@@ -279,6 +288,21 @@
|
|
|
279
288
|
'[' + STATE_ATTR + '="running"] [class*="_title"]{color:#4f8ef7}',
|
|
280
289
|
'[' + STATE_ATTR + '="unread"] [class*="_title"]{color:#3fae6b}',
|
|
281
290
|
'[' + STATE_ATTR + '="followup"] [class*="_title"]{color:#d99a2b}',
|
|
291
|
+
'[' + STATE_ATTR + '="waiting"] [class*="_title"]{color:#d99a2b}',
|
|
292
|
+
".wsr-search-bar{display:flex;gap:8px;align-items:center}",
|
|
293
|
+
".wsr-search-input{flex:1;min-width:0;height:30px;padding:0 10px;border-radius:6px;border:.5px solid var(--dsw-alias-border-l2);background:transparent;color:inherit;font-size:13px}",
|
|
294
|
+
".wsr-hit{display:flex;flex-direction:column;gap:4px;padding:8px 10px;border-radius:8px;cursor:pointer}",
|
|
295
|
+
".wsr-hit:hover{background:var(--dsw-alias-bg-layer-2)}",
|
|
296
|
+
".wsr-hit-head{display:flex;align-items:center;gap:8px;font-size:11px;color:var(--dsw-alias-label-tertiary)}",
|
|
297
|
+
".wsr-hit-title{flex:1;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;color:var(--dsw-alias-label-primary);font-size:13px}",
|
|
298
|
+
".wsr-hit-role{flex:none;padding:0 6px;border-radius:4px;background:var(--dsw-alias-bg-layer-2);font-size:10px}",
|
|
299
|
+
".wsr-hit-user{color:#4f8ef7}",
|
|
300
|
+
".wsr-hit-agent{color:#3fae6b}",
|
|
301
|
+
".wsr-hit-ws{flex:none;max-width:30%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}",
|
|
302
|
+
".wsr-hit-turn{flex:none}",
|
|
303
|
+
".wsr-hit-snippet{font-size:12px;line-height:18px;color:var(--dsw-alias-label-secondary);word-break:break-word}",
|
|
304
|
+
".wsr-mark{background:rgba(79,142,247,.28);color:inherit;border-radius:2px}",
|
|
305
|
+
'[data-wsr-flash="true"]{outline:2px solid #4f8ef7;outline-offset:2px;border-radius:8px}',
|
|
282
306
|
".wsr-dot{display:block;width:7px;height:7px;border-radius:50%;margin:0 auto;background:#d99a2b;cursor:pointer}",
|
|
283
307
|
".wsr-dot:hover{outline:2px solid var(--dsw-alias-border-l2)}",
|
|
284
308
|
".wsr-setRow{display:flex;align-items:center;gap:16px;justify-content:space-between;padding:12px 0}",
|
|
@@ -305,6 +329,9 @@
|
|
|
305
329
|
"pin.enabled": "\u663e\u793a\u7f6e\u9876\u533a",
|
|
306
330
|
"pin.head": "\u7f6e\u9876",
|
|
307
331
|
"pin.unpin": "\u53d6\u6d88\u7f6e\u9876",
|
|
332
|
+
"pin.status.running": "运行中",
|
|
333
|
+
"pin.status.done": "已完成",
|
|
334
|
+
"pin.status.followup": "需要跟进",
|
|
308
335
|
"argmode.title": "把工具参数计入主 agent",
|
|
309
336
|
"argmode.description": "工具参数是模型自己写的(pwsh 的命令、write 的文件内容),严格说算主 agent 的输出,不算工具的消耗。打开后消耗分布里工具只算返回结果,参数并入主 agent。",
|
|
310
337
|
"argmode.enabled": "工具参数计入主 agent",
|
|
@@ -374,6 +401,17 @@
|
|
|
374
401
|
"adopt.done": "已补登记 {n} 个会话。",
|
|
375
402
|
"adopt.cleaned": "清理了 {n} 处重复/失效的工作区归属。",
|
|
376
403
|
"adopt.failed": "扫描失败:",
|
|
404
|
+
"search.title": "搜索会话内容",
|
|
405
|
+
"search.placeholder": "输入关键词后回车;多个词用空格分隔,要求同时出现",
|
|
406
|
+
"search.run": "搜索",
|
|
407
|
+
"search.searching": "搜索中…",
|
|
408
|
+
"search.indexing": "还有 {n} 个会话正在建立索引,结果会陆续补上…",
|
|
409
|
+
"search.summary": "{hits} 处命中 · 共 {sessions} 个会话 · {ms} ms",
|
|
410
|
+
"search.empty": "没有匹配的内容。",
|
|
411
|
+
"search.failed": "搜索失败:",
|
|
412
|
+
"search.you": "你",
|
|
413
|
+
"search.assistant": "助手",
|
|
414
|
+
"search.turn": "第 {n} 轮",
|
|
377
415
|
"ui.reload": "刷新界面",
|
|
378
416
|
"ui.refresh": "刷新列表",
|
|
379
417
|
"trash.title": "回收站",
|
|
@@ -456,6 +494,9 @@
|
|
|
456
494
|
"argmode.enabled": "Tool arguments count for the agent",
|
|
457
495
|
"pin.head": "Pinned",
|
|
458
496
|
"pin.unpin": "Unpin",
|
|
497
|
+
"pin.status.running": "Running",
|
|
498
|
+
"pin.status.done": "Completed",
|
|
499
|
+
"pin.status.followup": "Needs follow-up",
|
|
459
500
|
"menu.flag": "Mark as needs follow-up",
|
|
460
501
|
"menu.unflag": "Clear follow-up mark",
|
|
461
502
|
"menu.trash": "Delete session (move to trash)",
|
|
@@ -522,6 +563,17 @@
|
|
|
522
563
|
"adopt.done": "Re-attached {n} session(s).",
|
|
523
564
|
"adopt.cleaned": " Cleared {n} stale membership(s).",
|
|
524
565
|
"adopt.failed": "Scan failed: ",
|
|
566
|
+
"search.title": "Search session content",
|
|
567
|
+
"search.placeholder": "Type a word and press Enter; separate words with spaces to require all of them",
|
|
568
|
+
"search.run": "Search",
|
|
569
|
+
"search.searching": "Searching\u2026",
|
|
570
|
+
"search.indexing": "Still indexing {n} session(s); results will fill in\u2026",
|
|
571
|
+
"search.summary": "{hits} hit(s) across {sessions} session(s) \u00b7 {ms} ms",
|
|
572
|
+
"search.empty": "Nothing matched.",
|
|
573
|
+
"search.failed": "Search failed: ",
|
|
574
|
+
"search.you": "You",
|
|
575
|
+
"search.assistant": "Assistant",
|
|
576
|
+
"search.turn": "turn {n}",
|
|
525
577
|
"ui.reload": "Reload UI",
|
|
526
578
|
"ui.refresh": "Refresh",
|
|
527
579
|
"trash.title": "Trash",
|
|
@@ -1093,11 +1145,88 @@
|
|
|
1093
1145
|
return false;
|
|
1094
1146
|
}
|
|
1095
1147
|
|
|
1148
|
+
/** The host's four status looks, mapped onto the plugin's own states. */
|
|
1149
|
+
function pinDotStateOf(state) {
|
|
1150
|
+
if (state === "running") return "ongoing";
|
|
1151
|
+
if (state === "waiting") return "warning";
|
|
1152
|
+
if (state === "unread") return "done";
|
|
1153
|
+
if (state === "followup") return "warning";
|
|
1154
|
+
return "idle";
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
/** The running spinner, drawn as the host draws it: track + arc, both circling. */
|
|
1158
|
+
function pinSpinner() {
|
|
1159
|
+
if (typeof document === "undefined" || typeof document.createElementNS !== "function") return null;
|
|
1160
|
+
try {
|
|
1161
|
+
var svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
|
1162
|
+
svg.setAttribute("class", "wsr-pin-spin");
|
|
1163
|
+
svg.setAttribute("viewBox", "0 0 24 24");
|
|
1164
|
+
svg.setAttribute("width", "14");
|
|
1165
|
+
svg.setAttribute("height", "14");
|
|
1166
|
+
svg.setAttribute("aria-hidden", "true");
|
|
1167
|
+
var track = document.createElementNS("http://www.w3.org/2000/svg", "circle");
|
|
1168
|
+
track.setAttribute("class", "wsr-pin-track");
|
|
1169
|
+
track.setAttribute("cx", "12");
|
|
1170
|
+
track.setAttribute("cy", "12");
|
|
1171
|
+
track.setAttribute("r", "9.5");
|
|
1172
|
+
var arc = document.createElementNS("http://www.w3.org/2000/svg", "circle");
|
|
1173
|
+
arc.setAttribute("class", "wsr-pin-arc");
|
|
1174
|
+
arc.setAttribute("cx", "12");
|
|
1175
|
+
arc.setAttribute("cy", "12");
|
|
1176
|
+
arc.setAttribute("r", "9.5");
|
|
1177
|
+
svg.appendChild(track);
|
|
1178
|
+
svg.appendChild(arc);
|
|
1179
|
+
return svg;
|
|
1180
|
+
} catch (error) {
|
|
1181
|
+
return null;
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
/**
|
|
1186
|
+
* Repaint the leading marker of a pinned row.
|
|
1187
|
+
*
|
|
1188
|
+
* A grouped row gets this from the host (`SessionStatusDots`: spinner while
|
|
1189
|
+
* running, a dot when done). Pinned rows are the plugin's own DOM, so the
|
|
1190
|
+
* same shapes are drawn here; the paint is skipped when the state did not
|
|
1191
|
+
* change, so a running spinner is not re-created on every pass.
|
|
1192
|
+
*/
|
|
1193
|
+
function paintPinState(row, state) {
|
|
1194
|
+
if (row === null || typeof row.querySelector !== "function") return;
|
|
1195
|
+
var slot = row.querySelector(".wsr-pin-slot");
|
|
1196
|
+
if (slot === null) return;
|
|
1197
|
+
var key = pinDotStateOf(state);
|
|
1198
|
+
if (slot.getAttribute("data-wsr-dot") === key) return;
|
|
1199
|
+
slot.setAttribute("data-wsr-dot", key);
|
|
1200
|
+
while (slot.firstChild !== undefined && slot.firstChild !== null) slot.removeChild(slot.firstChild);
|
|
1201
|
+
if (key === "ongoing") {
|
|
1202
|
+
slot.setAttribute("title", t("pin.status.running"));
|
|
1203
|
+
var spinner = pinSpinner();
|
|
1204
|
+
if (spinner !== null) slot.appendChild(spinner);
|
|
1205
|
+
return;
|
|
1206
|
+
}
|
|
1207
|
+
if (key === "idle") {
|
|
1208
|
+
slot.setAttribute("title", "");
|
|
1209
|
+
return;
|
|
1210
|
+
}
|
|
1211
|
+
var label = key === "done" ? t("pin.status.done") : t("pin.status.followup");
|
|
1212
|
+
var dot = document.createElement("span");
|
|
1213
|
+
dot.className = "wsr-pin-dot";
|
|
1214
|
+
dot.setAttribute("data-state", key);
|
|
1215
|
+
dot.setAttribute("role", "img");
|
|
1216
|
+
dot.setAttribute("aria-label", label);
|
|
1217
|
+
dot.setAttribute("title", label);
|
|
1218
|
+
slot.appendChild(dot);
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1096
1221
|
function pinnedRowElement(item, state) {
|
|
1097
1222
|
var row = document.createElement("div");
|
|
1098
1223
|
row.className = "wsr-pin-row";
|
|
1099
1224
|
row.setAttribute("data-row-key", "pin:" + item.id);
|
|
1100
1225
|
setRowState(row, state === undefined ? null : state);
|
|
1226
|
+
var slot = document.createElement("span");
|
|
1227
|
+
slot.className = "wsr-pin-slot";
|
|
1228
|
+
row.appendChild(slot);
|
|
1229
|
+
paintPinState(row, state === undefined ? null : state);
|
|
1101
1230
|
var title = document.createElement("span");
|
|
1102
1231
|
title.className = "wsr-pin-title";
|
|
1103
1232
|
title.textContent = item.title;
|
|
@@ -1126,7 +1255,8 @@
|
|
|
1126
1255
|
}
|
|
1127
1256
|
|
|
1128
1257
|
/** Hide the copies inside their own groups so nothing is listed twice. */
|
|
1129
|
-
function hideOriginalRows(entries, pinnedIds) {
|
|
1258
|
+
function hideOriginalRows(entries, pinnedIds) {
|
|
1259
|
+
for (var i = 0; i < entries.length; i += 1) {
|
|
1130
1260
|
var rows = rowElements(entries[i].section);
|
|
1131
1261
|
for (var j = 0; j < rows.length; j += 1) {
|
|
1132
1262
|
var id = sessionIdOf(rows[j]);
|
|
@@ -1161,7 +1291,9 @@
|
|
|
1161
1291
|
for (var i = 0; i < rows.length; i += 1) {
|
|
1162
1292
|
var key = rows[i].getAttribute("data-row-key");
|
|
1163
1293
|
var id = key === null ? "" : key.slice("pin:".length);
|
|
1164
|
-
|
|
1294
|
+
var state = statesOfId(states, id);
|
|
1295
|
+
setRowState(rows[i], state);
|
|
1296
|
+
paintPinState(rows[i], state);
|
|
1165
1297
|
}
|
|
1166
1298
|
}
|
|
1167
1299
|
|
|
@@ -1271,7 +1403,13 @@
|
|
|
1271
1403
|
conversation moved on - continuing a conversation is often the very
|
|
1272
1404
|
reason a follow-up is still owed. */
|
|
1273
1405
|
var flagged = flags[id] !== undefined;
|
|
1274
|
-
|
|
1406
|
+
/* Waiting on the user outranks everything the host itself reports:
|
|
1407
|
+
an approval, a plan review or a question is what the row is for now. */
|
|
1408
|
+
var pending = status !== null && status.pendingInteraction !== null && status.pendingInteraction !== undefined
|
|
1409
|
+
? status.pendingInteraction.kind
|
|
1410
|
+
: undefined;
|
|
1411
|
+
var waiting = pending === "approval" || pending === "plan-review" || pending === "question";
|
|
1412
|
+
states[id] = waiting ? "waiting" : running ? "running" : unread ? "unread" : flagged ? "followup" : null;
|
|
1275
1413
|
}
|
|
1276
1414
|
return states;
|
|
1277
1415
|
}
|
|
@@ -3210,6 +3348,263 @@
|
|
|
3210
3348
|
});
|
|
3211
3349
|
}
|
|
3212
3350
|
|
|
3351
|
+
/* ---------- session content search -------------------------------- */
|
|
3352
|
+
|
|
3353
|
+
/** What the uncontrolled input holds; the panel keeps only results in state. */
|
|
3354
|
+
var searchDraft = "";
|
|
3355
|
+
/** Bumped per search so a slow answer cannot overwrite a newer one. */
|
|
3356
|
+
var searchGeneration = 0;
|
|
3357
|
+
|
|
3358
|
+
function searchPost(body) {
|
|
3359
|
+
return fetch("/ws-recency/search", {
|
|
3360
|
+
method: "POST",
|
|
3361
|
+
headers: { "content-type": "application/json" },
|
|
3362
|
+
body: JSON.stringify(body || {}),
|
|
3363
|
+
}).then(function (response) {
|
|
3364
|
+
return response.text().then(function (text) {
|
|
3365
|
+
var parsed = null;
|
|
3366
|
+
try { parsed = JSON.parse(text); } catch (error) { parsed = null; }
|
|
3367
|
+
return { status: response.status, text: text, json: parsed };
|
|
3368
|
+
});
|
|
3369
|
+
});
|
|
3370
|
+
}
|
|
3371
|
+
|
|
3372
|
+
function SearchPanelIcon(props) {
|
|
3373
|
+
var Icon = ui.IconSearchOutlineRegular;
|
|
3374
|
+
if (typeof Icon !== "function") return null;
|
|
3375
|
+
var size = props !== undefined && props !== null && typeof props.size === "number" ? props.size : 16;
|
|
3376
|
+
return h(Icon, { size: size });
|
|
3377
|
+
}
|
|
3378
|
+
|
|
3379
|
+
function conversationScroller() {
|
|
3380
|
+
if (typeof document === "undefined") return null;
|
|
3381
|
+
try {
|
|
3382
|
+
var direct = document.querySelector("[data-conversation-scroll]");
|
|
3383
|
+
if (direct !== null) return direct;
|
|
3384
|
+
/* The marker is the documented hook; without it, take the nearest
|
|
3385
|
+
ancestor that actually scrolls rather than giving up. */
|
|
3386
|
+
var anyRow = document.querySelector("[data-chat-turn]");
|
|
3387
|
+
var node = anyRow === null ? null : anyRow.parentElement;
|
|
3388
|
+
while (node !== null && node !== undefined) {
|
|
3389
|
+
if (typeof node.scrollHeight === "number" && node.scrollHeight > node.clientHeight && node.clientHeight > 0) return node;
|
|
3390
|
+
node = node.parentElement;
|
|
3391
|
+
}
|
|
3392
|
+
return null;
|
|
3393
|
+
} catch (error) {
|
|
3394
|
+
return null;
|
|
3395
|
+
}
|
|
3396
|
+
}
|
|
3397
|
+
|
|
3398
|
+
function turnRowOf(turn) {
|
|
3399
|
+
if (typeof document === "undefined" || !isFinite(turn)) return null;
|
|
3400
|
+
try {
|
|
3401
|
+
return document.querySelector('[data-chat-turn="' + String(turn) + '"]');
|
|
3402
|
+
} catch (error) {
|
|
3403
|
+
return null;
|
|
3404
|
+
}
|
|
3405
|
+
}
|
|
3406
|
+
|
|
3407
|
+
/** Ring the row the search sent the user to, without taking keyboard focus. */
|
|
3408
|
+
function flashTurnRow(row) {
|
|
3409
|
+
try {
|
|
3410
|
+
if (typeof row.scrollIntoView === "function") row.scrollIntoView({ block: "center" });
|
|
3411
|
+
} catch (error) {
|
|
3412
|
+
/* some engines only accept the boolean form */
|
|
3413
|
+
}
|
|
3414
|
+
try {
|
|
3415
|
+
row.setAttribute("data-wsr-flash", "true");
|
|
3416
|
+
window.setTimeout(function () {
|
|
3417
|
+
if (row.getAttribute("data-wsr-flash") !== null) row.removeAttribute("data-wsr-flash");
|
|
3418
|
+
}, 2600);
|
|
3419
|
+
} catch (error) {
|
|
3420
|
+
/* the conversation may have been replaced while we waited */
|
|
3421
|
+
}
|
|
3422
|
+
}
|
|
3423
|
+
|
|
3424
|
+
/**
|
|
3425
|
+
* Bring one turn of one session into view.
|
|
3426
|
+
*
|
|
3427
|
+
* The chat mounts recent turns first and loads older history as the
|
|
3428
|
+
* scroller approaches its top, so this walks the scroller upwards until
|
|
3429
|
+
* the turn exists, then centres it. Every step is DOM-only: nothing here
|
|
3430
|
+
* steals focus or synthesises keystrokes.
|
|
3431
|
+
*/
|
|
3432
|
+
function revealTurn(sessionId, turn) {
|
|
3433
|
+
if (typeof document === "undefined" || !isFinite(turn)) return;
|
|
3434
|
+
var deadline = Date.now() + 8000;
|
|
3435
|
+
var steps = 0;
|
|
3436
|
+
var tick = function () {
|
|
3437
|
+
if (Date.now() > deadline) return;
|
|
3438
|
+
if (currentConversationId() !== sessionId) {
|
|
3439
|
+
window.setTimeout(tick, 150);
|
|
3440
|
+
return;
|
|
3441
|
+
}
|
|
3442
|
+
var row = turnRowOf(turn);
|
|
3443
|
+
if (row !== null) {
|
|
3444
|
+
flashTurnRow(row);
|
|
3445
|
+
return;
|
|
3446
|
+
}
|
|
3447
|
+
var scroller = conversationScroller();
|
|
3448
|
+
if (scroller === null) {
|
|
3449
|
+
window.setTimeout(tick, 150);
|
|
3450
|
+
return;
|
|
3451
|
+
}
|
|
3452
|
+
if (steps >= 24) {
|
|
3453
|
+
window.setTimeout(tick, 300);
|
|
3454
|
+
return;
|
|
3455
|
+
}
|
|
3456
|
+
steps += 1;
|
|
3457
|
+
var height = typeof scroller.clientHeight === "number" && scroller.clientHeight > 0 ? scroller.clientHeight : 600;
|
|
3458
|
+
var top = Number(scroller.scrollTop) || 0;
|
|
3459
|
+
scroller.scrollTop = Math.max(0, top - Math.max(240, height * 0.8));
|
|
3460
|
+
window.setTimeout(tick, 260);
|
|
3461
|
+
};
|
|
3462
|
+
window.setTimeout(tick, 120);
|
|
3463
|
+
}
|
|
3464
|
+
|
|
3465
|
+
function jumpToSearchHit(hit) {
|
|
3466
|
+
openSessionRow(hit.sessionId);
|
|
3467
|
+
revealTurn(hit.sessionId, hit.turn);
|
|
3468
|
+
}
|
|
3469
|
+
|
|
3470
|
+
/** Snippet text with every query term wrapped in a mark. */
|
|
3471
|
+
function highlightSnippet(snippet, terms) {
|
|
3472
|
+
var text = typeof snippet === "string" ? snippet : "";
|
|
3473
|
+
var lower = text.toLowerCase();
|
|
3474
|
+
var ranges = [];
|
|
3475
|
+
for (var i = 0; i < terms.length; i += 1) {
|
|
3476
|
+
var term = String(terms[i] || "").toLowerCase();
|
|
3477
|
+
if (term === "") continue;
|
|
3478
|
+
var from = 0;
|
|
3479
|
+
while (from < lower.length) {
|
|
3480
|
+
var at = lower.indexOf(term, from);
|
|
3481
|
+
if (at < 0) break;
|
|
3482
|
+
ranges.push({ start: at, end: at + term.length });
|
|
3483
|
+
from = at + term.length;
|
|
3484
|
+
}
|
|
3485
|
+
}
|
|
3486
|
+
ranges.sort(function (a, b) { return a.start - b.start; });
|
|
3487
|
+
var pieces = [];
|
|
3488
|
+
var cursor = 0;
|
|
3489
|
+
for (var j = 0; j < ranges.length; j += 1) {
|
|
3490
|
+
var range = ranges[j];
|
|
3491
|
+
if (range.start < cursor) continue;
|
|
3492
|
+
if (range.start > cursor) pieces.push(h("span", { key: "t" + j }, text.slice(cursor, range.start)));
|
|
3493
|
+
pieces.push(h("mark", { key: "m" + j, className: "wsr-mark" }, text.slice(range.start, range.end)));
|
|
3494
|
+
cursor = range.end;
|
|
3495
|
+
}
|
|
3496
|
+
if (cursor < text.length) pieces.push(h("span", { key: "tail" }, text.slice(cursor)));
|
|
3497
|
+
return pieces;
|
|
3498
|
+
}
|
|
3499
|
+
|
|
3500
|
+
/**
|
|
3501
|
+
* The search page: one input, one list of hits.
|
|
3502
|
+
*
|
|
3503
|
+
* The host answers with the sessions it managed to index inside its time
|
|
3504
|
+
* budget plus how many are still pending, so this asks again until the
|
|
3505
|
+
* index has caught up instead of making the user press search twice.
|
|
3506
|
+
*/
|
|
3507
|
+
function SearchPanelPage() {
|
|
3508
|
+
var state = react.useState({ loaded: false, query: "", hits: [], terms: [], scanned: 0, total: 0, pending: 0, took: 0, busy: false, error: "" });
|
|
3509
|
+
var view = state[0];
|
|
3510
|
+
var setView = state[1];
|
|
3511
|
+
|
|
3512
|
+
var run = function (text, refresh) {
|
|
3513
|
+
var query = typeof text === "string" ? text.trim() : "";
|
|
3514
|
+
searchGeneration += 1;
|
|
3515
|
+
var generation = searchGeneration;
|
|
3516
|
+
if (query === "") {
|
|
3517
|
+
setView({ loaded: true, query: "", hits: [], terms: [], scanned: 0, total: 0, pending: 0, took: 0, busy: false, error: "" });
|
|
3518
|
+
return;
|
|
3519
|
+
}
|
|
3520
|
+
setView({ loaded: true, query: query, hits: view.hits, terms: view.terms, scanned: view.scanned, total: view.total, pending: view.pending, took: view.took, busy: true, error: "" });
|
|
3521
|
+
var ask = function (needRefresh) {
|
|
3522
|
+
searchPost({ query: query, refresh: needRefresh === true }).then(function (answer) {
|
|
3523
|
+
if (generation !== searchGeneration) return;
|
|
3524
|
+
var json = answer.json;
|
|
3525
|
+
if (json === null || json.ok !== true) {
|
|
3526
|
+
setView({ loaded: true, query: query, hits: [], terms: [], scanned: 0, total: 0, pending: 0, took: 0, busy: false, error: t("search.failed") + "HTTP " + answer.status });
|
|
3527
|
+
return;
|
|
3528
|
+
}
|
|
3529
|
+
setView({
|
|
3530
|
+
loaded: true,
|
|
3531
|
+
query: query,
|
|
3532
|
+
hits: Array.isArray(json.hits) ? json.hits : [],
|
|
3533
|
+
terms: Array.isArray(json.terms) ? json.terms : [],
|
|
3534
|
+
scanned: json.scanned || 0,
|
|
3535
|
+
total: json.total || 0,
|
|
3536
|
+
pending: json.pending || 0,
|
|
3537
|
+
took: json.took || 0,
|
|
3538
|
+
busy: false,
|
|
3539
|
+
error: "",
|
|
3540
|
+
});
|
|
3541
|
+
if (json.pending > 0) {
|
|
3542
|
+
window.setTimeout(function () {
|
|
3543
|
+
if (generation === searchGeneration) ask(false);
|
|
3544
|
+
}, 150);
|
|
3545
|
+
}
|
|
3546
|
+
}).catch(function (error) {
|
|
3547
|
+
if (generation !== searchGeneration) return;
|
|
3548
|
+
setView({ loaded: true, query: query, hits: [], terms: [], scanned: 0, total: 0, pending: 0, took: 0, busy: false, error: t("search.failed") + (error && error.message ? error.message : error) });
|
|
3549
|
+
});
|
|
3550
|
+
};
|
|
3551
|
+
ask(refresh === true);
|
|
3552
|
+
};
|
|
3553
|
+
|
|
3554
|
+
var input = h("input", {
|
|
3555
|
+
className: "wsr-search-input",
|
|
3556
|
+
type: "search",
|
|
3557
|
+
placeholder: t("search.placeholder"),
|
|
3558
|
+
defaultValue: "",
|
|
3559
|
+
onChange: function (event) {
|
|
3560
|
+
searchDraft = event !== undefined && event.target !== undefined ? String(event.target.value || "") : "";
|
|
3561
|
+
},
|
|
3562
|
+
onKeyDown: function (event) {
|
|
3563
|
+
if (event !== undefined && event.key === "Enter") {
|
|
3564
|
+
if (typeof event.preventDefault === "function") event.preventDefault();
|
|
3565
|
+
run(searchDraft, false);
|
|
3566
|
+
}
|
|
3567
|
+
},
|
|
3568
|
+
});
|
|
3569
|
+
|
|
3570
|
+
var rows = view.hits.map(function (hit, index) {
|
|
3571
|
+
return h("div", {
|
|
3572
|
+
className: "wsr-hit",
|
|
3573
|
+
key: String(hit.sessionId || "") + ":" + String(hit.seq || 0) + ":" + String(index),
|
|
3574
|
+
title: hit.cwd || "",
|
|
3575
|
+
onClick: function () { jumpToSearchHit(hit); },
|
|
3576
|
+
},
|
|
3577
|
+
h("div", { className: "wsr-hit-head" },
|
|
3578
|
+
h("span", { className: "wsr-hit-role " + (hit.role === "user" ? "wsr-hit-user" : "wsr-hit-agent") }, hit.role === "user" ? t("search.you") : t("search.assistant")),
|
|
3579
|
+
h("span", { className: "wsr-hit-title" }, hit.title || hit.sessionId || ""),
|
|
3580
|
+
typeof hit.workspace === "string" && hit.workspace !== "" ? h("span", { className: "wsr-hit-ws" }, hit.workspace) : null,
|
|
3581
|
+
h("span", { className: "wsr-hit-turn" }, t("search.turn", { n: String(hit.turn) })),
|
|
3582
|
+
),
|
|
3583
|
+
h("div", { className: "wsr-hit-snippet" },
|
|
3584
|
+
hit.lead === true ? "\u2026" : null,
|
|
3585
|
+
highlightSnippet(hit.snippet || "", view.terms),
|
|
3586
|
+
hit.tail === true ? "\u2026" : null,
|
|
3587
|
+
),
|
|
3588
|
+
);
|
|
3589
|
+
});
|
|
3590
|
+
|
|
3591
|
+
return h("div", { className: "wsr-page wsr-search-page" },
|
|
3592
|
+
h("div", { className: "wsr-page-head" },
|
|
3593
|
+
h("div", { className: "wsr-page-heading" }, t("search.title")),
|
|
3594
|
+
),
|
|
3595
|
+
h("div", { className: "wsr-search-bar" },
|
|
3596
|
+
input,
|
|
3597
|
+
h("button", { type: "button", className: "wsr-btn", onClick: function () { run(searchDraft, false); } }, t("search.run")),
|
|
3598
|
+
),
|
|
3599
|
+
view.busy ? h("div", { className: "wsr-page-note" }, t("search.searching")) : null,
|
|
3600
|
+
view.error !== "" ? h("div", { className: "wsr-page-note wsr-page-error" }, view.error) : null,
|
|
3601
|
+
view.pending > 0 ? h("div", { className: "wsr-page-note" }, t("search.indexing", { n: String(view.pending) })) : null,
|
|
3602
|
+
view.loaded && !view.busy ? h("div", { className: "wsr-page-note" }, t("search.summary", { hits: String(view.hits.length), sessions: String(view.total), ms: String(view.took) })) : null,
|
|
3603
|
+
view.loaded && !view.busy && view.hits.length === 0 ? h("div", { className: "wsr-page-note" }, t("search.empty")) : null,
|
|
3604
|
+
h("div", { className: "wsr-page-list" }, rows),
|
|
3605
|
+
);
|
|
3606
|
+
}
|
|
3607
|
+
|
|
3213
3608
|
/** The sidebar's trash entry glyph. */
|
|
3214
3609
|
function TrashPanelIcon(props) {
|
|
3215
3610
|
var Icon = ui.IconTrashOutlineRegular;
|
|
@@ -3689,6 +4084,24 @@
|
|
|
3689
4084
|
}, ColorsRow);
|
|
3690
4085
|
});
|
|
3691
4086
|
|
|
4087
|
+
ctx.slots.inject("sidebar.panellist", function () {
|
|
4088
|
+
return ctx.slots.register({
|
|
4089
|
+
name: "sidebar.panellist",
|
|
4090
|
+
id: "ws-recency-search",
|
|
4091
|
+
order: 19,
|
|
4092
|
+
locale: NS,
|
|
4093
|
+
label: function () { return t("search.title"); },
|
|
4094
|
+
}, SearchPanelIcon);
|
|
4095
|
+
});
|
|
4096
|
+
|
|
4097
|
+
ctx.slots.inject("main", function () {
|
|
4098
|
+
return ctx.slots.register({
|
|
4099
|
+
name: "main",
|
|
4100
|
+
key: "ws-recency-search",
|
|
4101
|
+
locale: NS,
|
|
4102
|
+
}, SearchPanelPage);
|
|
4103
|
+
});
|
|
4104
|
+
|
|
3692
4105
|
ctx.slots.inject("sidebar.panellist", function () {
|
|
3693
4106
|
return ctx.slots.register({
|
|
3694
4107
|
name: "sidebar.panellist",
|
|
@@ -3827,6 +4240,15 @@
|
|
|
3827
4240
|
send: sendToSession,
|
|
3828
4241
|
};
|
|
3829
4242
|
|
|
4243
|
+
/* Test hook: the jump and the highlighting are pure DOM helpers. */
|
|
4244
|
+
exports.__search = {
|
|
4245
|
+
draft: function () { return searchDraft; },
|
|
4246
|
+
highlight: highlightSnippet,
|
|
4247
|
+
reveal: revealTurn,
|
|
4248
|
+
jump: jumpToSearchHit,
|
|
4249
|
+
turnRow: turnRowOf,
|
|
4250
|
+
};
|
|
4251
|
+
|
|
3830
4252
|
exports.name = "dsh-ws-recency";
|
|
3831
4253
|
exports.inject = inject;
|
|
3832
4254
|
exports.apply = apply_;
|
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.26.1";
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
38
|
* Exact routes the client half calls. Registered on the bare web server, so they
|
|
@@ -44,6 +44,7 @@ export const HEALTH_PATH = "/ws-recency/health";
|
|
|
44
44
|
export const MOVE_SESSION_PATH = "/ws-recency/session-move";
|
|
45
45
|
export const ADOPT_SESSION_PATH = "/ws-recency/session-adopt";
|
|
46
46
|
export const TRASH_LIST_PATH = "/ws-recency/trash";
|
|
47
|
+
export const SEARCH_CONTENT_PATH = "/ws-recency/search";
|
|
47
48
|
|
|
48
49
|
export const HANDOFF_PATH = "/ws-recency/handoff";
|
|
49
50
|
|
|
@@ -599,9 +600,245 @@ async function readSessionEvents(ctx, sessionId) {
|
|
|
599
600
|
return all;
|
|
600
601
|
}
|
|
601
602
|
|
|
603
|
+
/* ------------------------------------------------------------------------- *
|
|
604
|
+
* Session content search
|
|
605
|
+
*
|
|
606
|
+
* Literal search over what was actually said: the user's prompts and the
|
|
607
|
+
* assistant's text blocks. Each session keeps a small cache of that text keyed
|
|
608
|
+
* by the log's size+revision, so a query only decodes sessions that changed;
|
|
609
|
+
* it stops when its time budget is spent and reports how many are still
|
|
610
|
+
* pending, which is what the panel shows as "still indexing".
|
|
611
|
+
* ------------------------------------------------------------------------- */
|
|
612
|
+
|
|
613
|
+
const SEARCH_INDEX_VERSION = 1;
|
|
614
|
+
/** Longer messages are cut for the cache: snippets never need the tail. */
|
|
615
|
+
const SEARCH_MESSAGE_CAP = 200000;
|
|
616
|
+
const SEARCH_PER_SESSION = 3;
|
|
617
|
+
const SEARCH_TOTAL_CAP = 60;
|
|
618
|
+
const SEARCH_BUDGET_MS = 6000;
|
|
619
|
+
const SEARCH_NEW_PER_QUERY = 8;
|
|
620
|
+
|
|
621
|
+
/** Same normalisation on both sides of a cwd comparison. */
|
|
622
|
+
function normalizedPath(value) {
|
|
623
|
+
return typeof value === "string" ? value.replace(/[\\/]+$/, "").toLowerCase() : "";
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
async function searchIndexDirectory() {
|
|
627
|
+
return join(await dshHome(), ".wsr", "search");
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
function searchCacheKey(stat) {
|
|
631
|
+
const size = Number(stat !== null && stat !== undefined ? stat.sizeBytes : 0) || 0;
|
|
632
|
+
const revision = stat !== null && stat !== undefined && stat.revision !== undefined && stat.revision !== null ? String(stat.revision) : "";
|
|
633
|
+
return `${size}:${revision}`;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/** The cached text of one session, or null when it is missing or stale. */
|
|
637
|
+
async function readSearchCache(directory, sessionId) {
|
|
638
|
+
try {
|
|
639
|
+
const raw = await readFile(join(directory, `${sessionId}.json`), "utf8");
|
|
640
|
+
const parsed = JSON.parse(raw);
|
|
641
|
+
if (parsed === null || typeof parsed !== "object") return null;
|
|
642
|
+
if (parsed.v !== SEARCH_INDEX_VERSION || typeof parsed.key !== "string" || !Array.isArray(parsed.messages)) return null;
|
|
643
|
+
return parsed;
|
|
644
|
+
} catch {
|
|
645
|
+
return null;
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
async function writeSearchCache(directory, sessionId, payload) {
|
|
650
|
+
try {
|
|
651
|
+
await mkdir(directory, { recursive: true });
|
|
652
|
+
const target = join(directory, `${sessionId}.json`);
|
|
653
|
+
const temporary = `${target}.${process.pid}.tmp`;
|
|
654
|
+
await writeFile(temporary, JSON.stringify(payload), "utf8");
|
|
655
|
+
await rename(temporary, target);
|
|
656
|
+
} catch {
|
|
657
|
+
/* the cache is best effort: a failed write only costs a re-decode */
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
/**
|
|
662
|
+
* The prose of one message.
|
|
663
|
+
*
|
|
664
|
+
* Deliberately narrower than `blockText`: reasoning and tool-call arguments are
|
|
665
|
+
* not conversation text, and indexing them would make the search hit things the
|
|
666
|
+
* user never read.
|
|
667
|
+
*/
|
|
668
|
+
function conversationText(content) {
|
|
669
|
+
if (typeof content === "string") return content;
|
|
670
|
+
if (!Array.isArray(content)) return "";
|
|
671
|
+
const parts = [];
|
|
672
|
+
for (const block of content) {
|
|
673
|
+
if (block === null || typeof block !== "object") continue;
|
|
674
|
+
if (block.type === "text" && typeof block.text === "string") parts.push(block.text);
|
|
675
|
+
}
|
|
676
|
+
return parts.join("\n");
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
/**
|
|
680
|
+
* One session's searchable transcript.
|
|
681
|
+
*
|
|
682
|
+
* Turns come from `data.turn` when the event carries it and from the running
|
|
683
|
+
* `turn/start` count otherwise, so a hit can name the turn the chat view uses
|
|
684
|
+
* for its own anchors.
|
|
685
|
+
*/
|
|
686
|
+
function extractSearchMessages(events) {
|
|
687
|
+
const messages = [];
|
|
688
|
+
let turn = 0;
|
|
689
|
+
for (const event of events) {
|
|
690
|
+
if (event === null || typeof event !== "object") continue;
|
|
691
|
+
if (event.type === "turn/start") {
|
|
692
|
+
turn += 1;
|
|
693
|
+
continue;
|
|
694
|
+
}
|
|
695
|
+
if (event.type !== "user/message" && event.type !== "assistant/message") continue;
|
|
696
|
+
const data = event.data ?? {};
|
|
697
|
+
const content = data.message !== null && data.message !== undefined ? data.message.content : data.content;
|
|
698
|
+
let text = conversationText(content).trim();
|
|
699
|
+
if (text === "") continue;
|
|
700
|
+
if (text.length > SEARCH_MESSAGE_CAP) text = text.slice(0, SEARCH_MESSAGE_CAP);
|
|
701
|
+
const declared = Number(data.turn);
|
|
702
|
+
messages.push({
|
|
703
|
+
turn: Number.isSafeInteger(declared) ? declared : turn,
|
|
704
|
+
seq: Number.isSafeInteger(Number(event.seq)) ? Number(event.seq) : 0,
|
|
705
|
+
at: Number.isFinite(Number(event.time)) ? Number(event.time) : 0,
|
|
706
|
+
role: event.type === "user/message" ? "user" : "assistant",
|
|
707
|
+
text,
|
|
708
|
+
});
|
|
709
|
+
}
|
|
710
|
+
return messages;
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
function searchSnippet(text, needle) {
|
|
714
|
+
const haystack = text.toLowerCase();
|
|
715
|
+
const index = needle === "" ? -1 : haystack.indexOf(needle);
|
|
716
|
+
if (index < 0) return { snippet: text.slice(0, 160), lead: true, tail: text.length > 160 };
|
|
717
|
+
const start = Math.max(0, index - 60);
|
|
718
|
+
const end = Math.min(text.length, index + needle.length + 120);
|
|
719
|
+
const body = text.slice(start, end).replace(/\s+/g, " ").trim();
|
|
720
|
+
return { snippet: body, lead: start > 0, tail: end < text.length };
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
/**
|
|
724
|
+
* Search every session's transcript.
|
|
725
|
+
*
|
|
726
|
+
* `terms` are the whitespace-separated pieces of the query; all of them have to
|
|
727
|
+
* appear in the same message, so a two-word query narrows instead of widening.
|
|
728
|
+
*/
|
|
729
|
+
export async function searchSessions(ctx, query, options = {}) {
|
|
730
|
+
const started = Date.now();
|
|
731
|
+
const terms = String(query ?? "").toLowerCase().split(/\s+/).filter((term) => term !== "");
|
|
732
|
+
if (terms.length === 0) return { ok: true, query: "", terms: [], hits: [], scanned: 0, total: 0, pending: 0, took: 0 };
|
|
733
|
+
|
|
734
|
+
const directory = await searchIndexDirectory();
|
|
735
|
+
const workspaces = typeof ctx.workspaceRegistry?.list === "function" ? ctx.workspaceRegistry.list() : [];
|
|
736
|
+
const workspaceByPath = new Map();
|
|
737
|
+
for (const workspace of Array.isArray(workspaces) ? workspaces : []) {
|
|
738
|
+
const path = normalizedPath(workspace?.path ?? workspace?.cwd);
|
|
739
|
+
if (path !== "") workspaceByPath.set(path, typeof workspace.name === "string" ? workspace.name : workspace.title || "");
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
let snapshots = [];
|
|
743
|
+
try {
|
|
744
|
+
snapshots = await ctx.sessionPersistence.list();
|
|
745
|
+
} catch {
|
|
746
|
+
snapshots = [];
|
|
747
|
+
}
|
|
748
|
+
const sessions = [];
|
|
749
|
+
for (const snapshot of Array.isArray(snapshots) ? snapshots : []) {
|
|
750
|
+
const header = snapshot?.header;
|
|
751
|
+
if (header === null || header === undefined || typeof header.id !== "string") continue;
|
|
752
|
+
sessions.push({ id: header.id, header, sizeBytes: Number(snapshot.sizeBytes) || 0, updatedAt: Number(header.updatedAt ?? header.createdAt ?? 0) });
|
|
753
|
+
}
|
|
754
|
+
sessions.sort((a, b) => b.updatedAt - a.updatedAt);
|
|
755
|
+
|
|
756
|
+
const refresh = options.refresh === true;
|
|
757
|
+
const hits = [];
|
|
758
|
+
let scanned = 0;
|
|
759
|
+
let pending = 0;
|
|
760
|
+
let decoded = 0;
|
|
761
|
+
|
|
762
|
+
for (const session of sessions) {
|
|
763
|
+
if (hits.length >= SEARCH_TOTAL_CAP) break;
|
|
764
|
+
scanned += 1;
|
|
765
|
+
let stat = null;
|
|
766
|
+
try {
|
|
767
|
+
stat = await ctx.sessionPersistence.stat(session.id);
|
|
768
|
+
} catch {
|
|
769
|
+
stat = null;
|
|
770
|
+
}
|
|
771
|
+
const key = searchCacheKey(stat);
|
|
772
|
+
let cached = refresh ? null : await readSearchCache(directory, session.id);
|
|
773
|
+
if (cached !== null && cached.key !== key) cached = null;
|
|
774
|
+
if (cached === null) {
|
|
775
|
+
if (decoded >= SEARCH_NEW_PER_QUERY || Date.now() - started > SEARCH_BUDGET_MS) {
|
|
776
|
+
pending += 1;
|
|
777
|
+
continue;
|
|
778
|
+
}
|
|
779
|
+
decoded += 1;
|
|
780
|
+
let messages = [];
|
|
781
|
+
try {
|
|
782
|
+
messages = extractSearchMessages(await readSessionEvents(ctx, session.id));
|
|
783
|
+
} catch {
|
|
784
|
+
messages = [];
|
|
785
|
+
}
|
|
786
|
+
await writeSearchCache(directory, session.id, { v: SEARCH_INDEX_VERSION, key, messages });
|
|
787
|
+
cached = { key, messages };
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
let found = 0;
|
|
791
|
+
for (const message of cached.messages) {
|
|
792
|
+
if (message === null || typeof message !== "object" || typeof message.text !== "string") continue;
|
|
793
|
+
const haystack = message.text.toLowerCase();
|
|
794
|
+
if (!terms.every((term) => haystack.indexOf(term) !== -1)) continue;
|
|
795
|
+
const { snippet, lead, tail } = searchSnippet(message.text, terms[0]);
|
|
796
|
+
const title = session.header.title ?? "";
|
|
797
|
+
const cwd = normalizedPath(session.header.cwd);
|
|
798
|
+
hits.push({
|
|
799
|
+
sessionId: session.id,
|
|
800
|
+
title: typeof title === "string" && title.trim() !== "" ? title.trim() : titleFromMessages(cached.messages, session.id),
|
|
801
|
+
workspace: workspaceByPath.get(cwd) ?? "",
|
|
802
|
+
cwd: typeof session.header.cwd === "string" ? session.header.cwd : "",
|
|
803
|
+
turn: message.turn,
|
|
804
|
+
seq: message.seq,
|
|
805
|
+
at: message.at,
|
|
806
|
+
role: message.role,
|
|
807
|
+
snippet,
|
|
808
|
+
lead: lead === true,
|
|
809
|
+
tail: tail === true,
|
|
810
|
+
});
|
|
811
|
+
found += 1;
|
|
812
|
+
if (found >= SEARCH_PER_SESSION || hits.length >= SEARCH_TOTAL_CAP) break;
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
hits.sort((a, b) => b.at - a.at);
|
|
817
|
+
return {
|
|
818
|
+
ok: true,
|
|
819
|
+
query: String(query ?? "").trim(),
|
|
820
|
+
terms,
|
|
821
|
+
hits,
|
|
822
|
+
scanned: sessions.length,
|
|
823
|
+
total: sessions.length,
|
|
824
|
+
pending,
|
|
825
|
+
decoded,
|
|
826
|
+
took: Date.now() - started,
|
|
827
|
+
};
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
/** First prompt line, the same fallback the sidebar uses for an untitled session. */
|
|
831
|
+
function titleFromMessages(messages, sessionId) {
|
|
832
|
+
for (const message of Array.isArray(messages) ? messages : []) {
|
|
833
|
+
if (message?.role !== "user" || typeof message.text !== "string") continue;
|
|
834
|
+
const first = message.text.split("\n")[0].trim();
|
|
835
|
+
if (first !== "") return first.slice(0, 80);
|
|
836
|
+
}
|
|
837
|
+
return sessionId;
|
|
838
|
+
}
|
|
839
|
+
|
|
602
840
|
/** Statistics for one session, cached briefly so re-opening the panel is cheap. */
|
|
603
|
-
export async function sessionStatistics(ctx, sessionId) {
|
|
604
|
-
let stat;
|
|
841
|
+
export async function sessionStatistics(ctx, sessionId) { let stat;
|
|
605
842
|
try {
|
|
606
843
|
stat = await ctx.sessionPersistence.stat(sessionId);
|
|
607
844
|
} catch {
|
|
@@ -1656,6 +1893,26 @@ export async function handleMoveRequest(ctx, req, res) {
|
|
|
1656
1893
|
}
|
|
1657
1894
|
}
|
|
1658
1895
|
|
|
1896
|
+
/** POST { query, refresh? } -> matching snippets across every session's transcript. */
|
|
1897
|
+
export async function handleSearchRequest(ctx, req, res) {
|
|
1898
|
+
if ((req.method ?? "POST").toUpperCase() !== "POST") {
|
|
1899
|
+
sendJson(res, 405, { ok: false, error: "method not allowed" });
|
|
1900
|
+
return;
|
|
1901
|
+
}
|
|
1902
|
+
if (!loopbackOrigin(req)) {
|
|
1903
|
+
sendJson(res, 403, { ok: false, error: "origin not trusted" });
|
|
1904
|
+
return;
|
|
1905
|
+
}
|
|
1906
|
+
try {
|
|
1907
|
+
const body = await readJsonBody(req);
|
|
1908
|
+
const query = body !== null && typeof body.query === "string" ? body.query : "";
|
|
1909
|
+
const result = await searchSessions(ctx, query, { refresh: body !== null && body.refresh === true });
|
|
1910
|
+
sendJson(res, 200, result);
|
|
1911
|
+
} catch (error) {
|
|
1912
|
+
sendJson(res, 500, { ok: false, error: error instanceof Error ? error.message : String(error) });
|
|
1913
|
+
}
|
|
1914
|
+
}
|
|
1915
|
+
|
|
1659
1916
|
/** POST { sessionId } -> token statistics for that session. */
|
|
1660
1917
|
export async function handleStatsRequest(ctx, req, res) {
|
|
1661
1918
|
if ((req.method ?? "POST").toUpperCase() !== "POST") {
|
|
@@ -1701,7 +1958,7 @@ export function apply(ctx) {
|
|
|
1701
1958
|
handler: (req, res) => sendJson(res, 200, {
|
|
1702
1959
|
ok: true,
|
|
1703
1960
|
version: HOST_VERSION,
|
|
1704
|
-
routes: { stats: statsMounted, trash: trashMounted, move: moveMounted },
|
|
1961
|
+
routes: { stats: statsMounted, trash: trashMounted, move: moveMounted, search: searchMounted },
|
|
1705
1962
|
}),
|
|
1706
1963
|
}), "ws-recency: health route");
|
|
1707
1964
|
|
|
@@ -1733,6 +1990,15 @@ export function apply(ctx) {
|
|
|
1733
1990
|
}), "ws-recency: adopt route");
|
|
1734
1991
|
});
|
|
1735
1992
|
|
|
1993
|
+
webCtx.inject(["sessionPersistence", "workspaceRegistry"], (searchCtx) => {
|
|
1994
|
+
searchMounted = true;
|
|
1995
|
+
webCtx.effect(() => server.register({
|
|
1996
|
+
kind: "exact",
|
|
1997
|
+
path: SEARCH_CONTENT_PATH,
|
|
1998
|
+
handler: (req, res) => handleSearchRequest(searchCtx, req, res),
|
|
1999
|
+
}), "ws-recency: search route");
|
|
2000
|
+
});
|
|
2001
|
+
|
|
1736
2002
|
webCtx.inject(["sessionPersistence"], (handoffCtx) => {
|
|
1737
2003
|
webCtx.effect(() => server.register({
|
|
1738
2004
|
kind: "exact",
|
|
@@ -1758,3 +2024,4 @@ export function apply(ctx) {
|
|
|
1758
2024
|
let statsMounted = false;
|
|
1759
2025
|
let trashMounted = false;
|
|
1760
2026
|
let moveMounted = false;
|
|
2027
|
+
let searchMounted = false;
|
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 with an argument/result split and an option to count tool arguments as the agent's writing,
|
|
3
|
+
"version": "0.26.1",
|
|
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",
|
|
7
7
|
"repository": {
|