dsh-skill-harbor 0.2.0 → 0.3.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/dsh.plugin.json +1 -1
- package/lib/audit/normalize.js +77 -0
- package/lib/audit/rules.js +167 -0
- package/lib/budget/announcement.js +53 -0
- package/lib/client/api.js +79 -0
- package/lib/client/labels.js +6 -0
- package/lib/client/locales.js +688 -0
- package/lib/client/panel.js +284 -0
- package/lib/client.js +13 -4
- package/lib/config.js +42 -0
- package/lib/index.js +14 -6
- package/lib/market/install.js +169 -0
- package/lib/market/zip.js +201 -0
- package/lib/routes/index.js +385 -255
- package/package.json +1 -1
- package/src/audit/normalize.js +77 -0
- package/src/audit/rules.js +167 -0
- package/src/budget/announcement.js +53 -0
- package/src/client/api.js +79 -0
- package/src/client/labels.js +6 -0
- package/src/client/locales.js +688 -0
- package/src/client/panel.js +284 -0
- package/src/client.js +13 -4
- package/src/config.js +42 -0
- package/src/index.js +14 -6
- package/src/market/install.js +169 -0
- package/src/market/zip.js +201 -0
- package/src/routes/index.js +385 -255
package/lib/routes/index.js
CHANGED
|
@@ -1,255 +1,385 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 路由注册总装 —— 所有 HTTP 入口在这里一次性挂上。
|
|
3
|
-
*
|
|
4
|
-
* 设计要点:
|
|
5
|
-
* - 前缀统一 `/dsh-skill-harbor/api`,写操作全部要求 CSRF 头
|
|
6
|
-
* - 写路由只接受 `snapshot.allowlist` 里的路径(不是请求里带来的任意路径)
|
|
7
|
-
* - 面板席位在 Phase 2 是独立 api + 客户端渲染,不注册 settings tab
|
|
8
|
-
*/
|
|
9
|
-
import { writeFileSync, renameSync, unlinkSync, existsSync } from 'node:fs'
|
|
10
|
-
|
|
11
|
-
import { ROUTE_PREFIX, guarded, sendJson } from './guard.js'
|
|
12
|
-
import { buildSnapshot, readSkillBody } from './store.js'
|
|
13
|
-
import { isAllowedPath, safePath, isSkillMarkdown } from './safe-path.js'
|
|
14
|
-
import { FILE_STATE, isWritable } from '../collect/observe.js'
|
|
15
|
-
import { extractSkillMeta } from '../collect/frontmatter.js'
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
*
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
cacheAt
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
cached
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
const
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
sendJson(res,
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
sendJson(res,
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
const
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
sendJson(res,
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 路由注册总装 —— 所有 HTTP 入口在这里一次性挂上。
|
|
3
|
+
*
|
|
4
|
+
* 设计要点:
|
|
5
|
+
* - 前缀统一 `/dsh-skill-harbor/api`,写操作全部要求 CSRF 头
|
|
6
|
+
* - 写路由只接受 `snapshot.allowlist` 里的路径(不是请求里带来的任意路径)
|
|
7
|
+
* - 面板席位在 Phase 2 是独立 api + 客户端渲染,不注册 settings tab
|
|
8
|
+
*/
|
|
9
|
+
import { writeFileSync, renameSync, unlinkSync, existsSync } from 'node:fs'
|
|
10
|
+
|
|
11
|
+
import { ROUTE_PREFIX, guarded, sendJson } from './guard.js'
|
|
12
|
+
import { buildSnapshot, readSkillBody } from './store.js'
|
|
13
|
+
import { isAllowedPath, safePath, isSkillMarkdown } from './safe-path.js'
|
|
14
|
+
import { FILE_STATE, isWritable } from '../collect/observe.js'
|
|
15
|
+
import { extractSkillMeta } from '../collect/frontmatter.js'
|
|
16
|
+
import { auditSkillText } from '../audit/rules.js'
|
|
17
|
+
import { budgetReport } from '../budget/announcement.js'
|
|
18
|
+
import { parseSkillsFromArchive, decidePlacement, installSkill } from '../market/install.js'
|
|
19
|
+
|
|
20
|
+
/** 关闭/开启模型的 frontmatter 键。 */
|
|
21
|
+
const DISABLE_KEY = 'disable-model-invocation'
|
|
22
|
+
|
|
23
|
+
/** 在正文里切换 `disable-model-invocation`,保留其余内容原样。 */
|
|
24
|
+
export function setModelInvocation(text, enabled) {
|
|
25
|
+
const line = enabled ? 'false' : 'true'
|
|
26
|
+
const prefixed = /^(---\r?\n)([\s\S]*?)(\r?\n---)/.exec(text)
|
|
27
|
+
|
|
28
|
+
if (prefixed === null) {
|
|
29
|
+
// 没有 frontmatter:新建一个
|
|
30
|
+
return `---\n${DISABLE_KEY}: ${line}\n---\n${text}`
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const head = prefixed[2]
|
|
34
|
+
const entry = `${DISABLE_KEY}: ${line}`
|
|
35
|
+
const existing = new RegExp(`^${DISABLE_KEY}\\s*:.*$`, 'm')
|
|
36
|
+
|
|
37
|
+
const nextHead = existing.test(head)
|
|
38
|
+
? head.replace(existing, entry)
|
|
39
|
+
: `${head}${head.trim() === '' ? '' : '\n'}${entry}`
|
|
40
|
+
|
|
41
|
+
return `${prefixed[1]}${nextHead}${prefixed[3]}${text.slice(prefixed[0].length)}`
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** 原子写:先写临时文件再 rename,避免半截文件。 */
|
|
45
|
+
function atomicWrite(path, content) {
|
|
46
|
+
const temp = `${path}.harbor-tmp-${process.pid}`
|
|
47
|
+
writeFileSync(temp, content, 'utf8')
|
|
48
|
+
try {
|
|
49
|
+
renameSync(temp, path)
|
|
50
|
+
} catch (error) {
|
|
51
|
+
try {
|
|
52
|
+
if (existsSync(temp)) unlinkSync(temp)
|
|
53
|
+
} catch {
|
|
54
|
+
/* ignore */
|
|
55
|
+
}
|
|
56
|
+
throw error
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 注册全部路由。
|
|
62
|
+
* @param {object} ctx
|
|
63
|
+
* @param {object} config
|
|
64
|
+
* @param {object} [deps] 注入点(便于单测不依赖真实 webServer)
|
|
65
|
+
*/
|
|
66
|
+
export function registerRoutes(ctx, config = {}, deps = {}) {
|
|
67
|
+
const webServer = deps.webServer ?? ctx.webServer
|
|
68
|
+
if (webServer === undefined || webServer === null || typeof webServer.register !== 'function') {
|
|
69
|
+
// 没有 webserver(如纯 CLI 场景):不注册,但 provider 仍然工作
|
|
70
|
+
return () => {}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const logger = ctx?.logger ?? console
|
|
74
|
+
const disposers = []
|
|
75
|
+
|
|
76
|
+
/** 每次请求都重新扫描?否 —— 用短 TTL 缓存,写后立即失效。 */
|
|
77
|
+
let cached = null
|
|
78
|
+
let cacheAt = 0
|
|
79
|
+
const CACHE_MS = 1500
|
|
80
|
+
|
|
81
|
+
const snapshot = async () => {
|
|
82
|
+
const now = Date.now()
|
|
83
|
+
if (cached !== null && now - cacheAt < CACHE_MS) return cached
|
|
84
|
+
const registrySummaries = typeof ctx?.skills?.list === 'function' ? await ctx.skills.list() : []
|
|
85
|
+
cached = buildSnapshot(config, registrySummaries)
|
|
86
|
+
cacheAt = now
|
|
87
|
+
return cached
|
|
88
|
+
}
|
|
89
|
+
const invalidate = () => {
|
|
90
|
+
cached = null
|
|
91
|
+
cacheAt = 0
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const register = (path, methods, handler) => {
|
|
95
|
+
const dispose = webServer.register({
|
|
96
|
+
kind: 'handler',
|
|
97
|
+
path: `${ROUTE_PREFIX}${path}`,
|
|
98
|
+
auth: 'required',
|
|
99
|
+
handler: guarded(ctx, { methods }, handler),
|
|
100
|
+
})
|
|
101
|
+
disposers.push(dispose)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// ---- 读:列表 ----
|
|
105
|
+
register('/list', ['GET'], async (req, res) => {
|
|
106
|
+
const snap = await snapshot()
|
|
107
|
+
sendJson(res, 200, {
|
|
108
|
+
takenAt: snap.takenAt,
|
|
109
|
+
skills: snap.observations.map(observation => ({
|
|
110
|
+
name: observation.name,
|
|
111
|
+
description: observation.description,
|
|
112
|
+
source: observation.source,
|
|
113
|
+
rank: observation.rank,
|
|
114
|
+
fileState: observation.fileState,
|
|
115
|
+
modelInvocable: observation.modelInvocable,
|
|
116
|
+
announcementChars: observation.announcementChars,
|
|
117
|
+
shadowedBy: observation.shadowedBy ?? null,
|
|
118
|
+
// 只有 editable 才暴露 path,且可写
|
|
119
|
+
path: observation.fileState === FILE_STATE.EDITABLE ? observation.path : null,
|
|
120
|
+
writable: isWritable(observation),
|
|
121
|
+
})),
|
|
122
|
+
duplicates: snap.duplicates.map(group => ({
|
|
123
|
+
name: group.name,
|
|
124
|
+
count: group.count,
|
|
125
|
+
winner: group.winner === null ? null : { source: group.winner.source, rank: group.winner.rank },
|
|
126
|
+
})),
|
|
127
|
+
totals: {
|
|
128
|
+
all: snap.observations.length,
|
|
129
|
+
editable: snap.observations.filter(o => o.fileState === FILE_STATE.EDITABLE).length,
|
|
130
|
+
providerFile: snap.observations.filter(o => o.fileState === FILE_STATE.PROVIDER_FILE).length,
|
|
131
|
+
virtual: snap.observations.filter(o => o.fileState === FILE_STATE.VIRTUAL).length,
|
|
132
|
+
shadowed: snap.observations.filter(o => o.shadowedBy !== undefined).length,
|
|
133
|
+
},
|
|
134
|
+
})
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
// ---- 读:单技能正文 ----
|
|
138
|
+
register('/read', ['GET'], async (req, res) => {
|
|
139
|
+
const snap = await snapshot()
|
|
140
|
+
const url = new URL(req.url ?? '/', 'http://localhost')
|
|
141
|
+
const target = url.searchParams.get('path')
|
|
142
|
+
|
|
143
|
+
const allowed = isAllowedPath(target, snap.allowlist)
|
|
144
|
+
if (!allowed.ok) {
|
|
145
|
+
sendJson(res, 403, { error: allowed.reason })
|
|
146
|
+
return
|
|
147
|
+
}
|
|
148
|
+
const body = readSkillBody(target, snap.allowlist)
|
|
149
|
+
if (!body.ok) {
|
|
150
|
+
sendJson(res, 404, { error: body.reason })
|
|
151
|
+
return
|
|
152
|
+
}
|
|
153
|
+
sendJson(res, 200, { path: target, content: body.content, meta: extractSkillMeta(body.content) })
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
// ---- 写:启停模型调用 ----
|
|
157
|
+
register('/set-enabled', ['POST'], async (req, res, body) => {
|
|
158
|
+
const snap = await snapshot()
|
|
159
|
+
const target = body?.path
|
|
160
|
+
const enabled = body?.enabled
|
|
161
|
+
|
|
162
|
+
if (typeof enabled !== 'boolean') {
|
|
163
|
+
sendJson(res, 400, { error: 'enabled-must-be-boolean' })
|
|
164
|
+
return
|
|
165
|
+
}
|
|
166
|
+
const allowed = isAllowedPath(target, snap.allowlist)
|
|
167
|
+
if (!allowed.ok) {
|
|
168
|
+
sendJson(res, 403, { error: allowed.reason })
|
|
169
|
+
return
|
|
170
|
+
}
|
|
171
|
+
if (!isSkillMarkdown(target)) {
|
|
172
|
+
sendJson(res, 400, { error: 'not-a-skill-markdown' })
|
|
173
|
+
return
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
try {
|
|
177
|
+
const current = readSkillBody(target, snap.allowlist)
|
|
178
|
+
if (!current.ok) {
|
|
179
|
+
sendJson(res, 404, { error: current.reason })
|
|
180
|
+
return
|
|
181
|
+
}
|
|
182
|
+
atomicWrite(target, setModelInvocation(current.content, enabled))
|
|
183
|
+
invalidate()
|
|
184
|
+
sendJson(res, 200, { ok: true, path: target, enabled })
|
|
185
|
+
} catch (error) {
|
|
186
|
+
logger.warn?.('[dsh-skill-harbor] set-enabled failed', error)
|
|
187
|
+
sendJson(res, 500, { error: 'write-failed', message: String(error?.message ?? error) })
|
|
188
|
+
}
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
// ---- 写:整体更新正文 ----
|
|
192
|
+
register('/update', ['POST'], async (req, res, body) => {
|
|
193
|
+
const snap = await snapshot()
|
|
194
|
+
const target = body?.path
|
|
195
|
+
const content = body?.content
|
|
196
|
+
|
|
197
|
+
if (typeof content !== 'string') {
|
|
198
|
+
sendJson(res, 400, { error: 'content-must-be-string' })
|
|
199
|
+
return
|
|
200
|
+
}
|
|
201
|
+
const allowed = isAllowedPath(target, snap.allowlist)
|
|
202
|
+
if (!allowed.ok) {
|
|
203
|
+
sendJson(res, 403, { error: allowed.reason })
|
|
204
|
+
return
|
|
205
|
+
}
|
|
206
|
+
if (!isSkillMarkdown(target)) {
|
|
207
|
+
sendJson(res, 400, { error: 'not-a-skill-markdown' })
|
|
208
|
+
return
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
try {
|
|
212
|
+
atomicWrite(target, content)
|
|
213
|
+
invalidate()
|
|
214
|
+
sendJson(res, 200, { ok: true, path: target })
|
|
215
|
+
} catch (error) {
|
|
216
|
+
logger.warn?.('[dsh-skill-harbor] update failed', error)
|
|
217
|
+
sendJson(res, 500, { error: 'write-failed', message: String(error?.message ?? error) })
|
|
218
|
+
}
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
// ---- 写:删除 ----
|
|
222
|
+
register('/delete', ['POST'], async (req, res, body) => {
|
|
223
|
+
const snap = await snapshot()
|
|
224
|
+
const target = body?.path
|
|
225
|
+
|
|
226
|
+
const allowed = isAllowedPath(target, snap.allowlist)
|
|
227
|
+
if (!allowed.ok) {
|
|
228
|
+
sendJson(res, 403, { error: allowed.reason })
|
|
229
|
+
return
|
|
230
|
+
}
|
|
231
|
+
if (!isSkillMarkdown(target)) {
|
|
232
|
+
sendJson(res, 400, { error: 'not-a-skill-markdown' })
|
|
233
|
+
return
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
try {
|
|
237
|
+
unlinkSync(target)
|
|
238
|
+
invalidate()
|
|
239
|
+
sendJson(res, 200, { ok: true, path: target })
|
|
240
|
+
} catch (error) {
|
|
241
|
+
logger.warn?.('[dsh-skill-harbor] delete failed', error)
|
|
242
|
+
sendJson(res, 500, { error: 'delete-failed', message: String(error?.message ?? error) })
|
|
243
|
+
}
|
|
244
|
+
})
|
|
245
|
+
|
|
246
|
+
// ---- 读:审计全量 ----
|
|
247
|
+
register('/audit', ['GET'], async (req, res) => {
|
|
248
|
+
const snap = await snapshot()
|
|
249
|
+
const results = []
|
|
250
|
+
for (const observation of snap.observations) {
|
|
251
|
+
if (observation.fileState !== FILE_STATE.EDITABLE) {
|
|
252
|
+
// provider-file / virtual 没有可信路径 → 明确是 skipped,不是 ok
|
|
253
|
+
results.push({
|
|
254
|
+
name: observation.name,
|
|
255
|
+
source: observation.source,
|
|
256
|
+
fileState: observation.fileState,
|
|
257
|
+
verdict: 'skipped',
|
|
258
|
+
reason: observation.fileState === FILE_STATE.PROVIDER_FILE
|
|
259
|
+
? 'file-exists-but-not-scanned'
|
|
260
|
+
: 'no-instruction-file',
|
|
261
|
+
findings: [],
|
|
262
|
+
})
|
|
263
|
+
continue
|
|
264
|
+
}
|
|
265
|
+
const body = readSkillBody(observation.path, snap.allowlist)
|
|
266
|
+
if (!body.ok) {
|
|
267
|
+
results.push({
|
|
268
|
+
name: observation.name,
|
|
269
|
+
source: observation.source,
|
|
270
|
+
fileState: observation.fileState,
|
|
271
|
+
verdict: 'skipped',
|
|
272
|
+
reason: body.reason,
|
|
273
|
+
findings: [],
|
|
274
|
+
})
|
|
275
|
+
continue
|
|
276
|
+
}
|
|
277
|
+
const audit = auditSkillText(body.content, { name: observation.name })
|
|
278
|
+
results.push({
|
|
279
|
+
name: observation.name,
|
|
280
|
+
source: observation.source,
|
|
281
|
+
fileState: observation.fileState,
|
|
282
|
+
verdict: audit.verdict,
|
|
283
|
+
findings: audit.findings,
|
|
284
|
+
stats: audit.stats,
|
|
285
|
+
})
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
sendJson(res, 200, {
|
|
289
|
+
takenAt: snap.takenAt,
|
|
290
|
+
totals: {
|
|
291
|
+
ok: results.filter(r => r.verdict === 'ok').length,
|
|
292
|
+
warn: results.filter(r => r.verdict === 'warn').length,
|
|
293
|
+
fail: results.filter(r => r.verdict === 'fail').length,
|
|
294
|
+
skipped: results.filter(r => r.verdict === 'skipped').length,
|
|
295
|
+
},
|
|
296
|
+
results,
|
|
297
|
+
})
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
// ---- 读:字符预算 ----
|
|
301
|
+
register('/budget', ['GET'], async (req, res) => {
|
|
302
|
+
const snap = await snapshot()
|
|
303
|
+
sendJson(res, 200, { takenAt: snap.takenAt, ...budgetReport(snap.observations) })
|
|
304
|
+
})
|
|
305
|
+
|
|
306
|
+
// ---- 写:导入归档 ----
|
|
307
|
+
register('/import', ['POST'], async (req, res, body) => {
|
|
308
|
+
const snap = await snapshot()
|
|
309
|
+
const archiveBase64 = body?.archive
|
|
310
|
+
const targetSource = body?.targetSource ?? 'user-dsh'
|
|
311
|
+
|
|
312
|
+
if (typeof archiveBase64 !== 'string' || archiveBase64 === '') {
|
|
313
|
+
sendJson(res, 400, { error: 'archive-required' })
|
|
314
|
+
return
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
let buffer
|
|
318
|
+
try {
|
|
319
|
+
buffer = Buffer.from(archiveBase64, 'base64')
|
|
320
|
+
} catch {
|
|
321
|
+
sendJson(res, 400, { error: 'invalid-base64' })
|
|
322
|
+
return
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const targetRoot = snap.roots.find(root => root.source === targetSource)
|
|
326
|
+
if (targetRoot === undefined) {
|
|
327
|
+
sendJson(res, 400, {
|
|
328
|
+
error: 'unknown-target-source',
|
|
329
|
+
available: snap.roots.map(root => root.source),
|
|
330
|
+
})
|
|
331
|
+
return
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
let parsed
|
|
335
|
+
try {
|
|
336
|
+
parsed = parseSkillsFromArchive(buffer)
|
|
337
|
+
} catch (error) {
|
|
338
|
+
const code = error?.code ?? 'archive-invalid'
|
|
339
|
+
const status = code === 'archive-too-large' || code === 'too-many-entries' || code === 'total-too-large' ? 413
|
|
340
|
+
: code === 'encrypted-entry' ? 422
|
|
341
|
+
: 400
|
|
342
|
+
sendJson(res, status, { error: code, message: String(error?.message ?? error) })
|
|
343
|
+
return
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const results = []
|
|
347
|
+
for (const skill of parsed.skills) {
|
|
348
|
+
const placement = decidePlacement({
|
|
349
|
+
skillName: skill.name,
|
|
350
|
+
targetRoot,
|
|
351
|
+
allRoots: snap.roots,
|
|
352
|
+
observations: snap.observations,
|
|
353
|
+
})
|
|
354
|
+
if (!placement.ok) {
|
|
355
|
+
results.push({ name: skill.name, installed: false, reason: placement.reason, message: placement.message })
|
|
356
|
+
continue
|
|
357
|
+
}
|
|
358
|
+
const outcome = installSkill({ skill, targetDir: placement.targetDir })
|
|
359
|
+
results.push({ name: skill.name, ...outcome })
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const installedCount = results.filter(r => r.installed).length
|
|
363
|
+
if (installedCount > 0) invalidate()
|
|
364
|
+
|
|
365
|
+
sendJson(res, installedCount > 0 ? 200 : 422, {
|
|
366
|
+
ok: installedCount > 0,
|
|
367
|
+
skippedSymlinks: parsed.skippedSymlinks,
|
|
368
|
+
totalBytes: parsed.totalBytes,
|
|
369
|
+
results,
|
|
370
|
+
})
|
|
371
|
+
})
|
|
372
|
+
|
|
373
|
+
return () => {
|
|
374
|
+
for (const dispose of disposers) {
|
|
375
|
+
try {
|
|
376
|
+
dispose()
|
|
377
|
+
} catch {
|
|
378
|
+
/* ignore */
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
disposers.length = 0
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
export { ROUTE_PREFIX, safePath }
|