@kangyishuai/dsh-btw 0.3.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 -8
- package/README.zh-CN.md +25 -8
- package/lib/client.js +880 -385
- 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 +61 -19
- package/lib/subagent.js +11 -0
- package/lib/threads.js +91 -175
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +1 -1
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
|
+
}
|