@jidekaixin2dian/dsh-toolgate 0.0.0-stage → 0.1.6
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 +76 -0
- package/LICENSE +21 -0
- package/README.en.md +83 -0
- package/README.md +85 -3
- package/README.zh.md +6 -0
- package/SECURITY.md +16 -0
- package/assets/readme-schema-v2.svg +18 -0
- package/cordis.patch.yml +6 -0
- package/icon.svg +10 -0
- package/lib/api-payload.js +10 -0
- package/lib/audit.js +25 -0
- package/lib/client.js +536 -0
- package/lib/config-files.js +73 -0
- package/lib/groups.js +59 -0
- package/lib/host-paths.js +39 -0
- package/lib/index.js +501 -0
- package/lib/mcp-config.js +239 -0
- package/lib/skills-config.js +86 -0
- package/lib/skills-runtime.js +139 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +103 -6
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
// Parse nodes for identity; print only changed top-level patches. Other source
|
|
2
|
+
// ranges remain intact, including comments, unknown fields and !!js scalars.
|
|
3
|
+
import os from 'node:os'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
import { Document, isAlias, isMap, isSeq, parseDocument, visit } from 'yaml'
|
|
6
|
+
import { commitFiles, readFile } from './config-files.js'
|
|
7
|
+
|
|
8
|
+
const MCP = '@deepseek-ai/dsh-mcp-client'
|
|
9
|
+
const yamlOptions = { keepSourceTokens: true, customTags: [{ tag: 'tag:yaml.org,2002:js', resolve: value => value }] }
|
|
10
|
+
|
|
11
|
+
export function paths(options = {}) {
|
|
12
|
+
const profileDir = options.profileDir || process.env.DSH_TOOLGATE_PROFILE_DIR
|
|
13
|
+
if (!profileDir || !path.isAbsolute(profileDir)) throw new Error('宿主未提供当前 profile,拒绝猜测配置目录')
|
|
14
|
+
const home = options.home || process.env.DSH_TOOLGATE_HOME || process.env.DSH_HOME || path.join(os.homedir(), '.dsh')
|
|
15
|
+
return { home: path.resolve(home), profileDir, project: options.profilePatch || path.join(profileDir, 'cordis.patch.yml'), global: path.join(home, 'cordis.patch.yml') }
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function parse(content, file = 'cordis.patch.yml') {
|
|
19
|
+
const doc = parseDocument(content ?? '[]\n', yamlOptions)
|
|
20
|
+
if (doc.errors.length || !isSeq(doc.contents)) throw new Error('无法安全编辑 YAML 配置:' + file)
|
|
21
|
+
doc.toJS() // reject unresolved/excessive aliases without evaluating !!js
|
|
22
|
+
return doc
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function dynamic(node) {
|
|
26
|
+
let result = false
|
|
27
|
+
visit(node, (_key, value) => { if (isAlias(value) || value?.tag || (isMap(value) && value.has('<<'))) result = true })
|
|
28
|
+
return result
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function definitions(doc, level) {
|
|
32
|
+
const refs = []
|
|
33
|
+
const entries = (seq, top) => {
|
|
34
|
+
if (!isSeq(seq)) return
|
|
35
|
+
for (const node of seq.items) {
|
|
36
|
+
if (!isMap(node)) continue
|
|
37
|
+
refs.push({ node, seq, top, level, id: node.get('id') })
|
|
38
|
+
if (node.get('group') === true) entries(node.get('config', true), top)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
for (const top of doc.contents.items) if (isMap(top)) entries(top.get('insert', true), top)
|
|
42
|
+
return refs
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Standalone helper, oracle-tested against the public host function. The
|
|
46
|
+
// running plugin supplies applyEntryPatches itself as the composer.
|
|
47
|
+
export function composePatches(initial, patches) {
|
|
48
|
+
const rows = structuredClone(initial), ids = new Map()
|
|
49
|
+
const index = items => { for (const row of items) { if (row.id) ids.set(row.id, row); if (row.group && Array.isArray(row.config)) index(row.config) } }
|
|
50
|
+
index(rows)
|
|
51
|
+
for (const patch of structuredClone(patches)) {
|
|
52
|
+
if (patch.insert) {
|
|
53
|
+
const target = patch.id ? ids.get(patch.id) : undefined
|
|
54
|
+
if (patch.id && !target?.group) continue
|
|
55
|
+
if (target) { if (!Array.isArray(target.config)) target.config = []; target.config.push(...patch.insert) }
|
|
56
|
+
else rows.push(...patch.insert)
|
|
57
|
+
index(patch.insert)
|
|
58
|
+
} else {
|
|
59
|
+
const target = ids.get(patch.id)
|
|
60
|
+
if (!target || (patch.name && patch.name !== target.name)) continue
|
|
61
|
+
for (const [key, value] of Object.entries(patch)) if (!['id', 'name'].includes(key)) target[key] = value
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return rows
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const flatten = rows => rows.flatMap(row => [row, ...(row.group && Array.isArray(row.config) ? flatten(row.config) : [])])
|
|
68
|
+
|
|
69
|
+
export function parseServers(content, level) {
|
|
70
|
+
const doc = parse(content)
|
|
71
|
+
return flatten(composePatches([], doc.toJS())).filter(row => row.name === MCP).map(row => ({
|
|
72
|
+
...row, level, disabled: !!row.disabled,
|
|
73
|
+
managed: content.split(/\r?\n/).some(line => line === '# dsh-toolgate:server:' + row.id || line === '# dsh-context-budget:server:' + row.id),
|
|
74
|
+
}))
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function renderChanged(before, changed, doc) {
|
|
78
|
+
if (!changed.size) return before
|
|
79
|
+
if (doc.contents.flow) {
|
|
80
|
+
doc.contents.items = doc.contents.items.filter(node => !changed.has(node) || changed.get(node) !== null)
|
|
81
|
+
const result = doc.toString({ lineWidth: 0 })
|
|
82
|
+
parse(result)
|
|
83
|
+
return result
|
|
84
|
+
}
|
|
85
|
+
const text = before ?? '[]\n', edits = []
|
|
86
|
+
for (const [original, replacement] of changed) {
|
|
87
|
+
const start = text.lastIndexOf('\n', original.range[0]) + 1, end = original.range[2]
|
|
88
|
+
let value = ''
|
|
89
|
+
if (replacement) {
|
|
90
|
+
const copy = replacement.clone()
|
|
91
|
+
copy.commentBefore = undefined // preceding comments are outside this range
|
|
92
|
+
value = new Document([copy]).toString({ lineWidth: 0 })
|
|
93
|
+
if (text.includes('\r\n')) value = value.replaceAll('\n', '\r\n')
|
|
94
|
+
}
|
|
95
|
+
edits.push({ start, end, value })
|
|
96
|
+
}
|
|
97
|
+
let result = text
|
|
98
|
+
for (const edit of edits.sort((a, b) => b.start - a.start)) result = result.slice(0, edit.start) + edit.value + result.slice(edit.end)
|
|
99
|
+
if (parseDocument(result, yamlOptions).contents === null) result += '[]\n'
|
|
100
|
+
parse(result)
|
|
101
|
+
return result
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function append(content, row) {
|
|
105
|
+
const doc = parse(content)
|
|
106
|
+
if (doc.contents.items.length === 0) { doc.contents.flow = false; doc.contents.items.push(doc.createNode(row)); return doc.toString({ lineWidth: 0 }) }
|
|
107
|
+
return content.replace(/\s*$/, '\n') + new Document([row]).toString({ lineWidth: 0 })
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function normalizeRow(input) {
|
|
111
|
+
const serverName = String(input.serverName || '').trim(), id = String(input.id || serverName).trim()
|
|
112
|
+
if (!/^[A-Za-z0-9_-]{1,32}$/.test(serverName)) throw new Error('serverName 必须是 1–32 位字母、数字、_ 或 -')
|
|
113
|
+
if (!/^[A-Za-z0-9_.:@%+=/-]{1,80}$/.test(id)) throw new Error('id 含非法字符')
|
|
114
|
+
const transport = input.transport || 'stdio'
|
|
115
|
+
if (!['stdio', 'streamable-http'].includes(transport)) throw new Error('transport 必须是 stdio 或 streamable-http')
|
|
116
|
+
const config = { serverName, transport }
|
|
117
|
+
const map = value => {
|
|
118
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
119
|
+
if (Object.values(value).some(item => typeof item !== 'string')) throw new Error('env/headers 的值必须是字符串')
|
|
120
|
+
return { ...value }
|
|
121
|
+
}
|
|
122
|
+
return Object.fromEntries(String(value || '').split('\n').filter(Boolean).map(line => {
|
|
123
|
+
const at = line.indexOf('=')
|
|
124
|
+
if (at <= 0) throw new Error('env/headers 每行必须为 KEY=VALUE')
|
|
125
|
+
return [line.slice(0, at).trim(), line.slice(at + 1)]
|
|
126
|
+
}))
|
|
127
|
+
}
|
|
128
|
+
if (transport === 'stdio') {
|
|
129
|
+
config.command = String(input.command || '').trim()
|
|
130
|
+
if (!config.command) throw new Error('command 不能为空')
|
|
131
|
+
config.args = Array.isArray(input.args) ? input.args.map(String) : String(input.args || '').split('\n').filter(Boolean)
|
|
132
|
+
if (Object.hasOwn(input, 'env')) config.env = map(input.env)
|
|
133
|
+
} else {
|
|
134
|
+
config.url = String(input.url || '').trim()
|
|
135
|
+
if (!/^https?:$/.test(new URL(config.url).protocol)) throw new Error('url 必须为 HTTP(S) 地址')
|
|
136
|
+
if (Object.hasOwn(input, 'headers')) config.headers = map(input.headers)
|
|
137
|
+
}
|
|
138
|
+
if (input.toolCallTimeoutMs !== undefined && input.toolCallTimeoutMs !== '') {
|
|
139
|
+
const timeout = Number(input.toolCallTimeoutMs)
|
|
140
|
+
if (!Number.isSafeInteger(timeout) || timeout < 0) throw new Error('toolCallTimeoutMs 必须为非负整数')
|
|
141
|
+
if (timeout) config.toolCallTimeoutMs = timeout
|
|
142
|
+
}
|
|
143
|
+
return { id, config, level: input.level === 'global' ? 'global' : 'project' }
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function createMcpConfig(options = {}) {
|
|
147
|
+
const compose = options.compose || composePatches
|
|
148
|
+
const read = () => {
|
|
149
|
+
const p = paths(options)
|
|
150
|
+
if (path.resolve(p.project) === path.resolve(p.global)) throw new Error('profile 和 global 配置不能是同一文件')
|
|
151
|
+
const files = ['project', 'global'].map(level => {
|
|
152
|
+
const before = readFile(p[level]), doc = parse(before, p[level])
|
|
153
|
+
return { level, file: p[level], before, doc, changed: new Map() }
|
|
154
|
+
})
|
|
155
|
+
const refs = files.flatMap(file => definitions(file.doc, file.level).map(ref => ({ ...ref, file })))
|
|
156
|
+
const base = options.basePatches?.() || []
|
|
157
|
+
const patches = [...base, ...files.flatMap(file => file.doc.toJS()), ...(options.overlays || [])]
|
|
158
|
+
return { p, files, refs, effective: flatten(compose([], patches, () => {})) }
|
|
159
|
+
}
|
|
160
|
+
const listing = snapshot => {
|
|
161
|
+
const s = snapshot || read()
|
|
162
|
+
const rows = s.effective.filter(row => row.name === MCP).map(row => {
|
|
163
|
+
const refs = s.refs.filter(ref => ref.id === row.id), ref = refs.length === 1 ? refs[0] : undefined
|
|
164
|
+
const editable = !!ref && s.effective.filter(entry => entry.id === row.id).length === 1 && !dynamic(ref.node)
|
|
165
|
+
return { ...row, disabled: !!row.disabled, config: row.config || {}, level: ref?.level || 'bundle', managed: !!ref,
|
|
166
|
+
editable, reason: editable ? '' : '配置来自 bundle/overlay、动态 YAML 或重复 id,不能安全编辑' }
|
|
167
|
+
})
|
|
168
|
+
return { rows, paths: { project: s.p.project, global: s.p.global } }
|
|
169
|
+
}
|
|
170
|
+
const locate = (s, id) => {
|
|
171
|
+
const refs = s.refs.filter(ref => ref.id === id)
|
|
172
|
+
if (refs.length !== 1 || s.effective.filter(entry => entry.id === id).length !== 1 || refs[0].node.get('name') !== MCP || dynamic(refs[0].node)) throw new Error('配置目标不唯一或不可安全编辑,请在原配置中处理')
|
|
173
|
+
return refs[0]
|
|
174
|
+
}
|
|
175
|
+
const commit = s => {
|
|
176
|
+
const changes = s.files.map(file => ({ file: file.file, before: file.before, after: file.after ?? renderChanged(file.before, file.changed, file.doc) }))
|
|
177
|
+
for (const change of changes) parse(change.after, change.file)
|
|
178
|
+
const backups = commitFiles(changes, s.p.home)
|
|
179
|
+
return { ...listing(), backups }
|
|
180
|
+
}
|
|
181
|
+
return {
|
|
182
|
+
listServers: () => listing(),
|
|
183
|
+
addServer(input) {
|
|
184
|
+
const row = normalizeRow(input), s = read()
|
|
185
|
+
if (s.effective.some(existing => existing.id === row.id || (existing.name === MCP && existing.config?.serverName === row.config.serverName))) throw new Error('id 或 serverName 已存在')
|
|
186
|
+
const file = s.files.find(file => file.level === row.level)
|
|
187
|
+
file.after = append(file.before, { insert: [{ id: row.id, name: MCP, config: row.config }] })
|
|
188
|
+
return commit(s)
|
|
189
|
+
},
|
|
190
|
+
editServer(input) {
|
|
191
|
+
const row = normalizeRow(input), s = read(), ref = locate(s, row.id)
|
|
192
|
+
if (row.level !== ref.level) throw new Error('编辑不能隐式移动配置层;请在原层保存')
|
|
193
|
+
if (s.effective.some(existing => existing.id !== row.id && existing.name === MCP && existing.config?.serverName === row.config.serverName)) throw new Error('serverName 已存在')
|
|
194
|
+
const overridden = s.files.flatMap(file => file.doc.contents.items).some(node => isMap(node) && !node.has('insert') && node.get('id') === row.id && (!node.get('name') || node.get('name') === MCP) && node.has('config'))
|
|
195
|
+
if (overridden || (options.overlays || []).some(patch => patch.id === row.id && patch.config)) throw new Error('存在 config 覆盖,请在覆盖来源编辑,避免丢失原有字段')
|
|
196
|
+
let config = ref.node.get('config', true)
|
|
197
|
+
if (!isMap(config)) { config = ref.file.doc.createNode({}); ref.node.set('config', config) }
|
|
198
|
+
for (const [key, value] of Object.entries(row.config)) config.set(key, value)
|
|
199
|
+
for (const key of row.config.transport === 'stdio' ? ['url', 'headers'] : ['command', 'args', 'env']) config.delete(key)
|
|
200
|
+
if (!row.config.toolCallTimeoutMs) config.delete('toolCallTimeoutMs')
|
|
201
|
+
ref.file.changed.set(ref.top, ref.top)
|
|
202
|
+
return commit(s)
|
|
203
|
+
},
|
|
204
|
+
removeServer(id) {
|
|
205
|
+
if (!id) throw new Error('id 不能为空')
|
|
206
|
+
const s = read(), ref = locate(s, String(id))
|
|
207
|
+
ref.seq.items.splice(ref.seq.items.indexOf(ref.node), 1)
|
|
208
|
+
ref.file.changed.set(ref.top, ref.seq.items.length || ref.top.items.length > 1 ? ref.top : null)
|
|
209
|
+
for (const file of s.files) for (const node of file.doc.contents.items) {
|
|
210
|
+
if (isMap(node) && !node.has('insert') && node.get('id') === id && (!node.get('name') || node.get('name') === MCP)) file.changed.set(node, null)
|
|
211
|
+
}
|
|
212
|
+
return commit(s)
|
|
213
|
+
},
|
|
214
|
+
setServerEnabled(id, enabled) {
|
|
215
|
+
const s = read()
|
|
216
|
+
locate(s, String(id))
|
|
217
|
+
if ((options.overlays || []).some(patch => patch.id === id && Object.hasOwn(patch, 'disabled'))) throw new Error('启动参数覆盖了启停状态,请先移除该覆盖')
|
|
218
|
+
const file = s.files.at(-1)
|
|
219
|
+
const last = file.doc.contents.items.findLast(node => isMap(node) && !node.has('insert') && node.get('id') === id && (!node.get('name') || node.get('name') === MCP) && node.has('disabled'))
|
|
220
|
+
if (last && !dynamic(last)) { last.set('disabled', !enabled); file.changed.set(last, last) }
|
|
221
|
+
else file.after = append(file.before, { id, name: MCP, disabled: !enabled })
|
|
222
|
+
return commit(s)
|
|
223
|
+
},
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export const listServers = () => createMcpConfig().listServers()
|
|
228
|
+
export const addServer = input => createMcpConfig().addServer(input)
|
|
229
|
+
export const editServer = input => createMcpConfig().editServer(input)
|
|
230
|
+
export const removeServer = id => createMcpConfig().removeServer(id)
|
|
231
|
+
export const setServerEnabled = (id, enabled) => createMcpConfig().setServerEnabled(id, enabled)
|
|
232
|
+
|
|
233
|
+
export function publicServers(result) {
|
|
234
|
+
return { ...result, rows: result.rows.map(row => {
|
|
235
|
+
const { env, headers } = row.config
|
|
236
|
+
const config = Object.fromEntries(['serverName', 'transport', 'url', 'command', 'args', 'toolCallTimeoutMs'].filter(key => Object.hasOwn(row.config, key)).map(key => [key, row.config[key]]))
|
|
237
|
+
return { ...Object.fromEntries(['id', 'name', 'disabled', 'level', 'managed', 'editable', 'reason'].map(key => [key, row[key]])), config, envKeys: Object.keys(env || {}), headerKeys: Object.keys(headers || {}) }
|
|
238
|
+
}) }
|
|
239
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// Skill file create/delete for the two writable skill roots:
|
|
2
|
+
// ~/.dsh/skills (user)
|
|
3
|
+
// <cwd>/.dsh/skills (project)
|
|
4
|
+
// Deletion moves the directory into <root>/.trash, never hard-deletes.
|
|
5
|
+
import fs from 'node:fs'
|
|
6
|
+
import os from 'node:os'
|
|
7
|
+
import path from 'node:path'
|
|
8
|
+
import { randomUUID } from 'node:crypto'
|
|
9
|
+
|
|
10
|
+
const NAME_RE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/
|
|
11
|
+
|
|
12
|
+
export function roots(location = {}) {
|
|
13
|
+
return {
|
|
14
|
+
user: path.join(location.home || process.env.DSH_HOME || path.join(os.homedir(), '.dsh'), 'skills'),
|
|
15
|
+
project: Object.hasOwn(location, 'cwd') && !location.cwd ? null : path.join(location.cwd || process.cwd(), '.dsh', 'skills'),
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function rootFor(scope, location) {
|
|
20
|
+
const r = roots(location)
|
|
21
|
+
const key = scope === 'project' ? 'project' : 'user'
|
|
22
|
+
if (!r[key]) throw new Error('请先选择有工作目录的会话,再操作项目 Skill')
|
|
23
|
+
return { key, dir: r[key] }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function safeDirectory(root, name) {
|
|
27
|
+
const target = path.join(root, name)
|
|
28
|
+
const canonicalRoot = fs.realpathSync(root)
|
|
29
|
+
if (fs.lstatSync(target).isSymbolicLink() || fs.realpathSync(target) !== path.join(canonicalRoot, name)) throw new Error('拒绝操作指向 Skill 根目录外的链接')
|
|
30
|
+
return target
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function canDeleteSkill(skill, location) {
|
|
34
|
+
if (!['user-dsh', 'project-dsh'].includes(skill.source) || !skill.path || !NAME_RE.test(skill.name)) return false
|
|
35
|
+
try {
|
|
36
|
+
const { dir } = rootFor(skill.source === 'project-dsh' ? 'project' : 'user', location)
|
|
37
|
+
const expected = path.join(dir, skill.name, 'SKILL.md')
|
|
38
|
+
if (path.resolve(skill.path) !== path.resolve(expected)) return false
|
|
39
|
+
safeDirectory(dir, skill.name)
|
|
40
|
+
return fs.lstatSync(expected).isFile() && !fs.lstatSync(expected).isSymbolicLink()
|
|
41
|
+
} catch { return false }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function createSkill(input, location) {
|
|
45
|
+
const name = String(input?.name || '').trim()
|
|
46
|
+
const description = String(input?.description || '').trim()
|
|
47
|
+
const body = String(input?.body || '').replace(/\r\n/g, '\n').trim()
|
|
48
|
+
if (!NAME_RE.test(name)) throw new Error('Skill 名必须是 kebab-case(小写字母/数字/-)')
|
|
49
|
+
if (!description) throw new Error('description 不能为空')
|
|
50
|
+
const { key, dir } = rootFor(input?.scope, location)
|
|
51
|
+
const target = path.join(dir, name)
|
|
52
|
+
if (fs.existsSync(target)) throw new Error('Skill 已存在: ' + name)
|
|
53
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
54
|
+
fs.mkdirSync(target)
|
|
55
|
+
safeDirectory(dir, name)
|
|
56
|
+
const content = [
|
|
57
|
+
'---',
|
|
58
|
+
'name: ' + name,
|
|
59
|
+
'description: ' + JSON.stringify(description),
|
|
60
|
+
'---',
|
|
61
|
+
'',
|
|
62
|
+
body,
|
|
63
|
+
'',
|
|
64
|
+
].join('\n')
|
|
65
|
+
fs.writeFileSync(path.join(target, 'SKILL.md'), content, { encoding: 'utf8', flag: 'wx', mode: 0o600 })
|
|
66
|
+
return { name, scope: key, path: path.join(target, 'SKILL.md') }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function deleteSkill(input, location) {
|
|
70
|
+
const name = String(input?.name || '').trim()
|
|
71
|
+
if (!NAME_RE.test(name)) throw new Error('Skill 名非法')
|
|
72
|
+
const scope = input?.scope === 'project' ? 'project' : 'user'
|
|
73
|
+
const { dir } = rootFor(scope, location)
|
|
74
|
+
const target = path.join(dir, name)
|
|
75
|
+
if (!fs.existsSync(target)) throw new Error('Skill 不存在于 ' + dir + ': ' + name)
|
|
76
|
+
safeDirectory(dir, name)
|
|
77
|
+
if (input.path && path.resolve(input.path) !== path.resolve(path.join(target, 'SKILL.md'))) throw new Error('Skill 来源路径已变化,请刷新')
|
|
78
|
+
const trash = path.join(dir, '.trash')
|
|
79
|
+
fs.mkdirSync(trash, { recursive: true })
|
|
80
|
+
if (fs.lstatSync(trash).isSymbolicLink() || fs.realpathSync(trash) !== path.join(fs.realpathSync(dir), '.trash')) throw new Error('回收站不能是外部链接')
|
|
81
|
+
const stamp = new Date().toISOString().replace(/[:.]/g, '-')
|
|
82
|
+
const dest = path.join(trash, stamp + '-' + randomUUID() + '-' + name)
|
|
83
|
+
fs.renameSync(target, dest)
|
|
84
|
+
return { name, scope, movedTo: dest }
|
|
85
|
+
}
|
|
86
|
+
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { canDeleteSkill, createSkill, deleteSkill } from './skills-config.js'
|
|
2
|
+
import { projectRoot, skillView } from './host-paths.js'
|
|
3
|
+
|
|
4
|
+
const OVERRIDE = 'dsh-toolgate-override'
|
|
5
|
+
const identity = skill => JSON.stringify([skill.name, skill.provider, skill.path || '', skill.source])
|
|
6
|
+
|
|
7
|
+
// Discovery remains the registry's job. The provider returns only previously
|
|
8
|
+
// resolved definitions, so it never recursively calls skills.get() from list().
|
|
9
|
+
export function installSkills(ctx, state, locations, save) {
|
|
10
|
+
let control, disposed = false
|
|
11
|
+
const definitions = new Map()
|
|
12
|
+
const optionsFor = view => ({ ...(view.cwd ? { cwd: view.cwd } : {}), ...(view.scope ? { scope: view.scope } : {}) })
|
|
13
|
+
const snapshot = async view => {
|
|
14
|
+
const options = optionsFor(view)
|
|
15
|
+
if (typeof ctx.skills.snapshot === 'function') return ctx.skills.snapshot(options)
|
|
16
|
+
return { skills: await ctx.skills.list(options), complete: true }
|
|
17
|
+
}
|
|
18
|
+
const applies = (rule, options) => !rule.cwd || rule.cwd === projectRoot(options?.cwd)
|
|
19
|
+
const provider = {
|
|
20
|
+
name: OVERRIDE,
|
|
21
|
+
list(options) {
|
|
22
|
+
return [...definitions.values()].filter(({ rule }) => applies(rule, options)).map(({ def, rule }) => ({
|
|
23
|
+
...def, provider: OVERRIDE, rank: 0, locator: rule.key,
|
|
24
|
+
}))
|
|
25
|
+
},
|
|
26
|
+
get(candidate) { return definitions.get(candidate.locator)?.def },
|
|
27
|
+
}
|
|
28
|
+
ctx.skills.registerProvider(value => { control = value; return provider })
|
|
29
|
+
const resolve = async (rule, view) => {
|
|
30
|
+
const def = await ctx.skills.get(rule.name, optionsFor(view))
|
|
31
|
+
if (disposed || !def || identity(def) !== rule.identity) return false
|
|
32
|
+
definitions.set(rule.key, { rule, def: { ...def, provider: OVERRIDE, rank: 0, invocation: { modelInvocable: false, userInvocable: false } } })
|
|
33
|
+
return true
|
|
34
|
+
}
|
|
35
|
+
const ready = (async () => {
|
|
36
|
+
for (const rule of state.skillRules) {
|
|
37
|
+
try { await resolve(rule, { cwd: rule.cwd || locations.workspace }) } catch { /* unavailable provider stays unresolved */ }
|
|
38
|
+
}
|
|
39
|
+
// Resolve legacy global names once; persist concrete identities instead of
|
|
40
|
+
// accidentally suppressing a different same-name project skill forever.
|
|
41
|
+
const migrated = state.disabledSkills.length > 0
|
|
42
|
+
for (const name of state.disabledSkills) {
|
|
43
|
+
const def = await ctx.skills.get(name, { cwd: locations.workspace || undefined })
|
|
44
|
+
if (!def || disposed) continue
|
|
45
|
+
const rule = { name, identity: identity(def), cwd: def.source.startsWith('project') ? projectRoot(locations.workspace) : null }
|
|
46
|
+
rule.key = JSON.stringify([rule.identity, rule.cwd])
|
|
47
|
+
if (!state.skillRules.some(existing => existing.key === rule.key)) state.skillRules.push(rule)
|
|
48
|
+
await resolve(rule, { cwd: rule.cwd || locations.workspace })
|
|
49
|
+
}
|
|
50
|
+
if (!disposed) { control.invalidate(); state.disabledSkills = []; if (migrated) save() }
|
|
51
|
+
})()
|
|
52
|
+
// Keep initialization failures observable through list/write requests while
|
|
53
|
+
// preventing a provider failure from becoming an unhandled rejection.
|
|
54
|
+
void ready.catch(error => ctx.logger?.warn?.('Toolgate skill initialization failed: %s', error.message))
|
|
55
|
+
let refreshing = false
|
|
56
|
+
ctx.on('skills/change', () => {
|
|
57
|
+
if (disposed || refreshing) return
|
|
58
|
+
refreshing = true
|
|
59
|
+
void ready.then(async () => {
|
|
60
|
+
let changed = false
|
|
61
|
+
for (const rule of state.skillRules) {
|
|
62
|
+
if (disposed || definitions.has(rule.key)) continue
|
|
63
|
+
changed = await resolve(rule, { cwd: rule.cwd || locations.workspace }) || changed
|
|
64
|
+
}
|
|
65
|
+
if (changed && !disposed) control.invalidate()
|
|
66
|
+
}).catch(error => ctx.logger?.warn?.('Toolgate skill refresh failed: %s', error.message)).finally(() => { refreshing = false })
|
|
67
|
+
})
|
|
68
|
+
const rowsFor = async view => {
|
|
69
|
+
const snap = await snapshot(view)
|
|
70
|
+
const rows = []
|
|
71
|
+
for (const summary of snap.skills) {
|
|
72
|
+
const cached = summary.provider === OVERRIDE ? [...definitions.values()].find(({ rule }) => rule.name === summary.name && applies(rule, view)) : undefined
|
|
73
|
+
const source = cached ? JSON.parse(cached.rule.identity) : null
|
|
74
|
+
const row = source ? { ...summary, provider: source[1], path: source[2], source: source[3] } : summary
|
|
75
|
+
const global = await ctx.skills.get(row.name, { cwd: view.cwd })
|
|
76
|
+
const rootMatches = global?.provider === OVERRIDE ? !!cached : !!global && identity(global) === identity(row)
|
|
77
|
+
const rule = state.skillRules.find(rule => rule.identity === identity(row) && applies(rule, view))
|
|
78
|
+
rows.push({ name: row.name, description: row.description || '', provider: row.provider, path: row.path || null, source: row.source,
|
|
79
|
+
scope: row.source.startsWith('project') ? 'project' : 'user', disabled: !!rule,
|
|
80
|
+
toggleable: rootMatches, deletable: canDeleteSkill(row, { home: locations.home, cwd: view.projectRoot }),
|
|
81
|
+
})
|
|
82
|
+
}
|
|
83
|
+
return { skills: rows.sort((a, b) => a.name.localeCompare(b.name)), complete: snap.complete !== false,
|
|
84
|
+
workspace: view.projectRoot || null, cwd: view.cwd || null, sessionId: view.sessionId, sessions: view.sessions, userRoot: locations.home }
|
|
85
|
+
}
|
|
86
|
+
const selected = async args => {
|
|
87
|
+
const view = skillView(ctx, args), listing = await rowsFor(view)
|
|
88
|
+
const row = listing.skills.find(row => row.name === args.name)
|
|
89
|
+
if (!row || row.provider !== args.provider || row.path !== (args.path || null) || row.source !== args.source) throw new Error('Skill 来源已变化,请刷新后重试')
|
|
90
|
+
return { view, row }
|
|
91
|
+
}
|
|
92
|
+
ctx.effect(() => async () => { disposed = true; await ready; definitions.clear() })
|
|
93
|
+
return {
|
|
94
|
+
async list(args = {}) { await ready; return rowsFor(skillView(ctx, args)) },
|
|
95
|
+
async toggle(args) {
|
|
96
|
+
await ready
|
|
97
|
+
const { view, row } = await selected(args)
|
|
98
|
+
if (!row.toggleable) throw new Error('该 Skill 属于独立作用域,无法用全局 provider 覆盖')
|
|
99
|
+
const cwd = row.source.startsWith('project') ? view.projectRoot : null
|
|
100
|
+
const rule = { name: row.name, identity: identity(row), cwd }
|
|
101
|
+
rule.key = JSON.stringify([rule.identity, cwd])
|
|
102
|
+
const previousRules = [...state.skillRules]
|
|
103
|
+
const previousDefinition = definitions.get(rule.key)
|
|
104
|
+
if (args.enabled !== false) {
|
|
105
|
+
state.skillRules = state.skillRules.filter(existing => existing.key !== rule.key)
|
|
106
|
+
definitions.delete(rule.key)
|
|
107
|
+
} else if (!state.skillRules.some(existing => existing.key === rule.key)) {
|
|
108
|
+
if (!await resolve(rule, view)) throw new Error('Skill 来源已变化,请刷新后重试')
|
|
109
|
+
state.skillRules.push(rule)
|
|
110
|
+
}
|
|
111
|
+
try { save() } catch (error) {
|
|
112
|
+
state.skillRules = previousRules
|
|
113
|
+
if (previousDefinition) definitions.set(rule.key, previousDefinition)
|
|
114
|
+
else definitions.delete(rule.key)
|
|
115
|
+
control.invalidate()
|
|
116
|
+
throw error
|
|
117
|
+
}
|
|
118
|
+
control.invalidate()
|
|
119
|
+
return rowsFor(view)
|
|
120
|
+
},
|
|
121
|
+
async create(args) {
|
|
122
|
+
await ready
|
|
123
|
+
const view = skillView(ctx, args)
|
|
124
|
+
const created = createSkill(args, { home: locations.home, cwd: view.projectRoot })
|
|
125
|
+
control.invalidate()
|
|
126
|
+
return { created, ...await rowsFor(view) }
|
|
127
|
+
},
|
|
128
|
+
async delete(args) {
|
|
129
|
+
await ready
|
|
130
|
+
const { view, row } = await selected(args)
|
|
131
|
+
if (!row.deletable) throw new Error('仅可回收所选工作区或 DSH_HOME 下真实的 .dsh/skills 目录')
|
|
132
|
+
const removed = deleteSkill(row, { home: locations.home, cwd: view.projectRoot })
|
|
133
|
+
state.skillRules = state.skillRules.filter(rule => rule.identity !== identity(row))
|
|
134
|
+
for (const [key, cached] of definitions) if (cached.rule.identity === identity(row)) definitions.delete(key)
|
|
135
|
+
control.invalidate(); save()
|
|
136
|
+
return { removed, ...await rowsFor(view) }
|
|
137
|
+
},
|
|
138
|
+
}
|
|
139
|
+
}
|
package/locale/en.json
ADDED
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,103 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@jidekaixin2dian/dsh-toolgate",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@jidekaixin2dian/dsh-toolgate",
|
|
3
|
+
"version": "0.1.6",
|
|
4
|
+
"description": "Manage MCP servers and skills in one place; hide or restore groups of tool definitions to control what enters the next prompt.",
|
|
5
|
+
"icon": "icon.svg",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "lib/index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./lib/index.js",
|
|
10
|
+
"./client": "./lib/client.js",
|
|
11
|
+
"./package.json": "./package.json",
|
|
12
|
+
"./locale/*.json": "./locale/*.json"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"lib",
|
|
16
|
+
"cordis.patch.yml",
|
|
17
|
+
"README.md",
|
|
18
|
+
"README.en.md",
|
|
19
|
+
"CHANGELOG.md",
|
|
20
|
+
"README.zh.md",
|
|
21
|
+
"LICENSE",
|
|
22
|
+
"icon.svg",
|
|
23
|
+
"locale/*.json",
|
|
24
|
+
"assets",
|
|
25
|
+
"SECURITY.md"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "node --test \"test/*.test.js\"",
|
|
29
|
+
"setup:host": "node scripts/setup-host.mjs",
|
|
30
|
+
"test:host": "node scripts/test-host.mjs",
|
|
31
|
+
"pack:check": "node scripts/pack-check.mjs"
|
|
32
|
+
},
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://github.com/jidekaixin2dian/dsh-toolgate.git"
|
|
36
|
+
},
|
|
37
|
+
"homepage": "https://github.com/jidekaixin2dian/dsh-toolgate#readme",
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/jidekaixin2dian/dsh-toolgate/issues"
|
|
40
|
+
},
|
|
41
|
+
"keywords": [
|
|
42
|
+
"dsh",
|
|
43
|
+
"dsh-plugin",
|
|
44
|
+
"deepseek-harness",
|
|
45
|
+
"mcp",
|
|
46
|
+
"skills",
|
|
47
|
+
"context",
|
|
48
|
+
"tokens"
|
|
49
|
+
],
|
|
50
|
+
"author": "jidekaixin2dian",
|
|
51
|
+
"dsh": {
|
|
52
|
+
"bundle": {
|
|
53
|
+
"patch": "./cordis.patch.yml"
|
|
54
|
+
},
|
|
55
|
+
"client": {
|
|
56
|
+
"platform": "web",
|
|
57
|
+
"inject": [
|
|
58
|
+
"@deepseek-ai/dsh-client-connection",
|
|
59
|
+
"@deepseek-ai/dsh-client-locale",
|
|
60
|
+
"@deepseek-ai/dsh-client-ui-settings",
|
|
61
|
+
"@deepseek-ai/dsh-api-remotes"
|
|
62
|
+
]
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
"peerDependencies": {
|
|
66
|
+
"@deepseek-ai/cordis-plugin-include": "*",
|
|
67
|
+
"@deepseek-ai/dsh-app-boot": "*",
|
|
68
|
+
"@deepseek-ai/dsh-tools": ">=0.2.0-rc.1 <0.2.1-0 || >=0.2.1-rc.1 <0.3.0-0",
|
|
69
|
+
"@deepseek-ai/schemastery": "*"
|
|
70
|
+
},
|
|
71
|
+
"license": "MIT",
|
|
72
|
+
"dependencies": {
|
|
73
|
+
"yaml": "2.9.1"
|
|
74
|
+
},
|
|
75
|
+
"engines": {
|
|
76
|
+
"node": ">=24.0.0",
|
|
77
|
+
"dsh": ">=0.2.1-rc.2"
|
|
78
|
+
},
|
|
79
|
+
"peerDependenciesMeta": {
|
|
80
|
+
"@deepseek-ai/dsh-tools": {
|
|
81
|
+
"optional": true
|
|
82
|
+
},
|
|
83
|
+
"@deepseek-ai/dsh-app-boot": {
|
|
84
|
+
"optional": true
|
|
85
|
+
},
|
|
86
|
+
"@deepseek-ai/cordis-plugin-include": {
|
|
87
|
+
"optional": true
|
|
88
|
+
},
|
|
89
|
+
"@deepseek-ai/schemastery": {
|
|
90
|
+
"optional": true
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
"devDependencies": {
|
|
94
|
+
"@playwright/test": "1.64.1",
|
|
95
|
+
"esbuild": "0.28.2",
|
|
96
|
+
"react": "19.3.1",
|
|
97
|
+
"react-dom": "19.3.1"
|
|
98
|
+
},
|
|
99
|
+
"publishConfig": {
|
|
100
|
+
"access": "public",
|
|
101
|
+
"registry": "https://registry.npmjs.org/"
|
|
102
|
+
}
|
|
103
|
+
}
|