@dabuliu_wsy/dsh-im 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +197 -3
- package/cordis.patch.yml +4 -0
- package/dist/core/agent-state.d.ts +38 -0
- package/dist/core/agent-state.js +59 -0
- package/dist/core/arbiter-bridge.d.ts +25 -0
- package/dist/core/arbiter-bridge.js +20 -0
- package/dist/core/arbiter.d.ts +29 -0
- package/dist/core/arbiter.js +59 -0
- package/dist/core/avatar-brands.d.ts +23 -0
- package/dist/core/avatar-brands.js +75 -0
- package/dist/core/avatar-presets.d.ts +21 -0
- package/dist/core/avatar-presets.js +38 -0
- package/dist/core/avatar.d.ts +1 -0
- package/dist/core/avatar.js +18 -0
- package/dist/core/blocked.d.ts +12 -0
- package/dist/core/blocked.js +23 -0
- package/dist/core/budget-plan.d.ts +41 -0
- package/dist/core/budget-plan.js +69 -0
- package/dist/core/calibration-samples.d.ts +77 -0
- package/dist/core/calibration-samples.js +168 -0
- package/dist/core/calibrator.d.ts +57 -0
- package/dist/core/calibrator.js +125 -0
- package/dist/core/chapter-tree.d.ts +128 -0
- package/dist/core/chapter-tree.js +400 -0
- package/dist/core/consensus.d.ts +135 -0
- package/dist/core/consensus.js +394 -0
- package/dist/core/context-budget.d.ts +153 -0
- package/dist/core/context-budget.js +183 -0
- package/dist/core/curator.d.ts +28 -0
- package/dist/core/curator.js +190 -0
- package/dist/core/dual-window.d.ts +86 -0
- package/dist/core/dual-window.js +175 -0
- package/dist/core/events.d.ts +110 -0
- package/dist/core/events.js +117 -0
- package/dist/core/facts.d.ts +114 -0
- package/dist/core/facts.js +400 -0
- package/dist/core/fence.d.ts +34 -0
- package/dist/core/fence.js +129 -0
- package/dist/core/floor.d.ts +18 -0
- package/dist/core/floor.js +80 -0
- package/dist/core/ids.d.ts +11 -0
- package/dist/core/ids.js +29 -0
- package/dist/core/im.d.ts +167 -0
- package/dist/core/im.js +1307 -0
- package/dist/core/index.d.ts +33 -0
- package/dist/core/index.js +33 -0
- package/dist/core/log.d.ts +21 -0
- package/dist/core/log.js +63 -0
- package/dist/core/memory-claims.d.ts +67 -0
- package/dist/core/memory-claims.js +164 -0
- package/dist/core/memory-context.d.ts +121 -0
- package/dist/core/memory-context.js +303 -0
- package/dist/core/memory-visibility.d.ts +58 -0
- package/dist/core/memory-visibility.js +36 -0
- package/dist/core/memory.d.ts +12 -0
- package/dist/core/memory.js +73 -0
- package/dist/core/metrics.d.ts +49 -0
- package/dist/core/metrics.js +89 -0
- package/dist/core/multi-hop.d.ts +116 -0
- package/dist/core/multi-hop.js +278 -0
- package/dist/core/persist.d.ts +21 -0
- package/dist/core/persist.js +94 -0
- package/dist/core/proactive.d.ts +54 -0
- package/dist/core/proactive.js +108 -0
- package/dist/core/prompt.d.ts +82 -0
- package/dist/core/prompt.js +164 -0
- package/dist/core/receipt.d.ts +107 -0
- package/dist/core/receipt.js +86 -0
- package/dist/core/retrieval.d.ts +69 -0
- package/dist/core/retrieval.js +260 -0
- package/dist/core/role-state.d.ts +16 -0
- package/dist/core/role-state.js +65 -0
- package/dist/core/runtime.d.ts +137 -0
- package/dist/core/runtime.js +574 -0
- package/dist/core/summary.d.ts +19 -0
- package/dist/core/summary.js +96 -0
- package/dist/core/text.d.ts +10 -0
- package/dist/core/text.js +45 -0
- package/dist/core/time.d.ts +22 -0
- package/dist/core/time.js +84 -0
- package/dist/core/tool-schema.d.ts +12 -0
- package/dist/core/tool-schema.js +92 -0
- package/dist/core/tools.d.ts +137 -0
- package/dist/core/tools.js +348 -0
- package/dist/core/types.d.ts +400 -0
- package/dist/core/types.js +1 -0
- package/dist/core/window.d.ts +6 -0
- package/dist/core/window.js +7 -0
- package/dist/core/workspace-node.d.ts +28 -0
- package/dist/core/workspace-node.js +232 -0
- package/dist/core/workspace.d.ts +9 -0
- package/dist/core/workspace.js +45 -0
- package/dist/host/agent-session.d.ts +279 -0
- package/dist/host/agent-session.js +935 -0
- package/dist/host/attachment-gc.d.ts +37 -0
- package/dist/host/attachment-gc.js +97 -0
- package/dist/host/calibration-samples-store.d.ts +5 -0
- package/dist/host/calibration-samples-store.js +67 -0
- package/dist/host/capabilities.d.ts +45 -0
- package/dist/host/capabilities.js +67 -0
- package/dist/host/derived-store.d.ts +36 -0
- package/dist/host/derived-store.js +199 -0
- package/dist/host/dsh-llm.d.ts +143 -0
- package/dist/host/dsh-llm.js +395 -0
- package/dist/host/event-store.d.ts +47 -0
- package/dist/host/event-store.js +154 -0
- package/dist/host/fs-atomic.d.ts +1 -0
- package/dist/host/fs-atomic.js +26 -0
- package/dist/host/im-api.d.ts +352 -0
- package/dist/host/im-api.js +1137 -0
- package/dist/host/index.d.ts +103 -0
- package/dist/host/index.js +453 -0
- package/dist/host/memory-index.d.ts +65 -0
- package/dist/host/memory-index.js +181 -0
- package/dist/host/metrics-store.d.ts +7 -0
- package/dist/host/metrics-store.js +104 -0
- package/dist/host/models.d.ts +39 -0
- package/dist/host/models.js +67 -0
- package/dist/host/persona-tools.d.ts +57 -0
- package/dist/host/persona-tools.js +226 -0
- package/dist/host/preflight.d.ts +31 -0
- package/dist/host/preflight.js +29 -0
- package/dist/host/receipt-store.d.ts +26 -0
- package/dist/host/receipt-store.js +68 -0
- package/dist/host/run-check.d.ts +51 -0
- package/dist/host/run-check.js +190 -0
- package/dist/host/state-roots.d.ts +50 -0
- package/dist/host/state-roots.js +163 -0
- package/dist/host/tool-cache.d.ts +25 -0
- package/dist/host/tool-cache.js +90 -0
- package/dist/host/tool-call-budget.d.ts +29 -0
- package/dist/host/tool-call-budget.js +38 -0
- package/dsh-plugin.json +25 -0
- package/lib/client.inner.cjs +3346 -0
- package/lib/client.js +3354 -0
- package/package.json +86 -6
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 群级分层摘要投影(L0 详细 → L1 粗 → L2 事实/决策/待办)。
|
|
3
|
+
*
|
|
4
|
+
* - L1:旧消息压缩成一行一条(增量合并:只把上次覆盖之后的新旧消息追加进来);
|
|
5
|
+
* - L2:事实投影里的不可压缩项(决定/待办/承诺/约束)永远保留;
|
|
6
|
+
* - `untilSeq`:摘要覆盖到的消息 seq,prompt 窗口只取它之后的消息。
|
|
7
|
+
*
|
|
8
|
+
* 本文件是纯函数、不调模型:它替代“同步 LLM 摘要”这条慢路径,
|
|
9
|
+
* 以后接 LLM 摘要时把 L1 来源换掉即可(接口不变、失败仍能回退到窗口)。
|
|
10
|
+
*/
|
|
11
|
+
import { factsFromMessages, formatFact, INCOMPRESSIBLE_KINDS, isPromptVisibleFact, projectFactsFromList } from './facts.js';
|
|
12
|
+
import { stableHash } from './receipt.js';
|
|
13
|
+
/** 与 prompt 默认窗口一致:最近 20 条留原文,更早的进 L1。 */
|
|
14
|
+
export const SUMMARY_KEEP_RECENT = 20;
|
|
15
|
+
/** L1 的字符预算:超了就从最旧的行开始丢(L2 不可压缩项不受影响)。 */
|
|
16
|
+
export const L1_CHAR_BUDGET = 8000;
|
|
17
|
+
/** L2 最多保留多少条事实(按不可压缩项优先)。 */
|
|
18
|
+
export const L2_MAX_FACTS = 40;
|
|
19
|
+
const L1_ITEM_MAX = 60;
|
|
20
|
+
const NOISE = /本轮开始|本轮弃权|全员暂停|跨场召回|先当参考|汇总已写入/;
|
|
21
|
+
function seqOf(message, index) {
|
|
22
|
+
return message.seq ?? index + 1;
|
|
23
|
+
}
|
|
24
|
+
function l1Line(message, seq) {
|
|
25
|
+
const text = message.text.replace(/\s+/g, ' ').trim();
|
|
26
|
+
return `[#${seq}] ${message.authorName}:${text.slice(0, L1_ITEM_MAX)}`;
|
|
27
|
+
}
|
|
28
|
+
function digestVersion(input) {
|
|
29
|
+
return `digest-v1:${stableHash([
|
|
30
|
+
`until=${input.untilSeq}`,
|
|
31
|
+
input.l1,
|
|
32
|
+
...input.l2,
|
|
33
|
+
input.factsVersion,
|
|
34
|
+
].join('\n')).split(':').at(-1) ?? '0'}`;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 生成/增量更新摘要投影。`previous` 传上一次的摘要,只追加新增的旧消息。
|
|
38
|
+
*/
|
|
39
|
+
export function buildThreadDigest(thread, previous, options = {}) {
|
|
40
|
+
const messages = thread.messages;
|
|
41
|
+
const keep = Math.max(1, options.keep ?? SUMMARY_KEEP_RECENT);
|
|
42
|
+
const budget = Math.max(200, options.charBudget ?? L1_CHAR_BUDGET);
|
|
43
|
+
const boundaryIndex = Math.max(0, messages.length - keep);
|
|
44
|
+
const oldMessages = messages.slice(0, boundaryIndex);
|
|
45
|
+
// 消息回退(state 回滚)时旧摘要可能覆盖到已不存在的消息,直接重算。
|
|
46
|
+
const lastSeq = messages.length ? seqOf(messages[messages.length - 1], messages.length - 1) : 0;
|
|
47
|
+
const base = previous && previous.untilSeq <= lastSeq ? previous : undefined;
|
|
48
|
+
const coveredSeq = base?.untilSeq ?? 0;
|
|
49
|
+
// 旧摘要只有在仍存在“旧消息区”时才延续;否则(回退/变短)从头算。
|
|
50
|
+
const carried = boundaryIndex > 0 && base ? base.l1 : '';
|
|
51
|
+
const lines = carried ? carried.split('\n').filter(Boolean) : [];
|
|
52
|
+
for (let index = 0; index < oldMessages.length; index++) {
|
|
53
|
+
const message = oldMessages[index];
|
|
54
|
+
if (NOISE.test(message.text))
|
|
55
|
+
continue;
|
|
56
|
+
const seq = seqOf(message, index);
|
|
57
|
+
if (seq <= coveredSeq)
|
|
58
|
+
continue;
|
|
59
|
+
lines.push(l1Line(message, seq));
|
|
60
|
+
}
|
|
61
|
+
while (lines.length > 1 && lines.join('\n').length > budget)
|
|
62
|
+
lines.shift();
|
|
63
|
+
const l1 = lines.join('\n');
|
|
64
|
+
const untilSeq = oldMessages.length ? seqOf(oldMessages[oldMessages.length - 1], boundaryIndex - 1) : 0;
|
|
65
|
+
// L2:不可压缩项永远保留(用户的决定/待办优先)。
|
|
66
|
+
// 能力/环境自述(“我没有 shell / 只能在群里 / 私聊发不出去”)不进 L2:
|
|
67
|
+
// 它们只是那一轮的便签,回灌进 prompt 会让角色把自己的临时说法当成长期约束。
|
|
68
|
+
const factProjection = options.facts
|
|
69
|
+
? projectFactsFromList(options.facts)
|
|
70
|
+
: factsFromMessages(messages);
|
|
71
|
+
const incompressible = factProjection.active
|
|
72
|
+
.filter((fact) => INCOMPRESSIBLE_KINDS.includes(fact.kind))
|
|
73
|
+
.filter((fact) => isPromptVisibleFact(fact))
|
|
74
|
+
.slice(0, L2_MAX_FACTS);
|
|
75
|
+
const l2 = incompressible.map(formatFact);
|
|
76
|
+
const factsVersion = factProjection.version;
|
|
77
|
+
return {
|
|
78
|
+
version: digestVersion({ untilSeq, l1, l2, factsVersion }),
|
|
79
|
+
untilSeq,
|
|
80
|
+
l1,
|
|
81
|
+
l2,
|
|
82
|
+
factsVersion,
|
|
83
|
+
at: Date.now(),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/** prompt 里怎么放摘要:L2(事实)在前、L1(旧消息压缩)在后。 */
|
|
87
|
+
export function digestSummaryText(digest) {
|
|
88
|
+
if (!digest)
|
|
89
|
+
return '';
|
|
90
|
+
const parts = [];
|
|
91
|
+
if (digest.l2.length)
|
|
92
|
+
parts.push('[群事实/决定]', ...digest.l2);
|
|
93
|
+
if (digest.l1)
|
|
94
|
+
parts.push('[更早消息摘要]', digest.l1);
|
|
95
|
+
return parts.join('\n');
|
|
96
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 旧版本把 DSH 的 reasoning block 当正文落库(2026-11-02 前),历史消息里可能残留
|
|
3
|
+
* “The user says… / Let me… / Maybe:” 这类整段英文思维链,后面才是真正的中文回复。
|
|
4
|
+
*
|
|
5
|
+
* 这里做**只读清洗**:只处理“开头 ≥80 个英文字符、随后第一段中文正文”的形状,
|
|
6
|
+
* 把前面的思维链剥掉;不改 state、不写盘。新消息不会再产生这种形状。
|
|
7
|
+
*/
|
|
8
|
+
export declare function stripLegacyReasoning(text: string): string;
|
|
9
|
+
/** 是否疑似旧版泄漏的思维链(只用于统计/诊断,不改内容)。 */
|
|
10
|
+
export declare function looksLikeLegacyReasoning(text: string): boolean;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 旧版本把 DSH 的 reasoning block 当正文落库(2026-11-02 前),历史消息里可能残留
|
|
3
|
+
* “The user says… / Let me… / Maybe:” 这类整段英文思维链,后面才是真正的中文回复。
|
|
4
|
+
*
|
|
5
|
+
* 这里做**只读清洗**:只处理“开头 ≥80 个英文字符、随后第一段中文正文”的形状,
|
|
6
|
+
* 把前面的思维链剥掉;不改 state、不写盘。新消息不会再产生这种形状。
|
|
7
|
+
*/
|
|
8
|
+
export function stripLegacyReasoning(text) {
|
|
9
|
+
if (!text)
|
|
10
|
+
return text;
|
|
11
|
+
const trimmed = text.trimStart();
|
|
12
|
+
// 开头就必须是英文,否则不碰(避免误伤纯中文回复)。
|
|
13
|
+
if (!/^[A-Za-z"'“”‘’(\[]/.test(trimmed))
|
|
14
|
+
return text;
|
|
15
|
+
const lines = text.split('\n');
|
|
16
|
+
let englishLetters = 0;
|
|
17
|
+
let asciiWords = 0;
|
|
18
|
+
for (let index = 0; index < lines.length; index++) {
|
|
19
|
+
const line = lines[index].trim();
|
|
20
|
+
if (!line)
|
|
21
|
+
continue;
|
|
22
|
+
const letters = (line.match(/[A-Za-z]/g) ?? []).length;
|
|
23
|
+
const cjk = (line.match(/[\u4e00-\u9fff]/g) ?? []).length;
|
|
24
|
+
const visible = line.replace(/\s/g, '').length;
|
|
25
|
+
// 第一段“以中文开头、且中文为主”的行视为正文开始;前面必须已经积累了足够英文。
|
|
26
|
+
// 不能选 “Maybe: 在。…” 这种带英文前缀/引号的思维链草稿行。
|
|
27
|
+
if (index > 0
|
|
28
|
+
&& visible >= 4
|
|
29
|
+
&& /^[\u4e00-\u9fff]/.test(line)
|
|
30
|
+
&& cjk / visible >= 0.6
|
|
31
|
+
&& englishLetters >= 80
|
|
32
|
+
&& asciiWords >= 12) {
|
|
33
|
+
const answer = lines.slice(index).join('\n').trim();
|
|
34
|
+
if (answer.length >= 2)
|
|
35
|
+
return answer;
|
|
36
|
+
}
|
|
37
|
+
englishLetters += letters;
|
|
38
|
+
asciiWords += (line.match(/[A-Za-z]{2,}/g) ?? []).length;
|
|
39
|
+
}
|
|
40
|
+
return text;
|
|
41
|
+
}
|
|
42
|
+
/** 是否疑似旧版泄漏的思维链(只用于统计/诊断,不改内容)。 */
|
|
43
|
+
export function looksLikeLegacyReasoning(text) {
|
|
44
|
+
return stripLegacyReasoning(text) !== text;
|
|
45
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 时间展示口径(纯函数,`now` 可注入,便于单测)。
|
|
3
|
+
*
|
|
4
|
+
* 聊天里只显示「23:55」会分不清是哪一天:同一个会话跨天后,昨天 23:55 和今天 17:08
|
|
5
|
+
* 看起来像同一天的两个时间点(真机截图就踩过这个坑)。所以统一按**本地自然日**分级:
|
|
6
|
+
* 今天 → `17:08`;昨天 → `昨天 23:55`;今年更早 → `3月5日 17:08`;跨年 → `2025年3月5日 17:08`。
|
|
7
|
+
* 会话列表更窄,用紧凑版:`17:08` / `昨天` / `3月5日` / `2025/3/5`。
|
|
8
|
+
* 聊天流里跨天处另有分隔条(`formatDayLabel`):`今天` / `昨天` / `3月5日` / `2025年3月5日`。
|
|
9
|
+
*/
|
|
10
|
+
/** 同一天(同一本地日历日)。非法时间戳一律 false。 */
|
|
11
|
+
export declare function isSameDay(a: number, b: number): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* 相隔几个自然日:0=今天,1=昨天,-1=明天。
|
|
14
|
+
* 用本地零点相减再四舍五入,跨夏令时(23/25 小时的那天)也不会算错一天。
|
|
15
|
+
*/
|
|
16
|
+
export declare function dayGap(ts: number, now?: number): number | undefined;
|
|
17
|
+
/** 气泡上方/消息头的时间:今天只给时刻,更早补日期。 */
|
|
18
|
+
export declare function formatStamp(ts: number, now?: number): string;
|
|
19
|
+
/** 会话列表用:窄栏放不下「昨天 23:55」,只给日期(今天仍给时刻)。 */
|
|
20
|
+
export declare function formatListStamp(ts: number, now?: number): string;
|
|
21
|
+
/** 聊天流里的日期分隔条文案。 */
|
|
22
|
+
export declare function formatDayLabel(ts: number, now?: number): string;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 时间展示口径(纯函数,`now` 可注入,便于单测)。
|
|
3
|
+
*
|
|
4
|
+
* 聊天里只显示「23:55」会分不清是哪一天:同一个会话跨天后,昨天 23:55 和今天 17:08
|
|
5
|
+
* 看起来像同一天的两个时间点(真机截图就踩过这个坑)。所以统一按**本地自然日**分级:
|
|
6
|
+
* 今天 → `17:08`;昨天 → `昨天 23:55`;今年更早 → `3月5日 17:08`;跨年 → `2025年3月5日 17:08`。
|
|
7
|
+
* 会话列表更窄,用紧凑版:`17:08` / `昨天` / `3月5日` / `2025/3/5`。
|
|
8
|
+
* 聊天流里跨天处另有分隔条(`formatDayLabel`):`今天` / `昨天` / `3月5日` / `2025年3月5日`。
|
|
9
|
+
*/
|
|
10
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
11
|
+
function toDate(ts) {
|
|
12
|
+
if (!Number.isFinite(ts))
|
|
13
|
+
return undefined;
|
|
14
|
+
const date = new Date(ts);
|
|
15
|
+
return Number.isNaN(date.getTime()) ? undefined : date;
|
|
16
|
+
}
|
|
17
|
+
function clock(date) {
|
|
18
|
+
return `${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`;
|
|
19
|
+
}
|
|
20
|
+
function monthDay(date) {
|
|
21
|
+
return `${date.getMonth() + 1}月${date.getDate()}日`;
|
|
22
|
+
}
|
|
23
|
+
/** 同一天(同一本地日历日)。非法时间戳一律 false。 */
|
|
24
|
+
export function isSameDay(a, b) {
|
|
25
|
+
const left = toDate(a);
|
|
26
|
+
const right = toDate(b);
|
|
27
|
+
if (!left || !right)
|
|
28
|
+
return false;
|
|
29
|
+
return left.getFullYear() === right.getFullYear() && left.getMonth() === right.getMonth() && left.getDate() === right.getDate();
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* 相隔几个自然日:0=今天,1=昨天,-1=明天。
|
|
33
|
+
* 用本地零点相减再四舍五入,跨夏令时(23/25 小时的那天)也不会算错一天。
|
|
34
|
+
*/
|
|
35
|
+
export function dayGap(ts, now = Date.now()) {
|
|
36
|
+
const date = toDate(ts);
|
|
37
|
+
const base = toDate(now);
|
|
38
|
+
if (!date || !base)
|
|
39
|
+
return undefined;
|
|
40
|
+
const startOfDay = (d) => new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime();
|
|
41
|
+
return Math.round((startOfDay(base) - startOfDay(date)) / DAY_MS);
|
|
42
|
+
}
|
|
43
|
+
/** 气泡上方/消息头的时间:今天只给时刻,更早补日期。 */
|
|
44
|
+
export function formatStamp(ts, now = Date.now()) {
|
|
45
|
+
const date = toDate(ts);
|
|
46
|
+
if (!date)
|
|
47
|
+
return '';
|
|
48
|
+
const gap = dayGap(ts, now) ?? 0;
|
|
49
|
+
if (gap === 0)
|
|
50
|
+
return clock(date);
|
|
51
|
+
if (gap === 1)
|
|
52
|
+
return `昨天 ${clock(date)}`;
|
|
53
|
+
if (date.getFullYear() === new Date(now).getFullYear())
|
|
54
|
+
return `${monthDay(date)} ${clock(date)}`;
|
|
55
|
+
return `${date.getFullYear()}年${monthDay(date)} ${clock(date)}`;
|
|
56
|
+
}
|
|
57
|
+
/** 会话列表用:窄栏放不下「昨天 23:55」,只给日期(今天仍给时刻)。 */
|
|
58
|
+
export function formatListStamp(ts, now = Date.now()) {
|
|
59
|
+
const date = toDate(ts);
|
|
60
|
+
if (!date)
|
|
61
|
+
return '';
|
|
62
|
+
const gap = dayGap(ts, now) ?? 0;
|
|
63
|
+
if (gap === 0)
|
|
64
|
+
return clock(date);
|
|
65
|
+
if (gap === 1)
|
|
66
|
+
return '昨天';
|
|
67
|
+
if (date.getFullYear() === new Date(now).getFullYear())
|
|
68
|
+
return monthDay(date);
|
|
69
|
+
return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
|
|
70
|
+
}
|
|
71
|
+
/** 聊天流里的日期分隔条文案。 */
|
|
72
|
+
export function formatDayLabel(ts, now = Date.now()) {
|
|
73
|
+
const date = toDate(ts);
|
|
74
|
+
if (!date)
|
|
75
|
+
return '';
|
|
76
|
+
const gap = dayGap(ts, now) ?? 0;
|
|
77
|
+
if (gap === 0)
|
|
78
|
+
return '今天';
|
|
79
|
+
if (gap === 1)
|
|
80
|
+
return '昨天';
|
|
81
|
+
if (date.getFullYear() === new Date(now).getFullYear())
|
|
82
|
+
return monthDay(date);
|
|
83
|
+
return `${date.getFullYear()}年${monthDay(date)}`;
|
|
84
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 角色可勾选的工具 → DSH `ctx.llm.stream({ tools })` 需要的 schema。
|
|
3
|
+
*
|
|
4
|
+
* 纯数据,不发网络请求,也不含任何模型地址/密钥:
|
|
5
|
+
* 插件只把工具清单交给宿主 DSH,真正调模型的是 `ctx.llm`。
|
|
6
|
+
*/
|
|
7
|
+
export interface ToolSpec {
|
|
8
|
+
name: string;
|
|
9
|
+
description: string;
|
|
10
|
+
parameters: Record<string, unknown>;
|
|
11
|
+
}
|
|
12
|
+
export declare function toolsForAllow(ids: string[] | undefined): ToolSpec[];
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
export function toolsForAllow(ids) {
|
|
2
|
+
const allow = new Set(ids ?? []);
|
|
3
|
+
const tools = [];
|
|
4
|
+
if (allow.has('read') || allow.has('search')) {
|
|
5
|
+
tools.push({
|
|
6
|
+
name: 'list_dir',
|
|
7
|
+
description: '列出关联项目里某个相对路径下的文件和目录。',
|
|
8
|
+
parameters: {
|
|
9
|
+
type: 'object',
|
|
10
|
+
properties: { path: { type: 'string', description: '相对项目根的目录,空表示根目录' } },
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
if (allow.has('read')) {
|
|
15
|
+
tools.push({
|
|
16
|
+
name: 'read_file',
|
|
17
|
+
description: '读取关联项目里的 UTF-8 文本文件(.dsh-im 内部状态目录不可读)。',
|
|
18
|
+
parameters: {
|
|
19
|
+
type: 'object',
|
|
20
|
+
properties: { path: { type: 'string', description: '相对项目根的文件路径' } },
|
|
21
|
+
required: ['path'],
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
if (allow.has('search')) {
|
|
26
|
+
tools.push({
|
|
27
|
+
name: 'search_files',
|
|
28
|
+
description: '在关联项目文本文件中搜索一段关键字(不搜 .dsh-im 内部状态目录)。',
|
|
29
|
+
parameters: {
|
|
30
|
+
type: 'object',
|
|
31
|
+
properties: { query: { type: 'string' } },
|
|
32
|
+
required: ['query'],
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
if (allow.has('write')) {
|
|
37
|
+
tools.push({
|
|
38
|
+
name: 'write_file',
|
|
39
|
+
description: '写到本群的群工作区(.dsh-im/groups/<本群>/下),用于纪要/草案/验收标准;不能改项目源码。',
|
|
40
|
+
parameters: {
|
|
41
|
+
type: 'object',
|
|
42
|
+
properties: {
|
|
43
|
+
path: { type: 'string', description: '相对群工作区的路径,例如 纪要/验收标准.md' },
|
|
44
|
+
content: { type: 'string', description: '文件内容' },
|
|
45
|
+
},
|
|
46
|
+
required: ['path', 'content'],
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
if (allow.has('check')) {
|
|
51
|
+
tools.push({
|
|
52
|
+
name: 'run_check',
|
|
53
|
+
description: '跑一次项目检查(不能装依赖、不能执行任意命令、不能写源码):'
|
|
54
|
+
+ 'target="typecheck" 跑 `npx tsc -p tsconfig.json --noEmit`;'
|
|
55
|
+
+ '其余取值等价 `npx vitest run [目标]`(填相对工作区的测试文件/目录,省略表示全量)。'
|
|
56
|
+
+ '讨论 bug 需要证据时用它;跑不了先看报错里的“当前检查根”,那是群关联项目目录。',
|
|
57
|
+
parameters: {
|
|
58
|
+
type: 'object',
|
|
59
|
+
properties: {
|
|
60
|
+
target: { type: 'string', description: 'typecheck,或相对工作区的测试文件/目录(省略=全量 vitest)' },
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
if (allow.has('memory')) {
|
|
66
|
+
tools.push({
|
|
67
|
+
name: 'memory_search',
|
|
68
|
+
description: '在当前会话的历史记录里检索相关旧消息(只返回句柄:#seq / 状态 / 作者 / 40 字标签,**不含原文**)。'
|
|
69
|
+
+ '句柄只表示"存在",不要复述没看到的内容;需要原文时用 memory_expand 按 #seq 展开。',
|
|
70
|
+
parameters: {
|
|
71
|
+
type: 'object',
|
|
72
|
+
properties: { query: { type: 'string', description: '检索词:可以用 #seq、引号里的原话、文件名、时间/编号、人名' } },
|
|
73
|
+
required: ['query'],
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
tools.push({
|
|
77
|
+
name: 'memory_expand',
|
|
78
|
+
description: '展开历史原文:传 seqs(如 "12" 或 "12,30",最多 3 条、每条 600 字)按句柄展开;'
|
|
79
|
+
+ '或传 question 走同轮多跳展开(问题→章节目录→代表句柄→最多再跳一次,≤2 跳/轮;'
|
|
80
|
+
+ '回执里带 hops/expandChars/hopBudgetUsed,触顶会显式说明缺口)。两种形式都按不可信输入包裹。',
|
|
81
|
+
parameters: {
|
|
82
|
+
type: 'object',
|
|
83
|
+
properties: {
|
|
84
|
+
seqs: { type: 'string', description: '要展开的消息序号,如 "12" 或 "12,30"(最多 3 条)' },
|
|
85
|
+
question: { type: 'string', description: '(可选)要查的问题;传了就按问题做多跳展开,和 seqs 二选一' },
|
|
86
|
+
hops: { type: 'string', description: '(可选)多跳展开的跳数,目前上限 2,默认按上限执行' },
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
return tools;
|
|
92
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 角色工具授权模型:来源组 + 单工具。
|
|
3
|
+
*
|
|
4
|
+
* 背景:DSH 的 tools.register 没有 owner/来源元数据,唯一可靠信号是命名约定
|
|
5
|
+
* (MCP 的 mcp__<server>__<tool>)。所以授权不看“内置还是自己装的”,只看
|
|
6
|
+
* “这个工具属于哪个来源组 / 是否被显式授予”。
|
|
7
|
+
*
|
|
8
|
+
* 授权条目(Persona.toolAllow: string[]):
|
|
9
|
+
* - read/search/write/check:产品内置插件工具能力(read_file/list_dir/search_files/write_file/run_check)
|
|
10
|
+
* - grp:<groupId>:整组授予(含以后新增的工具),例如 grp:mcp:aippt、grp:browser
|
|
11
|
+
* - tool:<toolName>:只授予单个宿主工具
|
|
12
|
+
* - 旧数据兼容:browser → grp:browser,image → grp:image
|
|
13
|
+
*
|
|
14
|
+
* 契约详见 docs/TOOL-GRANTS.md。
|
|
15
|
+
*/
|
|
16
|
+
/** 主会话继承下来的能力标签。角色只能从这里减,不能往外加。没有“任意终端”。 */
|
|
17
|
+
export declare const INHERITED_TOOLS: {
|
|
18
|
+
id: string;
|
|
19
|
+
label: string;
|
|
20
|
+
}[];
|
|
21
|
+
/** 产品内置的 4 个能力(插件自己注册的工具,不依赖宿主工具层)。 */
|
|
22
|
+
export declare const CORE_CAPABILITIES: readonly ['read', 'search', 'write', 'check', 'memory'];
|
|
23
|
+
export type CoreCapability = (typeof CORE_CAPABILITIES)[number];
|
|
24
|
+
/** 每个内置能力对应的插件工具(native 与 ctx.llm 两条路径共用同一目录)。 */
|
|
25
|
+
export declare const CORE_CAPABILITY_TOOLS: Record<CoreCapability, readonly string[]>;
|
|
26
|
+
/** list_dir 由读或搜索任一能力覆盖。 */
|
|
27
|
+
export declare const LIST_DIR_TOOL = "list_dir";
|
|
28
|
+
/** 新建角色默认勾上的能力。浏览器/画图/宿主扩展默认关闭。 */
|
|
29
|
+
export declare function defaultToolAllow(): string[];
|
|
30
|
+
export declare function inheritedToolIds(): string[];
|
|
31
|
+
export declare function toolLabel(id: string): string;
|
|
32
|
+
/** 插件注册进模型的工具名。 */
|
|
33
|
+
export declare const PLUGIN_TOOL_NAMES: readonly ["list_dir", 'read_file', 'search_files', 'write_file', 'run_check', 'memory_search', 'memory_expand'];
|
|
34
|
+
/** 画图工具(Magpie MCP server 产出)。 */
|
|
35
|
+
export declare const IMAGE_TOOL_NAMES: Set<string>;
|
|
36
|
+
/** 工具来源组声明:声明优先于 MCP 命名约定。 */
|
|
37
|
+
export interface ToolGroupDecl {
|
|
38
|
+
id: string;
|
|
39
|
+
label: string;
|
|
40
|
+
match?: {
|
|
41
|
+
/** 精确工具名 */
|
|
42
|
+
names?: string[];
|
|
43
|
+
/** 工具名前缀 */
|
|
44
|
+
prefixes?: string[];
|
|
45
|
+
/** MCP server 名(匹配 mcp__<server>__<tool>) */
|
|
46
|
+
mcpServers?: string[];
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* 出厂声明。bcdp / magpie 不再享受代码特例,只是这里的两条声明,
|
|
51
|
+
* 用户配置的 toolGroups 会追加在后面(同 id 覆盖)。
|
|
52
|
+
*/
|
|
53
|
+
export declare const BUILTIN_TOOL_GROUPS: ToolGroupDecl[];
|
|
54
|
+
/** 未声明来源的宿主工具统一落到这个组,仍可单工具勾选。 */
|
|
55
|
+
export declare const OTHER_TOOL_GROUP: {
|
|
56
|
+
readonly id: 'ext:other';
|
|
57
|
+
readonly label: '其他宿主工具(未声明来源)';
|
|
58
|
+
};
|
|
59
|
+
/** MCP 约定分组的 id 前缀。 */
|
|
60
|
+
export declare const MCP_GROUP_PREFIX = "mcp:";
|
|
61
|
+
/**
|
|
62
|
+
* 永远不可授予的宿主工具前缀:连接器管理(配置/发布/导入)属于部署操作,
|
|
63
|
+
* 专家角色任何情况下都不能用。它们不进入工具目录(UI 不可见、不可勾),
|
|
64
|
+
* guard 里还有第二道无条件拒绝。
|
|
65
|
+
*/
|
|
66
|
+
export declare const NEVER_GRANTABLE_PREFIXES: readonly ['mcp_connector_'];
|
|
67
|
+
export declare function isNeverGrantableHostTool(name: string): boolean;
|
|
68
|
+
export interface DiscoveredToolGroup {
|
|
69
|
+
id: string;
|
|
70
|
+
label: string;
|
|
71
|
+
/** builtin=出厂声明,declared=用户配置声明,mcp=命名约定,other=兜底 */
|
|
72
|
+
kind: 'builtin' | 'declared' | 'mcp' | 'other';
|
|
73
|
+
tools: string[];
|
|
74
|
+
}
|
|
75
|
+
export interface ToolGroupCatalog {
|
|
76
|
+
/** 产品内置能力(插件工具,两条传输都可用)。 */
|
|
77
|
+
capabilities: {
|
|
78
|
+
id: CoreCapability;
|
|
79
|
+
label: string;
|
|
80
|
+
tools: string[];
|
|
81
|
+
}[];
|
|
82
|
+
/** 宿主工具来源组(只在 native 生效)。 */
|
|
83
|
+
groups: DiscoveredToolGroup[];
|
|
84
|
+
}
|
|
85
|
+
/** 从 tools.schemas() 的返回值里取工具名(兼容 string / {name} 两种形状)。 */
|
|
86
|
+
export declare function schemaToolNames(listed: unknown): string[];
|
|
87
|
+
/** 解析 mcp__<server>__<tool>。 */
|
|
88
|
+
export declare function mcpServerOf(name: string): string | undefined;
|
|
89
|
+
/** 工具 → 来源组 id(声明优先 → MCP 约定 → 兜底组)。 */
|
|
90
|
+
export declare function groupIdForTool(name: string, declared?: readonly ToolGroupDecl[]): string;
|
|
91
|
+
export declare function groupLabelFor(groupId: string, declared?: readonly ToolGroupDecl[]): string;
|
|
92
|
+
/** 发现宿主工具来源组。names 是宿主 tools.schemas() 的全局视图。 */
|
|
93
|
+
export declare function discoverToolGroups(names: readonly string[], declared?: readonly ToolGroupDecl[]): DiscoveredToolGroup[];
|
|
94
|
+
/** 产品内置能力 + 宿主来源组的完整目录(客户端角色编辑器 / 状态接口用)。 */
|
|
95
|
+
export declare function toolGroupCatalog(hostNames: readonly string[], declared?: readonly ToolGroupDecl[]): ToolGroupCatalog;
|
|
96
|
+
export type GrantKind = 'capability' | 'group' | 'tool';
|
|
97
|
+
/** 解析一个授权条目;非法返回 null。 */
|
|
98
|
+
export declare function parseGrant(entry: string): {
|
|
99
|
+
kind: GrantKind;
|
|
100
|
+
value: string;
|
|
101
|
+
} | null;
|
|
102
|
+
/**
|
|
103
|
+
* 规整授权条目:旧 id 迁移(browser/image → grp:)、去重、丢掉非法/已下线 id。
|
|
104
|
+
* 传了 toolDeny(旧数据)时按“内置能力全集 − deny”换算成 toolAllow。
|
|
105
|
+
*/
|
|
106
|
+
export declare function normalizeToolAllow(ids: unknown, deny?: unknown): string[] | undefined;
|
|
107
|
+
/** 只保留当前存在的能力 id(老数据里的 `shell` 等会被静默丢弃)。 */
|
|
108
|
+
export declare function legacyKnownToolAllow(ids: unknown): string[] | undefined;
|
|
109
|
+
/** 授权条目集合。 */
|
|
110
|
+
export declare function grantSet(toolAllow?: string[]): Set<string>;
|
|
111
|
+
/** 插件工具是否被授予(read/search/write/check)。 */
|
|
112
|
+
export declare function pluginToolGranted(name: string, toolAllow?: string[]): boolean;
|
|
113
|
+
/** 宿主工具是否被授予(单工具或所属组)。 */
|
|
114
|
+
export declare function hostToolGranted(name: string, toolAllow?: string[], declared?: readonly ToolGroupDecl[]): boolean;
|
|
115
|
+
/** 当前授权下真正生效的宿主工具(用于可见性收窄与会话重建判定)。 */
|
|
116
|
+
export declare function effectiveHostTools(hostNames: readonly string[], toolAllow?: string[], declared?: readonly ToolGroupDecl[]): string[];
|
|
117
|
+
/** 这个角色勾了、但这次没有注册进 agent 的能力(只统计 4 个插件能力)。 */
|
|
118
|
+
export declare function nativeCapabilityGap(toolAllow?: string[], covered?: readonly string[]): string[];
|
|
119
|
+
/** 勾选对应到要注册的插件工具。list_dir 属于读或搜索。undefined 表示继承全部能力。 */
|
|
120
|
+
export declare function pluginToolsForAllow(toolAllow?: string[]): string[];
|
|
121
|
+
/**
|
|
122
|
+
* 宿主工具执行前的硬拒绝。插件工具交给按能力收窄的那一层;宿主工具只认
|
|
123
|
+
* tool:/grp: 授权;另外永远拒绝连接器管理类工具。
|
|
124
|
+
*/
|
|
125
|
+
export declare function personaHostToolDenial(name: string, toolAllow?: string[], declared?: readonly ToolGroupDecl[]): string | undefined;
|
|
126
|
+
/** 授权条目的可读标签(成员卡/兼容横幅用)。 */
|
|
127
|
+
export declare function labelForGrant(entry: string, catalog?: ToolGroupCatalog): string;
|
|
128
|
+
/** 只校验形状,不校验“是否存在”:组/工具可能来自已卸载插件。 */
|
|
129
|
+
export declare function assertKnownTools(ids: string[]): void;
|
|
130
|
+
/** 旧接口:toolAllow/toolDeny → 规整后的 toolAllow。 */
|
|
131
|
+
export declare function resolveToolFilter(input: {
|
|
132
|
+
toolAllow?: string[];
|
|
133
|
+
toolDeny?: string[];
|
|
134
|
+
}): {
|
|
135
|
+
toolAllow?: string[];
|
|
136
|
+
toolDeny?: string[];
|
|
137
|
+
};
|