@code-yeongyu/senpi 2026.7.20 → 2026.7.22
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 +41 -0
- package/README.md +1 -0
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +1 -0
- package/dist/cli/args.js.map +1 -1
- package/dist/core/agent-session.d.ts +32 -0
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +409 -68
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/auth-storage.js +1 -1
- package/dist/core/auth-storage.js.map +1 -1
- package/dist/core/compaction/compaction.d.ts +2 -2
- package/dist/core/compaction/compaction.d.ts.map +1 -1
- package/dist/core/compaction/compaction.js +67 -16
- package/dist/core/compaction/compaction.js.map +1 -1
- package/dist/core/compaction/stream-watchdog.d.ts +31 -0
- package/dist/core/compaction/stream-watchdog.d.ts.map +1 -0
- package/dist/core/compaction/stream-watchdog.js +78 -0
- package/dist/core/compaction/stream-watchdog.js.map +1 -0
- package/dist/core/extensions/builtin/btw/index.d.ts +3 -0
- package/dist/core/extensions/builtin/btw/index.d.ts.map +1 -0
- package/dist/core/extensions/builtin/btw/index.js +120 -0
- package/dist/core/extensions/builtin/btw/index.js.map +1 -0
- package/dist/core/extensions/builtin/btw/panel.d.ts +21 -0
- package/dist/core/extensions/builtin/btw/panel.d.ts.map +1 -0
- package/dist/core/extensions/builtin/btw/panel.js +61 -0
- package/dist/core/extensions/builtin/btw/panel.js.map +1 -0
- package/dist/core/extensions/builtin/btw/side-query.d.ts +31 -0
- package/dist/core/extensions/builtin/btw/side-query.d.ts.map +1 -0
- package/dist/core/extensions/builtin/btw/side-query.js +68 -0
- package/dist/core/extensions/builtin/btw/side-query.js.map +1 -0
- package/dist/core/extensions/builtin/compaction/index.d.ts.map +1 -1
- package/dist/core/extensions/builtin/compaction/index.js +47 -11
- package/dist/core/extensions/builtin/compaction/index.js.map +1 -1
- package/dist/core/extensions/builtin/compaction/openai-remote.d.ts.map +1 -1
- package/dist/core/extensions/builtin/compaction/openai-remote.js +3 -1
- package/dist/core/extensions/builtin/compaction/openai-remote.js.map +1 -1
- package/dist/core/extensions/builtin/compaction/speculative.d.ts +17 -0
- package/dist/core/extensions/builtin/compaction/speculative.d.ts.map +1 -1
- package/dist/core/extensions/builtin/compaction/speculative.js +93 -26
- package/dist/core/extensions/builtin/compaction/speculative.js.map +1 -1
- package/dist/core/extensions/builtin/config-reload/index.d.ts +39 -0
- package/dist/core/extensions/builtin/config-reload/index.d.ts.map +1 -0
- package/dist/core/extensions/builtin/config-reload/index.js +659 -0
- package/dist/core/extensions/builtin/config-reload/index.js.map +1 -0
- package/dist/core/extensions/builtin/config-reload/log.d.ts +55 -0
- package/dist/core/extensions/builtin/config-reload/log.d.ts.map +1 -0
- package/dist/core/extensions/builtin/config-reload/log.js +128 -0
- package/dist/core/extensions/builtin/config-reload/log.js.map +1 -0
- package/dist/core/extensions/builtin/config-reload/protocol.d.ts +71 -0
- package/dist/core/extensions/builtin/config-reload/protocol.d.ts.map +1 -0
- package/dist/core/extensions/builtin/config-reload/protocol.js +83 -0
- package/dist/core/extensions/builtin/config-reload/protocol.js.map +1 -0
- package/dist/core/extensions/builtin/config-reload/watch-engine.d.ts +43 -0
- package/dist/core/extensions/builtin/config-reload/watch-engine.d.ts.map +1 -0
- package/dist/core/extensions/builtin/config-reload/watch-engine.js +298 -0
- package/dist/core/extensions/builtin/config-reload/watch-engine.js.map +1 -0
- package/dist/core/extensions/builtin/goal/command-registration.d.ts +2 -2
- package/dist/core/extensions/builtin/goal/command-registration.d.ts.map +1 -1
- package/dist/core/extensions/builtin/goal/command-registration.js +3 -4
- package/dist/core/extensions/builtin/goal/command-registration.js.map +1 -1
- package/dist/core/extensions/builtin/goal/index.d.ts.map +1 -1
- package/dist/core/extensions/builtin/goal/index.js +11 -27
- package/dist/core/extensions/builtin/goal/index.js.map +1 -1
- package/dist/core/extensions/builtin/goal/tool-registration.d.ts +2 -2
- package/dist/core/extensions/builtin/goal/tool-registration.d.ts.map +1 -1
- package/dist/core/extensions/builtin/goal/tool-registration.js +2 -3
- package/dist/core/extensions/builtin/goal/tool-registration.js.map +1 -1
- package/dist/core/extensions/builtin/goal/turn-usage.d.ts +20 -0
- package/dist/core/extensions/builtin/goal/turn-usage.d.ts.map +1 -0
- package/dist/core/extensions/builtin/goal/turn-usage.js +61 -0
- package/dist/core/extensions/builtin/goal/turn-usage.js.map +1 -0
- package/dist/core/extensions/builtin/index.d.ts.map +1 -1
- package/dist/core/extensions/builtin/index.js +9 -0
- package/dist/core/extensions/builtin/index.js.map +1 -1
- package/dist/core/extensions/builtin/look-at/arguments.d.ts +27 -0
- package/dist/core/extensions/builtin/look-at/arguments.d.ts.map +1 -0
- package/dist/core/extensions/builtin/look-at/arguments.js +109 -0
- package/dist/core/extensions/builtin/look-at/arguments.js.map +1 -0
- package/dist/core/extensions/builtin/look-at/commands.d.ts +9 -0
- package/dist/core/extensions/builtin/look-at/commands.d.ts.map +1 -0
- package/dist/core/extensions/builtin/look-at/commands.js +102 -0
- package/dist/core/extensions/builtin/look-at/commands.js.map +1 -0
- package/dist/core/extensions/builtin/look-at/image-input.d.ts +21 -0
- package/dist/core/extensions/builtin/look-at/image-input.d.ts.map +1 -0
- package/dist/core/extensions/builtin/look-at/image-input.js +151 -0
- package/dist/core/extensions/builtin/look-at/image-input.js.map +1 -0
- package/dist/core/extensions/builtin/look-at/index.d.ts +3 -0
- package/dist/core/extensions/builtin/look-at/index.d.ts.map +1 -0
- package/dist/core/extensions/builtin/look-at/index.js +58 -0
- package/dist/core/extensions/builtin/look-at/index.js.map +1 -0
- package/dist/core/extensions/builtin/look-at/model-selector.d.ts +9 -0
- package/dist/core/extensions/builtin/look-at/model-selector.d.ts.map +1 -0
- package/dist/core/extensions/builtin/look-at/model-selector.js +77 -0
- package/dist/core/extensions/builtin/look-at/model-selector.js.map +1 -0
- package/dist/core/extensions/builtin/look-at/prompts.d.ts +5 -0
- package/dist/core/extensions/builtin/look-at/prompts.d.ts.map +1 -0
- package/dist/core/extensions/builtin/look-at/prompts.js +19 -0
- package/dist/core/extensions/builtin/look-at/prompts.js.map +1 -0
- package/dist/core/extensions/builtin/look-at/render.d.ts +26 -0
- package/dist/core/extensions/builtin/look-at/render.d.ts.map +1 -0
- package/dist/core/extensions/builtin/look-at/render.js +70 -0
- package/dist/core/extensions/builtin/look-at/render.js.map +1 -0
- package/dist/core/extensions/builtin/look-at/runner.d.ts +16 -0
- package/dist/core/extensions/builtin/look-at/runner.d.ts.map +1 -0
- package/dist/core/extensions/builtin/look-at/runner.js +108 -0
- package/dist/core/extensions/builtin/look-at/runner.js.map +1 -0
- package/dist/core/extensions/builtin/look-at/settings.d.ts +13 -0
- package/dist/core/extensions/builtin/look-at/settings.d.ts.map +1 -0
- package/dist/core/extensions/builtin/look-at/settings.js +20 -0
- package/dist/core/extensions/builtin/look-at/settings.js.map +1 -0
- package/dist/core/extensions/builtin/mcp/commands.d.ts.map +1 -1
- package/dist/core/extensions/builtin/mcp/commands.js +2 -0
- package/dist/core/extensions/builtin/mcp/commands.js.map +1 -1
- package/dist/core/extensions/builtin/mcp/config-schema.d.ts +6 -0
- package/dist/core/extensions/builtin/mcp/config-schema.d.ts.map +1 -1
- package/dist/core/extensions/builtin/mcp/config-schema.js +1 -0
- package/dist/core/extensions/builtin/mcp/config-schema.js.map +1 -1
- package/dist/core/extensions/builtin/mcp/config.d.ts.map +1 -1
- package/dist/core/extensions/builtin/mcp/config.js +9 -1
- package/dist/core/extensions/builtin/mcp/config.js.map +1 -1
- package/dist/core/extensions/builtin/mcp/service.d.ts.map +1 -1
- package/dist/core/extensions/builtin/mcp/service.js +22 -6
- package/dist/core/extensions/builtin/mcp/service.js.map +1 -1
- package/dist/core/extensions/builtin/mcp/startup-race.d.ts +10 -0
- package/dist/core/extensions/builtin/mcp/startup-race.d.ts.map +1 -1
- package/dist/core/extensions/builtin/mcp/startup-race.js +18 -1
- package/dist/core/extensions/builtin/mcp/startup-race.js.map +1 -1
- package/dist/core/extensions/builtin/model-fallback/index.d.ts +4 -0
- package/dist/core/extensions/builtin/model-fallback/index.d.ts.map +1 -0
- package/dist/core/extensions/builtin/model-fallback/index.js +55 -0
- package/dist/core/extensions/builtin/model-fallback/index.js.map +1 -0
- package/dist/core/extensions/builtin/model-fallback/settings.d.ts +6 -0
- package/dist/core/extensions/builtin/model-fallback/settings.d.ts.map +1 -0
- package/dist/core/extensions/builtin/model-fallback/settings.js +11 -0
- package/dist/core/extensions/builtin/model-fallback/settings.js.map +1 -0
- package/dist/core/extensions/builtin/model-fallback/ui.d.ts +12 -0
- package/dist/core/extensions/builtin/model-fallback/ui.d.ts.map +1 -0
- package/dist/core/extensions/builtin/model-fallback/ui.js +72 -0
- package/dist/core/extensions/builtin/model-fallback/ui.js.map +1 -0
- package/dist/core/extensions/builtin/nested-agents-md/index.d.ts.map +1 -1
- package/dist/core/extensions/builtin/nested-agents-md/index.js +4 -1
- package/dist/core/extensions/builtin/nested-agents-md/index.js.map +1 -1
- package/dist/core/extensions/builtin/rules/index.d.ts.map +1 -1
- package/dist/core/extensions/builtin/rules/index.js +4 -1
- package/dist/core/extensions/builtin/rules/index.js.map +1 -1
- package/dist/core/extensions/builtin/terminal/output-format.d.ts +28 -0
- package/dist/core/extensions/builtin/terminal/output-format.d.ts.map +1 -0
- package/dist/core/extensions/builtin/terminal/output-format.js +74 -0
- package/dist/core/extensions/builtin/terminal/output-format.js.map +1 -0
- package/dist/core/extensions/builtin/terminal/runtime-session.d.ts +3 -0
- package/dist/core/extensions/builtin/terminal/runtime-session.d.ts.map +1 -1
- package/dist/core/extensions/builtin/terminal/runtime-session.js +22 -2
- package/dist/core/extensions/builtin/terminal/runtime-session.js.map +1 -1
- package/dist/core/extensions/builtin/terminal/shared.d.ts +7 -0
- package/dist/core/extensions/builtin/terminal/shared.d.ts.map +1 -1
- package/dist/core/extensions/builtin/terminal/shared.js +14 -0
- package/dist/core/extensions/builtin/terminal/shared.js.map +1 -1
- package/dist/core/extensions/builtin/terminal/tools/bash-output.d.ts +5 -1
- package/dist/core/extensions/builtin/terminal/tools/bash-output.d.ts.map +1 -1
- package/dist/core/extensions/builtin/terminal/tools/bash-output.js +88 -6
- package/dist/core/extensions/builtin/terminal/tools/bash-output.js.map +1 -1
- package/dist/core/extensions/builtin/terminal/tools/bash.d.ts +12 -1
- package/dist/core/extensions/builtin/terminal/tools/bash.d.ts.map +1 -1
- package/dist/core/extensions/builtin/terminal/tools/bash.js +82 -8
- package/dist/core/extensions/builtin/terminal/tools/bash.js.map +1 -1
- package/dist/core/extensions/builtin/terminal/tools/render.d.ts +16 -0
- package/dist/core/extensions/builtin/terminal/tools/render.d.ts.map +1 -0
- package/dist/core/extensions/builtin/terminal/tools/render.js +30 -0
- package/dist/core/extensions/builtin/terminal/tools/render.js.map +1 -0
- package/dist/core/extensions/builtin/terminal/tools/spawn.d.ts +2 -0
- package/dist/core/extensions/builtin/terminal/tools/spawn.d.ts.map +1 -1
- package/dist/core/extensions/builtin/terminal/tools/spawn.js +1 -1
- package/dist/core/extensions/builtin/terminal/tools/spawn.js.map +1 -1
- package/dist/core/extensions/builtin/todotools/tools/todo.d.ts.map +1 -1
- package/dist/core/extensions/builtin/todotools/tools/todo.js +22 -6
- package/dist/core/extensions/builtin/todotools/tools/todo.js.map +1 -1
- package/dist/core/extensions/builtin/webfetch/webfetch/fetcher.d.ts +1 -0
- package/dist/core/extensions/builtin/webfetch/webfetch/fetcher.d.ts.map +1 -1
- package/dist/core/extensions/builtin/webfetch/webfetch/fetcher.js +13 -4
- package/dist/core/extensions/builtin/webfetch/webfetch/fetcher.js.map +1 -1
- package/dist/core/extensions/builtin/webfetch/webfetch/renderers.d.ts.map +1 -1
- package/dist/core/extensions/builtin/webfetch/webfetch/renderers.js +17 -2
- package/dist/core/extensions/builtin/webfetch/webfetch/renderers.js.map +1 -1
- package/dist/core/extensions/builtin/webfetch/webfetch/tool.d.ts +8 -1
- package/dist/core/extensions/builtin/webfetch/webfetch/tool.d.ts.map +1 -1
- package/dist/core/extensions/builtin/webfetch/webfetch/tool.js +45 -3
- package/dist/core/extensions/builtin/webfetch/webfetch/tool.js.map +1 -1
- package/dist/core/extensions/builtin/websearch/websearch/renderers.d.ts.map +1 -1
- package/dist/core/extensions/builtin/websearch/websearch/renderers.js +13 -4
- package/dist/core/extensions/builtin/websearch/websearch/renderers.js.map +1 -1
- package/dist/core/extensions/builtin/websearch/websearch/search.d.ts +3 -2
- package/dist/core/extensions/builtin/websearch/websearch/search.d.ts.map +1 -1
- package/dist/core/extensions/builtin/websearch/websearch/search.js +2 -1
- package/dist/core/extensions/builtin/websearch/websearch/search.js.map +1 -1
- package/dist/core/extensions/builtin/websearch/websearch/tool.d.ts.map +1 -1
- package/dist/core/extensions/builtin/websearch/websearch/tool.js +17 -1
- package/dist/core/extensions/builtin/websearch/websearch/tool.js.map +1 -1
- package/dist/core/extensions/builtin/websearch/websearch/types.d.ts +2 -0
- package/dist/core/extensions/builtin/websearch/websearch/types.d.ts.map +1 -1
- package/dist/core/extensions/builtin/websearch/websearch/types.js.map +1 -1
- package/dist/core/extensions/runner.d.ts +6 -0
- package/dist/core/extensions/runner.d.ts.map +1 -1
- package/dist/core/extensions/runner.js +71 -3
- package/dist/core/extensions/runner.js.map +1 -1
- package/dist/core/extensions/types.d.ts +96 -10
- package/dist/core/extensions/types.d.ts.map +1 -1
- package/dist/core/extensions/types.js.map +1 -1
- package/dist/core/model-config-schema.d.ts +1942 -0
- package/dist/core/model-config-schema.d.ts.map +1 -0
- package/dist/core/model-config-schema.js +197 -0
- package/dist/core/model-config-schema.js.map +1 -0
- package/dist/core/model-config.d.ts +2 -578
- package/dist/core/model-config.d.ts.map +1 -1
- package/dist/core/model-config.js +1 -194
- package/dist/core/model-config.js.map +1 -1
- package/dist/core/model-resolver.d.ts.map +1 -1
- package/dist/core/model-resolver.js +1 -0
- package/dist/core/model-resolver.js.map +1 -1
- package/dist/core/model-runtime.d.ts.map +1 -1
- package/dist/core/model-runtime.js +9 -3
- package/dist/core/model-runtime.js.map +1 -1
- package/dist/core/provider-composer.d.ts +1 -0
- package/dist/core/provider-composer.d.ts.map +1 -1
- package/dist/core/provider-composer.js +3 -2
- package/dist/core/provider-composer.js.map +1 -1
- package/dist/core/provider-display-names.d.ts.map +1 -1
- package/dist/core/provider-display-names.js +1 -0
- package/dist/core/provider-display-names.js.map +1 -1
- package/dist/core/retry-fallback/chains.d.ts +29 -0
- package/dist/core/retry-fallback/chains.d.ts.map +1 -0
- package/dist/core/retry-fallback/chains.js +109 -0
- package/dist/core/retry-fallback/chains.js.map +1 -0
- package/dist/core/retry-fallback/controller.d.ts +77 -0
- package/dist/core/retry-fallback/controller.d.ts.map +1 -0
- package/dist/core/retry-fallback/controller.js +157 -0
- package/dist/core/retry-fallback/controller.js.map +1 -0
- package/dist/core/retry-fallback/cooldown.d.ts +21 -0
- package/dist/core/retry-fallback/cooldown.d.ts.map +1 -0
- package/dist/core/retry-fallback/cooldown.js +49 -0
- package/dist/core/retry-fallback/cooldown.js.map +1 -0
- package/dist/core/retry-fallback/log.d.ts +10 -0
- package/dist/core/retry-fallback/log.d.ts.map +1 -0
- package/dist/core/retry-fallback/log.js +128 -0
- package/dist/core/retry-fallback/log.js.map +1 -0
- package/dist/core/retry-fallback/validate.d.ts +8 -0
- package/dist/core/retry-fallback/validate.d.ts.map +1 -0
- package/dist/core/retry-fallback/validate.js +63 -0
- package/dist/core/retry-fallback/validate.js.map +1 -0
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/sdk.js +1 -0
- package/dist/core/sdk.js.map +1 -1
- package/dist/core/session-manager.d.ts +39 -3
- package/dist/core/session-manager.d.ts.map +1 -1
- package/dist/core/session-manager.js +125 -16
- package/dist/core/session-manager.js.map +1 -1
- package/dist/core/settings-manager.d.ts +33 -0
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +153 -6
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/modes/interactive/components/footer.d.ts +2 -1
- package/dist/modes/interactive/components/footer.d.ts.map +1 -1
- package/dist/modes/interactive/components/footer.js +30 -20
- package/dist/modes/interactive/components/footer.js.map +1 -1
- package/dist/modes/interactive/components/todo-strike.d.ts +8 -0
- package/dist/modes/interactive/components/todo-strike.d.ts.map +1 -0
- package/dist/modes/interactive/components/todo-strike.js +35 -0
- package/dist/modes/interactive/components/todo-strike.js.map +1 -0
- package/dist/modes/interactive/components/tool-execution-renderer.d.ts.map +1 -1
- package/dist/modes/interactive/components/tool-execution-renderer.js +8 -1
- package/dist/modes/interactive/components/tool-execution-renderer.js.map +1 -1
- package/dist/modes/interactive/components/tool-execution.d.ts +4 -0
- package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
- package/dist/modes/interactive/components/tool-execution.js +51 -2
- package/dist/modes/interactive/components/tool-execution.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +6 -0
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +124 -4
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/interactive/tool-progress.d.ts +8 -0
- package/dist/modes/interactive/tool-progress.d.ts.map +1 -0
- package/dist/modes/interactive/tool-progress.js +26 -0
- package/dist/modes/interactive/tool-progress.js.map +1 -0
- package/dist/modes/interactive/tool-result-reveal.d.ts +18 -0
- package/dist/modes/interactive/tool-result-reveal.d.ts.map +1 -0
- package/dist/modes/interactive/tool-result-reveal.js +155 -0
- package/dist/modes/interactive/tool-result-reveal.js.map +1 -0
- package/dist/utils/fs-watch.d.ts +2 -2
- package/dist/utils/fs-watch.d.ts.map +1 -1
- package/dist/utils/fs-watch.js +2 -2
- package/dist/utils/fs-watch.js.map +1 -1
- package/docs/extensions.md +79 -1
- package/docs/mcp.md +10 -1
- package/docs/models.md +102 -1
- package/docs/providers.md +3 -1
- package/docs/settings.md +35 -0
- package/node_modules/@code-yeongyu/senpi-codemode/CHANGELOG.md +24 -0
- package/node_modules/@code-yeongyu/senpi-codemode/package.json +3 -3
- package/node_modules/@code-yeongyu/senpi-codemode/src/tool/cell-handler.ts +13 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/agent-loop.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/agent-loop.js +76 -8
- package/node_modules/@earendil-works/pi-agent-core/dist/agent-loop.js.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/agent.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/agent.js +26 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/agent.js.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/agent-harness.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/agent-harness.js +24 -2
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/agent-harness.js.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/types.d.ts +5 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/types.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/types.js.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/package.json +2 -2
- package/node_modules/@earendil-works/pi-ai/README.md +2 -0
- package/node_modules/@earendil-works/pi-ai/dist/api/anthropic-messages.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/anthropic-messages.js +68 -25
- package/node_modules/@earendil-works/pi-ai/dist/api/anthropic-messages.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/lazy.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/lazy.js +33 -6
- package/node_modules/@earendil-works/pi-ai/dist/api/lazy.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-responses-shared.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-responses-shared.js +6 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-responses-shared.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/transform-messages.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/transform-messages.js +62 -52
- package/node_modules/@earendil-works/pi-ai/dist/api/transform-messages.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api-registry.d.ts +19 -0
- package/node_modules/@earendil-works/pi-ai/dist/api-registry.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/api-registry.js +50 -0
- package/node_modules/@earendil-works/pi-ai/dist/api-registry.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/compat.d.ts +2 -17
- package/node_modules/@earendil-works/pi-ai/dist/compat.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/compat.js +3 -52
- package/node_modules/@earendil-works/pi-ai/dist/compat.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/env-api-keys.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/env-api-keys.js +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/env-api-keys.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/image-models.generated.d.ts +45 -0
- package/node_modules/@earendil-works/pi-ai/dist/image-models.generated.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/image-models.generated.js +45 -0
- package/node_modules/@earendil-works/pi-ai/dist/image-models.generated.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/index.d.ts +5 -0
- package/node_modules/@earendil-works/pi-ai/dist/index.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/index.js +5 -0
- package/node_modules/@earendil-works/pi-ai/dist/index.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/model.d.ts +27 -0
- package/node_modules/@earendil-works/pi-ai/dist/model.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/model.js +2 -0
- package/node_modules/@earendil-works/pi-ai/dist/model.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/models.generated.d.ts +1178 -1072
- package/node_modules/@earendil-works/pi-ai/dist/models.generated.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/models.generated.js +2 -0
- package/node_modules/@earendil-works/pi-ai/dist/models.generated.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/alibaba-token-plan.d.ts +3 -0
- package/node_modules/@earendil-works/pi-ai/dist/providers/alibaba-token-plan.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/providers/alibaba-token-plan.js +15 -0
- package/node_modules/@earendil-works/pi-ai/dist/providers/alibaba-token-plan.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/providers/alibaba-token-plan.models.d.ts +64 -0
- package/node_modules/@earendil-works/pi-ai/dist/providers/alibaba-token-plan.models.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/providers/alibaba-token-plan.models.js +5 -0
- package/node_modules/@earendil-works/pi-ai/dist/providers/alibaba-token-plan.models.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/providers/all.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/all.js +2 -0
- package/node_modules/@earendil-works/pi-ai/dist/providers/all.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/alibaba-token-plan.json +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/google.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/kimi-coding.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/opencode.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/openrouter.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/vercel-ai-gateway.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/faux.d.ts +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/providers/faux.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/faux.js +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/providers/faux.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/google.models.d.ts +8 -0
- package/node_modules/@earendil-works/pi-ai/dist/providers/google.models.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/google.models.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/kimi-coding.models.d.ts +0 -4
- package/node_modules/@earendil-works/pi-ai/dist/providers/kimi-coding.models.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/kimi-coding.models.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/opencode.models.d.ts +12 -4
- package/node_modules/@earendil-works/pi-ai/dist/providers/opencode.models.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/opencode.models.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/openrouter.models.d.ts +20 -4
- package/node_modules/@earendil-works/pi-ai/dist/providers/openrouter.models.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/openrouter.models.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/vercel-ai-gateway.models.d.ts +16 -0
- package/node_modules/@earendil-works/pi-ai/dist/providers/vercel-ai-gateway.models.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/vercel-ai-gateway.models.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/index.d.ts +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/index.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/index.js +8 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/index.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-stream-helpers.d.ts +20 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-stream-helpers.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-stream-helpers.js +79 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-stream-helpers.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-tag-scanner.d.ts +2 -18
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-tag-scanner.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-tag-scanner.js +4 -218
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-tag-scanner.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-tag-syntax.d.ts +38 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-tag-syntax.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-tag-syntax.js +210 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-tag-syntax.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/recovery-stream.d.ts +9 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/recovery-stream.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/recovery-stream.js +231 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/recovery-stream.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/recovery-wrapper-state.d.ts +38 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/recovery-wrapper-state.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/recovery-wrapper-state.js +84 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/recovery-wrapper-state.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/stream-boundary.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/stream-boundary.js +20 -57
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/stream-boundary.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/stream.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/stream.js +16 -99
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/stream.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/antml/index.d.ts +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/antml/index.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/antml/index.js +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/antml/index.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/antml/recovery-stream.d.ts +5 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/antml/recovery-stream.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/antml/recovery-stream.js +6 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/antml/recovery-stream.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-code-mask.d.ts +19 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-code-mask.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-code-mask.js +185 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-code-mask.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-content-lifecycle.d.ts +11 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-content-lifecycle.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-content-lifecycle.js +24 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-content-lifecycle.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-diagnostics.d.ts +4 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-diagnostics.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-diagnostics.js +13 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-diagnostics.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-event-stream.d.ts +15 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-event-stream.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-event-stream.js +37 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-event-stream.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-message-snapshot.d.ts +4 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-message-snapshot.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-message-snapshot.js +61 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-message-snapshot.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-native-projection.d.ts +29 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-native-projection.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-native-projection.js +148 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-native-projection.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-stream-failure.d.ts +5 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-stream-failure.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-stream-failure.js +36 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-stream-failure.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-stream-terminal.d.ts +15 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-stream-terminal.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-stream-terminal.js +88 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-stream-terminal.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-stream-wrapper.d.ts +3 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-stream-wrapper.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-stream-wrapper.js +233 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-stream-wrapper.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-text-projection.d.ts +23 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-text-projection.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-text-projection.js +77 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/recovery-text-projection.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/stream-message-metadata.d.ts +5 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/stream-message-metadata.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/stream-message-metadata.js +63 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/stream-message-metadata.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/stream-thinking-projection.d.ts +13 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/stream-thinking-projection.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/stream-thinking-projection.js +52 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/stream-thinking-projection.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/stream-wrapper-shared.d.ts +41 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/stream-wrapper-shared.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/stream-wrapper-shared.js +209 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/stream-wrapper-shared.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/stream-wrapper.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/stream-wrapper.js +70 -346
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/stream-wrapper.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/types.d.ts +18 -23
- package/node_modules/@earendil-works/pi-ai/dist/types.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/types.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/utils/retry.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/utils/retry.js +5 -1
- package/node_modules/@earendil-works/pi-ai/dist/utils/retry.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/utils/stop-details.d.ts +4 -0
- package/node_modules/@earendil-works/pi-ai/dist/utils/stop-details.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/utils/stop-details.js +6 -0
- package/node_modules/@earendil-works/pi-ai/dist/utils/stop-details.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/package.json +1 -1
- package/node_modules/@earendil-works/pi-pty/package.json +1 -1
- package/node_modules/@earendil-works/pi-tui/package.json +1 -1
- package/package.json +6 -6
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { lstatSync, readdirSync, readFileSync } from "node:fs";
|
|
3
|
+
import { basename, isAbsolute, join, normalize, resolve, sep } from "node:path";
|
|
4
|
+
import { watchWithErrorHandler } from "../../../../utils/fs-watch.js";
|
|
5
|
+
const DEFAULT_DEBOUNCE_MS = 200;
|
|
6
|
+
const DEFAULT_CLOCK = {
|
|
7
|
+
setTimeout: (callback, delayMs) => setTimeout(callback, delayMs),
|
|
8
|
+
clearTimeout: (timer) => clearTimeout(timer),
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Hash-gates filesystem events. Targets are directories by design: watching a
|
|
12
|
+
* file directly breaks when editors atomically rename a replacement over it.
|
|
13
|
+
*/
|
|
14
|
+
export class ConfigReloadWatchEngine {
|
|
15
|
+
#states;
|
|
16
|
+
#unsubscribes = [];
|
|
17
|
+
#subscribe;
|
|
18
|
+
#onRealChange;
|
|
19
|
+
#onError;
|
|
20
|
+
#hashFile;
|
|
21
|
+
#clock;
|
|
22
|
+
#debounceMs;
|
|
23
|
+
#pending = new Map();
|
|
24
|
+
#timer;
|
|
25
|
+
#closed = false;
|
|
26
|
+
constructor(options) {
|
|
27
|
+
this.#subscribe = options.subscribe;
|
|
28
|
+
this.#onRealChange = options.onRealChange;
|
|
29
|
+
this.#onError = options.onError ?? (() => { });
|
|
30
|
+
this.#hashFile = options.hashFile ?? hashFile;
|
|
31
|
+
this.#clock = options.clock ?? DEFAULT_CLOCK;
|
|
32
|
+
this.#debounceMs = options.debounceMs ?? DEFAULT_DEBOUNCE_MS;
|
|
33
|
+
this.#states = options.targets.map((target) => {
|
|
34
|
+
const resolvedTarget = { ...target, path: resolve(target.path) };
|
|
35
|
+
const scan = this.#scan(resolvedTarget);
|
|
36
|
+
return {
|
|
37
|
+
target: resolvedTarget,
|
|
38
|
+
hashes: scan.hashes,
|
|
39
|
+
allowedDirectories: scan.allowedDirectories,
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
for (const state of this.#states) {
|
|
43
|
+
try {
|
|
44
|
+
this.#unsubscribes.push(this.#subscribe(state.target.path, (eventType, filename) => {
|
|
45
|
+
this.#onEvent(state, eventType, filename);
|
|
46
|
+
}, { recursive: state.target.kind === "dir-recursive" }));
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
this.#reportError(error, state.target.path);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
close() {
|
|
54
|
+
if (this.#closed) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
this.#closed = true;
|
|
58
|
+
if (this.#timer) {
|
|
59
|
+
this.#clock.clearTimeout(this.#timer);
|
|
60
|
+
this.#timer = undefined;
|
|
61
|
+
}
|
|
62
|
+
for (const unsubscribe of this.#unsubscribes.splice(0)) {
|
|
63
|
+
try {
|
|
64
|
+
unsubscribe();
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
this.#reportError(error, "watch subscription");
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
getBaselineSnapshot() {
|
|
72
|
+
const snapshot = new Map();
|
|
73
|
+
for (const state of this.#states) {
|
|
74
|
+
for (const [path, hash] of state.hashes) {
|
|
75
|
+
snapshot.set(path, hash);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return snapshot;
|
|
79
|
+
}
|
|
80
|
+
#onEvent(state, _eventType, filename) {
|
|
81
|
+
if (this.#closed) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (filename === null) {
|
|
85
|
+
this.#pending.set(state, null);
|
|
86
|
+
}
|
|
87
|
+
else if (this.#pending.get(state) !== null) {
|
|
88
|
+
const affected = this.#pending.get(state) ?? new Set();
|
|
89
|
+
const relPath = normalizeRelativePath(filename);
|
|
90
|
+
if (relPath && this.#matches(state.target, relPath)) {
|
|
91
|
+
affected.add(relPath);
|
|
92
|
+
this.#pending.set(state, affected);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
this.#schedule();
|
|
96
|
+
}
|
|
97
|
+
#schedule() {
|
|
98
|
+
if (this.#timer) {
|
|
99
|
+
this.#clock.clearTimeout(this.#timer);
|
|
100
|
+
}
|
|
101
|
+
this.#timer = this.#clock.setTimeout(() => {
|
|
102
|
+
this.#timer = undefined;
|
|
103
|
+
this.#evaluatePending();
|
|
104
|
+
}, this.#debounceMs);
|
|
105
|
+
}
|
|
106
|
+
#evaluatePending() {
|
|
107
|
+
if (this.#closed) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const changes = new Map();
|
|
111
|
+
for (const [state, affected] of this.#pending) {
|
|
112
|
+
this.#evaluateState(state, affected, changes);
|
|
113
|
+
}
|
|
114
|
+
this.#pending.clear();
|
|
115
|
+
if (changes.size > 0) {
|
|
116
|
+
const changedPaths = [...changes.keys()].sort();
|
|
117
|
+
this.#onRealChange({
|
|
118
|
+
changedPaths,
|
|
119
|
+
created: changedPaths.filter((path) => changes.get(path)?.created),
|
|
120
|
+
deleted: changedPaths.filter((path) => changes.get(path)?.deleted),
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
#evaluateState(state, affected, changes) {
|
|
125
|
+
const previousHashes = new Map(state.hashes);
|
|
126
|
+
const previousDirectories = new Set(state.allowedDirectories);
|
|
127
|
+
const next = affected === null ? this.#scan(state.target) : this.#scanAffected(state.target, affected, state);
|
|
128
|
+
if (affected === null) {
|
|
129
|
+
state.hashes.clear();
|
|
130
|
+
for (const [path, hash] of next.hashes) {
|
|
131
|
+
state.hashes.set(path, hash);
|
|
132
|
+
}
|
|
133
|
+
state.allowedDirectories.clear();
|
|
134
|
+
for (const path of next.allowedDirectories) {
|
|
135
|
+
state.allowedDirectories.add(path);
|
|
136
|
+
}
|
|
137
|
+
this.#compare(previousHashes, previousDirectories, state.hashes, state.allowedDirectories, changes);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const prefixes = [...affected].map((relPath) => join(state.target.path, relPath));
|
|
141
|
+
for (const path of [...state.hashes.keys()]) {
|
|
142
|
+
if (prefixes.some((prefix) => isWithin(path, prefix))) {
|
|
143
|
+
state.hashes.delete(path);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
for (const path of [...state.allowedDirectories]) {
|
|
147
|
+
if (prefixes.some((prefix) => isWithin(path, prefix))) {
|
|
148
|
+
state.allowedDirectories.delete(path);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
for (const [path, hash] of next.hashes) {
|
|
152
|
+
state.hashes.set(path, hash);
|
|
153
|
+
}
|
|
154
|
+
for (const path of next.allowedDirectories) {
|
|
155
|
+
state.allowedDirectories.add(path);
|
|
156
|
+
}
|
|
157
|
+
this.#compare(previousHashes, previousDirectories, state.hashes, state.allowedDirectories, changes);
|
|
158
|
+
}
|
|
159
|
+
#scanAffected(target, affected, state) {
|
|
160
|
+
const result = { hashes: new Map(), allowedDirectories: new Set() };
|
|
161
|
+
for (const relPath of affected) {
|
|
162
|
+
const absolutePath = join(target.path, relPath);
|
|
163
|
+
this.#scanPath(target, absolutePath, relPath, result, state);
|
|
164
|
+
}
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
#scan(target) {
|
|
168
|
+
const result = { hashes: new Map(), allowedDirectories: new Set() };
|
|
169
|
+
this.#scanPath(target, target.path, "", result);
|
|
170
|
+
return result;
|
|
171
|
+
}
|
|
172
|
+
#scanPath(target, absolutePath, relPath, result, state) {
|
|
173
|
+
if (relPath && !this.#matches(target, relPath)) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
let entry;
|
|
177
|
+
try {
|
|
178
|
+
entry = lstatSync(absolutePath);
|
|
179
|
+
}
|
|
180
|
+
catch (error) {
|
|
181
|
+
if (state && !isMissing(error)) {
|
|
182
|
+
this.#reportError(error, absolutePath);
|
|
183
|
+
}
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
if (entry.isSymbolicLink()) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
if (entry.isFile()) {
|
|
190
|
+
try {
|
|
191
|
+
result.hashes.set(absolutePath, this.#hashFile(absolutePath));
|
|
192
|
+
}
|
|
193
|
+
catch (error) {
|
|
194
|
+
this.#reportError(error, absolutePath);
|
|
195
|
+
}
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
if (!entry.isDirectory()) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
const dotDirectory = relPath !== "" && basename(relPath).startsWith(".");
|
|
202
|
+
const explicitlyAllowedDirectory = relPath !== "" && this.#isExplicitlyAllowedDirectory(target, relPath);
|
|
203
|
+
if (explicitlyAllowedDirectory) {
|
|
204
|
+
result.allowedDirectories.add(absolutePath);
|
|
205
|
+
}
|
|
206
|
+
if (relPath && target.kind === "dir") {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
if (dotDirectory && !explicitlyAllowedDirectory) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
try {
|
|
213
|
+
for (const child of readdirSync(absolutePath, { withFileTypes: true })) {
|
|
214
|
+
if (child.isSymbolicLink() || child.name === "node_modules" || child.name === ".git") {
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
const childRelPath = relPath ? join(relPath, child.name) : child.name;
|
|
218
|
+
if (child.isDirectory() &&
|
|
219
|
+
child.name.startsWith(".") &&
|
|
220
|
+
!this.#isExplicitlyAllowedDirectory(target, childRelPath)) {
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
this.#scanPath(target, join(absolutePath, child.name), childRelPath, result, state);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
catch (error) {
|
|
227
|
+
this.#reportError(error, absolutePath);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
#compare(previousHashes, previousDirectories, nextHashes, nextDirectories, changes) {
|
|
231
|
+
for (const [path, hash] of nextHashes) {
|
|
232
|
+
const oldHash = previousHashes.get(path);
|
|
233
|
+
if (oldHash !== hash) {
|
|
234
|
+
changes.set(path, { created: oldHash === undefined, deleted: false });
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
for (const path of previousHashes.keys()) {
|
|
238
|
+
if (!nextHashes.has(path)) {
|
|
239
|
+
changes.set(path, { created: false, deleted: true });
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
for (const path of nextDirectories) {
|
|
243
|
+
if (!previousDirectories.has(path)) {
|
|
244
|
+
changes.set(path, { created: true, deleted: false });
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
for (const path of previousDirectories) {
|
|
248
|
+
if (!nextDirectories.has(path)) {
|
|
249
|
+
changes.set(path, { created: false, deleted: true });
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
#matches(target, relPath) {
|
|
254
|
+
if (!target.kind.includes("recursive") && relPath.includes(sep)) {
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
if (target.allowList &&
|
|
258
|
+
!target.allowList.some((allowed) => relPath === allowed || relPath.startsWith(`${allowed}${sep}`))) {
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
261
|
+
return target.filter?.(relPath) ?? true;
|
|
262
|
+
}
|
|
263
|
+
#isExplicitlyAllowedDirectory(target, relPath) {
|
|
264
|
+
return (target.allowList?.some((allowed) => relPath === allowed || relPath.startsWith(`${allowed}${sep}`)) ?? false);
|
|
265
|
+
}
|
|
266
|
+
#reportError(error, path) {
|
|
267
|
+
try {
|
|
268
|
+
this.#onError(error, path);
|
|
269
|
+
}
|
|
270
|
+
catch {
|
|
271
|
+
// Error reporting must not take down the watcher.
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
/** Production event source. Tests inject a deterministic source instead. */
|
|
276
|
+
export function createFsWatchEventSource(onError = () => { }) {
|
|
277
|
+
return (path, listener, options) => {
|
|
278
|
+
const watcher = watchWithErrorHandler(path, listener, () => onError(new Error(`fs.watch failed for ${path}`), path), { recursive: options?.recursive ?? false });
|
|
279
|
+
return () => watcher?.close();
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
function hashFile(path) {
|
|
283
|
+
return createHash("sha256").update(readFileSync(path)).digest("hex");
|
|
284
|
+
}
|
|
285
|
+
function normalizeRelativePath(filename) {
|
|
286
|
+
if (isAbsolute(filename)) {
|
|
287
|
+
return undefined;
|
|
288
|
+
}
|
|
289
|
+
const normalized = normalize(filename);
|
|
290
|
+
return normalized === "." || normalized === ".." || normalized.startsWith(`..${sep}`) ? undefined : normalized;
|
|
291
|
+
}
|
|
292
|
+
function isWithin(path, prefix) {
|
|
293
|
+
return path === prefix || path.startsWith(`${prefix}${sep}`);
|
|
294
|
+
}
|
|
295
|
+
function isMissing(error) {
|
|
296
|
+
return typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
|
|
297
|
+
}
|
|
298
|
+
//# sourceMappingURL=watch-engine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"watch-engine.js","sourceRoot":"","sources":["../../../../../src/core/extensions/builtin/config-reload/watch-engine.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,YAAY,EAAc,MAAM,SAAS,CAAC;AAC3E,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAChF,OAAO,EAAE,qBAAqB,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, type Stats } from \"node:fs\";\nimport { basename, isAbsolute, join, normalize, resolve, sep } from \"node:path\";\nimport { watchWithErrorHandler } from \"../../../../utils/fs-watch.ts\";\n\nexport type WatchTarget = {\n\treadonly id: string;\n\treadonly kind: \"dir\" | \"dir-recursive\";\n\treadonly path: string;\n\treadonly allowList?: readonly string[];\n\treadonly filter?: (relPath: string) => boolean;\n};\n\nexport type WatchEventListener = (eventType: string, filename: string | null) => void;\n\nexport type WatchEventSource = (\n\tpath: string,\n\tlistener: WatchEventListener,\n\toptions?: { readonly recursive: boolean },\n) => () => void;\n\nexport type WatchClock = {\n\tsetTimeout(callback: () => void, delayMs: number): ReturnType<typeof setTimeout>;\n\tclearTimeout(timer: ReturnType<typeof setTimeout>): void;\n};\n\nexport type RealChange = {\n\treadonly changedPaths: readonly string[];\n\treadonly created: readonly string[];\n\treadonly deleted: readonly string[];\n};\n\nexport type ConfigReloadWatchEngineOptions = {\n\treadonly targets: readonly WatchTarget[];\n\treadonly subscribe: WatchEventSource;\n\treadonly onRealChange: (change: RealChange) => void;\n\treadonly onError?: (error: unknown, path: string) => void;\n\treadonly debounceMs?: number;\n\treadonly clock?: WatchClock;\n\t/** Test seam. Production callers use SHA-256 of file bytes. */\n\treadonly hashFile?: (path: string) => string;\n};\n\ntype TargetState = {\n\treadonly target: ResolvedWatchTarget;\n\treadonly hashes: Map<string, string>;\n\treadonly allowedDirectories: Set<string>;\n};\n\ntype ResolvedWatchTarget = Omit<WatchTarget, \"path\"> & { readonly path: string };\n\ntype ScanResult = {\n\treadonly hashes: Map<string, string>;\n\treadonly allowedDirectories: Set<string>;\n};\n\nconst DEFAULT_DEBOUNCE_MS = 200;\nconst DEFAULT_CLOCK: WatchClock = {\n\tsetTimeout: (callback, delayMs) => setTimeout(callback, delayMs),\n\tclearTimeout: (timer) => clearTimeout(timer),\n};\n\n/**\n * Hash-gates filesystem events. Targets are directories by design: watching a\n * file directly breaks when editors atomically rename a replacement over it.\n */\nexport class ConfigReloadWatchEngine {\n\treadonly #states: TargetState[];\n\treadonly #unsubscribes: (() => void)[] = [];\n\treadonly #subscribe: WatchEventSource;\n\treadonly #onRealChange: (change: RealChange) => void;\n\treadonly #onError: (error: unknown, path: string) => void;\n\treadonly #hashFile: (path: string) => string;\n\treadonly #clock: WatchClock;\n\treadonly #debounceMs: number;\n\treadonly #pending = new Map<TargetState, Set<string> | null>();\n\t#timer: ReturnType<typeof setTimeout> | undefined;\n\t#closed = false;\n\n\tconstructor(options: ConfigReloadWatchEngineOptions) {\n\t\tthis.#subscribe = options.subscribe;\n\t\tthis.#onRealChange = options.onRealChange;\n\t\tthis.#onError = options.onError ?? (() => {});\n\t\tthis.#hashFile = options.hashFile ?? hashFile;\n\t\tthis.#clock = options.clock ?? DEFAULT_CLOCK;\n\t\tthis.#debounceMs = options.debounceMs ?? DEFAULT_DEBOUNCE_MS;\n\t\tthis.#states = options.targets.map((target) => {\n\t\t\tconst resolvedTarget = { ...target, path: resolve(target.path) };\n\t\t\tconst scan = this.#scan(resolvedTarget);\n\t\t\treturn {\n\t\t\t\ttarget: resolvedTarget,\n\t\t\t\thashes: scan.hashes,\n\t\t\t\tallowedDirectories: scan.allowedDirectories,\n\t\t\t};\n\t\t});\n\n\t\tfor (const state of this.#states) {\n\t\t\ttry {\n\t\t\t\tthis.#unsubscribes.push(\n\t\t\t\t\tthis.#subscribe(\n\t\t\t\t\t\tstate.target.path,\n\t\t\t\t\t\t(eventType, filename) => {\n\t\t\t\t\t\t\tthis.#onEvent(state, eventType, filename);\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{ recursive: state.target.kind === \"dir-recursive\" },\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t} catch (error) {\n\t\t\t\tthis.#reportError(error, state.target.path);\n\t\t\t}\n\t\t}\n\t}\n\n\tclose(): void {\n\t\tif (this.#closed) {\n\t\t\treturn;\n\t\t}\n\t\tthis.#closed = true;\n\t\tif (this.#timer) {\n\t\t\tthis.#clock.clearTimeout(this.#timer);\n\t\t\tthis.#timer = undefined;\n\t\t}\n\t\tfor (const unsubscribe of this.#unsubscribes.splice(0)) {\n\t\t\ttry {\n\t\t\t\tunsubscribe();\n\t\t\t} catch (error) {\n\t\t\t\tthis.#reportError(error, \"watch subscription\");\n\t\t\t}\n\t\t}\n\t}\n\n\tgetBaselineSnapshot(): ReadonlyMap<string, string> {\n\t\tconst snapshot = new Map<string, string>();\n\t\tfor (const state of this.#states) {\n\t\t\tfor (const [path, hash] of state.hashes) {\n\t\t\t\tsnapshot.set(path, hash);\n\t\t\t}\n\t\t}\n\t\treturn snapshot;\n\t}\n\n\t#onEvent(state: TargetState, _eventType: string, filename: string | null): void {\n\t\tif (this.#closed) {\n\t\t\treturn;\n\t\t}\n\t\tif (filename === null) {\n\t\t\tthis.#pending.set(state, null);\n\t\t} else if (this.#pending.get(state) !== null) {\n\t\t\tconst affected = this.#pending.get(state) ?? new Set<string>();\n\t\t\tconst relPath = normalizeRelativePath(filename);\n\t\t\tif (relPath && this.#matches(state.target, relPath)) {\n\t\t\t\taffected.add(relPath);\n\t\t\t\tthis.#pending.set(state, affected);\n\t\t\t}\n\t\t}\n\t\tthis.#schedule();\n\t}\n\n\t#schedule(): void {\n\t\tif (this.#timer) {\n\t\t\tthis.#clock.clearTimeout(this.#timer);\n\t\t}\n\t\tthis.#timer = this.#clock.setTimeout(() => {\n\t\t\tthis.#timer = undefined;\n\t\t\tthis.#evaluatePending();\n\t\t}, this.#debounceMs);\n\t}\n\n\t#evaluatePending(): void {\n\t\tif (this.#closed) {\n\t\t\treturn;\n\t\t}\n\t\tconst changes = new Map<string, { created: boolean; deleted: boolean }>();\n\t\tfor (const [state, affected] of this.#pending) {\n\t\t\tthis.#evaluateState(state, affected, changes);\n\t\t}\n\t\tthis.#pending.clear();\n\t\tif (changes.size > 0) {\n\t\t\tconst changedPaths = [...changes.keys()].sort();\n\t\t\tthis.#onRealChange({\n\t\t\t\tchangedPaths,\n\t\t\t\tcreated: changedPaths.filter((path) => changes.get(path)?.created),\n\t\t\t\tdeleted: changedPaths.filter((path) => changes.get(path)?.deleted),\n\t\t\t});\n\t\t}\n\t}\n\n\t#evaluateState(\n\t\tstate: TargetState,\n\t\taffected: Set<string> | null,\n\t\tchanges: Map<string, { created: boolean; deleted: boolean }>,\n\t): void {\n\t\tconst previousHashes = new Map(state.hashes);\n\t\tconst previousDirectories = new Set(state.allowedDirectories);\n\t\tconst next = affected === null ? this.#scan(state.target) : this.#scanAffected(state.target, affected, state);\n\n\t\tif (affected === null) {\n\t\t\tstate.hashes.clear();\n\t\t\tfor (const [path, hash] of next.hashes) {\n\t\t\t\tstate.hashes.set(path, hash);\n\t\t\t}\n\t\t\tstate.allowedDirectories.clear();\n\t\t\tfor (const path of next.allowedDirectories) {\n\t\t\t\tstate.allowedDirectories.add(path);\n\t\t\t}\n\t\t\tthis.#compare(previousHashes, previousDirectories, state.hashes, state.allowedDirectories, changes);\n\t\t\treturn;\n\t\t}\n\n\t\tconst prefixes = [...affected].map((relPath) => join(state.target.path, relPath));\n\t\tfor (const path of [...state.hashes.keys()]) {\n\t\t\tif (prefixes.some((prefix) => isWithin(path, prefix))) {\n\t\t\t\tstate.hashes.delete(path);\n\t\t\t}\n\t\t}\n\t\tfor (const path of [...state.allowedDirectories]) {\n\t\t\tif (prefixes.some((prefix) => isWithin(path, prefix))) {\n\t\t\t\tstate.allowedDirectories.delete(path);\n\t\t\t}\n\t\t}\n\t\tfor (const [path, hash] of next.hashes) {\n\t\t\tstate.hashes.set(path, hash);\n\t\t}\n\t\tfor (const path of next.allowedDirectories) {\n\t\t\tstate.allowedDirectories.add(path);\n\t\t}\n\t\tthis.#compare(previousHashes, previousDirectories, state.hashes, state.allowedDirectories, changes);\n\t}\n\n\t#scanAffected(target: ResolvedWatchTarget, affected: Set<string>, state: TargetState): ScanResult {\n\t\tconst result: ScanResult = { hashes: new Map(), allowedDirectories: new Set() };\n\t\tfor (const relPath of affected) {\n\t\t\tconst absolutePath = join(target.path, relPath);\n\t\t\tthis.#scanPath(target, absolutePath, relPath, result, state);\n\t\t}\n\t\treturn result;\n\t}\n\n\t#scan(target: ResolvedWatchTarget): ScanResult {\n\t\tconst result: ScanResult = { hashes: new Map(), allowedDirectories: new Set() };\n\t\tthis.#scanPath(target, target.path, \"\", result);\n\t\treturn result;\n\t}\n\n\t#scanPath(\n\t\ttarget: ResolvedWatchTarget,\n\t\tabsolutePath: string,\n\t\trelPath: string,\n\t\tresult: ScanResult,\n\t\tstate?: TargetState,\n\t): void {\n\t\tif (relPath && !this.#matches(target, relPath)) {\n\t\t\treturn;\n\t\t}\n\t\tlet entry: Stats;\n\t\ttry {\n\t\t\tentry = lstatSync(absolutePath);\n\t\t} catch (error) {\n\t\t\tif (state && !isMissing(error)) {\n\t\t\t\tthis.#reportError(error, absolutePath);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t\tif (entry.isSymbolicLink()) {\n\t\t\treturn;\n\t\t}\n\t\tif (entry.isFile()) {\n\t\t\ttry {\n\t\t\t\tresult.hashes.set(absolutePath, this.#hashFile(absolutePath));\n\t\t\t} catch (error) {\n\t\t\t\tthis.#reportError(error, absolutePath);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t\tif (!entry.isDirectory()) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst dotDirectory = relPath !== \"\" && basename(relPath).startsWith(\".\");\n\t\tconst explicitlyAllowedDirectory = relPath !== \"\" && this.#isExplicitlyAllowedDirectory(target, relPath);\n\t\tif (explicitlyAllowedDirectory) {\n\t\t\tresult.allowedDirectories.add(absolutePath);\n\t\t}\n\t\tif (relPath && target.kind === \"dir\") {\n\t\t\treturn;\n\t\t}\n\t\tif (dotDirectory && !explicitlyAllowedDirectory) {\n\t\t\treturn;\n\t\t}\n\t\ttry {\n\t\t\tfor (const child of readdirSync(absolutePath, { withFileTypes: true })) {\n\t\t\t\tif (child.isSymbolicLink() || child.name === \"node_modules\" || child.name === \".git\") {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tconst childRelPath = relPath ? join(relPath, child.name) : child.name;\n\t\t\t\tif (\n\t\t\t\t\tchild.isDirectory() &&\n\t\t\t\t\tchild.name.startsWith(\".\") &&\n\t\t\t\t\t!this.#isExplicitlyAllowedDirectory(target, childRelPath)\n\t\t\t\t) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tthis.#scanPath(target, join(absolutePath, child.name), childRelPath, result, state);\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tthis.#reportError(error, absolutePath);\n\t\t}\n\t}\n\n\t#compare(\n\t\tpreviousHashes: ReadonlyMap<string, string>,\n\t\tpreviousDirectories: ReadonlySet<string>,\n\t\tnextHashes: ReadonlyMap<string, string>,\n\t\tnextDirectories: ReadonlySet<string>,\n\t\tchanges: Map<string, { created: boolean; deleted: boolean }>,\n\t): void {\n\t\tfor (const [path, hash] of nextHashes) {\n\t\t\tconst oldHash = previousHashes.get(path);\n\t\t\tif (oldHash !== hash) {\n\t\t\t\tchanges.set(path, { created: oldHash === undefined, deleted: false });\n\t\t\t}\n\t\t}\n\t\tfor (const path of previousHashes.keys()) {\n\t\t\tif (!nextHashes.has(path)) {\n\t\t\t\tchanges.set(path, { created: false, deleted: true });\n\t\t\t}\n\t\t}\n\t\tfor (const path of nextDirectories) {\n\t\t\tif (!previousDirectories.has(path)) {\n\t\t\t\tchanges.set(path, { created: true, deleted: false });\n\t\t\t}\n\t\t}\n\t\tfor (const path of previousDirectories) {\n\t\t\tif (!nextDirectories.has(path)) {\n\t\t\t\tchanges.set(path, { created: false, deleted: true });\n\t\t\t}\n\t\t}\n\t}\n\n\t#matches(target: ResolvedWatchTarget, relPath: string): boolean {\n\t\tif (!target.kind.includes(\"recursive\") && relPath.includes(sep)) {\n\t\t\treturn false;\n\t\t}\n\t\tif (\n\t\t\ttarget.allowList &&\n\t\t\t!target.allowList.some((allowed) => relPath === allowed || relPath.startsWith(`${allowed}${sep}`))\n\t\t) {\n\t\t\treturn false;\n\t\t}\n\t\treturn target.filter?.(relPath) ?? true;\n\t}\n\n\t#isExplicitlyAllowedDirectory(target: ResolvedWatchTarget, relPath: string): boolean {\n\t\treturn (\n\t\t\ttarget.allowList?.some((allowed) => relPath === allowed || relPath.startsWith(`${allowed}${sep}`)) ?? false\n\t\t);\n\t}\n\n\t#reportError(error: unknown, path: string): void {\n\t\ttry {\n\t\t\tthis.#onError(error, path);\n\t\t} catch {\n\t\t\t// Error reporting must not take down the watcher.\n\t\t}\n\t}\n}\n\n/** Production event source. Tests inject a deterministic source instead. */\nexport function createFsWatchEventSource(onError: (error: unknown, path: string) => void = () => {}): WatchEventSource {\n\treturn (path, listener, options) => {\n\t\tconst watcher = watchWithErrorHandler(\n\t\t\tpath,\n\t\t\tlistener,\n\t\t\t() => onError(new Error(`fs.watch failed for ${path}`), path),\n\t\t\t{ recursive: options?.recursive ?? false },\n\t\t);\n\t\treturn () => watcher?.close();\n\t};\n}\n\nfunction hashFile(path: string): string {\n\treturn createHash(\"sha256\").update(readFileSync(path)).digest(\"hex\");\n}\n\nfunction normalizeRelativePath(filename: string): string | undefined {\n\tif (isAbsolute(filename)) {\n\t\treturn undefined;\n\t}\n\tconst normalized = normalize(filename);\n\treturn normalized === \".\" || normalized === \"..\" || normalized.startsWith(`..${sep}`) ? undefined : normalized;\n}\n\nfunction isWithin(path: string, prefix: string): boolean {\n\treturn path === prefix || path.startsWith(`${prefix}${sep}`);\n}\n\nfunction isMissing(error: unknown): boolean {\n\treturn typeof error === \"object\" && error !== null && \"code\" in error && error.code === \"ENOENT\";\n}\n"]}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { ExtensionAPI, ExtensionContext } from "../../types.ts";
|
|
2
|
-
import type { Goal, GoalAccountingMode, GoalStoreRef
|
|
2
|
+
import type { Goal, GoalAccountingMode, GoalStoreRef } from "./types.ts";
|
|
3
3
|
export type GoalCommandRegistrationDeps = {
|
|
4
4
|
readonly goalStoreRef: (ctx: ExtensionContext) => GoalStoreRef;
|
|
5
|
-
readonly accountCurrentAgentTurn: (ctx: ExtensionContext,
|
|
5
|
+
readonly accountCurrentAgentTurn: (ctx: ExtensionContext, mode: GoalAccountingMode) => Promise<Goal | null>;
|
|
6
6
|
readonly beginAgentGoalAccounting: (goal: Goal) => void;
|
|
7
7
|
readonly stopAgentGoalAccounting: (goalId: string) => void;
|
|
8
8
|
readonly clearAgentGoalAccounting: () => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"command-registration.d.ts","sourceRoot":"","sources":["../../../../../src/core/extensions/builtin/goal/command-registration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAIrE,OAAO,KAAK,EAAE,IAAI,EAAE,kBAAkB,EAAE,YAAY,EAAE,
|
|
1
|
+
{"version":3,"file":"command-registration.d.ts","sourceRoot":"","sources":["../../../../../src/core/extensions/builtin/goal/command-registration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAIrE,OAAO,KAAK,EAAE,IAAI,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAOzE,MAAM,MAAM,2BAA2B,GAAG;IACzC,QAAQ,CAAC,YAAY,EAAE,CAAC,GAAG,EAAE,gBAAgB,KAAK,YAAY,CAAC;IAC/D,QAAQ,CAAC,uBAAuB,EAAE,CAAC,GAAG,EAAE,gBAAgB,EAAE,IAAI,EAAE,kBAAkB,KAAK,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,CAAC;IAC5G,QAAQ,CAAC,wBAAwB,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,CAAC;IACxD,QAAQ,CAAC,uBAAuB,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;IAC3D,QAAQ,CAAC,wBAAwB,EAAE,MAAM,IAAI,CAAC;IAC9C,QAAQ,CAAC,qBAAqB,EAAE,CAAC,EAAE,EAAE,YAAY,EAAE,GAAG,EAAE,gBAAgB,EAAE,IAAI,EAAE,IAAI,KAAK,IAAI,CAAC;IAC9F,QAAQ,CAAC,aAAa,EAAE,CAAC,GAAG,EAAE,gBAAgB,EAAE,IAAI,EAAE,IAAI,GAAG,IAAI,KAAK,IAAI,CAAC;CAC3E,CAAC;AAEF,wBAAgB,mBAAmB,CAAC,EAAE,EAAE,YAAY,EAAE,IAAI,EAAE,2BAA2B,GAAG,IAAI,CAoD7F"}
|
|
@@ -5,7 +5,6 @@ const GOAL_USAGE = "Usage: /goal <objective>";
|
|
|
5
5
|
const GOAL_EMPTY_HINT = "No goal is currently set.";
|
|
6
6
|
const REPLACE_GOAL_CHOICE = "Replace current goal";
|
|
7
7
|
const CANCEL_REPLACE_GOAL_CHOICE = "Cancel";
|
|
8
|
-
const EMPTY_USAGE = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0 };
|
|
9
8
|
export function registerGoalCommand(pi, deps) {
|
|
10
9
|
pi.registerCommand("goal", {
|
|
11
10
|
description: "Set, inspect, pause, resume, or clear the persistent goal",
|
|
@@ -25,7 +24,7 @@ export function registerGoalCommand(pi, deps) {
|
|
|
25
24
|
}
|
|
26
25
|
case "setStatus": {
|
|
27
26
|
if (command.status === "paused") {
|
|
28
|
-
await deps.accountCurrentAgentTurn(ctx,
|
|
27
|
+
await deps.accountCurrentAgentTurn(ctx, "active");
|
|
29
28
|
}
|
|
30
29
|
const goal = await updateGoal(deps.goalStoreRef(ctx), { status: command.status });
|
|
31
30
|
if (goal.status === "active") {
|
|
@@ -40,7 +39,7 @@ export function registerGoalCommand(pi, deps) {
|
|
|
40
39
|
return;
|
|
41
40
|
}
|
|
42
41
|
case "clear": {
|
|
43
|
-
await deps.accountCurrentAgentTurn(ctx,
|
|
42
|
+
await deps.accountCurrentAgentTurn(ctx, "active");
|
|
44
43
|
const cleared = await clearGoal(deps.goalStoreRef(ctx));
|
|
45
44
|
deps.clearAgentGoalAccounting();
|
|
46
45
|
deps.refreshGoalUi(ctx, null);
|
|
@@ -64,7 +63,7 @@ async function setGoalObjective(pi, ctx, objective, deps) {
|
|
|
64
63
|
return;
|
|
65
64
|
}
|
|
66
65
|
if (current?.status === "active") {
|
|
67
|
-
await deps.accountCurrentAgentTurn(ctx,
|
|
66
|
+
await deps.accountCurrentAgentTurn(ctx, "active");
|
|
68
67
|
}
|
|
69
68
|
const goal = current === null ? await createGoal(ref, objective) : await updateGoal(ref, { objective });
|
|
70
69
|
if (goal.status === "active")
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"command-registration.js","sourceRoot":"","sources":["../../../../../src/core/extensions/builtin/goal/command-registration.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACjE,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAGzE,MAAM,UAAU,GAAG,0BAA0B,CAAC;AAC9C,MAAM,eAAe,GAAG,2BAA2B,CAAC;AACpD,MAAM,mBAAmB,GAAG,sBAAsB,CAAC;AACnD,MAAM,0BAA0B,GAAG,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"command-registration.js","sourceRoot":"","sources":["../../../../../src/core/extensions/builtin/goal/command-registration.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACjE,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAGzE,MAAM,UAAU,GAAG,0BAA0B,CAAC;AAC9C,MAAM,eAAe,GAAG,2BAA2B,CAAC;AACpD,MAAM,mBAAmB,GAAG,sBAAsB,CAAC;A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QAAE,OAAO,IAAI,CAAC;IAC5B,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,EAAE,CAAC,MAAM,CAAC,iCAAiC,SAAS,EAAE,EAAE;QAChF,mBAAmB;QACnB,0BAA0B;KAC1B,CAAC,CAAC;IACH,OAAO,MAAM,KAAK,mBAAmB,CAAC;AACvC,CAAC","sourcesContent":["import type { ExtensionAPI, ExtensionContext } from \"../../types.ts\";\nimport { parseGoalCommand } from \"./command.ts\";\nimport { formatGoalForTool, goalStatusLabel } from \"./format.ts\";\nimport { clearGoal, createGoal, readGoal, updateGoal } from \"./store.ts\";\nimport type { Goal, GoalAccountingMode, GoalStoreRef } from \"./types.ts\";\n\nconst GOAL_USAGE = \"Usage: /goal <objective>\";\nconst GOAL_EMPTY_HINT = \"No goal is currently set.\";\nconst REPLACE_GOAL_CHOICE = \"Replace current goal\";\nconst CANCEL_REPLACE_GOAL_CHOICE = \"Cancel\";\n\nexport type GoalCommandRegistrationDeps = {\n\treadonly goalStoreRef: (ctx: ExtensionContext) => GoalStoreRef;\n\treadonly accountCurrentAgentTurn: (ctx: ExtensionContext, mode: GoalAccountingMode) => Promise<Goal | null>;\n\treadonly beginAgentGoalAccounting: (goal: Goal) => void;\n\treadonly stopAgentGoalAccounting: (goalId: string) => void;\n\treadonly clearAgentGoalAccounting: () => void;\n\treadonly queueGoalContinuation: (pi: ExtensionAPI, ctx: ExtensionContext, goal: Goal) => void;\n\treadonly refreshGoalUi: (ctx: ExtensionContext, goal: Goal | null) => void;\n};\n\nexport function registerGoalCommand(pi: ExtensionAPI, deps: GoalCommandRegistrationDeps): void {\n\tpi.registerCommand(\"goal\", {\n\t\tdescription: \"Set, inspect, pause, resume, or clear the persistent goal\",\n\t\thandler: async (rawArgs, ctx) => {\n\t\t\tconst command = parseGoalCommand(rawArgs);\n\t\t\ttry {\n\t\t\t\tswitch (command.kind) {\n\t\t\t\t\tcase \"show\": {\n\t\t\t\t\t\tconst goal = await readGoal(deps.goalStoreRef(ctx));\n\t\t\t\t\t\tdeps.refreshGoalUi(ctx, goal);\n\t\t\t\t\t\tctx.ui.notify(\n\t\t\t\t\t\t\tgoal === null ? `${GOAL_USAGE}\\n${GOAL_EMPTY_HINT}` : formatGoalForTool(goal),\n\t\t\t\t\t\t\tgoal ? \"info\" : \"warning\",\n\t\t\t\t\t\t);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\tcase \"setObjective\": {\n\t\t\t\t\t\tawait setGoalObjective(pi, ctx, command.objective, deps);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\tcase \"setStatus\": {\n\t\t\t\t\t\tif (command.status === \"paused\") {\n\t\t\t\t\t\t\tawait deps.accountCurrentAgentTurn(ctx, \"active\");\n\t\t\t\t\t\t}\n\t\t\t\t\t\tconst goal = await updateGoal(deps.goalStoreRef(ctx), { status: command.status });\n\t\t\t\t\t\tif (goal.status === \"active\") {\n\t\t\t\t\t\t\tdeps.beginAgentGoalAccounting(goal);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tdeps.stopAgentGoalAccounting(goal.id);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tdeps.refreshGoalUi(ctx, goal);\n\t\t\t\t\t\tctx.ui.notify(`Goal ${goalStatusLabel(goal.status)}\\n${formatGoalForTool(goal)}`, \"info\");\n\t\t\t\t\t\tdeps.queueGoalContinuation(pi, ctx, goal);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\tcase \"clear\": {\n\t\t\t\t\t\tawait deps.accountCurrentAgentTurn(ctx, \"active\");\n\t\t\t\t\t\tconst cleared = await clearGoal(deps.goalStoreRef(ctx));\n\t\t\t\t\t\tdeps.clearAgentGoalAccounting();\n\t\t\t\t\t\tdeps.refreshGoalUi(ctx, null);\n\t\t\t\t\t\tctx.ui.notify(\n\t\t\t\t\t\t\tcleared ? \"Goal cleared\" : \"No goal to clear\\nThis thread does not currently have a goal.\",\n\t\t\t\t\t\t\tcleared ? \"info\" : \"warning\",\n\t\t\t\t\t\t);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tctx.ui.notify(error instanceof Error ? error.message : String(error), \"error\");\n\t\t\t}\n\t\t},\n\t});\n}\n\nasync function setGoalObjective(\n\tpi: ExtensionAPI,\n\tctx: ExtensionContext,\n\tobjective: string,\n\tdeps: GoalCommandRegistrationDeps,\n): Promise<void> {\n\tconst ref = deps.goalStoreRef(ctx);\n\tconst current = await readGoal(ref);\n\tif (current !== null) {\n\t\tconst shouldReplace = await confirmReplaceGoal(ctx, objective);\n\t\tif (!shouldReplace) return;\n\t}\n\n\tif (current?.status === \"active\") {\n\t\tawait deps.accountCurrentAgentTurn(ctx, \"active\");\n\t}\n\tconst goal = current === null ? await createGoal(ref, objective) : await updateGoal(ref, { objective });\n\tif (goal.status === \"active\") deps.beginAgentGoalAccounting(goal);\n\tdeps.refreshGoalUi(ctx, goal);\n\tctx.ui.notify(`Goal ${goalStatusLabel(goal.status)}\\n${formatGoalForTool(goal)}`, \"info\");\n\tdeps.queueGoalContinuation(pi, ctx, goal);\n}\n\nasync function confirmReplaceGoal(ctx: ExtensionContext, objective: string): Promise<boolean> {\n\tif (!ctx.hasUI) return true;\n\tconst choice = await ctx.ui.select(`Replace goal?\\nNew objective: ${objective}`, [\n\t\tREPLACE_GOAL_CHOICE,\n\t\tCANCEL_REPLACE_GOAL_CHOICE,\n\t]);\n\treturn choice === REPLACE_GOAL_CHOICE;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/core/extensions/builtin/goal/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAoB,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/core/extensions/builtin/goal/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAoB,MAAM,gBAAgB,CAAC;AAsBrE,MAAM,CAAC,OAAO,UAAU,aAAa,CAAC,EAAE,EAAE,YAAY,GAAG,IAAI,CA2L5D"}
|
|
@@ -8,17 +8,17 @@ import { formatGoalForTool, goalStatusLabel } from "./format.js";
|
|
|
8
8
|
import { buildContinuationPrompt } from "./prompt.js";
|
|
9
9
|
import { accountGoalUsage, readGoal, updateGoal } from "./store.js";
|
|
10
10
|
import { registerGoalTools } from "./tool-registration.js";
|
|
11
|
-
import {
|
|
11
|
+
import { TurnUsageTracker } from "./turn-usage.js";
|
|
12
12
|
import { updateGoalUi } from "./ui.js";
|
|
13
13
|
const GOAL_CONTINUATION_MESSAGE_TYPE = "goal-continuation";
|
|
14
14
|
const RESUME_GOAL_CHOICE = "Resume goal";
|
|
15
15
|
const LEAVE_GOAL_PAUSED_CHOICE = "Leave paused";
|
|
16
|
-
const EMPTY_USAGE = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0 };
|
|
17
16
|
const STALE_EXTENSION_CONTEXT_ERROR_PREFIX = "This extension ctx is stale after session replacement or reload.";
|
|
18
17
|
export default function goalExtension(pi) {
|
|
19
18
|
let agentTurnInProgress = false;
|
|
20
19
|
let agentGoalAccounting = null;
|
|
21
20
|
let completedThisTurnGoalId = null;
|
|
21
|
+
const turnUsage = new TurnUsageTracker();
|
|
22
22
|
const goalTicker = new GoalElapsedTicker({
|
|
23
23
|
render: (renderCtx, renderGoal, live) => {
|
|
24
24
|
try {
|
|
@@ -66,6 +66,7 @@ export default function goalExtension(pi) {
|
|
|
66
66
|
pi.on("agent_start", async (_event, ctx) => {
|
|
67
67
|
agentTurnInProgress = true;
|
|
68
68
|
completedThisTurnGoalId = null;
|
|
69
|
+
turnUsage.reset();
|
|
69
70
|
const goal = await readGoal(goalStoreRef(ctx));
|
|
70
71
|
if (goal?.status === "active") {
|
|
71
72
|
beginAgentGoalAccounting(goal);
|
|
@@ -74,9 +75,12 @@ export default function goalExtension(pi) {
|
|
|
74
75
|
agentGoalAccounting = null;
|
|
75
76
|
}
|
|
76
77
|
});
|
|
78
|
+
pi.on("message_end", async (event) => {
|
|
79
|
+
turnUsage.noteMessageEnd(event.message);
|
|
80
|
+
});
|
|
77
81
|
pi.on("agent_end", async (event, ctx) => {
|
|
78
82
|
const mode = completedThisTurnGoalId === null ? "active" : "activeOrComplete";
|
|
79
|
-
const goal = await accountCurrentAgentTurn(ctx,
|
|
83
|
+
const goal = await accountCurrentAgentTurn(ctx, mode, event.messages);
|
|
80
84
|
agentTurnInProgress = false;
|
|
81
85
|
completedThisTurnGoalId = null;
|
|
82
86
|
if (goal?.status === "active") {
|
|
@@ -94,7 +98,7 @@ export default function goalExtension(pi) {
|
|
|
94
98
|
});
|
|
95
99
|
pi.on("session_shutdown", async (_event, ctx) => {
|
|
96
100
|
if (agentGoalAccounting !== null) {
|
|
97
|
-
await accountCurrentAgentTurn(ctx,
|
|
101
|
+
await accountCurrentAgentTurn(ctx, "active");
|
|
98
102
|
}
|
|
99
103
|
clearAgentGoalAccounting();
|
|
100
104
|
goalTicker.stop();
|
|
@@ -121,6 +125,7 @@ export default function goalExtension(pi) {
|
|
|
121
125
|
return;
|
|
122
126
|
if (agentGoalAccounting?.goalId === goal.id)
|
|
123
127
|
return;
|
|
128
|
+
turnUsage.discardPending();
|
|
124
129
|
agentGoalAccounting = { goalId: goal.id, measuredFromMilliseconds: Date.now() };
|
|
125
130
|
}
|
|
126
131
|
function markGoalCompletedThisTurn(goal) {
|
|
@@ -161,11 +166,12 @@ export default function goalExtension(pi) {
|
|
|
161
166
|
throw error;
|
|
162
167
|
}
|
|
163
168
|
}
|
|
164
|
-
async function accountCurrentAgentTurn(ctx,
|
|
169
|
+
async function accountCurrentAgentTurn(ctx, mode, agentRunMessages) {
|
|
165
170
|
const accounting = agentGoalAccounting;
|
|
166
171
|
const ref = goalStoreRef(ctx);
|
|
167
172
|
if (accounting === null)
|
|
168
173
|
return readGoal(ref);
|
|
174
|
+
const usage = agentRunMessages === undefined ? turnUsage.takePending() : turnUsage.takeRemaining(agentRunMessages);
|
|
169
175
|
const now = Date.now();
|
|
170
176
|
const elapsedSeconds = Math.max(0, Math.round((now - accounting.measuredFromMilliseconds) / 1000));
|
|
171
177
|
const goal = await accountGoalUsage(ref, usage, elapsedSeconds, mode, accounting.goalId);
|
|
@@ -206,26 +212,4 @@ function goalStoreRef(ctx) {
|
|
|
206
212
|
function cwdStoreKey(cwd) {
|
|
207
213
|
return createHash("sha256").update(cwd).digest("hex").slice(0, 24);
|
|
208
214
|
}
|
|
209
|
-
function collectAssistantUsage(messages) {
|
|
210
|
-
const usage = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0 };
|
|
211
|
-
for (const message of messages) {
|
|
212
|
-
if (!isAssistantUsageMessage(message))
|
|
213
|
-
continue;
|
|
214
|
-
usage.input += numericUsageField(message.usage, "input");
|
|
215
|
-
usage.output += numericUsageField(message.usage, "output");
|
|
216
|
-
usage.cacheRead += numericUsageField(message.usage, "cacheRead");
|
|
217
|
-
usage.cacheWrite += numericUsageField(message.usage, "cacheWrite");
|
|
218
|
-
usage.totalTokens += numericUsageField(message.usage, "totalTokens");
|
|
219
|
-
}
|
|
220
|
-
return usage;
|
|
221
|
-
}
|
|
222
|
-
function isAssistantUsageMessage(message) {
|
|
223
|
-
if (!isRecord(message))
|
|
224
|
-
return false;
|
|
225
|
-
return message.role === "assistant" && isRecord(message.usage);
|
|
226
|
-
}
|
|
227
|
-
function numericUsageField(usage, key) {
|
|
228
|
-
const value = usage[key];
|
|
229
|
-
return typeof value === "number" && Number.isFinite(value) ? value : 0;
|
|
230
|
-
}
|
|
231
215
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/core/extensions/builtin/goal/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACz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AO,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;AACxE,CAAC","sourcesContent":["import { createHash } from \"node:crypto\";\nimport { join } from \"node:path\";\nimport { getAgentDir } from \"../../../../config.ts\";\nimport type { ExtensionAPI, ExtensionContext } from \"../../types.ts\";\nimport { registerGoalCommand } from \"./command-registration.ts\";\nimport { shouldQueueGoalContinuationAfterAgentEnd, shouldQueueGoalContinuationWhenIdle } from \"./continuation.ts\";\nimport { GoalElapsedTicker } from \"./elapsed-ticker.ts\";\nimport { formatGoalForTool, goalStatusLabel } from \"./format.ts\";\nimport { buildContinuationPrompt } from \"./prompt.ts\";\nimport { accountGoalUsage, readGoal, updateGoal } from \"./store.ts\";\nimport { registerGoalTools } from \"./tool-registration.ts\";\nimport type { Goal, GoalAccountingMode, GoalStoreRef, TokenUsageSnapshot } from \"./types.ts\";\nimport { isRecord } from \"./types.ts\";\nimport { updateGoalUi } from \"./ui.ts\";\n\nconst GOAL_CONTINUATION_MESSAGE_TYPE = \"goal-continuation\";\nconst RESUME_GOAL_CHOICE = \"Resume goal\";\nconst LEAVE_GOAL_PAUSED_CHOICE = \"Leave paused\";\nconst EMPTY_USAGE: TokenUsageSnapshot = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0 };\nconst STALE_EXTENSION_CONTEXT_ERROR_PREFIX = \"This extension ctx is stale after session replacement or reload.\";\n\ntype AssistantUsageMessage = {\n\trole: \"assistant\";\n\tusage: Record<string, unknown>;\n};\ntype AgentGoalAccounting = {\n\tgoalId: string;\n\tmeasuredFromMilliseconds: number;\n};\n\nexport default function goalExtension(pi: ExtensionAPI): void {\n\tlet agentTurnInProgress = false;\n\tlet agentGoalAccounting: AgentGoalAccounting | null = null;\n\tlet completedThisTurnGoalId: string | null = null;\n\n\tconst goalTicker = new GoalElapsedTicker({\n\t\trender: (renderCtx, renderGoal, live) => {\n\t\t\ttry {\n\t\t\t\tupdateGoalUi(renderCtx, renderGoal, live);\n\t\t\t} catch (error) {\n\t\t\t\tif (error instanceof Error && error.message.startsWith(STALE_EXTENSION_CONTEXT_ERROR_PREFIX)) return;\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t},\n\t});\n\n\tregisterGoalTools(pi, {\n\t\tgoalStoreRef,\n\t\taccountCurrentAgentTurn,\n\t\tbeginAgentGoalAccounting,\n\t\tmarkGoalCompletedThisTurn,\n\t\trefreshGoalUi,\n\t});\n\tregisterGoalCommand(pi, {\n\t\tgoalStoreRef,\n\t\taccountCurrentAgentTurn,\n\t\tbeginAgentGoalAccounting,\n\t\tstopAgentGoalAccounting,\n\t\tclearAgentGoalAccounting,\n\t\tqueueGoalContinuation,\n\t\trefreshGoalUi,\n\t});\n\n\tpi.on(\"session_start\", async (event, ctx) => {\n\t\tconst goal = await readGoal(goalStoreRef(ctx));\n\t\tif (goal?.status === \"active\") {\n\t\t\tbeginAgentGoalAccounting(goal);\n\t\t} else {\n\t\t\tclearAgentGoalAccounting();\n\t\t}\n\t\trefreshGoalUi(ctx, goal);\n\t\tif (await maybePromptResumePausedGoal(pi, ctx, event.reason, goal)) {\n\t\t\treturn;\n\t\t}\n\t\tif (shouldQueueGoalContinuationWhenIdle(goal, ctx.isIdle(), ctx.hasPendingMessages())) {\n\t\t\tqueueHiddenGoalPrompt(pi, buildContinuationPrompt(goal));\n\t\t}\n\t});\n\n\tpi.on(\"agent_start\", async (_event, ctx) => {\n\t\tagentTurnInProgress = true;\n\t\tcompletedThisTurnGoalId = null;\n\t\tconst goal = await readGoal(goalStoreRef(ctx));\n\t\tif (goal?.status === \"active\") {\n\t\t\tbeginAgentGoalAccounting(goal);\n\t\t} else {\n\t\t\tagentGoalAccounting = null;\n\t\t}\n\t});\n\n\tpi.on(\"agent_end\", async (event, ctx) => {\n\t\tconst mode: GoalAccountingMode = completedThisTurnGoalId === null ? \"active\" : \"activeOrComplete\";\n\t\tconst goal = await accountCurrentAgentTurn(ctx, collectAssistantUsage(event.messages), mode);\n\t\tagentTurnInProgress = false;\n\t\tcompletedThisTurnGoalId = null;\n\t\tif (goal?.status === \"active\") {\n\t\t\tbeginAgentGoalAccounting(goal);\n\t\t} else {\n\t\t\tclearAgentGoalAccounting();\n\t\t}\n\t\trefreshGoalUiBestEffort(ctx, goal);\n\t\tif (\n\t\t\tgoal?.status === \"active\" &&\n\t\t\t!ctx.signal?.aborted &&\n\t\t\tshouldQueueGoalContinuationAfterAgentEnd(goal, ctx.hasPendingMessages(), event.messages)\n\t\t) {\n\t\t\tqueueHiddenGoalPrompt(pi, buildContinuationPrompt(goal));\n\t\t}\n\t});\n\n\tpi.on(\"session_shutdown\", async (_event, ctx) => {\n\t\tif (agentGoalAccounting !== null) {\n\t\t\tawait accountCurrentAgentTurn(ctx, EMPTY_USAGE, \"active\");\n\t\t}\n\t\tclearAgentGoalAccounting();\n\t\tgoalTicker.stop();\n\t});\n\n\tasync function maybePromptResumePausedGoal(\n\t\tpi: ExtensionAPI,\n\t\tctx: ExtensionContext,\n\t\tsessionStartReason: string,\n\t\tgoal: Goal | null,\n\t): Promise<boolean> {\n\t\tif (!isResumeOfPausedGoal(ctx, sessionStartReason, goal)) {\n\t\t\treturn false;\n\t\t}\n\n\t\tconst choice = await ctx.ui.select(`Resume paused goal?\\nGoal: ${goal.objective}`, [\n\t\t\tRESUME_GOAL_CHOICE,\n\t\t\tLEAVE_GOAL_PAUSED_CHOICE,\n\t\t]);\n\t\tif (choice !== RESUME_GOAL_CHOICE) return true;\n\n\t\tconst resumed = await updateGoal(goalStoreRef(ctx), { status: \"active\" });\n\t\tbeginAgentGoalAccounting(resumed);\n\t\trefreshGoalUi(ctx, resumed);\n\t\tctx.ui.notify(`Goal ${goalStatusLabel(resumed.status)}\\n${formatGoalForTool(resumed)}`, \"info\");\n\t\tqueueGoalContinuation(pi, ctx, resumed);\n\t\treturn true;\n\t}\n\n\tfunction beginAgentGoalAccounting(goal: Goal): void {\n\t\tif (goal.status !== \"active\") return;\n\t\tif (agentGoalAccounting?.goalId === goal.id) return;\n\t\tagentGoalAccounting = { goalId: goal.id, measuredFromMilliseconds: Date.now() };\n\t}\n\n\tfunction markGoalCompletedThisTurn(goal: Goal): void {\n\t\tif (!agentTurnInProgress) return;\n\t\tcompletedThisTurnGoalId = goal.id;\n\t\tagentGoalAccounting = { goalId: goal.id, measuredFromMilliseconds: Date.now() };\n\t}\n\n\tfunction stopAgentGoalAccounting(goalId: string): void {\n\t\tif (agentGoalAccounting?.goalId === goalId) {\n\t\t\tagentGoalAccounting = null;\n\t\t}\n\t\tif (completedThisTurnGoalId === goalId) {\n\t\t\tcompletedThisTurnGoalId = null;\n\t\t}\n\t}\n\n\tfunction clearAgentGoalAccounting(): void {\n\t\tagentGoalAccounting = null;\n\t\tcompletedThisTurnGoalId = null;\n\t}\n\n\tfunction refreshGoalUi(ctx: ExtensionContext, goal: Goal | null): void {\n\t\tconst accounting = agentGoalAccounting;\n\t\tif (ctx.hasUI && goal?.status === \"active\" && accounting?.goalId === goal.id) {\n\t\t\tgoalTicker.sync(ctx, goal, accounting.measuredFromMilliseconds);\n\t\t\treturn;\n\t\t}\n\t\tgoalTicker.stop();\n\t\tupdateGoalUi(ctx, goal);\n\t}\n\n\tfunction refreshGoalUiBestEffort(ctx: ExtensionContext, goal: Goal | null): void {\n\t\ttry {\n\t\t\trefreshGoalUi(ctx, goal);\n\t\t} catch (error) {\n\t\t\tif (error instanceof Error && error.message.startsWith(STALE_EXTENSION_CONTEXT_ERROR_PREFIX)) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tasync function accountCurrentAgentTurn(\n\t\tctx: ExtensionContext,\n\t\tusage: TokenUsageSnapshot,\n\t\tmode: GoalAccountingMode,\n\t): Promise<Goal | null> {\n\t\tconst accounting = agentGoalAccounting;\n\t\tconst ref = goalStoreRef(ctx);\n\t\tif (accounting === null) return readGoal(ref);\n\n\t\tconst now = Date.now();\n\t\tconst elapsedSeconds = Math.max(0, Math.round((now - accounting.measuredFromMilliseconds) / 1000));\n\t\tconst goal = await accountGoalUsage(ref, usage, elapsedSeconds, mode, accounting.goalId);\n\t\tif (goal?.id === accounting.goalId) {\n\t\t\tagentGoalAccounting = { goalId: accounting.goalId, measuredFromMilliseconds: now };\n\t\t} else {\n\t\t\tclearAgentGoalAccounting();\n\t\t}\n\t\treturn goal;\n\t}\n}\n\nfunction isResumeOfPausedGoal(ctx: ExtensionContext, sessionStartReason: string, goal: Goal | null): goal is Goal {\n\treturn (\n\t\tsessionStartReason === \"resume\" &&\n\t\tgoal?.status === \"paused\" &&\n\t\tctx.hasUI &&\n\t\tctx.isIdle() &&\n\t\t!ctx.hasPendingMessages()\n\t);\n}\n\nfunction queueGoalContinuation(pi: ExtensionAPI, ctx: ExtensionContext, goal: Goal): void {\n\tif (shouldQueueGoalContinuationWhenIdle(goal, ctx.isIdle(), ctx.hasPendingMessages())) {\n\t\tqueueHiddenGoalPrompt(pi, buildContinuationPrompt(goal));\n\t}\n}\n\nfunction queueHiddenGoalPrompt(pi: ExtensionAPI, content: string): void {\n\tpi.sendMessage(\n\t\t{ customType: GOAL_CONTINUATION_MESSAGE_TYPE, content, display: false },\n\t\t{ triggerTurn: true, deliverAs: \"followUp\" },\n\t);\n}\n\nfunction goalStoreRef(ctx: ExtensionContext): GoalStoreRef {\n\tconst sessionFile = ctx.sessionManager.getSessionFile();\n\tconst baseDir =\n\t\tsessionFile === undefined\n\t\t\t? join(getAgentDir(), \"extensions\", \"goal\", \"no-session\", cwdStoreKey(ctx.cwd))\n\t\t\t: join(ctx.sessionManager.getSessionDir(), \"extensions\", \"goal\");\n\n\treturn {\n\t\tbaseDir,\n\t\tthreadId: ctx.sessionManager.getSessionId(),\n\t};\n}\n\nfunction cwdStoreKey(cwd: string): string {\n\treturn createHash(\"sha256\").update(cwd).digest(\"hex\").slice(0, 24);\n}\n\nfunction collectAssistantUsage(messages: unknown[]): TokenUsageSnapshot {\n\tconst usage: TokenUsageSnapshot = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0 };\n\tfor (const message of messages) {\n\t\tif (!isAssistantUsageMessage(message)) continue;\n\t\tusage.input += numericUsageField(message.usage, \"input\");\n\t\tusage.output += numericUsageField(message.usage, \"output\");\n\t\tusage.cacheRead += numericUsageField(message.usage, \"cacheRead\");\n\t\tusage.cacheWrite += numericUsageField(message.usage, \"cacheWrite\");\n\t\tusage.totalTokens += numericUsageField(message.usage, \"totalTokens\");\n\t}\n\treturn usage;\n}\n\nfunction isAssistantUsageMessage(message: unknown): message is AssistantUsageMessage {\n\tif (!isRecord(message)) return false;\n\treturn message.role === \"assistant\" && isRecord(message.usage);\n}\n\nfunction numericUsageField(usage: Record<string, unknown>, key: string): number {\n\tconst value = usage[key];\n\treturn typeof value === \"number\" && Number.isFinite(value) ? value : 0;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/core/extensions/builtin/goal/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAEpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAChE,OAAO,EAAE,wCAAwC,EAAE,mCAAmC,EAAE,MAAM,mBAAmB,CAAC;AAClH,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACjE,OAAO,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACpE,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OA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AAU;IACjF,IAAI,mCAAmC,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,EAAE,EAAE,GAAG,CAAC,kBAAkB,EAAE,CAAC,EAAE,CAAC;QACvF,qBAAqB,CAAC,EAAE,EAAE,uBAAuB,CAAC,IAAI,CAAC,CAAC,CAAC;IAC1D,CAAC;AACF,CAAC;AAED,SAAS,qBAAqB,CAAC,EAAgB,EAAE,OAAe;IAC/D,EAAE,CAAC,WAAW,CACb,EAAE,UAAU,EAAE,8BAA8B,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,EACvE,EAAE,WAAW,EAAE,IAAI,EAAE,SAAS,EAAE,UAAU,EAAE,CAC5C,CAAC;AACH,CAAC;AAED,SAAS,YAAY,CAAC,GAAqB;IAC1C,MAAM,WAAW,GAAG,GAAG,CAAC,cAAc,CAAC,cAAc,EAAE,CAAC;IACxD,MAAM,OAAO,GACZ,WAAW,KAAK,SAAS;QACxB,CAAC,CAAC,IAAI,CAAC,WAAW,EAAE,EAAE,YAAY,EAAE,MAAM,EAAE,YAAY,EAAE,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAC/E,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,cAAc,CAAC,aAAa,EAAE,EAAE,YAAY,EAAE,MAAM,CAAC,CAAC;IAEnE,OAAO;QACN,OAAO;QACP,QAAQ,EAAE,GAAG,CAAC,cAAc,CAAC,YAAY,EAAE;KAC3C,CAAC;AACH,CAAC;AAED,SAAS,WAAW,CAAC,GAAW;IAC/B,OAAO,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;AACpE,CAAC","sourcesContent":["import { createHash } from \"node:crypto\";\nimport { join } from \"node:path\";\nimport { getAgentDir } from \"../../../../config.ts\";\nimport type { ExtensionAPI, ExtensionContext } from \"../../types.ts\";\nimport { registerGoalCommand } from \"./command-registration.ts\";\nimport { shouldQueueGoalContinuationAfterAgentEnd, shouldQueueGoalContinuationWhenIdle } from \"./continuation.ts\";\nimport { GoalElapsedTicker } from \"./elapsed-ticker.ts\";\nimport { formatGoalForTool, goalStatusLabel } from \"./format.ts\";\nimport { buildContinuationPrompt } from \"./prompt.ts\";\nimport { accountGoalUsage, readGoal, updateGoal } from \"./store.ts\";\nimport { registerGoalTools } from \"./tool-registration.ts\";\nimport { TurnUsageTracker } from \"./turn-usage.ts\";\nimport type { Goal, GoalAccountingMode, GoalStoreRef } from \"./types.ts\";\nimport { updateGoalUi } from \"./ui.ts\";\n\nconst GOAL_CONTINUATION_MESSAGE_TYPE = \"goal-continuation\";\nconst RESUME_GOAL_CHOICE = \"Resume goal\";\nconst LEAVE_GOAL_PAUSED_CHOICE = \"Leave paused\";\nconst STALE_EXTENSION_CONTEXT_ERROR_PREFIX = \"This extension ctx is stale after session replacement or reload.\";\n\ntype AgentGoalAccounting = {\n\tgoalId: string;\n\tmeasuredFromMilliseconds: number;\n};\n\nexport default function goalExtension(pi: ExtensionAPI): void {\n\tlet agentTurnInProgress = false;\n\tlet agentGoalAccounting: AgentGoalAccounting | null = null;\n\tlet completedThisTurnGoalId: string | null = null;\n\tconst turnUsage = new TurnUsageTracker();\n\n\tconst goalTicker = new GoalElapsedTicker({\n\t\trender: (renderCtx, renderGoal, live) => {\n\t\t\ttry {\n\t\t\t\tupdateGoalUi(renderCtx, renderGoal, live);\n\t\t\t} catch (error) {\n\t\t\t\tif (error instanceof Error && error.message.startsWith(STALE_EXTENSION_CONTEXT_ERROR_PREFIX)) return;\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t},\n\t});\n\n\tregisterGoalTools(pi, {\n\t\tgoalStoreRef,\n\t\taccountCurrentAgentTurn,\n\t\tbeginAgentGoalAccounting,\n\t\tmarkGoalCompletedThisTurn,\n\t\trefreshGoalUi,\n\t});\n\tregisterGoalCommand(pi, {\n\t\tgoalStoreRef,\n\t\taccountCurrentAgentTurn,\n\t\tbeginAgentGoalAccounting,\n\t\tstopAgentGoalAccounting,\n\t\tclearAgentGoalAccounting,\n\t\tqueueGoalContinuation,\n\t\trefreshGoalUi,\n\t});\n\n\tpi.on(\"session_start\", async (event, ctx) => {\n\t\tconst goal = await readGoal(goalStoreRef(ctx));\n\t\tif (goal?.status === \"active\") {\n\t\t\tbeginAgentGoalAccounting(goal);\n\t\t} else {\n\t\t\tclearAgentGoalAccounting();\n\t\t}\n\t\trefreshGoalUi(ctx, goal);\n\t\tif (await maybePromptResumePausedGoal(pi, ctx, event.reason, goal)) {\n\t\t\treturn;\n\t\t}\n\t\tif (shouldQueueGoalContinuationWhenIdle(goal, ctx.isIdle(), ctx.hasPendingMessages())) {\n\t\t\tqueueHiddenGoalPrompt(pi, buildContinuationPrompt(goal));\n\t\t}\n\t});\n\n\tpi.on(\"agent_start\", async (_event, ctx) => {\n\t\tagentTurnInProgress = true;\n\t\tcompletedThisTurnGoalId = null;\n\t\tturnUsage.reset();\n\t\tconst goal = await readGoal(goalStoreRef(ctx));\n\t\tif (goal?.status === \"active\") {\n\t\t\tbeginAgentGoalAccounting(goal);\n\t\t} else {\n\t\t\tagentGoalAccounting = null;\n\t\t}\n\t});\n\n\tpi.on(\"message_end\", async (event) => {\n\t\tturnUsage.noteMessageEnd(event.message);\n\t});\n\n\tpi.on(\"agent_end\", async (event, ctx) => {\n\t\tconst mode: GoalAccountingMode = completedThisTurnGoalId === null ? \"active\" : \"activeOrComplete\";\n\t\tconst goal = await accountCurrentAgentTurn(ctx, mode, event.messages);\n\t\tagentTurnInProgress = false;\n\t\tcompletedThisTurnGoalId = null;\n\t\tif (goal?.status === \"active\") {\n\t\t\tbeginAgentGoalAccounting(goal);\n\t\t} else {\n\t\t\tclearAgentGoalAccounting();\n\t\t}\n\t\trefreshGoalUiBestEffort(ctx, goal);\n\t\tif (\n\t\t\tgoal?.status === \"active\" &&\n\t\t\t!ctx.signal?.aborted &&\n\t\t\tshouldQueueGoalContinuationAfterAgentEnd(goal, ctx.hasPendingMessages(), event.messages)\n\t\t) {\n\t\t\tqueueHiddenGoalPrompt(pi, buildContinuationPrompt(goal));\n\t\t}\n\t});\n\n\tpi.on(\"session_shutdown\", async (_event, ctx) => {\n\t\tif (agentGoalAccounting !== null) {\n\t\t\tawait accountCurrentAgentTurn(ctx, \"active\");\n\t\t}\n\t\tclearAgentGoalAccounting();\n\t\tgoalTicker.stop();\n\t});\n\n\tasync function maybePromptResumePausedGoal(\n\t\tpi: ExtensionAPI,\n\t\tctx: ExtensionContext,\n\t\tsessionStartReason: string,\n\t\tgoal: Goal | null,\n\t): Promise<boolean> {\n\t\tif (!isResumeOfPausedGoal(ctx, sessionStartReason, goal)) {\n\t\t\treturn false;\n\t\t}\n\n\t\tconst choice = await ctx.ui.select(`Resume paused goal?\\nGoal: ${goal.objective}`, [\n\t\t\tRESUME_GOAL_CHOICE,\n\t\t\tLEAVE_GOAL_PAUSED_CHOICE,\n\t\t]);\n\t\tif (choice !== RESUME_GOAL_CHOICE) return true;\n\n\t\tconst resumed = await updateGoal(goalStoreRef(ctx), { status: \"active\" });\n\t\tbeginAgentGoalAccounting(resumed);\n\t\trefreshGoalUi(ctx, resumed);\n\t\tctx.ui.notify(`Goal ${goalStatusLabel(resumed.status)}\\n${formatGoalForTool(resumed)}`, \"info\");\n\t\tqueueGoalContinuation(pi, ctx, resumed);\n\t\treturn true;\n\t}\n\n\tfunction beginAgentGoalAccounting(goal: Goal): void {\n\t\tif (goal.status !== \"active\") return;\n\t\tif (agentGoalAccounting?.goalId === goal.id) return;\n\t\tturnUsage.discardPending();\n\t\tagentGoalAccounting = { goalId: goal.id, measuredFromMilliseconds: Date.now() };\n\t}\n\n\tfunction markGoalCompletedThisTurn(goal: Goal): void {\n\t\tif (!agentTurnInProgress) return;\n\t\tcompletedThisTurnGoalId = goal.id;\n\t\tagentGoalAccounting = { goalId: goal.id, measuredFromMilliseconds: Date.now() };\n\t}\n\n\tfunction stopAgentGoalAccounting(goalId: string): void {\n\t\tif (agentGoalAccounting?.goalId === goalId) {\n\t\t\tagentGoalAccounting = null;\n\t\t}\n\t\tif (completedThisTurnGoalId === goalId) {\n\t\t\tcompletedThisTurnGoalId = null;\n\t\t}\n\t}\n\n\tfunction clearAgentGoalAccounting(): void {\n\t\tagentGoalAccounting = null;\n\t\tcompletedThisTurnGoalId = null;\n\t}\n\n\tfunction refreshGoalUi(ctx: ExtensionContext, goal: Goal | null): void {\n\t\tconst accounting = agentGoalAccounting;\n\t\tif (ctx.hasUI && goal?.status === \"active\" && accounting?.goalId === goal.id) {\n\t\t\tgoalTicker.sync(ctx, goal, accounting.measuredFromMilliseconds);\n\t\t\treturn;\n\t\t}\n\t\tgoalTicker.stop();\n\t\tupdateGoalUi(ctx, goal);\n\t}\n\n\tfunction refreshGoalUiBestEffort(ctx: ExtensionContext, goal: Goal | null): void {\n\t\ttry {\n\t\t\trefreshGoalUi(ctx, goal);\n\t\t} catch (error) {\n\t\t\tif (error instanceof Error && error.message.startsWith(STALE_EXTENSION_CONTEXT_ERROR_PREFIX)) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tasync function accountCurrentAgentTurn(\n\t\tctx: ExtensionContext,\n\t\tmode: GoalAccountingMode,\n\t\tagentRunMessages?: unknown[],\n\t): Promise<Goal | null> {\n\t\tconst accounting = agentGoalAccounting;\n\t\tconst ref = goalStoreRef(ctx);\n\t\tif (accounting === null) return readGoal(ref);\n\n\t\tconst usage =\n\t\t\tagentRunMessages === undefined ? turnUsage.takePending() : turnUsage.takeRemaining(agentRunMessages);\n\t\tconst now = Date.now();\n\t\tconst elapsedSeconds = Math.max(0, Math.round((now - accounting.measuredFromMilliseconds) / 1000));\n\t\tconst goal = await accountGoalUsage(ref, usage, elapsedSeconds, mode, accounting.goalId);\n\t\tif (goal?.id === accounting.goalId) {\n\t\t\tagentGoalAccounting = { goalId: accounting.goalId, measuredFromMilliseconds: now };\n\t\t} else {\n\t\t\tclearAgentGoalAccounting();\n\t\t}\n\t\treturn goal;\n\t}\n}\n\nfunction isResumeOfPausedGoal(ctx: ExtensionContext, sessionStartReason: string, goal: Goal | null): goal is Goal {\n\treturn (\n\t\tsessionStartReason === \"resume\" &&\n\t\tgoal?.status === \"paused\" &&\n\t\tctx.hasUI &&\n\t\tctx.isIdle() &&\n\t\t!ctx.hasPendingMessages()\n\t);\n}\n\nfunction queueGoalContinuation(pi: ExtensionAPI, ctx: ExtensionContext, goal: Goal): void {\n\tif (shouldQueueGoalContinuationWhenIdle(goal, ctx.isIdle(), ctx.hasPendingMessages())) {\n\t\tqueueHiddenGoalPrompt(pi, buildContinuationPrompt(goal));\n\t}\n}\n\nfunction queueHiddenGoalPrompt(pi: ExtensionAPI, content: string): void {\n\tpi.sendMessage(\n\t\t{ customType: GOAL_CONTINUATION_MESSAGE_TYPE, content, display: false },\n\t\t{ triggerTurn: true, deliverAs: \"followUp\" },\n\t);\n}\n\nfunction goalStoreRef(ctx: ExtensionContext): GoalStoreRef {\n\tconst sessionFile = ctx.sessionManager.getSessionFile();\n\tconst baseDir =\n\t\tsessionFile === undefined\n\t\t\t? join(getAgentDir(), \"extensions\", \"goal\", \"no-session\", cwdStoreKey(ctx.cwd))\n\t\t\t: join(ctx.sessionManager.getSessionDir(), \"extensions\", \"goal\");\n\n\treturn {\n\t\tbaseDir,\n\t\tthreadId: ctx.sessionManager.getSessionId(),\n\t};\n}\n\nfunction cwdStoreKey(cwd: string): string {\n\treturn createHash(\"sha256\").update(cwd).digest(\"hex\").slice(0, 24);\n}\n"]}
|