dsh-plugin-capabilities 0.3.3 → 0.3.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-plugin-capabilities",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "description": "Manage skills and MCP servers from the Web UI Settings. 在设置页管理 dsh 的技能与 MCP 服务器。",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -63,6 +63,8 @@
63
63
  },
64
64
  "dependencies": {
65
65
  "@deepseek-ai/dsh-skill-filesystem": "^0.1.0-rc.6",
66
+ "dompurify": "^3.4.13",
67
+ "marked": "^18.0.10",
66
68
  "smol-toml": "^1.3.1",
67
69
  "yaml": "^2.6.0"
68
70
  },
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Local Markdown preview for the skill editor: marked + DOMPurify, bundled
3
+ * into the plugin. The host exports its own Markdown renderer (the chat
4
+ * pipeline), but whether a given host build serves it through the frozen
5
+ * platform table is not guaranteed — bundling our own keeps the preview
6
+ * working on every host version. Skill bodies come from third-party
7
+ * repositories, so the rendered HTML is sanitized before it mounts.
8
+ */
9
+
10
+ import { useMemo } from 'react'
11
+ import type { ReactElement } from 'react'
12
+ import DOMPurify from 'dompurify'
13
+ import { marked } from 'marked'
14
+
15
+ const parse = (text: string): string =>
16
+ DOMPurify.sanitize(marked.parse(text, { async: false, gfm: true, breaks: false }) as string)
17
+
18
+ export function MarkdownPreview(props: { text: string }): ReactElement {
19
+ const html = useMemo(() => parse(props.text), [props.text])
20
+ return <div className="dpc-mdBody" dangerouslySetInnerHTML={{ __html: html }} />
21
+ }
@@ -5,8 +5,9 @@
5
5
 
6
6
  import { useEffect, useState } from 'react'
7
7
  import type { ReactElement } from 'react'
8
- import { Button, IconRefreshOutline14, IconSkillOutline16, MarkdownText, Modal, StateDot } from '@deepseek-ai/dsh-client-ui-primitives'
8
+ import { Button, IconRefreshOutline14, IconSkillOutline16, Modal, StateDot } from '@deepseek-ai/dsh-client-ui-primitives'
9
9
  import { CSS } from './css.ts'
10
+ import { MarkdownPreview } from './MarkdownPreview.tsx'
10
11
  import type { OpenTarget, Translate } from './index.ts'
11
12
  import type { RootRowView } from './MarketTab.tsx'
12
13
 
