@cheemsup/dsh-plugin-console 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 +23 -0
- package/README.md +95 -3
- package/cordis.patch.yml +5 -0
- package/icon.svg +203 -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 +247 -0
- package/lib/client.dialog.js +77 -0
- package/lib/client.disable-preview.js +904 -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.impact-tree.js +487 -0
- package/lib/client.influence.js +361 -0
- package/lib/client.js +246 -0
- package/lib/client.page.js +646 -0
- package/lib/client.plugin-filters.js +62 -0
- package/lib/client.plugins.js +506 -0
- package/lib/client.preview-messages.js +506 -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 +448 -0
- package/lib/client.relation-graph.js +364 -0
- package/lib/client.schema.js +327 -0
- package/lib/client.search.js +409 -0
- package/lib/client.session-detail.js +503 -0
- package/lib/client.sessions.js +495 -0
- package/lib/client.shared.js +562 -0
- package/lib/client.store.js +452 -0
- package/lib/client.styles.js +589 -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 +341 -0
- package/lib/host/catalog.js +133 -0
- package/lib/host/categorize.js +40 -0
- package/lib/host/config-layers.js +589 -0
- package/lib/host/constants.js +69 -0
- package/lib/host/control.js +159 -0
- package/lib/host/disable-preview.js +651 -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 +138 -0
- package/lib/host/injection-owners.js +60 -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-config.js +554 -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/disable-preview.js +19 -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 +467 -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 +106 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +124 -6
package/lib/host/http.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 路由层的公共 HTTP 小工具。
|
|
3
|
+
*
|
|
4
|
+
* 每个路由只做三件事:解析参数 → 调一个服务函数 → 序列化结果。
|
|
5
|
+
* 三件里的第一件和第三件在这里,第二件在各自的模块里。
|
|
6
|
+
* @module dsh-plugin-console/host/http
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** 成功响应:200,JSON,不缓存。 */
|
|
10
|
+
export function jsonOk(body) {
|
|
11
|
+
return new Response(JSON.stringify(body), {
|
|
12
|
+
status: 200,
|
|
13
|
+
headers: {
|
|
14
|
+
'content-type': 'application/json; charset=utf-8',
|
|
15
|
+
'cache-control': 'no-store',
|
|
16
|
+
},
|
|
17
|
+
})
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** 客户端错误:4xx,带一句可读的原因。 */
|
|
21
|
+
export function jsonError(status, message) {
|
|
22
|
+
return new Response(JSON.stringify({ error: message }), {
|
|
23
|
+
status,
|
|
24
|
+
headers: { 'content-type': 'application/json; charset=utf-8' },
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 把一次路由执行包起来:任何抛出都变成 500 + `{error}`。
|
|
30
|
+
*
|
|
31
|
+
* 路由里不写 try/catch 的理由:这段是唯一需要它的地方,重复六遍只会淹没业务。
|
|
32
|
+
* @param {Function} handler - 真正的实现。
|
|
33
|
+
* @returns {Function} fetch 处理函数。
|
|
34
|
+
*/
|
|
35
|
+
export function jsonRoute(handler) {
|
|
36
|
+
return async (request) => {
|
|
37
|
+
try {
|
|
38
|
+
return await handler(request)
|
|
39
|
+
} catch (error) {
|
|
40
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
41
|
+
return jsonError(500, message)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 读 JSON 请求体;空体或坏 JSON 一律当 `{}`。
|
|
48
|
+
* @param {Request} request - 请求。
|
|
49
|
+
* @returns {Promise<any>} 请求体。
|
|
50
|
+
*/
|
|
51
|
+
export async function readJsonBody(request) {
|
|
52
|
+
const body = await request?.json?.().catch(() => ({}))
|
|
53
|
+
return body ?? {}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 读 Query 参数。
|
|
58
|
+
* @param {Request} request - 请求。
|
|
59
|
+
* @param {string} name - 参数名。
|
|
60
|
+
* @returns {string | undefined} 参数值。
|
|
61
|
+
*/
|
|
62
|
+
export function searchParam(request, name) {
|
|
63
|
+
const value = new URL(request.url).searchParams.get(name)
|
|
64
|
+
return typeof value === 'string' ? value : undefined
|
|
65
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 影响预览:禁用/掩码一个插件会发生什么。
|
|
3
|
+
*
|
|
4
|
+
* 全部基于快照里已有的事实:工具与提示词段的归属、服务供需、模式行、活会话
|
|
5
|
+
* 的运行时工具集。不调用任何写操作,也不读 session log。
|
|
6
|
+
* @module dsh-plugin-console/host/impact
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { expandRegistrationName } from './attribution.js'
|
|
10
|
+
import { readSnapshot } from './snapshot.js'
|
|
11
|
+
|
|
12
|
+
/** 取一个条目的展示名。 */
|
|
13
|
+
function labelOf(entry) {
|
|
14
|
+
return entry?.title || entry?.packageName || entry?.moduleName || entry?.entryId || '未知插件'
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** 从一个名字的所有者里判断是否会真的消失。 */
|
|
18
|
+
function ownership(owners) {
|
|
19
|
+
return {
|
|
20
|
+
owners,
|
|
21
|
+
shared: owners.length > 1,
|
|
22
|
+
lost: owners.length <= 1,
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 计算一个目标条目的影响面。
|
|
28
|
+
* @param {object} snapshot - 完整快照。
|
|
29
|
+
* @param {string} rowId - 目标行 id 或运行时 id。
|
|
30
|
+
* @returns {object} 影响面;找不到时 `{found:false}`。
|
|
31
|
+
*/
|
|
32
|
+
export function computeImpact(snapshot, rowId) {
|
|
33
|
+
const entries = Array.isArray(snapshot?.entries) ? snapshot.entries : []
|
|
34
|
+
const entry = entries.find((candidate) => candidate.rowId === rowId)
|
|
35
|
+
?? entries.find((candidate) => candidate.entryId === rowId)
|
|
36
|
+
if (entry === undefined) return { found: false, reason: '找不到这个插件条目' }
|
|
37
|
+
|
|
38
|
+
const toolRows = []
|
|
39
|
+
for (const name of entry.registrations?.tools ?? []) {
|
|
40
|
+
const tool = (snapshot?.tools?.attributed ?? []).find((candidate) => candidate.name === name)
|
|
41
|
+
const owners = tool?.owners ?? []
|
|
42
|
+
toolRows.push({ name, ...ownership(owners) })
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const promptRows = []
|
|
46
|
+
const promptByName = new Map(
|
|
47
|
+
[...(snapshot?.prompt?.sections ?? []), ...(snapshot?.prompt?.contexts ?? [])]
|
|
48
|
+
.map((row) => [row.name, row]),
|
|
49
|
+
)
|
|
50
|
+
for (const name of [...(entry.registrations?.sections ?? []), ...(entry.registrations?.contexts ?? [])]) {
|
|
51
|
+
for (const actual of expandRegistrationName(name, entry, promptByName)) {
|
|
52
|
+
const row = promptByName.get(actual)
|
|
53
|
+
const owners = row?.owners ?? []
|
|
54
|
+
promptRows.push({ name: actual, ...ownership(owners) })
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const services = (entry.provides ?? []).map((name) => {
|
|
59
|
+
const record = snapshot?.services?.[name] ?? {}
|
|
60
|
+
const providers = record.providers ?? []
|
|
61
|
+
const consumers = record.consumers ?? []
|
|
62
|
+
return {
|
|
63
|
+
name,
|
|
64
|
+
providers,
|
|
65
|
+
consumers,
|
|
66
|
+
soleProvider: providers.length <= 1,
|
|
67
|
+
affectedConsumers: providers.length <= 1 ? consumers : [],
|
|
68
|
+
}
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
const modes = (snapshot?.modes ?? [])
|
|
72
|
+
.filter((mode) => (mode.rows ?? []).some((row) => row.entryId === entry.rowId || row.moduleName === entry.moduleName))
|
|
73
|
+
.map((mode) => ({ id: mode.id, name: mode.name ?? mode.id }))
|
|
74
|
+
const modeIds = new Set(modes.map((mode) => mode.id))
|
|
75
|
+
|
|
76
|
+
const liveSessions = (snapshot?.tools?.liveAgents ?? [])
|
|
77
|
+
.filter((agent) => modeIds.has(agent.preset) || (agent.tools ?? []).some((name) => (entry.registrations?.tools ?? []).includes(name)))
|
|
78
|
+
.map((agent) => ({ sessionId: agent.sessionId, preset: agent.preset }))
|
|
79
|
+
|
|
80
|
+
const allSessions = [
|
|
81
|
+
...(snapshot?.workspaces ?? []).flatMap((workspace) => workspace.sessions ?? []),
|
|
82
|
+
...(snapshot?.ungroupedSessions ?? []),
|
|
83
|
+
]
|
|
84
|
+
const seenSessions = new Set()
|
|
85
|
+
let coldSessionCount = 0
|
|
86
|
+
for (const session of allSessions) {
|
|
87
|
+
if (typeof session?.id !== 'string' || seenSessions.has(session.id)) continue
|
|
88
|
+
seenSessions.add(session.id)
|
|
89
|
+
if (session.live === true) continue
|
|
90
|
+
if (modeIds.has(session.preset)) coldSessionCount++
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
found: true,
|
|
95
|
+
rowId: entry.rowId,
|
|
96
|
+
entryId: entry.entryId,
|
|
97
|
+
moduleName: entry.moduleName,
|
|
98
|
+
title: labelOf(entry),
|
|
99
|
+
enabled: entry.enabled === true,
|
|
100
|
+
phase: entry.phase ?? null,
|
|
101
|
+
tools: {
|
|
102
|
+
owned: toolRows.map((row) => row.name),
|
|
103
|
+
lost: toolRows.filter((row) => row.lost).map((row) => row.name),
|
|
104
|
+
shared: toolRows.filter((row) => row.shared).map((row) => row.name),
|
|
105
|
+
rows: toolRows,
|
|
106
|
+
},
|
|
107
|
+
sections: {
|
|
108
|
+
owned: promptRows.map((row) => row.name),
|
|
109
|
+
lost: promptRows.filter((row) => row.lost).map((row) => row.name),
|
|
110
|
+
shared: promptRows.filter((row) => row.shared).map((row) => row.name),
|
|
111
|
+
rows: promptRows,
|
|
112
|
+
},
|
|
113
|
+
services: {
|
|
114
|
+
rows: services,
|
|
115
|
+
soleProviders: services.filter((service) => service.soleProvider).map((service) => service.name),
|
|
116
|
+
affectedConsumers: [...new Set(services.flatMap((service) => service.affectedConsumers))],
|
|
117
|
+
},
|
|
118
|
+
modes,
|
|
119
|
+
liveSessions,
|
|
120
|
+
coldSessionCount,
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* 读取一个目标的影响预览。
|
|
126
|
+
* @param {object} ctx - host 插件上下文。
|
|
127
|
+
* @param {string} rowId - 目标行 id/运行时 id。
|
|
128
|
+
* @returns {Promise<object>} 影响面 + 快照时间与 HMR 提示。
|
|
129
|
+
*/
|
|
130
|
+
export async function readImpact(ctx, rowId) {
|
|
131
|
+
const snapshot = await readSnapshot(ctx, {})
|
|
132
|
+
return {
|
|
133
|
+
...computeImpact(snapshot, rowId),
|
|
134
|
+
generatedAt: snapshot.generatedAt,
|
|
135
|
+
// HMR 是否可用决定全局变更是否需要重启;未知时返回 false,界面按「可能需要重启」写。
|
|
136
|
+
hmr: ctx.get('hmr') !== undefined,
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 会话日志里 `user/message.source.kind` → 产出它的包。
|
|
3
|
+
*
|
|
4
|
+
* 这是**推断**,不是注册表事实:宿主没有把「这条消息由谁注入」登记在任何
|
|
5
|
+
* 公开接口上,只能按已知的来源类型映射回包名。禁用预演用它回答「哪些运行时
|
|
6
|
+
* 消息可能不再注入」,因此调用方必须把结果标成「推断」。
|
|
7
|
+
*
|
|
8
|
+
* 抽成独立模块而不是留在 `session-influence.js`:那边会拖进整个会话日志读取
|
|
9
|
+
* 链路,而禁用预演只需要这张静态表。
|
|
10
|
+
* @module dsh-plugin-console/host/injection-owners
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** source.kind → 产出它的包。没有映射的 kind 留空,由调用方标「未知」。 */
|
|
14
|
+
export const MESSAGE_INJECTION_OWNERS = {
|
|
15
|
+
'runtime-context': ['@deepseek-ai/dsh-agent-loop'],
|
|
16
|
+
'time-context': ['@deepseek-ai/dsh-time-context'],
|
|
17
|
+
'skill-catalog': ['@deepseek-ai/dsh-tool-skill'],
|
|
18
|
+
'agent-message': ['@deepseek-ai/dsh-subagent'],
|
|
19
|
+
'subagent-settled': ['@deepseek-ai/dsh-subagent'],
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** source.kind 的中文说明;界面只展示这个说法。 */
|
|
23
|
+
export const MESSAGE_INJECTION_LABELS = {
|
|
24
|
+
'runtime-context': '运行时上下文',
|
|
25
|
+
'time-context': '时间上下文',
|
|
26
|
+
'skill-catalog': 'skill 目录',
|
|
27
|
+
'agent-message': '子 Agent 消息',
|
|
28
|
+
'subagent-settled': '子 Agent 结束消息',
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** 去掉 scope 与子路径,得到裸包名。 */
|
|
32
|
+
function bareName(name) {
|
|
33
|
+
if (typeof name !== 'string') return ''
|
|
34
|
+
const parts = name.split('/')
|
|
35
|
+
return name.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0]
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 一个条目可能负责的注入类型。
|
|
40
|
+
*
|
|
41
|
+
* 同时用包名与模块名匹配:Loader 行给的是模块说明符,包名是它的裸名;
|
|
42
|
+
* 子路径模块(`@scope/pkg/lib/x`)也归到同一包。
|
|
43
|
+
* @param {string | null | undefined} packageName - 条目包名。
|
|
44
|
+
* @param {string | null | undefined} moduleName - 条目模块说明符。
|
|
45
|
+
* @returns {Array<{sourceKind: string, label: string, owners: string[]}>} 匹配到的注入类型。
|
|
46
|
+
*/
|
|
47
|
+
export function injectionKindsOf(packageName, moduleName) {
|
|
48
|
+
const candidates = new Set([bareName(packageName), bareName(moduleName)].filter((name) => name !== ''))
|
|
49
|
+
if (candidates.size === 0) return []
|
|
50
|
+
const rows = []
|
|
51
|
+
for (const [sourceKind, owners] of Object.entries(MESSAGE_INJECTION_OWNERS)) {
|
|
52
|
+
if (!owners.some((owner) => candidates.has(owner))) continue
|
|
53
|
+
rows.push({
|
|
54
|
+
sourceKind,
|
|
55
|
+
label: MESSAGE_INJECTION_LABELS[sourceKind] ?? sourceKind,
|
|
56
|
+
owners: [...owners],
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
return rows
|
|
60
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 组合包层与用户 patch 层的归因。
|
|
3
|
+
*
|
|
4
|
+
* 一份快照要回答「这一行是哪来的」,三个来源必须分开记:
|
|
5
|
+
* 1. profile 声明的 bundle 顺序(决定层级归因的优先级);
|
|
6
|
+
* 2. 每个可用 bundle 插入了哪些行、覆盖了哪些行;
|
|
7
|
+
* 3. 用户自己在 profile / home 两层 `cordis.patch.yml` 里插入或覆盖的行。
|
|
8
|
+
* @module dsh-plugin-console/host/layers
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { readJsonFile, readTextFile } from './fs.js'
|
|
12
|
+
import { scanPatchRows } from './patch-scan.js'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 采集组合包层。
|
|
16
|
+
*
|
|
17
|
+
* `rowOwner` 只记**首个**插入该行的 bundle(后插入的只是覆盖),
|
|
18
|
+
* `rowOverriders` 记所有覆盖它的 bundle,顺序即应用顺序。
|
|
19
|
+
* @param {object} profile - `profileContext` 服务。
|
|
20
|
+
* @param {object} pluginManager - `pluginManager` 服务。
|
|
21
|
+
* @returns {Promise<{bundleOrder: string[], profileManifest: object | null, rowOwner: Map, rowOverriders: Map, layerList: Array, enabledBundles: string[]}>} 层信息。
|
|
22
|
+
*/
|
|
23
|
+
export async function collectBundles(profile, pluginManager) {
|
|
24
|
+
// —— 1. profile 声明的 bundle 顺序:决定层级归因的优先级 ——
|
|
25
|
+
let bundleOrder = []
|
|
26
|
+
let profileManifest = null
|
|
27
|
+
if (profile?.dir) {
|
|
28
|
+
const manifest = await readJsonFile(`${profile.dir}/package.json`)
|
|
29
|
+
if (manifest !== null) {
|
|
30
|
+
profileManifest = manifest
|
|
31
|
+
bundleOrder = manifest?.dsh?.profile?.bundles ?? []
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// —— 2. 每个可用 bundle 插入了哪些行(用于层级归因) ——
|
|
36
|
+
/** @type {Map<string, {bundle: string, version?: string}>} 行 id → 首个插入它的 bundle */
|
|
37
|
+
const rowOwner = new Map()
|
|
38
|
+
/** @type {Map<string, string[]>} 行 id → 覆盖它的 bundle 列表 */
|
|
39
|
+
const rowOverriders = new Map()
|
|
40
|
+
/** @type {Array<{name: string, version?: string, kind: string}>} */
|
|
41
|
+
const layerList = []
|
|
42
|
+
|
|
43
|
+
let bundles = []
|
|
44
|
+
if (pluginManager && typeof pluginManager.listBundles === 'function') {
|
|
45
|
+
try {
|
|
46
|
+
bundles = await pluginManager.listBundles()
|
|
47
|
+
} catch {
|
|
48
|
+
bundles = []
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const enabledBundles = bundleOrder.length > 0
|
|
53
|
+
? bundleOrder
|
|
54
|
+
: bundles.filter((bundle) => bundle.enabled).map((bundle) => bundle.name)
|
|
55
|
+
|
|
56
|
+
for (const bundleName of enabledBundles) {
|
|
57
|
+
const bundle = bundles.find((candidate) => candidate.name === bundleName)
|
|
58
|
+
if (!bundle) continue
|
|
59
|
+
const kind = bundle.availability === 'profile' ? 'package' : 'builtin'
|
|
60
|
+
layerList.push({ name: bundle.name, version: bundle.version, kind })
|
|
61
|
+
for (const row of bundle.rows ?? []) {
|
|
62
|
+
if (!row?.rowId) continue
|
|
63
|
+
if (!rowOwner.has(row.rowId)) rowOwner.set(row.rowId, { bundle: bundle.name, version: bundle.version })
|
|
64
|
+
}
|
|
65
|
+
for (const rowId of bundle.overrides ?? []) {
|
|
66
|
+
const list = rowOverriders.get(rowId) ?? []
|
|
67
|
+
list.push(bundle.name)
|
|
68
|
+
rowOverriders.set(rowId, list)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return { bundleOrder, profileManifest, rowOwner, rowOverriders, layerList, enabledBundles }
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 采集用户自己的 patch 层:profile、home 与启动覆盖层。
|
|
77
|
+
*
|
|
78
|
+
* 三层在 DSH 里的应用顺序是 profile → home → overlays
|
|
79
|
+
* (`readProfilePatches`),所以归因数组也按这个顺序追加;
|
|
80
|
+
* 界面上的 `→` 才等于真实的优先级。文件只读不写。
|
|
81
|
+
* @param {object} profile - `profileContext` 服务。
|
|
82
|
+
* @returns {Promise<{userInserted: Set<string>, userOverridden: Set<string>, userOverrides: Map<string, Array<{layer: string, index?: number, file: string | null, keys: string[]}>>, userLayers: Array<object>}>} 用户层信息。
|
|
83
|
+
*/
|
|
84
|
+
export async function collectUserLayers(profile) {
|
|
85
|
+
const userInserted = new Set()
|
|
86
|
+
const userOverridden = new Set()
|
|
87
|
+
/** @type {Map<string, Array<{layer: string, index?: number, file: string | null, keys: string[]}>>} */
|
|
88
|
+
const userOverrides = new Map()
|
|
89
|
+
const userLayers = []
|
|
90
|
+
|
|
91
|
+
/** 一个 id 可能同时被 profile 与 home 改过;保留每一次,顺序即优先级。 */
|
|
92
|
+
const attribute = (id, info) => {
|
|
93
|
+
userOverridden.add(id)
|
|
94
|
+
const list = userOverrides.get(id) ?? []
|
|
95
|
+
list.push(info)
|
|
96
|
+
userOverrides.set(id, list)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const files = []
|
|
100
|
+
if (profile?.dir) files.push({ layer: 'profile', dir: profile.dir, file: profile.patchPath ?? `${profile.dir}/cordis.patch.yml` })
|
|
101
|
+
if (profile?.home) files.push({ layer: 'home', dir: profile.home, file: `${profile.home}/cordis.patch.yml` })
|
|
102
|
+
for (const { layer, dir, file } of files) {
|
|
103
|
+
const raw = await readTextFile(file)
|
|
104
|
+
if (!raw) continue
|
|
105
|
+
const { inserted, overridden } = scanPatchRows(raw)
|
|
106
|
+
for (const id of inserted.keys()) userInserted.add(id)
|
|
107
|
+
for (const [id, info] of overridden) attribute(id, { layer, file, keys: info.keys })
|
|
108
|
+
userLayers.push({ name: layer, dir, rows: inserted.size, overrides: overridden.size })
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// 启动覆盖层(`--patch`、遥测开关等)已经是解析好的 PatchOptions,
|
|
112
|
+
// 不再读文件;它们应用在 profile / home 之上,所以排在归因最后。
|
|
113
|
+
const overlays = Array.isArray(profile?.overlays) ? profile.overlays : []
|
|
114
|
+
for (const [index, patch] of overlays.entries()) {
|
|
115
|
+
if (patch === null || typeof patch !== 'object') continue
|
|
116
|
+
if (Array.isArray(patch.insert)) {
|
|
117
|
+
for (const row of patch.insert) {
|
|
118
|
+
if (typeof row?.id === 'string') userInserted.add(row.id)
|
|
119
|
+
}
|
|
120
|
+
continue
|
|
121
|
+
}
|
|
122
|
+
if (typeof patch.id !== 'string') continue
|
|
123
|
+
attribute(patch.id, {
|
|
124
|
+
layer: 'overlay',
|
|
125
|
+
...(overlays.length > 1 ? { index } : {}),
|
|
126
|
+
file: null,
|
|
127
|
+
keys: Object.keys(patch).filter((key) => key !== 'id'),
|
|
128
|
+
})
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return { userInserted, userOverridden, userOverrides, userLayers }
|
|
132
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 会话贡献掩码:把一个插件的工具贡献从**一个会话**里隐藏。
|
|
3
|
+
*
|
|
4
|
+
* 掩码只动工具 deny 名单,不卸载插件、不停服务、不移除事件监听,也不改模板
|
|
5
|
+
* 的提示词覆盖。它和模板共存的方式是「编译成一份 plan 再物化」:
|
|
6
|
+
* base = 会话自己的 manifest recipe(模板关闭时为 null)
|
|
7
|
+
* mask = 所有掩码记录里的工具名并集
|
|
8
|
+
* plan = mergeRecipes(base, mask)
|
|
9
|
+
* 这样同一个 agent scope 只会被物化一次,不会出现同名注册冲突。
|
|
10
|
+
*
|
|
11
|
+
* 掩码记录会持久化;resume / 子 Agent 创建时由 `installMaskInheritance` 重放。
|
|
12
|
+
* @module dsh-plugin-console/host/masks
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { applyRecipeToSession, clearRecipeForSession } from './templates/materialize.js'
|
|
16
|
+
import { deleteMask, getManifest, masksForSession, putMask } from './templates/repository.js'
|
|
17
|
+
import { ensureTemplateStore, templateTable } from './templates/store.js'
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 合并模板 recipe 与掩码 recipe。
|
|
21
|
+
*
|
|
22
|
+
* 只合并工具 deny;提示词覆盖保留模板自己的(掩码目前不碰提示词段)。
|
|
23
|
+
* @param {object | null} base - 模板 recipe。
|
|
24
|
+
* @param {object | null} mask - 掩码 recipe。
|
|
25
|
+
* @returns {object} 合并后的 recipe。
|
|
26
|
+
*/
|
|
27
|
+
export function mergeRecipes(base, mask) {
|
|
28
|
+
const left = base ?? {}
|
|
29
|
+
const right = mask ?? {}
|
|
30
|
+
const disabled = [...new Set([...(left.tools?.disabled ?? []), ...(right.tools?.disabled ?? [])])]
|
|
31
|
+
return {
|
|
32
|
+
...left,
|
|
33
|
+
prompt: { ...(left.prompt ?? {}), ...(right.prompt ?? {}) },
|
|
34
|
+
tools: { ...(left.tools ?? {}), ...(right.tools ?? {}), disabled },
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** 一个会话的全部掩码编译成 recipe;没有掩码时 null。 */
|
|
39
|
+
export function buildMaskRecipe(state, sessionId) {
|
|
40
|
+
const masks = masksForSession(state, sessionId)
|
|
41
|
+
if (masks.length === 0) return null
|
|
42
|
+
const tools = [...new Set(masks.flatMap((mask) => Array.isArray(mask.tools) ? mask.tools : []))]
|
|
43
|
+
return tools.length > 0 ? { tools: { disabled: tools } } : null
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 会话的模板 recipe;没有 manifest 或模板显式关闭时为 null。 */
|
|
47
|
+
export function baseRecipeForSession(state, sessionId) {
|
|
48
|
+
const manifest = getManifest(state, sessionId)
|
|
49
|
+
if (manifest === undefined) return null
|
|
50
|
+
const activation = templateTable(state, 'activations').get(sessionId)
|
|
51
|
+
if (activation !== undefined && activation.enabled !== true) return null
|
|
52
|
+
return manifest.recipe ?? null
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** 取会话的模式:header 优先,注册表兜底。 */
|
|
56
|
+
function presetOf(ctx, agent) {
|
|
57
|
+
const header = agent?.session?.header
|
|
58
|
+
if (typeof header?.agentPreset === 'string') return header.agentPreset
|
|
59
|
+
try {
|
|
60
|
+
return ctx.get('agentPresets')?.presetForSession?.(agent?.session) ?? undefined
|
|
61
|
+
} catch {
|
|
62
|
+
return undefined
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 按「模板 + 掩码」重新物化一个会话。
|
|
68
|
+
*
|
|
69
|
+
* 没有活 agent 时不物化,只返回 recorded;resume 时会由监听器重放。
|
|
70
|
+
* @param {object} ctx - host 插件上下文。
|
|
71
|
+
* @param {object} state - 模板存储状态。
|
|
72
|
+
* @param {string} sessionId - 会话 id。
|
|
73
|
+
* @returns {Promise<object>} 物化结果。
|
|
74
|
+
*/
|
|
75
|
+
export async function reapplySessionPlan(ctx, state, sessionId) {
|
|
76
|
+
await ensureTemplateStore(ctx, state)
|
|
77
|
+
const base = baseRecipeForSession(state, sessionId)
|
|
78
|
+
const mask = buildMaskRecipe(state, sessionId)
|
|
79
|
+
if (base === null && mask === null) {
|
|
80
|
+
const cleared = await clearRecipeForSession(sessionId)
|
|
81
|
+
return { applied: false, live: true, cleared, reason: '没有提示词模板也没有掩码' }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const agent = ctx.get('agents')?.get?.(sessionId)
|
|
85
|
+
if (agent === undefined) {
|
|
86
|
+
return { applied: false, live: false, recorded: true, reason: '会话未激活;掩码已记录,resume 时按它重放' }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const result = await applyRecipeToSession(ctx, sessionId, mergeRecipes(base, mask), presetOf(ctx, agent))
|
|
90
|
+
return {
|
|
91
|
+
...result,
|
|
92
|
+
live: true,
|
|
93
|
+
maskTools: mask?.tools?.disabled ?? [],
|
|
94
|
+
baseApplied: base !== null,
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 记录一条掩码并立即重放 plan。
|
|
100
|
+
* @param {object} ctx - host 插件上下文。
|
|
101
|
+
* @param {object} state - 模板存储状态。
|
|
102
|
+
* @param {{sessionId: string, rowId: string, tools: string[]}} input - 目标。
|
|
103
|
+
* @returns {Promise<object>} 记录与物化结果。
|
|
104
|
+
*/
|
|
105
|
+
export async function addMask(ctx, state, input) {
|
|
106
|
+
await ensureTemplateStore(ctx, state)
|
|
107
|
+
const record = await putMask(state, input.sessionId, input.rowId, input.tools)
|
|
108
|
+
return { record, result: await reapplySessionPlan(ctx, state, input.sessionId) }
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* 删除一条掩码并重放剩余 plan。
|
|
113
|
+
* @param {object} ctx - host 插件上下文。
|
|
114
|
+
* @param {object} state - 模板存储状态。
|
|
115
|
+
* @param {{sessionId: string, rowId: string}} input - 目标。
|
|
116
|
+
* @returns {Promise<object>} 删除与物化结果。
|
|
117
|
+
*/
|
|
118
|
+
export async function removeMask(ctx, state, input) {
|
|
119
|
+
await ensureTemplateStore(ctx, state)
|
|
120
|
+
await deleteMask(state, input.sessionId, input.rowId)
|
|
121
|
+
return { result: await reapplySessionPlan(ctx, state, input.sessionId) }
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* 在 agent 创建时重放掩码。
|
|
126
|
+
*
|
|
127
|
+
* 必须排在模板继承之后:模板先把 base 物化,掩码再把它和 mask 合并重物化。
|
|
128
|
+
* 没有掩码时什么都不做,避免每次 resume 都重复注册。
|
|
129
|
+
* @param {object} ctx - host 插件上下文。
|
|
130
|
+
* @param {object} state - 模板存储状态。
|
|
131
|
+
* @returns {void}
|
|
132
|
+
*/
|
|
133
|
+
export function installMaskInheritance(ctx, state) {
|
|
134
|
+
ctx.on('agent/created', async ({ agent }) => {
|
|
135
|
+
const sessionId = typeof agent?.id === 'string' ? agent.id : agent?.session?.id
|
|
136
|
+
if (typeof sessionId !== 'string') return
|
|
137
|
+
try {
|
|
138
|
+
await ensureTemplateStore(ctx, state)
|
|
139
|
+
if (masksForSession(state, sessionId).length === 0) return
|
|
140
|
+
await reapplySessionPlan(ctx, state, sessionId)
|
|
141
|
+
} catch (error) {
|
|
142
|
+
ctx.logger?.warn?.('plugin-console: 掩码重放失败:' + (error instanceof Error ? error.message : String(error)))
|
|
143
|
+
}
|
|
144
|
+
})
|
|
145
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 模式(Agent 预设)的两份事实:声明组合与运行时实测组合。
|
|
3
|
+
*
|
|
4
|
+
* 模式不是全局 Loader 的一部分,它挂在每个 Agent 自己的隔离 scope 里,所以必须
|
|
5
|
+
* 单独读:`compositionInventory()` 给声明值,`inspectCompositions(agent.ctx)` 给
|
|
6
|
+
* 此刻真正挂载的结果。两者的差额来自禁用行、条件行与仍在等依赖的条目。
|
|
7
|
+
* @module dsh-plugin-console/host/modes
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { FIBER_PHASE } from './constants.js'
|
|
11
|
+
import { readScopeTools } from './tools.js'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 读取 Agent 预设(模式)的当前组合。
|
|
15
|
+
*
|
|
16
|
+
* 每个 preset 自带一串子插件行,这是「模式」这一层贡献的插件;
|
|
17
|
+
* 和 base/profile 的条目合并后才是会话最终得到的插件集合。
|
|
18
|
+
* @param {object} ctx - host 插件上下文。
|
|
19
|
+
* @param {Map<string, Array<object>>} rowsByModule - 模块名 → 全局行摘要。
|
|
20
|
+
* @returns {Promise<Array<object>>} 预设列表;无 roster 时为空数组。
|
|
21
|
+
*/
|
|
22
|
+
export async function readModes(ctx, rowsByModule) {
|
|
23
|
+
const presets = ctx.get('agentPresets')
|
|
24
|
+
if (presets === void 0 || typeof presets.compositionInventory !== 'function') return []
|
|
25
|
+
let inventory
|
|
26
|
+
try {
|
|
27
|
+
inventory = await presets.compositionInventory()
|
|
28
|
+
} catch {
|
|
29
|
+
return []
|
|
30
|
+
}
|
|
31
|
+
// `order` 只出现在 preset 的元数据列表里,compositionInventory 不带它。
|
|
32
|
+
// 它不是插件排序,而是模式自己的选择器顺序;读不到就不编造。
|
|
33
|
+
const orderById = new Map()
|
|
34
|
+
try {
|
|
35
|
+
const listed = typeof presets.list === 'function' ? await presets.list() : []
|
|
36
|
+
for (const row of Array.isArray(listed) ? listed : []) {
|
|
37
|
+
if (typeof row?.id === 'string' && typeof row.order === 'number') orderById.set(row.id, row.order)
|
|
38
|
+
}
|
|
39
|
+
} catch {
|
|
40
|
+
// 元数据列表读不到不影响组合本身
|
|
41
|
+
}
|
|
42
|
+
return inventory.map((mode) => ({
|
|
43
|
+
id: mode.id,
|
|
44
|
+
name: typeof mode.name === 'string' ? mode.name : undefined,
|
|
45
|
+
description: typeof mode.description === 'string' ? mode.description : undefined,
|
|
46
|
+
order: orderById.get(mode.id),
|
|
47
|
+
isDefault: Boolean(mode.isDefault),
|
|
48
|
+
broken: typeof mode.broken === 'string' ? mode.broken : undefined,
|
|
49
|
+
rows: (mode.rows ?? []).map((row) => ({
|
|
50
|
+
entryId: typeof row.entryId === 'string' ? row.entryId : null,
|
|
51
|
+
moduleName: row.moduleName,
|
|
52
|
+
// 读 preset 行引用的全局条目:会话追溯链的中间一环
|
|
53
|
+
globalRows: rowsByModule?.get(row.moduleName) ?? [],
|
|
54
|
+
enabled: row.enabled === 'conditional' ? null : row.enabled !== false,
|
|
55
|
+
conditional: row.enabled === 'conditional',
|
|
56
|
+
condition: typeof row.condition === 'string' ? row.condition : undefined,
|
|
57
|
+
phase: row.fiberState === undefined ? null : (FIBER_PHASE[row.fiberState] ?? null),
|
|
58
|
+
})),
|
|
59
|
+
})).sort((left, right) => (left.order ?? Infinity) - (right.order ?? Infinity) || left.id.localeCompare(right.id))
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 读取一个活会话此刻**真正在运行**的模式组合。
|
|
64
|
+
*
|
|
65
|
+
* `inspectCompositions(agentCtx)` 只返回 `state === ACTIVE` 且未被禁用的条目。
|
|
66
|
+
* 因此它给的是实测值而不是声明值;冷会话没有 fiber,取不到,返回 undefined。
|
|
67
|
+
* @param {object} ctx - host 插件上下文。
|
|
68
|
+
* @param {object} agent - 活跃 Agent(`agents.get(sessionId)`)。
|
|
69
|
+
* @returns {{preset: string, modules: Array<string>, leakedServices: string[], tools?: string[]} | undefined} 运行时组合。
|
|
70
|
+
*/
|
|
71
|
+
export function readRuntimeComposition(ctx, agent) {
|
|
72
|
+
const presets = ctx.get('agentPresets')
|
|
73
|
+
if (presets === undefined || typeof presets.inspectCompositions !== 'function') return undefined
|
|
74
|
+
const agentCtx = agent?.ctx
|
|
75
|
+
if (agentCtx === undefined) return undefined
|
|
76
|
+
let compositions
|
|
77
|
+
try {
|
|
78
|
+
compositions = presets.inspectCompositions(agentCtx)
|
|
79
|
+
} catch {
|
|
80
|
+
return undefined
|
|
81
|
+
}
|
|
82
|
+
const composition = compositions?.[0]
|
|
83
|
+
if (composition === undefined) return undefined
|
|
84
|
+
const view = readScopeTools(ctx, agentCtx)
|
|
85
|
+
return {
|
|
86
|
+
preset: composition.id,
|
|
87
|
+
modules: (composition.modules ?? [])
|
|
88
|
+
.map((module) => module?.moduleName)
|
|
89
|
+
.filter((name) => typeof name === 'string'),
|
|
90
|
+
leakedServices: (composition.leakedServices ?? []).filter((name) => typeof name === 'string'),
|
|
91
|
+
/** 读 agent scope 此刻可用的工具:已算完全局层与 scope 层的遮蔽。 */
|
|
92
|
+
tools: view?.visible,
|
|
93
|
+
/** restrict 之前的已知集;与 visible 的差集才是「被 restrict 过滤」。 */
|
|
94
|
+
knownNames: view?.knownNames,
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 名字归一与来源层判定。
|
|
3
|
+
*
|
|
4
|
+
* 这三个函数处理的是「一行的身份从哪来」这类问题:包名怎么从模块标识里取、
|
|
5
|
+
* 没有组合包认领的行该算哪一层、哪些行只是 Loader 的结构行。
|
|
6
|
+
* @module dsh-plugin-console/host/names
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 从包名得到「裸包名」:`@scope/name` 或 `name`,丢弃子路径导出。
|
|
11
|
+
* @param {string} specifier - Loader 行的模块标识。
|
|
12
|
+
* @returns {string | undefined} 裸包名;不是包标识时 undefined。
|
|
13
|
+
*/
|
|
14
|
+
export function barePackageName(specifier) {
|
|
15
|
+
if (typeof specifier !== 'string' || specifier.startsWith('.') || specifier.startsWith('cordis:')) return undefined
|
|
16
|
+
const parts = specifier.split('/')
|
|
17
|
+
if (specifier.startsWith('@')) return parts.length >= 2 ? `${parts[0]}/${parts[1]}` : undefined
|
|
18
|
+
return parts[0]
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 没有 bundle 认领的行,按来源判层。
|
|
23
|
+
*
|
|
24
|
+
* - `cordis:*` 是 Loader 结构行(include 根、group),不属于任何组合包;
|
|
25
|
+
* - 深度 0 且无 bundle 认领的行,是插件在运行时用 `ctx.plugin()` 直接挂上的
|
|
26
|
+
* 子 fiber(例如目录选择器的原生实现),也不来自任何 patch;
|
|
27
|
+
* - 其余未认领的行只可能是 profile 或 home 补丁自己插入的。
|
|
28
|
+
* @param {string} moduleName - 行的模块标识。
|
|
29
|
+
* @param {number} depth - 条目在 id 路径上的深度;顶层根行的深度是 0。
|
|
30
|
+
* @returns {string} 层标识。
|
|
31
|
+
*/
|
|
32
|
+
export function fallbackLayer(moduleName, depth) {
|
|
33
|
+
if (typeof moduleName === 'string' && moduleName.startsWith('cordis:')) return 'cordis'
|
|
34
|
+
return depth === 0 ? 'runtime' : 'profile'
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 结构行(cordis:include、cordis:group)不是插件,不计入任何统计。
|
|
39
|
+
* @param {object} entry - 已采集的条目。
|
|
40
|
+
* @returns {boolean} 是否结构行。
|
|
41
|
+
*/
|
|
42
|
+
export function isStructuralEntry(entry) {
|
|
43
|
+
return typeof entry.moduleName === 'string' && entry.moduleName.startsWith('cordis:')
|
|
44
|
+
}
|