cc-viewer 1.9.23 → 1.9.24
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/LICENSE.md +28 -0
- package/README.md +1 -1
- package/dist/assets/App-BwRRqrP5.css +1 -0
- package/dist/assets/App-Cfmm41z0.js +2 -0
- package/dist/assets/ArchivePreview-BKXTsPsN.css +1 -0
- package/dist/assets/ArchivePreview-dKWXN9Zp.js +1 -0
- package/dist/assets/{MdxEditorPanel-CTAS2DJv.js → MdxEditorPanel-CT6w8hjl.js} +1 -1
- package/dist/assets/Mobile-noWViQpL.js +1 -0
- package/dist/assets/{ProxyStatsModal-C4sl-cuB.js → ProxyStatsModal-BeCRPqaC.js} +1 -1
- package/dist/assets/{_baseUniq-CsF04anE.js → _baseUniq-BGBEhBne.js} +1 -1
- package/dist/assets/{arc-BOacYe6N.js → arc-4pbWOEmh.js} +1 -1
- package/dist/assets/{architectureDiagram-Q4EWVU46-BSUEfjC_.js → architectureDiagram-Q4EWVU46-BHZPDnby.js} +1 -1
- package/dist/assets/{blockDiagram-DXYQGD6D-CC37UGtW.js → blockDiagram-DXYQGD6D-DszLU7Pg.js} +1 -1
- package/dist/assets/{c4Diagram-AHTNJAMY-IzgRD4uO.js → c4Diagram-AHTNJAMY-DbDJNYxl.js} +1 -1
- package/dist/assets/{channel-C6PxyQUn.js → channel-BpIsxDHj.js} +1 -1
- package/dist/assets/{chunk-4BX2VUAB-cdQt_dcU.js → chunk-4BX2VUAB-ChcBwBh2.js} +1 -1
- package/dist/assets/{chunk-4TB4RGXK-BpTDA_Pb.js → chunk-4TB4RGXK-DzBEAck_.js} +1 -1
- package/dist/assets/{chunk-55IACEB6-CcitNo0P.js → chunk-55IACEB6-9UDvmw07.js} +1 -1
- package/dist/assets/{chunk-EDXVE4YY-BjKwZ9I7.js → chunk-EDXVE4YY-DHEU2DKg.js} +1 -1
- package/dist/assets/{chunk-FMBD7UC4-vZDVX3tI.js → chunk-FMBD7UC4-sysT0Sy2.js} +1 -1
- package/dist/assets/{chunk-OYMX7WX6-DGkYahGo.js → chunk-OYMX7WX6-DLXt2vYo.js} +1 -1
- package/dist/assets/{chunk-QZHKN3VN-C9065hSE.js → chunk-QZHKN3VN-CDIkC7QK.js} +1 -1
- package/dist/assets/{chunk-YZCP3GAM-D_pcTg6_.js → chunk-YZCP3GAM-D9b_INz7.js} +1 -1
- package/dist/assets/classDiagram-6PBFFD2Q-4Lh0SB4X.js +1 -0
- package/dist/assets/classDiagram-v2-HSJHXN6E-4Lh0SB4X.js +1 -0
- package/dist/assets/clone-cWiXtXrO.js +1 -0
- package/dist/assets/{cose-bilkent-S5V4N54A-BPPFMIGv.js → cose-bilkent-S5V4N54A-DJQwV_Dp.js} +1 -1
- package/dist/assets/{dagre-KV5264BT-fjzhKO5o.js → dagre-KV5264BT-bl9Pq21t.js} +1 -1
- package/dist/assets/{diagram-5BDNPKRD-5L6FzTry.js → diagram-5BDNPKRD-BA8CJxzI.js} +1 -1
- package/dist/assets/{diagram-G4DWMVQ6-GOwcGpff.js → diagram-G4DWMVQ6-DzSQULuz.js} +1 -1
- package/dist/assets/{diagram-MMDJMWI5-BDsFE25W.js → diagram-MMDJMWI5-DhfHWnT4.js} +1 -1
- package/dist/assets/{diagram-TYMM5635-BqjpbSGH.js → diagram-TYMM5635-BbAoFfGS.js} +1 -1
- package/dist/assets/{erDiagram-SMLLAGMA-DWO3Dk1v.js → erDiagram-SMLLAGMA-CMjA2eBk.js} +1 -1
- package/dist/assets/{flowDiagram-DWJPFMVM-Br4nNBVd.js → flowDiagram-DWJPFMVM-bOubqgJ6.js} +1 -1
- package/dist/assets/{ganttDiagram-T4ZO3ILL-9sN-VOhw.js → ganttDiagram-T4ZO3ILL-BUIu7WwI.js} +1 -1
- package/dist/assets/{gitGraphDiagram-UUTBAWPF-BsPcTAKr.js → gitGraphDiagram-UUTBAWPF-B_vrztDU.js} +1 -1
- package/dist/assets/{graph-o31GzaB6.js → graph-BR3YGx52.js} +1 -1
- package/dist/assets/{index-DAjx9lT3.js → index-B88yR2TU.js} +1 -1
- package/dist/assets/{index-PDvAAbti.js → index-BMpUfdYI.js} +1 -1
- package/dist/assets/{index-DyV81jSF.js → index-BQx5MOpr.js} +1 -1
- package/dist/assets/index-Ca3a2tOc.js +2 -0
- package/dist/assets/{index-CuvDJTY7.js → index-ClTuiPoz.js} +1 -1
- package/dist/assets/{index-tZqiJJGq.js → index-DtZAmvwJ.js} +1 -1
- package/dist/assets/{index-B3b0lRUB.js → index-DwMyfQJq.js} +1 -1
- package/dist/assets/{index-CXMr3tiK.js → index-mvm_Pxcf.js} +1 -1
- package/dist/assets/{infoDiagram-42DDH7IO-VUMzEo4o.js → infoDiagram-42DDH7IO-Co9Db2ir.js} +1 -1
- package/dist/assets/{ishikawaDiagram-UXIWVN3A-BuzrU3sx.js → ishikawaDiagram-UXIWVN3A-4z7EydCd.js} +1 -1
- package/dist/assets/{journeyDiagram-VCZTEJTY-BZtR5BAF.js → journeyDiagram-VCZTEJTY-CtLfbhYE.js} +1 -1
- package/dist/assets/{jszip.min-CtodH5MV.js → jszip.min-DtbrAiWv.js} +1 -1
- package/dist/assets/{kanban-definition-6JOO6SKY-Cj_97xXb.js → kanban-definition-6JOO6SKY-9ZrhOvdX.js} +1 -1
- package/dist/assets/{layout-Du0I-tlh.js → layout-Ddo06NJO.js} +1 -1
- package/dist/assets/{linear-BJtxCASI.js → linear-BZUsBSwS.js} +1 -1
- package/dist/assets/{mermaid.core-QlorX0Yw.js → mermaid.core-O-x0KuTp.js} +2 -2
- package/dist/assets/{min-BVnQfunZ.js → min-PzJh19JL.js} +1 -1
- package/dist/assets/{mindmap-definition-QFDTVHPH-Bh_KL4qU.js → mindmap-definition-QFDTVHPH-C3MVo-7p.js} +1 -1
- package/dist/assets/{pieDiagram-DEJITSTG-Bvv0rC_q.js → pieDiagram-DEJITSTG-CMn9-kQc.js} +1 -1
- package/dist/assets/{quadrantDiagram-34T5L4WZ-Bw8cGCRU.js → quadrantDiagram-34T5L4WZ-9eyqTdkj.js} +1 -1
- package/dist/assets/{requirementDiagram-MS252O5E-Cg9jQ71h.js → requirementDiagram-MS252O5E-BJbuDOpR.js} +1 -1
- package/dist/assets/{sankeyDiagram-XADWPNL6-BXJo6-CD.js → sankeyDiagram-XADWPNL6-CzQ1KxD3.js} +1 -1
- package/dist/assets/seqResourceLoaders-DH-Nl0yh.css +41 -0
- package/dist/assets/seqResourceLoaders-DeqzMajS.js +2 -0
- package/dist/assets/{sequenceDiagram-FGHM5R23-D7zpGxwq.js → sequenceDiagram-FGHM5R23-CfmSEAYp.js} +1 -1
- package/dist/assets/{stateDiagram-FHFEXIEX-Der2CqR1.js → stateDiagram-FHFEXIEX-Brw2ZXQC.js} +1 -1
- package/dist/assets/{stateDiagram-v2-QKLJ7IA2-DetvG_Lc.js → stateDiagram-v2-QKLJ7IA2-Dnyo23Rp.js} +1 -1
- package/dist/assets/{timeline-definition-GMOUNBTQ-0UenzEZ6.js → timeline-definition-GMOUNBTQ-DNi7GnlG.js} +1 -1
- package/dist/assets/{vendor-antd-I6I2F7Rv.js → vendor-antd-DWLgSkPo.js} +1 -1
- package/dist/assets/{vendor-codemirror-Zkm6UHQg.js → vendor-codemirror-CKsm_iG4.js} +1 -1
- package/dist/assets/{vendor-mdxeditor-CC09MDyZ.js → vendor-mdxeditor-Dah6NK0y.js} +2 -2
- package/dist/assets/{vendor-qrcode-gZpmSeeh.js → vendor-qrcode-CDcb8weP.js} +1 -1
- package/dist/assets/{vendor-virtuoso-BGwznQ5y.js → vendor-virtuoso-DKUobZpA.js} +1 -1
- package/dist/assets/{vennDiagram-DHZGUBPP-COzYyZ5h.js → vennDiagram-DHZGUBPP-Cdkdx7gB.js} +1 -1
- package/dist/assets/{wardley-RL74JXVD-VZdOtN8t.js → wardley-RL74JXVD-YKzsVN4U.js} +1 -1
- package/dist/assets/{wardleyDiagram-NUSXRM2D-CVkL75Qy.js → wardleyDiagram-NUSXRM2D-gBsb6aut.js} +1 -1
- package/dist/assets/{xychartDiagram-5P7HB3ND-6CHBAYyF.js → xychartDiagram-5P7HB3ND-Bh63NWof.js} +1 -1
- package/dist/index.html +4 -4
- package/node_modules/@ccv/core/src/ask-auto-answer.js +122 -0
- package/package.json +1 -1
- package/server/i18n.js +0 -20
- package/server/lib/ask/ask-bridge.js +4 -1
- package/server/lib/ask/ask-reaper.js +1 -0
- package/server/lib/ask-auto-answer.js +153 -0
- package/server/lib/auto-approve.js +3 -1
- package/server/lib/binary-sniff.js +35 -0
- package/server/lib/sdk-manager.js +65 -19
- package/server/routes/ask-perm.js +87 -6
- package/server/routes/files-content.js +46 -8
- package/server/server.js +52 -0
- package/server/system-prompt-templates/presets/kimi-k3.md +8 -0
- package/dist/assets/App-BvVdj099.css +0 -1
- package/dist/assets/App-DEA3HoZr.js +0 -2
- package/dist/assets/Mobile-CgVpXvs2.js +0 -1
- package/dist/assets/classDiagram-6PBFFD2Q-BUmU_G-i.js +0 -1
- package/dist/assets/classDiagram-v2-HSJHXN6E-BUmU_G-i.js +0 -1
- package/dist/assets/clone-CvoOne2K.js +0 -1
- package/dist/assets/index-CS47d9oh.js +0 -2
- package/dist/assets/seqResourceLoaders-CORH46PN.js +0 -2
- package/dist/assets/seqResourceLoaders-v4YzKhTS.css +0 -41
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cc-viewer",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.24",
|
|
4
4
|
"description": "Claude Code logging, visualization, and management toolkit — launch a web viewer alongside Claude Code with full request/response tracing, proxy, and mobile support",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "server.js",
|
package/server/i18n.js
CHANGED
|
@@ -1167,26 +1167,6 @@ const i18nData = {
|
|
|
1167
1167
|
"tr": "Mevcut günlüğü indir",
|
|
1168
1168
|
"uk": "Завантажити поточний лог"
|
|
1169
1169
|
},
|
|
1170
|
-
"ui.exportPrompts": {
|
|
1171
|
-
"zh": "查看用户 Prompt",
|
|
1172
|
-
"en": "Export user prompts",
|
|
1173
|
-
"zh-TW": "匯出用戶 Prompt",
|
|
1174
|
-
"ko": "사용자 Prompt 내보내기",
|
|
1175
|
-
"ja": "ユーザー Prompt をエクスポート",
|
|
1176
|
-
"de": "Benutzer-Prompts exportieren",
|
|
1177
|
-
"es": "Exportar prompts de usuario",
|
|
1178
|
-
"fr": "Exporter les prompts utilisateur",
|
|
1179
|
-
"it": "Esporta prompt utente",
|
|
1180
|
-
"da": "Eksportér bruger-prompts",
|
|
1181
|
-
"pl": "Eksportuj prompty użytkownika",
|
|
1182
|
-
"ru": "Экспорт пользовательских Prompt",
|
|
1183
|
-
"ar": "تصدير Prompt المستخدم",
|
|
1184
|
-
"no": "Eksporter bruker-prompts",
|
|
1185
|
-
"pt-BR": "Exportar prompts do usuário",
|
|
1186
|
-
"th": "ส่งออก Prompt ผู้ใช้",
|
|
1187
|
-
"tr": "Kullanıcı Prompt'larını dışa aktar",
|
|
1188
|
-
"uk": "Експорт Prompt користувача"
|
|
1189
|
-
},
|
|
1190
1170
|
"ui.exportPromptsTxt": {
|
|
1191
1171
|
"zh": "导出到本地 .txt",
|
|
1192
1172
|
"en": "Export to local .txt",
|
|
@@ -126,7 +126,10 @@ function sleep(ms) { return new Promise(r => setTimeout(r, ms)); }
|
|
|
126
126
|
|
|
127
127
|
function postToViewer() {
|
|
128
128
|
return new Promise((resolve, reject) => {
|
|
129
|
-
|
|
129
|
+
// Hook runs in the session's cwd — report it so the server can resolve the
|
|
130
|
+
// project-forked askAutoAnswerMinutes pref for THIS session's project, not the
|
|
131
|
+
// server's currently-bound one (workspace mode hosts several projects).
|
|
132
|
+
const body = JSON.stringify({ questions, toolUseId, cwd: process.cwd() });
|
|
130
133
|
const req = httpClient.request({
|
|
131
134
|
hostname: '127.0.0.1',
|
|
132
135
|
port: Number(port),
|
|
@@ -96,6 +96,7 @@ export async function reapDeadAskWaiters(deps, state, { now = Date.now() } = {})
|
|
|
96
96
|
// row means another instance's answer consumed it — do not fabricate a
|
|
97
97
|
// cancelled placeholder or grey out a real answer).
|
|
98
98
|
if (entry.timer) clearTimeout(entry.timer);
|
|
99
|
+
if (entry.auto?.timer) clearTimeout(entry.auto.timer); // hosting timer must not outlive the reaped entry
|
|
99
100
|
deps.pendingAskHooks.delete(id);
|
|
100
101
|
lastPoll.delete(id);
|
|
101
102
|
const diskRow = deps.loadAskStore()[id];
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// Server-side auto-answer ("managed answering") countdown for AskUserQuestion.
|
|
2
|
+
//
|
|
3
|
+
// Mirrors auto-approve.js (plan/permission) but with MINUTE semantics and a
|
|
4
|
+
// different value contract, because the feature spec differs in two ways:
|
|
5
|
+
// - 0 minutes means "answer immediately" (unattended flows), not "off";
|
|
6
|
+
// - "off" is the ABSENCE of a value (null/undefined), not 0.
|
|
7
|
+
// Do NOT route this through computeAutoApprove: its 300s cap would silently
|
|
8
|
+
// disable the 10/20/30-minute tiers, and its Number() coercion would turn the
|
|
9
|
+
// persisted "off" (null) into 0 = instant — the most dangerous trap here.
|
|
10
|
+
//
|
|
11
|
+
// Deadline model is identical to auto-approve.js: armed at ask arrival
|
|
12
|
+
// (wall-clock), fires at deadline + ASK_AUTO_ANSWER_BUFFER_MS so a connected
|
|
13
|
+
// client's exact-deadline answer can still win the first-write race.
|
|
14
|
+
//
|
|
15
|
+
// Semantics of the setting (shared with the client):
|
|
16
|
+
// prefs.approvalModal.askAutoAnswerMinutes
|
|
17
|
+
// null/undefined/absent = off (default), 0 = instant, N>0 = N minutes.
|
|
18
|
+
|
|
19
|
+
import { readPrefsRaw } from './prefs-store.js';
|
|
20
|
+
import { getCurrentProjectKey, hasFork, resolveScoped } from './project-prefs.js';
|
|
21
|
+
import { reportSwallowed } from './error-report.js';
|
|
22
|
+
|
|
23
|
+
export { pickAutoAnswers } from '@ccv/core/ask-auto-answer';
|
|
24
|
+
|
|
25
|
+
export const ASK_AUTO_ANSWER_BUFFER_MS = 1000;
|
|
26
|
+
// A hand-edited preferences.json can hold an absurdly large value; beyond
|
|
27
|
+
// 2^31-1 ms Node's setTimeout clamps the delay to 1ms (instant answer). Cap below
|
|
28
|
+
// the 24h ask timeout so the advertised deadline is always reachable: the ask's own
|
|
29
|
+
// 24h timeout is armed first, so the max tier must leave room for the fire buffer.
|
|
30
|
+
// 1439 min = 24h - 1min, comfortably under ASK_TIMEOUT_MS.
|
|
31
|
+
export const MAX_ASK_AUTO_ANSWER_MINUTES = 1439;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Extract the configured minutes from an effective prefs object.
|
|
35
|
+
* Strict typing on purpose: null/undefined/strings/'10'/NaN are all OFF; only a
|
|
36
|
+
* real finite number in [0, MAX] enables hosting. Returns null when disabled.
|
|
37
|
+
*/
|
|
38
|
+
export function effectiveAskAutoAnswerMinutes(prefs) {
|
|
39
|
+
const raw = prefs?.approvalModal?.askAutoAnswerMinutes;
|
|
40
|
+
if (typeof raw !== 'number' || !Number.isFinite(raw)) return null;
|
|
41
|
+
if (raw < 0 || raw > MAX_ASK_AUTO_ANSWER_MINUTES) return null;
|
|
42
|
+
return raw;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Read prefs (project-fork aware) and compute the auto-answer deadline for a
|
|
47
|
+
* pending AskUserQuestion. `projectKey` is the session cwd (SDK) or
|
|
48
|
+
* hook-reported cwd (PTY hook); falls back to the server's current project key.
|
|
49
|
+
*
|
|
50
|
+
* Returns:
|
|
51
|
+
* { enabled:false } — no hosting
|
|
52
|
+
* { enabled:true, instant, minutes, seconds, autoAnswerAt } — absolute epoch ms deadline
|
|
53
|
+
*/
|
|
54
|
+
export function computeAskAutoAnswer(projectKey, { now = Date.now(), readPrefs = readPrefsRaw, file } = {}) {
|
|
55
|
+
let prefs = {};
|
|
56
|
+
try {
|
|
57
|
+
prefs = readPrefs(file) || {};
|
|
58
|
+
} catch (err) {
|
|
59
|
+
reportSwallowed('ask-auto-answer.read-prefs', err);
|
|
60
|
+
return { enabled: false };
|
|
61
|
+
}
|
|
62
|
+
const key = typeof projectKey === 'string' && projectKey ? projectKey : getCurrentProjectKey();
|
|
63
|
+
const effective = hasFork(prefs, key) ? resolveScoped(prefs, prefs.prefsByProject[key]) : prefs;
|
|
64
|
+
const minutes = effectiveAskAutoAnswerMinutes(effective);
|
|
65
|
+
if (minutes === null) return { enabled: false };
|
|
66
|
+
const instant = minutes === 0;
|
|
67
|
+
const seconds = minutes * 60;
|
|
68
|
+
return {
|
|
69
|
+
enabled: true,
|
|
70
|
+
instant,
|
|
71
|
+
minutes,
|
|
72
|
+
seconds,
|
|
73
|
+
autoAnswerAt: instant ? now : now + seconds * 1000,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Delay for the server-side timer: the user-visible deadline plus the request
|
|
79
|
+
* buffer, never negative. Instant (0 minutes) fires on the same tick (0).
|
|
80
|
+
*/
|
|
81
|
+
export function askAutoAnswerFireDelayMs(computed, now = Date.now()) {
|
|
82
|
+
if (!computed?.enabled) return null;
|
|
83
|
+
if (computed.instant) return 0;
|
|
84
|
+
return Math.max(0, computed.autoAnswerAt - now) + ASK_AUTO_ANSWER_BUFFER_MS;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Arm the server-side timer. Returns the auto state stored on the pending
|
|
89
|
+
* entry: { autoAnswerAt, seconds, minutes, cancelled, timer }.
|
|
90
|
+
* `onFire` is invoked once when the deadline (+buffer) passes; callers must
|
|
91
|
+
* re-check the entry is still pending and not cancelled inside onFire.
|
|
92
|
+
* Instant fires SYNCHRONOUSLY — the caller MUST register the pending entry
|
|
93
|
+
* (and emit the pending frame) before calling this.
|
|
94
|
+
*/
|
|
95
|
+
export function createAskAutoAnswerState(computed, onFire) {
|
|
96
|
+
const delay = askAutoAnswerFireDelayMs(computed);
|
|
97
|
+
if (delay === null) return null; // disabled — never arm
|
|
98
|
+
const state = {
|
|
99
|
+
autoAnswerAt: computed.autoAnswerAt,
|
|
100
|
+
seconds: computed.seconds,
|
|
101
|
+
minutes: computed.minutes,
|
|
102
|
+
cancelled: false,
|
|
103
|
+
timer: null,
|
|
104
|
+
};
|
|
105
|
+
if (delay === 0) {
|
|
106
|
+
onFire();
|
|
107
|
+
return state;
|
|
108
|
+
}
|
|
109
|
+
state.timer = setTimeout(onFire, delay);
|
|
110
|
+
return state;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Cancel an armed auto-answer state: clears the timer and marks it cancelled.
|
|
115
|
+
* Accepts either the auto state itself or the whole pending entry ({ auto,
|
|
116
|
+
* autoCancelled }) — the entry form also stamps the durable entry-level flag so
|
|
117
|
+
* replays never re-arm. Returns true only on the first cancellation (so callers
|
|
118
|
+
* broadcast once).
|
|
119
|
+
*/
|
|
120
|
+
export function cancelAskAutoAnswerState(autoOrEntry) {
|
|
121
|
+
if (!autoOrEntry) return false;
|
|
122
|
+
// Entry form: { auto, autoCancelled }
|
|
123
|
+
if (Object.prototype.hasOwnProperty.call(autoOrEntry, 'autoCancelled')) {
|
|
124
|
+
const entry = autoOrEntry;
|
|
125
|
+
if (entry.autoCancelled) return false;
|
|
126
|
+
entry.autoCancelled = true;
|
|
127
|
+
if (entry.auto) {
|
|
128
|
+
entry.auto.cancelled = true;
|
|
129
|
+
if (entry.auto.timer) {
|
|
130
|
+
clearTimeout(entry.auto.timer);
|
|
131
|
+
entry.auto.timer = null;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
const auto = autoOrEntry;
|
|
137
|
+
if (auto.cancelled) return false;
|
|
138
|
+
auto.cancelled = true;
|
|
139
|
+
if (auto.timer) {
|
|
140
|
+
clearTimeout(auto.timer);
|
|
141
|
+
auto.timer = null;
|
|
142
|
+
}
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Fields merged into pending/replay frames for the client countdown UI. */
|
|
147
|
+
export function replayAskAutoAnswerFields(auto, autoCancelled) {
|
|
148
|
+
return {
|
|
149
|
+
autoAnswerAt: autoCancelled ? null : (auto?.autoAnswerAt ?? null),
|
|
150
|
+
autoAnswerSeconds: auto?.seconds ?? null,
|
|
151
|
+
autoCancelled: autoCancelled === true,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
@@ -19,7 +19,9 @@
|
|
|
19
19
|
// plan: prefs.approvalModal.planAutoApproveSeconds (default 0 = off)
|
|
20
20
|
// perm: prefs.autoApproveSeconds (default 3 when unset)
|
|
21
21
|
// 0/off, N>0 = N seconds, -1 = instant (resolve immediately, no buffer).
|
|
22
|
-
// AskUserQuestion
|
|
22
|
+
// AskUserQuestion hosting ("问答题自动托管") is a separate feature with minute
|
|
23
|
+
// semantics and lives in ./ask-auto-answer.js — it is not routed through this module
|
|
24
|
+
// (the 300s cap would silently disable its 10/20/30-minute tiers).
|
|
23
25
|
|
|
24
26
|
import { readPrefsRaw } from './prefs-store.js';
|
|
25
27
|
import { getCurrentProjectKey, hasFork, resolveScoped } from './project-prefs.js';
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { isUtf8 } from 'node:buffer';
|
|
2
|
+
|
|
3
|
+
export function getUtf16Encoding(input) {
|
|
4
|
+
const littleEndian = input[0] === 0xff && input[1] === 0xfe;
|
|
5
|
+
const bigEndian = input[0] === 0xfe && input[1] === 0xff;
|
|
6
|
+
if (!littleEndian && !bigEndian) return null;
|
|
7
|
+
// A UTF-32LE BOM starts with the UTF-16LE prefix. Never guess through it.
|
|
8
|
+
if (littleEndian && input[2] === 0 && input[3] === 0) return null;
|
|
9
|
+
const payload = input.subarray(2);
|
|
10
|
+
if (payload.length % 2 !== 0) return null;
|
|
11
|
+
const text = bigEndian ? Buffer.from(payload).swap16().toString('utf16le') : payload.toString('utf16le');
|
|
12
|
+
for (let index = 0; index < text.length; index++) {
|
|
13
|
+
const code = text.charCodeAt(index);
|
|
14
|
+
if (code === 0 || code === 0xfffd) return null;
|
|
15
|
+
if (code >= 0xd800 && code <= 0xdbff) {
|
|
16
|
+
const next = text.charCodeAt(++index);
|
|
17
|
+
if (!(next >= 0xdc00 && next <= 0xdfff)) return null;
|
|
18
|
+
} else if (code >= 0xdc00 && code <= 0xdfff) return null;
|
|
19
|
+
}
|
|
20
|
+
return bigEndian ? 'utf16be' : 'utf16le';
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Inspect the entire already-bounded buffer before it can become editable text.
|
|
24
|
+
// Legacy encodings without a reliable BOM intentionally fall back to download
|
|
25
|
+
// rather than being decoded into corruptible replacement characters. ESC and
|
|
26
|
+
// backspace are accepted for ANSI logs and terminal progress output.
|
|
27
|
+
export function isBinaryBuffer(input) {
|
|
28
|
+
if (input.length === 0 || getUtf16Encoding(input)) return false;
|
|
29
|
+
let controls = 0;
|
|
30
|
+
for (const byte of input) {
|
|
31
|
+
if (byte === 0) return true;
|
|
32
|
+
if (byte < 0x08 || (byte > 0x0d && byte < 0x20 && byte !== 0x1b) || byte === 0x7f) controls++;
|
|
33
|
+
}
|
|
34
|
+
return controls / input.length > 0.02 || !isUtf8(input);
|
|
35
|
+
}
|
|
@@ -43,6 +43,13 @@ import {
|
|
|
43
43
|
cancelAutoApproveState,
|
|
44
44
|
replayAutoApproveFields,
|
|
45
45
|
} from './auto-approve.js';
|
|
46
|
+
import {
|
|
47
|
+
computeAskAutoAnswer,
|
|
48
|
+
createAskAutoAnswerState,
|
|
49
|
+
cancelAskAutoAnswerState,
|
|
50
|
+
replayAskAutoAnswerFields,
|
|
51
|
+
pickAutoAnswers,
|
|
52
|
+
} from './ask-auto-answer.js';
|
|
46
53
|
|
|
47
54
|
let _query;
|
|
48
55
|
try {
|
|
@@ -2130,11 +2137,25 @@ function _createSession(key, cwd) {
|
|
|
2130
2137
|
// server/lib/ask/ask-constants.js.
|
|
2131
2138
|
const askTimeoutMs = ASK_TIMEOUT_MS;
|
|
2132
2139
|
const askStartedAt = Date.now();
|
|
2133
|
-
|
|
2140
|
+
// Server-side hosting ("问答题自动托管"): approvalModal.askAutoAnswerMinutes
|
|
2141
|
+
// (0 = instant, N = N minutes) answers the ask at the wall-clock deadline even
|
|
2142
|
+
// with no browser connected. Unlike plan/perm this deliberately applies to
|
|
2143
|
+
// unattended scheduled-task runs (ctx != null): hosting is opt-in (default off)
|
|
2144
|
+
// and the 0-minute tier exists precisely for those flows — without it an
|
|
2145
|
+
// ephemeral run would hang the full 24h. The pick prefers the first option
|
|
2146
|
+
// carrying a localized "Recommended" marker, else the first option.
|
|
2147
|
+
const askAuto = computeAskAutoAnswer(_cwd);
|
|
2148
|
+
const askAutoAnswers = askAuto.enabled ? pickAutoAnswers(input.questions) : null;
|
|
2149
|
+
const askHosting = askAuto.enabled && !!askAutoAnswers;
|
|
2150
|
+
const askPayload = { type: 'sdk-ask-pending', id, questions: input.questions, startedAt: askStartedAt, timeoutMs: askTimeoutMs, cwd: _cwd, sessionId: effSessionId(), instanceKey, ...origin,
|
|
2151
|
+
// Always stamp the hosting fields (null when off) so a client whose global pref
|
|
2152
|
+
// disagrees with the fork-resolved server value never re-arms locally.
|
|
2153
|
+
...replayAskAutoAnswerFields(askHosting ? askAuto : null, false) };
|
|
2134
2154
|
if (_broadcastWs) {
|
|
2135
2155
|
_broadcastWs(askPayload);
|
|
2136
2156
|
}
|
|
2137
|
-
const answers = await _waitForApproval(id, askTimeoutMs, 'ask', askPayload, options?.signal, ctx
|
|
2157
|
+
const answers = await _waitForApproval(id, askTimeoutMs, 'ask', askPayload, options?.signal, ctx,
|
|
2158
|
+
askHosting ? askAuto : null, askHosting ? askAutoAnswers : undefined);
|
|
2138
2159
|
if (answers === null) {
|
|
2139
2160
|
return { behavior: 'deny', message: 'Timeout waiting for user answer' };
|
|
2140
2161
|
}
|
|
@@ -2258,7 +2279,7 @@ function _createSession(key, cwd) {
|
|
|
2258
2279
|
} catch (err) { console.warn('[sdk-manager] approval-dismiss broadcast threw:', err?.message); }
|
|
2259
2280
|
}
|
|
2260
2281
|
|
|
2261
|
-
function _waitForApproval(id, timeoutMs, kind, replay = null, signal = null, ctx = null, autoMeta = null) {
|
|
2282
|
+
function _waitForApproval(id, timeoutMs, kind, replay = null, signal = null, ctx = null, autoMeta = null, autoValue = undefined) {
|
|
2262
2283
|
// Record the id on the ephemeral run's ctx ONLY when it actually parks (the three
|
|
2263
2284
|
// callers below sit after every auto-allow/hard-deny early-return, so an auto-allowed
|
|
2264
2285
|
// tool never lands here) — keeps ctx.approvalIds bounded (review P3: no unbounded growth).
|
|
@@ -2294,15 +2315,25 @@ function _createSession(key, cwd) {
|
|
|
2294
2315
|
},
|
|
2295
2316
|
};
|
|
2296
2317
|
_pendingApprovals.set(id, entry);
|
|
2297
|
-
// Arm the server-side fallback AFTER the entry is in the map: instant (-1
|
|
2298
|
-
// synchronously and resolves through
|
|
2318
|
+
// Arm the server-side fallback AFTER the entry is in the map: instant (-1 for
|
|
2319
|
+
// plan/perm, 0 minutes for ask hosting) fires synchronously and resolves through
|
|
2320
|
+
// the same first-wins wrapper. Ask hosting arms its own state factory (minute
|
|
2321
|
+
// semantics) and resolves the pre-picked answers map; plan/perm keep the generic
|
|
2322
|
+
// auto-approve state. A null autoValue (no selectable option) never reaches here —
|
|
2323
|
+
// the ask caller passes autoMeta only when answers were actually picked, otherwise
|
|
2324
|
+
// resolve(null) would be misread as the timeout/deny path.
|
|
2299
2325
|
if (autoMeta?.enabled) {
|
|
2300
|
-
|
|
2326
|
+
const fireResolveValue = autoValue !== undefined
|
|
2327
|
+
? autoValue
|
|
2328
|
+
: (kind === 'plan' ? { approve: true, feedback: '' } : { decision: 'allow' });
|
|
2329
|
+
const fireResolvedType = kind === 'ask' ? 'sdk-ask-resolved' : (kind === 'plan' ? 'sdk-plan-resolved' : 'perm-hook-resolved');
|
|
2330
|
+
const fireReason = kind === 'ask' ? 'auto-answered' : 'auto-approved';
|
|
2331
|
+
entry.auto = (kind === 'ask' ? createAskAutoAnswerState : createAutoApproveState)(autoMeta, () => {
|
|
2301
2332
|
const p = _pendingApprovals.get(id);
|
|
2302
2333
|
if (!p || p.autoCancelled) return;
|
|
2303
|
-
p.resolve(
|
|
2334
|
+
p.resolve(fireResolveValue);
|
|
2304
2335
|
try {
|
|
2305
|
-
_broadcastWs?.({ type:
|
|
2336
|
+
_broadcastWs?.({ type: fireResolvedType, id, reason: fireReason, cwd: _cwd, sessionId: (ctx && ctx.sessionId) || _sessionId || null, instanceKey });
|
|
2306
2337
|
} catch (err) { reportSwallowed('sdk-manager.auto-approve-broadcast', err); }
|
|
2307
2338
|
});
|
|
2308
2339
|
}
|
|
@@ -2350,30 +2381,44 @@ function _createSession(key, cwd) {
|
|
|
2350
2381
|
if (remaining <= 0) continue;
|
|
2351
2382
|
out.push({
|
|
2352
2383
|
id, kind: p.kind || null, replay: p.replay || null, remainingMs: remaining,
|
|
2353
|
-
|
|
2384
|
+
// Ask hosting replays under its own field names (autoAnswerAt/…) — an ask
|
|
2385
|
+
// entry must never advertise autoApproveAt (plan/perm-only contract, pinned
|
|
2386
|
+
// by sdk-manager-auto-approve.test.js).
|
|
2387
|
+
...(p.kind === 'ask'
|
|
2388
|
+
? replayAskAutoAnswerFields(p.auto, p.autoCancelled)
|
|
2389
|
+
: replayAutoApproveFields(p.auto, p.autoCancelled)),
|
|
2354
2390
|
});
|
|
2355
2391
|
}
|
|
2356
2392
|
return out;
|
|
2357
2393
|
}
|
|
2358
2394
|
|
|
2359
2395
|
/**
|
|
2360
|
-
* Cancel the SERVER-SIDE auto-approve countdown for a pending plan/perm
|
|
2361
|
-
* (client clicked the countdown-cancel button). Unlike cancelApproval this does NOT
|
|
2396
|
+
* Cancel the SERVER-SIDE auto-approve / hosting countdown for a pending plan/perm/ask
|
|
2397
|
+
* approval (client clicked the countdown-cancel button). Unlike cancelApproval this does NOT
|
|
2362
2398
|
* resolve the approval — it stays pending for a manual decision; only the fallback
|
|
2363
2399
|
* timer is destroyed and the cancelled flag is replayed to fresh clients so they
|
|
2364
2400
|
* never re-arm. Returns true when the id matched a pending approval of that kind
|
|
2365
2401
|
* (even on a repeat cancel — the state change is durable while pending); false when
|
|
2366
2402
|
* nothing matched (caller falls back to the legacy relay-only behavior).
|
|
2403
|
+
* For ask specifically, false is also returned when no hosting timer was armed
|
|
2404
|
+
* (hosting off / no selectable option) so the caller can fall through to the
|
|
2405
|
+
* PTY-hook entry.
|
|
2367
2406
|
*/
|
|
2368
2407
|
function _autoCancelApproval(id, kind) {
|
|
2369
|
-
|
|
2370
|
-
if (kind !== 'plan' && kind !== 'perm') return false;
|
|
2408
|
+
if (kind !== 'plan' && kind !== 'perm' && kind !== 'ask') return false;
|
|
2371
2409
|
const pending = _pendingApprovals.get(id);
|
|
2410
|
+
// A kind-collision (the id belongs to a plan/perm pending) must NOT be treated as
|
|
2411
|
+
// handled: return false so the caller falls through to the PTY-hook entry, which may
|
|
2412
|
+
// legitimately own that ask id. Without this the hosting cancel is silently dropped.
|
|
2372
2413
|
if (!pending || pending.kind !== kind) return false;
|
|
2373
|
-
|
|
2414
|
+
// Ask with no armed hosting timer: nothing to cancel, but the durable latch is still
|
|
2415
|
+
// stamped (harmless) and the caller falls through to relay the broadcast so sibling
|
|
2416
|
+
// tabs stop their countdown.
|
|
2417
|
+
if (kind === 'ask' && !pending.auto) return false;
|
|
2418
|
+
const first = kind === 'ask' ? cancelAskAutoAnswerState(pending) : cancelAutoApproveState(pending);
|
|
2374
2419
|
if (first && _broadcastWs) {
|
|
2375
2420
|
try {
|
|
2376
|
-
_broadcastWs({ type: kind === 'plan' ? 'sdk-plan-auto-cancel' : 'perm-hook-auto-cancel', id, cwd: _cwd, sessionId: _sessionId || null, instanceKey });
|
|
2421
|
+
_broadcastWs({ type: kind === 'plan' ? 'sdk-plan-auto-cancel' : (kind === 'ask' ? 'ask-auto-cancel' : 'perm-hook-auto-cancel'), id, cwd: _cwd, sessionId: _sessionId || null, instanceKey });
|
|
2377
2422
|
} catch (err) { reportSwallowed('sdk-manager.auto-cancel-broadcast', err); }
|
|
2378
2423
|
}
|
|
2379
2424
|
return true;
|
|
@@ -3046,10 +3091,11 @@ export function cancelApproval(id, reason) {
|
|
|
3046
3091
|
}
|
|
3047
3092
|
|
|
3048
3093
|
/**
|
|
3049
|
-
* Cancel the server-side auto-approve countdown for a plan/perm approval,
|
|
3050
|
-
*
|
|
3051
|
-
*
|
|
3052
|
-
*
|
|
3094
|
+
* Cancel the server-side auto-approve countdown for a plan/perm approval, or the
|
|
3095
|
+
* hosting ("问答题自动托管") timer for an AskUserQuestion approval, scanning EVERY
|
|
3096
|
+
* session (auto-cancel WS handlers). The approval stays pending for a manual
|
|
3097
|
+
* decision. Returns false when no pending approval matched, so the caller can fall
|
|
3098
|
+
* back to the legacy relay-only broadcast.
|
|
3053
3099
|
*/
|
|
3054
3100
|
export function autoCancelApproval(id, kind) {
|
|
3055
3101
|
for (const sess of new Set([..._sessions.values(), ..._ephemeralHosts])) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Ask / permission hook bridge routes (moved verbatim from server.js handleRequest).
|
|
2
|
-
import { loadAskStore, consumeIfFinal as askStoreConsumeIfFinal, markCancelled as askStoreMarkCancelled } from '../lib/ask/ask-store.js';
|
|
2
|
+
import { loadAskStore, consumeIfFinal as askStoreConsumeIfFinal, markCancelled as askStoreMarkCancelled, markAnswered as askStoreMarkAnswered } from '../lib/ask/ask-store.js';
|
|
3
3
|
import { runWaterfallHook, runParallelHook } from '../lib/plugin-loader.js';
|
|
4
4
|
import { sendEventToClients } from '../lib/log-watcher.js';
|
|
5
5
|
import { filterClientsByScope } from '../lib/v2/view-router.js';
|
|
@@ -9,6 +9,7 @@ import { getCurrentProjectKey, hasFork, resolveScoped } from '../lib/project-pre
|
|
|
9
9
|
import { reportSwallowed } from '../lib/error-report.js';
|
|
10
10
|
import { computeAutoApprove, createAutoApproveState } from '../lib/auto-approve.js';
|
|
11
11
|
import { isPublishCommand } from '../lib/approval-policy.js';
|
|
12
|
+
import { computeAskAutoAnswer, createAskAutoAnswerState, pickAutoAnswers, replayAskAutoAnswerFields } from '../lib/ask-auto-answer.js';
|
|
12
13
|
|
|
13
14
|
function pendingAsks(req, res, parsedUrl, isLocal, deps) {
|
|
14
15
|
try {
|
|
@@ -17,6 +18,8 @@ function pendingAsks(req, res, parsedUrl, isLocal, deps) {
|
|
|
17
18
|
// 答案投递仍由 WS ask-hook-answer 路径走 server 端 first-write-wins)。
|
|
18
19
|
const memEntries = [...deps.pendingAskHooks.entries()].map(([id, e]) => ({
|
|
19
20
|
id, questions: e.questions, createdAt: e.createdAt, source: 'memory',
|
|
21
|
+
// Hosting fields for the reconnect recovery chain (client countdown UI).
|
|
22
|
+
...replayAskAutoAnswerFields(e.auto, e.autoCancelled),
|
|
20
23
|
}));
|
|
21
24
|
const memIds = new Set(memEntries.map(e => e.id));
|
|
22
25
|
const diskAll = loadAskStore();
|
|
@@ -25,7 +28,12 @@ function pendingAsks(req, res, parsedUrl, isLocal, deps) {
|
|
|
25
28
|
// 另外过滤 questions 数组非空(防御性)。
|
|
26
29
|
const diskOnly = Object.values(diskAll)
|
|
27
30
|
.filter(e => !memIds.has(e.id) && e.status === 'pending' && Array.isArray(e.questions) && e.questions.length > 0)
|
|
28
|
-
.map(e => ({
|
|
31
|
+
.map(e => ({
|
|
32
|
+
id: e.id, questions: e.questions, createdAt: e.createdAt, source: 'disk',
|
|
33
|
+
// Timers are memory-only: after a server restart an orphaned pending ask
|
|
34
|
+
// degrades to manual answering (documented decision — no schema migration).
|
|
35
|
+
autoAnswerAt: null, autoAnswerSeconds: null, autoCancelled: false,
|
|
36
|
+
}));
|
|
29
37
|
const all = [...memEntries, ...diskOnly].sort((a, b) => a.createdAt - b.createdAt);
|
|
30
38
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
31
39
|
res.end(JSON.stringify({ pendingAsks: all, askProtocolVersion: 1 }));
|
|
@@ -56,7 +64,7 @@ function askHook(req, res, parsedUrl, isLocal, deps) {
|
|
|
56
64
|
return;
|
|
57
65
|
}
|
|
58
66
|
try {
|
|
59
|
-
const { questions, toolUseId } = JSON.parse(body);
|
|
67
|
+
const { questions, toolUseId, cwd } = JSON.parse(body);
|
|
60
68
|
if (!Array.isArray(questions) || questions.length === 0) {
|
|
61
69
|
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
62
70
|
res.end(JSON.stringify({ error: 'Missing questions' }));
|
|
@@ -81,6 +89,7 @@ function askHook(req, res, parsedUrl, isLocal, deps) {
|
|
|
81
89
|
const oldest = deps.pendingAskHooks.get(oldestId);
|
|
82
90
|
if (oldest) {
|
|
83
91
|
clearTimeout(oldest.timer);
|
|
92
|
+
if (oldest.auto?.timer) clearTimeout(oldest.auto.timer);
|
|
84
93
|
try { if (!oldest.res.headersSent) { oldest.res.writeHead(429, { 'Content-Type': 'application/json' }); oldest.res.end(JSON.stringify({ error: 'Too many concurrent requests' })); } } catch {}
|
|
85
94
|
deps.pendingAskHooks.delete(oldestId);
|
|
86
95
|
deps.persistAskDelete(oldestId);
|
|
@@ -106,13 +115,25 @@ function askHook(req, res, parsedUrl, isLocal, deps) {
|
|
|
106
115
|
&& toolUseId.length > 0 && toolUseId.length <= 256
|
|
107
116
|
&& /^[a-zA-Z0-9_-]+$/.test(toolUseId);
|
|
108
117
|
let id;
|
|
118
|
+
// A durable per-ask hosting cancel carried over from the slot being replaced
|
|
119
|
+
// (ask-bridge retry) — initialized before the placeholder is created below.
|
|
120
|
+
let _placeholderAutoCancelled = false;
|
|
109
121
|
if (wantsToolUseId) {
|
|
110
122
|
const existing = deps.pendingAskHooks.get(toolUseId);
|
|
111
123
|
if (!existing) {
|
|
112
124
|
id = toolUseId;
|
|
113
125
|
} else if (!existing.res || existing.res.writableEnded || existing.res.destroyed) {
|
|
114
126
|
// 旧 res 已断(包含占位 res:null 残留)— ask-bridge 重试同 toolUseId 合理,复用槽位
|
|
127
|
+
// Clear the stale hosting timer too: it would otherwise fire later against the
|
|
128
|
+
// re-registered entry and answer it at the OLD deadline with the OLD selection.
|
|
129
|
+
// The new entry arms a fresh full-length timer from this re-arrival (the disk
|
|
130
|
+
// schema carries no deadline, so the original one cannot be preserved).
|
|
115
131
|
if (existing.timer) clearTimeout(existing.timer);
|
|
132
|
+
if (existing.auto?.timer) clearTimeout(existing.auto.timer);
|
|
133
|
+
// Preserve a durable per-ask hosting cancel across the retry: a user who
|
|
134
|
+
// cancelled hosting for THIS ask must not be auto-answered by the re-posted
|
|
135
|
+
// incarnation. (The cancelling tab also re-asserts via its local latch.)
|
|
136
|
+
if (existing.autoCancelled === true) _placeholderAutoCancelled = true;
|
|
116
137
|
deps.pendingAskHooks.delete(toolUseId);
|
|
117
138
|
deps.persistAskDelete(toolUseId);
|
|
118
139
|
id = toolUseId;
|
|
@@ -132,7 +153,11 @@ function askHook(req, res, parsedUrl, isLocal, deps) {
|
|
|
132
153
|
// TOCTOU 防御:占位 id 之前先注册 res.on('close'),否则 await runWaterfallHook 期间 client
|
|
133
154
|
// abort 的 close 事件落空 → entry 残到 HOOK_TIMEOUT(24h)。占位 set 提前到 await 之前防两条同 ms 并发
|
|
134
155
|
// POST 的 do-while 都通过 collision check 后 set 互相覆盖(first res 永泄漏到 HOOK_TIMEOUT)。
|
|
135
|
-
|
|
156
|
+
// Both hosting fields live on the placeholder too: a client ask-auto-cancel
|
|
157
|
+
// landing during the plugin-waterfall window stamps the durable flag here, and
|
|
158
|
+
// the live entry below inherits it (otherwise the cancel would be silently lost
|
|
159
|
+
// when the live object replaces the placeholder).
|
|
160
|
+
const _placeholderEntry = { questions, res, timer: null, createdAt: Date.now(), shortPoll: shortPollMode, autoCancelled: _placeholderAutoCancelled, auto: null };
|
|
136
161
|
deps.pendingAskHooks.set(id, _placeholderEntry);
|
|
137
162
|
deps.persistAskEntry(id, _placeholderEntry);
|
|
138
163
|
// Seed waiter liveness for the reaper (ask-reaper.js). Optional chaining:
|
|
@@ -146,6 +171,7 @@ function askHook(req, res, parsedUrl, isLocal, deps) {
|
|
|
146
171
|
if (entry) {
|
|
147
172
|
if (entry.shortPoll) return;
|
|
148
173
|
if (entry.timer) clearTimeout(entry.timer);
|
|
174
|
+
if (entry.auto?.timer) clearTimeout(entry.auto.timer);
|
|
149
175
|
deps.pendingAskHooks.delete(id);
|
|
150
176
|
deps.persistAskDelete(id);
|
|
151
177
|
if (deps.terminalWss) {
|
|
@@ -179,6 +205,7 @@ function askHook(req, res, parsedUrl, isLocal, deps) {
|
|
|
179
205
|
const timer = setTimeout(() => {
|
|
180
206
|
const entry = deps.pendingAskHooks.get(id);
|
|
181
207
|
if (entry) {
|
|
208
|
+
if (entry.auto?.timer) clearTimeout(entry.auto.timer); // defensive: outlives hosting in normal flows
|
|
182
209
|
deps.pendingAskHooks.delete(id);
|
|
183
210
|
deps.persistAskDelete(id);
|
|
184
211
|
try {
|
|
@@ -199,7 +226,15 @@ function askHook(req, res, parsedUrl, isLocal, deps) {
|
|
|
199
226
|
}, HOOK_TIMEOUT);
|
|
200
227
|
|
|
201
228
|
const askStartedAt = Date.now();
|
|
202
|
-
|
|
229
|
+
// Server-side hosting ("问答题自动托管"): when approvalModal.askAutoAnswerMinutes
|
|
230
|
+
// is set (0 = instant, N = N minutes), the server answers the ask itself at the
|
|
231
|
+
// wall-clock deadline (+1s buffer) even with no browser connected. Computed after
|
|
232
|
+
// the plugin waterfall so a plugin-answered ask never arms. The pick prefers the
|
|
233
|
+
// first option carrying a localized "Recommended" marker, else the first option.
|
|
234
|
+
const autoMeta = computeAskAutoAnswer(typeof cwd === 'string' && cwd ? cwd : undefined, { file: deps.getPrefsFile?.() });
|
|
235
|
+
const autoAnswers = autoMeta.enabled ? pickAutoAnswers(questions) : null;
|
|
236
|
+
const hosting = autoMeta.enabled && !!autoAnswers;
|
|
237
|
+
const _liveEntry = { ..._placeholderEntry, timer, createdAt: askStartedAt };
|
|
203
238
|
deps.pendingAskHooks.set(id, _liveEntry);
|
|
204
239
|
deps.persistAskEntry(id, _liveEntry);
|
|
205
240
|
|
|
@@ -214,8 +249,16 @@ function askHook(req, res, parsedUrl, isLocal, deps) {
|
|
|
214
249
|
}
|
|
215
250
|
|
|
216
251
|
// Broadcast to all terminal WS clients — 附 startedAt + timeoutMs 让前端渲染倒计时
|
|
252
|
+
// Honor a cancel inherited from the placeholder (slot-reuse) or landed during the
|
|
253
|
+
// plugin waterfall: never advertise a countdown that the fire guard will no-op.
|
|
254
|
+
const hostingActive = hosting && !_liveEntry.autoCancelled;
|
|
217
255
|
if (deps.terminalWss) {
|
|
218
|
-
|
|
256
|
+
// Always stamp the hosting fields (null when off) so a client whose global pref
|
|
257
|
+
// disagrees with the fork-resolved server value never re-arms locally.
|
|
258
|
+
const pmsg = JSON.stringify({
|
|
259
|
+
type: 'ask-hook-pending', id, questions, startedAt: askStartedAt, timeoutMs: HOOK_TIMEOUT,
|
|
260
|
+
...replayAskAutoAnswerFields(hostingActive ? autoMeta : null, _liveEntry.autoCancelled),
|
|
261
|
+
});
|
|
219
262
|
deps.terminalWss.clients.forEach((client) => {
|
|
220
263
|
if (client.readyState === 1) {
|
|
221
264
|
try { client.send(pmsg); } catch {}
|
|
@@ -223,6 +266,43 @@ function askHook(req, res, parsedUrl, isLocal, deps) {
|
|
|
223
266
|
});
|
|
224
267
|
}
|
|
225
268
|
deps.notifyParentPending({ type: 'ask-hook-pending', id, questions });
|
|
269
|
+
|
|
270
|
+
// Arm AFTER the entry is registered AND the pending frame is out: instant (0min)
|
|
271
|
+
// fires synchronously and resolves through the entry, so clients observe the
|
|
272
|
+
// order pending → resolved (never a ghost modal for an already-answered ask).
|
|
273
|
+
if (hostingActive) {
|
|
274
|
+
_liveEntry.auto = createAskAutoAnswerState(autoMeta, async () => {
|
|
275
|
+
const entry = deps.pendingAskHooks.get(id);
|
|
276
|
+
if (!entry || entry.autoCancelled) return;
|
|
277
|
+
if (entry.timer) clearTimeout(entry.timer);
|
|
278
|
+
deps.pendingAskHooks.delete(id);
|
|
279
|
+
deps.askWaiterLastPoll?.delete(id);
|
|
280
|
+
if (entry.shortPoll) {
|
|
281
|
+
// first-write-wins: a client answer (or another cc-viewer instance sharing
|
|
282
|
+
// ask-store.json) may have beaten the timer — never notify listeners with
|
|
283
|
+
// answers that disagree with the disk record.
|
|
284
|
+
let wrote = false;
|
|
285
|
+
try { wrote = await askStoreMarkAnswered(id, autoAnswers); } catch (err) { reportSwallowed('ask-perm.ask-auto-answered', err); }
|
|
286
|
+
if (!wrote) return;
|
|
287
|
+
deps.notifyShortPollAnswer?.(id, autoAnswers);
|
|
288
|
+
} else {
|
|
289
|
+
try {
|
|
290
|
+
if (entry.res && !entry.res.headersSent) {
|
|
291
|
+
entry.res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
292
|
+
entry.res.end(JSON.stringify({ answers: autoAnswers }));
|
|
293
|
+
}
|
|
294
|
+
} catch {}
|
|
295
|
+
deps.persistAskDelete(id);
|
|
296
|
+
}
|
|
297
|
+
if (deps.terminalWss) {
|
|
298
|
+
const rmsg = JSON.stringify({ type: 'ask-hook-resolved', id, reason: 'auto-answered' });
|
|
299
|
+
deps.terminalWss.clients.forEach((c) => {
|
|
300
|
+
if (c.readyState === 1) try { c.send(rmsg); } catch {}
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
deps.notifyParentPending({ type: 'ask-hook-resolved', id });
|
|
304
|
+
});
|
|
305
|
+
}
|
|
226
306
|
} catch {
|
|
227
307
|
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
228
308
|
res.end(JSON.stringify({ error: 'Invalid request body' }));
|
|
@@ -342,6 +422,7 @@ function askHookCancel(req, res, parsedUrl, isLocal, deps) {
|
|
|
342
422
|
const entry = deps.pendingAskHooks.get(id);
|
|
343
423
|
if (entry) {
|
|
344
424
|
if (entry.timer) clearTimeout(entry.timer);
|
|
425
|
+
if (entry.auto?.timer) clearTimeout(entry.auto.timer);
|
|
345
426
|
deps.pendingAskHooks.delete(id);
|
|
346
427
|
deps.askWaiterLastPoll?.delete(id);
|
|
347
428
|
if (entry.shortPoll) {
|