@capyclaw/skills-service 0.1.2 → 0.1.4
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 +5 -5
- package/package.json +11 -2
- package/server.mjs +201 -194
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# capyclaw-skills-service
|
|
2
2
|
|
|
3
3
|
DSH 独立技能服务(服务解耦标准形态)。把原「`ctx.skills` 进程内注册表」的
|
|
4
4
|
技能内容(SKILL.md)搬出 dsh 进程,作为独立端口服务:
|
|
@@ -11,17 +11,17 @@ GET /skills/:name 单技能正文(原 provider.get 形状)
|
|
|
11
11
|
|
|
12
12
|
## 技能数据源
|
|
13
13
|
|
|
14
|
-
自动扫描两类布局(都找 `
|
|
14
|
+
自动扫描两类布局(都找 `capyclaw-ppt-skill-*` 目录里的 `lib/index.mjs`):
|
|
15
15
|
|
|
16
|
-
- dev 树:`../plugin/
|
|
17
|
-
- 套件安装态:本目录同级(`node_modules/
|
|
16
|
+
- dev 树:`../plugin/capyclaw-ppt-skill-*`
|
|
17
|
+
- 套件安装态:本目录同级(`node_modules/capyclaw-ppt-skill-*`)
|
|
18
18
|
|
|
19
19
|
可用 `--providers <dir>[[;|,]dir…]` 显式指定。
|
|
20
20
|
|
|
21
21
|
## 由谁拉起
|
|
22
22
|
|
|
23
23
|
`dsh-service-broker` 按清单以独立进程拉起(套件捆绑布局的清单见
|
|
24
|
-
|
|
24
|
+
@capyclaw/ppt-suite 包根 `services.json`,端口 39321)。dsh 侧经 broker 的
|
|
25
25
|
skills 瘦客户端(`ctx.skills` 兼容形状)访问本服务。
|
|
26
26
|
|
|
27
27
|
## 独立运行
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capyclaw/skills-service",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "DSH 独立技能服务:聚合 @capyclaw/ppt-skill-* 的 SKILL.md 内容,HTTP 面暴露(GET /healthz、/skills、/skills/:name)。服务解耦标准形态:技能内容不再进程内注册,dsh 侧经 broker 的 skills 瘦客户端访问。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./server.mjs",
|
|
@@ -14,5 +14,14 @@
|
|
|
14
14
|
"server.mjs",
|
|
15
15
|
"README.md"
|
|
16
16
|
],
|
|
17
|
-
"license": "MIT"
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/Maple496/capyclaw.git",
|
|
21
|
+
"directory": "extensions/bridges/ppt-bridge/capyclaw-skills-service"
|
|
22
|
+
},
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/Maple496/capyclaw/issues"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/Maple496/capyclaw/tree/main/extensions/bridges/ppt-bridge/capyclaw-skills-service#readme"
|
|
18
27
|
}
|
package/server.mjs
CHANGED
|
@@ -1,194 +1,201 @@
|
|
|
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 侧经 dsh-service-broker 的 skills 瘦客户端(ctx.skills)访问本服务。
|
|
12
|
-
* 技能数据源 = 各
|
|
13
|
-
*
|
|
14
|
-
* 由 dsh-service-broker 按清单拉起:
|
|
15
|
-
* { "id": "dsh-skills",
|
|
16
|
-
* "cmd": ["node", "<本目录>/server.mjs", "--port", "${PORT}"],
|
|
17
|
-
* "ports": { "main": 39321 },
|
|
18
|
-
* "url": "http://127.0.0.1:${PORT}" }
|
|
19
|
-
*/
|
|
20
|
-
import http from 'node:http'
|
|
21
|
-
import fs from 'node:fs'
|
|
22
|
-
import path from 'node:path'
|
|
23
|
-
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
24
|
-
|
|
25
|
-
const HERE = path.dirname(fileURLToPath(import.meta.url))
|
|
26
|
-
const DEFAULT_PORT = 39321
|
|
27
|
-
|
|
28
|
-
function arg(name, dflt) {
|
|
29
|
-
const i = process.argv.indexOf(name)
|
|
30
|
-
return i >= 0 && process.argv[i + 1] ? process.argv[i + 1] : dflt
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function loopback(method, url) {
|
|
34
|
-
return new Promise((resolve, reject) => {
|
|
35
|
-
const req = http.request(url, { method }, (res) => {
|
|
36
|
-
let text = ''
|
|
37
|
-
res.on('data', (c) => { text += c })
|
|
38
|
-
res.on('end', () => resolve({ status: res.statusCode, text }))
|
|
39
|
-
})
|
|
40
|
-
req.setTimeout(1200, () => { req.destroy(new Error('timeout')) })
|
|
41
|
-
req.once('error', reject)
|
|
42
|
-
req.end()
|
|
43
|
-
})
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** 收集技能 provider:目录列表,每个须含 lib/index.mjs(导出 createProvider)。 */
|
|
47
|
-
async function loadProviders(dirs) {
|
|
48
|
-
const providers = []
|
|
49
|
-
const seen = new Set()
|
|
50
|
-
for (const dir of dirs) {
|
|
51
|
-
const key = path.resolve(dir).toLowerCase()
|
|
52
|
-
if (seen.has(key)) continue
|
|
53
|
-
seen.add(key)
|
|
54
|
-
const entry = path.join(dir, 'lib', 'index.mjs')
|
|
55
|
-
if (!fs.existsSync(entry)) continue
|
|
56
|
-
try {
|
|
57
|
-
const mod = await import(pathToFileURL(entry).href)
|
|
58
|
-
if (typeof mod.createProvider !== 'function') continue
|
|
59
|
-
const provider = mod.createProvider({})
|
|
60
|
-
const skills = await provider.list()
|
|
61
|
-
providers.push({ dir, provider, skills })
|
|
62
|
-
console.log(`[dsh-skills] provider ${path.basename(dir)}: ${skills.map((s) => s.name).join(', ') || '(空)'}`)
|
|
63
|
-
} catch (err) {
|
|
64
|
-
console.warn(`[dsh-skills] 跳过 ${path.basename(dir)}: ${err.message}`)
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return providers
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* dev 树:../plugin/
|
|
72
|
-
* npm 扁平安装:node_modules/@capyclaw/ppt-skill
|
|
73
|
-
* 作用域目录,ppt-skill-*
|
|
74
|
-
* pnpm
|
|
75
|
-
*
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
path.resolve(HERE, '..', '
|
|
81
|
-
path.resolve(HERE, '..'
|
|
82
|
-
path.resolve(HERE, '..', '
|
|
83
|
-
path.resolve(HERE, 'node_modules'),
|
|
84
|
-
path.resolve(HERE, '..', '
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
if (e.
|
|
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
|
-
|
|
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
|
-
|
|
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 侧经 dsh-service-broker 的 skills 瘦客户端(ctx.skills)访问本服务。
|
|
12
|
+
* 技能数据源 = 各 capyclaw-ppt-skill-* 插件目录(不搬内容,按目录聚合)。
|
|
13
|
+
*
|
|
14
|
+
* 由 dsh-service-broker 按清单拉起:
|
|
15
|
+
* { "id": "dsh-skills",
|
|
16
|
+
* "cmd": ["node", "<本目录>/server.mjs", "--port", "${PORT}"],
|
|
17
|
+
* "ports": { "main": 39321 },
|
|
18
|
+
* "url": "http://127.0.0.1:${PORT}" }
|
|
19
|
+
*/
|
|
20
|
+
import http from 'node:http'
|
|
21
|
+
import fs from 'node:fs'
|
|
22
|
+
import path from 'node:path'
|
|
23
|
+
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
24
|
+
|
|
25
|
+
const HERE = path.dirname(fileURLToPath(import.meta.url))
|
|
26
|
+
const DEFAULT_PORT = 39321
|
|
27
|
+
|
|
28
|
+
function arg(name, dflt) {
|
|
29
|
+
const i = process.argv.indexOf(name)
|
|
30
|
+
return i >= 0 && process.argv[i + 1] ? process.argv[i + 1] : dflt
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function loopback(method, url) {
|
|
34
|
+
return new Promise((resolve, reject) => {
|
|
35
|
+
const req = http.request(url, { method }, (res) => {
|
|
36
|
+
let text = ''
|
|
37
|
+
res.on('data', (c) => { text += c })
|
|
38
|
+
res.on('end', () => resolve({ status: res.statusCode, text }))
|
|
39
|
+
})
|
|
40
|
+
req.setTimeout(1200, () => { req.destroy(new Error('timeout')) })
|
|
41
|
+
req.once('error', reject)
|
|
42
|
+
req.end()
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 收集技能 provider:目录列表,每个须含 lib/index.mjs(导出 createProvider)。 */
|
|
47
|
+
async function loadProviders(dirs) {
|
|
48
|
+
const providers = []
|
|
49
|
+
const seen = new Set()
|
|
50
|
+
for (const dir of dirs) {
|
|
51
|
+
const key = path.resolve(dir).toLowerCase()
|
|
52
|
+
if (seen.has(key)) continue
|
|
53
|
+
seen.add(key)
|
|
54
|
+
const entry = path.join(dir, 'lib', 'index.mjs')
|
|
55
|
+
if (!fs.existsSync(entry)) continue
|
|
56
|
+
try {
|
|
57
|
+
const mod = await import(pathToFileURL(entry).href)
|
|
58
|
+
if (typeof mod.createProvider !== 'function') continue
|
|
59
|
+
const provider = mod.createProvider({})
|
|
60
|
+
const skills = await provider.list()
|
|
61
|
+
providers.push({ dir, provider, skills })
|
|
62
|
+
console.log(`[dsh-skills] provider ${path.basename(dir)}: ${skills.map((s) => s.name).join(', ') || '(空)'}`)
|
|
63
|
+
} catch (err) {
|
|
64
|
+
console.warn(`[dsh-skills] 跳过 ${path.basename(dir)}: ${err.message}`)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return providers
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** 候选技能目录:四种布局都要覆盖。
|
|
71
|
+
* dev 树:../plugin/capyclaw-ppt-skill-*。
|
|
72
|
+
* npm 扁平安装:node_modules/@capyclaw/ppt-skill-*——父目录本身就是 @capyclaw
|
|
73
|
+
* 作用域目录,ppt-skill-* 是它的直接子项。
|
|
74
|
+
* pnpm 提升可见时:<profile>/node_modules/@capyclaw/ppt-skill-*(同上)。
|
|
75
|
+
* pnpm 严格布局:本包真实路径在 <profile>/node_modules/.pnpm/<pkg>/node_modules/@capyclaw/skills-service,
|
|
76
|
+
* 上溯 4 级是 .pnpm;技能包以虚拟目录 @capyclaw+ppt-skill-<x>@<ver> 平铺在 .pnpm 下,
|
|
77
|
+
* 每个虚拟目录的真实树在其 node_modules/@capyclaw/ 里。 */
|
|
78
|
+
function defaultProviderDirs() {
|
|
79
|
+
const roots = [
|
|
80
|
+
path.resolve(HERE, '..', 'plugin'),
|
|
81
|
+
path.resolve(HERE, '..'),
|
|
82
|
+
path.resolve(HERE, '..', '..'),
|
|
83
|
+
path.resolve(HERE, '..', '..', 'node_modules'),
|
|
84
|
+
path.resolve(HERE, '..', 'node_modules'),
|
|
85
|
+
path.resolve(HERE, 'node_modules'),
|
|
86
|
+
path.resolve(HERE, '..', '..', '..', '..'),
|
|
87
|
+
]
|
|
88
|
+
const seen = new Set()
|
|
89
|
+
const dirs = []
|
|
90
|
+
const add = (dir) => {
|
|
91
|
+
const key = path.resolve(dir)
|
|
92
|
+
if (!seen.has(key)) { seen.add(key); dirs.push(key) }
|
|
93
|
+
}
|
|
94
|
+
for (const root of roots) {
|
|
95
|
+
let entries = []
|
|
96
|
+
try { entries = fs.readdirSync(root, { withFileTypes: true }) } catch { continue }
|
|
97
|
+
for (const e of entries) {
|
|
98
|
+
if (!e.isDirectory()) continue
|
|
99
|
+
// dev 目录名 capyclaw-ppt-skill-* 与安装态 @capyclaw/ppt-skill-* 都认(后者在作用域目录下出现)
|
|
100
|
+
if (e.name.startsWith('capyclaw-ppt-skill-') || (root.endsWith('@capyclaw') && e.name.startsWith('ppt-skill-'))) {
|
|
101
|
+
add(path.join(root, e.name))
|
|
102
|
+
} else if (e.name === '@capyclaw') {
|
|
103
|
+
// 作用域目录:扫一层,收 ppt-skill-*
|
|
104
|
+
let scoped = []
|
|
105
|
+
try { scoped = fs.readdirSync(path.join(root, e.name), { withFileTypes: true }) } catch { continue }
|
|
106
|
+
for (const s of scoped) {
|
|
107
|
+
if (s.isDirectory() && s.name.startsWith('ppt-skill-')) add(path.join(root, e.name, s.name))
|
|
108
|
+
}
|
|
109
|
+
} else if (e.name.startsWith('@capyclaw+ppt-skill-') && e.name.includes('@', '@capyclaw+ppt-skill-'.length)) {
|
|
110
|
+
// pnpm 虚拟目录:@capyclaw+ppt-skill-<x>@<version> → 真实树在 <dir>/node_modules/@capyclaw/<x>
|
|
111
|
+
const base = e.name.slice('@capyclaw+'.length).split('@')[0]
|
|
112
|
+
const real = path.join(root, e.name, 'node_modules', '@capyclaw', base)
|
|
113
|
+
if (fs.existsSync(path.join(real, 'package.json'))) add(real)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return dirs
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async function main() {
|
|
121
|
+
const port = Number(arg('--port', process.env.PORT || DEFAULT_PORT)) || DEFAULT_PORT
|
|
122
|
+
const providersArg = arg('--providers', '')
|
|
123
|
+
const dirs = providersArg
|
|
124
|
+
? providersArg.split(path.delimiter === ';' ? ';' : ',').map((s) => s.trim()).filter(Boolean)
|
|
125
|
+
: defaultProviderDirs()
|
|
126
|
+
|
|
127
|
+
// 复用优先:端口上已是本服务(healthz 自报 dsh-skills)→ 直接退出 0,
|
|
128
|
+
// 让中间件按「已有健康实例」收编,不双开。
|
|
129
|
+
try {
|
|
130
|
+
const probe = await loopback('GET', `http://127.0.0.1:${port}/healthz`)
|
|
131
|
+
if (probe.status === 200 && probe.text.includes('"dsh-skills"')) {
|
|
132
|
+
console.log(`[dsh-skills] 端口 ${port} 已有本服务实例,复用退出`)
|
|
133
|
+
process.exit(0)
|
|
134
|
+
}
|
|
135
|
+
} catch { /* 没有实例,正常监听 */ }
|
|
136
|
+
|
|
137
|
+
const providers = await loadProviders(dirs)
|
|
138
|
+
const byName = new Map()
|
|
139
|
+
for (const { provider, skills } of providers) {
|
|
140
|
+
for (const skill of skills) byName.set(skill.name, { provider, skill })
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const server = http.createServer(async (req, res) => {
|
|
144
|
+
const send = (code, obj) => {
|
|
145
|
+
const body = JSON.stringify(obj)
|
|
146
|
+
res.writeHead(code, { 'content-type': 'application/json; charset=utf-8', 'content-length': Buffer.byteLength(body) })
|
|
147
|
+
res.end(body)
|
|
148
|
+
}
|
|
149
|
+
try {
|
|
150
|
+
const url = new URL(req.url, 'http://127.0.0.1')
|
|
151
|
+
if (url.pathname === '/healthz') {
|
|
152
|
+
return send(200, {
|
|
153
|
+
ok: true,
|
|
154
|
+
service: 'dsh-skills',
|
|
155
|
+
pid: process.pid,
|
|
156
|
+
port: server.address()?.port || port,
|
|
157
|
+
providers: providers.length,
|
|
158
|
+
skills: byName.size,
|
|
159
|
+
uptimeSec: Math.round(process.uptime()),
|
|
160
|
+
})
|
|
161
|
+
}
|
|
162
|
+
if (url.pathname === '/skills' && req.method === 'GET') {
|
|
163
|
+
return send(200, { ok: true, skills: [...byName.values()].map(({ skill }) => skill) })
|
|
164
|
+
}
|
|
165
|
+
const hit = /^\/skills\/([^/]+)$/.exec(url.pathname)
|
|
166
|
+
if (hit && req.method === 'GET') {
|
|
167
|
+
const entry = byName.get(decodeURIComponent(hit[1]))
|
|
168
|
+
if (!entry) return send(404, { ok: false, error: `未知 skill ${decodeURIComponent(hit[1])}` })
|
|
169
|
+
return send(200, await entry.provider.get({ name: entry.skill.name }))
|
|
170
|
+
}
|
|
171
|
+
send(404, { ok: false, error: `未知端点 ${req.method} ${url.pathname}` })
|
|
172
|
+
} catch (err) {
|
|
173
|
+
send(500, { ok: false, error: err.message })
|
|
174
|
+
}
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
await new Promise((resolve, reject) => {
|
|
178
|
+
server.once('error', reject)
|
|
179
|
+
server.listen(port, '127.0.0.1', resolve)
|
|
180
|
+
})
|
|
181
|
+
const actual = server.address().port
|
|
182
|
+
console.log(`[dsh-skills] 就绪 http://127.0.0.1:${actual}/skills(${providers.length} 个 provider,${byName.size} 个技能)`)
|
|
183
|
+
|
|
184
|
+
let stopping = false
|
|
185
|
+
const shutdown = async () => {
|
|
186
|
+
if (stopping) return
|
|
187
|
+
stopping = true
|
|
188
|
+
console.log('[dsh-skills] 停机中…')
|
|
189
|
+
await new Promise((r) => {
|
|
190
|
+
try { server.closeAll(); server.close(() => r()) } catch { r() }
|
|
191
|
+
})
|
|
192
|
+
process.exit(0)
|
|
193
|
+
}
|
|
194
|
+
process.on('SIGINT', shutdown)
|
|
195
|
+
process.on('SIGTERM', shutdown)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
main().catch((err) => {
|
|
199
|
+
console.error('[dsh-skills] 启动失败:', err.message)
|
|
200
|
+
process.exit(1)
|
|
201
|
+
})
|