@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
|
@@ -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
|
+
}
|