dsh-client-auto-continue 0.12.1 → 0.12.2
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/README.zh.md +8 -6
- package/lib/client.js +8 -6
- package/lib/client.js.map +2 -2
- package/lib/index.js +28 -6
- package/lib/types/host/engine.d.ts +4 -0
- package/lib/types/shared/core.d.ts +1 -1
- package/package.json +2 -2
- package/src/client/settings-form.ts +12 -10
- package/src/host/engine.ts +30 -6
- package/src/shared/core.ts +1 -1
package/lib/index.js
CHANGED
|
@@ -631,6 +631,7 @@ var LOOP_GUARD_CANCEL_CAUSE = {
|
|
|
631
631
|
};
|
|
632
632
|
var LOOP_COMPLETED_REASON = "turn/end:completed";
|
|
633
633
|
var LOOP_SILENT_REASON = "turn/end:loop-silent";
|
|
634
|
+
var MANUAL_NOTIFICATION_REASON = "manual:notification";
|
|
634
635
|
var isLoopReason = (reason) => reason === LOOP_COMPLETED_REASON || reason === LOOP_SILENT_REASON;
|
|
635
636
|
var STREAM_NEAR_DUPLICATE_MAX_CHARS = 2048;
|
|
636
637
|
var STREAM_TAIL_MAX_CHARS = 4096;
|
|
@@ -798,12 +799,31 @@ var AutoContinueRunner = class {
|
|
|
798
799
|
}
|
|
799
800
|
return state;
|
|
800
801
|
}
|
|
802
|
+
/** Mark a child session and clear queued grace/cooldown sends. */
|
|
803
|
+
markSubagent(sessionId, state) {
|
|
804
|
+
state.subagent = true;
|
|
805
|
+
this.cancelPending(sessionId, "子代理会话");
|
|
806
|
+
}
|
|
807
|
+
/** Read durable origin from the live agent for direct action paths as well. */
|
|
808
|
+
isSubagent(sessionId, state) {
|
|
809
|
+
if (state.subagent) return true;
|
|
810
|
+
const agent = this.ctx.agents.get(sessionId);
|
|
811
|
+
if (agent?.session?.header?.origin !== "subagent") return false;
|
|
812
|
+
this.markSubagent(sessionId, state);
|
|
813
|
+
return true;
|
|
814
|
+
}
|
|
801
815
|
/**
|
|
802
816
|
* 事件入口(host 单实例): 预处理工具调用/结果/模型消息(护栏与循环信号),
|
|
803
817
|
* 然后交给回合状态机。
|
|
804
818
|
*/
|
|
805
819
|
onHostEvent(session, event) {
|
|
806
820
|
const sessionId = session.id;
|
|
821
|
+
const sessionState = this.states.get(sessionId);
|
|
822
|
+
if (session.header?.origin === "subagent") {
|
|
823
|
+
this.markSubagent(sessionId, sessionState ?? this.state(sessionId));
|
|
824
|
+
return;
|
|
825
|
+
}
|
|
826
|
+
if (sessionState?.subagent) return;
|
|
807
827
|
if ((event.type === "user/message" || event.type === "assistant/message") && typeof event.surfaceOp === "object" && event.surfaceOp !== null) {
|
|
808
828
|
this.state(sessionId).tools.recordSurfaceReplacement(event.seq);
|
|
809
829
|
return;
|
|
@@ -1004,6 +1024,7 @@ var AutoContinueRunner = class {
|
|
|
1004
1024
|
* 才会用 loopText 重启回合——DSH 的 first-cause 语义保证用户 Stop 优先。
|
|
1005
1025
|
*/
|
|
1006
1026
|
interruptLoop(sessionId, state) {
|
|
1027
|
+
if (this.isSubagent(sessionId, state)) return;
|
|
1007
1028
|
if (state.loopFired) return;
|
|
1008
1029
|
if (Date.now() - state.lastAttemptAt < this.cooldownFor(state)) {
|
|
1009
1030
|
this.log(`跳过循环打断 ${sessionId}: 处于冷却期`);
|
|
@@ -1238,13 +1259,13 @@ var AutoContinueRunner = class {
|
|
|
1238
1259
|
async resumeNow(sessionId) {
|
|
1239
1260
|
if (this.disposed) return;
|
|
1240
1261
|
const state = this.state(sessionId);
|
|
1241
|
-
if (state
|
|
1262
|
+
if (this.isSubagent(sessionId, state)) return;
|
|
1242
1263
|
if (state.pendingTimer !== void 0) {
|
|
1243
1264
|
clearTimeout(state.pendingTimer);
|
|
1244
1265
|
state.pendingTimer = void 0;
|
|
1245
1266
|
}
|
|
1246
1267
|
try {
|
|
1247
|
-
await this.fire(sessionId,
|
|
1268
|
+
await this.fire(sessionId, MANUAL_NOTIFICATION_REASON, true);
|
|
1248
1269
|
} catch (error) {
|
|
1249
1270
|
console.error(`[auto-continue] 手动续跑异常 ${sessionId}: ${error instanceof Error ? error.message : String(error)}`);
|
|
1250
1271
|
}
|
|
@@ -1262,7 +1283,7 @@ var AutoContinueRunner = class {
|
|
|
1262
1283
|
schedule(sessionId, reason, force = false) {
|
|
1263
1284
|
const state = this.state(sessionId);
|
|
1264
1285
|
const config = this.getConfig();
|
|
1265
|
-
if (state
|
|
1286
|
+
if (this.isSubagent(sessionId, state)) return;
|
|
1266
1287
|
if (config.paused) {
|
|
1267
1288
|
this.log(`跳过 ${sessionId}(${reason}): 全局暂停中`);
|
|
1268
1289
|
return;
|
|
@@ -1344,14 +1365,15 @@ var AutoContinueRunner = class {
|
|
|
1344
1365
|
if (this.disposed) return;
|
|
1345
1366
|
const state = this.state(sessionId);
|
|
1346
1367
|
const config = this.getConfig();
|
|
1368
|
+
const manualResume = force && reason === MANUAL_NOTIFICATION_REASON;
|
|
1347
1369
|
if (reason.endsWith(":silent") && !config.resumeSilentTurns) return;
|
|
1348
1370
|
if (isLoopReason(reason) && !config.resumeCompletedTurns) return;
|
|
1349
|
-
if (state
|
|
1350
|
-
if (config.paused) {
|
|
1371
|
+
if (this.isSubagent(sessionId, state)) return;
|
|
1372
|
+
if (config.paused && !manualResume) {
|
|
1351
1373
|
this.log(`跳过 ${sessionId}(${reason}): 全局暂停中`);
|
|
1352
1374
|
return;
|
|
1353
1375
|
}
|
|
1354
|
-
if (Date.now() < (this.pauseUntil.get(sessionId) ?? 0)) {
|
|
1376
|
+
if (!manualResume && Date.now() < (this.pauseUntil.get(sessionId) ?? 0)) {
|
|
1355
1377
|
this.log(`跳过 ${sessionId}(${reason}): 会话暂停中`);
|
|
1356
1378
|
return;
|
|
1357
1379
|
}
|
|
@@ -68,6 +68,10 @@ export declare class AutoContinueRunner {
|
|
|
68
68
|
handleNoticeAction(sessionId: SessionId | undefined, action: string): void;
|
|
69
69
|
dispose(): void;
|
|
70
70
|
private state;
|
|
71
|
+
/** Mark a child session and clear queued grace/cooldown sends. */
|
|
72
|
+
private markSubagent;
|
|
73
|
+
/** Read durable origin from the live agent for direct action paths as well. */
|
|
74
|
+
private isSubagent;
|
|
71
75
|
/**
|
|
72
76
|
* 事件入口(host 单实例): 预处理工具调用/结果/模型消息(护栏与循环信号),
|
|
73
77
|
* 然后交给回合状态机。
|
|
@@ -289,7 +289,7 @@ export interface SessionState {
|
|
|
289
289
|
running: boolean | undefined;
|
|
290
290
|
/** 当前排队消息数(来自 session/queue 帧)。 */
|
|
291
291
|
queued: number;
|
|
292
|
-
/** 子代理会话(
|
|
292
|
+
/** 子代理会话(session header origin is `subagent`). */
|
|
293
293
|
subagent: boolean;
|
|
294
294
|
/** 最近一次回合失败的事实(用于分类与模板填充)。 */
|
|
295
295
|
lastFailure: FailureFacts | undefined;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-client-auto-continue",
|
|
3
3
|
"description": "DSH Web UI plugin: automatically sends a localized continue prompt when a request is interrupted by network errors or other non-human causes",
|
|
4
|
-
"version": "0.12.
|
|
4
|
+
"version": "0.12.2",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
7
7
|
"types": "lib/types/index.d.ts",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"watch": "node build.mjs --watch",
|
|
48
48
|
"typecheck": "tsc --noEmit",
|
|
49
49
|
"test:runtime": "node tests/settings-host-live.mjs 0.1.7 && node tests/settings-host-live.mjs 0.2.0 && node tests/profile-compatibility.mjs && node tests/client-settings-services.mjs 0.2.0",
|
|
50
|
-
"test": "node tests/scan-on-boot.mjs && node tests/silent-turn-boundaries.mjs && node tests/autonomous-loop.mjs && node tests/stats-freshness.mjs && node tests/simulate-host.mjs && node tests/settings-host-compat.mjs && node tests/simulate-client-loader.mjs && node tests/client-settings-services.mjs && node tests/locale-mirror-bounded.mjs && node tests/settings-card-ui.mjs && node tests/prepare-github-package.mjs",
|
|
50
|
+
"test": "node tests/scan-on-boot.mjs && node tests/silent-turn-boundaries.mjs && node tests/autonomous-loop.mjs && node tests/manual-resume-pauses.mjs && node tests/stats-freshness.mjs && node tests/simulate-host.mjs && node tests/settings-host-compat.mjs && node tests/simulate-client-loader.mjs && node tests/client-settings-services.mjs && node tests/client-settings-form.mjs && node tests/locale-mirror-bounded.mjs && node tests/settings-card-ui.mjs && node tests/prepare-github-package.mjs",
|
|
51
51
|
"prepack": "npm run build"
|
|
52
52
|
},
|
|
53
53
|
"keywords": [
|
|
@@ -214,9 +214,9 @@ export class CardForm<T> {
|
|
|
214
214
|
const plan = this.plan();
|
|
215
215
|
const writes = plan.flatMap((item) => (item.run === undefined ? [] : [item.run]));
|
|
216
216
|
if (plan.length === 0 || this.saving || writes.length !== plan.length) return;
|
|
217
|
-
//
|
|
218
|
-
//
|
|
219
|
-
const
|
|
217
|
+
// Keep the staged edits this save writes. A field can be staged again
|
|
218
|
+
// while a write is pending, so clear it only if that exact edit remains.
|
|
219
|
+
const edits = plan.map(({ field, staged }) => ({ field, staged }));
|
|
220
220
|
this.saving = true;
|
|
221
221
|
this.failed = false;
|
|
222
222
|
this.publish();
|
|
@@ -225,7 +225,9 @@ export class CardForm<T> {
|
|
|
225
225
|
landed = (await write()) && landed;
|
|
226
226
|
}
|
|
227
227
|
if (landed) {
|
|
228
|
-
for (const field of
|
|
228
|
+
for (const { field, staged } of edits) {
|
|
229
|
+
if (this.staged.get(field) === staged) this.staged.delete(field);
|
|
230
|
+
}
|
|
229
231
|
}
|
|
230
232
|
this.saving = false;
|
|
231
233
|
this.failed = !landed;
|
|
@@ -238,19 +240,19 @@ export class CardForm<T> {
|
|
|
238
240
|
* refuses rather than dropping the edit. A staged edit that matches the
|
|
239
241
|
* effective section is not a write at all.
|
|
240
242
|
*/
|
|
241
|
-
private plan(): { field: string; run: (() => Promise<boolean>) | undefined }[] {
|
|
242
|
-
const plan: { field: string; run: (() => Promise<boolean>) | undefined }[] = [];
|
|
243
|
+
private plan(): { field: string; staged: StagedEdit; run: (() => Promise<boolean>) | undefined }[] {
|
|
244
|
+
const plan: { field: string; staged: StagedEdit; run: (() => Promise<boolean>) | undefined }[] = [];
|
|
243
245
|
for (const [field, staged] of this.staged) {
|
|
244
246
|
const spec = this.specOf(field);
|
|
245
247
|
if (staged.clear) {
|
|
246
|
-
if (this.stored(field)) plan.push({ field, run: () => this.clear(field) });
|
|
248
|
+
if (this.stored(field)) plan.push({ field, staged, run: () => this.clear(field) });
|
|
247
249
|
continue;
|
|
248
250
|
}
|
|
249
251
|
if (staged.text === spec.format(this.sectionValue(field))) continue;
|
|
250
252
|
const write = spec.parse(staged.text);
|
|
251
|
-
if (write === undefined) plan.push({ field, run: undefined });
|
|
252
|
-
else if (write.kind === 'clear') plan.push({ field, run: () => this.clear(field) });
|
|
253
|
-
else plan.push({ field, run: () => this.store(field, write.value) });
|
|
253
|
+
if (write === undefined) plan.push({ field, staged, run: undefined });
|
|
254
|
+
else if (write.kind === 'clear') plan.push({ field, staged, run: () => this.clear(field) });
|
|
255
|
+
else plan.push({ field, staged, run: () => this.store(field, write.value) });
|
|
254
256
|
}
|
|
255
257
|
return plan;
|
|
256
258
|
}
|
package/src/host/engine.ts
CHANGED
|
@@ -92,6 +92,7 @@ const LOOP_GUARD_CANCEL_CAUSE = {
|
|
|
92
92
|
*/
|
|
93
93
|
const LOOP_COMPLETED_REASON = 'turn/end:completed';
|
|
94
94
|
const LOOP_SILENT_REASON = 'turn/end:loop-silent';
|
|
95
|
+
const MANUAL_NOTIFICATION_REASON = 'manual:notification';
|
|
95
96
|
const isLoopReason = (reason: string): boolean =>
|
|
96
97
|
reason === LOOP_COMPLETED_REASON || reason === LOOP_SILENT_REASON;
|
|
97
98
|
|
|
@@ -369,12 +370,33 @@ export class AutoContinueRunner {
|
|
|
369
370
|
return state;
|
|
370
371
|
}
|
|
371
372
|
|
|
373
|
+
/** Mark a child session and clear queued grace/cooldown sends. */
|
|
374
|
+
private markSubagent(sessionId: SessionId, state: SessionState): void {
|
|
375
|
+
state.subagent = true;
|
|
376
|
+
this.cancelPending(sessionId, '子代理会话');
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** Read durable origin from the live agent for direct action paths as well. */
|
|
380
|
+
private isSubagent(sessionId: SessionId, state: SessionState): boolean {
|
|
381
|
+
if (state.subagent) return true;
|
|
382
|
+
const agent = this.ctx.agents.get(sessionId);
|
|
383
|
+
if (agent?.session?.header?.origin !== 'subagent') return false;
|
|
384
|
+
this.markSubagent(sessionId, state);
|
|
385
|
+
return true;
|
|
386
|
+
}
|
|
387
|
+
|
|
372
388
|
/**
|
|
373
389
|
* 事件入口(host 单实例): 预处理工具调用/结果/模型消息(护栏与循环信号),
|
|
374
390
|
* 然后交给回合状态机。
|
|
375
391
|
*/
|
|
376
392
|
private onHostEvent(session: Session, event: SessionEvent): void {
|
|
377
393
|
const sessionId = session.id;
|
|
394
|
+
const sessionState = this.states.get(sessionId);
|
|
395
|
+
if (session.header?.origin === 'subagent') {
|
|
396
|
+
this.markSubagent(sessionId, sessionState ?? this.state(sessionId));
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
if (sessionState?.subagent) return;
|
|
378
400
|
if (
|
|
379
401
|
(event.type === 'user/message' || event.type === 'assistant/message') &&
|
|
380
402
|
typeof event.surfaceOp === 'object' &&
|
|
@@ -620,6 +642,7 @@ export class AutoContinueRunner {
|
|
|
620
642
|
* 才会用 loopText 重启回合——DSH 的 first-cause 语义保证用户 Stop 优先。
|
|
621
643
|
*/
|
|
622
644
|
private interruptLoop(sessionId: SessionId, state: SessionState): void {
|
|
645
|
+
if (this.isSubagent(sessionId, state)) return;
|
|
623
646
|
if (state.loopFired) return;
|
|
624
647
|
// 打断本身受冷却约束: 距上次打断/发送太近时不再打断, 防止反复打断刷屏
|
|
625
648
|
if (Date.now() - state.lastAttemptAt < this.cooldownFor(state)) {
|
|
@@ -906,13 +929,13 @@ export class AutoContinueRunner {
|
|
|
906
929
|
async resumeNow(sessionId: SessionId): Promise<void> {
|
|
907
930
|
if (this.disposed) return;
|
|
908
931
|
const state = this.state(sessionId);
|
|
909
|
-
if (state
|
|
932
|
+
if (this.isSubagent(sessionId, state)) return;
|
|
910
933
|
if (state.pendingTimer !== undefined) {
|
|
911
934
|
clearTimeout(state.pendingTimer);
|
|
912
935
|
state.pendingTimer = undefined;
|
|
913
936
|
}
|
|
914
937
|
try {
|
|
915
|
-
await this.fire(sessionId,
|
|
938
|
+
await this.fire(sessionId, MANUAL_NOTIFICATION_REASON, true);
|
|
916
939
|
} catch (error) {
|
|
917
940
|
console.error(`[auto-continue] 手动续跑异常 ${sessionId}: ${error instanceof Error ? error.message : String(error)}`);
|
|
918
941
|
}
|
|
@@ -932,7 +955,7 @@ export class AutoContinueRunner {
|
|
|
932
955
|
private schedule(sessionId: SessionId, reason: string, force = false): void {
|
|
933
956
|
const state = this.state(sessionId);
|
|
934
957
|
const config = this.getConfig();
|
|
935
|
-
if (state
|
|
958
|
+
if (this.isSubagent(sessionId, state)) return; // 子代理会话由父代理处理, 不抢跑
|
|
936
959
|
if (config.paused) {
|
|
937
960
|
this.log(`跳过 ${sessionId}(${reason}): 全局暂停中`);
|
|
938
961
|
return;
|
|
@@ -1025,16 +1048,17 @@ export class AutoContinueRunner {
|
|
|
1025
1048
|
if (this.disposed) return;
|
|
1026
1049
|
const state = this.state(sessionId);
|
|
1027
1050
|
const config = this.getConfig();
|
|
1051
|
+
const manualResume = force && reason === MANUAL_NOTIFICATION_REASON;
|
|
1028
1052
|
if (reason.endsWith(':silent') && !config.resumeSilentTurns) return;
|
|
1029
1053
|
// 宽限期内循环被关闭: 发送前必须重新检查开关, 与 :silent 恢复同样处理。
|
|
1030
1054
|
// 否则「关掉循环」后仍会多发出一次续跑。
|
|
1031
1055
|
if (isLoopReason(reason) && !config.resumeCompletedTurns) return;
|
|
1032
|
-
if (state
|
|
1033
|
-
if (config.paused) {
|
|
1056
|
+
if (this.isSubagent(sessionId, state)) return; // 子代理会话由父代理处理, 不抢跑
|
|
1057
|
+
if (config.paused && !manualResume) {
|
|
1034
1058
|
this.log(`跳过 ${sessionId}(${reason}): 全局暂停中`);
|
|
1035
1059
|
return;
|
|
1036
1060
|
}
|
|
1037
|
-
if (Date.now() < (this.pauseUntil.get(sessionId) ?? 0)) {
|
|
1061
|
+
if (!manualResume && Date.now() < (this.pauseUntil.get(sessionId) ?? 0)) {
|
|
1038
1062
|
this.log(`跳过 ${sessionId}(${reason}): 会话暂停中`);
|
|
1039
1063
|
return;
|
|
1040
1064
|
}
|
package/src/shared/core.ts
CHANGED
|
@@ -830,7 +830,7 @@ export interface SessionState {
|
|
|
830
830
|
running: boolean | undefined;
|
|
831
831
|
/** 当前排队消息数(来自 session/queue 帧)。 */
|
|
832
832
|
queued: number;
|
|
833
|
-
/** 子代理会话(
|
|
833
|
+
/** 子代理会话(session header origin is `subagent`). */
|
|
834
834
|
subagent: boolean;
|
|
835
835
|
/** 最近一次回合失败的事实(用于分类与模板填充)。 */
|
|
836
836
|
lastFailure: FailureFacts | undefined;
|