@capyclaw/plugin-generator 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/create-doc-model.mjs +128 -0
- package/bin/create-plugin.mjs +460 -0
- package/bin/pack-plugin.mjs +93 -0
- package/package.json +19 -4
- package/templates/doc-model/README.md.tmpl +51 -0
- package/templates/doc-model/schema.mjs.tmpl +69 -0
- package/templates/doc-model/test.mjs.tmpl +92 -0
- package/templates/doc-model/tools.mjs.tmpl +169 -0
- 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
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 文档模型脚手架:从「一份领域文档的形态」直接生成可用的读/定位/改/校验四件套。
|
|
4
|
+
*
|
|
5
|
+
* 生成内容(不依赖具体宿主,可直接放进任意 workbench 的 server/ 下):
|
|
6
|
+
* model/schema.mjs 唯一需要按领域改的文件(怎么分页、ID 在哪、文本在哪)
|
|
7
|
+
* model/tools.mjs <ns>_read / <ns>_edit / <ns>_validate 三个工具工厂
|
|
8
|
+
* model/model.test.mjs 定位 → 打补丁 → 校验 的回归测试
|
|
9
|
+
* model/README.md 接入步骤
|
|
10
|
+
*
|
|
11
|
+
* 用法:
|
|
12
|
+
* node tools/capyclaw-plugin-generator/bin/create-doc-model.mjs \
|
|
13
|
+
* --ns sheet --title "表格工作台" --out ./my-plugins/sheet
|
|
14
|
+
*/
|
|
15
|
+
import fs from 'node:fs'
|
|
16
|
+
import path from 'node:path'
|
|
17
|
+
import { fileURLToPath } from 'node:url'
|
|
18
|
+
|
|
19
|
+
const HERE = path.dirname(fileURLToPath(import.meta.url))
|
|
20
|
+
const TEMPLATES = path.join(HERE, '..', 'templates', 'doc-model')
|
|
21
|
+
|
|
22
|
+
function printHelp() {
|
|
23
|
+
console.log(`
|
|
24
|
+
文档模型脚手架
|
|
25
|
+
|
|
26
|
+
用法:
|
|
27
|
+
node create-doc-model.mjs --ns sheet --title "表格工作台" --out ./out/sheet
|
|
28
|
+
|
|
29
|
+
参数:
|
|
30
|
+
--ns <kebab> 命名空间/工具前缀(必填,如 sheet → sheet_read)
|
|
31
|
+
--title <text> 显示标题(默认取 ns)
|
|
32
|
+
--out <dir> 输出目录(必填)
|
|
33
|
+
--force 覆盖已存在的文件
|
|
34
|
+
--help, -h 显示帮助
|
|
35
|
+
|
|
36
|
+
生成:
|
|
37
|
+
<out>/model/schema.mjs
|
|
38
|
+
<out>/model/tools.mjs
|
|
39
|
+
<out>/model/model.test.mjs
|
|
40
|
+
<out>/model/README.md
|
|
41
|
+
`)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function parseArgs(argv) {
|
|
45
|
+
const options = {}
|
|
46
|
+
for (let i = 0; i < argv.length; i++) {
|
|
47
|
+
const arg = argv[i]
|
|
48
|
+
const next = () => {
|
|
49
|
+
const value = argv[++i]
|
|
50
|
+
if (value === undefined) throw new Error(`参数缺少值: ${arg}`)
|
|
51
|
+
return value
|
|
52
|
+
}
|
|
53
|
+
if (arg === '--help' || arg === '-h') options.help = true
|
|
54
|
+
else if (arg === '--force') options.force = true
|
|
55
|
+
else if (arg === '--ns') options.ns = next()
|
|
56
|
+
else if (arg.startsWith('--ns=')) options.ns = arg.slice(5)
|
|
57
|
+
else if (arg === '--title') options.title = next()
|
|
58
|
+
else if (arg.startsWith('--title=')) options.title = arg.slice(8)
|
|
59
|
+
else if (arg === '--out') options.out = next()
|
|
60
|
+
else if (arg.startsWith('--out=')) options.out = arg.slice(6)
|
|
61
|
+
else throw new Error(`未知参数: ${arg}`)
|
|
62
|
+
}
|
|
63
|
+
return options
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function slugOf(value) {
|
|
67
|
+
return String(value || '').trim().toLowerCase().replace(/[^a-z0-9-]+/g, '-').replace(/^-+|-+$/g, '')
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function render(templateRel, tokens) {
|
|
71
|
+
const source = fs.readFileSync(path.join(TEMPLATES, templateRel), 'utf8')
|
|
72
|
+
return source.replace(/\{\{([A-Z_]+)\}\}/g, (match, key) => (key in tokens ? tokens[key] : match))
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function writeFile(file, content, force) {
|
|
76
|
+
fs.mkdirSync(path.dirname(file), { recursive: true })
|
|
77
|
+
if (fs.existsSync(file) && !force) throw new Error(`已存在,加 --force 覆盖: ${file}`)
|
|
78
|
+
fs.writeFileSync(file, content, 'utf8')
|
|
79
|
+
console.log('+ ' + path.relative(process.cwd(), file))
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function main() {
|
|
83
|
+
let options
|
|
84
|
+
try {
|
|
85
|
+
options = parseArgs(process.argv.slice(2))
|
|
86
|
+
} catch (error) {
|
|
87
|
+
console.error(`✗ ${error.message}`)
|
|
88
|
+
process.exit(1)
|
|
89
|
+
}
|
|
90
|
+
if (options.help) return printHelp()
|
|
91
|
+
|
|
92
|
+
const ns = slugOf(options.ns)
|
|
93
|
+
if (!ns) {
|
|
94
|
+
console.error('✗ 缺少 --ns(命名空间,如 sheet)')
|
|
95
|
+
process.exit(1)
|
|
96
|
+
}
|
|
97
|
+
const out = options.out ? path.resolve(options.out) : null
|
|
98
|
+
if (!out) {
|
|
99
|
+
console.error('✗ 缺少 --out(输出目录)')
|
|
100
|
+
process.exit(1)
|
|
101
|
+
}
|
|
102
|
+
const title = options.title || options.ns
|
|
103
|
+
const tokens = {
|
|
104
|
+
NS: ns,
|
|
105
|
+
TITLE: title,
|
|
106
|
+
NS_LABEL: title,
|
|
107
|
+
CONST_PREFIX: ns.toUpperCase().replace(/-/g, '_'),
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const files = [
|
|
111
|
+
['schema.mjs.tmpl', 'model/schema.mjs'],
|
|
112
|
+
['tools.mjs.tmpl', 'model/tools.mjs'],
|
|
113
|
+
['test.mjs.tmpl', 'model/model.test.mjs'],
|
|
114
|
+
['README.md.tmpl', 'model/README.md'],
|
|
115
|
+
]
|
|
116
|
+
try {
|
|
117
|
+
for (const [template, dest] of files) {
|
|
118
|
+
writeFile(path.join(out, dest), render(template, tokens), options.force)
|
|
119
|
+
}
|
|
120
|
+
} catch (error) {
|
|
121
|
+
console.error(`✗ ${error.message}`)
|
|
122
|
+
process.exit(1)
|
|
123
|
+
}
|
|
124
|
+
console.log(`\n✓ 已生成 ${ns} 文档模型(4 个文件)`)
|
|
125
|
+
console.log(` 下一步:改 model/schema.mjs 的适配函数 → node ${path.join(path.relative(process.cwd(), out), 'model', 'model.test.mjs')}`)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
main()
|
|
@@ -0,0 +1,460 @@
|
|
|
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 '../../../../A/build/sync-shared.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
|
+
/* server/shared/*.mjs 是包依赖转发(sync-shared 生成),实现来自这些包:
|
|
132
|
+
* 新增插件必须声明依赖,否则装完即用时解析不到。 */
|
|
133
|
+
'@capyclaw/service-host': '^0.1.3',
|
|
134
|
+
'@capyclaw/tool-host': '^0.1.3',
|
|
135
|
+
'@capyclaw/asset-service': '^0.1.3',
|
|
136
|
+
'@capyclaw/editor-host': '^0.1.3',
|
|
137
|
+
},
|
|
138
|
+
engines: {
|
|
139
|
+
dsh: '^0.2.0-rc.2',
|
|
140
|
+
},
|
|
141
|
+
meta: { title, description },
|
|
142
|
+
files,
|
|
143
|
+
license: 'MIT',
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function makeCordisPatch({ name, hostId, serverName, port, type }) {
|
|
148
|
+
const host = ` - id: ${hostId}
|
|
149
|
+
name: ${JSON.stringify(name)}
|
|
150
|
+
config:
|
|
151
|
+
port: ${port}`
|
|
152
|
+
if (type === 'minimal') {
|
|
153
|
+
return `# 由脚手架生成;只注册宿主插件,不提供 MCP 服务。
|
|
154
|
+
|
|
155
|
+
- insert:
|
|
156
|
+
${host}
|
|
157
|
+
`
|
|
158
|
+
}
|
|
159
|
+
return `# 由脚手架生成;端口或 MCP 配置变化时同步修改这里和 dsh-suite.json。
|
|
160
|
+
|
|
161
|
+
- insert:
|
|
162
|
+
${host}
|
|
163
|
+
- id: mcp-${serverName}
|
|
164
|
+
name: '@deepseek-ai/dsh-mcp-client'
|
|
165
|
+
config:
|
|
166
|
+
serverName: ${serverName}
|
|
167
|
+
transport: streamable-http
|
|
168
|
+
url: 'http://127.0.0.1:${port}/mcp'
|
|
169
|
+
failOnStartupError: false
|
|
170
|
+
toolCallTimeoutMs: 120000
|
|
171
|
+
`
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function makeSuiteManifest({ suiteName, name, title, description, hostId, serverName, toolPrefix, port }) {
|
|
175
|
+
return {
|
|
176
|
+
name: suiteName,
|
|
177
|
+
version: VERSION,
|
|
178
|
+
title: `${title} 套件`,
|
|
179
|
+
description: description || `${title} 运行时与 MCP 工具`,
|
|
180
|
+
packages: {
|
|
181
|
+
[name]: {
|
|
182
|
+
role: 'runtime',
|
|
183
|
+
version: `^${VERSION}`,
|
|
184
|
+
path: `../${name}`,
|
|
185
|
+
provides: {
|
|
186
|
+
tools: [`${toolPrefix}_*`],
|
|
187
|
+
services: [`${serverName}-service`],
|
|
188
|
+
ports: [port],
|
|
189
|
+
capabilities: {
|
|
190
|
+
[`${serverName}.runtime`]: VERSION,
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
rows: [
|
|
196
|
+
{ id: hostId, name, config: { port } },
|
|
197
|
+
{
|
|
198
|
+
id: `mcp-${serverName}`,
|
|
199
|
+
name: '@deepseek-ai/dsh-mcp-client',
|
|
200
|
+
config: {
|
|
201
|
+
serverName,
|
|
202
|
+
transport: 'streamable-http',
|
|
203
|
+
url: `http://127.0.0.1:${port}/mcp`,
|
|
204
|
+
failOnStartupError: false,
|
|
205
|
+
toolCallTimeoutMs: 120000,
|
|
206
|
+
},
|
|
207
|
+
},
|
|
208
|
+
],
|
|
209
|
+
checks: [
|
|
210
|
+
{
|
|
211
|
+
id: `${serverName}-health`,
|
|
212
|
+
type: 'http',
|
|
213
|
+
url: `http://127.0.0.1:${port}/healthz`,
|
|
214
|
+
expectStatus: 200,
|
|
215
|
+
expectBody: { ok: true },
|
|
216
|
+
timeoutMs: 3000,
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
id: `${serverName}-contract`,
|
|
220
|
+
type: 'contract',
|
|
221
|
+
url: `http://127.0.0.1:${port}/mcp`,
|
|
222
|
+
tool: `${toolPrefix}_capabilities`,
|
|
223
|
+
major: 1,
|
|
224
|
+
timeoutMs: 5000,
|
|
225
|
+
},
|
|
226
|
+
],
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
async function main() {
|
|
231
|
+
let options
|
|
232
|
+
try {
|
|
233
|
+
options = parseArgs(process.argv.slice(2))
|
|
234
|
+
} catch (error) {
|
|
235
|
+
console.error(`✗ ${error.message}\n`)
|
|
236
|
+
printHelp()
|
|
237
|
+
process.exit(1)
|
|
238
|
+
}
|
|
239
|
+
if (options.help) {
|
|
240
|
+
printHelp()
|
|
241
|
+
return
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (!options.name) {
|
|
245
|
+
if (options.yes) throw new Error('--yes 模式必须提供 --name')
|
|
246
|
+
options.name = await ask('插件包名(kebab-case,例 dsh-my-service): ')
|
|
247
|
+
}
|
|
248
|
+
if (!options.type) {
|
|
249
|
+
options.type = options.yes ? 'service' : (await ask('类型 service/ui/minimal(默认 service): ')) || 'service'
|
|
250
|
+
}
|
|
251
|
+
if (!options.title) {
|
|
252
|
+
options.title = options.yes ? options.name : (await ask('显示标题(默认取插件名): ')) || options.name
|
|
253
|
+
}
|
|
254
|
+
if (!options.description) {
|
|
255
|
+
options.description = options.yes ? `${options.title} DSH 插件` : (await ask('描述(可留空): '))
|
|
256
|
+
}
|
|
257
|
+
if (!options.port) {
|
|
258
|
+
options.port = options.yes ? DEFAULT_PORT : Number(await ask(`默认端口(${DEFAULT_PORT}): `) || DEFAULT_PORT)
|
|
259
|
+
}
|
|
260
|
+
if (!options.out) options.out = options.yes ? path.join(HERE, 'my-plugins') : (await ask('输出父目录(默认 ./my-plugins): ')) || path.join(HERE, 'my-plugins')
|
|
261
|
+
|
|
262
|
+
const name = options.name
|
|
263
|
+
const type = options.type
|
|
264
|
+
if (!/^[a-z0-9][a-z0-9-]*$/.test(name)) throw new Error('--name 必须是 kebab-case(小写字母/数字/连字符)')
|
|
265
|
+
if (!['service', 'ui', 'minimal'].includes(type)) throw new Error('--type 必须是 service/ui/minimal')
|
|
266
|
+
if (!Number.isInteger(options.port) || options.port < 1 || options.port > 65535) throw new Error('--port 必须是 1-65535 的整数')
|
|
267
|
+
|
|
268
|
+
const slug = slugOf(name)
|
|
269
|
+
const serverName = slug.replace(/[^a-zA-Z0-9_-]/g, '-')
|
|
270
|
+
const toolPrefix = serverName.replace(/-/g, '_')
|
|
271
|
+
const hostId = `${name}-host`
|
|
272
|
+
const suiteName = `${name}-suite`
|
|
273
|
+
const domainName = options.domain || slug
|
|
274
|
+
if (!/^[a-z0-9][a-z0-9-]*$/.test(domainName)) throw new Error('--domain 必须是 kebab-case')
|
|
275
|
+
|
|
276
|
+
const rootDir = path.join(path.resolve(options.out), name)
|
|
277
|
+
if (fs.existsSync(rootDir)) throw new Error(`目标已存在: ${rootDir}`)
|
|
278
|
+
|
|
279
|
+
const withSuite = !options.pluginOnly && type !== 'minimal'
|
|
280
|
+
if (type === 'minimal' && !options.pluginOnly) {
|
|
281
|
+
console.log('· minimal 类型不生成 suite/domain,按 plugin-only 处理')
|
|
282
|
+
}
|
|
283
|
+
const pluginDir = withSuite ? path.join(rootDir, 'plugin', name) : rootDir
|
|
284
|
+
const suiteDir = path.join(rootDir, 'plugin', suiteName)
|
|
285
|
+
const domainDir = path.join(rootDir, 'domains', domainName)
|
|
286
|
+
|
|
287
|
+
const tokens = {
|
|
288
|
+
NAME: name,
|
|
289
|
+
SLUG: slug,
|
|
290
|
+
SLUG_U: slug.replace(/-/g, '_'),
|
|
291
|
+
TITLE: options.title,
|
|
292
|
+
DESCRIPTION: options.description,
|
|
293
|
+
VERSION,
|
|
294
|
+
PORT: String(options.port),
|
|
295
|
+
HOST_ID: hostId,
|
|
296
|
+
SERVER_NAME: serverName,
|
|
297
|
+
TOOL_PREFIX: toolPrefix,
|
|
298
|
+
ENV_PREFIX: `DSH_${slug.toUpperCase().replace(/-/g, '_')}`,
|
|
299
|
+
SUITE_NAME: suiteName,
|
|
300
|
+
DOMAIN_NAME: domainName,
|
|
301
|
+
DOMAIN_PROFILE: `domain-${domainName}`,
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
fs.mkdirSync(pluginDir, { recursive: true })
|
|
305
|
+
writeJson(path.join(pluginDir, 'package.json'), makePackageJson({
|
|
306
|
+
name,
|
|
307
|
+
title: options.title,
|
|
308
|
+
description: options.description,
|
|
309
|
+
type,
|
|
310
|
+
version: VERSION,
|
|
311
|
+
}))
|
|
312
|
+
fs.writeFileSync(path.join(pluginDir, 'cordis.patch.yml'), makeCordisPatch({ name, hostId, serverName, port: options.port, type }))
|
|
313
|
+
|
|
314
|
+
if (type === 'minimal') {
|
|
315
|
+
fs.mkdirSync(path.join(pluginDir, 'lib'), { recursive: true })
|
|
316
|
+
fs.writeFileSync(path.join(pluginDir, 'lib', 'host.mjs'), render(`export const name = '{{HOST_ID}}'
|
|
317
|
+
|
|
318
|
+
export function apply() {
|
|
319
|
+
console.log('[{{NAME}}] 已加载')
|
|
320
|
+
}
|
|
321
|
+
`, tokens))
|
|
322
|
+
} else {
|
|
323
|
+
writeTemplate('plugin/lib/host.mjs.tmpl', path.join(pluginDir, 'lib', 'host.mjs'), tokens)
|
|
324
|
+
writeTemplate('plugin/server/relay.mjs.tmpl', path.join(pluginDir, 'server', 'relay.mjs'), tokens)
|
|
325
|
+
writeTemplate('plugin/server/tools.mjs.tmpl', path.join(pluginDir, 'server', 'tools.mjs'), tokens)
|
|
326
|
+
writeTemplate('plugin/server/impl.mjs.tmpl', path.join(pluginDir, 'server', 'impl.mjs'), tokens)
|
|
327
|
+
writeTemplate('plugin/contract/contract.json.tmpl', path.join(pluginDir, 'contract', `${slug}-contract.json`), tokens)
|
|
328
|
+
if (type === 'ui') {
|
|
329
|
+
writeTemplate('plugin/editor/index.html.tmpl', path.join(pluginDir, 'editor', 'index.html'), tokens)
|
|
330
|
+
writeTemplate('plugin/editor/app.js.tmpl', path.join(pluginDir, 'editor', 'app.js'), tokens)
|
|
331
|
+
}
|
|
332
|
+
syncSharedDir(path.join(pluginDir, 'server', 'shared'), { force: true })
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
fs.writeFileSync(path.join(pluginDir, 'README.md'), `# ${options.title}
|
|
336
|
+
|
|
337
|
+
${options.description}
|
|
338
|
+
|
|
339
|
+
## 开发
|
|
340
|
+
|
|
341
|
+
\`\`\`bash
|
|
342
|
+
npm run validate-plugin -- ${pluginDir.replace(/\\/g, '/')}
|
|
343
|
+
\`\`\`
|
|
344
|
+
|
|
345
|
+
## 安装到 DSH
|
|
346
|
+
|
|
347
|
+
\`\`\`bash
|
|
348
|
+
dsh plugin --profile web add ${pluginDir.replace(/\\/g, '/')}
|
|
349
|
+
\`\`\`
|
|
350
|
+
|
|
351
|
+
${type === 'ui' ? `启动后浏览器打开:http://127.0.0.1:${options.port}/\n` : ''}`)
|
|
352
|
+
|
|
353
|
+
if (withSuite) {
|
|
354
|
+
const suite = makeSuiteManifest({
|
|
355
|
+
suiteName,
|
|
356
|
+
name,
|
|
357
|
+
title: options.title,
|
|
358
|
+
description: options.description,
|
|
359
|
+
hostId,
|
|
360
|
+
serverName,
|
|
361
|
+
toolPrefix,
|
|
362
|
+
port: options.port,
|
|
363
|
+
})
|
|
364
|
+
writeJson(path.join(suiteDir, 'package.json'), {
|
|
365
|
+
name: suiteName,
|
|
366
|
+
version: VERSION,
|
|
367
|
+
private: true,
|
|
368
|
+
description: `${options.title} 套件`,
|
|
369
|
+
type: 'module',
|
|
370
|
+
main: './lib/index.mjs',
|
|
371
|
+
exports: {
|
|
372
|
+
'.': './lib/index.mjs',
|
|
373
|
+
'./package.json': './package.json',
|
|
374
|
+
},
|
|
375
|
+
dependencies: { [name]: `^${VERSION}` },
|
|
376
|
+
dsh: {
|
|
377
|
+
bundle: { patch: './cordis.patch.yml' },
|
|
378
|
+
suite: './dsh-suite.json',
|
|
379
|
+
},
|
|
380
|
+
engines: {
|
|
381
|
+
dsh: '^0.2.0-rc.2',
|
|
382
|
+
},
|
|
383
|
+
files: ['lib/', 'dsh-suite.json', 'cordis.patch.yml', 'README.md', 'package.json'],
|
|
384
|
+
license: 'MIT',
|
|
385
|
+
})
|
|
386
|
+
writeJson(path.join(suiteDir, 'dsh-suite.json'), suite)
|
|
387
|
+
fs.mkdirSync(path.join(suiteDir, 'lib'), { recursive: true })
|
|
388
|
+
fs.writeFileSync(path.join(suiteDir, 'cordis.patch.yml'), renderCordisPatch(suite.rows, { header: true }))
|
|
389
|
+
fs.writeFileSync(path.join(suiteDir, 'lib', 'index.mjs'), `export const name = '${suiteName}'\n\nexport function apply() {}\n`)
|
|
390
|
+
fs.writeFileSync(path.join(suiteDir, 'README.md'), `# ${suiteName}\n\n安装:\n\n\`\`\`bash\nnode C/packages/capyclaw-suite-kit/bin/dsh-suite.mjs install ${suiteDir.replace(/\\/g, '/')} --profile web\n\`\`\`\n`)
|
|
391
|
+
|
|
392
|
+
writeJson(path.join(domainDir, 'dsh-domain.json'), {
|
|
393
|
+
name: domainName,
|
|
394
|
+
version: VERSION,
|
|
395
|
+
title: `${options.title} 领域包`,
|
|
396
|
+
description: `只加载 ${options.title} 所需的运行时和工具。`,
|
|
397
|
+
baseBundles: ['@deepseek-ai/dsh-base', '@deepseek-ai/dsh-web-app'],
|
|
398
|
+
components: [`../../plugin/${suiteName}`],
|
|
399
|
+
profile: { name: tokens.DOMAIN_PROFILE, port: 3080 },
|
|
400
|
+
runtime: {
|
|
401
|
+
driver: 'node',
|
|
402
|
+
workId: 'main',
|
|
403
|
+
bindings: [
|
|
404
|
+
{
|
|
405
|
+
row: suite.rows[0].id,
|
|
406
|
+
config: { port: '${ports.content}' },
|
|
407
|
+
},
|
|
408
|
+
],
|
|
409
|
+
model: {
|
|
410
|
+
id: 'llm-deepseek',
|
|
411
|
+
plugin: '@deepseek-ai/dsh-llm-deepseek-api-key',
|
|
412
|
+
apiKeyEnv: 'DEEPSEEK_API_KEY',
|
|
413
|
+
baseUrlEnv: 'DEEPSEEK_BASE_URL',
|
|
414
|
+
baseURL: 'https://api.deepseek.com/anthropic',
|
|
415
|
+
},
|
|
416
|
+
requires: {
|
|
417
|
+
tools: [`${toolPrefix}_*`],
|
|
418
|
+
services: [`${serverName}-service`],
|
|
419
|
+
capabilities: [`${serverName}.runtime`],
|
|
420
|
+
},
|
|
421
|
+
scope: {
|
|
422
|
+
sessions: { path: '.dsh/sessions', isolated: true },
|
|
423
|
+
files: [
|
|
424
|
+
{ id: 'workspace', kind: 'workspace', path: 'workspace', patterns: ['**'] },
|
|
425
|
+
{ id: 'plugin-content', kind: 'content', path: 'content/plugin', patterns: ['**'] },
|
|
426
|
+
],
|
|
427
|
+
workspace: { workId: 'main', files: ['**'], allowNew: true },
|
|
428
|
+
network: { bind: ['127.0.0.1'] },
|
|
429
|
+
processes: { killTree: true },
|
|
430
|
+
},
|
|
431
|
+
ports: {
|
|
432
|
+
harness: { preferred: 3080, range: 100 },
|
|
433
|
+
/* 脚手架生成的插件只声明「可接受的端口范围」,不继承任何既有插件的端口:
|
|
434
|
+
* 曾经这里写死过某个工作台的固定端口,于是生成的插件会一起去抢同一个号。
|
|
435
|
+
* 要固定就用 --port(默认 DEFAULT_PORT)。 */
|
|
436
|
+
content: { preferred: options.port || DEFAULT_PORT, range: 100 },
|
|
437
|
+
},
|
|
438
|
+
},
|
|
439
|
+
removeRows: suite.rows.map((row) => row.id),
|
|
440
|
+
})
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
console.log(`\n✓ 已生成: ${rootDir}`)
|
|
444
|
+
console.log(` plugin: ${pluginDir}`)
|
|
445
|
+
if (withSuite) {
|
|
446
|
+
console.log(` suite : ${suiteDir}`)
|
|
447
|
+
console.log(` domain: ${domainDir}`)
|
|
448
|
+
}
|
|
449
|
+
console.log('\n下一步:')
|
|
450
|
+
console.log(` npm run validate-plugin -- ${pluginDir.replace(/\\/g, '/')}`)
|
|
451
|
+
if (withSuite) {
|
|
452
|
+
console.log(` node C/packages/capyclaw-suite-kit/bin/dsh-suite.mjs validate ${suiteDir}`)
|
|
453
|
+
console.log(` node C/packages/capyclaw-suite-kit/bin/dsh-domain.mjs validate ${domainDir}`)
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
main().catch((error) => {
|
|
458
|
+
console.error(`\n✗ ${error.message}`)
|
|
459
|
+
process.exit(1)
|
|
460
|
+
})
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* DSH 插件独立打包 CLI(单插件 → 可分发的 zip)。
|
|
4
|
+
*
|
|
5
|
+
* 用法:
|
|
6
|
+
* node pack-plugin.mjs <插件目录> [--out <目录>]
|
|
7
|
+
*
|
|
8
|
+
* 输出:
|
|
9
|
+
* <out>/<A|B|C>/<name>-<version>.zip(默认 <out> = 仓根 dist/;类别由分发面决定,
|
|
10
|
+
* 仓外插件按独立类 B 落盘)
|
|
11
|
+
*
|
|
12
|
+
* 这是 `dsh-release release <目录>` 的薄封装:打包实现只有一份
|
|
13
|
+
* (@capyclaw/plugin-kit 的 packPlugin),闸门也只有一份(验证器 + 发布闸 +
|
|
14
|
+
* vendor 依赖内置)。不再有绕过发布闸、把 zip 写到 dist/ 之外的第二条路径。
|
|
15
|
+
*/
|
|
16
|
+
import fs from 'node:fs'
|
|
17
|
+
import path from 'node:path'
|
|
18
|
+
|
|
19
|
+
import { buildIndex, DEFAULT_OUT, releasePlugin } from '../../capyclaw-plugin-release/lib/index.mjs'
|
|
20
|
+
import { distributionSurface } from '../../capyclaw-plugin-release/lib/surface.mjs'
|
|
21
|
+
|
|
22
|
+
function usage() {
|
|
23
|
+
console.log(`
|
|
24
|
+
DSH 插件打包工具(与 dsh-release 同一实现)
|
|
25
|
+
|
|
26
|
+
用法:
|
|
27
|
+
node pack-plugin.mjs <插件目录> [--out <源目录>]
|
|
28
|
+
|
|
29
|
+
说明:
|
|
30
|
+
- 默认输出到仓根 dist/,产物落 dist/<A|B|C>/<name>-<version>.zip
|
|
31
|
+
- 打包前过两道闸:插件规范验证(dsh-release 同款)+ 发布闸
|
|
32
|
+
- 始终包含 package.json、README、LICENSE、THIRD_PARTY_NOTICES(存在时)
|
|
33
|
+
- 按 package.json files 收集,支持目录、文件与 ! 排除项
|
|
34
|
+
- 打包后重建 <out>/index.json,保证索引与 zip 同步
|
|
35
|
+
`)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function parseArgs(argv) {
|
|
39
|
+
const options = { src: '', out: DEFAULT_OUT }
|
|
40
|
+
for (let i = 0; i < argv.length; i++) {
|
|
41
|
+
const arg = argv[i]
|
|
42
|
+
if (arg === '--help' || arg === '-h') options.help = true
|
|
43
|
+
else if (arg === '--out' || arg === '-o') options.out = argv[++i]
|
|
44
|
+
else if (arg.startsWith('--out=')) options.out = arg.slice('--out='.length)
|
|
45
|
+
else if (!options.src) options.src = arg
|
|
46
|
+
else throw new Error(`未知参数: ${arg}`)
|
|
47
|
+
}
|
|
48
|
+
return options
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function main() {
|
|
52
|
+
let options
|
|
53
|
+
try {
|
|
54
|
+
options = parseArgs(process.argv.slice(2))
|
|
55
|
+
} catch (error) {
|
|
56
|
+
console.error(`✗ ${error.message}`)
|
|
57
|
+
usage()
|
|
58
|
+
process.exit(1)
|
|
59
|
+
}
|
|
60
|
+
if (options.help || !options.src) {
|
|
61
|
+
usage()
|
|
62
|
+
process.exit(options.help ? 0 : 1)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const srcDir = path.resolve(options.src)
|
|
66
|
+
if (!fs.existsSync(path.join(srcDir, 'package.json'))) {
|
|
67
|
+
console.error(`✗ 找不到插件 package.json: ${srcDir}`)
|
|
68
|
+
process.exit(1)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
try {
|
|
72
|
+
const out = path.resolve(options.out)
|
|
73
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(srcDir, 'package.json'), 'utf8'))
|
|
74
|
+
const entry = distributionSurface({ out }).plugins.find((item) => item.name === pkg.name)
|
|
75
|
+
const result = await releasePlugin(srcDir, { out, className: entry?.cls })
|
|
76
|
+
const index = buildIndex(out)
|
|
77
|
+
console.log(`✓ 已打包: ${result.outFile}`)
|
|
78
|
+
console.log(` 文件数: ${result.files.length}${result.vendored?.length ? `;内置依赖: ${result.vendored.join(', ')}` : ''}`)
|
|
79
|
+
console.log(` 源索引: ${path.join(path.resolve(options.out), 'index.json')}(${index.plugins.length} 条)`)
|
|
80
|
+
console.log(' 安装:')
|
|
81
|
+
console.log(' 1) 解压到任意目录')
|
|
82
|
+
if (result.pkg.dsh?.suite) {
|
|
83
|
+
console.log(' 2) node C/packages/capyclaw-suite-kit/bin/dsh-suite.mjs install <解压后的目录> --profile web')
|
|
84
|
+
} else {
|
|
85
|
+
console.log(' 2) dsh plugin --profile web add <解压后的目录>')
|
|
86
|
+
}
|
|
87
|
+
} catch (error) {
|
|
88
|
+
console.error(`✗ ${error.message}`)
|
|
89
|
+
process.exit(1)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
await main()
|