dsh-question-timeout-protection 0.1.4 → 0.2.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/dist/index.mjs CHANGED
@@ -30,18 +30,18 @@ const TIMED_TIMEOUT_PARAMETER = "timeout";
30
30
  * 语义要点: 只说明本轮提问已超时、用户没有正式提交, 不对草稿做任何断言 ——
31
31
  * 草稿为空时不该告诉模型"草稿保留了下来"; 有草稿时草稿走 draft 字段随本结果一起回来。
32
32
  */
33
- const TIMEOUT_PENDING_NOTICE = "该轮提问已超时,用户没有正式提交; 请在回复末尾告知用户这一点。";
33
+ const TIMEOUT_PENDING_NOTICE = "该轮提问已超时,用户没有正式提交; 界面可能已经收起这张卡片, 也可能仍可作答 —— 用户之后补交的答复会作为一条用户消息送达, 请勿把超时本身或草稿当成用户的确认; 请在回复末尾告知用户这一点。";
34
34
  /**
35
35
  * 没有收到任何草稿快照时的说明。
36
36
  * 与「收到空草稿」不同: 后者表示用户确实没写, 前者是快照没送达, 模型不能据此断定用户没输入。
37
37
  */
38
- const TIMEOUT_PENDING_MISSING_NOTICE = "该轮提问已超时,用户没有正式提交; 本轮没有收到草稿快照(草稿通道不可达或页面未打开), 因此拿不到超时前的输入内容, 请不要据此断言用户没有输入, 并在回复末尾告知用户这次提问已超时。";
38
+ const TIMEOUT_PENDING_MISSING_NOTICE = "该轮提问已超时,用户没有正式提交; 本轮没有收到草稿快照(草稿通道不可达或页面未打开), 因此拿不到超时前的输入内容, 请不要据此断言用户没有输入; 界面可能已经收起这张卡片, 也可能仍可作答 —— 用户之后补交的答复会作为一条用户消息送达, 请勿把超时当成用户的确认; 并在回复末尾告知用户这次提问已超时。";
39
39
  /**
40
40
  * 有草稿时随 pending 结果返回给模型的说明。
41
41
  * 草稿本身放在 pending 结果的 draft 字段里, 这里只说明它的存在与用法,
42
42
  * 避免模型把整段说明当成草稿内容。
43
43
  */
44
- const TIMEOUT_PENDING_DRAFT_NOTICE = "该轮提问已超时,用户没有正式提交; 超时前留下的草稿已随本结果带回, 内容见 draft 字段, 请据此继续任务, 并在最后告知用户这次超时以及你对草稿的处理决策。";
44
+ const TIMEOUT_PENDING_DRAFT_NOTICE = "该轮提问已超时。用户未正式提交,但留下了一份草稿,详见 draft 字段。请自行判断草稿完整度、决定下一步事项。判断过程不得告知用户,在下一步事项之后总结报告对本次问答的决策结果。";
45
45
  /** 是否把超时草稿注入对话上下文 */
