@capyclaw/browser-suite 0.1.2 → 0.2.37
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/LICENSE +21 -0
- package/README.md +9 -5
- package/composition.lock.json +37 -25
- package/contract/browser-contract.json +484 -25
- package/cordis.patch.yml +3 -9
- package/dsh-suite.json +110 -82
- package/lib/index.mjs +1 -1
- package/node_modules/@capyclaw/asset-service/README.md +48 -0
- package/node_modules/@capyclaw/asset-service/cordis.patch.yml +4 -0
- package/node_modules/@capyclaw/asset-service/dsh-suite.json +29 -0
- package/node_modules/@capyclaw/asset-service/lib/host.mjs +14 -0
- package/node_modules/@capyclaw/asset-service/package.json +59 -0
- package/node_modules/@capyclaw/asset-service/src/index.mjs +304 -0
- package/node_modules/@capyclaw/browser-skill-api/README.md +3 -0
- package/node_modules/@capyclaw/browser-skill-api/contract/browser-contract.json +1349 -0
- package/node_modules/@capyclaw/browser-skill-api/cordis.patch.yml +4 -0
- package/node_modules/@capyclaw/browser-skill-api/package.json +49 -0
- package/node_modules/@capyclaw/browser-skill-api/plugin.mjs +15 -0
- package/node_modules/@capyclaw/browser-skill-api/skills/browser-api/SKILL.md +85 -0
- package/node_modules/@capyclaw/browser-skill-api/skills/browser-api/references/tool-contract.md +692 -0
- package/node_modules/@capyclaw/browser-skill-browser/README.md +3 -0
- package/node_modules/@capyclaw/browser-skill-browser/contract/browser-contract.json +1349 -0
- package/node_modules/@capyclaw/browser-skill-browser/cordis.patch.yml +4 -0
- package/node_modules/@capyclaw/browser-skill-browser/package.json +49 -0
- package/node_modules/@capyclaw/browser-skill-browser/plugin.mjs +15 -0
- package/node_modules/@capyclaw/browser-skill-browser/skills/browser/SKILL.md +27 -0
- package/node_modules/@capyclaw/browser-skill-ops/README.md +3 -0
- package/node_modules/@capyclaw/browser-skill-ops/contract/browser-contract.json +1349 -0
- package/node_modules/@capyclaw/browser-skill-ops/cordis.patch.yml +4 -0
- package/node_modules/@capyclaw/browser-skill-ops/package.json +49 -0
- package/node_modules/@capyclaw/browser-skill-ops/plugin.mjs +15 -0
- package/node_modules/@capyclaw/browser-skill-ops/skills/browser-ops/SKILL.md +63 -0
- package/node_modules/@capyclaw/browser-workbench/README.md +89 -0
- package/node_modules/@capyclaw/browser-workbench/contract/browser-contract.json +1349 -0
- package/node_modules/@capyclaw/browser-workbench/demo/form.html +112 -0
- package/node_modules/@capyclaw/browser-workbench/lib/host.mjs +1 -0
- package/node_modules/@capyclaw/browser-workbench/package.json +71 -0
- package/node_modules/@capyclaw/browser-workbench/plugin.mjs +1 -0
- package/node_modules/@capyclaw/browser-workbench/server/constants.mjs +16 -0
- package/node_modules/@capyclaw/browser-workbench/server/contract-snapshots.mjs +81 -0
- package/node_modules/@capyclaw/browser-workbench/server/contract.mjs +24 -0
- package/node_modules/@capyclaw/browser-workbench/server/embedded-host.mjs +247 -0
- package/node_modules/@capyclaw/browser-workbench/server/engine.mjs +2466 -0
- package/node_modules/@capyclaw/browser-workbench/server/governor.mjs +189 -0
- package/node_modules/@capyclaw/browser-workbench/server/http-utils.mjs +2 -0
- package/node_modules/@capyclaw/browser-workbench/server/human.mjs +628 -0
- package/node_modules/@capyclaw/browser-workbench/server/infra/http.mjs +2 -0
- package/node_modules/@capyclaw/browser-workbench/server/infra/tuning.mjs +2 -0
- package/node_modules/@capyclaw/browser-workbench/server/input.mjs +154 -0
- package/node_modules/@capyclaw/browser-workbench/server/login-vault.mjs +109 -0
- package/node_modules/@capyclaw/browser-workbench/server/mcp/handler.mjs +2 -0
- package/node_modules/@capyclaw/browser-workbench/server/mcp/instructions.mjs +31 -0
- package/node_modules/@capyclaw/browser-workbench/server/orphans.mjs +163 -0
- package/node_modules/@capyclaw/browser-workbench/server/relay.mjs +198 -0
- package/node_modules/@capyclaw/browser-workbench/server/routes/api.mjs +233 -0
- package/node_modules/@capyclaw/browser-workbench/server/routes/assets.mjs +2 -0
- package/node_modules/@capyclaw/browser-workbench/server/routes/mcp.mjs +17 -0
- package/node_modules/@capyclaw/browser-workbench/server/routes/workbench.mjs +33 -0
- package/node_modules/@capyclaw/browser-workbench/server/session-engines.mjs +223 -0
- package/node_modules/@capyclaw/browser-workbench/server/shared/SHARED_MANIFEST.json +70 -0
- package/node_modules/@capyclaw/browser-workbench/server/shared/asset-service.mjs +2 -0
- package/node_modules/@capyclaw/browser-workbench/server/shared/editor-host.mjs +2 -0
- package/node_modules/@capyclaw/browser-workbench/server/shared/kit/editor/lease.mjs +2 -0
- package/node_modules/@capyclaw/browser-workbench/server/shared/kit/service/api.mjs +2 -0
- package/node_modules/@capyclaw/browser-workbench/server/shared/kit/service/http.mjs +2 -0
- package/node_modules/@capyclaw/browser-workbench/server/shared/kit/service/mcp-http.mjs +2 -0
- package/node_modules/@capyclaw/browser-workbench/server/shared/kit/tools/validate.mjs +2 -0
- package/node_modules/@capyclaw/browser-workbench/server/shared/kit/tuning.mjs +2 -0
- package/node_modules/@capyclaw/browser-workbench/server/shared/kit/util/fs-lock.mjs +2 -0
- package/node_modules/@capyclaw/browser-workbench/server/shared/service-host.mjs +2 -0
- package/node_modules/@capyclaw/browser-workbench/server/shared/tool-host.mjs +2 -0
- package/node_modules/@capyclaw/browser-workbench/server/shared/tuning.mjs +2 -0
- package/node_modules/@capyclaw/browser-workbench/server/tools/capabilities/contract.mjs +55 -0
- package/node_modules/@capyclaw/browser-workbench/server/tools/capabilities/evaluate.mjs +36 -0
- package/node_modules/@capyclaw/browser-workbench/server/tools/capabilities/index.mjs +26 -0
- package/node_modules/@capyclaw/browser-workbench/server/tools/capabilities/interaction.mjs +215 -0
- package/node_modules/@capyclaw/browser-workbench/server/tools/capabilities/navigation.mjs +35 -0
- package/node_modules/@capyclaw/browser-workbench/server/tools/capabilities/screenshot.mjs +50 -0
- package/node_modules/@capyclaw/browser-workbench/server/tools/capabilities/session.mjs +81 -0
- package/node_modules/@capyclaw/browser-workbench/server/tools/capabilities/snapshot.mjs +30 -0
- package/node_modules/@capyclaw/browser-workbench/server/tools/capabilities/tabs.mjs +35 -0
- package/node_modules/@capyclaw/browser-workbench/server/tools/index.mjs +12 -0
- package/node_modules/@capyclaw/browser-workbench/server/tools/kernel/define.mjs +5 -0
- package/node_modules/@capyclaw/browser-workbench/server/tools/kernel/output.mjs +30 -0
- package/node_modules/@capyclaw/browser-workbench/server/tools/kernel/ports.mjs +59 -0
- package/node_modules/@capyclaw/browser-workbench/server/tools/kernel/registry.mjs +17 -0
- package/node_modules/@capyclaw/browser-workbench/server/tools.mjs +3 -0
- package/node_modules/@capyclaw/browser-workbench/server/workbench-framework.mjs +305 -0
- package/node_modules/@capyclaw/browser-workbench/ui/app.js +447 -0
- package/node_modules/@capyclaw/browser-workbench/ui/index.html +46 -0
- package/node_modules/@capyclaw/browser-workbench/ui/style.css +144 -0
- package/node_modules/@capyclaw/document-workbench/README.md +125 -0
- package/node_modules/@capyclaw/document-workbench/package.json +44 -0
- package/node_modules/@capyclaw/document-workbench/src/bridge/index.mjs +321 -0
- package/node_modules/@capyclaw/document-workbench/src/client/index.mjs +625 -0
- package/node_modules/@capyclaw/document-workbench/src/client/screenshot.mjs +98 -0
- package/node_modules/@capyclaw/document-workbench/src/index.mjs +9 -0
- package/node_modules/@capyclaw/document-workbench/src/model/audit.mjs +101 -0
- package/node_modules/@capyclaw/document-workbench/src/model/conformance.mjs +92 -0
- package/node_modules/@capyclaw/document-workbench/src/model/ids.mjs +56 -0
- package/node_modules/@capyclaw/document-workbench/src/model/index.mjs +18 -0
- package/node_modules/@capyclaw/document-workbench/src/model/locate.mjs +118 -0
- package/node_modules/@capyclaw/document-workbench/src/model/manifest.mjs +148 -0
- package/node_modules/@capyclaw/document-workbench/src/model/ops.mjs +160 -0
- package/node_modules/@capyclaw/document-workbench/src/model/validate.mjs +72 -0
- package/node_modules/@capyclaw/document-workbench/src/ops/index.mjs +93 -0
- package/node_modules/@capyclaw/document-workbench/src/session/context.mjs +103 -0
- package/node_modules/@capyclaw/document-workbench/src/session/index.mjs +394 -0
- package/node_modules/@capyclaw/document-workbench/src/session/lease.mjs +105 -0
- package/node_modules/@capyclaw/document-workbench/src/session/mode.mjs +80 -0
- package/node_modules/@capyclaw/document-workbench/src/session/sanitize.mjs +107 -0
- package/node_modules/@capyclaw/document-workbench/src/session/state.mjs +159 -0
- package/node_modules/@capyclaw/document-workbench/src/session/watch.mjs +63 -0
- package/node_modules/@capyclaw/document-workbench/src/store/file-backend.mjs +158 -0
- package/node_modules/@capyclaw/document-workbench/src/testing/index.mjs +75 -0
- package/node_modules/@capyclaw/foundation/README.md +51 -0
- package/node_modules/@capyclaw/foundation/abi/bridge.mjs +86 -0
- package/node_modules/@capyclaw/foundation/abi/errors.mjs +63 -0
- package/node_modules/@capyclaw/foundation/abi/index.mjs +35 -0
- package/node_modules/@capyclaw/foundation/abi/kinds.mjs +271 -0
- package/node_modules/@capyclaw/foundation/abi/loader.mjs +345 -0
- package/node_modules/@capyclaw/foundation/abi/schema.mjs +98 -0
- package/node_modules/@capyclaw/foundation/abi/spec.mjs +255 -0
- package/node_modules/@capyclaw/foundation/abi/validate.mjs +24 -0
- package/node_modules/@capyclaw/foundation/abi/version.mjs +82 -0
- package/node_modules/@capyclaw/foundation/cordis.patch.yml +5 -0
- package/node_modules/@capyclaw/foundation/kit/index.mjs +6 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/client-panel.mjs +98 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/host-event.mjs +35 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/http-router.mjs +69 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +55 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/own-server.mjs +113 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +69 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +120 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/service-provider.mjs +77 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/skill-provider.mjs +56 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/tool-provider.mjs +50 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/ui-panel.mjs +64 -0
- package/node_modules/@capyclaw/foundation/lib/ctx-service.mjs +42 -0
- package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +34 -0
- package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +85 -0
- package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +50 -0
- package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +160 -0
- package/node_modules/@capyclaw/foundation/lib/host.mjs +23 -0
- package/node_modules/@capyclaw/foundation/lib/kernel.mjs +185 -0
- package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +115 -0
- package/node_modules/@capyclaw/foundation/lib/subsystems/events.mjs +60 -0
- package/node_modules/@capyclaw/foundation/lib/subsystems/log.mjs +51 -0
- package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +188 -0
- package/node_modules/@capyclaw/foundation/package.json +53 -0
- package/node_modules/@capyclaw/foundation/runtime/index.mjs +20 -0
- package/node_modules/@capyclaw/plugin-kit/INTEGRATION.md +100 -0
- package/node_modules/@capyclaw/plugin-kit/README.md +70 -0
- package/node_modules/@capyclaw/plugin-kit/bin/sync.mjs +103 -0
- package/node_modules/@capyclaw/plugin-kit/package.json +69 -0
- package/node_modules/@capyclaw/plugin-kit/src/cli.mjs +56 -0
- package/node_modules/@capyclaw/plugin-kit/src/config.mjs +142 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +129 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +139 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/shadow.mjs +219 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/transport.js +13 -0
- package/node_modules/@capyclaw/plugin-kit/src/index.mjs +13 -0
- package/node_modules/@capyclaw/plugin-kit/src/pack.mjs +230 -0
- package/node_modules/@capyclaw/plugin-kit/src/runtime/context.mjs +128 -0
- package/node_modules/@capyclaw/plugin-kit/src/self-contained.mjs +307 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/api.mjs +45 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/http.mjs +113 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/mcp-http.mjs +150 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/server.mjs +131 -0
- package/node_modules/@capyclaw/plugin-kit/src/skills/provider.mjs +82 -0
- package/node_modules/@capyclaw/plugin-kit/src/store/blob-store.mjs +109 -0
- package/node_modules/@capyclaw/plugin-kit/src/store/content-store.mjs +17 -0
- package/node_modules/@capyclaw/plugin-kit/src/store/journal-store.mjs +89 -0
- package/node_modules/@capyclaw/plugin-kit/src/tools/result.mjs +57 -0
- package/node_modules/@capyclaw/plugin-kit/src/tools/validate.mjs +150 -0
- package/node_modules/@capyclaw/plugin-kit/src/tuning.mjs +82 -0
- package/node_modules/@capyclaw/plugin-kit/src/util/fs-lock.mjs +132 -0
- package/node_modules/@capyclaw/plugin-kit/src/util/http.mjs +130 -0
- package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +656 -0
- package/node_modules/@capyclaw/plugin-kit/src/zip.mjs +181 -0
- package/node_modules/@capyclaw/portal/README.md +70 -0
- package/node_modules/@capyclaw/portal/lib/index.mjs +387 -0
- package/node_modules/@capyclaw/portal/lib/ledger.mjs +194 -0
- package/node_modules/@capyclaw/portal/lib/portal-agent.ps1 +811 -0
- package/node_modules/@capyclaw/portal/lib/portal-darwin.mjs +183 -0
- package/node_modules/@capyclaw/portal/package.json +40 -0
- package/node_modules/@capyclaw/service-broker/README.md +104 -0
- package/node_modules/@capyclaw/service-broker/cordis.patch.yml +8 -0
- package/node_modules/@capyclaw/service-broker/dsh-suite.json +21 -0
- package/node_modules/@capyclaw/service-broker/lib/control.mjs +193 -0
- package/node_modules/@capyclaw/service-broker/lib/discovery.mjs +109 -0
- package/node_modules/@capyclaw/service-broker/lib/engine.mjs +1236 -0
- package/node_modules/@capyclaw/service-broker/lib/host.mjs +182 -0
- package/node_modules/@capyclaw/service-broker/lib/manifest.mjs +249 -0
- package/node_modules/@capyclaw/service-broker/lib/recovery.mjs +325 -0
- package/node_modules/@capyclaw/service-broker/lib/skills-client.mjs +28 -0
- package/node_modules/@capyclaw/service-broker/lib/spec.mjs +78 -0
- package/node_modules/@capyclaw/service-broker/package.json +65 -0
- package/node_modules/@capyclaw/service-broker/server.mjs +81 -0
- package/node_modules/@capyclaw/service-broker/service.schema.json +62 -0
- package/node_modules/@capyclaw/service-broker/services.json +3 -0
- package/node_modules/@capyclaw/service-host/README.md +86 -0
- package/node_modules/@capyclaw/service-host/cordis.patch.yml +4 -0
- package/node_modules/@capyclaw/service-host/dsh-suite.json +29 -0
- package/node_modules/@capyclaw/service-host/lib/host.mjs +22 -0
- package/node_modules/@capyclaw/service-host/package.json +60 -0
- package/node_modules/@capyclaw/service-host/src/index.mjs +571 -0
- package/node_modules/@capyclaw/service-host/src/tuning.mjs +63 -0
- package/node_modules/@capyclaw/tool-host/README.md +56 -0
- package/node_modules/@capyclaw/tool-host/cordis.patch.yml +4 -0
- package/node_modules/@capyclaw/tool-host/dsh-suite.json +30 -0
- package/node_modules/@capyclaw/tool-host/lib/host.mjs +16 -0
- package/node_modules/@capyclaw/tool-host/package.json +59 -0
- package/node_modules/@capyclaw/tool-host/src/descriptors.mjs +166 -0
- package/node_modules/@capyclaw/tool-host/src/index.mjs +416 -0
- package/package.json +31 -14
- package/services.json +1 -21
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 编辑计划:定位器 + 补丁 → 规范化 op 列表(id/path 都已解析)。
|
|
3
|
+
*
|
|
4
|
+
* 分层原则:
|
|
5
|
+
* - 本模块只产出「要改哪个节点、改什么」,不假设引擎命令名;
|
|
6
|
+
* - 插件可以拿 plan 去映射自己的 exec 命令(setValue / updateById / patchElement…),
|
|
7
|
+
* 也可以直接用 applyPlan 做纯树改写(工具侧无引擎也能跑,便于测试)。
|
|
8
|
+
*/
|
|
9
|
+
import { locate } from './locate.mjs'
|
|
10
|
+
|
|
11
|
+
const isObj = (value) => !!value && typeof value === 'object' && !Array.isArray(value)
|
|
12
|
+
|
|
13
|
+
function parsePath(path) {
|
|
14
|
+
const m = /^p(\d+)\/n(\d+)((?:\/c\d+)*)$/.exec(String(path || ''))
|
|
15
|
+
if (!m) return null
|
|
16
|
+
const children = (m[3] || '').split('/').filter(Boolean).map((seg) => Number(seg.slice(1)))
|
|
17
|
+
return { page: Number(m[1]), node: Number(m[2]), children }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function targetOf(manifest, locator) {
|
|
21
|
+
const hit = locate(manifest, locator)
|
|
22
|
+
return { id: hit.id, path: hit.path, page: hit.page, index: hit.index, role: hit.role, type: hit.type }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {object} manifest buildManifest 的结果
|
|
27
|
+
* @param {Array<{
|
|
28
|
+
* op?: 'update'|'remove'|'move'|'insert',
|
|
29
|
+
* locate?: object|string,
|
|
30
|
+
* patch?: object,
|
|
31
|
+
* page?: number,
|
|
32
|
+
* index?: number,
|
|
33
|
+
* node?: object,
|
|
34
|
+
* }>} edits
|
|
35
|
+
*/
|
|
36
|
+
export function planEdit(manifest, edits) {
|
|
37
|
+
const list = Array.isArray(edits) ? edits : [edits]
|
|
38
|
+
return list.map((edit, i) => {
|
|
39
|
+
if (!isObj(edit)) throw new Error(`第 ${i} 条编辑不是对象`)
|
|
40
|
+
const op = edit.op || 'update'
|
|
41
|
+
if (op === 'insert') {
|
|
42
|
+
if (!Number.isInteger(edit.page)) throw new Error(`第 ${i} 条 insert 缺少 page`)
|
|
43
|
+
return { op, page: edit.page, index: Number.isInteger(edit.index) ? edit.index : null, node: edit.node || {} }
|
|
44
|
+
}
|
|
45
|
+
const target = targetOf(manifest, edit.locate ?? edit.target ?? edit.id)
|
|
46
|
+
if (op === 'update') {
|
|
47
|
+
if (!isObj(edit.patch) || !Object.keys(edit.patch).length) throw new Error(`第 ${i} 条 update 缺少 patch`)
|
|
48
|
+
return { op, target, patch: edit.patch }
|
|
49
|
+
}
|
|
50
|
+
if (op === 'remove') return { op, target }
|
|
51
|
+
if (op === 'move') {
|
|
52
|
+
if (!Number.isInteger(edit.page)) throw new Error(`第 ${i} 条 move 缺少目标 page`)
|
|
53
|
+
return { op, target, page: edit.page, index: Number.isInteger(edit.index) ? edit.index : null }
|
|
54
|
+
}
|
|
55
|
+
throw new Error(`第 ${i} 条编辑的 op 不支持:${op}`)
|
|
56
|
+
})
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function listAt(doc, schema, path) {
|
|
60
|
+
const parsed = parsePath(path)
|
|
61
|
+
if (!parsed) return null
|
|
62
|
+
const pagesOf = schema.pages || ((d) => (Array.isArray(d?.pages) ? d.pages : Array.isArray(d?.slides) ? d.slides : d?.deck?.slides || [d]))
|
|
63
|
+
const nodesOf = schema.nodes || ((p) => (Array.isArray(p?.elements) ? p.elements : Array.isArray(p?.children) ? p.children : []))
|
|
64
|
+
const childrenOf = typeof schema.children === 'function' ? schema.children : () => null
|
|
65
|
+
const page = pagesOf(doc)?.[parsed.page]
|
|
66
|
+
if (!page) return null
|
|
67
|
+
let list = nodesOf(page, parsed.page)
|
|
68
|
+
let node = list?.[parsed.node]
|
|
69
|
+
for (const childIndex of parsed.children) {
|
|
70
|
+
if (!node) return null
|
|
71
|
+
list = childrenOf(node)
|
|
72
|
+
node = list?.[childIndex]
|
|
73
|
+
}
|
|
74
|
+
return { list, node, page, parsed }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function setText(node, text, schema) {
|
|
78
|
+
if (typeof schema.setText === 'function') {
|
|
79
|
+
schema.setText(node, text)
|
|
80
|
+
return
|
|
81
|
+
}
|
|
82
|
+
if ('value' in node && !('content' in node)) node.value = text
|
|
83
|
+
else if ('content' in node) node.content = text
|
|
84
|
+
else node.text = text
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function setBox(node, box) {
|
|
88
|
+
if (!isObj(box)) return
|
|
89
|
+
const { x, y, w, h } = box
|
|
90
|
+
if ('left' in node || 'top' in node) {
|
|
91
|
+
if (Number.isFinite(x)) node.left = x
|
|
92
|
+
if (Number.isFinite(y)) node.top = y
|
|
93
|
+
if (Number.isFinite(w)) node.width = w
|
|
94
|
+
if (Number.isFinite(h)) node.height = h
|
|
95
|
+
return
|
|
96
|
+
}
|
|
97
|
+
if (Number.isFinite(x)) node.x = x
|
|
98
|
+
if (Number.isFinite(y)) node.y = y
|
|
99
|
+
if (Number.isFinite(w)) node.w = w
|
|
100
|
+
if (Number.isFinite(h)) node.h = h
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function applyPatch(node, patch, schema) {
|
|
104
|
+
if (patch.text !== undefined) setText(node, patch.text, schema)
|
|
105
|
+
if (patch.box !== undefined) setBox(node, patch.box)
|
|
106
|
+
if (isObj(patch.style)) Object.assign(node, patch.style)
|
|
107
|
+
if (isObj(patch.attrs)) Object.assign(node, patch.attrs)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* 纯树改写:不抛“找不到”(默认记 skipped),便于批量编辑一次返回全部问题。
|
|
112
|
+
* @returns {{ doc: object, applied: Array, skipped: Array }}
|
|
113
|
+
*/
|
|
114
|
+
export function applyPlan(doc, plan, schema = {}) {
|
|
115
|
+
const next = structuredClone(doc)
|
|
116
|
+
const applied = []
|
|
117
|
+
const skipped = []
|
|
118
|
+
for (const op of plan) {
|
|
119
|
+
if (op.op === 'insert') {
|
|
120
|
+
const pagesOf = schema.pages || ((d) => (Array.isArray(d?.pages) ? d.pages : Array.isArray(d?.slides) ? d.slides : d?.deck?.slides))
|
|
121
|
+
const nodesOf = schema.nodes || ((p) => (Array.isArray(p?.elements) ? p.elements : p.children || []))
|
|
122
|
+
const page = pagesOf(next)?.[op.page]
|
|
123
|
+
if (!page) { skipped.push({ op, reason: 'NO_PAGE' }); continue }
|
|
124
|
+
const list = nodesOf(page, op.page)
|
|
125
|
+
if (!Array.isArray(list)) { skipped.push({ op, reason: 'NO_NODE_LIST' }); continue }
|
|
126
|
+
const at = op.index === null || op.index === undefined ? list.length : Math.max(0, Math.min(op.index, list.length))
|
|
127
|
+
list.splice(at, 0, structuredClone(op.node))
|
|
128
|
+
applied.push(op)
|
|
129
|
+
continue
|
|
130
|
+
}
|
|
131
|
+
const hit = listAt(next, schema, op.target.path)
|
|
132
|
+
if (!hit || !hit.node) { skipped.push({ op, reason: 'NOT_FOUND' }); continue }
|
|
133
|
+
if (op.op === 'update') {
|
|
134
|
+
applyPatch(hit.node, op.patch, schema)
|
|
135
|
+
applied.push(op)
|
|
136
|
+
} else if (op.op === 'remove') {
|
|
137
|
+
hit.list.splice(hit.parsed.children.length ? hit.list.indexOf(hit.node) : hit.parsed.node, 1)
|
|
138
|
+
applied.push(op)
|
|
139
|
+
} else if (op.op === 'move') {
|
|
140
|
+
const raw = hit.list.splice(hit.parsed.children.length ? hit.list.indexOf(hit.node) : hit.parsed.node, 1)[0]
|
|
141
|
+
const dest = listAt(next, schema, `p${op.page}/n0`)
|
|
142
|
+
if (!dest || !Array.isArray(dest.list)) { skipped.push({ op, reason: 'NO_PAGE' }); continue }
|
|
143
|
+
const at = op.index === null || op.index === undefined ? dest.list.length : Math.max(0, Math.min(op.index, dest.list.length))
|
|
144
|
+
dest.list.splice(at, 0, raw)
|
|
145
|
+
applied.push(op)
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (isObj(next)) next.rev = (next.rev | 0) + (applied.length ? 1 : 0)
|
|
149
|
+
return { doc: next, applied, skipped }
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** 便利:一次「定位 + 打补丁」(最常用的编辑形态)。 */
|
|
153
|
+
export function patchAt(manifest, locator, patch) {
|
|
154
|
+
const hit = locate(manifest, locator)
|
|
155
|
+
return {
|
|
156
|
+
op: 'update',
|
|
157
|
+
target: { id: hit.id, path: hit.path, page: hit.page, index: hit.index, role: hit.role, type: hit.type },
|
|
158
|
+
patch,
|
|
159
|
+
}
|
|
160
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 模型校验:把「能不能安全地按 ID 编辑」变成机器可判定的检查。
|
|
3
|
+
*
|
|
4
|
+
* 检查项(与 generate-then-edit 的工作流直接相关):
|
|
5
|
+
* ID_DUPLICATE 同一 ID 出现两次 → 寻址会指错元素
|
|
6
|
+
* ID_MISSING 节点没有 ID → 只能按路径改,移动后就断
|
|
7
|
+
* PAGE_EMPTY 空页 → 通常是生成失败
|
|
8
|
+
* BOX_OUT_OF_PAGE 元素越界 → 排版问题(需要 pageSize)
|
|
9
|
+
* TEXT_TOO_LONG 单节点文本超限 → 版面溢出
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const isObj = (value) => !!value && typeof value === 'object' && !Array.isArray(value)
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {ReturnType<import('./manifest.mjs').buildManifest>} manifest
|
|
16
|
+
* @param {{ pageSize?: (pageIndex:number)=> {w:number,h:number}|null, maxChars?: number, allowMissingIds?: boolean }} options
|
|
17
|
+
*/
|
|
18
|
+
export function validateManifest(manifest, options = {}) {
|
|
19
|
+
const issues = []
|
|
20
|
+
const seen = new Map()
|
|
21
|
+
const maxChars = Number(options.maxChars) || 0
|
|
22
|
+
const checkPageEmpty = options.pageEmpty !== false
|
|
23
|
+
|
|
24
|
+
for (const node of manifest.nodes) {
|
|
25
|
+
if (!node.id) {
|
|
26
|
+
if (!options.allowMissingIds) issues.push({ code: 'ID_MISSING', path: node.path, message: '元素缺少稳定 ID' })
|
|
27
|
+
continue
|
|
28
|
+
}
|
|
29
|
+
if (seen.has(node.id)) {
|
|
30
|
+
issues.push({ code: 'ID_DUPLICATE', path: node.path, message: `ID 重复:${node.id}(另一处 ${seen.get(node.id)})` })
|
|
31
|
+
} else {
|
|
32
|
+
seen.set(node.id, node.path)
|
|
33
|
+
}
|
|
34
|
+
if (maxChars && node.charCount > maxChars) {
|
|
35
|
+
issues.push({ code: 'TEXT_TOO_LONG', path: node.path, message: `${node.id} 文本 ${node.charCount} 字,超过 ${maxChars}` })
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (typeof options.pageSize === 'function') {
|
|
40
|
+
for (const page of manifest.pages) {
|
|
41
|
+
const size = options.pageSize(page.index)
|
|
42
|
+
if (!size || !Number.isFinite(size.w) || !Number.isFinite(size.h)) continue
|
|
43
|
+
for (const node of manifest.nodes.filter((n) => n.page === page.index)) {
|
|
44
|
+
if (!node.box) continue
|
|
45
|
+
const { x, y, w, h } = node.box
|
|
46
|
+
if (x < -1 || y < -1 || x + w > size.w + 1 || y + h > size.h + 1) {
|
|
47
|
+
issues.push({
|
|
48
|
+
code: 'BOX_OUT_OF_PAGE',
|
|
49
|
+
path: node.path,
|
|
50
|
+
message: `${node.id} 越界:(${x},${y}) ${w}×${h} 超出页面 ${size.w}×${size.h}`,
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
for (const page of manifest.pages) {
|
|
58
|
+
if (checkPageEmpty && !page.count) {
|
|
59
|
+
issues.push({ code: 'PAGE_EMPTY', path: `p${page.index}`, message: `第 ${page.index} 页没有任何元素` })
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return { ok: issues.length === 0, issues }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** 把校验结果压成给模型看的一行摘要。 */
|
|
67
|
+
export function describeIssues(result, limit = 6) {
|
|
68
|
+
if (result.ok) return '模型校验通过'
|
|
69
|
+
const head = result.issues.slice(0, limit).map((issue) => `${issue.code} ${issue.path}:${issue.message}`)
|
|
70
|
+
const more = result.issues.length > limit ? `…(共 ${result.issues.length} 条)` : ''
|
|
71
|
+
return head.join('\n') + (more ? '\n' + more : '')
|
|
72
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 文档 op 注册与分发:把「查表 + snapshot 策略 + 未知指令」收敛到一处。
|
|
3
|
+
* op 实现按域放在调用方,注册表只提供统一元数据与分发。
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export function defineOp({ name, run, apply, mutates = true, view = false, description = '' } = {}) {
|
|
7
|
+
if (!name || typeof name !== 'string') throw new Error('defineOp 需要 name')
|
|
8
|
+
const fn = run || apply
|
|
9
|
+
if (typeof fn !== 'function') throw new Error(`op "${name}" 需要 run 函数`)
|
|
10
|
+
return {
|
|
11
|
+
name,
|
|
12
|
+
run: fn,
|
|
13
|
+
mutates: mutates !== false,
|
|
14
|
+
view: view === true,
|
|
15
|
+
description,
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function normalizeDefinition(name, raw) {
|
|
20
|
+
if (!raw || typeof raw !== 'object') throw new Error(`op "${name}" 定义不合法`)
|
|
21
|
+
const fn = raw.run || raw.apply
|
|
22
|
+
if (typeof fn !== 'function') throw new Error(`op "${name}" 需要 run 函数`)
|
|
23
|
+
return {
|
|
24
|
+
...raw,
|
|
25
|
+
name,
|
|
26
|
+
run: fn,
|
|
27
|
+
mutates: raw.mutates !== false,
|
|
28
|
+
view: raw.view === true,
|
|
29
|
+
description: raw.description || '',
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** 把 { domain: { opName: definition } } 摊平成一张 op 表。 */
|
|
34
|
+
export function mergeOpModules(modules = {}) {
|
|
35
|
+
const out = {}
|
|
36
|
+
for (const table of Object.values(modules)) {
|
|
37
|
+
Object.assign(out, table)
|
|
38
|
+
}
|
|
39
|
+
return out
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function createOpRegistry({
|
|
43
|
+
ops = {},
|
|
44
|
+
modules = null,
|
|
45
|
+
snapshot = null,
|
|
46
|
+
assertReady = null,
|
|
47
|
+
unknownMessage = null,
|
|
48
|
+
} = {}) {
|
|
49
|
+
const source = modules ? mergeOpModules(modules) : ops
|
|
50
|
+
const definitions = {}
|
|
51
|
+
for (const [name, raw] of Object.entries(source)) {
|
|
52
|
+
if (Object.hasOwn(definitions, name)) throw new Error(`重复的 op: ${name}`)
|
|
53
|
+
definitions[name] = normalizeDefinition(name, raw)
|
|
54
|
+
}
|
|
55
|
+
const names = Object.keys(definitions)
|
|
56
|
+
const viewNames = names.filter((name) => definitions[name].view)
|
|
57
|
+
|
|
58
|
+
async function apply(target, cmd, ctx = {}) {
|
|
59
|
+
if (assertReady) assertReady(target)
|
|
60
|
+
else if (target && typeof target.isMounted === 'function' && !target.isMounted()) {
|
|
61
|
+
throw new Error('编辑器未就绪')
|
|
62
|
+
}
|
|
63
|
+
const name = cmd && cmd.op
|
|
64
|
+
const def = definitions[name]
|
|
65
|
+
if (!def) {
|
|
66
|
+
const detail = unknownMessage
|
|
67
|
+
? unknownMessage(name)
|
|
68
|
+
: '未知指令: ' + String(name || JSON.stringify(cmd).slice(0, 40))
|
|
69
|
+
throw new Error(detail)
|
|
70
|
+
}
|
|
71
|
+
if (def.mutates && snapshot) snapshot(target)
|
|
72
|
+
return def.run(target, cmd || {}, ctx)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function isViewOp(cmdOrName) {
|
|
76
|
+
const name = typeof cmdOrName === 'string' ? cmdOrName : cmdOrName && cmdOrName.op
|
|
77
|
+
return !!(name && definitions[name] && definitions[name].view)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
definitions,
|
|
82
|
+
names,
|
|
83
|
+
viewNames,
|
|
84
|
+
has: (name) => Object.hasOwn(definitions, name),
|
|
85
|
+
isViewOp,
|
|
86
|
+
apply,
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** 兼容旧调用:用 definitions 建一个 applyOp。 */
|
|
91
|
+
export function createOpDispatcher(definitions, options = {}) {
|
|
92
|
+
return createOpRegistry({ ops: definitions, ...options }).apply
|
|
93
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 会话上下文(共享单一真源):把「这次请求属于哪个会话」带到不经过 HTTP 的深处。
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要它:工作台不再是一份全局 store,而是每会话一份。路由在装配时
|
|
5
|
+
* 只拿到 store 门面,真正用哪一份必须等到**请求发生时**才知道;而桥 / 稿件 /
|
|
6
|
+
* 导入 / 领域 shim 之间的调用链全在同一个请求里。AsyncLocalStorage 让这条链上
|
|
7
|
+
* 的每一层都能读到同一个会话 id,不需要把 sid 逐层透传。
|
|
8
|
+
*
|
|
9
|
+
* 除 HTTP 之外还有一个入口:工具调用。DSH 同进程注册的工具会拿到执行上下文
|
|
10
|
+
* (`exec.agent.session.id`),`bindToolsToCallerSession` 就借它把「调用者会话」
|
|
11
|
+
* 缝进来——工具永远改调用它的那个会话,而不是"用户此刻在看的那个窗口"。
|
|
12
|
+
* 只有在拿不到执行上下文(MCP 传输形态)时,才回落到最近聚焦的窗口。
|
|
13
|
+
*
|
|
14
|
+
* 三个 bridge(ppt/canvas/browser)共用本模块:embedded 形态同进程,ALS 单例
|
|
15
|
+
* 天然按异步链隔离各自的会话上下文,互不串扰。
|
|
16
|
+
*/
|
|
17
|
+
import { AsyncLocalStorage } from 'node:async_hooks'
|
|
18
|
+
|
|
19
|
+
const storage = new AsyncLocalStorage()
|
|
20
|
+
|
|
21
|
+
/** 在指定会话的上下文里跑一段逻辑(await 链与定时器都会继承)。
|
|
22
|
+
* payload 是对象而非裸字符串:除会话 id 外还携带一个「本请求已钉住的 store」槽位,
|
|
23
|
+
* 让 store 门面在请求内首次访问时解析一次并复用,避免请求中途 active 被别的窗口
|
|
24
|
+
* /activate 翻转后,同一请求的后续写入改向到另一个会话(跨会话写)。 */
|
|
25
|
+
export function runWithSession(sessionId, fn) {
|
|
26
|
+
return storage.run({ sid: String(sessionId || ''), pinned: null }, fn)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** 当前请求所属的会话 id;不在请求里时返回空串。 */
|
|
30
|
+
export function currentSession() {
|
|
31
|
+
const value = storage.getStore()
|
|
32
|
+
return value && typeof value.sid === 'string' ? value.sid : ''
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** 当前请求的会话上下文对象({sid, pinned});不在请求里时返回 null。
|
|
36
|
+
* store 门面用它把「本请求用哪一份 store」钉住一次,整个请求复用。 */
|
|
37
|
+
export function sessionContext() {
|
|
38
|
+
const value = storage.getStore()
|
|
39
|
+
return value && typeof value === 'object' ? value : null
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 从原生工具调用的执行上下文里取「调用者会话」。
|
|
44
|
+
*
|
|
45
|
+
* DSH 同进程注册的工具会拿到 `exec`(`ctx.tools.register({ execute(args, exec) })`),
|
|
46
|
+
* 其中 `exec.agent.session.id` 就是发起这次工具调用的会话。有了它,工具写入不再
|
|
47
|
+
* 依赖「用户此刻在看哪个窗口」,而是落到**调用它的那个会话**自己的文档上。
|
|
48
|
+
*
|
|
49
|
+
* 拿不到(MCP 传输形态、旧宿主)返回空串,由 store 门面回落到最近聚焦的窗口。
|
|
50
|
+
*/
|
|
51
|
+
export function sessionFromExec(exec) {
|
|
52
|
+
const sid = exec && exec.agent && exec.agent.session ? exec.agent.session.id : ''
|
|
53
|
+
return typeof sid === 'string' ? sid.trim() : ''
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 把工具实现包一层:有调用者会话就在它的上下文里跑,没有就按原样跑。
|
|
58
|
+
*
|
|
59
|
+
* 为什么包在实现层而不是路由层:工具不经过 HTTP,store 门面拿不到 `req.sessionId`,
|
|
60
|
+
* 只能靠 ALS 把会话缝进去。包装后所有工具都不用改签名。
|
|
61
|
+
*/
|
|
62
|
+
export function bindToolsToCallerSession(registry) {
|
|
63
|
+
const impls = registry && registry.TOOL_IMPL
|
|
64
|
+
if (!impls || typeof impls !== 'object') return registry
|
|
65
|
+
for (const name of Object.keys(impls)) {
|
|
66
|
+
const impl = impls[name]
|
|
67
|
+
if (typeof impl !== 'function' || impl.__sessionBound) continue
|
|
68
|
+
const bound = (args, exec) => {
|
|
69
|
+
const sid = sessionFromExec(exec)
|
|
70
|
+
/* exec 原样透传:今天的实现体只用 args,但未来若有工具要执行上下文
|
|
71
|
+
* (进度/中止等),不能在这层被静默丢掉。
|
|
72
|
+
* 即使拿不到调用者会话(MCP 传输形态)也建立上下文:store 门面会在本次调用
|
|
73
|
+
* 首次访问时把「最近聚焦窗口」钉住,整个工具执行期间不再随 active 翻转改向。 */
|
|
74
|
+
return runWithSession(sid, () => impl(args, exec))
|
|
75
|
+
}
|
|
76
|
+
bound.__sessionBound = true
|
|
77
|
+
impls[name] = bound
|
|
78
|
+
}
|
|
79
|
+
return registry
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 把所有路由包成一条:先定会话,再按原顺序跑到第一个接手的路由。
|
|
84
|
+
*
|
|
85
|
+
* 顺序必须保持(mcp → assets → editor → import → manuscript → bridge),
|
|
86
|
+
* 否则 bridge 会先把 POST 当桥协议读走。
|
|
87
|
+
*/
|
|
88
|
+
export function createSessionDispatch(handlers, stores) {
|
|
89
|
+
return async function dispatchSession(req, res, u) {
|
|
90
|
+
const sessionId = String((req && req.sessionId) || (u && u.sessionId) || '')
|
|
91
|
+
/* 打开/刷新某会话的编辑器页面本身就是「用户现在在这个会话里」的信号。
|
|
92
|
+
* 轮询(/watch)不参与,否则两扇窗会互相抢「当前会话」。 */
|
|
93
|
+
if (sessionId && stores && (u.pathname === '/' || u.pathname === '/index.html')) {
|
|
94
|
+
stores.setActive(sessionId)
|
|
95
|
+
}
|
|
96
|
+
return runWithSession(sessionId, async () => {
|
|
97
|
+
for (const handler of handlers) {
|
|
98
|
+
if (await handler(req, res, u)) return true
|
|
99
|
+
}
|
|
100
|
+
return false
|
|
101
|
+
})
|
|
102
|
+
}
|
|
103
|
+
}
|