@capyclaw/browser-workbench 0.1.0 → 0.1.18
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 +304 -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/{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 +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/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/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 +223 -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,625 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 浏览器编辑器客户端:租约/心跳/长轮询/镜像/应用/截图请求。
|
|
3
|
+
*
|
|
4
|
+
* 不直接碰 DOM 与具体编辑器;加载、dump、应用、截图渲染都由 adapter 注入。
|
|
5
|
+
* 该文件必须保持 browser-safe:禁止 import node:*。
|
|
6
|
+
*
|
|
7
|
+
* 生命周期加固(2026-11-17 跨会话串写事故后的结构整改):
|
|
8
|
+
* A 结构化取消——每 client 一个 AbortController,stop 即 abort,在飞请求立刻 reject;
|
|
9
|
+
* stopped 是终态(单调),ensureLinked/claimLoop/activate 都认它,不再复活旧 client。
|
|
10
|
+
* B 单写者所有权——adapter 包成 guardAdapter 边界,stopped 时一切写操作 no-op,
|
|
11
|
+
* 一条不变量替代散落的自觉检查,漏检也不会污染共享画面。
|
|
12
|
+
* D 确定性停机——stop() 可 await(排水在飞 pump/shoot),调用方确知旧 client 安静后
|
|
13
|
+
* 再 restore 新会话,取代「睡 3.5s 再 re-assert」的补丁。
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const DEFAULTS = {
|
|
17
|
+
pollTimeoutSec: 3,
|
|
18
|
+
claimRetryMs: 800,
|
|
19
|
+
activeClaimRetryMs: 1500,
|
|
20
|
+
heartbeatMs: 3000,
|
|
21
|
+
stateDebounceMs: 800,
|
|
22
|
+
errorRetryMs: 500,
|
|
23
|
+
shotIdleMs: 3000,
|
|
24
|
+
/* 待命 claim 连续失败时的退避上限:前几次保持快节奏(服务重启后尽快回连),
|
|
25
|
+
* 持续失败才指数退避到该上限,避免对宕机服务高频空转。 */
|
|
26
|
+
claimRetryMaxMs: 3000,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 单写者所有权边界(B):已停的 client 绝不能再改共享 adapter。
|
|
33
|
+
* 就地换会话后,旧 client 可能仍有在飞续体跑到 adapter 调用点;散落的 stopped
|
|
34
|
+
* 自觉检查容易漏,这里在 adapter 边界统一强制——stopped 时写操作(load/apply/
|
|
35
|
+
* applyUi/renderScreenshot)一律 no-op,读操作(dump/meta/isMounted/onChange)原样透传。
|
|
36
|
+
*/
|
|
37
|
+
function guardAdapter(adapter, isStopped) {
|
|
38
|
+
return {
|
|
39
|
+
...adapter,
|
|
40
|
+
load: (value, opts) => (isStopped() ? undefined : adapter.load(value, opts)),
|
|
41
|
+
apply: (cmds) => (isStopped() ? undefined : adapter.apply(cmds)),
|
|
42
|
+
applyUi: (cmds) => (isStopped() ? undefined : adapter.applyUi?.(cmds)),
|
|
43
|
+
renderScreenshot: (opts) => (isStopped() ? Promise.resolve('') : adapter.renderScreenshot?.(opts)),
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function createEditorClient({
|
|
48
|
+
api,
|
|
49
|
+
apiBase = '',
|
|
50
|
+
clientId,
|
|
51
|
+
sessionId = '',
|
|
52
|
+
shadow = false,
|
|
53
|
+
adapter,
|
|
54
|
+
authority = 'server',
|
|
55
|
+
documentKey = 'document',
|
|
56
|
+
documentPath = '/document',
|
|
57
|
+
storage = typeof localStorage !== 'undefined' ? localStorage : null,
|
|
58
|
+
workerFactory = null,
|
|
59
|
+
heartbeatWorkerUrl = './heartbeat-worker.js',
|
|
60
|
+
singletonKey = 'document-workbench-singleton',
|
|
61
|
+
intervals = {},
|
|
62
|
+
onStatus = () => {},
|
|
63
|
+
onNote = () => {},
|
|
64
|
+
} = {}) {
|
|
65
|
+
if (typeof api !== 'function') throw new Error('createEditorClient 需要 api(path, init)')
|
|
66
|
+
if (!adapter || typeof adapter.load !== 'function' || typeof adapter.dump !== 'function') {
|
|
67
|
+
throw new Error('createEditorClient 需要 adapter.load / adapter.dump')
|
|
68
|
+
}
|
|
69
|
+
if (authority !== 'server' && authority !== 'editor') {
|
|
70
|
+
throw new Error('authority 只能是 server 或 editor')
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const timing = { ...DEFAULTS, ...intervals }
|
|
74
|
+
const st = {
|
|
75
|
+
clientId,
|
|
76
|
+
active: false,
|
|
77
|
+
stopped: false,
|
|
78
|
+
heartbeat: null,
|
|
79
|
+
heartbeatWorker: null,
|
|
80
|
+
lastSeq: 0,
|
|
81
|
+
lastShotReq: 0,
|
|
82
|
+
lastUiSeq: 0,
|
|
83
|
+
busy: false,
|
|
84
|
+
shooting: false,
|
|
85
|
+
pumping: false,
|
|
86
|
+
watching: false,
|
|
87
|
+
onchange: null,
|
|
88
|
+
unsubscribe: null,
|
|
89
|
+
stateTimer: null,
|
|
90
|
+
shotCache: null,
|
|
91
|
+
singletonKey,
|
|
92
|
+
pendingShotReq: null,
|
|
93
|
+
pendingShotOpts: null,
|
|
94
|
+
lastClaimError: '',
|
|
95
|
+
restoreError: '',
|
|
96
|
+
claimFailures: 0,
|
|
97
|
+
/* 唤醒机制:online/focus/visibilitychange 到来时立即打断退避睡眠重试 claim,
|
|
98
|
+
* 不必等满退避间隔——网络恢复/回到前台时尽快回连。 */
|
|
99
|
+
wakeFlag: false,
|
|
100
|
+
wakeResolve: null,
|
|
101
|
+
wakeListeners: [],
|
|
102
|
+
/* A:结构化取消——stop 即 abort,在飞请求立刻 reject。 */
|
|
103
|
+
abort: new AbortController(),
|
|
104
|
+
/* D:可 await 停机排水——记录在飞 pump/shoot 任务,stop 等它们 settle。 */
|
|
105
|
+
pumpTask: null,
|
|
106
|
+
shootTask: null,
|
|
107
|
+
stopTask: null,
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/* B:把 adapter 包成所有权边界(重赋值参数绑定,下文所有 adapter.* 自动走守卫)。
|
|
111
|
+
* 上面校验用的是原始 adapter;包装后 stopped 时写操作一律 no-op。 */
|
|
112
|
+
adapter = guardAdapter(adapter, () => st.stopped)
|
|
113
|
+
|
|
114
|
+
const note = (message, kind = '') => onNote(message, kind)
|
|
115
|
+
const badge = (text, kind = '') => onStatus(text, kind)
|
|
116
|
+
|
|
117
|
+
/* 失败原因必须留在页面上。以前 claim/restore 的 catch 一律静默 return false,
|
|
118
|
+
* 现象是「编辑器装起来了,可是里面不是我的稿子,也没有任何提示」——用户看到的是
|
|
119
|
+
* 一扇别人的示例稿,agent 看到的是 owner=''、editorConnected=false,两边都没线索。 */
|
|
120
|
+
const describeError = (caught) => {
|
|
121
|
+
const text = String((caught && caught.message) || caught || '').trim()
|
|
122
|
+
return text || '原因不明'
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/* 唤醒:置位标志并打断当前退避睡眠。事件(online/focus/可见)到来时调用。 */
|
|
126
|
+
function wake() {
|
|
127
|
+
st.wakeFlag = true
|
|
128
|
+
const resolve = st.wakeResolve
|
|
129
|
+
st.wakeResolve = null
|
|
130
|
+
if (resolve) resolve()
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/* 可被唤醒打断的睡眠:有待处理唤醒则立即返回,否则睡满 ms。 */
|
|
134
|
+
function sleepOrWake(ms) {
|
|
135
|
+
if (st.stopped) return Promise.resolve()
|
|
136
|
+
if (st.wakeFlag) { st.wakeFlag = false; return Promise.resolve() }
|
|
137
|
+
return new Promise((resolve) => {
|
|
138
|
+
const timer = setTimeout(() => { st.wakeResolve = null; resolve() }, ms)
|
|
139
|
+
st.wakeResolve = () => { clearTimeout(timer); st.wakeResolve = null; resolve() }
|
|
140
|
+
})
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/* 待命 claim 的退避:前 3 次保持基础节奏(重启后尽快回连),之后指数退避到上限,
|
|
144
|
+
* 叠加 ±25% 抖动避免多窗口同时重试造成惊群。成功 claim 会把 failures 清零。 */
|
|
145
|
+
function claimDelay(failures) {
|
|
146
|
+
const base = timing.claimRetryMs
|
|
147
|
+
if (failures <= 3) return base
|
|
148
|
+
const exp = Math.min(timing.claimRetryMaxMs, base * Math.pow(1.6, failures - 3))
|
|
149
|
+
return Math.round(exp * (0.75 + Math.random() * 0.5))
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function request(path, init) {
|
|
153
|
+
/* A:所有请求挂本 client 的取消信号;stop() abort 后在飞请求立刻 reject。 */
|
|
154
|
+
return api(path, { ...(init || {}), signal: st.abort.signal })
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function post(path, body) {
|
|
158
|
+
return request(path, {
|
|
159
|
+
method: 'POST',
|
|
160
|
+
headers: { 'content-type': 'application/json' },
|
|
161
|
+
body: JSON.stringify(body),
|
|
162
|
+
})
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async function claim(force) {
|
|
166
|
+
try {
|
|
167
|
+
/* 能力/版本随 claim 上报,服务端存进 owner.meta;
|
|
168
|
+
* 工具可据此判断页面是否支持某个 op(例如 import-pptx)。 */
|
|
169
|
+
const meta = typeof adapter.meta === 'function' ? adapter.meta() : null
|
|
170
|
+
/* 影子显示器:meta 声明 shadow:true,服务端/工具据此区分「兜底无头渲染」与真实
|
|
171
|
+
* 用户窗口。影子永远礼貌 claim(force=false),真实窗口到来时经租约 'lost' 让位。 */
|
|
172
|
+
const claimMeta = shadow ? { ...(meta && typeof meta === 'object' ? meta : {}), shadow: true } : meta
|
|
173
|
+
const result = await post('/claim', {
|
|
174
|
+
id: st.clientId,
|
|
175
|
+
force: !!force,
|
|
176
|
+
...(claimMeta && typeof claimMeta === 'object' ? { meta: claimMeta } : {}),
|
|
177
|
+
})
|
|
178
|
+
if (result && result.ok) {
|
|
179
|
+
st.lastClaimError = ''
|
|
180
|
+
return true
|
|
181
|
+
}
|
|
182
|
+
st.lastClaimError = String((result && (result.error || result.reason)) || '服务端拒绝了 claim')
|
|
183
|
+
return false
|
|
184
|
+
} catch (caught) {
|
|
185
|
+
st.lastClaimError = describeError(caught)
|
|
186
|
+
return false
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async function ensureLinked() {
|
|
191
|
+
for (let attempt = 1; ; attempt += 1) {
|
|
192
|
+
if (st.stopped) return /* A:已停的 client 不再无限重试 claim(换会话后旧 client 失效) */
|
|
193
|
+
if (await claim(!shadow)) return
|
|
194
|
+
/* 无限重试本身没问题,但一声不响就成问题了:第一次失败就要把原因说出来。 */
|
|
195
|
+
if (attempt === 1 || attempt % 10 === 0) {
|
|
196
|
+
const reason = st.lastClaimError || '原因不明'
|
|
197
|
+
note(`链接编辑器失败(第 ${attempt} 次):${reason}`, 'error')
|
|
198
|
+
badge('离线', 'warn')
|
|
199
|
+
}
|
|
200
|
+
await sleepOrWake(claimDelay(attempt))
|
|
201
|
+
if (st.stopped) return
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async function restore() {
|
|
206
|
+
try {
|
|
207
|
+
const doc = await request('')
|
|
208
|
+
const value = doc && doc[documentKey]
|
|
209
|
+
if (value !== undefined && value !== null) await adapter.load(value, { origin: 'server' })
|
|
210
|
+
st.lastSeq = Number(doc && doc.seq) || 0
|
|
211
|
+
st.lastUiSeq = Number(doc && doc.ui_seq) || 0
|
|
212
|
+
st.restoreError = ''
|
|
213
|
+
return doc
|
|
214
|
+
} catch (caught) {
|
|
215
|
+
/* 镜像不可用 = 保留编辑器默认文档;但必须把原因写在页面上,
|
|
216
|
+
* 否则现象就是「打开的是别人的示例稿」,没人知道为什么。 */
|
|
217
|
+
st.restoreError = describeError(caught)
|
|
218
|
+
note(`读取服务端文档失败:${st.restoreError}(当前显示的是编辑器自带的示例文档)`, 'warn')
|
|
219
|
+
return null
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
async function activate() {
|
|
224
|
+
if (st.stopped) return /* A:stopped 是终态,已停的 client 不能被复活(生命周期单调) */
|
|
225
|
+
if (st.active) return
|
|
226
|
+
st.active = true
|
|
227
|
+
/* 影子不写单例键:否则真实 GUI 窗口会因 storage 事件误入待命。影子的所有权
|
|
228
|
+
* 完全由租约治理(让位走 worker 'lost'),不参与 localStorage 单例选举。 */
|
|
229
|
+
if (!shadow) {
|
|
230
|
+
try { storage?.setItem(st.singletonKey, st.clientId) } catch { /* quota / privacy mode */ }
|
|
231
|
+
}
|
|
232
|
+
badge('', 'ok')
|
|
233
|
+
/* 别把 restore 刚写出来的失败原因一抹了事:那条提示是用户唯一能看到的线索。 */
|
|
234
|
+
if (!st.restoreError) note('')
|
|
235
|
+
watch()
|
|
236
|
+
pump()
|
|
237
|
+
startHeartbeatWorker()
|
|
238
|
+
|
|
239
|
+
/* 补发未完成的截图请求:编辑器重启后旧请求的结果丢失。 */
|
|
240
|
+
try {
|
|
241
|
+
const status = await request('/status')
|
|
242
|
+
if (status && status.shot_req > 0
|
|
243
|
+
&& (!status.shot || (status.shot.req | 0) < status.shot_req)) {
|
|
244
|
+
st.lastShotReq = status.shot_req
|
|
245
|
+
shoot(status.shot_req, status.shot_opts)
|
|
246
|
+
}
|
|
247
|
+
} catch { /* 下一轮 pump 兜底 */ }
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
async function claimLoop() {
|
|
251
|
+
/* 影子不监听单例 storage 事件(它不写单例键,也不因 GUI 写键而待命);
|
|
252
|
+
* 影子的待命/让位一律由租约状态驱动。 */
|
|
253
|
+
if (!shadow && storage && typeof window !== 'undefined' && window.addEventListener) {
|
|
254
|
+
window.addEventListener('storage', (event) => {
|
|
255
|
+
if (event.key === st.singletonKey && event.newValue && event.newValue !== st.clientId) {
|
|
256
|
+
if (st.active) {
|
|
257
|
+
st.active = false
|
|
258
|
+
stopHeartbeatWorker()
|
|
259
|
+
badge('', 'standby')
|
|
260
|
+
note('')
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
})
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/* 网络恢复 / 回到前台 / 标签页重新可见时立即唤醒重试,不等满退避间隔。
|
|
267
|
+
* 监听器登记进 st.wakeListeners,stop() 时统一摘除(不泄漏)。 */
|
|
268
|
+
const onWake = () => wake()
|
|
269
|
+
if (typeof window !== 'undefined' && window.addEventListener) {
|
|
270
|
+
window.addEventListener('online', onWake)
|
|
271
|
+
window.addEventListener('focus', onWake)
|
|
272
|
+
st.wakeListeners.push([window, 'online', onWake], [window, 'focus', onWake])
|
|
273
|
+
}
|
|
274
|
+
if (typeof document !== 'undefined' && document.addEventListener) {
|
|
275
|
+
const onVisible = () => { if (!document.hidden) wake() }
|
|
276
|
+
document.addEventListener('visibilitychange', onVisible)
|
|
277
|
+
st.wakeListeners.push([document, 'visibilitychange', onVisible])
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/* 从第一刻就开始看内容:待命窗口也要跟上,不能等抢到租约才第一次读。
|
|
281
|
+
* pump() 自己幂等(st.pumping 守着),activate() 里再调一次是空操作;
|
|
282
|
+
* 写、截图、回执仍然只由 active 窗口做。 */
|
|
283
|
+
pump()
|
|
284
|
+
|
|
285
|
+
/* 影子首 claim 也礼貌(force=false):真实窗口持租约时影子安静待命,绝不抢占。 */
|
|
286
|
+
let firstClaim = !shadow
|
|
287
|
+
for (;;) {
|
|
288
|
+
if (st.stopped) break
|
|
289
|
+
if (!st.active) {
|
|
290
|
+
if (await claim(firstClaim)) {
|
|
291
|
+
firstClaim = false
|
|
292
|
+
st.claimFailures = 0
|
|
293
|
+
await activate()
|
|
294
|
+
} else {
|
|
295
|
+
firstClaim = false
|
|
296
|
+
st.claimFailures += 1
|
|
297
|
+
badge('', 'standby')
|
|
298
|
+
/* 第 1 次失败就把原因说出来;之后每 10 次再说一次,不刷屏也不静默。 */
|
|
299
|
+
if (st.claimFailures === 1 || st.claimFailures % 10 === 0) {
|
|
300
|
+
note(`链接编辑器失败(第 ${st.claimFailures} 次):${st.lastClaimError || '原因不明'}`, 'warn')
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
if (st.active) await sleepOrWake(timing.activeClaimRetryMs)
|
|
305
|
+
else await sleepOrWake(claimDelay(st.claimFailures))
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** editor authority:应用一批命令并回执。 */
|
|
310
|
+
async function apply(doc) {
|
|
311
|
+
if (st.stopped) return /* 已停的 client 不再应用命令/回执(就地换会话后旧 client 失效) */
|
|
312
|
+
st.busy = true
|
|
313
|
+
let applied = 0
|
|
314
|
+
let error = ''
|
|
315
|
+
try {
|
|
316
|
+
applied = await adapter.apply(doc.cmds || [])
|
|
317
|
+
if (!Number.isFinite(applied)) applied = (doc.cmds || []).length
|
|
318
|
+
} catch (caught) {
|
|
319
|
+
error = String((caught && caught.message) || caught)
|
|
320
|
+
if (!adapter.isMounted?.()) st.lastSeq = 0
|
|
321
|
+
}
|
|
322
|
+
try {
|
|
323
|
+
await post('/ack', { seq: doc.seq, applied, error })
|
|
324
|
+
} catch { /* 回执失败由下一轮轮询补 */ }
|
|
325
|
+
st.busy = false
|
|
326
|
+
note(error ? `第 ${doc.seq} 队应用失败:${error}` : `第 ${doc.seq} 队已应用(${applied} 条)`, error ? 'error' : 'ok')
|
|
327
|
+
Promise.resolve().then(() => pushState(doc.seq)).catch(() => {})
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** 本地快捷命令:直接改编辑器,然后把镜像推回服务端。 */
|
|
331
|
+
async function applyLocal(commands) {
|
|
332
|
+
if (st.stopped) return /* 已停的 client 不再改共享编辑器 */
|
|
333
|
+
st.busy = true
|
|
334
|
+
try {
|
|
335
|
+
await adapter.apply(commands || [])
|
|
336
|
+
await pushState()
|
|
337
|
+
} finally {
|
|
338
|
+
st.busy = false
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** 经服务端权威批次执行命令(POST 主端点 → runBatch):与 agent 写入同一条
|
|
343
|
+
* 路径——服务端 applyOp + journal + 串行队列,UI 不再私设写路径。返回轻量
|
|
344
|
+
* 回执 {seq, applied_seq, applied, error};镜像同步交给 watch 循环。 */
|
|
345
|
+
async function pushCommands(commands, title) {
|
|
346
|
+
return post('', { cmds: commands || [], ...(title ? { title } : {}) })
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** 单轮长轮询;pump 循环调用它,测试也可单独调用。
|
|
350
|
+
*
|
|
351
|
+
* 待命(standby)也要照着看:另一个窗口在编辑时,这扇窗只是不能写,但必须继续
|
|
352
|
+
* 跟上内容——否则用户会盯着一份已经过期的画面(甚至空态),以为「稿子没生成」。
|
|
353
|
+
* 写、截图、回执仍然只在 active 时才做。 */
|
|
354
|
+
async function pumpOnce() {
|
|
355
|
+
if (st.stopped) return false
|
|
356
|
+
const doc = await request(`/watch?seq=${st.lastSeq}&shot=${st.lastShotReq}&ui=${st.lastUiSeq}&timeout=${timing.pollTimeoutSec}`)
|
|
357
|
+
/* A 已 abort 在飞长轮询;这里再认一次 stopped 是边界兜底(B 的 guardAdapter
|
|
358
|
+
* 也保证即使漏检,已停 client 的 adapter.load 仍是 no-op,不会灌旧会话 deck)。 */
|
|
359
|
+
if (st.stopped || !doc) return false
|
|
360
|
+
|
|
361
|
+
/* 服务端 shot_req 计数被重置(runtime 换代/服务重启)时回退本地游标,
|
|
362
|
+
否则 doc.shot_req 永远小于旧游标,截图从此失灵直到页面刷新。 */
|
|
363
|
+
if ((doc.shot_req | 0) < (st.lastShotReq | 0)) st.lastShotReq = doc.shot_req | 0
|
|
364
|
+
if (st.active && doc.shot_req && doc.shot_req > st.lastShotReq) {
|
|
365
|
+
st.lastShotReq = doc.shot_req
|
|
366
|
+
shoot(doc.shot_req, doc.shot_opts)
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
if ((doc.seq | 0) !== (st.lastSeq | 0)) {
|
|
370
|
+
st.lastSeq = doc.seq | 0
|
|
371
|
+
if (authority === 'editor') {
|
|
372
|
+
if (st.active && doc.cmds && doc.cmds.length) await apply(doc)
|
|
373
|
+
} else if (st.lastSeq > 0) {
|
|
374
|
+
st.busy = true
|
|
375
|
+
try {
|
|
376
|
+
const mirror = await request(documentPath)
|
|
377
|
+
if (st.stopped) return false /* 取镜像途中被停:不 load(finally 会复位 busy) */
|
|
378
|
+
const value = mirror && mirror[documentKey]
|
|
379
|
+
if (value !== undefined) await adapter.load(value, { origin: 'server' })
|
|
380
|
+
} catch { /* 镜像失败下一轮重试 */ }
|
|
381
|
+
finally { st.busy = false }
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if ((doc.ui_seq | 0) > (st.lastUiSeq | 0)) {
|
|
386
|
+
st.lastUiSeq = doc.ui_seq | 0
|
|
387
|
+
st.busy = true
|
|
388
|
+
try {
|
|
389
|
+
const uiDoc = await request('/ui')
|
|
390
|
+
if (st.stopped) return false /* 同镜像分支:已停的 client 不再动共享 adapter */
|
|
391
|
+
await adapter.applyUi?.(uiDoc.ui || [])
|
|
392
|
+
} catch { /* 页面级命令失败不阻塞下一轮 */ }
|
|
393
|
+
finally { st.busy = false }
|
|
394
|
+
}
|
|
395
|
+
return true
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
function pump() {
|
|
399
|
+
if (st.pumping) return st.pumpTask
|
|
400
|
+
st.pumping = true
|
|
401
|
+
/* D:记录在飞 pump 任务,stop() 排水时 await 它确知轮询已安静。 */
|
|
402
|
+
st.pumpTask = (async () => {
|
|
403
|
+
try {
|
|
404
|
+
for (;;) {
|
|
405
|
+
if (st.stopped) break
|
|
406
|
+
/* 待命也继续轮询:看得见最新内容,只是写不进去(写与截图都在 pumpOnce 里按
|
|
407
|
+
* active 判过)。这样「另一个窗口在编辑」不会让这扇窗停在旧画面。 */
|
|
408
|
+
try { await pumpOnce() } catch { if (!st.stopped) await sleep(timing.errorRetryMs) }
|
|
409
|
+
}
|
|
410
|
+
} finally {
|
|
411
|
+
st.pumping = false
|
|
412
|
+
}
|
|
413
|
+
})()
|
|
414
|
+
return st.pumpTask
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** 截图:永远回传新渲染帧,不回旧缓存。
|
|
418
|
+
* opts 是服务端随请求带来的渲染选项(例如只拍第 N 页 / 指定宽度),
|
|
419
|
+
* adapter 不认识就直接忽略——协议对老适配器透明。 */
|
|
420
|
+
function shoot(req, opts = null) {
|
|
421
|
+
if (st.shooting) {
|
|
422
|
+
st.pendingShotReq = req
|
|
423
|
+
st.pendingShotOpts = opts
|
|
424
|
+
return st.shootTask
|
|
425
|
+
}
|
|
426
|
+
if (st.stopped) return Promise.resolve() /* 已停的 client 不再渲染/回传截图 */
|
|
427
|
+
st.shooting = true
|
|
428
|
+
/* D:记录在飞 shoot 任务,stop() 排水时 await 它。 */
|
|
429
|
+
st.shootTask = (async () => {
|
|
430
|
+
try {
|
|
431
|
+
const idleDeadline = Date.now() + timing.shotIdleMs
|
|
432
|
+
while (st.busy && !st.stopped && Date.now() < idleDeadline) await sleep(100)
|
|
433
|
+
|
|
434
|
+
let current = req
|
|
435
|
+
let currentOpts = opts
|
|
436
|
+
for (;;) {
|
|
437
|
+
let data = ''
|
|
438
|
+
for (let attempt = 0; attempt < 2 && !data; attempt++) {
|
|
439
|
+
try { data = await adapter.renderScreenshot(currentOpts || undefined) } catch { await sleep(400) }
|
|
440
|
+
}
|
|
441
|
+
if (!data) {
|
|
442
|
+
try { await post('/shot-result', { req: current, data: '' }) } catch { /* 工具端会快速失败 */ }
|
|
443
|
+
throw new Error('画面渲染为空')
|
|
444
|
+
}
|
|
445
|
+
st.shotCache = data
|
|
446
|
+
if (st.stopped) break /* 渲染途中被停:不回传旧会话画面 */
|
|
447
|
+
await post('/shot-result', { req: current, data })
|
|
448
|
+
if (st.pendingShotReq && st.pendingShotReq !== current) {
|
|
449
|
+
current = st.pendingShotReq
|
|
450
|
+
currentOpts = st.pendingShotOpts
|
|
451
|
+
st.pendingShotReq = null
|
|
452
|
+
st.pendingShotOpts = null
|
|
453
|
+
continue
|
|
454
|
+
}
|
|
455
|
+
break
|
|
456
|
+
}
|
|
457
|
+
} catch (caught) {
|
|
458
|
+
badge('', 'error')
|
|
459
|
+
note('截图失败:' + String((caught && caught.message) || caught))
|
|
460
|
+
} finally {
|
|
461
|
+
st.shooting = false
|
|
462
|
+
st.pendingShotReq = null
|
|
463
|
+
st.pendingShotOpts = null
|
|
464
|
+
}
|
|
465
|
+
})()
|
|
466
|
+
return st.shootTask
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/** 编辑器 change → 防抖 POST /state(agent 的「看」)。 */
|
|
470
|
+
function watch() {
|
|
471
|
+
if (st.watching || typeof adapter.onChange !== 'function') return
|
|
472
|
+
st.watching = true
|
|
473
|
+
st.onchange = () => {
|
|
474
|
+
clearTimeout(st.stateTimer)
|
|
475
|
+
st.stateTimer = setTimeout(() => { pushState().catch(() => {}) }, timing.stateDebounceMs)
|
|
476
|
+
}
|
|
477
|
+
st.unsubscribe = adapter.onChange(st.onchange) || null
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
async function pushState(seq) {
|
|
481
|
+
if (!st.active) return
|
|
482
|
+
if (adapter.isMounted && !adapter.isMounted()) return
|
|
483
|
+
try {
|
|
484
|
+
/* E:镜像携带客户端身份与会话声明,供服务端 journal 留痕与 validateState
|
|
485
|
+
* 校验(异会话镜像响亮拒绝)。sessionId 可选,旧宿主不带也兼容。 */
|
|
486
|
+
const body = { [documentKey]: adapter.dump(), clientId: st.clientId }
|
|
487
|
+
if (sessionId) body.sessionId = sessionId
|
|
488
|
+
if (seq != null) body.seq = Number(seq)
|
|
489
|
+
else if (st.lastSeq) body.seq = Number(st.lastSeq)
|
|
490
|
+
await post('/state', body)
|
|
491
|
+
} catch { /* 下一轮 change 再推 */ }
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
function release() {
|
|
495
|
+
try {
|
|
496
|
+
if (typeof navigator !== 'undefined' && navigator.sendBeacon) {
|
|
497
|
+
const body = new Blob([JSON.stringify({ id: st.clientId })], { type: 'application/json' })
|
|
498
|
+
navigator.sendBeacon(apiBase + '/release', body)
|
|
499
|
+
return
|
|
500
|
+
}
|
|
501
|
+
/* release 常在 stop() 之后调用(换会话/关页),不能挂已 abort 的信号——
|
|
502
|
+
* 直接走 api(不经 request 的 signal),确保租约释放送达。 */
|
|
503
|
+
api('/release', {
|
|
504
|
+
method: 'POST',
|
|
505
|
+
headers: { 'content-type': 'application/json' },
|
|
506
|
+
body: JSON.stringify({ id: st.clientId }),
|
|
507
|
+
}).catch(() => {})
|
|
508
|
+
} catch { /* 页面关闭路径不能抛 */ }
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/* ---- Web Worker 心跳(不受标签页节流影响) ---- */
|
|
512
|
+
function startHeartbeatWorker() {
|
|
513
|
+
if (st.heartbeatWorker) return
|
|
514
|
+
try {
|
|
515
|
+
const worker = workerFactory
|
|
516
|
+
? workerFactory()
|
|
517
|
+
: (typeof Worker !== 'undefined' ? new Worker(heartbeatWorkerUrl) : null)
|
|
518
|
+
if (!worker) { startFallbackHeartbeat(); return }
|
|
519
|
+
st.heartbeatWorker = worker
|
|
520
|
+
if (st.heartbeat) { clearInterval(st.heartbeat); st.heartbeat = null }
|
|
521
|
+
worker.onmessage = (event) => {
|
|
522
|
+
const msg = event.data || {}
|
|
523
|
+
if (msg.type === 'fatal') {
|
|
524
|
+
/* 连续多次心跳失败才降级到主线程——瞬时单次失败让 worker 自己重试,
|
|
525
|
+
* 保住抗标签页节流的能力(隐藏窗口主线程定时器会被浏览器节流到分钟级)。 */
|
|
526
|
+
stopHeartbeatWorker()
|
|
527
|
+
startFallbackHeartbeat()
|
|
528
|
+
} else if (msg.type === 'lost') {
|
|
529
|
+
/* 租约被别的窗口抢走:worker 已自停。清掉它(重连后重建),回到待命,
|
|
530
|
+
* 由 claimLoop 礼貌重 claim——不 force 抢回,尊重当前所有者。 */
|
|
531
|
+
stopHeartbeatWorker()
|
|
532
|
+
if (st.active) {
|
|
533
|
+
st.active = false
|
|
534
|
+
badge('', 'standby')
|
|
535
|
+
note('编辑器所有权已转移到其他窗口,正在重新链接', 'warn')
|
|
536
|
+
}
|
|
537
|
+
} else if (msg.type === 'recovered') {
|
|
538
|
+
if (st.active) badge('', 'ok')
|
|
539
|
+
}
|
|
540
|
+
/* 瞬时 'error' 不再立即降级:worker 内部重试,达到 fatal 阈值才降级。 */
|
|
541
|
+
}
|
|
542
|
+
worker.onerror = () => {
|
|
543
|
+
stopHeartbeatWorker()
|
|
544
|
+
startFallbackHeartbeat()
|
|
545
|
+
}
|
|
546
|
+
worker.postMessage({ type: 'start', data: { clientId: st.clientId, apiBase } })
|
|
547
|
+
} catch {
|
|
548
|
+
startFallbackHeartbeat()
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
function stopHeartbeatWorker() {
|
|
553
|
+
if (st.heartbeatWorker) {
|
|
554
|
+
try { st.heartbeatWorker.postMessage({ type: 'stop' }) } catch { /* ignore */ }
|
|
555
|
+
try { st.heartbeatWorker.terminate() } catch { /* ignore */ }
|
|
556
|
+
st.heartbeatWorker = null
|
|
557
|
+
}
|
|
558
|
+
if (st.heartbeat) {
|
|
559
|
+
clearInterval(st.heartbeat)
|
|
560
|
+
st.heartbeat = null
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
function startFallbackHeartbeat() {
|
|
565
|
+
if (st.heartbeat) return
|
|
566
|
+
st.heartbeat = setInterval(async () => {
|
|
567
|
+
if (!st.active) return
|
|
568
|
+
const ok = await claim()
|
|
569
|
+
if (!ok) {
|
|
570
|
+
st.active = false
|
|
571
|
+
badge('', 'standby')
|
|
572
|
+
note(`心跳丢失:${st.lastClaimError || '原因不明'}(正在重连)`, 'warn')
|
|
573
|
+
}
|
|
574
|
+
}, timing.heartbeatMs)
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
async function stop() {
|
|
578
|
+
if (st.stopped) return st.stopTask /* 幂等:重复 stop 返回同一个排水 Promise */
|
|
579
|
+
st.stopped = true
|
|
580
|
+
st.active = false
|
|
581
|
+
clearTimeout(st.stateTimer)
|
|
582
|
+
st.stateTimer = null
|
|
583
|
+
stopHeartbeatWorker()
|
|
584
|
+
/* 摘除唤醒监听并唤醒一次:让正在退避睡眠的 claimLoop 立刻认 stopped 退出,
|
|
585
|
+
* 不再泄漏 window/document 监听器(就地换会话会反复 create/stop)。 */
|
|
586
|
+
for (const [target, type, handler] of st.wakeListeners) {
|
|
587
|
+
try { target.removeEventListener(type, handler) } catch { /* ignore */ }
|
|
588
|
+
}
|
|
589
|
+
st.wakeListeners = []
|
|
590
|
+
wake()
|
|
591
|
+
try { st.unsubscribe?.() } catch { /* ignore */ }
|
|
592
|
+
st.unsubscribe = null
|
|
593
|
+
st.watching = false
|
|
594
|
+
/* A:abort 在飞请求(watch/mirror/ui/shot-result/claim),让它们立刻 reject。 */
|
|
595
|
+
try { st.abort.abort() } catch { /* ignore */ }
|
|
596
|
+
/* D:排水——等在飞 pump/shoot settle,调用方可 await stop() 确知旧 client 已安静,
|
|
597
|
+
* 再 restore 新会话(确定性切换握手,取代 SWITCH_REASSERT_MS 睡觉补丁)。 */
|
|
598
|
+
st.stopTask = Promise.allSettled([st.pumpTask, st.shootTask].filter(Boolean)).then(() => {})
|
|
599
|
+
return st.stopTask
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
return {
|
|
603
|
+
state: st,
|
|
604
|
+
claim,
|
|
605
|
+
ensureLinked,
|
|
606
|
+
restore,
|
|
607
|
+
activate,
|
|
608
|
+
claimLoop,
|
|
609
|
+
apply,
|
|
610
|
+
applyLocal,
|
|
611
|
+
pushCommands,
|
|
612
|
+
pumpOnce,
|
|
613
|
+
pump,
|
|
614
|
+
shoot,
|
|
615
|
+
watch,
|
|
616
|
+
pushState,
|
|
617
|
+
release,
|
|
618
|
+
startHeartbeatWorker,
|
|
619
|
+
stopHeartbeatWorker,
|
|
620
|
+
startFallbackHeartbeat,
|
|
621
|
+
stop,
|
|
622
|
+
note,
|
|
623
|
+
badge,
|
|
624
|
+
}
|
|
625
|
+
}
|