46
46
  function injectsContext(mode) {
47
47
  return mode === "context" || mode === "both";
@@ -53,10 +53,12 @@ const SYNC_PATH = "/question-timeout-protection/state";
53
53
  /** 外层程序未显式声明预算时的兜底值(毫秒), 与宿主 run_code 的缺省预算一致 */
54
54
  const DEFAULT_PTC_BUDGET_MS = 12e4;
55
55
  /** 在外层预算到期前预留的收尾时间(毫秒): 覆盖最多两次取草稿的宽限期(DRAFT_GRACE_MS)与结算竞态 */
56
- const PTC_BUDGET_MARGIN_MS = 7e3;
56
+ const PTC_BUDGET_MARGIN_MS = 8e3;
57
57
  /**
58
58
  * 外层预算不足以等待作答时的说明(模型侧)。
59
59
  * 这一轮没有真正等待, 用户也没看到问题, 因此必须让模型知道重试时要放宽外层预算。
60
+ * 该说明随工具错误返回, 不随 pending 结果: pending 会让宿主投影把调用转成可作答,
61
+ * 与"没有开始等待"矛盾。
60
62
  */
61
63
  const TIMEOUT_PENDING_BUDGET_NOTICE = "外层 run_code 的剩余执行预算不足以等待用户作答, 这次提问没有开始等待, 用户也没有看到问题; 需要提问时请把 run_code 的 timeoutMs 设为大于提问等待时长后重试。";
62
64
  /**
@@ -252,7 +254,7 @@ let truncationWarned = false;
252
254
  const MAX_DRAFT_SNAPSHOTS = 256;
253
255
  const MAX_TRACKED_CALLS = 512;
254
256
  const TRACKED_CALL_TTL_MS = 9e5;
255
- const SETTLE_MS = 600;
257
+ const SETTLE_MS = 1e3;
256
258
  const POLL_INTERVAL_MS = 100;
257
259
  function rememberInfiniteCall(callId) {
258
260
  if (infiniteCalls.size >= MAX_INFINITE_CALLS && !infiniteCalls.has(callId)) {
@@ -341,7 +343,10 @@ function sweepTrackedCalls() {
341
343
  draftReports.delete(callId);
342
344
  }
343
345
  for (const callId of callQuestionOrders.keys()) if (!callDeadlines.has(callId)) callQuestionOrders.delete(callId);
344
- for (const [callId, registeredAt] of infiniteCalls) if (now - registeredAt > TRACKED_CALL_TTL_MS) infiniteCalls.delete(callId);
346
+ for (const [callId, registeredAt] of infiniteCalls) {
347
+ if (callDeadlines.has(callId)) continue;
348
+ if (now - registeredAt > TRACKED_CALL_TTL_MS) infiniteCalls.delete(callId);
349
+ }
345
350
  while (callDeadlines.size > MAX_TRACKED_CALLS) {
346
351
  const oldest = callDeadlines.keys().next();
347
352
  if (oldest.done) break;
@@ -383,20 +388,29 @@ const LOOPBACK_HOSTS = /* @__PURE__ */ new Set([
383
388
  "localhost",
384
389
  "[::1]"
385
390
  ]);
386
- function isLoopbackHost(host) {
391
+ let allowedAuthority;
392
+ function authorityOf(origin) {
393
+ try {
394
+ return new URL(origin).host.toLowerCase();
395
+ } catch {
396
+ return;
397
+ }
398
+ }
399
+ function isTrustedHost(host) {
387
400
  if (host === void 0 || host === "") return false;
388
401
  try {
389
402
  const parsed = new URL("http://" + host);
390
403
  if (parsed.username !== "" || parsed.password !== "") return false;
391
404
  if (parsed.pathname !== "/" || parsed.search !== "" || parsed.hash !== "") return false;
392
- return LOOPBACK_HOSTS.has(parsed.hostname.toLowerCase());
405
+ if (LOOPBACK_HOSTS.has(parsed.hostname.toLowerCase())) return true;
406
+ return allowedAuthority !== void 0 && parsed.host.toLowerCase() === allowedAuthority;
393
407
  } catch {
394
408
  return false;
395
409
  }
396
410
  }
397
411
  function serveToken(request, response) {
398
412
  if (request.method !== "GET" || request.url !== "/question-timeout-protection/state-token") return false;
399
- if (!isLoopbackHost(request.headers.host)) {
413
+ if (!isTrustedHost(request.headers.host)) {
400
414
  response.writeHead(403);
401
415
  response.end();
402
416
  return true;
@@ -419,7 +433,7 @@ function serveToken(request, response) {
419
433
  return true;
420
434
  }
421
435
  function handleStateRequest(request, response) {
422
- if (!isLoopbackHost(request.headers.host)) {
436
+ if (!isTrustedHost(request.headers.host)) {
423
437
  response.writeHead(403);
424
438
  response.end();
425
439
  return;
@@ -581,6 +595,10 @@ function registerQuestionSyncRoute(webServer) {
581
595
  off();
582
596
  } catch {}
583
597
  };
598
+ try {
599
+ const origin = webServer.getOrigin?.();
600
+ if (typeof origin === "string" && origin !== "") allowedAuthority = authorityOf(origin);
601
+ } catch {}
584
602
  syncChannelReady = true;
585
603
  console.info("[question-timeout-protection] 状态同步路由已挂在宿主 HTTP 服务上: " + SYNC_PATH);
586
604
  }
@@ -593,18 +611,9 @@ function registerQuestionSyncRoute(webServer) {
593
611
  if (channelUsers > 0) return;
594
612
  detachRoutes?.();
595
613
  detachRoutes = void 0;
596
- clearSyncState();
614
+ syncChannelReady = false;
597
615
  };
598
616
  }
599
- function clearSyncState() {
600
- syncChannelReady = false;
601
- infiniteCalls.clear();
602
- draftReports.clear();
603
- draftReportedAt.clear();
604
- callDeadlines.clear();
605
- callQuestionOrders.clear();
606
- settledCalls.clear();
607
- }
608
617
  let syncChannelReady = false;
609
618
  let channelUsers = 0;
610
619
  let detachRoutes;
@@ -637,14 +646,23 @@ function isInfiniteWait(callId) {
637
646
  function clearInfiniteWait(callId) {
638
647
  infiniteCalls.delete(callId);
639
648
  }
640
- /** 清空状态通道里登记的全部状态 */
649
+ /** 卸载状态通道: 只摘路由与就绪标志, 登记表留给 TTL 与容量上限回收 */
641
650
  function stopQuestionSync() {
642
651
  if (channelUsers > 0) return;
643
- clearSyncState();
652
+ detachRoutes?.();
653
+ detachRoutes = void 0;
654
+ syncChannelReady = false;
644
655
  }
645
656
  // src/question/tool.ts
646
- const TOOL_DESCRIPTION = "Ask brief, direct, self-contained questions about missing information, preferences, or decisions. Use user-facing terms; assume no knowledge of background work or internal names. Use distinct stable question IDs. A timed call that expires before an answer batch returns a pending result: that round has already been settled as timed out, and an answer submitted later is not adopted by this tool, so decide yourself whether to ask again. A timed call made from inside run_code is additionally bounded by that program's elapsed-time budget: keep the enclosing run_code timeoutMs above this timeout, otherwise the question is aborted and no draft can be returned.";
657
+ const TOOL_DESCRIPTION = "Ask brief, direct, self-contained questions about missing information, preferences, or decisions. Use user-facing terms; assume no knowledge of background work or internal names. Use distinct stable question IDs. Pass timeout as a top-level argument of this call, never inside a question item. An answer item with empty selected and no custom means the user skipped that question: continue without it, do not ask it again, and do not treat the skip as permission. A timed call that expires before an answer batch returns a pending result: that round timed out, the card may be closed or still answerable, and an answer the user submits later arrives as a separate user message, so never treat the pending result or its draft as a confirmed answer. A timed call made from inside run_code is additionally bounded by that program's elapsed-time budget: keep the enclosing run_code timeoutMs above this timeout, otherwise the question is rejected without being shown and no draft can be returned.";
647
658
  const QUESTIONS_DESCRIPTION = "Questions to ask the user.";
659
+ function nestedTimeout(questions) {
660
+ for (const item of questions) {
661
+ if (item === null || typeof item !== "object") continue;
662
+ const value = item.timeout;
663
+ if (typeof value === "number" && Number.isInteger(value) && value >= 1 && value <= 2147483) return value;
664
+ }
665
+ }
648
666
  function resolveTimeout(requested, fallback) {
649
667
  const raw = requested ?? fallback;
650
668
  if (raw === -1) return -1;
@@ -795,7 +813,7 @@ function createAskUserQuestionTool(ctx, config) {
795
813
  message: {
796
814
  type: "string",
797
815
  required: true,
798
- description: "How to continue while the questions stay answerable."
816
+ description: "How to continue after the timeout; the card may be closed or still answerable."
799
817
  },
800
818
  draftMissing: {
801
819
  type: "boolean",
@@ -804,7 +822,7 @@ function createAskUserQuestionTool(ctx, config) {
804
822
  },
805
823
  draft: {
806
824
  type: "array",
807
- description: "Unsubmitted draft the user had entered when the wait expired; one item per question, empty when there was no draft.",
825
+ description: "Unsubmitted draft the user had entered when the wait expired. It contains only questions that had a selection or typed text; a question missing from this list was not answered before the timeout, which is not the same as the user skipping it.",
808
826
  items: {
809
827
  type: "object",
810
828
  additionalProperties: false,
@@ -818,7 +836,7 @@ function createAskUserQuestionTool(ctx, config) {
818
836
  type: "array",
819
837
  required: true,
820
838
  items: { type: "string" },
821
- description: "Option labels the user had selected; empty with no custom means the question was left blank."
839
+ description: "Option labels the user had selected for this question."
822
840
  },
823
841
  custom: {
824
842
  type: "string",
@@ -864,7 +882,7 @@ function createAskUserQuestionTool(ctx, config) {
864
882
  }]
865
883
  },
866
884
  async execute(args, exec) {
867
- const timeoutSeconds = resolveTimeout(args.timeout, configValue(config.timeoutSeconds) ?? 120);
885
+ const timeoutSeconds = resolveTimeout(args.timeout ?? nestedTimeout(args.questions), configValue(config.timeoutSeconds) ?? 120);
868
886
  const infiniteEnabled = configValue(config.infiniteWait) ?? true;
869
887
  const draftFallbackEnabled = configValue(config.draftFallback) ?? true;
870
888
  const injectsDraft = injectsContext(configValue(config.fallback) ?? "both");
@@ -911,11 +929,7 @@ function createAskUserQuestionTool(ctx, config) {
911
929
  const parentCallId = exec.rootCallId === exec.callId ? void 0 : exec.rootCallId;
912
930
  const parentRemaining = parentCallId === void 0 ? void 0 : runBudgetRemaining(parentCallId);
913
931
  const usableMs = parentRemaining === void 0 ? void 0 : parentRemaining - PTC_BUDGET_MARGIN_MS;
914
- if (usableMs !== void 0 && usableMs < 2e3) return {
915
- pending: true,
916
- callId: exec.callId,
917
- message: TIMEOUT_PENDING_BUDGET_NOTICE
918
- };
932
+ if (usableMs !== void 0 && usableMs < 2e3) throw new Error(TIMEOUT_PENDING_BUDGET_NOTICE);
919
933
  const timeoutMs = usableMs === void 0 ? requestedTimeoutMs : Math.min(requestedTimeoutMs, usableMs);
920
934
  registerCallDeadline(exec.callId, Date.now() + timeoutMs);
921
935
  registerCallQuestions(exec.callId, questions.map((question) => question.id));
@@ -1007,7 +1021,7 @@ function createAskUserQuestionTool(ctx, config) {
1007
1021
  const selectedChars = selected.reduce((sum, label) => sum + label.length, 0);
1008
1022
  if (selectedChars > questionLimit) {
1009
1023
  truncated = true;
1010
- break;
1024
+ continue;
1011
1025
  }
1012
1026
  const room = questionLimit - selectedChars;
1013
1027
  const keptCustom = custom.length > room ? custom.slice(0, room) : custom;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-question-timeout-protection",
3
- "version": "0.1.4",
3
+ "version": "0.2.0",
4
4
  "description": "dsh提问超时保护插件: 一旦模型提问超时,将你的回答草稿保留在UI或上下文中,而不是直接丢弃。",
5
5
  "type": "module",
6
6
  "author": "JularDepick",
@@ -11,7 +11,7 @@
11
11
  },
12
12
  "engines": {
13
13
  "node": ">=20",
14
- "dsh": ">=0.2.1-rc.1 <0.2.3-rc.1"
14
+ "dsh": ">=0.2.1-rc.1 <0.2.2-rc.0 || >=0.2.2-rc.0 <0.2.3-rc.1"
15
15
  },
16
16
  "main": "./dist/index.mjs",
17
17
  "types": "./dist/index.d.mts",
@@ -39,10 +39,10 @@
39
39
  "scripts": {
40
40
  "build": "tsdown",
41
41
  "dev": "tsdown --watch",
42
- "pack:release": "node scripts/check-translation.mjs && tsdown && pnpm pack --ignore-scripts && node scripts/pack-release.mjs --strict",
42
+ "pack:release": "node scripts/check-translation.mjs --strict && tsdown && pnpm pack --ignore-scripts && node scripts/pack-release.mjs --strict && node scripts/verify-tarball.mjs",
43
43
  "prepare": "node scripts/check-translation.mjs && tsdown",
44
- "prepack": "node scripts/check-translation.mjs && tsdown",
45
- "postpack": "node scripts/pack-release.mjs",
44
+ "prepack": "node scripts/check-translation.mjs --strict && tsdown",
45
+ "postpack": "node scripts/pack-release.mjs && node scripts/verify-tarball.mjs --if-present",
46
46
  "typecheck": "tsc --noEmit",
47
47
  "check:translation": "node scripts/check-translation.mjs"
48
48
  },
@@ -51,9 +51,9 @@
51
51
  },
52
52
  "peerDependencies": {
53
53
  "@deepseek-ai/cordis": "~4.0.5 || ~4.1.0",
54
- "@deepseek-ai/dsh-agent": ">=0.2.1-rc.1 <0.2.3-rc.1",
55
- "@deepseek-ai/dsh-tools": ">=0.2.1-rc.1 <0.2.3-rc.1",
56
- "@deepseek-ai/dsh-user-questions": ">=0.2.1-rc.1 <0.2.3-rc.1"
54
+ "@deepseek-ai/dsh-agent": ">=0.2.1-rc.1 <0.2.2-rc.0 || >=0.2.2-rc.0 <0.2.3-rc.1",
55
+ "@deepseek-ai/dsh-tools": ">=0.2.1-rc.1 <0.2.2-rc.0 || >=0.2.2-rc.0 <0.2.3-rc.1",
56
+ "@deepseek-ai/dsh-user-questions": ">=0.2.1-rc.1 <0.2.2-rc.0 || >=0.2.2-rc.0 <0.2.3-rc.1"
57
57
  },
58
58
  "devDependencies": {
59
59
  "@deepseek-ai/cordis": "~4.1.0",
@@ -1,8 +1,8 @@
1
1
  [meta]
2
- for=v0.1.4
2
+ for=v0.2.0
3
3
  lang=zh-CN
4
4
  name=简体中文
5
- endtime=2026-11-15 14:20:00+08:00
5
+ endtime=2026-11-25 17:20:00+08:00
6
6
 
7
7
  [translation]
8
8
  settings.settingsSummary=提问超时保护
@@ -31,6 +31,8 @@ control.infiniteWait=无限等待
31
31
  control.autoInfiniteLabel=问答自动无限等待
32
32
  control.autoInfiniteOn=开
33
33
  control.autoInfiniteOff=关
34
+ control.customRadio=自定义回答
35
+ control.customHint=已保留自定义内容: {text}(点这里选用)
34
36
  control.autoInfiniteTitle=开启后每次提问都自动进入无限等待, 关闭后回到插件的计时行为
35
37
  bubble.draftLabel=提问·超时草稿
36
38
  draft.selected=已选: {items}
@@ -1,8 +1,8 @@
1
1
  [meta]
2
- for=v0.1.4
2
+ for=v0.2.0
3
3
  lang=en-US
4
4
  name=English (US)
5
- endtime=2026-11-15 14:20:00+08:00
5
+ endtime=2026-11-25 17:20:00+08:00
6
6
 
7
7
  [translation]
8
8
  settings.settingsSummary=Question timeout protection
@@ -31,6 +31,8 @@ control.infiniteWait=Wait indefinitely
31
31
  control.autoInfiniteLabel=Auto wait indefinitely
32
32
  control.autoInfiniteOn=On
33
33
  control.autoInfiniteOff=Off
34
+ control.customRadio=Custom answer
35
+ control.customHint=Custom answer kept: {text} (click to use it)
34
36
  control.autoInfiniteTitle=When on, every question enters indefinite wait automatically; when off, the plugin countdown applies
35
37
  bubble.draftLabel=Question · timed-out draft
36
38
  draft.selected=Selected: {items}
@@ -1,8 +1,8 @@
1
1
  [meta]
2
- for=v0.1.4
2
+ for=v0.2.0
3
3
  lang=zh-CN
4
4
  name=简体中文
5
- endtime=2026-11-15 14:20:00+08:00
5
+ endtime=2026-11-25 17:20:00+08:00
6
6
 
7
7
  [translation]
8
8
  settings.settingsSummary=提问超时保护
@@ -31,6 +31,8 @@ control.infiniteWait=无限等待
31
31
  control.autoInfiniteLabel=问答自动无限等待
32
32
  control.autoInfiniteOn=开
33
33
  control.autoInfiniteOff=关
34
+ control.customRadio=自定义回答
35
+ control.customHint=已保留自定义内容: {text}(点这里选用)
34
36
  control.autoInfiniteTitle=开启后每次提问都自动进入无限等待, 关闭后回到插件的计时行为
35
37
  bubble.draftLabel=提问·超时草稿
36
38
  draft.selected=已选: {items}