@capyclaw/skills-service 0.1.3 → 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/README.md +7 -7
- package/node_modules/@capyclaw/foundation/README.md +51 -0
- package/node_modules/@capyclaw/foundation/abi/bridge.mjs +86 -0
- package/node_modules/@capyclaw/foundation/abi/errors.mjs +63 -0
- package/node_modules/@capyclaw/foundation/abi/index.mjs +35 -0
- package/node_modules/@capyclaw/foundation/abi/kinds.mjs +271 -0
- package/node_modules/@capyclaw/foundation/abi/loader.mjs +345 -0
- package/node_modules/@capyclaw/foundation/abi/schema.mjs +98 -0
- package/node_modules/@capyclaw/foundation/abi/spec.mjs +255 -0
- package/node_modules/@capyclaw/foundation/abi/validate.mjs +24 -0
- package/node_modules/@capyclaw/foundation/abi/version.mjs +82 -0
- package/node_modules/@capyclaw/foundation/cordis.patch.yml +5 -0
- package/node_modules/@capyclaw/foundation/kit/index.mjs +6 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/client-panel.mjs +98 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/host-event.mjs +35 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/http-router.mjs +69 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +55 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/own-server.mjs +113 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +68 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +113 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/service-provider.mjs +77 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/skill-provider.mjs +56 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/tool-provider.mjs +50 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/ui-panel.mjs +64 -0
- package/node_modules/@capyclaw/foundation/lib/ctx-service.mjs +42 -0
- package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +34 -0
- package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +85 -0
- package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +50 -0
- package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +160 -0
- package/node_modules/@capyclaw/foundation/lib/host.mjs +23 -0
- package/node_modules/@capyclaw/foundation/lib/kernel.mjs +185 -0
- package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +87 -0
- package/node_modules/@capyclaw/foundation/lib/subsystems/events.mjs +60 -0
- package/node_modules/@capyclaw/foundation/lib/subsystems/log.mjs +51 -0
- package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +188 -0
- package/node_modules/@capyclaw/foundation/package.json +53 -0
- package/node_modules/@capyclaw/foundation/runtime/index.mjs +20 -0
- package/node_modules/@capyclaw/plugin-kit/INTEGRATION.md +100 -0
- package/node_modules/@capyclaw/plugin-kit/README.md +70 -0
- package/node_modules/@capyclaw/plugin-kit/bin/sync.mjs +103 -0
- package/node_modules/@capyclaw/plugin-kit/package.json +69 -0
- package/node_modules/@capyclaw/plugin-kit/src/cli.mjs +56 -0
- package/node_modules/@capyclaw/plugin-kit/src/config.mjs +142 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +129 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +139 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/shadow.mjs +219 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/transport.js +13 -0
- package/node_modules/@capyclaw/plugin-kit/src/index.mjs +13 -0
- package/node_modules/@capyclaw/plugin-kit/src/pack.mjs +230 -0
- package/node_modules/@capyclaw/plugin-kit/src/runtime/context.mjs +128 -0
- package/node_modules/@capyclaw/plugin-kit/src/self-contained.mjs +307 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/api.mjs +45 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/http.mjs +113 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/mcp-http.mjs +143 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/server.mjs +131 -0
- package/node_modules/@capyclaw/plugin-kit/src/skills/provider.mjs +82 -0
- package/node_modules/@capyclaw/plugin-kit/src/store/blob-store.mjs +109 -0
- package/node_modules/@capyclaw/plugin-kit/src/store/content-store.mjs +17 -0
- package/node_modules/@capyclaw/plugin-kit/src/store/journal-store.mjs +89 -0
- package/node_modules/@capyclaw/plugin-kit/src/tools/result.mjs +41 -0
- package/node_modules/@capyclaw/plugin-kit/src/tools/validate.mjs +150 -0
- package/node_modules/@capyclaw/plugin-kit/src/tuning.mjs +82 -0
- package/node_modules/@capyclaw/plugin-kit/src/util/fs-lock.mjs +132 -0
- package/node_modules/@capyclaw/plugin-kit/src/util/http.mjs +130 -0
- package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +656 -0
- package/node_modules/@capyclaw/plugin-kit/src/zip.mjs +181 -0
- package/package.json +18 -2
- package/server.mjs +237 -201
package/server.mjs
CHANGED
|
@@ -1,201 +1,237 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* 职责:把原「ctx.skills 进程内注册表」的技能内容(SKILL.md)搬出 dsh
|
|
6
|
-
* 进程,作为独立端口服务对外提供:
|
|
7
|
-
* GET /healthz { ok, service, pid, port, providers, skills }
|
|
8
|
-
* GET /skills 全量技能清单(汇总各 provider)
|
|
9
|
-
* GET /skills/:name 单技能正文(原 provider.get 形状)
|
|
10
|
-
*
|
|
11
|
-
* dsh 侧经
|
|
12
|
-
* 技能数据源 = 各
|
|
13
|
-
*
|
|
14
|
-
* 由
|
|
15
|
-
* { "id": "dsh-skills",
|
|
16
|
-
* "cmd": ["node", "<本目录>/server.mjs", "--port", "${PORT}"],
|
|
17
|
-
* "ports": { "main":
|
|
18
|
-
* "url": "http://127.0.0.1:${PORT}" }
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
req.
|
|
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
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
-
const
|
|
123
|
-
const dirs =
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* capyclaw-skills-service:独立技能服务(服务解耦标准形态)。
|
|
4
|
+
*
|
|
5
|
+
* 职责:把原「ctx.skills 进程内注册表」的技能内容(SKILL.md)搬出 dsh
|
|
6
|
+
* 进程,作为独立端口服务对外提供:
|
|
7
|
+
* GET /healthz { ok, service, pid, port, providers, skills }
|
|
8
|
+
* GET /skills 全量技能清单(汇总各 provider)
|
|
9
|
+
* GET /skills/:name 单技能正文(原 provider.get 形状)
|
|
10
|
+
*
|
|
11
|
+
* dsh 侧经 @capyclaw/service-broker 的 skills 瘦客户端(ctx.skills)访问本服务。
|
|
12
|
+
* 技能数据源 = 各 capyclaw-ppt-skill-* 插件目录(不搬内容,按目录聚合)。
|
|
13
|
+
*
|
|
14
|
+
* 由 @capyclaw/service-broker 按清单拉起:
|
|
15
|
+
* { "id": "dsh-skills",
|
|
16
|
+
* "cmd": ["node", "<本目录>/server.mjs", "--port", "${PORT}"],
|
|
17
|
+
* "ports": { "main": "auto" },
|
|
18
|
+
* "url": "http://127.0.0.1:${PORT}" }
|
|
19
|
+
*
|
|
20
|
+
* 端口一律由 broker 分配(`'auto'`);独立手跑时不指定 `--port` 则由 OS 随机分配
|
|
21
|
+
* (默认 0),真实端口见启动日志与 `/healthz`。**不要**在这里写死数字:写死会让
|
|
22
|
+
* 多套件/多实例互抢,也会把用户和文档绑在一个端口上。
|
|
23
|
+
*/
|
|
24
|
+
import http from 'node:http'
|
|
25
|
+
import fs from 'node:fs'
|
|
26
|
+
import path from 'node:path'
|
|
27
|
+
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
28
|
+
import { createKernel } from '@capyclaw/foundation/runtime'
|
|
29
|
+
|
|
30
|
+
const HERE = path.dirname(fileURLToPath(import.meta.url))
|
|
31
|
+
/** 0 = 交给 OS 分配(独立运行时的默认;broker 拉起时用 `--port ${PORT}` 显式给)。 */
|
|
32
|
+
const DEFAULT_PORT = 0
|
|
33
|
+
|
|
34
|
+
function arg(name, dflt) {
|
|
35
|
+
const i = process.argv.indexOf(name)
|
|
36
|
+
return i >= 0 && process.argv[i + 1] ? process.argv[i + 1] : dflt
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function loopback(method, url) {
|
|
40
|
+
return new Promise((resolve, reject) => {
|
|
41
|
+
const req = http.request(url, { method }, (res) => {
|
|
42
|
+
let text = ''
|
|
43
|
+
res.on('data', (c) => { text += c })
|
|
44
|
+
res.on('end', () => resolve({ status: res.statusCode, text }))
|
|
45
|
+
})
|
|
46
|
+
req.setTimeout(1200, () => { req.destroy(new Error('timeout')) })
|
|
47
|
+
req.once('error', reject)
|
|
48
|
+
req.end()
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** 收集技能 provider:每个插件目录含 plugin.mjs(foundation ABI)。
|
|
53
|
+
* 宿主只提供 ctx.skills,其余能力不承诺——与 dsh 里的装配路径完全一致。 */
|
|
54
|
+
async function loadProviders(dirs) {
|
|
55
|
+
const providers = []
|
|
56
|
+
const seen = new Set()
|
|
57
|
+
for (const dir of dirs) {
|
|
58
|
+
const key = path.resolve(dir).toLowerCase()
|
|
59
|
+
if (seen.has(key)) continue
|
|
60
|
+
seen.add(key)
|
|
61
|
+
const entry = path.join(dir, 'plugin.mjs')
|
|
62
|
+
if (!fs.existsSync(entry)) continue
|
|
63
|
+
try {
|
|
64
|
+
const registered = []
|
|
65
|
+
const ctx = {
|
|
66
|
+
skills: {
|
|
67
|
+
registerProvider(factory) {
|
|
68
|
+
registered.push(factory())
|
|
69
|
+
return () => {}
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
}
|
|
73
|
+
const kernel = createKernel(ctx)
|
|
74
|
+
ctx.os = kernel.os
|
|
75
|
+
const mod = await import(pathToFileURL(entry).href)
|
|
76
|
+
const dispose = mod.apply(ctx, {})
|
|
77
|
+
const provider = registered[0]
|
|
78
|
+
if (!provider) throw new Error('plugin.mjs 未注册 provider')
|
|
79
|
+
const skills = await provider.list()
|
|
80
|
+
providers.push({ dir, provider, skills, dispose, kernel })
|
|
81
|
+
console.log(`[dsh-skills] provider ${path.basename(dir)}: ${skills.map((s) => s.name).join(', ') || '(空)'}`)
|
|
82
|
+
} catch (err) {
|
|
83
|
+
console.warn(`[dsh-skills] 跳过 ${path.basename(dir)}: ${err.message}`)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return providers
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** 候选技能目录:四种布局都要覆盖。
|
|
90
|
+
* dev 树:<repo>/projects/<项目>/artifacts/plugin/capyclaw-ppt-skill-*(构建产物目录)。
|
|
91
|
+
* npm 扁平安装:node_modules/@capyclaw/ppt-skill-*——父目录本身就是 @capyclaw
|
|
92
|
+
* 作用域目录,ppt-skill-* 是它的直接子项。
|
|
93
|
+
* pnpm 提升可见时:<profile>/node_modules/@capyclaw/ppt-skill-*(同上)。
|
|
94
|
+
* pnpm 严格布局:本包真实路径在 <profile>/node_modules/.pnpm/<pkg>/node_modules/@capyclaw/skills-service,
|
|
95
|
+
* 上溯 4 级是 .pnpm;技能包以虚拟目录 @capyclaw+ppt-skill-<x>@<ver> 平铺在 .pnpm 下,
|
|
96
|
+
* 每个虚拟目录的真实树在其 node_modules/@capyclaw/ 里。 */
|
|
97
|
+
|
|
98
|
+
/** dev 树里的技能包根:仓库根 A/projects/<项目>/artifacts/plugin(每个项目一个,互不混放)。 */
|
|
99
|
+
function devPluginRoots() {
|
|
100
|
+
const projectsDir = path.resolve(HERE, '..', '..', '..', 'A', 'projects')
|
|
101
|
+
const out = []
|
|
102
|
+
let entries = []
|
|
103
|
+
try { entries = fs.readdirSync(projectsDir, { withFileTypes: true }) } catch { return out }
|
|
104
|
+
for (const entry of entries) {
|
|
105
|
+
if (!entry.isDirectory()) continue
|
|
106
|
+
const root = path.join(projectsDir, entry.name, 'artifacts', 'plugin')
|
|
107
|
+
if (fs.existsSync(root)) out.push(root)
|
|
108
|
+
}
|
|
109
|
+
return out
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function defaultProviderDirs() {
|
|
113
|
+
const roots = [
|
|
114
|
+
...devPluginRoots(),
|
|
115
|
+
path.resolve(HERE, '..'),
|
|
116
|
+
path.resolve(HERE, '..', '..'),
|
|
117
|
+
path.resolve(HERE, '..', '..', 'node_modules'),
|
|
118
|
+
path.resolve(HERE, '..', 'node_modules'),
|
|
119
|
+
path.resolve(HERE, 'node_modules'),
|
|
120
|
+
path.resolve(HERE, '..', '..', '..', '..'),
|
|
121
|
+
]
|
|
122
|
+
const seen = new Set()
|
|
123
|
+
const dirs = []
|
|
124
|
+
const add = (dir) => {
|
|
125
|
+
const key = path.resolve(dir)
|
|
126
|
+
if (!seen.has(key)) { seen.add(key); dirs.push(key) }
|
|
127
|
+
}
|
|
128
|
+
for (const root of roots) {
|
|
129
|
+
let entries = []
|
|
130
|
+
try { entries = fs.readdirSync(root, { withFileTypes: true }) } catch { continue }
|
|
131
|
+
for (const e of entries) {
|
|
132
|
+
if (!e.isDirectory()) continue
|
|
133
|
+
// dev 目录名 capyclaw-ppt-skill-* 与安装态 @capyclaw/ppt-skill-* 都认(后者在作用域目录下出现)
|
|
134
|
+
if (e.name.startsWith('capyclaw-ppt-skill-') || (root.endsWith('@capyclaw') && e.name.startsWith('ppt-skill-'))) {
|
|
135
|
+
add(path.join(root, e.name))
|
|
136
|
+
} else if (e.name === '@capyclaw') {
|
|
137
|
+
// 作用域目录:扫一层,收 ppt-skill-*
|
|
138
|
+
let scoped = []
|
|
139
|
+
try { scoped = fs.readdirSync(path.join(root, e.name), { withFileTypes: true }) } catch { continue }
|
|
140
|
+
for (const s of scoped) {
|
|
141
|
+
if (s.isDirectory() && s.name.startsWith('ppt-skill-')) add(path.join(root, e.name, s.name))
|
|
142
|
+
}
|
|
143
|
+
} else if (e.name.startsWith('@capyclaw+ppt-skill-') && e.name.includes('@', '@capyclaw+ppt-skill-'.length)) {
|
|
144
|
+
// pnpm 虚拟目录:@capyclaw+ppt-skill-<x>@<version> → 真实树在 <dir>/node_modules/@capyclaw/<x>
|
|
145
|
+
const base = e.name.slice('@capyclaw+'.length).split('@')[0]
|
|
146
|
+
const real = path.join(root, e.name, 'node_modules', '@capyclaw', base)
|
|
147
|
+
if (fs.existsSync(path.join(real, 'package.json'))) add(real)
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return dirs
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async function main() {
|
|
155
|
+
const raw = arg('--port', process.env.PORT || DEFAULT_PORT)
|
|
156
|
+
const port = Number(raw) >= 0 ? Number(raw) : DEFAULT_PORT
|
|
157
|
+
const providersArg = arg('--providers', '')
|
|
158
|
+
const dirs = providersArg
|
|
159
|
+
? providersArg.split(path.delimiter === ';' ? ';' : ',').map((s) => s.trim()).filter(Boolean)
|
|
160
|
+
: defaultProviderDirs()
|
|
161
|
+
|
|
162
|
+
// 复用优先:端口上已是本服务(healthz 自报 dsh-skills)→ 直接退出 0,
|
|
163
|
+
// 让中间件按「已有健康实例」收编,不双开。端口为 0(OS 随机)时无从复用,跳过。
|
|
164
|
+
try {
|
|
165
|
+
if (port === 0) throw new Error('no fixed port')
|
|
166
|
+
const probe = await loopback('GET', `http://127.0.0.1:${port}/healthz`)
|
|
167
|
+
if (probe.status === 200 && probe.text.includes('"dsh-skills"')) {
|
|
168
|
+
console.log(`[dsh-skills] 端口 ${port} 已有本服务实例,复用退出`)
|
|
169
|
+
process.exit(0)
|
|
170
|
+
}
|
|
171
|
+
} catch { /* 没有实例,正常监听 */ }
|
|
172
|
+
|
|
173
|
+
const providers = await loadProviders(dirs)
|
|
174
|
+
const byName = new Map()
|
|
175
|
+
for (const { provider, skills } of providers) {
|
|
176
|
+
for (const skill of skills) byName.set(skill.name, { provider, skill })
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const server = http.createServer(async (req, res) => {
|
|
180
|
+
const send = (code, obj) => {
|
|
181
|
+
const body = JSON.stringify(obj)
|
|
182
|
+
res.writeHead(code, { 'content-type': 'application/json; charset=utf-8', 'content-length': Buffer.byteLength(body) })
|
|
183
|
+
res.end(body)
|
|
184
|
+
}
|
|
185
|
+
try {
|
|
186
|
+
const url = new URL(req.url, 'http://127.0.0.1')
|
|
187
|
+
if (url.pathname === '/healthz') {
|
|
188
|
+
return send(200, {
|
|
189
|
+
ok: true,
|
|
190
|
+
service: 'dsh-skills',
|
|
191
|
+
pid: process.pid,
|
|
192
|
+
port: server.address()?.port || port,
|
|
193
|
+
providers: providers.length,
|
|
194
|
+
skills: byName.size,
|
|
195
|
+
uptimeSec: Math.round(process.uptime()),
|
|
196
|
+
})
|
|
197
|
+
}
|
|
198
|
+
if (url.pathname === '/skills' && req.method === 'GET') {
|
|
199
|
+
return send(200, { ok: true, skills: [...byName.values()].map(({ skill }) => skill) })
|
|
200
|
+
}
|
|
201
|
+
const hit = /^\/skills\/([^/]+)$/.exec(url.pathname)
|
|
202
|
+
if (hit && req.method === 'GET') {
|
|
203
|
+
const entry = byName.get(decodeURIComponent(hit[1]))
|
|
204
|
+
if (!entry) return send(404, { ok: false, error: `未知 skill ${decodeURIComponent(hit[1])}` })
|
|
205
|
+
return send(200, await entry.provider.get({ name: entry.skill.name }))
|
|
206
|
+
}
|
|
207
|
+
send(404, { ok: false, error: `未知端点 ${req.method} ${url.pathname}` })
|
|
208
|
+
} catch (err) {
|
|
209
|
+
send(500, { ok: false, error: err.message })
|
|
210
|
+
}
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
await new Promise((resolve, reject) => {
|
|
214
|
+
server.once('error', reject)
|
|
215
|
+
server.listen(port, '127.0.0.1', resolve)
|
|
216
|
+
})
|
|
217
|
+
const actual = server.address().port
|
|
218
|
+
console.log(`[dsh-skills] 就绪 http://127.0.0.1:${actual}/skills(${providers.length} 个 provider,${byName.size} 个技能)`)
|
|
219
|
+
|
|
220
|
+
let stopping = false
|
|
221
|
+
const shutdown = async () => {
|
|
222
|
+
if (stopping) return
|
|
223
|
+
stopping = true
|
|
224
|
+
console.log('[dsh-skills] 停机中…')
|
|
225
|
+
await new Promise((r) => {
|
|
226
|
+
try { server.closeAll(); server.close(() => r()) } catch { r() }
|
|
227
|
+
})
|
|
228
|
+
process.exit(0)
|
|
229
|
+
}
|
|
230
|
+
process.on('SIGINT', shutdown)
|
|
231
|
+
process.on('SIGTERM', shutdown)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
main().catch((err) => {
|
|
235
|
+
console.error('[dsh-skills] 启动失败:', err.message)
|
|
236
|
+
process.exit(1)
|
|
237
|
+
})
|