@kangyishuai/dsh-btw 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/client.js +241 -86
- package/lib/limits.js +5 -2
- package/lib/threads.js +87 -12
- package/package.json +1 -1
package/lib/client.js
CHANGED
|
@@ -27,8 +27,8 @@ window.__ModuleLoader__.load({
|
|
|
27
27
|
const DRAFT_POLL_INTERVAL_MS = 20;
|
|
28
28
|
/** 浮层列出的线程条数上限。 */
|
|
29
29
|
const LIMITS_CLIENT_THREADS = 20;
|
|
30
|
-
/**
|
|
31
|
-
const
|
|
30
|
+
/** 提交后轮询历史,等新记录出现;按实测 /btw 耗时(32–40s)给足 120s 窗口。 */
|
|
31
|
+
const REFRESH_MAX_MS = 120_000;
|
|
32
32
|
const REFRESH_INTERVAL_MS = 1500;
|
|
33
33
|
|
|
34
34
|
const zh = {
|
|
@@ -49,6 +49,12 @@ window.__ModuleLoader__.load({
|
|
|
49
49
|
promoted: "已插入输入框草稿(未发送)",
|
|
50
50
|
promoteBlocked: "输入框已有内容,先清空再转正",
|
|
51
51
|
promoteFailed: "生成转正草稿失败",
|
|
52
|
+
sending: "正在等待答案…(通常 20–40 秒)",
|
|
53
|
+
sentPending: "命令已提交,答案稍后出现在对话里",
|
|
54
|
+
expandAnswer: "展开全文",
|
|
55
|
+
collapseAnswer: "收起",
|
|
56
|
+
newQuestion: "+ 新旁问",
|
|
57
|
+
submitHint: "提交时会以 /btw 命令写入对话",
|
|
52
58
|
scopeAny: "范围:不限",
|
|
53
59
|
scopeSession: "范围:整个会话",
|
|
54
60
|
scopeRecent: "范围:最近几轮",
|
|
@@ -84,6 +90,12 @@ window.__ModuleLoader__.load({
|
|
|
84
90
|
promoted: "Inserted into the composer as a draft (not sent)",
|
|
85
91
|
promoteBlocked: "Composer already has content; clear it before promoting",
|
|
86
92
|
promoteFailed: "Could not create the promotion draft",
|
|
93
|
+
sending: "Waiting for the answer… (usually 20–40s)",
|
|
94
|
+
sentPending: "Command submitted; the answer will appear in the conversation",
|
|
95
|
+
expandAnswer: "Show all",
|
|
96
|
+
collapseAnswer: "Collapse",
|
|
97
|
+
newQuestion: "+ New question",
|
|
98
|
+
submitHint: "Submitting writes a /btw command into the conversation",
|
|
87
99
|
scopeAny: "Scope: any",
|
|
88
100
|
scopeSession: "Scope: whole session",
|
|
89
101
|
scopeRecent: "Scope: recent turns",
|
|
@@ -107,15 +119,21 @@ window.__ModuleLoader__.load({
|
|
|
107
119
|
".dsh-btw-btn{height:24px;padding:0 8px;border-radius:var(--dsw-radius-sm,6px);border:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.35));background:transparent;color:var(--dsw-alias-label-secondary,GrayText);font-size:12px;cursor:pointer;}",
|
|
108
120
|
".dsh-btw-btn:hover{background:var(--dsw-alias-interactive-bg-hover,rgba(128,128,128,.16));color:var(--dsw-alias-label-primary,CanvasText);}",
|
|
109
121
|
".dsh-btw-btn:disabled{opacity:.5;cursor:default;}",
|
|
110
|
-
".dsh-btw-panel{position:
|
|
111
|
-
".dsh-btw-head{display:flex;align-items:center;justify-content:space-between;gap:8px;margin-bottom:8px;font-weight:600;}",
|
|
122
|
+
".dsh-btw-panel{position:fixed;left:50%;top:50%;transform:translate(-50%,-50%);width:min(720px,94vw);max-height:min(74vh,680px);display:flex;flex-direction:column;min-height:0;z-index:2147482000;background:var(--dsw-alias-bg-layer-2,var(--dsw-alias-bg-base,Canvas));color:var(--dsw-alias-label-primary,CanvasText);border:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.35));border-radius:12px;padding:12px 14px;font-size:12px;line-height:1.55;box-shadow:0 18px 48px rgba(0,0,0,.38);}",
|
|
123
|
+
".dsh-btw-head{display:flex;align-items:center;justify-content:space-between;gap:8px;margin-bottom:8px;font-weight:600;flex:none;}",
|
|
124
|
+
".dsh-btw-scroll{flex:1 1 auto;min-height:0;overflow-y:auto;overscroll-behavior:contain;}",
|
|
112
125
|
".dsh-btw-item{padding:6px 0;border-top:1px solid var(--dsw-alias-border-l1,rgba(128,128,128,.2));}",
|
|
113
126
|
".dsh-btw-q{color:var(--dsw-alias-label-secondary,GrayText);}",
|
|
114
127
|
".dsh-btw-a{white-space:pre-wrap;word-break:break-word;margin-top:2px;}",
|
|
128
|
+
".dsh-btw-a.is-clamped{display:-webkit-box;-webkit-box-orient:vertical;-webkit-line-clamp:6;line-clamp:6;overflow:hidden;}",
|
|
115
129
|
".dsh-btw-meta{margin-top:2px;color:var(--dsw-alias-label-tertiary,GrayText);font-size:11px;}",
|
|
116
130
|
".dsh-btw-dim{color:var(--dsw-alias-label-tertiary,GrayText);padding:6px 0;}",
|
|
117
131
|
".dsh-btw-err{color:var(--dsw-alias-state-error-primary,#d9534f);padding:6px 0;}",
|
|
118
132
|
".dsh-btw-row{display:flex;gap:6px;margin-top:8px;}",
|
|
133
|
+
".dsh-btw-status{position:sticky;top:-10px;z-index:2;display:flex;align-items:center;gap:6px;margin:0 0 8px;padding:5px 8px;border-radius:var(--dsw-radius-sm,6px);background:var(--dsw-alias-bg-layer-3,var(--dsw-alias-bg-layer-2,Canvas));color:var(--dsw-alias-label-secondary,GrayText);font-size:12px;box-shadow:0 1px 0 var(--dsw-alias-border-l1,rgba(128,128,128,.2));}",
|
|
134
|
+
".dsh-btw-status.is-busy{color:var(--dsw-alias-label-primary,CanvasText);}",
|
|
135
|
+
".dsh-btw-spinner{display:inline-block;animation:dsh-btw-spin 1s linear infinite;}",
|
|
136
|
+
"@keyframes dsh-btw-spin{from{transform:rotate(0)}to{transform:rotate(360deg)}}",
|
|
119
137
|
".dsh-btw-setrow{display:flex;flex-wrap:wrap;align-items:center;gap:8px;margin:6px 0;}",
|
|
120
138
|
".dsh-btw-setlabel{min-width:0;color:var(--dsw-alias-label-secondary,GrayText);}",
|
|
121
139
|
".dsh-btw-input{flex:1 1 auto;min-width:0;resize:vertical;min-height:52px;padding:6px 8px;border-radius:var(--dsw-radius-sm,6px);border:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.35));background:var(--dsw-alias-bg-base,Canvas);color:var(--dsw-alias-label-primary,CanvasText);font:inherit;}",
|
|
@@ -162,6 +180,41 @@ window.__ModuleLoader__.load({
|
|
|
162
180
|
return () => style.remove();
|
|
163
181
|
}, "btw: styles");
|
|
164
182
|
|
|
183
|
+
/**
|
|
184
|
+
* 按钮(composer 工具行)与浮层(`shell.overlay`)是两个独立组件实例,
|
|
185
|
+
* 共享状态必须提升到 apply 作用域。
|
|
186
|
+
*
|
|
187
|
+
* 为什么浮层改注册到 `shell.overlay`:`conversation.input.overlay` 的 slot 文档写着
|
|
188
|
+
* "fixed overlays must portal to the document body because the docked composer contains them"。
|
|
189
|
+
* 放在 composer 里会被裁剪 —— 实测提示条跑到浮层下方 302px、位置还随对话流滚动漂移。
|
|
190
|
+
* `shell.overlay` 的语义是 "frame-wide floating layer, above every column and outside
|
|
191
|
+
* their scroll containers",换过去从根上解决,不必手写 portal。
|
|
192
|
+
*/
|
|
193
|
+
const store = (() => {
|
|
194
|
+
/** @type {{ open: boolean, sessionId: string }} */
|
|
195
|
+
let state = { open: false, sessionId: "" };
|
|
196
|
+
/** @type {Set<() => void>} */
|
|
197
|
+
const listeners = new Set();
|
|
198
|
+
return {
|
|
199
|
+
get: () => state,
|
|
200
|
+
patch(/** @type {Partial<{ open: boolean, sessionId: string }>} */ next) {
|
|
201
|
+
state = { ...state, ...next };
|
|
202
|
+
for (const listener of listeners) listener();
|
|
203
|
+
},
|
|
204
|
+
subscribe(/** @type {() => void} */ listener) {
|
|
205
|
+
listeners.add(listener);
|
|
206
|
+
return () => listeners.delete(listener);
|
|
207
|
+
},
|
|
208
|
+
};
|
|
209
|
+
})();
|
|
210
|
+
|
|
211
|
+
/** 订阅 store 的极简 hook。 */
|
|
212
|
+
const useStore = () => {
|
|
213
|
+
const [, force] = React.useState(0);
|
|
214
|
+
React.useEffect(() => store.subscribe(() => force((/** @type {number} */ n) => n + 1)), []);
|
|
215
|
+
return store.get();
|
|
216
|
+
};
|
|
217
|
+
|
|
165
218
|
/** 当前界面语言(转正草稿用)。取不到默认 zh。 */
|
|
166
219
|
const langOf = () => {
|
|
167
220
|
try {
|
|
@@ -296,10 +349,44 @@ window.__ModuleLoader__.load({
|
|
|
296
349
|
return "ok";
|
|
297
350
|
};
|
|
298
351
|
|
|
352
|
+
/**
|
|
353
|
+
* composer 工具行里的「旁问」按钮。
|
|
354
|
+
*
|
|
355
|
+
* 它同时负责把当前 sessionId 写进 store —— 浮层注册在 root scope 的
|
|
356
|
+
* `shell.overlay` 上,拿不到 session props,只能由按钮转交。
|
|
357
|
+
*/
|
|
358
|
+
function BtwTrigger(/** @type {any} */ props) {
|
|
359
|
+
const sessionId = typeof props?.sessionId === "string" ? props.sessionId : "";
|
|
360
|
+
const shared = useStore();
|
|
361
|
+
React.useEffect(() => {
|
|
362
|
+
if (shared.sessionId !== sessionId) store.patch({ sessionId });
|
|
363
|
+
}, [sessionId, shared.sessionId]);
|
|
364
|
+
return h(
|
|
365
|
+
"button",
|
|
366
|
+
{
|
|
367
|
+
type: "button",
|
|
368
|
+
className: "dsh-btw-btn",
|
|
369
|
+
"data-btw-toggle": "true",
|
|
370
|
+
title: t("button"),
|
|
371
|
+
onClick: () => store.patch({ open: !store.get().open }),
|
|
372
|
+
},
|
|
373
|
+
t("button"),
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
|
|
299
377
|
/** 浮层:只读列表 + 浮层内发问。 */
|
|
300
378
|
function BtwPanel(/** @type {any} */ props) {
|
|
301
|
-
|
|
302
|
-
const
|
|
379
|
+
void props;
|
|
380
|
+
const shared = useStore();
|
|
381
|
+
const open = shared.open;
|
|
382
|
+
const sessionId = shared.sessionId;
|
|
383
|
+
const setOpen = (/** @type {boolean | ((prev: boolean) => boolean)} */ next) =>
|
|
384
|
+
store.patch({
|
|
385
|
+
open:
|
|
386
|
+
typeof next === "function"
|
|
387
|
+
? /** @type {(prev: boolean) => boolean} */ (next)(store.get().open)
|
|
388
|
+
: next,
|
|
389
|
+
});
|
|
303
390
|
const [threads, setThreads] = React.useState(/** @type {any[] | null} */ (null));
|
|
304
391
|
const [failed, setFailed] = React.useState(false);
|
|
305
392
|
const [activeId, setActiveId] = React.useState("");
|
|
@@ -308,6 +395,12 @@ window.__ModuleLoader__.load({
|
|
|
308
395
|
const [notice, setNotice] = React.useState("");
|
|
309
396
|
const [badge, setBadge] = React.useState("");
|
|
310
397
|
const [scope, setScope] = React.useState("");
|
|
398
|
+
const [expandedAnswers, setExpandedAnswers] = React.useState(
|
|
399
|
+
/** @type {Record<string, boolean>} */ ({}),
|
|
400
|
+
);
|
|
401
|
+
// 新建旁问的输入框默认收起:实测同时出现「线程内追问框 + 底部新建框」时,
|
|
402
|
+
// 用户分不清哪个是哪个(两个输入框视觉几乎一样)。
|
|
403
|
+
const [showNew, setShowNew] = React.useState(false);
|
|
311
404
|
const aliveRef = React.useRef(true);
|
|
312
405
|
|
|
313
406
|
React.useEffect(
|
|
@@ -361,12 +454,20 @@ window.__ModuleLoader__.load({
|
|
|
361
454
|
return () => document.removeEventListener("keydown", onKey);
|
|
362
455
|
}, [open, load]);
|
|
363
456
|
|
|
364
|
-
/**
|
|
457
|
+
/**
|
|
458
|
+
* 发问:新线程走 `/btw <q>`;追问走 `/btw --thread <id> <q>`。
|
|
459
|
+
*
|
|
460
|
+
* 2026-11-17 修复(实测用户反馈「点了没反应、按钮永久卡死」):
|
|
461
|
+
* 1. 旧实现轮询 20×1.5s=30s,而真实 /btw 耗时 32–40s,永远等不到 → 改为按
|
|
462
|
+
* 实际耗时给足窗口(REFRESH_MAX_MS),并保留「线程数增加即提前结束」。
|
|
463
|
+
* 2. poll 的 `.then()` 没有 `.catch()`,一次 RPC 失败就断链,
|
|
464
|
+
* `setBusy(false)` 永不执行 → 改用 try/finally 兜底,任何路径都恢复按钮。
|
|
465
|
+
*/
|
|
365
466
|
const submit = async (/** @type {string} */ threadId) => {
|
|
366
467
|
const text = question.trim();
|
|
367
468
|
if (text === "" || busy) return;
|
|
368
469
|
setBusy(true);
|
|
369
|
-
setNotice("");
|
|
470
|
+
setNotice(t("sending"));
|
|
370
471
|
const before = Array.isArray(threads) ? threads.length : 0;
|
|
371
472
|
const flags = [
|
|
372
473
|
threadId === "" ? "" : `--thread ${threadId}`,
|
|
@@ -375,32 +476,36 @@ window.__ModuleLoader__.load({
|
|
|
375
476
|
.filter(Boolean)
|
|
376
477
|
.join(" ");
|
|
377
478
|
const line = flags === "" ? `/btw ${text}` : `/btw ${flags} ${text}`;
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
479
|
+
try {
|
|
480
|
+
const result = await sendText(sessionId, line);
|
|
481
|
+
if (result !== "ok") {
|
|
482
|
+
setNotice(t("sendFailed"));
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
setQuestion("");
|
|
486
|
+
setNotice(t("sent"));
|
|
487
|
+
// 轮询到「线程数增加」为止,最长 REFRESH_MAX_MS;单次失败不中断。
|
|
488
|
+
const deadline = Date.now() + REFRESH_MAX_MS;
|
|
489
|
+
let refreshed = false;
|
|
490
|
+
while (Date.now() < deadline && aliveRef.current) {
|
|
491
|
+
await new Promise((resolve) => setTimeout(resolve, REFRESH_INTERVAL_MS));
|
|
492
|
+
if (!aliveRef.current) break;
|
|
493
|
+
let count = before;
|
|
494
|
+
try {
|
|
495
|
+
count = await load();
|
|
496
|
+
} catch {
|
|
497
|
+
count = before; // 单次失败继续轮询,不静默卡死
|
|
394
498
|
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
return;
|
|
499
|
+
if (count > before) {
|
|
500
|
+
refreshed = true;
|
|
501
|
+
break;
|
|
399
502
|
}
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
}
|
|
403
|
-
|
|
503
|
+
}
|
|
504
|
+
setNotice(refreshed ? "" : t("sentPending"));
|
|
505
|
+
} finally {
|
|
506
|
+
// 无论如何都要放开按钮:卡死的按钮是实测最劝退的问题。
|
|
507
|
+
if (aliveRef.current) setBusy(false);
|
|
508
|
+
}
|
|
404
509
|
};
|
|
405
510
|
|
|
406
511
|
/**
|
|
@@ -445,19 +550,8 @@ window.__ModuleLoader__.load({
|
|
|
445
550
|
setNotice(t("promoted"));
|
|
446
551
|
};
|
|
447
552
|
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
{
|
|
451
|
-
type: "button",
|
|
452
|
-
className: "dsh-btw-btn",
|
|
453
|
-
"data-btw-toggle": "true",
|
|
454
|
-
title: t("button"),
|
|
455
|
-
onClick: () => setOpen((/** @type {boolean} */ value) => !value),
|
|
456
|
-
},
|
|
457
|
-
t("button"),
|
|
458
|
-
);
|
|
459
|
-
|
|
460
|
-
if (!open) return h("span", { "data-btw-root": "true" }, button);
|
|
553
|
+
// 浮层不画按钮:按钮由 BtwTrigger 注册在 composer 工具行里。
|
|
554
|
+
if (!open) return null;
|
|
461
555
|
|
|
462
556
|
const renderThread = (/** @type {any} */ thread, /** @type {number} */ index) => {
|
|
463
557
|
const expanded = activeId === thread.threadId;
|
|
@@ -481,11 +575,43 @@ window.__ModuleLoader__.load({
|
|
|
481
575
|
)
|
|
482
576
|
: null,
|
|
483
577
|
),
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
578
|
+
(() => {
|
|
579
|
+
// 长答案默认折叠 6 行:实测一条 3000+ 字答案会把浮层撑满,
|
|
580
|
+
// 用户只能看到答案中部、找不到成本与按钮。
|
|
581
|
+
// 优先用 host 清理过的纯文本(保留代码块);没有就回退原文。
|
|
582
|
+
const answerText = String(thread.answerPlain ?? thread.answer ?? "");
|
|
583
|
+
const key = String(thread.threadId ?? "");
|
|
584
|
+
const isOpen = expandedAnswers[key] === true;
|
|
585
|
+
const needsClamp = answerText.length > 400;
|
|
586
|
+
return h(
|
|
587
|
+
React.Fragment,
|
|
588
|
+
null,
|
|
589
|
+
h(
|
|
590
|
+
"div",
|
|
591
|
+
{
|
|
592
|
+
className: `dsh-btw-a${needsClamp && !isOpen ? " is-clamped" : ""}`,
|
|
593
|
+
"data-btw-answer": "true",
|
|
594
|
+
},
|
|
595
|
+
answerText,
|
|
596
|
+
),
|
|
597
|
+
needsClamp
|
|
598
|
+
? h(
|
|
599
|
+
"button",
|
|
600
|
+
{
|
|
601
|
+
type: "button",
|
|
602
|
+
className: "dsh-btw-btn",
|
|
603
|
+
"data-btw-expand": "true",
|
|
604
|
+
onClick: () =>
|
|
605
|
+
setExpandedAnswers((/** @type {any} */ prev) => ({
|
|
606
|
+
...prev,
|
|
607
|
+
[key]: !isOpen,
|
|
608
|
+
})),
|
|
609
|
+
},
|
|
610
|
+
isOpen ? t("collapseAnswer") : t("expandAnswer"),
|
|
611
|
+
)
|
|
612
|
+
: null,
|
|
613
|
+
);
|
|
614
|
+
})(),
|
|
489
615
|
h(
|
|
490
616
|
"div",
|
|
491
617
|
{ className: "dsh-btw-meta", "data-btw-meta": "true" },
|
|
@@ -565,7 +691,6 @@ window.__ModuleLoader__.load({
|
|
|
565
691
|
return h(
|
|
566
692
|
"span",
|
|
567
693
|
{ "data-btw-root": "true" },
|
|
568
|
-
button,
|
|
569
694
|
h(
|
|
570
695
|
"div",
|
|
571
696
|
{ className: "dsh-btw-panel", "data-btw-overlay": "true", role: "dialog" },
|
|
@@ -573,6 +698,16 @@ window.__ModuleLoader__.load({
|
|
|
573
698
|
"div",
|
|
574
699
|
{ className: "dsh-btw-head" },
|
|
575
700
|
h("span", null, t("title")),
|
|
701
|
+
h(
|
|
702
|
+
"button",
|
|
703
|
+
{
|
|
704
|
+
type: "button",
|
|
705
|
+
className: "dsh-btw-btn",
|
|
706
|
+
"data-btw-new": "true",
|
|
707
|
+
onClick: () => setShowNew((/** @type {boolean} */ v) => !v),
|
|
708
|
+
},
|
|
709
|
+
showNew ? t("collapse") : t("newQuestion"),
|
|
710
|
+
),
|
|
576
711
|
h(
|
|
577
712
|
"button",
|
|
578
713
|
{
|
|
@@ -584,43 +719,58 @@ window.__ModuleLoader__.load({
|
|
|
584
719
|
t("close"),
|
|
585
720
|
),
|
|
586
721
|
),
|
|
722
|
+
// 提示条固定在**头部正下方**:旧实现放在浮层最底部,列表一长就被推到
|
|
723
|
+
// 滚动区外(实测浮层底 616px、提示在 918px),用户提交后完全看不到反馈。
|
|
724
|
+
notice === "" && !busy
|
|
725
|
+
? null
|
|
726
|
+
: h(
|
|
727
|
+
"div",
|
|
728
|
+
{
|
|
729
|
+
className: `dsh-btw-status${busy ? " is-busy" : ""}`,
|
|
730
|
+
"data-btw-notice": "true",
|
|
731
|
+
},
|
|
732
|
+
busy ? h("span", { className: "dsh-btw-spinner" }, "⟳") : null,
|
|
733
|
+
h("span", null, busy ? t("sending") : notice),
|
|
734
|
+
),
|
|
587
735
|
failed
|
|
588
736
|
? h(
|
|
589
737
|
"div",
|
|
590
738
|
{ className: "dsh-btw-err", "data-btw-error": "history_unavailable" },
|
|
591
739
|
`${t("failed")} — ${lastRpcError}`,
|
|
592
740
|
)
|
|
593
|
-
: body,
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
741
|
+
: h("div", { className: "dsh-btw-scroll", "data-btw-list": "true" }, body),
|
|
742
|
+
showNew
|
|
743
|
+
? h(
|
|
744
|
+
"div",
|
|
745
|
+
{ className: "dsh-btw-row", "data-btw-newline": "true" },
|
|
746
|
+
h("textarea", {
|
|
747
|
+
className: "dsh-btw-input",
|
|
748
|
+
"data-btw-input": "true",
|
|
749
|
+
placeholder: t("placeholder"),
|
|
750
|
+
value: activeId === "" ? question : "",
|
|
751
|
+
rows: 2,
|
|
752
|
+
onChange: (/** @type {any} */ event) =>
|
|
753
|
+
setQuestion(String(event?.target?.value ?? "")),
|
|
754
|
+
onKeyDown: (/** @type {any} */ event) => {
|
|
755
|
+
if (event?.key === "Enter" && (event.metaKey || event.ctrlKey)) {
|
|
756
|
+
event.preventDefault();
|
|
757
|
+
void submit("");
|
|
758
|
+
}
|
|
759
|
+
},
|
|
760
|
+
}),
|
|
761
|
+
h(
|
|
762
|
+
"button",
|
|
763
|
+
{
|
|
764
|
+
type: "button",
|
|
765
|
+
className: "dsh-btw-btn",
|
|
766
|
+
"data-btw-send": "true",
|
|
767
|
+
disabled: busy || question.trim() === "",
|
|
768
|
+
onClick: () => void submit(""),
|
|
769
|
+
},
|
|
770
|
+
t("send"),
|
|
771
|
+
),
|
|
772
|
+
)
|
|
773
|
+
: null,
|
|
624
774
|
h(
|
|
625
775
|
"div",
|
|
626
776
|
{ className: "dsh-btw-row" },
|
|
@@ -639,12 +789,10 @@ window.__ModuleLoader__.load({
|
|
|
639
789
|
h("option", { value: "selection" }, t("scopeSelection")),
|
|
640
790
|
),
|
|
641
791
|
),
|
|
792
|
+
h("div", { className: "dsh-btw-meta", "data-btw-hint": "true" }, t("submitHint")),
|
|
642
793
|
badge === ""
|
|
643
794
|
? null
|
|
644
795
|
: h("div", { className: "dsh-btw-meta", "data-btw-badge": "true" }, badge),
|
|
645
|
-
notice === ""
|
|
646
|
-
? null
|
|
647
|
-
: h("div", { className: "dsh-btw-dim", "data-btw-notice": "true" }, notice),
|
|
648
796
|
),
|
|
649
797
|
);
|
|
650
798
|
}
|
|
@@ -805,7 +953,14 @@ window.__ModuleLoader__.load({
|
|
|
805
953
|
ctx.slots.inject("conversation.input.left", () =>
|
|
806
954
|
ctx.slots.register(
|
|
807
955
|
{ name: "conversation.input.left", id: "btw-toggle", order: 40, locale: NS },
|
|
808
|
-
(/** @type {any} */ props) => h(
|
|
956
|
+
(/** @type {any} */ props) => h(BtwTrigger, props),
|
|
957
|
+
),
|
|
958
|
+
);
|
|
959
|
+
|
|
960
|
+
// 浮层挂在 shell.overlay(frame-wide,所有列之上、滚动容器之外),不受 composer 裁剪。
|
|
961
|
+
ctx.slots.inject("shell.overlay", () =>
|
|
962
|
+
ctx.slots.register({ name: "shell.overlay", id: "btw-panel", order: 60, locale: NS }, () =>
|
|
963
|
+
h(BtwPanel, null),
|
|
809
964
|
),
|
|
810
965
|
);
|
|
811
966
|
};
|
package/lib/limits.js
CHANGED
|
@@ -16,8 +16,11 @@ export const LIMITS = {
|
|
|
16
16
|
allowedToolsMax: 8,
|
|
17
17
|
/** 单条线程最多追问几轮(超过提示开新线程) */
|
|
18
18
|
threadTurns: 10,
|
|
19
|
-
/**
|
|
20
|
-
|
|
19
|
+
/**
|
|
20
|
+
* 转正草稿的最大字符数。实测 4000 字会把 composer 撑满整屏(用户原话
|
|
21
|
+
* 「输入框把对话都挤没了」),收到 1200 字:够保留结论与要点。
|
|
22
|
+
*/
|
|
23
|
+
promoteChars: 1200,
|
|
21
24
|
};
|
|
22
25
|
|
|
23
26
|
/**
|
package/lib/threads.js
CHANGED
|
@@ -18,10 +18,13 @@ export const PROMOTE_SECTIONS = [
|
|
|
18
18
|
* @returns {string}
|
|
19
19
|
*/
|
|
20
20
|
export function newThreadId(now = Date.now(), rand = Math.random) {
|
|
21
|
-
|
|
21
|
+
// 短 id:浮层发问要走 composer,命令文本(`/btw --thread <id> ...`)会短暂出现在
|
|
22
|
+
// 用户输入框里,id 越短越不刺眼。实测 16 字符的 `t_mwkfmjk1_qrb3` 太显眼。
|
|
23
|
+
// 单机单会话内冲突概率可忽略(时间 36 进制 + 2 位随机)。
|
|
24
|
+
const suffix = Math.floor(rand() * 36 ** 2)
|
|
22
25
|
.toString(36)
|
|
23
|
-
.padStart(
|
|
24
|
-
return `t_${now.toString(36)}
|
|
26
|
+
.padStart(2, "0");
|
|
27
|
+
return `t_${now.toString(36)}${suffix}`;
|
|
25
28
|
}
|
|
26
29
|
|
|
27
30
|
/**
|
|
@@ -92,24 +95,93 @@ export function renderPromoteDraft(record, options = {}) {
|
|
|
92
95
|
const found = PROMOTE_SECTIONS.find((entry) => entry.key === key);
|
|
93
96
|
return found === undefined ? key : lang === "en" ? found.en : found.zh;
|
|
94
97
|
};
|
|
95
|
-
const question = String(record?.question ?? "")
|
|
96
|
-
const answer = String(record?.answer ?? "")
|
|
98
|
+
const question = stripMarkdown(String(record?.question ?? ""));
|
|
99
|
+
const answer = stripMarkdown(String(record?.answer ?? ""));
|
|
97
100
|
/** @type {string[]} */
|
|
98
101
|
const lines = [
|
|
99
|
-
|
|
102
|
+
`【${section("question")}】`,
|
|
100
103
|
question === "" ? "—" : question,
|
|
101
104
|
"",
|
|
102
|
-
|
|
105
|
+
`【${section("answer")}】`,
|
|
103
106
|
answer === "" ? "—" : answer,
|
|
104
107
|
];
|
|
105
|
-
const details = String(options.details ?? "")
|
|
106
|
-
if (details !== "") lines.push("",
|
|
107
|
-
const unresolved = String(options.unresolved ?? "")
|
|
108
|
-
if (unresolved !== "") lines.push("",
|
|
108
|
+
const details = stripMarkdown(String(options.details ?? ""));
|
|
109
|
+
if (details !== "") lines.push("", `【${section("details")}】`, details);
|
|
110
|
+
const unresolved = stripMarkdown(String(options.unresolved ?? ""));
|
|
111
|
+
if (unresolved !== "") lines.push("", `【${section("unresolved")}】`, unresolved);
|
|
109
112
|
const text = lines.join("\n").trim();
|
|
110
113
|
return text.length > maxChars ? `${text.slice(0, maxChars - 1)}…` : text;
|
|
111
114
|
}
|
|
112
115
|
|
|
116
|
+
/**
|
|
117
|
+
* 去掉常见 Markdown 标记,让文本适合直接放进输入框。
|
|
118
|
+
*
|
|
119
|
+
* 为什么必须做:实测用户点「转正」后,输入框里塞进来的是 `## 我问了什么`、
|
|
120
|
+
* `**每个 PR 上实际出现的检查是 8 个**`、`|---|` 这类源码,用户原话是
|
|
121
|
+
* 「为什么给我一堆井号和竖线?」。我们没有 Markdown 渲染器(ADR 0001 不引入构建),
|
|
122
|
+
* 所以草稿必须自己变成纯文本。
|
|
123
|
+
* @param {string} text
|
|
124
|
+
* @returns {string}
|
|
125
|
+
*/
|
|
126
|
+
/**
|
|
127
|
+
* 去掉常见 Markdown 标记,让文本适合直接阅读/放进输入框。
|
|
128
|
+
*
|
|
129
|
+
* @param {string} text
|
|
130
|
+
* @param {{ keepCode?: boolean }} [options] keepCode 默认 true:``` 围栏内的内容原样保留
|
|
131
|
+
* (代码缩进有意义,清掉会毁掉可读性)。
|
|
132
|
+
* @returns {string}
|
|
133
|
+
*/
|
|
134
|
+
export function stripMarkdown(text, options = {}) {
|
|
135
|
+
const raw = String(text ?? "");
|
|
136
|
+
if (options.keepCode === false) return stripAllMarkdown(raw);
|
|
137
|
+
// 先把围栏块抽出占位,避免下面的标记清理破坏代码内容。
|
|
138
|
+
/** @type {string[]} */
|
|
139
|
+
const fences = [];
|
|
140
|
+
const parked = raw.replace(
|
|
141
|
+
/```[a-zA-Z0-9_-]*\n([\s\S]*?)```/gu,
|
|
142
|
+
(/** @type {string} */ _m, /** @type {string} */ body) => {
|
|
143
|
+
fences.push(body.replace(/\n+$/u, ""));
|
|
144
|
+
return `@@BTWFENCE${fences.length - 1}@@`;
|
|
145
|
+
},
|
|
146
|
+
);
|
|
147
|
+
const cleaned = stripAllMarkdown(parked).replace(
|
|
148
|
+
/@@BTWFENCE(\d+)@@/gu,
|
|
149
|
+
(/** @type {string} */ _m, /** @type {string} */ i) => `\n${fences[Number(i)] ?? ""}\n`,
|
|
150
|
+
);
|
|
151
|
+
return cleaned.replace(/\n{3,}/gu, "\n\n").trim();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* 纯标记清理(条目级,不含代码块保护)。
|
|
156
|
+
* @param {string} text
|
|
157
|
+
* @returns {string}
|
|
158
|
+
*/
|
|
159
|
+
function stripAllMarkdown(text) {
|
|
160
|
+
return (
|
|
161
|
+
String(text ?? "")
|
|
162
|
+
.replace(/^\s{0,3}#{1,6}\s+/gmu, "") // 标题井号
|
|
163
|
+
.replace(/^[ \t]*\|?[\s:|\t-]+\|[\s:|\t-]*$/gmu, "") // 表格分隔行 |---|---|
|
|
164
|
+
// 表格数据行 | a | b | → "a — b"(竖线对普通用户就是噪音)
|
|
165
|
+
.replace(
|
|
166
|
+
/^[ \t]*\|(.+)\|[ \t]*$/gmu,
|
|
167
|
+
(/** @type {string} */ _m, /** @type {string} */ body) =>
|
|
168
|
+
body
|
|
169
|
+
.split("|")
|
|
170
|
+
.map((cell) => cell.trim())
|
|
171
|
+
.filter((cell) => cell !== "")
|
|
172
|
+
.join(" — "),
|
|
173
|
+
)
|
|
174
|
+
.replace(/\*\*([^*\n]+)\*\*/gu, "$1") // **粗体**
|
|
175
|
+
.replace(/(?<![*\w])\*([^*\n]+)\*(?!\*)/gu, "$1") // *斜体*
|
|
176
|
+
.replace(/`{3,}[a-z]*\n?/giu, "") // ``` 代码围栏
|
|
177
|
+
.replace(/`([^`\n]+)`/gu, "$1") // `行内代码`
|
|
178
|
+
.replace(/^\s*[-*+]\s+/gmu, "· ") // 列表符号
|
|
179
|
+
.replace(/^\s*>\s?/gmu, "") // 引用
|
|
180
|
+
.replace(/\n{3,}/gu, "\n\n") // 压掉多余空行
|
|
181
|
+
.trim()
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
113
185
|
/**
|
|
114
186
|
* 从答案里猜「关键细节」候选(列表项/代码片段),给转正确认层做默认值。
|
|
115
187
|
* @param {string} answer
|
|
@@ -154,7 +226,7 @@ export function parseThreadFlag(raw) {
|
|
|
154
226
|
/**
|
|
155
227
|
* 线程列表(给浮层展示):按最后一条时间倒序,每项带条数与首问。
|
|
156
228
|
* @param {any[]} records
|
|
157
|
-
* @returns {{ threadId: string, turns: number, question: string, answer: string, lastId: string, cost: any, ms: number, at: number }[]}
|
|
229
|
+
* @returns {{ threadId: string, turns: number, question: string, answer: string, answerPlain: string, lastId: string, cost: any, ms: number, at: number }[]}
|
|
158
230
|
*/
|
|
159
231
|
export function listThreads(records) {
|
|
160
232
|
return groupThreads(records)
|
|
@@ -163,6 +235,9 @@ export function listThreads(records) {
|
|
|
163
235
|
turns: thread.items.length,
|
|
164
236
|
question: String(thread.items[0]?.question ?? ""),
|
|
165
237
|
answer: String(thread.last?.answer ?? ""),
|
|
238
|
+
// 展示用:去掉 Markdown 噪音但保留代码块(列表里显示原文的话,用户看到的是
|
|
239
|
+
// `**粗体**`、`|表|格|`、`|---|`,实测被抱怨「为什么给我一堆井号和竖线」)。
|
|
240
|
+
answerPlain: stripMarkdown(String(thread.last?.answer ?? "")),
|
|
166
241
|
lastId: String(thread.last?.id ?? ""),
|
|
167
242
|
cost: thread.last?.cost ?? null,
|
|
168
243
|
ms: Number(thread.last?.ms ?? 0),
|