@@ -19,6 +20,9 @@ export interface SkillRowView {
19
20
  source: string
20
21
  provider: string
21
22
  editable: boolean
23
+ /** Only user-root skills can be deleted (the delete route removes one
24
+ * directory under $DSH_HOME/skills); other editable sources just save. */
25
+ removable: boolean
22
26
  /** The skill's folder when it lives on disk (drives open-folder). */
23
27
  dir?: string
24
28
  /** File-backed skills can be enabled/disabled through the policy route. */
@@ -369,7 +373,7 @@ export function SkillsTab(props: { t: Translate; injected: SkillsInjected }): Re
369
373
  <Button variant="ghost" size="sm" disabled={busy} onClick={() => void openExisting(skill)}>
370
374
  {skill.editable ? t('edit') : t('view')}
371
375
  </Button>
372
- {skill.editable && (
376
+ {skill.removable && (
373
377
  <Button variant="ghost" size="sm" disabled={busy} onClick={() => setConfirmName(skill.name)}>{t('delete')}</Button>
374
378
  )}
375
379
  </div>
@@ -505,9 +509,7 @@ export function SkillsTab(props: { t: Translate; injected: SkillsInjected }): Re
505
509
  </div>
506
510
  </div>
507
511
  {preview
508
- ? (typeof MarkdownText === 'function'
509
- ? <div className="dpc-mdPreview"><MarkdownText text={editor.content} /></div>
510
- : <textarea className="dpc-textarea" value={editor.content} readOnly />)
512
+ ? <div className="dpc-mdPreview"><MarkdownPreview text={editor.content} /></div>
511
513
  : (
512
514
  <textarea
513
515
  className="dpc-textarea"
package/src/client/css.ts CHANGED
@@ -62,6 +62,21 @@ export const CSS = `
62
62
  .dpc-modalForm.dpc-modalForm{width:min(760px,100%)}
63
63
  .dpc-modalScroll.dpc-modalScroll{max-height:calc(100vh - 160px);overflow-y:auto}
64
64
  .dpc-mdPreview{min-height:320px;max-height:60vh;overflow-y:auto;border:1px solid var(--dsw-alias-border-l2);border-radius:8px;padding:8px 12px;background:var(--dsw-alias-bg-layer-1);font-size:13px}
65
+ /* Rendered-markdown body (bundled marked + DOMPurify). */
66
+ .dpc-mdBody h1,.dpc-mdBody h2,.dpc-mdBody h3,.dpc-mdBody h4{margin:14px 0 6px;color:var(--dsw-alias-label-primary);line-height:1.4}
67
+ .dpc-mdBody h1{font-size:18px}.dpc-mdBody h2{font-size:16px}.dpc-mdBody h3{font-size:14px}.dpc-mdBody h4{font-size:13px}
68
+ .dpc-mdBody p{margin:6px 0;line-height:20px}
69
+ .dpc-mdBody ul,.dpc-mdBody ol{margin:6px 0;padding-left:20px}
70
+ .dpc-mdBody li{margin:2px 0;line-height:19px}
71
+ .dpc-mdBody code{font-family:var(--ds-font-family-code);font-size:12px;background:var(--dsw-alias-bg-layer-3);border-radius:4px;padding:1px 5px}
72
+ .dpc-mdBody pre{margin:8px 0;padding:10px 12px;overflow-x:auto;border:1px solid var(--dsw-alias-border-l2);border-radius:8px;background:var(--dsw-alias-bg-layer-3)}
73
+ .dpc-mdBody pre code{padding:0;background:transparent;font-size:12px;line-height:18px}
74
+ .dpc-mdBody a{color:var(--dsw-alias-state-business-primary)}
75
+ .dpc-mdBody blockquote{margin:8px 0;padding:2px 12px;border-left:3px solid var(--dsw-alias-border-l2);color:var(--dsw-alias-label-secondary)}
76
+ .dpc-mdBody table{border-collapse:collapse;margin:8px 0}
77
+ .dpc-mdBody th,.dpc-mdBody td{border:1px solid var(--dsw-alias-border-l2);padding:4px 10px;font-size:12px;line-height:18px}
78
+ .dpc-mdBody hr{border:0;border-top:1px solid var(--dsw-alias-border-l2);margin:12px 0}
79
+ .dpc-mdBody>*:first-child{margin-top:0}
65
80
  .dpc-importScroll{display:flex;flex-direction:column;gap:14px;max-height:min(400px,52vh);overflow-y:auto;padding:2px 4px 2px 2px}
66
81
  .dpc-importGroup{display:flex;flex-direction:column;gap:8px}
67
82
  .dpc-importHead{display:flex;align-items:center;gap:8px;padding:0 2px}
@@ -6,7 +6,7 @@ export const zh = {
6
6
  mcpTab: 'MCP',
7
7
  marketTab: '市场',
8
8
  skillsTitle: '技能管理',
9
- skillsIntro: '查看与编辑 dsh 发现的技能;用户级技能(DSH_HOME/skills)可在此新建、修改、删除,文件被监听,保存即生效。若存在 ~/.claude/skills 或 ~/.codex/skills,会自动纳入扫描(零拷贝、实时同步)。卡片上的开关控制是否加载:停用即从模型与用户目录同时摘除,开启恢复默认。',
9
+ skillsIntro: '查看与编辑 dsh 发现的技能;用户级技能(DSH_HOME/skills)可在此新建、修改、删除,市场/本地仓库安装的技能也能就地编辑(写回时保留文件里其余 frontmatter 键),文件被监听,保存即生效。若存在 ~/.claude/skills 或 ~/.codex/skills,会自动纳入扫描(零拷贝、实时同步)。卡片上的开关控制是否加载:停用即从模型与用户目录同时摘除,开启恢复默认。',
10
10
  newSkill: '新建技能',
11
11
  editSkill: '编辑技能',
12
12
  viewSkill: '查看技能',
@@ -139,7 +139,7 @@ export const en = {
139
139
  mcpTab: 'MCP',
140
140
  marketTab: 'Market',
141
141
  skillsTitle: 'Skills',
142
- skillsIntro: 'View and edit the skills dsh discovers; user-level skills (DSH_HOME/skills) can be created, edited, and deleted here the directory is watched, saves apply without a restart. ~/.claude/skills and ~/.codex/skills are scanned too when present (zero-copy, live-synced). The switch on each card toggles loading: disabling removes the skill from both the model and user catalogs; enabling restores the default policy.',
142
+ skillsIntro: 'View and edit the skills dsh discovers; user-level skills (DSH_HOME/skills) can be created, edited, and deleted here, and skills installed from market/local repositories edit in place too (their other frontmatter keys survive the write). ~/.claude/skills and ~/.codex/skills are scanned too when present (zero-copy, live-synced). The switch on each card toggles loading: disabling removes the skill from both the model and user catalogs; enabling restores the default policy.',
143
143
  newSkill: 'New skill',
144
144
  editSkill: 'Edit skill',
145
145
  viewSkill: 'View skill',
@@ -40,12 +40,6 @@ declare module '@deepseek-ai/dsh-client-ui-primitives' {
40
40
  className?: string | undefined
41
41
  }): ReactElement
42
42
 
43
- /** Untrusted-Markdown renderer over the app's own mdast pipeline. */
44
- export function MarkdownText(props: {
45
- text: string
46
- streaming?: boolean | undefined
47
- }): ReactElement
48
-
49
43
  export function IconRefreshOutline14(props: {
50
44
  className?: string | undefined
51
45
  size?: number | undefined
package/src/routes.ts CHANGED
@@ -2,26 +2,51 @@
2
2
 
3
3
  import type { IncomingMessage, ServerResponse } from 'node:http'
4
4
  import { mkdirSync } from 'node:fs'
5
+ import { join, sep } from 'node:path'
5
6
  import { scanAllMcp } from './agents.ts'
6
7
  import { readJsonBody, sameOrigin, sendJson } from './http.ts'
7
8
  import { loadMarketIndex, type MarketMcpServer } from './market.ts'
8
9
  import { openDirectory } from './opener.ts'
9
10
  import { addGitRepo, addLocalRepo, rootExists } from './repos.ts'
10
11
  import { dshLaunch, restartOwnedByShell, scheduleRestart, trustedRestartRequest } from './restart.ts'
11
- import { deleteSkill, setSkillPolicy, userSkillsDir, validateSkillInput, writeSkill, type SkillInput } from './skills.ts'
12
+ import { deleteSkill, setSkillPolicy, updateSkillFile, userSkillsDir, validateSkillInput, writeSkill, type SkillInput } from './skills.ts'
12
13
  import { findRootByUrl, loadState, pluginStateDir, removeSkillRoot, type SkillRootEntry } from './state.ts'
13
14
  import { listMcp, removeMcp, setMcpDisabled, upsertMcp, validateMcpInput, type McpInput } from './mcp.ts'
14
15
  import type { CapabilitiesHost, HostSkill } from './types.ts'
15
16
 
16
- /** Only this source is writable from the Settings page (provider rank 400). */
17
- const EDITABLE_SOURCE = 'user-dsh'
17
+ /**
18
+ * A 'custom' skill is writable only when its folder sits inside a root this
19
+ * plugin manages: the materialized repositories under the plugin state dir,
20
+ * or a registered local root. Vendored skills shipped inside the plugin
21
+ * package (under node_modules) are custom-sourced too but stay read-only —
22
+ * edits there would die with the next plugin update.
23
+ */
24
+ function customSkillWritable(dir: string, dshHome: string | undefined): boolean {
25
+ const state = pluginStateDir(dshHome)
26
+ if (dir === state || dir.startsWith(state + sep)) return true
27
+ return loadState(dshHome).skillRoots.some(entry =>
28
+ entry.roots.some(root => dir === root || dir.startsWith(root + sep)))
29
+ }
30
+
31
+ /** Whether the save route may write this catalog row back to disk. */
32
+ function skillWritable(skill: HostSkill, dir: string | undefined, dshHome: string | undefined): boolean {
33
+ if (dir === undefined) return false
34
+ if (skill.source === 'user-dsh') return true
35
+ return skill.source === 'custom' && customSkillWritable(dir, dshHome)
36
+ }
18
37
 
19
38
  /** One catalog skill as the browser sees it (editable/dir flags added). */
20
- type SkillRow = HostSkill & { editable: boolean; dir?: string; policyEditable: boolean }
39
+ type SkillRow = HostSkill & { editable: boolean; removable: boolean; dir?: string; policyEditable: boolean }
21
40
 
22
- function toSkillRow(skill: HostSkill): SkillRow {
41
+ function toSkillRow(skill: HostSkill, dshHome: string | undefined): SkillRow {
23
42
  const dir = skill.resourceBase?.kind === 'directory' ? skill.resourceBase.path : undefined
24
- return { ...skill, editable: skill.source === EDITABLE_SOURCE, ...(dir !== undefined ? { dir } : {}), policyEditable: dir !== undefined }
43
+ return {
44
+ ...skill,
45
+ editable: skillWritable(skill, dir, dshHome),
46
+ removable: skill.source === 'user-dsh',
47
+ ...(dir !== undefined ? { dir } : {}),
48
+ policyEditable: dir !== undefined,
49
+ }
25
50
  }
26
51
 
27
52
  /** One registered repository plus a liveness flag (roots can go stale). */
@@ -49,7 +74,7 @@ export function mountCapabilitiesRoutes(host: CapabilitiesHost, config: Capabili
49
74
  }
50
75
  try {
51
76
  const skills = await host.skills.list()
52
- sendJson(response, 200, { skills: skills.map(toSkillRow) })
77
+ sendJson(response, 200, { skills: skills.map(skill => toSkillRow(skill, process.env.DSH_HOME)) })
53
78
  } catch (error) {
54
79
  sendJson(response, 500, { error: error instanceof Error ? error.message : String(error) })
55
80
  }
@@ -108,7 +133,22 @@ export function mountCapabilitiesRoutes(host: CapabilitiesHost, config: Capabili
108
133
  sendJson(response, 400, { error: invalid })
109
134
  return
110
135
  }
111
- writeSkill(input)
136
+ // An existing skill edits in place (its own folder, whichever
137
+ // editable source it comes from — preserving frontmatter keys the
138
+ // editor does not own); a new name creates in the user root. The
139
+ // file location is resolved server-side from the catalog, never
140
+ // taken from the request.
141
+ const existing = (await host.skills.list()).find(skill => skill.name === input.name)
142
+ if (existing !== undefined) {
143
+ const dir = existing.resourceBase?.kind === 'directory' ? existing.resourceBase.path : undefined
144
+ if (!skillWritable(existing, dir, process.env.DSH_HOME)) {
145
+ sendJson(response, 403, { error: `skills from source '${existing.source}' are read-only` })
146
+ return
147
+ }
148
+ updateSkillFile(join(dir as string, 'SKILL.md'), input)
149
+ } else {
150
+ writeSkill(input)
151
+ }
112
152
  sendJson(response, 200, { ok: true, name: input.name })
113
153
  } catch (error) {
114
154
  sendJson(response, 500, { error: error instanceof Error ? error.message : String(error) })
@@ -2,7 +2,7 @@ import { mkdtempSync, readFileSync, rmSync, existsSync, writeFileSync, mkdirSync
2
2
  import { tmpdir } from 'node:os'
3
3
  import { join } from 'node:path'
4
4
  import { afterAll, describe, expect, it } from 'vitest'
5
- import { deleteSkill, serializeSkill, setSkillPolicy, validateSkillInput, writeSkill, type SkillInput } from './skills.ts'
5
+ import { deleteSkill, serializeSkill, setSkillPolicy, updateSkillFile, validateSkillInput, writeSkill, type SkillInput } from './skills.ts'
6
6
 
7
7
  const root = mkdtempSync(join(tmpdir(), 'dsh-caps-skills-'))
8
8
  afterAll(() => rmSync(root, { recursive: true, force: true }))
@@ -58,6 +58,29 @@ describe('writeSkill / deleteSkill', () => {
58
58
  expect(existsSync(join(root, 'skills', 'my-skill'))).toBe(false)
59
59
  expect(deleteSkill('my-skill', root)).toBe(false)
60
60
  })
61
+
62
+ it('updateSkillFile edits in place, preserving keys the editor does not own', () => {
63
+ const dir = join(root, 'keep-skill')
64
+ mkdirSync(dir, { recursive: true })
65
+ const file = join(dir, 'SKILL.md')
66
+ writeFileSync(file, '---\nname: keep-skill\nlicense: MIT\nallowed-tools: Bash\ndescription: "old one"\n---\n\n# Old\n')
67
+ updateSkillFile(file, {
68
+ name: 'keep-skill',
69
+ description: 'new one',
70
+ whenToUse: undefined,
71
+ modelInvocable: false,
72
+ userInvocable: true,
73
+ content: '# New body',
74
+ })
75
+ const after = readFileSync(file, 'utf8')
76
+ expect(after).toContain('license: MIT')
77
+ expect(after).toContain('allowed-tools: Bash')
78
+ expect(after).toContain('description: "new one"')
79
+ expect(after).toContain('disable-model-invocation: true')
80
+ expect(after).not.toContain('user-invocable')
81
+ expect(after).toContain('# New body')
82
+ expect(after).not.toContain('# Old')
83
+ })
61
84
  it('never touches sibling directories on delete', () => {
62
85
  mkdirSync(join(root, 'skills', 'other'), { recursive: true })
63
86
  writeFileSync(join(root, 'skills', 'other', 'SKILL.md'), '---\nname: other\ndescription: d\n---\n\nx')
package/src/skills.ts CHANGED
@@ -100,3 +100,26 @@ export function setSkillPolicy(file: string, enabled: boolean): void {
100
100
  if (!enabled) kept.push('disable-model-invocation: true', 'user-invocable: false')
101
101
  writeFileSync(file, `---${newline}${kept.join(newline)}${newline}---${body}`, 'utf8')
102
102
  }
103
+
104
+ /**
105
+ * Edit an existing skill file in place — for skills that do not live in the
106
+ * user root (market/custom repositories). Only the keys the editor owns
107
+ * (name, description, whenToUse, the two invocation flags) are rewritten;
108
+ * every other frontmatter key — license, allowed-tools, anything a
109
+ * repository ships — survives in order, like setSkillPolicy. The body is
110
+ * replaced by the editor's content.
111
+ */
112
+ export function updateSkillFile(file: string, input: SkillInput): void {
113
+ const text = readFileSync(file, 'utf8')
114
+ const match = /^---\r?\n([\s\S]*?)\r?\n---/.exec(text)
115
+ if (match === null) throw new Error('skill file has no frontmatter block')
116
+ const newline = text.includes('\r\n---') ? '\r\n' : '\n'
117
+ const lines = match[1].split(/\r?\n/)
118
+ const kept = lines.filter(line => !/^(name|description|whenToUse|disable-model-invocation|user-invocable):/.test(line))
119
+ const head = [`name: ${input.name}`, `description: ${quote(input.description)}`]
120
+ if (input.whenToUse !== undefined && input.whenToUse !== '') head.push(`whenToUse: ${quote(input.whenToUse)}`)
121
+ if (!input.modelInvocable) kept.push('disable-model-invocation: true')
122
+ if (!input.userInvocable) kept.push('user-invocable: false')
123
+ const body = input.content.replace(/\r\n/g, '\n').trim()
124
+ writeFileSync(file, `---${newline}${[...head, ...kept].join(newline)}${newline}---${newline}${newline}${body}${newline}`, 'utf8')
125
+ }
package/src/smoke.test.ts CHANGED
@@ -158,7 +158,7 @@ describe.skipIf(process.env.DSH_DESKTOP_PLUGIN_SMOKE !== '1' || !guard || !nodeO
158
158
  // skill enters the catalog under source "custom".
159
159
  const repoDir = join(smokeRoot, 'my-repo')
160
160
  mkdirSync(join(repoDir, 'repo-skill'), { recursive: true })
161
- writeFileSync(join(repoDir, 'repo-skill', 'SKILL.md'), '---\nname: repo-skill\ndescription: from a local repo\n---\n\nhi')
161
+ writeFileSync(join(repoDir, 'repo-skill', 'SKILL.md'), '---\nname: repo-skill\ndescription: from a local repo\nlicense: MIT\n---\n\nhi')
162
162
  const addRoot = await post('/dsh-plugin-capabilities/roots/add', { kind: 'local', path: repoDir })
163
163
  expect(addRoot.status).toBe(200)
164
164
  expect(readFileSync(join(smokeRoot, 'dsh-plugin-capabilities', 'state.json'), 'utf8')).toContain('my-repo') // roots recorded
@@ -185,6 +185,21 @@ describe.skipIf(process.env.DSH_DESKTOP_PLUGIN_SMOKE !== '1' || !guard || !nodeO
185
185
  const policyRestore = await post('/dsh-plugin-capabilities/skill/policy', { name: 'repo-skill', enabled: true })
186
186
  expect(policyRestore.status).toBe(200)
187
187
 
188
+ // 7b. Custom-repo skills edit in place through the save route; the
189
+ // frontmatter keys the editor does not own (license) survive the write.
190
+ const edit = await post('/dsh-plugin-capabilities/skill/save', {
191
+ name: 'repo-skill',
192
+ description: 'edited in place',
193
+ modelInvocable: true,
194
+ userInvocable: true,
195
+ content: '# Edited\n\nnew body',
196
+ })
197
+ expect(edit.status).toBe(200)
198
+ const editedFile = readFileSync(join(repoDir, 'repo-skill', 'SKILL.md'), 'utf8')
199
+ expect(editedFile).toContain('license: MIT')
200
+ expect(editedFile).toContain('description: "edited in place"')
201
+ expect(editedFile).toContain('# Edited')
202
+
188
203
  // 8. Market: MCP index serves (remote or bundled fallback) and one-click
189
204
  // install writes a profile row.
190
205
  const marketIndex = await fetch(`${base}/dsh-plugin-capabilities/market/mcp`)