@capyclaw/plugin-generator 0.0.0-stage → 1.0.0
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/bin/cli.mjs +78 -0
- package/bin/create-plugin.mjs +451 -0
- package/bin/pack-plugin.mjs +86 -0
- package/lib/index.mjs +250 -0
- package/package.json +19 -4
- package/templates/plugin/contract/contract.json.tmpl +14 -0
- package/templates/plugin/editor/app.js.tmpl +44 -0
- package/templates/plugin/editor/index.html.tmpl +38 -0
- package/templates/plugin/lib/host.mjs.tmpl +27 -0
- package/templates/plugin/server/impl.mjs.tmpl +14 -0
- package/templates/plugin/server/relay.mjs.tmpl +82 -0
- package/templates/plugin/server/tools.mjs.tmpl +62 -0
- package/README.md +0 -3
package/bin/cli.mjs
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* DSH 插件生成器 CLI
|
|
4
|
+
* 用法: create-plugin <plugin-name> [options]
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { fileURLToPath } from 'node:url'
|
|
8
|
+
import { dirname } from 'node:path'
|
|
9
|
+
import { generatePlugin } from '../lib/index.mjs'
|
|
10
|
+
|
|
11
|
+
const __filename = fileURLToPath(import.meta.url)
|
|
12
|
+
const __dirname = dirname(__filename)
|
|
13
|
+
|
|
14
|
+
const args = process.argv.slice(2)
|
|
15
|
+
const pluginName = args[0]
|
|
16
|
+
|
|
17
|
+
if (!pluginName || pluginName.startsWith('-')) {
|
|
18
|
+
console.log('用法: create-plugin <plugin-name> [options]')
|
|
19
|
+
console.log('')
|
|
20
|
+
console.log('示例:')
|
|
21
|
+
console.log(' create-plugin my-plugin --tier 0 # 纯客户端插件')
|
|
22
|
+
console.log(' create-plugin my-service --tier 1 # 标准服务插件')
|
|
23
|
+
console.log(' create-plugin my-suite --tier suite # 插件套件')
|
|
24
|
+
console.log('')
|
|
25
|
+
console.log('选项:')
|
|
26
|
+
console.log(' --tier <0|1|2|suite> 插件层级 (默认: 0)')
|
|
27
|
+
console.log(' --category <ui|service|bridge|tool|suite> 插件分类 (默认: ui)')
|
|
28
|
+
console.log(' --platform <web|desktop|both> 支持平台 (默认: web)')
|
|
29
|
+
console.log(' --output <path> 输出目录 (默认: extensions/official/)')
|
|
30
|
+
process.exit(1)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// 解析选项
|
|
34
|
+
const options = {
|
|
35
|
+
tier: 0,
|
|
36
|
+
category: 'ui',
|
|
37
|
+
platform: 'web',
|
|
38
|
+
output: null
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
for (let i = 1; i < args.length; i++) {
|
|
42
|
+
const arg = args[i]
|
|
43
|
+
if (arg === '--tier' && args[i + 1]) {
|
|
44
|
+
options.tier = args[i + 1] === 'suite' ? 'suite' : parseInt(args[i + 1], 10)
|
|
45
|
+
i++
|
|
46
|
+
} else if (arg === '--category' && args[i + 1]) {
|
|
47
|
+
options.category = args[i + 1]
|
|
48
|
+
i++
|
|
49
|
+
} else if (arg === '--platform' && args[i + 1]) {
|
|
50
|
+
options.platform = args[i + 1]
|
|
51
|
+
i++
|
|
52
|
+
} else if (arg === '--output' && args[i + 1]) {
|
|
53
|
+
options.output = args[i + 1]
|
|
54
|
+
i++
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
console.log('🚀 创建 DSH 插件:', pluginName)
|
|
59
|
+
console.log('━'.repeat(60))
|
|
60
|
+
console.log('配置:')
|
|
61
|
+
console.log(' 层级:', options.tier)
|
|
62
|
+
console.log(' 分类:', options.category)
|
|
63
|
+
console.log(' 平台:', options.platform)
|
|
64
|
+
console.log('')
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
const result = await generatePlugin(pluginName, options)
|
|
68
|
+
console.log('\n✅ 插件创建成功!')
|
|
69
|
+
console.log('\n插件位置:', result.path)
|
|
70
|
+
console.log('\n下一步:')
|
|
71
|
+
console.log(` cd ${result.path}`)
|
|
72
|
+
console.log(' npm install')
|
|
73
|
+
console.log(` npm run validate-plugin -- ${result.path}`)
|
|
74
|
+
console.log('')
|
|
75
|
+
} catch (error) {
|
|
76
|
+
console.error('\n❌ 创建失败:', error.message)
|
|
77
|
+
process.exit(1)
|
|
78
|
+
}
|
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* DSH 插件脚手架(标准版)。
|
|
4
|
+
*
|
|
5
|
+
* 默认生成一个可安装的完整单元:
|
|
6
|
+
* <out>/<name>/plugin/<name> 运行时插件
|
|
7
|
+
* <out>/<name>/plugin/<name>-suite suite 包
|
|
8
|
+
* <out>/<name>/domains/<domain>/dsh-domain.json
|
|
9
|
+
*
|
|
10
|
+
* 只生成单个插件包:--plugin-only
|
|
11
|
+
*/
|
|
12
|
+
import fs from 'node:fs'
|
|
13
|
+
import path from 'node:path'
|
|
14
|
+
import readline from 'node:readline'
|
|
15
|
+
import { fileURLToPath } from 'node:url'
|
|
16
|
+
import { renderCordisPatch } from '@capyclaw/suite-kit/compose'
|
|
17
|
+
import { syncSharedDir } from '../../../scripts/lib/shared-sync.mjs'
|
|
18
|
+
|
|
19
|
+
const HERE = path.dirname(fileURLToPath(import.meta.url))
|
|
20
|
+
const TEMPLATES = path.join(HERE, '..', 'templates')
|
|
21
|
+
const VERSION = '1.0.0'
|
|
22
|
+
const DEFAULT_PORT = 39500
|
|
23
|
+
|
|
24
|
+
function printHelp() {
|
|
25
|
+
console.log(`
|
|
26
|
+
DSH 插件脚手架
|
|
27
|
+
|
|
28
|
+
用法:
|
|
29
|
+
node create-plugin.mjs
|
|
30
|
+
node create-plugin.mjs --name dsh-my-service --type service --port 39500 --out ./my-plugins --yes
|
|
31
|
+
|
|
32
|
+
参数:
|
|
33
|
+
--name <kebab-case> 插件包名(必填)
|
|
34
|
+
--type <service|ui|minimal> 插件类型(默认 service)
|
|
35
|
+
--title <text> 显示标题(默认取 name)
|
|
36
|
+
--description <text> 描述
|
|
37
|
+
--port <1-65535> 默认端口(默认 ${DEFAULT_PORT})
|
|
38
|
+
--out <dir> 输出父目录(默认 ./my-plugins)
|
|
39
|
+
--domain <name> domain 名称(默认取去掉 dsh- 前缀的名字)
|
|
40
|
+
--plugin-only 只生成插件包,不生成 suite/domain
|
|
41
|
+
--yes, -y 非交互模式;缺失参数用默认值
|
|
42
|
+
--help, -h 显示帮助
|
|
43
|
+
|
|
44
|
+
生成结构(默认):
|
|
45
|
+
<out>/<name>/plugin/<name>/
|
|
46
|
+
<out>/<name>/plugin/<name>-suite/
|
|
47
|
+
<out>/<name>/domains/<domain>/dsh-domain.json
|
|
48
|
+
`)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function parseArgs(argv) {
|
|
52
|
+
const options = {}
|
|
53
|
+
for (let i = 0; i < argv.length; i++) {
|
|
54
|
+
const arg = argv[i]
|
|
55
|
+
const next = () => {
|
|
56
|
+
const value = argv[++i]
|
|
57
|
+
if (value === undefined) throw new Error(`参数缺少值: ${arg}`)
|
|
58
|
+
return value
|
|
59
|
+
}
|
|
60
|
+
if (arg === '--help' || arg === '-h') options.help = true
|
|
61
|
+
else if (arg === '--yes' || arg === '-y') options.yes = true
|
|
62
|
+
else if (arg === '--plugin-only') options.pluginOnly = true
|
|
63
|
+
else if (arg === '--name') options.name = next()
|
|
64
|
+
else if (arg.startsWith('--name=')) options.name = arg.slice('--name='.length)
|
|
65
|
+
else if (arg === '--type') options.type = next()
|
|
66
|
+
else if (arg.startsWith('--type=')) options.type = arg.slice('--type='.length)
|
|
67
|
+
else if (arg === '--title') options.title = next()
|
|
68
|
+
else if (arg.startsWith('--title=')) options.title = arg.slice('--title='.length)
|
|
69
|
+
else if (arg === '--description') options.description = next()
|
|
70
|
+
else if (arg.startsWith('--description=')) options.description = arg.slice('--description='.length)
|
|
71
|
+
else if (arg === '--port') options.port = Number(next())
|
|
72
|
+
else if (arg.startsWith('--port=')) options.port = Number(arg.slice('--port='.length))
|
|
73
|
+
else if (arg === '--out') options.out = next()
|
|
74
|
+
else if (arg.startsWith('--out=')) options.out = arg.slice('--out='.length)
|
|
75
|
+
else if (arg === '--domain') options.domain = next()
|
|
76
|
+
else if (arg.startsWith('--domain=')) options.domain = arg.slice('--domain='.length)
|
|
77
|
+
else throw new Error(`未知参数: ${arg}`)
|
|
78
|
+
}
|
|
79
|
+
return options
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function ask(question) {
|
|
83
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout })
|
|
84
|
+
return new Promise((resolve) => rl.question(question, (answer) => {
|
|
85
|
+
rl.close()
|
|
86
|
+
resolve(answer.trim())
|
|
87
|
+
}))
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function slugOf(name) {
|
|
91
|
+
return name.replace(/^dsh-/, '')
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function render(text, tokens) {
|
|
95
|
+
return text.replace(/\{\{([A-Z0-9_]+)\}\}/g, (_, key) => tokens[key] ?? '')
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function writeTemplate(templateRel, destFile, tokens) {
|
|
99
|
+
const source = fs.readFileSync(path.join(TEMPLATES, templateRel), 'utf8')
|
|
100
|
+
fs.mkdirSync(path.dirname(destFile), { recursive: true })
|
|
101
|
+
fs.writeFileSync(destFile, render(source, tokens))
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function writeJson(file, value) {
|
|
105
|
+
fs.mkdirSync(path.dirname(file), { recursive: true })
|
|
106
|
+
fs.writeFileSync(file, JSON.stringify(value, null, 2) + '\n')
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function makePackageJson({ name, title, description, type, version }) {
|
|
110
|
+
const files = ['package.json', 'lib/', 'cordis.patch.yml', 'README.md']
|
|
111
|
+
if (type === 'service' || type === 'ui') files.push('server/', 'contract/')
|
|
112
|
+
if (type === 'ui') files.push('editor/')
|
|
113
|
+
return {
|
|
114
|
+
name,
|
|
115
|
+
version,
|
|
116
|
+
private: true,
|
|
117
|
+
description,
|
|
118
|
+
type: 'module',
|
|
119
|
+
main: './lib/host.mjs',
|
|
120
|
+
exports: {
|
|
121
|
+
'.': './lib/host.mjs',
|
|
122
|
+
'./package.json': './package.json',
|
|
123
|
+
},
|
|
124
|
+
dsh: {
|
|
125
|
+
bundle: { patch: './cordis.patch.yml' },
|
|
126
|
+
category: type === 'ui' ? 'ui' : 'service',
|
|
127
|
+
platform: 'web',
|
|
128
|
+
},
|
|
129
|
+
dependencies: {
|
|
130
|
+
'@capyclaw/plugin-kit': '^0.2.0',
|
|
131
|
+
},
|
|
132
|
+
engines: {
|
|
133
|
+
dsh: '^0.2.0-rc.2',
|
|
134
|
+
},
|
|
135
|
+
meta: { title, description },
|
|
136
|
+
files,
|
|
137
|
+
license: 'MIT',
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function makeCordisPatch({ name, hostId, serverName, port, type }) {
|
|
142
|
+
const host = ` - id: ${hostId}
|
|
143
|
+
name: ${JSON.stringify(name)}
|
|
144
|
+
config:
|
|
145
|
+
port: ${port}`
|
|
146
|
+
if (type === 'minimal') {
|
|
147
|
+
return `# 由脚手架生成;只注册宿主插件,不提供 MCP 服务。
|
|
148
|
+
|
|
149
|
+
- insert:
|
|
150
|
+
${host}
|
|
151
|
+
`
|
|
152
|
+
}
|
|
153
|
+
return `# 由脚手架生成;端口或 MCP 配置变化时同步修改这里和 dsh-suite.json。
|
|
154
|
+
|
|
155
|
+
- insert:
|
|
156
|
+
${host}
|
|
157
|
+
- id: mcp-${serverName}
|
|
158
|
+
name: '@deepseek-ai/dsh-mcp-client'
|
|
159
|
+
config:
|
|
160
|
+
serverName: ${serverName}
|
|
161
|
+
transport: streamable-http
|
|
162
|
+
url: 'http://127.0.0.1:${port}/mcp'
|
|
163
|
+
failOnStartupError: false
|
|
164
|
+
toolCallTimeoutMs: 120000
|
|
165
|
+
`
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function makeSuiteManifest({ suiteName, name, title, description, hostId, serverName, toolPrefix, port }) {
|
|
169
|
+
return {
|
|
170
|
+
name: suiteName,
|
|
171
|
+
version: VERSION,
|
|
172
|
+
title: `${title} 套件`,
|
|
173
|
+
description: description || `${title} 运行时与 MCP 工具`,
|
|
174
|
+
packages: {
|
|
175
|
+
[name]: {
|
|
176
|
+
role: 'runtime',
|
|
177
|
+
version: `^${VERSION}`,
|
|
178
|
+
path: `../${name}`,
|
|
179
|
+
provides: {
|
|
180
|
+
tools: [`${toolPrefix}_*`],
|
|
181
|
+
services: [`${serverName}-service`],
|
|
182
|
+
ports: [port],
|
|
183
|
+
capabilities: {
|
|
184
|
+
[`${serverName}.runtime`]: VERSION,
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
rows: [
|
|
190
|
+
{ id: hostId, name, config: { port } },
|
|
191
|
+
{
|
|
192
|
+
id: `mcp-${serverName}`,
|
|
193
|
+
name: '@deepseek-ai/dsh-mcp-client',
|
|
194
|
+
config: {
|
|
195
|
+
serverName,
|
|
196
|
+
transport: 'streamable-http',
|
|
197
|
+
url: `http://127.0.0.1:${port}/mcp`,
|
|
198
|
+
failOnStartupError: false,
|
|
199
|
+
toolCallTimeoutMs: 120000,
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
],
|
|
203
|
+
checks: [
|
|
204
|
+
{
|
|
205
|
+
id: `${serverName}-health`,
|
|
206
|
+
type: 'http',
|
|
207
|
+
url: `http://127.0.0.1:${port}/healthz`,
|
|
208
|
+
expectStatus: 200,
|
|
209
|
+
expectBody: { ok: true },
|
|
210
|
+
timeoutMs: 3000,
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
id: `${serverName}-contract`,
|
|
214
|
+
type: 'contract',
|
|
215
|
+
url: `http://127.0.0.1:${port}/mcp`,
|
|
216
|
+
tool: `${toolPrefix}_capabilities`,
|
|
217
|
+
major: 1,
|
|
218
|
+
timeoutMs: 5000,
|
|
219
|
+
},
|
|
220
|
+
],
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
async function main() {
|
|
225
|
+
let options
|
|
226
|
+
try {
|
|
227
|
+
options = parseArgs(process.argv.slice(2))
|
|
228
|
+
} catch (error) {
|
|
229
|
+
console.error(`✗ ${error.message}\n`)
|
|
230
|
+
printHelp()
|
|
231
|
+
process.exit(1)
|
|
232
|
+
}
|
|
233
|
+
if (options.help) {
|
|
234
|
+
printHelp()
|
|
235
|
+
return
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (!options.name) {
|
|
239
|
+
if (options.yes) throw new Error('--yes 模式必须提供 --name')
|
|
240
|
+
options.name = await ask('插件包名(kebab-case,例 dsh-my-service): ')
|
|
241
|
+
}
|
|
242
|
+
if (!options.type) {
|
|
243
|
+
options.type = options.yes ? 'service' : (await ask('类型 service/ui/minimal(默认 service): ')) || 'service'
|
|
244
|
+
}
|
|
245
|
+
if (!options.title) {
|
|
246
|
+
options.title = options.yes ? options.name : (await ask('显示标题(默认取插件名): ')) || options.name
|
|
247
|
+
}
|
|
248
|
+
if (!options.description) {
|
|
249
|
+
options.description = options.yes ? `${options.title} DSH 插件` : (await ask('描述(可留空): '))
|
|
250
|
+
}
|
|
251
|
+
if (!options.port) {
|
|
252
|
+
options.port = options.yes ? DEFAULT_PORT : Number(await ask(`默认端口(${DEFAULT_PORT}): `) || DEFAULT_PORT)
|
|
253
|
+
}
|
|
254
|
+
if (!options.out) options.out = options.yes ? path.join(HERE, 'my-plugins') : (await ask('输出父目录(默认 ./my-plugins): ')) || path.join(HERE, 'my-plugins')
|
|
255
|
+
|
|
256
|
+
const name = options.name
|
|
257
|
+
const type = options.type
|
|
258
|
+
if (!/^[a-z0-9][a-z0-9-]*$/.test(name)) throw new Error('--name 必须是 kebab-case(小写字母/数字/连字符)')
|
|
259
|
+
if (!['service', 'ui', 'minimal'].includes(type)) throw new Error('--type 必须是 service/ui/minimal')
|
|
260
|
+
if (!Number.isInteger(options.port) || options.port < 1 || options.port > 65535) throw new Error('--port 必须是 1-65535 的整数')
|
|
261
|
+
|
|
262
|
+
const slug = slugOf(name)
|
|
263
|
+
const serverName = slug.replace(/[^a-zA-Z0-9_-]/g, '-')
|
|
264
|
+
const toolPrefix = serverName.replace(/-/g, '_')
|
|
265
|
+
const hostId = `${name}-host`
|
|
266
|
+
const suiteName = `${name}-suite`
|
|
267
|
+
const domainName = options.domain || slug
|
|
268
|
+
if (!/^[a-z0-9][a-z0-9-]*$/.test(domainName)) throw new Error('--domain 必须是 kebab-case')
|
|
269
|
+
|
|
270
|
+
const rootDir = path.join(path.resolve(options.out), name)
|
|
271
|
+
if (fs.existsSync(rootDir)) throw new Error(`目标已存在: ${rootDir}`)
|
|
272
|
+
|
|
273
|
+
const withSuite = !options.pluginOnly && type !== 'minimal'
|
|
274
|
+
if (type === 'minimal' && !options.pluginOnly) {
|
|
275
|
+
console.log('· minimal 类型不生成 suite/domain,按 plugin-only 处理')
|
|
276
|
+
}
|
|
277
|
+
const pluginDir = withSuite ? path.join(rootDir, 'plugin', name) : rootDir
|
|
278
|
+
const suiteDir = path.join(rootDir, 'plugin', suiteName)
|
|
279
|
+
const domainDir = path.join(rootDir, 'domains', domainName)
|
|
280
|
+
|
|
281
|
+
const tokens = {
|
|
282
|
+
NAME: name,
|
|
283
|
+
SLUG: slug,
|
|
284
|
+
SLUG_U: slug.replace(/-/g, '_'),
|
|
285
|
+
TITLE: options.title,
|
|
286
|
+
DESCRIPTION: options.description,
|
|
287
|
+
VERSION,
|
|
288
|
+
PORT: String(options.port),
|
|
289
|
+
HOST_ID: hostId,
|
|
290
|
+
SERVER_NAME: serverName,
|
|
291
|
+
TOOL_PREFIX: toolPrefix,
|
|
292
|
+
ENV_PREFIX: `DSH_${slug.toUpperCase().replace(/-/g, '_')}`,
|
|
293
|
+
SUITE_NAME: suiteName,
|
|
294
|
+
DOMAIN_NAME: domainName,
|
|
295
|
+
DOMAIN_PROFILE: `domain-${domainName}`,
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
fs.mkdirSync(pluginDir, { recursive: true })
|
|
299
|
+
writeJson(path.join(pluginDir, 'package.json'), makePackageJson({
|
|
300
|
+
name,
|
|
301
|
+
title: options.title,
|
|
302
|
+
description: options.description,
|
|
303
|
+
type,
|
|
304
|
+
version: VERSION,
|
|
305
|
+
}))
|
|
306
|
+
fs.writeFileSync(path.join(pluginDir, 'cordis.patch.yml'), makeCordisPatch({ name, hostId, serverName, port: options.port, type }))
|
|
307
|
+
|
|
308
|
+
if (type === 'minimal') {
|
|
309
|
+
fs.mkdirSync(path.join(pluginDir, 'lib'), { recursive: true })
|
|
310
|
+
fs.writeFileSync(path.join(pluginDir, 'lib', 'host.mjs'), render(`export const name = '{{HOST_ID}}'
|
|
311
|
+
|
|
312
|
+
export function apply() {
|
|
313
|
+
console.log('[{{NAME}}] 已加载')
|
|
314
|
+
}
|
|
315
|
+
`, tokens))
|
|
316
|
+
} else {
|
|
317
|
+
writeTemplate('plugin/lib/host.mjs.tmpl', path.join(pluginDir, 'lib', 'host.mjs'), tokens)
|
|
318
|
+
writeTemplate('plugin/server/relay.mjs.tmpl', path.join(pluginDir, 'server', 'relay.mjs'), tokens)
|
|
319
|
+
writeTemplate('plugin/server/tools.mjs.tmpl', path.join(pluginDir, 'server', 'tools.mjs'), tokens)
|
|
320
|
+
writeTemplate('plugin/server/impl.mjs.tmpl', path.join(pluginDir, 'server', 'impl.mjs'), tokens)
|
|
321
|
+
writeTemplate('plugin/contract/contract.json.tmpl', path.join(pluginDir, 'contract', `${slug}-contract.json`), tokens)
|
|
322
|
+
if (type === 'ui') {
|
|
323
|
+
writeTemplate('plugin/editor/index.html.tmpl', path.join(pluginDir, 'editor', 'index.html'), tokens)
|
|
324
|
+
writeTemplate('plugin/editor/app.js.tmpl', path.join(pluginDir, 'editor', 'app.js'), tokens)
|
|
325
|
+
}
|
|
326
|
+
syncSharedDir(path.join(pluginDir, 'server', 'shared'), { force: true })
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
fs.writeFileSync(path.join(pluginDir, 'README.md'), `# ${options.title}
|
|
330
|
+
|
|
331
|
+
${options.description}
|
|
332
|
+
|
|
333
|
+
## 开发
|
|
334
|
+
|
|
335
|
+
\`\`\`bash
|
|
336
|
+
npm run validate-plugin -- ${pluginDir.replace(/\\/g, '/')}
|
|
337
|
+
\`\`\`
|
|
338
|
+
|
|
339
|
+
## 安装到 DSH
|
|
340
|
+
|
|
341
|
+
\`\`\`bash
|
|
342
|
+
dsh plugin --profile web add ${pluginDir.replace(/\\/g, '/')}
|
|
343
|
+
\`\`\`
|
|
344
|
+
|
|
345
|
+
${type === 'ui' ? `启动后浏览器打开:http://127.0.0.1:${options.port}/\n` : ''}`)
|
|
346
|
+
|
|
347
|
+
if (withSuite) {
|
|
348
|
+
const suite = makeSuiteManifest({
|
|
349
|
+
suiteName,
|
|
350
|
+
name,
|
|
351
|
+
title: options.title,
|
|
352
|
+
description: options.description,
|
|
353
|
+
hostId,
|
|
354
|
+
serverName,
|
|
355
|
+
toolPrefix,
|
|
356
|
+
port: options.port,
|
|
357
|
+
})
|
|
358
|
+
writeJson(path.join(suiteDir, 'package.json'), {
|
|
359
|
+
name: suiteName,
|
|
360
|
+
version: VERSION,
|
|
361
|
+
private: true,
|
|
362
|
+
description: `${options.title} 套件`,
|
|
363
|
+
type: 'module',
|
|
364
|
+
main: './lib/index.mjs',
|
|
365
|
+
exports: {
|
|
366
|
+
'.': './lib/index.mjs',
|
|
367
|
+
'./package.json': './package.json',
|
|
368
|
+
},
|
|
369
|
+
dependencies: { [name]: `^${VERSION}` },
|
|
370
|
+
dsh: {
|
|
371
|
+
bundle: { patch: './cordis.patch.yml' },
|
|
372
|
+
suite: './dsh-suite.json',
|
|
373
|
+
},
|
|
374
|
+
engines: {
|
|
375
|
+
dsh: '^0.2.0-rc.2',
|
|
376
|
+
},
|
|
377
|
+
files: ['lib/', 'dsh-suite.json', 'cordis.patch.yml', 'README.md', 'package.json'],
|
|
378
|
+
license: 'MIT',
|
|
379
|
+
})
|
|
380
|
+
writeJson(path.join(suiteDir, 'dsh-suite.json'), suite)
|
|
381
|
+
fs.mkdirSync(path.join(suiteDir, 'lib'), { recursive: true })
|
|
382
|
+
fs.writeFileSync(path.join(suiteDir, 'cordis.patch.yml'), renderCordisPatch(suite.rows, { header: true }))
|
|
383
|
+
fs.writeFileSync(path.join(suiteDir, 'lib', 'index.mjs'), `export const name = '${suiteName}'\n\nexport function apply() {}\n`)
|
|
384
|
+
fs.writeFileSync(path.join(suiteDir, 'README.md'), `# ${suiteName}\n\n安装:\n\n\`\`\`bash\nnode packages/capyclaw-suite-kit/bin/dsh-suite.mjs install ${suiteDir.replace(/\\/g, '/')} --profile web\n\`\`\`\n`)
|
|
385
|
+
|
|
386
|
+
writeJson(path.join(domainDir, 'dsh-domain.json'), {
|
|
387
|
+
name: domainName,
|
|
388
|
+
version: VERSION,
|
|
389
|
+
title: `${options.title} 领域包`,
|
|
390
|
+
description: `只加载 ${options.title} 所需的运行时和工具。`,
|
|
391
|
+
baseBundles: ['@deepseek-ai/dsh-base', '@deepseek-ai/dsh-web-app'],
|
|
392
|
+
components: [`../../plugin/${suiteName}`],
|
|
393
|
+
profile: { name: tokens.DOMAIN_PROFILE, port: 3080 },
|
|
394
|
+
runtime: {
|
|
395
|
+
driver: 'node',
|
|
396
|
+
workId: 'main',
|
|
397
|
+
bindings: [
|
|
398
|
+
{
|
|
399
|
+
row: suite.rows[0].id,
|
|
400
|
+
config: { port: '${ports.content}' },
|
|
401
|
+
},
|
|
402
|
+
],
|
|
403
|
+
model: {
|
|
404
|
+
id: 'llm-deepseek',
|
|
405
|
+
plugin: '@deepseek-ai/dsh-llm-deepseek-api-key',
|
|
406
|
+
apiKeyEnv: 'DEEPSEEK_API_KEY',
|
|
407
|
+
baseUrlEnv: 'DEEPSEEK_BASE_URL',
|
|
408
|
+
baseURL: 'https://api.deepseek.com/anthropic',
|
|
409
|
+
},
|
|
410
|
+
requires: {
|
|
411
|
+
tools: [`${toolPrefix}_*`],
|
|
412
|
+
services: [`${serverName}-service`],
|
|
413
|
+
capabilities: [`${serverName}.runtime`],
|
|
414
|
+
},
|
|
415
|
+
scope: {
|
|
416
|
+
sessions: { path: '.dsh/sessions', isolated: true },
|
|
417
|
+
files: [
|
|
418
|
+
{ id: 'workspace', kind: 'workspace', path: 'workspace', patterns: ['**'] },
|
|
419
|
+
{ id: 'plugin-content', kind: 'content', path: 'content/plugin', patterns: ['**'] },
|
|
420
|
+
],
|
|
421
|
+
workspace: { workId: 'main', files: ['**'], allowNew: true },
|
|
422
|
+
network: { bind: ['127.0.0.1'] },
|
|
423
|
+
processes: { killTree: true },
|
|
424
|
+
},
|
|
425
|
+
ports: {
|
|
426
|
+
harness: { preferred: 3080, range: 100 },
|
|
427
|
+
content: { preferred: options.port || 39311, range: 100 },
|
|
428
|
+
},
|
|
429
|
+
},
|
|
430
|
+
removeRows: suite.rows.map((row) => row.id),
|
|
431
|
+
})
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
console.log(`\n✓ 已生成: ${rootDir}`)
|
|
435
|
+
console.log(` plugin: ${pluginDir}`)
|
|
436
|
+
if (withSuite) {
|
|
437
|
+
console.log(` suite : ${suiteDir}`)
|
|
438
|
+
console.log(` domain: ${domainDir}`)
|
|
439
|
+
}
|
|
440
|
+
console.log('\n下一步:')
|
|
441
|
+
console.log(` npm run validate-plugin -- ${pluginDir.replace(/\\/g, '/')}`)
|
|
442
|
+
if (withSuite) {
|
|
443
|
+
console.log(` node packages/capyclaw-suite-kit/bin/dsh-suite.mjs validate ${suiteDir}`)
|
|
444
|
+
console.log(` node packages/capyclaw-suite-kit/bin/dsh-domain.mjs validate ${domainDir}`)
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
main().catch((error) => {
|
|
449
|
+
console.error(`\n✗ ${error.message}`)
|
|
450
|
+
process.exit(1)
|
|
451
|
+
})
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* DSH 插件独立打包工具。
|
|
4
|
+
*
|
|
5
|
+
* 用法:
|
|
6
|
+
* node pack-plugin.mjs <插件目录> [--out <目录>]
|
|
7
|
+
*
|
|
8
|
+
* 输出:
|
|
9
|
+
* <out>/<name>-v<version>.zip
|
|
10
|
+
*/
|
|
11
|
+
import fs from 'node:fs'
|
|
12
|
+
import path from 'node:path'
|
|
13
|
+
import { fileURLToPath } from 'node:url'
|
|
14
|
+
import { packPlugin } from '@capyclaw/plugin-kit/packaging/pack'
|
|
15
|
+
|
|
16
|
+
const HERE = path.dirname(fileURLToPath(import.meta.url))
|
|
17
|
+
|
|
18
|
+
function usage() {
|
|
19
|
+
console.log(`
|
|
20
|
+
DSH 插件打包工具
|
|
21
|
+
|
|
22
|
+
用法:
|
|
23
|
+
node pack-plugin.mjs <插件目录> [--out <目录>]
|
|
24
|
+
|
|
25
|
+
说明:
|
|
26
|
+
- 始终包含 package.json、README、LICENSE、THIRD_PARTY_NOTICES(存在时)
|
|
27
|
+
- 支持 package.json files 中的目录、文件和 ! 排除项
|
|
28
|
+
- 打包后自动自检,再写跨平台 ZIP
|
|
29
|
+
`)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function parseArgs(argv) {
|
|
33
|
+
const options = { src: '', out: path.join(HERE, 'dist') }
|
|
34
|
+
for (let i = 0; i < argv.length; i++) {
|
|
35
|
+
const arg = argv[i]
|
|
36
|
+
if (arg === '--help' || arg === '-h') options.help = true
|
|
37
|
+
else if (arg === '--out' || arg === '-o') options.out = argv[++i]
|
|
38
|
+
else if (arg.startsWith('--out=')) options.out = arg.slice('--out='.length)
|
|
39
|
+
else if (!options.src) options.src = arg
|
|
40
|
+
else throw new Error(`未知参数: ${arg}`)
|
|
41
|
+
}
|
|
42
|
+
return options
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function main() {
|
|
46
|
+
let options
|
|
47
|
+
try {
|
|
48
|
+
options = parseArgs(process.argv.slice(2))
|
|
49
|
+
} catch (error) {
|
|
50
|
+
console.error(`✗ ${error.message}`)
|
|
51
|
+
usage()
|
|
52
|
+
process.exit(1)
|
|
53
|
+
}
|
|
54
|
+
if (options.help || !options.src) {
|
|
55
|
+
usage()
|
|
56
|
+
process.exit(options.help ? 0 : 1)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const srcDir = path.resolve(options.src)
|
|
60
|
+
if (!fs.existsSync(path.join(srcDir, 'package.json'))) {
|
|
61
|
+
console.error(`✗ 找不到插件 package.json: ${srcDir}`)
|
|
62
|
+
process.exit(1)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
const result = packPlugin({
|
|
67
|
+
srcDir,
|
|
68
|
+
outDir: path.resolve(options.out),
|
|
69
|
+
})
|
|
70
|
+
console.log(`✓ 已打包: ${result.outFile}`)
|
|
71
|
+
console.log(` 文件数: ${result.entries.length}`)
|
|
72
|
+
for (const warning of result.warnings || []) console.warn(` ⚠ ${warning}`)
|
|
73
|
+
console.log(' 安装:')
|
|
74
|
+
console.log(' 1) 解压到任意目录')
|
|
75
|
+
if (result.pkg.dsh?.suite) {
|
|
76
|
+
console.log(' 2) node packages/capyclaw-suite-kit/bin/dsh-suite.mjs install <解压后的目录> --profile web')
|
|
77
|
+
} else {
|
|
78
|
+
console.log(' 2) dsh plugin --profile web add <解压后的目录>')
|
|
79
|
+
}
|
|
80
|
+
} catch (error) {
|
|
81
|
+
console.error(`✗ ${error.message}`)
|
|
82
|
+
process.exit(1)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
main()
|
package/lib/index.mjs
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DSH 插件生成器核心
|
|
3
|
+
* 自动生成符合规范的插件骨架
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { mkdirSync, writeFileSync, existsSync } from 'node:fs'
|
|
7
|
+
import { join, resolve } from 'node:path'
|
|
8
|
+
import { fileURLToPath } from 'node:url'
|
|
9
|
+
import { dirname } from 'node:path'
|
|
10
|
+
|
|
11
|
+
const __filename = fileURLToPath(import.meta.url)
|
|
12
|
+
const __dirname = dirname(__filename)
|
|
13
|
+
const rootDir = resolve(__dirname, '../../..')
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 生成插件
|
|
17
|
+
*/
|
|
18
|
+
export async function generatePlugin(pluginName, options = {}) {
|
|
19
|
+
const {
|
|
20
|
+
tier = 0,
|
|
21
|
+
category = 'ui',
|
|
22
|
+
platform = 'web',
|
|
23
|
+
output = null
|
|
24
|
+
} = options
|
|
25
|
+
|
|
26
|
+
// 验证插件名
|
|
27
|
+
if (!/^[a-z0-9-]+$/.test(pluginName)) {
|
|
28
|
+
throw new Error('插件名只能包含小写字母、数字和连字符')
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const fullName = `dsh-${pluginName}`
|
|
32
|
+
|
|
33
|
+
// 确定输出目录
|
|
34
|
+
let pluginDir
|
|
35
|
+
if (output) {
|
|
36
|
+
pluginDir = resolve(process.cwd(), output, fullName)
|
|
37
|
+
} else {
|
|
38
|
+
const baseDir = tier === 'suite' ? 'extensions/suites' : 'extensions/official'
|
|
39
|
+
pluginDir = join(rootDir, baseDir, fullName)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 检查是否已存在
|
|
43
|
+
if (existsSync(pluginDir)) {
|
|
44
|
+
throw new Error(`插件目录已存在: ${pluginDir}`)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// 创建目录结构
|
|
48
|
+
mkdirSync(pluginDir, { recursive: true })
|
|
49
|
+
mkdirSync(join(pluginDir, 'lib'), { recursive: true })
|
|
50
|
+
|
|
51
|
+
// 生成 package.json
|
|
52
|
+
const packageJson = generatePackageJson(fullName, tier, category, platform)
|
|
53
|
+
writeFileSync(join(pluginDir, 'package.json'), JSON.stringify(packageJson, null, 2))
|
|
54
|
+
|
|
55
|
+
// 生成主入口
|
|
56
|
+
const mainEntry = generateMainEntry(fullName, tier)
|
|
57
|
+
writeFileSync(join(pluginDir, 'lib/index.mjs'), mainEntry)
|
|
58
|
+
|
|
59
|
+
// 生成 README
|
|
60
|
+
const readme = generateReadme(fullName, category, platform)
|
|
61
|
+
writeFileSync(join(pluginDir, 'README.md'), readme)
|
|
62
|
+
|
|
63
|
+
// 如果需要客户端代码,创建 client/
|
|
64
|
+
if (category === 'ui' || tier >= 1) {
|
|
65
|
+
mkdirSync(join(pluginDir, 'client'), { recursive: true })
|
|
66
|
+
const clientCode = generateClientCode(fullName)
|
|
67
|
+
writeFileSync(join(pluginDir, 'client/client.js'), clientCode)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 生成 cordis.patch.yml
|
|
71
|
+
const cordisPatch = generateCordisPatch(fullName, category)
|
|
72
|
+
writeFileSync(join(pluginDir, 'cordis.patch.yml'), cordisPatch)
|
|
73
|
+
|
|
74
|
+
return { path: pluginDir, name: fullName }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 生成 package.json
|
|
79
|
+
*/
|
|
80
|
+
function generatePackageJson(name, tier, category, platform) {
|
|
81
|
+
const pkg = {
|
|
82
|
+
name,
|
|
83
|
+
version: '1.0.0',
|
|
84
|
+
description: `DSH ${category} 插件`,
|
|
85
|
+
type: 'module',
|
|
86
|
+
main: './lib/index.mjs',
|
|
87
|
+
exports: {
|
|
88
|
+
'.': './lib/index.mjs',
|
|
89
|
+
'./package.json': './package.json'
|
|
90
|
+
},
|
|
91
|
+
dependencies: {},
|
|
92
|
+
dsh: {
|
|
93
|
+
bundle: {
|
|
94
|
+
patch: './cordis.patch.yml'
|
|
95
|
+
},
|
|
96
|
+
category,
|
|
97
|
+
platform
|
|
98
|
+
},
|
|
99
|
+
engines: {
|
|
100
|
+
dsh: '^0.2.0-rc.2'
|
|
101
|
+
},
|
|
102
|
+
meta: {
|
|
103
|
+
title: name.replace('dsh-', '').split('-').map(w => w[0].toUpperCase() + w.slice(1)).join(' '),
|
|
104
|
+
description: `${category} 插件,支持 ${platform} 平台`
|
|
105
|
+
},
|
|
106
|
+
files: [
|
|
107
|
+
'lib/',
|
|
108
|
+
'cordis.patch.yml',
|
|
109
|
+
'README.md'
|
|
110
|
+
],
|
|
111
|
+
private: true
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// 根据层级添加依赖
|
|
115
|
+
if (tier === 'suite') {
|
|
116
|
+
pkg.dependencies['@capyclaw/suite-kit'] = '^0.1.0'
|
|
117
|
+
pkg.dsh.suite = './dsh-suite.json'
|
|
118
|
+
} else {
|
|
119
|
+
pkg.dependencies['@capyclaw/plugin-kit'] = '^0.2.0'
|
|
120
|
+
|
|
121
|
+
if (tier >= 1) {
|
|
122
|
+
pkg.dependencies['dsh-service-host'] = '^0.1.0'
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (tier >= 2) {
|
|
126
|
+
pkg.dependencies['dsh-tool-host'] = '^0.1.0'
|
|
127
|
+
pkg.dependencies['dsh-asset-service'] = '^0.1.0'
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// 如果有客户端代码,添加到 exports 和 files
|
|
132
|
+
if (category === 'ui' || tier >= 1) {
|
|
133
|
+
pkg.exports['./client'] = './client/client.js'
|
|
134
|
+
pkg.dsh.client = {
|
|
135
|
+
platform,
|
|
136
|
+
immediately: true
|
|
137
|
+
}
|
|
138
|
+
pkg.files.push('client/')
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return pkg
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* 生成主入口代码
|
|
146
|
+
*/
|
|
147
|
+
function generateMainEntry(name, tier) {
|
|
148
|
+
if (tier === 'suite') {
|
|
149
|
+
return `/**
|
|
150
|
+
* ${name} - DSH 插件套件
|
|
151
|
+
*/
|
|
152
|
+
|
|
153
|
+
export const name = '${name}'
|
|
154
|
+
|
|
155
|
+
export function apply(ctx) {
|
|
156
|
+
ctx.logger.info('${name} 套件已加载')
|
|
157
|
+
}
|
|
158
|
+
`
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return `/**
|
|
162
|
+
* ${name} - DSH 插件主入口
|
|
163
|
+
*/
|
|
164
|
+
|
|
165
|
+
export const name = '${name}'
|
|
166
|
+
|
|
167
|
+
export function apply(ctx, config) {
|
|
168
|
+
ctx.logger.info('${name} 插件已加载', config)
|
|
169
|
+
|
|
170
|
+
// TODO: 实现插件功能
|
|
171
|
+
}
|
|
172
|
+
`
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* 生成 README
|
|
177
|
+
*/
|
|
178
|
+
function generateReadme(name, category, platform) {
|
|
179
|
+
return `# ${name}
|
|
180
|
+
|
|
181
|
+
DSH ${category} 插件
|
|
182
|
+
|
|
183
|
+
## 功能
|
|
184
|
+
|
|
185
|
+
TODO: 描述插件功能
|
|
186
|
+
|
|
187
|
+
## 安装
|
|
188
|
+
|
|
189
|
+
\`\`\`bash
|
|
190
|
+
# 在 DSH profile 中安装
|
|
191
|
+
npm install ${name}
|
|
192
|
+
\`\`\`
|
|
193
|
+
|
|
194
|
+
## 配置
|
|
195
|
+
|
|
196
|
+
\`\`\`yaml
|
|
197
|
+
# cordis.patch.yml
|
|
198
|
+
- id: ${name}
|
|
199
|
+
name: ${name}
|
|
200
|
+
config:
|
|
201
|
+
# TODO: 配置项
|
|
202
|
+
\`\`\`
|
|
203
|
+
|
|
204
|
+
## 开发
|
|
205
|
+
|
|
206
|
+
\`\`\`bash
|
|
207
|
+
npm run validate-plugin -- <插件目录>
|
|
208
|
+
\`\`\`
|
|
209
|
+
|
|
210
|
+
## 平台支持
|
|
211
|
+
|
|
212
|
+
- ${platform}
|
|
213
|
+
|
|
214
|
+
## License
|
|
215
|
+
|
|
216
|
+
MIT
|
|
217
|
+
`
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* 生成客户端代码
|
|
222
|
+
*/
|
|
223
|
+
function generateClientCode(name) {
|
|
224
|
+
return `/**
|
|
225
|
+
* ${name} - 客户端代码
|
|
226
|
+
* 在浏览器环境中运行
|
|
227
|
+
*/
|
|
228
|
+
|
|
229
|
+
;(function() {
|
|
230
|
+
'use strict'
|
|
231
|
+
|
|
232
|
+
console.log('[${name}] 客户端代码已加载')
|
|
233
|
+
|
|
234
|
+
// TODO: 实现客户端功能
|
|
235
|
+
})()
|
|
236
|
+
`
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* 生成 cordis.patch.yml
|
|
241
|
+
*/
|
|
242
|
+
function generateCordisPatch(name, category) {
|
|
243
|
+
return `# ${name} Cordis 配置
|
|
244
|
+
|
|
245
|
+
- id: ${name}
|
|
246
|
+
name: ${name}
|
|
247
|
+
config:
|
|
248
|
+
# TODO: 添加配置项
|
|
249
|
+
`
|
|
250
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capyclaw/plugin-generator",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "DSH 插件脚手架生成器:快速创建符合规范的插件骨架",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./lib/index.mjs",
|
|
7
|
+
"bin": {
|
|
8
|
+
"create-plugin": "./bin/cli.mjs"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {},
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/Maple496/capyclaw.git",
|
|
15
|
+
"directory": "tools/capyclaw-plugin-generator"
|
|
16
|
+
},
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/Maple496/capyclaw/issues"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/Maple496/capyclaw/tree/main/tools/capyclaw-plugin-generator#readme"
|
|
21
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
const output = document.getElementById('output')
|
|
2
|
+
const toolsEl = document.getElementById('tools')
|
|
3
|
+
const input = document.getElementById('text')
|
|
4
|
+
|
|
5
|
+
async function rpc(body) {
|
|
6
|
+
const res = await fetch('/mcp', {
|
|
7
|
+
method: 'POST',
|
|
8
|
+
headers: { 'content-type': 'application/json' },
|
|
9
|
+
body: JSON.stringify(body),
|
|
10
|
+
})
|
|
11
|
+
return res.json()
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async function loadInfo() {
|
|
15
|
+
try {
|
|
16
|
+
const info = await fetch('/api/info').then((res) => res.json())
|
|
17
|
+
for (const tool of info.tools || []) {
|
|
18
|
+
const span = document.createElement('span')
|
|
19
|
+
span.className = 'tool'
|
|
20
|
+
span.textContent = tool.name
|
|
21
|
+
span.title = tool.description || ''
|
|
22
|
+
toolsEl.appendChild(span)
|
|
23
|
+
}
|
|
24
|
+
} catch {
|
|
25
|
+
toolsEl.textContent = '服务信息暂不可用'
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
document.getElementById('send').addEventListener('click', async () => {
|
|
30
|
+
output.textContent = '调用中…'
|
|
31
|
+
try {
|
|
32
|
+
const response = await rpc({
|
|
33
|
+
jsonrpc: '2.0',
|
|
34
|
+
id: Date.now(),
|
|
35
|
+
method: 'tools/call',
|
|
36
|
+
params: { name: '{{TOOL_PREFIX}}_echo', arguments: { text: input.value } },
|
|
37
|
+
})
|
|
38
|
+
output.textContent = JSON.stringify(response.result || response.error, null, 2)
|
|
39
|
+
} catch (error) {
|
|
40
|
+
output.textContent = String(error)
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
loadInfo()
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="zh-CN">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<title>{{TITLE}}</title>
|
|
7
|
+
<style>
|
|
8
|
+
:root { color-scheme: light dark; font-family: system-ui, -apple-system, "Segoe UI", sans-serif; }
|
|
9
|
+
body { margin: 0; padding: 32px; max-width: 880px; margin-inline: auto; }
|
|
10
|
+
h1 { font-size: 22px; margin: 0 0 8px; }
|
|
11
|
+
p { color: #667085; margin: 0 0 24px; }
|
|
12
|
+
.card { border: 1px solid #e4e7ec; border-radius: 12px; padding: 20px; margin-bottom: 16px; }
|
|
13
|
+
label { display: block; font-size: 13px; margin-bottom: 8px; color: #475467; }
|
|
14
|
+
input { width: 100%; box-sizing: border-box; padding: 10px 12px; border: 1px solid #d0d5dd; border-radius: 8px; font: inherit; }
|
|
15
|
+
button { margin-top: 12px; padding: 10px 16px; border: 0; border-radius: 8px; background: #155eef; color: white; font: inherit; cursor: pointer; }
|
|
16
|
+
pre { background: #f9fafb; border-radius: 8px; padding: 12px; overflow: auto; white-space: pre-wrap; word-break: break-word; }
|
|
17
|
+
.tools { display: flex; flex-wrap: wrap; gap: 8px; }
|
|
18
|
+
.tool { font-size: 12px; background: #eef4ff; color: #1849a9; padding: 4px 8px; border-radius: 999px; }
|
|
19
|
+
</style>
|
|
20
|
+
</head>
|
|
21
|
+
<body>
|
|
22
|
+
<h1>{{TITLE}}</h1>
|
|
23
|
+
<p>{{DESCRIPTION}}</p>
|
|
24
|
+
|
|
25
|
+
<div class="card">
|
|
26
|
+
<div class="tools" id="tools"></div>
|
|
27
|
+
</div>
|
|
28
|
+
|
|
29
|
+
<div class="card">
|
|
30
|
+
<label for="text">测试 {{TOOL_PREFIX}}_echo</label>
|
|
31
|
+
<input id="text" value="hello" autocomplete="off">
|
|
32
|
+
<button id="send">调用</button>
|
|
33
|
+
<pre id="output"></pre>
|
|
34
|
+
</div>
|
|
35
|
+
|
|
36
|
+
<script src="/app.js"></script>
|
|
37
|
+
</body>
|
|
38
|
+
</html>
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* {{TITLE}} 宿主插件(Cordis 入口)。
|
|
3
|
+
*
|
|
4
|
+
* 服务宿主、工具面、状态、资产、编辑器全部来自公共插件;
|
|
5
|
+
* 本文件只负责生命周期装配。
|
|
6
|
+
*/
|
|
7
|
+
import path from 'node:path'
|
|
8
|
+
import { fileURLToPath } from 'node:url'
|
|
9
|
+
import { startService } from '../server/relay.mjs'
|
|
10
|
+
|
|
11
|
+
export const name = '{{HOST_ID}}'
|
|
12
|
+
|
|
13
|
+
export async function apply(ctx, config = {}) {
|
|
14
|
+
const packageDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
|
|
15
|
+
const service = await startService({
|
|
16
|
+
packageDir,
|
|
17
|
+
port: Number(config.port) || {{PORT}},
|
|
18
|
+
hostDir: config.hostDir || process.env.{{ENV_PREFIX}}_HOME,
|
|
19
|
+
strictPort: config.strictPort,
|
|
20
|
+
lockFile: config.lockFile,
|
|
21
|
+
})
|
|
22
|
+
console.log(`[{{NAME}}] ${service.reused ? '复用' : '启动'}服务: ${service.url}`)
|
|
23
|
+
return async () => {
|
|
24
|
+
await service.stop()
|
|
25
|
+
console.log('[{{NAME}}] 服务已停止')
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** {{TITLE}} 业务实现。工具层只做 schema/分派,业务放这里。 */
|
|
2
|
+
import { capabilities as report } from './tools.mjs'
|
|
3
|
+
|
|
4
|
+
export function echo({ text }) {
|
|
5
|
+
return `echo: ${String(text)}`
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function capabilities() {
|
|
9
|
+
return {
|
|
10
|
+
name: '{{NAME}}',
|
|
11
|
+
version: '{{VERSION}}',
|
|
12
|
+
...report(),
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* {{TITLE}} 服务组合根。
|
|
3
|
+
*
|
|
4
|
+
* 宿主(端口/锁/复用/健康/指标/静态/关闭)由 dsh-service-host 提供;
|
|
5
|
+
* 工具面(能力 DSL/注册表/校验/契约/MCP)由 dsh-tool-host 提供;
|
|
6
|
+
* 本文件只保留业务装配。
|
|
7
|
+
*/
|
|
8
|
+
import fs from 'node:fs'
|
|
9
|
+
import path from 'node:path'
|
|
10
|
+
import { fileURLToPath } from 'node:url'
|
|
11
|
+
import { createServiceApp, send } from './shared/service-host.mjs'
|
|
12
|
+
import { createEditorHost } from './shared/editor-host.mjs'
|
|
13
|
+
import {
|
|
14
|
+
tools, callTool, contractVersion, capabilities, buildInstructions, createMcp,
|
|
15
|
+
} from './tools.mjs'
|
|
16
|
+
|
|
17
|
+
const HERE = path.dirname(fileURLToPath(import.meta.url))
|
|
18
|
+
const DEFAULT_PORT = {{PORT}}
|
|
19
|
+
const SERVICE_NAME = '{{NAME}}'
|
|
20
|
+
const PKG = JSON.parse(fs.readFileSync(path.join(HERE, '..', 'package.json'), 'utf8'))
|
|
21
|
+
|
|
22
|
+
const EDITOR_FILES = {
|
|
23
|
+
'/': ['index.html', 'text/html; charset=utf-8'],
|
|
24
|
+
'/index.html': ['index.html', 'text/html; charset=utf-8'],
|
|
25
|
+
'/app.js': ['app.js', 'text/javascript; charset=utf-8'],
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export async function startService(options = {}) {
|
|
29
|
+
const packageDir = options.packageDir || path.resolve(HERE, '..')
|
|
30
|
+
const editorDir = path.join(packageDir, 'editor')
|
|
31
|
+
const hasEditor = fs.existsSync(path.join(editorDir, 'index.html'))
|
|
32
|
+
|
|
33
|
+
const app = createServiceApp({
|
|
34
|
+
id: SERVICE_NAME,
|
|
35
|
+
title: PKG.meta?.title || PKG.name,
|
|
36
|
+
version: PKG.version,
|
|
37
|
+
contractVersion,
|
|
38
|
+
slug: '{{SLUG}}',
|
|
39
|
+
health: () => ({
|
|
40
|
+
editor: hasEditor ? `http://127.0.0.1:${app.port}/` : '',
|
|
41
|
+
tools: tools.length,
|
|
42
|
+
}),
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
const instructions = buildInstructions({
|
|
46
|
+
title: PKG.meta?.title || PKG.name,
|
|
47
|
+
intro: `本服务提供 {{TOOL_PREFIX}}_* 工具。`,
|
|
48
|
+
startup: [`先调用 \`{{TOOL_PREFIX}}_capabilities\` 确认 contractVersion 为 1.x。`],
|
|
49
|
+
capabilities: capabilities().capabilities,
|
|
50
|
+
rules: ['工具返回已含回执;改完一句话回复,不要重复验证。'],
|
|
51
|
+
})
|
|
52
|
+
const mcp = createMcp({ name: SERVICE_NAME, version: PKG.version }, instructions)
|
|
53
|
+
app.use((req, res, u) => {
|
|
54
|
+
if (u.pathname !== '/mcp') return false
|
|
55
|
+
return mcp(req, res).then(() => true)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
app.get('/api/info', (req, res) => {
|
|
59
|
+
send(req, res, 200, {
|
|
60
|
+
ok: true,
|
|
61
|
+
name: SERVICE_NAME,
|
|
62
|
+
version: PKG.version,
|
|
63
|
+
contractVersion,
|
|
64
|
+
editor: hasEditor,
|
|
65
|
+
tools: tools.map((tool) => ({ name: tool.name, description: tool.description })),
|
|
66
|
+
})
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
if (hasEditor) {
|
|
70
|
+
const editor = createEditorHost({ id: SERVICE_NAME, dir: editorDir, files: EDITOR_FILES })
|
|
71
|
+
for (const route of editor.routes()) app.use(route)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const info = await app.listen({
|
|
75
|
+
port: Number(options.port ?? DEFAULT_PORT),
|
|
76
|
+
hostDir: options.hostDir,
|
|
77
|
+
strictPort: options.strictPort,
|
|
78
|
+
lockFile: options.lockFile,
|
|
79
|
+
allowReuse: options.allowReuse !== false,
|
|
80
|
+
})
|
|
81
|
+
return { ...info, editorUrl: `${info.url}/`, stop: () => info.stop() }
|
|
82
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP 工具面:能力声明 → 注册表 → 工具清单。
|
|
3
|
+
*
|
|
4
|
+
* 新增工具:在 impl.mjs 写实现,这里 defineTool 声明 schema,并加入能力。
|
|
5
|
+
*/
|
|
6
|
+
import {
|
|
7
|
+
defineTool, defineCapability, createRegistry, createMcpHandler,
|
|
8
|
+
createCapabilityReport, buildInstructions,
|
|
9
|
+
} from './shared/tool-host.mjs'
|
|
10
|
+
import { echo, capabilities as capabilitiesImpl } from './impl.mjs'
|
|
11
|
+
|
|
12
|
+
export const contractVersion = '1.0.0'
|
|
13
|
+
|
|
14
|
+
const echoTool = defineTool({
|
|
15
|
+
name: '{{TOOL_PREFIX}}_echo',
|
|
16
|
+
description: '回显输入文本,用于验证服务连通性。',
|
|
17
|
+
inputSchema: {
|
|
18
|
+
type: 'object',
|
|
19
|
+
additionalProperties: false,
|
|
20
|
+
properties: {
|
|
21
|
+
text: { type: 'string', maxLength: 2000, description: '要回显的文本' },
|
|
22
|
+
},
|
|
23
|
+
required: ['text'],
|
|
24
|
+
},
|
|
25
|
+
handler: (args) => ({ content: [{ type: 'text', text: echo(args) }] }),
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
const capabilitiesTool = defineTool({
|
|
29
|
+
name: '{{TOOL_PREFIX}}_capabilities',
|
|
30
|
+
description: '返回契约版本、工具清单和能力说明。',
|
|
31
|
+
inputSchema: { type: 'object', additionalProperties: false, properties: {} },
|
|
32
|
+
kind: 'read',
|
|
33
|
+
handler: () => ({ content: [{ type: 'text', text: JSON.stringify(capabilitiesImpl(), null, 2) }] }),
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
const capability = defineCapability({
|
|
37
|
+
id: '{{SLUG}}',
|
|
38
|
+
title: '{{TITLE}}',
|
|
39
|
+
description: '{{DESCRIPTION}}',
|
|
40
|
+
tags: ['service'],
|
|
41
|
+
tools: [echoTool, capabilitiesTool],
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
export const registry = createRegistry([capability], {
|
|
45
|
+
report: ({ editorUrl = '', runtime = {}, ...rest }) => createCapabilityReport({
|
|
46
|
+
...rest,
|
|
47
|
+
contractVersion,
|
|
48
|
+
packageVersion: '{{VERSION}}',
|
|
49
|
+
editorUrl,
|
|
50
|
+
runtime,
|
|
51
|
+
}),
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
export const tools = registry.TOOLS
|
|
55
|
+
export const callTool = (name, args) => registry.TOOL_IMPL[name]?.(args)
|
|
56
|
+
export function capabilities() { return registry.ctx.contract.report() }
|
|
57
|
+
|
|
58
|
+
export function createMcp(serverInfo, instructions) {
|
|
59
|
+
return createMcpHandler({ registry, serverInfo, instructions })
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export { buildInstructions }
|
package/README.md
DELETED