dsh-client-auto-continue 0.12.1 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,6 +6,20 @@
6
6
  */
7
7
 
8
8
  const css = `
9
+ .dshAcComposer { display: inline-flex; align-items: center; gap: 7px; min-height: 32px; flex: 0 0 auto; font: inherit; }
10
+ .dshAcComposerLabel { color: var(--dsw-alias-label-secondary); font-size: 12px; white-space: nowrap; }
11
+ .dshAcSwitchTarget { display: inline-flex; align-items: center; min-height: 32px; }
12
+ .dshAcSwitchTarget > button { position: relative; }
13
+ .dshAcSwitchTarget > button::before { content: ''; position: absolute; inset: -6px -3px; }
14
+ .dshAcComposerError { color: var(--dsw-alias-state-error-primary, var(--dsw-alias-label-secondary)); font-size: 12px; max-width: 160px; }
15
+ .dshAcComposerError:empty { display: none; }
16
+ .dshAcSwitch { position: relative; box-sizing: border-box; flex: none; width: 36px; height: 20px; padding: 2px; border: 0; border-radius: 999px; background: var(--dsw-alias-border-l3); cursor: pointer; }
17
+ .dshAcSwitch[aria-checked='true'] { background: var(--dsw-alias-brand-primary); }
18
+ .dshAcSwitch > span { display: block; width: 16px; height: 16px; border-radius: 50%; background: var(--dsw-alias-label-primary-foreground); }
19
+ .dshAcSwitch[aria-checked='true'] > span { transform: translateX(16px); }
20
+ .dshAcSwitch[aria-checked='false'] > span { background: var(--dsw-alias-switch-thumb, var(--dsw-alias-label-primary-foreground)); }
21
+ .dshAcSwitch:disabled { opacity: .5; cursor: default; }
22
+ .dshAcSwitch:focus-visible { outline: 2px solid var(--dsw-alias-brand-primary); outline-offset: 2px; }
9
23
  .dshAcCard {
10
24
  --dsh-ac-violet: #8b7cff;
11
25
  --dsh-ac-cyan: #45cce5;
@@ -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.subagent) return;
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, 'manual:notification', true);
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.subagent) return; // 子代理会话由父代理处理, 不抢跑
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.subagent) return; // 子代理会话由父代理处理, 不抢跑
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/index.ts CHANGED
@@ -74,6 +74,8 @@ export const AutoContinueSchema = z.object({
74
74
  notify: z.boolean().default(false),
75
75
  /** Globally pause auto-continue: no live or scan send. */
76
76
  paused: z.boolean().default(false),
77
+ /** Show the global auto-continue switch in the conversation composer. */
78
+ showComposerToggle: z.boolean().default(true),
77
79
  /** Loop guard: detect a running turn spinning in place and restart it. */
78
80
  loopGuard: z.boolean().default(true),
79
81
  /** A model message shorter than this many chars counts as a short sentence (loop signal). */
@@ -84,6 +84,8 @@ export interface AutoContinueSettings {
84
84
  notify?: boolean;
85
85
  /** Globally pause auto-continue: no live or scan send, queued pending sends cancelled. */
86
86
  paused?: boolean;
87
+ /** Show the global auto-continue switch in the conversation composer. */
88
+ showComposerToggle?: boolean;
87
89
  /** Loop guard: detect a running turn spinning in place (short talk without tools, or the same tool repeating) and restart it. */
88
90
  loopGuard?: boolean;
89
91
  /** A model message shorter than this many chars counts as a "short sentence" (loop signal). */
@@ -124,6 +126,7 @@ export const DEFAULT_CONFIG: AutoContinueConfig = {
124
126
  backoffMaxMs: 300000,
125
127
  notify: false,
126
128
  paused: false,
129
+ showComposerToggle: true,
127
130
  loopGuard: true,
128
131
  loopShortChars: 40,
129
132
  loopWindowMs: 30000,
@@ -196,6 +199,7 @@ export function resolveConfig(section: AutoContinueSettings | undefined): AutoCo
196
199
  backoffMaxMs: numberOr(value.backoffMaxMs, DEFAULT_CONFIG.backoffMaxMs),
197
200
  notify: booleanOr(value.notify, DEFAULT_CONFIG.notify),
198
201
  paused: booleanOr(value.paused, DEFAULT_CONFIG.paused),
202
+ showComposerToggle: booleanOr(value.showComposerToggle, DEFAULT_CONFIG.showComposerToggle),
199
203
  loopGuard: booleanOr(value.loopGuard, DEFAULT_CONFIG.loopGuard),
200
204
  loopShortChars: Math.max(1, numberOr(value.loopShortChars, DEFAULT_CONFIG.loopShortChars)),
201
205
  loopWindowMs: Math.max(1000, numberOr(value.loopWindowMs, DEFAULT_CONFIG.loopWindowMs)),
@@ -830,7 +834,7 @@ export interface SessionState {
830
834
  running: boolean | undefined;
831
835
  /** 当前排队消息数(来自 session/queue 帧)。 */
832
836
  queued: number;
833
- /** 子代理会话(host/session-added 带 parentSessionId)。 */
837
+ /** 子代理会话(session header origin is `subagent`). */
834
838
  subagent: boolean;
835
839
  /** 最近一次回合失败的事实(用于分类与模板填充)。 */
836
840
  lastFailure: FailureFacts | undefined;