dsh-capability-switch 0.0.0-stage → 1.4.9
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/CHANGELOG.md +259 -0
- package/LICENSE +21 -0
- package/README.md +211 -2
- package/cordis.patch.yml +9 -0
- package/docs/screenshots/mcp.png +0 -0
- package/docs/screenshots/overview.png +0 -0
- package/icon.svg +5 -0
- package/lib/client.js +808 -0
- package/lib/imports.js +120 -0
- package/lib/index.js +313 -0
- package/lib/mcp.js +306 -0
- package/lib/skills.js +526 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +61 -4
package/lib/skills.js
ADDED
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
import { promises as fs, createReadStream } from 'node:fs'
|
|
2
|
+
import { homedir } from 'node:os'
|
|
3
|
+
import { basename, dirname, join, relative, resolve, sep } from 'node:path'
|
|
4
|
+
import { createHash, randomUUID } from 'node:crypto'
|
|
5
|
+
|
|
6
|
+
const DISABLED = '.disabled'
|
|
7
|
+
const MAX_SKILL_BYTES = 1024 * 1024
|
|
8
|
+
|
|
9
|
+
export function dshHome() {
|
|
10
|
+
return resolve(process.env.DSH_HOME?.trim() || join(homedir(), '.dsh'))
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function realOrSelf(path) {
|
|
14
|
+
return fs.realpath(path).catch(() => path)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function within(root, target) {
|
|
18
|
+
const rel = relative(root, target)
|
|
19
|
+
return rel === '' || (rel !== '..' && !rel.startsWith('..' + sep) && !rel.startsWith(sep) && !/^[A-Za-z]:[\\/]/.test(rel))
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function roots(cwd) {
|
|
23
|
+
const home = dshHome()
|
|
24
|
+
const agentsHome = resolve(process.env.DSH_AGENTS_HOME?.trim() || join(homedir(), '.agents'))
|
|
25
|
+
const list = [
|
|
26
|
+
{ path: join(home, 'skills'), source: 'user-dsh', scope: 'user', writable: true },
|
|
27
|
+
{ path: join(agentsHome, 'skills'), source: 'user-agents', scope: 'user', writable: false },
|
|
28
|
+
]
|
|
29
|
+
if (typeof cwd === 'string' && cwd.trim()) {
|
|
30
|
+
const project = resolve(cwd)
|
|
31
|
+
const projectReal = await realOrSelf(project)
|
|
32
|
+
const projectAgentsReal = await realOrSelf(join(project, '.agents', 'skills'))
|
|
33
|
+
list.push(
|
|
34
|
+
{ path: join(project, '.dsh', 'skills'), source: 'project-dsh', scope: 'project', writable: false },
|
|
35
|
+
{ path: join(project, '.agents', 'skills'), source: 'project-agents', scope: 'project', writable: within(projectReal, projectAgentsReal) },
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
return list
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function parseScalar(value) {
|
|
42
|
+
const text = value.trim()
|
|
43
|
+
if (text.startsWith('"')) {
|
|
44
|
+
try { return JSON.parse(text) } catch { return text }
|
|
45
|
+
}
|
|
46
|
+
if (text.startsWith("'") && text.endsWith("'")) return text.slice(1, -1).replaceAll("''", "'")
|
|
47
|
+
return text.replace(/\s+#.*$/, '').trim()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function metadata(text, fallback) {
|
|
51
|
+
const match = /^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n|$)/.exec(text)
|
|
52
|
+
const result = { name: fallback, description: '' }
|
|
53
|
+
if (!match) return result
|
|
54
|
+
for (const line of match[1].split(/\r?\n/)) {
|
|
55
|
+
const field = /^(name|description):\s*(.*)$/.exec(line)
|
|
56
|
+
if (field) result[field[1]] = parseScalar(field[2])
|
|
57
|
+
}
|
|
58
|
+
return result
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function readEntry(path) {
|
|
62
|
+
const info = await fs.stat(path)
|
|
63
|
+
if (!info.isFile() || info.size > MAX_SKILL_BYTES) throw new Error('技能入口不是文件或超过 1 MiB')
|
|
64
|
+
return await fs.readFile(path, 'utf8')
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function scanRoot(root) {
|
|
68
|
+
let entries
|
|
69
|
+
try { entries = await fs.readdir(root.path, { withFileTypes: true }) } catch (error) {
|
|
70
|
+
if (error.code === 'ENOENT' || error.code === 'ENOTDIR') return []
|
|
71
|
+
throw error
|
|
72
|
+
}
|
|
73
|
+
const rows = []
|
|
74
|
+
for (const entry of entries) {
|
|
75
|
+
if (entry.name === '.system') continue
|
|
76
|
+
let path
|
|
77
|
+
let form
|
|
78
|
+
if ((entry.isDirectory() || entry.isSymbolicLink()) && !entry.name.endsWith(DISABLED)) {
|
|
79
|
+
const directory = join(root.path, entry.name)
|
|
80
|
+
for (const candidate of ['SKILL.md', 'SKILL.md' + DISABLED]) {
|
|
81
|
+
try {
|
|
82
|
+
const info = await fs.stat(join(directory, candidate))
|
|
83
|
+
if (info.isFile()) { path = join(directory, candidate); form = 'directory'; break }
|
|
84
|
+
} catch (error) { if (error.code !== 'ENOENT') throw error }
|
|
85
|
+
}
|
|
86
|
+
} else if (entry.isFile() && /\.md(?:\.disabled)?$/.test(entry.name)) {
|
|
87
|
+
path = join(root.path, entry.name)
|
|
88
|
+
form = 'file'
|
|
89
|
+
}
|
|
90
|
+
if (!path) continue
|
|
91
|
+
try {
|
|
92
|
+
const text = await readEntry(path)
|
|
93
|
+
const fallback = form === 'directory' ? basename(dirname(path)) : basename(path).replace(/\.md(?:\.disabled)?$/, '')
|
|
94
|
+
const meta = metadata(text, fallback)
|
|
95
|
+
rows.push({
|
|
96
|
+
id: path, name: meta.name || fallback, description: meta.description || '',
|
|
97
|
+
path, directory: form === 'directory' ? dirname(path) : dirname(path), form,
|
|
98
|
+
enabled: !path.endsWith(DISABLED), source: root.source, scope: root.scope,
|
|
99
|
+
writable: root.writable, rootPath: root.path,
|
|
100
|
+
})
|
|
101
|
+
} catch (error) {
|
|
102
|
+
rows.push({ id: path, name: basename(dirname(path)), description: '', path, directory: dirname(path), form: 'invalid', enabled: !path.endsWith(DISABLED), source: root.source, scope: root.scope, writable: root.writable, rootPath: root.path, error: error.message })
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return rows
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const SOURCE_PRIORITY = { 'project-dsh': 0, 'user-dsh': 1, 'project-agents': 2, 'user-agents': 3 }
|
|
109
|
+
|
|
110
|
+
function sortAndDedupe(rows) {
|
|
111
|
+
const ordered = rows.sort((a, b) => a.name.localeCompare(b.name, 'zh-CN')
|
|
112
|
+
|| (SOURCE_PRIORITY[a.source] ?? 4) - (SOURCE_PRIORITY[b.source] ?? 4)
|
|
113
|
+
|| a.path.localeCompare(b.path))
|
|
114
|
+
const seen = new Set()
|
|
115
|
+
return ordered.filter(row => {
|
|
116
|
+
if (seen.has(row.name)) return false
|
|
117
|
+
seen.add(row.name)
|
|
118
|
+
return true
|
|
119
|
+
})
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export async function listSkills(cwd) {
|
|
123
|
+
const groups = await Promise.all((await roots(cwd)).map(scanRoot))
|
|
124
|
+
return sortAndDedupe(groups.flat().filter(row => row.source !== 'user-agents'))
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export async function listProjectSkills(cwd) {
|
|
128
|
+
const groups = await Promise.all((await roots(cwd)).filter(root => root.scope === 'project').map(scanRoot))
|
|
129
|
+
return sortAndDedupe(groups.flat())
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async function claim(path, cwd) {
|
|
133
|
+
if (typeof path !== 'string' || !path.trim()) throw new Error('缺少技能入口路径')
|
|
134
|
+
const requested = resolve(path)
|
|
135
|
+
const allRoots = await roots(cwd)
|
|
136
|
+
let matched
|
|
137
|
+
for (const root of allRoots.filter(root => root.writable)) {
|
|
138
|
+
const rootPath = resolve(root.path)
|
|
139
|
+
const rootReal = await realOrSelf(rootPath)
|
|
140
|
+
const base = within(rootPath, requested) ? rootPath : within(rootReal, requested) ? rootReal : undefined
|
|
141
|
+
if (base !== undefined) {
|
|
142
|
+
matched = { root, base, rootPath, rootReal }
|
|
143
|
+
break
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (matched === undefined) {
|
|
147
|
+
throw new Error('全局 Agent 源目录只读;请操作用户级技能或当前工作区的 .agents/skills')
|
|
148
|
+
}
|
|
149
|
+
const parts = relative(matched.base, requested).split(sep)
|
|
150
|
+
const name = basename(requested)
|
|
151
|
+
const valid = (parts.length === 1 && /\.md(?:\.disabled)?$/.test(name))
|
|
152
|
+
|| (parts.length === 2 && (name === 'SKILL.md' || name === 'SKILL.md' + DISABLED))
|
|
153
|
+
if (!valid) throw new Error('只允许修改技能入口文件')
|
|
154
|
+
const entry = parts.length === 2 ? dirname(requested) : requested
|
|
155
|
+
const info = await fs.lstat(entry)
|
|
156
|
+
const target = await realOrSelf(requested)
|
|
157
|
+
if (info.isSymbolicLink()) {
|
|
158
|
+
const linkTarget = resolve(dirname(entry), await fs.readlink(entry))
|
|
159
|
+
const allowedRoots = matched.root.source === 'user-dsh'
|
|
160
|
+
? allRoots.filter(root => root.source === 'user-dsh' || root.source === 'user-agents')
|
|
161
|
+
: [matched.root]
|
|
162
|
+
let allowed = false
|
|
163
|
+
for (const allowedRoot of allowedRoots) {
|
|
164
|
+
const allowedPath = resolve(allowedRoot.path)
|
|
165
|
+
const allowedReal = await realOrSelf(allowedPath)
|
|
166
|
+
if (within(allowedPath, linkTarget) || within(allowedReal, linkTarget)) { allowed = true; break }
|
|
167
|
+
}
|
|
168
|
+
if (!allowed) throw new Error('技能软链接指向了不允许的目录')
|
|
169
|
+
} else if (!within(matched.rootReal, target)) {
|
|
170
|
+
throw new Error('只允许修改技能根目录中的真实技能')
|
|
171
|
+
}
|
|
172
|
+
return { path: target, root: matched.root }
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async function claimLexical(path, cwd) {
|
|
176
|
+
if (typeof path !== 'string' || !path.trim()) throw new Error('缺少技能入口路径')
|
|
177
|
+
const requested = resolve(path)
|
|
178
|
+
for (const root of await roots(cwd)) {
|
|
179
|
+
if (!root.writable) continue
|
|
180
|
+
const rootPath = resolve(root.path)
|
|
181
|
+
if (!within(rootPath, requested)) continue
|
|
182
|
+
const parts = relative(rootPath, requested).split(sep)
|
|
183
|
+
const name = basename(requested)
|
|
184
|
+
const valid = (parts.length === 1 && /\.md(?:\.disabled)?$/.test(name))
|
|
185
|
+
|| (parts.length === 2 && (name === 'SKILL.md' || name === 'SKILL.md' + DISABLED))
|
|
186
|
+
if (valid) return { path: requested, root, parts }
|
|
187
|
+
throw new Error('只允许删除技能入口')
|
|
188
|
+
}
|
|
189
|
+
throw new Error('只允许删除用户级或当前工作区的 .agents/skills 技能')
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export async function readSkill(path, cwd) {
|
|
193
|
+
const claimed = await claim(path, cwd)
|
|
194
|
+
return { path: claimed.path, text: await readEntry(claimed.path) }
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export async function toggleSkill(path, enabled, cwd) {
|
|
198
|
+
const claimed = await claim(path, cwd)
|
|
199
|
+
const current = claimed.path
|
|
200
|
+
const disabled = current.endsWith(DISABLED)
|
|
201
|
+
if (enabled === !disabled) return { path: current, enabled, unchanged: true }
|
|
202
|
+
const next = enabled ? current.slice(0, -DISABLED.length) : current + DISABLED
|
|
203
|
+
try { await fs.lstat(next); throw new Error('目标文件已存在,未覆盖') } catch (error) { if (error.code !== 'ENOENT') throw error }
|
|
204
|
+
await fs.rename(current, next)
|
|
205
|
+
return { path: next, enabled, unchanged: false }
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function assertSkillName(name) {
|
|
209
|
+
if (!/^[a-z0-9][a-z0-9-_]{0,63}$/.test(name)) throw new Error('技能目录名需为 1–64 位小写字母、数字、连字符或下划线')
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export async function createSkill(input) {
|
|
213
|
+
const name = String(input.name || '').trim()
|
|
214
|
+
const description = String(input.description || '').trim()
|
|
215
|
+
const body = String(input.body || '').trim()
|
|
216
|
+
assertSkillName(name)
|
|
217
|
+
if (!description) throw new Error('请填写技能描述')
|
|
218
|
+
const scope = input.scope === 'project' ? 'project' : 'user'
|
|
219
|
+
if (scope === 'project' && (typeof input.cwd !== 'string' || !input.cwd.trim())) {
|
|
220
|
+
throw new Error('新建项目级技能需要当前工作区')
|
|
221
|
+
}
|
|
222
|
+
let root
|
|
223
|
+
if (scope === 'project') {
|
|
224
|
+
const project = resolve(input.cwd)
|
|
225
|
+
let projectReal
|
|
226
|
+
try { projectReal = await fs.realpath(project) } catch { throw new Error('当前工作区不存在') }
|
|
227
|
+
const agentsReal = await realOrSelf(join(project, '.agents'))
|
|
228
|
+
if (!within(projectReal, agentsReal)) throw new Error('项目级技能目录不能指向当前工作区外')
|
|
229
|
+
root = join(project, '.agents', 'skills')
|
|
230
|
+
await fs.mkdir(root, { recursive: true })
|
|
231
|
+
if (!within(projectReal, await realOrSelf(root))) throw new Error('项目级技能目录不能指向当前工作区外')
|
|
232
|
+
} else {
|
|
233
|
+
root = join(dshHome(), 'skills')
|
|
234
|
+
}
|
|
235
|
+
const directory = join(root, name)
|
|
236
|
+
const path = join(directory, 'SKILL.md')
|
|
237
|
+
await fs.mkdir(root, { recursive: true })
|
|
238
|
+
try { await fs.lstat(directory); throw new Error('同名技能目录已存在') } catch (error) { if (error.code !== 'ENOENT') throw error }
|
|
239
|
+
const text = `---\nname: ${JSON.stringify(name)}\ndescription: ${JSON.stringify(description)}\n---\n\n${body || '# ' + name}\n`
|
|
240
|
+
await fs.mkdir(directory)
|
|
241
|
+
try { await fs.writeFile(path, text, { encoding: 'utf8', flag: 'wx' }) } catch (error) { await fs.rm(directory, { recursive: true }); throw error }
|
|
242
|
+
return { path, name, scope }
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async function atomicWrite(path, text) {
|
|
246
|
+
if (Buffer.byteLength(text, 'utf8') > MAX_SKILL_BYTES) throw new Error('技能文件不能超过 1 MiB')
|
|
247
|
+
const backupDir = join(dshHome(), 'capability-switch', 'backups')
|
|
248
|
+
await fs.mkdir(backupDir, { recursive: true })
|
|
249
|
+
const backup = join(backupDir, `${Date.now()}-${randomUUID()}-${basename(path)}`)
|
|
250
|
+
await fs.copyFile(path, backup)
|
|
251
|
+
const temp = join(dirname(path), `.${basename(path)}.${randomUUID()}.tmp`)
|
|
252
|
+
await fs.writeFile(temp, text, 'utf8')
|
|
253
|
+
await fs.rename(temp, path)
|
|
254
|
+
return backup
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export async function updateSkill(path, text, cwd) {
|
|
258
|
+
if (typeof text !== 'string' || !text.trim()) throw new Error('SKILL.md 内容不能为空')
|
|
259
|
+
if (!/^---\r?\n[\s\S]*?\r?\n---(?:\r?\n|$)/.test(text)) throw new Error('SKILL.md 必须包含完整的 YAML frontmatter')
|
|
260
|
+
const claimed = await claim(path, cwd)
|
|
261
|
+
if (claimed.path.endsWith(DISABLED)) throw new Error('请先启用技能,再编辑入口文件')
|
|
262
|
+
const backup = await atomicWrite(claimed.path, text)
|
|
263
|
+
return { path: claimed.path, backup }
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export async function deleteSkill(path, confirm, cwd) {
|
|
267
|
+
if (confirm !== true) throw new Error('删除技能需要明确确认')
|
|
268
|
+
const claimed = await claimLexical(path, cwd)
|
|
269
|
+
const source = claimed.parts.length === 2 ? dirname(claimed.path) : claimed.path
|
|
270
|
+
const info = await fs.lstat(source)
|
|
271
|
+
const trash = join(dshHome(), 'capability-switch', 'trash')
|
|
272
|
+
await fs.mkdir(trash, { recursive: true })
|
|
273
|
+
const target = join(trash, `${Date.now()}-${randomUUID()}-${basename(source)}`)
|
|
274
|
+
if (info.isSymbolicLink()) {
|
|
275
|
+
const linkTarget = await fs.readlink(source).catch(() => '')
|
|
276
|
+
await fs.rename(source, target)
|
|
277
|
+
return { from: source, trash: target, unlinked: true, linkTarget }
|
|
278
|
+
}
|
|
279
|
+
await fs.rename(source, target)
|
|
280
|
+
return { from: source, trash: target, unlinked: false }
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
async function linksToSource(target, source) {
|
|
284
|
+
let info
|
|
285
|
+
try { info = await fs.lstat(target) } catch (error) { if (error.code === 'ENOENT') return false; throw error }
|
|
286
|
+
if (!info.isSymbolicLink()) return false
|
|
287
|
+
try {
|
|
288
|
+
const [targetReal, sourceReal] = await Promise.all([fs.realpath(target), fs.realpath(source)])
|
|
289
|
+
return targetReal === sourceReal
|
|
290
|
+
} catch { return false }
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
async function linkSkill(source, target) {
|
|
294
|
+
const info = await fs.stat(source)
|
|
295
|
+
if (!info.isDirectory() && !info.isFile()) throw new Error('技能来源必须是目录或文件')
|
|
296
|
+
const linkType = process.platform === 'win32' ? info.isDirectory() ? 'junction' : 'file' : undefined
|
|
297
|
+
await fs.symlink(resolve(source), target, linkType)
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
async function copySkill(source, target) {
|
|
301
|
+
const info = await fs.stat(source)
|
|
302
|
+
if (info.isDirectory()) {
|
|
303
|
+
await fs.cp(source, target, { recursive: true, dereference: true, force: true })
|
|
304
|
+
} else if (info.isFile()) {
|
|
305
|
+
await fs.copyFile(source, target)
|
|
306
|
+
} else {
|
|
307
|
+
throw new Error('技能来源必须是目录或文件')
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
async function fingerprintSkill(root) {
|
|
312
|
+
const hash = createHash('sha256')
|
|
313
|
+
const visited = new Set()
|
|
314
|
+
async function walk(target, rel) {
|
|
315
|
+
const info = await fs.stat(target)
|
|
316
|
+
if (info.isDirectory()) {
|
|
317
|
+
const real = await fs.realpath(target)
|
|
318
|
+
if (visited.has(real)) throw new Error('技能目录包含循环链接')
|
|
319
|
+
visited.add(real)
|
|
320
|
+
hash.update('d\0' + rel + '\0')
|
|
321
|
+
const names = (await fs.readdir(target)).sort()
|
|
322
|
+
for (const name of names) await walk(join(target, name), join(rel, name))
|
|
323
|
+
visited.delete(real)
|
|
324
|
+
} else if (info.isFile()) {
|
|
325
|
+
hash.update('f\0' + rel + '\0' + info.size + '\0')
|
|
326
|
+
for await (const chunk of createReadStream(target)) hash.update(chunk)
|
|
327
|
+
} else {
|
|
328
|
+
throw new Error('技能来源包含不支持的文件类型')
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
await walk(root, '')
|
|
332
|
+
return hash.digest('hex')
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function copyStatePath() {
|
|
336
|
+
return join(dshHome(), 'capability-switch', 'skill-sync-state.json')
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
async function readCopyState() {
|
|
340
|
+
try {
|
|
341
|
+
const parsed = JSON.parse(await fs.readFile(copyStatePath(), 'utf8'))
|
|
342
|
+
if (!parsed || parsed.version !== 1 || !parsed.copies || typeof parsed.copies !== 'object') throw new Error()
|
|
343
|
+
return parsed
|
|
344
|
+
} catch {
|
|
345
|
+
return { version: 1, copies: {} }
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
async function writeCopyState(state) {
|
|
350
|
+
const file = copyStatePath()
|
|
351
|
+
await fs.mkdir(dirname(file), { recursive: true })
|
|
352
|
+
const temporary = join(dirname(file), `.${basename(file)}.${randomUUID()}.tmp`)
|
|
353
|
+
await fs.writeFile(temporary, JSON.stringify(state, null, 2) + '\n')
|
|
354
|
+
await fs.rename(temporary, file)
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
let refreshSequence = 0
|
|
358
|
+
|
|
359
|
+
/** Force a completed skill catalog snapshot to be rebuilt after a host-owned filesystem mutation. */
|
|
360
|
+
export function invalidateSkillCatalog(ctx) {
|
|
361
|
+
const skills = ctx.get('skills')
|
|
362
|
+
if (!skills) return false
|
|
363
|
+
if (typeof skills.invalidateCache === 'function') {
|
|
364
|
+
skills.invalidateCache()
|
|
365
|
+
try { ctx.emit('plugin-manager/changed', { reason: 'plugin' }) } catch { /* client cache clear is best effort */ }
|
|
366
|
+
return true
|
|
367
|
+
}
|
|
368
|
+
if (typeof skills.registerProvider === 'function') {
|
|
369
|
+
try {
|
|
370
|
+
const dispose = skills.registerProvider(() => ({
|
|
371
|
+
name: `capability-switch-refresh-${++refreshSequence}`,
|
|
372
|
+
list: async () => [],
|
|
373
|
+
get: async () => undefined,
|
|
374
|
+
}))
|
|
375
|
+
dispose()
|
|
376
|
+
return true
|
|
377
|
+
} catch { /* fall through to the public notification */ }
|
|
378
|
+
}
|
|
379
|
+
try { ctx.emit('skills/change') } catch { /* best effort after a disposed plugin */ }
|
|
380
|
+
return false
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const HIDDEN_SKILL_SOURCES = new Set(['user-agents'])
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Hide only the global Agent directory from every catalog/load consumer.
|
|
387
|
+
* Project-level DSH and Agent skills stay visible in every Agent preset, and
|
|
388
|
+
* the global Agent directory remains available to sync previews.
|
|
389
|
+
*/
|
|
390
|
+
export function installSkillTakeover(ctx) {
|
|
391
|
+
const service = ctx.get('skills')
|
|
392
|
+
if (!service || typeof service.list !== 'function' || typeof service.snapshot !== 'function' || typeof service.get !== 'function') {
|
|
393
|
+
return () => {}
|
|
394
|
+
}
|
|
395
|
+
const original = { list: service.list, snapshot: service.snapshot, get: service.get }
|
|
396
|
+
const own = Object.fromEntries(Object.keys(original).map(key => [key, Object.hasOwn(service, key)]))
|
|
397
|
+
const visible = skill => !HIDDEN_SKILL_SOURCES.has(skill.source)
|
|
398
|
+
const list = async function (...args) {
|
|
399
|
+
return (await original.list.apply(this, args)).filter(visible)
|
|
400
|
+
}
|
|
401
|
+
const snapshot = async function (...args) {
|
|
402
|
+
const result = await original.snapshot.apply(this, args)
|
|
403
|
+
return { ...result, skills: result.skills.filter(visible) }
|
|
404
|
+
}
|
|
405
|
+
const get = async function (name, ...args) {
|
|
406
|
+
const result = await original.snapshot.apply(this, args)
|
|
407
|
+
if (!result.skills.some(skill => skill.name === name && visible(skill))) return undefined
|
|
408
|
+
return await original.get.apply(this, [name, ...args])
|
|
409
|
+
}
|
|
410
|
+
Object.assign(service, { list, snapshot, get })
|
|
411
|
+
return () => {
|
|
412
|
+
for (const [key, method] of Object.entries(original)) {
|
|
413
|
+
if (service[key] !== ({ list, snapshot, get })[key]) continue
|
|
414
|
+
if (own[key]) service[key] = method
|
|
415
|
+
else delete service[key]
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
export async function previewAgentSkillSync(mode = 'symlink') {
|
|
421
|
+
if (mode !== 'symlink' && mode !== 'copy') throw new Error('同步模式必须是 symlink 或 copy')
|
|
422
|
+
const allRoots = await roots()
|
|
423
|
+
const sourceRoot = allRoots.find(root => root.source === 'user-agents')
|
|
424
|
+
const targetRoot = allRoots.find(root => root.source === 'user-dsh')
|
|
425
|
+
const sourceRows = (await scanRoot(sourceRoot)).filter(row => row.form !== 'invalid')
|
|
426
|
+
const state = mode === 'copy' ? await readCopyState() : null
|
|
427
|
+
const items = []
|
|
428
|
+
for (const row of sourceRows) {
|
|
429
|
+
const source = row.form === 'file' ? row.path : row.directory
|
|
430
|
+
const name = relative(sourceRoot.path, source)
|
|
431
|
+
if (!name || name.startsWith('..') || name.includes(sep)) throw new Error('技能同步仅支持源目录的直接子项')
|
|
432
|
+
const target = join(targetRoot.path, name)
|
|
433
|
+
try {
|
|
434
|
+
let action = 'import'
|
|
435
|
+
let info
|
|
436
|
+
try { info = await fs.lstat(target) } catch (error) { if (error.code !== 'ENOENT') throw error }
|
|
437
|
+
if (mode === 'symlink') {
|
|
438
|
+
if (await linksToSource(target, source)) action = 'skip'
|
|
439
|
+
else if (info) action = 'update'
|
|
440
|
+
} else if (info) {
|
|
441
|
+
const sourceHash = await fingerprintSkill(source)
|
|
442
|
+
if (info.isSymbolicLink()) {
|
|
443
|
+
action = 'update'
|
|
444
|
+
} else {
|
|
445
|
+
const targetHash = await fingerprintSkill(target)
|
|
446
|
+
if (sourceHash === targetHash) {
|
|
447
|
+
action = 'skip'
|
|
448
|
+
} else {
|
|
449
|
+
const baseline = state.copies[name]
|
|
450
|
+
if (!baseline) action = 'conflict'
|
|
451
|
+
else if (targetHash === baseline.targetHash) action = 'update'
|
|
452
|
+
else if (sourceHash === baseline.sourceHash) action = 'local-edit'
|
|
453
|
+
else action = 'conflict'
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
items.push({ name, action, source, target })
|
|
458
|
+
} catch (error) {
|
|
459
|
+
items.push({ name, action: 'error', error: error?.message || String(error), source, target })
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
return {
|
|
463
|
+
items, mode, sourceRoot: sourceRoot.path, targetRoot: targetRoot.path,
|
|
464
|
+
backupRoot: join(dshHome(), 'capability-switch', 'skill-sync-backups'),
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/** Execute reviewed items by creating DSH symlinks or independent copies, reporting every outcome. */
|
|
469
|
+
export async function syncAgentSkills(plan, names, mode = 'symlink') {
|
|
470
|
+
if (mode !== 'symlink' && mode !== 'copy') throw new Error('同步模式必须是 symlink 或 copy')
|
|
471
|
+
plan ||= await previewAgentSkillSync(mode)
|
|
472
|
+
if (names !== undefined && !Array.isArray(names)) throw new Error('names 必须是技能名称数组')
|
|
473
|
+
const selected = names === undefined ? null : new Set(names.filter(name => typeof name === 'string'))
|
|
474
|
+
const copyState = await readCopyState()
|
|
475
|
+
let stateDirty = false
|
|
476
|
+
const result = {
|
|
477
|
+
imported: [], updated: [], skipped: [], failed: [], mode,
|
|
478
|
+
sourceRoot: plan.sourceRoot, targetRoot: plan.targetRoot, backupRoot: plan.backupRoot, backupDir: null,
|
|
479
|
+
}
|
|
480
|
+
await fs.mkdir(plan.targetRoot, { recursive: true })
|
|
481
|
+
for (const item of plan.items) {
|
|
482
|
+
if (item.action === 'error') { result.failed.push({ name: item.name, error: item.error }); continue }
|
|
483
|
+
if (item.action === 'skip') { result.skipped.push(item.name); continue }
|
|
484
|
+
if (selected === null && (item.action === 'local-edit' || item.action === 'conflict')) {
|
|
485
|
+
result.skipped.push(item.name)
|
|
486
|
+
continue
|
|
487
|
+
}
|
|
488
|
+
if (selected !== null && !selected.has(item.name)) continue
|
|
489
|
+
let backup = null
|
|
490
|
+
const temporary = join(plan.targetRoot, `.capability-switch-${randomUUID()}.tmp`)
|
|
491
|
+
try {
|
|
492
|
+
if (item.action === 'update' || item.action === 'local-edit' || item.action === 'conflict') {
|
|
493
|
+
result.backupDir ??= join(plan.backupRoot, `${Date.now()}-${randomUUID()}`)
|
|
494
|
+
backup = join(result.backupDir, item.name)
|
|
495
|
+
await fs.mkdir(dirname(backup), { recursive: true })
|
|
496
|
+
await fs.rename(item.target, backup)
|
|
497
|
+
}
|
|
498
|
+
if (mode === 'copy') await copySkill(item.source, temporary)
|
|
499
|
+
else await linkSkill(item.source, temporary)
|
|
500
|
+
await fs.rename(temporary, item.target)
|
|
501
|
+
if (mode === 'copy') {
|
|
502
|
+
copyState.copies[item.name] = {
|
|
503
|
+
sourceHash: await fingerprintSkill(item.source),
|
|
504
|
+
targetHash: await fingerprintSkill(item.target),
|
|
505
|
+
syncedAt: new Date().toISOString(),
|
|
506
|
+
}
|
|
507
|
+
stateDirty = true
|
|
508
|
+
} else if (copyState.copies[item.name]) {
|
|
509
|
+
delete copyState.copies[item.name]
|
|
510
|
+
stateDirty = true
|
|
511
|
+
}
|
|
512
|
+
if (item.action === 'import') result.imported.push(item.name)
|
|
513
|
+
else result.updated.push(item.name)
|
|
514
|
+
} catch (error) {
|
|
515
|
+
await fs.rm(temporary, { recursive: true, force: true })
|
|
516
|
+
if (backup) {
|
|
517
|
+
try { await fs.rename(backup, item.target) } catch (restoreError) { error.message += ';恢复原文件失败:' + restoreError.message }
|
|
518
|
+
}
|
|
519
|
+
result.failed.push({ name: item.name, error: error.message })
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
if (stateDirty) {
|
|
523
|
+
try { await writeCopyState(copyState) } catch (error) { result.stateError = error?.message || String(error) }
|
|
524
|
+
}
|
|
525
|
+
return result
|
|
526
|
+
}
|
package/locale/en.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "Skills & MCP capabilities",
|
|
4
|
+
"description": "Manage Skills and MCP servers with a CC Switch-inspired unified list from the Capabilities sidebar page or Settings → Capabilities. Search, create, edit, toggle, delete, and test MCP connections. Skills writes are limited to user-level roots and project entries are read-only; MCP changes affect the active DSH profile. Display settings customize the sidebar entry, default tab, descriptions, and row density and persist in the DSH plugin configuration."
|
|
5
|
+
}
|
|
6
|
+
}
|
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-capability-switch",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.4.9",
|
|
4
|
+
"description": "A DSH sidebar plugin for managing Skills and MCP servers.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "lock-down",
|
|
7
|
+
"homepage": "https://github.com/lock-down/dsh-capability-switch#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/lock-down/dsh-capability-switch.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/lock-down/dsh-capability-switch/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"dsh",
|
|
17
|
+
"deepseek-harness",
|
|
18
|
+
"skills",
|
|
19
|
+
"mcp",
|
|
20
|
+
"plugin"
|
|
21
|
+
],
|
|
22
|
+
"type": "module",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": "./lib/index.js",
|
|
25
|
+
"./client": "./lib/client.js",
|
|
26
|
+
"./locale/*.json": "./locale/*.json",
|
|
27
|
+
"./icon": "./icon.svg"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"lib",
|
|
31
|
+
"cordis.patch.yml",
|
|
32
|
+
"locale/*.json",
|
|
33
|
+
"docs",
|
|
34
|
+
"icon.svg",
|
|
35
|
+
"README.md",
|
|
36
|
+
"CHANGELOG.md",
|
|
37
|
+
"LICENSE"
|
|
38
|
+
],
|
|
39
|
+
"scripts": {
|
|
40
|
+
"test": "node test/run.mjs"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"smol-toml": "^1.9.1",
|
|
44
|
+
"yaml": "^2.9.1"
|
|
45
|
+
},
|
|
46
|
+
"engines": {
|
|
47
|
+
"dsh": ">=0.2.2-rc.3 <0.3.0"
|
|
48
|
+
},
|
|
49
|
+
"dsh": {
|
|
50
|
+
"bundle": {
|
|
51
|
+
"patch": "./cordis.patch.yml"
|
|
52
|
+
},
|
|
53
|
+
"client": {
|
|
54
|
+
"platform": "web",
|
|
55
|
+
"immediately": true,
|
|
56
|
+
"inject": [
|
|
57
|
+
"@deepseek-ai/dsh-client-ui-layout",
|
|
58
|
+
"@deepseek-ai/dsh-client-ui-sidebar",
|
|
59
|
+
"@deepseek-ai/dsh-client-locale"
|
|
60
|
+
]
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|