@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
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// 「追问待处理队列」:浮层想发起追问,但只能借 composer 提交一条命令
|
|
3
|
+
// (client 没有 commands 服务,真实 Agent 只能从命令上下文取 —— 见 ADR 0016)。
|
|
4
|
+
//
|
|
5
|
+
// 直接写 `/btw --thread t_xxx <问题>` 会把内部参数暴露在用户输入框里(实测被抱怨)。
|
|
6
|
+
// 改成:client 先把 {threadId, question, scope} 交给 host 暂存,再提交**裸 `/btw`**;
|
|
7
|
+
// host 处理命令时先查队列,取到就当追问处理,取不到才走「重开上一条」的老语义。
|
|
8
|
+
//
|
|
9
|
+
// 纯逻辑、不 import 宿主包,便于单测。
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @typedef {{ threadId: string, question: string, scope: string, at: number }} PendingFollowUp
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 建一个按 sessionId 分槽的待处理队列。
|
|
17
|
+
*
|
|
18
|
+
* 单槽而非数组:同一个会话在任意时刻只会有一次「即将提交的追问」,
|
|
19
|
+
* 覆盖旧值比排队更符合实际(用户连点两次时后一次生效)。
|
|
20
|
+
* @param {{ ttlMs?: number, now?: () => number }} [options]
|
|
21
|
+
*/
|
|
22
|
+
export function createFollowUpQueue(options = {}) {
|
|
23
|
+
const ttlMs = Number(options.ttlMs) || 60_000;
|
|
24
|
+
const now = options.now ?? Date.now;
|
|
25
|
+
/** @type {Map<string, PendingFollowUp>} */
|
|
26
|
+
const slots = new Map();
|
|
27
|
+
return {
|
|
28
|
+
/**
|
|
29
|
+
* 登记一次待处理的追问。
|
|
30
|
+
* @param {string} sessionId
|
|
31
|
+
* @param {{ threadId: string, question: string, scope?: string }} input
|
|
32
|
+
* @returns {void}
|
|
33
|
+
*/
|
|
34
|
+
push(sessionId, input) {
|
|
35
|
+
const threadId = String(input?.threadId ?? "");
|
|
36
|
+
const question = String(input?.question ?? "");
|
|
37
|
+
if (sessionId === "" || threadId === "" || question === "") return;
|
|
38
|
+
slots.set(sessionId, {
|
|
39
|
+
threadId,
|
|
40
|
+
question,
|
|
41
|
+
scope: String(input?.scope ?? ""),
|
|
42
|
+
at: now(),
|
|
43
|
+
});
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 取出并清空(一次性)。过期的视为不存在。
|
|
48
|
+
* @param {string} sessionId
|
|
49
|
+
* @returns {PendingFollowUp | undefined}
|
|
50
|
+
*/
|
|
51
|
+
take(sessionId) {
|
|
52
|
+
const item = slots.get(sessionId);
|
|
53
|
+
if (item === undefined) return undefined;
|
|
54
|
+
slots.delete(sessionId);
|
|
55
|
+
if (now() - item.at > ttlMs) return undefined;
|
|
56
|
+
return item;
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
/** 诊断用。 */
|
|
60
|
+
size() {
|
|
61
|
+
return slots.size;
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
package/lib/format.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// 可读性格式化:token 数自适应 K/M/B/T,耗时自适应 ms/s/m/h。
|
|
3
|
+
//
|
|
4
|
+
// 刻意做成纯函数、不 import 宿主包:
|
|
5
|
+
// - host 半用它渲染命令结果里的成本行(`旁问 · 新增 12.3K tokens · …`);
|
|
6
|
+
// - client 半是 classic script,无法 import 本模块,`lib/client.js` 里有一份
|
|
7
|
+
// **行为等价**的副本;两处都由各自的单测/冒烟兜住(见 README 的「成本徽章」)。
|
|
8
|
+
|
|
9
|
+
const TOKEN_UNITS = [
|
|
10
|
+
{ scale: 1e3, suffix: "K" },
|
|
11
|
+
{ scale: 1e6, suffix: "M" },
|
|
12
|
+
{ scale: 1e9, suffix: "B" },
|
|
13
|
+
{ scale: 1e12, suffix: "T" },
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 保留一位小数并去掉末尾的 `.0`。
|
|
18
|
+
* @param {number} value
|
|
19
|
+
* @returns {string}
|
|
20
|
+
*/
|
|
21
|
+
function oneDecimal(value) {
|
|
22
|
+
return (Math.round(value * 10) / 10).toFixed(1).replace(/\.0+$/u, "");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 小于 100 的数值保留一位小数(1.2K / 12.3K),再大就取整(123K / 123M)。
|
|
27
|
+
* @param {number} value
|
|
28
|
+
* @returns {string}
|
|
29
|
+
*/
|
|
30
|
+
function scaledText(value) {
|
|
31
|
+
return value < 100 ? oneDecimal(value) : String(Math.round(value));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 自适应 token 数:999 → "999";1234 → "1.2K";123456 → "123K";
|
|
36
|
+
* 1.2e6 → "1.2M";3.4e9 → "3.4B";5e12 → "5T"。
|
|
37
|
+
* 负数、NaN、非数字一律返回 "0"(宁可显示 0 也不显示 NaN)。
|
|
38
|
+
* @param {unknown} value
|
|
39
|
+
* @returns {string}
|
|
40
|
+
*/
|
|
41
|
+
export function formatTokens(value) {
|
|
42
|
+
const number = Math.max(0, Math.round(Number(value) || 0));
|
|
43
|
+
if (number < 1000) return String(number);
|
|
44
|
+
let index = 0;
|
|
45
|
+
for (let i = 0; i < TOKEN_UNITS.length; i += 1) {
|
|
46
|
+
if (number >= TOKEN_UNITS[i].scale) index = i;
|
|
47
|
+
}
|
|
48
|
+
let unit = TOKEN_UNITS[index];
|
|
49
|
+
let scaled = number / unit.scale;
|
|
50
|
+
// 999_999 / 1e3 = 999.999 → 四舍五入成 1000K 很丑,升一档写成 1M。
|
|
51
|
+
if (index < TOKEN_UNITS.length - 1 && scaled >= 999.5) {
|
|
52
|
+
index += 1;
|
|
53
|
+
unit = TOKEN_UNITS[index];
|
|
54
|
+
scaled = number / unit.scale;
|
|
55
|
+
}
|
|
56
|
+
return `${scaledText(scaled)}${unit.suffix}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 自适应耗时:999 → "999ms";1234 → "1.2s";90_000 → "1.5m";5_400_000 → "1.5h"。
|
|
61
|
+
* 上限单位到小时(旁问最长 120s,面板里不会出现天)。
|
|
62
|
+
* @param {unknown} value
|
|
63
|
+
* @returns {string}
|
|
64
|
+
*/
|
|
65
|
+
export function formatDuration(value) {
|
|
66
|
+
const ms = Math.max(0, Math.round(Number(value) || 0));
|
|
67
|
+
if (ms < 1000) return `${ms}ms`;
|
|
68
|
+
const seconds = ms / 1000;
|
|
69
|
+
if (seconds < 59.95) return `${scaledText(seconds)}s`;
|
|
70
|
+
const minutes = seconds / 60;
|
|
71
|
+
if (minutes < 59.95) return `${scaledText(minutes)}m`;
|
|
72
|
+
return `${scaledText(minutes / 60)}h`;
|
|
73
|
+
}
|
package/lib/limits.js
CHANGED
|
@@ -10,6 +10,8 @@ export const LIMITS = {
|
|
|
10
10
|
questionChars: 4000,
|
|
11
11
|
/** 答案最大字符数(超出截断并标记 truncated) */
|
|
12
12
|
answerChars: 8000,
|
|
13
|
+
/** 流式缓冲的最大字符数(面板展示用;答案本身另有 answerChars 上限) */
|
|
14
|
+
liveTextChars: 12_000,
|
|
13
15
|
/** 旁问总超时(含排队与模型时间) */
|
|
14
16
|
totalMs: 120_000,
|
|
15
17
|
/** 允许的只读工具白名单上限(默认空 = 全拒) */
|
package/lib/live.js
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// 旁问的实时文本缓冲:在 `llm/stream` waterfall 上旁听子会话的 text-delta,
|
|
3
|
+
// 供 `btw/live` 给面板做流式显示。
|
|
4
|
+
//
|
|
5
|
+
// 边界:
|
|
6
|
+
// - 只观察,不改写、不短路:无论是否命中,都把原 chunks 原样 yield 出去;
|
|
7
|
+
// - 只记录**已登记的旁问子会话**(按 childSessionId 精确匹配),不碰主会话;
|
|
8
|
+
// - 文本只存在内存里,随 run 结束释放;不落盘(README 的副作用说明)。
|
|
9
|
+
import { LIMITS } from "./limits.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @typedef {{
|
|
13
|
+
* threadId: string,
|
|
14
|
+
* question: string,
|
|
15
|
+
* parentSessionId: string,
|
|
16
|
+
* childId: string,
|
|
17
|
+
* text: string,
|
|
18
|
+
* startedAt: number,
|
|
19
|
+
* updatedAt: number,
|
|
20
|
+
* done: boolean,
|
|
21
|
+
* }} LiveRun
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 建一个按父会话分组的实时旁问表。
|
|
26
|
+
* @param {any} ctx host 上下文(需要 `on`)
|
|
27
|
+
* @param {{ now?: () => number }} [options]
|
|
28
|
+
*/
|
|
29
|
+
export function createLiveTracker(ctx, options = {}) {
|
|
30
|
+
const now = options.now ?? Date.now;
|
|
31
|
+
/** @type {Map<string, Map<string, LiveRun>>} */
|
|
32
|
+
const byParent = new Map();
|
|
33
|
+
/** @type {Map<string, LiveRun>} */
|
|
34
|
+
const byChild = new Map();
|
|
35
|
+
|
|
36
|
+
const listen = typeof ctx?.on === "function" ? ctx.on.bind(ctx) : undefined;
|
|
37
|
+
if (listen !== undefined) {
|
|
38
|
+
listen("llm/stream", (/** @type {any} */ request, /** @type {any} */ next) => {
|
|
39
|
+
const sessionId = String(request?.sessionId ?? "");
|
|
40
|
+
const run = sessionId === "" ? undefined : byChild.get(sessionId);
|
|
41
|
+
if (run === undefined) return next();
|
|
42
|
+
const inner = next();
|
|
43
|
+
return (async function* () {
|
|
44
|
+
for await (const chunk of inner) {
|
|
45
|
+
if (chunk?.type === "text-delta" && typeof chunk.text === "string") {
|
|
46
|
+
if (!run.done && run.text.length < LIMITS.liveTextChars) {
|
|
47
|
+
run.text = (run.text + chunk.text).slice(0, LIMITS.liveTextChars);
|
|
48
|
+
run.updatedAt = now();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
yield chunk;
|
|
52
|
+
}
|
|
53
|
+
})();
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
/**
|
|
59
|
+
* 登记一次旁问(在调用 fork 之前)。返回的 run 要原样交给 attachChild / finish。
|
|
60
|
+
* @param {string} parentSessionId
|
|
61
|
+
* @param {{ threadId: string, question: string }} input
|
|
62
|
+
* @returns {LiveRun}
|
|
63
|
+
*/
|
|
64
|
+
begin(parentSessionId, input) {
|
|
65
|
+
const startedAt = now();
|
|
66
|
+
/** @type {LiveRun} */
|
|
67
|
+
const run = {
|
|
68
|
+
threadId: String(input?.threadId ?? ""),
|
|
69
|
+
question: String(input?.question ?? ""),
|
|
70
|
+
parentSessionId: String(parentSessionId ?? ""),
|
|
71
|
+
childId: "",
|
|
72
|
+
text: "",
|
|
73
|
+
startedAt,
|
|
74
|
+
updatedAt: startedAt,
|
|
75
|
+
done: false,
|
|
76
|
+
};
|
|
77
|
+
let bucket = byParent.get(run.parentSessionId);
|
|
78
|
+
if (bucket === undefined) {
|
|
79
|
+
bucket = new Map();
|
|
80
|
+
byParent.set(run.parentSessionId, bucket);
|
|
81
|
+
}
|
|
82
|
+
bucket.set(run.threadId, run);
|
|
83
|
+
return run;
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* fork 拿到 childId 后登记,之后该子会话的 text-delta 才会进入 run.text。
|
|
88
|
+
* @param {LiveRun | undefined} run
|
|
89
|
+
* @param {unknown} childId
|
|
90
|
+
* @returns {void}
|
|
91
|
+
*/
|
|
92
|
+
attachChild(run, childId) {
|
|
93
|
+
if (run === undefined || run === null || run.done) return;
|
|
94
|
+
const id = String(childId ?? "");
|
|
95
|
+
if (id === "") return;
|
|
96
|
+
run.childId = id;
|
|
97
|
+
byChild.set(id, run);
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 一次旁问结束时释放(无论成功失败都要调)。
|
|
102
|
+
* @param {LiveRun | undefined} run
|
|
103
|
+
* @returns {void}
|
|
104
|
+
*/
|
|
105
|
+
finish(run) {
|
|
106
|
+
if (run === undefined || run === null || run.done) return;
|
|
107
|
+
run.done = true;
|
|
108
|
+
if (run.childId !== "") byChild.delete(run.childId);
|
|
109
|
+
const bucket = byParent.get(run.parentSessionId);
|
|
110
|
+
if (bucket !== undefined && bucket.get(run.threadId) === run) {
|
|
111
|
+
bucket.delete(run.threadId);
|
|
112
|
+
if (bucket.size === 0) byParent.delete(run.parentSessionId);
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 某父会话当前在跑的旁问快照(按开始时间正序)。
|
|
118
|
+
* @param {string} parentSessionId
|
|
119
|
+
* @returns {(LiveRun & { ms: number })[]}
|
|
120
|
+
*/
|
|
121
|
+
list(parentSessionId) {
|
|
122
|
+
const bucket = byParent.get(String(parentSessionId ?? ""));
|
|
123
|
+
if (bucket === undefined) return [];
|
|
124
|
+
const at = now();
|
|
125
|
+
return [...bucket.values()]
|
|
126
|
+
.filter((run) => !run.done)
|
|
127
|
+
.sort((left, right) => left.startedAt - right.startedAt)
|
|
128
|
+
.map((run) => ({ ...run, ms: Math.max(0, at - run.startedAt) }));
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
/** 诊断用:当前跟踪中的子会话数。 */
|
|
132
|
+
size() {
|
|
133
|
+
return byChild.size;
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
}
|