yukari-dsh-plugin 0.0.0-stage → 0.7.2

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.
Files changed (105) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +17 -2
  3. package/client.js +8340 -0
  4. package/cordis.patch.yml +95 -0
  5. package/host/browser/index.js +397 -0
  6. package/host/browser/lib/browser.js +885 -0
  7. package/host/browser/lib/cdp.js +302 -0
  8. package/host/browser/lib/libraries.js +309 -0
  9. package/host/browser/lib/page-scripts.js +243 -0
  10. package/host/browser/lib/runtime.js +351 -0
  11. package/host/browser/lib/url-guard.js +122 -0
  12. package/host/browser/locale/en.json +6 -0
  13. package/host/browser/locale/zh.json +6 -0
  14. package/host/chats/index.js +254 -0
  15. package/host/chats/lib/admin.js +893 -0
  16. package/host/chats/lib/cleanup.js +272 -0
  17. package/host/chats/lib/paths.js +108 -0
  18. package/host/chats/lib/preview.js +74 -0
  19. package/host/chats/locale/en.json +6 -0
  20. package/host/chats/locale/zh.json +6 -0
  21. package/host/files/index.js +403 -0
  22. package/host/files/locale/en.json +6 -0
  23. package/host/files/locale/zh.json +6 -0
  24. package/host/github/index.js +121 -0
  25. package/host/github/lib/api.js +310 -0
  26. package/host/github/lib/atomic.js +42 -0
  27. package/host/github/lib/controller.js +450 -0
  28. package/host/github/lib/git.js +389 -0
  29. package/host/github/lib/redact.js +22 -0
  30. package/host/github/lib/signing.js +124 -0
  31. package/host/github/lib/store.js +181 -0
  32. package/host/github/locale/en.json +6 -0
  33. package/host/github/locale/zh.json +6 -0
  34. package/host/presets/index.js +450 -0
  35. package/host/presets/lib/definition.js +267 -0
  36. package/host/presets/lib/sources.js +278 -0
  37. package/host/presets/lib/store.js +136 -0
  38. package/host/presets/locale/en.json +6 -0
  39. package/host/presets/locale/zh.json +6 -0
  40. package/host/proxy/index.js +236 -0
  41. package/host/proxy/lib/controller.js +1151 -0
  42. package/host/proxy/lib/egress.js +120 -0
  43. package/host/proxy/lib/local-proxy.js +422 -0
  44. package/host/proxy/lib/shadowsocks.js +335 -0
  45. package/host/proxy/lib/ss-uri.js +501 -0
  46. package/host/proxy/lib/subscriptions.js +140 -0
  47. package/host/proxy/locale/en.json +6 -0
  48. package/host/proxy/locale/zh.json +6 -0
  49. package/host/purge/index.js +538 -0
  50. package/host/purge/lib/preset.js +140 -0
  51. package/host/purge/lib/prompts.js +95 -0
  52. package/host/purge/lib/retry-gate.js +97 -0
  53. package/host/purge/lib/rewriter.js +322 -0
  54. package/host/purge/lib/settings.js +54 -0
  55. package/host/purge/lib/stream-retry.js +255 -0
  56. package/host/purge/locale/en.json +6 -0
  57. package/host/purge/locale/zh.json +6 -0
  58. package/host/purge/presets/unrestricted/agent.cordis.yml +273 -0
  59. package/host/purge/presets/unrestricted/preset.json +75 -0
  60. package/host/purge/presets/unrestricted/preset.yml +3 -0
  61. package/host/rewind/index.js +127 -0
  62. package/host/rewind/lib/controller.js +449 -0
  63. package/host/rewind/lib/marker.js +53 -0
  64. package/host/rewind/lib/safe-path.js +47 -0
  65. package/host/rewind/lib/store.js +585 -0
  66. package/host/rewind/lib/surface.js +108 -0
  67. package/host/rewind/locale/en.json +6 -0
  68. package/host/rewind/locale/zh.json +6 -0
  69. package/host/rules/index.js +624 -0
  70. package/host/rules/lib/paths.js +87 -0
  71. package/host/rules/lib/render.js +92 -0
  72. package/host/rules/lib/scope.js +146 -0
  73. package/host/rules/lib/settings.js +55 -0
  74. package/host/rules/lib/store.js +222 -0
  75. package/host/rules/locale/en.json +6 -0
  76. package/host/rules/locale/zh.json +6 -0
  77. package/host/runtime/index.js +577 -0
  78. package/host/runtime/lib/npm.js +293 -0
  79. package/host/runtime/lib/restart.js +105 -0
  80. package/host/runtime/lib/update-guard.js +70 -0
  81. package/host/runtime/lib/versions.js +188 -0
  82. package/host/runtime/locale/en.json +6 -0
  83. package/host/runtime/locale/zh.json +6 -0
  84. package/host/shared/busy.js +40 -0
  85. package/host/shared/home.js +27 -0
  86. package/host/shared/http.js +159 -0
  87. package/host/shared/migrate.js +60 -0
  88. package/host/shared/mounted.js +54 -0
  89. package/host/shared/zip-writer.js +319 -0
  90. package/host/shared/zip.js +180 -0
  91. package/host/sites/index.js +258 -0
  92. package/host/sites/lib/sites.js +630 -0
  93. package/host/sites/locale/en.json +6 -0
  94. package/host/sites/locale/zh.json +6 -0
  95. package/host/skills/index.js +176 -0
  96. package/host/skills/lib/frontmatter.js +149 -0
  97. package/host/skills/lib/library.js +408 -0
  98. package/host/skills/lib/upload.js +115 -0
  99. package/host/skills/locale/en.json +6 -0
  100. package/host/skills/locale/zh.json +6 -0
  101. package/icon.png +0 -0
  102. package/index.js +702 -0
  103. package/locale/en.json +5 -0
  104. package/locale/zh.json +5 -0
  105. package/package.json +113 -4
