dsh-client-auto-continue 0.15.0 → 0.16.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.
package/lib/client.js CHANGED
@@ -37,6 +37,10 @@ __export(index_exports, {
37
37
  module.exports = __toCommonJS(index_exports);
38
38
 
39
39
  // src/shared/core.ts
40
+ var isSessionMode = (value) => value === "default" || value === "off" || value === "recover" || value === "loop";
41
+ function sessionMode(modes, sessionId) {
42
+ return Object.prototype.hasOwnProperty.call(modes, sessionId) && isSessionMode(modes[sessionId]) ? modes[sessionId] : "default";
43
+ }
40
44
  var LOCALIZED_TEXT_DEFAULTS = {
41
45
  zh: {
42
46
  continueText: "继续",
@@ -79,6 +83,7 @@ var DEFAULT_CONFIG = {
79
83
  backoffMaxMs: 3e5,
80
84
  notify: false,
81
85
  paused: false,
86
+ sessionModes: {},
82
87
  showComposerToggle: true,
83
88
  loopGuard: true,
84
89
  loopShortChars: 40,
@@ -121,11 +126,17 @@ var zh = {
121
126
  "category.safety.description": "避免重复执行工具,识别并打断无效循环。",
122
127
  "category.status.title": "状态与日志",
123
128
  "category.status.description": "查看活动记录与暂停的会话。",
124
- "field.paused": "自动继续",
125
- "field.showComposerToggle": "在会话区显示自动继续开关",
126
- "field.showComposerToggleHint": "默认显示。隐藏开关不会暂停自动继续;点击保存后生效。",
129
+ "field.paused": "全局启用自动继续",
130
+ "field.showComposerToggle": "在会话区显示模式选择",
131
+ "field.showComposerToggleHint": "默认显示。隐藏控件不会改变会话模式;点击保存后生效。",
127
132
  "composer.label": "自动继续",
128
- "composer.scope": "控制当前配置下所有会话的自动继续,修改立即保存。",
133
+ "composer.scope": "只控制当前会话,立即保存并同步到其他标签页。关闭后也不再自动打断循环。",
134
+ "composer.default": "跟随默认",
135
+ "composer.off": "关闭",
136
+ "composer.recover": "仅故障恢复",
137
+ "composer.loop": "自主循环",
138
+ "composer.paused": "全局已暂停",
139
+ "composer.unavailable": "等待宿主连接",
129
140
  "composer.saving": "正在保存自动继续设置…",
130
141
  "composer.saveFailed": "保存失败,请重试",
131
142
  "field.pausedHint": "恢复意外中断的会话。关闭并保存后,全局暂停恢复并取消已排队的继续。循环守卫独立运行;如需停止自动打断,请同时关闭循环守卫。",
@@ -137,8 +148,8 @@ var zh = {
137
148
  "default.continueTextMaxTokens": LOCALIZED_TEXT_DEFAULTS.zh.continueTextMaxTokens,
138
149
  "field.resumeSilentTurns": "续跑无输出回合",
139
150
  "field.resumeSilentTurnsHint": "已观察到模型活动但没有可见输出时续跑;不把空回合、图片或扩展内容误判为静默,冷却与连续上限照常生效。",
140
- "field.resumeCompletedTurns": "自主循环(每个回合后都续跑)",
141
- "field.resumeCompletedTurnsHint": "正常完成后经过宽限期自动续跑,不受恢复冷却和次数上限限制;仅有推理或显式无输出的回合仍消耗重试预算。暂停或关闭该选项可停止后续接力,手动停止或发送消息会取消当前待发送的继续提示。",
151
+ "field.resumeCompletedTurns": "默认会话模式",
152
+ "field.resumeCompletedTurnsHint": "用于没有单独设置模式的会话。仅故障恢复不会接力正常完成的回合;自主循环会在正常完成后继续。无输出和失败回合仍受重试预算约束。",
142
153
  "field.continueTextLoop": "自主循环的继续文本",
143
154
  "field.continueTextLoopHint": "自主循环每次续跑时发送的文本。",
144
155
  "default.continueTextLoop": LOCALIZED_TEXT_DEFAULTS.zh.continueTextLoop,
@@ -268,11 +279,17 @@ var en = {
268
279
  "category.safety.description": "Avoid duplicate tool actions and break unproductive loops.",
269
280
  "category.status.title": "Status and logs",
270
281
  "category.status.description": "View activity and paused sessions.",
271
- "field.paused": "Auto-continue",
272
- "field.showComposerToggle": "Show auto-continue switch in the composer",
273
- "field.showComposerToggleHint": "Shown by default. Hiding the switch does not pause auto-continue. Click Save to apply.",
282
+ "field.paused": "Enable auto-continue globally",
283
+ "field.showComposerToggle": "Show session mode in the composer",
284
+ "field.showComposerToggleHint": "Shown by default. Hiding the control does not change session modes. Click Save to apply.",
274
285
  "composer.label": "Auto-continue",
275
- "composer.scope": "Controls auto-continue for all sessions in this profile. Changes save immediately.",
286
+ "composer.scope": "Only affects this session. Saved immediately and synced across tabs. Off also disables loop-guard interruptions here.",
287
+ "composer.default": "Follow default",
288
+ "composer.off": "Off",
289
+ "composer.recover": "Recovery only",
290
+ "composer.loop": "Autonomous loop",
291
+ "composer.paused": "Globally paused",
292
+ "composer.unavailable": "Waiting for host",
276
293
  "composer.saving": "Saving auto-continue setting…",
277
294
  "composer.saveFailed": "Could not save. Try again.",
278
295
  "field.pausedHint": "Resume interrupted sessions. Turn off and save to pause recovery globally and cancel queued continuations. The loop guard stays active; turn it off separately to stop automatic interruptions.",
@@ -284,8 +301,8 @@ var en = {
284
301
  "default.continueTextMaxTokens": LOCALIZED_TEXT_DEFAULTS.en.continueTextMaxTokens,
285
302
  "field.resumeSilentTurns": "Resume silent turns",
286
303
  "field.resumeSilentTurnsHint": "Resume observed model activity without visible output. No-op turns, images and extension blocks are not treated as silent; cooldown and retry limits still apply.",
287
- "field.resumeCompletedTurns": "Autonomous loop (continue after every turn)",
288
- "field.resumeCompletedTurnsHint": "Continue normal completions after grace, without the recovery cooldown or cap. Reasoning-only and explicit silent endings still consume the retry budget. Pause or disable this option to stop future handoffs; Stop or a manual message cancels the queued continuation.",
304
+ "field.resumeCompletedTurns": "Default session mode",
305
+ "field.resumeCompletedTurnsHint": "Applies to sessions without their own mode. Recovery only leaves normal completions alone; autonomous loop continues after them. Silent and failed turns still consume the retry budget.",
289
306
  "field.continueTextLoop": "Continue text (autonomous loop)",
290
307
  "field.continueTextLoopHint": "Text sent on each autonomous-loop continuation.",
291
308
  "default.continueTextLoop": LOCALIZED_TEXT_DEFAULTS.en.continueTextLoop,
@@ -451,6 +468,21 @@ function readTodayStats() {
451
468
  function readRecoveryAttempts() {
452
469
  return state.recoveryAttempts;
453
470
  }
471
+ function readSessionPolicy() {
472
+ return state.sessionPolicy;
473
+ }
474
+ async function setSessionMode(sessionId, mode) {
475
+ if (!state.sessionPolicy?.writable) throw new Error("Session settings are unavailable");
476
+ const response = await fetch("/api/auto-continue-action", {
477
+ method: "POST",
478
+ headers: { "content-type": "application/json" },
479
+ body: JSON.stringify({ action: "set-session-mode", sessionId, mode })
480
+ });
481
+ const result = await response.json();
482
+ if (!response.ok || result?.ok !== true || result.sessionId !== sessionId || result.mode !== mode) {
483
+ throw new Error("Session mode was not saved");
484
+ }
485
+ }
454
486
  function resetTodayStats() {
455
487
  void postAction({ action: "reset-stats" });
456
488
  }
@@ -475,10 +507,12 @@ async function postAction(payload) {
475
507
  }
476
508
  function handleEvent(event) {
477
509
  if (event.type === "state") {
510
+ const policy = event.sessionPolicy;
478
511
  state = {
479
512
  stats: event.stats ?? EMPTY_STATS,
480
513
  paused: event.paused ?? [],
481
- recoveryAttempts: event.recoveryAttempts ?? []
514
+ recoveryAttempts: event.recoveryAttempts ?? [],
515
+ sessionPolicy: policy && typeof policy.modes === "object" && policy.modes !== null && !Array.isArray(policy.modes) && (policy.defaultMode === "recover" || policy.defaultMode === "loop") && typeof policy.paused === "boolean" && typeof policy.writable === "boolean" ? policy : void 0
482
516
  };
483
517
  for (const listener of listeners) listener();
484
518
  } else if (event.type === "notice" && event.notice !== void 0) {
@@ -522,6 +556,11 @@ function showNotification(notice) {
522
556
  function startBridge() {
523
557
  let stopped = false;
524
558
  let controller;
559
+ const disconnected = () => {
560
+ state = { ...state, sessionPolicy: void 0 };
561
+ for (const listener of listeners) listener();
562
+ };
563
+ disconnected();
525
564
  const loop = async () => {
526
565
  while (!stopped) {
527
566
  controller = new AbortController();
@@ -533,7 +572,7 @@ function startBridge() {
533
572
  let buffer = "";
534
573
  for (; ; ) {
535
574
  const { done, value } = await reader.read();
536
- if (done) break;
575
+ if (done || stopped) break;
537
576
  buffer += decoder.decode(value, { stream: true });
538
577
  let idx = buffer.indexOf("\n\n");
539
578
  while (idx !== -1) {
@@ -552,13 +591,17 @@ function startBridge() {
552
591
  }
553
592
  } catch {
554
593
  }
555
- if (!stopped) await new Promise((resolve) => setTimeout(resolve, 3e3));
594
+ if (!stopped) {
595
+ disconnected();
596
+ await new Promise((resolve) => setTimeout(resolve, 3e3));
597
+ }
556
598
  }
557
599
  };
558
600
  void loop();
559
601
  return () => {
560
602
  stopped = true;
561
603
  controller?.abort();
604
+ disconnected();
562
605
  };
563
606
  }
564
607
 
@@ -764,7 +807,10 @@ var CardForm = class {
764
807
 
765
808
  // src/client/styles.ts
766
809
  var css = `
767
- .dshAcComposer { display: inline-flex; align-items: center; gap: 7px; min-height: 32px; flex: 0 0 auto; font: inherit; }
810
+ .dshAcComposer { display: inline-flex; align-items: center; flex-wrap: wrap; gap: 7px; min-height: 32px; max-width: 100%; font: inherit; }
811
+ .dshAcMode { min-height: 32px; max-width: 100%; padding: 4px 6px; border: 1px solid var(--dsw-alias-border-l2); border-radius: 6px; background: var(--dsw-alias-bg-base); color: var(--dsw-alias-label-primary); font: inherit; font-size: 12px; }
812
+ .dshAcMode:disabled { opacity: .5; }
813
+ .dshAcMode:focus-visible { outline: 2px solid var(--dsw-alias-brand-primary); outline-offset: 2px; }
768
814
  .dshAcComposerLabel { color: var(--dsw-alias-label-secondary); font-size: 12px; white-space: nowrap; }
769
815
  .dshAcSwitchTarget { display: inline-flex; align-items: center; min-height: 32px; }
770
816
  .dshAcSwitchTarget > button { position: relative; }
@@ -953,8 +999,8 @@ var AutoContinueSettingsCardController = class {
953
999
  constructor(scope, getLocale) {
954
1000
  this.scope = scope;
955
1001
  this.getLocale = getLocale;
956
- this.composerSaving = false;
957
- this.composerFailed = false;
1002
+ this.composerSaving = /* @__PURE__ */ new Set();
1003
+ this.composerFailed = /* @__PURE__ */ new Set();
958
1004
  this.disposed = false;
959
1005
  this.saveSequence = 0;
960
1006
  this.form = new CardForm(scope, [
@@ -992,6 +1038,7 @@ var AutoContinueSettingsCardController = class {
992
1038
  ]);
993
1039
  this.store = this.form.bind(() => this.projection(), createSnapshotStore);
994
1040
  this.composer = this.form.bind(() => this.composerProjection(), createSnapshotStore);
1041
+ this.stopComposer = subscribeBridge(() => this.composer.set(this.composerProjection()));
995
1042
  }
996
1043
  projection() {
997
1044
  return {
@@ -1055,37 +1102,38 @@ var AutoContinueSettingsCardController = class {
1055
1102
  this.notificationFeedback = showSettingsNotification(before === after ? "saved" : after ? "enabled" : "disabled", this.getLocale(), result);
1056
1103
  this.store.set(this.projection());
1057
1104
  }
1058
- injectComposer() {
1059
- return { hooks: { autoContinueComposer: this.composer }, setEnabled: (enabled) => void this.setEnabled(enabled) };
1105
+ injectComposer(sessionId) {
1106
+ return { hooks: { autoContinueComposer: this.composer }, sessionId, setMode: (mode) => void this.setMode(sessionId, mode) };
1060
1107
  }
1061
1108
  composerProjection() {
1062
1109
  const snapshot = this.scope.getSnapshot();
1110
+ const policy = readSessionPolicy();
1063
1111
  return {
1064
1112
  visible: snapshot.status === "ready" && (snapshot.value?.showComposerToggle ?? DEFAULT_CONFIG.showComposerToggle),
1065
- enabled: !(snapshot.value?.paused ?? DEFAULT_CONFIG.paused),
1066
- writable: canWriteSettings(snapshot),
1067
- saving: this.composerSaving,
1068
- failed: this.composerFailed
1113
+ policy,
1114
+ writable: canWriteSettings(snapshot) && policy?.writable === true,
1115
+ saving: new Set(this.composerSaving),
1116
+ failed: new Set(this.composerFailed)
1069
1117
  };
1070
1118
  }
1071
- async setEnabled(enabled) {
1072
- if (this.disposed || this.composerSaving || !this.composerProjection().writable) return;
1073
- this.composerSaving = true;
1074
- this.composerFailed = false;
1119
+ async setMode(sessionId, mode) {
1120
+ if (this.disposed || !sessionId || this.composerSaving.has(sessionId) || !this.composerProjection().writable) return;
1121
+ this.composerSaving.add(sessionId);
1122
+ this.composerFailed.delete(sessionId);
1075
1123
  this.composer.set(this.composerProjection());
1076
1124
  try {
1077
- const result = await this.scope.set("paused", !enabled);
1078
- this.composerFailed = result === false || this.composerProjection().enabled !== enabled;
1125
+ await setSessionMode(sessionId, mode);
1079
1126
  } catch {
1080
- this.composerFailed = true;
1127
+ this.composerFailed.add(sessionId);
1081
1128
  } finally {
1082
- this.composerSaving = false;
1129
+ this.composerSaving.delete(sessionId);
1083
1130
  if (!this.disposed) this.composer.set(this.composerProjection());
1084
1131
  }
1085
1132
  }
1086
1133
  /** Release this card's subscription to the provider-owned settings form. */
1087
1134
  dispose() {
1088
1135
  this.disposed = true;
1136
+ this.stopComposer();
1089
1137
  this.form.dispose();
1090
1138
  }
1091
1139
  };
@@ -1145,7 +1193,22 @@ function SettingField({ name, id, state: state2, actions, disabled, t }) {
1145
1193
  ] }),
1146
1194
  /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "dshAcFieldControl", children: [
1147
1195
  state2.overridden && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("button", { type: "button", className: "dshAcReset", disabled, "aria-label": `${t("chrome.reset")}: ${label}`, onClick: () => actions.resetField(name), children: t("chrome.reset") }),
1148
- boolean ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "dshAcSwitchTarget", children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
1196
+ name === "resumeCompletedTurns" ? /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
1197
+ "select",
1198
+ {
1199
+ id,
1200
+ className: "dshAcMode",
1201
+ "aria-label": label,
1202
+ "aria-describedby": `${id}-hint`,
1203
+ disabled,
1204
+ value: String(value),
1205
+ onChange: (event) => actions.edit(name, event.target.value),
1206
+ children: [
1207
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("option", { value: "false", children: t("composer.recover") }),
1208
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("option", { value: "true", children: t("composer.loop") })
1209
+ ]
1210
+ }
1211
+ ) : boolean ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "dshAcSwitchTarget", children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
1149
1212
  NativeSwitch,
1150
1213
  {
1151
1214
  label,
@@ -1376,21 +1439,36 @@ function AutoContinueSettingsCard(props) {
1376
1439
  var import_jsx_runtime3 = require("react/jsx-runtime");
1377
1440
  function AutoContinueComposerToggle(props) {
1378
1441
  const state2 = props.useAutoContinueComposer((snapshot) => snapshot);
1379
- if (!state2.visible) return null;
1380
- const title = props.t(!state2.writable ? "chrome.readOnly" : state2.saving ? "composer.saving" : "composer.scope");
1442
+ if (!state2.visible || !props.sessionId) return null;
1443
+ const saving = state2.saving.has(props.sessionId);
1444
+ const policy = state2.policy;
1445
+ const title = props.t(!policy ? "composer.unavailable" : !state2.writable ? "chrome.readOnly" : saving ? "composer.saving" : "composer.scope");
1381
1446
  return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "dshAcComposer", title, children: [
1382
1447
  /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "dshAcComposerLabel", children: props.t("composer.label") }),
1383
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "dshAcSwitchTarget", children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1384
- NativeSwitch,
1448
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
1449
+ "select",
1385
1450
  {
1386
- checked: state2.enabled,
1387
- onChange: props.setEnabled,
1388
- disabled: !state2.writable || state2.saving,
1389
- label: props.t("composer.label"),
1390
- title
1451
+ className: "dshAcMode",
1452
+ "aria-label": props.t("composer.label"),
1453
+ title,
1454
+ disabled: !state2.writable || saving,
1455
+ value: sessionMode(policy?.modes ?? {}, props.sessionId),
1456
+ onChange: (event) => {
1457
+ if (isSessionMode(event.target.value)) props.setMode(event.target.value);
1458
+ },
1459
+ children: [
1460
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("option", { value: "default", children: [
1461
+ props.t("composer.default"),
1462
+ policy ? ` (${props.t(`composer.${policy.defaultMode}`)})` : ""
1463
+ ] }),
1464
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("option", { value: "off", children: props.t("composer.off") }),
1465
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("option", { value: "recover", children: props.t("composer.recover") }),
1466
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("option", { value: "loop", children: props.t("composer.loop") })
1467
+ ]
1391
1468
  }
1392
- ) }),
1393
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "dshAcComposerError", role: "status", children: state2.failed ? props.t("composer.saveFailed") : null })
1469
+ ),
1470
+ policy?.paused && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "dshAcComposerLabel", children: props.t("composer.paused") }),
1471
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "dshAcComposerError", role: "status", children: state2.failed.has(props.sessionId) ? props.t("composer.saveFailed") : saving ? props.t("composer.saving") : !policy ? props.t("composer.unavailable") : null })
1394
1472
  ] });
1395
1473
  }
1396
1474
 
@@ -1438,7 +1516,7 @@ function mountSettings(ctx, scope) {
1438
1516
  name: "conversation.input.left",
1439
1517
  id: "auto-continue",
1440
1518
  locale: NS,
1441
- inject: () => controller.injectComposer()
1519
+ inject: (sessionId) => controller.injectComposer(sessionId)
1442
1520
  },
1443
1521
  AutoContinueComposerToggle
1444
1522
  )