@edwardlee5423/magi 0.1.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +184 -0
- package/dist/agent/headless-agent.d.ts +15 -0
- package/dist/agent/headless-agent.js +105 -0
- package/dist/agent/headless-agent.js.map +1 -0
- package/dist/agent/local-plan.d.ts +20 -0
- package/dist/agent/local-plan.js +30 -0
- package/dist/agent/local-plan.js.map +1 -0
- package/dist/agent/query-engine.d.ts +71 -0
- package/dist/agent/query-engine.js +938 -0
- package/dist/agent/query-engine.js.map +1 -0
- package/dist/agent/query.d.ts +128 -0
- package/dist/agent/query.js +674 -0
- package/dist/agent/query.js.map +1 -0
- package/dist/agent/system-prompt.d.ts +12 -0
- package/dist/agent/system-prompt.js +104 -0
- package/dist/agent/system-prompt.js.map +1 -0
- package/dist/agent/tools.d.ts +71 -0
- package/dist/agent/tools.js +37 -0
- package/dist/agent/tools.js.map +1 -0
- package/dist/agents/roles.d.ts +9 -0
- package/dist/agents/roles.js +18 -0
- package/dist/agents/roles.js.map +1 -0
- package/dist/agents/task-queue.d.ts +13 -0
- package/dist/agents/task-queue.js +50 -0
- package/dist/agents/task-queue.js.map +1 -0
- package/dist/cli.d.ts +7 -0
- package/dist/cli.js +1540 -0
- package/dist/cli.js.map +1 -0
- package/dist/colors.d.ts +20 -0
- package/dist/colors.js +92 -0
- package/dist/colors.js.map +1 -0
- package/dist/commands/agents.d.ts +9 -0
- package/dist/commands/agents.js +37 -0
- package/dist/commands/agents.js.map +1 -0
- package/dist/commands/clear.d.ts +9 -0
- package/dist/commands/clear.js +20 -0
- package/dist/commands/clear.js.map +1 -0
- package/dist/commands/commit.d.ts +8 -0
- package/dist/commands/commit.js +35 -0
- package/dist/commands/commit.js.map +1 -0
- package/dist/commands/compact.d.ts +8 -0
- package/dist/commands/compact.js +29 -0
- package/dist/commands/compact.js.map +1 -0
- package/dist/commands/copy.d.ts +8 -0
- package/dist/commands/copy.js +49 -0
- package/dist/commands/copy.js.map +1 -0
- package/dist/commands/cost.d.ts +8 -0
- package/dist/commands/cost.js +94 -0
- package/dist/commands/cost.js.map +1 -0
- package/dist/commands/diff.d.ts +8 -0
- package/dist/commands/diff.js +38 -0
- package/dist/commands/diff.js.map +1 -0
- package/dist/commands/doctor.d.ts +8 -0
- package/dist/commands/doctor.js +23 -0
- package/dist/commands/doctor.js.map +1 -0
- package/dist/commands/export.d.ts +8 -0
- package/dist/commands/export.js +42 -0
- package/dist/commands/export.js.map +1 -0
- package/dist/commands/fork.d.ts +8 -0
- package/dist/commands/fork.js +20 -0
- package/dist/commands/fork.js.map +1 -0
- package/dist/commands/goal.d.ts +9 -0
- package/dist/commands/goal.js +54 -0
- package/dist/commands/goal.js.map +1 -0
- package/dist/commands/help.d.ts +8 -0
- package/dist/commands/help.js +65 -0
- package/dist/commands/help.js.map +1 -0
- package/dist/commands/hooks.d.ts +8 -0
- package/dist/commands/hooks.js +56 -0
- package/dist/commands/hooks.js.map +1 -0
- package/dist/commands/image.d.ts +17 -0
- package/dist/commands/image.js +74 -0
- package/dist/commands/image.js.map +1 -0
- package/dist/commands/init.d.ts +26 -0
- package/dist/commands/init.js +214 -0
- package/dist/commands/init.js.map +1 -0
- package/dist/commands/mcp.d.ts +8 -0
- package/dist/commands/mcp.js +217 -0
- package/dist/commands/mcp.js.map +1 -0
- package/dist/commands/memory.d.ts +8 -0
- package/dist/commands/memory.js +79 -0
- package/dist/commands/memory.js.map +1 -0
- package/dist/commands/model.d.ts +8 -0
- package/dist/commands/model.js +21 -0
- package/dist/commands/model.js.map +1 -0
- package/dist/commands/permissions.d.ts +9 -0
- package/dist/commands/permissions.js +30 -0
- package/dist/commands/permissions.js.map +1 -0
- package/dist/commands/proactive.d.ts +9 -0
- package/dist/commands/proactive.js +24 -0
- package/dist/commands/proactive.js.map +1 -0
- package/dist/commands/register-all.d.ts +1 -0
- package/dist/commands/register-all.js +68 -0
- package/dist/commands/register-all.js.map +1 -0
- package/dist/commands/registry.d.ts +64 -0
- package/dist/commands/registry.js +235 -0
- package/dist/commands/registry.js.map +1 -0
- package/dist/commands/rename.d.ts +8 -0
- package/dist/commands/rename.js +16 -0
- package/dist/commands/rename.js.map +1 -0
- package/dist/commands/review.d.ts +8 -0
- package/dist/commands/review.js +13 -0
- package/dist/commands/review.js.map +1 -0
- package/dist/commands/rewind.d.ts +8 -0
- package/dist/commands/rewind.js +45 -0
- package/dist/commands/rewind.js.map +1 -0
- package/dist/commands/route.d.ts +9 -0
- package/dist/commands/route.js +83 -0
- package/dist/commands/route.js.map +1 -0
- package/dist/commands/sessions.d.ts +9 -0
- package/dist/commands/sessions.js +25 -0
- package/dist/commands/sessions.js.map +1 -0
- package/dist/commands/skill.d.ts +9 -0
- package/dist/commands/skill.js +46 -0
- package/dist/commands/skill.js.map +1 -0
- package/dist/commands/status.d.ts +8 -0
- package/dist/commands/status.js +41 -0
- package/dist/commands/status.js.map +1 -0
- package/dist/commands/summary.d.ts +8 -0
- package/dist/commands/summary.js +39 -0
- package/dist/commands/summary.js.map +1 -0
- package/dist/commands/tasks.d.ts +9 -0
- package/dist/commands/tasks.js +70 -0
- package/dist/commands/tasks.js.map +1 -0
- package/dist/commands/todos.d.ts +9 -0
- package/dist/commands/todos.js +27 -0
- package/dist/commands/todos.js.map +1 -0
- package/dist/commands/tutorial.d.ts +8 -0
- package/dist/commands/tutorial.js +159 -0
- package/dist/commands/tutorial.js.map +1 -0
- package/dist/commands/upgrade.d.ts +9 -0
- package/dist/commands/upgrade.js +73 -0
- package/dist/commands/upgrade.js.map +1 -0
- package/dist/commands/usage.d.ts +8 -0
- package/dist/commands/usage.js +54 -0
- package/dist/commands/usage.js.map +1 -0
- package/dist/commands/vim.d.ts +10 -0
- package/dist/commands/vim.js +34 -0
- package/dist/commands/vim.js.map +1 -0
- package/dist/config.d.ts +104 -0
- package/dist/config.js +533 -0
- package/dist/config.js.map +1 -0
- package/dist/context/compaction.d.ts +66 -0
- package/dist/context/compaction.js +353 -0
- package/dist/context/compaction.js.map +1 -0
- package/dist/context/layers.d.ts +32 -0
- package/dist/context/layers.js +114 -0
- package/dist/context/layers.js.map +1 -0
- package/dist/context/token-budget.d.ts +21 -0
- package/dist/context/token-budget.js +70 -0
- package/dist/context/token-budget.js.map +1 -0
- package/dist/control/auth.d.ts +17 -0
- package/dist/control/auth.js +31 -0
- package/dist/control/auth.js.map +1 -0
- package/dist/control/daemon.d.ts +38 -0
- package/dist/control/daemon.js +135 -0
- package/dist/control/daemon.js.map +1 -0
- package/dist/control/mdns.d.ts +51 -0
- package/dist/control/mdns.js +344 -0
- package/dist/control/mdns.js.map +1 -0
- package/dist/control/peer-client.d.ts +49 -0
- package/dist/control/peer-client.js +170 -0
- package/dist/control/peer-client.js.map +1 -0
- package/dist/control/server.d.ts +30 -0
- package/dist/control/server.js +887 -0
- package/dist/control/server.js.map +1 -0
- package/dist/cost.d.ts +23 -0
- package/dist/cost.js +80 -0
- package/dist/cost.js.map +1 -0
- package/dist/doctor.d.ts +8 -0
- package/dist/doctor.js +22 -0
- package/dist/doctor.js.map +1 -0
- package/dist/env.d.ts +7 -0
- package/dist/env.js +70 -0
- package/dist/env.js.map +1 -0
- package/dist/errors.d.ts +6 -0
- package/dist/errors.js +13 -0
- package/dist/errors.js.map +1 -0
- package/dist/events.d.ts +19 -0
- package/dist/events.js +236 -0
- package/dist/events.js.map +1 -0
- package/dist/fs-utils.d.ts +21 -0
- package/dist/fs-utils.js +48 -0
- package/dist/fs-utils.js.map +1 -0
- package/dist/goal.d.ts +32 -0
- package/dist/goal.js +172 -0
- package/dist/goal.js.map +1 -0
- package/dist/headless.d.ts +38 -0
- package/dist/headless.js +547 -0
- package/dist/headless.js.map +1 -0
- package/dist/history.d.ts +8 -0
- package/dist/history.js +77 -0
- package/dist/history.js.map +1 -0
- package/dist/hooks/events.d.ts +14 -0
- package/dist/hooks/events.js +37 -0
- package/dist/hooks/events.js.map +1 -0
- package/dist/hooks/runner.d.ts +73 -0
- package/dist/hooks/runner.js +256 -0
- package/dist/hooks/runner.js.map +1 -0
- package/dist/hooks/trigger.d.ts +9 -0
- package/dist/hooks/trigger.js +18 -0
- package/dist/hooks/trigger.js.map +1 -0
- package/dist/index.d.ts +61 -0
- package/dist/index.js +62 -0
- package/dist/index.js.map +1 -0
- package/dist/interactions.d.ts +102 -0
- package/dist/interactions.js +250 -0
- package/dist/interactions.js.map +1 -0
- package/dist/logger.d.ts +32 -0
- package/dist/logger.js +115 -0
- package/dist/logger.js.map +1 -0
- package/dist/markdown.d.ts +18 -0
- package/dist/markdown.js +293 -0
- package/dist/markdown.js.map +1 -0
- package/dist/mcp/approval.d.ts +9 -0
- package/dist/mcp/approval.js +27 -0
- package/dist/mcp/approval.js.map +1 -0
- package/dist/mcp/client.d.ts +61 -0
- package/dist/mcp/client.js +276 -0
- package/dist/mcp/client.js.map +1 -0
- package/dist/mcp/connection-manager.d.ts +30 -0
- package/dist/mcp/connection-manager.js +126 -0
- package/dist/mcp/connection-manager.js.map +1 -0
- package/dist/mcp/oauth-callback.d.ts +23 -0
- package/dist/mcp/oauth-callback.js +108 -0
- package/dist/mcp/oauth-callback.js.map +1 -0
- package/dist/mcp/oauth-flow.d.ts +45 -0
- package/dist/mcp/oauth-flow.js +145 -0
- package/dist/mcp/oauth-flow.js.map +1 -0
- package/dist/mcp/oauth.d.ts +82 -0
- package/dist/mcp/oauth.js +169 -0
- package/dist/mcp/oauth.js.map +1 -0
- package/dist/mcp/tool-registry.d.ts +29 -0
- package/dist/mcp/tool-registry.js +207 -0
- package/dist/mcp/tool-registry.js.map +1 -0
- package/dist/mcp/transport.d.ts +33 -0
- package/dist/mcp/transport.js +493 -0
- package/dist/mcp/transport.js.map +1 -0
- package/dist/mcp/types.d.ts +57 -0
- package/dist/mcp/types.js +2 -0
- package/dist/mcp/types.js.map +1 -0
- package/dist/memdir.d.ts +58 -0
- package/dist/memdir.js +219 -0
- package/dist/memdir.js.map +1 -0
- package/dist/memory-selection.d.ts +30 -0
- package/dist/memory-selection.js +102 -0
- package/dist/memory-selection.js.map +1 -0
- package/dist/memory.d.ts +85 -0
- package/dist/memory.js +259 -0
- package/dist/memory.js.map +1 -0
- package/dist/paths.d.ts +25 -0
- package/dist/paths.js +99 -0
- package/dist/paths.js.map +1 -0
- package/dist/permissions.d.ts +17 -0
- package/dist/permissions.js +72 -0
- package/dist/permissions.js.map +1 -0
- package/dist/plugins/manifest.d.ts +17 -0
- package/dist/plugins/manifest.js +98 -0
- package/dist/plugins/manifest.js.map +1 -0
- package/dist/plugins/marketplace.d.ts +22 -0
- package/dist/plugins/marketplace.js +98 -0
- package/dist/plugins/marketplace.js.map +1 -0
- package/dist/proactive.d.ts +15 -0
- package/dist/proactive.js +49 -0
- package/dist/proactive.js.map +1 -0
- package/dist/providers/errors.d.ts +14 -0
- package/dist/providers/errors.js +66 -0
- package/dist/providers/errors.js.map +1 -0
- package/dist/providers/format-proxy.d.ts +100 -0
- package/dist/providers/format-proxy.js +235 -0
- package/dist/providers/format-proxy.js.map +1 -0
- package/dist/providers/http.d.ts +4 -0
- package/dist/providers/http.js +21 -0
- package/dist/providers/http.js.map +1 -0
- package/dist/providers/ir.d.ts +84 -0
- package/dist/providers/ir.js +76 -0
- package/dist/providers/ir.js.map +1 -0
- package/dist/providers/messages-compatible.d.ts +19 -0
- package/dist/providers/messages-compatible.js +592 -0
- package/dist/providers/messages-compatible.js.map +1 -0
- package/dist/providers/openai.d.ts +18 -0
- package/dist/providers/openai.js +430 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/providers/registry.d.ts +10 -0
- package/dist/providers/registry.js +16 -0
- package/dist/providers/registry.js.map +1 -0
- package/dist/providers/sse.d.ts +6 -0
- package/dist/providers/sse.js +104 -0
- package/dist/providers/sse.js.map +1 -0
- package/dist/routing/model-alias.d.ts +8 -0
- package/dist/routing/model-alias.js +23 -0
- package/dist/routing/model-alias.js.map +1 -0
- package/dist/routing/model-router.d.ts +55 -0
- package/dist/routing/model-router.js +233 -0
- package/dist/routing/model-router.js.map +1 -0
- package/dist/routing/router.d.ts +26 -0
- package/dist/routing/router.js +63 -0
- package/dist/routing/router.js.map +1 -0
- package/dist/rules/agents-loader.d.ts +12 -0
- package/dist/rules/agents-loader.js +61 -0
- package/dist/rules/agents-loader.js.map +1 -0
- package/dist/runner/client.d.ts +67 -0
- package/dist/runner/client.js +175 -0
- package/dist/runner/client.js.map +1 -0
- package/dist/session-store.d.ts +250 -0
- package/dist/session-store.js +629 -0
- package/dist/session-store.js.map +1 -0
- package/dist/skills/bundled.d.ts +14 -0
- package/dist/skills/bundled.js +148 -0
- package/dist/skills/bundled.js.map +1 -0
- package/dist/skills/loader.d.ts +11 -0
- package/dist/skills/loader.js +73 -0
- package/dist/skills/loader.js.map +1 -0
- package/dist/slash-menu.d.ts +14 -0
- package/dist/slash-menu.js +166 -0
- package/dist/slash-menu.js.map +1 -0
- package/dist/slash.d.ts +75 -0
- package/dist/slash.js +126 -0
- package/dist/slash.js.map +1 -0
- package/dist/spinner.d.ts +27 -0
- package/dist/spinner.js +68 -0
- package/dist/spinner.js.map +1 -0
- package/dist/ssh/exec.d.ts +20 -0
- package/dist/ssh/exec.js +39 -0
- package/dist/ssh/exec.js.map +1 -0
- package/dist/ssh/file.d.ts +32 -0
- package/dist/ssh/file.js +60 -0
- package/dist/ssh/file.js.map +1 -0
- package/dist/syntax-highlight.d.ts +19 -0
- package/dist/syntax-highlight.js +181 -0
- package/dist/syntax-highlight.js.map +1 -0
- package/dist/tools/agent-tool.d.ts +53 -0
- package/dist/tools/agent-tool.js +62 -0
- package/dist/tools/agent-tool.js.map +1 -0
- package/dist/tools/archive-create.d.ts +35 -0
- package/dist/tools/archive-create.js +37 -0
- package/dist/tools/archive-create.js.map +1 -0
- package/dist/tools/archive-extract.d.ts +28 -0
- package/dist/tools/archive-extract.js +36 -0
- package/dist/tools/archive-extract.js.map +1 -0
- package/dist/tools/base64.d.ts +27 -0
- package/dist/tools/base64.js +19 -0
- package/dist/tools/base64.js.map +1 -0
- package/dist/tools/browser.d.ts +125 -0
- package/dist/tools/browser.js +241 -0
- package/dist/tools/browser.js.map +1 -0
- package/dist/tools/config-tool.d.ts +31 -0
- package/dist/tools/config-tool.js +167 -0
- package/dist/tools/config-tool.js.map +1 -0
- package/dist/tools/cron.d.ts +110 -0
- package/dist/tools/cron.js +299 -0
- package/dist/tools/cron.js.map +1 -0
- package/dist/tools/date.d.ts +24 -0
- package/dist/tools/date.js +26 -0
- package/dist/tools/date.js.map +1 -0
- package/dist/tools/dir-create.d.ts +21 -0
- package/dist/tools/dir-create.js +19 -0
- package/dist/tools/dir-create.js.map +1 -0
- package/dist/tools/dir-list.d.ts +36 -0
- package/dist/tools/dir-list.js +47 -0
- package/dist/tools/dir-list.js.map +1 -0
- package/dist/tools/disk-usage.d.ts +30 -0
- package/dist/tools/disk-usage.js +22 -0
- package/dist/tools/disk-usage.js.map +1 -0
- package/dist/tools/download-file.d.ts +28 -0
- package/dist/tools/download-file.js +42 -0
- package/dist/tools/download-file.js.map +1 -0
- package/dist/tools/environment.d.ts +31 -0
- package/dist/tools/environment.js +33 -0
- package/dist/tools/environment.js.map +1 -0
- package/dist/tools/errors.d.ts +5 -0
- package/dist/tools/errors.js +9 -0
- package/dist/tools/errors.js.map +1 -0
- package/dist/tools/file-copy.d.ts +34 -0
- package/dist/tools/file-copy.js +45 -0
- package/dist/tools/file-copy.js.map +1 -0
- package/dist/tools/file-delete.d.ts +34 -0
- package/dist/tools/file-delete.js +42 -0
- package/dist/tools/file-delete.js.map +1 -0
- package/dist/tools/file-find.d.ts +46 -0
- package/dist/tools/file-find.js +33 -0
- package/dist/tools/file-find.js.map +1 -0
- package/dist/tools/file-move.d.ts +33 -0
- package/dist/tools/file-move.js +32 -0
- package/dist/tools/file-move.js.map +1 -0
- package/dist/tools/files.d.ts +38 -0
- package/dist/tools/files.js +123 -0
- package/dist/tools/files.js.map +1 -0
- package/dist/tools/git-branch-delete.d.ts +27 -0
- package/dist/tools/git-branch-delete.js +25 -0
- package/dist/tools/git-branch-delete.js.map +1 -0
- package/dist/tools/git-reset.d.ts +30 -0
- package/dist/tools/git-reset.js +28 -0
- package/dist/tools/git-reset.js.map +1 -0
- package/dist/tools/git-stash.d.ts +28 -0
- package/dist/tools/git-stash.js +24 -0
- package/dist/tools/git-stash.js.map +1 -0
- package/dist/tools/git.d.ts +77 -0
- package/dist/tools/git.js +300 -0
- package/dist/tools/git.js.map +1 -0
- package/dist/tools/github.d.ts +68 -0
- package/dist/tools/github.js +77 -0
- package/dist/tools/github.js.map +1 -0
- package/dist/tools/head-tail.d.ts +34 -0
- package/dist/tools/head-tail.js +23 -0
- package/dist/tools/head-tail.js.map +1 -0
- package/dist/tools/http-request.d.ts +44 -0
- package/dist/tools/http-request.js +57 -0
- package/dist/tools/http-request.js.map +1 -0
- package/dist/tools/json-query.d.ts +27 -0
- package/dist/tools/json-query.js +90 -0
- package/dist/tools/json-query.js.map +1 -0
- package/dist/tools/kill-process.d.ts +32 -0
- package/dist/tools/kill-process.js +51 -0
- package/dist/tools/kill-process.js.map +1 -0
- package/dist/tools/lsp.d.ts +98 -0
- package/dist/tools/lsp.js +398 -0
- package/dist/tools/lsp.js.map +1 -0
- package/dist/tools/monitor.d.ts +29 -0
- package/dist/tools/monitor.js +78 -0
- package/dist/tools/monitor.js.map +1 -0
- package/dist/tools/network-check.d.ts +34 -0
- package/dist/tools/network-check.js +57 -0
- package/dist/tools/network-check.js.map +1 -0
- package/dist/tools/notebook.d.ts +63 -0
- package/dist/tools/notebook.js +158 -0
- package/dist/tools/notebook.js.map +1 -0
- package/dist/tools/plan-mode.d.ts +48 -0
- package/dist/tools/plan-mode.js +57 -0
- package/dist/tools/plan-mode.js.map +1 -0
- package/dist/tools/process-list.d.ts +35 -0
- package/dist/tools/process-list.js +42 -0
- package/dist/tools/process-list.js.map +1 -0
- package/dist/tools/registry.d.ts +132 -0
- package/dist/tools/registry.js +2262 -0
- package/dist/tools/registry.js.map +1 -0
- package/dist/tools/search.d.ts +38 -0
- package/dist/tools/search.js +327 -0
- package/dist/tools/search.js.map +1 -0
- package/dist/tools/shell.d.ts +18 -0
- package/dist/tools/shell.js +249 -0
- package/dist/tools/shell.js.map +1 -0
- package/dist/tools/skill-tool.d.ts +22 -0
- package/dist/tools/skill-tool.js +66 -0
- package/dist/tools/skill-tool.js.map +1 -0
- package/dist/tools/sleep.d.ts +16 -0
- package/dist/tools/sleep.js +24 -0
- package/dist/tools/sleep.js.map +1 -0
- package/dist/tools/snip.d.ts +26 -0
- package/dist/tools/snip.js +95 -0
- package/dist/tools/snip.js.map +1 -0
- package/dist/tools/system-info.d.ts +17 -0
- package/dist/tools/system-info.js +33 -0
- package/dist/tools/system-info.js.map +1 -0
- package/dist/tools/tasks.d.ts +191 -0
- package/dist/tools/tasks.js +348 -0
- package/dist/tools/tasks.js.map +1 -0
- package/dist/tools/text-stats.d.ts +25 -0
- package/dist/tools/text-stats.js +25 -0
- package/dist/tools/text-stats.js.map +1 -0
- package/dist/tools/todo.d.ts +69 -0
- package/dist/tools/todo.js +197 -0
- package/dist/tools/todo.js.map +1 -0
- package/dist/tools/tool-search.d.ts +29 -0
- package/dist/tools/tool-search.js +114 -0
- package/dist/tools/tool-search.js.map +1 -0
- package/dist/tools/tree-view.d.ts +34 -0
- package/dist/tools/tree-view.js +39 -0
- package/dist/tools/tree-view.js.map +1 -0
- package/dist/tools/user-message.d.ts +39 -0
- package/dist/tools/user-message.js +70 -0
- package/dist/tools/user-message.js.map +1 -0
- package/dist/tools/user-question.d.ts +88 -0
- package/dist/tools/user-question.js +236 -0
- package/dist/tools/user-question.js.map +1 -0
- package/dist/tools/web-browser.d.ts +53 -0
- package/dist/tools/web-browser.js +373 -0
- package/dist/tools/web-browser.js.map +1 -0
- package/dist/tools/web-fetch.d.ts +22 -0
- package/dist/tools/web-fetch.js +146 -0
- package/dist/tools/web-fetch.js.map +1 -0
- package/dist/tools/web-search.d.ts +51 -0
- package/dist/tools/web-search.js +198 -0
- package/dist/tools/web-search.js.map +1 -0
- package/dist/tools/which.d.ts +22 -0
- package/dist/tools/which.js +21 -0
- package/dist/tools/which.js.map +1 -0
- package/dist/tools/whoami.d.ts +17 -0
- package/dist/tools/whoami.js +24 -0
- package/dist/tools/whoami.js.map +1 -0
- package/dist/tools/workspace-diagnostics.d.ts +62 -0
- package/dist/tools/workspace-diagnostics.js +421 -0
- package/dist/tools/workspace-diagnostics.js.map +1 -0
- package/dist/tools/workspace.d.ts +14 -0
- package/dist/tools/workspace.js +65 -0
- package/dist/tools/workspace.js.map +1 -0
- package/dist/tools/worktree.d.ts +70 -0
- package/dist/tools/worktree.js +150 -0
- package/dist/tools/worktree.js.map +1 -0
- package/dist/tui/interactions.d.ts +25 -0
- package/dist/tui/interactions.js +232 -0
- package/dist/tui/interactions.js.map +1 -0
- package/dist/tui/multiline-input.d.ts +14 -0
- package/dist/tui/multiline-input.js +550 -0
- package/dist/tui/multiline-input.js.map +1 -0
- package/dist/tui/paste.d.ts +40 -0
- package/dist/tui/paste.js +113 -0
- package/dist/tui/paste.js.map +1 -0
- package/dist/tui/prompt-reader.d.ts +26 -0
- package/dist/tui/prompt-reader.js +553 -0
- package/dist/tui/prompt-reader.js.map +1 -0
- package/dist/tui/transcript.d.ts +34 -0
- package/dist/tui/transcript.js +275 -0
- package/dist/tui/transcript.js.map +1 -0
- package/dist/tui.d.ts +53 -0
- package/dist/tui.js +669 -0
- package/dist/tui.js.map +1 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +2 -0
- package/dist/version.js.map +1 -0
- package/dist/vim/lineEditor.d.ts +18 -0
- package/dist/vim/lineEditor.js +420 -0
- package/dist/vim/lineEditor.js.map +1 -0
- package/dist/web/panel-html.d.ts +1 -0
- package/dist/web/panel-html.js +532 -0
- package/dist/web/panel-html.js.map +1 -0
- package/dist/web/panel.d.ts +4 -0
- package/dist/web/panel.js +121 -0
- package/dist/web/panel.js.map +1 -0
- package/package.json +42 -0
|
@@ -0,0 +1,938 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { parsePromptIntoParts, textMessage } from "../providers/ir.js";
|
|
3
|
+
import { runAgentQuery } from "./query.js";
|
|
4
|
+
import { executeHooks } from "../hooks/runner.js";
|
|
5
|
+
import { compactSessionWithHooks } from "../context/compaction.js";
|
|
6
|
+
import { computeSessionContextBudget } from "../context/token-budget.js";
|
|
7
|
+
import { buildLayeredContext } from "../context/layers.js";
|
|
8
|
+
import { interactionErrorStatus } from "../interactions.js";
|
|
9
|
+
import { appendMemory, extractExplicitMemoryWrite } from "../memory.js";
|
|
10
|
+
import { selectRelevantMemories } from "../memory-selection.js";
|
|
11
|
+
import { buildSystemInstructions } from "./system-prompt.js";
|
|
12
|
+
import { getBuiltinToolDefinitions } from "../tools/registry.js";
|
|
13
|
+
import { searchMemdir, readMemdirIndex } from "../memdir.js";
|
|
14
|
+
import { formatGoalContext, getGoal } from "../goal.js";
|
|
15
|
+
export class QueryEngine {
|
|
16
|
+
input;
|
|
17
|
+
toolUses = new Map();
|
|
18
|
+
constructor(input) {
|
|
19
|
+
this.input = input;
|
|
20
|
+
}
|
|
21
|
+
async submitMessage(prompt) {
|
|
22
|
+
const jobId = this.input.jobId ?? randomUUID();
|
|
23
|
+
const events = [];
|
|
24
|
+
const currentUserMessageId = this.input.store.appendMessage({
|
|
25
|
+
sessionId: this.input.sessionId,
|
|
26
|
+
role: "user",
|
|
27
|
+
content: prompt,
|
|
28
|
+
metadata: { source: "query-engine" }
|
|
29
|
+
});
|
|
30
|
+
this.input.store.recordJob({
|
|
31
|
+
id: jobId,
|
|
32
|
+
sessionId: this.input.sessionId,
|
|
33
|
+
kind: "agent.query",
|
|
34
|
+
status: "running",
|
|
35
|
+
metadata: {
|
|
36
|
+
provider: this.input.routes[0]?.providerName,
|
|
37
|
+
model: this.input.routes[0]?.model
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
this.input.store.recordAudit({
|
|
41
|
+
sessionId: this.input.sessionId,
|
|
42
|
+
jobId,
|
|
43
|
+
action: "agent.query.started",
|
|
44
|
+
target: this.input.routes[0]?.providerName,
|
|
45
|
+
metadata: { routeCount: this.input.routes.length }
|
|
46
|
+
});
|
|
47
|
+
this.input.activeInteractions?.registerJob({ sessionId: this.input.sessionId, jobId });
|
|
48
|
+
const memoryWrite = await this.handleExplicitMemoryWrite(prompt, jobId);
|
|
49
|
+
events.push(...memoryWrite);
|
|
50
|
+
const promptHooks = await this.executeSessionHooks("user_prompt_submit", jobId, {
|
|
51
|
+
source: "query",
|
|
52
|
+
provider: this.input.routes[0]?.providerName,
|
|
53
|
+
model: this.input.routes[0]?.model,
|
|
54
|
+
prompt
|
|
55
|
+
});
|
|
56
|
+
events.push(...promptHooks);
|
|
57
|
+
const startHooks = await this.executeSessionHooks("session_start", jobId, {
|
|
58
|
+
source: "query",
|
|
59
|
+
provider: this.input.routes[0]?.providerName,
|
|
60
|
+
model: this.input.routes[0]?.model
|
|
61
|
+
});
|
|
62
|
+
events.push(...startHooks);
|
|
63
|
+
const preparedContext = await this.prepareContext(prompt, jobId, currentUserMessageId);
|
|
64
|
+
events.push(...preparedContext.events);
|
|
65
|
+
const iterator = runAgentQuery({
|
|
66
|
+
routes: this.input.routes,
|
|
67
|
+
messages: preparedContext.messages,
|
|
68
|
+
cwd: this.input.cwd,
|
|
69
|
+
env: this.input.env,
|
|
70
|
+
stateRoot: this.input.stateRoot,
|
|
71
|
+
webSearchConfig: this.input.webSearchConfig,
|
|
72
|
+
permissionMode: this.input.permissionMode,
|
|
73
|
+
approvalResolver: (request) => this.resolveApproval(jobId, request),
|
|
74
|
+
userQuestionResolver: (request) => this.resolveUserQuestion(jobId, request),
|
|
75
|
+
userMessageSink: this.input.userMessageSink,
|
|
76
|
+
spawnSubAgent: this.input.spawnSubAgent,
|
|
77
|
+
hooks: this.input.hooks,
|
|
78
|
+
sessionId: this.input.sessionId,
|
|
79
|
+
signal: this.input.signal,
|
|
80
|
+
mcp: this.input.mcp ? {
|
|
81
|
+
servers: this.input.mcp.servers,
|
|
82
|
+
tokenLookup: (serverName) => this.input.store.getMcpOAuthToken(serverName)?.accessToken,
|
|
83
|
+
tokenRefresh: async (serverName) => {
|
|
84
|
+
try {
|
|
85
|
+
const { refreshStoredToken } = await import("../mcp/oauth-flow.js");
|
|
86
|
+
return await refreshStoredToken({ serverName, store: this.input.store });
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
} : undefined,
|
|
93
|
+
onStreamEvent: this.input.onStreamEvent
|
|
94
|
+
});
|
|
95
|
+
let final;
|
|
96
|
+
try {
|
|
97
|
+
let next = await iterator.next();
|
|
98
|
+
while (!next.done) {
|
|
99
|
+
events.push(next.value);
|
|
100
|
+
events.push(...await this.persistEvent(jobId, next.value));
|
|
101
|
+
next = await iterator.next();
|
|
102
|
+
}
|
|
103
|
+
final = next.value;
|
|
104
|
+
this.input.store.appendMessage({
|
|
105
|
+
sessionId: this.input.sessionId,
|
|
106
|
+
role: "assistant",
|
|
107
|
+
content: final.text,
|
|
108
|
+
metadata: {
|
|
109
|
+
provider: final.providerName,
|
|
110
|
+
model: final.model,
|
|
111
|
+
turns: final.turns,
|
|
112
|
+
attempts: final.attempts
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
this.input.store.updateJobStatus({
|
|
116
|
+
id: jobId,
|
|
117
|
+
status: "completed",
|
|
118
|
+
metadata: {
|
|
119
|
+
provider: final.providerName,
|
|
120
|
+
model: final.model,
|
|
121
|
+
turns: final.turns,
|
|
122
|
+
attempts: final.attempts
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
this.input.store.recordUsage({
|
|
126
|
+
sessionId: this.input.sessionId,
|
|
127
|
+
provider: final.providerName,
|
|
128
|
+
model: final.model,
|
|
129
|
+
inputTokens: final.usage.inputTokens,
|
|
130
|
+
outputTokens: final.usage.outputTokens,
|
|
131
|
+
costUsd: 0
|
|
132
|
+
});
|
|
133
|
+
this.input.store.recordAudit({
|
|
134
|
+
sessionId: this.input.sessionId,
|
|
135
|
+
jobId,
|
|
136
|
+
action: "agent.query.completed",
|
|
137
|
+
target: final.providerName,
|
|
138
|
+
metadata: { turns: final.turns, attempts: final.attempts }
|
|
139
|
+
});
|
|
140
|
+
const endHooks = await this.executeSessionHooks("session_end", jobId, {
|
|
141
|
+
source: "query",
|
|
142
|
+
provider: final.providerName,
|
|
143
|
+
model: final.model,
|
|
144
|
+
lastAssistantMessage: final.text
|
|
145
|
+
});
|
|
146
|
+
events.push(...endHooks);
|
|
147
|
+
return { ...final, jobId, events };
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
const cancelled = isAbortError(error) || this.input.signal?.aborted === true;
|
|
151
|
+
this.input.store.updateJobStatus({
|
|
152
|
+
id: jobId,
|
|
153
|
+
status: cancelled ? "cancelled" : "failed",
|
|
154
|
+
metadata: { error: error instanceof Error ? error.message : String(error) }
|
|
155
|
+
});
|
|
156
|
+
if (!cancelled || !events.some((event) => event.type === "cancelled")) {
|
|
157
|
+
this.input.store.recordAudit({
|
|
158
|
+
sessionId: this.input.sessionId,
|
|
159
|
+
jobId,
|
|
160
|
+
action: cancelled ? "agent.query.cancelled" : "agent.query.failed",
|
|
161
|
+
metadata: {
|
|
162
|
+
error: error instanceof Error ? error.message : String(error),
|
|
163
|
+
reason: this.input.signal?.reason ? String(this.input.signal.reason) : undefined
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
await this.executeSessionHooks("session_end", jobId, {
|
|
168
|
+
source: "query",
|
|
169
|
+
provider: this.input.routes[0]?.providerName,
|
|
170
|
+
model: this.input.routes[0]?.model,
|
|
171
|
+
error: error instanceof Error ? error.message : String(error)
|
|
172
|
+
});
|
|
173
|
+
throw error;
|
|
174
|
+
}
|
|
175
|
+
finally {
|
|
176
|
+
this.input.activeInteractions?.unregisterJob(jobId);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
async resolveApproval(jobId, request) {
|
|
180
|
+
if (!this.input.activeInteractions) {
|
|
181
|
+
return this.input.approvalResolver?.(request) ?? false;
|
|
182
|
+
}
|
|
183
|
+
const wait = this.input.activeInteractions.waitForApproval({
|
|
184
|
+
sessionId: this.input.sessionId,
|
|
185
|
+
jobId,
|
|
186
|
+
toolUse: request.toolUse,
|
|
187
|
+
reason: request.reason,
|
|
188
|
+
timeoutMs: this.input.interactionTimeoutMs
|
|
189
|
+
});
|
|
190
|
+
const cleanupAbort = this.cancelInteractionOnAbort({
|
|
191
|
+
jobId,
|
|
192
|
+
toolUseId: request.toolUse.id,
|
|
193
|
+
reason: "request aborted"
|
|
194
|
+
});
|
|
195
|
+
const pending = this.input.activeInteractions.getInteraction({ jobId, toolUseId: request.toolUse.id });
|
|
196
|
+
this.input.store.recordAudit({
|
|
197
|
+
sessionId: this.input.sessionId,
|
|
198
|
+
jobId,
|
|
199
|
+
action: "agent.approval.pending",
|
|
200
|
+
target: request.toolUse.name,
|
|
201
|
+
metadata: {
|
|
202
|
+
status: "pending",
|
|
203
|
+
interactionKind: "approval",
|
|
204
|
+
toolUseId: request.toolUse.id,
|
|
205
|
+
toolUse: request.toolUse,
|
|
206
|
+
reason: request.reason,
|
|
207
|
+
diff: request.diff,
|
|
208
|
+
timeoutAt: pending?.timeoutAt
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
try {
|
|
212
|
+
const approved = await wait;
|
|
213
|
+
const resolved = this.input.activeInteractions.getInteraction({ jobId, toolUseId: request.toolUse.id });
|
|
214
|
+
this.input.store.recordAudit({
|
|
215
|
+
sessionId: this.input.sessionId,
|
|
216
|
+
jobId,
|
|
217
|
+
action: "agent.approval.resolved",
|
|
218
|
+
target: request.toolUse.name,
|
|
219
|
+
metadata: {
|
|
220
|
+
status: "resolved",
|
|
221
|
+
interactionKind: "approval",
|
|
222
|
+
toolUseId: request.toolUse.id,
|
|
223
|
+
approved,
|
|
224
|
+
resolvedAt: resolved?.updatedAt
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
return approved;
|
|
228
|
+
}
|
|
229
|
+
catch (error) {
|
|
230
|
+
const status = interactionErrorStatus(error);
|
|
231
|
+
if (status) {
|
|
232
|
+
const current = this.input.activeInteractions.getInteraction({ jobId, toolUseId: request.toolUse.id });
|
|
233
|
+
this.input.store.recordAudit({
|
|
234
|
+
sessionId: this.input.sessionId,
|
|
235
|
+
jobId,
|
|
236
|
+
action: status === "timeout" ? "agent.approval.timeout" : "agent.approval.cancelled",
|
|
237
|
+
target: request.toolUse.name,
|
|
238
|
+
metadata: {
|
|
239
|
+
status,
|
|
240
|
+
interactionKind: "approval",
|
|
241
|
+
toolUseId: request.toolUse.id,
|
|
242
|
+
reason: current?.cancelReason ?? request.reason,
|
|
243
|
+
error: error instanceof Error ? error.message : String(error)
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
throw error;
|
|
248
|
+
}
|
|
249
|
+
finally {
|
|
250
|
+
cleanupAbort();
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
async resolveUserQuestion(jobId, request) {
|
|
254
|
+
if (!this.input.activeInteractions) {
|
|
255
|
+
if (!this.input.userQuestionResolver) {
|
|
256
|
+
throw new Error("AskUserQuestion requires an interactive user question resolver");
|
|
257
|
+
}
|
|
258
|
+
return await this.input.userQuestionResolver(request);
|
|
259
|
+
}
|
|
260
|
+
const wait = this.input.activeInteractions.waitForQuestion({
|
|
261
|
+
sessionId: this.input.sessionId,
|
|
262
|
+
jobId,
|
|
263
|
+
toolUse: request.toolUse,
|
|
264
|
+
question: request.question,
|
|
265
|
+
timeoutMs: this.input.interactionTimeoutMs
|
|
266
|
+
});
|
|
267
|
+
const cleanupAbort = this.cancelInteractionOnAbort({
|
|
268
|
+
jobId,
|
|
269
|
+
toolUseId: request.toolUse.id,
|
|
270
|
+
reason: "request aborted"
|
|
271
|
+
});
|
|
272
|
+
const pending = this.input.activeInteractions.getInteraction({ jobId, toolUseId: request.toolUse.id });
|
|
273
|
+
this.input.store.recordAudit({
|
|
274
|
+
sessionId: this.input.sessionId,
|
|
275
|
+
jobId,
|
|
276
|
+
action: "agent.user_question.pending",
|
|
277
|
+
target: request.toolUse.name,
|
|
278
|
+
metadata: {
|
|
279
|
+
status: "pending",
|
|
280
|
+
interactionKind: "question",
|
|
281
|
+
toolUseId: request.toolUse.id,
|
|
282
|
+
toolUse: request.toolUse,
|
|
283
|
+
questionCount: request.question.questions.length,
|
|
284
|
+
question: request.question,
|
|
285
|
+
timeoutAt: pending?.timeoutAt
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
try {
|
|
289
|
+
const answer = await wait;
|
|
290
|
+
const resolved = this.input.activeInteractions.getInteraction({ jobId, toolUseId: request.toolUse.id });
|
|
291
|
+
this.input.store.recordAudit({
|
|
292
|
+
sessionId: this.input.sessionId,
|
|
293
|
+
jobId,
|
|
294
|
+
action: "agent.user_question.resolved",
|
|
295
|
+
target: request.toolUse.name,
|
|
296
|
+
metadata: {
|
|
297
|
+
status: "resolved",
|
|
298
|
+
interactionKind: "question",
|
|
299
|
+
toolUseId: request.toolUse.id,
|
|
300
|
+
questionCount: request.question.questions.length,
|
|
301
|
+
answer,
|
|
302
|
+
resolvedAt: resolved?.updatedAt
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
return answer;
|
|
306
|
+
}
|
|
307
|
+
catch (error) {
|
|
308
|
+
const status = interactionErrorStatus(error);
|
|
309
|
+
if (status) {
|
|
310
|
+
const current = this.input.activeInteractions.getInteraction({ jobId, toolUseId: request.toolUse.id });
|
|
311
|
+
this.input.store.recordAudit({
|
|
312
|
+
sessionId: this.input.sessionId,
|
|
313
|
+
jobId,
|
|
314
|
+
action: status === "timeout" ? "agent.user_question.timeout" : "agent.user_question.cancelled",
|
|
315
|
+
target: request.toolUse.name,
|
|
316
|
+
metadata: {
|
|
317
|
+
status,
|
|
318
|
+
interactionKind: "question",
|
|
319
|
+
toolUseId: request.toolUse.id,
|
|
320
|
+
questionCount: request.question.questions.length,
|
|
321
|
+
reason: current?.cancelReason,
|
|
322
|
+
error: error instanceof Error ? error.message : String(error)
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
throw error;
|
|
327
|
+
}
|
|
328
|
+
finally {
|
|
329
|
+
cleanupAbort();
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
cancelInteractionOnAbort(input) {
|
|
333
|
+
const signal = this.input.signal;
|
|
334
|
+
if (!signal) {
|
|
335
|
+
return () => undefined;
|
|
336
|
+
}
|
|
337
|
+
const cancel = () => {
|
|
338
|
+
try {
|
|
339
|
+
this.input.activeInteractions?.cancelInteraction(input);
|
|
340
|
+
}
|
|
341
|
+
catch {
|
|
342
|
+
// The interaction may have already resolved or timed out.
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
if (signal.aborted) {
|
|
346
|
+
cancel();
|
|
347
|
+
return () => undefined;
|
|
348
|
+
}
|
|
349
|
+
signal.addEventListener("abort", cancel, { once: true });
|
|
350
|
+
return () => signal.removeEventListener("abort", cancel);
|
|
351
|
+
}
|
|
352
|
+
async executeSessionHooks(event, jobId, context, extraContext) {
|
|
353
|
+
const results = await executeHooks({
|
|
354
|
+
event,
|
|
355
|
+
hooks: this.input.hooks ?? [],
|
|
356
|
+
env: this.input.env,
|
|
357
|
+
context: {
|
|
358
|
+
sessionId: this.input.sessionId,
|
|
359
|
+
jobId,
|
|
360
|
+
cwd: this.input.cwd,
|
|
361
|
+
permissionMode: this.input.permissionMode,
|
|
362
|
+
...context,
|
|
363
|
+
...extraContext
|
|
364
|
+
},
|
|
365
|
+
promptModel: async ({ model, messages }) => {
|
|
366
|
+
const route = this.input.routes[0];
|
|
367
|
+
const response = await route.adapter.complete({ model, messages });
|
|
368
|
+
return { text: response.text };
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
const events = results.map((result) => ({
|
|
372
|
+
type: "hook_result",
|
|
373
|
+
event,
|
|
374
|
+
result
|
|
375
|
+
}));
|
|
376
|
+
for (const hookEvent of events) {
|
|
377
|
+
await this.persistEvent(jobId, hookEvent);
|
|
378
|
+
}
|
|
379
|
+
return events;
|
|
380
|
+
}
|
|
381
|
+
async prepareContext(prompt, jobId, currentUserMessageId) {
|
|
382
|
+
const events = [];
|
|
383
|
+
const session = this.input.store.getSession(this.input.sessionId);
|
|
384
|
+
if (!session) {
|
|
385
|
+
return { messages: [textMessage("user", prompt)], events };
|
|
386
|
+
}
|
|
387
|
+
const summaries = this.input.store.listContextSummaries(session.id);
|
|
388
|
+
const budget = computeSessionContextBudget({ session, summaries });
|
|
389
|
+
const tokenThreshold = this.input.contextOptions?.autoCompactTokenThreshold;
|
|
390
|
+
const messageThreshold = this.input.contextOptions?.autoCompactMessageThreshold;
|
|
391
|
+
// Count messages NOT yet covered by an existing summary so we don't
|
|
392
|
+
// re-trigger compaction immediately after a recent compact.
|
|
393
|
+
const lastSummary = summaries[summaries.length - 1];
|
|
394
|
+
const messagesSinceCompact = lastSummary
|
|
395
|
+
? Math.max(0, session.messages.length - lastSummary.sourceMessageCount)
|
|
396
|
+
: session.messages.length;
|
|
397
|
+
const tokenTriggered = tokenThreshold !== undefined && budget.estimatedTokens > tokenThreshold;
|
|
398
|
+
const messageTriggered = messageThreshold !== undefined && messagesSinceCompact > messageThreshold;
|
|
399
|
+
if (tokenTriggered || messageTriggered) {
|
|
400
|
+
const route = this.input.contextOptions?.compactionRoute ?? this.input.routes[0];
|
|
401
|
+
const compactModel = this.input.contextOptions?.compactionRoute?.model ?? this.input.contextOptions?.compactionModel;
|
|
402
|
+
const compacted = await compactSessionWithHooks({
|
|
403
|
+
store: this.input.store,
|
|
404
|
+
sessionId: session.id,
|
|
405
|
+
hooks: this.input.hooks ?? [],
|
|
406
|
+
cwd: this.input.cwd,
|
|
407
|
+
env: this.input.env,
|
|
408
|
+
trigger: "auto",
|
|
409
|
+
modelRunner: compactModel
|
|
410
|
+
? {
|
|
411
|
+
adapter: route.adapter,
|
|
412
|
+
providerName: route.providerName,
|
|
413
|
+
model: compactModel
|
|
414
|
+
}
|
|
415
|
+
: undefined
|
|
416
|
+
});
|
|
417
|
+
const compactEvent = {
|
|
418
|
+
type: "compact_boundary",
|
|
419
|
+
summaryId: compacted.summary.id,
|
|
420
|
+
sourceMessageCount: compacted.summary.sourceMessageCount,
|
|
421
|
+
estimatedTokensBefore: budget.estimatedTokens
|
|
422
|
+
};
|
|
423
|
+
events.push(compactEvent);
|
|
424
|
+
await this.persistEvent(jobId, compactEvent);
|
|
425
|
+
}
|
|
426
|
+
return {
|
|
427
|
+
messages: buildSessionMessages({
|
|
428
|
+
store: this.input.store,
|
|
429
|
+
sessionId: session.id,
|
|
430
|
+
prompt,
|
|
431
|
+
currentUserMessageId,
|
|
432
|
+
recentMessages: this.input.contextOptions?.recentMessages ?? 20,
|
|
433
|
+
memoryContext: await this.buildMemoryContext(prompt, jobId),
|
|
434
|
+
goalContext: this.input.memoryOptions?.paths ? formatGoalContext(getGoal(this.input.memoryOptions.paths, session.id)) : undefined,
|
|
435
|
+
cwd: this.input.cwd,
|
|
436
|
+
paths: this.input.memoryOptions?.paths
|
|
437
|
+
}),
|
|
438
|
+
events
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
async handleExplicitMemoryWrite(prompt, jobId) {
|
|
442
|
+
const memory = this.input.memoryOptions;
|
|
443
|
+
if (!memory?.paths || memory.enabled === false || memory.autoWrite === "off") {
|
|
444
|
+
return [];
|
|
445
|
+
}
|
|
446
|
+
const write = extractExplicitMemoryWrite(prompt);
|
|
447
|
+
if (!write) {
|
|
448
|
+
return [];
|
|
449
|
+
}
|
|
450
|
+
const result = appendMemory({
|
|
451
|
+
paths: memory.paths,
|
|
452
|
+
scope: write.scope,
|
|
453
|
+
cwd: this.input.cwd,
|
|
454
|
+
sessionId: this.input.sessionId,
|
|
455
|
+
text: write.text,
|
|
456
|
+
store: this.input.store,
|
|
457
|
+
detailed: true
|
|
458
|
+
});
|
|
459
|
+
this.input.store.recordAudit({
|
|
460
|
+
sessionId: this.input.sessionId,
|
|
461
|
+
jobId,
|
|
462
|
+
action: result.appended ? "agent.memory.written" : result.conflicts.length > 0 ? "agent.memory.conflict" : "agent.memory.duplicate",
|
|
463
|
+
target: result.file,
|
|
464
|
+
metadata: {
|
|
465
|
+
scope: write.scope,
|
|
466
|
+
appended: result.appended,
|
|
467
|
+
duplicate: result.duplicate,
|
|
468
|
+
conflictCount: result.conflicts.length,
|
|
469
|
+
key: result.entry.key
|
|
470
|
+
}
|
|
471
|
+
});
|
|
472
|
+
return [];
|
|
473
|
+
}
|
|
474
|
+
async buildMemoryContext(prompt, jobId) {
|
|
475
|
+
const memory = this.input.memoryOptions;
|
|
476
|
+
if (!memory?.paths || memory.enabled === false) {
|
|
477
|
+
return undefined;
|
|
478
|
+
}
|
|
479
|
+
const sections = [];
|
|
480
|
+
// 1. Memdir index — always loaded if present
|
|
481
|
+
const memdirIndex = readMemdirIndex(memory.paths);
|
|
482
|
+
if (memdirIndex.trim()) {
|
|
483
|
+
sections.push(memdirIndex.trim());
|
|
484
|
+
}
|
|
485
|
+
// 2. Memdir relevant entries — top matches
|
|
486
|
+
const memdirMatches = searchMemdir({
|
|
487
|
+
paths: memory.paths,
|
|
488
|
+
query: prompt,
|
|
489
|
+
maxResults: 5
|
|
490
|
+
});
|
|
491
|
+
if (memdirMatches.length > 0) {
|
|
492
|
+
const lines = ["[Relevant memdir entries]"];
|
|
493
|
+
for (const entry of memdirMatches) {
|
|
494
|
+
lines.push(`## ${entry.name} (${entry.type})`);
|
|
495
|
+
lines.push(entry.description);
|
|
496
|
+
if (entry.body) {
|
|
497
|
+
const summary = entry.body.length > 600 ? entry.body.slice(0, 600) + "..." : entry.body;
|
|
498
|
+
lines.push(summary);
|
|
499
|
+
}
|
|
500
|
+
lines.push("");
|
|
501
|
+
}
|
|
502
|
+
sections.push(lines.join("\n").trim());
|
|
503
|
+
}
|
|
504
|
+
// 3. Legacy memory.ts entries — for backwards compatibility
|
|
505
|
+
const result = await selectRelevantMemories({
|
|
506
|
+
paths: memory.paths,
|
|
507
|
+
cwd: this.input.cwd,
|
|
508
|
+
sessionId: this.input.sessionId,
|
|
509
|
+
scopes: memory.scopes,
|
|
510
|
+
maxResults: memory.maxResults ?? 5,
|
|
511
|
+
prompt,
|
|
512
|
+
selectionRoute: memory.selectionRoute,
|
|
513
|
+
signal: this.input.signal
|
|
514
|
+
});
|
|
515
|
+
if (result.entries.length === 0) {
|
|
516
|
+
// Legacy memory empty, but memdir sections may have content
|
|
517
|
+
if (sections.length === 0)
|
|
518
|
+
return undefined;
|
|
519
|
+
return sections.join("\n\n");
|
|
520
|
+
}
|
|
521
|
+
this.input.store.recordAudit({
|
|
522
|
+
sessionId: this.input.sessionId,
|
|
523
|
+
jobId,
|
|
524
|
+
action: "agent.memory.retrieved",
|
|
525
|
+
target: this.input.sessionId,
|
|
526
|
+
metadata: {
|
|
527
|
+
resultCount: result.entries.length,
|
|
528
|
+
method: result.method,
|
|
529
|
+
scopes: Array.from(new Set(result.entries.map((entry) => entry.scope))),
|
|
530
|
+
keys: result.entries.map((entry) => entry.key).filter((key) => Boolean(key))
|
|
531
|
+
}
|
|
532
|
+
});
|
|
533
|
+
if (result.formatted) {
|
|
534
|
+
sections.push(result.formatted);
|
|
535
|
+
}
|
|
536
|
+
return sections.join("\n\n");
|
|
537
|
+
}
|
|
538
|
+
async persistEvent(jobId, event) {
|
|
539
|
+
if (event.type === "tool_use") {
|
|
540
|
+
this.toolUses.set(event.toolUse.id, event.toolUse);
|
|
541
|
+
this.input.store.recordAudit({
|
|
542
|
+
sessionId: this.input.sessionId,
|
|
543
|
+
jobId,
|
|
544
|
+
action: "agent.tool.use",
|
|
545
|
+
target: event.toolUse.name,
|
|
546
|
+
metadata: { id: event.toolUse.id, input: event.toolUse.input }
|
|
547
|
+
});
|
|
548
|
+
return [];
|
|
549
|
+
}
|
|
550
|
+
if (event.type === "request_start") {
|
|
551
|
+
this.input.store.recordAudit({
|
|
552
|
+
sessionId: this.input.sessionId,
|
|
553
|
+
jobId,
|
|
554
|
+
action: "agent.request.started",
|
|
555
|
+
target: this.input.routes[0]?.providerName,
|
|
556
|
+
metadata: {
|
|
557
|
+
provider: this.input.routes[0]?.providerName,
|
|
558
|
+
model: this.input.routes[0]?.model
|
|
559
|
+
}
|
|
560
|
+
});
|
|
561
|
+
return [];
|
|
562
|
+
}
|
|
563
|
+
if (event.type === "text_delta") {
|
|
564
|
+
this.input.store.recordAudit({
|
|
565
|
+
sessionId: this.input.sessionId,
|
|
566
|
+
jobId,
|
|
567
|
+
action: "agent.text.delta",
|
|
568
|
+
target: this.input.routes[0]?.providerName,
|
|
569
|
+
metadata: {
|
|
570
|
+
length: event.text.length,
|
|
571
|
+
preview: event.text.slice(0, 240)
|
|
572
|
+
}
|
|
573
|
+
});
|
|
574
|
+
return [];
|
|
575
|
+
}
|
|
576
|
+
if (event.type === "assistant_message") {
|
|
577
|
+
this.input.store.recordAudit({
|
|
578
|
+
sessionId: this.input.sessionId,
|
|
579
|
+
jobId,
|
|
580
|
+
action: "agent.assistant.message",
|
|
581
|
+
target: this.input.routes[0]?.providerName,
|
|
582
|
+
metadata: {
|
|
583
|
+
partCount: event.message.content.length,
|
|
584
|
+
textLength: event.message.content
|
|
585
|
+
.filter((part) => part.type === "text")
|
|
586
|
+
.reduce((sum, part) => sum + part.text.length, 0),
|
|
587
|
+
toolUseCount: event.message.content.filter((part) => part.type === "tool-use").length
|
|
588
|
+
}
|
|
589
|
+
});
|
|
590
|
+
return [];
|
|
591
|
+
}
|
|
592
|
+
if (event.type === "tool_result") {
|
|
593
|
+
this.input.store.appendMessage({
|
|
594
|
+
sessionId: this.input.sessionId,
|
|
595
|
+
role: "tool",
|
|
596
|
+
content: event.content,
|
|
597
|
+
metadata: {
|
|
598
|
+
toolCallId: event.toolCallId,
|
|
599
|
+
toolName: event.toolName,
|
|
600
|
+
isError: event.isError,
|
|
601
|
+
retryable: event.retryable
|
|
602
|
+
}
|
|
603
|
+
});
|
|
604
|
+
this.input.store.recordAudit({
|
|
605
|
+
sessionId: this.input.sessionId,
|
|
606
|
+
jobId,
|
|
607
|
+
action: event.isError ? "agent.tool.failed" : "agent.tool.completed",
|
|
608
|
+
target: event.toolName,
|
|
609
|
+
metadata: { toolCallId: event.toolCallId }
|
|
610
|
+
});
|
|
611
|
+
const toolUse = this.toolUses.get(event.toolCallId);
|
|
612
|
+
const extraEvents = [];
|
|
613
|
+
if (event.isError && event.content.startsWith("Permission ")) {
|
|
614
|
+
this.input.store.recordAudit({
|
|
615
|
+
sessionId: this.input.sessionId,
|
|
616
|
+
jobId,
|
|
617
|
+
action: "agent.permission.denied",
|
|
618
|
+
target: event.toolName,
|
|
619
|
+
metadata: { toolCallId: event.toolCallId, reason: event.content }
|
|
620
|
+
});
|
|
621
|
+
extraEvents.push(...await this.executeSessionHooks("permission_denied", jobId, {
|
|
622
|
+
source: "query",
|
|
623
|
+
provider: this.input.routes[0]?.providerName,
|
|
624
|
+
model: this.input.routes[0]?.model,
|
|
625
|
+
error: event.content
|
|
626
|
+
}, {
|
|
627
|
+
toolName: event.toolName,
|
|
628
|
+
toolInput: toolUse?.input,
|
|
629
|
+
toolUseId: event.toolCallId,
|
|
630
|
+
reason: event.content
|
|
631
|
+
}));
|
|
632
|
+
}
|
|
633
|
+
if (event.toolName === "TodoWrite" && !event.isError) {
|
|
634
|
+
this.input.store.recordAudit({
|
|
635
|
+
sessionId: this.input.sessionId,
|
|
636
|
+
jobId,
|
|
637
|
+
action: "agent.todo.updated",
|
|
638
|
+
target: this.input.sessionId,
|
|
639
|
+
metadata: buildTodoAuditMetadata(event.toolCallId, toolUse)
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
if (event.toolName === "Config" && !event.isError) {
|
|
643
|
+
if (toolUse?.input.value !== undefined) {
|
|
644
|
+
this.input.store.recordAudit({
|
|
645
|
+
sessionId: this.input.sessionId,
|
|
646
|
+
jobId,
|
|
647
|
+
action: "agent.config.updated",
|
|
648
|
+
target: typeof toolUse.input.setting === "string" ? toolUse.input.setting : "unknown",
|
|
649
|
+
metadata: {
|
|
650
|
+
toolCallId: event.toolCallId,
|
|
651
|
+
valueType: typeof toolUse.input.value
|
|
652
|
+
}
|
|
653
|
+
});
|
|
654
|
+
extraEvents.push(...await this.executeSessionHooks("config_change", jobId, {
|
|
655
|
+
source: "query",
|
|
656
|
+
provider: this.input.routes[0]?.providerName,
|
|
657
|
+
model: this.input.routes[0]?.model
|
|
658
|
+
}, {
|
|
659
|
+
toolName: event.toolName,
|
|
660
|
+
toolInput: toolUse.input,
|
|
661
|
+
toolUseId: event.toolCallId,
|
|
662
|
+
filePath: this.input.stateRoot ? `${this.input.stateRoot}/../config.yaml` : undefined
|
|
663
|
+
}));
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
if (event.toolName === "Skill" && !event.isError) {
|
|
667
|
+
if (typeof toolUse?.input.skill === "string") {
|
|
668
|
+
this.input.store.recordAudit({
|
|
669
|
+
sessionId: this.input.sessionId,
|
|
670
|
+
jobId,
|
|
671
|
+
action: "agent.skill.loaded",
|
|
672
|
+
target: toolUse.input.skill,
|
|
673
|
+
metadata: {
|
|
674
|
+
toolCallId: event.toolCallId,
|
|
675
|
+
argsProvided: typeof toolUse.input.args === "string"
|
|
676
|
+
}
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
return extraEvents;
|
|
681
|
+
}
|
|
682
|
+
if (event.type === "fallback_switched") {
|
|
683
|
+
this.input.store.recordAudit({
|
|
684
|
+
sessionId: this.input.sessionId,
|
|
685
|
+
jobId,
|
|
686
|
+
action: "agent.provider.fallback",
|
|
687
|
+
target: event.toProvider,
|
|
688
|
+
metadata: event
|
|
689
|
+
});
|
|
690
|
+
return await this.executeSessionHooks("notification", jobId, {
|
|
691
|
+
source: "query",
|
|
692
|
+
provider: event.toProvider,
|
|
693
|
+
model: event.toModel,
|
|
694
|
+
message: `Provider fallback switched from ${event.fromProvider} to ${event.toProvider}`,
|
|
695
|
+
title: "Provider fallback",
|
|
696
|
+
notificationType: "provider_fallback"
|
|
697
|
+
});
|
|
698
|
+
}
|
|
699
|
+
if (event.type === "approval_request") {
|
|
700
|
+
this.input.store.recordAudit({
|
|
701
|
+
sessionId: this.input.sessionId,
|
|
702
|
+
jobId,
|
|
703
|
+
action: "agent.approval.requested",
|
|
704
|
+
target: event.toolUse.name,
|
|
705
|
+
metadata: { toolUse: event.toolUse, reason: event.reason }
|
|
706
|
+
});
|
|
707
|
+
return await this.executeSessionHooks("permission_request", jobId, {
|
|
708
|
+
source: "query",
|
|
709
|
+
provider: this.input.routes[0]?.providerName,
|
|
710
|
+
model: this.input.routes[0]?.model
|
|
711
|
+
}, {
|
|
712
|
+
toolName: event.toolUse.name,
|
|
713
|
+
toolInput: event.toolUse.input,
|
|
714
|
+
toolUseId: event.toolUse.id,
|
|
715
|
+
reason: event.reason
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
if (event.type === "user_question") {
|
|
719
|
+
this.input.store.recordAudit({
|
|
720
|
+
sessionId: this.input.sessionId,
|
|
721
|
+
jobId,
|
|
722
|
+
action: "agent.user_question.answered",
|
|
723
|
+
target: event.toolUse.name,
|
|
724
|
+
metadata: {
|
|
725
|
+
toolUse: event.toolUse,
|
|
726
|
+
questionCount: event.question.questions.length,
|
|
727
|
+
answer: event.answer
|
|
728
|
+
}
|
|
729
|
+
});
|
|
730
|
+
return [];
|
|
731
|
+
}
|
|
732
|
+
if (event.type === "user_message") {
|
|
733
|
+
this.input.store.recordAudit({
|
|
734
|
+
sessionId: this.input.sessionId,
|
|
735
|
+
jobId,
|
|
736
|
+
action: "agent.user_message.sent",
|
|
737
|
+
target: event.toolUse.name,
|
|
738
|
+
metadata: {
|
|
739
|
+
toolUse: event.toolUse,
|
|
740
|
+
message: event.message,
|
|
741
|
+
result: event.result
|
|
742
|
+
}
|
|
743
|
+
});
|
|
744
|
+
return [];
|
|
745
|
+
}
|
|
746
|
+
if (event.type === "hook_result") {
|
|
747
|
+
this.persistHookResult(jobId, event.event, event.result, {
|
|
748
|
+
toolCallId: event.toolCallId,
|
|
749
|
+
toolName: event.toolName
|
|
750
|
+
});
|
|
751
|
+
return [];
|
|
752
|
+
}
|
|
753
|
+
if (event.type === "compact_boundary") {
|
|
754
|
+
this.input.store.recordAudit({
|
|
755
|
+
sessionId: this.input.sessionId,
|
|
756
|
+
jobId,
|
|
757
|
+
action: "agent.context.compacted",
|
|
758
|
+
target: event.summaryId,
|
|
759
|
+
metadata: event
|
|
760
|
+
});
|
|
761
|
+
return [];
|
|
762
|
+
}
|
|
763
|
+
if (event.type === "usage") {
|
|
764
|
+
this.input.store.recordAudit({
|
|
765
|
+
sessionId: this.input.sessionId,
|
|
766
|
+
jobId,
|
|
767
|
+
action: "agent.usage.reported",
|
|
768
|
+
target: this.input.routes[0]?.providerName,
|
|
769
|
+
metadata: {
|
|
770
|
+
inputTokens: event.usage.inputTokens,
|
|
771
|
+
outputTokens: event.usage.outputTokens
|
|
772
|
+
}
|
|
773
|
+
});
|
|
774
|
+
return [];
|
|
775
|
+
}
|
|
776
|
+
if (event.type === "max_turns_reached") {
|
|
777
|
+
this.input.store.recordAudit({
|
|
778
|
+
sessionId: this.input.sessionId,
|
|
779
|
+
jobId,
|
|
780
|
+
action: "agent.query.max_turns",
|
|
781
|
+
metadata: { maxTurnsReached: true }
|
|
782
|
+
});
|
|
783
|
+
return [];
|
|
784
|
+
}
|
|
785
|
+
if (event.type === "cancelled") {
|
|
786
|
+
this.input.store.recordAudit({
|
|
787
|
+
sessionId: this.input.sessionId,
|
|
788
|
+
jobId,
|
|
789
|
+
action: "agent.query.cancelled",
|
|
790
|
+
target: this.input.routes[0]?.providerName,
|
|
791
|
+
metadata: { reason: event.reason }
|
|
792
|
+
});
|
|
793
|
+
return [];
|
|
794
|
+
}
|
|
795
|
+
if (event.type === "done") {
|
|
796
|
+
this.input.store.recordAudit({
|
|
797
|
+
sessionId: this.input.sessionId,
|
|
798
|
+
jobId,
|
|
799
|
+
action: "agent.query.done",
|
|
800
|
+
target: this.input.routes[0]?.providerName,
|
|
801
|
+
metadata: {
|
|
802
|
+
textLength: event.text.length,
|
|
803
|
+
messageCount: event.messages.length
|
|
804
|
+
}
|
|
805
|
+
});
|
|
806
|
+
return [];
|
|
807
|
+
}
|
|
808
|
+
if (event.type === "error") {
|
|
809
|
+
this.input.store.recordAudit({
|
|
810
|
+
sessionId: this.input.sessionId,
|
|
811
|
+
jobId,
|
|
812
|
+
action: "agent.query.error",
|
|
813
|
+
metadata: event
|
|
814
|
+
});
|
|
815
|
+
return [];
|
|
816
|
+
}
|
|
817
|
+
return [];
|
|
818
|
+
}
|
|
819
|
+
persistHookResult(jobId, event, result, metadata) {
|
|
820
|
+
this.input.store.recordAudit({
|
|
821
|
+
sessionId: this.input.sessionId,
|
|
822
|
+
jobId,
|
|
823
|
+
action: result.error ? "agent.hook.failed" : "agent.hook.completed",
|
|
824
|
+
target: `${event}:${result.hook.type}`,
|
|
825
|
+
metadata: {
|
|
826
|
+
event,
|
|
827
|
+
hookType: result.hook.type,
|
|
828
|
+
condition: result.hook.if,
|
|
829
|
+
toolCallId: metadata?.toolCallId,
|
|
830
|
+
toolName: metadata?.toolName,
|
|
831
|
+
exitCode: result.exitCode,
|
|
832
|
+
blocked: result.blocked,
|
|
833
|
+
timedOut: result.timedOut,
|
|
834
|
+
output: result.output,
|
|
835
|
+
error: result.error
|
|
836
|
+
}
|
|
837
|
+
});
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
function isAbortError(error) {
|
|
841
|
+
return error instanceof DOMException && error.name === "AbortError"
|
|
842
|
+
|| error instanceof Error && error.name === "AbortError";
|
|
843
|
+
}
|
|
844
|
+
function buildTodoAuditMetadata(toolCallId, toolUse) {
|
|
845
|
+
const todos = Array.isArray(toolUse?.input.todos) ? toolUse.input.todos : [];
|
|
846
|
+
return {
|
|
847
|
+
toolCallId,
|
|
848
|
+
todoCount: todos.length,
|
|
849
|
+
statusCounts: countTodoStatuses(todos),
|
|
850
|
+
todos
|
|
851
|
+
};
|
|
852
|
+
}
|
|
853
|
+
function countTodoStatuses(todos) {
|
|
854
|
+
const counts = {
|
|
855
|
+
pending: 0,
|
|
856
|
+
in_progress: 0,
|
|
857
|
+
completed: 0
|
|
858
|
+
};
|
|
859
|
+
for (const todo of todos) {
|
|
860
|
+
if (typeof todo !== "object" || todo === null || Array.isArray(todo)) {
|
|
861
|
+
continue;
|
|
862
|
+
}
|
|
863
|
+
const status = todo.status;
|
|
864
|
+
if (status === "pending" || status === "in_progress" || status === "completed") {
|
|
865
|
+
counts[status] += 1;
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
return counts;
|
|
869
|
+
}
|
|
870
|
+
function buildSessionMessages(input) {
|
|
871
|
+
const session = input.store.getSession(input.sessionId);
|
|
872
|
+
if (!session) {
|
|
873
|
+
return [textMessage("user", input.prompt)];
|
|
874
|
+
}
|
|
875
|
+
const messages = [];
|
|
876
|
+
// Build layered system prompt
|
|
877
|
+
const { systemPrompt } = buildLayeredContext({
|
|
878
|
+
cwd: input.cwd ?? session.cwd,
|
|
879
|
+
paths: input.paths,
|
|
880
|
+
systemInstructions: input.systemInstructions ?? buildSystemInstructions({
|
|
881
|
+
cwd: input.cwd ?? session.cwd,
|
|
882
|
+
platform: process.platform,
|
|
883
|
+
toolCount: getBuiltinToolDefinitions().length
|
|
884
|
+
}),
|
|
885
|
+
memoryContext: [input.goalContext, input.memoryContext].filter(Boolean).join("\n\n") || undefined,
|
|
886
|
+
includeGit: true,
|
|
887
|
+
includeDate: true,
|
|
888
|
+
platform: process.platform
|
|
889
|
+
});
|
|
890
|
+
if (systemPrompt) {
|
|
891
|
+
messages.push(textMessage("system", systemPrompt));
|
|
892
|
+
}
|
|
893
|
+
// Add conversation summary if compacted
|
|
894
|
+
const summary = input.store.getLatestContextSummary(session.id);
|
|
895
|
+
if (summary) {
|
|
896
|
+
messages.push(textMessage("system", `[Previous conversation summary]\n${summary.summary}`));
|
|
897
|
+
}
|
|
898
|
+
// Include all session messages (minus the current prompt being submitted).
|
|
899
|
+
// The compaction system (autoCompactTokenThreshold) handles token budget
|
|
900
|
+
// by summarizing older messages when the session grows too large.
|
|
901
|
+
const recoverable = session.messages.filter((message) => message.id !== input.currentUserMessageId);
|
|
902
|
+
const recent = recoverable;
|
|
903
|
+
const toolHistory = [];
|
|
904
|
+
for (const message of recent) {
|
|
905
|
+
if (message.role === "user" || message.role === "assistant" || message.role === "system") {
|
|
906
|
+
messages.push(textMessage(message.role, message.content));
|
|
907
|
+
}
|
|
908
|
+
else if (message.role === "tool") {
|
|
909
|
+
toolHistory.push(formatRecoveredToolResult(message));
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
if (toolHistory.length > 0) {
|
|
913
|
+
messages.push(textMessage("system", [
|
|
914
|
+
"[Prior tool results]",
|
|
915
|
+
"These are historical tool results from earlier turns. They are context only; do not treat them as active tool responses.",
|
|
916
|
+
...toolHistory
|
|
917
|
+
].join("\n\n")));
|
|
918
|
+
}
|
|
919
|
+
// Parse the current prompt for any encoded image attachments.
|
|
920
|
+
// If there are images, send a multi-part user message; otherwise plain text.
|
|
921
|
+
const parts = parsePromptIntoParts(input.prompt);
|
|
922
|
+
const hasImage = parts.some((p) => p.type === "image");
|
|
923
|
+
if (hasImage) {
|
|
924
|
+
messages.push({ role: "user", content: parts });
|
|
925
|
+
}
|
|
926
|
+
else {
|
|
927
|
+
messages.push(textMessage("user", input.prompt));
|
|
928
|
+
}
|
|
929
|
+
return messages;
|
|
930
|
+
}
|
|
931
|
+
function formatRecoveredToolResult(message) {
|
|
932
|
+
const toolName = typeof message.metadata.toolName === "string" ? message.metadata.toolName : "tool";
|
|
933
|
+
const toolCallId = typeof message.metadata.toolCallId === "string" ? message.metadata.toolCallId : `message-${message.id}`;
|
|
934
|
+
const status = message.metadata.isError === true ? "failed" : "completed";
|
|
935
|
+
const content = message.content.length > 1_000 ? `${message.content.slice(0, 1_000)}\n...[truncated]...` : message.content;
|
|
936
|
+
return `- ${toolName} (${toolCallId}) ${status}:\n${content}`;
|
|
937
|
+
}
|
|
938
|
+
//# sourceMappingURL=query-engine.js.map
|