dsh-powertools 0.0.0-stage → 0.2.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.
- package/LICENSE +21 -0
- package/README.md +98 -2
- package/client.js +3213 -0
- package/cordis.patch.yml +3 -0
- package/icon.svg +9 -0
- package/index.js +90 -0
- package/lib/credentials.js +173 -0
- package/lib/enhance.js +159 -0
- package/lib/errors.js +11 -0
- package/lib/instructions.js +298 -0
- package/lib/mcp.js +124 -0
- package/lib/platform.js +70 -0
- package/lib/sessions.js +516 -0
- package/lib/skill-repositories.js +152 -0
- package/lib/skills.js +280 -0
- package/lib/tasks.js +244 -0
- package/licenses/lucide.txt +43 -0
- package/licenses/magi-code.txt +203 -0
- package/locale/en.json +1 -0
- package/locale/zh.json +1 -0
- package/node_modules/yaml/LICENSE +13 -0
- package/node_modules/yaml/README.md +172 -0
- package/node_modules/yaml/bin.mjs +11 -0
- package/node_modules/yaml/browser/dist/compose/compose-collection.js +88 -0
- package/node_modules/yaml/browser/dist/compose/compose-doc.js +43 -0
- package/node_modules/yaml/browser/dist/compose/compose-node.js +109 -0
- package/node_modules/yaml/browser/dist/compose/compose-scalar.js +86 -0
- package/node_modules/yaml/browser/dist/compose/composer.js +219 -0
- package/node_modules/yaml/browser/dist/compose/resolve-block-map.js +115 -0
- package/node_modules/yaml/browser/dist/compose/resolve-block-scalar.js +198 -0
- package/node_modules/yaml/browser/dist/compose/resolve-block-seq.js +49 -0
- package/node_modules/yaml/browser/dist/compose/resolve-end.js +37 -0
- package/node_modules/yaml/browser/dist/compose/resolve-flow-collection.js +207 -0
- package/node_modules/yaml/browser/dist/compose/resolve-flow-scalar.js +226 -0
- package/node_modules/yaml/browser/dist/compose/resolve-props.js +146 -0
- package/node_modules/yaml/browser/dist/compose/util-contains-newline.js +34 -0
- package/node_modules/yaml/browser/dist/compose/util-empty-scalar-position.js +26 -0
- package/node_modules/yaml/browser/dist/compose/util-flow-indent-check.js +15 -0
- package/node_modules/yaml/browser/dist/compose/util-map-includes.js +13 -0
- package/node_modules/yaml/browser/dist/doc/Document.js +335 -0
- package/node_modules/yaml/browser/dist/doc/anchors.js +71 -0
- package/node_modules/yaml/browser/dist/doc/applyReviver.js +55 -0
- package/node_modules/yaml/browser/dist/doc/createNode.js +88 -0
- package/node_modules/yaml/browser/dist/doc/directives.js +176 -0
- package/node_modules/yaml/browser/dist/errors.js +57 -0
- package/node_modules/yaml/browser/dist/index.js +17 -0
- package/node_modules/yaml/browser/dist/log.js +11 -0
- package/node_modules/yaml/browser/dist/nodes/Alias.js +118 -0
- package/node_modules/yaml/browser/dist/nodes/Collection.js +147 -0
- package/node_modules/yaml/browser/dist/nodes/Node.js +38 -0
- package/node_modules/yaml/browser/dist/nodes/Pair.js +36 -0
- package/node_modules/yaml/browser/dist/nodes/Scalar.js +24 -0
- package/node_modules/yaml/browser/dist/nodes/YAMLMap.js +144 -0
- package/node_modules/yaml/browser/dist/nodes/YAMLSeq.js +113 -0
- package/node_modules/yaml/browser/dist/nodes/addPairToJSMap.js +63 -0
- package/node_modules/yaml/browser/dist/nodes/identity.js +36 -0
- package/node_modules/yaml/browser/dist/nodes/toJS.js +37 -0
- package/node_modules/yaml/browser/dist/parse/cst-scalar.js +214 -0
- package/node_modules/yaml/browser/dist/parse/cst-stringify.js +61 -0
- package/node_modules/yaml/browser/dist/parse/cst-visit.js +97 -0
- package/node_modules/yaml/browser/dist/parse/cst.js +98 -0
- package/node_modules/yaml/browser/dist/parse/lexer.js +721 -0
- package/node_modules/yaml/browser/dist/parse/line-counter.js +39 -0
- package/node_modules/yaml/browser/dist/parse/parser.js +975 -0
- package/node_modules/yaml/browser/dist/public-api.js +102 -0
- package/node_modules/yaml/browser/dist/schema/Schema.js +37 -0
- package/node_modules/yaml/browser/dist/schema/common/map.js +17 -0
- package/node_modules/yaml/browser/dist/schema/common/null.js +15 -0
- package/node_modules/yaml/browser/dist/schema/common/seq.js +17 -0
- package/node_modules/yaml/browser/dist/schema/common/string.js +14 -0
- package/node_modules/yaml/browser/dist/schema/core/bool.js +19 -0
- package/node_modules/yaml/browser/dist/schema/core/float.js +43 -0
- package/node_modules/yaml/browser/dist/schema/core/int.js +38 -0
- package/node_modules/yaml/browser/dist/schema/core/schema.js +23 -0
- package/node_modules/yaml/browser/dist/schema/json/schema.js +62 -0
- package/node_modules/yaml/browser/dist/schema/tags.js +96 -0
- package/node_modules/yaml/browser/dist/schema/yaml-1.1/binary.js +58 -0
- package/node_modules/yaml/browser/dist/schema/yaml-1.1/bool.js +26 -0
- package/node_modules/yaml/browser/dist/schema/yaml-1.1/float.js +46 -0
- package/node_modules/yaml/browser/dist/schema/yaml-1.1/int.js +71 -0
- package/node_modules/yaml/browser/dist/schema/yaml-1.1/merge.js +67 -0
- package/node_modules/yaml/browser/dist/schema/yaml-1.1/omap.js +74 -0
- package/node_modules/yaml/browser/dist/schema/yaml-1.1/pairs.js +78 -0
- package/node_modules/yaml/browser/dist/schema/yaml-1.1/schema.js +39 -0
- package/node_modules/yaml/browser/dist/schema/yaml-1.1/set.js +93 -0
- package/node_modules/yaml/browser/dist/schema/yaml-1.1/timestamp.js +101 -0
- package/node_modules/yaml/browser/dist/stringify/foldFlowLines.js +146 -0
- package/node_modules/yaml/browser/dist/stringify/stringify.js +129 -0
- package/node_modules/yaml/browser/dist/stringify/stringifyCollection.js +153 -0
- package/node_modules/yaml/browser/dist/stringify/stringifyComment.js +20 -0
- package/node_modules/yaml/browser/dist/stringify/stringifyDocument.js +85 -0
- package/node_modules/yaml/browser/dist/stringify/stringifyNumber.js +25 -0
- package/node_modules/yaml/browser/dist/stringify/stringifyPair.js +150 -0
- package/node_modules/yaml/browser/dist/stringify/stringifyString.js +336 -0
- package/node_modules/yaml/browser/dist/util.js +11 -0
- package/node_modules/yaml/browser/dist/visit.js +233 -0
- package/node_modules/yaml/browser/index.js +5 -0
- package/node_modules/yaml/browser/package.json +3 -0
- package/node_modules/yaml/dist/cli.d.ts +8 -0
- package/node_modules/yaml/dist/cli.mjs +201 -0
- package/node_modules/yaml/dist/compose/compose-collection.d.ts +11 -0
- package/node_modules/yaml/dist/compose/compose-collection.js +90 -0
- package/node_modules/yaml/dist/compose/compose-doc.d.ts +7 -0
- package/node_modules/yaml/dist/compose/compose-doc.js +45 -0
- package/node_modules/yaml/dist/compose/compose-node.d.ts +29 -0
- package/node_modules/yaml/dist/compose/compose-node.js +112 -0
- package/node_modules/yaml/dist/compose/compose-scalar.d.ts +5 -0
- package/node_modules/yaml/dist/compose/compose-scalar.js +88 -0
- package/node_modules/yaml/dist/compose/composer.d.ts +63 -0
- package/node_modules/yaml/dist/compose/composer.js +224 -0
- package/node_modules/yaml/dist/compose/resolve-block-map.d.ts +6 -0
- package/node_modules/yaml/dist/compose/resolve-block-map.js +117 -0
- package/node_modules/yaml/dist/compose/resolve-block-scalar.d.ts +11 -0
- package/node_modules/yaml/dist/compose/resolve-block-scalar.js +200 -0
- package/node_modules/yaml/dist/compose/resolve-block-seq.d.ts +6 -0
- package/node_modules/yaml/dist/compose/resolve-block-seq.js +51 -0
- package/node_modules/yaml/dist/compose/resolve-end.d.ts +6 -0
- package/node_modules/yaml/dist/compose/resolve-end.js +39 -0
- package/node_modules/yaml/dist/compose/resolve-flow-collection.d.ts +7 -0
- package/node_modules/yaml/dist/compose/resolve-flow-collection.js +209 -0
- package/node_modules/yaml/dist/compose/resolve-flow-scalar.d.ts +10 -0
- package/node_modules/yaml/dist/compose/resolve-flow-scalar.js +228 -0
- package/node_modules/yaml/dist/compose/resolve-props.d.ts +23 -0
- package/node_modules/yaml/dist/compose/resolve-props.js +148 -0
- package/node_modules/yaml/dist/compose/util-contains-newline.d.ts +2 -0
- package/node_modules/yaml/dist/compose/util-contains-newline.js +36 -0
- package/node_modules/yaml/dist/compose/util-empty-scalar-position.d.ts +2 -0
- package/node_modules/yaml/dist/compose/util-empty-scalar-position.js +28 -0
- package/node_modules/yaml/dist/compose/util-flow-indent-check.d.ts +3 -0
- package/node_modules/yaml/dist/compose/util-flow-indent-check.js +17 -0
- package/node_modules/yaml/dist/compose/util-map-includes.d.ts +4 -0
- package/node_modules/yaml/dist/compose/util-map-includes.js +15 -0
- package/node_modules/yaml/dist/doc/Document.d.ts +141 -0
- package/node_modules/yaml/dist/doc/Document.js +337 -0
- package/node_modules/yaml/dist/doc/anchors.d.ts +24 -0
- package/node_modules/yaml/dist/doc/anchors.js +76 -0
- package/node_modules/yaml/dist/doc/applyReviver.d.ts +9 -0
- package/node_modules/yaml/dist/doc/applyReviver.js +57 -0
- package/node_modules/yaml/dist/doc/createNode.d.ts +17 -0
- package/node_modules/yaml/dist/doc/createNode.js +90 -0
- package/node_modules/yaml/dist/doc/directives.d.ts +49 -0
- package/node_modules/yaml/dist/doc/directives.js +178 -0
- package/node_modules/yaml/dist/errors.d.ts +21 -0
- package/node_modules/yaml/dist/errors.js +62 -0
- package/node_modules/yaml/dist/index.d.ts +25 -0
- package/node_modules/yaml/dist/index.js +50 -0
- package/node_modules/yaml/dist/log.d.ts +3 -0
- package/node_modules/yaml/dist/log.js +19 -0
- package/node_modules/yaml/dist/nodes/Alias.d.ts +29 -0
- package/node_modules/yaml/dist/nodes/Alias.js +120 -0
- package/node_modules/yaml/dist/nodes/Collection.d.ts +73 -0
- package/node_modules/yaml/dist/nodes/Collection.js +151 -0
- package/node_modules/yaml/dist/nodes/Node.d.ts +53 -0
- package/node_modules/yaml/dist/nodes/Node.js +40 -0
- package/node_modules/yaml/dist/nodes/Pair.d.ts +22 -0
- package/node_modules/yaml/dist/nodes/Pair.js +39 -0
- package/node_modules/yaml/dist/nodes/Scalar.d.ts +47 -0
- package/node_modules/yaml/dist/nodes/Scalar.js +27 -0
- package/node_modules/yaml/dist/nodes/YAMLMap.d.ts +53 -0
- package/node_modules/yaml/dist/nodes/YAMLMap.js +147 -0
- package/node_modules/yaml/dist/nodes/YAMLSeq.d.ts +60 -0
- package/node_modules/yaml/dist/nodes/YAMLSeq.js +115 -0
- package/node_modules/yaml/dist/nodes/addPairToJSMap.d.ts +4 -0
- package/node_modules/yaml/dist/nodes/addPairToJSMap.js +65 -0
- package/node_modules/yaml/dist/nodes/identity.d.ts +23 -0
- package/node_modules/yaml/dist/nodes/identity.js +53 -0
- package/node_modules/yaml/dist/nodes/toJS.d.ts +29 -0
- package/node_modules/yaml/dist/nodes/toJS.js +39 -0
- package/node_modules/yaml/dist/options.d.ts +350 -0
- package/node_modules/yaml/dist/parse/cst-scalar.d.ts +64 -0
- package/node_modules/yaml/dist/parse/cst-scalar.js +218 -0
- package/node_modules/yaml/dist/parse/cst-stringify.d.ts +8 -0
- package/node_modules/yaml/dist/parse/cst-stringify.js +63 -0
- package/node_modules/yaml/dist/parse/cst-visit.d.ts +39 -0
- package/node_modules/yaml/dist/parse/cst-visit.js +99 -0
- package/node_modules/yaml/dist/parse/cst.d.ts +109 -0
- package/node_modules/yaml/dist/parse/cst.js +112 -0
- package/node_modules/yaml/dist/parse/lexer.d.ts +87 -0
- package/node_modules/yaml/dist/parse/lexer.js +723 -0
- package/node_modules/yaml/dist/parse/line-counter.d.ts +22 -0
- package/node_modules/yaml/dist/parse/line-counter.js +41 -0
- package/node_modules/yaml/dist/parse/parser.d.ts +84 -0
- package/node_modules/yaml/dist/parse/parser.js +980 -0
- package/node_modules/yaml/dist/public-api.d.ts +44 -0
- package/node_modules/yaml/dist/public-api.js +107 -0
- package/node_modules/yaml/dist/schema/Schema.d.ts +17 -0
- package/node_modules/yaml/dist/schema/Schema.js +39 -0
- package/node_modules/yaml/dist/schema/common/map.d.ts +2 -0
- package/node_modules/yaml/dist/schema/common/map.js +19 -0
- package/node_modules/yaml/dist/schema/common/null.d.ts +4 -0
- package/node_modules/yaml/dist/schema/common/null.js +17 -0
- package/node_modules/yaml/dist/schema/common/seq.d.ts +2 -0
- package/node_modules/yaml/dist/schema/common/seq.js +19 -0
- package/node_modules/yaml/dist/schema/common/string.d.ts +2 -0
- package/node_modules/yaml/dist/schema/common/string.js +16 -0
- package/node_modules/yaml/dist/schema/core/bool.d.ts +4 -0
- package/node_modules/yaml/dist/schema/core/bool.js +21 -0
- package/node_modules/yaml/dist/schema/core/float.d.ts +4 -0
- package/node_modules/yaml/dist/schema/core/float.js +47 -0
- package/node_modules/yaml/dist/schema/core/int.d.ts +4 -0
- package/node_modules/yaml/dist/schema/core/int.js +42 -0
- package/node_modules/yaml/dist/schema/core/schema.d.ts +1 -0
- package/node_modules/yaml/dist/schema/core/schema.js +25 -0
- package/node_modules/yaml/dist/schema/json/schema.d.ts +2 -0
- package/node_modules/yaml/dist/schema/json/schema.js +64 -0
- package/node_modules/yaml/dist/schema/json-schema.d.ts +69 -0
- package/node_modules/yaml/dist/schema/tags.d.ts +48 -0
- package/node_modules/yaml/dist/schema/tags.js +99 -0
- package/node_modules/yaml/dist/schema/types.d.ts +92 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/binary.d.ts +2 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/binary.js +70 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/bool.d.ts +7 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/bool.js +29 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/float.d.ts +4 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/float.js +50 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/int.d.ts +5 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/int.js +76 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/merge.d.ts +9 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/merge.js +71 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/omap.d.ts +22 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/omap.js +77 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/pairs.d.ts +10 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/pairs.js +82 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/schema.d.ts +1 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/schema.js +41 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/set.d.ts +28 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/set.js +96 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/timestamp.d.ts +6 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/timestamp.js +105 -0
- package/node_modules/yaml/dist/stringify/foldFlowLines.d.ts +34 -0
- package/node_modules/yaml/dist/stringify/foldFlowLines.js +151 -0
- package/node_modules/yaml/dist/stringify/stringify.d.ts +21 -0
- package/node_modules/yaml/dist/stringify/stringify.js +132 -0
- package/node_modules/yaml/dist/stringify/stringifyCollection.d.ts +17 -0
- package/node_modules/yaml/dist/stringify/stringifyCollection.js +155 -0
- package/node_modules/yaml/dist/stringify/stringifyComment.d.ts +10 -0
- package/node_modules/yaml/dist/stringify/stringifyComment.js +24 -0
- package/node_modules/yaml/dist/stringify/stringifyDocument.d.ts +4 -0
- package/node_modules/yaml/dist/stringify/stringifyDocument.js +87 -0
- package/node_modules/yaml/dist/stringify/stringifyNumber.d.ts +2 -0
- package/node_modules/yaml/dist/stringify/stringifyNumber.js +27 -0
- package/node_modules/yaml/dist/stringify/stringifyPair.d.ts +3 -0
- package/node_modules/yaml/dist/stringify/stringifyPair.js +152 -0
- package/node_modules/yaml/dist/stringify/stringifyString.d.ts +9 -0
- package/node_modules/yaml/dist/stringify/stringifyString.js +338 -0
- package/node_modules/yaml/dist/test-events.d.ts +4 -0
- package/node_modules/yaml/dist/test-events.js +134 -0
- package/node_modules/yaml/dist/util.d.ts +16 -0
- package/node_modules/yaml/dist/util.js +28 -0
- package/node_modules/yaml/dist/visit.d.ts +102 -0
- package/node_modules/yaml/dist/visit.js +236 -0
- package/node_modules/yaml/package.json +97 -0
- package/node_modules/yaml/util.js +2 -0
- package/package.json +74 -4
package/lib/skills.js
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { skillRepositories } from './skill-repositories.js'
|
|
2
|
+
import { constants } from 'node:fs'
|
|
3
|
+
import { access, copyFile, lstat, mkdir, mkdtemp, open, readdir, rm, writeFile } from 'node:fs/promises'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
import { createHash, randomUUID } from 'node:crypto'
|
|
6
|
+
import { parseDocument } from 'yaml'
|
|
7
|
+
import { requireValue } from './errors.js'
|
|
8
|
+
import { confinedPath, homeRoot, inside, moveLocal, readOpenFlags, statOrMissing } from './platform.js'
|
|
9
|
+
|
|
10
|
+
const maxMarkdown = 512 * 1024
|
|
11
|
+
const filesystemSources = new Set(['project-dsh', 'project-agents', 'user-dsh', 'user-agents'])
|
|
12
|
+
const validName = name => typeof name === 'string' && name.length <= 100 && /^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(name)
|
|
13
|
+
const digest = text => createHash('sha256').update(text).digest('hex')
|
|
14
|
+
|
|
15
|
+
export function parseSkill(text) {
|
|
16
|
+
const match = /^---\r?\n([\s\S]*?)^---(?:\r?\n|$)/m.exec(text)
|
|
17
|
+
requireValue(match?.index === 0, 'SKILL.md 缺少 YAML frontmatter。')
|
|
18
|
+
const document = parseDocument(match[1])
|
|
19
|
+
requireValue(!document.errors.length, 'SKILL.md 的 YAML frontmatter 无效。')
|
|
20
|
+
const data = document.toJS()
|
|
21
|
+
requireValue(data && validName(data.name) && typeof data.description === 'string' && data.description.trim(), 'Skill 须有有效的 name 和 description。')
|
|
22
|
+
requireValue(!['disableModelInvocation', 'modelInvocable', 'userInvocable'].some(key => Object.hasOwn(data, key)), '请使用 disable-model-invocation 和 user-invocable。')
|
|
23
|
+
for (const key of ['disable-model-invocation', 'user-invocable']) {
|
|
24
|
+
if (!Object.hasOwn(data, key)) continue
|
|
25
|
+
requireValue(['true', 'false', '1', '0', 'yes', 'no', 'on', 'off'].includes(String(data[key]).toLowerCase()), `${key} 值无效。`)
|
|
26
|
+
}
|
|
27
|
+
return { document, data, body: text.slice(match[0].length), eol: text.startsWith('---\r\n') ? '\r\n' : '\n' }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function readSkillFile(file) {
|
|
31
|
+
const info = await lstat(file)
|
|
32
|
+
requireValue(info.isFile() && info.nlink === 1 && info.size <= maxMarkdown, 'SKILL.md 须为普通文件,且不超过 512 KiB。', 409)
|
|
33
|
+
const handle = await open(file, readOpenFlags())
|
|
34
|
+
try {
|
|
35
|
+
const opened = await handle.stat()
|
|
36
|
+
requireValue(opened.isFile() && opened.ino === info.ino && opened.dev === info.dev && opened.nlink === 1 && opened.size <= maxMarkdown, 'SKILL.md 已变化,请刷新。', 409)
|
|
37
|
+
const buffer = Buffer.alloc(maxMarkdown + 1)
|
|
38
|
+
const { bytesRead } = await handle.read({ buffer, length: buffer.length })
|
|
39
|
+
requireValue(bytesRead <= maxMarkdown, 'SKILL.md 超过 512 KiB。', 409)
|
|
40
|
+
return { text: buffer.subarray(0, bytesRead).toString('utf8'), mode: opened.mode & 0o777 }
|
|
41
|
+
} finally { await handle.close() }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function projectRoot(cwd) {
|
|
45
|
+
for (let current = cwd; ; current = path.dirname(current)) {
|
|
46
|
+
if (await statOrMissing(path.join(current, '.git'))) return current
|
|
47
|
+
if (path.dirname(current) === current) return cwd
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function rootsFor(home, cwd) {
|
|
52
|
+
const project = cwd ? await projectRoot(cwd) : undefined
|
|
53
|
+
return {
|
|
54
|
+
'user-dsh': path.join(home ?? homeRoot(process.env.DSH_HOME, '.dsh'), 'skills'),
|
|
55
|
+
'user-agents': path.join(homeRoot(process.env.DSH_AGENTS_HOME, '.agents'), 'skills'),
|
|
56
|
+
...(project && {
|
|
57
|
+
'project-dsh': path.join(project, '.dsh', 'skills'),
|
|
58
|
+
'project-agents': path.join(project, '.agents', 'skills'),
|
|
59
|
+
}),
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function editableFile(skill, roots) {
|
|
64
|
+
const root = roots[skill.source]
|
|
65
|
+
if (skill.provider !== 'filesystem' || !root || !skill.path || !inside(root, skill.path)) return undefined
|
|
66
|
+
const segments = path.relative(root, skill.path).split(path.sep)
|
|
67
|
+
if (segments[0] === '.system') return undefined
|
|
68
|
+
if (!(segments.length === 1 || segments.length === 2 && segments[1] === 'SKILL.md')) return undefined
|
|
69
|
+
const target = segments.length === 1 ? skill.path : path.dirname(skill.path)
|
|
70
|
+
try {
|
|
71
|
+
if (!(await lstat(root)).isDirectory() || !(await lstat(path.dirname(root))).isDirectory()) return undefined
|
|
72
|
+
const info = await lstat(skill.path)
|
|
73
|
+
const targetInfo = await lstat(target)
|
|
74
|
+
if (info.isFile() && info.nlink === 1 && !targetInfo.isSymbolicLink() && await confinedPath(root, skill.path)) {
|
|
75
|
+
await access(skill.path, constants.W_OK)
|
|
76
|
+
await access(path.dirname(skill.path), constants.W_OK)
|
|
77
|
+
return { file: skill.path, target, root }
|
|
78
|
+
}
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (!['ENOENT', 'EACCES', 'EPERM', 'EROFS'].includes(error.code)) throw error
|
|
81
|
+
}
|
|
82
|
+
return undefined
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function safeSkillRoot(root) {
|
|
86
|
+
// The resolved workspace/home may itself be a link. New discovery folders
|
|
87
|
+
// below it must be real directories, so an import cannot escape that anchor.
|
|
88
|
+
const parent = path.dirname(root)
|
|
89
|
+
await mkdir(parent, { recursive: true })
|
|
90
|
+
requireValue((await lstat(parent)).isDirectory(), 'Skill 目录的上级必须是本地目录。', 409)
|
|
91
|
+
await mkdir(root, { recursive: true })
|
|
92
|
+
requireValue((await lstat(root)).isDirectory(), 'Skill 根目录必须是本地目录。', 409)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async function importFolder(source, destination) {
|
|
96
|
+
let size = 0
|
|
97
|
+
let count = 0
|
|
98
|
+
async function copy(from, to, depth) {
|
|
99
|
+
requireValue(depth <= 20, 'Skill 目录层级过深。')
|
|
100
|
+
for (const item of await readdir(from, { withFileTypes: true })) {
|
|
101
|
+
requireValue(++count <= 500, 'Skill 文件数量超过 500。')
|
|
102
|
+
if (item.name === '.git') continue
|
|
103
|
+
const input = path.join(from, item.name)
|
|
104
|
+
const output = path.join(to, item.name)
|
|
105
|
+
if (item.isDirectory()) {
|
|
106
|
+
await mkdir(output)
|
|
107
|
+
await copy(input, output, depth + 1)
|
|
108
|
+
} else {
|
|
109
|
+
requireValue(item.isFile(), 'Skill 导入目录仅支持普通文件和目录。')
|
|
110
|
+
const info = await lstat(input)
|
|
111
|
+
requireValue(info.isFile() && info.nlink === 1, 'Skill 导入目录仅支持独立的普通文件。')
|
|
112
|
+
size += info.size
|
|
113
|
+
requireValue(size <= 32 * 1024 * 1024, 'Skill 导入目录超过 32 MiB。')
|
|
114
|
+
await copyFile(input, output, constants.COPYFILE_EXCL)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
await copy(source, destination, 0)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function listSkillFiles(target, skillFile) {
|
|
122
|
+
const info = await statOrMissing(target)
|
|
123
|
+
if (!info?.isDirectory()) return []
|
|
124
|
+
const files = []
|
|
125
|
+
async function walk(dir, prefix, depth) {
|
|
126
|
+
if (depth > 5 || files.length >= 100) return
|
|
127
|
+
const entries = (await readdir(dir, { withFileTypes: true })).sort((a, b) => a.name.localeCompare(b.name))
|
|
128
|
+
for (const item of entries) {
|
|
129
|
+
if (files.length >= 100) break
|
|
130
|
+
if (item.name.startsWith('.powertools-')) continue
|
|
131
|
+
const full = path.join(dir, item.name)
|
|
132
|
+
const rel = prefix ? `${prefix}/${item.name}` : item.name
|
|
133
|
+
if (item.isDirectory()) {
|
|
134
|
+
await walk(full, rel, depth + 1)
|
|
135
|
+
} else if (item.isFile() && full !== skillFile) {
|
|
136
|
+
const stat = await statOrMissing(full)
|
|
137
|
+
if (stat?.isFile()) files.push({ path: rel, size: stat.size })
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
await walk(target, '', 0)
|
|
142
|
+
return files
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function summarizeFrontmatter(data) {
|
|
146
|
+
if (!data || typeof data !== 'object') return []
|
|
147
|
+
const skip = new Set(['name', 'description', 'disable-model-invocation', 'user-invocable'])
|
|
148
|
+
return Object.entries(data)
|
|
149
|
+
.filter(([key]) => !skip.has(key))
|
|
150
|
+
.map(([key, value]) => ({
|
|
151
|
+
key,
|
|
152
|
+
value: typeof value === 'string' ? value : JSON.stringify(value),
|
|
153
|
+
}))
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function skillOperations(ctx) {
|
|
157
|
+
const repository = skillRepositories(ctx.profileContext.dir, readSkillFile, parseSkill)
|
|
158
|
+
let tail = Promise.resolve()
|
|
159
|
+
let invalidate = () => {}
|
|
160
|
+
if (typeof ctx.skills.registerProvider === 'function') {
|
|
161
|
+
ctx.skills.registerProvider(control => {
|
|
162
|
+
invalidate = control.invalidate
|
|
163
|
+
return { name: 'dsh-powertools-changes', list: async () => [], get: async () => undefined }
|
|
164
|
+
})
|
|
165
|
+
}
|
|
166
|
+
async function withCatalog(body, signal, operation) {
|
|
167
|
+
const workspace = body.workspaceId ? ctx.workspaceRegistry.get(body.workspaceId) : undefined
|
|
168
|
+
requireValue(!body.workspaceId || workspace, '工作区已移除,请刷新。', 404)
|
|
169
|
+
const session = workspace?.sessionIds.includes(body.sessionId) ? ctx.agents.get(body.sessionId) : undefined
|
|
170
|
+
const presets = ctx.agentPresets
|
|
171
|
+
const registry = (session && presets?.serviceFor?.(session, 'skills')) ?? ctx.skills
|
|
172
|
+
const lease = session ? undefined : await presets?.acquireScope?.()
|
|
173
|
+
try {
|
|
174
|
+
const lookup = { cwd: workspace?.path, scope: session ?? lease?.key, signal }
|
|
175
|
+
const roots = await rootsFor(ctx.profileContext?.home, lookup.cwd)
|
|
176
|
+
return await operation(registry, lookup, roots)
|
|
177
|
+
} finally { await lease?.[Symbol.asyncDispose]() }
|
|
178
|
+
}
|
|
179
|
+
return (body, signal) => {
|
|
180
|
+
const run = () => {
|
|
181
|
+
signal.throwIfAborted()
|
|
182
|
+
if (typeof body.action === 'string' && body.action.startsWith('repository-') && body.action !== 'repository-install') {
|
|
183
|
+
return repository(body, signal)
|
|
184
|
+
}
|
|
185
|
+
return withCatalog(body, signal, async (registry, lookup, roots) => {
|
|
186
|
+
signal.throwIfAborted()
|
|
187
|
+
if (body.action === 'list') {
|
|
188
|
+
const skills = await registry.list(lookup)
|
|
189
|
+
const rows = await Promise.all(skills.map(async skill => ({
|
|
190
|
+
name: skill.name, description: skill.description, source: skill.source,
|
|
191
|
+
provider: skill.provider, ...skill.invocation,
|
|
192
|
+
editable: filesystemSources.has(skill.source) && Boolean(await editableFile(skill, roots)),
|
|
193
|
+
})))
|
|
194
|
+
return { skills: rows }
|
|
195
|
+
}
|
|
196
|
+
if (body.action === 'import' || body.action === 'repository-install') {
|
|
197
|
+
const root = body.destination === 'project' ? roots['project-dsh'] : body.destination === 'personal' ? roots['user-dsh'] : undefined
|
|
198
|
+
requireValue(root, '请选择可用的导入位置。')
|
|
199
|
+
const fromRepository = body.action === 'repository-install'
|
|
200
|
+
const directory = fromRepository ? (await repository(body, signal)).directory : body.directory
|
|
201
|
+
requireValue(typeof directory === 'string' && path.isAbsolute(directory), '请输入本地 Skill 目录的绝对路径。')
|
|
202
|
+
const source = path.resolve(directory)
|
|
203
|
+
requireValue((await lstat(source)).isDirectory(), '请选择包含 SKILL.md 的普通目录。')
|
|
204
|
+
const { text } = await readSkillFile(path.join(source, 'SKILL.md'))
|
|
205
|
+
const { data } = parseSkill(text)
|
|
206
|
+
requireValue(!(await registry.list(lookup)).some(skill => skill.name === data.name), '当前目录范围中已有同名 Skill。', 409)
|
|
207
|
+
const destination = path.join(root, data.name)
|
|
208
|
+
requireValue(!inside(source, destination) && source !== destination, '导入目标不能在源目录内。')
|
|
209
|
+
await safeSkillRoot(root)
|
|
210
|
+
const staging = await mkdtemp(path.join(path.dirname(root), '.powertools-import-'))
|
|
211
|
+
try {
|
|
212
|
+
await importFolder(source, staging)
|
|
213
|
+
const copied = await readSkillFile(path.join(staging, 'SKILL.md'))
|
|
214
|
+
requireValue(copied.text === text, '导入时 SKILL.md 已变化,请重试。', 409)
|
|
215
|
+
signal.throwIfAborted()
|
|
216
|
+
requireValue(!await statOrMissing(destination), '目标目录已存在。', 409)
|
|
217
|
+
await moveLocal(staging, destination)
|
|
218
|
+
} finally { await rm(staging, { recursive: true, force: true }) }
|
|
219
|
+
invalidate()
|
|
220
|
+
return { name: data.name }
|
|
221
|
+
}
|
|
222
|
+
requireValue(validName(body.name), 'Skill 名称无效。')
|
|
223
|
+
const skill = await registry.get(body.name, lookup)
|
|
224
|
+
requireValue(skill, 'Skill 已变化,请刷新。', 404)
|
|
225
|
+
const writable = await editableFile(skill, roots)
|
|
226
|
+
if (body.action === 'preview' && !writable) {
|
|
227
|
+
const content = skill.content ?? ''
|
|
228
|
+
return { name: skill.name, markdown: content, bytes: Buffer.byteLength(content), raw: false, editable: false, files: [], meta: [], ...skill.invocation }
|
|
229
|
+
}
|
|
230
|
+
requireValue(writable, '此 Skill 的来源不支持写入。', 409)
|
|
231
|
+
const { text, mode } = await readSkillFile(writable.file)
|
|
232
|
+
const { document, data, body: markdown, eol } = parseSkill(text)
|
|
233
|
+
requireValue(data.name === body.name, 'Skill 已变化,请刷新。', 409)
|
|
234
|
+
const revision = digest(text)
|
|
235
|
+
if (body.action === 'preview') {
|
|
236
|
+
const files = await listSkillFiles(writable.target, writable.file)
|
|
237
|
+
return { name: skill.name, markdown: text, bytes: Buffer.byteLength(text), raw: true, editable: true, revision,
|
|
238
|
+
description: data.description, body: markdown, files, meta: summarizeFrontmatter(data), ...skill.invocation }
|
|
239
|
+
}
|
|
240
|
+
requireValue(body.revision === revision, '文件已被修改,请重新打开 Skill。', 409)
|
|
241
|
+
if (body.action === 'remove') {
|
|
242
|
+
requireValue(body.confirm === skill.name, '请确认要移除的 Skill。', 409)
|
|
243
|
+
const recovery = await mkdtemp(path.join(path.dirname(writable.root), 'powertools-recovery-'))
|
|
244
|
+
try {
|
|
245
|
+
signal.throwIfAborted()
|
|
246
|
+
await moveLocal(writable.target, path.join(recovery, path.basename(writable.target)))
|
|
247
|
+
} catch (error) {
|
|
248
|
+
await rm(recovery, { recursive: true, force: true })
|
|
249
|
+
throw error
|
|
250
|
+
}
|
|
251
|
+
invalidate()
|
|
252
|
+
return { recovery }
|
|
253
|
+
}
|
|
254
|
+
requireValue(body.action === 'edit', 'Skill 操作无效。')
|
|
255
|
+
requireValue(typeof body.description === 'string' && body.description.trim() && body.description.length <= 8000, '请输入 Skill 描述。')
|
|
256
|
+
requireValue(typeof body.body === 'string' && typeof body.modelInvocable === 'boolean' && typeof body.userInvocable === 'boolean', 'Skill 内容或调用权限无效。')
|
|
257
|
+
if (body.description !== data.description) document.set('description', body.description)
|
|
258
|
+
if (body.modelInvocable !== skill.invocation.modelInvocable) document.set('disable-model-invocation', !body.modelInvocable)
|
|
259
|
+
if (body.userInvocable !== skill.invocation.userInvocable) document.set('user-invocable', body.userInvocable)
|
|
260
|
+
const yaml = document.toString({ lineWidth: 0 }).replace(/\n/g, eol)
|
|
261
|
+
const frontmatter = yaml.endsWith(eol) ? yaml : `${yaml}${eol}`
|
|
262
|
+
const next = `---${eol}${frontmatter}---${eol}${body.body}`
|
|
263
|
+
requireValue(Buffer.byteLength(next) <= maxMarkdown, 'SKILL.md 超过 512 KiB。')
|
|
264
|
+
const temporary = path.join(path.dirname(writable.file), `.powertools-${randomUUID()}.tmp`)
|
|
265
|
+
try {
|
|
266
|
+
await writeFile(temporary, next, { flag: 'wx', mode })
|
|
267
|
+
signal.throwIfAborted()
|
|
268
|
+
requireValue((await readSkillFile(writable.file)).text === text, '文件已被修改,请重新打开 Skill。', 409)
|
|
269
|
+
await moveLocal(temporary, writable.file)
|
|
270
|
+
} finally { await rm(temporary, { force: true }) }
|
|
271
|
+
invalidate()
|
|
272
|
+
return {}
|
|
273
|
+
})
|
|
274
|
+
}
|
|
275
|
+
if (['list', 'preview', 'repository-list'].includes(body.action)) return run()
|
|
276
|
+
const pending = tail.then(run)
|
|
277
|
+
tail = pending.catch(() => {})
|
|
278
|
+
return pending
|
|
279
|
+
}
|
|
280
|
+
}
|
package/lib/tasks.js
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { RequestError, requireValue } from './errors.js'
|
|
2
|
+
|
|
3
|
+
const liveJobStates = new Set(['running', 'stopping'])
|
|
4
|
+
const maxJobOutputBytes = 16 * 1024
|
|
5
|
+
|
|
6
|
+
function projectJob(view) {
|
|
7
|
+
return {
|
|
8
|
+
id: view.id,
|
|
9
|
+
kind: view.kind ?? 'job',
|
|
10
|
+
label: view.label ?? view.id,
|
|
11
|
+
owner: view.owner ?? null,
|
|
12
|
+
status: view.status,
|
|
13
|
+
progress: view.progress ?? undefined,
|
|
14
|
+
detail: view.detail ?? undefined,
|
|
15
|
+
exitCode: Number.isInteger(view.exitCode) ? view.exitCode : undefined,
|
|
16
|
+
startedAt: view.startedAt ?? 0,
|
|
17
|
+
finishedAt: view.finishedAt ?? undefined,
|
|
18
|
+
output: {
|
|
19
|
+
total: view.output?.total ?? 0,
|
|
20
|
+
earliest: view.output?.earliest ?? 0,
|
|
21
|
+
hasSpill: Boolean(view.output?.spillPaths?.length),
|
|
22
|
+
},
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function collectAllJobs(ctx) {
|
|
27
|
+
const byId = new Map()
|
|
28
|
+
const addList = list => {
|
|
29
|
+
if (!Array.isArray(list)) return
|
|
30
|
+
for (const view of list) {
|
|
31
|
+
if (view?.id && !byId.has(view.id)) byId.set(view.id, view)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
addList(ctx.jobs.list())
|
|
35
|
+
const sessions = typeof ctx.sessions?.list === 'function' ? ctx.sessions.list() : []
|
|
36
|
+
for (const session of sessions) {
|
|
37
|
+
const id = session?.header?.id
|
|
38
|
+
if (id) addList(ctx.jobs.list(id))
|
|
39
|
+
}
|
|
40
|
+
return [...byId.values()].sort((a, b) => {
|
|
41
|
+
const aLive = liveJobStates.has(a.status) ? 0 : 1
|
|
42
|
+
const bLive = liveJobStates.has(b.status) ? 0 : 1
|
|
43
|
+
if (aLive !== bLive) return aLive - bLive
|
|
44
|
+
return (b.startedAt ?? 0) - (a.startedAt ?? 0) || String(a.id).localeCompare(String(b.id))
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function jobOperations(ctx) {
|
|
49
|
+
return (body, signal) => {
|
|
50
|
+
signal?.throwIfAborted()
|
|
51
|
+
if (body.action === 'list') {
|
|
52
|
+
return { jobs: collectAllJobs(ctx).map(projectJob) }
|
|
53
|
+
}
|
|
54
|
+
const id = typeof body.id === 'string' ? body.id.trim() : ''
|
|
55
|
+
requireValue(id, '请指定后台任务 ID。')
|
|
56
|
+
const all = collectAllJobs(ctx)
|
|
57
|
+
const found = all.find(item => item.id === id)
|
|
58
|
+
requireValue(found, '后台任务已不存在,请刷新列表。', 404)
|
|
59
|
+
const caller = found.owner ?? undefined
|
|
60
|
+
|
|
61
|
+
if (body.action === 'output') {
|
|
62
|
+
const total = found.output?.total ?? 0
|
|
63
|
+
const earliest = found.output?.earliest ?? 0
|
|
64
|
+
const from = Math.max(earliest, total - maxJobOutputBytes)
|
|
65
|
+
const read = ctx.jobs.readAt(id, from, caller)
|
|
66
|
+
const chunks = Array.isArray(read?.chunks) ? read.chunks : []
|
|
67
|
+
let text = chunks.map(chunk => chunk.text ?? '').join('')
|
|
68
|
+
const truncated = from > 0 || Boolean(read?.lossy) || Buffer.byteLength(text) > maxJobOutputBytes
|
|
69
|
+
if (Buffer.byteLength(text) > maxJobOutputBytes) {
|
|
70
|
+
text = Buffer.from(text, 'utf8').subarray(-maxJobOutputBytes).toString('utf8')
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
id,
|
|
74
|
+
text,
|
|
75
|
+
truncated,
|
|
76
|
+
total,
|
|
77
|
+
earliest,
|
|
78
|
+
status: found.status,
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (body.action === 'kill') {
|
|
83
|
+
requireValue(body.confirm === id, '请确认要停止的后台任务。', 409)
|
|
84
|
+
const outcome = ctx.jobs.kill(id, caller, '由 DSH 工具箱手动停止')
|
|
85
|
+
return { id, outcome }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (body.action === 'remove') {
|
|
89
|
+
requireValue(!liveJobStates.has(found.status), '运行中的任务无法清理记录,请先停止。', 409)
|
|
90
|
+
ctx.jobs.remove(id, caller)
|
|
91
|
+
return { id }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
throw new RequestError('后台任务操作无效。')
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function projectSchedule(entry) {
|
|
99
|
+
return {
|
|
100
|
+
id: entry.id,
|
|
101
|
+
kind: entry.kind,
|
|
102
|
+
title: entry.title ?? entry.id,
|
|
103
|
+
prompt: entry.prompt ?? '',
|
|
104
|
+
scheduledAt: entry.scheduledAt ?? '',
|
|
105
|
+
status: entry.status,
|
|
106
|
+
target: entry.target
|
|
107
|
+
? {
|
|
108
|
+
kind: entry.target.kind,
|
|
109
|
+
sessionId: entry.target.sessionId ?? null,
|
|
110
|
+
cwd: entry.target.create?.cwd ?? null,
|
|
111
|
+
agentPreset: entry.target.create?.agentPreset ?? null,
|
|
112
|
+
}
|
|
113
|
+
: null,
|
|
114
|
+
involvedSessionIds: Array.isArray(entry.involvedSessionIds) ? entry.involvedSessionIds : [],
|
|
115
|
+
executionActive: Boolean(entry.executionActive),
|
|
116
|
+
createdAt: entry.createdAt ?? null,
|
|
117
|
+
updatedAt: entry.updatedAt ?? null,
|
|
118
|
+
lastDelivery: entry.lastDelivery
|
|
119
|
+
? {
|
|
120
|
+
scheduledAt: entry.lastDelivery.scheduledAt,
|
|
121
|
+
deliveredAt: entry.lastDelivery.deliveredAt,
|
|
122
|
+
targetSessionId: entry.lastDelivery.targetSessionId,
|
|
123
|
+
}
|
|
124
|
+
: null,
|
|
125
|
+
afterSeconds: entry.afterSeconds ?? undefined,
|
|
126
|
+
everySeconds: entry.everySeconds ?? undefined,
|
|
127
|
+
time: entry.time ?? undefined,
|
|
128
|
+
timeZone: entry.timeZone ?? undefined,
|
|
129
|
+
weekdays: Array.isArray(entry.weekdays) ? entry.weekdays : undefined,
|
|
130
|
+
expression: entry.expression ?? undefined,
|
|
131
|
+
rrule: entry.rrule ?? undefined,
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function mapScheduleError(error, fallback) {
|
|
136
|
+
if (error instanceof RequestError) return error
|
|
137
|
+
const code = error?.code ?? error?.rpcError?.code
|
|
138
|
+
if (code === 'schedule/target-archived') return new RequestError('目标会话已归档,请先恢复会话后再执行。', 409)
|
|
139
|
+
if (code === 'schedule/input-invalid') return new RequestError('定时任务参数无效,请检查后重试。', 400)
|
|
140
|
+
return new RequestError(fallback, 409)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function scheduleOperations(ctx) {
|
|
144
|
+
let tail = Promise.resolve()
|
|
145
|
+
|
|
146
|
+
async function listAll() {
|
|
147
|
+
if (typeof ctx.schedule.catalog === 'function') {
|
|
148
|
+
const entries = await ctx.schedule.catalog()
|
|
149
|
+
return { schedules: entries.map(projectSchedule) }
|
|
150
|
+
}
|
|
151
|
+
const page = await ctx.schedule.list({ limit: 500 })
|
|
152
|
+
return { schedules: (page?.items ?? []).map(projectSchedule) }
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async function runAction(body, signal) {
|
|
156
|
+
signal?.throwIfAborted()
|
|
157
|
+
if (body.action === 'list') return listAll()
|
|
158
|
+
|
|
159
|
+
if (body.action === 'delete-inactive') {
|
|
160
|
+
requireValue(body.confirm === true, '请确认清理已结束的定时任务。', 409)
|
|
161
|
+
requireValue(typeof ctx.schedule.deleteInactive === 'function', '当前 DSH 版本不支持批量清理已结束任务。', 501)
|
|
162
|
+
const result = await ctx.schedule.deleteInactive()
|
|
163
|
+
return { deleted: result?.deleted ?? 0 }
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const id = typeof body.id === 'string' ? body.id.trim() : ''
|
|
167
|
+
requireValue(id, '请指定定时任务 ID。')
|
|
168
|
+
|
|
169
|
+
if (body.action === 'history') {
|
|
170
|
+
requireValue(typeof ctx.schedule.history === 'function', '当前 DSH 版本不支持查看投递历史。', 501)
|
|
171
|
+
const result = await ctx.schedule.history({ id, limit: 20 })
|
|
172
|
+
if (result?.code === 'schedule_not_found') throw new RequestError('定时任务不存在,请刷新列表。', 404)
|
|
173
|
+
return {
|
|
174
|
+
id,
|
|
175
|
+
records: (result?.records ?? []).map(rec => ({
|
|
176
|
+
scheduledAt: rec.scheduledAt,
|
|
177
|
+
deliveredAt: rec.deliveredAt,
|
|
178
|
+
targetSessionId: rec.targetSessionId,
|
|
179
|
+
messageId: rec.messageId,
|
|
180
|
+
})),
|
|
181
|
+
earlierRecordsPruned: Boolean(result?.earlierRecordsPruned),
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (body.action === 'run') {
|
|
186
|
+
requireValue(typeof ctx.schedule.run === 'function', '当前 DSH 版本不支持手动触发定时任务。', 501)
|
|
187
|
+
let result
|
|
188
|
+
try {
|
|
189
|
+
result = await ctx.schedule.run({ id }, signal)
|
|
190
|
+
} catch (error) {
|
|
191
|
+
throw mapScheduleError(error, '无法立即触发此定时任务。')
|
|
192
|
+
}
|
|
193
|
+
if (result?.code === 'schedule_not_found') throw new RequestError('定时任务不存在,请刷新列表。', 404)
|
|
194
|
+
if (result?.code === 'schedule_execution_pending') throw new RequestError('此定时任务正在执行中或处于触发间隔保护期内,请稍后重试。', 409)
|
|
195
|
+
return { schedule: projectSchedule(result) }
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (body.action === 'toggle') {
|
|
199
|
+
requireValue(typeof body.active === 'boolean', '请指定要切换的状态。')
|
|
200
|
+
requireValue(typeof ctx.schedule.edit === 'function', '当前 DSH 版本不支持编辑定时任务状态。', 501)
|
|
201
|
+
let result
|
|
202
|
+
try {
|
|
203
|
+
result = await ctx.schedule.edit({
|
|
204
|
+
id,
|
|
205
|
+
action: 'update',
|
|
206
|
+
patch: { active: body.active },
|
|
207
|
+
...(body.expectedUpdatedAt !== undefined ? { expectedUpdatedAt: body.expectedUpdatedAt } : {}),
|
|
208
|
+
})
|
|
209
|
+
} catch (error) {
|
|
210
|
+
throw mapScheduleError(error, '无法更新定时任务状态。')
|
|
211
|
+
}
|
|
212
|
+
if (result?.code === 'schedule_not_found') throw new RequestError('定时任务不存在,请刷新列表。', 404)
|
|
213
|
+
if (result?.code === 'schedule_conflict') throw new RequestError('定时任务已变化,请刷新后重试。', 409)
|
|
214
|
+
return { schedule: projectSchedule(result) }
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (body.action === 'delete') {
|
|
218
|
+
requireValue(body.confirm === id, '请确认要删除的定时任务。', 409)
|
|
219
|
+
requireValue(typeof ctx.schedule.edit === 'function', '当前 DSH 版本不支持删除定时任务。', 501)
|
|
220
|
+
let result
|
|
221
|
+
try {
|
|
222
|
+
result = await ctx.schedule.edit({
|
|
223
|
+
id,
|
|
224
|
+
action: 'delete',
|
|
225
|
+
...(body.expectedUpdatedAt !== undefined ? { expectedUpdatedAt: body.expectedUpdatedAt } : {}),
|
|
226
|
+
})
|
|
227
|
+
} catch (error) {
|
|
228
|
+
throw mapScheduleError(error, '无法删除此定时任务。')
|
|
229
|
+
}
|
|
230
|
+
if (result?.code === 'schedule_not_found') throw new RequestError('定时任务不存在,请刷新列表。', 404)
|
|
231
|
+
if (result?.code === 'schedule_conflict') throw new RequestError('定时任务已变化,请刷新后重试。', 409)
|
|
232
|
+
return { id, deleted: true }
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
throw new RequestError('定时任务操作无效。')
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return (body, signal) => {
|
|
239
|
+
if (body.action === 'list' || body.action === 'history') return runAction(body, signal)
|
|
240
|
+
const pending = tail.then(() => runAction(body, signal))
|
|
241
|
+
tail = pending.catch(() => {})
|
|
242
|
+
return pending
|
|
243
|
+
}
|
|
244
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lucide Icons and Contributors
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
The following Lucide icons are derived from the Feather project:
|
|
20
|
+
|
|
21
|
+
airplay, alert-circle, alert-octagon, alert-triangle, aperture, arrow-down-circle, arrow-down-left, arrow-down-right, arrow-down, arrow-left-circle, arrow-left, arrow-right-circle, arrow-right, arrow-up-circle, arrow-up-left, arrow-up-right, arrow-up, at-sign, calendar, cast, check, chevron-down, chevron-left, chevron-right, chevron-up, chevrons-down, chevrons-left, chevrons-right, chevrons-up, circle, clipboard, clock, code, columns, command, compass, corner-down-left, corner-down-right, corner-left-down, corner-left-up, corner-right-down, corner-right-up, corner-up-left, corner-up-right, crosshair, database, divide-circle, divide-square, dollar-sign, download, external-link, feather, frown, hash, headphones, help-circle, info, italic, key, layout, life-buoy, link-2, link, loader, lock, log-in, log-out, maximize, meh, minimize, minimize-2, minus-circle, minus-square, minus, monitor, moon, more-horizontal, more-vertical, move, music, navigation-2, navigation, octagon, pause-circle, percent, plus-circle, plus-square, plus, power, radio, rss, search, server, share, shopping-bag, sidebar, smartphone, smile, square, table-2, tablet, target, terminal, trash-2, trash, triangle, tv, type, upload, x-circle, x-octagon, x-square, x, zoom-in, zoom-out
|
|
22
|
+
|
|
23
|
+
The MIT License (MIT) (for the icons listed above)
|
|
24
|
+
|
|
25
|
+
Copyright (c) 2013-present Cole Bemis
|
|
26
|
+
|
|
27
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
28
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
29
|
+
in the Software without restriction, including without limitation the rights
|
|
30
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
31
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
32
|
+
furnished to do so, subject to the following conditions:
|
|
33
|
+
|
|
34
|
+
The above copyright notice and this permission notice shall be included in all
|
|
35
|
+
copies or substantial portions of the Software.
|
|
36
|
+
|
|
37
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
38
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
39
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
40
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
41
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
42
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
43
|
+
SOFTWARE.
|