@jadelclemens/pi-forge 0.0.0-stage → 0.6.0
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/CHANGELOG.md +486 -0
- package/LICENSE +21 -0
- package/PUBLIC_API.md +5 -0
- package/README.md +204 -2
- package/README.zh-CN.md +128 -0
- package/SUBAGENT_ADAPTER_CONTRACT.md +7 -0
- package/dist/active-state.d.ts +148 -0
- package/dist/active-state.d.ts.map +1 -0
- package/dist/active-state.js +374 -0
- package/dist/active-state.js.map +1 -0
- package/dist/agent-profile.d.ts +51 -0
- package/dist/agent-profile.d.ts.map +1 -0
- package/dist/agent-profile.js +197 -0
- package/dist/agent-profile.js.map +1 -0
- package/dist/capabilities.d.ts +39 -0
- package/dist/capabilities.d.ts.map +1 -0
- package/dist/capabilities.js +160 -0
- package/dist/capabilities.js.map +1 -0
- package/dist/capability-anchors.d.ts +45 -0
- package/dist/capability-anchors.d.ts.map +1 -0
- package/dist/capability-anchors.js +263 -0
- package/dist/capability-anchors.js.map +1 -0
- package/dist/capability-command.d.ts +4 -0
- package/dist/capability-command.d.ts.map +1 -0
- package/dist/capability-command.js +164 -0
- package/dist/capability-command.js.map +1 -0
- package/dist/capability-events.d.ts +79 -0
- package/dist/capability-events.d.ts.map +1 -0
- package/dist/capability-events.js +478 -0
- package/dist/capability-events.js.map +1 -0
- package/dist/capability-projection.d.ts +22 -0
- package/dist/capability-projection.d.ts.map +1 -0
- package/dist/capability-projection.js +273 -0
- package/dist/capability-projection.js.map +1 -0
- package/dist/capability-protocol.d.ts +21 -0
- package/dist/capability-protocol.d.ts.map +1 -0
- package/dist/capability-protocol.js +15 -0
- package/dist/capability-protocol.js.map +1 -0
- package/dist/capability-state.d.ts +74 -0
- package/dist/capability-state.d.ts.map +1 -0
- package/dist/capability-state.js +43 -0
- package/dist/capability-state.js.map +1 -0
- package/dist/capability-tool.d.ts +11 -0
- package/dist/capability-tool.d.ts.map +1 -0
- package/dist/capability-tool.js +25 -0
- package/dist/capability-tool.js.map +1 -0
- package/dist/capability-web-host.d.ts +11 -0
- package/dist/capability-web-host.d.ts.map +1 -0
- package/dist/capability-web-host.js +105 -0
- package/dist/capability-web-host.js.map +1 -0
- package/dist/catalog.d.ts +27 -0
- package/dist/catalog.d.ts.map +1 -0
- package/dist/catalog.js +59 -0
- package/dist/catalog.js.map +1 -0
- package/dist/codecs/agent-profile.d.ts +53 -0
- package/dist/codecs/agent-profile.d.ts.map +1 -0
- package/dist/codecs/agent-profile.js +176 -0
- package/dist/codecs/agent-profile.js.map +1 -0
- package/dist/codecs/capability.d.ts +71 -0
- package/dist/codecs/capability.d.ts.map +1 -0
- package/dist/codecs/capability.js +363 -0
- package/dist/codecs/capability.js.map +1 -0
- package/dist/codecs/index.d.ts +5 -0
- package/dist/codecs/index.d.ts.map +1 -0
- package/dist/codecs/index.js +3 -0
- package/dist/codecs/index.js.map +1 -0
- package/dist/codecs/prompt-stack.d.ts +18 -0
- package/dist/codecs/prompt-stack.d.ts.map +1 -0
- package/dist/codecs/prompt-stack.js +623 -0
- package/dist/codecs/prompt-stack.js.map +1 -0
- package/dist/command-contribution/index.d.ts +21 -0
- package/dist/command-contribution/index.d.ts.map +1 -0
- package/dist/command-contribution/index.js +14 -0
- package/dist/command-contribution/index.js.map +1 -0
- package/dist/compile-cycle.d.ts +26 -0
- package/dist/compile-cycle.d.ts.map +1 -0
- package/dist/compile-cycle.js +16 -0
- package/dist/compile-cycle.js.map +1 -0
- package/dist/compiler.d.ts +22 -0
- package/dist/compiler.d.ts.map +1 -0
- package/dist/compiler.js +737 -0
- package/dist/compiler.js.map +1 -0
- package/dist/context-diff-history.d.ts +69 -0
- package/dist/context-diff-history.d.ts.map +1 -0
- package/dist/context-diff-history.js +107 -0
- package/dist/context-diff-history.js.map +1 -0
- package/dist/context-diff-snapshot.d.ts +19 -0
- package/dist/context-diff-snapshot.d.ts.map +1 -0
- package/dist/context-diff-snapshot.js +146 -0
- package/dist/context-diff-snapshot.js.map +1 -0
- package/dist/context-diff.d.ts +70 -0
- package/dist/context-diff.d.ts.map +1 -0
- package/dist/context-diff.js +259 -0
- package/dist/context-diff.js.map +1 -0
- package/dist/extension-registry.d.ts +25 -0
- package/dist/extension-registry.d.ts.map +1 -0
- package/dist/extension-registry.js +9 -0
- package/dist/extension-registry.js.map +1 -0
- package/dist/forge-command.d.ts +10 -0
- package/dist/forge-command.d.ts.map +1 -0
- package/dist/forge-command.js +106 -0
- package/dist/forge-command.js.map +1 -0
- package/dist/forge-config.d.ts +9 -0
- package/dist/forge-config.d.ts.map +1 -0
- package/dist/forge-config.js +15 -0
- package/dist/forge-config.js.map +1 -0
- package/dist/forge-extensions.d.ts +28 -0
- package/dist/forge-extensions.d.ts.map +1 -0
- package/dist/forge-extensions.js +198 -0
- package/dist/forge-extensions.js.map +1 -0
- package/dist/forge-v1/analyzer.d.ts +3 -0
- package/dist/forge-v1/analyzer.d.ts.map +1 -0
- package/dist/forge-v1/analyzer.js +50 -0
- package/dist/forge-v1/analyzer.js.map +1 -0
- package/dist/forge-v1/index.d.ts +6 -0
- package/dist/forge-v1/index.d.ts.map +1 -0
- package/dist/forge-v1/index.js +13 -0
- package/dist/forge-v1/index.js.map +1 -0
- package/dist/forge-v1/parser.d.ts +3 -0
- package/dist/forge-v1/parser.d.ts.map +1 -0
- package/dist/forge-v1/parser.js +186 -0
- package/dist/forge-v1/parser.js.map +1 -0
- package/dist/forge-v1/renderer.d.ts +9 -0
- package/dist/forge-v1/renderer.d.ts.map +1 -0
- package/dist/forge-v1/renderer.js +181 -0
- package/dist/forge-v1/renderer.js.map +1 -0
- package/dist/forge-v1/types.d.ts +87 -0
- package/dist/forge-v1/types.d.ts.map +1 -0
- package/dist/forge-v1/types.js +4 -0
- package/dist/forge-v1/types.js.map +1 -0
- package/dist/host-compatibility.d.ts +13 -0
- package/dist/host-compatibility.d.ts.map +1 -0
- package/dist/host-compatibility.js +41 -0
- package/dist/host-compatibility.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +183 -0
- package/dist/index.js.map +1 -0
- package/dist/json-fingerprint.d.ts +11 -0
- package/dist/json-fingerprint.d.ts.map +1 -0
- package/dist/json-fingerprint.js +66 -0
- package/dist/json-fingerprint.js.map +1 -0
- package/dist/lifecycle.d.ts +46 -0
- package/dist/lifecycle.d.ts.map +1 -0
- package/dist/lifecycle.js +330 -0
- package/dist/lifecycle.js.map +1 -0
- package/dist/loader.d.ts +21 -0
- package/dist/loader.d.ts.map +1 -0
- package/dist/loader.js +58 -0
- package/dist/loader.js.map +1 -0
- package/dist/macro-engine.d.ts +18 -0
- package/dist/macro-engine.d.ts.map +1 -0
- package/dist/macro-engine.js +29 -0
- package/dist/macro-engine.js.map +1 -0
- package/dist/payload-capture.d.ts +31 -0
- package/dist/payload-capture.d.ts.map +1 -0
- package/dist/payload-capture.js +253 -0
- package/dist/payload-capture.js.map +1 -0
- package/dist/payload-command.d.ts +12 -0
- package/dist/payload-command.d.ts.map +1 -0
- package/dist/payload-command.js +197 -0
- package/dist/payload-command.js.map +1 -0
- package/dist/payload-state.d.ts +20 -0
- package/dist/payload-state.d.ts.map +1 -0
- package/dist/payload-state.js +20 -0
- package/dist/payload-state.js.map +1 -0
- package/dist/policy.d.ts +7 -0
- package/dist/policy.d.ts.map +1 -0
- package/dist/policy.js +41 -0
- package/dist/policy.js.map +1 -0
- package/dist/preset-command.d.ts +19 -0
- package/dist/preset-command.d.ts.map +1 -0
- package/dist/preset-command.js +261 -0
- package/dist/preset-command.js.map +1 -0
- package/dist/preview-inspection.d.ts +68 -0
- package/dist/preview-inspection.d.ts.map +1 -0
- package/dist/preview-inspection.js +293 -0
- package/dist/preview-inspection.js.map +1 -0
- package/dist/preview-text.d.ts +5 -0
- package/dist/preview-text.d.ts.map +1 -0
- package/dist/preview-text.js +27 -0
- package/dist/preview-text.js.map +1 -0
- package/dist/preview.d.ts +32 -0
- package/dist/preview.d.ts.map +1 -0
- package/dist/preview.js +272 -0
- package/dist/preview.js.map +1 -0
- package/dist/profile-command.d.ts +17 -0
- package/dist/profile-command.d.ts.map +1 -0
- package/dist/profile-command.js +385 -0
- package/dist/profile-command.js.map +1 -0
- package/dist/profile-service.d.ts +107 -0
- package/dist/profile-service.d.ts.map +1 -0
- package/dist/profile-service.js +248 -0
- package/dist/profile-service.js.map +1 -0
- package/dist/prompt-analysis.d.ts +24 -0
- package/dist/prompt-analysis.d.ts.map +1 -0
- package/dist/prompt-analysis.js +84 -0
- package/dist/prompt-analysis.js.map +1 -0
- package/dist/prompt-cache-warning.d.ts +23 -0
- package/dist/prompt-cache-warning.d.ts.map +1 -0
- package/dist/prompt-cache-warning.js +74 -0
- package/dist/prompt-cache-warning.js.map +1 -0
- package/dist/prompt-runtime.d.ts +19 -0
- package/dist/prompt-runtime.d.ts.map +1 -0
- package/dist/prompt-runtime.js +34 -0
- package/dist/prompt-runtime.js.map +1 -0
- package/dist/regex.d.ts +19 -0
- package/dist/regex.d.ts.map +1 -0
- package/dist/regex.js +507 -0
- package/dist/regex.js.map +1 -0
- package/dist/render-helpers.d.ts +38 -0
- package/dist/render-helpers.d.ts.map +1 -0
- package/dist/render-helpers.js +85 -0
- package/dist/render-helpers.js.map +1 -0
- package/dist/repositories/agent-profile.d.ts +36 -0
- package/dist/repositories/agent-profile.d.ts.map +1 -0
- package/dist/repositories/agent-profile.js +153 -0
- package/dist/repositories/agent-profile.js.map +1 -0
- package/dist/repositories/capability.d.ts +53 -0
- package/dist/repositories/capability.d.ts.map +1 -0
- package/dist/repositories/capability.js +294 -0
- package/dist/repositories/capability.js.map +1 -0
- package/dist/repositories/index.d.ts +3 -0
- package/dist/repositories/index.d.ts.map +1 -0
- package/dist/repositories/index.js +3 -0
- package/dist/repositories/index.js.map +1 -0
- package/dist/repositories/prompt-stack.d.ts +57 -0
- package/dist/repositories/prompt-stack.d.ts.map +1 -0
- package/dist/repositories/prompt-stack.js +175 -0
- package/dist/repositories/prompt-stack.js.map +1 -0
- package/dist/resource-identity.d.ts +33 -0
- package/dist/resource-identity.d.ts.map +1 -0
- package/dist/resource-identity.js +56 -0
- package/dist/resource-identity.js.map +1 -0
- package/dist/runtime/capability-runtime.d.ts +95 -0
- package/dist/runtime/capability-runtime.d.ts.map +1 -0
- package/dist/runtime/capability-runtime.js +991 -0
- package/dist/runtime/capability-runtime.js.map +1 -0
- package/dist/runtime/profile-runtime.d.ts +14 -0
- package/dist/runtime/profile-runtime.d.ts.map +1 -0
- package/dist/runtime/profile-runtime.js +56 -0
- package/dist/runtime/profile-runtime.js.map +1 -0
- package/dist/runtime/prompt-stack-runtime.d.ts +27 -0
- package/dist/runtime/prompt-stack-runtime.d.ts.map +1 -0
- package/dist/runtime/prompt-stack-runtime.js +149 -0
- package/dist/runtime/prompt-stack-runtime.js.map +1 -0
- package/dist/runtime/tool-policy-runtime.d.ts +25 -0
- package/dist/runtime/tool-policy-runtime.d.ts.map +1 -0
- package/dist/runtime/tool-policy-runtime.js +386 -0
- package/dist/runtime/tool-policy-runtime.js.map +1 -0
- package/dist/runtime/web-editor-runtime.d.ts +11 -0
- package/dist/runtime/web-editor-runtime.d.ts.map +1 -0
- package/dist/runtime/web-editor-runtime.js +141 -0
- package/dist/runtime/web-editor-runtime.js.map +1 -0
- package/dist/session-adapter.d.ts +30 -0
- package/dist/session-adapter.d.ts.map +1 -0
- package/dist/session-adapter.js +167 -0
- package/dist/session-adapter.js.map +1 -0
- package/dist/session-state.d.ts +23 -0
- package/dist/session-state.d.ts.map +1 -0
- package/dist/session-state.js +44 -0
- package/dist/session-state.js.map +1 -0
- package/dist/session-usage.d.ts +65 -0
- package/dist/session-usage.d.ts.map +1 -0
- package/dist/session-usage.js +134 -0
- package/dist/session-usage.js.map +1 -0
- package/dist/slot-renderers.d.ts +38 -0
- package/dist/slot-renderers.d.ts.map +1 -0
- package/dist/slot-renderers.js +285 -0
- package/dist/slot-renderers.js.map +1 -0
- package/dist/stack-migration.d.ts +35 -0
- package/dist/stack-migration.d.ts.map +1 -0
- package/dist/stack-migration.js +123 -0
- package/dist/stack-migration.js.map +1 -0
- package/dist/status-text.d.ts +6 -0
- package/dist/status-text.d.ts.map +1 -0
- package/dist/status-text.js +11 -0
- package/dist/status-text.js.map +1 -0
- package/dist/storage.d.ts +24 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +94 -0
- package/dist/storage.js.map +1 -0
- package/dist/subagent/fingerprints.d.ts +12 -0
- package/dist/subagent/fingerprints.d.ts.map +1 -0
- package/dist/subagent/fingerprints.js +17 -0
- package/dist/subagent/fingerprints.js.map +1 -0
- package/dist/subagent/host-port.d.ts +296 -0
- package/dist/subagent/host-port.d.ts.map +1 -0
- package/dist/subagent/host-port.js +560 -0
- package/dist/subagent/host-port.js.map +1 -0
- package/dist/subagent/index.d.ts +16 -0
- package/dist/subagent/index.d.ts.map +1 -0
- package/dist/subagent/index.js +15 -0
- package/dist/subagent/index.js.map +1 -0
- package/dist/subagent-host.d.ts +125 -0
- package/dist/subagent-host.d.ts.map +1 -0
- package/dist/subagent-host.js +327 -0
- package/dist/subagent-host.js.map +1 -0
- package/dist/template-render.d.ts +18 -0
- package/dist/template-render.d.ts.map +1 -0
- package/dist/template-render.js +232 -0
- package/dist/template-render.js.map +1 -0
- package/dist/types.d.ts +213 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/ui-contribution/contrib-port.d.ts +170 -0
- package/dist/ui-contribution/contrib-port.d.ts.map +1 -0
- package/dist/ui-contribution/contrib-port.js +640 -0
- package/dist/ui-contribution/contrib-port.js.map +1 -0
- package/dist/ui-contribution/index.d.ts +3 -0
- package/dist/ui-contribution/index.d.ts.map +1 -0
- package/dist/ui-contribution/index.js +2 -0
- package/dist/ui-contribution/index.js.map +1 -0
- package/dist/web-editor/client-script.d.ts +2 -0
- package/dist/web-editor/client-script.d.ts.map +1 -0
- package/dist/web-editor/client-script.generated.d.ts +2 -0
- package/dist/web-editor/client-script.generated.d.ts.map +1 -0
- package/dist/web-editor/client-script.generated.js +3 -0
- package/dist/web-editor/client-script.generated.js.map +1 -0
- package/dist/web-editor/client-script.js +2 -0
- package/dist/web-editor/client-script.js.map +1 -0
- package/dist/web-editor/client-styles.d.ts +2 -0
- package/dist/web-editor/client-styles.d.ts.map +1 -0
- package/dist/web-editor/client-styles.generated.d.ts +2 -0
- package/dist/web-editor/client-styles.generated.d.ts.map +1 -0
- package/dist/web-editor/client-styles.generated.js +3 -0
- package/dist/web-editor/client-styles.generated.js.map +1 -0
- package/dist/web-editor/client-styles.js +2 -0
- package/dist/web-editor/client-styles.js.map +1 -0
- package/dist/web-editor/contrib-service.d.ts +41 -0
- package/dist/web-editor/contrib-service.d.ts.map +1 -0
- package/dist/web-editor/contrib-service.js +174 -0
- package/dist/web-editor/contrib-service.js.map +1 -0
- package/dist/web-editor/index.d.ts +3 -0
- package/dist/web-editor/index.d.ts.map +1 -0
- package/dist/web-editor/index.js +2 -0
- package/dist/web-editor/index.js.map +1 -0
- package/dist/web-editor/line-diff.d.ts +28 -0
- package/dist/web-editor/line-diff.d.ts.map +1 -0
- package/dist/web-editor/line-diff.js +216 -0
- package/dist/web-editor/line-diff.js.map +1 -0
- package/dist/web-editor/page.d.ts +4 -0
- package/dist/web-editor/page.d.ts.map +1 -0
- package/dist/web-editor/page.js +26 -0
- package/dist/web-editor/page.js.map +1 -0
- package/dist/web-editor/schema-form.d.ts +63 -0
- package/dist/web-editor/schema-form.d.ts.map +1 -0
- package/dist/web-editor/schema-form.js +213 -0
- package/dist/web-editor/schema-form.js.map +1 -0
- package/dist/web-editor/server.d.ts +4 -0
- package/dist/web-editor/server.d.ts.map +1 -0
- package/dist/web-editor/server.js +637 -0
- package/dist/web-editor/server.js.map +1 -0
- package/dist/web-editor/styles.d.ts +2 -0
- package/dist/web-editor/styles.d.ts.map +1 -0
- package/dist/web-editor/styles.js +1744 -0
- package/dist/web-editor/styles.js.map +1 -0
- package/dist/web-editor/types.d.ts +304 -0
- package/dist/web-editor/types.d.ts.map +1 -0
- package/dist/web-editor/types.js +2 -0
- package/dist/web-editor/types.js.map +1 -0
- package/dist/web-host.d.ts +56 -0
- package/dist/web-host.d.ts.map +1 -0
- package/dist/web-host.js +750 -0
- package/dist/web-host.js.map +1 -0
- package/dist/workspace.d.ts +85 -0
- package/dist/workspace.d.ts.map +1 -0
- package/dist/workspace.js +409 -0
- package/dist/workspace.js.map +1 -0
- package/docs/README.md +49 -0
- package/docs/concepts/agent-profiles.md +62 -0
- package/docs/concepts/prompt-stacks.md +99 -0
- package/docs/design/0.5.1-plan.md +237 -0
- package/docs/design/README.md +37 -0
- package/docs/design/architecture-0.5.md +254 -0
- package/docs/design/archive/0.5-full-proposal/0.5-consumer-audit.md +62 -0
- package/docs/design/archive/0.5-full-proposal/0.5-inventory.md +208 -0
- package/docs/design/archive/0.5-full-proposal/0.5-phase0-decision-drafts.md +237 -0
- package/docs/design/archive/0.5-full-proposal/README.md +12 -0
- package/docs/design/archive/0.5-full-proposal/architecture-0.5.md +423 -0
- package/docs/design/archive/0.5-full-proposal/host-discovery-spike.md +103 -0
- package/docs/design/archive/0.5-full-proposal/template-language-spike.md +156 -0
- package/docs/design/archive/2026-09-12-system-update-design.md +229 -0
- package/docs/design/context-diff-plan.md +78 -0
- package/docs/design/decision-template.md +33 -0
- package/docs/design/pi-forge-system-update-design-notes.md +157 -0
- package/docs/design/roadmap-0.4-archive.md +216 -0
- package/docs/design/subagents/design-review.md +220 -0
- package/docs/design/subagents/interface-design.md +274 -0
- package/docs/design/subagents/sdk-spike-findings.md +117 -0
- package/docs/development/architecture-rules.md +148 -0
- package/docs/development/complexity-review.md +86 -0
- package/docs/development/release.md +59 -0
- package/docs/development/roadmap.md +117 -0
- package/docs/development/scoped-global-profiles-stacks.md +325 -0
- package/docs/development/setup.md +99 -0
- package/docs/getting-started.md +96 -0
- package/docs/guides/custom-macros-and-slots.md +69 -0
- package/docs/guides/debugging.md +39 -0
- package/docs/guides/delegation.md +105 -0
- package/docs/guides/migrating-to-0.5.md +136 -0
- package/docs/guides/pi-1-compatibility.md +160 -0
- package/docs/guides/use-cases.md +67 -0
- package/docs/guides/web-editor.md +161 -0
- package/docs/reference/active-state.md +77 -0
- package/docs/reference/capabilities.md +288 -0
- package/docs/reference/commands.md +89 -0
- package/docs/reference/configuration.md +70 -0
- package/docs/reference/features.md +305 -0
- package/docs/reference/macros-and-slots.md +103 -0
- package/docs/reference/provider-support.md +67 -0
- package/docs/reference/public-api.md +133 -0
- package/docs/reference/session-cache.md +112 -0
- package/docs/reference/stack-schema.md +243 -0
- package/docs/reference/subagent-host-port.md +57 -0
- package/docs/reference/ui-contribution-port.md +51 -0
- package/docs/zh-CN/README.md +41 -0
- package/docs/zh-CN/concepts/agent-profiles.md +48 -0
- package/docs/zh-CN/concepts/prompt-stacks.md +48 -0
- package/docs/zh-CN/getting-started.md +82 -0
- package/docs/zh-CN/guides/delegation.md +76 -0
- package/docs/zh-CN/guides/migrating-to-0.5.md +121 -0
- package/docs/zh-CN/guides/pi-1-compatibility.md +158 -0
- package/docs/zh-CN/guides/web-editor.md +134 -0
- package/docs/zh-CN/reference/capabilities.md +288 -0
- package/docs/zh-CN/reference/commands.md +89 -0
- package/docs/zh-CN/reference/provider-support.md +67 -0
- package/docs/zh-CN/reference/session-cache.md +112 -0
- package/examples/capabilities/review.json +12 -0
- package/examples/capabilities/write-tools.json +15 -0
- package/examples/custom-system-status-extension/index.ts +144 -0
- package/examples/custom-system-status-extension/prompt-stack.json +40 -0
- package/examples/default-prompt-stack.json +125 -0
- package/examples/fake-assistant-direct-output-prompt-stack.json +48 -0
- package/examples/hack-prompt-stack.json +118 -0
- package/examples/minimal-prompt-stack.json +40 -0
- package/examples/read-first-worker-prompt-stack.json +49 -0
- package/examples/validation-issues-prompt-stack.json +38 -0
- package/package.json +114 -4
|
@@ -0,0 +1,637 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { createServer } from "node:http";
|
|
3
|
+
import { AGENT_PROFILE_THINKING_LEVELS, AGENT_PROFILE_TYPE, } from "../agent-profile.js";
|
|
4
|
+
import { isCapabilityStateMutation, isCapabilityEnableRequest } from "../capability-state.js";
|
|
5
|
+
import { ContributionService } from "./contrib-service.js";
|
|
6
|
+
import { renderEditorHtml } from "./page.js";
|
|
7
|
+
// Port 0 asks Node to bind any available localhost port.
|
|
8
|
+
export const DEFAULT_WEB_EDITOR_PORT = 0;
|
|
9
|
+
class RequestBodyError extends Error {
|
|
10
|
+
status;
|
|
11
|
+
constructor(status, message) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = "RequestBodyError";
|
|
14
|
+
this.status = status;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export async function startWebEditorServer(host, options = {}) {
|
|
18
|
+
let currentHost = host;
|
|
19
|
+
const token = randomBytes(24).toString("base64url");
|
|
20
|
+
const sockets = new Set();
|
|
21
|
+
const contributionService = options.contributionTransport
|
|
22
|
+
? new ContributionService(options.contributionTransport, {
|
|
23
|
+
discoverTimeoutMs: options.contributionDiscoverTimeoutMs,
|
|
24
|
+
})
|
|
25
|
+
: undefined;
|
|
26
|
+
const server = createServer((req, res) => {
|
|
27
|
+
void handleRequest(currentHost, token, contributionService, req, res, () => currentHost).catch((error) => {
|
|
28
|
+
const status = error instanceof RequestBodyError ? error.status : 500;
|
|
29
|
+
sendJson(res, status, { error: error instanceof Error ? error.message : String(error) });
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
server.on("connection", (socket) => {
|
|
33
|
+
sockets.add(socket);
|
|
34
|
+
socket.on("close", () => sockets.delete(socket));
|
|
35
|
+
});
|
|
36
|
+
await new Promise((resolve, reject) => {
|
|
37
|
+
server.once("error", reject);
|
|
38
|
+
server.listen(options.port ?? DEFAULT_WEB_EDITOR_PORT, "127.0.0.1", () => {
|
|
39
|
+
server.off("error", reject);
|
|
40
|
+
resolve();
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
const address = server.address();
|
|
44
|
+
if (!address || typeof address === "string") {
|
|
45
|
+
await closeServer(server, sockets);
|
|
46
|
+
throw new Error("Failed to start pi-forge editor server.");
|
|
47
|
+
}
|
|
48
|
+
const url = `http://127.0.0.1:${address.port}/?token=${encodeURIComponent(token)}`;
|
|
49
|
+
contributionService?.start();
|
|
50
|
+
return {
|
|
51
|
+
url,
|
|
52
|
+
port: address.port,
|
|
53
|
+
updateHost: (nextHost) => {
|
|
54
|
+
currentHost = nextHost;
|
|
55
|
+
},
|
|
56
|
+
close: async () => {
|
|
57
|
+
await contributionService?.stop();
|
|
58
|
+
await closeServer(server, sockets);
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
async function handleRequest(host, token, contributionService, req, res, getCurrentHost) {
|
|
63
|
+
const url = new URL(req.url ?? "/", "http://127.0.0.1");
|
|
64
|
+
if (url.pathname === "/" && req.method === "GET") {
|
|
65
|
+
if (url.searchParams.get("token") !== token) {
|
|
66
|
+
sendText(res, 403, "Invalid pi-forge editor token.");
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
sendHtml(res, renderEditorHtml(resolvePageLang(host, req)));
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (!url.pathname.startsWith("/api/")) {
|
|
73
|
+
sendText(res, 404, "Not found.");
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (!hasValidToken(req, url, token)) {
|
|
77
|
+
sendJson(res, 403, { error: "Invalid pi-forge editor token." });
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const parts = url.pathname.split("/").filter(Boolean).map((part) => decodeURIComponent(part));
|
|
81
|
+
if (req.method === "GET" && parts[1] === "editor-config" && parts.length === 2) {
|
|
82
|
+
sendJson(res, 200, host.getEditorConfig());
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (req.method === "PUT" && parts[1] === "editor-config" && parts.length === 2) {
|
|
86
|
+
const body = await readJsonBody(req);
|
|
87
|
+
host = getCurrentHost ? getCurrentHost() : host;
|
|
88
|
+
const locale = isPlainObject(body) ? body.locale : undefined;
|
|
89
|
+
if (locale !== "en" && locale !== "zh-CN" && locale !== "auto") {
|
|
90
|
+
sendJson(res, 400, { error: 'locale must be "en", "zh-CN", or "auto".' });
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const result = host.setEditorLocale(locale);
|
|
94
|
+
if (!result.ok) {
|
|
95
|
+
sendJson(res, result.status ?? 500, { error: result.error });
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
sendJson(res, 200, { locale: result.locale });
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (req.method === "GET" && parts[1] === "stacks" && parts.length === 2) {
|
|
102
|
+
sendJson(res, 200, { stacks: host.listStacks(), cwd: host.cwd });
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (req.method === "GET" && parts[1] === "profiles" && parts.length === 2) {
|
|
106
|
+
sendJson(res, 200, host.listProfiles());
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (req.method === "GET" && parts[1] === "contrib" && parts.length === 2) {
|
|
110
|
+
if (!contributionService) {
|
|
111
|
+
sendJson(res, 200, { tabs: [], providerKey: null });
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const tabs = await contributionService.listTabs();
|
|
115
|
+
sendJson(res, 200, { tabs, providerKey: contributionService.providerKey });
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (req.method === "PUT" && parts[1] === "contrib" && parts.length === 3) {
|
|
119
|
+
if (!contributionService) {
|
|
120
|
+
sendJson(res, 503, { error: "No UI contribution provider is available." });
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const body = await readJsonBody(req);
|
|
124
|
+
host = getCurrentHost ? getCurrentHost() : host;
|
|
125
|
+
const patch = isPlainObject(body) && isPlainObject(body.patch)
|
|
126
|
+
? body.patch
|
|
127
|
+
: isPlainObject(body)
|
|
128
|
+
? body
|
|
129
|
+
: undefined;
|
|
130
|
+
if (!patch) {
|
|
131
|
+
sendJson(res, 400, { error: "Contribution patch must be a JSON object." });
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const result = await contributionService.writeValues(parts[2], patch);
|
|
135
|
+
if (!result.ok) {
|
|
136
|
+
sendJson(res, result.status, {
|
|
137
|
+
error: result.error,
|
|
138
|
+
...(result.errors ? { errors: result.errors } : {}),
|
|
139
|
+
});
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
sendJson(res, 200, { ok: true, ...(result.values ? { values: result.values } : {}) });
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
if (req.method === "POST" && parts[1] === "profiles" && parts[2] === "reload" && parts.length === 3) {
|
|
146
|
+
sendOperation(res, await host.reloadProfiles());
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (req.method === "POST" && parts[1] === "profiles" && parts[2] === "validate" && parts.length === 3) {
|
|
150
|
+
const body = await readJsonBody(req);
|
|
151
|
+
host = getCurrentHost ? getCurrentHost() : host;
|
|
152
|
+
const parsed = readProfilePayload(body);
|
|
153
|
+
if (!parsed.ok) {
|
|
154
|
+
sendJson(res, 400, { error: parsed.error });
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
const existingId = isPlainObject(body) && typeof body.existingId === "string" ? body.existingId : undefined;
|
|
158
|
+
const scopeResult = isPlainObject(body) ? parseScope(body.scope) : { ok: true, scope: undefined };
|
|
159
|
+
if (!scopeResult.ok) {
|
|
160
|
+
sendJson(res, 400, { error: scopeResult.error });
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
sendJson(res, 200, host.validateProfile(parsed.profile, existingId, scopeResult.scope ?? "project"));
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
if (req.method === "POST" && parts[1] === "profiles" && parts.length === 2) {
|
|
167
|
+
const body = await readJsonBody(req);
|
|
168
|
+
host = getCurrentHost ? getCurrentHost() : host;
|
|
169
|
+
const parsed = readProfilePayload(body);
|
|
170
|
+
if (!parsed.ok) {
|
|
171
|
+
sendJson(res, 400, { error: parsed.error });
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
const scopeResult = isPlainObject(body) ? parseScope(body.scope) : { ok: true, scope: undefined };
|
|
175
|
+
if (!scopeResult.ok) {
|
|
176
|
+
sendJson(res, 400, { error: scopeResult.error });
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
sendOperation(res, await host.createProfile(parsed.profile, scopeResult.scope ?? "project"));
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
if (req.method === "PUT" && parts[1] === "profiles" && parts.length === 3) {
|
|
183
|
+
const body = await readJsonBody(req);
|
|
184
|
+
host = getCurrentHost ? getCurrentHost() : host;
|
|
185
|
+
const parsed = readProfilePayload(body);
|
|
186
|
+
if (!parsed.ok) {
|
|
187
|
+
sendJson(res, 400, { error: parsed.error });
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
sendOperation(res, await host.saveProfile(parts[2], parsed.profile));
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (req.method === "POST" && parts[1] === "profiles" && parts[3] === "apply" && parts.length === 4) {
|
|
194
|
+
sendOperation(res, await host.applyProfile(parts[2]));
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (req.method === "DELETE" && parts[1] === "profiles" && parts.length === 3) {
|
|
198
|
+
sendOperation(res, await host.deleteProfile(parts[2]));
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
if (req.method === "GET" && parts[1] === "resources" && parts.length === 2) {
|
|
202
|
+
sendJson(res, 200, host.listResources());
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
if (req.method === "POST" && parts[1] === "stacks" && parts.length === 2) {
|
|
206
|
+
const body = await readJsonBody(req);
|
|
207
|
+
host = getCurrentHost ? getCurrentHost() : host;
|
|
208
|
+
const parsed = readStackPayload(body);
|
|
209
|
+
if (!parsed.ok) {
|
|
210
|
+
sendJson(res, 400, { error: parsed.error });
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
const scopeResult = isPlainObject(body) ? parseScope(body.scope) : { ok: true, scope: undefined };
|
|
214
|
+
if (!scopeResult.ok) {
|
|
215
|
+
sendJson(res, 400, { error: scopeResult.error });
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
const options = isPlainObject(body)
|
|
219
|
+
? {
|
|
220
|
+
activate: body.activate === true,
|
|
221
|
+
overwrite: body.overwrite === true,
|
|
222
|
+
scope: scopeResult.scope,
|
|
223
|
+
}
|
|
224
|
+
: {};
|
|
225
|
+
sendOperation(res, await host.createStack(parsed.stack, options));
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
if (req.method === "GET" && parts[1] === "stacks" && parts.length === 3) {
|
|
229
|
+
const loaded = host.getStack(parts[2]);
|
|
230
|
+
if (!loaded) {
|
|
231
|
+
sendJson(res, 404, { error: `Unknown preset: ${parts[2]}` });
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
sendJson(res, 200, loaded);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
if (req.method === "PUT" && parts[1] === "stacks" && parts.length === 3) {
|
|
238
|
+
const body = await readJsonBody(req);
|
|
239
|
+
host = getCurrentHost ? getCurrentHost() : host;
|
|
240
|
+
const parsed = readStackPayload(body);
|
|
241
|
+
if (!parsed.ok) {
|
|
242
|
+
sendJson(res, 400, { error: parsed.error });
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
const expectedSourceRevision = isPlainObject(body) && body.expectedSourceRevision !== undefined
|
|
246
|
+
? body.expectedSourceRevision
|
|
247
|
+
: undefined;
|
|
248
|
+
if (expectedSourceRevision !== undefined && typeof expectedSourceRevision !== "string") {
|
|
249
|
+
sendJson(res, 400, { error: "expectedSourceRevision must be a string when provided." });
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
const hostNow = getCurrentHost ? getCurrentHost() : host;
|
|
253
|
+
sendOperation(res, await hostNow.saveStack(parts[2], parsed.stack, expectedSourceRevision));
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
if (req.method === "DELETE" && parts[1] === "stacks" && parts.length === 3) {
|
|
257
|
+
sendOperation(res, await host.deleteStack(parts[2]));
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
if (req.method === "POST" && parts[1] === "stacks" && parts.length === 4 && parts[3] === "validate") {
|
|
261
|
+
const body = await readJsonBody(req);
|
|
262
|
+
host = getCurrentHost ? getCurrentHost() : host;
|
|
263
|
+
const parsed = readStackPayload(body);
|
|
264
|
+
if (!parsed.ok) {
|
|
265
|
+
sendJson(res, 400, { error: parsed.error });
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
sendJson(res, 200, { diagnostics: host.validateStack(parsed.stack) });
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
if (req.method === "POST" && parts[1] === "stacks" && parts.length === 4 && parts[3] === "preview") {
|
|
272
|
+
const body = await readJsonBody(req);
|
|
273
|
+
host = getCurrentHost ? getCurrentHost() : host;
|
|
274
|
+
const parsed = readStackPayload(body);
|
|
275
|
+
if (!parsed.ok) {
|
|
276
|
+
sendJson(res, 400, { error: parsed.error });
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
sendOperation(res, host.previewStack(parts[2], parsed.stack));
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
if (req.method === "GET" && parts[1] === "payload" && parts.length === 2) {
|
|
283
|
+
sendOperation(res, host.getPayload());
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
if (req.method === "POST" && parts[1] === "payload" && parts.length === 3 && parts[2] === "arm") {
|
|
287
|
+
const body = await readJsonBody(req);
|
|
288
|
+
host = getCurrentHost ? getCurrentHost() : host;
|
|
289
|
+
const savePath = isPlainObject(body) && typeof body.savePath === "string" && body.savePath.trim() ? body.savePath.trim() : undefined;
|
|
290
|
+
sendOperation(res, host.armPayload(savePath));
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
if (req.method === "DELETE" && parts[1] === "payload" && parts.length === 2) {
|
|
294
|
+
sendOperation(res, host.clearPayload());
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
if (req.method === "GET" && parts[1] === "context-diff" && parts.length === 2) {
|
|
298
|
+
sendOperation(res, host.getContextDiff());
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
if (parts[1] === "capabilities" && (parts.length === 2 || parts.length === 3)) {
|
|
302
|
+
const action = req.method === "GET" ? (parts.length === 2 ? "list" : "get")
|
|
303
|
+
: req.method === "POST" && parts.length === 2 ? "create"
|
|
304
|
+
: req.method === "POST" && parts[2] === "effective" ? "effective"
|
|
305
|
+
: req.method === "PUT" && parts.length === 3 ? "save"
|
|
306
|
+
: req.method === "DELETE" && parts.length === 3 ? "delete" : undefined;
|
|
307
|
+
if (!action) {
|
|
308
|
+
sendJson(res, 405, { ok: false, error: "Unsupported capability operation." });
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
const body = req.method === "GET" ? undefined : await readJsonBody(req);
|
|
312
|
+
const hostNow = getCurrentHost ? getCurrentHost() : host;
|
|
313
|
+
if (!hostNow.capabilityOperation) {
|
|
314
|
+
sendJson(res, 503, { ok: false, error: "Capability resources unavailable." });
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
if (action === "create" || action === "save" || action === "delete") {
|
|
318
|
+
try {
|
|
319
|
+
if (hostNow.isProjectTrusted?.() !== true) {
|
|
320
|
+
sendJson(res, 403, { ok: false, error: "Project is not trusted." });
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
catch {
|
|
325
|
+
sendJson(res, 503, { ok: false, error: "Session replaced. Refresh before writing." });
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
sendOperation(res, hostNow.capabilityOperation(action, parts[2], body));
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
if (req.method === "GET" && parts[1] === "capability-state" && parts[2] === "available" && parts.length === 3) {
|
|
333
|
+
const hostNow = getCurrentHost ? getCurrentHost() : host;
|
|
334
|
+
if (!hostNow.readCapabilityChoices) {
|
|
335
|
+
sendJson(res, 503, { ok: false, error: "Capability runtime is unavailable." });
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
const result = hostNow.readCapabilityChoices();
|
|
339
|
+
if (!result.ok) {
|
|
340
|
+
sendJson(res, result.status, { ok: false, error: result.error });
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
sendJson(res, 200, { ok: true, state: result.state, choices: result.choices });
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
if (req.method === "GET" && parts[1] === "capability-state" && parts[2] === "preview" && parts.length === 3) {
|
|
347
|
+
const hostNow = getCurrentHost ? getCurrentHost() : host;
|
|
348
|
+
sendOperation(res, hostNow.previewCapabilities
|
|
349
|
+
? hostNow.previewCapabilities()
|
|
350
|
+
: { ok: false, status: 503, error: "Capability runtime is unavailable." });
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
if (req.method === "POST" && parts[1] === "capability-state" && parts[2] === "enable" && parts.length === 3) {
|
|
354
|
+
const body = await readJsonBody(req);
|
|
355
|
+
host = getCurrentHost ? getCurrentHost() : host;
|
|
356
|
+
// Resolve the host only after consuming the body: session replacement during
|
|
357
|
+
// request parsing must never send activation to the retired host.
|
|
358
|
+
const hostNow = getCurrentHost ? getCurrentHost() : host;
|
|
359
|
+
try {
|
|
360
|
+
if (hostNow.isProjectTrusted?.() !== true) {
|
|
361
|
+
sendJson(res, 403, { ok: false, error: "Project is not trusted; refusing to activate capabilities." });
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
catch {
|
|
366
|
+
sendJson(res, 503, { ok: false, error: "Capability session is unavailable. Refresh after session replacement." });
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
if (!isCapabilityEnableRequest(body)) {
|
|
370
|
+
sendJson(res, 400, { ok: false, error: "Invalid capability activation payload." });
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
if (!hostNow.enableCapability) {
|
|
374
|
+
sendJson(res, 503, { ok: false, error: "Capability runtime is unavailable." });
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
const result = hostNow.enableCapability(body);
|
|
378
|
+
if (!result.ok) {
|
|
379
|
+
sendJson(res, result.status, { ok: false, error: result.error });
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
sendJson(res, 200, { ok: true, state: result.state });
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
if (req.method === "GET" && parts[1] === "capability-state" && parts.length === 2) {
|
|
386
|
+
const hostNow = getCurrentHost ? getCurrentHost() : host;
|
|
387
|
+
if (!hostNow.readCapabilityState) {
|
|
388
|
+
sendJson(res, 503, { ok: false, error: "Capability runtime is unavailable." });
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
const result = hostNow.readCapabilityState();
|
|
392
|
+
if (!result.ok) {
|
|
393
|
+
sendJson(res, result.status, { ok: false, error: result.error });
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
sendJson(res, 200, { ok: true, state: result.state });
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
if (req.method === "POST" && parts[1] === "capability-state" && parts.length === 2) {
|
|
400
|
+
const body = await readJsonBody(req);
|
|
401
|
+
const hostNow = getCurrentHost ? getCurrentHost() : host;
|
|
402
|
+
try {
|
|
403
|
+
if (hostNow.isProjectTrusted?.() !== true) {
|
|
404
|
+
sendJson(res, 403, { ok: false, error: "Project is not trusted; refusing to mutate capabilities." });
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
catch {
|
|
409
|
+
// Pi invalidates captured contexts during replacement/reload.
|
|
410
|
+
sendJson(res, 503, { ok: false, error: "Capability session is unavailable. Refresh after session replacement." });
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
if (!isCapabilityStateMutation(body)) {
|
|
414
|
+
sendJson(res, 400, { ok: false, error: "Invalid capability state mutation payload." });
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
if (!hostNow.mutateCapabilityState) {
|
|
418
|
+
sendJson(res, 503, { ok: false, error: "Capability runtime is unavailable." });
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
const result = hostNow.mutateCapabilityState(body);
|
|
422
|
+
if (!result.ok) {
|
|
423
|
+
sendJson(res, result.status, { ok: false, error: result.error });
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
sendJson(res, 200, { ok: true, state: result.state });
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
if (req.method === "POST" && parts[1] === "stacks" && parts.length === 4 && parts[3] === "activate") {
|
|
430
|
+
sendOperation(res, host.activateStack(parts[2]));
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
if (req.method === "POST" && parts[1] === "disable" && parts.length === 2) {
|
|
434
|
+
sendOperation(res, host.disableStacks());
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
if (req.method === "POST" && parts[1] === "reload" && parts.length === 2) {
|
|
438
|
+
sendOperation(res, await host.reloadStacks());
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
sendJson(res, 404, { error: "Unknown pi-forge editor API route." });
|
|
442
|
+
}
|
|
443
|
+
function hasValidToken(req, url, token) {
|
|
444
|
+
const header = req.headers["x-pi-forge-token"];
|
|
445
|
+
return header === token || url.searchParams.get("token") === token;
|
|
446
|
+
}
|
|
447
|
+
async function readJsonBody(req) {
|
|
448
|
+
const chunks = [];
|
|
449
|
+
let size = 0;
|
|
450
|
+
const maxBytes = 2_000_000;
|
|
451
|
+
for await (const chunk of req) {
|
|
452
|
+
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
453
|
+
size += buffer.length;
|
|
454
|
+
if (size > maxBytes)
|
|
455
|
+
throw new RequestBodyError(413, "Request body is too large.");
|
|
456
|
+
chunks.push(buffer);
|
|
457
|
+
}
|
|
458
|
+
const text = Buffer.concat(chunks).toString("utf8");
|
|
459
|
+
if (!text.trim())
|
|
460
|
+
return {};
|
|
461
|
+
try {
|
|
462
|
+
return JSON.parse(text);
|
|
463
|
+
}
|
|
464
|
+
catch {
|
|
465
|
+
throw new RequestBodyError(400, "Request body must contain valid JSON.");
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
function readStackPayload(body) {
|
|
469
|
+
const rawStack = isPlainObject(body) && "stack" in body ? body.stack : body;
|
|
470
|
+
if (!isPlainObject(rawStack))
|
|
471
|
+
return { ok: false, error: "Preset payload must be a JSON object." };
|
|
472
|
+
if (typeof rawStack.id !== "string" || !rawStack.id.trim())
|
|
473
|
+
return { ok: false, error: "Preset id must be a non-empty string." };
|
|
474
|
+
if (!Array.isArray(rawStack.items))
|
|
475
|
+
return { ok: false, error: "Preset must contain a Stack items array." };
|
|
476
|
+
for (const [index, item] of rawStack.items.entries()) {
|
|
477
|
+
if (!isPlainObject(item))
|
|
478
|
+
return { ok: false, error: `Item ${index + 1} must be an object.` };
|
|
479
|
+
if (item.kind !== "block" && item.kind !== "slot")
|
|
480
|
+
return { ok: false, error: `Item ${index + 1} kind must be block or slot.` };
|
|
481
|
+
if (typeof item.id !== "string" || !item.id.trim())
|
|
482
|
+
return { ok: false, error: `Item ${index + 1} id must be a non-empty string.` };
|
|
483
|
+
if (item.kind === "block" && typeof item.content !== "string")
|
|
484
|
+
return { ok: false, error: `Block item ${item.id} content must be a string.` };
|
|
485
|
+
if (item.kind === "slot" && typeof item.slot !== "string")
|
|
486
|
+
return { ok: false, error: `Slot item ${item.id} slot must be a string.` };
|
|
487
|
+
}
|
|
488
|
+
return { ok: true, stack: rawStack };
|
|
489
|
+
}
|
|
490
|
+
function readProfilePayload(body) {
|
|
491
|
+
const raw = isPlainObject(body) && "profile" in body ? body.profile : body;
|
|
492
|
+
if (!isPlainObject(raw))
|
|
493
|
+
return { ok: false, error: "Profile payload must be a JSON object." };
|
|
494
|
+
const allowedFields = new Set([
|
|
495
|
+
"schemaVersion",
|
|
496
|
+
"type",
|
|
497
|
+
"id",
|
|
498
|
+
"name",
|
|
499
|
+
"description",
|
|
500
|
+
"autoActivate",
|
|
501
|
+
"model",
|
|
502
|
+
"thinkingLevel",
|
|
503
|
+
"promptStack",
|
|
504
|
+
]);
|
|
505
|
+
const unsupported = Object.keys(raw).find((field) => !allowedFields.has(field));
|
|
506
|
+
if (unsupported)
|
|
507
|
+
return { ok: false, error: `Unsupported profile field: ${unsupported}` };
|
|
508
|
+
if (raw.schemaVersion !== 1)
|
|
509
|
+
return { ok: false, error: "Profile schemaVersion must be 1." };
|
|
510
|
+
if (raw.type !== AGENT_PROFILE_TYPE)
|
|
511
|
+
return { ok: false, error: `Profile type must be "${AGENT_PROFILE_TYPE}".` };
|
|
512
|
+
if (typeof raw.id !== "string")
|
|
513
|
+
return { ok: false, error: "Profile id must be a string." };
|
|
514
|
+
if (raw.name !== undefined && typeof raw.name !== "string")
|
|
515
|
+
return { ok: false, error: "Profile name must be a string when provided." };
|
|
516
|
+
if (raw.description !== undefined && typeof raw.description !== "string") {
|
|
517
|
+
return { ok: false, error: "Profile description must be a string when provided." };
|
|
518
|
+
}
|
|
519
|
+
if (raw.autoActivate !== undefined && typeof raw.autoActivate !== "boolean") {
|
|
520
|
+
return { ok: false, error: "Profile autoActivate must be a boolean when provided." };
|
|
521
|
+
}
|
|
522
|
+
if (!isPlainObject(raw.model))
|
|
523
|
+
return { ok: false, error: "Profile model must be an object." };
|
|
524
|
+
const unsupportedModelField = Object.keys(raw.model).find((field) => field !== "provider" && field !== "id");
|
|
525
|
+
if (unsupportedModelField)
|
|
526
|
+
return { ok: false, error: `Unsupported profile model field: ${unsupportedModelField}` };
|
|
527
|
+
if (typeof raw.model.provider !== "string" || typeof raw.model.id !== "string") {
|
|
528
|
+
return { ok: false, error: "Profile model provider and id must be strings." };
|
|
529
|
+
}
|
|
530
|
+
if (typeof raw.thinkingLevel !== "string"
|
|
531
|
+
|| !AGENT_PROFILE_THINKING_LEVELS.includes(raw.thinkingLevel)) {
|
|
532
|
+
return { ok: false, error: `Unsupported profile thinkingLevel: ${String(raw.thinkingLevel)}` };
|
|
533
|
+
}
|
|
534
|
+
if (raw.promptStack !== null && typeof raw.promptStack !== "string") {
|
|
535
|
+
return { ok: false, error: "Profile promptStack must be a string or null." };
|
|
536
|
+
}
|
|
537
|
+
return {
|
|
538
|
+
ok: true,
|
|
539
|
+
profile: {
|
|
540
|
+
schemaVersion: 1,
|
|
541
|
+
type: AGENT_PROFILE_TYPE,
|
|
542
|
+
id: raw.id,
|
|
543
|
+
name: raw.name,
|
|
544
|
+
description: raw.description,
|
|
545
|
+
autoActivate: raw.autoActivate,
|
|
546
|
+
model: {
|
|
547
|
+
provider: raw.model.provider,
|
|
548
|
+
id: raw.model.id,
|
|
549
|
+
},
|
|
550
|
+
thinkingLevel: raw.thinkingLevel,
|
|
551
|
+
promptStack: raw.promptStack,
|
|
552
|
+
},
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
function sendOperation(res, result) {
|
|
556
|
+
if (!result.ok) {
|
|
557
|
+
sendJson(res, result.status ?? 400, { error: result.error });
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
sendJson(res, 200, result);
|
|
561
|
+
}
|
|
562
|
+
function sendHtml(res, html) {
|
|
563
|
+
res.writeHead(200, {
|
|
564
|
+
"content-type": "text/html; charset=utf-8",
|
|
565
|
+
"cache-control": "no-store",
|
|
566
|
+
"connection": "close",
|
|
567
|
+
});
|
|
568
|
+
res.end(html);
|
|
569
|
+
}
|
|
570
|
+
function sendText(res, status, text) {
|
|
571
|
+
res.writeHead(status, {
|
|
572
|
+
"content-type": "text/plain; charset=utf-8",
|
|
573
|
+
"cache-control": "no-store",
|
|
574
|
+
"connection": "close",
|
|
575
|
+
});
|
|
576
|
+
res.end(text);
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* Resolve the initial page language: an explicit configured locale wins;
|
|
580
|
+
* "auto" follows the browser's Accept-Language header. The client keeps
|
|
581
|
+
* document.documentElement.lang in sync when the user switches locale.
|
|
582
|
+
*/
|
|
583
|
+
function resolvePageLang(host, req) {
|
|
584
|
+
const configured = host.getEditorConfig().locale;
|
|
585
|
+
if (configured === "en" || configured === "zh-CN")
|
|
586
|
+
return configured;
|
|
587
|
+
const accepted = req.headers["accept-language"] ?? "";
|
|
588
|
+
const ranges = accepted
|
|
589
|
+
.split(",")
|
|
590
|
+
.map((entry, index) => {
|
|
591
|
+
const [rawRange = "", ...parameters] = entry.trim().split(";");
|
|
592
|
+
let quality = 1;
|
|
593
|
+
for (const parameter of parameters) {
|
|
594
|
+
if (!/^q\s*=/iu.test(parameter.trim()))
|
|
595
|
+
continue;
|
|
596
|
+
const match = parameter.trim().match(/^q\s*=\s*(0(?:\.\d{0,3})?|\.\d{1,3}|1(?:\.0{0,3})?)$/iu);
|
|
597
|
+
quality = match ? Number(match[1]) : 0;
|
|
598
|
+
break;
|
|
599
|
+
}
|
|
600
|
+
return { range: rawRange.toLowerCase(), quality, index };
|
|
601
|
+
})
|
|
602
|
+
.filter(({ range, quality }) => range.length > 0 && quality > 0)
|
|
603
|
+
.sort((a, b) => b.quality - a.quality || a.index - b.index);
|
|
604
|
+
for (const { range } of ranges) {
|
|
605
|
+
if (range === "zh" || range.startsWith("zh-"))
|
|
606
|
+
return "zh-CN";
|
|
607
|
+
if (range === "en" || range.startsWith("en-") || range === "*")
|
|
608
|
+
return "en";
|
|
609
|
+
}
|
|
610
|
+
return "en";
|
|
611
|
+
}
|
|
612
|
+
function sendJson(res, status, value) {
|
|
613
|
+
res.writeHead(status, {
|
|
614
|
+
"content-type": "application/json; charset=utf-8",
|
|
615
|
+
"cache-control": "no-store",
|
|
616
|
+
"connection": "close",
|
|
617
|
+
});
|
|
618
|
+
res.end(JSON.stringify(value));
|
|
619
|
+
}
|
|
620
|
+
function closeServer(server, sockets) {
|
|
621
|
+
return new Promise((resolve, reject) => {
|
|
622
|
+
server.close((error) => error ? reject(error) : resolve());
|
|
623
|
+
for (const socket of sockets)
|
|
624
|
+
socket.destroy();
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
function parseScope(value) {
|
|
628
|
+
if (value === undefined)
|
|
629
|
+
return { ok: true };
|
|
630
|
+
if (value === "project" || value === "global")
|
|
631
|
+
return { ok: true, scope: value };
|
|
632
|
+
return { ok: false, error: 'scope must be "project" or "global".' };
|
|
633
|
+
}
|
|
634
|
+
function isPlainObject(value) {
|
|
635
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
636
|
+
}
|
|
637
|
+
//# sourceMappingURL=server.js.map
|