dsh-client-auto-continue 0.11.8 → 0.11.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -8
- package/README.zh.md +20 -8
- package/lib/client.js +54 -10
- package/lib/client.js.map +2 -2
- package/lib/index.js +197 -89
- package/lib/types/client/locales.d.ts +5 -0
- package/lib/types/client/settings-card.d.ts +2 -0
- package/lib/types/host/engine.d.ts +15 -11
- package/lib/types/index.d.ts +145 -11
- package/lib/types/shared/core.d.ts +14 -4
- package/package.json +9 -3
- package/src/client/locales.ts +20 -10
- package/src/client/settings-card.tsx +27 -0
- package/src/host/engine.ts +159 -87
- package/src/index.ts +74 -27
- package/src/shared/core.ts +26 -4
package/lib/index.js
CHANGED
|
@@ -101,6 +101,7 @@ var LOCALIZED_TEXT_DEFAULTS = {
|
|
|
101
101
|
zh: {
|
|
102
102
|
continueText: "继续",
|
|
103
103
|
continueTextMaxTokens: "继续",
|
|
104
|
+
continueTextSilent: "继续。你上一轮只输出了内部推理, 既没有回复也没有调用工具, 用户什么都没看到。每一轮都要以工具调用或可见回复结束。",
|
|
104
105
|
guardPendingText: "(上一步工具「{tool}」可能未完成, 先确认状态再继续, 不要重复执行)",
|
|
105
106
|
guardDoneText: "(上一步工具「{tool}」已完成, 结果: {result}; 不要重复执行, 直接继续)",
|
|
106
107
|
loopText: "(检测到你可能陷入循环, 请停止重复刚才的动作, 换一种方式继续)"
|
|
@@ -108,6 +109,7 @@ var LOCALIZED_TEXT_DEFAULTS = {
|
|
|
108
109
|
en: {
|
|
109
110
|
continueText: "Continue",
|
|
110
111
|
continueTextMaxTokens: "Continue",
|
|
112
|
+
continueTextSilent: "Continue. Your previous turn ended with internal reasoning only, with no message and no tool call, so nothing reached the user. Always finish a turn with a tool call or a visible answer.",
|
|
111
113
|
guardPendingText: '(The previous tool "{tool}" may not have completed. Check its state before continuing and do not run it again.)',
|
|
112
114
|
guardDoneText: '(The previous tool "{tool}" completed successfully. Result: {result}; do not run it again. Continue from there.)',
|
|
113
115
|
loopText: "(You may be stuck in a loop. Stop repeating the last action and continue with a different approach.)"
|
|
@@ -116,6 +118,7 @@ var LOCALIZED_TEXT_DEFAULTS = {
|
|
|
116
118
|
var DEFAULT_CONFIG = {
|
|
117
119
|
locale: "zh",
|
|
118
120
|
...LOCALIZED_TEXT_DEFAULTS.zh,
|
|
121
|
+
resumeSilentTurns: true,
|
|
119
122
|
guardTools: true,
|
|
120
123
|
graceMs: 3e3,
|
|
121
124
|
cooldownMs: 2e4,
|
|
@@ -149,12 +152,15 @@ function resolveConfig(section) {
|
|
|
149
152
|
const localized = LOCALIZED_TEXT_DEFAULTS[locale];
|
|
150
153
|
const text = typeof value.continueText === "string" && value.continueText.trim() !== "" ? value.continueText : localized.continueText;
|
|
151
154
|
const maxTokensText = typeof value.continueTextMaxTokens === "string" && value.continueTextMaxTokens.trim() !== "" ? value.continueTextMaxTokens : localized.continueTextMaxTokens;
|
|
155
|
+
const silentText = typeof value.continueTextSilent === "string" && value.continueTextSilent.trim() !== "" ? value.continueTextSilent : localized.continueTextSilent;
|
|
152
156
|
const guardPendingText = typeof value.guardPendingText === "string" && value.guardPendingText.trim() !== "" ? value.guardPendingText : localized.guardPendingText;
|
|
153
157
|
const guardDoneText = typeof value.guardDoneText === "string" && value.guardDoneText.trim() !== "" ? value.guardDoneText : localized.guardDoneText;
|
|
154
158
|
return {
|
|
155
159
|
locale,
|
|
156
160
|
continueText: text,
|
|
157
161
|
continueTextMaxTokens: maxTokensText,
|
|
162
|
+
resumeSilentTurns: booleanOr(value.resumeSilentTurns, DEFAULT_CONFIG.resumeSilentTurns),
|
|
163
|
+
continueTextSilent: silentText,
|
|
158
164
|
guardTools: booleanOr(value.guardTools, DEFAULT_CONFIG.guardTools),
|
|
159
165
|
guardPendingText,
|
|
160
166
|
guardDoneText,
|
|
@@ -522,9 +528,6 @@ function effectiveCooldown(consecutive, base, factor, max) {
|
|
|
522
528
|
const multiplier = Math.pow(factor, consecutive);
|
|
523
529
|
return Math.min(Math.max(base, base * multiplier), Math.max(base, max));
|
|
524
530
|
}
|
|
525
|
-
function sleep(ms) {
|
|
526
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
527
|
-
}
|
|
528
531
|
function todayKey() {
|
|
529
532
|
const d = /* @__PURE__ */ new Date();
|
|
530
533
|
const mm = String(d.getMonth() + 1).padStart(2, "0");
|
|
@@ -547,6 +550,9 @@ var freshState = () => ({
|
|
|
547
550
|
tools: new ToolInvocationTracker(),
|
|
548
551
|
lastTurn: void 0,
|
|
549
552
|
pendingRecoveryAt: 0,
|
|
553
|
+
turnOutput: "unknown",
|
|
554
|
+
outputTurn: void 0,
|
|
555
|
+
outputStartSeq: 0,
|
|
550
556
|
shortRun: 0,
|
|
551
557
|
lastShortAt: 0,
|
|
552
558
|
lastAssistantText: "",
|
|
@@ -648,8 +654,8 @@ function isLoopGuardCancelReason(value) {
|
|
|
648
654
|
}
|
|
649
655
|
var AutoContinueRunner = class {
|
|
650
656
|
/**
|
|
651
|
-
* @param ctx - host plugin context (agents registry, session events
|
|
652
|
-
* @param getConfig - read the current resolved configuration (
|
|
657
|
+
* @param ctx - host plugin context (agents registry, session events).
|
|
658
|
+
* @param getConfig - read the current resolved configuration (entry config).
|
|
653
659
|
*/
|
|
654
660
|
constructor(ctx, getConfig) {
|
|
655
661
|
this.ctx = ctx;
|
|
@@ -662,6 +668,7 @@ var AutoContinueRunner = class {
|
|
|
662
668
|
this.stateListeners = /* @__PURE__ */ new Set();
|
|
663
669
|
this.prioritizedFollowups = /* @__PURE__ */ new Set();
|
|
664
670
|
this.disposed = false;
|
|
671
|
+
this.bootScannedSessions = /* @__PURE__ */ new WeakSet();
|
|
665
672
|
this.disposeSessionEvents = ctx.on("session/event", (session, event) => {
|
|
666
673
|
try {
|
|
667
674
|
this.onHostEvent(session, event);
|
|
@@ -758,6 +765,10 @@ var AutoContinueRunner = class {
|
|
|
758
765
|
dispose() {
|
|
759
766
|
if (this.disposed) return;
|
|
760
767
|
this.disposed = true;
|
|
768
|
+
if (this.bootScanTimer !== void 0) clearTimeout(this.bootScanTimer);
|
|
769
|
+
this.bootScanTimer = void 0;
|
|
770
|
+
this.wakeBootScan?.();
|
|
771
|
+
this.wakeBootScan = void 0;
|
|
761
772
|
this.disposeSessionEvents();
|
|
762
773
|
this.disposeInboxEvents();
|
|
763
774
|
this.prioritizedFollowups.clear();
|
|
@@ -787,23 +798,51 @@ var AutoContinueRunner = class {
|
|
|
787
798
|
}
|
|
788
799
|
if (event.type === "tool/call") {
|
|
789
800
|
const state = this.state(sessionId);
|
|
790
|
-
if (state.tools.recordCall(event))
|
|
801
|
+
if (state.tools.recordCall(event)) {
|
|
802
|
+
state.shortRun = 0;
|
|
803
|
+
if (this.tracksOutput(state, event)) state.turnOutput = "visible";
|
|
804
|
+
}
|
|
791
805
|
} else if (event.type === "tool/result") {
|
|
792
806
|
const state = this.state(sessionId);
|
|
793
807
|
state.tools.recordResult(event);
|
|
794
808
|
} else if (event.type === "step/start") {
|
|
795
809
|
const state = this.state(sessionId);
|
|
810
|
+
if (this.tracksOutput(state, event) && state.turnOutput === "empty") state.turnOutput = "silent";
|
|
796
811
|
const repeat = state.tools.confirmRepeatAtStep(event.seq);
|
|
797
812
|
if (repeat !== void 0) this.checkLoop(sessionId, state, repeat);
|
|
798
813
|
} else if (event.type === "assistant/chunk") {
|
|
799
814
|
const state = this.state(sessionId);
|
|
815
|
+
if (this.tracksOutput(state, event)) {
|
|
816
|
+
const chunk = event.data.chunk;
|
|
817
|
+
const visible = chunk.type === "text-delta" && chunk.text.trim() !== "" || chunk.type === "tool-call-delta" || chunk.type === "block-end" && this.hasVisibleContent([chunk.block]) || chunk.type === "block-start" && chunk.blockType !== "reasoning" && chunk.blockType !== "text";
|
|
818
|
+
if (visible) state.turnOutput = "visible";
|
|
819
|
+
else if (state.turnOutput === "empty" && chunk.type !== "usage" && chunk.type !== "finish") state.turnOutput = "silent";
|
|
820
|
+
}
|
|
800
821
|
this.onAssistantChunk(sessionId, state, event);
|
|
801
822
|
} else if (event.type === "assistant/message") {
|
|
802
823
|
const state = this.state(sessionId);
|
|
824
|
+
if (this.tracksOutput(state, event)) {
|
|
825
|
+
if (this.hasVisibleContent(event.data.message.content)) state.turnOutput = "visible";
|
|
826
|
+
else if (state.turnOutput === "empty") state.turnOutput = "silent";
|
|
827
|
+
}
|
|
803
828
|
this.onAssistantMessage(sessionId, state, event);
|
|
804
829
|
}
|
|
805
830
|
this.onSessionEvent(sessionId, event);
|
|
806
831
|
}
|
|
832
|
+
tracksOutput(state, event) {
|
|
833
|
+
return state.running === true && state.outputTurn === event.data.turn && event.seq > state.outputStartSeq;
|
|
834
|
+
}
|
|
835
|
+
/** Treat unknown block kinds conservatively: only reasoning and blank text are silent. */
|
|
836
|
+
hasVisibleContent(content) {
|
|
837
|
+
if (!Array.isArray(content)) return false;
|
|
838
|
+
return content.some((part) => {
|
|
839
|
+
if (part === null || typeof part !== "object") return true;
|
|
840
|
+
const block = part;
|
|
841
|
+
if (block.type === "reasoning") return false;
|
|
842
|
+
if (block.type === "text") return typeof block.text !== "string" || block.text.trim() !== "";
|
|
843
|
+
return true;
|
|
844
|
+
});
|
|
845
|
+
}
|
|
807
846
|
/** 从 assistant/message 事件提取纯文本。 */
|
|
808
847
|
assistantText(event) {
|
|
809
848
|
const content = event.data.message.content;
|
|
@@ -988,6 +1027,9 @@ var AutoContinueRunner = class {
|
|
|
988
1027
|
state.streamLastSegment = "";
|
|
989
1028
|
state.streamRepeatRun = 0;
|
|
990
1029
|
state.loopFired = false;
|
|
1030
|
+
state.turnOutput = "empty";
|
|
1031
|
+
state.outputTurn = event.data.turn;
|
|
1032
|
+
state.outputStartSeq = event.seq;
|
|
991
1033
|
if (state.loopRetryTimer !== void 0) {
|
|
992
1034
|
clearTimeout(state.loopRetryTimer);
|
|
993
1035
|
state.loopRetryTimer = void 0;
|
|
@@ -999,16 +1041,29 @@ var AutoContinueRunner = class {
|
|
|
999
1041
|
const loopCancelPending = state.loopFired;
|
|
1000
1042
|
state.loopFired = false;
|
|
1001
1043
|
this.cancelPending(sessionId, "收到新的 turn/end");
|
|
1044
|
+
const turnOutput = state.outputTurn === event.data.turn ? state.turnOutput : "unknown";
|
|
1045
|
+
state.turnOutput = "unknown";
|
|
1046
|
+
state.outputTurn = void 0;
|
|
1002
1047
|
const reason = event.data.reason;
|
|
1003
1048
|
const reasonKind = readReasonKind(reason);
|
|
1004
1049
|
if (reasonKind === void 0) {
|
|
1005
1050
|
console.error(`[auto-continue] 忽略畸形 turn/end ${sessionId}: reason 无法解释`);
|
|
1006
1051
|
break;
|
|
1007
1052
|
}
|
|
1008
|
-
if (reasonKind === "completed") {
|
|
1009
|
-
state.consecutive = 0;
|
|
1053
|
+
if (reasonKind === "no-visible-output" || reasonKind === "completed" && turnOutput === "silent") {
|
|
1010
1054
|
state.lastFailure = void 0;
|
|
1011
|
-
|
|
1055
|
+
state.lastTurn = event.data.turn;
|
|
1056
|
+
state.lastFailureAt = Date.now();
|
|
1057
|
+
this.noteRecovery(sessionId, "error");
|
|
1058
|
+
if (this.getConfig().resumeSilentTurns) this.schedule(sessionId, `turn/end:${reasonKind}:silent`);
|
|
1059
|
+
break;
|
|
1060
|
+
}
|
|
1061
|
+
if (reasonKind === "completed") {
|
|
1062
|
+
if (turnOutput === "visible") {
|
|
1063
|
+
state.consecutive = 0;
|
|
1064
|
+
state.lastFailure = void 0;
|
|
1065
|
+
this.noteRecovery(sessionId, "completed");
|
|
1066
|
+
}
|
|
1012
1067
|
} else if (reasonKind === "aborted") {
|
|
1013
1068
|
if (isLoopGuardCancelReason(reason.reason)) {
|
|
1014
1069
|
if (loopCancelPending) this.bumpStat({ looped: 1 });
|
|
@@ -1032,7 +1087,7 @@ var AutoContinueRunner = class {
|
|
|
1032
1087
|
} catch (error) {
|
|
1033
1088
|
console.error(`[auto-continue] loop 重启异常 ${sessionId}: ${error instanceof Error ? error.message : String(error)}`);
|
|
1034
1089
|
}
|
|
1035
|
-
}, remaining);
|
|
1090
|
+
}, remaining + 10);
|
|
1036
1091
|
this.log(`loop 重启延迟 ${remaining}ms(冷却期) ${sessionId}`);
|
|
1037
1092
|
} else {
|
|
1038
1093
|
this.schedule(sessionId, "loop:aborted");
|
|
@@ -1210,11 +1265,18 @@ var AutoContinueRunner = class {
|
|
|
1210
1265
|
}
|
|
1211
1266
|
}, config.graceMs);
|
|
1212
1267
|
state.pendingTimer = timer;
|
|
1213
|
-
const template =
|
|
1268
|
+
const template = this.templateFor(config, reason);
|
|
1214
1269
|
this.log(
|
|
1215
1270
|
`检测到非人为中断 ${sessionId}(${reason}), ${config.graceMs}ms 后自动发送「${template}」`
|
|
1216
1271
|
);
|
|
1217
1272
|
}
|
|
1273
|
+
/** 按调度原因选择续跑模板: loop 重启、无输出回合、max-tokens, 其余用通用继续文本。 */
|
|
1274
|
+
templateFor(config, reason) {
|
|
1275
|
+
if (reason.startsWith("loop:")) return config.loopText;
|
|
1276
|
+
if (reason.endsWith(":silent")) return config.continueTextSilent;
|
|
1277
|
+
if (reason.includes("max-tokens")) return config.continueTextMaxTokens;
|
|
1278
|
+
return config.continueText;
|
|
1279
|
+
}
|
|
1218
1280
|
cancelPending(sessionId, why) {
|
|
1219
1281
|
const state = this.state(sessionId);
|
|
1220
1282
|
if (state.pendingTimer === void 0) return;
|
|
@@ -1226,6 +1288,7 @@ var AutoContinueRunner = class {
|
|
|
1226
1288
|
if (this.disposed) return;
|
|
1227
1289
|
const state = this.state(sessionId);
|
|
1228
1290
|
const config = this.getConfig();
|
|
1291
|
+
if (reason.endsWith(":silent") && !config.resumeSilentTurns) return;
|
|
1229
1292
|
if (state.subagent) return;
|
|
1230
1293
|
if (config.paused) {
|
|
1231
1294
|
this.log(`跳过 ${sessionId}(${reason}): 全局暂停中`);
|
|
@@ -1243,7 +1306,7 @@ var AutoContinueRunner = class {
|
|
|
1243
1306
|
this.log(`跳过 ${sessionId}(${reason}): 已连续自动继续 ${state.consecutive} 次, 等待用户介入或成功回合`);
|
|
1244
1307
|
return;
|
|
1245
1308
|
}
|
|
1246
|
-
const template =
|
|
1309
|
+
const template = this.templateFor(config, reason);
|
|
1247
1310
|
const text = this.buildContinueText(config, state, template);
|
|
1248
1311
|
const agent = this.ctx.agents.get(sessionId);
|
|
1249
1312
|
if (agent === void 0) {
|
|
@@ -1326,98 +1389,111 @@ var AutoContinueRunner = class {
|
|
|
1326
1389
|
return state.tools.guard();
|
|
1327
1390
|
}
|
|
1328
1391
|
async bootScanLoop() {
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
async scanLoop(attempts, delayMs) {
|
|
1333
|
-
for (let attempt = 0; attempt < attempts && !this.disposed; attempt += 1) {
|
|
1392
|
+
const deadline = Date.now() + this.getConfig().freshMs;
|
|
1393
|
+
for (let attempt = 0; !this.disposed; attempt += 1) {
|
|
1394
|
+
if (attempt > 0 && Date.now() >= deadline) return;
|
|
1334
1395
|
try {
|
|
1335
1396
|
if (await this.scanInterrupted()) return;
|
|
1336
1397
|
} catch (error) {
|
|
1337
1398
|
if (this.disposed) return;
|
|
1338
1399
|
if (attempt % 10 === 0) {
|
|
1339
1400
|
this.log(
|
|
1340
|
-
`扫描失败(${attempt + 1}
|
|
1401
|
+
`扫描失败(${attempt + 1}): ${error instanceof Error ? error.message : String(error)}`
|
|
1341
1402
|
);
|
|
1342
1403
|
}
|
|
1343
1404
|
}
|
|
1344
|
-
|
|
1405
|
+
const remaining = deadline - Date.now();
|
|
1406
|
+
if (this.disposed || remaining <= 0) return;
|
|
1407
|
+
await new Promise((resolve) => {
|
|
1408
|
+
this.wakeBootScan = resolve;
|
|
1409
|
+
this.bootScanTimer = setTimeout(() => {
|
|
1410
|
+
this.bootScanTimer = void 0;
|
|
1411
|
+
this.wakeBootScan = void 0;
|
|
1412
|
+
resolve();
|
|
1413
|
+
}, Math.min(3e3, remaining));
|
|
1414
|
+
});
|
|
1345
1415
|
}
|
|
1346
1416
|
}
|
|
1347
|
-
/**
|
|
1348
|
-
* 扫描最近中断过的会话: 最后回合以非人为原因结束, 且其后没有新回合或用户消息。
|
|
1349
|
-
* @returns 是否成功完成一次扫描(宿主就绪)。
|
|
1350
|
-
*/
|
|
1417
|
+
/** Inspect newly available sessions; process each settled startup history once. */
|
|
1351
1418
|
async scanInterrupted() {
|
|
1352
1419
|
const config = this.getConfig();
|
|
1353
|
-
if (config.paused) return
|
|
1420
|
+
if (config.paused || !config.scanOnBoot) return false;
|
|
1354
1421
|
const now = Date.now();
|
|
1355
1422
|
const candidates = [];
|
|
1356
1423
|
for (const agent of this.ctx.agents.list()) {
|
|
1357
1424
|
const session = agent.session;
|
|
1358
|
-
if (session.header.origin === "subagent") continue;
|
|
1359
|
-
const
|
|
1360
|
-
|
|
1361
|
-
(latest, event) => Math.max(latest, event.time),
|
|
1362
|
-
Number.isFinite(session.header.createdAt) ? session.header.createdAt : 0
|
|
1363
|
-
);
|
|
1364
|
-
candidates.push({
|
|
1365
|
-
sessionId: session.id,
|
|
1366
|
-
events,
|
|
1367
|
-
lastActivityAt,
|
|
1368
|
-
listIndex: candidates.length
|
|
1369
|
-
});
|
|
1370
|
-
}
|
|
1371
|
-
candidates.sort(
|
|
1372
|
-
(left, right) => right.lastActivityAt - left.lastActivityAt || left.listIndex - right.listIndex
|
|
1373
|
-
);
|
|
1374
|
-
for (const candidate of candidates.slice(0, config.scanLimit)) {
|
|
1375
|
-
if (this.disposed) return true;
|
|
1376
|
-
const state = this.state(candidate.sessionId);
|
|
1377
|
-
if (state.pendingTimer !== void 0) continue;
|
|
1378
|
-
if (state.consecutive >= config.maxConsecutive) continue;
|
|
1425
|
+
if (session.header.origin === "subagent" || this.bootScannedSessions.has(session)) continue;
|
|
1426
|
+
const state = this.state(session.id);
|
|
1427
|
+
if (state.pendingTimer !== void 0 || state.consecutive >= config.maxConsecutive) continue;
|
|
1379
1428
|
if (now - state.lastAttemptAt < this.cooldownFor(state)) continue;
|
|
1380
|
-
if (now < (this.pauseUntil.get(
|
|
1381
|
-
const events =
|
|
1429
|
+
if (now < (this.pauseUntil.get(session.id) ?? 0)) continue;
|
|
1430
|
+
const events = snapshotSessionEvents(session);
|
|
1431
|
+
if (events.length === 0) continue;
|
|
1382
1432
|
let lastEnd;
|
|
1383
1433
|
for (let i = events.length - 1; i >= 0; i -= 1) {
|
|
1384
1434
|
const event = events[i];
|
|
1385
|
-
if (event
|
|
1435
|
+
if (event?.type === "turn/end") {
|
|
1386
1436
|
lastEnd = event;
|
|
1387
1437
|
break;
|
|
1388
1438
|
}
|
|
1389
1439
|
}
|
|
1390
|
-
|
|
1391
|
-
const
|
|
1392
|
-
const
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
if (event.type === "turn/start") superseded = true;
|
|
1399
|
-
if (event.type === "user/message" && event.data.source.kind === "user") superseded = true;
|
|
1400
|
-
if (superseded) break;
|
|
1440
|
+
const reasonKind = readReasonKind(lastEnd?.data.reason);
|
|
1441
|
+
const silent = reasonKind === "no-visible-output" && config.resumeSilentTurns;
|
|
1442
|
+
const superseded = lastEnd !== void 0 && events.some(
|
|
1443
|
+
(event) => event.seq > lastEnd.seq && (event.type === "turn/start" || event.type === "user/message" && event.data.source.kind === "user")
|
|
1444
|
+
);
|
|
1445
|
+
if (lastEnd === void 0 || reasonKind === void 0 || !isNonHumanReason(reasonKind) && !silent || lastEnd.time < now - config.freshMs || superseded) {
|
|
1446
|
+
this.bootScannedSessions.add(session);
|
|
1447
|
+
continue;
|
|
1401
1448
|
}
|
|
1402
|
-
|
|
1403
|
-
this.applyGuardFromEvents(state, events, lastEnd.seq);
|
|
1404
|
-
const scanReason = `scan:turn/end:${reasonKind}`;
|
|
1405
|
-
this.log(`扫描发现中断 ${candidate.sessionId}(turn/end:${reasonKind}), 交给恢复策略处理`);
|
|
1449
|
+
let failure;
|
|
1406
1450
|
if (reasonKind === "error") {
|
|
1407
|
-
|
|
1451
|
+
failure = parseFailureFacts(lastEnd.data.reason.error);
|
|
1408
1452
|
if (failure === void 0) {
|
|
1409
|
-
|
|
1453
|
+
this.bootScannedSessions.add(session);
|
|
1454
|
+
console.error(`[auto-continue] 忽略畸形扫描 turn/end ${session.id}: error details 无法解释`);
|
|
1455
|
+
continue;
|
|
1456
|
+
}
|
|
1457
|
+
if (config.classify && !isTransientFailure(failure, config.retryableErrorPatterns)) {
|
|
1458
|
+
this.bootScannedSessions.add(session);
|
|
1459
|
+
this.onTurnFailure(session.id, "scan:turn/end:error", failure);
|
|
1410
1460
|
continue;
|
|
1411
1461
|
}
|
|
1462
|
+
}
|
|
1463
|
+
candidates.push({
|
|
1464
|
+
session,
|
|
1465
|
+
events,
|
|
1466
|
+
lastEnd,
|
|
1467
|
+
reasonKind,
|
|
1468
|
+
failure,
|
|
1469
|
+
lastActivityAt: events.reduce((latest, event) => Math.max(latest, event.time), 0),
|
|
1470
|
+
listIndex: candidates.length
|
|
1471
|
+
});
|
|
1472
|
+
}
|
|
1473
|
+
candidates.sort((left, right) => right.lastActivityAt - left.lastActivityAt || left.listIndex - right.listIndex);
|
|
1474
|
+
for (const candidate of candidates.slice(0, config.scanLimit)) {
|
|
1475
|
+
if (this.disposed) return true;
|
|
1476
|
+
const { session, events, lastEnd, reasonKind, failure } = candidate;
|
|
1477
|
+
this.bootScannedSessions.add(session);
|
|
1478
|
+
const state = this.state(session.id);
|
|
1479
|
+
this.applyGuardFromEvents(state, events, lastEnd.seq);
|
|
1480
|
+
const scanReason = `scan:turn/end:${reasonKind}${reasonKind === "no-visible-output" ? ":silent" : ""}`;
|
|
1481
|
+
this.log(`扫描发现中断 ${session.id}(turn/end:${reasonKind}), 交给恢复策略处理`);
|
|
1482
|
+
if (failure !== void 0) {
|
|
1412
1483
|
state.lastFailure = failure;
|
|
1413
1484
|
state.lastTurn = lastEnd.data.turn;
|
|
1414
1485
|
state.lastFailureAt = lastEnd.time;
|
|
1415
|
-
this.onTurnFailure(
|
|
1486
|
+
this.onTurnFailure(session.id, scanReason, failure);
|
|
1416
1487
|
} else {
|
|
1417
|
-
|
|
1488
|
+
if (reasonKind === "no-visible-output") {
|
|
1489
|
+
state.lastFailure = void 0;
|
|
1490
|
+
state.lastTurn = lastEnd.data.turn;
|
|
1491
|
+
state.lastFailureAt = lastEnd.time;
|
|
1492
|
+
}
|
|
1493
|
+
this.schedule(session.id, scanReason);
|
|
1418
1494
|
}
|
|
1419
1495
|
}
|
|
1420
|
-
return
|
|
1496
|
+
return false;
|
|
1421
1497
|
}
|
|
1422
1498
|
/** 从历史事件恢复上一步工具调用状态(扫描路径的幂等护栏)。 */
|
|
1423
1499
|
applyGuardFromEvents(state, events, untilSeq) {
|
|
@@ -1427,7 +1503,6 @@ var AutoContinueRunner = class {
|
|
|
1427
1503
|
|
|
1428
1504
|
// src/index.ts
|
|
1429
1505
|
var AUTO_CONTINUE_NS = "auto-continue";
|
|
1430
|
-
var SETTINGS_NS = AUTO_CONTINUE_NS;
|
|
1431
1506
|
var AutoContinueSchema = z2.object({
|
|
1432
1507
|
/** Active browser/UI locale mirrored by the client. */
|
|
1433
1508
|
locale: z2.string().default("zh"),
|
|
@@ -1435,6 +1510,10 @@ var AutoContinueSchema = z2.object({
|
|
|
1435
1510
|
continueText: z2.string().default(""),
|
|
1436
1511
|
/** Text sent when the output token ceiling is reached (same placeholders as `continueText`). */
|
|
1437
1512
|
continueTextMaxTokens: z2.string().default(""),
|
|
1513
|
+
/** Resume observed model activity that completes without visible output. */
|
|
1514
|
+
resumeSilentTurns: z2.boolean().default(true),
|
|
1515
|
+
/** Text sent to resume a silent turn (same placeholders as `continueText`). */
|
|
1516
|
+
continueTextSilent: z2.string().default(""),
|
|
1438
1517
|
/** Idempotency guard: inspect the last tool call before resuming and steer the model. */
|
|
1439
1518
|
guardTools: z2.boolean().default(true),
|
|
1440
1519
|
/** Guard text appended when the last tool call has no confirmed result (it may have partially executed). */
|
|
@@ -1447,11 +1526,11 @@ var AutoContinueSchema = z2.object({
|
|
|
1447
1526
|
cooldownMs: z2.natural().default(2e4),
|
|
1448
1527
|
/** Max consecutive auto-continues per session before stopping. */
|
|
1449
1528
|
maxConsecutive: z2.natural().min(1).default(3),
|
|
1450
|
-
/**
|
|
1529
|
+
/** Recover delayed interrupted sessions during the host startup window. */
|
|
1451
1530
|
scanOnBoot: z2.boolean().default(true),
|
|
1452
|
-
/** Max
|
|
1531
|
+
/** Max eligible recoveries per scan pass (most recently active first). */
|
|
1453
1532
|
scanLimit: z2.natural().min(1).default(8),
|
|
1454
|
-
/**
|
|
1533
|
+
/** Interruption freshness and duration of startup polling (ms). */
|
|
1455
1534
|
freshMs: z2.natural().default(15 * 60 * 1e3),
|
|
1456
1535
|
/** Log `[auto-continue]` lines to the browser console. */
|
|
1457
1536
|
verbose: z2.boolean().default(true),
|
|
@@ -1482,21 +1561,29 @@ var AutoContinueSchema = z2.object({
|
|
|
1482
1561
|
/** Text sent after the loop guard cancels and restarts a turn (supports {tool}). */
|
|
1483
1562
|
loopText: z2.string().default("")
|
|
1484
1563
|
});
|
|
1485
|
-
|
|
1564
|
+
var entrySchema = AutoContinueSchema;
|
|
1565
|
+
var Config = entrySchema.volatile?.() ?? AutoContinueSchema;
|
|
1566
|
+
function entryConfig(config) {
|
|
1567
|
+
return config !== void 0 && "get" in config ? config.get() : config;
|
|
1568
|
+
}
|
|
1569
|
+
function apply(ctx, config) {
|
|
1570
|
+
let legacyConfig;
|
|
1486
1571
|
ctx.inject(["settings"], (settingsCtx) => {
|
|
1487
|
-
settingsCtx.settings
|
|
1488
|
-
|
|
1572
|
+
const settings = settingsCtx.settings;
|
|
1573
|
+
if (typeof settings.register !== "function" || typeof settings.get !== "function") return;
|
|
1574
|
+
settings.register(AUTO_CONTINUE_NS, AutoContinueSchema, { applies: "live" });
|
|
1575
|
+
const read = () => settings.get(AUTO_CONTINUE_NS);
|
|
1576
|
+
legacyConfig = read;
|
|
1577
|
+
settingsCtx.effect(() => () => {
|
|
1578
|
+
if (legacyConfig === read) legacyConfig = void 0;
|
|
1489
1579
|
});
|
|
1490
1580
|
});
|
|
1491
|
-
let
|
|
1492
|
-
ctx.inject(["
|
|
1493
|
-
|
|
1494
|
-
const runner = new AutoContinueRunner(
|
|
1495
|
-
engineCtx,
|
|
1496
|
-
() => resolveConfig(engineCtx.settings.get(SETTINGS_NS))
|
|
1497
|
-
);
|
|
1498
|
-
runnerRef = runner;
|
|
1581
|
+
let disposeEngine;
|
|
1582
|
+
ctx.inject(["agents", "webServer"], (engineCtx) => {
|
|
1583
|
+
disposeEngine?.();
|
|
1584
|
+
const runner = new AutoContinueRunner(engineCtx, () => resolveConfig(legacyConfig?.() ?? entryConfig(config)));
|
|
1499
1585
|
const sseClients = /* @__PURE__ */ new Set();
|
|
1586
|
+
const closeClients = /* @__PURE__ */ new Set();
|
|
1500
1587
|
const pushToAll = (data) => {
|
|
1501
1588
|
for (const send of sseClients) {
|
|
1502
1589
|
try {
|
|
@@ -1523,7 +1610,7 @@ function apply(ctx) {
|
|
|
1523
1610
|
|
|
1524
1611
|
`);
|
|
1525
1612
|
});
|
|
1526
|
-
engineCtx.webServer.register({
|
|
1613
|
+
const stopBridge = engineCtx.webServer.register({
|
|
1527
1614
|
kind: "exact",
|
|
1528
1615
|
path: "/api/auto-continue-bridge",
|
|
1529
1616
|
handler: (req, res) => {
|
|
@@ -1539,10 +1626,17 @@ function apply(ctx) {
|
|
|
1539
1626
|
res.write(data);
|
|
1540
1627
|
};
|
|
1541
1628
|
sseClients.add(send);
|
|
1542
|
-
|
|
1629
|
+
const close = () => {
|
|
1630
|
+
res.end();
|
|
1631
|
+
};
|
|
1632
|
+
closeClients.add(close);
|
|
1633
|
+
req.on("close", () => {
|
|
1634
|
+
sseClients.delete(send);
|
|
1635
|
+
closeClients.delete(close);
|
|
1636
|
+
});
|
|
1543
1637
|
}
|
|
1544
1638
|
});
|
|
1545
|
-
engineCtx.webServer.register({
|
|
1639
|
+
const stopActions = engineCtx.webServer.register({
|
|
1546
1640
|
kind: "exact",
|
|
1547
1641
|
path: "/api/auto-continue-action",
|
|
1548
1642
|
handler: (req, res) => {
|
|
@@ -1569,15 +1663,29 @@ function apply(ctx) {
|
|
|
1569
1663
|
});
|
|
1570
1664
|
}
|
|
1571
1665
|
});
|
|
1666
|
+
let disposed = false;
|
|
1667
|
+
const dispose = () => {
|
|
1668
|
+
if (disposed) return;
|
|
1669
|
+
disposed = true;
|
|
1670
|
+
runner.dispose();
|
|
1671
|
+
stopBridge();
|
|
1672
|
+
stopActions();
|
|
1673
|
+
for (const close of closeClients) close();
|
|
1674
|
+
closeClients.clear();
|
|
1675
|
+
sseClients.clear();
|
|
1676
|
+
};
|
|
1677
|
+
disposeEngine = dispose;
|
|
1678
|
+
engineCtx.effect(() => dispose);
|
|
1572
1679
|
});
|
|
1573
1680
|
ctx.effect(() => () => {
|
|
1574
|
-
const
|
|
1575
|
-
|
|
1576
|
-
|
|
1681
|
+
const dispose = disposeEngine;
|
|
1682
|
+
disposeEngine = void 0;
|
|
1683
|
+
dispose?.();
|
|
1577
1684
|
});
|
|
1578
1685
|
}
|
|
1579
1686
|
export {
|
|
1580
1687
|
AUTO_CONTINUE_NS,
|
|
1581
1688
|
AutoContinueSchema,
|
|
1689
|
+
Config,
|
|
1582
1690
|
apply
|
|
1583
1691
|
};
|
|
@@ -25,6 +25,11 @@ export declare const zh: {
|
|
|
25
25
|
'field.continueTextMaxTokens': string;
|
|
26
26
|
'field.continueTextMaxTokensHint': string;
|
|
27
27
|
'default.continueTextMaxTokens': "继续";
|
|
28
|
+
'field.resumeSilentTurns': string;
|
|
29
|
+
'field.resumeSilentTurnsHint': string;
|
|
30
|
+
'field.continueTextSilent': string;
|
|
31
|
+
'field.continueTextSilentHint': string;
|
|
32
|
+
'default.continueTextSilent': "继续。你上一轮只输出了内部推理, 既没有回复也没有调用工具, 用户什么都没看到。每一轮都要以工具调用或可见回复结束。";
|
|
28
33
|
'field.guardTools': string;
|
|
29
34
|
'field.guardToolsHint': string;
|
|
30
35
|
'field.guardPendingText': string;
|
|
@@ -7,6 +7,8 @@ export interface AutoContinueSettingsCardState extends CardShell {
|
|
|
7
7
|
paused: CardFieldState;
|
|
8
8
|
continueText: CardFieldState;
|
|
9
9
|
continueTextMaxTokens: CardFieldState;
|
|
10
|
+
resumeSilentTurns: CardFieldState;
|
|
11
|
+
continueTextSilent: CardFieldState;
|
|
10
12
|
guardTools: CardFieldState;
|
|
11
13
|
guardPendingText: CardFieldState;
|
|
12
14
|
guardDoneText: CardFieldState;
|
|
@@ -6,10 +6,11 @@
|
|
|
6
6
|
* bugs (issue #13) cannot exist by construction. Listens to the session event
|
|
7
7
|
* firehose (`session/event`), sends through the agent registry
|
|
8
8
|
* (`agent.followup`), cancels through `agent.cancel`, and reads configuration
|
|
9
|
-
*
|
|
9
|
+
* injected by the entry (`apply`'s config argument, merged with schema
|
|
10
|
+
* defaults by `resolveConfig`).
|
|
10
11
|
*
|
|
11
|
-
* All behavior is driven by the `auto-continue`
|
|
12
|
-
*
|
|
12
|
+
* All behavior is driven by the `auto-continue` entry config; every knob below
|
|
13
|
+
* is user-configurable there.
|
|
13
14
|
*/
|
|
14
15
|
import type { Context } from '@deepseek-ai/cordis';
|
|
15
16
|
import type { SessionId } from '@deepseek-ai/dsh-session/types';
|
|
@@ -40,9 +41,12 @@ export declare class AutoContinueRunner {
|
|
|
40
41
|
private readonly disposeSessionEvents;
|
|
41
42
|
private readonly disposeInboxEvents;
|
|
42
43
|
private disposed;
|
|
44
|
+
private readonly bootScannedSessions;
|
|
45
|
+
private bootScanTimer;
|
|
46
|
+
private wakeBootScan;
|
|
43
47
|
/**
|
|
44
|
-
* @param ctx - host plugin context (agents registry, session events
|
|
45
|
-
* @param getConfig - read the current resolved configuration (
|
|
48
|
+
* @param ctx - host plugin context (agents registry, session events).
|
|
49
|
+
* @param getConfig - read the current resolved configuration (entry config).
|
|
46
50
|
*/
|
|
47
51
|
constructor(ctx: Context, getConfig: () => AutoContinueConfig);
|
|
48
52
|
private log;
|
|
@@ -69,6 +73,9 @@ export declare class AutoContinueRunner {
|
|
|
69
73
|
* 然后交给回合状态机。
|
|
70
74
|
*/
|
|
71
75
|
private onHostEvent;
|
|
76
|
+
private tracksOutput;
|
|
77
|
+
/** Treat unknown block kinds conservatively: only reasoning and blank text are silent. */
|
|
78
|
+
private hasVisibleContent;
|
|
72
79
|
/** 从 assistant/message 事件提取纯文本。 */
|
|
73
80
|
private assistantText;
|
|
74
81
|
private assistantChunkText;
|
|
@@ -102,6 +109,8 @@ export declare class AutoContinueRunner {
|
|
|
102
109
|
/** 本会话当前生效的冷却间隔(自适应退避)。 */
|
|
103
110
|
private cooldownFor;
|
|
104
111
|
private schedule;
|
|
112
|
+
/** 按调度原因选择续跑模板: loop 重启、无输出回合、max-tokens, 其余用通用继续文本。 */
|
|
113
|
+
private templateFor;
|
|
105
114
|
private cancelPending;
|
|
106
115
|
private fire;
|
|
107
116
|
/**
|
|
@@ -115,12 +124,7 @@ export declare class AutoContinueRunner {
|
|
|
115
124
|
/** 上一步工具调用的护栏状态(实时路径, 由 mux 帧维护)。 */
|
|
116
125
|
private currentGuard;
|
|
117
126
|
private bootScanLoop;
|
|
118
|
-
/**
|
|
119
|
-
private scanLoop;
|
|
120
|
-
/**
|
|
121
|
-
* 扫描最近中断过的会话: 最后回合以非人为原因结束, 且其后没有新回合或用户消息。
|
|
122
|
-
* @returns 是否成功完成一次扫描(宿主就绪)。
|
|
123
|
-
*/
|
|
127
|
+
/** Inspect newly available sessions; process each settled startup history once. */
|
|
124
128
|
private scanInterrupted;
|
|
125
129
|
/** 从历史事件恢复上一步工具调用状态(扫描路径的幂等护栏)。 */
|
|
126
130
|
private applyGuardFromEvents;
|