@capyclaw/browser-workbench 0.1.0 → 0.1.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -7
- package/contract/browser-contract.json +484 -25
- package/demo/form.html +27 -0
- package/lib/host.mjs +1 -68
- 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 +223 -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 +91 -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/{server/shared/state-service.mjs → node_modules/@capyclaw/document-workbench/src/store/file-backend.mjs} +6 -97
- 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 +68 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +113 -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 +87 -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 +143 -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 +41 -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/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 +142 -0
- package/node_modules/@capyclaw/tool-host/src/index.mjs +390 -0
- package/node_modules/playwright/LICENSE +202 -0
- package/node_modules/playwright/NOTICE +5 -0
- package/node_modules/playwright/README.md +318 -0
- package/node_modules/playwright/ThirdPartyNotices.txt +14 -0
- package/node_modules/playwright/cli.js +19 -0
- package/node_modules/playwright/index.d.ts +17 -0
- package/node_modules/playwright/index.js +17 -0
- package/node_modules/playwright/index.mjs +18 -0
- package/node_modules/playwright/lib/agents/agentParser.js +89 -0
- package/node_modules/playwright/lib/agents/copilot-setup-steps.yml +34 -0
- package/node_modules/playwright/lib/agents/generateAgents.js +395 -0
- package/node_modules/playwright/lib/agents/playwright-test-coverage.prompt.md +31 -0
- package/node_modules/playwright/lib/agents/playwright-test-generate.prompt.md +8 -0
- package/node_modules/playwright/lib/agents/playwright-test-generator.agent.md +88 -0
- package/node_modules/playwright/lib/agents/playwright-test-heal.prompt.md +6 -0
- package/node_modules/playwright/lib/agents/playwright-test-healer.agent.md +56 -0
- package/node_modules/playwright/lib/agents/playwright-test-plan.prompt.md +9 -0
- package/node_modules/playwright/lib/agents/playwright-test-planner.agent.md +74 -0
- package/node_modules/playwright/lib/cli/reportActions.js +78 -0
- package/node_modules/playwright/lib/cli/testActions.js +217 -0
- package/node_modules/playwright/lib/common/index.js +2937 -0
- package/node_modules/playwright/lib/common/index.js.txt +33 -0
- package/node_modules/playwright/lib/errorContext.js +129 -0
- package/node_modules/playwright/lib/globals.js +58 -0
- package/node_modules/playwright/lib/index.js +827 -0
- package/node_modules/playwright/lib/isomorphic.js +260 -0
- package/node_modules/playwright/lib/isomorphic.js.txt +9 -0
- package/node_modules/playwright/lib/loader/loaderProcessEntry.js +34 -0
- package/node_modules/playwright/lib/loader/loaderProcessEntry.js.txt +9 -0
- package/node_modules/playwright/lib/matchers/expect.js +13496 -0
- package/node_modules/playwright/lib/matchers/expect.js.LICENSE +640 -0
- package/node_modules/playwright/lib/matchers/expect.js.txt +67 -0
- package/node_modules/playwright/lib/package.js +47 -0
- package/node_modules/playwright/lib/program.js +242 -0
- package/node_modules/playwright/lib/runner/index.js +8721 -0
- package/node_modules/playwright/lib/runner/index.js.txt +62 -0
- package/node_modules/playwright/lib/transform/babelBundle.js +72490 -0
- package/node_modules/playwright/lib/transform/babelBundle.js.LICENSE +2384 -0
- package/node_modules/playwright/lib/transform/babelBundle.js.txt +328 -0
- package/node_modules/playwright/lib/transform/esmLoader.js +8066 -0
- package/node_modules/playwright/lib/transform/esmLoader.js.LICENSE +355 -0
- package/node_modules/playwright/lib/transform/esmLoader.js.txt +60 -0
- package/node_modules/playwright/lib/util.js +373 -0
- package/node_modules/playwright/lib/worker/workerProcessEntry.js +1860 -0
- package/node_modules/playwright/lib/worker/workerProcessEntry.js.txt +19 -0
- package/node_modules/playwright/package.json +50 -0
- package/node_modules/playwright/test.d.ts +18 -0
- package/node_modules/playwright/test.js +24 -0
- package/node_modules/playwright/test.mjs +35 -0
- package/node_modules/playwright/types/test.d.ts +11191 -0
- package/node_modules/playwright/types/testReporter.d.ts +950 -0
- package/node_modules/playwright-core/LICENSE +202 -0
- package/node_modules/playwright-core/NOTICE +5 -0
- package/node_modules/playwright-core/README.md +3 -0
- package/node_modules/playwright-core/ThirdPartyNotices.txt +13 -0
- package/node_modules/playwright-core/bin/install_media_pack.ps1 +5 -0
- package/node_modules/playwright-core/bin/install_webkit_wsl.ps1 +33 -0
- package/node_modules/playwright-core/bin/reinstall_chrome_beta_linux.sh +42 -0
- package/node_modules/playwright-core/bin/reinstall_chrome_beta_mac.sh +13 -0
- package/node_modules/playwright-core/bin/reinstall_chrome_beta_win.ps1 +24 -0
- package/node_modules/playwright-core/bin/reinstall_chrome_stable_linux.sh +42 -0
- package/node_modules/playwright-core/bin/reinstall_chrome_stable_mac.sh +12 -0
- package/node_modules/playwright-core/bin/reinstall_chrome_stable_win.ps1 +24 -0
- package/node_modules/playwright-core/bin/reinstall_msedge_beta_linux.sh +48 -0
- package/node_modules/playwright-core/bin/reinstall_msedge_beta_mac.sh +11 -0
- package/node_modules/playwright-core/bin/reinstall_msedge_beta_win.ps1 +23 -0
- package/node_modules/playwright-core/bin/reinstall_msedge_dev_linux.sh +48 -0
- package/node_modules/playwright-core/bin/reinstall_msedge_dev_mac.sh +11 -0
- package/node_modules/playwright-core/bin/reinstall_msedge_dev_win.ps1 +23 -0
- package/node_modules/playwright-core/bin/reinstall_msedge_stable_linux.sh +48 -0
- package/node_modules/playwright-core/bin/reinstall_msedge_stable_mac.sh +11 -0
- package/node_modules/playwright-core/bin/reinstall_msedge_stable_win.ps1 +24 -0
- package/node_modules/playwright-core/browsers.json +52 -0
- package/node_modules/playwright-core/cli.js +22 -0
- package/node_modules/playwright-core/index.d.ts +17 -0
- package/node_modules/playwright-core/index.js +17 -0
- package/node_modules/playwright-core/index.mjs +28 -0
- package/node_modules/playwright-core/lib/bootstrap.js +149 -0
- package/node_modules/playwright-core/lib/coreBundle.js +78041 -0
- package/node_modules/playwright-core/lib/entry/cliDaemon.js +5 -0
- package/node_modules/playwright-core/lib/entry/dashboardApp.js +3 -0
- package/node_modules/playwright-core/lib/entry/mcp.js +10 -0
- package/node_modules/playwright-core/lib/entry/oopBrowserDownload.js +3 -0
- package/node_modules/playwright-core/lib/package.js +50 -0
- package/node_modules/playwright-core/lib/server/chromium/appIcon.png +0 -0
- package/node_modules/playwright-core/lib/server/electron/loader.js +123 -0
- package/node_modules/playwright-core/lib/serverRegistry.js +1939 -0
- package/node_modules/playwright-core/lib/serverRegistry.js.LICENSE +65 -0
- package/node_modules/playwright-core/lib/tools/cli-client/channelSessions.js +141 -0
- package/node_modules/playwright-core/lib/tools/cli-client/cli.js +7 -0
- package/node_modules/playwright-core/lib/tools/cli-client/help.json +808 -0
- package/node_modules/playwright-core/lib/tools/cli-client/minimist.js +128 -0
- package/node_modules/playwright-core/lib/tools/cli-client/output.js +352 -0
- package/node_modules/playwright-core/lib/tools/cli-client/program.js +422 -0
- package/node_modules/playwright-core/lib/tools/cli-client/registry.js +180 -0
- package/node_modules/playwright-core/lib/tools/cli-client/session.js +261 -0
- package/node_modules/playwright-core/lib/tools/dashboard/appIcon.png +0 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-cli/SKILL.md +493 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/element-attributes.md +23 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/playwright-tests.md +39 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/pr-attachments.md +60 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/request-mocking.md +87 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/running-code.md +245 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/session-management.md +227 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/storage-state.md +275 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/test-generation.md +433 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/tracing.md +139 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/video-recording.md +216 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-component-testing/SKILL.md +136 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-component-testing/references/gallery-spec.md +144 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-component-testing/references/migration.md +89 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-component-testing/references/react.md +69 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-component-testing/references/typing.md +173 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-component-testing/references/vue.md +77 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-trace/SKILL.md +174 -0
- package/node_modules/playwright-core/lib/tools/utils/extension.js +112 -0
- package/node_modules/playwright-core/lib/tools/utils/socketConnection.js +108 -0
- package/node_modules/playwright-core/lib/utilsBundle.js +86078 -0
- package/node_modules/playwright-core/lib/utilsBundle.js.LICENSE +1890 -0
- package/node_modules/playwright-core/lib/vite/dashboard/assets/codeMirrorModule--QdMvsKi.css +1 -0
- package/node_modules/playwright-core/lib/vite/dashboard/assets/codeMirrorModule-CQ8RZGBm.js +32 -0
- package/node_modules/playwright-core/lib/vite/dashboard/assets/codicon-DCmgc-ay.ttf +0 -0
- package/node_modules/playwright-core/lib/vite/dashboard/assets/firefox-1bWoP6pv.svg +1 -0
- package/node_modules/playwright-core/lib/vite/dashboard/assets/firefox-beta-k3eOH_eK.svg +1 -0
- package/node_modules/playwright-core/lib/vite/dashboard/assets/firefox-nightly-Cp5nfeDT.svg +1 -0
- package/node_modules/playwright-core/lib/vite/dashboard/assets/index-AwgwcYie.css +1 -0
- package/node_modules/playwright-core/lib/vite/dashboard/assets/index-hRvO4yU2.js +12 -0
- package/node_modules/playwright-core/lib/vite/dashboard/assets/safari-na3_-uQk.svg +1 -0
- package/node_modules/playwright-core/lib/vite/dashboard/index.html +29 -0
- package/node_modules/playwright-core/lib/vite/dashboard/playwright-logo.svg +24 -0
- package/node_modules/playwright-core/lib/vite/htmlReport/index.html +16 -0
- package/node_modules/playwright-core/lib/vite/htmlReport/report.css +2 -0
- package/node_modules/playwright-core/lib/vite/htmlReport/report.js +32 -0
- package/node_modules/playwright-core/lib/vite/recorder/assets/codeMirrorModule--QdMvsKi.css +1 -0
- package/node_modules/playwright-core/lib/vite/recorder/assets/codeMirrorModule-DK72OMRi.js +32 -0
- package/node_modules/playwright-core/lib/vite/recorder/assets/codicon-DCmgc-ay.ttf +0 -0
- package/node_modules/playwright-core/lib/vite/recorder/assets/index-BZpYJZ-P.css +1 -0
- package/node_modules/playwright-core/lib/vite/recorder/assets/index-Cn0mmDuv.js +129 -0
- package/node_modules/playwright-core/lib/vite/recorder/index.html +29 -0
- package/node_modules/playwright-core/lib/vite/recorder/playwright-logo.svg +9 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/assets/codeMirrorModule-BEB2e8M3.js +32 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/assets/defaultSettingsView-D-g2vrvf.js +184 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/assets/urlMatch-L3liM589.js +1 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/assets/xtermModule-B794rD2Z.js +7 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/codeMirrorModule.-QdMvsKi.css +1 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/codicon.DCmgc-ay.ttf +0 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/defaultSettingsView.C8BeHuxd.css +1 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/index.B_TqY17P.css +1 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/index.html +44 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/index.oNry3Hjv.js +1 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/manifest.webmanifest +16 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/playwright-logo.svg +9 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/snapshot.B_Jk1wbt.js +1 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/snapshot.html +10 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/sw.bundle.js +4 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/uiMode.CYwC_gH1.js +5 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/uiMode.CyMfwkXJ.css +1 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/uiMode.html +18 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/xtermModule.kHJ-D0s7.css +1 -0
- package/node_modules/playwright-core/lib/webp_codec.LICENSE +173 -0
- package/node_modules/playwright-core/lib/webp_codec.wasm +0 -0
- package/node_modules/playwright-core/lib/xdg-open +1267 -0
- package/node_modules/playwright-core/package.json +34 -0
- package/node_modules/playwright-core/types/protocol.d.ts +24776 -0
- package/node_modules/playwright-core/types/structs.d.ts +52 -0
- package/node_modules/playwright-core/types/types.d.ts +27167 -0
- package/package.json +30 -12
- package/plugin.mjs +1 -0
- package/server/contract.mjs +3 -3
- package/server/embedded-host.mjs +247 -0
- package/server/engine.mjs +1906 -115
- package/server/governor.mjs +189 -0
- package/server/http-utils.mjs +1 -1
- package/server/human.mjs +628 -0
- package/server/infra/http.mjs +2 -113
- package/server/infra/tuning.mjs +2 -82
- package/server/input.mjs +154 -0
- package/server/login-vault.mjs +109 -0
- package/server/mcp/handler.mjs +1 -1
- package/server/mcp/instructions.mjs +5 -2
- package/server/orphans.mjs +163 -0
- package/server/relay.mjs +160 -188
- package/server/routes/api.mjs +126 -13
- package/server/routes/assets.mjs +1 -1
- package/server/session-engines.mjs +184 -0
- package/server/shared/SHARED_MANIFEST.json +47 -24
- package/server/shared/asset-service.mjs +2 -217
- package/server/shared/editor-host.mjs +2 -125
- package/server/shared/kit/editor/lease.mjs +2 -139
- package/server/shared/kit/service/api.mjs +2 -45
- package/server/shared/kit/service/http.mjs +2 -113
- package/server/shared/kit/service/mcp-http.mjs +2 -142
- package/server/shared/kit/tools/validate.mjs +2 -115
- package/server/shared/kit/tuning.mjs +2 -82
- package/server/shared/kit/util/fs-lock.mjs +2 -132
- package/server/shared/service-host.mjs +2 -511
- package/server/shared/tool-host.mjs +2 -341
- package/server/shared/tuning.mjs +2 -63
- package/server/tools/capabilities/contract.mjs +25 -5
- package/server/tools/capabilities/interaction.mjs +113 -9
- package/server/tools/capabilities/navigation.mjs +5 -2
- package/server/tools/capabilities/screenshot.mjs +25 -8
- package/server/tools/capabilities/session.mjs +13 -7
- package/server/tools/capabilities/snapshot.mjs +1 -1
- package/server/tools/capabilities/tabs.mjs +2 -2
- package/server/tools/kernel/define.mjs +1 -1
- package/server/tools/kernel/output.mjs +8 -0
- package/server/tools/kernel/ports.mjs +12 -7
- package/server/tools/kernel/registry.mjs +1 -1
- package/server/workbench-framework.mjs +305 -0
- package/ui/app.js +409 -119
- package/ui/index.html +29 -39
- package/ui/style.css +131 -63
- package/cordis.patch.yml +0 -25
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 每会话一个浏览器引擎(F2:会话间彻底隔离)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要它:以前全进程只有一个 Playwright 引擎,所有会话共用同一份标签页、
|
|
5
|
+
* 动作队列与登录态——会话 A 的导航/点击会落在会话 B 正在看的页面上,跨会话串写。
|
|
6
|
+
* 现在每个会话各持一个引擎(独立 profile/标签页/互斥队列),请求/工具调用发生时
|
|
7
|
+
* 按「当前会话」现场解析,互不相干。
|
|
8
|
+
*
|
|
9
|
+
* 会话身份的唯一真源是 @capyclaw/document-workbench/session-context 的 AsyncLocalStorage:
|
|
10
|
+
* - HTTP 请求:createSessionDispatch 从 URL 的 /s/<sid> 段缝入;
|
|
11
|
+
* - 工具调用:bindToolsToCallerSession 从 exec.agent.session.id 缝入。
|
|
12
|
+
* 解析顺序:当前会话(ALS)→ default。无会话身份时(standalone 开发态)落 default,
|
|
13
|
+
* 行为与旧单引擎一致。
|
|
14
|
+
*
|
|
15
|
+
* 引擎是重资源(Playwright 上下文),所以懒创建:第一次用到某会话才拉起,
|
|
16
|
+
* 且只 mkdir profile/screenshots,浏览器本体到首次 navigate 才启动(见 engine.mjs)。
|
|
17
|
+
* 缓存的是 Promise<engine>:并发首请求共享同一次创建,不会重复拉起。
|
|
18
|
+
*/
|
|
19
|
+
import path from 'node:path'
|
|
20
|
+
import { currentSession } from '@capyclaw/document-workbench/session-context'
|
|
21
|
+
|
|
22
|
+
const DEFAULT_SESSION = 'default'
|
|
23
|
+
/* 会话 id 只允许安全字符(要当目录名),与 service-host 的 SESSION_PATH 同口径。 */
|
|
24
|
+
const SID_RE = /^[A-Za-z0-9][A-Za-z0-9._~-]{0,127}$/
|
|
25
|
+
|
|
26
|
+
export function normalizeSessionId(value) {
|
|
27
|
+
const sid = String(value == null ? '' : value).trim()
|
|
28
|
+
return SID_RE.test(sid) ? sid : ''
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @param {object} options
|
|
33
|
+
* @param {string} options.hostDir 宿主数据目录;每会话引擎落 <hostDir>/sessions/<sid>/
|
|
34
|
+
* @param {object} [options.config] resolveEngineConfig 的产物(各会话共用同一配置)
|
|
35
|
+
* @param {Function} [options.createEngine] 引擎工厂(测试注入);缺省=createBrowserEngine。
|
|
36
|
+
* 缺省工厂用动态 import('./engine.mjs'):src/server 与产物 server/ 里引擎都在
|
|
37
|
+
* './engine.mjs'(build 把 src/mcp/engine.mjs 拷成 server/engine.mjs),动态 import
|
|
38
|
+
* 让本模块在 src 下也能被独立 import(测试注入工厂时根本不触发缺省工厂)。
|
|
39
|
+
*/
|
|
40
|
+
export function createSessionEngines({ hostDir, config, createEngine }) {
|
|
41
|
+
if (!hostDir) throw new Error('createSessionEngines: 缺少 hostDir')
|
|
42
|
+
const make = createEngine
|
|
43
|
+
|| ((opts) => import('./engine.mjs').then((m) => m.createBrowserEngine(opts)))
|
|
44
|
+
const cache = new Map() // sid → Promise<engine>
|
|
45
|
+
const ready = new Map() // sid → engine(已就绪,供回收巡检;避免 await 创建中的 Promise)
|
|
46
|
+
|
|
47
|
+
function forSession(value) {
|
|
48
|
+
const key = normalizeSessionId(value) || DEFAULT_SESSION
|
|
49
|
+
if (!cache.has(key)) {
|
|
50
|
+
const engineHostDir = path.join(hostDir, 'sessions', key)
|
|
51
|
+
let pending
|
|
52
|
+
pending = Promise.resolve()
|
|
53
|
+
.then(() => make({ hostDir: engineHostDir, config }))
|
|
54
|
+
.then((engine) => { ready.set(key, engine); return engine })
|
|
55
|
+
.catch((error) => {
|
|
56
|
+
/* 创建失败不缓存坏结果:下次请求可重试(别把一个瞬态错误固化成永久坏会话)。 */
|
|
57
|
+
if (cache.get(key) === pending) cache.delete(key)
|
|
58
|
+
ready.delete(key)
|
|
59
|
+
throw error
|
|
60
|
+
})
|
|
61
|
+
cache.set(key, pending)
|
|
62
|
+
}
|
|
63
|
+
return cache.get(key)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** 当前会话的引擎(Promise)。工具端口/路由在请求发生时调用,ALS 已缝好会话。 */
|
|
67
|
+
function current() {
|
|
68
|
+
return forSession(currentSession())
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/* ---- 资源回收(空闲 + LRU 上限)----
|
|
72
|
+
* 引擎对象常驻缓存(保 profile / 标签日志 / 会话身份),回收只释放浏览器进程:
|
|
73
|
+
* ① 空闲:running 且超过 idleTimeoutMs 没人用、且没人在看(watching=0)→ suspend;
|
|
74
|
+
* ② LRU 上限:running 数超过 maxRunningEngines → 按最久未用 suspend 到上限。
|
|
75
|
+
* 下次该会话再有请求时懒重启 + 恢复标签页(见 engine.mjs restoreJournal)。
|
|
76
|
+
* 注入共享引擎(测试/旧装配)时按引擎对象去重,避免一个实例被当成多个计数。 */
|
|
77
|
+
const cfg = config || {}
|
|
78
|
+
const idleTimeoutMs = Number.isFinite(cfg.idleTimeoutMs) ? cfg.idleTimeoutMs : 1800000
|
|
79
|
+
const maxRunning = Number.isFinite(cfg.maxRunningEngines) ? cfg.maxRunningEngines : 6
|
|
80
|
+
const reapIntervalMs = Number.isFinite(cfg.reapIntervalMs) && cfg.reapIntervalMs > 0 ? cfg.reapIntervalMs : 30000
|
|
81
|
+
|
|
82
|
+
let reaping = false
|
|
83
|
+
async function reap() {
|
|
84
|
+
/* 重入守卫:巡检是异步的(await suspend),定时器/手动触发可能叠在一起。
|
|
85
|
+
叠跑会重复挂起同一引擎、并把 LRU 计数算乱。一次只跑一轮。 */
|
|
86
|
+
if (reaping) return
|
|
87
|
+
reaping = true
|
|
88
|
+
try {
|
|
89
|
+
const entries = []
|
|
90
|
+
const seen = new Set()
|
|
91
|
+
for (const engine of ready.values()) {
|
|
92
|
+
if (seen.has(engine)) continue
|
|
93
|
+
seen.add(engine)
|
|
94
|
+
if (!engine || typeof engine.status !== 'function') continue
|
|
95
|
+
let st
|
|
96
|
+
try { st = engine.status() } catch { continue }
|
|
97
|
+
if (st.running) entries.push({ engine, st })
|
|
98
|
+
}
|
|
99
|
+
const now = Date.now()
|
|
100
|
+
const suspendable = (e) => typeof e.engine.suspend === 'function' && (e.st.watching || 0) === 0
|
|
101
|
+
/* ① 空闲回收。扫描时的 lastActivity 可能已陈旧:真正挂起前再读一次新鲜状态,
|
|
102
|
+
并把 ifIdleMs 传给引擎,让它在动作队列里挂起前二次复检(期间可能来了新动作)。 */
|
|
103
|
+
if (idleTimeoutMs > 0) {
|
|
104
|
+
for (const e of entries) {
|
|
105
|
+
const last = Number(e.st.lastActivity) || 0
|
|
106
|
+
if (!(last && now - last > idleTimeoutMs && suspendable(e))) continue
|
|
107
|
+
let fresh
|
|
108
|
+
try { fresh = e.engine.status() } catch { continue }
|
|
109
|
+
const freshLast = Number(fresh.lastActivity) || 0
|
|
110
|
+
if (!fresh.running || (fresh.watching || 0) !== 0) continue
|
|
111
|
+
if (freshLast && Date.now() - freshLast <= idleTimeoutMs) continue
|
|
112
|
+
try { await e.engine.suspend({ ifIdleMs: idleTimeoutMs }) } catch { /* 已退出 */ }
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/* ② LRU 上限:重新数 running(空闲回收可能已减),超限按最久未用挂起。 */
|
|
116
|
+
if (maxRunning > 0) {
|
|
117
|
+
const running = entries.filter((e) => { try { return e.engine.status().running } catch { return false } })
|
|
118
|
+
if (running.length > maxRunning) {
|
|
119
|
+
running.sort((a, b) => (Number(a.st.lastActivity) || 0) - (Number(b.st.lastActivity) || 0))
|
|
120
|
+
let excess = running.length - maxRunning
|
|
121
|
+
for (const e of running) {
|
|
122
|
+
if (excess <= 0) break
|
|
123
|
+
if (!suspendable(e)) continue
|
|
124
|
+
/* 强制挂起前也看一眼是否刚被观看/使用,避免把正在用的页挂起。 */
|
|
125
|
+
let fresh
|
|
126
|
+
try { fresh = e.engine.status() } catch { continue }
|
|
127
|
+
if (!fresh.running || (fresh.watching || 0) !== 0) continue
|
|
128
|
+
try { await e.engine.suspend(); excess -= 1 } catch { /* 已退出 */ }
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
} finally {
|
|
133
|
+
reaping = false
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
let reapTimer = null
|
|
138
|
+
if (idleTimeoutMs > 0 || maxRunning > 0) {
|
|
139
|
+
reapTimer = setInterval(() => { reap().catch(() => {}) }, reapIntervalMs)
|
|
140
|
+
reapTimer.unref?.()
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** 停服前挂起所有已创建的引擎(保留标签日志供下次恢复),避免僵尸浏览器进程。
|
|
144
|
+
* 停服竞态:框架先调 beforeStop 再关 HTTP,await 期间仍可能有请求进来重建引擎。
|
|
145
|
+
* 用 drain 循环把「await 期间新创建的」也扫掉(最多 5 轮,防持续流量下无限循环);
|
|
146
|
+
* 并发 closeAll 共享同一次 drain。drain 结束后缓存为空,后续请求仍可重建(既有语义)。 */
|
|
147
|
+
let closingPromise = null
|
|
148
|
+
function closeAll() {
|
|
149
|
+
if (closingPromise) return closingPromise
|
|
150
|
+
closingPromise = (async () => {
|
|
151
|
+
try {
|
|
152
|
+
if (reapTimer) { clearInterval(reapTimer); reapTimer = null }
|
|
153
|
+
for (let pass = 0; pass < 5; pass += 1) {
|
|
154
|
+
const pending = [...cache.values()]
|
|
155
|
+
cache.clear()
|
|
156
|
+
ready.clear()
|
|
157
|
+
if (pending.length === 0) break
|
|
158
|
+
await Promise.all(pending.map(async (p) => {
|
|
159
|
+
try {
|
|
160
|
+
const engine = await p
|
|
161
|
+
/* 停服走挂起(保标签日志);假引擎无 suspend 时回落 close。 */
|
|
162
|
+
if (typeof engine.suspend === 'function') await engine.suspend()
|
|
163
|
+
else await engine.close()
|
|
164
|
+
} catch { /* 浏览器已退出/创建失败时忽略 */ }
|
|
165
|
+
}))
|
|
166
|
+
}
|
|
167
|
+
cache.clear()
|
|
168
|
+
ready.clear()
|
|
169
|
+
} finally {
|
|
170
|
+
closingPromise = null
|
|
171
|
+
}
|
|
172
|
+
})()
|
|
173
|
+
return closingPromise
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return {
|
|
177
|
+
forSession,
|
|
178
|
+
current,
|
|
179
|
+
closeAll,
|
|
180
|
+
sessions: () => [...cache.keys()],
|
|
181
|
+
/* 手动触发一次回收巡检(测试用;生产由定时器驱动)。 */
|
|
182
|
+
reapNow: () => reap(),
|
|
183
|
+
}
|
|
184
|
+
}
|
|
@@ -1,47 +1,70 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.
|
|
2
|
+
"version": "0.2.0",
|
|
3
|
+
"mode": "re-export",
|
|
3
4
|
"packages": {
|
|
4
5
|
"service-host": {
|
|
5
6
|
"package": "@capyclaw/service-host",
|
|
6
|
-
"version": "0.1.
|
|
7
|
+
"version": "0.1.5",
|
|
7
8
|
"apiVersion": "1.0.0",
|
|
8
|
-
"
|
|
9
|
+
"spec": "@capyclaw/service-host/src",
|
|
10
|
+
"sha256": "af0a57d779bfe6a66dc935320178d591ef799a7e037f7a5823cdfa1fe5878eeb"
|
|
9
11
|
},
|
|
10
12
|
"tool-host": {
|
|
11
13
|
"package": "@capyclaw/tool-host",
|
|
12
|
-
"version": "0.
|
|
13
|
-
"apiVersion": "1.
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
"state-service": {
|
|
17
|
-
"package": "@capyclaw/state-service",
|
|
18
|
-
"version": "0.1.0",
|
|
19
|
-
"apiVersion": "1.0.0",
|
|
20
|
-
"sha256": "c77d43e1a994f5f5a0b27b8694dcbd662308f6a6363b42ebaa5a305057a09ea5"
|
|
14
|
+
"version": "0.2.0",
|
|
15
|
+
"apiVersion": "1.1.0",
|
|
16
|
+
"spec": "@capyclaw/tool-host/src",
|
|
17
|
+
"sha256": "23256dbcd5cbee0b96b33c97e4eb4e74d0e169cbdde52f91d4526f2fc0f60d3e"
|
|
21
18
|
},
|
|
22
19
|
"asset-service": {
|
|
23
20
|
"package": "@capyclaw/asset-service",
|
|
24
|
-
"version": "0.1.
|
|
21
|
+
"version": "0.1.4",
|
|
25
22
|
"apiVersion": "1.0.0",
|
|
26
|
-
"
|
|
23
|
+
"spec": "@capyclaw/asset-service/src",
|
|
24
|
+
"sha256": "311a83e95d56dbb69b739d61aced0889b64e85eddd95b883e322e0fa5be8bd6f"
|
|
27
25
|
},
|
|
28
26
|
"editor-host": {
|
|
29
27
|
"package": "@capyclaw/editor-host",
|
|
30
|
-
"version": "0.1.
|
|
28
|
+
"version": "0.1.5",
|
|
31
29
|
"apiVersion": "1.0.0",
|
|
32
|
-
"
|
|
30
|
+
"spec": "@capyclaw/editor-host/src",
|
|
31
|
+
"sha256": "c8b80dbd4f1b2bee9ef9001423a013031a21ea9a3b8897b5536fe6e5687f01b3"
|
|
33
32
|
}
|
|
34
33
|
},
|
|
35
34
|
"kit": {
|
|
36
|
-
"tuning.mjs":
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
"
|
|
41
|
-
|
|
42
|
-
|
|
35
|
+
"tuning.mjs": {
|
|
36
|
+
"spec": "@capyclaw/plugin-kit/tuning",
|
|
37
|
+
"sha256": "6dc1d89c97056b03c27d744e6256e0e8d88a772c340acc6e04a3413e9ee5cc8b"
|
|
38
|
+
},
|
|
39
|
+
"service/api.mjs": {
|
|
40
|
+
"spec": "@capyclaw/plugin-kit/service/api",
|
|
41
|
+
"sha256": "336a92237d5575709d421cb8199bad7c56fbd95bfd2b30b5d9b913f719db8402"
|
|
42
|
+
},
|
|
43
|
+
"service/http.mjs": {
|
|
44
|
+
"spec": "@capyclaw/plugin-kit/service/http",
|
|
45
|
+
"sha256": "5839bf8872a208f14a219ba7528e2d19e9884f6e6a2645689d58369952d0476f"
|
|
46
|
+
},
|
|
47
|
+
"service/mcp-http.mjs": {
|
|
48
|
+
"spec": "@capyclaw/plugin-kit/service/mcp-http",
|
|
49
|
+
"sha256": "54bce272839c9477d460a483ee4bce62ca9f61b56174247ed718a7711eec7ee2"
|
|
50
|
+
},
|
|
51
|
+
"tools/validate.mjs": {
|
|
52
|
+
"spec": "@capyclaw/plugin-kit/tools/validate",
|
|
53
|
+
"sha256": "d88424caa66913acfc49a340c6e6eea6fe930a676ee8d32581d6366877f21389"
|
|
54
|
+
},
|
|
55
|
+
"editor/lease.mjs": {
|
|
56
|
+
"spec": "@capyclaw/plugin-kit/editor/lease",
|
|
57
|
+
"sha256": "4b1fe0ea3678d27f5283827a1d658784b915914cb6a0780f9534426ffd66b25f"
|
|
58
|
+
},
|
|
59
|
+
"util/fs-lock.mjs": {
|
|
60
|
+
"spec": "@capyclaw/plugin-kit/util/fs-lock",
|
|
61
|
+
"sha256": "c528e30271d83be2c857c160246598f96e636dacb2561a5e2109337e84d0865b"
|
|
62
|
+
}
|
|
43
63
|
},
|
|
44
64
|
"pkgRoot": {
|
|
45
|
-
"capyclaw-service-host/tuning.mjs":
|
|
65
|
+
"capyclaw-service-host/tuning.mjs": {
|
|
66
|
+
"spec": "@capyclaw/service-host/tuning",
|
|
67
|
+
"sha256": "1425851a3bff8c672a58536096faa321c74a4d4333cc0ae9268c1cfe83696492"
|
|
68
|
+
}
|
|
46
69
|
}
|
|
47
70
|
}
|
|
@@ -1,217 +1,2 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* 功能插件不再各自实现 `/assets/<sha256>`、base64 上传、content-type 与缓存头。
|
|
5
|
-
* 资产按 sha256 去重,url 为 `/assets/<id>`。
|
|
6
|
-
*
|
|
7
|
-
* 两种后端:
|
|
8
|
-
* - workspace:storeClient(ctx.workspace 的 scoped client),资产归当前 work;
|
|
9
|
-
* - file:本地目录,适合无 workspace 的独立运行/测试。
|
|
10
|
-
*/
|
|
11
|
-
import crypto from 'node:crypto'
|
|
12
|
-
import fs from 'node:fs'
|
|
13
|
-
import path from 'node:path'
|
|
14
|
-
import { send, readBody } from '@capyclaw/plugin-kit/service/http'
|
|
15
|
-
import { createServiceApi } from '@capyclaw/plugin-kit/service/api'
|
|
16
|
-
|
|
17
|
-
function sha256(buffer) {
|
|
18
|
-
return crypto.createHash('sha256').update(buffer).digest('hex')
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const ASSET_ID_RE = /^[a-f0-9]{64}$/
|
|
22
|
-
|
|
23
|
-
function validId(id) {
|
|
24
|
-
return ASSET_ID_RE.test(String(id || ''))
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function normalizeBuffer(value) {
|
|
28
|
-
if (Buffer.isBuffer(value)) return value
|
|
29
|
-
if (value instanceof Uint8Array) return Buffer.from(value)
|
|
30
|
-
throw new Error('asset: 数据必须是 Buffer/Uint8Array')
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/** 基于 ctx.workspace 的 scoped WorkspaceClient。 */
|
|
34
|
-
export function createWorkspaceAssetStore({ storeClient, workId = 'main' } = {}) {
|
|
35
|
-
if (!storeClient || typeof storeClient.put !== 'function') throw new Error('createWorkspaceAssetStore 需要 storeClient')
|
|
36
|
-
return {
|
|
37
|
-
kind: 'workspace',
|
|
38
|
-
async put(value, contentType = 'application/octet-stream') {
|
|
39
|
-
const buffer = normalizeBuffer(value)
|
|
40
|
-
const id = sha256(buffer)
|
|
41
|
-
const key = `assets/${id}`
|
|
42
|
-
storeClient.put(workId, key, buffer, { contentType })
|
|
43
|
-
return { id, key, url: `/assets/${id}`, size: buffer.length, contentType }
|
|
44
|
-
},
|
|
45
|
-
get(id) {
|
|
46
|
-
if (!validId(id)) return null
|
|
47
|
-
const key = `assets/${id}`
|
|
48
|
-
try {
|
|
49
|
-
const data = storeClient.get(workId, key)
|
|
50
|
-
if (!data) return null
|
|
51
|
-
const meta = storeClient.list(workId).find((item) => item.key === key)
|
|
52
|
-
return { data, contentType: meta?.contentType || 'application/octet-stream' }
|
|
53
|
-
} catch {
|
|
54
|
-
/* 缺失/权限错误按 404 处理,不能把 500 泄漏给资产路由。 */
|
|
55
|
-
return null
|
|
56
|
-
}
|
|
57
|
-
},
|
|
58
|
-
has(id) {
|
|
59
|
-
if (!validId(id)) return false
|
|
60
|
-
try { return !!storeClient.get(workId, `assets/${id}`) } catch { return false }
|
|
61
|
-
},
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** 本地目录后端:<dir>/<sha256> + <dir>/<sha256>.meta.json。 */
|
|
66
|
-
export function createFileAssetStore({ dir } = {}) {
|
|
67
|
-
if (!dir) throw new Error('createFileAssetStore 需要 dir')
|
|
68
|
-
fs.mkdirSync(dir, { recursive: true })
|
|
69
|
-
return {
|
|
70
|
-
kind: 'file',
|
|
71
|
-
async put(value, contentType = 'application/octet-stream') {
|
|
72
|
-
const buffer = normalizeBuffer(value)
|
|
73
|
-
const id = sha256(buffer)
|
|
74
|
-
const file = path.join(dir, id)
|
|
75
|
-
const metaFile = file + '.meta.json'
|
|
76
|
-
const existingOk = (() => {
|
|
77
|
-
try { return fs.statSync(file).size === buffer.length } catch { return false }
|
|
78
|
-
})()
|
|
79
|
-
if (!existingOk) {
|
|
80
|
-
const tmp = `${file}.tmp-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}`
|
|
81
|
-
fs.writeFileSync(tmp, buffer)
|
|
82
|
-
fs.renameSync(tmp, file)
|
|
83
|
-
}
|
|
84
|
-
const metaTmp = `${metaFile}.tmp-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}`
|
|
85
|
-
fs.writeFileSync(metaTmp, JSON.stringify({ contentType, size: buffer.length }))
|
|
86
|
-
fs.renameSync(metaTmp, metaFile)
|
|
87
|
-
return { id, key: id, url: `/assets/${id}`, size: buffer.length, contentType }
|
|
88
|
-
},
|
|
89
|
-
get(id) {
|
|
90
|
-
if (!validId(id)) return null
|
|
91
|
-
const file = path.join(dir, path.basename(String(id)))
|
|
92
|
-
try {
|
|
93
|
-
const data = fs.readFileSync(file)
|
|
94
|
-
/* 内容寻址仓库必须复核 hash:截断/篡改的对象不能静默返回。 */
|
|
95
|
-
if (sha256(data) !== id) {
|
|
96
|
-
console.error(`[asset-service] 对象校验失败,拒绝返回: ${id}`)
|
|
97
|
-
return null
|
|
98
|
-
}
|
|
99
|
-
let contentType = 'application/octet-stream'
|
|
100
|
-
try { contentType = JSON.parse(fs.readFileSync(file + '.meta.json', 'utf8')).contentType || contentType } catch { /* 默认 */ }
|
|
101
|
-
return { data, contentType }
|
|
102
|
-
} catch {
|
|
103
|
-
return null
|
|
104
|
-
}
|
|
105
|
-
},
|
|
106
|
-
has(id) {
|
|
107
|
-
if (!validId(id)) return false
|
|
108
|
-
try { return fs.existsSync(path.join(dir, path.basename(String(id)))) } catch { return false }
|
|
109
|
-
},
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/** 防止 text/html / svg 在 loopback 同源执行脚本;图片仍按原 MIME 内联。 */
|
|
114
|
-
function safeContentType(type) {
|
|
115
|
-
const base = String(type || '').split(';')[0].trim().toLowerCase() || 'application/octet-stream'
|
|
116
|
-
if (base === 'text/html' || base === 'application/xhtml+xml' || base === 'image/svg+xml') {
|
|
117
|
-
return 'text/plain; charset=utf-8'
|
|
118
|
-
}
|
|
119
|
-
return base
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* service-host 兼容的资产路由。
|
|
124
|
-
* GET/HEAD `/assets/<id>`(同时兼容 `<basePath>/assets/<id>`);
|
|
125
|
-
* POST `/assets` 或 `<basePath>/assets`(JSON: { data: base64, contentType })。
|
|
126
|
-
*/
|
|
127
|
-
export function createAssetRoutes({ assetStore, basePath = '', maxUploadBytes = 10 * 1024 * 1024 } = {}) {
|
|
128
|
-
const prefix = basePath === '/' ? '' : String(basePath || '').replace(/\/+$/, '')
|
|
129
|
-
const getPrefixes = ['/assets/', prefix ? `${prefix}/assets/` : null].filter(Boolean)
|
|
130
|
-
const postPaths = new Set(['/assets', prefix ? `${prefix}/assets` : null].filter(Boolean))
|
|
131
|
-
return async function assetRoutes(req, res, u) {
|
|
132
|
-
const p = u.pathname
|
|
133
|
-
if (req.method === 'GET' || req.method === 'HEAD') {
|
|
134
|
-
const getPrefix = getPrefixes.find((candidate) => p.startsWith(candidate))
|
|
135
|
-
if (getPrefix) {
|
|
136
|
-
if (!assetStore) { send(req, res, 404, { error: 'asset store unavailable' }); return true }
|
|
137
|
-
const id = decodeURIComponent(p.slice(getPrefix.length))
|
|
138
|
-
if (!validId(id)) { send(req, res, 404, { error: 'asset not found' }); return true }
|
|
139
|
-
const asset = assetStore.get(id)
|
|
140
|
-
if (!asset) { send(req, res, 404, { error: 'asset not found' }); return true }
|
|
141
|
-
res.writeHead(200, {
|
|
142
|
-
'content-type': safeContentType(asset.contentType),
|
|
143
|
-
'content-length': asset.data.length,
|
|
144
|
-
'cache-control': 'public, max-age=31536000, immutable',
|
|
145
|
-
'x-content-type-options': 'nosniff',
|
|
146
|
-
'referrer-policy': 'no-referrer',
|
|
147
|
-
})
|
|
148
|
-
res.end(req.method === 'HEAD' ? undefined : asset.data)
|
|
149
|
-
return true
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
if (postPaths.has(p) && req.method === 'POST') {
|
|
153
|
-
if (!assetStore) { send(req, res, 503, { error: 'asset store unavailable' }); return true }
|
|
154
|
-
let body
|
|
155
|
-
try { body = await readBody(req, maxUploadBytes) } catch (error) {
|
|
156
|
-
const status = error?.code === 'BODY_TOO_LARGE' ? 413 : 400
|
|
157
|
-
send(req, res, status, { error: String(error.message || error) })
|
|
158
|
-
return true
|
|
159
|
-
}
|
|
160
|
-
if (!body || typeof body.data !== 'string' || !body.data) {
|
|
161
|
-
send(req, res, 400, { error: '缺少图片数据' })
|
|
162
|
-
return true
|
|
163
|
-
}
|
|
164
|
-
const buffer = Buffer.from(body.data, 'base64')
|
|
165
|
-
if (!buffer.length) { send(req, res, 400, { error: '图片数据为空' }); return true }
|
|
166
|
-
const asset = await assetStore.put(buffer, body.contentType || 'application/octet-stream')
|
|
167
|
-
send(req, res, 200, { ok: true, ...asset })
|
|
168
|
-
return true
|
|
169
|
-
}
|
|
170
|
-
return false
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
export function createAssetHost(config = {}) {
|
|
175
|
-
const defaultStore = config.storeClient
|
|
176
|
-
? createWorkspaceAssetStore({ storeClient: config.storeClient, workId: config.workId })
|
|
177
|
-
: config.dir
|
|
178
|
-
? createFileAssetStore({ dir: config.dir })
|
|
179
|
-
: null
|
|
180
|
-
|
|
181
|
-
return {
|
|
182
|
-
api: createServiceApi('assets', '1.0.0', ['workspace-store', 'file-store', 'routes', 'sha256', 'mount']),
|
|
183
|
-
version: '0.1.0',
|
|
184
|
-
createStore: createWorkspaceAssetStore,
|
|
185
|
-
createFileStore: createFileAssetStore,
|
|
186
|
-
routes: createAssetRoutes,
|
|
187
|
-
store: defaultStore,
|
|
188
|
-
/**
|
|
189
|
-
* 每个 service-host app 只挂一次资产路由,避免多个插件重复争抢 /assets。
|
|
190
|
-
* @param {object} hostApp createServiceApp() 返回值
|
|
191
|
-
* @param {{ store?: object, basePath?: string }} options
|
|
192
|
-
*/
|
|
193
|
-
mount(hostApp, { store = defaultStore, basePath = '' } = {}) {
|
|
194
|
-
if (!hostApp || typeof hostApp.use !== 'function') throw new Error('assets.mount 需要 service-host app')
|
|
195
|
-
if (!store) throw new Error('assets.mount: 没有可用 store')
|
|
196
|
-
if (hostApp.__dshAssetsMounted) return () => {}
|
|
197
|
-
hostApp.__dshAssetsMounted = true
|
|
198
|
-
const unuse = hostApp.use(createAssetRoutes({ assetStore: store, basePath }))
|
|
199
|
-
let disposed = false
|
|
200
|
-
return () => {
|
|
201
|
-
if (disposed) return
|
|
202
|
-
disposed = true
|
|
203
|
-
/* 真正卸载路由;否则热重载后旧 store 仍会抢答 /assets。 */
|
|
204
|
-
if (typeof unuse === 'function') unuse()
|
|
205
|
-
hostApp.__dshAssetsMounted = false
|
|
206
|
-
}
|
|
207
|
-
},
|
|
208
|
-
async put(value, contentType) {
|
|
209
|
-
if (!defaultStore) throw new Error('asset-service: 未配置 storeClient/dir')
|
|
210
|
-
return defaultStore.put(value, contentType)
|
|
211
|
-
},
|
|
212
|
-
get(id) {
|
|
213
|
-
if (!defaultStore) throw new Error('asset-service: 未配置 storeClient/dir')
|
|
214
|
-
return defaultStore.get(id)
|
|
215
|
-
},
|
|
216
|
-
}
|
|
217
|
-
}
|
|
1
|
+
/** 由 build/sync-shared.mjs 生成:实现只存在于 @capyclaw/asset-service/src,此处零拷贝。 */
|
|
2
|
+
export * from '@capyclaw/asset-service/src'
|
|
@@ -1,125 +1,2 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* 功能插件只提供 editor/ 静态目录,不再自己写:
|
|
5
|
-
* - 静态文件路由 / content-type / 404
|
|
6
|
-
* - “显示即连接”的租约仲裁(后打开者抢占,旧窗口静默待命)
|
|
7
|
-
* - claim / release / status / heartbeat 端点
|
|
8
|
-
* - 连接状态供 /healthz 使用
|
|
9
|
-
*
|
|
10
|
-
* 浏览器端可直接使用 @capyclaw/plugin-kit 的 editor/lease.mjs(无 Node 依赖)。
|
|
11
|
-
*/
|
|
12
|
-
import fs from 'node:fs'
|
|
13
|
-
import path from 'node:path'
|
|
14
|
-
import { createEditorLease } from '@capyclaw/plugin-kit/editor/lease'
|
|
15
|
-
import { send, readBody } from '@capyclaw/plugin-kit/service/http'
|
|
16
|
-
import { createServiceApi } from '@capyclaw/plugin-kit/service/api'
|
|
17
|
-
|
|
18
|
-
const MIME = {
|
|
19
|
-
'.html': 'text/html; charset=utf-8',
|
|
20
|
-
'.js': 'text/javascript; charset=utf-8',
|
|
21
|
-
'.mjs': 'text/javascript; charset=utf-8',
|
|
22
|
-
'.css': 'text/css; charset=utf-8',
|
|
23
|
-
'.json': 'application/json; charset=utf-8',
|
|
24
|
-
'.svg': 'image/svg+xml',
|
|
25
|
-
'.png': 'image/png',
|
|
26
|
-
'.jpg': 'image/jpeg',
|
|
27
|
-
'.jpeg': 'image/jpeg',
|
|
28
|
-
'.webp': 'image/webp',
|
|
29
|
-
'.woff2': 'font/woff2',
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function safeJoin(root, rel) {
|
|
33
|
-
const target = path.resolve(root, `.${path.sep}${rel}`)
|
|
34
|
-
const base = path.resolve(root)
|
|
35
|
-
return target === base || target.startsWith(base + path.sep) ? target : null
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function contentType(file) {
|
|
39
|
-
return MIME[path.extname(file).toLowerCase()] || 'application/octet-stream'
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* @param {{ id: string, dir: string, basePath?: string, index?: string, files?: Record<string,string|string[]> }} options
|
|
44
|
-
*/
|
|
45
|
-
export function createEditorHost({ id, dir, basePath = '', index = 'index.html', files = null } = {}) {
|
|
46
|
-
if (!id) throw new Error('createEditorHost: id 必填')
|
|
47
|
-
const prefix = basePath === '/' ? '' : String(basePath || '').replace(/\/+$/, '')
|
|
48
|
-
const lease = createEditorLease()
|
|
49
|
-
|
|
50
|
-
const staticRoutes = async function editorStatic(req, res, u) {
|
|
51
|
-
if (req.method !== 'GET' && req.method !== 'HEAD') return false
|
|
52
|
-
if (prefix && u.pathname !== prefix && !u.pathname.startsWith(prefix + '/')) return false
|
|
53
|
-
const relRaw = prefix ? u.pathname.slice(prefix.length) : u.pathname
|
|
54
|
-
const rel = relRaw === '' || relRaw === '/' ? index : decodeURIComponent(relRaw.replace(/^\/+/, ''))
|
|
55
|
-
let file = null
|
|
56
|
-
if (files) {
|
|
57
|
-
const hit = files[relRaw === '' ? '/' : relRaw] || files[relRaw]
|
|
58
|
-
if (!hit) return false
|
|
59
|
-
const name = Array.isArray(hit) ? hit[0] : hit
|
|
60
|
-
file = safeJoin(dir, name)
|
|
61
|
-
} else {
|
|
62
|
-
file = safeJoin(dir, rel)
|
|
63
|
-
}
|
|
64
|
-
if (!file) return false
|
|
65
|
-
try {
|
|
66
|
-
const data = fs.readFileSync(file)
|
|
67
|
-
res.writeHead(200, {
|
|
68
|
-
'content-type': contentType(file),
|
|
69
|
-
'content-length': data.length,
|
|
70
|
-
'x-content-type-options': 'nosniff',
|
|
71
|
-
'referrer-policy': 'no-referrer',
|
|
72
|
-
'cache-control': 'no-cache',
|
|
73
|
-
})
|
|
74
|
-
res.end(req.method === 'HEAD' ? undefined : data)
|
|
75
|
-
return true
|
|
76
|
-
} catch {
|
|
77
|
-
return false
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const leaseRoutes = async function editorLeaseRoutes(req, res, u) {
|
|
82
|
-
const claimPath = `${prefix}/claim`
|
|
83
|
-
const releasePath = `${prefix}/release`
|
|
84
|
-
const statusPath = `${prefix}/status`
|
|
85
|
-
if (u.pathname === claimPath && req.method === 'POST') {
|
|
86
|
-
const body = await readBody(req)
|
|
87
|
-
const result = lease.claim(String(body.id || ''), !!body.force)
|
|
88
|
-
send(req, res, result.ok ? 200 : 409, result)
|
|
89
|
-
return true
|
|
90
|
-
}
|
|
91
|
-
if (u.pathname === releasePath && req.method === 'POST') {
|
|
92
|
-
const body = await readBody(req)
|
|
93
|
-
send(req, res, 200, lease.release(String(body.id || '')))
|
|
94
|
-
return true
|
|
95
|
-
}
|
|
96
|
-
if (u.pathname === statusPath && req.method === 'GET') {
|
|
97
|
-
send(req, res, 200, { ok: true, connected: lease.isLive(), lease: lease.status() })
|
|
98
|
-
return true
|
|
99
|
-
}
|
|
100
|
-
return false
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return {
|
|
104
|
-
id,
|
|
105
|
-
lease,
|
|
106
|
-
routes() {
|
|
107
|
-
return [staticRoutes, leaseRoutes]
|
|
108
|
-
},
|
|
109
|
-
staticRoutes,
|
|
110
|
-
leaseRoutes,
|
|
111
|
-
status() {
|
|
112
|
-
return { connected: lease.isLive(), lease: lease.status() }
|
|
113
|
-
},
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
export function createEditorHostService(config = {}) {
|
|
118
|
-
return {
|
|
119
|
-
api: createServiceApi('editorHost', '1.0.0', ['static', 'lease', 'heartbeat', 'status']),
|
|
120
|
-
version: '0.1.0',
|
|
121
|
-
createEditorHost,
|
|
122
|
-
createLease: createEditorLease,
|
|
123
|
-
defaultHost: config.dir ? createEditorHost({ id: config.id || 'editor', ...config }) : null,
|
|
124
|
-
}
|
|
125
|
-
}
|
|
1
|
+
/** 由 build/sync-shared.mjs 生成:实现只存在于 @capyclaw/editor-host/src,此处零拷贝。 */
|
|
2
|
+
export * from '@capyclaw/editor-host/src'
|