@capyclaw/scrapling 1.1.7 → 1.1.9
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/node_modules/@capyclaw/foundation/abi/assets.mjs +26 -0
- package/node_modules/@capyclaw/foundation/abi/bridge.mjs +3 -1
- package/node_modules/@capyclaw/foundation/abi/index.mjs +15 -0
- package/node_modules/@capyclaw/foundation/abi/loader.mjs +8 -1
- package/node_modules/@capyclaw/foundation/abi/spec.mjs +5 -2
- package/node_modules/@capyclaw/foundation/abi/version.mjs +63 -5
- package/node_modules/@capyclaw/foundation/abi/visibility.mjs +90 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +21 -4
- package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +11 -10
- package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +8 -1
- package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +5 -1
- package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +5 -2
- package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +4 -0
- package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +29 -2
- package/node_modules/@capyclaw/foundation/lib/kernel.mjs +13 -3
- package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +31 -3
- package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +12 -2
- package/node_modules/@capyclaw/foundation/package.json +1 -1
- package/node_modules/@capyclaw/plugin-kit/package.json +4 -1
- package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +5 -2
- package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat.mjs +26 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +3 -82
- package/node_modules/@capyclaw/plugin-kit/src/ledger.mjs +151 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/static.mjs +79 -0
- package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +13 -5
- package/node_modules/@capyclaw/portal/package.json +1 -1
- package/node_modules/@capyclaw/service-broker/README.md +1 -1
- package/node_modules/@capyclaw/service-broker/lib/control.mjs +23 -7
- package/node_modules/@capyclaw/service-broker/lib/engine.mjs +60 -6
- package/node_modules/@capyclaw/service-broker/lib/host.mjs +11 -6
- package/node_modules/@capyclaw/service-broker/lib/manifest.mjs +15 -2
- package/node_modules/@capyclaw/service-broker/lib/recovery.mjs +0 -50
- package/node_modules/@capyclaw/service-broker/lib/skills-client.mjs +30 -10
- package/node_modules/@capyclaw/service-broker/package.json +1 -1
- package/package.json +1 -1
|
@@ -186,6 +186,10 @@ export class ServiceEngine {
|
|
|
186
186
|
this._unhealthy = new Map()
|
|
187
187
|
// 深探节流:id → 上次健康/工具探测时间。
|
|
188
188
|
this._lastProbe = new Map()
|
|
189
|
+
// 重启退避:id → 上次自动重启尝试时间(崩溃循环不再每 2s 硬拉一次)。
|
|
190
|
+
this._lastRestart = new Map()
|
|
191
|
+
// error 态恢复:id → { count, lastAt }(冷却指数翻倍,不再永久移出扫描)。
|
|
192
|
+
this._errorRetries = new Map()
|
|
189
193
|
// broker 自身的创建身份在本进程生命周期内不变;Windows 上 marker() 约 900ms,
|
|
190
194
|
// 每次 up 重算会白烧近 1 秒。null 时不缓存(瞬时查询失败可重试)。
|
|
191
195
|
this._brokerMarker = null
|
|
@@ -963,9 +967,10 @@ export class ServiceEngine {
|
|
|
963
967
|
maxUnhealthy = 3,
|
|
964
968
|
requireTools = true,
|
|
965
969
|
probeEveryMs = 15000,
|
|
970
|
+
errorRetryMs = 300000,
|
|
966
971
|
} = {}) {
|
|
967
972
|
if (this._guardTimer) return
|
|
968
|
-
this._guardConfig = { maxConsecutive, maxUnhealthy, requireTools, probeEveryMs }
|
|
973
|
+
this._guardConfig = { maxConsecutive, maxUnhealthy, requireTools, probeEveryMs, errorRetryMs }
|
|
969
974
|
this._guardTimer = setInterval(() => {
|
|
970
975
|
if (this._stopping || this._sweeping) return
|
|
971
976
|
this._sweeping = true
|
|
@@ -1048,6 +1053,13 @@ export class ServiceEngine {
|
|
|
1048
1053
|
cur[row.id] = { ...current, state: 'error', error: message }
|
|
1049
1054
|
await this._saveRegistry(cur)
|
|
1050
1055
|
})
|
|
1056
|
+
/* 冷却从「进入 error 态」起算,而不是从恢复尝试起算:否则 breaker 触发前的
|
|
1057
|
+
* 最后一次重启时间会被当成冷却起点,errorRetryMs 短于崩溃循环时长时,
|
|
1058
|
+
* 行刚变 error 就立刻被重试,冷却形同虚设。仅状态真的翻转时登记。 */
|
|
1059
|
+
const after = (await this._loadRegistry())[row.id]
|
|
1060
|
+
if (after?.state === 'error' && after.pid === row.pid && after.startedAt === row.startedAt) {
|
|
1061
|
+
this._errorRetries.set(row.id, { count: 0, lastAt: Date.now() })
|
|
1062
|
+
}
|
|
1051
1063
|
this.log(`✗ ${row.id} ${message}`)
|
|
1052
1064
|
}
|
|
1053
1065
|
|
|
@@ -1058,7 +1070,7 @@ export class ServiceEngine {
|
|
|
1058
1070
|
* 孤儿行)。只看 _owned 是历史故障的根因:服务被外部杀掉后行还在、_owned 里
|
|
1059
1071
|
* 没有它,于是 2s 一轮的守护永远跳过它,注册表一直写着 running 的假状态。 */
|
|
1060
1072
|
const rows = Object.values(services).filter((row) =>
|
|
1061
|
-
row.keepAlive && row.state === 'running'
|
|
1073
|
+
row.keepAlive && (row.state === 'running' || row.state === 'error')
|
|
1062
1074
|
&& (this._owned.has(row.id) || this._lastSpecs.has(row.id)))
|
|
1063
1075
|
/* 并行探测:一个服务探测慢/超时不能拖住其他服务的巡检。 */
|
|
1064
1076
|
await Promise.all(rows.map((row) => this._sweepOne(row, maxConsecutive).catch(() => {})))
|
|
@@ -1066,8 +1078,12 @@ export class ServiceEngine {
|
|
|
1066
1078
|
}
|
|
1067
1079
|
|
|
1068
1080
|
async _sweepOne(row, maxConsecutive) {
|
|
1081
|
+
/* error 态不是终点:带冷却地重试(见 _recoverError),不再永久移出扫描。 */
|
|
1082
|
+
if (row.state === 'error') return this._recoverError(row)
|
|
1069
1083
|
if (pidAlive(row.pid)) {
|
|
1070
|
-
|
|
1084
|
+
/* 注意:这里**不能**无条件清 _guardFailures——进程只要活过一个巡检间隔
|
|
1085
|
+
* 就把计数清零,「起-死-起」崩溃循环(活得比巡检间隔长)永远打不满熔断,
|
|
1086
|
+
* 变成无限重试。计数只在下方「稳定跑过一分钟」或手动 up 成功时翻篇。 */
|
|
1071
1087
|
/* 分两档节奏:
|
|
1072
1088
|
* - 存活检查每一轮都做(默认 2s 一轮)→ 进程一死,2s 内就重启;
|
|
1073
1089
|
* - 健康/工具深探按 probeEveryMs(默认 15s)做一次,避免频繁打 HTTP。
|
|
@@ -1082,7 +1098,11 @@ export class ServiceEngine {
|
|
|
1082
1098
|
/* "连续"重启次数:稳定跑过一分钟才算翻篇,避免“起-死-起”抖动
|
|
1083
1099
|
* 每次都把计数清零,最后无限重试。 */
|
|
1084
1100
|
const startedAt = Date.parse(row.startedAt || '') || 0
|
|
1085
|
-
if (startedAt && Date.now() - startedAt > 60_000)
|
|
1101
|
+
if (startedAt && Date.now() - startedAt > 60_000) {
|
|
1102
|
+
this._guardFailures.delete(row.id)
|
|
1103
|
+
this._errorRetries.delete(row.id)
|
|
1104
|
+
this._lastRestart.delete(row.id)
|
|
1105
|
+
}
|
|
1086
1106
|
return
|
|
1087
1107
|
}
|
|
1088
1108
|
const count = (this._unhealthy.get(row.id) || 0) + 1
|
|
@@ -1137,10 +1157,18 @@ export class ServiceEngine {
|
|
|
1137
1157
|
const failures = this._guardFailures.get(row.id) || 0
|
|
1138
1158
|
const spec = this._lastSpecs.get(row.id)
|
|
1139
1159
|
if (!spec) { await this._markError(row, '缺少重启规格,拒绝自动重启'); return }
|
|
1140
|
-
if (failures >= maxConsecutive) { await this._markError(row, '
|
|
1141
|
-
|
|
1160
|
+
if (failures >= maxConsecutive) { await this._markError(row, '连续重启次数超限,进入冷却(稍后自动重试)'); return }
|
|
1161
|
+
/* 重启退避:崩溃循环不能每 2s 硬拉一次,间隔随失败次数翻倍(2s→4s→8s…上限 60s)。 */
|
|
1162
|
+
const backoff = Math.min(2000 * 2 ** failures, 60_000)
|
|
1163
|
+
const lastRestart = this._lastRestart.get(row.id) || 0
|
|
1164
|
+
if (Date.now() - lastRestart < backoff) return
|
|
1165
|
+
this._lastRestart.set(row.id, Date.now())
|
|
1142
1166
|
try {
|
|
1143
1167
|
await this._upOnce(spec, {})
|
|
1168
|
+
/* 计数必须在重启**成功后**累加:_upOnce 的健康拉起路径会清 _guardFailures
|
|
1169
|
+
* (手动 up 语义=翻篇),守护重启若先加后清,崩溃循环永远打不满熔断。
|
|
1170
|
+
* 只有「稳定跑过 60s」或手动 up 才真正翻篇(见 _sweepOne)。 */
|
|
1171
|
+
this._guardFailures.set(row.id, failures + 1)
|
|
1144
1172
|
this.log(`↻ ${row.id} 已自动重启`)
|
|
1145
1173
|
} catch (err) {
|
|
1146
1174
|
await this._markError(row, `自动重启失败: ${err.message}`)
|
|
@@ -1148,6 +1176,32 @@ export class ServiceEngine {
|
|
|
1148
1176
|
})
|
|
1149
1177
|
}
|
|
1150
1178
|
|
|
1179
|
+
/** error 态恢复:冷却指数翻倍(默认 5min→10min→20min…上限 30min)后按规格重试。
|
|
1180
|
+
* _upOnce 自带仲裁——端口上已有健康实例就复用,不会双开;成功即回到 running,
|
|
1181
|
+
* 稳定跑过 60s 后冷却计数清零(见 _sweepOne)。没有重启规格的行无法自愈,留给手动 up。 */
|
|
1182
|
+
async _recoverError(row) {
|
|
1183
|
+
const cfg = this._guardConfig || {}
|
|
1184
|
+
const base = cfg.errorRetryMs ?? 300_000
|
|
1185
|
+
const retry = this._errorRetries.get(row.id) || { count: 0, lastAt: 0 }
|
|
1186
|
+
const cooldown = Math.min(base * 2 ** retry.count, 1_800_000)
|
|
1187
|
+
if (Date.now() - retry.lastAt < cooldown) return
|
|
1188
|
+
const spec = this._lastSpecs.get(row.id)
|
|
1189
|
+
if (!spec) return
|
|
1190
|
+
await this._withServiceLock(row.id, async () => {
|
|
1191
|
+
if (this._stopping) return
|
|
1192
|
+
this._errorRetries.set(row.id, { count: retry.count + 1, lastAt: Date.now() })
|
|
1193
|
+
/* error 恢复是新一轮:不背旧的连续重启计数。 */
|
|
1194
|
+
this._guardFailures.delete(row.id)
|
|
1195
|
+
this._unhealthy.delete(row.id)
|
|
1196
|
+
try {
|
|
1197
|
+
await this._upOnce(spec, {})
|
|
1198
|
+
this.log(`↻ ${row.id} 已从 error 态恢复(第 ${retry.count + 1} 次尝试)`)
|
|
1199
|
+
} catch (err) {
|
|
1200
|
+
this.log(`… ${row.id} error 恢复失败(第 ${retry.count + 1} 次尝试):${err.message},冷却后再试`)
|
|
1201
|
+
}
|
|
1202
|
+
})
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1151
1205
|
/** 与服务级启动锁串行,避免与并发的 up/stop 交叉执行。 */
|
|
1152
1206
|
_withServiceLock(id, fn) {
|
|
1153
1207
|
const prev = this._upLocks.get(id) || Promise.resolve()
|
|
@@ -5,8 +5,11 @@
|
|
|
5
5
|
* 只做三件事:
|
|
6
6
|
* 1. 按清单(config.manifest 指向 services.json)把各服务作为独立进程拉起;
|
|
7
7
|
* 2. 向同进程提供 ctx.serviceBroker(list/start/stop/apply 控制面);
|
|
8
|
-
* 3. 可选:config.skills.url 指向技能服务时,提供 ctx.
|
|
9
|
-
* (数据来自 HTTP
|
|
8
|
+
* 3. 可选:config.skills.url 指向技能服务时,提供 ctx.skillsHttp 瘦客户端
|
|
9
|
+
* (数据来自 HTTP)。技能的标准路径是 skillProvider@1 进程内注册 →
|
|
10
|
+
* DSH 原生 SkillRegistry(ctx.skills);此 HTTP 路是 standalone 可选模式,
|
|
11
|
+
* 默认不启用。服务名用 skillsHttp 而非 skills,避免遮蔽原生注册表
|
|
12
|
+
* (详见 lib/skills-client.mjs 头注释,P1-10 决策)。
|
|
10
13
|
*
|
|
11
14
|
* 生命周期:keepAlive 服务(默认)不随插件卸载停止——服务独立于 dsh;
|
|
12
15
|
* config.stopOnUnload === true 时才随卸载全停。
|
|
@@ -40,13 +43,15 @@ export const { name, inject, apply } = defineDriver({
|
|
|
40
43
|
}
|
|
41
44
|
const disposeService = ctx.provide('serviceBroker', api)
|
|
42
45
|
|
|
43
|
-
//
|
|
44
|
-
//
|
|
46
|
+
// 技能瘦客户端(可选 standalone 模式,默认不启用):list/get 形状与进程内
|
|
47
|
+
// 注册表一致,数据源改为 HTTP 技能服务。服务名 skillsHttp——'skills' 归 DSH
|
|
48
|
+
// 原生 SkillRegistry,provide('skills') 会遮蔽它并令 skillProvider@1 插件
|
|
49
|
+
// 因缺 registerProvider 挂载失败(P1-10 决策,见 lib/skills-client.mjs)。
|
|
45
50
|
let disposeSkills = null
|
|
46
51
|
if (config.skills?.url) {
|
|
47
52
|
const client = createSkillsClient(config.skills.url)
|
|
48
|
-
disposeSkills = ctx.provide('
|
|
49
|
-
console.log(`[service-broker]
|
|
53
|
+
disposeSkills = ctx.provide('skillsHttp', client)
|
|
54
|
+
console.log(`[service-broker] skillsHttp ← ${config.skills.url}`)
|
|
50
55
|
}
|
|
51
56
|
|
|
52
57
|
// 开机清单:有清单就按 up 命令投递给自己(幂等,复用优先)
|
|
@@ -39,12 +39,18 @@ export function findSuiteRoot(startDir = path.dirname(PKG_DIR)) {
|
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
/** 读 JSON 并剥 UTF-8 BOM:带 BOM 的清单会让 JSON.parse 抛「Unexpected token」,
|
|
43
|
+
* 表现为 boot 永久失败且极难归因(文件看着完全正常)。统一在此剥掉。 */
|
|
44
|
+
function parseJsonFile(file) {
|
|
45
|
+
return JSON.parse(readFileSync(file, 'utf8').replace(/^\uFEFF/, ''))
|
|
46
|
+
}
|
|
47
|
+
|
|
42
48
|
/** 套件根判据:含 dsh-suite.json + services.json,且不是 broker 自带的 dsh-suite.json。 */
|
|
43
49
|
function isSuiteRoot(dir) {
|
|
44
50
|
const suiteFile = path.join(dir, 'dsh-suite.json')
|
|
45
51
|
if (!existsSync(suiteFile) || !existsSync(path.join(dir, 'services.json'))) return false
|
|
46
52
|
try {
|
|
47
|
-
const manifest =
|
|
53
|
+
const manifest = parseJsonFile(suiteFile)
|
|
48
54
|
return Boolean(manifest) && typeof manifest === 'object' && manifest.name !== 'dsh-service-broker-suite'
|
|
49
55
|
} catch {
|
|
50
56
|
return false
|
|
@@ -201,7 +207,7 @@ export function loadManifest(manifestPath) {
|
|
|
201
207
|
|
|
202
208
|
function loadFile(file) {
|
|
203
209
|
if (!existsSync(file)) return []
|
|
204
|
-
const raw =
|
|
210
|
+
const raw = parseJsonFile(file)
|
|
205
211
|
const services = Array.isArray(raw) ? raw : raw.services
|
|
206
212
|
if (!Array.isArray(services)) throw new Error(`manifest 格式非法(缺 services 数组): ${file}`)
|
|
207
213
|
const specErrors = validateServiceManifest(services, file)
|
|
@@ -245,5 +251,12 @@ function expandPackageDir(service, base) {
|
|
|
245
251
|
for (const key of ['url', 'cwd', 'logFile']) {
|
|
246
252
|
if (typeof out[key] === 'string') out[key] = String(out[key]).replaceAll('${PACKAGE_DIR}', base)
|
|
247
253
|
}
|
|
254
|
+
/* env 值同样展开 ${PACKAGE_DIR}:旧实现只展开 url/cwd/logFile,env 里引用
|
|
255
|
+
* ${PACKAGE_DIR} 的变量(如自定义数据目录/脚本路径)原样传给子进程 → 路径错。 */
|
|
256
|
+
for (const [key, value] of Object.entries(out.env)) {
|
|
257
|
+
if (typeof value === 'string' && value.includes('${PACKAGE_DIR}')) {
|
|
258
|
+
out.env[key] = value.replaceAll('${PACKAGE_DIR}', base)
|
|
259
|
+
}
|
|
260
|
+
}
|
|
248
261
|
return out
|
|
249
262
|
}
|
|
@@ -273,53 +273,3 @@ export async function waitReceiptResolvable(receipt, ports, timeoutMs = 3000) {
|
|
|
273
273
|
await new Promise((resolve) => setTimeout(resolve, 200))
|
|
274
274
|
}
|
|
275
275
|
}
|
|
276
|
-
|
|
277
|
-
// Caller must serialize against starting a replacement broker and supply the
|
|
278
|
-
// identity captured when it spawned this broker. An absent PID alone is not a receipt.
|
|
279
|
-
export async function recoverServices(discovery, broker) {
|
|
280
|
-
const unresolved = [], stopped = []
|
|
281
|
-
if (!broker?.pid || !broker?.marker || !broker?.recoveryMarker || !absent(broker.pid)) return { stopped, unresolved: ['broker:not-confirmed-absent'] }
|
|
282
|
-
let discovered = {}
|
|
283
|
-
try { discovered = JSON.parse(fs.readFileSync(discovery, 'utf8'))?.services || {} } catch { /* discovery 缺失时只能依赖收据自身 */ }
|
|
284
|
-
let entries
|
|
285
|
-
try { entries = fs.readdirSync(receiptDir(discovery)) } catch (e) {
|
|
286
|
-
return { stopped, unresolved: [e.code === 'ENOENT' ? 'ownership:missing' : 'ownership:unreadable'] }
|
|
287
|
-
}
|
|
288
|
-
for (const name of entries) {
|
|
289
|
-
if (!name.endsWith('.json')) { unresolved.push(`ownership:${name}:unrecognized`); continue }
|
|
290
|
-
let r
|
|
291
|
-
try { r = JSON.parse(fs.readFileSync(path.join(receiptDir(discovery), name), 'utf8')) } catch { unresolved.push(`ownership:${name}:unreadable`); continue }
|
|
292
|
-
if (!r || `${r.id}.json` !== name || !/^[a-z0-9][a-z0-9._-]*$/i.test(r.id) || r.brokerPid !== broker.pid || r.brokerMarker !== broker.recoveryMarker || r.state !== 'spawned' || !Number.isSafeInteger(r.pid) || r.pid <= 0 || !r.identity) {
|
|
293
|
-
unresolved.push(`ownership:${name}:unverified`); continue
|
|
294
|
-
}
|
|
295
|
-
if (!absent(broker.pid)) { unresolved.push('broker:not-confirmed-absent'); break }
|
|
296
|
-
if (absent(r.pid)) {
|
|
297
|
-
// Dead root: only a receipt whose declared ports are all free can be
|
|
298
|
-
// resolved; any listener may be an unverified descendant.
|
|
299
|
-
if (await waitReceiptResolvable(r, discovered[r.id]?.ports)) {
|
|
300
|
-
removeReceipt(discovery, r.id)
|
|
301
|
-
stopped.push(r.id)
|
|
302
|
-
} else unresolved.push(`service:${r.id}:already-exited-descendants-unverified`)
|
|
303
|
-
continue
|
|
304
|
-
}
|
|
305
|
-
if (!absent(r.pid)) {
|
|
306
|
-
if (marker(r.pid) !== r.identity) { unresolved.push(`service:${r.id}:identity-unverified`); continue }
|
|
307
|
-
if (process.platform === 'win32') {
|
|
308
|
-
const result = spawnSync('taskkill', ['/PID', String(r.pid), '/F'], { stdio: 'ignore', timeout: 10000, windowsHide: true })
|
|
309
|
-
if (result.status !== 0 && !absent(r.pid)) { unresolved.push(`service:${r.id}:stop-failed`); continue }
|
|
310
|
-
} else {
|
|
311
|
-
try { process.kill(r.pid, 'SIGTERM') } catch (e) { if (e.code !== 'ESRCH') { unresolved.push(`service:${r.id}:stop-failed`); continue } }
|
|
312
|
-
}
|
|
313
|
-
for (let i = 0; i < 50 && !absent(r.pid); i++) await new Promise(resolve => setTimeout(resolve, 100))
|
|
314
|
-
if (!absent(r.pid)) { unresolved.push(`service:${r.id}:still-running`); continue }
|
|
315
|
-
}
|
|
316
|
-
// Root exit alone does not prove its descendants exited. It is safe to
|
|
317
|
-
// resolve once none of the declared ports is still listening; otherwise
|
|
318
|
-
// keep the receipt as evidence (fail closed).
|
|
319
|
-
if (await waitReceiptResolvable(r, discovered[r.id]?.ports)) {
|
|
320
|
-
removeReceipt(discovery, r.id)
|
|
321
|
-
stopped.push(r.id)
|
|
322
|
-
} else unresolved.push(`service:${r.id}:descendants-unverified`)
|
|
323
|
-
}
|
|
324
|
-
return { stopped, unresolved }
|
|
325
|
-
}
|
|
@@ -1,16 +1,29 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* 技能瘦客户端:把独立技能服务的 HTTP 面适配成进程内形状(list/get)。
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* 定位(P1-10 决策):技能的标准路径是 skillProvider@1 进程内注册 →
|
|
5
|
+
* DSH 原生 SkillRegistry(ctx.skills);本客户端只在显式配置 skills.url 的
|
|
6
|
+
* standalone 模式下启用,默认不启用。
|
|
7
|
+
*
|
|
8
|
+
* 服务名刻意叫 **skillsHttp** 而不是 skills:'skills' 是 DSH 原生 SkillRegistry
|
|
9
|
+
* 的服务名(foundation 经 getService(ctx,'skills') 取用,skillProvider@1 靠它的
|
|
10
|
+
* registerProvider 挂载)。若本客户端 provide('skills') 会遮蔽原生注册表,且瘦
|
|
11
|
+
* 客户端没有 registerProvider,全部 skillProvider@1 插件将 E_CAP_MISSING 挂载
|
|
12
|
+
* 失败。改名后原生 ctx.skills 永不被遮蔽,两条路可共存。
|
|
13
|
+
*
|
|
14
|
+
* 服务端形状见 C/packages/capyclaw-skills-service(GET /skills、GET /skills/:name)。
|
|
7
15
|
*/
|
|
8
16
|
export function createSkillsClient(baseUrl) {
|
|
9
17
|
const root = String(baseUrl || '').replace(/\/$/, '')
|
|
10
18
|
return {
|
|
11
|
-
api: { name: '
|
|
19
|
+
api: { name: 'skillsHttp', version: '1.0.0', kind: 'thin-client' },
|
|
12
20
|
async list(signal) {
|
|
13
|
-
|
|
21
|
+
let res
|
|
22
|
+
try {
|
|
23
|
+
res = await fetch(`${root}/skills`, { signal: signal ?? AbortSignal.timeout(5000) })
|
|
24
|
+
} catch (error) {
|
|
25
|
+
throw new Error(`skills 服务不可达(${root}):${error.message}`, { cause: error })
|
|
26
|
+
}
|
|
14
27
|
if (!res.ok) throw new Error(`skills list HTTP ${res.status}`)
|
|
15
28
|
const body = await res.json()
|
|
16
29
|
return Array.isArray(body?.skills) ? body.skills : []
|
|
@@ -18,10 +31,17 @@ export function createSkillsClient(baseUrl) {
|
|
|
18
31
|
async get(candidate, options = {}) {
|
|
19
32
|
const name = typeof candidate === 'string' ? candidate : candidate?.name
|
|
20
33
|
if (!name) throw new Error('skills get: 缺 name')
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
34
|
+
let res
|
|
35
|
+
try {
|
|
36
|
+
res = await fetch(`${root}/skills/${encodeURIComponent(name)}`, {
|
|
37
|
+
signal: options.signal ?? AbortSignal.timeout(10000),
|
|
38
|
+
})
|
|
39
|
+
} catch (error) {
|
|
40
|
+
throw new Error(`skills 服务不可达(${root}):${error.message}`, { cause: error })
|
|
41
|
+
}
|
|
42
|
+
/* 只有 404 才是「未知 skill」;500/503 是服务端故障,不能误导成名字写错。 */
|
|
43
|
+
if (res.status === 404) throw new Error(`未知 skill ${name}(HTTP 404)`)
|
|
44
|
+
if (!res.ok) throw new Error(`skills get ${name} 失败(HTTP ${res.status},服务端错误)`)
|
|
25
45
|
return res.json()
|
|
26
46
|
},
|
|
27
47
|
}
|