@cheemsup/dsh-plugin-console 0.0.0-stage → 0.1.1
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 +85 -2
- package/cordis.patch.yml +5 -0
- package/lib/client.activity.js +222 -0
- package/lib/client.apply.js +171 -0
- package/lib/client.changes.js +206 -0
- package/lib/client.cleanup.js +270 -0
- package/lib/client.control.js +242 -0
- package/lib/client.dialog.js +77 -0
- package/lib/client.draft.js +280 -0
- package/lib/client.editor-messages.js +421 -0
- package/lib/client.editor-store.js +266 -0
- package/lib/client.editor.js +433 -0
- package/lib/client.effects.js +115 -0
- package/lib/client.influence.js +361 -0
- package/lib/client.js +216 -0
- package/lib/client.page.js +584 -0
- package/lib/client.plugin-filters.js +62 -0
- package/lib/client.plugins.js +504 -0
- package/lib/client.preview-messages.js +495 -0
- package/lib/client.preview-raw.js +132 -0
- package/lib/client.preview-store.js +128 -0
- package/lib/client.preview-tools.js +194 -0
- package/lib/client.preview.js +447 -0
- package/lib/client.schema.js +327 -0
- package/lib/client.search.js +408 -0
- package/lib/client.session-detail.js +503 -0
- package/lib/client.sessions.js +495 -0
- package/lib/client.shared.js +559 -0
- package/lib/client.store.js +452 -0
- package/lib/client.styles.js +524 -0
- package/lib/client.template-card.js +240 -0
- package/lib/client.tools.js +366 -0
- package/lib/client.workspace-picker.js +204 -0
- package/lib/host/activity.js +252 -0
- package/lib/host/assembly-preview.js +244 -0
- package/lib/host/attribution.js +307 -0
- package/lib/host/catalog.js +133 -0
- package/lib/host/categorize.js +40 -0
- package/lib/host/constants.js +66 -0
- package/lib/host/control.js +159 -0
- package/lib/host/entries.js +289 -0
- package/lib/host/fiber.js +86 -0
- package/lib/host/fs.js +99 -0
- package/lib/host/health.js +178 -0
- package/lib/host/http.js +65 -0
- package/lib/host/impact.js +132 -0
- package/lib/host/layers.js +132 -0
- package/lib/host/masks.js +145 -0
- package/lib/host/modes.js +96 -0
- package/lib/host/names.js +44 -0
- package/lib/host/patch-scan.js +103 -0
- package/lib/host/preview-conditions.js +281 -0
- package/lib/host/preview-contexts.js +174 -0
- package/lib/host/preview-envelope.js +31 -0
- package/lib/host/preview-instructions.js +450 -0
- package/lib/host/preview-messages.js +284 -0
- package/lib/host/preview-modules.js +233 -0
- package/lib/host/preview-skills.js +200 -0
- package/lib/host/prompt.js +213 -0
- package/lib/host/recipe-overlay.js +209 -0
- package/lib/host/request-preview.js +114 -0
- package/lib/host/routes/activation.js +96 -0
- package/lib/host/routes/activity.js +22 -0
- package/lib/host/routes/apply.js +93 -0
- package/lib/host/routes/cleanup.js +62 -0
- package/lib/host/routes/control.js +22 -0
- package/lib/host/routes/impact.js +19 -0
- package/lib/host/routes/operations.js +22 -0
- package/lib/host/routes/preview.js +58 -0
- package/lib/host/routes/session-influence.js +23 -0
- package/lib/host/routes/snapshot.js +21 -0
- package/lib/host/routes/template.js +54 -0
- package/lib/host/routes/templates.js +28 -0
- package/lib/host/scope-impact.js +125 -0
- package/lib/host/session-events.js +39 -0
- package/lib/host/session-influence.js +475 -0
- package/lib/host/sessions.js +293 -0
- package/lib/host/snapshot.js +218 -0
- package/lib/host/source-scan.js +225 -0
- package/lib/host/templates/cleanup.js +219 -0
- package/lib/host/templates/inheritance.js +144 -0
- package/lib/host/templates/materialize.js +317 -0
- package/lib/host/templates/message-overlay.js +184 -0
- package/lib/host/templates/repository.js +323 -0
- package/lib/host/templates/store.js +131 -0
- package/lib/host/tool-set.js +150 -0
- package/lib/host/tools.js +129 -0
- package/lib/index.js +103 -0
- package/package.json +111 -4
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 面向用户的能力分类。
|
|
3
|
+
*
|
|
4
|
+
* 分类依据是包的职责而不是命名:用户问「哪些插件给我加了工具」时,
|
|
5
|
+
* `dsh-tool-fs` 和 `dsh-mcp-client` 应该在同一个栏,尽管后者名字里没有 tool。
|
|
6
|
+
* 顺序有意义——先命中先返回,所以 `tool-` 规则必须排在 `client-*` 之前。
|
|
7
|
+
* @module dsh-plugin-console/host/categorize
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 把插件归到一个面向用户的「能力类别」。
|
|
12
|
+
* @param {string} moduleName - 行的模块标识。
|
|
13
|
+
* @param {object} entry - 已采集的条目(用于看它注册了什么)。
|
|
14
|
+
* @returns {string} 类别 id。
|
|
15
|
+
*/
|
|
16
|
+
export function categorize(moduleName, entry) {
|
|
17
|
+
const name = typeof moduleName === 'string' ? moduleName : ''
|
|
18
|
+
// 结构行与 MCP 客户端优先
|
|
19
|
+
if (name.startsWith('cordis:')) return 'structure'
|
|
20
|
+
if (/dsh-mcp-client|dsh-mcp-resources/.test(name)) return 'extend'
|
|
21
|
+
// 工具:名字里带 tool,或实际注册了工具且不是 MCP
|
|
22
|
+
if (/dsh-tool[-/]|\/tools$|-tools$/.test(name)) return 'tool'
|
|
23
|
+
// 界面
|
|
24
|
+
if (/client-ui/.test(name)) return 'ui'
|
|
25
|
+
// 模型
|
|
26
|
+
if (/dsh-llm/.test(name)) return 'model'
|
|
27
|
+
// 会话与持久化
|
|
28
|
+
if (/dsh-session|session-projection|session-query|session-format|session-persistence/.test(name)) return 'session'
|
|
29
|
+
// 安全
|
|
30
|
+
if (/sandbox|approval|permission-presets|fs-observation-policy|authorization|credentials/.test(name)) return 'safety'
|
|
31
|
+
// 传输与网关
|
|
32
|
+
if (/dsh-api-|api-gateway|typert|remote-access|dsh-acp|jsonrpc/.test(name)) return 'transport'
|
|
33
|
+
// 扩展能力
|
|
34
|
+
if (/dsh-skill|dsh-command|dsh-goal|plan-mode|compaction|dsh-subagent|workflow|dsh-ptc|dsh-web|repeat-tool|spill|token-meter|context-|persona|agent-instructions|system-prompt|attachment|file-reference|deliverable/.test(name)) return 'extend'
|
|
35
|
+
// 客户端内核
|
|
36
|
+
if (/dsh-client-|dsh-web-app|dsh-frontend|dsh-web-frontend/.test(name)) return 'client'
|
|
37
|
+
// 其余是宿主基础设施
|
|
38
|
+
if (/dsh-storage|dsh-subprocess|dsh-timer|cordis-plugin|dsh-hmr|dsh-otel|dsh-shell-env|dsh-config-editor|dsh-settings|dsh-workspace|dsh-home-paths|dsh-atomic|dsh-timeout|dsh-chunked|dsh-jobs|plugin-manager|plugin-inventory|package-inventory|dsh-brand|dsh-deepseek-account|dsh-agent|dsh-scope|dsh-tools/.test(name)) return 'infra'
|
|
39
|
+
return 'other'
|
|
40
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* host 半的常量镜像。
|
|
3
|
+
*
|
|
4
|
+
* 这里只有「读代码的人需要知道、但运行时不一定拿得到」的东西:Cordis 的状态
|
|
5
|
+
* 枚举是跨包 const enum(没有运行时对象可 import),路由路径必须是字面量。
|
|
6
|
+
* @module dsh-plugin-console/host/constants
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** Fiber 状态常量镜像:cordis 的 FiberState 是跨包 const enum。 */
|
|
10
|
+
export const FIBER_STATE = {
|
|
11
|
+
PENDING: 0,
|
|
12
|
+
LOADING: 1,
|
|
13
|
+
ACTIVE: 2,
|
|
14
|
+
FAILED: 3,
|
|
15
|
+
DISPOSED: 4,
|
|
16
|
+
UNLOADING: 5,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Fiber 状态到公共阶段词汇的映射;已销毁折叠为 null。 */
|
|
20
|
+
export const FIBER_PHASE = {
|
|
21
|
+
[FIBER_STATE.PENDING]: 'pending',
|
|
22
|
+
[FIBER_STATE.LOADING]: 'loading',
|
|
23
|
+
[FIBER_STATE.ACTIVE]: 'active',
|
|
24
|
+
[FIBER_STATE.FAILED]: 'failed',
|
|
25
|
+
[FIBER_STATE.UNLOADING]: 'unloading',
|
|
26
|
+
[FIBER_STATE.DISPOSED]: null,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** 预测装配路由路径。 */
|
|
30
|
+
export const PREVIEW_PATH = '/api/plugin-console/preview'
|
|
31
|
+
|
|
32
|
+
/** 快照路由路径。 */
|
|
33
|
+
export const SNAPSHOT_PATH = '/api/plugin-console/snapshot'
|
|
34
|
+
|
|
35
|
+
/** 模板列表路由。 */
|
|
36
|
+
export const TEMPLATES_PATH = '/api/plugin-console/templates'
|
|
37
|
+
|
|
38
|
+
/** 单模板路由(含 revision 列表)。 */
|
|
39
|
+
export const TEMPLATE_PATH = '/api/plugin-console/template'
|
|
40
|
+
|
|
41
|
+
/** 把模板应用到一个会话。 */
|
|
42
|
+
export const APPLY_PATH = '/api/plugin-console/apply'
|
|
43
|
+
|
|
44
|
+
/** 每会话的启用开关:查询与切换。 */
|
|
45
|
+
export const ACTIVATION_PATH = '/api/plugin-console/activation'
|
|
46
|
+
|
|
47
|
+
/** 单会话的实际请求摘要(读 session log,按会话懒取)。 */
|
|
48
|
+
export const SESSION_INFLUENCE_PATH = '/api/plugin-console/session-influence'
|
|
49
|
+
|
|
50
|
+
/** 最近一段时间的工具活动(读 session log,按时间窗与会话数限制)。 */
|
|
51
|
+
export const ACTIVITY_PATH = '/api/plugin-console/activity'
|
|
52
|
+
|
|
53
|
+
/** 控制前的影响预览。 */
|
|
54
|
+
export const IMPACT_PATH = '/api/plugin-console/impact'
|
|
55
|
+
|
|
56
|
+
/** 执行一次控制(全局启停 / 会话掩码)。 */
|
|
57
|
+
export const CONTROL_PATH = '/api/plugin-console/control'
|
|
58
|
+
|
|
59
|
+
/** 最近的控制审计。 */
|
|
60
|
+
export const OPERATIONS_PATH = '/api/plugin-console/operations'
|
|
61
|
+
|
|
62
|
+
/** 清理模板数据:按工作区清绑定,或清空全部并删除存储文件。 */
|
|
63
|
+
export const CLEANUP_PATH = '/api/plugin-console/cleanup'
|
|
64
|
+
|
|
65
|
+
/** 失败 fiber 的诊断读取上限,避免单个挂起的 fiber 拖住整次快照。 */
|
|
66
|
+
export const DIAGNOSTIC_TIMEOUT_MS = 250
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 插件控制:全局启停(委托 pluginManager)与会话工具掩码。
|
|
3
|
+
*
|
|
4
|
+
* 两条边界:
|
|
5
|
+
* - 全局变更只调用 `pluginManager.setPluginEnabled`,不自己写 cordis.patch.yml;
|
|
6
|
+
* - 会话掩码只动工具 deny 名单,不卸载插件、不停服务、不移除监听器。
|
|
7
|
+
*
|
|
8
|
+
* 所有动作都要 `confirmed: true`——调用方必须先看过影响预览。每次动作写一条
|
|
9
|
+
* 审计记录,失败也写,审计写入失败不改变已经发生的动作。
|
|
10
|
+
* @module dsh-plugin-console/host/control
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { readSnapshot } from './snapshot.js'
|
|
14
|
+
import { addMask, removeMask } from './masks.js'
|
|
15
|
+
import { appendOperation } from './templates/repository.js'
|
|
16
|
+
import { ensureTemplateStore } from './templates/store.js'
|
|
17
|
+
|
|
18
|
+
/** 在快照里找目标条目:rowId 优先,其次 entryId。 */
|
|
19
|
+
function findEntry(snapshot, rowId) {
|
|
20
|
+
return (snapshot?.entries ?? []).find((entry) => entry.rowId === rowId)
|
|
21
|
+
?? (snapshot?.entries ?? []).find((entry) => entry.entryId === rowId)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** 一条审计的结果文本。 */
|
|
25
|
+
function resultText(result) {
|
|
26
|
+
if (typeof result === 'string') return result
|
|
27
|
+
try {
|
|
28
|
+
return JSON.stringify(result)
|
|
29
|
+
} catch {
|
|
30
|
+
return String(result)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** 写审计;失败不改变已经发生的控制动作。 */
|
|
35
|
+
async function record(ctx, state, facts) {
|
|
36
|
+
try {
|
|
37
|
+
await ensureTemplateStore(ctx, state)
|
|
38
|
+
return await appendOperation(state, facts)
|
|
39
|
+
} catch (error) {
|
|
40
|
+
ctx.logger?.warn?.('plugin-console: 控制审计写入失败:' + (error instanceof Error ? error.message : String(error)))
|
|
41
|
+
return undefined
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 执行一次控制。
|
|
47
|
+
* @param {object} ctx - host 插件上下文。
|
|
48
|
+
* @param {object} state - 模板存储状态。
|
|
49
|
+
* @param {object} input - `{ action, rowId, sessionId?, confirmed? }`。
|
|
50
|
+
* @returns {Promise<object>} `{ ok, ... }`;拒绝时 `ok:false` 并带原因。
|
|
51
|
+
*/
|
|
52
|
+
export async function controlPlugin(ctx, state, input = {}) {
|
|
53
|
+
const action = typeof input.action === 'string' ? input.action : undefined
|
|
54
|
+
const rowId = typeof input.rowId === 'string' ? input.rowId : undefined
|
|
55
|
+
if (action === undefined) return { ok: false, reason: '缺少 action' }
|
|
56
|
+
if (rowId === undefined) return { ok: false, reason: '缺少 rowId' }
|
|
57
|
+
if (input.confirmed !== true) return { ok: false, reason: '需要先确认影响预览' }
|
|
58
|
+
|
|
59
|
+
const snapshot = await readSnapshot(ctx, {})
|
|
60
|
+
const entry = findEntry(snapshot, rowId)
|
|
61
|
+
if (entry === undefined) return { ok: false, reason: '找不到这个插件条目' }
|
|
62
|
+
|
|
63
|
+
if (action === 'enable' || action === 'disable') {
|
|
64
|
+
if (entry.rowId === 'plugin-console' || entry.moduleName === '@cheemsup/dsh-plugin-console') {
|
|
65
|
+
return { ok: false, reason: '不能在这里关闭插件控制台自身' }
|
|
66
|
+
}
|
|
67
|
+
const pluginManager = ctx.get('pluginManager')
|
|
68
|
+
if (pluginManager === undefined || typeof pluginManager.setPluginEnabled !== 'function') {
|
|
69
|
+
return { ok: false, reason: '这个部署没有 pluginManager,无法做全局启停' }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
let target
|
|
73
|
+
try {
|
|
74
|
+
const listed = await pluginManager.listPlugins()
|
|
75
|
+
target = listed.find((item) => item.entryId === entry.entryId)
|
|
76
|
+
?? listed.find((item) => item.moduleName === entry.moduleName)
|
|
77
|
+
} catch (error) {
|
|
78
|
+
return { ok: false, reason: '读取 pluginManager 列表失败:' + (error instanceof Error ? error.message : String(error)) }
|
|
79
|
+
}
|
|
80
|
+
if (target === undefined) return { ok: false, reason: 'pluginManager 不认识这个条目' }
|
|
81
|
+
if (target.readOnlyReason === 'management-required') {
|
|
82
|
+
return { ok: false, reason: '这是管理插件自身,不能通过 profile patch 启停' }
|
|
83
|
+
}
|
|
84
|
+
if (target.readOnlyReason === 'unaddressable') {
|
|
85
|
+
return { ok: false, reason: '这一行没有唯一可寻址的 profile patch,无法启停' }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const enabled = action === 'enable'
|
|
89
|
+
let result
|
|
90
|
+
try {
|
|
91
|
+
result = await pluginManager.setPluginEnabled(target.entryId, enabled)
|
|
92
|
+
} catch (error) {
|
|
93
|
+
return { ok: false, reason: '启停失败:' + (error instanceof Error ? error.message : String(error)) }
|
|
94
|
+
}
|
|
95
|
+
const operation = await record(ctx, state, {
|
|
96
|
+
action,
|
|
97
|
+
scope: 'profile',
|
|
98
|
+
target: target.entryId,
|
|
99
|
+
before: { enabled: entry.enabled === true },
|
|
100
|
+
after: { enabled },
|
|
101
|
+
result: resultText(result ?? 'ok'),
|
|
102
|
+
})
|
|
103
|
+
return {
|
|
104
|
+
ok: true,
|
|
105
|
+
action,
|
|
106
|
+
entryId: target.entryId,
|
|
107
|
+
enabled,
|
|
108
|
+
result,
|
|
109
|
+
restartRequired: result === 'restart-required' || result?.restartRequired === true,
|
|
110
|
+
operation,
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (action === 'mask' || action === 'unmask') {
|
|
115
|
+
const sessionId = typeof input.sessionId === 'string' ? input.sessionId : undefined
|
|
116
|
+
if (sessionId === undefined) return { ok: false, reason: '会话掩码需要 sessionId' }
|
|
117
|
+
|
|
118
|
+
if (action === 'unmask') {
|
|
119
|
+
const { result } = await removeMask(ctx, state, { sessionId, rowId: entry.rowId })
|
|
120
|
+
const operation = await record(ctx, state, {
|
|
121
|
+
action,
|
|
122
|
+
scope: 'session',
|
|
123
|
+
target: entry.rowId,
|
|
124
|
+
before: { sessionId },
|
|
125
|
+
after: { sessionId },
|
|
126
|
+
result: result?.applied === true ? 'applied' : 'recorded',
|
|
127
|
+
})
|
|
128
|
+
return { ok: true, action, sessionId, rowId: entry.rowId, result, operation }
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const tools = entry.registrations?.tools ?? []
|
|
132
|
+
if (tools.length === 0) {
|
|
133
|
+
return { ok: false, reason: '这个插件没有工具型贡献,暂不支持会话掩码' }
|
|
134
|
+
}
|
|
135
|
+
const shared = tools.filter((name) => {
|
|
136
|
+
const tool = (snapshot.tools?.attributed ?? []).find((candidate) => candidate.name === name)
|
|
137
|
+
return (tool?.owners ?? []).length > 1
|
|
138
|
+
})
|
|
139
|
+
if (shared.length > 0) {
|
|
140
|
+
return {
|
|
141
|
+
ok: false,
|
|
142
|
+
reason: '这些工具与其他插件重名,按名字 deny 会连带隐藏对方的同名工具:' + shared.join('、'),
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const { record: mask, result } = await addMask(ctx, state, { sessionId, rowId: entry.rowId, tools })
|
|
147
|
+
const operation = await record(ctx, state, {
|
|
148
|
+
action,
|
|
149
|
+
scope: 'session',
|
|
150
|
+
target: entry.rowId,
|
|
151
|
+
before: { sessionId, visibleTools: tools },
|
|
152
|
+
after: { sessionId, maskedTools: tools },
|
|
153
|
+
result: result?.applied === true ? 'applied' : 'recorded',
|
|
154
|
+
})
|
|
155
|
+
return { ok: true, action, sessionId, rowId: entry.rowId, mask, result, operation }
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return { ok: false, reason: '未知动作:' + action }
|
|
159
|
+
}
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 逐条采集 Loader 行。
|
|
3
|
+
*
|
|
4
|
+
* 这是快照里最重的一段采集:一行的包身份、启用状态、fiber 阶段、服务供需、
|
|
5
|
+
* 副作用、源码注册名、类别都在这里定下来。它只依赖前面几个纯读取模块,
|
|
6
|
+
* 不依赖进程内的任何缓存,所以同一份 ctx 反复调用得到同一份结果。
|
|
7
|
+
* @module dsh-plugin-console/host/entries
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { categorize } from './categorize.js'
|
|
11
|
+
import { FIBER_PHASE } from './constants.js'
|
|
12
|
+
import { readDiagnostic, readEffects } from './fiber.js'
|
|
13
|
+
import { barePackageName, fallbackLayer } from './names.js'
|
|
14
|
+
import { scanPackageRegistrations } from './source-scan.js'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 覆盖者的展示名。
|
|
18
|
+
*
|
|
19
|
+
* bundle 只给包名——`pluginManager.listBundles()` 的 `overrides` 只暴露 id,
|
|
20
|
+
* 不提供改了哪些键;用户层则由扫描器记到层与键,一并显示出来。
|
|
21
|
+
* @param {{layer: string, index?: number, keys: string[]}} info - 一条用户层归因。
|
|
22
|
+
* @returns {string} 用于「覆盖」一栏的文本。
|
|
23
|
+
*/
|
|
24
|
+
function overrideLabel(info) {
|
|
25
|
+
const layer = info.layer === 'overlay'
|
|
26
|
+
? (typeof info.index === 'number' ? `启动覆盖层 ${info.index + 1}` : '启动覆盖层')
|
|
27
|
+
: `profile patch(${info.layer})`
|
|
28
|
+
// 键放进花括号而不是再接一个冒号:外层已经是「覆盖来源:…」,再套冒号会读成两级。
|
|
29
|
+
return info.keys.length > 0 ? `${layer}{${info.keys.join('、')}}` : layer
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param {object} ctx - host 插件上下文。
|
|
34
|
+
* @param {object} packages - `pluginPackages` 服务(可缺)。
|
|
35
|
+
* @param {Map<string, {bundle: string, version?: string}>} rowOwner - 行 id → 首个插入它的 bundle。
|
|
36
|
+
* @param {Map<string, string[]>} rowOverriders - 行 id → 覆盖它的 bundle 列表。
|
|
37
|
+
* @param {Set<string>} userInserted - 用户 patch 插入的行 id。
|
|
38
|
+
* @param {Map<string, Array<{layer: string, index?: number, keys: string[]}>>} userOverrides - 行 id → 用户层覆盖记录。
|
|
39
|
+
* @returns {Promise<{entries: Array<object>, services: object, stats: object}>} 条目、服务供需与统计。
|
|
40
|
+
*/
|
|
41
|
+
export async function collectEntries(ctx, packages, rowOwner, rowOverriders, userInserted, userOverrides) {
|
|
42
|
+
/** @type {Array<object>} */
|
|
43
|
+
const entries = []
|
|
44
|
+
/** @type {Map<string, string[]>} 服务名 → 提供它的运行时条目 id */
|
|
45
|
+
const providers = new Map()
|
|
46
|
+
/** @type {Map<string, string[]>} 服务名 → 需要它的运行时条目 id */
|
|
47
|
+
const consumers = new Map()
|
|
48
|
+
|
|
49
|
+
let loadIndex = 0
|
|
50
|
+
for (const entry of ctx.loader.entries()) {
|
|
51
|
+
const options = entry.options ?? {}
|
|
52
|
+
const fullId = entry.id
|
|
53
|
+
const rowId = fullId.startsWith('include:') ? fullId.slice('include:'.length) : fullId
|
|
54
|
+
const base = entry.parent?.tree?.ctx?.baseUrl
|
|
55
|
+
const moduleName = options.name
|
|
56
|
+
|
|
57
|
+
// 包身份与显示元信息;解析失败不影响行本身
|
|
58
|
+
let pkg
|
|
59
|
+
let meta
|
|
60
|
+
try {
|
|
61
|
+
pkg = base === undefined || !packages ? undefined : packages.packageOf(moduleName, base)
|
|
62
|
+
} catch {
|
|
63
|
+
pkg = undefined
|
|
64
|
+
}
|
|
65
|
+
try {
|
|
66
|
+
meta = base === undefined || !packages ? undefined : packages.metaOf(moduleName, base)
|
|
67
|
+
} catch {
|
|
68
|
+
meta = undefined
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const manifest = pkg?.manifest
|
|
72
|
+
const clientDecl = manifest?.dsh?.client
|
|
73
|
+
const face = clientDecl?.platform === 'web' ? 'both' : 'host'
|
|
74
|
+
|
|
75
|
+
// 有效启用状态:自身表达式 + 祖先 group 继承
|
|
76
|
+
let ownDisabled = false
|
|
77
|
+
let disabledExpr
|
|
78
|
+
const rawDisabled = options.disabled
|
|
79
|
+
if (rawDisabled && typeof rawDisabled === 'object' && '__jsExpr' in rawDisabled) {
|
|
80
|
+
disabledExpr = rawDisabled.__jsExpr
|
|
81
|
+
try {
|
|
82
|
+
ownDisabled = Boolean(entry.evaluate?.(disabledExpr))
|
|
83
|
+
} catch {
|
|
84
|
+
ownDisabled = false
|
|
85
|
+
}
|
|
86
|
+
} else {
|
|
87
|
+
ownDisabled = Boolean(rawDisabled)
|
|
88
|
+
}
|
|
89
|
+
let effectiveDisabled
|
|
90
|
+
try {
|
|
91
|
+
effectiveDisabled = Boolean(entry.disabled)
|
|
92
|
+
} catch {
|
|
93
|
+
effectiveDisabled = ownDisabled
|
|
94
|
+
}
|
|
95
|
+
const inheritedDisabled = effectiveDisabled && !ownDisabled
|
|
96
|
+
|
|
97
|
+
const fiber = entry.fiber
|
|
98
|
+
const phase = fiber === undefined ? null : (FIBER_PHASE[fiber.state] ?? null)
|
|
99
|
+
const diagnostic = phase === 'failed' ? await readDiagnostic(fiber) : undefined
|
|
100
|
+
|
|
101
|
+
const requires = fiber ? Object.keys(fiber.inject ?? {}) : []
|
|
102
|
+
const provides = fiber
|
|
103
|
+
? [...(fiber.provisions ?? [])].map((provision) => provision?.name).filter((name) => typeof name === 'string')
|
|
104
|
+
: []
|
|
105
|
+
const effects = readEffects(fiber)
|
|
106
|
+
const registrations = await scanPackageRegistrations(pkg?.dir)
|
|
107
|
+
|
|
108
|
+
// MCP 客户端的 serverName 决定了它注册的工具名前缀 mcp__<server>__*,
|
|
109
|
+
// 读配置就能把工具归属回这一行。
|
|
110
|
+
const mcpServerName = options.config && typeof options.config === 'object'
|
|
111
|
+
? (typeof options.config.serverName === 'string' ? options.config.serverName : undefined)
|
|
112
|
+
: undefined
|
|
113
|
+
|
|
114
|
+
const configEntries = readConfigEntries(options.config)
|
|
115
|
+
|
|
116
|
+
const segments = fullId.split(':')
|
|
117
|
+
const depth = segments.length - 1
|
|
118
|
+
const entryRow = {
|
|
119
|
+
entryId: fullId,
|
|
120
|
+
rowId,
|
|
121
|
+
/** Loader 里的声明顺序。DSH 没有插件级 order,这就是组合文件的加载链顺序。 */
|
|
122
|
+
loadIndex,
|
|
123
|
+
parentEntryId: segments.length > 1 ? segments.slice(0, -1).join(':') : null,
|
|
124
|
+
depth,
|
|
125
|
+
moduleName,
|
|
126
|
+
packageName: barePackageName(moduleName) ?? null,
|
|
127
|
+
version: typeof pkg?.version === 'string' ? pkg.version : null,
|
|
128
|
+
packageDir: typeof pkg?.dir === 'string' ? pkg.dir : null,
|
|
129
|
+
title: typeof meta?.title === 'string' ? meta.title : undefined,
|
|
130
|
+
description: typeof meta?.description === 'string' ? meta.description : undefined,
|
|
131
|
+
metaError: typeof meta?.error === 'string' ? meta.error : undefined,
|
|
132
|
+
layer: rowOwner.get(rowId)?.bundle ?? fallbackLayer(moduleName, depth),
|
|
133
|
+
layerVersion: rowOwner.get(rowId)?.version ?? null,
|
|
134
|
+
overriddenBy: [
|
|
135
|
+
...(rowOverriders.get(rowId) ?? []),
|
|
136
|
+
...(userOverrides.get(rowId) ?? []).map(overrideLabel),
|
|
137
|
+
],
|
|
138
|
+
userInserted: userInserted.has(rowId),
|
|
139
|
+
face,
|
|
140
|
+
enabled: !effectiveDisabled,
|
|
141
|
+
disabledExpr,
|
|
142
|
+
disabledValue: disabledExpr === undefined ? undefined : ownDisabled,
|
|
143
|
+
inheritedDisabled,
|
|
144
|
+
phase,
|
|
145
|
+
diagnostic,
|
|
146
|
+
requires,
|
|
147
|
+
provides,
|
|
148
|
+
effects,
|
|
149
|
+
registrations,
|
|
150
|
+
category: categorize(moduleName, registrations),
|
|
151
|
+
mcpServerName,
|
|
152
|
+
clientInject: Array.isArray(clientDecl?.inject) ? clientDecl.inject : [],
|
|
153
|
+
clientExternal: Array.isArray(clientDecl?.external) ? clientDecl.external : [],
|
|
154
|
+
configKeys: configEntries.map((row) => row.key),
|
|
155
|
+
configEntries,
|
|
156
|
+
isGroup: Boolean(options.group),
|
|
157
|
+
}
|
|
158
|
+
entries.push(entryRow)
|
|
159
|
+
loadIndex++
|
|
160
|
+
|
|
161
|
+
for (const name of provides) {
|
|
162
|
+
const list = providers.get(name) ?? []
|
|
163
|
+
list.push(fullId)
|
|
164
|
+
providers.set(name, list)
|
|
165
|
+
}
|
|
166
|
+
for (const name of requires) {
|
|
167
|
+
const list = consumers.get(name) ?? []
|
|
168
|
+
list.push(fullId)
|
|
169
|
+
consumers.set(name, list)
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// —— 汇总:服务名 → 提供者 / 消费者 ——
|
|
174
|
+
const services = {}
|
|
175
|
+
for (const name of new Set([...providers.keys(), ...consumers.keys()])) {
|
|
176
|
+
services[name] = {
|
|
177
|
+
providers: providers.get(name) ?? [],
|
|
178
|
+
consumers: consumers.get(name) ?? [],
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const pluginEntries = entries.filter((entry) => !entry.moduleName.startsWith('cordis:'))
|
|
183
|
+
const stats = {
|
|
184
|
+
entries: entries.length,
|
|
185
|
+
plugins: pluginEntries.length,
|
|
186
|
+
active: pluginEntries.filter((entry) => entry.phase === 'active').length,
|
|
187
|
+
pending: pluginEntries.filter((entry) => entry.phase === 'pending').length,
|
|
188
|
+
failed: pluginEntries.filter((entry) => entry.phase === 'failed').length,
|
|
189
|
+
disabled: pluginEntries.filter((entry) => !entry.enabled).length,
|
|
190
|
+
dual: pluginEntries.filter((entry) => entry.face === 'both').length,
|
|
191
|
+
thirdParty: pluginEntries.filter((entry) => entry.layer === 'profile' || entry.layer === 'runtime').length,
|
|
192
|
+
services: Object.keys(services).length,
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return { entries, services, stats }
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* 单个配置值在快照里的展示上限。
|
|
200
|
+
*
|
|
201
|
+
* 配置里可能有整份工具 schema,不能无限塞进快照;凭据类字段都很短,
|
|
202
|
+
* 这个上限只截断异常大的对象,并在值末尾写明原始长度。
|
|
203
|
+
*/
|
|
204
|
+
const CONFIG_VALUE_LIMIT = 2000
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* 把任意配置值转成可 JSON 序列化的文本。
|
|
208
|
+
*
|
|
209
|
+
* 配置由插件作者书写,可能带函数、BigInt、循环引用;直接放进快照会让
|
|
210
|
+
* 整份响应序列化失败,所以逐值归一,读不出来就退化成 `String(value)`。
|
|
211
|
+
* @param {unknown} value - `options.config` 里的一个值。
|
|
212
|
+
* @returns {string} 展示文本。
|
|
213
|
+
*/
|
|
214
|
+
function configText(value) {
|
|
215
|
+
if (typeof value === 'string') return value
|
|
216
|
+
if (value === undefined) return 'undefined'
|
|
217
|
+
if (value === null) return 'null'
|
|
218
|
+
if (typeof value === 'function') return `[function ${value.name || 'anonymous'}]`
|
|
219
|
+
if (typeof value === 'symbol') return String(value)
|
|
220
|
+
if (typeof value === 'bigint') return `${value}n`
|
|
221
|
+
try {
|
|
222
|
+
const text = JSON.stringify(value, replaceNonJson)
|
|
223
|
+
return text === undefined ? String(value) : text
|
|
224
|
+
} catch {
|
|
225
|
+
// 循环引用:正常对象走上面的快路径,只有真的序列化失败才做逐层检测。
|
|
226
|
+
return configTextCircular(value)
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** JSON 不认识的配置值:函数、BigInt、Symbol 转成可读文本。 */
|
|
231
|
+
function replaceNonJson(key, item) {
|
|
232
|
+
if (typeof item === 'bigint') return `${item}n`
|
|
233
|
+
if (typeof item === 'function') return `[function ${item.name || 'anonymous'}]`
|
|
234
|
+
if (typeof item === 'symbol') return String(item)
|
|
235
|
+
return item
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** 循环引用的退路:重复出现的对象标成 `[circular]`,其余照常序列化。 */
|
|
239
|
+
function configTextCircular(value) {
|
|
240
|
+
const seen = new WeakSet()
|
|
241
|
+
try {
|
|
242
|
+
return JSON.stringify(value, (key, item) => {
|
|
243
|
+
const replaced = replaceNonJson(key, item)
|
|
244
|
+
if (typeof replaced === 'object' && replaced !== null) {
|
|
245
|
+
if (seen.has(replaced)) return '[circular]'
|
|
246
|
+
seen.add(replaced)
|
|
247
|
+
}
|
|
248
|
+
return replaced
|
|
249
|
+
})
|
|
250
|
+
} catch {
|
|
251
|
+
return String(value)
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* 把 `options.config` 归一成「字段名 + 生效值」。
|
|
257
|
+
*
|
|
258
|
+
* 快照会原样带上这些值(包括凭据),因为「配置字段有哪些」不足以判断
|
|
259
|
+
* 插件为什么这样行为;值只经由本地面板展示,不写日志、不落盘。
|
|
260
|
+
* @param {unknown} config - Loader 行上的配置对象。
|
|
261
|
+
* @returns {Array<{key: string, value: string}>} 键值对;非对象配置返回空数组。
|
|
262
|
+
*/
|
|
263
|
+
function readConfigEntries(config) {
|
|
264
|
+
if (config === null || typeof config !== 'object' || Array.isArray(config)) return []
|
|
265
|
+
return Object.keys(config).map((key) => {
|
|
266
|
+
const text = configText(config[key])
|
|
267
|
+
return {
|
|
268
|
+
key,
|
|
269
|
+
value: text.length > CONFIG_VALUE_LIMIT
|
|
270
|
+
? `${text.slice(0, CONFIG_VALUE_LIMIT)}…(已截断,共 ${text.length} 字符)`
|
|
271
|
+
: text,
|
|
272
|
+
}
|
|
273
|
+
})
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* 条目按模块名索引:preset 行靠它连回全局条目。
|
|
278
|
+
* @param {Array<object>} entries - 已采集条目。
|
|
279
|
+
* @returns {Map<string, Array<object>>} 模块名 → 行摘要列表。
|
|
280
|
+
*/
|
|
281
|
+
export function indexRowsByModule(entries) {
|
|
282
|
+
const rowsByModule = new Map()
|
|
283
|
+
for (const entry of entries) {
|
|
284
|
+
const list = rowsByModule.get(entry.moduleName) ?? []
|
|
285
|
+
list.push({ rowId: entry.rowId, layer: entry.layer, phase: entry.phase, enabled: entry.enabled })
|
|
286
|
+
rowsByModule.set(entry.moduleName, list)
|
|
287
|
+
}
|
|
288
|
+
return rowsByModule
|
|
289
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* fiber 层的读取:失败诊断、副作用标签树、agent scope 标识。
|
|
3
|
+
*
|
|
4
|
+
* 这三件事都只依赖 Cordis fiber / ctx 的公开面,不依赖任何 registry,
|
|
5
|
+
* 因此单独成一层,供快照与物化共用。
|
|
6
|
+
* @module dsh-plugin-console/host/fiber
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { DIAGNOSTIC_TIMEOUT_MS } from './constants.js'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 读取失败 fiber 的启动诊断,带超时。
|
|
13
|
+
* @param {object} fiber - Cordis fiber。
|
|
14
|
+
* @returns {Promise<string | undefined>} 诊断文本。
|
|
15
|
+
*/
|
|
16
|
+
export async function readDiagnostic(fiber) {
|
|
17
|
+
if (!fiber || typeof fiber.await !== 'function') return undefined
|
|
18
|
+
try {
|
|
19
|
+
await Promise.race([
|
|
20
|
+
fiber.await().catch((error) => {
|
|
21
|
+
throw error
|
|
22
|
+
}),
|
|
23
|
+
new Promise((_resolve, reject) => {
|
|
24
|
+
const timer = setTimeout(() => reject(new Error('__timeout__')), DIAGNOSTIC_TIMEOUT_MS)
|
|
25
|
+
// 定时器不阻止进程退出
|
|
26
|
+
if (typeof timer.unref === 'function') timer.unref()
|
|
27
|
+
}),
|
|
28
|
+
])
|
|
29
|
+
return undefined
|
|
30
|
+
} catch (error) {
|
|
31
|
+
if (error instanceof Error && error.message === '__timeout__') return '诊断读取超时'
|
|
32
|
+
return error instanceof Error ? error.message : String(error)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 把一个 fiber 已注册的副作用整理成「它做了什么」的清单。
|
|
38
|
+
*
|
|
39
|
+
* Cordis 给每个 `ctx.effect()` 生成一个带 label 的元数据节点,`ctx.on`、`tools.register`、
|
|
40
|
+
* `ctx.provide` 这些公开 API 都会自己起一个有意义的 label,因此这份清单是框架给的,
|
|
41
|
+
* 不需要猜。子级 effect 会被压平成 `父 → 子` 的形式,便于前端直接展示。
|
|
42
|
+
* @param {object | undefined} fiber - Cordis fiber。
|
|
43
|
+
* @returns {Array<{label: string, children: Array<string>}>} 顶层 effect 及其压平后的子标签。
|
|
44
|
+
*/
|
|
45
|
+
export function readEffects(fiber) {
|
|
46
|
+
if (fiber === undefined || typeof fiber.getEffects !== 'function') return []
|
|
47
|
+
let metas
|
|
48
|
+
try {
|
|
49
|
+
metas = fiber.getEffects()
|
|
50
|
+
} catch {
|
|
51
|
+
return []
|
|
52
|
+
}
|
|
53
|
+
if (!Array.isArray(metas)) return []
|
|
54
|
+
|
|
55
|
+
/** 递归收集一个元数据节点下的全部子标签。 */
|
|
56
|
+
const flatten = (meta) => {
|
|
57
|
+
const out = []
|
|
58
|
+
for (const child of meta?.children ?? []) {
|
|
59
|
+
const label = typeof child?.label === 'string' ? child.label : undefined
|
|
60
|
+
if (label !== undefined && label !== 'anonymous') out.push(label)
|
|
61
|
+
out.push(...flatten(child))
|
|
62
|
+
}
|
|
63
|
+
return out
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return metas
|
|
67
|
+
.map((meta) => (typeof meta?.label === 'string' ? { label: meta.label, children: flatten(meta) } : undefined))
|
|
68
|
+
.filter((effect) => effect !== undefined)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 从 Agent 上下文里取出它的 scope 标识。
|
|
73
|
+
*
|
|
74
|
+
* `tools.view(scope)` 的 `scope` 参数是 `createScope` 写下的那个 symbol 的**值**,
|
|
75
|
+
* 不是 ctx 本身;直接把 ctx 传进去只会落到全局层。dsh-scope 没有把符号注册到
|
|
76
|
+
* `Symbol.for`,只能按描述找——这是唯一可用的公开路径。
|
|
77
|
+
* @param {object} ctx - 可能带 scope 标签的上下文。
|
|
78
|
+
* @returns {symbol | undefined} scope 标识。
|
|
79
|
+
*/
|
|
80
|
+
export function scopeKeyOf(ctx) {
|
|
81
|
+
if (ctx === undefined || ctx === null) return undefined
|
|
82
|
+
for (const symbol of Object.getOwnPropertySymbols(ctx)) {
|
|
83
|
+
if (symbol.description === 'dsh.scope') return ctx[symbol]
|
|
84
|
+
}
|
|
85
|
+
return undefined
|
|
86
|
+
}
|