@kangyishuai/dsh-btw 0.2.0 → 0.4.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/README.md +23 -6
- package/README.zh-CN.md +25 -6
- package/lib/client.js +899 -282
- package/lib/followup-queue.js +64 -0
- package/lib/format.js +73 -0
- package/lib/limits.js +2 -0
- package/lib/live.js +136 -0
- package/lib/markdown.js +695 -0
- package/lib/service.js +93 -20
- package/lib/subagent.js +11 -0
- package/lib/threads.js +91 -107
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +1 -1
package/lib/service.js
CHANGED
|
@@ -5,8 +5,12 @@
|
|
|
5
5
|
import { Remote, TypertRemoteService } from "@deepseek-ai/dsh-typert-protocol";
|
|
6
6
|
import { renderBadge } from "./badge.js";
|
|
7
7
|
import { parseAllowedTools } from "./config.js";
|
|
8
|
+
import { createFollowUpQueue } from "./followup-queue.js";
|
|
9
|
+
import { formatDuration, formatTokens } from "./format.js";
|
|
8
10
|
import { appendRecord, lastRecord, listRecent } from "./history.js";
|
|
9
11
|
import { clip, LIMITS } from "./limits.js";
|
|
12
|
+
import { createLiveTracker } from "./live.js";
|
|
13
|
+
import { parseMarkdown } from "./markdown.js";
|
|
10
14
|
import { format, loadMessages, pickLanguage } from "./messages.js";
|
|
11
15
|
import { historyFile, resolveDataRoot } from "./paths.js";
|
|
12
16
|
import { loadSettings, saveSettings } from "./settings.js";
|
|
@@ -52,7 +56,7 @@ const FALLBACK = {
|
|
|
52
56
|
cancelled: "The side question was cancelled.",
|
|
53
57
|
tooLong: "That question is too long ({length} characters, limit {max}).",
|
|
54
58
|
threadFull: "This side-question thread is full ({max} turns). Start a new one.",
|
|
55
|
-
meta: "btw · new {newTokens} tokens · cached {cachedTokens} · {ms}
|
|
59
|
+
meta: "btw · new {newTokens} tokens · cached {cachedTokens} · {ms}",
|
|
56
60
|
reopen: "Last side question: {question}",
|
|
57
61
|
};
|
|
58
62
|
|
|
@@ -75,6 +79,10 @@ export class BtwService extends TypertRemoteService {
|
|
|
75
79
|
this.allowedTools = this.configAllowedTools;
|
|
76
80
|
this.language = pickLanguage(this.config?.language);
|
|
77
81
|
this.usageBySession = observeUsage(ctx);
|
|
82
|
+
// 面板流式:按 childSessionId 旁听 text-delta,`btw/live` 轮询取快照。
|
|
83
|
+
this.liveTracker = createLiveTracker(ctx);
|
|
84
|
+
// 抽屉追问借 composer 提交裸 `/btw`,真实参数从这里取(见 followup-queue.js)。
|
|
85
|
+
this.followUps = createFollowUpQueue({});
|
|
78
86
|
/** @type {Record<string, string>} */
|
|
79
87
|
this.messages = FALLBACK;
|
|
80
88
|
// locale 加载(不阻塞激活)
|
|
@@ -131,9 +139,20 @@ export class BtwService extends TypertRemoteService {
|
|
|
131
139
|
const file = historyFile(this.dataRoot, agent?.id ?? "");
|
|
132
140
|
// 追问用 `/btw --thread <id> <问题>` 标记线程(见 threads.js 的 parseThreadFlag 说明)。
|
|
133
141
|
const parsed = parseCommandArgs(String(invocation?.rawInput ?? ""));
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
142
|
+
let threadId = parsed.threadId;
|
|
143
|
+
let scope = parsed.scope;
|
|
144
|
+
let rawQuestion = parsed.question;
|
|
145
|
+
// 裸 `/btw`(无参数)时先看有没有抽屉登记的追问:
|
|
146
|
+
// 有就当追问处理,没有才走「重开上一条」的老语义。
|
|
147
|
+
if (threadId === "" && rawQuestion === "") {
|
|
148
|
+
const queued = this.followUps.take(String(agent?.id ?? ""));
|
|
149
|
+
if (queued !== undefined) {
|
|
150
|
+
threadId = queued.threadId;
|
|
151
|
+
scope = queued.scope;
|
|
152
|
+
rawQuestion = queued.question;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const question = clip(rawQuestion, LIMITS.questionChars);
|
|
137
156
|
|
|
138
157
|
if (question.text === "") {
|
|
139
158
|
const previous = await lastRecord(file).catch(() => undefined);
|
|
@@ -165,17 +184,31 @@ export class BtwService extends TypertRemoteService {
|
|
|
165
184
|
};
|
|
166
185
|
}
|
|
167
186
|
const history = [...sessionHistory, ...priorThread].slice(-LIMITS.replayRecords);
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
187
|
+
// 线程 id 在 fork 之前就定下来:面板流式卡片和最终记录必须是同一个线程。
|
|
188
|
+
const recordThreadId = threadId === "" ? newThreadId() : threadId;
|
|
189
|
+
// 面板流式:登记这次 run;fork 拿到 childId 后 onChild 会接上实时文本。
|
|
190
|
+
const run = this.liveTracker.begin(String(agent?.id ?? ""), {
|
|
191
|
+
threadId: recordThreadId,
|
|
172
192
|
question: question.text,
|
|
173
|
-
scope,
|
|
174
|
-
history,
|
|
175
|
-
allowedTools,
|
|
176
|
-
signal: invocation?.signal,
|
|
177
|
-
usageBySession: this.usageBySession,
|
|
178
193
|
});
|
|
194
|
+
/** @type {Awaited<ReturnType<typeof runSideQuestion>>} */
|
|
195
|
+
let outcome;
|
|
196
|
+
try {
|
|
197
|
+
outcome = await runSideQuestion({
|
|
198
|
+
ctx: this.scoped,
|
|
199
|
+
workspaceRegistry: this.scoped?.workspaceRegistry,
|
|
200
|
+
agent,
|
|
201
|
+
question: question.text,
|
|
202
|
+
scope,
|
|
203
|
+
history,
|
|
204
|
+
allowedTools,
|
|
205
|
+
signal: invocation?.signal,
|
|
206
|
+
usageBySession: this.usageBySession,
|
|
207
|
+
onChild: (childId) => this.liveTracker.attachChild(run, childId),
|
|
208
|
+
});
|
|
209
|
+
} finally {
|
|
210
|
+
this.liveTracker.finish(run);
|
|
211
|
+
}
|
|
179
212
|
|
|
180
213
|
if (outcome.code === "subagents_unavailable") {
|
|
181
214
|
return { kind: "error", text: this.messages.forkUnavailable ?? FALLBACK.forkUnavailable };
|
|
@@ -195,8 +228,8 @@ export class BtwService extends TypertRemoteService {
|
|
|
195
228
|
const recordId = `btw_${Date.now().toString(36)}`;
|
|
196
229
|
const record = {
|
|
197
230
|
id: recordId,
|
|
198
|
-
// 线程:追问沿用传入的 threadId
|
|
199
|
-
threadId:
|
|
231
|
+
// 线程:追问沿用传入的 threadId;首问用 fork 前就定下的新线程 id(v0.4 流式共用)
|
|
232
|
+
threadId: recordThreadId,
|
|
200
233
|
parentSessionId: String(agent?.id ?? ""),
|
|
201
234
|
childSessionId: outcome.childId ?? null,
|
|
202
235
|
question: question.text,
|
|
@@ -224,7 +257,7 @@ export class BtwService extends TypertRemoteService {
|
|
|
224
257
|
}
|
|
225
258
|
|
|
226
259
|
/**
|
|
227
|
-
*
|
|
260
|
+
* 徽章数据(抽屉底部展示用):记录条数与范围说明。
|
|
228
261
|
* @param {{ sessionId?: string, scope?: string, lang?: string } | undefined} request
|
|
229
262
|
* @returns {Promise<{ records: number, badge: string }>}
|
|
230
263
|
*/
|
|
@@ -245,7 +278,7 @@ export class BtwService extends TypertRemoteService {
|
|
|
245
278
|
}
|
|
246
279
|
|
|
247
280
|
/**
|
|
248
|
-
*
|
|
281
|
+
* 线程列表(抽屉展示用)。参数必须是单一标识符(网关 SRC 校验)。
|
|
249
282
|
* @param {{ sessionId?: string, limit?: number } | undefined} request
|
|
250
283
|
* @returns {Promise<any[]>}
|
|
251
284
|
*/
|
|
@@ -262,6 +295,28 @@ export class BtwService extends TypertRemoteService {
|
|
|
262
295
|
return listThreads(records);
|
|
263
296
|
}
|
|
264
297
|
|
|
298
|
+
/**
|
|
299
|
+
* 流式快照(面板轮询用):该父会话里正在跑的旁问 + 已解析的 Markdown 块。
|
|
300
|
+
*
|
|
301
|
+
* 为什么走轮询而不是让 client 订阅子会话事件:子会话是 fork 出来的,
|
|
302
|
+
* 它带着父会话的全部已完成轮次作为种子;client 一旦 retain 它就会把整段历史
|
|
303
|
+
* 拉到浏览器里。旁问只需要最后几 KB 的增量文本,host 缓冲 + 短轮询更省。
|
|
304
|
+
* @param {{ sessionId?: string } | undefined} request
|
|
305
|
+
* @returns {Promise<{ runs: any[] }>}
|
|
306
|
+
*/
|
|
307
|
+
async live(request) {
|
|
308
|
+
const input = request ?? {};
|
|
309
|
+
return {
|
|
310
|
+
runs: this.liveTracker.list(String(input.sessionId ?? "")).map((run) => ({
|
|
311
|
+
threadId: run.threadId,
|
|
312
|
+
question: run.question,
|
|
313
|
+
childId: run.childId,
|
|
314
|
+
ms: run.ms,
|
|
315
|
+
blocks: parseMarkdown(run.text).blocks,
|
|
316
|
+
})),
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
|
|
265
320
|
/**
|
|
266
321
|
* 生成「一键转正」草稿文本。**只返回文本,不插入、不发送**——
|
|
267
322
|
* 语义已与用户确认:插入 composer 草稿,由用户自己决定发不发。
|
|
@@ -303,6 +358,22 @@ export class BtwService extends TypertRemoteService {
|
|
|
303
358
|
return lastRecord(historyFile(this.dataRoot, String(input.sessionId ?? "")));
|
|
304
359
|
}
|
|
305
360
|
|
|
361
|
+
/**
|
|
362
|
+
* 登记一次待处理的追问。client 在提交裸 `/btw` **之前**调用,
|
|
363
|
+
* 这样命令文本里不必出现 `--thread t_xxx`。
|
|
364
|
+
* @param {{ sessionId?: string, threadId?: string, question?: string, scope?: string } | undefined} request
|
|
365
|
+
* @returns {Promise<{ ok: boolean, size: number }>}
|
|
366
|
+
*/
|
|
367
|
+
async queueFollowUp(request) {
|
|
368
|
+
const input = request ?? {};
|
|
369
|
+
this.followUps.push(String(input.sessionId ?? ""), {
|
|
370
|
+
threadId: String(input.threadId ?? ""),
|
|
371
|
+
question: String(input.question ?? ""),
|
|
372
|
+
scope: String(input.scope ?? ""),
|
|
373
|
+
});
|
|
374
|
+
return { ok: true, size: this.followUps.size() };
|
|
375
|
+
}
|
|
376
|
+
|
|
306
377
|
/**
|
|
307
378
|
* 读当前设置(client 设置面板用)。
|
|
308
379
|
* @param {undefined} request 参数必须存在且是单一标识符(网关 SRC 校验)。
|
|
@@ -337,9 +408,9 @@ export class BtwService extends TypertRemoteService {
|
|
|
337
408
|
formatAnswer(record) {
|
|
338
409
|
const usage = record.cost ?? {};
|
|
339
410
|
const meta = format(this.messages.meta ?? FALLBACK.meta, {
|
|
340
|
-
newTokens:
|
|
341
|
-
cachedTokens:
|
|
342
|
-
ms:
|
|
411
|
+
newTokens: formatTokens(usage.inputTokens),
|
|
412
|
+
cachedTokens: formatTokens(usage.cacheReadTokens),
|
|
413
|
+
ms: formatDuration(record.ms),
|
|
343
414
|
});
|
|
344
415
|
const lines = [record.answer, "", `— ${meta}`];
|
|
345
416
|
if (record.status === "truncated")
|
|
@@ -358,7 +429,9 @@ export class BtwService extends TypertRemoteService {
|
|
|
358
429
|
|
|
359
430
|
declareRemote(BtwService, "history");
|
|
360
431
|
declareRemote(BtwService, "last");
|
|
432
|
+
declareRemote(BtwService, "live");
|
|
361
433
|
declareRemote(BtwService, "stats");
|
|
434
|
+
declareRemote(BtwService, "queueFollowUp");
|
|
362
435
|
declareRemote(BtwService, "threads");
|
|
363
436
|
declareRemote(BtwService, "promote");
|
|
364
437
|
declareRemote(BtwService, "settings");
|
package/lib/subagent.js
CHANGED
|
@@ -82,6 +82,7 @@ export function makeReadOnlyGuard(allowedTools) {
|
|
|
82
82
|
* scope?: string,
|
|
83
83
|
* workspaceRegistry?: any,
|
|
84
84
|
* now?: () => number,
|
|
85
|
+
* onChild?: (childId: string) => void,
|
|
85
86
|
* }} input
|
|
86
87
|
* @returns {Promise<{ ok: boolean, text: string, status: string, childId?: string, usage?: Usage, ms: number, code?: string }>}
|
|
87
88
|
*/
|
|
@@ -121,6 +122,16 @@ export async function runSideQuestion(input) {
|
|
|
121
122
|
delivery: "caller",
|
|
122
123
|
});
|
|
123
124
|
|
|
125
|
+
// 把子会话 id 尽早交给调用方:面板要靠它在 `llm/stream` 上旁听实时文本。
|
|
126
|
+
// 注意此时子代理的第一次模型请求通常还没开始(网络往返在后面),不会漏 chunk。
|
|
127
|
+
if (typeof input.onChild === "function") {
|
|
128
|
+
try {
|
|
129
|
+
input.onChild(String(activation?.childId ?? ""));
|
|
130
|
+
} catch {
|
|
131
|
+
/* 观察者失败不影响旁问本身 */
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
124
135
|
// 只读保护挂在子代理自己的 ctx 上,随子代理释放。
|
|
125
136
|
const child = ctx?.agents?.get?.(activation?.childId);
|
|
126
137
|
const tools = child?.ctx?.tools ?? ctx?.tools;
|
package/lib/threads.js
CHANGED
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
// 旁问线程与「一键转正」的**纯逻辑**:不 import 任何宿主包,便于 node --test 直接引用
|
|
3
3
|
// (service.js 会 import 宿主 RPC 协议,在单测环境里解析不到)。
|
|
4
4
|
import { LIMITS } from "./limits.js";
|
|
5
|
+
import { parseMarkdown, truncateMarkdown } from "./markdown.js";
|
|
5
6
|
|
|
6
|
-
/**
|
|
7
|
+
/** 转正草稿的四节模板(Markdown 标题;落进输入框是未渲染源码,发送后由 DSH 渲染)。 */
|
|
7
8
|
export const PROMOTE_SECTIONS = [
|
|
8
9
|
{ key: "question", zh: "我问了什么", en: "What I asked" },
|
|
9
10
|
{ key: "answer", zh: "结论", en: "Conclusion" },
|
|
@@ -18,7 +19,7 @@ export const PROMOTE_SECTIONS = [
|
|
|
18
19
|
* @returns {string}
|
|
19
20
|
*/
|
|
20
21
|
export function newThreadId(now = Date.now(), rand = Math.random) {
|
|
21
|
-
// 短 id
|
|
22
|
+
// 短 id:抽屉发问要走 composer,命令文本(`/btw --thread <id> ...`)会短暂出现在
|
|
22
23
|
// 用户输入框里,id 越短越不刺眼。实测 16 字符的 `t_mwkfmjk1_qrb3` 太显眼。
|
|
23
24
|
// 单机单会话内冲突概率可忽略(时间 36 进制 + 2 位随机)。
|
|
24
25
|
const suffix = Math.floor(rand() * 36 ** 2)
|
|
@@ -77,10 +78,77 @@ export function threadHistory(records, threadId, maxTurns = LIMITS.threadTurns)
|
|
|
77
78
|
return thread.items.slice(-maxTurns);
|
|
78
79
|
}
|
|
79
80
|
|
|
81
|
+
/**
|
|
82
|
+
* 把一条记录变成面板可渲染的轮次。
|
|
83
|
+
*
|
|
84
|
+
* `blocks` 是 host 半解析好的 Markdown 树:client 只做 React 元素映射,
|
|
85
|
+
* 既不用在 classic script 里重复写解析器,也不会把模型输出当 HTML 执行。
|
|
86
|
+
* @param {any} record
|
|
87
|
+
* @returns {{
|
|
88
|
+
* id: string,
|
|
89
|
+
* question: string,
|
|
90
|
+
* chars: number,
|
|
91
|
+
* blocks: any[],
|
|
92
|
+
* cost: any,
|
|
93
|
+
* ms: number,
|
|
94
|
+
* at: number,
|
|
95
|
+
* status: string,
|
|
96
|
+
* }}
|
|
97
|
+
*/
|
|
98
|
+
export function threadTurn(record) {
|
|
99
|
+
const answer = String(record?.answer ?? "");
|
|
100
|
+
return {
|
|
101
|
+
id: String(record?.id ?? ""),
|
|
102
|
+
question: String(record?.question ?? ""),
|
|
103
|
+
chars: answer.length,
|
|
104
|
+
blocks: parseMarkdown(answer).blocks,
|
|
105
|
+
cost: record?.cost ?? null,
|
|
106
|
+
ms: Number(record?.ms ?? 0),
|
|
107
|
+
at: Number(record?.at ?? 0),
|
|
108
|
+
status: String(record?.status ?? ""),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* 线程列表(给面板展示):**旧→新**排序,每项带完整轮次。
|
|
114
|
+
*
|
|
115
|
+
* 为什么改成正序:面板是聊天式界面,最新一条应该落在最下面;
|
|
116
|
+
* 反序会让「刚问的问题」出现在最上面,与自己/常聊天应用的心智相反。
|
|
117
|
+
* @param {any[]} records
|
|
118
|
+
* @returns {{
|
|
119
|
+
* threadId: string,
|
|
120
|
+
* turns: number,
|
|
121
|
+
* question: string,
|
|
122
|
+
* at: number,
|
|
123
|
+
* lastId: string,
|
|
124
|
+
* cost: any,
|
|
125
|
+
* ms: number,
|
|
126
|
+
* items: ReturnType<typeof threadTurn>[],
|
|
127
|
+
* }[]}
|
|
128
|
+
*/
|
|
129
|
+
export function listThreads(records) {
|
|
130
|
+
return groupThreads(records)
|
|
131
|
+
.map((thread) => {
|
|
132
|
+
const items = thread.items.map((record) => threadTurn(record));
|
|
133
|
+
return {
|
|
134
|
+
threadId: thread.threadId,
|
|
135
|
+
turns: items.length,
|
|
136
|
+
question: String(thread.items[0]?.question ?? ""),
|
|
137
|
+
at: thread.at,
|
|
138
|
+
lastId: String(thread.last?.id ?? ""),
|
|
139
|
+
cost: thread.last?.cost ?? null,
|
|
140
|
+
ms: Number(thread.last?.ms ?? 0),
|
|
141
|
+
items,
|
|
142
|
+
};
|
|
143
|
+
})
|
|
144
|
+
.sort((a, b) => a.at - b.at);
|
|
145
|
+
}
|
|
146
|
+
|
|
80
147
|
/**
|
|
81
148
|
* 把一条旁问渲染成可插入草稿的文本(**只生成文本,绝不发送**)。
|
|
82
149
|
*
|
|
83
|
-
*
|
|
150
|
+
* 语义(2026-11-17 与用户确认):转正保留 **Markdown 源码** —— 四节用 `##` 标题,
|
|
151
|
+
* 落进输入框是未渲染的原文,发送后由 DSH 正常渲染;不再剥掉 `**`/表格/围栏。
|
|
84
152
|
* @param {any} record
|
|
85
153
|
* @param {{ lang?: string, details?: string, unresolved?: string, maxChars?: number }} [options]
|
|
86
154
|
* @returns {string}
|
|
@@ -88,98 +156,37 @@ export function threadHistory(records, threadId, maxTurns = LIMITS.threadTurns)
|
|
|
88
156
|
export function renderPromoteDraft(record, options = {}) {
|
|
89
157
|
const lang = options.lang === "en" ? "en" : "zh";
|
|
90
158
|
const maxChars = Math.min(
|
|
91
|
-
Math.max(Number(options.maxChars) || LIMITS.
|
|
92
|
-
LIMITS.
|
|
159
|
+
Math.max(Number(options.maxChars) || LIMITS.promoteChars, 200),
|
|
160
|
+
LIMITS.promoteChars,
|
|
93
161
|
);
|
|
94
162
|
const section = (/** @type {string} */ key) => {
|
|
95
163
|
const found = PROMOTE_SECTIONS.find((entry) => entry.key === key);
|
|
96
164
|
return found === undefined ? key : lang === "en" ? found.en : found.zh;
|
|
97
165
|
};
|
|
98
|
-
const question =
|
|
99
|
-
const answer =
|
|
166
|
+
const question = String(record?.question ?? "").trim();
|
|
167
|
+
const answer = String(record?.answer ?? "").trim();
|
|
100
168
|
/** @type {string[]} */
|
|
101
169
|
const lines = [
|
|
102
|
-
|
|
170
|
+
`## ${section("question")}`,
|
|
171
|
+
"",
|
|
103
172
|
question === "" ? "—" : question,
|
|
104
173
|
"",
|
|
105
|
-
|
|
174
|
+
`## ${section("answer")}`,
|
|
175
|
+
"",
|
|
106
176
|
answer === "" ? "—" : answer,
|
|
107
177
|
];
|
|
108
|
-
const details =
|
|
109
|
-
if (details !== "")
|
|
110
|
-
|
|
111
|
-
|
|
178
|
+
const details = String(options.details ?? "").trim();
|
|
179
|
+
if (details !== "") {
|
|
180
|
+
lines.push("", `## ${section("details")}`, "", details);
|
|
181
|
+
}
|
|
182
|
+
const unresolved = String(options.unresolved ?? "").trim();
|
|
183
|
+
if (unresolved !== "") {
|
|
184
|
+
lines.push("", `## ${section("unresolved")}`, "", unresolved);
|
|
185
|
+
}
|
|
112
186
|
const text = lines.join("\n").trim();
|
|
113
|
-
|
|
114
|
-
|
|
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
|
-
);
|
|
187
|
+
if (text.length <= maxChars) return text;
|
|
188
|
+
// 截断点可能落在代码围栏里:先补闭合,补不下就退到围栏之前,避免输入框里留半段代码。
|
|
189
|
+
return `${truncateMarkdown(text, maxChars - 1)}…`;
|
|
183
190
|
}
|
|
184
191
|
|
|
185
192
|
/**
|
|
@@ -209,7 +216,7 @@ export const THREAD_FLAG = "--thread";
|
|
|
209
216
|
/**
|
|
210
217
|
* 解析命令参数里的线程标记。
|
|
211
218
|
*
|
|
212
|
-
* 为什么用「命令行标记」而不是 Remote
|
|
219
|
+
* 为什么用「命令行标记」而不是 Remote:抽屉发问必须走 composer 提交,因为真实 Agent
|
|
213
220
|
* 只能从命令上下文拿到(见 ADR 0016)。所以线程 id 只能随这行文本一起传进来。
|
|
214
221
|
* @param {string} raw
|
|
215
222
|
* @returns {{ threadId: string, question: string }}
|
|
@@ -223,29 +230,6 @@ export function parseThreadFlag(raw) {
|
|
|
223
230
|
return { threadId: match[1], question: match[2].trim() };
|
|
224
231
|
}
|
|
225
232
|
|
|
226
|
-
/**
|
|
227
|
-
* 线程列表(给浮层展示):按最后一条时间倒序,每项带条数与首问。
|
|
228
|
-
* @param {any[]} records
|
|
229
|
-
* @returns {{ threadId: string, turns: number, question: string, answer: string, answerPlain: string, lastId: string, cost: any, ms: number, at: number }[]}
|
|
230
|
-
*/
|
|
231
|
-
export function listThreads(records) {
|
|
232
|
-
return groupThreads(records)
|
|
233
|
-
.map((thread) => ({
|
|
234
|
-
threadId: thread.threadId,
|
|
235
|
-
turns: thread.items.length,
|
|
236
|
-
question: String(thread.items[0]?.question ?? ""),
|
|
237
|
-
answer: String(thread.last?.answer ?? ""),
|
|
238
|
-
// 展示用:去掉 Markdown 噪音但保留代码块(列表里显示原文的话,用户看到的是
|
|
239
|
-
// `**粗体**`、`|表|格|`、`|---|`,实测被抱怨「为什么给我一堆井号和竖线」)。
|
|
240
|
-
answerPlain: stripMarkdown(String(thread.last?.answer ?? "")),
|
|
241
|
-
lastId: String(thread.last?.id ?? ""),
|
|
242
|
-
cost: thread.last?.cost ?? null,
|
|
243
|
-
ms: Number(thread.last?.ms ?? 0),
|
|
244
|
-
at: thread.at,
|
|
245
|
-
}))
|
|
246
|
-
.sort((a, b) => b.at - a.at);
|
|
247
|
-
}
|
|
248
|
-
|
|
249
233
|
/** 范围档位(枚举,避免自由文本带来的歧义)。 */
|
|
250
234
|
export const SCOPES = ["session", "recent", "selection"];
|
|
251
235
|
|
package/locale/en.json
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"noHistory": "No side question yet in this session."
|
|
11
11
|
},
|
|
12
12
|
"answer": {
|
|
13
|
-
"meta": "btw · new {newTokens} tokens · cached {cachedTokens} · {ms}
|
|
13
|
+
"meta": "btw · new {newTokens} tokens · cached {cachedTokens} · {ms}",
|
|
14
14
|
"reopen": "Last side question: {question}",
|
|
15
15
|
"truncated": "The answer was cut off; ask again to retry.",
|
|
16
16
|
"questionHeading": "Side question: {question}"
|
package/locale/zh.json
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"noHistory": "本会话还没有旁问记录。"
|
|
11
11
|
},
|
|
12
12
|
"answer": {
|
|
13
|
-
"meta": "旁问 · 新增 {newTokens} tokens · 命中缓存 {cachedTokens} · {ms}
|
|
13
|
+
"meta": "旁问 · 新增 {newTokens} tokens · 命中缓存 {cachedTokens} · {ms}",
|
|
14
14
|
"reopen": "上一条旁问:{question}",
|
|
15
15
|
"truncated": "答案被截断,可再问一次重试。",
|
|
16
16
|
"questionHeading": "旁问:{question}"
|