@kangyishuai/dsh-btw 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/client.js CHANGED
@@ -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 REFRESH_TRIES = 20;
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,10 @@ window.__ModuleLoader__.load({
49
49
  promoted: "已插入输入框草稿(未发送)",
50
50
  promoteBlocked: "输入框已有内容,先清空再转正",
51
51
  promoteFailed: "生成转正草稿失败",
52
+ sending: "正在等待答案…(通常 20–40 秒)",
53
+ sentPending: "命令已提交,答案稍后出现在对话里",
54
+ expandAnswer: "展开全文",
55
+ collapseAnswer: "收起",
52
56
  scopeAny: "范围:不限",
53
57
  scopeSession: "范围:整个会话",
54
58
  scopeRecent: "范围:最近几轮",
@@ -84,6 +88,10 @@ window.__ModuleLoader__.load({
84
88
  promoted: "Inserted into the composer as a draft (not sent)",
85
89
  promoteBlocked: "Composer already has content; clear it before promoting",
86
90
  promoteFailed: "Could not create the promotion draft",
91
+ sending: "Waiting for the answer… (usually 20–40s)",
92
+ sentPending: "Command submitted; the answer will appear in the conversation",
93
+ expandAnswer: "Show all",
94
+ collapseAnswer: "Collapse",
87
95
  scopeAny: "Scope: any",
88
96
  scopeSession: "Scope: whole session",
89
97
  scopeRecent: "Scope: recent turns",
@@ -112,10 +120,15 @@ window.__ModuleLoader__.load({
112
120
  ".dsh-btw-item{padding:6px 0;border-top:1px solid var(--dsw-alias-border-l1,rgba(128,128,128,.2));}",
113
121
  ".dsh-btw-q{color:var(--dsw-alias-label-secondary,GrayText);}",
114
122
  ".dsh-btw-a{white-space:pre-wrap;word-break:break-word;margin-top:2px;}",
123
+ ".dsh-btw-a.is-clamped{display:-webkit-box;-webkit-box-orient:vertical;-webkit-line-clamp:6;line-clamp:6;overflow:hidden;}",
115
124
  ".dsh-btw-meta{margin-top:2px;color:var(--dsw-alias-label-tertiary,GrayText);font-size:11px;}",
116
125
  ".dsh-btw-dim{color:var(--dsw-alias-label-tertiary,GrayText);padding:6px 0;}",
117
126
  ".dsh-btw-err{color:var(--dsw-alias-state-error-primary,#d9534f);padding:6px 0;}",
118
127
  ".dsh-btw-row{display:flex;gap:6px;margin-top:8px;}",
128
+ ".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));}",
129
+ ".dsh-btw-status.is-busy{color:var(--dsw-alias-label-primary,CanvasText);}",
130
+ ".dsh-btw-spinner{display:inline-block;animation:dsh-btw-spin 1s linear infinite;}",
131
+ "@keyframes dsh-btw-spin{from{transform:rotate(0)}to{transform:rotate(360deg)}}",
119
132
  ".dsh-btw-setrow{display:flex;flex-wrap:wrap;align-items:center;gap:8px;margin:6px 0;}",
120
133
  ".dsh-btw-setlabel{min-width:0;color:var(--dsw-alias-label-secondary,GrayText);}",
121
134
  ".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;}",
@@ -308,6 +321,9 @@ window.__ModuleLoader__.load({
308
321
  const [notice, setNotice] = React.useState("");
309
322
  const [badge, setBadge] = React.useState("");
310
323
  const [scope, setScope] = React.useState("");
324
+ const [expandedAnswers, setExpandedAnswers] = React.useState(
325
+ /** @type {Record<string, boolean>} */ ({}),
326
+ );
311
327
  const aliveRef = React.useRef(true);
312
328
 
313
329
  React.useEffect(
@@ -361,12 +377,20 @@ window.__ModuleLoader__.load({
361
377
  return () => document.removeEventListener("keydown", onKey);
362
378
  }, [open, load]);
363
379
 
364
- /** 发问:新线程走 `/btw <q>`;追问走 `/btw --thread <id> <q>`。 */
380
+ /**
381
+ * 发问:新线程走 `/btw <q>`;追问走 `/btw --thread <id> <q>`。
382
+ *
383
+ * 2026-11-17 修复(实测用户反馈「点了没反应、按钮永久卡死」):
384
+ * 1. 旧实现轮询 20×1.5s=30s,而真实 /btw 耗时 32–40s,永远等不到 → 改为按
385
+ * 实际耗时给足窗口(REFRESH_MAX_MS),并保留「线程数增加即提前结束」。
386
+ * 2. poll 的 `.then()` 没有 `.catch()`,一次 RPC 失败就断链,
387
+ * `setBusy(false)` 永不执行 → 改用 try/finally 兜底,任何路径都恢复按钮。
388
+ */
365
389
  const submit = async (/** @type {string} */ threadId) => {
366
390
  const text = question.trim();
367
391
  if (text === "" || busy) return;
368
392
  setBusy(true);
369
- setNotice("");
393
+ setNotice(t("sending"));
370
394
  const before = Array.isArray(threads) ? threads.length : 0;
371
395
  const flags = [
372
396
  threadId === "" ? "" : `--thread ${threadId}`,
@@ -375,32 +399,36 @@ window.__ModuleLoader__.load({
375
399
  .filter(Boolean)
376
400
  .join(" ");
377
401
  const line = flags === "" ? `/btw ${text}` : `/btw ${flags} ${text}`;
378
- const result = await sendText(sessionId, line);
379
- if (result !== "ok") {
380
- setBusy(false);
381
- setNotice(t("sendFailed"));
382
- return;
383
- }
384
- setQuestion("");
385
- setNotice(t("sent"));
386
- let tries = 0;
387
- const poll = () => {
388
- load().then((/** @type {number} */ count) => {
389
- if (!aliveRef.current) return;
390
- if (count > before) {
391
- setBusy(false);
392
- setNotice("");
393
- return;
402
+ try {
403
+ const result = await sendText(sessionId, line);
404
+ if (result !== "ok") {
405
+ setNotice(t("sendFailed"));
406
+ return;
407
+ }
408
+ setQuestion("");
409
+ setNotice(t("sent"));
410
+ // 轮询到「线程数增加」为止,最长 REFRESH_MAX_MS;单次失败不中断。
411
+ const deadline = Date.now() + REFRESH_MAX_MS;
412
+ let refreshed = false;
413
+ while (Date.now() < deadline && aliveRef.current) {
414
+ await new Promise((resolve) => setTimeout(resolve, REFRESH_INTERVAL_MS));
415
+ if (!aliveRef.current) break;
416
+ let count = before;
417
+ try {
418
+ count = await load();
419
+ } catch {
420
+ count = before; // 单次失败继续轮询,不静默卡死
394
421
  }
395
- tries += 1;
396
- if (tries >= REFRESH_TRIES) {
397
- setBusy(false);
398
- return;
422
+ if (count > before) {
423
+ refreshed = true;
424
+ break;
399
425
  }
400
- setTimeout(poll, REFRESH_INTERVAL_MS);
401
- });
402
- };
403
- setTimeout(poll, REFRESH_INTERVAL_MS);
426
+ }
427
+ setNotice(refreshed ? "" : t("sentPending"));
428
+ } finally {
429
+ // 无论如何都要放开按钮:卡死的按钮是实测最劝退的问题。
430
+ if (aliveRef.current) setBusy(false);
431
+ }
404
432
  };
405
433
 
406
434
  /**
@@ -481,11 +509,42 @@ window.__ModuleLoader__.load({
481
509
  )
482
510
  : null,
483
511
  ),
484
- h(
485
- "div",
486
- { className: "dsh-btw-a", "data-btw-answer": "true" },
487
- String(thread.answer ?? ""),
488
- ),
512
+ (() => {
513
+ // 长答案默认折叠 6 行:实测一条 3000+ 字答案会把浮层撑满,
514
+ // 用户只能看到答案中部、找不到成本与按钮。
515
+ const answerText = String(thread.answer ?? "");
516
+ const key = String(thread.threadId ?? "");
517
+ const isOpen = expandedAnswers[key] === true;
518
+ const needsClamp = answerText.length > 400;
519
+ return h(
520
+ React.Fragment,
521
+ null,
522
+ h(
523
+ "div",
524
+ {
525
+ className: `dsh-btw-a${needsClamp && !isOpen ? " is-clamped" : ""}`,
526
+ "data-btw-answer": "true",
527
+ },
528
+ answerText,
529
+ ),
530
+ needsClamp
531
+ ? h(
532
+ "button",
533
+ {
534
+ type: "button",
535
+ className: "dsh-btw-btn",
536
+ "data-btw-expand": "true",
537
+ onClick: () =>
538
+ setExpandedAnswers((/** @type {any} */ prev) => ({
539
+ ...prev,
540
+ [key]: !isOpen,
541
+ })),
542
+ },
543
+ isOpen ? t("collapseAnswer") : t("expandAnswer"),
544
+ )
545
+ : null,
546
+ );
547
+ })(),
489
548
  h(
490
549
  "div",
491
550
  { className: "dsh-btw-meta", "data-btw-meta": "true" },
@@ -584,6 +643,19 @@ window.__ModuleLoader__.load({
584
643
  t("close"),
585
644
  ),
586
645
  ),
646
+ // 提示条固定在**头部正下方**:旧实现放在浮层最底部,列表一长就被推到
647
+ // 滚动区外(实测浮层底 616px、提示在 918px),用户提交后完全看不到反馈。
648
+ notice === "" && !busy
649
+ ? null
650
+ : h(
651
+ "div",
652
+ {
653
+ className: `dsh-btw-status${busy ? " is-busy" : ""}`,
654
+ "data-btw-notice": "true",
655
+ },
656
+ busy ? h("span", { className: "dsh-btw-spinner" }, "⟳") : null,
657
+ h("span", null, busy ? t("sending") : notice),
658
+ ),
587
659
  failed
588
660
  ? h(
589
661
  "div",
@@ -642,9 +714,6 @@ window.__ModuleLoader__.load({
642
714
  badge === ""
643
715
  ? null
644
716
  : 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
717
  ),
649
718
  );
650
719
  }
package/lib/limits.js CHANGED
@@ -16,8 +16,11 @@ export const LIMITS = {
16
16
  allowedToolsMax: 8,
17
17
  /** 单条线程最多追问几轮(超过提示开新线程) */
18
18
  threadTurns: 10,
19
- /** 转正草稿的最大字符数(插进 composer 前的上限) */
20
- promoteChars: 4000,
19
+ /**
20
+ * 转正草稿的最大字符数。实测 4000 字会把 composer 撑满整屏(用户原话
21
+ * 「输入框把对话都挤没了」),收到 1200 字:够保留结论与要点。
22
+ */
23
+ promoteChars: 1200,
21
24
  };
22
25
 
23
26
  /**
package/lib/threads.js CHANGED
@@ -92,24 +92,60 @@ export function renderPromoteDraft(record, options = {}) {
92
92
  const found = PROMOTE_SECTIONS.find((entry) => entry.key === key);
93
93
  return found === undefined ? key : lang === "en" ? found.en : found.zh;
94
94
  };
95
- const question = String(record?.question ?? "").trim();
96
- const answer = String(record?.answer ?? "").trim();
95
+ const question = stripMarkdown(String(record?.question ?? ""));
96
+ const answer = stripMarkdown(String(record?.answer ?? ""));
97
97
  /** @type {string[]} */
98
98
  const lines = [
99
- `## ${section("question")}`,
99
+ `【${section("question")}】`,
100
100
  question === "" ? "—" : question,
101
101
  "",
102
- `## ${section("answer")}`,
102
+ `【${section("answer")}】`,
103
103
  answer === "" ? "—" : answer,
104
104
  ];
105
- const details = String(options.details ?? "").trim();
106
- if (details !== "") lines.push("", `## ${section("details")}`, details);
107
- const unresolved = String(options.unresolved ?? "").trim();
108
- if (unresolved !== "") lines.push("", `## ${section("unresolved")}`, unresolved);
105
+ const details = stripMarkdown(String(options.details ?? ""));
106
+ if (details !== "") lines.push("", `【${section("details")}】`, details);
107
+ const unresolved = stripMarkdown(String(options.unresolved ?? ""));
108
+ if (unresolved !== "") lines.push("", `【${section("unresolved")}】`, unresolved);
109
109
  const text = lines.join("\n").trim();
110
110
  return text.length > maxChars ? `${text.slice(0, maxChars - 1)}…` : text;
111
111
  }
112
112
 
113
+ /**
114
+ * 去掉常见 Markdown 标记,让文本适合直接放进输入框。
115
+ *
116
+ * 为什么必须做:实测用户点「转正」后,输入框里塞进来的是 `## 我问了什么`、
117
+ * `**每个 PR 上实际出现的检查是 8 个**`、`|---|` 这类源码,用户原话是
118
+ * 「为什么给我一堆井号和竖线?」。我们没有 Markdown 渲染器(ADR 0001 不引入构建),
119
+ * 所以草稿必须自己变成纯文本。
120
+ * @param {string} text
121
+ * @returns {string}
122
+ */
123
+ export function stripMarkdown(text) {
124
+ return (
125
+ String(text ?? "")
126
+ .replace(/^\s{0,3}#{1,6}\s+/gmu, "") // 标题井号
127
+ .replace(/^[ \t]*\|?[\s:|\t-]+\|[\s:|\t-]*$/gmu, "") // 表格分隔行 |---|---|
128
+ // 表格数据行 | a | b | → "a — b"(竖线对普通用户就是噪音)
129
+ .replace(
130
+ /^[ \t]*\|(.+)\|[ \t]*$/gmu,
131
+ (/** @type {string} */ _m, /** @type {string} */ body) =>
132
+ body
133
+ .split("|")
134
+ .map((cell) => cell.trim())
135
+ .filter((cell) => cell !== "")
136
+ .join(" — "),
137
+ )
138
+ .replace(/\*\*([^*\n]+)\*\*/gu, "$1") // **粗体**
139
+ .replace(/(?<![*\w])\*([^*\n]+)\*(?!\*)/gu, "$1") // *斜体*
140
+ .replace(/`{3,}[a-z]*\n?/giu, "") // ``` 代码围栏
141
+ .replace(/`([^`\n]+)`/gu, "$1") // `行内代码`
142
+ .replace(/^\s*[-*+]\s+/gmu, "· ") // 列表符号
143
+ .replace(/^\s*>\s?/gmu, "") // 引用
144
+ .replace(/\n{3,}/gu, "\n\n") // 压掉多余空行
145
+ .trim()
146
+ );
147
+ }
148
+
113
149
  /**
114
150
  * 从答案里猜「关键细节」候选(列表项/代码片段),给转正确认层做默认值。
115
151
  * @param {string} answer
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kangyishuai/dsh-btw",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Read-only side questions that do not interrupt the main task",
5
5
  "keywords": [
6
6
  "dsh",