@dabuliu_wsy/dsh-im 0.0.0-stage → 0.3.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 +204 -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 +213 -0
- package/dist/core/im.js +1640 -0
- package/dist/core/index.d.ts +34 -0
- package/dist/core/index.js +34 -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/trash.d.ts +110 -0
- package/dist/core/trash.js +56 -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 +296 -0
- package/dist/host/agent-session.js +964 -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/group-files.d.ts +26 -0
- package/dist/host/group-files.js +142 -0
- package/dist/host/im-api.d.ts +408 -0
- package/dist/host/im-api.js +1188 -0
- package/dist/host/index.d.ts +103 -0
- package/dist/host/index.js +509 -0
- package/dist/host/media-type.d.ts +10 -0
- package/dist/host/media-type.js +39 -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/dist/host/trash-store.d.ts +32 -0
- package/dist/host/trash-store.js +316 -0
- package/dsh-plugin.json +25 -0
- package/lib/client.inner.cjs +3933 -0
- package/lib/client.js +3941 -0
- package/package.json +86 -6
|
@@ -0,0 +1,348 @@
|
|
|
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 const INHERITED_TOOLS = [
|
|
18
|
+
{ id: 'read', label: '读文件' },
|
|
19
|
+
{ id: 'search', label: '搜索' },
|
|
20
|
+
{ id: 'write', label: '写群目录' },
|
|
21
|
+
{ id: 'check', label: '跑检查(测试)' },
|
|
22
|
+
{ id: 'memory', label: '读历史(记忆检索)' },
|
|
23
|
+
{ id: 'browser', label: '浏览器(bcdp)' },
|
|
24
|
+
{ id: 'image', label: '画图(Magpie)' },
|
|
25
|
+
];
|
|
26
|
+
/** 产品内置的 4 个能力(插件自己注册的工具,不依赖宿主工具层)。 */
|
|
27
|
+
export const CORE_CAPABILITIES = ['read', 'search', 'write', 'check', 'memory'];
|
|
28
|
+
/** 每个内置能力对应的插件工具(native 与 ctx.llm 两条路径共用同一目录)。 */
|
|
29
|
+
export const CORE_CAPABILITY_TOOLS = {
|
|
30
|
+
read: ['read_file'],
|
|
31
|
+
search: ['search_files'],
|
|
32
|
+
write: ['write_file'],
|
|
33
|
+
check: ['run_check'],
|
|
34
|
+
memory: ['memory_search', 'memory_expand'],
|
|
35
|
+
};
|
|
36
|
+
/** list_dir 由读或搜索任一能力覆盖。 */
|
|
37
|
+
export const LIST_DIR_TOOL = 'list_dir';
|
|
38
|
+
/** 新建角色默认勾上的能力。浏览器/画图/宿主扩展默认关闭。 */
|
|
39
|
+
export function defaultToolAllow() {
|
|
40
|
+
return [...CORE_CAPABILITIES];
|
|
41
|
+
}
|
|
42
|
+
export function inheritedToolIds() {
|
|
43
|
+
return INHERITED_TOOLS.map((t) => t.id);
|
|
44
|
+
}
|
|
45
|
+
export function toolLabel(id) {
|
|
46
|
+
return INHERITED_TOOLS.find((t) => t.id === id)?.label ?? id;
|
|
47
|
+
}
|
|
48
|
+
/** 插件注册进模型的工具名。 */
|
|
49
|
+
export const PLUGIN_TOOL_NAMES = [LIST_DIR_TOOL, 'read_file', 'search_files', 'write_file', 'run_check', 'memory_search', 'memory_expand'];
|
|
50
|
+
/** 画图工具(Magpie MCP server 产出)。 */
|
|
51
|
+
export const IMAGE_TOOL_NAMES = new Set([
|
|
52
|
+
'mcp__magpie-image__generate_image',
|
|
53
|
+
'mcp__magpie-image__generate_video',
|
|
54
|
+
'mcp__magpie-image__generation_result',
|
|
55
|
+
]);
|
|
56
|
+
/**
|
|
57
|
+
* 出厂声明。bcdp / magpie 不再享受代码特例,只是这里的两条声明,
|
|
58
|
+
* 用户配置的 toolGroups 会追加在后面(同 id 覆盖)。
|
|
59
|
+
*/
|
|
60
|
+
export const BUILTIN_TOOL_GROUPS = [
|
|
61
|
+
{ id: 'browser', label: '浏览器(bcdp)', match: { prefixes: ['bcdp_'] } },
|
|
62
|
+
{ id: 'image', label: '画图(Magpie)', match: { mcpServers: ['magpie-image'] } },
|
|
63
|
+
];
|
|
64
|
+
/** 未声明来源的宿主工具统一落到这个组,仍可单工具勾选。 */
|
|
65
|
+
export const OTHER_TOOL_GROUP = { id: 'ext:other', label: '其他宿主工具(未声明来源)' };
|
|
66
|
+
/** MCP 约定分组的 id 前缀。 */
|
|
67
|
+
export const MCP_GROUP_PREFIX = 'mcp:';
|
|
68
|
+
/**
|
|
69
|
+
* 永远不可授予的宿主工具前缀:连接器管理(配置/发布/导入)属于部署操作,
|
|
70
|
+
* 专家角色任何情况下都不能用。它们不进入工具目录(UI 不可见、不可勾),
|
|
71
|
+
* guard 里还有第二道无条件拒绝。
|
|
72
|
+
*/
|
|
73
|
+
export const NEVER_GRANTABLE_PREFIXES = ['mcp_connector_'];
|
|
74
|
+
export function isNeverGrantableHostTool(name) {
|
|
75
|
+
return NEVER_GRANTABLE_PREFIXES.some((prefix) => name.startsWith(prefix));
|
|
76
|
+
}
|
|
77
|
+
/** 从 tools.schemas() 的返回值里取工具名(兼容 string / {name} 两种形状)。 */
|
|
78
|
+
export function schemaToolNames(listed) {
|
|
79
|
+
if (!Array.isArray(listed))
|
|
80
|
+
return [];
|
|
81
|
+
const names = [];
|
|
82
|
+
for (const item of listed) {
|
|
83
|
+
if (typeof item === 'string' && item)
|
|
84
|
+
names.push(item);
|
|
85
|
+
else if (item && typeof item === 'object' && typeof item.name === 'string')
|
|
86
|
+
names.push(item.name);
|
|
87
|
+
}
|
|
88
|
+
return names;
|
|
89
|
+
}
|
|
90
|
+
/** 解析 mcp__<server>__<tool>。 */
|
|
91
|
+
export function mcpServerOf(name) {
|
|
92
|
+
if (!name.startsWith('mcp__'))
|
|
93
|
+
return undefined;
|
|
94
|
+
const rest = name.slice('mcp__'.length);
|
|
95
|
+
const at = rest.indexOf('__');
|
|
96
|
+
if (at <= 0 || at + 2 >= rest.length)
|
|
97
|
+
return undefined;
|
|
98
|
+
return rest.slice(0, at);
|
|
99
|
+
}
|
|
100
|
+
function normalizeDecls(declared) {
|
|
101
|
+
const merged = new Map();
|
|
102
|
+
for (const decl of declared)
|
|
103
|
+
merged.set(decl.id, decl);
|
|
104
|
+
return [...merged.values()];
|
|
105
|
+
}
|
|
106
|
+
/** 一个工具落在哪个声明组(不看 MCP 约定)。 */
|
|
107
|
+
function declaredGroupOf(name, declares) {
|
|
108
|
+
const server = mcpServerOf(name);
|
|
109
|
+
for (const decl of declares) {
|
|
110
|
+
const match = decl.match;
|
|
111
|
+
if (!match)
|
|
112
|
+
continue;
|
|
113
|
+
if (match.names?.includes(name))
|
|
114
|
+
return decl;
|
|
115
|
+
if (match.prefixes?.some((prefix) => prefix.length > 0 && name.startsWith(prefix)))
|
|
116
|
+
return decl;
|
|
117
|
+
if (server !== undefined && match.mcpServers?.includes(server))
|
|
118
|
+
return decl;
|
|
119
|
+
}
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
/** 工具 → 来源组 id(声明优先 → MCP 约定 → 兜底组)。 */
|
|
123
|
+
export function groupIdForTool(name, declared = BUILTIN_TOOL_GROUPS) {
|
|
124
|
+
const decl = declaredGroupOf(name, normalizeDecls(declared));
|
|
125
|
+
if (decl)
|
|
126
|
+
return decl.id;
|
|
127
|
+
const server = mcpServerOf(name);
|
|
128
|
+
if (server !== undefined)
|
|
129
|
+
return `${MCP_GROUP_PREFIX}${server}`;
|
|
130
|
+
return OTHER_TOOL_GROUP.id;
|
|
131
|
+
}
|
|
132
|
+
export function groupLabelFor(groupId, declared = BUILTIN_TOOL_GROUPS) {
|
|
133
|
+
const decl = normalizeDecls(declared).find((item) => item.id === groupId);
|
|
134
|
+
if (decl)
|
|
135
|
+
return decl.label;
|
|
136
|
+
if (groupId.startsWith(MCP_GROUP_PREFIX))
|
|
137
|
+
return `MCP:${groupId.slice(MCP_GROUP_PREFIX.length)}`;
|
|
138
|
+
if (groupId === OTHER_TOOL_GROUP.id)
|
|
139
|
+
return OTHER_TOOL_GROUP.label;
|
|
140
|
+
return groupId;
|
|
141
|
+
}
|
|
142
|
+
/** 发现宿主工具来源组。names 是宿主 tools.schemas() 的全局视图。 */
|
|
143
|
+
export function discoverToolGroups(names, declared = BUILTIN_TOOL_GROUPS) {
|
|
144
|
+
const declares = normalizeDecls(declared);
|
|
145
|
+
const byId = new Map();
|
|
146
|
+
for (const name of [...new Set(names)].sort()) {
|
|
147
|
+
if (PLUGIN_TOOL_NAMES.includes(name))
|
|
148
|
+
continue;
|
|
149
|
+
// 连接器管理等永远不可授予的宿主工具不进目录:UI 不出现,也不参与授权计算。
|
|
150
|
+
if (isNeverGrantableHostTool(name))
|
|
151
|
+
continue;
|
|
152
|
+
const decl = declaredGroupOf(name, declares);
|
|
153
|
+
const server = mcpServerOf(name);
|
|
154
|
+
const builtin = decl !== undefined && BUILTIN_TOOL_GROUPS.some((item) => item.id === decl.id);
|
|
155
|
+
const id = decl?.id ?? (server !== undefined ? `${MCP_GROUP_PREFIX}${server}` : OTHER_TOOL_GROUP.id);
|
|
156
|
+
const group = byId.get(id) ?? {
|
|
157
|
+
id,
|
|
158
|
+
label: decl?.label ?? groupLabelFor(id, declares),
|
|
159
|
+
kind: decl === undefined ? (server !== undefined ? 'mcp' : 'other') : builtin ? 'builtin' : 'declared',
|
|
160
|
+
tools: [],
|
|
161
|
+
};
|
|
162
|
+
group.tools.push(name);
|
|
163
|
+
byId.set(id, group);
|
|
164
|
+
}
|
|
165
|
+
return [...byId.values()];
|
|
166
|
+
}
|
|
167
|
+
/** 产品内置能力 + 宿主来源组的完整目录(客户端角色编辑器 / 状态接口用)。 */
|
|
168
|
+
export function toolGroupCatalog(hostNames, declared = BUILTIN_TOOL_GROUPS) {
|
|
169
|
+
return {
|
|
170
|
+
capabilities: CORE_CAPABILITIES.map((id) => ({
|
|
171
|
+
id,
|
|
172
|
+
label: toolLabel(id),
|
|
173
|
+
tools: [...CORE_CAPABILITY_TOOLS[id]],
|
|
174
|
+
})),
|
|
175
|
+
groups: discoverToolGroups(hostNames, declared),
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
/** 解析一个授权条目;非法返回 null。 */
|
|
179
|
+
export function parseGrant(entry) {
|
|
180
|
+
if (typeof entry !== 'string')
|
|
181
|
+
return null;
|
|
182
|
+
const id = entry.trim();
|
|
183
|
+
if (!id)
|
|
184
|
+
return null;
|
|
185
|
+
if (id === 'browser' || id === 'image')
|
|
186
|
+
return { kind: 'group', value: id };
|
|
187
|
+
if (CORE_CAPABILITIES.includes(id))
|
|
188
|
+
return { kind: 'capability', value: id };
|
|
189
|
+
if (id.startsWith('grp:')) {
|
|
190
|
+
const value = id.slice(4).trim();
|
|
191
|
+
return value ? { kind: 'group', value } : null;
|
|
192
|
+
}
|
|
193
|
+
if (id.startsWith('tool:')) {
|
|
194
|
+
const value = id.slice(5).trim();
|
|
195
|
+
return /^[A-Za-z0-9_.:-]+$/.test(value) ? { kind: 'tool', value } : null;
|
|
196
|
+
}
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* 规整授权条目:旧 id 迁移(browser/image → grp:)、去重、丢掉非法/已下线 id。
|
|
201
|
+
* 传了 toolDeny(旧数据)时按“内置能力全集 − deny”换算成 toolAllow。
|
|
202
|
+
*/
|
|
203
|
+
export function normalizeToolAllow(ids, deny) {
|
|
204
|
+
const source = Array.isArray(ids)
|
|
205
|
+
? ids
|
|
206
|
+
: Array.isArray(deny)
|
|
207
|
+
? inheritedToolIds().filter((id) => !deny.includes(id))
|
|
208
|
+
: undefined;
|
|
209
|
+
if (source === undefined)
|
|
210
|
+
return undefined;
|
|
211
|
+
const out = [];
|
|
212
|
+
const seen = new Set();
|
|
213
|
+
for (const raw of source) {
|
|
214
|
+
if (typeof raw !== 'string')
|
|
215
|
+
continue;
|
|
216
|
+
const parsed = parseGrant(raw);
|
|
217
|
+
if (!parsed)
|
|
218
|
+
continue;
|
|
219
|
+
const normalized = parsed.kind === 'group' ? `grp:${parsed.value}` : parsed.kind === 'tool' ? `tool:${parsed.value}` : parsed.value;
|
|
220
|
+
if (seen.has(normalized))
|
|
221
|
+
continue;
|
|
222
|
+
seen.add(normalized);
|
|
223
|
+
out.push(normalized);
|
|
224
|
+
}
|
|
225
|
+
return out;
|
|
226
|
+
}
|
|
227
|
+
/** 只保留当前存在的能力 id(老数据里的 `shell` 等会被静默丢弃)。 */
|
|
228
|
+
export function legacyKnownToolAllow(ids) {
|
|
229
|
+
if (!Array.isArray(ids))
|
|
230
|
+
return undefined;
|
|
231
|
+
const known = new Set(inheritedToolIds());
|
|
232
|
+
return [...new Set(ids.filter((id) => typeof id === 'string' && known.has(id)))];
|
|
233
|
+
}
|
|
234
|
+
/** 授权条目集合。 */
|
|
235
|
+
export function grantSet(toolAllow) {
|
|
236
|
+
const set = new Set();
|
|
237
|
+
for (const entry of toolAllow ?? []) {
|
|
238
|
+
const parsed = parseGrant(entry);
|
|
239
|
+
if (!parsed)
|
|
240
|
+
continue;
|
|
241
|
+
if (parsed.kind === 'capability')
|
|
242
|
+
set.add(`cap:${parsed.value}`);
|
|
243
|
+
else if (parsed.kind === 'group')
|
|
244
|
+
set.add(`grp:${parsed.value}`);
|
|
245
|
+
else
|
|
246
|
+
set.add(`tool:${parsed.value}`);
|
|
247
|
+
}
|
|
248
|
+
return set;
|
|
249
|
+
}
|
|
250
|
+
/** 插件工具是否被授予(read/search/write/check)。 */
|
|
251
|
+
export function pluginToolGranted(name, toolAllow) {
|
|
252
|
+
if (toolAllow === undefined)
|
|
253
|
+
return true;
|
|
254
|
+
if (name === LIST_DIR_TOOL)
|
|
255
|
+
return toolAllow.includes('read') || toolAllow.includes('search');
|
|
256
|
+
const capability = CORE_CAPABILITIES.find((id) => CORE_CAPABILITY_TOOLS[id].includes(name));
|
|
257
|
+
return capability !== undefined && toolAllow.includes(capability);
|
|
258
|
+
}
|
|
259
|
+
/** 宿主工具是否被授予(单工具或所属组)。 */
|
|
260
|
+
export function hostToolGranted(name, toolAllow, declared = BUILTIN_TOOL_GROUPS) {
|
|
261
|
+
if (toolAllow === undefined)
|
|
262
|
+
return false;
|
|
263
|
+
if (isNeverGrantableHostTool(name))
|
|
264
|
+
return false;
|
|
265
|
+
const grants = grantSet(toolAllow);
|
|
266
|
+
if (grants.has(`tool:${name}`))
|
|
267
|
+
return true;
|
|
268
|
+
return grants.has(`grp:${groupIdForTool(name, declared)}`);
|
|
269
|
+
}
|
|
270
|
+
/** 当前授权下真正生效的宿主工具(用于可见性收窄与会话重建判定)。 */
|
|
271
|
+
export function effectiveHostTools(hostNames, toolAllow, declared = BUILTIN_TOOL_GROUPS) {
|
|
272
|
+
if (toolAllow === undefined)
|
|
273
|
+
return [];
|
|
274
|
+
return [...new Set(hostNames)].filter((name) => hostToolGranted(name, toolAllow, declared)).sort();
|
|
275
|
+
}
|
|
276
|
+
/** 这个角色勾了、但这次没有注册进 agent 的能力(只统计 4 个插件能力)。 */
|
|
277
|
+
export function nativeCapabilityGap(toolAllow, covered = []) {
|
|
278
|
+
const caps = new Set(toolAllow ?? []);
|
|
279
|
+
const have = new Set(covered);
|
|
280
|
+
return CORE_CAPABILITIES.filter((id) => caps.has(id) && !have.has(id));
|
|
281
|
+
}
|
|
282
|
+
/** 勾选对应到要注册的插件工具。list_dir 属于读或搜索。undefined 表示继承全部能力。 */
|
|
283
|
+
export function pluginToolsForAllow(toolAllow) {
|
|
284
|
+
const allowed = new Set(toolAllow ?? inheritedToolIds());
|
|
285
|
+
const names = [];
|
|
286
|
+
if (allowed.has('read') || allowed.has('search'))
|
|
287
|
+
names.push(LIST_DIR_TOOL);
|
|
288
|
+
if (allowed.has('read'))
|
|
289
|
+
names.push('read_file');
|
|
290
|
+
if (allowed.has('search'))
|
|
291
|
+
names.push('search_files');
|
|
292
|
+
if (allowed.has('write'))
|
|
293
|
+
names.push('write_file');
|
|
294
|
+
if (allowed.has('check'))
|
|
295
|
+
names.push('run_check');
|
|
296
|
+
// memory 也是插件能力(两个插件工具),native 与 ctx.llm 两条传输必须同权;
|
|
297
|
+
// 之前漏了这里会让原生会话静默没有记忆工具(真机复验发现)。
|
|
298
|
+
if (allowed.has('memory'))
|
|
299
|
+
names.push(...CORE_CAPABILITY_TOOLS.memory);
|
|
300
|
+
return names;
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* 宿主工具执行前的硬拒绝。插件工具交给按能力收窄的那一层;宿主工具只认
|
|
304
|
+
* tool:/grp: 授权;另外永远拒绝连接器管理类工具。
|
|
305
|
+
*/
|
|
306
|
+
export function personaHostToolDenial(name, toolAllow, declared = BUILTIN_TOOL_GROUPS) {
|
|
307
|
+
if (PLUGIN_TOOL_NAMES.includes(name))
|
|
308
|
+
return undefined;
|
|
309
|
+
if (!name)
|
|
310
|
+
return '专家角色不能使用宿主工具 (未命名)';
|
|
311
|
+
if (name.startsWith('mcp_connector_'))
|
|
312
|
+
return `专家角色不能使用连接器管理工具 ${name}`;
|
|
313
|
+
if (hostToolGranted(name, toolAllow, declared))
|
|
314
|
+
return undefined;
|
|
315
|
+
const group = groupLabelFor(groupIdForTool(name, declared), declared);
|
|
316
|
+
return `专家角色没有授予「${group}」里的工具 ${name}`;
|
|
317
|
+
}
|
|
318
|
+
/** 授权条目的可读标签(成员卡/兼容横幅用)。 */
|
|
319
|
+
export function labelForGrant(entry, catalog) {
|
|
320
|
+
const parsed = parseGrant(entry);
|
|
321
|
+
if (!parsed)
|
|
322
|
+
return entry;
|
|
323
|
+
if (parsed.kind === 'capability')
|
|
324
|
+
return toolLabel(parsed.value);
|
|
325
|
+
if (parsed.kind === 'group') {
|
|
326
|
+
const group = catalog?.groups.find((item) => item.id === parsed.value);
|
|
327
|
+
return group?.label ?? groupLabelFor(parsed.value);
|
|
328
|
+
}
|
|
329
|
+
return `工具 ${parsed.value}`;
|
|
330
|
+
}
|
|
331
|
+
/** 只校验形状,不校验“是否存在”:组/工具可能来自已卸载插件。 */
|
|
332
|
+
export function assertKnownTools(ids) {
|
|
333
|
+
const bad = ids.filter((id) => parseGrant(id) === null);
|
|
334
|
+
if (bad.length)
|
|
335
|
+
throw new Error(`不能新增工具:${bad.join('、')}(只支持 read/search/write/check 或 grp:/tool: 授权)`);
|
|
336
|
+
}
|
|
337
|
+
/** 旧接口:toolAllow/toolDeny → 规整后的 toolAllow。 */
|
|
338
|
+
export function resolveToolFilter(input) {
|
|
339
|
+
if (input.toolAllow) {
|
|
340
|
+
assertKnownTools(input.toolAllow);
|
|
341
|
+
return { toolAllow: normalizeToolAllow(input.toolAllow) };
|
|
342
|
+
}
|
|
343
|
+
if (input.toolDeny) {
|
|
344
|
+
assertKnownTools(input.toolDeny);
|
|
345
|
+
return { toolAllow: normalizeToolAllow(undefined, input.toolDeny) };
|
|
346
|
+
}
|
|
347
|
+
return {};
|
|
348
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 软删除(回收站)契约:纯类型 + 纯函数。
|
|
3
|
+
*
|
|
4
|
+
* **这里不能 import node:fs**(`src/core` 会被打进客户端 bundle)。
|
|
5
|
+
* 物理目录搬运在 `src/host/trash-store.ts`;状态变更在 `src/core/im.ts`。
|
|
6
|
+
*
|
|
7
|
+
* 删除口径(已和用户确认):
|
|
8
|
+
* - 「删除会话 / 解散群」:会话从 state 移除,数据目录移到 `<根>/.dsh-im/trash/<id>/data/<threadId>/`;
|
|
9
|
+
* - 「清空聊天记录」:会话保留,消息/任务板/共识/轨迹/附件移到回收站,之后还能恢复;
|
|
10
|
+
* - 「删除角色」:角色从通讯录移除;它的私聊/三人小窗会话一起删除;群内只移除成员、群历史保留。
|
|
11
|
+
*
|
|
12
|
+
* 快照(payload)随 trash manifest 落盘,恢复时不需要再读 state.json ——
|
|
13
|
+
* 删除那一刻的数据就是恢复后的数据,不靠"重建猜测"。
|
|
14
|
+
*/
|
|
15
|
+
import type { Activity, MemoryEntry, Persona, Thread, ThreadKind } from './types.ts';
|
|
16
|
+
export declare const TRASH_KINDS: readonly ['thread', 'persona', 'history'];
|
|
17
|
+
export type TrashKind = (typeof TRASH_KINDS)[number];
|
|
18
|
+
export declare const TRASH_KIND_LABEL: Record<TrashKind, string>;
|
|
19
|
+
/** 回收站清单项(跨 host/client 共用;host 落 manifest,客户端只读这份元数据)。 */
|
|
20
|
+
export interface TrashEntryMeta {
|
|
21
|
+
id: string;
|
|
22
|
+
kind: TrashKind;
|
|
23
|
+
/** 列表主标题:群名 / 角色名 / 会话名。 */
|
|
24
|
+
label: string;
|
|
25
|
+
/** 副标题:类型 + 规模(如「群 · 12 条消息 · 4 人」)。 */
|
|
26
|
+
detail: string;
|
|
27
|
+
deletedAt: number;
|
|
28
|
+
/** 已经移到回收站的数据目录(threadId);角色记录里是被连带删除的会话。 */
|
|
29
|
+
threadIds: string[];
|
|
30
|
+
/** 数据目录字节数(0 = 没有目录或统计失败)。 */
|
|
31
|
+
bytes: number;
|
|
32
|
+
restorable: boolean;
|
|
33
|
+
}
|
|
34
|
+
/** 删除角色时,该角色在某个群里的成员位置(恢复时放回原位、恢复活跃度)。 */
|
|
35
|
+
export interface TrashPersonaMembership {
|
|
36
|
+
threadId: string;
|
|
37
|
+
/** 删除时在 memberIds 里的下标。 */
|
|
38
|
+
index: number;
|
|
39
|
+
activity: Activity;
|
|
40
|
+
}
|
|
41
|
+
/** 恢复一条回收站记录所需的全部状态快照。 */
|
|
42
|
+
export interface TrashPayload {
|
|
43
|
+
/** kind=thread / history:被删 / 被清空前的会话快照(含消息)。 */
|
|
44
|
+
thread?: Thread;
|
|
45
|
+
/** kind=persona:被连带删除的私聊 / 三人小窗快照。 */
|
|
46
|
+
threads?: Thread[];
|
|
47
|
+
/** kind=persona:被删除的角色。 */
|
|
48
|
+
persona?: Persona;
|
|
49
|
+
/** kind=persona:该角色原本在哪些群、什么位置。 */
|
|
50
|
+
memberships?: TrashPersonaMembership[];
|
|
51
|
+
/** 被删除/清空会话时一并清理的线程级记忆(恢复时还原)。 */
|
|
52
|
+
memories?: MemoryEntry[];
|
|
53
|
+
/** 被删除/清空会话时一并清理的唤醒摘要(key = `personaId:threadId`)。 */
|
|
54
|
+
wakeExcerpts?: Record<string, string[]>;
|
|
55
|
+
}
|
|
56
|
+
/** ImService.deleteThread 的结果:删完的内存状态 + 交给 host 回收站的快照。 */
|
|
57
|
+
export interface DeleteThreadPlan {
|
|
58
|
+
threadId: string;
|
|
59
|
+
label: string;
|
|
60
|
+
detail: string;
|
|
61
|
+
snapshot: Thread;
|
|
62
|
+
}
|
|
63
|
+
/** ImService.clearThreadHistory 的结果。 */
|
|
64
|
+
export interface ClearHistoryPlan {
|
|
65
|
+
threadId: string;
|
|
66
|
+
label: string;
|
|
67
|
+
detail: string;
|
|
68
|
+
snapshot: Thread;
|
|
69
|
+
memories: MemoryEntry[];
|
|
70
|
+
wakeExcerpts: Record<string, string[]>;
|
|
71
|
+
}
|
|
72
|
+
/** ImService.deletePersona 的结果。 */
|
|
73
|
+
export interface DeletePersonaPlan {
|
|
74
|
+
persona: Persona;
|
|
75
|
+
label: string;
|
|
76
|
+
detail: string;
|
|
77
|
+
/** 被连带删除的私聊 / 三人小窗快照。 */
|
|
78
|
+
threads: Thread[];
|
|
79
|
+
memberships: TrashPersonaMembership[];
|
|
80
|
+
memories: MemoryEntry[];
|
|
81
|
+
wakeExcerpts: Record<string, string[]>;
|
|
82
|
+
}
|
|
83
|
+
/** ImService.restorePersona 的结果;部分会话/群可能已不存在,必须如实回报。 */
|
|
84
|
+
export interface RestorePersonaResult {
|
|
85
|
+
persona: Persona;
|
|
86
|
+
threads: Thread[];
|
|
87
|
+
skippedThreadIds: string[];
|
|
88
|
+
skippedMembershipThreadIds: string[];
|
|
89
|
+
}
|
|
90
|
+
/** 深拷贝:快照必须与内存对象脱钩,否则删除后拖动状态会污染回收站里的数据。 */
|
|
91
|
+
export declare function cloneJson<T>(value: T): T;
|
|
92
|
+
/**
|
|
93
|
+
* 生成可安全落盘的会话快照。
|
|
94
|
+
*
|
|
95
|
+
* 去掉的字段都是「运行时瞬时态」或「待落盘队列」:
|
|
96
|
+
* - speakingPersonaId / nextHint / blocked / floorDecisions:本轮状态,恢复后重新开始更合理;
|
|
97
|
+
* - pendingMetrics / pendingFactOps / extractedFacts / retractedFactIds:删除前已经落过盘,
|
|
98
|
+
* 留在快照里会在恢复后重复写事件/指标;
|
|
99
|
+
* - unread:恢复后应该是未读 0(用户主动恢复,不该再弹红点)。
|
|
100
|
+
*/
|
|
101
|
+
export declare function sanitizeThreadSnapshot(thread: Thread): Thread;
|
|
102
|
+
export declare function threadKindLabel(kind: ThreadKind): string;
|
|
103
|
+
/** 回收站副标题:类型 + 消息数 + 群/小窗人数。 */
|
|
104
|
+
export declare function trashThreadDetail(thread: Thread): string;
|
|
105
|
+
/** 清空记录后的副标题:写明清掉多少条,方便用户在回收站里认出来。 */
|
|
106
|
+
export declare function trashHistoryDetail(snapshot: Thread): string;
|
|
107
|
+
/** 删除角色的副标题:几个会话 + 几个群受影响。 */
|
|
108
|
+
export declare function trashPersonaDetail(_persona: Persona, threads: readonly Thread[], memberships: readonly TrashPersonaMembership[]): string;
|
|
109
|
+
/** 清空记录只有在「会话清空后没有新消息」时才能恢复(否则恢复会覆盖新记录)。 */
|
|
110
|
+
export declare function canRestoreHistory(thread: Thread): boolean;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export const TRASH_KINDS = ['thread', 'persona', 'history'];
|
|
2
|
+
export const TRASH_KIND_LABEL = {
|
|
3
|
+
thread: '会话',
|
|
4
|
+
persona: '角色',
|
|
5
|
+
history: '聊天记录',
|
|
6
|
+
};
|
|
7
|
+
/** 深拷贝:快照必须与内存对象脱钩,否则删除后拖动状态会污染回收站里的数据。 */
|
|
8
|
+
export function cloneJson(value) {
|
|
9
|
+
return JSON.parse(JSON.stringify(value));
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 生成可安全落盘的会话快照。
|
|
13
|
+
*
|
|
14
|
+
* 去掉的字段都是「运行时瞬时态」或「待落盘队列」:
|
|
15
|
+
* - speakingPersonaId / nextHint / blocked / floorDecisions:本轮状态,恢复后重新开始更合理;
|
|
16
|
+
* - pendingMetrics / pendingFactOps / extractedFacts / retractedFactIds:删除前已经落过盘,
|
|
17
|
+
* 留在快照里会在恢复后重复写事件/指标;
|
|
18
|
+
* - unread:恢复后应该是未读 0(用户主动恢复,不该再弹红点)。
|
|
19
|
+
*/
|
|
20
|
+
export function sanitizeThreadSnapshot(thread) {
|
|
21
|
+
const snapshot = cloneJson(thread);
|
|
22
|
+
delete snapshot.speakingPersonaId;
|
|
23
|
+
delete snapshot.nextHint;
|
|
24
|
+
delete snapshot.blocked;
|
|
25
|
+
delete snapshot.floorDecisions;
|
|
26
|
+
delete snapshot.pendingMetrics;
|
|
27
|
+
delete snapshot.pendingFactOps;
|
|
28
|
+
delete snapshot.extractedFacts;
|
|
29
|
+
delete snapshot.retractedFactIds;
|
|
30
|
+
snapshot.unread = 0;
|
|
31
|
+
snapshot.roundMode = 'idle';
|
|
32
|
+
return snapshot;
|
|
33
|
+
}
|
|
34
|
+
export function threadKindLabel(kind) {
|
|
35
|
+
return kind === 'group' ? '群' : kind === 'triad' ? '三人小窗' : '私聊';
|
|
36
|
+
}
|
|
37
|
+
/** 回收站副标题:类型 + 消息数 + 群/小窗人数。 */
|
|
38
|
+
export function trashThreadDetail(thread) {
|
|
39
|
+
const people = thread.kind === 'group' ? ` · ${thread.memberIds.length + 1} 人` : '';
|
|
40
|
+
return `${threadKindLabel(thread.kind)} · ${thread.messages.length} 条消息${people}`;
|
|
41
|
+
}
|
|
42
|
+
/** 清空记录后的副标题:写明清掉多少条,方便用户在回收站里认出来。 */
|
|
43
|
+
export function trashHistoryDetail(snapshot) {
|
|
44
|
+
return `聊天记录 · ${snapshot.messages.length} 条消息`;
|
|
45
|
+
}
|
|
46
|
+
/** 删除角色的副标题:几个会话 + 几个群受影响。 */
|
|
47
|
+
export function trashPersonaDetail(_persona, threads, memberships) {
|
|
48
|
+
const parts = [threads.length ? `${threads.length} 个会话一起删除` : '没有私聊会话'];
|
|
49
|
+
if (memberships.length)
|
|
50
|
+
parts.push(`从 ${memberships.length} 个群移除`);
|
|
51
|
+
return parts.join(' · ');
|
|
52
|
+
}
|
|
53
|
+
/** 清空记录只有在「会话清空后没有新消息」时才能恢复(否则恢复会覆盖新记录)。 */
|
|
54
|
+
export function canRestoreHistory(thread) {
|
|
55
|
+
return thread.messages.length === 0;
|
|
56
|
+
}
|