dsh-question-timeout-protection 0.0.0-stage → 0.1.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/COPYRIGHT +10 -0
- package/LICENSE +201 -0
- package/README.md +268 -2
- package/README_en-US.md +269 -0
- package/cordis.patch.yml +4 -0
- package/dist/client.js +2149 -0
- package/dist/index.d.mts +76 -0
- package/dist/index.mjs +1095 -0
- package/package.json +99 -4
- package/src/translation/.example_zh-CN.ini +43 -0
- package/src/translation/en-US.ini +43 -0
- package/src/translation/zh-CN.ini +43 -0
package/dist/client.js
ADDED
|
@@ -0,0 +1,2149 @@
|
|
|
1
|
+
window.__ModuleLoader__.load({ id: "dsh-question-timeout-protection", factory: (require) => {
|
|
2
|
+
var module = { exports: {} };
|
|
3
|
+
var exports = module.exports;
|
|
4
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
5
|
+
let react = require("react");
|
|
6
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
7
|
+
// src/config-value.ts
|
|
8
|
+
/** 读取 volatile 字段的当前值, 兼容普通值 */
|
|
9
|
+
function configValue(value) {
|
|
10
|
+
if (typeof value === "object" && value !== null && "get" in value) return value.get();
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
13
|
+
// src/constants.ts
|
|
14
|
+
/** 插件包名 */
|
|
15
|
+
const PLUGIN_NAME = "dsh-question-timeout-protection";
|
|
16
|
+
/** 默认语言(翻译回退与初始语言) */
|
|
17
|
+
const DEFAULT_LANGUAGE = "zh-CN";
|
|
18
|
+
/** 配置区块的插槽名: 插件级(插件详情页顶部), 不落在「包含的组件」的行里 */
|
|
19
|
+
const SETTINGS_SLOT = "plugins.bundle.config";
|
|
20
|
+
/** 插件配置的设置命名空间, 等于 cordis.patch.yml 里的行 id, 两处必须保持一致 */
|
|
21
|
+
const SETTINGS_NAMESPACE = "question-timeout-protection";
|
|
22
|
+
/** 插件配置页的文案命名空间 */
|
|
23
|
+
const SETTINGS_LOCALE = "questionTimeoutProtection";
|
|
24
|
+
/**
|
|
25
|
+
* 草稿快照超过通道上限时的提示。
|
|
26
|
+
* 与「草稿无法送达」不同: 那条说的是通道不可用, 这条说的是内容本身太大, 用户需要知道该删减。
|
|
27
|
+
*/
|
|
28
|
+
const DRAFT_TOO_LARGE_TEXT_ZH = "草稿过长, 未送达";
|
|
29
|
+
const DRAFT_TOO_LARGE_TEXT_EN = "Draft too large; not delivered";
|
|
30
|
+
/** 草稿文本里已选选项行的前缀 */
|
|
31
|
+
const DRAFT_SELECTED_LABEL_ZH = "已选: {items}";
|
|
32
|
+
const DRAFT_SELECTED_LABEL_EN = "Selected: {items}";
|
|
33
|
+
/** 草稿文本里自由输入行的前缀 */
|
|
34
|
+
const DRAFT_CUSTOM_LABEL_ZH = "补充: {text}";
|
|
35
|
+
const DRAFT_CUSTOM_LABEL_EN = "Additional: {text}";
|
|
36
|
+
/** 宿主提问卡片的容器选择器(容器带 data-question-key) */
|
|
37
|
+
const QUESTION_CARD_SELECTOR = "[data-question-key]";
|
|
38
|
+
/** 卡片 key 前缀,宿主形如 question:<sessionId>:<callId> */
|
|
39
|
+
const QUESTION_CARD_KEY_PREFIX = "question:";
|
|
40
|
+
/** 宿主提问卡片右上角操作区(类名带构建哈希,按后缀匹配) */
|
|
41
|
+
const HEADER_ACTIONS_SELECTOR = "[class*=\"_headerActions\"]";
|
|
42
|
+
/** 卡片输入框选择器 */
|
|
43
|
+
const CARD_INPUT_SELECTOR = "textarea, input";
|
|
44
|
+
/** 卡片内可点击的控件 */
|
|
45
|
+
const CARD_BUTTON_SELECTOR = "button";
|
|
46
|
+
/** 宿主倒计时与等待状态文本所在元素的选择器(类名带构建哈希,按后缀匹配) */
|
|
47
|
+
const WAIT_STATUS_SELECTOR = "[class*=\"_waitStatus\"]";
|
|
48
|
+
/**
|
|
49
|
+
* 宿主等待状态文案的识别子串: 宿主可能把「会一直等你回答」「已继续工作」渲染在别的容器里。
|
|
50
|
+
* 卡片侧与工具行侧是两套文案(工具行取自宿主会话包里的 ask.pending), 中英文都要覆盖;
|
|
51
|
+
* 取值由 scripts/check-translation.mjs 的 HOST_LABEL_BASELINE 逐字核对, 改动时同步那张表。
|
|
52
|
+
*/
|
|
53
|
+
const HOST_WAIT_PATTERNS = [
|
|
54
|
+
"会一直等你回答",
|
|
55
|
+
"已继续工作",
|
|
56
|
+
"Waiting until you answer",
|
|
57
|
+
"Work continued",
|
|
58
|
+
"continued; answer still available"
|
|
59
|
+
];
|
|
60
|
+
/**
|
|
61
|
+
* 宿主倒计时文案的解析规则: 运行态与暂停态并列, 中文与英文并列。
|
|
62
|
+
* 宿主在用户交互(选中选项等)后会切到暂停态文案, 两套都要能读出剩余秒数。
|
|
63
|
+
*/
|
|
64
|
+
const COUNTDOWN_PATTERNS = [
|
|
65
|
+
/(\d+)\s*秒后(?:继续工作|超时|继续)/,
|
|
66
|
+
/剩余\s*(\d+)\s*秒/,
|
|
67
|
+
/(?:Continuing in|Timeout in) (\d+)s/,
|
|
68
|
+
/Paused · (\d+)s remaining/
|
|
69
|
+
];
|
|
70
|
+
/** 判定宿主是否停在暂停态的文案特征: 暂停态下的剩余秒数不是真实到期时间 */
|
|
71
|
+
const PAUSED_TEXT_PATTERNS = [/已暂停/, /Paused/];
|
|
72
|
+
/** 「问答自动无限等待」常开开关的文案(中文与英文): 中文侧带「自动」, 与英文的 Auto 对齐 */
|
|
73
|
+
const AUTO_INFINITE_LABEL_ZH = "问答自动无限等待";
|
|
74
|
+
const AUTO_INFINITE_LABEL_EN = "Auto wait indefinitely";
|
|
75
|
+
/** 常开开关的悬停提示(中文与英文) */
|
|
76
|
+
const AUTO_INFINITE_TITLE_ZH = "开启后每次提问都自动进入无限等待, 关闭后回到插件的计时行为";
|
|
77
|
+
const AUTO_INFINITE_TITLE_EN = "When on, every question enters indefinite wait automatically; when off, the plugin countdown applies";
|
|
78
|
+
/** 常开开关按钮的类名与状态属性 */
|
|
79
|
+
const AUTO_INFINITE_BUTTON_CLASS = "dqtp-auto-infinite";
|
|
80
|
+
const AUTO_INFINITE_STATE_ATTRIBUTE = "data-dqtp-auto-infinite";
|
|
81
|
+
/** 常开开关的持久化键: 开关状态跨刷新保留 */
|
|
82
|
+
const AUTO_INFINITE_STORAGE_KEY = "dsh.question-timeout-protection.auto-infinite";
|
|
83
|
+
const MODEL_SELECTOR_BUTTON = "button[aria-label^=\"选择模型\"], button[aria-label^=\"Select model\"]";
|
|
84
|
+
/** 模型选择控件所在的工具栏容器(类名后缀) */
|
|
85
|
+
const COMPOSER_CONTROLS_SELECTOR = "[class*=\"standardControls\"]";
|
|
86
|
+
/** 兜底扫描间隔(毫秒), 防止漏掉宿主未触发 mutation 的更新 */
|
|
87
|
+
const SCAN_INTERVAL_MS = 1e3;
|
|
88
|
+
/** 单次状态通道请求(取令牌与上报)的超时: 请求挂起时不能让整页通道一直 pending */
|
|
89
|
+
const SYNC_REQUEST_TIMEOUT_MS = 8e3;
|
|
90
|
+
/** 草稿无法送达时在卡片上给出的可见提示 */
|
|
91
|
+
const DRAFT_SYNC_FAILED_TEXT_ZH = "草稿无法送达";
|
|
92
|
+
const DRAFT_SYNC_FAILED_TEXT_EN = "Draft cannot be delivered";
|
|
93
|
+
/** 宿主「慢慢回答」控件的文案(中文与英文), 用于隐藏该控件 */
|
|
94
|
+
const TAKE_TIME_TEXTS = ["慢慢回答", "Take time"];
|
|
95
|
+
/** 宿主问答卡片收起/展开控件的文案(中文与英文), 用于隐藏该控件 */
|
|
96
|
+
const COLLAPSE_CONTROL_LABELS = [
|
|
97
|
+
"收起问题卡片",
|
|
98
|
+
"展开问题卡片",
|
|
99
|
+
"Collapse the question card",
|
|
100
|
+
"Expand the question card"
|
|
101
|
+
];
|
|
102
|
+
/** 宿主的「收起问题面板」按钮文案(中文与英文) */
|
|
103
|
+
const CLOSE_PANEL_LABELS = ["收起问题面板,可从工具调用重新打开", "Close the panel — reopen it from the tool call"];
|
|
104
|
+
const ASK_REOPEN_LABELS = [
|
|
105
|
+
"回答",
|
|
106
|
+
"Answer",
|
|
107
|
+
"查看回答",
|
|
108
|
+
"View answers",
|
|
109
|
+
"回答を表示"
|
|
110
|
+
];
|
|
111
|
+
/** 隐藏宿主控件时附加的类名 */
|
|
112
|
+
const HIDDEN_CONTROL_CLASS = "dqtp-host-control-hidden";
|
|
113
|
+
/**
|
|
114
|
+
* 被插件按文案隐藏的宿主节点标记。
|
|
115
|
+
* 还原时只认这个标记, 不再复判当前文案: 宿主复用同一节点改写文案时也能还原。
|
|
116
|
+
*/
|
|
117
|
+
const HIDDEN_WAIT_ATTRIBUTE = "data-dqtp-host-wait";
|
|
118
|
+
/** 提问工具调用行的选择器(宿主 tool 行根节点) */
|
|
119
|
+
const ASK_ROW_SELECTOR = "div[data-tool=\"ask_user_question\"]";
|
|
120
|
+
/** 工具调用行的 callId 属性(宿主 ToolCall 包裹层) */
|
|
121
|
+
const TOOL_CALL_ID_ATTRIBUTE = "data-chat-call-id";
|
|
122
|
+
/** 工具调用行的 callId 选择器 */
|
|
123
|
+
const TOOL_CALL_ID_SELECTOR = "[data-chat-call-id]";
|
|
124
|
+
/** 宿主工具行里打开提问面板的气泡按钮(类名带构建哈希,按后缀匹配) */
|
|
125
|
+
const PANEL_BUTTON_SELECTOR = "button[class*=\"_panelButton\"]";
|
|
126
|
+
/** 插件自绘倒计时的属性名(值形如 question:<sessionId>:<callId>) */
|
|
127
|
+
const COUNTDOWN_ATTRIBUTE = "data-dqtp-countdown";
|
|
128
|
+
/** 插件自绘「无限等待」按钮的类名 */
|
|
129
|
+
const INFINITE_BUTTON_CLASS = "dqtp-infinite-wait";
|
|
130
|
+
/** 插件自绘「提问标识气泡」的类名 */
|
|
131
|
+
const BUBBLE_BUTTON_CLASS = "dqtp-question-bubble";
|
|
132
|
+
/** 「提问标识气泡」的状态属性: 仅在已记录超时草稿时出现 */
|
|
133
|
+
const BUBBLE_STATE_ATTRIBUTE = "data-dqtp-bubble";
|
|
134
|
+
/** 气泡草稿态取值 */
|
|
135
|
+
const BUBBLE_STATE_DRAFT = "draft";
|
|
136
|
+
/** 进入警示态时给倒计时附加的类名 */
|
|
137
|
+
const COUNTDOWN_WARNING_CLASS = "dqtp-countdown-warning";
|
|
138
|
+
/** 「无限等待」暂停态给倒计时附加的类名 */
|
|
139
|
+
const COUNTDOWN_PAUSED_CLASS = "dqtp-countdown-paused";
|
|
140
|
+
/** 超时锁定卡片时附加的类名 */
|
|
141
|
+
const CARD_LOCKED_CLASS = "dqtp-card-locked";
|
|
142
|
+
const CARD_ENHANCEMENTS_CLASS = "dqtp-card-enhancements";
|
|
143
|
+
/** 注入样式所用 style 元素的 id */
|
|
144
|
+
const COUNTDOWN_STYLE_ID = "dqtp-countdown-warning-style";
|
|
145
|
+
/** 只读草稿面板的元素 id */
|
|
146
|
+
const DRAFT_PANEL_ID = "dqtp-draft-panel";
|
|
147
|
+
/** 客户端状态同步端点路径 */
|
|
148
|
+
const SYNC_PATH = "/question-timeout-protection/state";
|
|
149
|
+
/** 卡片底部提交按钮的文案(中文与英文) */
|
|
150
|
+
const SUBMIT_TEXTS = ["提交", "Submit"];
|
|
151
|
+
/** 卡片底部下一题按钮的文案(中文与英文) */
|
|
152
|
+
const NEXT_QUESTION_TEXTS = ["下一题", "Next"];
|
|
153
|
+
/** 卡片底部跳过按钮的文案(中文与英文) */
|
|
154
|
+
const SKIP_QUESTION_TEXTS = ["跳过", "Skip"];
|
|
155
|
+
/** 倒计时文案模板,{seconds} 由剩余秒数替换 */
|
|
156
|
+
const COUNTDOWN_TEXT_ZH = "{seconds}秒后超时";
|
|
157
|
+
/** 倒计时文案模板(英文) */
|
|
158
|
+
const COUNTDOWN_TEXT_EN = "Timeout in {seconds}s";
|
|
159
|
+
/** 「无限等待」暂停后的倒计时文案(中文) */
|
|
160
|
+
const COUNTDOWN_PAUSED_TEXT_ZH = "已暂停";
|
|
161
|
+
/** 「无限等待」暂停后的倒计时文案(英文) */
|
|
162
|
+
const COUNTDOWN_PAUSED_TEXT_EN = "Paused";
|
|
163
|
+
/** 「无限等待」按钮文案(中文) */
|
|
164
|
+
const INFINITE_WAIT_LABEL_ZH = "无限等待";
|
|
165
|
+
/** 状态同步通道不可达时替代倒计时的提示文案(中文) */
|
|
166
|
+
const SYNC_UNAVAILABLE_TEXT_ZH = "无法无限等待";
|
|
167
|
+
/** 状态同步通道不可达时替代倒计时的提示文案(英文) */
|
|
168
|
+
const SYNC_UNAVAILABLE_TEXT_EN = "Cannot wait indefinitely";
|
|
169
|
+
/** 「无限等待」按钮文案(英文) */
|
|
170
|
+
const INFINITE_WAIT_LABEL_EN = "Wait indefinitely";
|
|
171
|
+
/** 「提问标识气泡」文案(中文),已记录超时草稿时显示 */
|
|
172
|
+
const BUBBLE_DRAFT_LABEL_ZH = "提问·超时草稿";
|
|
173
|
+
/** 「提问标识气泡」文案(英文),已记录超时草稿时显示 */
|
|
174
|
+
const BUBBLE_DRAFT_LABEL_EN = "Question · timed-out draft";
|
|
175
|
+
/** 只读草稿面板标题(中文) */
|
|
176
|
+
const DRAFT_PANEL_TITLE_ZH = "超时草稿";
|
|
177
|
+
/** 只读草稿面板标题(英文) */
|
|
178
|
+
const DRAFT_PANEL_TITLE_EN = "Timed-out draft";
|
|
179
|
+
/** 只读草稿面板的说明(中文),提示这一轮提问已结束 */
|
|
180
|
+
const DRAFT_PANEL_NOTE_ZH = "这一轮提问已超时结束, 不能继续回答; 以下只是超时时的草稿内容";
|
|
181
|
+
/** 只读草稿面板的说明(英文) */
|
|
182
|
+
const DRAFT_PANEL_NOTE_EN = "This round of questions timed out and can no longer be answered; the content below is only the draft as it stood at the timeout";
|
|
183
|
+
/** 只读草稿面板没有可展示内容时的提示(中文) */
|
|
184
|
+
const DRAFT_PANEL_EMPTY_ZH = "超时那一刻没有已选选项或已输入内容";
|
|
185
|
+
/** 只读草稿面板没有可展示内容时的提示(英文) */
|
|
186
|
+
const DRAFT_PANEL_EMPTY_EN = "No option was selected and no text had been entered at the timeout";
|
|
187
|
+
/** 只读草稿面板里某题没有作答时的提示(中文) */
|
|
188
|
+
const DRAFT_PANEL_UNANSWERED_ZH = "未作答";
|
|
189
|
+
/** 只读草稿面板里某题没有作答时的提示(英文) */
|
|
190
|
+
const DRAFT_PANEL_UNANSWERED_EN = "Not answered";
|
|
191
|
+
// src/locale-core.ts
|
|
192
|
+
function normalizeLocale(locale) {
|
|
193
|
+
const raw = locale.trim().toLowerCase().replace(/_/g, "-");
|
|
194
|
+
if (raw === "") return DEFAULT_LANGUAGE;
|
|
195
|
+
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(raw)) return DEFAULT_LANGUAGE;
|
|
196
|
+
const [language, ...rest] = raw.split("-");
|
|
197
|
+
const region = rest[0];
|
|
198
|
+
if (region !== void 0 && !/^[a-z0-9]+$/.test(region)) return DEFAULT_LANGUAGE;
|
|
199
|
+
if (language === "zh") return "zh-CN";
|
|
200
|
+
if (language === "en") return "en-US";
|
|
201
|
+
return region === void 0 ? language : language + "-" + region.toUpperCase();
|
|
202
|
+
}
|
|
203
|
+
// src/client/locale.ts
|
|
204
|
+
/** 当前界面语言代码, 已按 normalizeLocale 归一 */
|
|
205
|
+
function browserLanguage() {
|
|
206
|
+
const documentLang = typeof document === "undefined" ? "" : document.documentElement.lang;
|
|
207
|
+
const navigatorLang = typeof navigator === "undefined" ? "" : navigator.language;
|
|
208
|
+
return normalizeLocale(documentLang || navigatorLang || "zh-CN");
|
|
209
|
+
}
|
|
210
|
+
/** 界面语言是否中文: 只按语言段判定, 地区不参与(繁体界面也显示简体文案) */
|
|
211
|
+
function uiUsesChinese() {
|
|
212
|
+
return browserLanguage().split("-")[0] === "zh";
|
|
213
|
+
}
|
|
214
|
+
/** 按界面语言取中英文之一 */
|
|
215
|
+
function pick(zh, en) {
|
|
216
|
+
return uiUsesChinese() ? zh : en;
|
|
217
|
+
}
|
|
218
|
+
function isRecognisedLocale() {
|
|
219
|
+
const language = browserLanguage().split("-")[0];
|
|
220
|
+
return language === "zh" || language === "en";
|
|
221
|
+
}
|
|
222
|
+
// src/client/adapter.ts
|
|
223
|
+
const CARD_STYLE = `
|
|
224
|
+
@keyframes dqtp-countdown-blink {
|
|
225
|
+
0%, 100% { color: #e5484d; opacity: 1; }
|
|
226
|
+
50% { color: #e5484d; opacity: 0.35; }
|
|
227
|
+
}
|
|
228
|
+
[data-dqtp-countdown] {
|
|
229
|
+
color: var(--dsw-alias-label-secondary);
|
|
230
|
+
white-space: nowrap;
|
|
231
|
+
font-size: 12px;
|
|
232
|
+
line-height: 24px;
|
|
233
|
+
margin-right: 8px;
|
|
234
|
+
}
|
|
235
|
+
[data-dqtp-countdown].dqtp-countdown-warning {
|
|
236
|
+
color: #e5484d !important;
|
|
237
|
+
font-weight: 600;
|
|
238
|
+
animation: dqtp-countdown-blink 1s ease-in-out infinite;
|
|
239
|
+
}
|
|
240
|
+
[data-dqtp-countdown].dqtp-countdown-paused {
|
|
241
|
+
color: var(--dsw-alias-label-secondary);
|
|
242
|
+
}
|
|
243
|
+
.dqtp-host-control-hidden {
|
|
244
|
+
display: none !important;
|
|
245
|
+
}
|
|
246
|
+
.dqtp-infinite-wait {
|
|
247
|
+
flex-shrink: 0;
|
|
248
|
+
white-space: nowrap;
|
|
249
|
+
height: 24px;
|
|
250
|
+
padding: 0 10px;
|
|
251
|
+
border-radius: 999px;
|
|
252
|
+
border: 1px solid var(--dsw-alias-border-secondary, rgba(0, 0, 0, 0.16));
|
|
253
|
+
background: transparent;
|
|
254
|
+
color: var(--dsw-alias-label-primary);
|
|
255
|
+
font-size: 12px;
|
|
256
|
+
line-height: 22px;
|
|
257
|
+
cursor: pointer;
|
|
258
|
+
}
|
|
259
|
+
.dqtp-infinite-wait:hover {
|
|
260
|
+
background: var(--dsw-alias-interactive-bg-hover, rgba(0, 0, 0, 0.06));
|
|
261
|
+
}
|
|
262
|
+
.dqtp-auto-infinite {
|
|
263
|
+
flex-shrink: 0;
|
|
264
|
+
white-space: nowrap;
|
|
265
|
+
height: 24px;
|
|
266
|
+
padding: 0 10px;
|
|
267
|
+
margin-right: 6px;
|
|
268
|
+
border-radius: 999px;
|
|
269
|
+
border: 1px solid var(--dsw-alias-border-secondary, rgba(0, 0, 0, 0.16));
|
|
270
|
+
background: transparent;
|
|
271
|
+
color: var(--dsw-alias-label-primary);
|
|
272
|
+
font-size: 12px;
|
|
273
|
+
line-height: 22px;
|
|
274
|
+
cursor: pointer;
|
|
275
|
+
}
|
|
276
|
+
.dqtp-auto-infinite[data-dqtp-auto-infinite="on"] {
|
|
277
|
+
border-color: #e5484d;
|
|
278
|
+
color: #e5484d;
|
|
279
|
+
font-weight: 600;
|
|
280
|
+
}
|
|
281
|
+
.dqtp-question-bubble {
|
|
282
|
+
display: inline-flex;
|
|
283
|
+
align-items: center;
|
|
284
|
+
height: 22px;
|
|
285
|
+
padding: 0 10px;
|
|
286
|
+
margin-left: 6px;
|
|
287
|
+
border-radius: 999px;
|
|
288
|
+
border: 1px solid var(--dsw-alias-border-secondary, rgba(0, 0, 0, 0.16));
|
|
289
|
+
background: transparent;
|
|
290
|
+
color: var(--dsw-alias-label-primary);
|
|
291
|
+
font-size: 12px;
|
|
292
|
+
line-height: 20px;
|
|
293
|
+
white-space: nowrap;
|
|
294
|
+
cursor: pointer;
|
|
295
|
+
vertical-align: middle;
|
|
296
|
+
}
|
|
297
|
+
.dqtp-question-bubble[data-dqtp-bubble="draft"] {
|
|
298
|
+
border-color: #e5484d;
|
|
299
|
+
color: #e5484d;
|
|
300
|
+
font-weight: 600;
|
|
301
|
+
}
|
|
302
|
+
.${CARD_ENHANCEMENTS_CLASS} [data-question-key] button[role="radio"][aria-checked="true"] > span:first-child {
|
|
303
|
+
background: var(--dsw-alias-label-primary);
|
|
304
|
+
color: var(--dsw-alias-label-primary-foreground);
|
|
305
|
+
}
|
|
306
|
+
.dqtp-card-locked {
|
|
307
|
+
cursor: default;
|
|
308
|
+
}
|
|
309
|
+
#dqtp-draft-panel {
|
|
310
|
+
position: fixed;
|
|
311
|
+
z-index: 2147483000;
|
|
312
|
+
}
|
|
313
|
+
#dqtp-draft-panel .dqtp-draft-card {
|
|
314
|
+
width: min(360px, calc(100vw - 24px));
|
|
315
|
+
max-height: min(320px, calc(100vh - 32px));
|
|
316
|
+
display: flex;
|
|
317
|
+
flex-direction: column;
|
|
318
|
+
gap: 8px;
|
|
319
|
+
padding: 12px 14px;
|
|
320
|
+
border-radius: 10px;
|
|
321
|
+
border: 1px solid var(--dsw-alias-border-secondary, rgba(0, 0, 0, 0.16));
|
|
322
|
+
background: var(--dsw-alias-bg-elevated, #fff);
|
|
323
|
+
color: var(--dsw-alias-label-primary, #111);
|
|
324
|
+
box-shadow: 0 10px 28px rgba(0, 0, 0, 0.2);
|
|
325
|
+
font-size: 13px;
|
|
326
|
+
line-height: 20px;
|
|
327
|
+
text-align: left;
|
|
328
|
+
}
|
|
329
|
+
#dqtp-draft-panel .dqtp-draft-title {
|
|
330
|
+
font-size: 13px;
|
|
331
|
+
font-weight: 600;
|
|
332
|
+
}
|
|
333
|
+
#dqtp-draft-panel .dqtp-draft-note {
|
|
334
|
+
font-size: 12px;
|
|
335
|
+
line-height: 18px;
|
|
336
|
+
color: var(--dsw-alias-label-secondary, #666);
|
|
337
|
+
}
|
|
338
|
+
#dqtp-draft-panel .dqtp-draft-body {
|
|
339
|
+
flex: 1;
|
|
340
|
+
overflow: auto;
|
|
341
|
+
white-space: pre-wrap;
|
|
342
|
+
word-break: break-word;
|
|
343
|
+
font-size: 13px;
|
|
344
|
+
line-height: 20px;
|
|
345
|
+
padding: 8px 10px;
|
|
346
|
+
border-radius: 8px;
|
|
347
|
+
background: var(--dsw-alias-bg-secondary, rgba(0, 0, 0, 0.04));
|
|
348
|
+
}
|
|
349
|
+
@media (prefers-reduced-motion: reduce) {
|
|
350
|
+
[data-dqtp-countdown].dqtp-countdown-warning { animation: none; }
|
|
351
|
+
}
|
|
352
|
+
`;
|
|
353
|
+
const CARD_DISMISSED_ATTRIBUTE = "data-dqtp-dismissed";
|
|
354
|
+
function parseCardKey(key) {
|
|
355
|
+
if (key === null) return void 0;
|
|
356
|
+
if (!key.startsWith("question:")) return void 0;
|
|
357
|
+
const rest = key.slice(9);
|
|
358
|
+
const split = rest.indexOf(":");
|
|
359
|
+
if (split <= 0) return void 0;
|
|
360
|
+
return {
|
|
361
|
+
sessionId: rest.slice(0, split),
|
|
362
|
+
callId: rest.slice(split + 1)
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
function findCard(identity) {
|
|
366
|
+
const key = QUESTION_CARD_KEY_PREFIX + identity.sessionId + ":" + identity.callId;
|
|
367
|
+
const containers = sessionContainers();
|
|
368
|
+
for (const container of containers) {
|
|
369
|
+
if (container.getAttribute("data-conversation-session") !== identity.sessionId) continue;
|
|
370
|
+
for (const element of Array.from(container.querySelectorAll(QUESTION_CARD_SELECTOR))) if (element instanceof HTMLElement && element.getAttribute("data-question-key") === key) return element;
|
|
371
|
+
}
|
|
372
|
+
for (const element of Array.from(document.querySelectorAll(QUESTION_CARD_SELECTOR))) if (element instanceof HTMLElement && element.getAttribute("data-question-key") === key) return element;
|
|
373
|
+
}
|
|
374
|
+
function parseRemainingSeconds(text) {
|
|
375
|
+
for (const pattern of COUNTDOWN_PATTERNS) {
|
|
376
|
+
const matched = pattern.exec(text);
|
|
377
|
+
if (matched !== null) return Number(matched[1]);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
function readAutoInfinite() {
|
|
381
|
+
try {
|
|
382
|
+
return window.localStorage.getItem(AUTO_INFINITE_STORAGE_KEY) === "on";
|
|
383
|
+
} catch {
|
|
384
|
+
return false;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
function writeAutoInfinite(value) {
|
|
388
|
+
try {
|
|
389
|
+
window.localStorage.setItem(AUTO_INFINITE_STORAGE_KEY, value ? "on" : "off");
|
|
390
|
+
} catch {}
|
|
391
|
+
}
|
|
392
|
+
let styleUsers$1 = 0;
|
|
393
|
+
let enhancementUsers = 0;
|
|
394
|
+
function ensureStyle$1() {
|
|
395
|
+
styleUsers$1 += 1;
|
|
396
|
+
if (document.getElementById("dqtp-countdown-warning-style") === null) {
|
|
397
|
+
const style = document.createElement("style");
|
|
398
|
+
style.id = COUNTDOWN_STYLE_ID;
|
|
399
|
+
style.textContent = CARD_STYLE;
|
|
400
|
+
document.head.appendChild(style);
|
|
401
|
+
}
|
|
402
|
+
let released = false;
|
|
403
|
+
return () => {
|
|
404
|
+
if (released) return;
|
|
405
|
+
released = true;
|
|
406
|
+
styleUsers$1 -= 1;
|
|
407
|
+
if (styleUsers$1 > 0) return;
|
|
408
|
+
document.getElementById(COUNTDOWN_STYLE_ID)?.remove();
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
function stripRecommendedSuffix(label) {
|
|
412
|
+
return label.replace(/\s*(?:\((?:recommended|推荐)\)|((?:recommended|推荐)))\s*$/i, "").trim();
|
|
413
|
+
}
|
|
414
|
+
function recommendedOptionLabels(question) {
|
|
415
|
+
const labels = (question?.options ?? []).map((option) => typeof option.label === "string" ? option.label : "").filter((label) => label !== "");
|
|
416
|
+
const marked = labels.filter((label) => stripRecommendedSuffix(label) !== label);
|
|
417
|
+
if (question?.multiSelect === true) return marked;
|
|
418
|
+
return labels.length > 0 && stripRecommendedSuffix(labels[0]) !== labels[0] ? [labels[0]] : [];
|
|
419
|
+
}
|
|
420
|
+
function dropRecommended(selected, question) {
|
|
421
|
+
const recommended = new Set(recommendedOptionLabels(question));
|
|
422
|
+
return selected.filter((label) => !recommended.has(label));
|
|
423
|
+
}
|
|
424
|
+
function readSelected(card, question) {
|
|
425
|
+
const selected = [];
|
|
426
|
+
const originals = (question?.options ?? []).map((option) => typeof option.label === "string" ? option.label : "").filter((label) => label !== "");
|
|
427
|
+
for (const button of Array.from(card.querySelectorAll("button[aria-checked=\"true\"]"))) {
|
|
428
|
+
const display = (button.getAttribute("aria-label") ?? "").trim();
|
|
429
|
+
if (display === "") continue;
|
|
430
|
+
const label = originals.find((original) => stripRecommendedSuffix(original) === display) ?? display;
|
|
431
|
+
if (!selected.includes(label)) selected.push(label);
|
|
432
|
+
}
|
|
433
|
+
return selected;
|
|
434
|
+
}
|
|
435
|
+
function readCustom(card) {
|
|
436
|
+
const field = card.querySelector("[class*=\"_options\"] textarea") ?? card.querySelector("textarea, input");
|
|
437
|
+
if (field instanceof HTMLTextAreaElement || field instanceof HTMLInputElement) return field.value;
|
|
438
|
+
return "";
|
|
439
|
+
}
|
|
440
|
+
function emptyItem() {
|
|
441
|
+
return {
|
|
442
|
+
selected: [],
|
|
443
|
+
custom: "",
|
|
444
|
+
skipped: false
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
function hasItemContent(item) {
|
|
448
|
+
return item.selected.length > 0 || item.custom.trim() !== "";
|
|
449
|
+
}
|
|
450
|
+
function composeItem(item) {
|
|
451
|
+
const parts = [];
|
|
452
|
+
if (item.selected.length > 0) parts.push(pick(DRAFT_SELECTED_LABEL_ZH, DRAFT_SELECTED_LABEL_EN).replace("{items}", () => item.selected.join(" / ")));
|
|
453
|
+
const custom = item.custom.trim();
|
|
454
|
+
if (custom !== "") parts.push(pick(DRAFT_CUSTOM_LABEL_ZH, DRAFT_CUSTOM_LABEL_EN).replace("{text}", () => custom));
|
|
455
|
+
return parts.join("\n\n");
|
|
456
|
+
}
|
|
457
|
+
function readProgress(card) {
|
|
458
|
+
const element = card.querySelector("[class*=\"_progress\"]");
|
|
459
|
+
const matched = /(\d+)\s*\/\s*(\d+)/.exec(element?.textContent ?? "");
|
|
460
|
+
if (matched === null) return void 0;
|
|
461
|
+
const index = Number(matched[1]) - 1;
|
|
462
|
+
const count = Number(matched[2]);
|
|
463
|
+
if (!Number.isInteger(index) || index < 0 || !Number.isInteger(count) || count <= 0) return void 0;
|
|
464
|
+
return {
|
|
465
|
+
index,
|
|
466
|
+
count
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
const SESSION_CONTAINER_SELECTOR = "[data-conversation-session]";
|
|
470
|
+
function sessionContainers() {
|
|
471
|
+
const out = [];
|
|
472
|
+
for (const element of Array.from(document.querySelectorAll(SESSION_CONTAINER_SELECTOR))) if (element instanceof HTMLElement) out.push(element);
|
|
473
|
+
return out;
|
|
474
|
+
}
|
|
475
|
+
const hostDraftCache = /* @__PURE__ */ new Map();
|
|
476
|
+
function nextHostDraftRound() {
|
|
477
|
+
hostDraftCache.clear();
|
|
478
|
+
}
|
|
479
|
+
function invalidateHostDraftCache() {
|
|
480
|
+
hostDraftCache.clear();
|
|
481
|
+
}
|
|
482
|
+
function sameHostDraftRaws(a, b) {
|
|
483
|
+
if (a.size !== b.size) return false;
|
|
484
|
+
for (const [key, raw] of a) if (b.get(key) !== raw) return false;
|
|
485
|
+
return true;
|
|
486
|
+
}
|
|
487
|
+
function readHostDrafts(identity, count) {
|
|
488
|
+
try {
|
|
489
|
+
const storage = window.localStorage;
|
|
490
|
+
const cacheKey = QUESTION_CARD_KEY_PREFIX + identity.sessionId + ":" + identity.callId;
|
|
491
|
+
const raws = /* @__PURE__ */ new Map();
|
|
492
|
+
for (let i = 0; i < storage.length; i += 1) {
|
|
493
|
+
const key = storage.key(i);
|
|
494
|
+
if (key === null) continue;
|
|
495
|
+
if (!key.startsWith("dsh.user-questions.drafts.v1") || !key.includes(identity.sessionId)) continue;
|
|
496
|
+
const raw = storage.getItem(key);
|
|
497
|
+
if (raw !== null) raws.set(key, raw);
|
|
498
|
+
}
|
|
499
|
+
const cached = hostDraftCache.get(identity.sessionId);
|
|
500
|
+
let entries;
|
|
501
|
+
if (cached !== void 0 && sameHostDraftRaws(cached.raws, raws)) entries = cached.entries;
|
|
502
|
+
else {
|
|
503
|
+
entries = /* @__PURE__ */ new Map();
|
|
504
|
+
for (const raw of raws.values()) try {
|
|
505
|
+
const parsed = JSON.parse(raw);
|
|
506
|
+
for (const [requestKey, progress] of Object.entries(parsed.progressByRequest ?? {})) if (Array.isArray(progress?.drafts) && progress.drafts.length > 0 && !entries.has(requestKey)) entries.set(requestKey, progress.drafts);
|
|
507
|
+
} catch {}
|
|
508
|
+
hostDraftCache.set(identity.sessionId, {
|
|
509
|
+
raws,
|
|
510
|
+
entries
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
const drafts = entries.get(cacheKey);
|
|
514
|
+
if (drafts === void 0 || drafts.length === 0) return void 0;
|
|
515
|
+
return drafts.slice(0, count).map((entry) => {
|
|
516
|
+
const value = typeof entry === "object" && entry !== null ? entry : {};
|
|
517
|
+
return {
|
|
518
|
+
selected: Array.isArray(value.selected) ? value.selected.filter((label) => typeof label === "string") : [],
|
|
519
|
+
custom: typeof value.custom === "string" ? value.custom : "",
|
|
520
|
+
skipped: value.skipped === true
|
|
521
|
+
};
|
|
522
|
+
});
|
|
523
|
+
} catch {}
|
|
524
|
+
}
|
|
525
|
+
function readLiveDrafts(card, count, selected) {
|
|
526
|
+
const items = Array.from({ length: count }, () => emptyItem());
|
|
527
|
+
if (card === void 0) return items;
|
|
528
|
+
const progress = readProgress(card);
|
|
529
|
+
if (progress === void 0 || progress.index >= count) return items;
|
|
530
|
+
items[progress.index] = {
|
|
531
|
+
selected: [...selected],
|
|
532
|
+
custom: readCustom(card),
|
|
533
|
+
skipped: false
|
|
534
|
+
};
|
|
535
|
+
return items;
|
|
536
|
+
}
|
|
537
|
+
function mergeDrafts(host, live, snapshot, count, liveIndex) {
|
|
538
|
+
const merged = [];
|
|
539
|
+
for (let i = 0; i < count; i += 1) {
|
|
540
|
+
const hostItem = host?.[i];
|
|
541
|
+
if (hostItem !== void 0) {
|
|
542
|
+
merged.push({
|
|
543
|
+
selected: [...hostItem.selected],
|
|
544
|
+
custom: hostItem.custom,
|
|
545
|
+
skipped: hostItem.skipped
|
|
546
|
+
});
|
|
547
|
+
continue;
|
|
548
|
+
}
|
|
549
|
+
const liveItem = live?.[i];
|
|
550
|
+
if (i === liveIndex && liveItem !== void 0 && hasItemContent(liveItem)) {
|
|
551
|
+
merged.push({
|
|
552
|
+
selected: [...liveItem.selected],
|
|
553
|
+
custom: liveItem.custom,
|
|
554
|
+
skipped: liveItem.skipped
|
|
555
|
+
});
|
|
556
|
+
continue;
|
|
557
|
+
}
|
|
558
|
+
const snapshotItem = snapshot?.[i];
|
|
559
|
+
if (snapshotItem !== void 0) {
|
|
560
|
+
merged.push({
|
|
561
|
+
selected: [...snapshotItem.selected],
|
|
562
|
+
custom: snapshotItem.custom,
|
|
563
|
+
skipped: snapshotItem.skipped
|
|
564
|
+
});
|
|
565
|
+
continue;
|
|
566
|
+
}
|
|
567
|
+
merged.push(emptyItem());
|
|
568
|
+
}
|
|
569
|
+
return merged;
|
|
570
|
+
}
|
|
571
|
+
function resolveProjectionFace(ctx, sessionId) {
|
|
572
|
+
const pick = (sessions) => {
|
|
573
|
+
const face = (sessions?.binding?.(sessionId))?.session?.projections?.faceOf?.("userQuestions");
|
|
574
|
+
return typeof face === "object" && face !== null ? face : void 0;
|
|
575
|
+
};
|
|
576
|
+
try {
|
|
577
|
+
const direct = pick(ctx.sessions);
|
|
578
|
+
if (direct !== void 0) return direct;
|
|
579
|
+
} catch {}
|
|
580
|
+
try {
|
|
581
|
+
const getter = ctx.get;
|
|
582
|
+
return pick(typeof getter === "function" ? getter.call(ctx, "sessions") : void 0);
|
|
583
|
+
} catch {
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
function fetchWithTimeout(url, init) {
|
|
588
|
+
const controller = new AbortController();
|
|
589
|
+
const timer = setTimeout(() => controller.abort(), SYNC_REQUEST_TIMEOUT_MS);
|
|
590
|
+
return fetch(url, {
|
|
591
|
+
...init,
|
|
592
|
+
signal: controller.signal
|
|
593
|
+
}).finally(() => clearTimeout(timer));
|
|
594
|
+
}
|
|
595
|
+
function postState(url, body, token) {
|
|
596
|
+
return fetchWithTimeout(url, {
|
|
597
|
+
method: "POST",
|
|
598
|
+
headers: {
|
|
599
|
+
"content-type": "application/json",
|
|
600
|
+
"x-qtp-token": token
|
|
601
|
+
},
|
|
602
|
+
body: JSON.stringify(body)
|
|
603
|
+
}).then(async (response) => {
|
|
604
|
+
if (response.status === 401) {
|
|
605
|
+
invalidateToken();
|
|
606
|
+
return {
|
|
607
|
+
ok: false,
|
|
608
|
+
reason: "token-stale"
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
if (!response.ok) {
|
|
612
|
+
try {
|
|
613
|
+
const payload = await response.json();
|
|
614
|
+
if (typeof payload.reason === "string") return {
|
|
615
|
+
ok: false,
|
|
616
|
+
reason: payload.reason
|
|
617
|
+
};
|
|
618
|
+
} catch {}
|
|
619
|
+
return { ok: false };
|
|
620
|
+
}
|
|
621
|
+
try {
|
|
622
|
+
const payload = await response.json();
|
|
623
|
+
if (payload.ok !== true) return {
|
|
624
|
+
ok: false,
|
|
625
|
+
reason: typeof payload.reason === "string" ? payload.reason : void 0
|
|
626
|
+
};
|
|
627
|
+
return {
|
|
628
|
+
ok: true,
|
|
629
|
+
...payload.ignored === true ? { ignored: true } : {}
|
|
630
|
+
};
|
|
631
|
+
} catch {
|
|
632
|
+
return { ok: false };
|
|
633
|
+
}
|
|
634
|
+
}).catch(() => ({ ok: false }));
|
|
635
|
+
}
|
|
636
|
+
let tokenPromise;
|
|
637
|
+
function invalidateToken() {
|
|
638
|
+
tokenPromise = void 0;
|
|
639
|
+
}
|
|
640
|
+
async function fetchToken(url) {
|
|
641
|
+
try {
|
|
642
|
+
const response = await fetchWithTimeout(url, { headers: { accept: "application/json" } });
|
|
643
|
+
if (!response.ok) return void 0;
|
|
644
|
+
const payload = await response.json();
|
|
645
|
+
return typeof payload.token === "string" && payload.token !== "" ? payload.token : void 0;
|
|
646
|
+
} catch {
|
|
647
|
+
return;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
async function resolveToken() {
|
|
651
|
+
const cached = tokenPromise;
|
|
652
|
+
if (cached !== void 0) return cached;
|
|
653
|
+
const pending = fetchToken(SYNC_PATH + "-token");
|
|
654
|
+
tokenPromise = pending;
|
|
655
|
+
const token = await pending;
|
|
656
|
+
if (token === void 0 && tokenPromise === pending) tokenPromise = void 0;
|
|
657
|
+
return token;
|
|
658
|
+
}
|
|
659
|
+
async function reportInfiniteWait(identity, deadline, infinite = true) {
|
|
660
|
+
return postStateWithToken({
|
|
661
|
+
callId: identity.callId,
|
|
662
|
+
infinite,
|
|
663
|
+
...deadline === void 0 ? {} : { deadline }
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
async function reportDraftSnapshot(identity, answers) {
|
|
667
|
+
return postStateWithToken({
|
|
668
|
+
callId: identity.callId,
|
|
669
|
+
answers
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
async function postStateWithToken(body) {
|
|
673
|
+
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
674
|
+
const token = await resolveToken();
|
|
675
|
+
if (token === void 0) return { ok: false };
|
|
676
|
+
const reply = await postState(SYNC_PATH, body, token);
|
|
677
|
+
if (reply.ok) return reply;
|
|
678
|
+
if (reply.reason === "token-stale") continue;
|
|
679
|
+
return reply;
|
|
680
|
+
}
|
|
681
|
+
return { ok: false };
|
|
682
|
+
}
|
|
683
|
+
const DRAFT_REPORT_INTERVAL_MS = 800;
|
|
684
|
+
/**
|
|
685
|
+
* 安装提问超时适配器。
|
|
686
|
+
* @param ctx 客户端上下文
|
|
687
|
+
* @param source 配置来源, 每次读取都取当前生效值
|
|
688
|
+
* @returns 卸载函数
|
|
689
|
+
*/
|
|
690
|
+
function installQuestionTimeoutAdapter(ctx, source) {
|
|
691
|
+
if (typeof document === "undefined" || typeof MutationObserver === "undefined") return () => {};
|
|
692
|
+
const releaseStyle = ensureStyle$1();
|
|
693
|
+
enhancementUsers += 1;
|
|
694
|
+
const handled = /* @__PURE__ */ new Set();
|
|
695
|
+
const timedOut = /* @__PURE__ */ new Set();
|
|
696
|
+
const passive = /* @__PURE__ */ new Set();
|
|
697
|
+
const syncUnavailable = /* @__PURE__ */ new Set();
|
|
698
|
+
const paused = /* @__PURE__ */ new Map();
|
|
699
|
+
const deadlines = /* @__PURE__ */ new Map();
|
|
700
|
+
const draftSyncFailed = /* @__PURE__ */ new Set();
|
|
701
|
+
const draftSyncFailures = /* @__PURE__ */ new Map();
|
|
702
|
+
const drafts = /* @__PURE__ */ new Map();
|
|
703
|
+
const lastDrafts = /* @__PURE__ */ new Map();
|
|
704
|
+
const subscriptions = /* @__PURE__ */ new Map();
|
|
705
|
+
const lockedCards = /* @__PURE__ */ new WeakSet();
|
|
706
|
+
const dismissedCallIds = /* @__PURE__ */ new Set();
|
|
707
|
+
const deferred = /* @__PURE__ */ new Map();
|
|
708
|
+
const projectedCounts = /* @__PURE__ */ new Map();
|
|
709
|
+
const projectedQuestions = /* @__PURE__ */ new Map();
|
|
710
|
+
const optionInteractions = /* @__PURE__ */ new Map();
|
|
711
|
+
const callActive = /* @__PURE__ */ new Set();
|
|
712
|
+
const callTouchedAt = /* @__PURE__ */ new Map();
|
|
713
|
+
const draftTooLarge = /* @__PURE__ */ new Set();
|
|
714
|
+
const draftTooLargeAt = /* @__PURE__ */ new Map();
|
|
715
|
+
const callSessions = /* @__PURE__ */ new Map();
|
|
716
|
+
const readLiveSelected = (card, callId) => {
|
|
717
|
+
const index = readProgress(card)?.index ?? 0;
|
|
718
|
+
const question = projectedQuestions.get(callId)?.[index];
|
|
719
|
+
const selected = readSelected(card, question);
|
|
720
|
+
if (optionInteractions.get(callId)?.has(index) === true) return selected;
|
|
721
|
+
return dropRecommended(selected, question);
|
|
722
|
+
};
|
|
723
|
+
const gateDraftSelections = (items, callId) => {
|
|
724
|
+
if (items === void 0) return void 0;
|
|
725
|
+
const touched = optionInteractions.get(callId);
|
|
726
|
+
return items.map((item, index) => touched?.has(index) === true ? item : {
|
|
727
|
+
selected: dropRecommended(item.selected, projectedQuestions.get(callId)?.[index]),
|
|
728
|
+
custom: item.custom,
|
|
729
|
+
skipped: item.skipped
|
|
730
|
+
});
|
|
731
|
+
};
|
|
732
|
+
const rememberCallSession = (identity) => {
|
|
733
|
+
callTouchedAt.set(identity.callId, Date.now());
|
|
734
|
+
if (callSessions.get(identity.callId) === identity.sessionId) return;
|
|
735
|
+
callSessions.set(identity.callId, identity.sessionId);
|
|
736
|
+
while (callSessions.size > 512) {
|
|
737
|
+
const oldest = callSessions.keys().next();
|
|
738
|
+
if (oldest.done) break;
|
|
739
|
+
callSessions.delete(oldest.value);
|
|
740
|
+
}
|
|
741
|
+
};
|
|
742
|
+
const callSessionOf = (callId) => callSessions.get(callId) ?? drafts.get(callId)?.sessionId;
|
|
743
|
+
const knownCallIds = () => {
|
|
744
|
+
const ids = /* @__PURE__ */ new Set();
|
|
745
|
+
for (const id of handled) ids.add(id);
|
|
746
|
+
for (const id of timedOut) ids.add(id);
|
|
747
|
+
for (const id of passive) ids.add(id);
|
|
748
|
+
for (const id of syncUnavailable) ids.add(id);
|
|
749
|
+
for (const id of drafts.keys()) ids.add(id);
|
|
750
|
+
for (const id of lastDrafts.keys()) ids.add(id);
|
|
751
|
+
for (const id of deadlines.keys()) ids.add(id);
|
|
752
|
+
for (const id of paused.keys()) ids.add(id);
|
|
753
|
+
for (const id of deferred.keys()) ids.add(id);
|
|
754
|
+
for (const id of draftSyncFailures.keys()) ids.add(id);
|
|
755
|
+
for (const id of draftTooLarge.keys()) ids.add(id);
|
|
756
|
+
for (const id of draftTooLargeAt.keys()) ids.add(id);
|
|
757
|
+
for (const id of dismissedCallIds.keys()) ids.add(id);
|
|
758
|
+
for (const id of optionInteractions.keys()) ids.add(id);
|
|
759
|
+
return [...ids];
|
|
760
|
+
};
|
|
761
|
+
const forgetCall = (callId) => {
|
|
762
|
+
callSessions.delete(callId);
|
|
763
|
+
autoInfiniteAttempted.delete(callId);
|
|
764
|
+
handled.delete(callId);
|
|
765
|
+
timedOut.delete(callId);
|
|
766
|
+
passive.delete(callId);
|
|
767
|
+
syncUnavailable.delete(callId);
|
|
768
|
+
deadlines.delete(callId);
|
|
769
|
+
paused.delete(callId);
|
|
770
|
+
deferred.delete(callId);
|
|
771
|
+
draftSyncFailed.delete(callId);
|
|
772
|
+
draftSyncFailures.delete(callId);
|
|
773
|
+
draftReportedAt.delete(callId);
|
|
774
|
+
draftIgnored.delete(callId);
|
|
775
|
+
projectedCounts.delete(callId);
|
|
776
|
+
projectedQuestions.delete(callId);
|
|
777
|
+
optionInteractions.delete(callId);
|
|
778
|
+
callActive.delete(callId);
|
|
779
|
+
callTouchedAt.delete(callId);
|
|
780
|
+
draftTooLarge.delete(callId);
|
|
781
|
+
draftTooLargeAt.delete(callId);
|
|
782
|
+
dismissedCallIds.delete(callId);
|
|
783
|
+
lastDrafts.delete(callId);
|
|
784
|
+
};
|
|
785
|
+
const pruneCallStates = () => {
|
|
786
|
+
const now = Date.now();
|
|
787
|
+
for (const [callId, touchedAt] of callTouchedAt) {
|
|
788
|
+
if (now - touchedAt <= 18e5) continue;
|
|
789
|
+
forgetCall(callId);
|
|
790
|
+
}
|
|
791
|
+
while (callTouchedAt.size > 512) {
|
|
792
|
+
let oldestId;
|
|
793
|
+
let oldestAt = Number.POSITIVE_INFINITY;
|
|
794
|
+
for (const [callId, touchedAt] of callTouchedAt) {
|
|
795
|
+
if (touchedAt >= oldestAt) continue;
|
|
796
|
+
oldestAt = touchedAt;
|
|
797
|
+
oldestId = callId;
|
|
798
|
+
}
|
|
799
|
+
if (oldestId === void 0) break;
|
|
800
|
+
forgetCall(oldestId);
|
|
801
|
+
}
|
|
802
|
+
};
|
|
803
|
+
const pruneDrafts = () => {
|
|
804
|
+
while (drafts.size > 200) {
|
|
805
|
+
const oldest = drafts.keys().next();
|
|
806
|
+
if (oldest.done) break;
|
|
807
|
+
drafts.delete(oldest.value);
|
|
808
|
+
}
|
|
809
|
+
while (lastDrafts.size > 200) {
|
|
810
|
+
const oldest = lastDrafts.keys().next();
|
|
811
|
+
if (oldest.done) break;
|
|
812
|
+
lastDrafts.delete(oldest.value);
|
|
813
|
+
}
|
|
814
|
+
};
|
|
815
|
+
let panel;
|
|
816
|
+
let panelAnchor;
|
|
817
|
+
const options = () => source.getOptions();
|
|
818
|
+
const syncEnhancementsClass = () => {
|
|
819
|
+
document.documentElement.classList.toggle(CARD_ENHANCEMENTS_CLASS, options().cardEnhancements !== false);
|
|
820
|
+
};
|
|
821
|
+
let disposed = false;
|
|
822
|
+
let autoInfinite = readAutoInfinite();
|
|
823
|
+
const autoInfiniteWarned = /* @__PURE__ */ new Set();
|
|
824
|
+
const autoInfiniteAttempted = /* @__PURE__ */ new Set();
|
|
825
|
+
const draftReportedAt = /* @__PURE__ */ new Map();
|
|
826
|
+
const DRAFT_IGNORED_RETRY_MS = 3e4;
|
|
827
|
+
const draftIgnored = /* @__PURE__ */ new Map();
|
|
828
|
+
const countdownText = (remainingMs, isPaused) => {
|
|
829
|
+
const seconds = Math.max(0, Math.ceil(remainingMs / 1e3));
|
|
830
|
+
return (isPaused ? pick(COUNTDOWN_PAUSED_TEXT_ZH, COUNTDOWN_PAUSED_TEXT_EN) : pick(COUNTDOWN_TEXT_ZH, COUNTDOWN_TEXT_EN)).replace("{seconds}", String(seconds));
|
|
831
|
+
};
|
|
832
|
+
const ensureCountdown = (card, key) => {
|
|
833
|
+
const actions = card.querySelector(HEADER_ACTIONS_SELECTOR);
|
|
834
|
+
if (!(actions instanceof HTMLElement)) return void 0;
|
|
835
|
+
const found = actions.querySelector("[data-dqtp-countdown]");
|
|
836
|
+
if (found instanceof HTMLElement && found.getAttribute("data-dqtp-countdown") === key) return found;
|
|
837
|
+
if (found instanceof HTMLElement) found.remove();
|
|
838
|
+
const element = document.createElement("span");
|
|
839
|
+
element.setAttribute(COUNTDOWN_ATTRIBUTE, key);
|
|
840
|
+
actions.insertBefore(element, actions.firstChild);
|
|
841
|
+
return element;
|
|
842
|
+
};
|
|
843
|
+
let countdownWarned = false;
|
|
844
|
+
const warnCountdownOnce = () => {
|
|
845
|
+
if (countdownWarned) return;
|
|
846
|
+
countdownWarned = true;
|
|
847
|
+
console.warn("[question-timeout-protection] 卡片右上角找不到操作区, 无法插入倒计时, 这张卡片保持宿主原样");
|
|
848
|
+
};
|
|
849
|
+
const ensureInfiniteButton = (card, countdown) => {
|
|
850
|
+
const actions = card.querySelector(HEADER_ACTIONS_SELECTOR);
|
|
851
|
+
if (!(actions instanceof HTMLElement)) return void 0;
|
|
852
|
+
const found = actions.querySelector("button.dqtp-infinite-wait");
|
|
853
|
+
if (found instanceof HTMLElement) return found;
|
|
854
|
+
const button = document.createElement("button");
|
|
855
|
+
button.type = "button";
|
|
856
|
+
button.className = INFINITE_BUTTON_CLASS;
|
|
857
|
+
if (countdown !== void 0 && countdown.parentElement === actions) actions.insertBefore(button, countdown.nextSibling);
|
|
858
|
+
else actions.insertBefore(button, actions.firstChild);
|
|
859
|
+
return button;
|
|
860
|
+
};
|
|
861
|
+
const warnAutoInfiniteOnce = (reason, message) => {
|
|
862
|
+
if (autoInfiniteWarned.has(reason)) return;
|
|
863
|
+
autoInfiniteWarned.add(reason);
|
|
864
|
+
console.warn("[question-timeout-protection] " + message);
|
|
865
|
+
};
|
|
866
|
+
const directChildOf = (element, host) => {
|
|
867
|
+
let anchor = element;
|
|
868
|
+
while (anchor.parentElement !== null && anchor.parentElement !== host) anchor = anchor.parentElement;
|
|
869
|
+
return anchor.parentElement === host ? anchor : void 0;
|
|
870
|
+
};
|
|
871
|
+
const locateAutoInfiniteAnchor = (container) => {
|
|
872
|
+
const sessionId = container.getAttribute("data-conversation-session");
|
|
873
|
+
const structuralAnchor = (host) => {
|
|
874
|
+
const last = host.lastElementChild;
|
|
875
|
+
return last instanceof HTMLElement ? last : void 0;
|
|
876
|
+
};
|
|
877
|
+
const fromTrigger = (trigger) => {
|
|
878
|
+
const controls = trigger.closest(COMPOSER_CONTROLS_SELECTOR);
|
|
879
|
+
const host = controls instanceof HTMLElement ? controls : trigger.parentElement;
|
|
880
|
+
if (!(host instanceof HTMLElement)) return void 0;
|
|
881
|
+
return {
|
|
882
|
+
host,
|
|
883
|
+
anchor: directChildOf(trigger, host) ?? structuralAnchor(host)
|
|
884
|
+
};
|
|
885
|
+
};
|
|
886
|
+
const trigger = container.querySelector(MODEL_SELECTOR_BUTTON);
|
|
887
|
+
if (trigger instanceof HTMLElement) {
|
|
888
|
+
const located = fromTrigger(trigger);
|
|
889
|
+
if (located !== void 0) return located;
|
|
890
|
+
warnAutoInfiniteOnce("trigger", "找到了模型选择控件, 但它不在可插入的工具栏里, 「问答自动无限等待」开关改用工具栏容器内的结构定位");
|
|
891
|
+
}
|
|
892
|
+
const controls = container.querySelector(COMPOSER_CONTROLS_SELECTOR);
|
|
893
|
+
if (controls instanceof HTMLElement) {
|
|
894
|
+
warnAutoInfiniteOnce("fallback", "工具栏里暂时没有模型选择控件(尚未挂载或无障碍标签不符), 「问答自动无限等待」开关改用容器内的结构定位");
|
|
895
|
+
return {
|
|
896
|
+
host: controls,
|
|
897
|
+
anchor: structuralAnchor(controls)
|
|
898
|
+
};
|
|
899
|
+
}
|
|
900
|
+
for (const candidate of Array.from(document.querySelectorAll(COMPOSER_CONTROLS_SELECTOR))) {
|
|
901
|
+
if (!(candidate instanceof HTMLElement)) continue;
|
|
902
|
+
const owner = candidate.closest(SESSION_CONTAINER_SELECTOR);
|
|
903
|
+
if (sessionId !== null && owner instanceof HTMLElement && owner.getAttribute("data-conversation-session") !== sessionId) continue;
|
|
904
|
+
warnAutoInfiniteOnce("document", "会话容器里没有工具栏, 「问答自动无限等待」开关已退回全文档范围定位");
|
|
905
|
+
const scoped = candidate.querySelector(MODEL_SELECTOR_BUTTON);
|
|
906
|
+
if (scoped instanceof HTMLElement) {
|
|
907
|
+
const located = fromTrigger(scoped);
|
|
908
|
+
if (located !== void 0) return located;
|
|
909
|
+
}
|
|
910
|
+
return {
|
|
911
|
+
host: candidate,
|
|
912
|
+
anchor: structuralAnchor(candidate)
|
|
913
|
+
};
|
|
914
|
+
}
|
|
915
|
+
warnAutoInfiniteOnce("missing", "定位不到模型选择控件与工具栏容器, 「问答自动无限等待」开关未渲染, 请核对宿主 DOM 结构");
|
|
916
|
+
};
|
|
917
|
+
const ensureAutoInfiniteButtonIn = (container) => {
|
|
918
|
+
const located = locateAutoInfiniteAnchor(container);
|
|
919
|
+
if (located === void 0) return;
|
|
920
|
+
const { host, anchor } = located;
|
|
921
|
+
const existing = host.querySelector("button.dqtp-auto-infinite");
|
|
922
|
+
let button;
|
|
923
|
+
if (existing instanceof HTMLButtonElement) {
|
|
924
|
+
button = existing;
|
|
925
|
+
if (anchor !== void 0 && anchor !== button && button.nextElementSibling !== anchor) anchor.insertAdjacentElement("beforebegin", button);
|
|
926
|
+
} else {
|
|
927
|
+
button = document.createElement("button");
|
|
928
|
+
button.type = "button";
|
|
929
|
+
button.className = AUTO_INFINITE_BUTTON_CLASS;
|
|
930
|
+
if (anchor === void 0) host.appendChild(button);
|
|
931
|
+
else anchor.insertAdjacentElement("beforebegin", button);
|
|
932
|
+
}
|
|
933
|
+
button.setAttribute(AUTO_INFINITE_STATE_ATTRIBUTE, autoInfinite ? "on" : "off");
|
|
934
|
+
button.setAttribute("aria-pressed", autoInfinite ? "true" : "false");
|
|
935
|
+
button.title = pick(AUTO_INFINITE_TITLE_ZH, AUTO_INFINITE_TITLE_EN);
|
|
936
|
+
const label = pick(AUTO_INFINITE_LABEL_ZH, AUTO_INFINITE_LABEL_EN);
|
|
937
|
+
const state = autoInfinite ? pick("开", "On") : pick("关", "Off");
|
|
938
|
+
const text = label + " · " + state;
|
|
939
|
+
if (button.textContent !== text) button.textContent = text;
|
|
940
|
+
};
|
|
941
|
+
const ensureAutoInfiniteButton = (containers = sessionContainers()) => {
|
|
942
|
+
if (options().infiniteWait === false || options().cardEnhancements === false || !isRecognisedLocale()) {
|
|
943
|
+
const roots = containers.length > 0 ? containers : [document];
|
|
944
|
+
for (const root of roots) for (const element of Array.from(root.querySelectorAll("button.dqtp-auto-infinite"))) element.remove();
|
|
945
|
+
return;
|
|
946
|
+
}
|
|
947
|
+
const targets = containers.length > 0 ? containers : [document.documentElement];
|
|
948
|
+
for (const container of targets) ensureAutoInfiniteButtonIn(container);
|
|
949
|
+
};
|
|
950
|
+
const toggleAutoInfinite = () => {
|
|
951
|
+
autoInfinite = !autoInfinite;
|
|
952
|
+
writeAutoInfinite(autoInfinite);
|
|
953
|
+
ensureAutoInfiniteButton();
|
|
954
|
+
if (autoInfinite) scan();
|
|
955
|
+
};
|
|
956
|
+
const hideHostWaitStatus = (card) => {
|
|
957
|
+
for (const element of Array.from(card.querySelectorAll(WAIT_STATUS_SELECTOR))) if (element instanceof HTMLElement) element.classList.add(HIDDEN_CONTROL_CLASS);
|
|
958
|
+
};
|
|
959
|
+
const hideHostControls = (card) => {
|
|
960
|
+
for (const element of Array.from(card.querySelectorAll(CARD_BUTTON_SELECTOR))) {
|
|
961
|
+
const label = element.getAttribute("aria-label") ?? element.getAttribute("title") ?? "";
|
|
962
|
+
if (COLLAPSE_CONTROL_LABELS.includes(label)) {
|
|
963
|
+
element.classList.add(HIDDEN_CONTROL_CLASS);
|
|
964
|
+
continue;
|
|
965
|
+
}
|
|
966
|
+
if (TAKE_TIME_TEXTS.includes(element.textContent?.trim() ?? "")) element.classList.add(HIDDEN_CONTROL_CLASS);
|
|
967
|
+
}
|
|
968
|
+
};
|
|
969
|
+
const disabledOriginals = /* @__PURE__ */ new WeakMap();
|
|
970
|
+
const disableControl = (element) => {
|
|
971
|
+
if (!disabledOriginals.has(element)) disabledOriginals.set(element, element.disabled);
|
|
972
|
+
element.disabled = true;
|
|
973
|
+
};
|
|
974
|
+
const restoreDisabled = (root) => {
|
|
975
|
+
const controls = root.querySelectorAll("textarea, input, button");
|
|
976
|
+
for (const control of Array.from(controls)) {
|
|
977
|
+
const previous = disabledOriginals.get(control);
|
|
978
|
+
if (previous === void 0) continue;
|
|
979
|
+
control.disabled = previous;
|
|
980
|
+
disabledOriginals.delete(control);
|
|
981
|
+
}
|
|
982
|
+
};
|
|
983
|
+
const lockCard = (card) => {
|
|
984
|
+
if (lockedCards.has(card)) return;
|
|
985
|
+
lockedCards.add(card);
|
|
986
|
+
card.classList.add(CARD_LOCKED_CLASS);
|
|
987
|
+
for (const field of Array.from(card.querySelectorAll(CARD_INPUT_SELECTOR))) if (field instanceof HTMLTextAreaElement || field instanceof HTMLInputElement) disableControl(field);
|
|
988
|
+
for (const button of Array.from(card.querySelectorAll(CARD_BUTTON_SELECTOR))) {
|
|
989
|
+
if (!(button instanceof HTMLButtonElement)) continue;
|
|
990
|
+
const text = button.textContent?.trim() ?? "";
|
|
991
|
+
if (SUBMIT_TEXTS.includes(text) || NEXT_QUESTION_TEXTS.includes(text) || SKIP_QUESTION_TEXTS.includes(text)) disableControl(button);
|
|
992
|
+
}
|
|
993
|
+
};
|
|
994
|
+
const hideCard = (card) => {
|
|
995
|
+
const key = card.getAttribute("data-question-key") ?? "";
|
|
996
|
+
const callId = parseCardKey(key)?.callId ?? key;
|
|
997
|
+
const marked = card.getAttribute(CARD_DISMISSED_ATTRIBUTE) === key;
|
|
998
|
+
if (!marked) card.setAttribute(CARD_DISMISSED_ATTRIBUTE, key);
|
|
999
|
+
if (!marked && !dismissedCallIds.has(callId)) {
|
|
1000
|
+
dismissedCallIds.add(callId);
|
|
1001
|
+
for (const button of Array.from(card.querySelectorAll(CARD_BUTTON_SELECTOR))) {
|
|
1002
|
+
const label = (button.getAttribute("aria-label") ?? button.getAttribute("title") ?? "").trim();
|
|
1003
|
+
if (!CLOSE_PANEL_LABELS.includes(label)) continue;
|
|
1004
|
+
button.click();
|
|
1005
|
+
break;
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
card.classList.add(HIDDEN_CONTROL_CLASS);
|
|
1009
|
+
};
|
|
1010
|
+
const restoreCard = (card) => {
|
|
1011
|
+
card.classList.remove(HIDDEN_CONTROL_CLASS);
|
|
1012
|
+
for (const element of Array.from(card.querySelectorAll("." + HIDDEN_CONTROL_CLASS))) element.classList.remove(HIDDEN_CONTROL_CLASS);
|
|
1013
|
+
for (const element of Array.from(card.querySelectorAll("[data-dqtp-host-wait]"))) element.removeAttribute(HIDDEN_WAIT_ATTRIBUTE);
|
|
1014
|
+
for (const element of Array.from(card.querySelectorAll("[data-dqtp-countdown], button.dqtp-infinite-wait"))) element.remove();
|
|
1015
|
+
restoreDisabled(card);
|
|
1016
|
+
card.classList.remove(CARD_LOCKED_CLASS);
|
|
1017
|
+
lockedCards.delete(card);
|
|
1018
|
+
};
|
|
1019
|
+
const restoredCards = /* @__PURE__ */ new WeakMap();
|
|
1020
|
+
const restoreCardOnce = (card, key, extra = "") => {
|
|
1021
|
+
const callId = parseCardKey(card.getAttribute("data-question-key"))?.callId;
|
|
1022
|
+
const state = (callId !== void 0 && timedOut.has(callId) ? "t" : "") + (callId !== void 0 && passive.has(callId) ? "p" : "");
|
|
1023
|
+
const signature = key + ":" + state + ":" + extra;
|
|
1024
|
+
if (restoredCards.get(card) === signature) return;
|
|
1025
|
+
restoreCard(card);
|
|
1026
|
+
restoredCards.set(card, signature);
|
|
1027
|
+
};
|
|
1028
|
+
function hideHostWaitText(root) {
|
|
1029
|
+
for (const element of Array.from(root.querySelectorAll("span, div"))) {
|
|
1030
|
+
if (!(element instanceof HTMLElement)) continue;
|
|
1031
|
+
if (element.hasAttribute("data-dqtp-countdown") || element.querySelector("button") !== null) continue;
|
|
1032
|
+
const text = (element.textContent ?? "").trim();
|
|
1033
|
+
if (text === "" || text.length > 40) continue;
|
|
1034
|
+
if (!HOST_WAIT_PATTERNS.some((pattern) => text.includes(pattern))) continue;
|
|
1035
|
+
element.classList.add(HIDDEN_CONTROL_CLASS);
|
|
1036
|
+
element.setAttribute(HIDDEN_WAIT_ATTRIBUTE, "");
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
const decorateCard = (card, identity) => {
|
|
1040
|
+
rememberCallSession(identity);
|
|
1041
|
+
if (!isRecognisedLocale()) return;
|
|
1042
|
+
if (passive.has(identity.callId)) return;
|
|
1043
|
+
if (!deadlines.has(identity.callId) && !paused.has(identity.callId)) for (const element of Array.from(card.querySelectorAll(WAIT_STATUS_SELECTOR))) {
|
|
1044
|
+
if (!(element instanceof HTMLElement)) continue;
|
|
1045
|
+
const statusText = element.textContent?.trim() ?? "";
|
|
1046
|
+
if (PAUSED_TEXT_PATTERNS.some((pattern) => pattern.test(statusText))) break;
|
|
1047
|
+
const total = parseRemainingSeconds(statusText);
|
|
1048
|
+
if (total === void 0) continue;
|
|
1049
|
+
deadlines.set(identity.callId, Date.now() + total * 1e3);
|
|
1050
|
+
break;
|
|
1051
|
+
}
|
|
1052
|
+
const key = QUESTION_CARD_KEY_PREFIX + identity.sessionId + ":" + identity.callId;
|
|
1053
|
+
const frozen = paused.get(identity.callId);
|
|
1054
|
+
const deadline = deadlines.get(identity.callId);
|
|
1055
|
+
const expired = frozen === void 0 && deadline !== void 0 && Date.now() >= deadline;
|
|
1056
|
+
const live = frozen !== void 0 || deadline !== void 0;
|
|
1057
|
+
if (live && !expired && autoInfinite && isRecognisedLocale() && deadline !== void 0 && frozen === void 0 && options().infiniteWait && !timedOut.has(identity.callId) && !syncUnavailable.has(identity.callId) && !draftSyncFailed.has(identity.callId) && !autoInfiniteAttempted.has(identity.callId)) {
|
|
1058
|
+
autoInfiniteAttempted.add(identity.callId);
|
|
1059
|
+
pauseCountdown(card, identity);
|
|
1060
|
+
return;
|
|
1061
|
+
}
|
|
1062
|
+
if (expired && !timedOut.has(identity.callId)) {
|
|
1063
|
+
restoreCardOnce(card, key, "expired");
|
|
1064
|
+
return;
|
|
1065
|
+
}
|
|
1066
|
+
if (!live) {
|
|
1067
|
+
restoreCardOnce(card, key);
|
|
1068
|
+
if (timedOut.has(identity.callId)) {
|
|
1069
|
+
lockCard(card);
|
|
1070
|
+
hideCard(card);
|
|
1071
|
+
}
|
|
1072
|
+
return;
|
|
1073
|
+
}
|
|
1074
|
+
restoredCards.delete(card);
|
|
1075
|
+
const countdown = ensureCountdown(card, key);
|
|
1076
|
+
if (countdown === void 0) {
|
|
1077
|
+
warnCountdownOnce();
|
|
1078
|
+
restoreCardOnce(card, key, "no-countdown");
|
|
1079
|
+
return;
|
|
1080
|
+
}
|
|
1081
|
+
hideHostWaitStatus(card);
|
|
1082
|
+
hideHostWaitText(card);
|
|
1083
|
+
hideHostControls(card);
|
|
1084
|
+
if (countdown !== void 0) {
|
|
1085
|
+
let remainingMs;
|
|
1086
|
+
if (frozen !== void 0) remainingMs = frozen;
|
|
1087
|
+
else if (deadline !== void 0) remainingMs = Math.max(0, deadline - Date.now());
|
|
1088
|
+
if (draftSyncFailed.has(identity.callId)) {
|
|
1089
|
+
const text = pick(DRAFT_SYNC_FAILED_TEXT_ZH, DRAFT_SYNC_FAILED_TEXT_EN);
|
|
1090
|
+
if (countdown.textContent !== text) countdown.textContent = text;
|
|
1091
|
+
countdown.classList.remove(HIDDEN_CONTROL_CLASS, COUNTDOWN_PAUSED_CLASS);
|
|
1092
|
+
countdown.classList.toggle(COUNTDOWN_WARNING_CLASS, !expired);
|
|
1093
|
+
} else if (draftTooLarge.has(identity.callId)) {
|
|
1094
|
+
const text = pick(DRAFT_TOO_LARGE_TEXT_ZH, DRAFT_TOO_LARGE_TEXT_EN);
|
|
1095
|
+
if (countdown.textContent !== text) countdown.textContent = text;
|
|
1096
|
+
countdown.classList.remove(HIDDEN_CONTROL_CLASS, COUNTDOWN_PAUSED_CLASS);
|
|
1097
|
+
countdown.classList.toggle(COUNTDOWN_WARNING_CLASS, !expired);
|
|
1098
|
+
} else if (syncUnavailable.has(identity.callId)) {
|
|
1099
|
+
const text = pick(SYNC_UNAVAILABLE_TEXT_ZH, SYNC_UNAVAILABLE_TEXT_EN);
|
|
1100
|
+
if (countdown.textContent !== text) countdown.textContent = text;
|
|
1101
|
+
countdown.classList.remove(HIDDEN_CONTROL_CLASS, COUNTDOWN_PAUSED_CLASS);
|
|
1102
|
+
countdown.classList.toggle(COUNTDOWN_WARNING_CLASS, !expired);
|
|
1103
|
+
} else if (remainingMs === void 0 || timedOut.has(identity.callId) || expired) {
|
|
1104
|
+
countdown.textContent = "";
|
|
1105
|
+
countdown.classList.add(HIDDEN_CONTROL_CLASS);
|
|
1106
|
+
countdown.classList.remove(COUNTDOWN_WARNING_CLASS, COUNTDOWN_PAUSED_CLASS);
|
|
1107
|
+
} else {
|
|
1108
|
+
const next = countdownText(remainingMs, frozen !== void 0);
|
|
1109
|
+
if (countdown.textContent !== next) countdown.textContent = next;
|
|
1110
|
+
countdown.classList.remove(HIDDEN_CONTROL_CLASS);
|
|
1111
|
+
const seconds = Math.ceil(remainingMs / 1e3);
|
|
1112
|
+
countdown.classList.toggle(COUNTDOWN_WARNING_CLASS, frozen === void 0 && seconds <= options().warningSeconds);
|
|
1113
|
+
countdown.classList.toggle(COUNTDOWN_PAUSED_CLASS, frozen !== void 0);
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
const button = ensureInfiniteButton(card, countdown);
|
|
1117
|
+
if (button !== void 0) {
|
|
1118
|
+
const hidden = !options().infiniteWait || timedOut.has(identity.callId) || expired || frozen !== void 0 || syncUnavailable.has(identity.callId) || draftSyncFailed.has(identity.callId);
|
|
1119
|
+
button.classList.toggle(HIDDEN_CONTROL_CLASS, hidden);
|
|
1120
|
+
const label = pick(INFINITE_WAIT_LABEL_ZH, INFINITE_WAIT_LABEL_EN);
|
|
1121
|
+
if (button.textContent !== label) button.textContent = label;
|
|
1122
|
+
}
|
|
1123
|
+
if (timedOut.has(identity.callId)) {
|
|
1124
|
+
lockCard(card);
|
|
1125
|
+
hideCard(card);
|
|
1126
|
+
}
|
|
1127
|
+
};
|
|
1128
|
+
const clickHostTakeTime = (identity) => {
|
|
1129
|
+
const card = findCard(identity);
|
|
1130
|
+
if (card === void 0) return false;
|
|
1131
|
+
for (const element of Array.from(card.querySelectorAll(CARD_BUTTON_SELECTOR))) {
|
|
1132
|
+
if (!TAKE_TIME_TEXTS.includes(element.textContent?.trim() ?? "")) continue;
|
|
1133
|
+
element.click();
|
|
1134
|
+
return true;
|
|
1135
|
+
}
|
|
1136
|
+
return false;
|
|
1137
|
+
};
|
|
1138
|
+
const pauseCountdown = (card, identity) => {
|
|
1139
|
+
if (paused.has(identity.callId)) return;
|
|
1140
|
+
const deadline = deadlines.get(identity.callId);
|
|
1141
|
+
paused.set(identity.callId, deadline === void 0 ? 0 : Math.max(0, deadline - Date.now()));
|
|
1142
|
+
decorateCard(card, identity);
|
|
1143
|
+
reportInfiniteWait(identity, deadlines.get(identity.callId)).then((reply) => {
|
|
1144
|
+
if (disposed) return;
|
|
1145
|
+
if (reply.ok) {
|
|
1146
|
+
if (timedOut.has(identity.callId)) return;
|
|
1147
|
+
if (clickHostTakeTime(identity)) return;
|
|
1148
|
+
paused.delete(identity.callId);
|
|
1149
|
+
syncUnavailable.add(identity.callId);
|
|
1150
|
+
reportInfiniteWait(identity, void 0, false);
|
|
1151
|
+
console.warn("[question-timeout-protection] 宿主没有可用的暂停控件, 「无限等待」无法让宿主停止计时, 本次问答改由宿主原版处理");
|
|
1152
|
+
const unsettled = findCard(identity);
|
|
1153
|
+
if (unsettled !== void 0) decorateCard(unsettled, identity);
|
|
1154
|
+
return;
|
|
1155
|
+
}
|
|
1156
|
+
if (reply.reason === "expired") {
|
|
1157
|
+
paused.delete(identity.callId);
|
|
1158
|
+
console.warn("[question-timeout-protection] 「无限等待」上报时提问已到期, 保持超时结果");
|
|
1159
|
+
const current = findCard(identity);
|
|
1160
|
+
if (current !== void 0) decorateCard(current, identity);
|
|
1161
|
+
return;
|
|
1162
|
+
}
|
|
1163
|
+
paused.delete(identity.callId);
|
|
1164
|
+
syncUnavailable.add(identity.callId);
|
|
1165
|
+
console.warn("[question-timeout-protection] 「无限等待」状态上报失败, 本次问答改由宿主原版处理");
|
|
1166
|
+
const current = findCard(identity);
|
|
1167
|
+
if (current !== void 0) decorateCard(current, identity);
|
|
1168
|
+
});
|
|
1169
|
+
};
|
|
1170
|
+
const placePanel = (root, anchor) => {
|
|
1171
|
+
const card = root.firstElementChild;
|
|
1172
|
+
if (!(card instanceof HTMLElement)) return;
|
|
1173
|
+
const rect = anchor.getBoundingClientRect();
|
|
1174
|
+
const width = card.offsetWidth;
|
|
1175
|
+
const height = card.offsetHeight;
|
|
1176
|
+
const viewWidth = window.innerWidth;
|
|
1177
|
+
const viewHeight = window.innerHeight;
|
|
1178
|
+
let left = rect.left + rect.width / 2 - width / 2;
|
|
1179
|
+
left = Math.max(8, Math.min(left, viewWidth - width - 8));
|
|
1180
|
+
let top = rect.bottom + 8;
|
|
1181
|
+
if (top + height > viewHeight - 8) {
|
|
1182
|
+
const above = rect.top - 8 - height;
|
|
1183
|
+
top = above >= 8 ? above : Math.max(8, viewHeight - height - 8);
|
|
1184
|
+
}
|
|
1185
|
+
root.style.left = String(Math.round(left)) + "px";
|
|
1186
|
+
root.style.top = String(Math.round(top)) + "px";
|
|
1187
|
+
};
|
|
1188
|
+
const repositionPanel = () => {
|
|
1189
|
+
if (panel === void 0 || panelAnchor === void 0) return;
|
|
1190
|
+
if (!panelAnchor.isConnected) {
|
|
1191
|
+
closeDraftPanel();
|
|
1192
|
+
return;
|
|
1193
|
+
}
|
|
1194
|
+
const rect = panelAnchor.getBoundingClientRect();
|
|
1195
|
+
if (rect.bottom < 0 || rect.top > window.innerHeight) {
|
|
1196
|
+
closeDraftPanel();
|
|
1197
|
+
return;
|
|
1198
|
+
}
|
|
1199
|
+
placePanel(panel, panelAnchor);
|
|
1200
|
+
};
|
|
1201
|
+
const closeDraftPanel = () => {
|
|
1202
|
+
if (panel === void 0) return;
|
|
1203
|
+
panel.remove();
|
|
1204
|
+
panel = void 0;
|
|
1205
|
+
panelAnchor = void 0;
|
|
1206
|
+
window.removeEventListener("scroll", repositionPanel, true);
|
|
1207
|
+
window.removeEventListener("resize", repositionPanel);
|
|
1208
|
+
document.removeEventListener("keydown", onPanelKeyDown, true);
|
|
1209
|
+
document.removeEventListener("pointerdown", onPanelPointerDown, true);
|
|
1210
|
+
};
|
|
1211
|
+
function onPanelKeyDown(event) {
|
|
1212
|
+
if (!(event instanceof KeyboardEvent) || event.key !== "Escape") return;
|
|
1213
|
+
closeDraftPanel();
|
|
1214
|
+
}
|
|
1215
|
+
function onPanelPointerDown(event) {
|
|
1216
|
+
if (panel === void 0) return;
|
|
1217
|
+
const target = event.target;
|
|
1218
|
+
if (!(target instanceof Node)) return;
|
|
1219
|
+
if (panel.contains(target)) return;
|
|
1220
|
+
if (panelAnchor !== void 0 && panelAnchor.contains(target)) return;
|
|
1221
|
+
closeDraftPanel();
|
|
1222
|
+
}
|
|
1223
|
+
const openDraftPanel = (draft, anchor) => {
|
|
1224
|
+
closeDraftPanel();
|
|
1225
|
+
const root = document.createElement("div");
|
|
1226
|
+
root.id = DRAFT_PANEL_ID;
|
|
1227
|
+
const card = document.createElement("div");
|
|
1228
|
+
card.className = "dqtp-draft-card";
|
|
1229
|
+
const title = document.createElement("div");
|
|
1230
|
+
title.className = "dqtp-draft-title";
|
|
1231
|
+
title.textContent = pick(DRAFT_PANEL_TITLE_ZH, DRAFT_PANEL_TITLE_EN);
|
|
1232
|
+
const note = document.createElement("div");
|
|
1233
|
+
note.className = "dqtp-draft-note";
|
|
1234
|
+
note.textContent = pick(DRAFT_PANEL_NOTE_ZH, DRAFT_PANEL_NOTE_EN);
|
|
1235
|
+
const body = document.createElement("div");
|
|
1236
|
+
body.className = "dqtp-draft-body";
|
|
1237
|
+
const blocks = draft.questions.map((question, index) => {
|
|
1238
|
+
const item = draft.items[index] ?? emptyItem();
|
|
1239
|
+
const heading = (question.header ?? question.question ?? "").trim();
|
|
1240
|
+
const content = hasItemContent(item) ? composeItem(item) : pick(DRAFT_PANEL_UNANSWERED_ZH, DRAFT_PANEL_UNANSWERED_EN);
|
|
1241
|
+
return (heading === "" ? "" : heading + "\n") + content;
|
|
1242
|
+
});
|
|
1243
|
+
body.textContent = blocks.length === 0 ? pick(DRAFT_PANEL_EMPTY_ZH, DRAFT_PANEL_EMPTY_EN) : blocks.join("\n\n");
|
|
1244
|
+
card.appendChild(title);
|
|
1245
|
+
card.appendChild(note);
|
|
1246
|
+
card.appendChild(body);
|
|
1247
|
+
root.appendChild(card);
|
|
1248
|
+
document.body.appendChild(root);
|
|
1249
|
+
panel = root;
|
|
1250
|
+
panelAnchor = anchor;
|
|
1251
|
+
placePanel(root, anchor);
|
|
1252
|
+
window.addEventListener("scroll", repositionPanel, true);
|
|
1253
|
+
window.addEventListener("resize", repositionPanel);
|
|
1254
|
+
document.addEventListener("keydown", onPanelKeyDown, true);
|
|
1255
|
+
document.addEventListener("pointerdown", onPanelPointerDown, true);
|
|
1256
|
+
};
|
|
1257
|
+
const resolveCallId = (row) => {
|
|
1258
|
+
const host = row.closest(TOOL_CALL_ID_SELECTOR);
|
|
1259
|
+
if (!(host instanceof HTMLElement)) return void 0;
|
|
1260
|
+
const value = host.getAttribute(TOOL_CALL_ID_ATTRIBUTE);
|
|
1261
|
+
return value === null || value === "" ? void 0 : value;
|
|
1262
|
+
};
|
|
1263
|
+
const ensureBubble = (row) => {
|
|
1264
|
+
const found = row.querySelector("button.dqtp-question-bubble");
|
|
1265
|
+
if (found instanceof HTMLElement) return found;
|
|
1266
|
+
const button = document.createElement("button");
|
|
1267
|
+
button.type = "button";
|
|
1268
|
+
button.className = BUBBLE_BUTTON_CLASS;
|
|
1269
|
+
const anchor = row.querySelector(PANEL_BUTTON_SELECTOR);
|
|
1270
|
+
if (anchor instanceof HTMLElement && anchor.parentElement !== null) anchor.parentElement.insertBefore(button, anchor.nextSibling);
|
|
1271
|
+
else row.appendChild(button);
|
|
1272
|
+
return button;
|
|
1273
|
+
};
|
|
1274
|
+
const findHostReopen = (row) => {
|
|
1275
|
+
for (const button of Array.from(row.querySelectorAll(CARD_BUTTON_SELECTOR))) {
|
|
1276
|
+
if (button.classList.contains("dqtp-question-bubble")) continue;
|
|
1277
|
+
const label = (button.getAttribute("aria-label") ?? button.getAttribute("title") ?? "").trim();
|
|
1278
|
+
const text = (button.textContent ?? "").trim();
|
|
1279
|
+
if (ASK_REOPEN_LABELS.includes(label) || ASK_REOPEN_LABELS.includes(text)) return button;
|
|
1280
|
+
}
|
|
1281
|
+
};
|
|
1282
|
+
const hideHostReopen = (row) => {
|
|
1283
|
+
const button = findHostReopen(row);
|
|
1284
|
+
if (button !== void 0) button.classList.add(HIDDEN_CONTROL_CLASS);
|
|
1285
|
+
};
|
|
1286
|
+
const showHostReopen = (row) => {
|
|
1287
|
+
const button = findHostReopen(row);
|
|
1288
|
+
if (button !== void 0) button.classList.remove(HIDDEN_CONTROL_CLASS);
|
|
1289
|
+
};
|
|
1290
|
+
const showHostWaitText = (row) => {
|
|
1291
|
+
for (const element of Array.from(row.querySelectorAll("[data-dqtp-host-wait]"))) {
|
|
1292
|
+
if (!(element instanceof HTMLElement)) continue;
|
|
1293
|
+
element.classList.remove(HIDDEN_CONTROL_CLASS);
|
|
1294
|
+
element.removeAttribute(HIDDEN_WAIT_ATTRIBUTE);
|
|
1295
|
+
}
|
|
1296
|
+
};
|
|
1297
|
+
const decoratedRows = /* @__PURE__ */ new WeakMap();
|
|
1298
|
+
const decorateRow = (row) => {
|
|
1299
|
+
const callId = resolveCallId(row);
|
|
1300
|
+
if (callId === void 0) return;
|
|
1301
|
+
const plain = !timedOut.has(callId) && !passive.has(callId) && !drafts.has(callId);
|
|
1302
|
+
const signature = callId + (row.querySelector("button.dqtp-question-bubble") !== null ? "1" : "0") + (timedOut.has(callId) ? "T" : "-") + (passive.has(callId) ? "P" : "-");
|
|
1303
|
+
if (plain && decoratedRows.get(row) === signature) return;
|
|
1304
|
+
if (!timedOut.has(callId)) {
|
|
1305
|
+
showHostReopen(row);
|
|
1306
|
+
showHostWaitText(row);
|
|
1307
|
+
}
|
|
1308
|
+
const button = ensureBubble(row);
|
|
1309
|
+
if (button === void 0) return;
|
|
1310
|
+
if (passive.has(callId)) {
|
|
1311
|
+
button.classList.add(HIDDEN_CONTROL_CLASS);
|
|
1312
|
+
decoratedRows.set(row, signature);
|
|
1313
|
+
return;
|
|
1314
|
+
}
|
|
1315
|
+
const draft = drafts.get(callId);
|
|
1316
|
+
const hidden = draft === void 0;
|
|
1317
|
+
button.classList.toggle(HIDDEN_CONTROL_CLASS, hidden);
|
|
1318
|
+
if (draft === void 0) {
|
|
1319
|
+
if (timedOut.has(callId)) {
|
|
1320
|
+
hideHostReopen(row);
|
|
1321
|
+
hideHostWaitText(row);
|
|
1322
|
+
}
|
|
1323
|
+
decoratedRows.set(row, signature);
|
|
1324
|
+
return;
|
|
1325
|
+
}
|
|
1326
|
+
const label = pick(BUBBLE_DRAFT_LABEL_ZH, BUBBLE_DRAFT_LABEL_EN);
|
|
1327
|
+
if (button.textContent !== label) button.textContent = label;
|
|
1328
|
+
if (button.getAttribute("data-dqtp-bubble") !== "draft") button.setAttribute(BUBBLE_STATE_ATTRIBUTE, BUBBLE_STATE_DRAFT);
|
|
1329
|
+
if (timedOut.has(callId)) {
|
|
1330
|
+
hideHostReopen(row);
|
|
1331
|
+
hideHostWaitText(row);
|
|
1332
|
+
}
|
|
1333
|
+
decoratedRows.set(row, signature);
|
|
1334
|
+
};
|
|
1335
|
+
const closeCardAfterTimeout = (identity) => {
|
|
1336
|
+
const card = findCard(identity);
|
|
1337
|
+
if (card === void 0) return;
|
|
1338
|
+
lockCard(card);
|
|
1339
|
+
hideCard(card);
|
|
1340
|
+
};
|
|
1341
|
+
const enforceTimeoutOutcome = (card, identity) => {
|
|
1342
|
+
if (timedOut.has(identity.callId)) {
|
|
1343
|
+
lockCard(card);
|
|
1344
|
+
hideCard(card);
|
|
1345
|
+
return;
|
|
1346
|
+
}
|
|
1347
|
+
if (!passive.has(identity.callId)) return;
|
|
1348
|
+
restoreCardOnce(card, QUESTION_CARD_KEY_PREFIX + identity.sessionId + ":" + identity.callId);
|
|
1349
|
+
};
|
|
1350
|
+
const reportCurrentDraft = (identity, card) => {
|
|
1351
|
+
if (!options().draftFallback) return;
|
|
1352
|
+
if (draftSyncFailed.has(identity.callId)) return;
|
|
1353
|
+
const ignoredAt = draftIgnored.get(identity.callId);
|
|
1354
|
+
if (ignoredAt !== void 0) {
|
|
1355
|
+
if (Date.now() - ignoredAt < DRAFT_IGNORED_RETRY_MS) return;
|
|
1356
|
+
draftIgnored.delete(identity.callId);
|
|
1357
|
+
}
|
|
1358
|
+
const tooLargeAt = draftTooLargeAt.get(identity.callId);
|
|
1359
|
+
if (tooLargeAt !== void 0) {
|
|
1360
|
+
if (Date.now() - tooLargeAt < 5e3) return;
|
|
1361
|
+
draftTooLargeAt.delete(identity.callId);
|
|
1362
|
+
}
|
|
1363
|
+
if (timedOut.has(identity.callId) || passive.has(identity.callId)) return;
|
|
1364
|
+
if (!callActive.has(identity.callId)) return;
|
|
1365
|
+
rememberCallSession(identity);
|
|
1366
|
+
const progress = readProgress(card);
|
|
1367
|
+
const projected = projectedCounts.get(identity.callId) ?? 1;
|
|
1368
|
+
const count = Math.max(progress?.count ?? 1, projected);
|
|
1369
|
+
const items = mergeDrafts(gateDraftSelections(readHostDrafts(identity, count), identity.callId), readLiveDrafts(card, count, readLiveSelected(card, identity.callId)), lastDrafts.get(identity.callId), count, progress?.index);
|
|
1370
|
+
const last = draftReportedAt.get(identity.callId);
|
|
1371
|
+
const now = Date.now();
|
|
1372
|
+
if (last !== void 0 && now - last < DRAFT_REPORT_INTERVAL_MS) return;
|
|
1373
|
+
draftReportedAt.set(identity.callId, now);
|
|
1374
|
+
const answers = items.map((item, index) => {
|
|
1375
|
+
const custom = item.custom.trim();
|
|
1376
|
+
return {
|
|
1377
|
+
index,
|
|
1378
|
+
selected: [...item.selected],
|
|
1379
|
+
...custom === "" ? {} : { custom }
|
|
1380
|
+
};
|
|
1381
|
+
});
|
|
1382
|
+
const callId = identity.callId;
|
|
1383
|
+
reportDraftSnapshot(identity, answers).then((reply) => {
|
|
1384
|
+
if (disposed) return;
|
|
1385
|
+
const settled = !callActive.has(callId);
|
|
1386
|
+
if (reply.ok) {
|
|
1387
|
+
draftSyncFailures.delete(callId);
|
|
1388
|
+
draftSyncFailed.delete(callId);
|
|
1389
|
+
if (reply.ignored === true) {
|
|
1390
|
+
draftIgnored.set(callId, Date.now());
|
|
1391
|
+
return;
|
|
1392
|
+
}
|
|
1393
|
+
draftTooLargeAt.delete(callId);
|
|
1394
|
+
if (draftTooLarge.delete(callId)) {
|
|
1395
|
+
const card = findCard(identity);
|
|
1396
|
+
if (card !== void 0) decorateCard(card, identity);
|
|
1397
|
+
}
|
|
1398
|
+
return;
|
|
1399
|
+
}
|
|
1400
|
+
if (settled) return;
|
|
1401
|
+
if (reply.reason === "too-large") {
|
|
1402
|
+
draftTooLargeAt.set(identity.callId, Date.now());
|
|
1403
|
+
if (!draftTooLarge.has(identity.callId)) {
|
|
1404
|
+
draftTooLarge.add(identity.callId);
|
|
1405
|
+
console.warn("[question-timeout-protection] 草稿快照超过通道上限, 本次未能送达");
|
|
1406
|
+
const card = findCard(identity);
|
|
1407
|
+
if (card !== void 0) decorateCard(card, identity);
|
|
1408
|
+
}
|
|
1409
|
+
return;
|
|
1410
|
+
}
|
|
1411
|
+
const failures = (draftSyncFailures.get(identity.callId) ?? 0) + 1;
|
|
1412
|
+
draftSyncFailures.set(identity.callId, failures);
|
|
1413
|
+
if (failures < 3) return;
|
|
1414
|
+
if (draftSyncFailed.has(identity.callId)) return;
|
|
1415
|
+
draftSyncFailed.add(identity.callId);
|
|
1416
|
+
console.warn("[question-timeout-protection] 草稿快照上报连续失败, 本次问答改由宿主原版处理");
|
|
1417
|
+
const current = findCard(identity);
|
|
1418
|
+
if (current !== void 0) decorateCard(current, identity);
|
|
1419
|
+
});
|
|
1420
|
+
};
|
|
1421
|
+
const evaluateRow = (identity, row) => {
|
|
1422
|
+
rememberCallSession(identity);
|
|
1423
|
+
const card = findCard(identity);
|
|
1424
|
+
if (handled.has(row.callId)) return;
|
|
1425
|
+
const stored = lastDrafts.get(row.callId);
|
|
1426
|
+
if (card === void 0 && stored === void 0) {
|
|
1427
|
+
deferred.set(row.callId, row);
|
|
1428
|
+
return;
|
|
1429
|
+
}
|
|
1430
|
+
handled.add(row.callId);
|
|
1431
|
+
deferred.delete(row.callId);
|
|
1432
|
+
const count = row.questions.length;
|
|
1433
|
+
const liveIndex = card === void 0 ? void 0 : readProgress(card)?.index;
|
|
1434
|
+
const items = mergeDrafts(gateDraftSelections(readHostDrafts(identity, count), identity.callId), readLiveDrafts(card, count, card === void 0 ? [] : readLiveSelected(card, row.callId)), stored, count, liveIndex);
|
|
1435
|
+
lastDrafts.set(row.callId, items);
|
|
1436
|
+
const current = options();
|
|
1437
|
+
if (!(current.draftFallback && !syncUnavailable.has(row.callId) && !draftSyncFailed.has(row.callId) && items.some(hasItemContent))) {
|
|
1438
|
+
if (current.draftFallback && !syncUnavailable.has(row.callId) && !draftSyncFailed.has(row.callId) && !items.some(hasItemContent)) {
|
|
1439
|
+
timedOut.add(row.callId);
|
|
1440
|
+
drafts.delete(row.callId);
|
|
1441
|
+
if (card !== void 0) {
|
|
1442
|
+
lockCard(card);
|
|
1443
|
+
hideCard(card);
|
|
1444
|
+
}
|
|
1445
|
+
return;
|
|
1446
|
+
}
|
|
1447
|
+
passive.add(row.callId);
|
|
1448
|
+
timedOut.delete(row.callId);
|
|
1449
|
+
drafts.delete(row.callId);
|
|
1450
|
+
if (card !== void 0) restoreCard(card);
|
|
1451
|
+
return;
|
|
1452
|
+
}
|
|
1453
|
+
timedOut.add(row.callId);
|
|
1454
|
+
if (card !== void 0) lockCard(card);
|
|
1455
|
+
drafts.set(row.callId, {
|
|
1456
|
+
sessionId: identity.sessionId,
|
|
1457
|
+
callId: row.callId,
|
|
1458
|
+
questions: row.questions,
|
|
1459
|
+
items
|
|
1460
|
+
});
|
|
1461
|
+
pruneDrafts();
|
|
1462
|
+
closeCardAfterTimeout(identity);
|
|
1463
|
+
};
|
|
1464
|
+
const evaluateProjection = (sessionId, snapshot) => {
|
|
1465
|
+
if (typeof snapshot !== "object" || snapshot === null) return;
|
|
1466
|
+
const active = snapshot.active;
|
|
1467
|
+
if (!Array.isArray(active)) return;
|
|
1468
|
+
const live = /* @__PURE__ */ new Set();
|
|
1469
|
+
for (const row of active) {
|
|
1470
|
+
if (typeof row.callId !== "string") continue;
|
|
1471
|
+
live.add(row.callId);
|
|
1472
|
+
callActive.add(row.callId);
|
|
1473
|
+
callTouchedAt.set(row.callId, Date.now());
|
|
1474
|
+
if (!Array.isArray(row.questions)) continue;
|
|
1475
|
+
projectedCounts.set(row.callId, row.questions.length);
|
|
1476
|
+
projectedQuestions.set(row.callId, row.questions);
|
|
1477
|
+
if (row.state !== "continued") continue;
|
|
1478
|
+
evaluateRow({
|
|
1479
|
+
sessionId,
|
|
1480
|
+
callId: row.callId
|
|
1481
|
+
}, row);
|
|
1482
|
+
}
|
|
1483
|
+
for (const callId of knownCallIds()) {
|
|
1484
|
+
if (live.has(callId)) continue;
|
|
1485
|
+
if (callSessionOf(callId) !== sessionId) continue;
|
|
1486
|
+
forgetCall(callId);
|
|
1487
|
+
}
|
|
1488
|
+
};
|
|
1489
|
+
const subscribeWarned = /* @__PURE__ */ new Set();
|
|
1490
|
+
const evaluateWarned = /* @__PURE__ */ new Set();
|
|
1491
|
+
const polledSessions = /* @__PURE__ */ new Map();
|
|
1492
|
+
const subscriptionSeen = /* @__PURE__ */ new Map();
|
|
1493
|
+
const subscriptionMissing = /* @__PURE__ */ new Map();
|
|
1494
|
+
const enforcePolledLimit = () => {
|
|
1495
|
+
while (polledSessions.size > 8) {
|
|
1496
|
+
let oldestSession;
|
|
1497
|
+
let oldestAt = Number.POSITIVE_INFINITY;
|
|
1498
|
+
for (const [id, seenAt] of subscriptionSeen) {
|
|
1499
|
+
if (seenAt >= oldestAt) continue;
|
|
1500
|
+
if (!polledSessions.has(id)) continue;
|
|
1501
|
+
oldestAt = seenAt;
|
|
1502
|
+
oldestSession = id;
|
|
1503
|
+
}
|
|
1504
|
+
if (oldestSession === void 0) break;
|
|
1505
|
+
polledSessions.delete(oldestSession);
|
|
1506
|
+
subscriptionSeen.delete(oldestSession);
|
|
1507
|
+
subscribeWarned.delete(oldestSession);
|
|
1508
|
+
evaluateWarned.delete(oldestSession);
|
|
1509
|
+
console.warn("[question-timeout-protection] 会话轮询兜底数量达到上限, 已释放最久未出现的会话, 该会话的超时兜底可能不再生效");
|
|
1510
|
+
}
|
|
1511
|
+
};
|
|
1512
|
+
const ensureSubscription = (sessionId) => {
|
|
1513
|
+
if (subscriptions.has(sessionId)) return;
|
|
1514
|
+
if (polledSessions.has(sessionId)) {
|
|
1515
|
+
if (subscriptions.size >= 8) return;
|
|
1516
|
+
polledSessions.delete(sessionId);
|
|
1517
|
+
}
|
|
1518
|
+
const face = resolveProjectionFace(ctx, sessionId);
|
|
1519
|
+
if (face === void 0) {
|
|
1520
|
+
if (!subscribeWarned.has(sessionId)) {
|
|
1521
|
+
subscribeWarned.add(sessionId);
|
|
1522
|
+
console.warn("[question-timeout-protection] 无法订阅提问投影, 该会话的超时兜底不会生效");
|
|
1523
|
+
}
|
|
1524
|
+
return;
|
|
1525
|
+
}
|
|
1526
|
+
const evaluate = () => {
|
|
1527
|
+
if (disposed) return;
|
|
1528
|
+
try {
|
|
1529
|
+
evaluateProjection(sessionId, face.getSnapshot?.());
|
|
1530
|
+
} catch (error) {
|
|
1531
|
+
if (!evaluateWarned.has(sessionId)) {
|
|
1532
|
+
evaluateWarned.add(sessionId);
|
|
1533
|
+
console.warn("[question-timeout-protection] 评估提问投影时出错, 该会话的兜底可能受影响", error);
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
};
|
|
1537
|
+
if (typeof face.subscribe !== "function") {
|
|
1538
|
+
polledSessions.set(sessionId, evaluate);
|
|
1539
|
+
subscriptionSeen.set(sessionId, Date.now());
|
|
1540
|
+
if (!subscribeWarned.has(sessionId)) {
|
|
1541
|
+
subscribeWarned.add(sessionId);
|
|
1542
|
+
console.warn("[question-timeout-protection] 提问投影不支持订阅, 改用轮询兜底");
|
|
1543
|
+
}
|
|
1544
|
+
enforcePolledLimit();
|
|
1545
|
+
evaluate();
|
|
1546
|
+
return;
|
|
1547
|
+
}
|
|
1548
|
+
subscriptions.set(sessionId, face.subscribe(evaluate));
|
|
1549
|
+
subscriptionSeen.set(sessionId, Date.now());
|
|
1550
|
+
while (subscriptions.size > 8) {
|
|
1551
|
+
let oldestSession;
|
|
1552
|
+
let oldestAt = Number.POSITIVE_INFINITY;
|
|
1553
|
+
for (const [id, seenAt] of subscriptionSeen) {
|
|
1554
|
+
if (!subscriptions.has(id)) continue;
|
|
1555
|
+
if (seenAt < oldestAt) {
|
|
1556
|
+
oldestAt = seenAt;
|
|
1557
|
+
oldestSession = id;
|
|
1558
|
+
}
|
|
1559
|
+
}
|
|
1560
|
+
if (oldestSession === void 0) break;
|
|
1561
|
+
subscriptions.get(oldestSession)?.();
|
|
1562
|
+
subscriptions.delete(oldestSession);
|
|
1563
|
+
subscriptionSeen.delete(oldestSession);
|
|
1564
|
+
const evictedSession = oldestSession;
|
|
1565
|
+
polledSessions.set(evictedSession, () => {
|
|
1566
|
+
const face = resolveProjectionFace(ctx, evictedSession);
|
|
1567
|
+
if (face === void 0) return;
|
|
1568
|
+
evaluateProjection(evictedSession, face.getSnapshot?.());
|
|
1569
|
+
});
|
|
1570
|
+
subscriptionSeen.set(evictedSession, Date.now());
|
|
1571
|
+
enforcePolledLimit();
|
|
1572
|
+
console.warn("[question-timeout-protection] 会话订阅数量达到上限, 已把最久未出现的会话改为轮询兜底, 该会话的超时接管会延后生效");
|
|
1573
|
+
}
|
|
1574
|
+
evaluate();
|
|
1575
|
+
};
|
|
1576
|
+
const onDocumentClick = (event) => {
|
|
1577
|
+
const target = event.target;
|
|
1578
|
+
if (!(target instanceof Element)) return;
|
|
1579
|
+
const option = target.closest("button[role=\"radio\"], button[role=\"checkbox\"]");
|
|
1580
|
+
if (option instanceof HTMLElement) {
|
|
1581
|
+
const card = option.closest(QUESTION_CARD_SELECTOR);
|
|
1582
|
+
if (card instanceof HTMLElement) {
|
|
1583
|
+
const identity = parseCardKey(card.getAttribute("data-question-key"));
|
|
1584
|
+
if (identity !== void 0) {
|
|
1585
|
+
rememberCallSession(identity);
|
|
1586
|
+
const index = readProgress(card)?.index ?? 0;
|
|
1587
|
+
const touched = optionInteractions.get(identity.callId) ?? /* @__PURE__ */ new Set();
|
|
1588
|
+
touched.add(index);
|
|
1589
|
+
optionInteractions.set(identity.callId, touched);
|
|
1590
|
+
}
|
|
1591
|
+
}
|
|
1592
|
+
}
|
|
1593
|
+
const button = target.closest(CARD_BUTTON_SELECTOR);
|
|
1594
|
+
if (!(button instanceof HTMLElement)) return;
|
|
1595
|
+
if (button.classList.contains("dqtp-auto-infinite")) {
|
|
1596
|
+
event.preventDefault();
|
|
1597
|
+
event.stopPropagation();
|
|
1598
|
+
toggleAutoInfinite();
|
|
1599
|
+
return;
|
|
1600
|
+
}
|
|
1601
|
+
if (button.classList.contains("dqtp-infinite-wait")) {
|
|
1602
|
+
event.preventDefault();
|
|
1603
|
+
event.stopPropagation();
|
|
1604
|
+
const card = button.closest(QUESTION_CARD_SELECTOR);
|
|
1605
|
+
const identity = parseCardKey(card instanceof HTMLElement ? card.getAttribute("data-question-key") : null);
|
|
1606
|
+
if (card instanceof HTMLElement && identity !== void 0) pauseCountdown(card, identity);
|
|
1607
|
+
return;
|
|
1608
|
+
}
|
|
1609
|
+
if (button.classList.contains("dqtp-question-bubble")) {
|
|
1610
|
+
event.preventDefault();
|
|
1611
|
+
event.stopPropagation();
|
|
1612
|
+
const row = button.closest(ASK_ROW_SELECTOR);
|
|
1613
|
+
if (!(row instanceof HTMLElement)) return;
|
|
1614
|
+
const callId = resolveCallId(row);
|
|
1615
|
+
if (callId === void 0) return;
|
|
1616
|
+
const draft = drafts.get(callId);
|
|
1617
|
+
if (draft === void 0) return;
|
|
1618
|
+
if (panel !== void 0 && panelAnchor === button) closeDraftPanel();
|
|
1619
|
+
else openDraftPanel(draft, button);
|
|
1620
|
+
return;
|
|
1621
|
+
}
|
|
1622
|
+
if (button.closest("[class*=\"_footerActions\"]") === null) return;
|
|
1623
|
+
if (!SUBMIT_TEXTS.includes(button.textContent?.trim() ?? "")) return;
|
|
1624
|
+
const card = button.closest(QUESTION_CARD_SELECTOR);
|
|
1625
|
+
const identity = parseCardKey(card instanceof HTMLElement ? card.getAttribute("data-question-key") : null);
|
|
1626
|
+
if (identity === void 0) return;
|
|
1627
|
+
paused.delete(identity.callId);
|
|
1628
|
+
deadlines.delete(identity.callId);
|
|
1629
|
+
lastDrafts.delete(identity.callId);
|
|
1630
|
+
};
|
|
1631
|
+
document.addEventListener("click", onDocumentClick, true);
|
|
1632
|
+
const visibleSessionIds = (containers) => {
|
|
1633
|
+
const ids = [];
|
|
1634
|
+
for (const element of containers) {
|
|
1635
|
+
const id = element.getAttribute("data-conversation-session");
|
|
1636
|
+
if (typeof id === "string" && id !== "" && !ids.includes(id)) ids.push(id);
|
|
1637
|
+
}
|
|
1638
|
+
return ids;
|
|
1639
|
+
};
|
|
1640
|
+
const scanOnce = () => {
|
|
1641
|
+
pruneCallStates();
|
|
1642
|
+
const containers = sessionContainers();
|
|
1643
|
+
const enhancementOn = options().cardEnhancements !== false;
|
|
1644
|
+
nextHostDraftRound();
|
|
1645
|
+
syncEnhancementsClass();
|
|
1646
|
+
ensureAutoInfiniteButton(containers);
|
|
1647
|
+
for (const evaluate of polledSessions.values()) evaluate();
|
|
1648
|
+
const visibleSessions = new Set(visibleSessionIds(containers));
|
|
1649
|
+
for (const sessionId of visibleSessions) ensureSubscription(sessionId);
|
|
1650
|
+
for (const [sessionId, off] of subscriptions) {
|
|
1651
|
+
if (visibleSessions.has(sessionId)) {
|
|
1652
|
+
subscriptionMissing.delete(sessionId);
|
|
1653
|
+
continue;
|
|
1654
|
+
}
|
|
1655
|
+
const misses = (subscriptionMissing.get(sessionId) ?? 0) + 1;
|
|
1656
|
+
if (misses < 600) {
|
|
1657
|
+
subscriptionMissing.set(sessionId, misses);
|
|
1658
|
+
continue;
|
|
1659
|
+
}
|
|
1660
|
+
off();
|
|
1661
|
+
subscriptions.delete(sessionId);
|
|
1662
|
+
subscriptionMissing.delete(sessionId);
|
|
1663
|
+
subscriptionSeen.delete(sessionId);
|
|
1664
|
+
subscribeWarned.delete(sessionId);
|
|
1665
|
+
evaluateWarned.delete(sessionId);
|
|
1666
|
+
}
|
|
1667
|
+
const roots = containers.length > 0 ? containers : [document];
|
|
1668
|
+
const cards = [];
|
|
1669
|
+
for (const root of roots) for (const element of Array.from(root.querySelectorAll(QUESTION_CARD_SELECTOR))) if (element instanceof HTMLElement) cards.push(element);
|
|
1670
|
+
for (const element of cards) {
|
|
1671
|
+
const identity = parseCardKey(element.getAttribute("data-question-key"));
|
|
1672
|
+
if (identity === void 0) continue;
|
|
1673
|
+
const pending = deferred.get(identity.callId);
|
|
1674
|
+
if (pending !== void 0) {
|
|
1675
|
+
deferred.delete(identity.callId);
|
|
1676
|
+
evaluateRow(identity, pending);
|
|
1677
|
+
}
|
|
1678
|
+
subscriptionSeen.set(identity.sessionId, Date.now());
|
|
1679
|
+
ensureSubscription(identity.sessionId);
|
|
1680
|
+
if (enhancementOn && callActive.has(identity.callId)) {
|
|
1681
|
+
const previous = lastDrafts.get(identity.callId);
|
|
1682
|
+
const progress = readProgress(element);
|
|
1683
|
+
const count = Math.max(progress?.count ?? 1, previous?.length ?? 1);
|
|
1684
|
+
lastDrafts.set(identity.callId, mergeDrafts(gateDraftSelections(readHostDrafts(identity, count), identity.callId), readLiveDrafts(element, count, readLiveSelected(element, identity.callId)), previous, count, progress?.index));
|
|
1685
|
+
}
|
|
1686
|
+
if (enhancementOn) decorateCard(element, identity);
|
|
1687
|
+
else restoreCardOnce(element, QUESTION_CARD_KEY_PREFIX + identity.sessionId + ":" + identity.callId);
|
|
1688
|
+
enforceTimeoutOutcome(element, identity);
|
|
1689
|
+
reportCurrentDraft(identity, element);
|
|
1690
|
+
}
|
|
1691
|
+
if (enhancementOn) {
|
|
1692
|
+
for (const root of roots) for (const row of Array.from(root.querySelectorAll(ASK_ROW_SELECTOR))) if (row instanceof HTMLElement) decorateRow(row);
|
|
1693
|
+
} else for (const root of roots) for (const row of Array.from(root.querySelectorAll(ASK_ROW_SELECTOR))) {
|
|
1694
|
+
if (!(row instanceof HTMLElement)) continue;
|
|
1695
|
+
showHostReopen(row);
|
|
1696
|
+
showHostWaitText(row);
|
|
1697
|
+
for (const bubble of Array.from(row.querySelectorAll("button.dqtp-question-bubble"))) bubble.remove();
|
|
1698
|
+
}
|
|
1699
|
+
repositionPanel();
|
|
1700
|
+
};
|
|
1701
|
+
let scanWarned = false;
|
|
1702
|
+
const scan = () => {
|
|
1703
|
+
if (disposed) return;
|
|
1704
|
+
try {
|
|
1705
|
+
scanOnce();
|
|
1706
|
+
} catch (error) {
|
|
1707
|
+
if (scanWarned) return;
|
|
1708
|
+
scanWarned = true;
|
|
1709
|
+
console.warn("[question-timeout-protection] 扫描界面时出错, 本轮跳过", error);
|
|
1710
|
+
}
|
|
1711
|
+
};
|
|
1712
|
+
let scanTimer;
|
|
1713
|
+
const scheduleScan = () => {
|
|
1714
|
+
if (scanTimer !== void 0) return;
|
|
1715
|
+
scanTimer = window.setTimeout(() => {
|
|
1716
|
+
scanTimer = void 0;
|
|
1717
|
+
scan();
|
|
1718
|
+
}, 120);
|
|
1719
|
+
};
|
|
1720
|
+
const onDraftStorageChange = (event) => {
|
|
1721
|
+
const key = event.key;
|
|
1722
|
+
if (typeof key === "string" && !key.startsWith("dsh.user-questions.drafts.v1")) return;
|
|
1723
|
+
invalidateHostDraftCache();
|
|
1724
|
+
scheduleScan();
|
|
1725
|
+
};
|
|
1726
|
+
const onDraftContentChange = (event) => {
|
|
1727
|
+
const target = event.target;
|
|
1728
|
+
if (!(target instanceof Element) || target.closest("[data-question-key]") === null) return;
|
|
1729
|
+
invalidateHostDraftCache();
|
|
1730
|
+
scheduleScan();
|
|
1731
|
+
};
|
|
1732
|
+
window.addEventListener("storage", onDraftStorageChange);
|
|
1733
|
+
document.addEventListener("input", onDraftContentChange, true);
|
|
1734
|
+
document.addEventListener("change", onDraftContentChange, true);
|
|
1735
|
+
const observer = new MutationObserver(scheduleScan);
|
|
1736
|
+
const observeTarget = document.body ?? document.documentElement ?? null;
|
|
1737
|
+
if (observeTarget !== null) observer.observe(observeTarget, {
|
|
1738
|
+
childList: true,
|
|
1739
|
+
subtree: true,
|
|
1740
|
+
characterData: true
|
|
1741
|
+
});
|
|
1742
|
+
else console.warn("[question-timeout-protection] 页面尚无根节点, 本次跳过 DOM 变化观察, 仅靠定时扫描");
|
|
1743
|
+
const timer = window.setInterval(scan, SCAN_INTERVAL_MS);
|
|
1744
|
+
scan();
|
|
1745
|
+
return () => {
|
|
1746
|
+
disposed = true;
|
|
1747
|
+
observer.disconnect();
|
|
1748
|
+
window.clearInterval(timer);
|
|
1749
|
+
if (scanTimer !== void 0) window.clearTimeout(scanTimer);
|
|
1750
|
+
document.removeEventListener("click", onDocumentClick, true);
|
|
1751
|
+
window.removeEventListener("storage", onDraftStorageChange);
|
|
1752
|
+
document.removeEventListener("input", onDraftContentChange, true);
|
|
1753
|
+
document.removeEventListener("change", onDraftContentChange, true);
|
|
1754
|
+
closeDraftPanel();
|
|
1755
|
+
for (const off of subscriptions.values()) off();
|
|
1756
|
+
subscriptions.clear();
|
|
1757
|
+
for (const element of Array.from(document.querySelectorAll(QUESTION_CARD_SELECTOR))) if (element instanceof HTMLElement) {
|
|
1758
|
+
restoreCard(element);
|
|
1759
|
+
element.removeAttribute(CARD_DISMISSED_ATTRIBUTE);
|
|
1760
|
+
}
|
|
1761
|
+
for (const element of Array.from(document.querySelectorAll("[data-dqtp-countdown], button.dqtp-infinite-wait, button.dqtp-question-bubble"))) element.remove();
|
|
1762
|
+
for (const element of Array.from(document.querySelectorAll("." + HIDDEN_CONTROL_CLASS))) element.classList.remove(HIDDEN_CONTROL_CLASS);
|
|
1763
|
+
for (const element of Array.from(document.querySelectorAll("button.dqtp-auto-infinite"))) element.remove();
|
|
1764
|
+
releaseStyle();
|
|
1765
|
+
enhancementUsers -= 1;
|
|
1766
|
+
if (enhancementUsers <= 0) document.documentElement.classList.remove(CARD_ENHANCEMENTS_CLASS);
|
|
1767
|
+
syncUnavailable.clear();
|
|
1768
|
+
autoInfiniteAttempted.clear();
|
|
1769
|
+
dismissedCallIds.clear();
|
|
1770
|
+
subscribeWarned.clear();
|
|
1771
|
+
evaluateWarned.clear();
|
|
1772
|
+
};
|
|
1773
|
+
}
|
|
1774
|
+
// src/client/i18n.ts
|
|
1775
|
+
const ZH = {
|
|
1776
|
+
settingsSummary: "提问超时保护",
|
|
1777
|
+
fallbackSection: "超时兜底",
|
|
1778
|
+
fallbackNote: "提问超时后如何处理已经输入的内容",
|
|
1779
|
+
injectLabel: "注入到上下文",
|
|
1780
|
+
injectDesc: "超时前把草稿快照交给服务端, 到期时随工具结果按题带回模型上下文, 由模型自行决定是否重新提问",
|
|
1781
|
+
fixedSection: "固定特性",
|
|
1782
|
+
fixedNote: "随插件启用状态生效, 不可单独关闭",
|
|
1783
|
+
fixedKeepLabel: "界面保留草稿",
|
|
1784
|
+
fixedKeepDesc: "超时后把已选选项与已输入内容记入提问气泡, 可在气泡里只读查看",
|
|
1785
|
+
cardEnhancementsLabel: "卡片增强",
|
|
1786
|
+
cardEnhancementsDesc: "右上角倒计时与警示闪烁, 单选选项的方形标号填充底色, 提问标识气泡; 只影响界面装饰",
|
|
1787
|
+
infiniteWaitLabel: "无限等待",
|
|
1788
|
+
infiniteWaitDesc: "倒计时右侧新增「无限等待」按钮, 点击后本次问答倒计时暂停, 直到用户提交",
|
|
1789
|
+
draftFallbackLabel: "超时草稿兜底",
|
|
1790
|
+
draftFallbackDesc: "超时后按上面的兜底范围处理已输入的内容",
|
|
1791
|
+
rejected: "保存被拒绝, 已重新读取宿主配置",
|
|
1792
|
+
loading: "正在读取配置",
|
|
1793
|
+
unavailable: "当前部署未暴露该配置"
|
|
1794
|
+
};
|
|
1795
|
+
const EN = {
|
|
1796
|
+
settingsSummary: "Question timeout protection",
|
|
1797
|
+
fallbackSection: "Timeout fallback",
|
|
1798
|
+
fallbackNote: "What happens to the content the user already typed once the wait expires",
|
|
1799
|
+
injectLabel: "Inject into context",
|
|
1800
|
+
injectDesc: "The draft snapshot is sent to the server before the timeout; on expiry it comes back with the tool result per question and enters the model context, so the model can decide whether to ask again",
|
|
1801
|
+
fixedSection: "Fixed features",
|
|
1802
|
+
fixedNote: "Follows the plugin's enabled state and cannot be turned off separately",
|
|
1803
|
+
fixedKeepLabel: "Keep the draft in the UI",
|
|
1804
|
+
fixedKeepDesc: "At timeout, records the selected options and typed text into the question bubble, where they can be viewed read-only",
|
|
1805
|
+
cardEnhancementsLabel: "Card enhancements",
|
|
1806
|
+
cardEnhancementsDesc: "Top-right countdown with warning flash, a background fill on the square marker of the selected single-choice option, and the question bubble; affects decoration only",
|
|
1807
|
+
infiniteWaitLabel: "Wait indefinitely",
|
|
1808
|
+
infiniteWaitDesc: "Adds a \"Wait indefinitely\" button next to the countdown; clicking it pauses the countdown of this question until the user submits",
|
|
1809
|
+
draftFallbackLabel: "Draft fallback at timeout",
|
|
1810
|
+
draftFallbackDesc: "Handles what was typed once the wait expires, per the fallback above",
|
|
1811
|
+
rejected: "The save was rejected, so the host configuration was re-read",
|
|
1812
|
+
loading: "Reading the configuration",
|
|
1813
|
+
unavailable: "This deployment does not expose the setting"
|
|
1814
|
+
};
|
|
1815
|
+
/** 按界面语言返回文案(判定与卡片控件共用) */
|
|
1816
|
+
function clientText(key) {
|
|
1817
|
+
return uiUsesChinese() ? ZH[key] : EN[key];
|
|
1818
|
+
}
|
|
1819
|
+
/** 配置页的文案字典(供宿主 locale 注册) */
|
|
1820
|
+
const SETTINGS_DICTIONARY = {
|
|
1821
|
+
zh: ZH,
|
|
1822
|
+
en: EN
|
|
1823
|
+
};
|
|
1824
|
+
// src/client/settings.tsx
|
|
1825
|
+
const PENDING = {
|
|
1826
|
+
status: "loading",
|
|
1827
|
+
writable: false
|
|
1828
|
+
};
|
|
1829
|
+
const SERVICE_RETRY_INTERVAL_MS = 1e3;
|
|
1830
|
+
const SERVICE_RETRY_LIMIT = 5;
|
|
1831
|
+
const STYLE_FLAG = "dqtpSettings";
|
|
1832
|
+
const SETTINGS_STYLE = `
|
|
1833
|
+
.dqtp-form{display:flex;flex-direction:column;gap:14px;max-width:560px;font-size:13px;line-height:1.55;color:var(--dsw-alias-label-primary,#1f2328)}
|
|
1834
|
+
.dqtp-card{border:1px solid var(--dsw-alias-border-secondary,rgba(127,127,127,.28));border-radius:12px;padding:2px 14px 6px}
|
|
1835
|
+
.dqtp-card-head{display:flex;flex-wrap:wrap;align-items:baseline;gap:6px 10px;padding:11px 0 7px}
|
|
1836
|
+
.dqtp-card-title{font-size:13px;font-weight:600}
|
|
1837
|
+
.dqtp-card-note{font-size:12px;color:var(--dsw-alias-label-secondary,#6b7280)}
|
|
1838
|
+
.dqtp-option{display:flex;align-items:flex-start;gap:10px;padding:9px 0;cursor:pointer}
|
|
1839
|
+
.dqtp-option input{flex:none;width:15px;height:15px;margin:2px 0 0;accent-color:var(--dsw-alias-brand-primary,#3b6cf0);cursor:pointer}
|
|
1840
|
+
.dqtp-option-body{display:flex;flex-direction:column;gap:2px;min-width:0}
|
|
1841
|
+
.dqtp-option-label{font-weight:500}
|
|
1842
|
+
.dqtp-option-desc{font-size:12px;color:var(--dsw-alias-label-secondary,#6b7280)}
|
|
1843
|
+
.dqtp-fixed{display:flex;align-items:flex-start;gap:10px;padding:9px 0}
|
|
1844
|
+
.dqtp-fixed + .dqtp-fixed,.dqtp-option + .dqtp-fixed,.dqtp-fixed + .dqtp-option{border-top:1px solid var(--dsw-alias-border-tertiary,rgba(127,127,127,.16))}
|
|
1845
|
+
.dqtp-fixed-mark{flex:none;width:15px;font-size:12px;line-height:20px;text-align:center;color:var(--dsw-alias-label-secondary,#6b7280)}
|
|
1846
|
+
.dqtp-fixed-label{font-weight:500;color:var(--dsw-alias-label-secondary,#4b5563)}
|
|
1847
|
+
.dqtp-notice{font-size:12px;color:var(--dsw-alias-label-secondary,#6b7280)}
|
|
1848
|
+
.dqtp-problem{font-size:12px;color:var(--dsw-alias-label-danger,#c0392b)}
|
|
1849
|
+
`;
|
|
1850
|
+
let styleUsers = 0;
|
|
1851
|
+
function ensureStyle() {
|
|
1852
|
+
if (typeof document === "undefined") return () => {};
|
|
1853
|
+
styleUsers += 1;
|
|
1854
|
+
if (document.querySelector("style[data-dqtpSettings]") === null) {
|
|
1855
|
+
const style = document.createElement("style");
|
|
1856
|
+
style.dataset[STYLE_FLAG] = "true";
|
|
1857
|
+
style.textContent = SETTINGS_STYLE;
|
|
1858
|
+
document.head.append(style);
|
|
1859
|
+
}
|
|
1860
|
+
let released = false;
|
|
1861
|
+
return () => {
|
|
1862
|
+
if (released) return;
|
|
1863
|
+
released = true;
|
|
1864
|
+
styleUsers -= 1;
|
|
1865
|
+
if (styleUsers > 0) return;
|
|
1866
|
+
document.querySelector("style[data-dqtpSettings]")?.remove();
|
|
1867
|
+
};
|
|
1868
|
+
}
|
|
1869
|
+
const FIXED_ROWS = [
|
|
1870
|
+
{
|
|
1871
|
+
label: "fixedKeepLabel",
|
|
1872
|
+
desc: "fixedKeepDesc"
|
|
1873
|
+
},
|
|
1874
|
+
{
|
|
1875
|
+
label: "cardEnhancementsLabel",
|
|
1876
|
+
desc: "cardEnhancementsDesc"
|
|
1877
|
+
},
|
|
1878
|
+
{
|
|
1879
|
+
label: "infiniteWaitLabel",
|
|
1880
|
+
desc: "infiniteWaitDesc"
|
|
1881
|
+
},
|
|
1882
|
+
{
|
|
1883
|
+
label: "draftFallbackLabel",
|
|
1884
|
+
desc: "draftFallbackDesc"
|
|
1885
|
+
}
|
|
1886
|
+
];
|
|
1887
|
+
function readInject(value) {
|
|
1888
|
+
const mode = value?.fallback ?? "both";
|
|
1889
|
+
return mode === "context" || mode === "both";
|
|
1890
|
+
}
|
|
1891
|
+
function SettingsPage({ host }) {
|
|
1892
|
+
const forms = readClientService(host, "configForms");
|
|
1893
|
+
const [form, setForm] = (0, react.useState)(() => forms?.get(SETTINGS_NAMESPACE));
|
|
1894
|
+
const [snapshot, setSnapshot] = (0, react.useState)(() => form?.getSnapshot() ?? PENDING);
|
|
1895
|
+
const [injectContext, setInjectContext] = (0, react.useState)(() => readInject(snapshot.value));
|
|
1896
|
+
const [busy, setBusy] = (0, react.useState)(false);
|
|
1897
|
+
const [notice, setNotice] = (0, react.useState)("");
|
|
1898
|
+
(0, react.useEffect)(() => ensureStyle(), []);
|
|
1899
|
+
(0, react.useEffect)(() => {
|
|
1900
|
+
if (form !== void 0) return;
|
|
1901
|
+
const current = readClientService(host, "configForms");
|
|
1902
|
+
if (current !== void 0) {
|
|
1903
|
+
setForm(current.get(SETTINGS_NAMESPACE));
|
|
1904
|
+
return;
|
|
1905
|
+
}
|
|
1906
|
+
let attempts = 0;
|
|
1907
|
+
const timer = window.setInterval(() => {
|
|
1908
|
+
attempts += 1;
|
|
1909
|
+
const late = readClientService(host, "configForms");
|
|
1910
|
+
if (late !== void 0) {
|
|
1911
|
+
window.clearInterval(timer);
|
|
1912
|
+
setForm(late.get(SETTINGS_NAMESPACE));
|
|
1913
|
+
return;
|
|
1914
|
+
}
|
|
1915
|
+
if (attempts < SERVICE_RETRY_LIMIT) return;
|
|
1916
|
+
window.clearInterval(timer);
|
|
1917
|
+
setSnapshot((currentSnapshot) => currentSnapshot.status === "loading" ? {
|
|
1918
|
+
status: "unavailable",
|
|
1919
|
+
writable: false
|
|
1920
|
+
} : currentSnapshot);
|
|
1921
|
+
}, SERVICE_RETRY_INTERVAL_MS);
|
|
1922
|
+
return () => window.clearInterval(timer);
|
|
1923
|
+
}, [form, host]);
|
|
1924
|
+
(0, react.useEffect)(() => {
|
|
1925
|
+
if (form === void 0) return;
|
|
1926
|
+
setSnapshot(form.getSnapshot());
|
|
1927
|
+
return form.subscribe(() => setSnapshot(form.getSnapshot()));
|
|
1928
|
+
}, [form]);
|
|
1929
|
+
(0, react.useEffect)(() => {
|
|
1930
|
+
if (busy) return;
|
|
1931
|
+
setInjectContext(readInject(snapshot.value));
|
|
1932
|
+
}, [
|
|
1933
|
+
snapshot.revision,
|
|
1934
|
+
snapshot.status,
|
|
1935
|
+
busy
|
|
1936
|
+
]);
|
|
1937
|
+
const ready = form !== void 0 && snapshot.status === "ready" && snapshot.writable !== false;
|
|
1938
|
+
const disabled = !ready || busy;
|
|
1939
|
+
const toggle = (next) => {
|
|
1940
|
+
if (form === void 0 || !ready || busy) return;
|
|
1941
|
+
setInjectContext(next);
|
|
1942
|
+
setBusy(true);
|
|
1943
|
+
setNotice("");
|
|
1944
|
+
form.mutate([{
|
|
1945
|
+
op: "set",
|
|
1946
|
+
path: ["fallback"],
|
|
1947
|
+
value: next ? "both" : "ui"
|
|
1948
|
+
}], snapshot.revision).then((accepted) => {
|
|
1949
|
+
if (!accepted) {
|
|
1950
|
+
setInjectContext(readInject(form.getSnapshot().value));
|
|
1951
|
+
setNotice(clientText("rejected"));
|
|
1952
|
+
}
|
|
1953
|
+
}).catch(() => {
|
|
1954
|
+
setInjectContext(readInject(form.getSnapshot().value));
|
|
1955
|
+
setNotice(clientText("rejected"));
|
|
1956
|
+
}).finally(() => setBusy(false));
|
|
1957
|
+
};
|
|
1958
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("form", {
|
|
1959
|
+
className: "dqtp-form",
|
|
1960
|
+
onSubmit: (event) => event.preventDefault(),
|
|
1961
|
+
children: [
|
|
1962
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("section", {
|
|
1963
|
+
className: "dqtp-card",
|
|
1964
|
+
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("header", {
|
|
1965
|
+
className: "dqtp-card-head",
|
|
1966
|
+
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
1967
|
+
className: "dqtp-card-title",
|
|
1968
|
+
children: clientText("fallbackSection")
|
|
1969
|
+
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
1970
|
+
className: "dqtp-card-note",
|
|
1971
|
+
children: clientText("fallbackNote")
|
|
1972
|
+
})]
|
|
1973
|
+
}), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("label", {
|
|
1974
|
+
className: "dqtp-option",
|
|
1975
|
+
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("input", {
|
|
1976
|
+
type: "checkbox",
|
|
1977
|
+
disabled,
|
|
1978
|
+
checked: injectContext,
|
|
1979
|
+
onChange: (event) => toggle(event.target.checked)
|
|
1980
|
+
}), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("span", {
|
|
1981
|
+
className: "dqtp-option-body",
|
|
1982
|
+
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
1983
|
+
className: "dqtp-option-label",
|
|
1984
|
+
children: clientText("injectLabel")
|
|
1985
|
+
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
1986
|
+
className: "dqtp-option-desc",
|
|
1987
|
+
children: clientText("injectDesc")
|
|
1988
|
+
})]
|
|
1989
|
+
})]
|
|
1990
|
+
})]
|
|
1991
|
+
}),
|
|
1992
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("section", {
|
|
1993
|
+
className: "dqtp-card",
|
|
1994
|
+
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("header", {
|
|
1995
|
+
className: "dqtp-card-head",
|
|
1996
|
+
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
1997
|
+
className: "dqtp-card-title",
|
|
1998
|
+
children: clientText("fixedSection")
|
|
1999
|
+
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
2000
|
+
className: "dqtp-card-note",
|
|
2001
|
+
children: clientText("fixedNote")
|
|
2002
|
+
})]
|
|
2003
|
+
}), FIXED_ROWS.map((row) => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
|
|
2004
|
+
className: "dqtp-fixed",
|
|
2005
|
+
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
2006
|
+
className: "dqtp-fixed-mark",
|
|
2007
|
+
children: "✓"
|
|
2008
|
+
}), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("span", {
|
|
2009
|
+
className: "dqtp-option-body",
|
|
2010
|
+
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
2011
|
+
className: "dqtp-fixed-label",
|
|
2012
|
+
children: clientText(row.label)
|
|
2013
|
+
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
2014
|
+
className: "dqtp-option-desc",
|
|
2015
|
+
children: clientText(row.desc)
|
|
2016
|
+
})]
|
|
2017
|
+
})]
|
|
2018
|
+
}, row.label))]
|
|
2019
|
+
}),
|
|
2020
|
+
notice === "" ? null : /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
2021
|
+
className: "dqtp-notice",
|
|
2022
|
+
role: "status",
|
|
2023
|
+
children: notice
|
|
2024
|
+
}),
|
|
2025
|
+
ready || busy ? null : /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
|
|
2026
|
+
className: "dqtp-problem",
|
|
2027
|
+
role: "status",
|
|
2028
|
+
children: clientText(snapshot.status === "loading" ? "loading" : "unavailable")
|
|
2029
|
+
})
|
|
2030
|
+
]
|
|
2031
|
+
});
|
|
2032
|
+
}
|
|
2033
|
+
function readClientService(ctx, name) {
|
|
2034
|
+
try {
|
|
2035
|
+
const getter = ctx.get;
|
|
2036
|
+
if (typeof getter === "function") {
|
|
2037
|
+
const value = getter.call(ctx, name);
|
|
2038
|
+
if (value !== void 0 && value !== null) return value;
|
|
2039
|
+
}
|
|
2040
|
+
} catch {}
|
|
2041
|
+
try {
|
|
2042
|
+
const value = ctx[name];
|
|
2043
|
+
if (value !== void 0 && value !== null) return value;
|
|
2044
|
+
} catch {}
|
|
2045
|
+
}
|
|
2046
|
+
/** 注册插件详情页顶部的配置区块 */
|
|
2047
|
+
function registerSettingsPage(ctx) {
|
|
2048
|
+
const slots = readClientService(ctx, "slots");
|
|
2049
|
+
if (slots === void 0 || typeof slots.inject !== "function" || typeof slots.register !== "function") {
|
|
2050
|
+
console.error("[question-timeout-protection] 缺少插槽服务, 配置页未注册");
|
|
2051
|
+
return () => {};
|
|
2052
|
+
}
|
|
2053
|
+
const locale = readClientService(ctx, "locale");
|
|
2054
|
+
const disposers = [];
|
|
2055
|
+
try {
|
|
2056
|
+
if (locale !== void 0 && typeof locale.register === "function") {
|
|
2057
|
+
const off = locale.register(SETTINGS_LOCALE, SETTINGS_DICTIONARY);
|
|
2058
|
+
if (typeof off === "function") disposers.push(off);
|
|
2059
|
+
}
|
|
2060
|
+
} catch {}
|
|
2061
|
+
try {
|
|
2062
|
+
const off = slots.inject(SETTINGS_SLOT, function* register() {
|
|
2063
|
+
yield slots.register({
|
|
2064
|
+
name: SETTINGS_SLOT,
|
|
2065
|
+
key: PLUGIN_NAME,
|
|
2066
|
+
locale: SETTINGS_LOCALE
|
|
2067
|
+
}, (props) => props.view === "summary" ? clientText("settingsSummary") : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(SettingsPage, { host: ctx }));
|
|
2068
|
+
console.info("[question-timeout-protection] 插件配置已注册", SETTINGS_SLOT, PLUGIN_NAME);
|
|
2069
|
+
});
|
|
2070
|
+
if (typeof off === "function") disposers.push(off);
|
|
2071
|
+
} catch (error) {
|
|
2072
|
+
console.error("[question-timeout-protection] 配置页注册失败", error);
|
|
2073
|
+
}
|
|
2074
|
+
return () => {
|
|
2075
|
+
for (const dispose of disposers) dispose();
|
|
2076
|
+
};
|
|
2077
|
+
}
|
|
2078
|
+
// src/client/index.tsx
|
|
2079
|
+
const name = "dsh-question-timeout-protection/client";
|
|
2080
|
+
/**
|
|
2081
|
+
* 只硬注入插槽: 适配器真正需要的是插槽能否注册, 会话绑定、状态上报通道、文案与配置表单
|
|
2082
|
+
* 一律软取。硬注入任何一项缺失都会让整个客户端 apply 不执行, 连倒计时都会一起消失,
|
|
2083
|
+
* 而这些能力缺失时插件本可以降级工作。
|
|
2084
|
+
*/
|
|
2085
|
+
const inject = ["slots"];
|
|
2086
|
+
function resolveOptions(config) {
|
|
2087
|
+
return {
|
|
2088
|
+
warningSeconds: configValue(config?.warningSeconds) ?? 5,
|
|
2089
|
+
cardEnhancements: configValue(config?.cardEnhancements) ?? true,
|
|
2090
|
+
infiniteWait: configValue(config?.infiniteWait) ?? true,
|
|
2091
|
+
draftFallback: configValue(config?.draftFallback) ?? true
|
|
2092
|
+
};
|
|
2093
|
+
}
|
|
2094
|
+
/**
|
|
2095
|
+
* 构造实时配置来源。
|
|
2096
|
+
* 客户端模块不会收到插件配置, 因此以宿主 configForms 的快照为准;
|
|
2097
|
+
* 服务缺失或尚未就绪时回退到启动时的默认值。
|
|
2098
|
+
*/
|
|
2099
|
+
function createOptionsSource(ctx, base) {
|
|
2100
|
+
let form;
|
|
2101
|
+
let warnedMissing = false;
|
|
2102
|
+
const startedAt = Date.now();
|
|
2103
|
+
const degraded = { ...base };
|
|
2104
|
+
return { getOptions() {
|
|
2105
|
+
try {
|
|
2106
|
+
if (form === void 0) form = readClientService(ctx, "configForms")?.get(SETTINGS_NAMESPACE);
|
|
2107
|
+
const value = form?.getSnapshot()?.value;
|
|
2108
|
+
if (value === void 0) {
|
|
2109
|
+
if (!warnedMissing && Date.now() - startedAt > 5e3) {
|
|
2110
|
+
warnedMissing = true;
|
|
2111
|
+
console.warn("[question-timeout-protection] 读不到宿主配置, 本次按 schema 默认值工作");
|
|
2112
|
+
}
|
|
2113
|
+
return degraded;
|
|
2114
|
+
}
|
|
2115
|
+
return {
|
|
2116
|
+
warningSeconds: typeof value.warningSeconds === "number" ? value.warningSeconds : base.warningSeconds,
|
|
2117
|
+
cardEnhancements: typeof value.cardEnhancements === "boolean" ? value.cardEnhancements : base.cardEnhancements,
|
|
2118
|
+
infiniteWait: typeof value.infiniteWait === "boolean" ? value.infiniteWait : base.infiniteWait,
|
|
2119
|
+
draftFallback: typeof value.draftFallback === "boolean" ? value.draftFallback : base.draftFallback
|
|
2120
|
+
};
|
|
2121
|
+
} catch {
|
|
2122
|
+
return degraded;
|
|
2123
|
+
}
|
|
2124
|
+
} };
|
|
2125
|
+
}
|
|
2126
|
+
function apply(rawCtx, config) {
|
|
2127
|
+
const ctx = rawCtx;
|
|
2128
|
+
let disposeAdapter = () => {};
|
|
2129
|
+
let disposeSettings = () => {};
|
|
2130
|
+
try {
|
|
2131
|
+
disposeAdapter = installQuestionTimeoutAdapter(ctx, createOptionsSource(ctx, resolveOptions(config)));
|
|
2132
|
+
} catch (error) {
|
|
2133
|
+
console.error("[question-timeout-protection] 卡片适配器安装失败", error);
|
|
2134
|
+
}
|
|
2135
|
+
try {
|
|
2136
|
+
disposeSettings = registerSettingsPage(ctx);
|
|
2137
|
+
} catch (error) {
|
|
2138
|
+
console.error("[question-timeout-protection] 配置页注册失败", error);
|
|
2139
|
+
}
|
|
2140
|
+
if (typeof ctx.effect === "function") {
|
|
2141
|
+
ctx.effect(() => disposeAdapter);
|
|
2142
|
+
ctx.effect(() => disposeSettings);
|
|
2143
|
+
} else console.warn("[question-timeout-protection] 客户端缺少 effect 生命周期钩子, 适配器将在页面存续期间常驻");
|
|
2144
|
+
}
|
|
2145
|
+
exports.apply = apply;
|
|
2146
|
+
exports.inject = inject;
|
|
2147
|
+
exports.name = name;
|
|
2148
|
+
|
|
2149
|
+
return module.exports; } });
|