dsh-planbox-dyb 0.8.1 → 0.8.3
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 -6
- package/client.js +82 -29
- package/index.js +180 -36
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,24 +18,26 @@ dsh plugin --profile web add dsh-planbox-dyb
|
|
|
18
18
|
|
|
19
19
|
1. 模型会在需要重启时主动登记;你也可以点每轮回答下方的「加入计划任务」。
|
|
20
20
|
2. 重启后打开侧栏底部的「计划任务」(右上角红点是数量)。
|
|
21
|
-
3.
|
|
21
|
+
3. 先点「建议」看模型打算怎么继续,再决定「中断恢复」或「忽略」。
|
|
22
22
|
|
|
23
23
|
## 面板
|
|
24
24
|
|
|
25
|
-
-
|
|
25
|
+
- **已登记**:你点「加入计划任务」手动登记的任务。
|
|
26
26
|
- **异常中断**:上一回合报错或崩溃中断的会话。
|
|
27
|
-
-
|
|
27
|
+
- **扫描到**:模型登记或从会话消息里扫到的任务。
|
|
28
|
+
|
|
29
|
+
每行会标注对应的「第 N 轮」对话;同会话的手动登记和自动结果分开显示。
|
|
28
30
|
|
|
29
31
|
按钮:
|
|
30
32
|
|
|
31
33
|
- **建议**:把续跑建议填进输入框,不发送;输入框已有内容会先确认是否从下一行追加。
|
|
32
|
-
-
|
|
34
|
+
- **查看**:跳到对应会话,不关闭面板,并把该任务置顶;会提示打开的是第几轮对话。
|
|
33
35
|
- **忽略**:删除这一条;扫描/异常项会隐藏到该会话下次更新。
|
|
34
36
|
- **全部忽略**:确认后清空当前列表。
|
|
35
|
-
-
|
|
37
|
+
- **中断恢复**:只处理「异常中断」,每项发一句「继续」;发送成功后记录删除,不跟踪后续进度。
|
|
36
38
|
|
|
37
39
|
## 注意
|
|
38
40
|
|
|
39
41
|
- 只支持 DSH Web;headless 没有面板。
|
|
40
42
|
- 不自动续跑、不跟踪任务是否完成,进度需要你自己看会话。
|
|
41
|
-
-
|
|
43
|
+
- 可以和自动续跑插件共存;已被自动续跑的会话会从「异常中断」消失,「中断恢复」投递前也会复核,不会重复发。
|
package/client.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* dsh-planbox-dyb — 浏览器侧。
|
|
3
3
|
*
|
|
4
4
|
* 侧栏底部一个“计划任务”入口:列出已登记的计划任务和扫描到的“等重启后继续”会话。
|
|
5
|
-
* 面板只做审核:建议 / 查看 /
|
|
5
|
+
* 面板只做审核:建议 / 查看 / 忽略;底部的「中断恢复」只对异常中断项投递一句「继续」。
|
|
6
6
|
* 面板浮在侧栏上方,点遮罩或 Esc 关闭。
|
|
7
7
|
*/
|
|
8
8
|
window.__ModuleLoader__.load({
|
|
@@ -15,7 +15,7 @@ window.__ModuleLoader__.load({
|
|
|
15
15
|
const NS = "planbox";
|
|
16
16
|
const API = "/api/planbox";
|
|
17
17
|
const POLL_MS = 60_000;
|
|
18
|
-
const CLIENT_VERSION = "0.8.
|
|
18
|
+
const CLIENT_VERSION = "0.8.3";
|
|
19
19
|
|
|
20
20
|
const FALLBACK = {
|
|
21
21
|
open: "计划任务",
|
|
@@ -32,9 +32,13 @@ window.__ModuleLoader__.load({
|
|
|
32
32
|
pinnedSection: "正在查看",
|
|
33
33
|
scannedSection: "扫描到({n})",
|
|
34
34
|
running: "运行中",
|
|
35
|
+
anchorTurn: "第 {turn} 轮",
|
|
36
|
+
anchorTurnSeq: "第 {turn} 轮 · #{seq}",
|
|
37
|
+
anchorSeq: "消息 #{seq}",
|
|
38
|
+
viewAnchor: "已打开:{label}",
|
|
35
39
|
resume: "继续",
|
|
36
40
|
view: "查看",
|
|
37
|
-
resumeAll: "
|
|
41
|
+
resumeAll: "中断恢复",
|
|
38
42
|
ignoreAll: "全部忽略",
|
|
39
43
|
ignoreAllConfirm: "确定忽略全部 {n} 项?扫描/中断项会写入忽略记录。",
|
|
40
44
|
ignoreAllOk: "确定",
|
|
@@ -84,6 +88,10 @@ window.__ModuleLoader__.load({
|
|
|
84
88
|
pinnedSection: "Viewing",
|
|
85
89
|
scannedSection: "Scanned ({n})",
|
|
86
90
|
running: "Running",
|
|
91
|
+
anchorTurn: "Turn {turn}",
|
|
92
|
+
anchorTurnSeq: "Turn {turn} · #{seq}",
|
|
93
|
+
anchorSeq: "Message #{seq}",
|
|
94
|
+
viewAnchor: "Opened: {label}",
|
|
87
95
|
resume: "Resume",
|
|
88
96
|
view: "View",
|
|
89
97
|
resumeAll: "Restore interrupted",
|
|
@@ -255,6 +263,7 @@ window.__ModuleLoader__.load({
|
|
|
255
263
|
".dpb-next{font-size:11px;line-height:16px;color:#9dbcff;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}",
|
|
256
264
|
".dpb-snippet{font-size:11.5px;line-height:17px;color:var(--dsw-alias-label-secondary,#b6bac0);display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden;overflow-wrap:anywhere}",
|
|
257
265
|
".dpb-meta{display:flex;align-items:center;gap:5px;font-size:10.5px;line-height:15px;color:var(--dsw-alias-label-tertiary,#8b8f95);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}",
|
|
266
|
+
".dpb-anchor{flex:none;color:var(--dsw-alias-state-business-primary,#4c8dff)}",
|
|
258
267
|
".dpb-dot{opacity:.6}",
|
|
259
268
|
".dpb-row-actions{flex:none;display:flex;align-items:center;gap:2px}",
|
|
260
269
|
".dpb-mini{padding:0 7px;height:24px;border:0;border-radius:6px;background:transparent;color:var(--dsw-alias-label-secondary,#9aa0a6);font:inherit;font-size:11px;cursor:pointer}",
|
|
@@ -360,14 +369,31 @@ window.__ModuleLoader__.load({
|
|
|
360
369
|
};
|
|
361
370
|
}
|
|
362
371
|
|
|
372
|
+
/** 任务对应的最近一次对话:第 N 轮 / 消息 #seq。 */
|
|
373
|
+
function anchorText(t, item) {
|
|
374
|
+
const anchor = item?.anchor;
|
|
375
|
+
if (anchor === null || anchor === undefined) return "";
|
|
376
|
+
if (anchor.turn > 0 && anchor.seq > 0) {
|
|
377
|
+
return t("anchorTurnSeq", { turn: anchor.turn, seq: anchor.seq });
|
|
378
|
+
}
|
|
379
|
+
if (anchor.turn > 0) return t("anchorTurn", { turn: anchor.turn });
|
|
380
|
+
if (anchor.seq > 0) return t("anchorSeq", { seq: anchor.seq });
|
|
381
|
+
return "";
|
|
382
|
+
}
|
|
383
|
+
|
|
363
384
|
function Row(props) {
|
|
364
385
|
const { item, t, busy, pinned, onOpen, onDismiss, onSuggest } = props;
|
|
365
386
|
const queue = item.source === "queue";
|
|
366
387
|
const tag = tagFor(item);
|
|
367
388
|
const detail = queue ? item.snippet || item.prompt : item.snippet;
|
|
368
389
|
const full = queue ? item.prompt : item.snippet;
|
|
390
|
+
const anchor = anchorText(t, item);
|
|
391
|
+
const anchorTitle =
|
|
392
|
+
anchor === "" || !item.anchor?.at
|
|
393
|
+
? anchor
|
|
394
|
+
: `${anchor} · ${new Date(item.anchor.at).toLocaleString()}`;
|
|
369
395
|
// 整行可点:和「查看」同一条路径(跳转 + 置顶),行内按钮自己 stopPropagation。
|
|
370
|
-
const open = () => onOpen(item.sessionId, item.id);
|
|
396
|
+
const open = () => onOpen(item.sessionId, item.id, item.anchor);
|
|
371
397
|
const next = nextStep(item);
|
|
372
398
|
return h(
|
|
373
399
|
"div",
|
|
@@ -400,6 +426,9 @@ window.__ModuleLoader__.load({
|
|
|
400
426
|
h(
|
|
401
427
|
"div",
|
|
402
428
|
{ className: "dpb-meta" },
|
|
429
|
+
anchor !== ""
|
|
430
|
+
? h("span", { className: "dpb-anchor", title: anchorTitle }, anchor)
|
|
431
|
+
: null,
|
|
403
432
|
h("span", null, ago(t, item.updatedAt)),
|
|
404
433
|
item.running ? h("span", { className: "dpb-dot" }, "·") : null,
|
|
405
434
|
item.running ? h("span", null, t("running")) : null,
|
|
@@ -432,7 +461,7 @@ window.__ModuleLoader__.load({
|
|
|
432
461
|
"aria-label": t("view"),
|
|
433
462
|
onClick: (event) => {
|
|
434
463
|
event.stopPropagation();
|
|
435
|
-
onOpen(item.sessionId, item.id);
|
|
464
|
+
onOpen(item.sessionId, item.id, item.anchor);
|
|
436
465
|
},
|
|
437
466
|
},
|
|
438
467
|
t("view"),
|
|
@@ -456,7 +485,7 @@ window.__ModuleLoader__.load({
|
|
|
456
485
|
);
|
|
457
486
|
}
|
|
458
487
|
|
|
459
|
-
/**
|
|
488
|
+
/** 「中断恢复」只处理异常中断项。 */
|
|
460
489
|
function restorableItems(items) {
|
|
461
490
|
return items.filter((item) => item.source === "abnormal");
|
|
462
491
|
}
|
|
@@ -481,7 +510,7 @@ window.__ModuleLoader__.load({
|
|
|
481
510
|
function Panel(props) {
|
|
482
511
|
const { t, items, loading, busy, busyAction, error, result, pinnedId, onClose, onRefresh, onResume, onOpen, onDismiss, onSuggest, onDismissAll } = props;
|
|
483
512
|
const [confirmDismiss, setConfirmDismiss] = React.useState(false);
|
|
484
|
-
//
|
|
513
|
+
// 「中断恢复」只处理异常中断项,其余任务由用户自己审核。
|
|
485
514
|
const interrupted = restorableItems(items);
|
|
486
515
|
|
|
487
516
|
React.useEffect(() => {
|
|
@@ -651,7 +680,7 @@ window.__ModuleLoader__.load({
|
|
|
651
680
|
{
|
|
652
681
|
type: "button",
|
|
653
682
|
className: "dpb-secondary",
|
|
654
|
-
disabled: busy ||
|
|
683
|
+
disabled: busy || items.length === 0,
|
|
655
684
|
onClick: () => setConfirmDismiss(true),
|
|
656
685
|
},
|
|
657
686
|
t("ignoreAll"),
|
|
@@ -661,7 +690,7 @@ window.__ModuleLoader__.load({
|
|
|
661
690
|
{
|
|
662
691
|
type: "button",
|
|
663
692
|
className: "dpb-primary",
|
|
664
|
-
disabled: busy ||
|
|
693
|
+
disabled: busy || interrupted.length === 0,
|
|
665
694
|
title: interrupted.length === 0 ? t("noInterrupted") : undefined,
|
|
666
695
|
onClick: () => onResume(interrupted.map((item) => item.id)),
|
|
667
696
|
},
|
|
@@ -688,6 +717,8 @@ window.__ModuleLoader__.load({
|
|
|
688
717
|
// 「建议」:pendingFill 非空时弹确认框;fillNotice 是短暂的操作反馈。
|
|
689
718
|
const [pendingFill, setPendingFill] = React.useState(null);
|
|
690
719
|
const [fillNotice, setFillNotice] = React.useState(null);
|
|
720
|
+
const loadSeq = React.useRef(0);
|
|
721
|
+
const busyRef = React.useRef(false);
|
|
691
722
|
|
|
692
723
|
React.useEffect(() => {
|
|
693
724
|
panelState.open = open;
|
|
@@ -708,14 +739,19 @@ window.__ModuleLoader__.load({
|
|
|
708
739
|
|
|
709
740
|
const load = React.useCallback(
|
|
710
741
|
async (refresh) => {
|
|
711
|
-
if (muted.current) return;
|
|
742
|
+
if (muted.current || busyRef.current) return;
|
|
743
|
+
const seq = loadSeq.current + 1;
|
|
744
|
+
loadSeq.current = seq;
|
|
712
745
|
setLoading(true);
|
|
713
746
|
try {
|
|
714
747
|
const data = await request(`${API}/pending${refresh ? "?refresh=1" : ""}`);
|
|
748
|
+
if (seq !== loadSeq.current || muted.current) return;
|
|
715
749
|
setItems(Array.isArray(data.items) ? data.items : []);
|
|
716
750
|
setError(data.scanError ? t("scanFailed", { message: data.scanError }) : null);
|
|
717
751
|
} catch (cause) {
|
|
718
|
-
|
|
752
|
+
if (seq === loadSeq.current && !muted.current) {
|
|
753
|
+
setError(t("loadFailed", { message: String(cause?.message ?? cause) }));
|
|
754
|
+
}
|
|
719
755
|
} finally {
|
|
720
756
|
setLoading(false);
|
|
721
757
|
}
|
|
@@ -751,6 +787,8 @@ window.__ModuleLoader__.load({
|
|
|
751
787
|
if (busy) return;
|
|
752
788
|
const wanted = Array.isArray(ids) ? ids.filter((id) => typeof id === "string") : [];
|
|
753
789
|
if (wanted.length === 0) return;
|
|
790
|
+
busyRef.current = true;
|
|
791
|
+
loadSeq.current += 1;
|
|
754
792
|
setBusyAction("resume");
|
|
755
793
|
setResult(null);
|
|
756
794
|
try {
|
|
@@ -782,6 +820,7 @@ window.__ModuleLoader__.load({
|
|
|
782
820
|
} catch (cause) {
|
|
783
821
|
setError(t("loadFailed", { message: String(cause?.message ?? cause) }));
|
|
784
822
|
} finally {
|
|
823
|
+
busyRef.current = false;
|
|
785
824
|
setBusyAction(null);
|
|
786
825
|
}
|
|
787
826
|
},
|
|
@@ -793,6 +832,8 @@ window.__ModuleLoader__.load({
|
|
|
793
832
|
async (id) => {
|
|
794
833
|
if (busy) return;
|
|
795
834
|
const snapshot = items;
|
|
835
|
+
busyRef.current = true;
|
|
836
|
+
loadSeq.current += 1;
|
|
796
837
|
setBusyAction("dismiss");
|
|
797
838
|
setResult(null);
|
|
798
839
|
setItems((previous) => previous.filter((item) => item.id !== id));
|
|
@@ -807,6 +848,7 @@ window.__ModuleLoader__.load({
|
|
|
807
848
|
setItems(snapshot);
|
|
808
849
|
setError(t("loadFailed", { message: String(cause?.message ?? cause) }));
|
|
809
850
|
} finally {
|
|
851
|
+
busyRef.current = false;
|
|
810
852
|
setBusyAction(null);
|
|
811
853
|
}
|
|
812
854
|
},
|
|
@@ -820,6 +862,8 @@ window.__ModuleLoader__.load({
|
|
|
820
862
|
const wanted = Array.isArray(ids) ? ids : [];
|
|
821
863
|
if (wanted.length === 0) return;
|
|
822
864
|
const snapshot = items;
|
|
865
|
+
busyRef.current = true;
|
|
866
|
+
loadSeq.current += 1;
|
|
823
867
|
setBusyAction("dismiss");
|
|
824
868
|
setResult(null);
|
|
825
869
|
setItems([]);
|
|
@@ -834,33 +878,41 @@ window.__ModuleLoader__.load({
|
|
|
834
878
|
setItems(snapshot);
|
|
835
879
|
setError(t("loadFailed", { message: String(cause?.message ?? cause) }));
|
|
836
880
|
} finally {
|
|
881
|
+
busyRef.current = false;
|
|
837
882
|
setBusyAction(null);
|
|
838
883
|
}
|
|
839
884
|
},
|
|
840
885
|
[busy, items, t],
|
|
841
886
|
);
|
|
842
887
|
|
|
843
|
-
const openSession = React.useCallback(
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
if (workspace !== null && typeof workspace.openSession === "function") {
|
|
888
|
+
const openSession = React.useCallback(
|
|
889
|
+
(sessionId, itemId, anchor) => {
|
|
890
|
+
// 会话切换可能让组件重挂载,先把“面板开着 + 看的是哪条”同步写进模块状态。
|
|
891
|
+
panelState.open = true;
|
|
892
|
+
panelState.pinnedId = itemId ?? null;
|
|
893
|
+
setPinnedId(itemId ?? null);
|
|
894
|
+
const label = anchorText(t, { anchor });
|
|
895
|
+
if (label !== "") {
|
|
896
|
+
setFillNotice({ bad: false, text: t("viewAnchor", { label }) });
|
|
897
|
+
}
|
|
898
|
+
let workspace = null;
|
|
855
899
|
try {
|
|
856
|
-
workspace
|
|
857
|
-
return;
|
|
900
|
+
workspace = pluginCtx?.get?.("uiWorkspace") ?? null;
|
|
858
901
|
} catch (cause) {
|
|
859
|
-
|
|
902
|
+
workspace = null;
|
|
860
903
|
}
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
904
|
+
if (workspace !== null && typeof workspace.openSession === "function") {
|
|
905
|
+
try {
|
|
906
|
+
workspace.openSession(sessionId);
|
|
907
|
+
return;
|
|
908
|
+
} catch (cause) {
|
|
909
|
+
/* 回退到 URL 跳转 */
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
window.location.assign(`/?session=${encodeURIComponent(sessionId)}`);
|
|
913
|
+
},
|
|
914
|
+
[t],
|
|
915
|
+
);
|
|
864
916
|
|
|
865
917
|
const copySuggestion = React.useCallback(
|
|
866
918
|
async (text) => {
|
|
@@ -1187,6 +1239,7 @@ window.__ModuleLoader__.load({
|
|
|
1187
1239
|
orderPending,
|
|
1188
1240
|
tagFor,
|
|
1189
1241
|
restorableItems,
|
|
1242
|
+
anchorText,
|
|
1190
1243
|
nextStep,
|
|
1191
1244
|
},
|
|
1192
1245
|
};
|
package/index.js
CHANGED
|
@@ -193,6 +193,68 @@ export function tailTurnState(records) {
|
|
|
193
193
|
return result;
|
|
194
194
|
}
|
|
195
195
|
|
|
196
|
+
/**
|
|
197
|
+
* 取任务对应的“最近一次对话”锚点:优先最后一条真实用户消息,退化为最近回合。
|
|
198
|
+
* 用于面板显示和「查看」时说明是哪一轮对话。
|
|
199
|
+
* @param {readonly unknown[]} records - sessionController.events() 返回的记录。
|
|
200
|
+
* @returns {{ turn: number, seq: number, at: number, messageId: string, kind: string } | null} 锚点。
|
|
201
|
+
*/
|
|
202
|
+
export function anchorOf(records) {
|
|
203
|
+
if (!Array.isArray(records)) return null;
|
|
204
|
+
let user = null;
|
|
205
|
+
let turn = null;
|
|
206
|
+
for (let index = records.length - 1; index >= 0; index -= 1) {
|
|
207
|
+
const record = records[index];
|
|
208
|
+
const event = record && typeof record === 'object' && record.type === 'event' ? record.event : null;
|
|
209
|
+
if (!event) continue;
|
|
210
|
+
const seq = Number(event.seq ?? record.seq ?? 0) || 0;
|
|
211
|
+
const at = Number(event.time ?? record.time ?? 0) || 0;
|
|
212
|
+
if (
|
|
213
|
+
user === null &&
|
|
214
|
+
event.type === 'user/message' &&
|
|
215
|
+
!isInjectedSource(event.data?.source)
|
|
216
|
+
) {
|
|
217
|
+
user = {
|
|
218
|
+
seq,
|
|
219
|
+
at,
|
|
220
|
+
messageId: typeof event.data?.id === 'string' ? event.data.id : '',
|
|
221
|
+
kind: 'user',
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
if (
|
|
225
|
+
turn === null &&
|
|
226
|
+
(event.type === 'turn/start' || event.type === 'turn/end')
|
|
227
|
+
) {
|
|
228
|
+
turn = {
|
|
229
|
+
turn: Number(event.data?.turn ?? 0) || 0,
|
|
230
|
+
seq,
|
|
231
|
+
at,
|
|
232
|
+
kind: 'turn',
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
if (user !== null && turn !== null) break;
|
|
236
|
+
}
|
|
237
|
+
const chosen = user ?? turn;
|
|
238
|
+
if (chosen === null) return null;
|
|
239
|
+
return {
|
|
240
|
+
turn: turn?.turn ?? 0,
|
|
241
|
+
seq: chosen.seq,
|
|
242
|
+
at: chosen.at,
|
|
243
|
+
messageId: user?.messageId ?? '',
|
|
244
|
+
kind: chosen.kind,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function normalizeAnchor(value) {
|
|
249
|
+
if (value === null || typeof value !== 'object') return null;
|
|
250
|
+
const turn = Number(value.turn ?? 0) || 0;
|
|
251
|
+
const seq = Number(value.seq ?? 0) || 0;
|
|
252
|
+
const at = Number(value.at ?? 0) || 0;
|
|
253
|
+
const messageId = typeof value.messageId === 'string' ? value.messageId : '';
|
|
254
|
+
if (turn === 0 && seq === 0 && at === 0) return null;
|
|
255
|
+
return { turn, seq, at, messageId, kind: value.kind === 'turn' ? 'turn' : 'user' };
|
|
256
|
+
}
|
|
257
|
+
|
|
196
258
|
/** 是否是异常中断(错误、崩溃补记,或未正常收尾的回合)。 */
|
|
197
259
|
function isAbnormalTurn(turn) {
|
|
198
260
|
return turn.open || turn.kind === 'error' || turn.kind === 'interrupted';
|
|
@@ -268,6 +330,8 @@ export function createStore(options = {}) {
|
|
|
268
330
|
queueFile,
|
|
269
331
|
dismissedFile,
|
|
270
332
|
scanCache: null,
|
|
333
|
+
scanEpoch: 0,
|
|
334
|
+
scanRefresh: null,
|
|
271
335
|
|
|
272
336
|
/** 当前登记队列。 */
|
|
273
337
|
queue() {
|
|
@@ -276,9 +340,12 @@ export function createStore(options = {}) {
|
|
|
276
340
|
);
|
|
277
341
|
},
|
|
278
342
|
|
|
279
|
-
/**
|
|
343
|
+
/** 新增(同一会话、同一来源类别只保留最新一条;手动与自动分开)。 */
|
|
280
344
|
add(entry) {
|
|
281
|
-
const
|
|
345
|
+
const automatic = entry.origin === 'model';
|
|
346
|
+
const items = store.queue().filter(
|
|
347
|
+
(item) => !(item.sessionId === entry.sessionId && (item.origin === 'model') === automatic),
|
|
348
|
+
);
|
|
282
349
|
items.unshift(entry);
|
|
283
350
|
writeJsonAtomic(queueFile, { version: 1, items });
|
|
284
351
|
return entry;
|
|
@@ -402,6 +469,7 @@ function queueItems(store) {
|
|
|
402
469
|
origin: entry.origin === 'model' ? 'model' : 'manual',
|
|
403
470
|
note: typeof entry.note === 'string' ? entry.note : '',
|
|
404
471
|
snapshot: entry.snapshot && typeof entry.snapshot === 'object' ? entry.snapshot : null,
|
|
472
|
+
anchor: normalizeAnchor(entry.anchor),
|
|
405
473
|
updatedAt: Number(entry.createdAt ?? Date.now()),
|
|
406
474
|
};
|
|
407
475
|
})
|
|
@@ -433,12 +501,14 @@ async function queueItemsWithDetails(ctx, store, options = {}) {
|
|
|
433
501
|
const detailed = [];
|
|
434
502
|
for (const item of base) {
|
|
435
503
|
let assistantText = '';
|
|
504
|
+
let records = null;
|
|
436
505
|
try {
|
|
437
506
|
const page = await ctx.sessionController.events(
|
|
438
507
|
{ address: { kind: 'session', sessionId: item.sessionId }, maxMessages: 30 },
|
|
439
508
|
controller,
|
|
440
509
|
);
|
|
441
|
-
|
|
510
|
+
records = page?.records;
|
|
511
|
+
assistantText = tailMessages(records).lastAssistantText;
|
|
442
512
|
} catch {
|
|
443
513
|
/* 读不到就用续跑说明兜底 */
|
|
444
514
|
}
|
|
@@ -448,6 +518,7 @@ async function queueItemsWithDetails(ctx, store, options = {}) {
|
|
|
448
518
|
title: summary ? summaryTitle(summary) : item.title,
|
|
449
519
|
snippet: tailOf(assistantText !== '' ? assistantText : item.prompt),
|
|
450
520
|
detailSource: assistantText !== '' ? 'assistant' : 'prompt',
|
|
521
|
+
anchor: item.anchor ?? normalizeAnchor(anchorOf(records)),
|
|
451
522
|
});
|
|
452
523
|
}
|
|
453
524
|
return detailed;
|
|
@@ -494,6 +565,7 @@ function detectCandidate(summary, tail, turn, options) {
|
|
|
494
565
|
title: summaryTitle(summary),
|
|
495
566
|
snippet: tailOf(detail),
|
|
496
567
|
prompt: '',
|
|
568
|
+
anchor: normalizeAnchor(options.anchor),
|
|
497
569
|
updatedAt: Number(summary.updatedAt ?? options.now),
|
|
498
570
|
running: false,
|
|
499
571
|
cwd: typeof summary.cwd === 'string' ? summary.cwd : undefined,
|
|
@@ -510,6 +582,7 @@ function detectCandidate(summary, tail, turn, options) {
|
|
|
510
582
|
title: summaryTitle(summary),
|
|
511
583
|
snippet: snippetAround(realUserText, MARKER_PATTERNS),
|
|
512
584
|
prompt: '',
|
|
585
|
+
anchor: normalizeAnchor(options.anchor),
|
|
513
586
|
updatedAt: Number(summary.updatedAt ?? options.now),
|
|
514
587
|
running: summary.running === true,
|
|
515
588
|
cwd: typeof summary.cwd === 'string' ? summary.cwd : undefined,
|
|
@@ -526,10 +599,26 @@ function detectCandidate(summary, tail, turn, options) {
|
|
|
526
599
|
*/
|
|
527
600
|
export async function scanCandidates(ctx, store, options = {}) {
|
|
528
601
|
const now = typeof options.now === 'number' ? options.now : Date.now();
|
|
529
|
-
if (!options.refresh && store.scanCache
|
|
602
|
+
if (!options.refresh && store.scanCache) {
|
|
603
|
+
if (now - store.scanCache.at < SCAN_TTL_MS) {
|
|
604
|
+
return store.scanCache.value;
|
|
605
|
+
}
|
|
606
|
+
// 过期缓存先返回,后台更新;面板打开不再等整轮扫描。
|
|
607
|
+
if (store.scanRefresh === null) {
|
|
608
|
+
store.scanRefresh = scanCandidates(ctx, store, {
|
|
609
|
+
refresh: true,
|
|
610
|
+
background: true,
|
|
611
|
+
now: Date.now(),
|
|
612
|
+
})
|
|
613
|
+
.catch(() => null)
|
|
614
|
+
.finally(() => {
|
|
615
|
+
store.scanRefresh = null;
|
|
616
|
+
});
|
|
617
|
+
}
|
|
530
618
|
return store.scanCache.value;
|
|
531
619
|
}
|
|
532
620
|
|
|
621
|
+
const epoch = store.scanEpoch ?? 0;
|
|
533
622
|
const controller = withTimeout(options.signal, 20_000);
|
|
534
623
|
const items = [];
|
|
535
624
|
let scanError;
|
|
@@ -560,13 +649,15 @@ export async function scanCandidates(ctx, store, options = {}) {
|
|
|
560
649
|
{ address: { kind: 'session', sessionId: summary.sessionId }, maxMessages: 80 },
|
|
561
650
|
controller,
|
|
562
651
|
);
|
|
652
|
+
const records = page?.records;
|
|
563
653
|
settled[index] = detectCandidate(
|
|
564
654
|
summary,
|
|
565
|
-
tailMessages(
|
|
566
|
-
tailTurnState(
|
|
655
|
+
tailMessages(records),
|
|
656
|
+
tailTurnState(records),
|
|
567
657
|
{
|
|
568
658
|
dismissed,
|
|
569
659
|
now,
|
|
660
|
+
anchor: anchorOf(records),
|
|
570
661
|
abnormalWindowMs: options.abnormalWindowMs ?? abnormalWindowMs(),
|
|
571
662
|
},
|
|
572
663
|
);
|
|
@@ -584,8 +675,19 @@ export async function scanCandidates(ctx, store, options = {}) {
|
|
|
584
675
|
scanError = String(error?.message ?? error);
|
|
585
676
|
}
|
|
586
677
|
|
|
587
|
-
const
|
|
588
|
-
|
|
678
|
+
const dismissedNow = store.dismissed();
|
|
679
|
+
const liveItems = items.filter((item) => {
|
|
680
|
+
const ignoredAt = Number(dismissedNow[item.sessionId] ?? 0);
|
|
681
|
+
return !(ignoredAt > 0 && Number(item.updatedAt ?? 0) <= ignoredAt);
|
|
682
|
+
});
|
|
683
|
+
const value = {
|
|
684
|
+
items: liveItems,
|
|
685
|
+
scannedAt: now,
|
|
686
|
+
...(scanError === undefined ? {} : { scanError }),
|
|
687
|
+
};
|
|
688
|
+
if ((store.scanEpoch ?? 0) === epoch) {
|
|
689
|
+
store.scanCache = { at: now, value };
|
|
690
|
+
}
|
|
589
691
|
return value;
|
|
590
692
|
}
|
|
591
693
|
|
|
@@ -597,10 +699,14 @@ export async function scanCandidates(ctx, store, options = {}) {
|
|
|
597
699
|
* @returns {Promise<{ items: object[], queueCount: number, scanCount: number, scannedAt: number, scanError?: string }>} 汇总。
|
|
598
700
|
*/
|
|
599
701
|
export async function collectPending(ctx, store, options = {}) {
|
|
600
|
-
const queue = await
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
702
|
+
const [queue, scanned] = await Promise.all([
|
|
703
|
+
queueItemsWithDetails(ctx, store, options),
|
|
704
|
+
scanCandidates(ctx, store, options),
|
|
705
|
+
]);
|
|
706
|
+
// 只有自动登记项会吞掉同会话的扫描结果;手动登记与自动结果要分开显示。
|
|
707
|
+
const known = new Set(
|
|
708
|
+
queue.filter((item) => item.origin === 'model').map((item) => item.sessionId),
|
|
709
|
+
);
|
|
604
710
|
const extra = scanned.items
|
|
605
711
|
.filter((item) => !known.has(item.sessionId))
|
|
606
712
|
.sort((left, right) => right.updatedAt - left.updatedAt);
|
|
@@ -665,9 +771,11 @@ async function itemsForIds(ctx, store, ids, options = {}) {
|
|
|
665
771
|
{ address: { kind: 'session', sessionId: summary.sessionId }, maxMessages: 80 },
|
|
666
772
|
controller,
|
|
667
773
|
);
|
|
668
|
-
const
|
|
774
|
+
const records = page?.records;
|
|
775
|
+
const item = detectCandidate(summary, tailMessages(records), tailTurnState(records), {
|
|
669
776
|
dismissed,
|
|
670
777
|
now: Date.now(),
|
|
778
|
+
anchor: anchorOf(records),
|
|
671
779
|
});
|
|
672
780
|
if (item !== null) selected.push(item);
|
|
673
781
|
} catch {
|
|
@@ -679,6 +787,8 @@ async function itemsForIds(ctx, store, ids, options = {}) {
|
|
|
679
787
|
|
|
680
788
|
/** 已处理的项从扫描缓存里移除,避免下一次 GET 又看到它。 */
|
|
681
789
|
function forgetFromCache(store, doneIds, doneSessions) {
|
|
790
|
+
// 让正在进行的扫描在写入缓存前发现“已忽略/已恢复”,避免旧结果把数字又刷回来。
|
|
791
|
+
store.scanEpoch = (store.scanEpoch ?? 0) + 1;
|
|
682
792
|
const cache = store.scanCache;
|
|
683
793
|
if (!cache) return;
|
|
684
794
|
cache.value = {
|
|
@@ -876,7 +986,7 @@ export async function resumeSelected(ctx, store, options = {}) {
|
|
|
876
986
|
* 登记一条待续任务。
|
|
877
987
|
* @param {object} ctx - 宿主上下文。
|
|
878
988
|
* @param {ReturnType<typeof createStore>} store - 持久存储。
|
|
879
|
-
* @param {{ sessionId: string, title: string, prompt?: string, note?: string }} input - 登记内容。
|
|
989
|
+
* @param {{ sessionId: string, title: string, prompt?: string, note?: string, anchor?: object }} input - 登记内容。
|
|
880
990
|
* @returns {object} 新登记项。
|
|
881
991
|
*/
|
|
882
992
|
export function deferTask(store, input) {
|
|
@@ -892,6 +1002,7 @@ export function deferTask(store, input) {
|
|
|
892
1002
|
note: String(input.note ?? '').trim(),
|
|
893
1003
|
origin: input.origin === 'model' ? 'model' : 'manual',
|
|
894
1004
|
snapshot: normalizeSnapshot(input),
|
|
1005
|
+
anchor: normalizeAnchor(input.anchor),
|
|
895
1006
|
createdAt: Date.now(),
|
|
896
1007
|
};
|
|
897
1008
|
return store.add(entry);
|
|
@@ -913,30 +1024,30 @@ export async function deferFromSession(ctx, store, sessionId, overrides = {}, op
|
|
|
913
1024
|
|
|
914
1025
|
let title = String(overrides.title ?? '').trim();
|
|
915
1026
|
let prompt = String(overrides.prompt ?? '').trim();
|
|
1027
|
+
let anchor = null;
|
|
916
1028
|
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
/* 标题拿不到就用兜底 */
|
|
926
|
-
}
|
|
1029
|
+
const controller = withTimeout(options.signal, 8_000);
|
|
1030
|
+
if (title === '') {
|
|
1031
|
+
try {
|
|
1032
|
+
const listed = await ctx.sessionController.list({}, controller);
|
|
1033
|
+
const summary = (listed?.items ?? []).find((item) => item?.sessionId === target);
|
|
1034
|
+
if (summary) title = summaryTitle(summary);
|
|
1035
|
+
} catch {
|
|
1036
|
+
/* 标题拿不到就用兜底 */
|
|
927
1037
|
}
|
|
1038
|
+
}
|
|
1039
|
+
try {
|
|
1040
|
+
const page = await ctx.sessionController.events(
|
|
1041
|
+
{ address: { kind: 'session', sessionId: target }, maxMessages: 30 },
|
|
1042
|
+
controller,
|
|
1043
|
+
);
|
|
1044
|
+
anchor = anchorOf(page?.records);
|
|
928
1045
|
if (prompt === '') {
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
{ address: { kind: 'session', sessionId: target }, maxMessages: 20 },
|
|
932
|
-
controller,
|
|
933
|
-
);
|
|
934
|
-
const lastUser = tailMessages(page?.records).lastRealUserText;
|
|
935
|
-
if (lastUser !== '') prompt = lastUser.slice(0, 200);
|
|
936
|
-
} catch {
|
|
937
|
-
/* 拿不到就用兜底 */
|
|
938
|
-
}
|
|
1046
|
+
const lastUser = tailMessages(page?.records).lastRealUserText;
|
|
1047
|
+
if (lastUser !== '') prompt = lastUser.slice(0, 200);
|
|
939
1048
|
}
|
|
1049
|
+
} catch {
|
|
1050
|
+
/* 读不到就用兜底 */
|
|
940
1051
|
}
|
|
941
1052
|
|
|
942
1053
|
return deferTask(store, {
|
|
@@ -945,6 +1056,7 @@ export async function deferFromSession(ctx, store, sessionId, overrides = {}, op
|
|
|
945
1056
|
prompt: prompt === '' ? '继续完成此前未完成的任务。' : prompt,
|
|
946
1057
|
note: overrides.note,
|
|
947
1058
|
origin: overrides.origin,
|
|
1059
|
+
anchor,
|
|
948
1060
|
});
|
|
949
1061
|
}
|
|
950
1062
|
|
|
@@ -1157,8 +1269,21 @@ export function createToolDefinitions(ctx, store) {
|
|
|
1157
1269
|
},
|
|
1158
1270
|
},
|
|
1159
1271
|
async execute(args, exec) {
|
|
1272
|
+
const sessionId = sessionIdOf(exec);
|
|
1273
|
+
let anchor = null;
|
|
1274
|
+
if (sessionId !== '') {
|
|
1275
|
+
try {
|
|
1276
|
+
const page = await ctx.sessionController.events(
|
|
1277
|
+
{ address: { kind: 'session', sessionId }, maxMessages: 30 },
|
|
1278
|
+
withTimeout(undefined, 8_000),
|
|
1279
|
+
);
|
|
1280
|
+
anchor = anchorOf(page?.records);
|
|
1281
|
+
} catch {
|
|
1282
|
+
/* 读不到就不带锚点 */
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1160
1285
|
const entry = deferTask(store, {
|
|
1161
|
-
sessionId
|
|
1286
|
+
sessionId,
|
|
1162
1287
|
title: args?.title,
|
|
1163
1288
|
prompt: args?.prompt,
|
|
1164
1289
|
nextSteps: args?.nextSteps,
|
|
@@ -1167,6 +1292,7 @@ export function createToolDefinitions(ctx, store) {
|
|
|
1167
1292
|
lastFailedCommand: args?.lastFailedCommand,
|
|
1168
1293
|
note: args?.note,
|
|
1169
1294
|
origin: 'model',
|
|
1295
|
+
anchor,
|
|
1170
1296
|
});
|
|
1171
1297
|
return { ok: true, id: entry.id, title: entry.title, pending: store.queue().length };
|
|
1172
1298
|
},
|
|
@@ -1193,6 +1319,17 @@ export function createToolDefinitions(ctx, store) {
|
|
|
1193
1319
|
origin: { type: 'string' },
|
|
1194
1320
|
sessionId: { type: 'string' },
|
|
1195
1321
|
title: { type: 'string' },
|
|
1322
|
+
anchor: {
|
|
1323
|
+
type: 'object',
|
|
1324
|
+
additionalProperties: false,
|
|
1325
|
+
properties: {
|
|
1326
|
+
turn: { type: 'integer' },
|
|
1327
|
+
seq: { type: 'integer' },
|
|
1328
|
+
at: { type: 'integer' },
|
|
1329
|
+
messageId: { type: 'string' },
|
|
1330
|
+
kind: { type: 'string' },
|
|
1331
|
+
},
|
|
1332
|
+
},
|
|
1196
1333
|
snapshot: {
|
|
1197
1334
|
type: 'object',
|
|
1198
1335
|
additionalProperties: false,
|
|
@@ -1224,7 +1361,13 @@ export function createToolDefinitions(ctx, store) {
|
|
|
1224
1361
|
? '已登记'
|
|
1225
1362
|
: '扫描到';
|
|
1226
1363
|
const detail = item.snippet ? `\n ${item.snippet}` : '';
|
|
1227
|
-
|
|
1364
|
+
const anchor =
|
|
1365
|
+
item.anchor?.turn > 0
|
|
1366
|
+
? ` · 第 ${item.anchor.turn} 轮`
|
|
1367
|
+
: item.anchor?.seq > 0
|
|
1368
|
+
? ` · 消息 #${item.anchor.seq}`
|
|
1369
|
+
: '';
|
|
1370
|
+
return `- [${tag}] ${item.title}(${item.sessionId})${anchor}${detail}`;
|
|
1228
1371
|
};
|
|
1229
1372
|
const registered = value.items.filter(
|
|
1230
1373
|
(item) => item.source === 'queue' && item.origin !== 'model',
|
|
@@ -1257,6 +1400,7 @@ export function createToolDefinitions(ctx, store) {
|
|
|
1257
1400
|
origin: item.origin ?? 'manual',
|
|
1258
1401
|
sessionId: item.sessionId,
|
|
1259
1402
|
title: item.title,
|
|
1403
|
+
...(item.anchor === null || item.anchor === undefined ? {} : { anchor: item.anchor }),
|
|
1260
1404
|
...(item.snapshot === null || item.snapshot === undefined ? {} : { snapshot: item.snapshot }),
|
|
1261
1405
|
updatedAt: Math.round(item.updatedAt),
|
|
1262
1406
|
prompt: item.prompt ?? '',
|
package/package.json
CHANGED