dsh-coexist-plugin 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 唐宋元明清
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,88 @@
1
- # Temporary Holding Version
1
+ # dsh-coexist-plugin
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ **让 DSH 里抢同一个资源的插件尽量都留下。**
4
+ *Plugin coexistence for DeepSeek Harness: when two plugins claim the same tool, command, provider or service, keep both instead of silently dropping one.*
5
+
6
+ ---
7
+
8
+ ## 它解决什么
9
+
10
+ DSH 里装了两个插件、它们注册了同一个工具名时,后加载的那个通常会失败或覆盖前一个。`dsh-coexist-plugin` 在宿主注册阶段接管冲突:
11
+
12
+ - **同名 tool**:第二个实现自动注册为 `name__coexist1`(N 个插件依次 `__coexist2`…),两个都能调用。
13
+ - **同名命令 / provider**:运行期自动加后缀别名,调用记录写入报告。
14
+ - **service key 冲突**:诊断双方来源;可选有限代理或上下文隔离,让双方各自可用。
15
+ - **设置页「插件共存」**:状态卡片、检查冲突、一键修复、回滚、安装/卸载、端口释放、保留原名,全部可视化操作。
16
+
17
+ ## 安装
18
+
19
+ npm:[dsh-coexist-plugin](https://www.npmjs.com/package/dsh-coexist-plugin) ·
20
+ CLI:[dsh-coexist-cli](https://www.npmjs.com/package/dsh-coexist-cli)
21
+
22
+ 上架插件市场后可直接在市场内安装。也可以:
23
+
24
+ ```powershell
25
+ # 通过 CLI 安装(会自动做版本检查、写 profile、失败回滚)
26
+ dsh-coexist install dsh-coexist-plugin --profile desktop
27
+
28
+ # 没有全局 CLI 时
29
+ npx dsh-coexist-cli install dsh-coexist-plugin --profile desktop
30
+ ```
31
+
32
+ 插件自带 `dsh-coexist-cli` 依赖,设置页不依赖全局命令;卸载插件不会残留全局工具。
33
+
34
+ 安装完成后重启 DSH,在 **设置 → 插件共存** 查看状态。
35
+
36
+ ## 设置页
37
+
38
+ 「插件共存」提供:
39
+
40
+ | 操作 | 说明 |
41
+ |---|---|
42
+ | 刷新状态 | 当前 profile、插件数、运行期失败、别名数、备份数 |
43
+ | 检查冲突 | 静态扫描 + 端口探测 + 日志,列出可修复项 |
44
+ | 一键修复 | 生成计划并应用(loader id、重复 bundle、端口、patch 覆盖) |
45
+ | 回滚最近备份 | 恢复 `package.json` / `cordis.patch.yml` / lockfile,可重新安装依赖 |
46
+ | 安装 / 卸载 | 安装前做兼容检查,失败自动回滚 |
47
+ | 插件市场 | 一键跳到宿主插件页的市场标签;没有市场面板时回退到浏览器目录 |
48
+ | service 隔离 | 两个插件提供同名服务时,让后加载的插件用自己的副本;默认关闭,出现 service 冲突再开 |
49
+ | 保留原名 | 把某个插件放到第三方插件段最前,冲突时它保留原名 |
50
+
51
+ 所有写操作都会先备份,并写入本地 `audit.jsonl`。
52
+
53
+ ## 卸载
54
+
55
+ - 在设置页「已启用插件」里点 **卸载**,或执行
56
+ `dsh-coexist uninstall dsh-coexist-plugin --profile desktop --purge`。
57
+ - 会移除 bundle、依赖、`node_modules` 安装目录、本地安装的分阶段副本,以及随插件分发的 CLI。
58
+ - 加 `--purge` 会连同 `~/.dsh/coexist`(备份、报告、配置)一起删除;不加则保留,方便以后回滚或排查。
59
+ - 从 DSH 宿主的“插件页”卸载会清理安装产物(分阶段副本、CLI、旧启动器);
60
+ 备份/报告仍保留,彻底清理请用 `--purge`。
61
+ - 插件日志写在 `~/.dsh/coexist/plugin.log`。
62
+
63
+ ## 兼容性
64
+
65
+ - DSH 桌面版 **2.0.21-next**(内部 `@deepseek-ai/dsh` **0.2.1-rc.4**)实测通过。
66
+ - 声明 `engines.dsh: ">=0.2.0 <0.3.0"`;`@deepseek-ai/dsh >=0.2.0` 为可选 peer。
67
+ - 目前只在 **Windows** 上做过真实回归;其他平台未验证。
68
+ - 与 DSH 官方 bundle、`dshmarket`、`@deepseek-ai/dsh-web-app` 同场加载无冲突。
69
+
70
+ ## 安全与隐私
71
+
72
+ - 设置页接口注册在宿主 web 服务的 `localhost` 同源路径下,不额外监听端口。
73
+ - 接口只接受 `POST` + `application/json`、本机回环连接;http(s) `Origin` 必须同源。
74
+ 跨站表单、`text/plain` 请求和局域网来源都会被拒绝。
75
+ - 插件本身不联网、不上传任何数据;所有状态写入本地 `~/.dsh/coexist`。
76
+ - 安装/卸载/回滚都会先备份;卸载、回滚、结束端口进程在 UI 中有二次确认。
77
+ - `serviceIsolate` 默认关闭,需显式开启并重启。
78
+
79
+ ## 已知限制
80
+
81
+ - UI 插槽(浏览器客户端注册)与文件锁冲突只能报告,host 插件无法安全改名。
82
+ - service 只做到诊断、有限代理与上下文隔离,不做跨进程隔离。
83
+ - 不处理语义互斥(两个工具逻辑上不能同时启用)。
84
+ - 不提供字节级 `node_modules` 回滚。
85
+
86
+ ## License
87
+
88
+ MIT
@@ -0,0 +1,20 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" width="128" height="128" viewBox="0 0 128 128" role="img" aria-label="dsh-coexist">
2
+ <defs>
3
+ <linearGradient id="bg" x1="0" y1="0" x2="1" y2="1">
4
+ <stop offset="0" stop-color="#1f2937"/>
5
+ <stop offset="1" stop-color="#0b1220"/>
6
+ </linearGradient>
7
+ <linearGradient id="a" x1="0" y1="0" x2="1" y2="1">
8
+ <stop offset="0" stop-color="#38bdf8"/>
9
+ <stop offset="1" stop-color="#6366f1"/>
10
+ </linearGradient>
11
+ <linearGradient id="b" x1="1" y1="0" x2="0" y2="1">
12
+ <stop offset="0" stop-color="#f472b6"/>
13
+ <stop offset="1" stop-color="#f59e0b"/>
14
+ </linearGradient>
15
+ </defs>
16
+ <rect width="128" height="128" rx="28" fill="url(#bg)"/>
17
+ <rect x="22" y="34" width="52" height="52" rx="14" fill="none" stroke="url(#a)" stroke-width="9"/>
18
+ <rect x="54" y="42" width="52" height="52" rx="14" fill="none" stroke="url(#b)" stroke-width="9"/>
19
+ <circle cx="64" cy="64" r="7" fill="#e5e7eb"/>
20
+ </svg>
@@ -0,0 +1,12 @@
1
+ - insert:
2
+ - id: coexist
3
+ name: dsh-coexist-plugin
4
+ config:
5
+ # alias(默认):同名不同定义双持;fallback:主实现失败时降级到备份。
6
+ mode: alias
7
+ # 有限 service 代理:仅在双方接口不重叠时合并,默认关闭。
8
+ serviceBridge: false
9
+ # service 冲突时给后加载插件独立作用域,默认关闭。
10
+ serviceIsolate: false
11
+ # 留空表示自动接管所有工具名。
12
+ watch: []
package/index.js ADDED
@@ -0,0 +1,267 @@
1
+ import { appendFileSync, mkdirSync } from 'node:fs'
2
+ import os from 'node:os'
3
+ import path from 'node:path'
4
+ import { installPrototypePatch } from './src/patch.js'
5
+ import { installToolAliasing } from './src/aliasing.js'
6
+ import { installCommandPatch } from './src/commands.js'
7
+ import { installProviderPatch } from './src/providers.js'
8
+ import { installToolObservation, installToolPolicies } from './src/toolpolicy.js'
9
+ import { createDiagnostics, defaultReportFile } from './src/diagnostics.js'
10
+ import { loadCoexistConfig, loadRuntimePlan } from './src/config.js'
11
+ import { installUiRoutes } from './src/ui.js'
12
+ import { installUninstallCleanup } from './src/cleanup.js'
13
+
14
+ const logFile =
15
+ process.env.DSH_COEXIST_LOG ??
16
+ path.join(os.homedir(), '.dsh', 'coexist', 'plugin.log')
17
+
18
+ function log(message) {
19
+ try {
20
+ mkdirSync(path.dirname(logFile), { recursive: true })
21
+ appendFileSync(logFile, `[${new Date().toISOString()}] ${message}\n`)
22
+ } catch {
23
+ // Logging must never break the host.
24
+ }
25
+ console.log(message)
26
+ }
27
+
28
+ function currentService(ctx, name) {
29
+ try {
30
+ return typeof ctx.get === 'function' ? ctx.get(name) : ctx[name]
31
+ } catch {
32
+ return undefined
33
+ }
34
+ }
35
+
36
+ function resolveMode(config = {}) {
37
+ const value = config.mode ?? process.env.DSH_COEXIST_MODE ?? 'alias'
38
+ if (value === 'fallback' || value === 'route') return value
39
+ return 'alias'
40
+ }
41
+
42
+ let cachedConfig = null
43
+
44
+ function currentConfig() {
45
+ if (!cachedConfig) cachedConfig = loadCoexistConfig()
46
+ return cachedConfig
47
+ }
48
+
49
+ function activateTools(ctx, service, source, diagnostics, config) {
50
+ const coexist = currentConfig()
51
+ const capabilities = globalThis.__dshCoexistCapabilities ?? {}
52
+ Object.assign(capabilities, {
53
+ tools: true,
54
+ toolsRegister: typeof service?.register === 'function',
55
+ toolsGuard: typeof service?.guard === 'function',
56
+ schemas: typeof service?.schemas === 'function',
57
+ get: typeof service?.get === 'function',
58
+ })
59
+ globalThis.__dshCoexistCapabilities = capabilities
60
+
61
+ const ToolService = service ? Object.getPrototypeOf(service)?.constructor : null
62
+ const installed = installPrototypePatch(ToolService, {
63
+ auto: true,
64
+ mode: resolveMode(config),
65
+ routes: coexist.value.routes,
66
+ logFile,
67
+ onAlias: (name, alias) => globalThis.__dshCoexistRuntime?.onAlias?.(name, alias),
68
+ onCall: (event) => globalThis.__dshCoexistRuntime?.onAliasCall?.(event),
69
+ onFallback: (event) => globalThis.__dshCoexistRuntime?.onFallback?.(event),
70
+ onRoute: (event) => globalThis.__dshCoexistRuntime?.onRoute?.(event),
71
+ })
72
+ if (!installed) return false
73
+
74
+ log(`[coexist] prototype patch active (${source}: ${ToolService?.name ?? 'unknown'})`)
75
+
76
+ const policy = installToolPolicies(service, {
77
+ policies: coexist.value.toolPolicies,
78
+ log,
79
+ })
80
+ if (policy.installed) {
81
+ try {
82
+ ctx.effect?.(() => () => {
83
+ if (globalThis.__dshCoexistToolPolicyDispose === policy.dispose) {
84
+ policy.dispose?.()
85
+ delete globalThis.__dshCoexistToolPolicyDispose
86
+ }
87
+ })
88
+ } catch {
89
+ // 没有 effect API 时保留进程级状态。
90
+ }
91
+ }
92
+
93
+ const observation = installToolObservation(ctx, {
94
+ enabled: coexist.value.observeTools !== false,
95
+ log,
96
+ onCall: (event) => globalThis.__dshCoexistRuntime?.onToolCall?.(event),
97
+ })
98
+ if (observation.installed) {
99
+ try {
100
+ ctx.effect?.(() => () => {
101
+ if (globalThis.__dshCoexistToolObservationDispose === observation.dispose) {
102
+ observation.dispose?.()
103
+ delete globalThis.__dshCoexistToolObservationDispose
104
+ }
105
+ })
106
+ } catch {
107
+ // 没有 effect API 时保留进程级状态。
108
+ }
109
+ }
110
+
111
+ return true
112
+ }
113
+
114
+ function activateCommands(service, source) {
115
+ const capabilities = globalThis.__dshCoexistCapabilities ?? {}
116
+ capabilities.commands = true
117
+ globalThis.__dshCoexistCapabilities = capabilities
118
+
119
+ const CommandService = service ? Object.getPrototypeOf(service)?.constructor : null
120
+ const installed = installCommandPatch(CommandService, {
121
+ log,
122
+ onAlias: (event) => globalThis.__dshCoexistRuntime?.onCommandAlias?.(event),
123
+ })
124
+ if (installed) log(`[coexist] command patch active (${source})`)
125
+ return installed
126
+ }
127
+
128
+ function activateProviders(service, source) {
129
+ const capabilities = globalThis.__dshCoexistCapabilities ?? {}
130
+ capabilities.providers = true
131
+ globalThis.__dshCoexistCapabilities = capabilities
132
+
133
+ const LlmService = service ? Object.getPrototypeOf(service)?.constructor : null
134
+ const installed = installProviderPatch(LlmService, {
135
+ log,
136
+ onAlias: (event) => globalThis.__dshCoexistRuntime?.onProviderAlias?.(event),
137
+ onConflict: (event) => globalThis.__dshCoexistRuntime?.onProviderConflict?.(event),
138
+ })
139
+ if (installed) log(`[coexist] provider patch active (${source})`)
140
+ return installed
141
+ }
142
+
143
+ function resolveServiceBridge(config = {}) {
144
+ return config.serviceBridge === true || process.env.DSH_COEXIST_SERVICE_BRIDGE === '1'
145
+ }
146
+
147
+ function resolveServiceIsolate(config = {}) {
148
+ return config.serviceIsolate === true || process.env.DSH_COEXIST_SERVICE_ISOLATE === '1'
149
+ }
150
+
151
+ function ensureDiagnostics(ctx, config, extras = {}) {
152
+ const previous = globalThis.__dshCoexistDiagnostics
153
+ if (previous && typeof previous.dispose === 'function') {
154
+ previous.dispose()
155
+ }
156
+
157
+ globalThis.__dshCoexistCapabilities = {}
158
+ const coexist = currentConfig()
159
+ const diagnostics = createDiagnostics(ctx, {
160
+ log,
161
+ mode: resolveMode(config),
162
+ serviceBridge: resolveServiceBridge(config),
163
+ serviceIsolate: resolveServiceIsolate(config),
164
+ servicePolicies: coexist.value.services,
165
+ serviceMerge: coexist.value.serviceMerge,
166
+ recover: coexist.value.recover === true,
167
+ hooks: coexist.value.hooks,
168
+ plan: extras.plan ?? null,
169
+ })
170
+ globalThis.__dshCoexistDiagnostics = diagnostics
171
+ log(`[coexist] diagnostics report -> ${defaultReportFile()}`)
172
+ if (coexist.exists) log(`[coexist] config loaded from ${coexist.file}`)
173
+ return diagnostics
174
+ }
175
+
176
+ export const name = 'coexist'
177
+
178
+ // 不声明 inject:需要在 tools/commands/llm 服务刚提供、其他插件 apply 之前就装上 patch。
179
+ // 若服务已存在则立即 patch;否则等 internal/service 事件,事件是同步派发的,
180
+ // 仍早于所有 inject 插件的 apply。
181
+ export function apply(ctx, config = {}) {
182
+ const runtime = loadRuntimePlan()
183
+ const planSummary = runtime.plan
184
+ ? {
185
+ id: runtime.plan.id ?? null,
186
+ createdAt: runtime.plan.createdAt ?? null,
187
+ actions: Array.isArray(runtime.plan.actions) ? runtime.plan.actions.length : 0,
188
+ applied: runtime.state?.lastApplied ?? null,
189
+ }
190
+ : null
191
+ if (planSummary) {
192
+ log(`[coexist] runtime plan loaded: ${planSummary.id} (${planSummary.actions} actions)`)
193
+ }
194
+
195
+ const diagnostics = ensureDiagnostics(ctx, config, { plan: planSummary })
196
+
197
+ try {
198
+ if (typeof ctx.effect === 'function') {
199
+ ctx.effect(() => () => {
200
+ if (globalThis.__dshCoexistDiagnostics === diagnostics) {
201
+ diagnostics.dispose()
202
+ delete globalThis.__dshCoexistDiagnostics
203
+ }
204
+ })
205
+ }
206
+ } catch {
207
+ // 没有 effect API 时至少保留进程级清理。
208
+ }
209
+
210
+ const installed = { tools: false, commands: false, providers: false }
211
+ const toolsWasPatched = globalThis.__dshCoexistPrototypePatched === true
212
+
213
+ const tryInstall = (serviceName, service, source) => {
214
+ if (!service) return false
215
+
216
+ if (serviceName === 'tools') {
217
+ if (installed.tools) return true
218
+ if (activateTools(ctx, service, source, diagnostics, config)) {
219
+ installed.tools = true
220
+ return true
221
+ }
222
+ if (!toolsWasPatched) {
223
+ installToolAliasing(ctx, config)
224
+ installed.tools = true
225
+ }
226
+ return installed.tools
227
+ }
228
+
229
+ if (serviceName === 'commands') {
230
+ if (installed.commands) return true
231
+ installed.commands = activateCommands(service, source)
232
+ return installed.commands
233
+ }
234
+
235
+ if (serviceName === 'llm') {
236
+ if (installed.providers) return true
237
+ installed.providers = activateProviders(service, source)
238
+ return installed.providers
239
+ }
240
+
241
+ return false
242
+ }
243
+
244
+ tryInstall('tools', currentService(ctx, 'tools'), 'tools service')
245
+ tryInstall('commands', currentService(ctx, 'commands'), 'commands service')
246
+ tryInstall('llm', currentService(ctx, 'llm'), 'llm service')
247
+
248
+ try {
249
+ installUiRoutes(ctx, { ...config, log })
250
+ } catch (error) {
251
+ log(`[coexist] ui routes unavailable: ${error.message}`)
252
+ }
253
+
254
+ try {
255
+ installUninstallCleanup(ctx, { ...config, log })
256
+ } catch (error) {
257
+ log(`[coexist] host uninstall cleanup unavailable: ${error.message}`)
258
+ }
259
+
260
+ if (!installed.tools) log('[coexist] waiting for tools/commands/llm service')
261
+
262
+ return ctx.on('internal/service', (serviceName, service) => {
263
+ if (serviceName !== 'tools' && serviceName !== 'commands' && serviceName !== 'llm') return
264
+ if (!service) return
265
+ tryInstall(serviceName, service, 'internal/service')
266
+ })
267
+ }