@xi4z/dsh-utils-utils-agents 0.0.0-stage → 0.7.1
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/lib/git.d.ts +87 -0
- package/lib/git.js +403 -0
- package/lib/git.js.map +1 -0
- package/lib/global-repo.d.ts +57 -0
- package/lib/global-repo.js +301 -0
- package/lib/global-repo.js.map +1 -0
- package/lib/index.d.ts +111 -0
- package/lib/index.js +470 -0
- package/lib/index.js.map +1 -0
- package/lib/legacy.d.ts +30 -0
- package/lib/legacy.js +141 -0
- package/lib/legacy.js.map +1 -0
- package/lib/native.d.ts +74 -0
- package/lib/native.js +232 -0
- package/lib/native.js.map +1 -0
- package/lib/presets.d.ts +34 -0
- package/lib/presets.js +312 -0
- package/lib/presets.js.map +1 -0
- package/lib/shared.d.ts +105 -0
- package/lib/shared.js +507 -0
- package/lib/shared.js.map +1 -0
- package/lib/skill-discovery.d.ts +52 -0
- package/lib/skill-discovery.js +850 -0
- package/lib/skill-discovery.js.map +1 -0
- package/lib/skill-panel.d.ts +36 -0
- package/lib/skill-panel.js +409 -0
- package/lib/skill-panel.js.map +1 -0
- package/lib/update.d.ts +129 -0
- package/lib/update.js +507 -0
- package/lib/update.js.map +1 -0
- package/lib/util.d.ts +215 -0
- package/lib/util.js +475 -0
- package/lib/util.js.map +1 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +52 -3
- package/src/git.ts +444 -0
- package/src/global-repo.ts +343 -0
- package/src/index.ts +522 -0
- package/src/legacy.ts +170 -0
- package/src/native.ts +291 -0
- package/src/presets.ts +329 -0
- package/src/shared.ts +589 -0
- package/src/skill-discovery.ts +917 -0
- package/src/skill-panel.ts +488 -0
- package/src/update.ts +574 -0
- package/src/util.ts +570 -0
- package/README.md +0 -3
package/src/index.ts
ADDED
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* utils-agents: the one host component for skills and presets.
|
|
3
|
+
*
|
|
4
|
+
* It merges what used to be three separate packages/rows:
|
|
5
|
+
*
|
|
6
|
+
* - nested `SKILL.md` discovery from `@xi4z/dsh-utils-main`;
|
|
7
|
+
* - the model-facing skill visibility filter from
|
|
8
|
+
* `@xi4z/dsh-utils-skill-panel`;
|
|
9
|
+
* - the preset panel (skills tree, Markdown editor, per-preset Git skill
|
|
10
|
+
* repository, shared presets and native-preset import/export).
|
|
11
|
+
*
|
|
12
|
+
* The panel picks one preset directory (or the global AGENTS.md pseudo preset)
|
|
13
|
+
* and then lists its skills tree, edits its Markdown and syncs its repository.
|
|
14
|
+
* All browser requests use opaque file ids; the preset id is re-resolved
|
|
15
|
+
* against the configured preset roots on every call.
|
|
16
|
+
*
|
|
17
|
+
* @module @xi4z/dsh-utils-utils-agents
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
21
|
+
import z from '@deepseek-ai/schemastery'
|
|
22
|
+
import {
|
|
23
|
+
BODY_LIMIT_BYTES,
|
|
24
|
+
DEFAULT_MAX_BYTES,
|
|
25
|
+
GLOBAL_PRESET,
|
|
26
|
+
AgentsError,
|
|
27
|
+
failure,
|
|
28
|
+
fileRow,
|
|
29
|
+
json,
|
|
30
|
+
normalizeOptions,
|
|
31
|
+
optionalText,
|
|
32
|
+
readJsonBody,
|
|
33
|
+
readTextFile,
|
|
34
|
+
requiredText,
|
|
35
|
+
resolveCwd,
|
|
36
|
+
sessionPresetOf,
|
|
37
|
+
writeTextFile,
|
|
38
|
+
type AgentsConfig,
|
|
39
|
+
} from './util.js'
|
|
40
|
+
import {
|
|
41
|
+
discoverPresets,
|
|
42
|
+
globalFiles,
|
|
43
|
+
presetById,
|
|
44
|
+
presetFiles,
|
|
45
|
+
resolveFileId,
|
|
46
|
+
toggleSkill,
|
|
47
|
+
} from './presets.js'
|
|
48
|
+
import {
|
|
49
|
+
globalKeyStatus,
|
|
50
|
+
readRepoState,
|
|
51
|
+
repoView,
|
|
52
|
+
saveGlobalKey,
|
|
53
|
+
saveRepoConfig,
|
|
54
|
+
saveRepoKey,
|
|
55
|
+
syncRepo,
|
|
56
|
+
type KeyStatus,
|
|
57
|
+
} from './git.js'
|
|
58
|
+
import { createLegacyBridge, type LegacyBridge } from './legacy.js'
|
|
59
|
+
import { sharedPresetView, syncSharedPresets } from './shared.js'
|
|
60
|
+
import { pullSharedRepo, saveSharedRepoConfig, saveSharedRepoKey, sharedRepoView } from './global-repo.js'
|
|
61
|
+
import { importNativePresets, nativePresetView, publishPresets } from './native.js'
|
|
62
|
+
import { createUpdateChecker, startUpdateWatcher, type UpdateChecker } from './update.js'
|
|
63
|
+
import {
|
|
64
|
+
SKILL_DISCOVERY_DEFAULTS,
|
|
65
|
+
applySkillDiscovery,
|
|
66
|
+
type SkillDiscoveryConfig,
|
|
67
|
+
} from './skill-discovery.js'
|
|
68
|
+
import {
|
|
69
|
+
ROUTE_PATH as SKILL_PANEL_ROUTE,
|
|
70
|
+
applySkillPanel,
|
|
71
|
+
type SkillPanelConfig,
|
|
72
|
+
} from './skill-panel.js'
|
|
73
|
+
|
|
74
|
+
export const name = 'utils-agents'
|
|
75
|
+
|
|
76
|
+
/** The merged component still needs the host skill registry. */
|
|
77
|
+
export const inject = ['skills']
|
|
78
|
+
|
|
79
|
+
/** Raw row config: agents settings plus the merged discovery and visibility fields. */
|
|
80
|
+
export interface UtilsAgentsConfig extends AgentsConfig, SkillDiscoveryConfig, SkillPanelConfig {}
|
|
81
|
+
|
|
82
|
+
/** Exact Connection routes owned by this component. */
|
|
83
|
+
export const ROUTE_PATHS = {
|
|
84
|
+
list: '/api/dsh-utils/utils-agents/list',
|
|
85
|
+
files: '/api/dsh-utils/utils-agents/files',
|
|
86
|
+
file: '/api/dsh-utils/utils-agents/file',
|
|
87
|
+
skill: '/api/dsh-utils/utils-agents/skill',
|
|
88
|
+
repo: '/api/dsh-utils/utils-agents/repo',
|
|
89
|
+
key: '/api/dsh-utils/utils-agents/key',
|
|
90
|
+
sync: '/api/dsh-utils/utils-agents/sync',
|
|
91
|
+
shared: '/api/dsh-utils/utils-agents/shared',
|
|
92
|
+
sharedSync: '/api/dsh-utils/utils-agents/shared-sync',
|
|
93
|
+
sharedRepo: '/api/dsh-utils/utils-agents/shared-repo',
|
|
94
|
+
sharedKey: '/api/dsh-utils/utils-agents/shared-key',
|
|
95
|
+
sharedPull: '/api/dsh-utils/utils-agents/shared-pull',
|
|
96
|
+
deployKey: '/api/dsh-utils/utils-agents/deploy-key',
|
|
97
|
+
update: '/api/dsh-utils/utils-agents/update',
|
|
98
|
+
native: '/api/dsh-utils/utils-agents/native-presets',
|
|
99
|
+
nativeImport: '/api/dsh-utils/utils-agents/native-presets-import',
|
|
100
|
+
publish: '/api/dsh-utils/utils-agents/presets-publish',
|
|
101
|
+
skillList: SKILL_PANEL_ROUTE,
|
|
102
|
+
} as const
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Settings schema. Every field is volatile: the routes re-read the live
|
|
106
|
+
* references, so saving the form takes effect without a restart.
|
|
107
|
+
*/
|
|
108
|
+
export const Config = z.object({
|
|
109
|
+
// -- nested SKILL.md discovery (merged from @xi4z/dsh-utils-main) --
|
|
110
|
+
minDepth: z.natural().min(1).default(SKILL_DISCOVERY_DEFAULTS.minDepth)
|
|
111
|
+
.description('第一个可能包含 SKILL.md 的目录层级。').volatile(),
|
|
112
|
+
maxDepth: z.natural().min(1).default(SKILL_DISCOVERY_DEFAULTS.maxDepth)
|
|
113
|
+
.description('递归扫描的最深目录层级。').volatile(),
|
|
114
|
+
watch: z.boolean().default(SKILL_DISCOVERY_DEFAULTS.watch)
|
|
115
|
+
.description('技能文件变化后刷新技能目录。').volatile(),
|
|
116
|
+
debounceMs: z.natural().default(SKILL_DISCOVERY_DEFAULTS.debounceMs)
|
|
117
|
+
.description('监听失效的合并间隔(毫秒)。').volatile(),
|
|
118
|
+
pollIntervalMs: z.natural().min(250).default(SKILL_DISCOVERY_DEFAULTS.pollIntervalMs)
|
|
119
|
+
.description('不支持递归监听时的兜底轮询间隔(毫秒)。').volatile(),
|
|
120
|
+
includeProjectRoots: z.boolean().default(SKILL_DISCOVERY_DEFAULTS.includeProjectRoots)
|
|
121
|
+
.description('扫描项目级 .dsh/skills 与 .agents/skills。').volatile(),
|
|
122
|
+
includeUserRoots: z.boolean().default(SKILL_DISCOVERY_DEFAULTS.includeUserRoots)
|
|
123
|
+
.description('扫描用户级技能根。').volatile(),
|
|
124
|
+
followSymlinks: z.boolean().default(SKILL_DISCOVERY_DEFAULTS.followSymlinks)
|
|
125
|
+
.description('跟随符号链接目录(带环路保护)。').volatile(),
|
|
126
|
+
extraRoots: z.array(z.string().min(1)).default([...SKILL_DISCOVERY_DEFAULTS.extraRoots])
|
|
127
|
+
.description('额外的技能根目录。').volatile(),
|
|
128
|
+
ignoreDirs: z.array(z.string().min(1)).default([...SKILL_DISCOVERY_DEFAULTS.ignoreDirs])
|
|
129
|
+
.description('永不进入的目录名。').volatile(),
|
|
130
|
+
// -- model-facing skill visibility (merged from @xi4z/dsh-utils-skill-panel) --
|
|
131
|
+
disabledSkills: z.array(z.string()).default([])
|
|
132
|
+
.description('模型技能目录里隐藏的技能名;仍可手动加载。').volatile(),
|
|
133
|
+
// -- preset panel, skill repositories and AGENTS.md --
|
|
134
|
+
maxBytes: z.natural().min(1024).max(BODY_LIMIT_BYTES).default(DEFAULT_MAX_BYTES)
|
|
135
|
+
.description('单个 Markdown 文件的读取/写入上限(字节)。').volatile(),
|
|
136
|
+
presetDirs: z.array(z.string().min(1)).default([])
|
|
137
|
+
.description('额外的预设根目录;每个子目录被视为一个预设。').volatile(),
|
|
138
|
+
presetSourceDirs: z.array(z.string().min(1)).default([])
|
|
139
|
+
.description('额外的技能仓库目录;其 .dsh 下的预设可同步到本机。').volatile(),
|
|
140
|
+
builtinPresets: z.array(z.string().min(1)).default(['standard', 'ptc', 'minimal', 'creator'])
|
|
141
|
+
.description('保留为原生的预设 id;其余原生声明可导入为目录预设。').volatile(),
|
|
142
|
+
extraAgentsFiles: z.array(z.string().min(1)).default([])
|
|
143
|
+
.description('全局视图额外暴露的 AGENTS.md 类绝对路径。').volatile(),
|
|
144
|
+
scanNestedAgents: z.boolean().default(false)
|
|
145
|
+
.description('全局视图递归寻找嵌套 AGENTS.md / CLAUDE.md。').volatile(),
|
|
146
|
+
nestedDepth: z.natural().min(1).max(8).default(3)
|
|
147
|
+
.description('全局视图的递归深度。').volatile(),
|
|
148
|
+
defaultRepoUrl: z.string().default('https://github.com/xi4z/skills')
|
|
149
|
+
.description('新预设的默认技能仓库地址。').volatile(),
|
|
150
|
+
defaultBranch: z.string().default('Agent')
|
|
151
|
+
.description('新预设的默认技能仓库分支。').volatile(),
|
|
152
|
+
updateCheck: z.boolean().default(true)
|
|
153
|
+
.description('定时检查 npm 上 @xi4z/dsh-utils 的发布版本。').volatile(),
|
|
154
|
+
updateCheckIntervalHours: z.natural().min(1).max(168).default(6)
|
|
155
|
+
.description('两次自动版本检查之间的小时数。').volatile(),
|
|
156
|
+
updateRegistry: z.string().default('https://registry.npmjs.org')
|
|
157
|
+
.description('查询发布版本用的 npm registry 地址。').volatile(),
|
|
158
|
+
updateAutoInstall: z.boolean().default(true)
|
|
159
|
+
.description('查到新版本后在后台执行 pnpm add 自动安装(重启后生效;link/file 安装会跳过)。').volatile(),
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
/** One exact Connection Fetch route. */
|
|
163
|
+
interface ConnectionLike {
|
|
164
|
+
readonly fetch: {
|
|
165
|
+
register(route: {
|
|
166
|
+
readonly path: string
|
|
167
|
+
readonly methods: readonly string[]
|
|
168
|
+
readonly requestBody: string
|
|
169
|
+
readonly fetch: (request: Request) => Promise<Response>
|
|
170
|
+
}): () => Promise<void>
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Activate the utils-agents routes.
|
|
176
|
+
* @param ctx - host Cordis context.
|
|
177
|
+
* @param config - row config; volatile fields arrive as live references.
|
|
178
|
+
*/
|
|
179
|
+
export function apply(ctx: Context, config: UtilsAgentsConfig = {}): void {
|
|
180
|
+
// The two former components are host-only halves of this one now; the browser
|
|
181
|
+
// half in @xi4z/dsh-utils-ui still renders their pages on this row.
|
|
182
|
+
applySkillDiscovery(ctx, config)
|
|
183
|
+
applySkillPanel(ctx, config)
|
|
184
|
+
|
|
185
|
+
const legacy = createLegacyBridge(ctx, config)
|
|
186
|
+
const updates = createUpdateChecker(ctx, () => normalizeOptions(config))
|
|
187
|
+
startUpdateWatcher(ctx, updates, () => normalizeOptions(config))
|
|
188
|
+
ctx.effect(() => () => { void legacy.dispose() }, 'utils-agents: legacy preset bridge')
|
|
189
|
+
// Register directory presets as soon as the registry exists, so the normal
|
|
190
|
+
// preset picker sees them even before the Utils panel is opened.
|
|
191
|
+
ctx.inject(['agentPresets'], () => {
|
|
192
|
+
void legacy.ensure().catch((error) => {
|
|
193
|
+
ctx.logger.warn('utils-agents: legacy preset registration failed: ' + String(error))
|
|
194
|
+
})
|
|
195
|
+
})
|
|
196
|
+
ctx.inject(['connection'], (scope) => {
|
|
197
|
+
const connection = (scope as unknown as { connection?: ConnectionLike }).connection
|
|
198
|
+
if (!connection) return
|
|
199
|
+
registerRoute(scope, ctx, connection, ROUTE_PATHS.list, ['GET'], (request) => handleList(ctx, config, legacy, request))
|
|
200
|
+
registerRoute(scope, ctx, connection, ROUTE_PATHS.files, ['GET'], (request) => handleFiles(ctx, config, request))
|
|
201
|
+
registerRoute(scope, ctx, connection, ROUTE_PATHS.file, ['GET', 'POST'], (request) => handleFile(ctx, config, request))
|
|
202
|
+
registerRoute(scope, ctx, connection, ROUTE_PATHS.skill, ['POST'], (request) => handleSkill(ctx, config, request))
|
|
203
|
+
registerRoute(scope, ctx, connection, ROUTE_PATHS.repo, ['GET', 'POST'], (request) => handleRepo(ctx, config, request))
|
|
204
|
+
registerRoute(scope, ctx, connection, ROUTE_PATHS.key, ['POST'], (request) => handleKey(ctx, config, request))
|
|
205
|
+
registerRoute(scope, ctx, connection, ROUTE_PATHS.sync, ['POST'], (request) => handleSync(ctx, config, request))
|
|
206
|
+
registerRoute(scope, ctx, connection, ROUTE_PATHS.shared, ['GET'], (request) => handleShared(ctx, config, request))
|
|
207
|
+
registerRoute(scope, ctx, connection, ROUTE_PATHS.sharedSync, ['POST'], (request) => handleSharedSync(ctx, config, legacy, request))
|
|
208
|
+
registerRoute(scope, ctx, connection, ROUTE_PATHS.sharedRepo, ['GET', 'POST'], (request) => handleSharedRepo(ctx, config, request))
|
|
209
|
+
registerRoute(scope, ctx, connection, ROUTE_PATHS.sharedKey, ['POST'], (request) => handleSharedKey(ctx, config, request))
|
|
210
|
+
registerRoute(scope, ctx, connection, ROUTE_PATHS.sharedPull, ['POST'], (request) => handleSharedPull(ctx, config, request))
|
|
211
|
+
registerRoute(scope, ctx, connection, ROUTE_PATHS.deployKey, ['GET', 'POST'], (request) => handleDeployKey(ctx, request))
|
|
212
|
+
registerRoute(scope, ctx, connection, ROUTE_PATHS.update, ['GET', 'POST'], (request) => handleUpdate(ctx, config, updates, request))
|
|
213
|
+
registerRoute(scope, ctx, connection, ROUTE_PATHS.native, ['GET'], (request) => handleNative(ctx, config, request))
|
|
214
|
+
registerRoute(scope, ctx, connection, ROUTE_PATHS.nativeImport, ['POST'], (request) => handleNativeImport(ctx, config, legacy, request))
|
|
215
|
+
registerRoute(scope, ctx, connection, ROUTE_PATHS.publish, ['POST'], (request) => handlePublish(ctx, config, request))
|
|
216
|
+
})
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Register one route and log a refusal instead of throwing out of activation.
|
|
221
|
+
*/
|
|
222
|
+
function registerRoute(
|
|
223
|
+
scope: Context,
|
|
224
|
+
ctx: Context,
|
|
225
|
+
connection: ConnectionLike,
|
|
226
|
+
path: string,
|
|
227
|
+
methods: readonly string[],
|
|
228
|
+
fetch: (request: Request) => Promise<Response>,
|
|
229
|
+
): void {
|
|
230
|
+
scope.effect(() => {
|
|
231
|
+
try {
|
|
232
|
+
return connection.fetch.register({ path, methods, requestBody: 'buffered', fetch })
|
|
233
|
+
} catch (error) {
|
|
234
|
+
ctx.logger.warn('utils-agents: cannot register ' + path + ': ' + String(error))
|
|
235
|
+
return () => Promise.resolve()
|
|
236
|
+
}
|
|
237
|
+
}, 'utils-agents: ' + path)
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** GET /list: every preset plus the global AGENTS.md family. */
|
|
241
|
+
async function handleList(ctx: Context, config: AgentsConfig, legacy: LegacyBridge, request: Request): Promise<Response> {
|
|
242
|
+
try {
|
|
243
|
+
const options = normalizeOptions(config)
|
|
244
|
+
const sessionId = optionalText(new URL(request.url).searchParams.get('sessionId'))
|
|
245
|
+
const cwd = await resolveCwd(ctx, sessionId, request.signal)
|
|
246
|
+
await legacy.ensure()
|
|
247
|
+
const statuses = legacy.status()
|
|
248
|
+
const presets = (await discoverPresets(ctx, options)).map((preset) => {
|
|
249
|
+
const status = statuses.get(preset.id)
|
|
250
|
+
return { ...preset, registered: status?.registered === true, broken: status?.broken }
|
|
251
|
+
})
|
|
252
|
+
const sessionPreset = sessionPresetOf(ctx, sessionId)
|
|
253
|
+
const matching = sessionPreset === null ? undefined : presets.find((preset) => preset.id === sessionPreset)
|
|
254
|
+
const defaultPresetId = matching?.id ?? presets[0]?.id ?? GLOBAL_PRESET
|
|
255
|
+
const global = await globalFiles(ctx, options, cwd)
|
|
256
|
+
return json({
|
|
257
|
+
sessionId,
|
|
258
|
+
cwd,
|
|
259
|
+
sessionPreset,
|
|
260
|
+
defaultPresetId,
|
|
261
|
+
presets,
|
|
262
|
+
global: {
|
|
263
|
+
id: GLOBAL_PRESET,
|
|
264
|
+
name: '全局 / 项目',
|
|
265
|
+
description: '用户级与项目级 AGENTS.md;不管理预设技能。',
|
|
266
|
+
files: global,
|
|
267
|
+
},
|
|
268
|
+
}, 200)
|
|
269
|
+
} catch (error) {
|
|
270
|
+
return failure(ctx, 'cannot list presets', error)
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** GET /files: Markdown inventory for one preset or the global target. */
|
|
275
|
+
async function handleFiles(ctx: Context, config: AgentsConfig, request: Request): Promise<Response> {
|
|
276
|
+
try {
|
|
277
|
+
const options = normalizeOptions(config)
|
|
278
|
+
const presetId = requiredText(new URL(request.url).searchParams.get('presetId'), '缺少 presetId')
|
|
279
|
+
if (presetId === GLOBAL_PRESET) {
|
|
280
|
+
const cwd = await resolveCwd(ctx, optionalText(new URL(request.url).searchParams.get('sessionId')), request.signal)
|
|
281
|
+
return json({ presetId, preset: null, files: await globalFiles(ctx, options, cwd) }, 200)
|
|
282
|
+
}
|
|
283
|
+
const preset = await presetById(ctx, options, presetId)
|
|
284
|
+
return json({ presetId, preset, files: await presetFiles(ctx, preset) }, 200)
|
|
285
|
+
} catch (error) {
|
|
286
|
+
return failure(ctx, 'cannot list preset files', error)
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** GET/POST /file: read or atomically write one Markdown file. */
|
|
291
|
+
async function handleFile(ctx: Context, config: AgentsConfig, request: Request): Promise<Response> {
|
|
292
|
+
try {
|
|
293
|
+
const options = normalizeOptions(config)
|
|
294
|
+
const sessionId = optionalText(new URL(request.url).searchParams.get('sessionId'))
|
|
295
|
+
const cwd = await resolveCwd(ctx, sessionId, request.signal)
|
|
296
|
+
if (request.method === 'POST') {
|
|
297
|
+
const body = await readJsonBody(request)
|
|
298
|
+
if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new AgentsError('请求体必须是 JSON 对象', 400)
|
|
299
|
+
const record = body as Record<string, unknown>
|
|
300
|
+
const id = requiredText(record.id, '缺少文件 id')
|
|
301
|
+
const content = typeof record.content === 'string' ? record.content : undefined
|
|
302
|
+
if (content === undefined) throw new AgentsError('content 必须是字符串', 400)
|
|
303
|
+
const baseMtimeMs = typeof record.baseMtimeMs === 'number' && Number.isFinite(record.baseMtimeMs) ? record.baseMtimeMs : null
|
|
304
|
+
const resolved = await resolveFileId(ctx, options, id, cwd)
|
|
305
|
+
const written = await writeTextFile(resolved.path, content, baseMtimeMs, options.maxBytes)
|
|
306
|
+
const row = await fileRow(id, resolved.row.label, resolved.path, resolved.row.presetId, resolved.row.kind)
|
|
307
|
+
return json({ file: { ...row, scope: resolved.row.scope }, bytes: written.bytes }, 200)
|
|
308
|
+
}
|
|
309
|
+
const id = requiredText(new URL(request.url).searchParams.get('id'), '缺少文件 id')
|
|
310
|
+
const resolved = await resolveFileId(ctx, options, id, cwd)
|
|
311
|
+
if (!resolved.row.exists) return json({ file: resolved.row, content: '' }, 200)
|
|
312
|
+
if (!resolved.row.editable) throw new AgentsError('这个文件不是可编辑的普通文件', 409)
|
|
313
|
+
const content = (await readTextFile(resolved.path, options.maxBytes)).content
|
|
314
|
+
return json({ file: resolved.row, content }, 200)
|
|
315
|
+
} catch (error) {
|
|
316
|
+
return failure(ctx, 'cannot handle file', error)
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** POST /skill: toggle one preset skill by renaming SKILL.md. */
|
|
321
|
+
async function handleSkill(ctx: Context, config: AgentsConfig, request: Request): Promise<Response> {
|
|
322
|
+
try {
|
|
323
|
+
const options = normalizeOptions(config)
|
|
324
|
+
const body = await readJsonBody(request)
|
|
325
|
+
if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new AgentsError('请求体必须是 JSON 对象', 400)
|
|
326
|
+
const record = body as Record<string, unknown>
|
|
327
|
+
const presetId = requiredText(record.presetId, '缺少 presetId')
|
|
328
|
+
const path = requiredText(record.path, '缺少 skill path')
|
|
329
|
+
const enabled = record.enabled === true
|
|
330
|
+
const preset = await presetById(ctx, options, presetId)
|
|
331
|
+
return json({ skill: await toggleSkill(preset, path, enabled) }, 200)
|
|
332
|
+
} catch (error) {
|
|
333
|
+
return failure(ctx, 'cannot toggle skill', error)
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/** GET/POST /repo: read or save the per-preset repository settings. */
|
|
338
|
+
async function handleRepo(ctx: Context, config: AgentsConfig, request: Request): Promise<Response> {
|
|
339
|
+
try {
|
|
340
|
+
const options = normalizeOptions(config)
|
|
341
|
+
if (request.method === 'GET') {
|
|
342
|
+
const presetId = requiredText(new URL(request.url).searchParams.get('presetId'), '缺少 presetId')
|
|
343
|
+
const preset = await presetById(ctx, options, presetId)
|
|
344
|
+
return json(await repoView(ctx, preset, options), 200)
|
|
345
|
+
}
|
|
346
|
+
const body = await readJsonBody(request)
|
|
347
|
+
if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new AgentsError('请求体必须是 JSON 对象', 400)
|
|
348
|
+
const record = body as Record<string, unknown>
|
|
349
|
+
const presetId = requiredText(record.presetId, '缺少 presetId')
|
|
350
|
+
const preset = await presetById(ctx, options, presetId)
|
|
351
|
+
return json(await saveRepoConfig(ctx, preset, options, record), 200)
|
|
352
|
+
} catch (error) {
|
|
353
|
+
return failure(ctx, 'cannot update repository', error)
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/** POST /key: store or clear the pasted deploy key. */
|
|
358
|
+
async function handleKey(ctx: Context, config: AgentsConfig, request: Request): Promise<Response> {
|
|
359
|
+
try {
|
|
360
|
+
const options = normalizeOptions(config)
|
|
361
|
+
const body = await readJsonBody(request)
|
|
362
|
+
if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new AgentsError('请求体必须是 JSON 对象', 400)
|
|
363
|
+
const record = body as Record<string, unknown>
|
|
364
|
+
const presetId = requiredText(record.presetId, '缺少 presetId')
|
|
365
|
+
const key = typeof record.key === 'string' ? record.key : ''
|
|
366
|
+
const preset = await presetById(ctx, options, presetId)
|
|
367
|
+
const saved: KeyStatus = await saveRepoKey(preset, key)
|
|
368
|
+
return json({ key: saved, state: await readRepoState(preset, options) }, 200)
|
|
369
|
+
} catch (error) {
|
|
370
|
+
return failure(ctx, 'cannot save deploy key', error)
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** GET /shared: presets carried by the skill repositories' .dsh directory. */
|
|
375
|
+
async function handleShared(ctx: Context, config: AgentsConfig, request: Request): Promise<Response> {
|
|
376
|
+
try {
|
|
377
|
+
const options = normalizeOptions(config)
|
|
378
|
+
const presetId = requiredText(new URL(request.url).searchParams.get('presetId'), '缺少 presetId')
|
|
379
|
+
const preset = await presetById(ctx, options, presetId)
|
|
380
|
+
return json(await sharedPresetView(ctx, options, preset), 200)
|
|
381
|
+
} catch (error) {
|
|
382
|
+
return failure(ctx, 'cannot list shared presets', error)
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/** POST /shared-sync: copy .dsh presets into the local preset home. */
|
|
387
|
+
async function handleSharedSync(ctx: Context, config: AgentsConfig, legacy: LegacyBridge, request: Request): Promise<Response> {
|
|
388
|
+
try {
|
|
389
|
+
const options = normalizeOptions(config)
|
|
390
|
+
const body = await readJsonBody(request)
|
|
391
|
+
if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new AgentsError('请求体必须是 JSON 对象', 400)
|
|
392
|
+
const record = body as Record<string, unknown>
|
|
393
|
+
const presetId = requiredText(record.presetId, '缺少 presetId')
|
|
394
|
+
const preset = await presetById(ctx, options, presetId)
|
|
395
|
+
const payload = await syncSharedPresets(ctx, options, preset, record)
|
|
396
|
+
// Register anything newly copied so it appears in the preset picker at once.
|
|
397
|
+
try {
|
|
398
|
+
await legacy.ensure()
|
|
399
|
+
} catch (error) {
|
|
400
|
+
ctx.logger.warn('utils-agents: preset registration after shared sync failed: ' + String(error))
|
|
401
|
+
}
|
|
402
|
+
return json(payload, 200)
|
|
403
|
+
} catch (error) {
|
|
404
|
+
return failure(ctx, 'cannot sync shared presets', error)
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/** GET /native-presets: declared presets the panel could take over. */
|
|
409
|
+
async function handleNative(ctx: Context, config: AgentsConfig, request: Request): Promise<Response> {
|
|
410
|
+
try {
|
|
411
|
+
return json(await nativePresetView(ctx, normalizeOptions(config)), 200)
|
|
412
|
+
} catch (error) {
|
|
413
|
+
return failure(ctx, 'cannot list native presets', error)
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** POST /native-presets-import: export non-built-in declarations as directories. */
|
|
418
|
+
async function handleNativeImport(ctx: Context, config: AgentsConfig, legacy: LegacyBridge, request: Request): Promise<Response> {
|
|
419
|
+
try {
|
|
420
|
+
const body = await readJsonBody(request)
|
|
421
|
+
if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new AgentsError('请求体必须是 JSON 对象', 400)
|
|
422
|
+
const payload = await importNativePresets(ctx, normalizeOptions(config), body as Record<string, unknown>)
|
|
423
|
+
try {
|
|
424
|
+
await legacy.ensure()
|
|
425
|
+
} catch (error) {
|
|
426
|
+
ctx.logger.warn('utils-agents: preset registration after native import failed: ' + String(error))
|
|
427
|
+
}
|
|
428
|
+
return json(payload, 200)
|
|
429
|
+
} catch (error) {
|
|
430
|
+
return failure(ctx, 'cannot import native presets', error)
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/** POST /presets-publish: copy local presets into the shared repository and push. */
|
|
435
|
+
async function handlePublish(ctx: Context, config: AgentsConfig, request: Request): Promise<Response> {
|
|
436
|
+
try {
|
|
437
|
+
const body = await readJsonBody(request)
|
|
438
|
+
if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new AgentsError('请求体必须是 JSON 对象', 400)
|
|
439
|
+
return json(await publishPresets(ctx, normalizeOptions(config), body as Record<string, unknown>), 200)
|
|
440
|
+
} catch (error) {
|
|
441
|
+
return failure(ctx, 'cannot publish presets', error)
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/** GET/POST /shared-repo: stored settings for the tracked skill repository. */
|
|
446
|
+
async function handleSharedRepo(ctx: Context, config: AgentsConfig, request: Request): Promise<Response> {
|
|
447
|
+
try {
|
|
448
|
+
const options = normalizeOptions(config)
|
|
449
|
+
if (request.method === 'GET') return json(await sharedRepoView(ctx, options), 200)
|
|
450
|
+
const body = await readJsonBody(request)
|
|
451
|
+
if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new AgentsError('请求体必须是 JSON 对象', 400)
|
|
452
|
+
return json(await saveSharedRepoConfig(ctx, options, body as Record<string, unknown>), 200)
|
|
453
|
+
} catch (error) {
|
|
454
|
+
return failure(ctx, 'cannot save shared repository', error)
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/** POST /shared-key: store or clear the shared repository Deploy Key. */
|
|
459
|
+
async function handleSharedKey(ctx: Context, config: AgentsConfig, request: Request): Promise<Response> {
|
|
460
|
+
try {
|
|
461
|
+
const options = normalizeOptions(config)
|
|
462
|
+
const body = await readJsonBody(request)
|
|
463
|
+
if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new AgentsError('请求体必须是 JSON 对象', 400)
|
|
464
|
+
const record = body as Record<string, unknown>
|
|
465
|
+
const key = typeof record.key === 'string' ? record.key : ''
|
|
466
|
+
return json(await saveSharedRepoKey(ctx, options, key), 200)
|
|
467
|
+
} catch (error) {
|
|
468
|
+
return failure(ctx, 'cannot save shared deploy key', error)
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/** POST /shared-pull: clone or fast-forward the tracked skill repository. */
|
|
473
|
+
async function handleSharedPull(ctx: Context, config: AgentsConfig, request: Request): Promise<Response> {
|
|
474
|
+
try {
|
|
475
|
+
const options = normalizeOptions(config)
|
|
476
|
+
return json(await pullSharedRepo(ctx, options), 200)
|
|
477
|
+
} catch (error) {
|
|
478
|
+
return failure(ctx, 'cannot pull shared repository', error)
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/** GET/POST /update: the published bundle version and its last check. */
|
|
483
|
+
async function handleUpdate(ctx: Context, config: AgentsConfig, checker: UpdateChecker, request: Request): Promise<Response> {
|
|
484
|
+
try {
|
|
485
|
+
const options = normalizeOptions(config)
|
|
486
|
+
const schedule = { enabled: options.updateCheck, intervalHours: options.updateCheckIntervalHours, registry: options.updateRegistry }
|
|
487
|
+
const update = request.method === 'GET' ? await checker.status() : await checker.check()
|
|
488
|
+
return json({ update, schedule }, 200)
|
|
489
|
+
} catch (error) {
|
|
490
|
+
return failure(ctx, 'cannot check the published version', error)
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/** GET/POST /deploy-key: the panel-wide Deploy Key for private repositories. */
|
|
495
|
+
async function handleDeployKey(ctx: Context, request: Request): Promise<Response> {
|
|
496
|
+
try {
|
|
497
|
+
if (request.method === 'GET') return json({ key: await globalKeyStatus() }, 200)
|
|
498
|
+
const body = await readJsonBody(request)
|
|
499
|
+
if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new AgentsError('请求体必须是 JSON 对象', 400)
|
|
500
|
+
const record = body as Record<string, unknown>
|
|
501
|
+
const key = typeof record.key === 'string' ? record.key : ''
|
|
502
|
+
return json({ key: await saveGlobalKey(key) }, 200)
|
|
503
|
+
} catch (error) {
|
|
504
|
+
return failure(ctx, 'cannot save the shared deploy key', error)
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/** POST /sync: clone or fast-forward the preset repository. */
|
|
509
|
+
async function handleSync(ctx: Context, config: AgentsConfig, request: Request): Promise<Response> {
|
|
510
|
+
try {
|
|
511
|
+
const options = normalizeOptions(config)
|
|
512
|
+
const body = await readJsonBody(request)
|
|
513
|
+
if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new AgentsError('请求体必须是 JSON 对象', 400)
|
|
514
|
+
const record = body as Record<string, unknown>
|
|
515
|
+
const presetId = requiredText(record.presetId, '缺少 presetId')
|
|
516
|
+
const preset = await presetById(ctx, options, presetId)
|
|
517
|
+
return json(await syncRepo(ctx, preset, options), 200)
|
|
518
|
+
} catch (error) {
|
|
519
|
+
return failure(ctx, 'cannot sync repository', error)
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|