@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
package/dist/compiler.js
ADDED
|
@@ -0,0 +1,737 @@
|
|
|
1
|
+
import { forgeV1 } from "./forge-v1/index.js";
|
|
2
|
+
import { applyRegexRulesToMessages, applyRegexRulesToString } from "./regex.js";
|
|
3
|
+
import { ForgeTemplateRenderer } from "./template-render.js";
|
|
4
|
+
import { getRegisteredSlot, renderSlotText } from "./slot-renderers.js";
|
|
5
|
+
import { isCapabilityControlMessage } from "./capability-protocol.js";
|
|
6
|
+
const ZERO_USAGE = {
|
|
7
|
+
input: 0,
|
|
8
|
+
output: 0,
|
|
9
|
+
cacheRead: 0,
|
|
10
|
+
cacheWrite: 0,
|
|
11
|
+
totalTokens: 0,
|
|
12
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
13
|
+
};
|
|
14
|
+
const SUMMARY_ROLES = new Set(["branchSummary", "compactionSummary"]);
|
|
15
|
+
export class PromptCompilationContext {
|
|
16
|
+
stack;
|
|
17
|
+
runtime;
|
|
18
|
+
templateRenderer;
|
|
19
|
+
constructor(stack, runtime) {
|
|
20
|
+
this.stack = stack;
|
|
21
|
+
this.runtime = runtime;
|
|
22
|
+
this.templateRenderer = new ForgeTemplateRenderer(stack, runtime);
|
|
23
|
+
}
|
|
24
|
+
compileSystemPrompt(baseSystemPrompt) {
|
|
25
|
+
return compileSystemPromptWithRenderer(this.stack, this.runtime, baseSystemPrompt, this.templateRenderer);
|
|
26
|
+
}
|
|
27
|
+
compileMessages(originalMessages) {
|
|
28
|
+
return compileMessagesWithRenderer(this.stack, this.runtime, originalMessages, this.templateRenderer);
|
|
29
|
+
}
|
|
30
|
+
setLatestUserMessage(message) {
|
|
31
|
+
this.runtime.latestUserMessage = message;
|
|
32
|
+
this.templateRenderer.setLatestUserMessage(message);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function compileSystemPromptWithRenderer(stack, runtime, baseSystemPrompt, templateRenderer) {
|
|
36
|
+
const diagnostics = cacheSensitiveContentDiagnostics(stack);
|
|
37
|
+
const parts = [];
|
|
38
|
+
for (const item of enabledItems(stack)) {
|
|
39
|
+
if (item.role !== "system")
|
|
40
|
+
continue;
|
|
41
|
+
if (item.kind === "block") {
|
|
42
|
+
const text = templateRenderer.render(item.content, diagnostics, item.id).trim();
|
|
43
|
+
if (text)
|
|
44
|
+
parts.push(text);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (item.slot === "chat-history") {
|
|
48
|
+
diagnostics.push({ level: "warning", message: "chat-history slot cannot be placed in the system prompt.", itemId: item.id });
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const rendered = renderSlotWithDependencies(item, stack, runtime, diagnostics, templateRenderer).trim();
|
|
52
|
+
if (rendered)
|
|
53
|
+
parts.push(rendered);
|
|
54
|
+
}
|
|
55
|
+
const compiled = parts.join("\n\n");
|
|
56
|
+
const mode = stack.mode ?? "replace";
|
|
57
|
+
let systemPrompt;
|
|
58
|
+
if (!compiled) {
|
|
59
|
+
const hasCompileErrors = diagnostics.some((diagnostic) => diagnostic.level === "error");
|
|
60
|
+
diagnostics.push({
|
|
61
|
+
level: "warning",
|
|
62
|
+
message: hasCompileErrors
|
|
63
|
+
? "Compiled system prompt failed; preserving base system prompt."
|
|
64
|
+
: "Compiled system prompt is empty; preserving base system prompt.",
|
|
65
|
+
});
|
|
66
|
+
systemPrompt = baseSystemPrompt;
|
|
67
|
+
}
|
|
68
|
+
else if (mode === "append") {
|
|
69
|
+
systemPrompt = compiled ? `${baseSystemPrompt}\n\n${compiled}` : baseSystemPrompt;
|
|
70
|
+
}
|
|
71
|
+
else if (mode === "prepend") {
|
|
72
|
+
systemPrompt = compiled ? `${compiled}\n\n${baseSystemPrompt}` : baseSystemPrompt;
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
systemPrompt = compiled;
|
|
76
|
+
}
|
|
77
|
+
systemPrompt = applyRegexRulesToString(stack, systemPrompt, "compiled", "system", diagnostics);
|
|
78
|
+
return { systemPrompt, diagnostics };
|
|
79
|
+
}
|
|
80
|
+
export function compileSystemPrompt(stack, runtime, baseSystemPrompt) {
|
|
81
|
+
return compileSystemPromptWithRenderer(stack, runtime, baseSystemPrompt, new ForgeTemplateRenderer(stack, runtime));
|
|
82
|
+
}
|
|
83
|
+
function compileMessagesWithRenderer(stack, runtime, originalMessages, templateRenderer) {
|
|
84
|
+
const diagnostics = cacheSensitiveContentDiagnostics(stack);
|
|
85
|
+
let messages = [];
|
|
86
|
+
let messageSources = [];
|
|
87
|
+
let insertedHistory = false;
|
|
88
|
+
for (const item of enabledItems(stack)) {
|
|
89
|
+
if (item.kind === "slot" && item.slot === "chat-history") {
|
|
90
|
+
if (insertedHistory && !stack.context?.allowDuplicateChatHistory) {
|
|
91
|
+
diagnostics.push({
|
|
92
|
+
level: "warning",
|
|
93
|
+
message: "Skipped duplicate chat-history slot.",
|
|
94
|
+
itemId: item.id,
|
|
95
|
+
});
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const historyMessages = getChatHistoryMessages(originalMessages, item, diagnostics);
|
|
99
|
+
const transformedHistory = applyRegexRulesToMessages(stack, historyMessages, "history", diagnostics);
|
|
100
|
+
messages.push(...transformedHistory);
|
|
101
|
+
messageSources.push(...chatHistoryMessageSources(transformedHistory, item));
|
|
102
|
+
insertedHistory = true;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (!item.role || item.role === "system")
|
|
106
|
+
continue;
|
|
107
|
+
const content = item.kind === "block"
|
|
108
|
+
? templateRenderer.render(item.content, diagnostics, item.id)
|
|
109
|
+
: renderSlotWithDependencies(item, stack, runtime, diagnostics, templateRenderer);
|
|
110
|
+
if (!content.trim())
|
|
111
|
+
continue;
|
|
112
|
+
const message = createSyntheticMessage(item.role, content, stack, runtime);
|
|
113
|
+
messages.push(message);
|
|
114
|
+
messageSources.push(stackItemMessageSource(message, item));
|
|
115
|
+
}
|
|
116
|
+
if (!insertedHistory) {
|
|
117
|
+
messages.push(...originalMessages);
|
|
118
|
+
messageSources.push(...implicitHistoryMessageSources(originalMessages));
|
|
119
|
+
}
|
|
120
|
+
messages = applyRegexRulesToMessages(stack, messages, "compiled", diagnostics);
|
|
121
|
+
if (stack.context?.mergeConsecutiveRoles) {
|
|
122
|
+
const merged = mergeConsecutiveStackItemMessages(messages, messageSources, stack.context.mergeSeparator ?? "\n\n", diagnostics);
|
|
123
|
+
messages = merged.messages;
|
|
124
|
+
messageSources = merged.sources;
|
|
125
|
+
}
|
|
126
|
+
// Pi's initial System (prompt + initial tool declarations) is a protocol
|
|
127
|
+
// header, not movable conversation history. Filters/regex preserve control
|
|
128
|
+
// message identity, so move only that exact incoming head and its provenance.
|
|
129
|
+
// Never mistake a later System delta for the initial header.
|
|
130
|
+
if (originalMessages[0]?.role === "system") {
|
|
131
|
+
const leadingIndex = messages.indexOf(originalMessages[0]);
|
|
132
|
+
if (leadingIndex > 0) {
|
|
133
|
+
messages.unshift(messages.splice(leadingIndex, 1)[0]);
|
|
134
|
+
messageSources.unshift(messageSources.splice(leadingIndex, 1)[0]);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return { messages, messageSources, diagnostics };
|
|
138
|
+
}
|
|
139
|
+
const MERGEABLE_MESSAGE_ROLES = new Set(["user", "assistant"]);
|
|
140
|
+
/**
|
|
141
|
+
* Merge runs of consecutive stack-item messages that share the same declared
|
|
142
|
+
* role. Runs only combine stack-authored synthetic messages: chat-history
|
|
143
|
+
* output (including the implicit history tail) and custom-role items are hard
|
|
144
|
+
* boundaries. Runs execute after compiled-stage regex so rule semantics are
|
|
145
|
+
* unchanged, and only text-only messages participate.
|
|
146
|
+
*/
|
|
147
|
+
function mergeConsecutiveStackItemMessages(messages, sources, separator, diagnostics) {
|
|
148
|
+
const mergedMessages = [];
|
|
149
|
+
const mergedSources = [];
|
|
150
|
+
for (let index = 0; index < messages.length; index++) {
|
|
151
|
+
const message = messages[index];
|
|
152
|
+
const source = sources[index];
|
|
153
|
+
const role = messageRole(message);
|
|
154
|
+
const last = mergedMessages.length - 1;
|
|
155
|
+
const lastMessage = last >= 0 ? mergedMessages[last] : undefined;
|
|
156
|
+
const lastSource = last >= 0 ? mergedSources[last] : undefined;
|
|
157
|
+
// A merge run requires both neighbours to be mergeable stack items of the
|
|
158
|
+
// same role. History and custom-role messages are hard boundaries.
|
|
159
|
+
const canMerge = lastMessage !== undefined
|
|
160
|
+
&& lastSource?.kind === "stack-item"
|
|
161
|
+
&& MERGEABLE_MESSAGE_ROLES.has(messageRole(lastMessage))
|
|
162
|
+
&& source?.kind === "stack-item"
|
|
163
|
+
&& MERGEABLE_MESSAGE_ROLES.has(role)
|
|
164
|
+
&& role === messageRole(lastMessage)
|
|
165
|
+
&& isTextOnlyMessage(lastMessage)
|
|
166
|
+
&& isTextOnlyMessage(message);
|
|
167
|
+
if (!canMerge) {
|
|
168
|
+
mergedMessages.push(message);
|
|
169
|
+
// sources always parallel messages; the fallback stays a merge boundary.
|
|
170
|
+
mergedSources.push(source ?? { kind: "implicit-history", role });
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
const priorItems = lastSource.mergedItems ?? [{ itemId: lastSource.itemId, itemName: lastSource.itemName }];
|
|
174
|
+
const mergedItems = [...priorItems, { itemId: source.itemId, itemName: source.itemName }];
|
|
175
|
+
if (priorItems.length === 1) {
|
|
176
|
+
diagnostics.push({
|
|
177
|
+
level: "info",
|
|
178
|
+
message: `Merged consecutive ${role} stack items into one message.`,
|
|
179
|
+
itemId: lastSource.itemId,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
mergedMessages[last] = appendMessageText(lastMessage, message, separator);
|
|
183
|
+
mergedSources[last] = {
|
|
184
|
+
kind: "stack-item",
|
|
185
|
+
itemId: lastSource.itemId,
|
|
186
|
+
itemName: mergedItems.map((item) => item.itemName?.trim() || item.itemId || "item").join(" + "),
|
|
187
|
+
role,
|
|
188
|
+
mergedItems,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
return { messages: mergedMessages, sources: mergedSources };
|
|
192
|
+
}
|
|
193
|
+
function isTextOnlyMessage(message) {
|
|
194
|
+
const content = message.content;
|
|
195
|
+
if (typeof content === "string")
|
|
196
|
+
return true;
|
|
197
|
+
if (!Array.isArray(content))
|
|
198
|
+
return false;
|
|
199
|
+
return content.every((part) => isPlainObject(part) && part.type === "text" && typeof part.text === "string");
|
|
200
|
+
}
|
|
201
|
+
function appendMessageText(base, addition, separator) {
|
|
202
|
+
const baseContent = base.content;
|
|
203
|
+
const additionContent = addition.content;
|
|
204
|
+
const baseText = typeof baseContent === "string" ? baseContent : textPartsToText(baseContent);
|
|
205
|
+
const additionText = typeof additionContent === "string" ? additionContent : textPartsToText(additionContent);
|
|
206
|
+
return { ...base, content: [{ type: "text", text: baseText + separator + additionText }] };
|
|
207
|
+
}
|
|
208
|
+
function textPartsToText(content) {
|
|
209
|
+
if (!Array.isArray(content))
|
|
210
|
+
return "";
|
|
211
|
+
return content
|
|
212
|
+
.map((part) => (isPlainObject(part) && part.type === "text" && typeof part.text === "string" ? part.text : ""))
|
|
213
|
+
.join("");
|
|
214
|
+
}
|
|
215
|
+
function renderSlotWithDependencies(item, stack, runtime, diagnostics, templateRenderer) {
|
|
216
|
+
const slotDefinition = getRegisteredSlot(item.slot);
|
|
217
|
+
if (slotDefinition?.dependencies?.length) {
|
|
218
|
+
const before = diagnostics.length;
|
|
219
|
+
const slotEnv = templateRenderer.environmentForDependencies(slotDefinition.dependencies, diagnostics, item.id);
|
|
220
|
+
const newErrors = diagnostics.slice(before).some((diagnostic) => diagnostic.level === "error");
|
|
221
|
+
if (newErrors)
|
|
222
|
+
return "";
|
|
223
|
+
return renderSlotText(item, stack, runtime, diagnostics, slotEnv);
|
|
224
|
+
}
|
|
225
|
+
return renderSlotText(item, stack, runtime, diagnostics, templateRenderer.environment());
|
|
226
|
+
}
|
|
227
|
+
export function compileMessages(stack, runtime, originalMessages) {
|
|
228
|
+
return compileMessagesWithRenderer(stack, runtime, originalMessages, new ForgeTemplateRenderer(stack, runtime));
|
|
229
|
+
}
|
|
230
|
+
export function getLatestUserMessage(messages) {
|
|
231
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
232
|
+
const message = messages[i];
|
|
233
|
+
if (message.role === "user") {
|
|
234
|
+
const text = contentToText(message.content);
|
|
235
|
+
if (text.trim())
|
|
236
|
+
return text;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return undefined;
|
|
240
|
+
}
|
|
241
|
+
export function agentMessageToPreviewText(message) {
|
|
242
|
+
let text = contentToText(message.content);
|
|
243
|
+
const role = message.role;
|
|
244
|
+
if (role === "bashExecution") {
|
|
245
|
+
const bash = message;
|
|
246
|
+
text += `Ran ${bash.command ?? ""}\n${bash.output ?? ""}`;
|
|
247
|
+
}
|
|
248
|
+
if (role === "branchSummary" || role === "compactionSummary") {
|
|
249
|
+
text += message.summary ?? "";
|
|
250
|
+
}
|
|
251
|
+
return text;
|
|
252
|
+
}
|
|
253
|
+
function getChatHistoryMessages(messages, item, diagnostics) {
|
|
254
|
+
let result = messages;
|
|
255
|
+
const options = item.options ?? {};
|
|
256
|
+
let shouldRepairToolPairs = false;
|
|
257
|
+
if (options.includeLastUserMessage === false) {
|
|
258
|
+
const lastUserIndex = findLastUserMessageIndex(result);
|
|
259
|
+
if (lastUserIndex !== -1)
|
|
260
|
+
result = result.filter((_message, index) => index !== lastUserIndex);
|
|
261
|
+
}
|
|
262
|
+
if (options.includeSummaries === false) {
|
|
263
|
+
const next = result.filter((message) => isCapabilityControlMessage(message) || !isSummaryMessage(message));
|
|
264
|
+
addHistoryFilterDiagnostic(diagnostics, item.id, "summary", result.length, next.length);
|
|
265
|
+
result = next;
|
|
266
|
+
}
|
|
267
|
+
if (isStringArray(options.roles) && options.roles.length > 0) {
|
|
268
|
+
const allowedRoles = new Set(options.roles);
|
|
269
|
+
const next = result.filter((message) => isCapabilityControlMessage(message) || allowedRoles.has(messageRole(message)));
|
|
270
|
+
addHistoryFilterDiagnostic(diagnostics, item.id, "role", result.length, next.length);
|
|
271
|
+
result = next;
|
|
272
|
+
shouldRepairToolPairs = true;
|
|
273
|
+
}
|
|
274
|
+
if (options.toolMode === "drop") {
|
|
275
|
+
result = dropToolHistory(result, diagnostics, item.id);
|
|
276
|
+
}
|
|
277
|
+
else if (options.toolMode !== undefined && options.toolMode !== "keep") {
|
|
278
|
+
diagnostics.push({ level: "warning", message: `Unsupported chat-history toolMode: ${String(options.toolMode)}.`, itemId: item.id });
|
|
279
|
+
}
|
|
280
|
+
if (options.stripAssistantThinking === true) {
|
|
281
|
+
result = stripAssistantThinkingFromHistory(result, diagnostics, item.id);
|
|
282
|
+
}
|
|
283
|
+
const limited = limitChatHistory(result, options, diagnostics, item.id);
|
|
284
|
+
if (limited !== result) {
|
|
285
|
+
result = limited;
|
|
286
|
+
shouldRepairToolPairs = true;
|
|
287
|
+
}
|
|
288
|
+
if (shouldRepairToolPairs && options.toolMode !== "drop") {
|
|
289
|
+
result = repairToolHistory(result, diagnostics, item.id);
|
|
290
|
+
}
|
|
291
|
+
return result;
|
|
292
|
+
}
|
|
293
|
+
function findLastUserMessageIndex(messages) {
|
|
294
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
295
|
+
if (messages[i].role === "user")
|
|
296
|
+
return i;
|
|
297
|
+
}
|
|
298
|
+
return -1;
|
|
299
|
+
}
|
|
300
|
+
function limitChatHistory(messages, options, diagnostics, itemId) {
|
|
301
|
+
let result = messages;
|
|
302
|
+
const maxMessages = positiveIntegerOption(options.maxMessages);
|
|
303
|
+
const maxChars = positiveIntegerOption(options.maxChars);
|
|
304
|
+
if (maxMessages !== undefined) {
|
|
305
|
+
const next = takeRecentMessagesWithinCount(result, maxMessages);
|
|
306
|
+
if (next.length < result.length) {
|
|
307
|
+
diagnostics.push({
|
|
308
|
+
level: "info",
|
|
309
|
+
message: `Trimmed chat history from ${result.length} to ${next.length} message(s) by maxMessages.`,
|
|
310
|
+
itemId,
|
|
311
|
+
});
|
|
312
|
+
result = next;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
if (maxChars !== undefined) {
|
|
316
|
+
const next = takeRecentMessagesWithinChars(result, maxChars);
|
|
317
|
+
if (next.length < result.length) {
|
|
318
|
+
diagnostics.push({
|
|
319
|
+
level: "info",
|
|
320
|
+
message: `Trimmed chat history from ${result.length} to ${next.length} message(s) by maxChars.`,
|
|
321
|
+
itemId,
|
|
322
|
+
});
|
|
323
|
+
result = next;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
return result;
|
|
327
|
+
}
|
|
328
|
+
function takeRecentMessagesWithinCount(messages, maxMessages) {
|
|
329
|
+
const keepIndices = new Set();
|
|
330
|
+
let ordinaryCount = 0;
|
|
331
|
+
for (let index = messages.length - 1; index >= 0; index--) {
|
|
332
|
+
const message = messages[index];
|
|
333
|
+
if (isCapabilityControlMessage(message)) {
|
|
334
|
+
keepIndices.add(index);
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
if (ordinaryCount < maxMessages) {
|
|
338
|
+
keepIndices.add(index);
|
|
339
|
+
ordinaryCount++;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
if (keepIndices.size === messages.length)
|
|
344
|
+
return messages;
|
|
345
|
+
return messages.filter((_, index) => keepIndices.has(index));
|
|
346
|
+
}
|
|
347
|
+
function takeRecentMessagesWithinChars(messages, maxChars) {
|
|
348
|
+
const keepIndices = new Set();
|
|
349
|
+
let ordinaryChars = 0;
|
|
350
|
+
let ordinaryCount = 0;
|
|
351
|
+
let ordinaryExceeded = false;
|
|
352
|
+
for (let index = messages.length - 1; index >= 0; index--) {
|
|
353
|
+
const message = messages[index];
|
|
354
|
+
if (isCapabilityControlMessage(message)) {
|
|
355
|
+
keepIndices.add(index);
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
if (ordinaryExceeded) {
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
const messageChars = agentMessageToPreviewText(message).length;
|
|
362
|
+
if (ordinaryCount > 0 && ordinaryChars + messageChars > maxChars) {
|
|
363
|
+
ordinaryExceeded = true;
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
keepIndices.add(index);
|
|
367
|
+
ordinaryCount++;
|
|
368
|
+
ordinaryChars += messageChars;
|
|
369
|
+
if (ordinaryChars >= maxChars) {
|
|
370
|
+
ordinaryExceeded = true;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
if (keepIndices.size === messages.length)
|
|
374
|
+
return messages;
|
|
375
|
+
return messages.filter((_, index) => keepIndices.has(index));
|
|
376
|
+
}
|
|
377
|
+
function addHistoryFilterDiagnostic(diagnostics, itemId, filter, before, after) {
|
|
378
|
+
if (before === after)
|
|
379
|
+
return;
|
|
380
|
+
diagnostics.push({
|
|
381
|
+
level: "info",
|
|
382
|
+
message: `Filtered ${before - after} chat-history message(s) by ${filter}.`,
|
|
383
|
+
itemId,
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
function dropToolHistory(messages, diagnostics, itemId) {
|
|
387
|
+
let removedToolCalls = 0;
|
|
388
|
+
let droppedToolResults = 0;
|
|
389
|
+
let droppedEmptyMessages = 0;
|
|
390
|
+
let changed = false;
|
|
391
|
+
const result = [];
|
|
392
|
+
for (const message of messages) {
|
|
393
|
+
if (isCapabilityControlMessage(message)) {
|
|
394
|
+
result.push(message);
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
if (isToolResultMessage(message)) {
|
|
398
|
+
droppedToolResults++;
|
|
399
|
+
changed = true;
|
|
400
|
+
continue;
|
|
401
|
+
}
|
|
402
|
+
const stripped = stripToolCallParts(message, () => false);
|
|
403
|
+
removedToolCalls += stripped.removedCalls;
|
|
404
|
+
if (stripped.message !== message)
|
|
405
|
+
changed = true;
|
|
406
|
+
if (!stripped.message) {
|
|
407
|
+
droppedEmptyMessages++;
|
|
408
|
+
continue;
|
|
409
|
+
}
|
|
410
|
+
result.push(stripped.message);
|
|
411
|
+
}
|
|
412
|
+
if (!changed)
|
|
413
|
+
return messages;
|
|
414
|
+
diagnostics.push({
|
|
415
|
+
level: "info",
|
|
416
|
+
message: `Dropped tool history from chat-history: removed ${removedToolCalls} tool call(s), dropped ${droppedToolResults} tool result message(s)` +
|
|
417
|
+
(droppedEmptyMessages > 0 ? `, and dropped ${droppedEmptyMessages} empty message(s).` : "."),
|
|
418
|
+
itemId,
|
|
419
|
+
});
|
|
420
|
+
return result;
|
|
421
|
+
}
|
|
422
|
+
function repairToolHistory(messages, diagnostics, itemId) {
|
|
423
|
+
const includedCallIds = new Set();
|
|
424
|
+
const includedResultIds = new Set();
|
|
425
|
+
for (const message of messages) {
|
|
426
|
+
if (isCapabilityControlMessage(message))
|
|
427
|
+
continue;
|
|
428
|
+
for (const id of toolCallIdsForMessage(message))
|
|
429
|
+
includedCallIds.add(id);
|
|
430
|
+
const resultId = toolResultMessageId(message);
|
|
431
|
+
if (resultId)
|
|
432
|
+
includedResultIds.add(resultId);
|
|
433
|
+
}
|
|
434
|
+
let removedToolCalls = 0;
|
|
435
|
+
let droppedToolResults = 0;
|
|
436
|
+
let droppedEmptyMessages = 0;
|
|
437
|
+
let changed = false;
|
|
438
|
+
const result = [];
|
|
439
|
+
for (const message of messages) {
|
|
440
|
+
if (isCapabilityControlMessage(message)) {
|
|
441
|
+
result.push(message);
|
|
442
|
+
continue;
|
|
443
|
+
}
|
|
444
|
+
if (isToolResultMessage(message)) {
|
|
445
|
+
const resultId = toolResultMessageId(message);
|
|
446
|
+
if (!resultId || !includedCallIds.has(resultId)) {
|
|
447
|
+
droppedToolResults++;
|
|
448
|
+
changed = true;
|
|
449
|
+
continue;
|
|
450
|
+
}
|
|
451
|
+
result.push(message);
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
const stripped = stripToolCallParts(message, (id) => !!id && includedResultIds.has(id));
|
|
455
|
+
removedToolCalls += stripped.removedCalls;
|
|
456
|
+
if (stripped.message !== message)
|
|
457
|
+
changed = true;
|
|
458
|
+
if (!stripped.message) {
|
|
459
|
+
droppedEmptyMessages++;
|
|
460
|
+
continue;
|
|
461
|
+
}
|
|
462
|
+
result.push(stripped.message);
|
|
463
|
+
}
|
|
464
|
+
if (!changed)
|
|
465
|
+
return messages;
|
|
466
|
+
diagnostics.push({
|
|
467
|
+
level: "info",
|
|
468
|
+
message: `Repaired tool history after chat-history filtering: removed ${removedToolCalls} dangling tool call(s), dropped ${droppedToolResults} dangling tool result message(s)` +
|
|
469
|
+
(droppedEmptyMessages > 0 ? `, and dropped ${droppedEmptyMessages} empty message(s).` : "."),
|
|
470
|
+
itemId,
|
|
471
|
+
});
|
|
472
|
+
return result;
|
|
473
|
+
}
|
|
474
|
+
function stripToolCallParts(message, keep) {
|
|
475
|
+
const content = message.content;
|
|
476
|
+
if (!Array.isArray(content))
|
|
477
|
+
return { message, removedCalls: 0 };
|
|
478
|
+
const nextContent = content.filter((part) => !isToolCallContent(part) || keep(toolCallPartId(part)));
|
|
479
|
+
const removedCalls = content.length - nextContent.length;
|
|
480
|
+
if (removedCalls === 0)
|
|
481
|
+
return { message, removedCalls: 0 };
|
|
482
|
+
if (nextContent.length === 0)
|
|
483
|
+
return { removedCalls };
|
|
484
|
+
return { message: { ...message, content: nextContent }, removedCalls };
|
|
485
|
+
}
|
|
486
|
+
function toolCallIdsForMessage(message) {
|
|
487
|
+
const content = message.content;
|
|
488
|
+
if (!Array.isArray(content))
|
|
489
|
+
return [];
|
|
490
|
+
return content
|
|
491
|
+
.map((part) => toolCallPartId(part))
|
|
492
|
+
.filter((id) => !!id);
|
|
493
|
+
}
|
|
494
|
+
function isToolCallContent(value) {
|
|
495
|
+
return isPlainObject(value) && value.type === "toolCall";
|
|
496
|
+
}
|
|
497
|
+
function toolCallPartId(value) {
|
|
498
|
+
if (!isPlainObject(value))
|
|
499
|
+
return undefined;
|
|
500
|
+
return firstString(value.id, value.toolCallId, value.callId);
|
|
501
|
+
}
|
|
502
|
+
function isToolResultMessage(message) {
|
|
503
|
+
return messageRole(message) === "toolResult";
|
|
504
|
+
}
|
|
505
|
+
function toolResultMessageId(message) {
|
|
506
|
+
if (!isToolResultMessage(message))
|
|
507
|
+
return undefined;
|
|
508
|
+
const raw = message;
|
|
509
|
+
return firstString(raw.toolCallId, raw.id, raw.callId);
|
|
510
|
+
}
|
|
511
|
+
function isSummaryMessage(message) {
|
|
512
|
+
return SUMMARY_ROLES.has(messageRole(message));
|
|
513
|
+
}
|
|
514
|
+
function messageRole(message) {
|
|
515
|
+
return String(message.role ?? "");
|
|
516
|
+
}
|
|
517
|
+
function positiveIntegerOption(value) {
|
|
518
|
+
return typeof value === "number" && Number.isInteger(value) && value > 0 ? value : undefined;
|
|
519
|
+
}
|
|
520
|
+
function firstString(...values) {
|
|
521
|
+
for (const value of values) {
|
|
522
|
+
if (typeof value === "string" && value)
|
|
523
|
+
return value;
|
|
524
|
+
}
|
|
525
|
+
return undefined;
|
|
526
|
+
}
|
|
527
|
+
function isStringArray(value) {
|
|
528
|
+
return Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
529
|
+
}
|
|
530
|
+
function isPlainObject(value) {
|
|
531
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
532
|
+
}
|
|
533
|
+
function chatHistoryMessageSources(messages, item) {
|
|
534
|
+
return messages.map((message, index) => ({
|
|
535
|
+
kind: "chat-history",
|
|
536
|
+
itemId: item.id,
|
|
537
|
+
itemName: item.name,
|
|
538
|
+
slot: item.slot,
|
|
539
|
+
historyIndex: index + 1,
|
|
540
|
+
historyCount: messages.length,
|
|
541
|
+
role: String(message.role ?? ""),
|
|
542
|
+
}));
|
|
543
|
+
}
|
|
544
|
+
function implicitHistoryMessageSources(messages) {
|
|
545
|
+
return messages.map((message, index) => ({
|
|
546
|
+
kind: "implicit-history",
|
|
547
|
+
itemName: "Conversation history",
|
|
548
|
+
slot: "chat-history",
|
|
549
|
+
historyIndex: index + 1,
|
|
550
|
+
historyCount: messages.length,
|
|
551
|
+
role: String(message.role ?? ""),
|
|
552
|
+
}));
|
|
553
|
+
}
|
|
554
|
+
function stackItemMessageSource(message, item) {
|
|
555
|
+
return {
|
|
556
|
+
kind: "stack-item",
|
|
557
|
+
itemId: item.id,
|
|
558
|
+
itemName: item.name,
|
|
559
|
+
role: String(message.role ?? ""),
|
|
560
|
+
};
|
|
561
|
+
}
|
|
562
|
+
function stripAssistantThinkingFromHistory(messages, diagnostics, itemId) {
|
|
563
|
+
let strippedBlocks = 0;
|
|
564
|
+
let changedMessages = 0;
|
|
565
|
+
let droppedMessages = 0;
|
|
566
|
+
let changed = false;
|
|
567
|
+
const result = [];
|
|
568
|
+
for (const message of messages) {
|
|
569
|
+
if (isCapabilityControlMessage(message)) {
|
|
570
|
+
result.push(message);
|
|
571
|
+
continue;
|
|
572
|
+
}
|
|
573
|
+
const stripped = stripAssistantThinkingFromMessage(message);
|
|
574
|
+
if (stripped.message !== message) {
|
|
575
|
+
changed = true;
|
|
576
|
+
strippedBlocks += stripped.removedBlocks;
|
|
577
|
+
changedMessages++;
|
|
578
|
+
}
|
|
579
|
+
if (!stripped.message) {
|
|
580
|
+
droppedMessages++;
|
|
581
|
+
continue;
|
|
582
|
+
}
|
|
583
|
+
result.push(stripped.message);
|
|
584
|
+
}
|
|
585
|
+
if (!changed)
|
|
586
|
+
return messages;
|
|
587
|
+
diagnostics.push({
|
|
588
|
+
level: "info",
|
|
589
|
+
message: `Stripped ${strippedBlocks} assistant thinking block(s) from ${changedMessages} chat-history message(s)` +
|
|
590
|
+
(droppedMessages > 0 ? ` and dropped ${droppedMessages} empty assistant message(s).` : "."),
|
|
591
|
+
itemId,
|
|
592
|
+
});
|
|
593
|
+
return result;
|
|
594
|
+
}
|
|
595
|
+
function stripAssistantThinkingFromMessage(message) {
|
|
596
|
+
if (message.role !== "assistant")
|
|
597
|
+
return { message, removedBlocks: 0 };
|
|
598
|
+
const content = message.content;
|
|
599
|
+
if (!Array.isArray(content))
|
|
600
|
+
return { message, removedBlocks: 0 };
|
|
601
|
+
const nextContent = content.filter((part) => !isThinkingContent(part));
|
|
602
|
+
const removedBlocks = content.length - nextContent.length;
|
|
603
|
+
if (removedBlocks === 0)
|
|
604
|
+
return { message, removedBlocks: 0 };
|
|
605
|
+
if (nextContent.length === 0)
|
|
606
|
+
return { removedBlocks };
|
|
607
|
+
return { message: { ...message, content: nextContent }, removedBlocks };
|
|
608
|
+
}
|
|
609
|
+
function isThinkingContent(value) {
|
|
610
|
+
return !!value && typeof value === "object" && !Array.isArray(value) && value.type === "thinking";
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* Merge diagnostic lists, dropping exact duplicates. compileSystemPrompt and
|
|
614
|
+
* compileMessages each emit stack-level diagnostics for the same stack, so
|
|
615
|
+
* callers combining their results would otherwise show every issue twice.
|
|
616
|
+
*/
|
|
617
|
+
export function dedupeDiagnostics(lists) {
|
|
618
|
+
const seen = new Set();
|
|
619
|
+
const merged = [];
|
|
620
|
+
for (const list of lists) {
|
|
621
|
+
for (const diagnostic of list) {
|
|
622
|
+
const key = `${diagnostic.level}${diagnostic.itemId ?? ""}${diagnostic.message}`;
|
|
623
|
+
if (seen.has(key))
|
|
624
|
+
continue;
|
|
625
|
+
seen.add(key);
|
|
626
|
+
merged.push(diagnostic);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
return merged;
|
|
630
|
+
}
|
|
631
|
+
function cacheSensitiveContentDiagnostics(stack) {
|
|
632
|
+
const diagnostics = [];
|
|
633
|
+
for (const item of stack.items) {
|
|
634
|
+
if (item.enabled === false)
|
|
635
|
+
continue;
|
|
636
|
+
if (item.kind === "block") {
|
|
637
|
+
const usesTime = templateUsesRuntimeField(item.content, "time");
|
|
638
|
+
const usesDate = templateUsesRuntimeField(item.content, "date");
|
|
639
|
+
if (usesTime) {
|
|
640
|
+
diagnostics.push({
|
|
641
|
+
level: "warning",
|
|
642
|
+
message: "{{time}} changes every turn, invalidating prompt-prefix cache from this item onward. Use {{date}} instead, or accept the cache cost.",
|
|
643
|
+
itemId: item.id,
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
if (usesDate) {
|
|
647
|
+
diagnostics.push({
|
|
648
|
+
level: "info",
|
|
649
|
+
message: "{{date}} changes daily; the prompt-prefix cache rebuilds once per day and stays stable within the day. Usually fine.",
|
|
650
|
+
itemId: item.id,
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
continue;
|
|
654
|
+
}
|
|
655
|
+
if ((item.slot === "date" || item.slot === "date-cwd") && item.options?.includeTime === true) {
|
|
656
|
+
diagnostics.push({
|
|
657
|
+
level: "warning",
|
|
658
|
+
message: `Slot "${item.slot}" has includeTime enabled: the value changes every turn, invalidating prompt-prefix cache from this item onward. Remove includeTime, or accept the cache cost.`,
|
|
659
|
+
itemId: item.id,
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
return diagnostics;
|
|
664
|
+
}
|
|
665
|
+
function templateUsesRuntimeField(content, field) {
|
|
666
|
+
const parsed = forgeV1.parse(content);
|
|
667
|
+
if (!parsed.ok)
|
|
668
|
+
return false;
|
|
669
|
+
const analyzed = forgeV1.analyze(parsed.ast);
|
|
670
|
+
return analyzed.dependencies.some((dependency) => dependencyUsesRuntimeField(dependency, field));
|
|
671
|
+
}
|
|
672
|
+
function dependencyUsesRuntimeField(dependency, field) {
|
|
673
|
+
const path = dependency.path;
|
|
674
|
+
if (!path)
|
|
675
|
+
return false;
|
|
676
|
+
return (dependency.kind === "legacy" && path.length === 1 && path[0] === field)
|
|
677
|
+
|| (dependency.kind === "runtime" && path.length === 2 && path[0] === "runtime" && path[1] === field);
|
|
678
|
+
}
|
|
679
|
+
function enabledItems(stack) {
|
|
680
|
+
return stack.items.filter((item) => item.enabled !== false);
|
|
681
|
+
}
|
|
682
|
+
function createSyntheticMessage(role, content, stack, runtime) {
|
|
683
|
+
const timestamp = runtime.now.getTime();
|
|
684
|
+
const visible = stack.defaults?.syntheticMessagesVisible ?? false;
|
|
685
|
+
if (role === "custom") {
|
|
686
|
+
return {
|
|
687
|
+
role: "custom",
|
|
688
|
+
customType: "pi-forge",
|
|
689
|
+
content: [{ type: "text", text: content }],
|
|
690
|
+
display: visible,
|
|
691
|
+
details: { stackId: stack.id },
|
|
692
|
+
timestamp,
|
|
693
|
+
};
|
|
694
|
+
}
|
|
695
|
+
if (role === "assistant") {
|
|
696
|
+
const model = runtime.model;
|
|
697
|
+
return {
|
|
698
|
+
role: "assistant",
|
|
699
|
+
content: [{ type: "text", text: content }],
|
|
700
|
+
api: model?.api ?? "unknown",
|
|
701
|
+
provider: model?.provider ?? "unknown",
|
|
702
|
+
model: model?.id ?? "unknown",
|
|
703
|
+
usage: ZERO_USAGE,
|
|
704
|
+
stopReason: "stop",
|
|
705
|
+
timestamp,
|
|
706
|
+
};
|
|
707
|
+
}
|
|
708
|
+
return {
|
|
709
|
+
role: "user",
|
|
710
|
+
content: [{ type: "text", text: content }],
|
|
711
|
+
timestamp,
|
|
712
|
+
};
|
|
713
|
+
}
|
|
714
|
+
function contentToText(content) {
|
|
715
|
+
if (typeof content === "string")
|
|
716
|
+
return content;
|
|
717
|
+
if (!Array.isArray(content))
|
|
718
|
+
return "";
|
|
719
|
+
return content
|
|
720
|
+
.map((part) => {
|
|
721
|
+
if (!part || typeof part !== "object")
|
|
722
|
+
return "";
|
|
723
|
+
const obj = part;
|
|
724
|
+
if (obj.type === "text" && typeof obj.text === "string")
|
|
725
|
+
return obj.text;
|
|
726
|
+
if (obj.type === "thinking" && typeof obj.thinking === "string")
|
|
727
|
+
return `<thinking>\n${obj.thinking}\n</thinking>`;
|
|
728
|
+
if (obj.type === "toolCall")
|
|
729
|
+
return `[toolCall: ${String(obj.name ?? "unknown")}]`;
|
|
730
|
+
if (obj.type === "image")
|
|
731
|
+
return "[image]";
|
|
732
|
+
return "";
|
|
733
|
+
})
|
|
734
|
+
.filter(Boolean)
|
|
735
|
+
.join("\n");
|
|
736
|
+
}
|
|
737
|
+
//# sourceMappingURL=compiler.js.map
|