@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
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.1",
|
|
4
|
+
"description": "DSH 插件脚手架生成器:快速创建符合规范的插件骨架",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"create-plugin": "./bin/create-plugin.mjs",
|
|
8
|
+
"create-doc-model": "./bin/create-doc-model.mjs"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {},
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/Maple496/capyclaw.git",
|
|
15
|
+
"directory": "C/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/C/tools/capyclaw-plugin-generator#readme"
|
|
21
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# {{TITLE}} · 文档模型
|
|
2
|
+
|
|
3
|
+
由 `create-doc-model.mjs` 生成。这套文件把「PPT 那种一次读就能按元素 ID 精确修改」
|
|
4
|
+
的路径变成可复用件——你只需要描述**这份文档长什么样**。
|
|
5
|
+
|
|
6
|
+
## 三层
|
|
7
|
+
|
|
8
|
+
| 层 | 文件 | 说明 |
|
|
9
|
+
|---|---|---|
|
|
10
|
+
| 领域适配 | `schema.mjs` | 唯一需要改的文件:怎么分页、节点在哪、ID/文本/几何怎么取 |
|
|
11
|
+
| 模型 | `@capyclaw/document-workbench/model` | 稳定 ID、清单投影、定位器、编辑计划、校验(已复用,不要改) |
|
|
12
|
+
| 工具 | `tools.mjs` | `{{NS}}_read` / `{{NS}}_edit` / `{{NS}}_validate` 三个 MCP 工具 |
|
|
13
|
+
|
|
14
|
+
## 接入(约 20 行)
|
|
15
|
+
|
|
16
|
+
在你的 workbench 里接上两个 I/O,然后把 capability 加进工具注册表:
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
import { createModelTools } from './model/tools.mjs'
|
|
20
|
+
|
|
21
|
+
const { capability, tools } = createModelTools({
|
|
22
|
+
schema,
|
|
23
|
+
readDoc: () => bridge.getDoc(), // 读当前文档树
|
|
24
|
+
writeDoc: (doc, title) => bridge.push([{ // 编辑器权威桥:一条 setValue
|
|
25
|
+
cmd: 'setValue', args: [doc],
|
|
26
|
+
}], title),
|
|
27
|
+
defineTool, defineCapability,
|
|
28
|
+
ns: '{{NS}}',
|
|
29
|
+
})
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## 用法(模型视角)
|
|
33
|
+
|
|
34
|
+
1. `{{NS}}_read` → 拿到每个元素的 `id / 路径 / 槽位 / 几何 / 文本`
|
|
35
|
+
2. `{{NS}}_edit` → `edits: [{ locate: { page: 1, role: 'title' }, patch: { text: '新标题' } }]`
|
|
36
|
+
3. `{{NS}}_validate` → 重复 ID、空页、越界、超长文本
|
|
37
|
+
|
|
38
|
+
定位器支持 `id / path / page / role / text / type / index`,多条件取交集;
|
|
39
|
+
不唯一时返回候选列表,不猜。
|
|
40
|
+
|
|
41
|
+
## 两条硬约定
|
|
42
|
+
|
|
43
|
+
1. **生成时就写 ID**:用 `slotId('title', pageIndex, index)`,以后移动/改样式都不变。
|
|
44
|
+
没有 ID 的元素只能按路径寻址,重排后就会指错。
|
|
45
|
+
2. **一次读、一次改**:不要边读边改、也不要按截图猜坐标;改完再读一次确认即可。
|
|
46
|
+
|
|
47
|
+
## 测试
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
node model/model.test.mjs
|
|
51
|
+
```
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 文档模型适配({{TITLE}}):唯一需要按领域填写的文件。
|
|
3
|
+
*
|
|
4
|
+
* 模型层只问 6 个问题,回答完就有了「按 ID 精确编辑」的能力:
|
|
5
|
+
* 1. 怎么分页 pages(doc)
|
|
6
|
+
* 2. 每页有哪些节点 nodes(page, pageIndex)
|
|
7
|
+
* 3. 节点 ID 在哪 nodeId(node, index, pageIndex)
|
|
8
|
+
* 4. 节点是什么类型 nodeType(node)
|
|
9
|
+
* 5. 语义槽位(可选) role(node) title/body/card/value/label/foot
|
|
10
|
+
* 6. 几何与文本在哪 box(node) / text(node)
|
|
11
|
+
*
|
|
12
|
+
* 关键约定:**ID 必须在生成时就写进文档**,且后续移动/改样式不变。
|
|
13
|
+
* 生成时用 slotId('<槽位>', pageIndex, index) 即可,例如:
|
|
14
|
+
* { id: slotId('title', i), type: 'text', ... }
|
|
15
|
+
* 没有 ID 的元素只能按路径寻址,一旦重排就会指错。
|
|
16
|
+
*/
|
|
17
|
+
import { slotId } from '@capyclaw/document-workbench/model'
|
|
18
|
+
|
|
19
|
+
export const schema = {
|
|
20
|
+
pages: (doc) => (Array.isArray(doc && doc.pages) ? doc.pages : []),
|
|
21
|
+
pageId: (page, index) => (page && page.id) || `p${index}`,
|
|
22
|
+
nodes: (page) => (page && Array.isArray(page.elements) ? page.elements : []),
|
|
23
|
+
nodeId: (node) => (node && node.id) || null,
|
|
24
|
+
nodeType: (node) => (node && node.type) || 'unknown',
|
|
25
|
+
role: (node) => (node && node.role) || null,
|
|
26
|
+
box: (node) => {
|
|
27
|
+
if (!node) return null
|
|
28
|
+
const { left, top, width, height } = node
|
|
29
|
+
if (![left, top, width, height].every((n) => Number.isFinite(n))) return null
|
|
30
|
+
return { x: left, y: top, w: width, h: height }
|
|
31
|
+
},
|
|
32
|
+
text: (node) => (node && (node.text ?? node.value ?? node.content)) || '',
|
|
33
|
+
pageSize: () => null, // 已知页面尺寸时返回 { w, h },可启用越界校验
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** 空文档形态:写入前的最小骨架。 */
|
|
37
|
+
export function emptyDoc() {
|
|
38
|
+
return { pages: [], rev: 0 }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 示例文档:脚手架自带一份可跑的 fixture,替换成你的真实形态后测试仍然有效。
|
|
43
|
+
* 注意所有元素都带稳定 ID——这是后续能被精确修改的前提。
|
|
44
|
+
*/
|
|
45
|
+
export function sampleDoc() {
|
|
46
|
+
return {
|
|
47
|
+
rev: 1,
|
|
48
|
+
pages: [
|
|
49
|
+
{
|
|
50
|
+
id: 'p0',
|
|
51
|
+
elements: [
|
|
52
|
+
{ id: slotId('title', 0), type: 'text', role: 'title', left: 60, top: 60, width: 880, height: 80, text: '示例标题' },
|
|
53
|
+
{ id: slotId('body', 0), type: 'text', role: 'body', left: 60, top: 170, width: 880, height: 260, text: '示例正文' },
|
|
54
|
+
],
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
id: 'p1',
|
|
58
|
+
elements: [
|
|
59
|
+
{ id: slotId('title', 1), type: 'text', role: 'title', left: 60, top: 60, width: 880, height: 80, text: '第二页' },
|
|
60
|
+
{ id: slotId('value', 1, 0), type: 'text', role: 'value', left: 60, top: 200, width: 240, height: 120, text: '70%' },
|
|
61
|
+
{ id: slotId('value', 1, 1), type: 'text', role: 'value', left: 360, top: 200, width: 240, height: 120, text: '3.5x' },
|
|
62
|
+
],
|
|
63
|
+
},
|
|
64
|
+
],
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** 生成节点时可用的 ID 助手(与 PT 的 lt_stat_value_0_1 同风格)。 */
|
|
69
|
+
export { slotId }
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* {{TITLE}} 模型回归:定位 → 打补丁 → 校验,以及三个工具的端到端链路。
|
|
3
|
+
*
|
|
4
|
+
* 这些测试不需要编辑器/服务:模型层是纯函数,工具通过注入的 readDoc/writeDoc 工作。
|
|
5
|
+
*/
|
|
6
|
+
import test from 'node:test'
|
|
7
|
+
import assert from 'node:assert/strict'
|
|
8
|
+
|
|
9
|
+
import { applyPlan, buildManifest, locate, planEdit, validateManifest } from '@capyclaw/document-workbench/model'
|
|
10
|
+
import { sampleDoc, schema } from './schema.mjs'
|
|
11
|
+
import { createModelTools } from './tools.mjs'
|
|
12
|
+
|
|
13
|
+
const defineTool = (def) => ({ ...def })
|
|
14
|
+
const defineCapability = (def) => ({ ...def })
|
|
15
|
+
|
|
16
|
+
test('清单:每个元素都有稳定 ID 与路径', () => {
|
|
17
|
+
const manifest = buildManifest(sampleDoc(), schema)
|
|
18
|
+
assert.equal(manifest.pages.length, 2)
|
|
19
|
+
assert.ok(manifest.nodes.length >= 4)
|
|
20
|
+
for (const node of manifest.nodes) {
|
|
21
|
+
assert.match(node.id, /^[A-Za-z0-9._:-]+$/, `非法 ID: ${node.id}`)
|
|
22
|
+
assert.match(node.path, /^p\d+\/n\d+/)
|
|
23
|
+
}
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
test('定位:按语义槽位精确命中,且不唯一时报候选', () => {
|
|
27
|
+
const manifest = buildManifest(sampleDoc(), schema)
|
|
28
|
+
const title = locate(manifest, { page: 0, role: 'title' })
|
|
29
|
+
assert.equal(title.page, 0)
|
|
30
|
+
assert.ok(title.text.includes('示例标题'))
|
|
31
|
+
assert.throws(() => locate(manifest, { role: 'value' }), (error) => {
|
|
32
|
+
assert.equal(error.code, 'LOCATOR_AMBIGUOUS')
|
|
33
|
+
assert.equal(error.candidates.length, 2)
|
|
34
|
+
return true
|
|
35
|
+
})
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
test('编辑:定位 + 补丁 → 纯函数改写,原文档不变', () => {
|
|
39
|
+
const doc = sampleDoc()
|
|
40
|
+
const manifest = buildManifest(doc, schema)
|
|
41
|
+
const plan = planEdit(manifest, [
|
|
42
|
+
{ locate: { page: 1, role: 'title' }, patch: { text: '改过的标题' } },
|
|
43
|
+
{ locate: { page: 1, role: 'value' }, patch: { style: { color: '#e3b23c' } } },
|
|
44
|
+
].map((edit) => (edit.locate.role === 'value' ? { ...edit, locate: { page: 1, role: 'value', index: 1 } } : edit)))
|
|
45
|
+
const { doc: next, applied } = applyPlan(doc, plan, schema)
|
|
46
|
+
assert.equal(applied.length, 2)
|
|
47
|
+
assert.equal(next.pages[1].elements[0].text, '改过的标题')
|
|
48
|
+
assert.equal(next.pages[1].elements[1].color, '#e3b23c')
|
|
49
|
+
assert.equal(doc.pages[1].elements[0].text, '第二页', '原文档不能被就地修改')
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
test('工具端到端:{{NS}}_read → {{NS}}_edit → {{NS}}_validate', async () => {
|
|
53
|
+
let doc = sampleDoc()
|
|
54
|
+
const { tools } = createModelTools({
|
|
55
|
+
schema,
|
|
56
|
+
defineTool,
|
|
57
|
+
defineCapability,
|
|
58
|
+
readDoc: () => doc,
|
|
59
|
+
writeDoc: (next) => { doc = next },
|
|
60
|
+
ns: '{{NS}}',
|
|
61
|
+
})
|
|
62
|
+
const byName = Object.fromEntries(tools.map((tool) => [tool.name, tool]))
|
|
63
|
+
assert.deepEqual(Object.keys(byName).sort(), ['{{NS}}_edit', '{{NS}}_read', '{{NS}}_validate'])
|
|
64
|
+
|
|
65
|
+
const read = await byName['{{NS}}_read'].handler({})
|
|
66
|
+
assert.match(read.content[0].text, /第 1 页/)
|
|
67
|
+
assert.match(read.content[0].text, /title_0/)
|
|
68
|
+
|
|
69
|
+
const edited = await byName['{{NS}}_edit'].handler({
|
|
70
|
+
edits: [{ locate: { page: 1, role: 'title' }, patch: { text: '工具改的标题' } }],
|
|
71
|
+
})
|
|
72
|
+
assert.match(edited.content[0].text, /已修改 1 个元素/)
|
|
73
|
+
assert.equal(doc.pages[1].elements[0].text, '工具改的标题')
|
|
74
|
+
|
|
75
|
+
const validated = await byName['{{NS}}_validate'].handler({})
|
|
76
|
+
assert.equal(validated.isError, false)
|
|
77
|
+
assert.match(validated.content[0].text, /校验通过/)
|
|
78
|
+
|
|
79
|
+
const miss = await byName['{{NS}}_edit'].handler({
|
|
80
|
+
edits: [{ locate: { text: '不存在的文本' }, patch: { text: 'x' } }],
|
|
81
|
+
})
|
|
82
|
+
assert.equal(miss.isError, true)
|
|
83
|
+
assert.match(miss.content[0].text, /定位失败/)
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
test('校验能抓出重复 ID(最常见的生成缺陷)', () => {
|
|
87
|
+
const doc = sampleDoc()
|
|
88
|
+
doc.pages[1].elements[1].id = doc.pages[1].elements[0].id
|
|
89
|
+
const result = validateManifest(buildManifest(doc, schema))
|
|
90
|
+
assert.equal(result.ok, false)
|
|
91
|
+
assert.ok(result.issues.some((issue) => issue.code === 'ID_DUPLICATE'))
|
|
92
|
+
})
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 文档模型工具({{TITLE}}):读清单 → 按定位器精确改 → 校验,三件套。
|
|
3
|
+
*
|
|
4
|
+
* 特点(这就是 PPT 那条快路径的通用形态):
|
|
5
|
+
* - 读一次拿到每个元素的稳定 ID / 路径 / 语义槽位 / 几何 / 文本;
|
|
6
|
+
* - 改的时候只给「定位器 + 补丁」,模型不需要截图、不需要猜坐标;
|
|
7
|
+
* - 找不到或不唯一会明确报错并给候选,不会静默改错元素。
|
|
8
|
+
*
|
|
9
|
+
* 宿主只需注入两个 I/O:
|
|
10
|
+
* readDoc() → 当前文档(领域树)
|
|
11
|
+
* writeDoc(doc, title) → 把整棵树写回(编辑器权威桥里通常是 setValue 一条命令)
|
|
12
|
+
*/
|
|
13
|
+
import {
|
|
14
|
+
applyPlan, buildManifest, describeIssues, locateAll, planEdit, validateManifest,
|
|
15
|
+
} from '@capyclaw/document-workbench/model'
|
|
16
|
+
import { schema as defaultSchema } from './schema.mjs'
|
|
17
|
+
|
|
18
|
+
const NS = '{{NS}}'
|
|
19
|
+
|
|
20
|
+
function manifestText(manifest) {
|
|
21
|
+
const lines = []
|
|
22
|
+
for (const page of manifest.pages) {
|
|
23
|
+
lines.push(`# 第 ${page.index + 1} 页${page.title ? ' · ' + page.title : ''}(${page.count} 个元素)`)
|
|
24
|
+
for (const node of manifest.nodes.filter((n) => n.page === page.index)) {
|
|
25
|
+
const box = node.box ? ` @(${Math.round(node.box.x)},${Math.round(node.box.y)}) ${Math.round(node.box.w)}×${Math.round(node.box.h)}` : ''
|
|
26
|
+
const text = node.text ? ` 「${node.text.slice(0, 40)}${node.text.length > 40 ? '…' : ''}」` : ''
|
|
27
|
+
lines.push(`- p${node.page}/n${node.index} ${node.id} · ${node.role || node.type}${box}${text}`)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return lines.join('\n')
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @param {{
|
|
35
|
+
* schema?: object,
|
|
36
|
+
* readDoc: () => Promise<object>|object,
|
|
37
|
+
* writeDoc: (doc: object, title?: string) => Promise<any>,
|
|
38
|
+
* defineTool: Function,
|
|
39
|
+
* defineCapability: Function,
|
|
40
|
+
* ns?: string,
|
|
41
|
+
* }} options
|
|
42
|
+
*/
|
|
43
|
+
export function createModelTools(options = {}) {
|
|
44
|
+
const {
|
|
45
|
+
schema = defaultSchema,
|
|
46
|
+
readDoc,
|
|
47
|
+
writeDoc,
|
|
48
|
+
defineTool,
|
|
49
|
+
defineCapability,
|
|
50
|
+
ns = NS,
|
|
51
|
+
} = options
|
|
52
|
+
if (typeof readDoc !== 'function') throw new Error('createModelTools 需要 readDoc')
|
|
53
|
+
if (typeof writeDoc !== 'function') throw new Error('createModelTools 需要 writeDoc')
|
|
54
|
+
|
|
55
|
+
const manifestOf = async () => buildManifest(await readDoc(), schema)
|
|
56
|
+
|
|
57
|
+
const readTool = defineTool({
|
|
58
|
+
name: `${ns}_read`,
|
|
59
|
+
description: '读取文档清单:每个元素的稳定 ID、路径、语义槽位、几何与文本。改文档前先读一次,后续按 ID 精确定位。',
|
|
60
|
+
inputSchema: {
|
|
61
|
+
type: 'object',
|
|
62
|
+
properties: {
|
|
63
|
+
mode: { type: 'string', enum: ['manifest', 'json', 'node'], description: 'manifest(默认,模型可读文本)/ json(原始清单)/ node(单个元素)' },
|
|
64
|
+
locate: { type: 'object', description: 'mode=node 时的定位器:{ id | path | page | role | text | type | index }' },
|
|
65
|
+
page: { type: 'number', description: '只读某一页(0 起)' },
|
|
66
|
+
},
|
|
67
|
+
additionalProperties: false,
|
|
68
|
+
},
|
|
69
|
+
kind: 'read',
|
|
70
|
+
handler: async (args = {}) => {
|
|
71
|
+
const manifest = await manifestOf()
|
|
72
|
+
if (args.mode === 'node') {
|
|
73
|
+
const hits = locateAll(manifest, args.locate || {})
|
|
74
|
+
if (!hits.length) return { content: [{ type: 'text', text: '没有匹配的元素' }] }
|
|
75
|
+
return { content: [{ type: 'text', text: JSON.stringify(hits, null, 2) }] }
|
|
76
|
+
}
|
|
77
|
+
if (args.mode === 'json') {
|
|
78
|
+
const view = Number.isInteger(args.page)
|
|
79
|
+
? { ...manifest, nodes: manifest.nodes.filter((n) => n.page === args.page) }
|
|
80
|
+
: manifest
|
|
81
|
+
return { content: [{ type: 'text', text: JSON.stringify(view, null, 2) }] }
|
|
82
|
+
}
|
|
83
|
+
const scoped = Number.isInteger(args.page)
|
|
84
|
+
? { ...manifest, pages: manifest.pages.filter((p) => p.index === args.page), nodes: manifest.nodes.filter((n) => n.page === args.page) }
|
|
85
|
+
: manifest
|
|
86
|
+
return { content: [{ type: 'text', text: manifestText(scoped) }] }
|
|
87
|
+
},
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
const editTool = defineTool({
|
|
91
|
+
name: `${ns}_edit`,
|
|
92
|
+
description: '按定位器修改元素:edits = [{ locate, patch }]。locate 支持 id/path/page/role/text/type/index;patch 支持 text / box{x,y,w,h} / style / attrs。定位不唯一会返回候选。',
|
|
93
|
+
inputSchema: {
|
|
94
|
+
type: 'object',
|
|
95
|
+
properties: {
|
|
96
|
+
edits: {
|
|
97
|
+
type: 'array',
|
|
98
|
+
items: {
|
|
99
|
+
type: 'object',
|
|
100
|
+
properties: {
|
|
101
|
+
locate: { type: 'object', description: '定位器' },
|
|
102
|
+
patch: { type: 'object', description: '要改的字段:text / box / style / attrs' },
|
|
103
|
+
op: { type: 'string', enum: ['update', 'remove', 'insert', 'move'] },
|
|
104
|
+
page: { type: 'number' },
|
|
105
|
+
index: { type: 'number' },
|
|
106
|
+
},
|
|
107
|
+
required: ['locate'],
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
title: { type: 'string', description: '批次说明(进 undo 历史)' },
|
|
111
|
+
},
|
|
112
|
+
required: ['edits'],
|
|
113
|
+
additionalProperties: false,
|
|
114
|
+
},
|
|
115
|
+
kind: 'write',
|
|
116
|
+
handler: async (args = {}) => {
|
|
117
|
+
const doc = await readDoc()
|
|
118
|
+
const manifest = buildManifest(doc, schema)
|
|
119
|
+
let plan
|
|
120
|
+
try {
|
|
121
|
+
plan = planEdit(manifest, args.edits || [])
|
|
122
|
+
} catch (error) {
|
|
123
|
+
return { content: [{ type: 'text', text: `定位失败:${error.message}` }], isError: true }
|
|
124
|
+
}
|
|
125
|
+
const { doc: next, applied, skipped } = applyPlan(doc, plan, schema)
|
|
126
|
+
if (!applied.length) {
|
|
127
|
+
return { content: [{ type: 'text', text: `没有可应用的修改(${skipped.map((s) => s.reason).join(',') || '空计划'})` }], isError: true }
|
|
128
|
+
}
|
|
129
|
+
await writeDoc(next, args.title || '模型编辑')
|
|
130
|
+
const who = applied.map((op) => `${op.target ? op.target.id : 'insert@' + op.page}`).join('、')
|
|
131
|
+
return {
|
|
132
|
+
content: [{
|
|
133
|
+
type: 'text',
|
|
134
|
+
text: `已修改 ${applied.length} 个元素:${who}${skipped.length ? `(跳过 ${skipped.length})` : ''}`,
|
|
135
|
+
}],
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
const validateTool = defineTool({
|
|
141
|
+
name: `${ns}_validate`,
|
|
142
|
+
description: '校验文档模型:重复/缺失 ID、空页、越界、超长文本。写入后建议跑一次。',
|
|
143
|
+
inputSchema: {
|
|
144
|
+
type: 'object',
|
|
145
|
+
properties: {
|
|
146
|
+
maxChars: { type: 'number', description: '单元素文本上限(默认不检查)' },
|
|
147
|
+
},
|
|
148
|
+
additionalProperties: false,
|
|
149
|
+
},
|
|
150
|
+
kind: 'read',
|
|
151
|
+
handler: async (args = {}) => {
|
|
152
|
+
const manifest = await manifestOf()
|
|
153
|
+
const pageSize = typeof schema.pageSize === 'function' ? schema.pageSize : undefined
|
|
154
|
+
const result = validateManifest(manifest, { maxChars: args.maxChars, pageSize })
|
|
155
|
+
return { content: [{ type: 'text', text: describeIssues(result) }], isError: !result.ok }
|
|
156
|
+
},
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
const capability = defineCapability({
|
|
160
|
+
id: `${ns}-model`,
|
|
161
|
+
title: '{{TITLE}} · 文档模型',
|
|
162
|
+
description: '结构化读 + 稳定 ID 寻址 + 按 ID 精确修改 + 模型校验。',
|
|
163
|
+
tags: ['content'],
|
|
164
|
+
features: ['doc.read', 'doc.write', 'doc.validate'],
|
|
165
|
+
tools: [readTool, editTool, validateTool],
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
return { capability, tools: [readTool, editTool, validateTool] }
|
|
169
|
+
}
|
|
@@ -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
|
+
* 宿主(端口/锁/复用/健康/指标/静态/关闭)由 @capyclaw/service-host 提供;
|
|
5
|
+
* 工具面(能力 DSL/注册表/校验/契约/MCP)由 @capyclaw/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
|
+
}
|