@@ -0,0 +1,176 @@
1
+ /**
2
+ * Yukari Skills: install, inspect, edit and switch off agent skills.
3
+ *
4
+ * The skills themselves are a harness feature, not this plugin's invention:
5
+ * `@deepseek-ai/dsh-skill-filesystem` discovers them in `$DSH_HOME/skills` (the
6
+ * `user-dsh` root, rank 400), `@deepseek-ai/dsh-skill` merges them into one
7
+ * catalog and `@deepseek-ai/dsh-tool-skill` renders that catalog to the model and
8
+ * loads one body on demand. This component is only the caretaker of that
9
+ * directory:
10
+ *
11
+ * - it lists the entries the provider would discover, and says why one it would
12
+ * skip is being skipped;
13
+ * - it writes new ones (a flat `.md` or an unpacked `.zip` bundle) and edits
14
+ * existing bodies, validating frontmatter with the same grammar;
15
+ * - it disables a skill by moving it to `$DSH_HOME/skills-disabled`, a directory
16
+ * no provider scans, and enables it by moving it back.
17
+ *
18
+ * No restart is involved in either direction: the provider watches the root, so
19
+ * the next model step sees the new catalog. Nothing here reads `ctx.skills`,
20
+ * because the registry visible from a host row only carries the global layer,
21
+ * while filesystem discovery belongs to each agent preset's own layer.
22
+ * @module yukari-dsh-plugin/skills
23
+ */
24
+ import { createReadStream } from 'node:fs'
25
+ import { stat } from 'node:fs/promises'
26
+ import path from 'node:path'
27
+ import { markMounted } from '../shared/mounted.js'
28
+ import { appError, attachmentHeader, guardRoute, readJsonBody, sendJson, streamToResponse } from '../shared/http.js'
29
+ import {
30
+ MAX_TEXT_BYTES,
31
+ archiveEntries,
32
+ assertForm,
33
+ assertKey,
34
+ libraries,
35
+ readSkill,
36
+ removeSkill,
37
+ saveSkill,
38
+ scan,
39
+ setEnabled,
40
+ } from './lib/library.js'
41
+ import { MAX_ARCHIVE_ENTRIES, MAX_UPLOAD_BYTES, installUpload, readRawBody } from './lib/upload.js'
42
+ import { writeZip } from '../shared/zip-writer.js'
43
+
44
+ /** JSON API prefix served to the panel. */
45
+ export const API_PATH = '/skills/api'
46
+
47
+ /** Services this component needs. */
48
+ export const inject = ['webServer', 'connection']
49
+
50
+ /** Body ceiling for the JSON actions; the editor's text has its own limit. */
51
+ const BODY_LIMIT = MAX_TEXT_BYTES + 64 * 1024
52
+
53
+ export async function apply(ctx) {
54
+ const { root, disabledRoot } = libraries()
55
+ markMounted(ctx, 'skills', { root })
56
+
57
+ /** Every mutation runs alone: two clicks must not interleave moves and writes. */
58
+ let queue = Promise.resolve()
59
+
60
+ function serialized(operation) {
61
+ const run = queue.then(operation, operation)
62
+ queue = run.then(
63
+ () => undefined,
64
+ () => undefined,
65
+ )
66
+ return run
67
+ }
68
+
69
+ /** One payload every view starts from. */
70
+ async function state() {
71
+ const items = await scan()
72
+ return {
73
+ ok: true,
74
+ root,
75
+ disabledRoot,
76
+ items,
77
+ limits: { uploadMb: Math.round(MAX_UPLOAD_BYTES / 1048576), textKb: Math.round(MAX_TEXT_BYTES / 1024), entries: MAX_ARCHIVE_ENTRIES },
78
+ }
79
+ }
80
+
81
+ /** One entry from the panel's `key` / `form` pair. */
82
+ function selection(url, body) {
83
+ const key = assertKey(url?.searchParams?.get('key') ?? body?.key)
84
+ const form = assertForm(url?.searchParams?.get('form') ?? body?.form)
85
+ return { key, form }
86
+ }
87
+
88
+ ctx.effect(
89
+ () =>
90
+ ctx.webServer.register({
91
+ kind: 'prefix',
92
+ path: API_PATH,
93
+ handler: guardRoute(ctx, async (req, res) => {
94
+ const url = new URL(String(req.url), 'http://localhost')
95
+ const action = url.pathname.slice(API_PATH.length).replace(/^\/+|\/+$/g, '')
96
+
97
+ if (req.method === 'GET' && (action === '' || action === 'state')) {
98
+ sendJson(res, 200, await state())
99
+ return
100
+ }
101
+
102
+ if (req.method === 'GET' && action === 'skill') {
103
+ const { key, form } = selection(url)
104
+ sendJson(res, 200, { ok: true, ...(await readSkill(key, form)) })
105
+ return
106
+ }
107
+
108
+ if (req.method === 'GET' && action === 'download') {
109
+ const { key, form } = selection(url)
110
+ const { entries, item } = await archiveEntries(key, form)
111
+ const base = item.name === '' ? key : item.name
112
+ res.setHeader('x-content-type-options', 'nosniff')
113
+ if (form === 'file') {
114
+ const file = entries[0].path
115
+ const info = await stat(file)
116
+ res.statusCode = 200
117
+ res.setHeader('content-type', 'text/markdown; charset=utf-8')
118
+ res.setHeader('content-length', String(info.size))
119
+ res.setHeader('content-disposition', attachmentHeader(`${base}.md`))
120
+ await streamToResponse(createReadStream(file), res)
121
+ return
122
+ }
123
+ res.statusCode = 200
124
+ res.setHeader('content-type', 'application/zip')
125
+ res.setHeader('content-disposition', attachmentHeader(`${base}.zip`))
126
+ try {
127
+ await writeZip(res, entries)
128
+ } catch (error) {
129
+ if (!res.headersSent) {
130
+ res.removeHeader('content-type')
131
+ res.removeHeader('content-disposition')
132
+ }
133
+ throw error
134
+ }
135
+ res.end()
136
+ return
137
+ }
138
+
139
+ if (req.method === 'POST' && action === 'upload') {
140
+ const filename = url.searchParams.get('filename') ?? ''
141
+ const bytes = await readRawBody(req)
142
+ const result = await serialized(() => installUpload({ bytes, filename }))
143
+ sendJson(res, 200, { ok: true, ...result })
144
+ return
145
+ }
146
+
147
+ if (req.method === 'POST') {
148
+ const body = await readJsonBody(req, BODY_LIMIT)
149
+ if (action === 'save') {
150
+ const { key, form } = selection(undefined, body)
151
+ const text = typeof body.text === 'string' ? body.text : ''
152
+ const result = await serialized(() => saveSkill(key, form, text))
153
+ sendJson(res, 200, { ok: true, ...result })
154
+ return
155
+ }
156
+ if (action === 'enable' || action === 'disable') {
157
+ const { key, form } = selection(undefined, body)
158
+ const item = await serialized(() => setEnabled(key, form, action === 'enable'))
159
+ sendJson(res, 200, { ok: true, item })
160
+ return
161
+ }
162
+ if (action === 'delete') {
163
+ const { key, form } = selection(undefined, body)
164
+ sendJson(res, 200, { ok: true, ...(await serialized(() => removeSkill(key, form))) })
165
+ return
166
+ }
167
+ }
168
+
169
+ sendJson(res, 404, { ok: false, error: `未知的操作:${req.method} ${action === '' ? '/' : action}` })
170
+ }),
171
+ }),
172
+ 'yukari-skills: routes',
173
+ )
174
+
175
+ ctx.logger?.info?.('yukari-skills: %s', path.join(API_PATH, 'state'))
176
+ }
@@ -0,0 +1,149 @@
1
+ /**
2
+ * SKILL.md frontmatter, read with the host's own grammar.
3
+ *
4
+ * The panel has to answer one question before it writes anything: will the host
5
+ * provider actually load this file? Answering it with a hand-rolled parser would
6
+ * disagree on block scalars (`description: |`), quoted values and nested
7
+ * metadata — the shapes real skill files use — so the YAML document is parsed by
8
+ * `yaml`, the same library `dsh-skill-filesystem` uses, and the field rules
9
+ * below are copied from it: kebab-case `name`, required `description`, the two
10
+ * invocation keys with their accepted boolean spellings, and the legacy keys the
11
+ * host rejects outright.
12
+ *
13
+ * A malformed file is reported, never thrown: the panel lists it with a reason
14
+ * instead of hiding it, because "why is my skill missing" is the question this
15
+ * page exists to answer.
16
+ * @module yukari-dsh-plugin/skills/lib/frontmatter
17
+ */
18
+ import { parse as parseYaml } from 'yaml'
19
+
20
+ /** The public skill-name grammar, identical to `dsh-skill`. */
21
+ const SKILL_NAME = /^[a-z0-9]+(?:-[a-z0-9]+)*$/
22
+
23
+ /** Keys the host refuses; each maps to the canonical replacement. */
24
+ const LEGACY_KEYS = {
25
+ disableModelInvocation: 'disable-model-invocation',
26
+ modelInvocable: 'disable-model-invocation',
27
+ userInvocable: 'user-invocable',
28
+ }
29
+
30
+ /** Why a file would not load, and what to tell the operator. */
31
+ const PROBLEMS = {
32
+ 'no-frontmatter': '文件必须以一行「---」开头的 YAML 前言开始,否则宿主读不到技能信息。',
33
+ 'bad-yaml': '前言不是有效的 YAML。',
34
+ 'missing-name': '前言缺少 name。',
35
+ 'missing-description': '前言缺少 description。',
36
+ 'bad-name': 'name 必须是 kebab-case:小写字母、数字,用「-」分隔。',
37
+ 'bad-invocation': '调用开关的取值不合法,只能是 true 或 false。',
38
+ }
39
+
40
+ /**
41
+ * Locate the YAML frontmatter exactly the way the host does: the first line is
42
+ * `---`, the block ends at the next line that is only `---`.
43
+ * @param text - the whole file.
44
+ * @returns `{ yaml, body }`, or null when there is no frontmatter block.
45
+ */
46
+ function splitFrontmatter(text) {
47
+ const firstLineEnd = text.indexOf('\n')
48
+ if (firstLineEnd < 0) return null
49
+ if (text.slice(0, firstLineEnd).replace(/\r$/, '') !== '---') return null
50
+ let lineStart = firstLineEnd + 1
51
+ while (lineStart <= text.length) {
52
+ const nextNewline = text.indexOf('\n', lineStart)
53
+ const lineEnd = nextNewline < 0 ? text.length : nextNewline
54
+ if (text.slice(lineStart, lineEnd).replace(/\r$/, '') === '---') {
55
+ const bodyStart = nextNewline < 0 ? text.length : nextNewline + 1
56
+ return { yaml: text.slice(firstLineEnd + 1, lineStart), body: text.slice(bodyStart) }
57
+ }
58
+ if (nextNewline < 0) return null
59
+ lineStart = nextNewline + 1
60
+ }
61
+ return null
62
+ }
63
+
64
+ /** One non-empty string field, or undefined. */
65
+ function textField(data, key) {
66
+ const value = data[key]
67
+ return typeof value === 'string' && value.length > 0 ? value : undefined
68
+ }
69
+
70
+ /**
71
+ * Read one boolean the way the host does: a YAML boolean, `1`/`0`, `"1"`/`"0"`,
72
+ * or the case-insensitive words true/yes/on and false/no/off.
73
+ * @param data - frontmatter mapping.
74
+ * @param key - field name.
75
+ * @returns the boolean, undefined when absent.
76
+ */
77
+ function booleanField(data, key) {
78
+ if (!Object.hasOwn(data, key)) return undefined
79
+ const value = data[key]
80
+ if (typeof value === 'boolean') return value
81
+ if (value === 1 || value === '1') return true
82
+ if (value === 0 || value === '0') return false
83
+ if (typeof value === 'string') {
84
+ switch (value.toLowerCase()) {
85
+ case 'true':
86
+ case 'yes':
87
+ case 'on':
88
+ return true
89
+ case 'false':
90
+ case 'no':
91
+ case 'off':
92
+ return false
93
+ }
94
+ }
95
+ throw new TypeError(`${key} must be a boolean`)
96
+ }
97
+
98
+ /**
99
+ * Parse one skill file.
100
+ *
101
+ * The result always carries the fields the panel displays; `problem` is the
102
+ * reason the host would skip the file, and is null for a loadable skill.
103
+ * @param text - the whole file.
104
+ * @returns `{ name, description, whenToUse, modelInvocable, userInvocable, problem, detail }`.
105
+ */
106
+ export function readSkillText(text) {
107
+ const empty = { name: '', description: '', whenToUse: '', modelInvocable: true, userInvocable: true }
108
+ const split = splitFrontmatter(String(text ?? ''))
109
+ if (split === null) return { ...empty, problem: 'no-frontmatter', detail: PROBLEMS['no-frontmatter'] }
110
+
111
+ let data
112
+ try {
113
+ data = parseYaml(split.yaml)
114
+ } catch (error) {
115
+ return { ...empty, problem: 'bad-yaml', detail: `${PROBLEMS['bad-yaml']}(${String(error?.message ?? error).split('\n')[0]})` }
116
+ }
117
+ if (typeof data !== 'object' || data === null || Array.isArray(data)) {
118
+ return { ...empty, problem: 'bad-yaml', detail: PROBLEMS['bad-yaml'] }
119
+ }
120
+
121
+ const name = textField(data, 'name')
122
+ const description = textField(data, 'description')
123
+ const whenToUse = textField(data, 'whenToUse') ?? ''
124
+ const base = { name: name ?? '', description: description ?? '', whenToUse, modelInvocable: true, userInvocable: true }
125
+ if (name === undefined) return { ...base, problem: 'missing-name', detail: PROBLEMS['missing-name'] }
126
+ if (description === undefined) return { ...base, problem: 'missing-description', detail: PROBLEMS['missing-description'] }
127
+ if (!SKILL_NAME.test(name)) return { ...base, problem: 'bad-name', detail: PROBLEMS['bad-name'] }
128
+
129
+ for (const [legacy, canonical] of Object.entries(LEGACY_KEYS)) {
130
+ if (Object.hasOwn(data, legacy)) {
131
+ return { ...base, problem: 'bad-invocation', detail: `前言里的 ${legacy} 已废弃,请改用 ${canonical}。` }
132
+ }
133
+ }
134
+ try {
135
+ const disableModel = booleanField(data, 'disable-model-invocation')
136
+ const userInvocable = booleanField(data, 'user-invocable')
137
+ return {
138
+ name,
139
+ description,
140
+ whenToUse,
141
+ modelInvocable: disableModel !== true,
142
+ userInvocable: userInvocable !== false,
143
+ problem: null,
144
+ detail: null,
145
+ }
146
+ } catch (error) {
147
+ return { ...base, problem: 'bad-invocation', detail: `${PROBLEMS['bad-invocation']}(${String(error?.message ?? error)})` }
148
+ }
149
+ }