lonny-agent 0.2.6 → 0.2.7
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/.github/workflows/ci.yml +21 -0
- package/AGENTS.md +106 -0
- package/CHANGELOG.md +24 -0
- package/dist/agent/__tests__/session-restore.test.d.ts +2 -0
- package/dist/agent/__tests__/session-restore.test.d.ts.map +1 -0
- package/dist/agent/__tests__/session-restore.test.js +260 -0
- package/dist/agent/__tests__/session-restore.test.js.map +1 -0
- package/dist/agent/commands.d.ts +26 -0
- package/dist/agent/commands.d.ts.map +1 -0
- package/dist/agent/commands.js +253 -0
- package/dist/agent/commands.js.map +1 -0
- package/dist/agent/compaction.js +8 -1
- package/dist/agent/compaction.js.map +1 -1
- package/dist/agent/index.d.ts +2 -1
- package/dist/agent/index.d.ts.map +1 -1
- package/dist/agent/index.js +2 -2
- package/dist/agent/index.js.map +1 -1
- package/dist/agent/memory.d.ts +12 -0
- package/dist/agent/memory.d.ts.map +1 -0
- package/dist/agent/memory.js +51 -0
- package/dist/agent/memory.js.map +1 -0
- package/dist/agent/prompt-builder.d.ts +6 -1
- package/dist/agent/prompt-builder.d.ts.map +1 -1
- package/dist/agent/prompt-builder.js +89 -26
- package/dist/agent/prompt-builder.js.map +1 -1
- package/dist/agent/providers/openai.d.ts +1 -0
- package/dist/agent/providers/openai.d.ts.map +1 -1
- package/dist/agent/providers/openai.js +16 -3
- package/dist/agent/providers/openai.js.map +1 -1
- package/dist/agent/session-chat.d.ts +3 -0
- package/dist/agent/session-chat.d.ts.map +1 -0
- package/dist/agent/session-chat.js +384 -0
- package/dist/agent/session-chat.js.map +1 -0
- package/dist/agent/session-display.d.ts +29 -0
- package/dist/agent/session-display.d.ts.map +1 -0
- package/dist/agent/session-display.js +179 -0
- package/dist/agent/session-display.js.map +1 -0
- package/dist/agent/session-persistence.d.ts +55 -0
- package/dist/agent/session-persistence.d.ts.map +1 -0
- package/dist/agent/session-persistence.js +101 -0
- package/dist/agent/session-persistence.js.map +1 -0
- package/dist/agent/session-utils.d.ts +5 -0
- package/dist/agent/session-utils.d.ts.map +1 -0
- package/dist/agent/session-utils.js +51 -0
- package/dist/agent/session-utils.js.map +1 -0
- package/dist/agent/session.d.ts +15 -17
- package/dist/agent/session.d.ts.map +1 -1
- package/dist/agent/session.js +226 -608
- package/dist/agent/session.js.map +1 -1
- package/dist/cli/index.d.ts +4 -0
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +67 -2
- package/dist/cli/index.js.map +1 -1
- package/dist/config/index.d.ts +2 -2
- package/dist/config/index.d.ts.map +1 -1
- package/dist/index.js +24 -3
- package/dist/index.js.map +1 -1
- package/dist/tools/__tests__/bash.test.js +168 -1
- package/dist/tools/__tests__/bash.test.js.map +1 -1
- package/dist/tools/__tests__/edit.test.js +253 -6
- package/dist/tools/__tests__/edit.test.js.map +1 -1
- package/dist/tools/__tests__/fetch.test.js +1 -1
- package/dist/tools/__tests__/fetch.test.js.map +1 -1
- package/dist/tools/__tests__/glob.test.js +1 -1
- package/dist/tools/__tests__/glob.test.js.map +1 -1
- package/dist/tools/__tests__/grep.test.js +1 -1
- package/dist/tools/__tests__/grep.test.js.map +1 -1
- package/dist/tools/__tests__/ls.test.js +1 -1
- package/dist/tools/__tests__/ls.test.js.map +1 -1
- package/dist/tools/__tests__/read.test.js +1 -1
- package/dist/tools/__tests__/read.test.js.map +1 -1
- package/dist/tools/__tests__/registry.test.js +313 -0
- package/dist/tools/__tests__/registry.test.js.map +1 -1
- package/dist/tools/__tests__/write_plan.test.js +1 -1
- package/dist/tools/__tests__/write_plan.test.js.map +1 -1
- package/dist/tools/bash.d.ts.map +1 -1
- package/dist/tools/bash.js +659 -39
- package/dist/tools/bash.js.map +1 -1
- package/dist/tools/codebase/ast/__tests__/ast-edit-demo.d.ts +2 -0
- package/dist/tools/codebase/ast/__tests__/ast-edit-demo.d.ts.map +1 -0
- package/dist/tools/codebase/ast/__tests__/ast-edit-demo.js +7 -0
- package/dist/tools/codebase/ast/__tests__/ast-edit-demo.js.map +1 -0
- package/dist/tools/codebase/ast/__tests__/ast-edit-final.d.ts +6 -0
- package/dist/tools/codebase/ast/__tests__/ast-edit-final.d.ts.map +1 -0
- package/dist/tools/codebase/ast/__tests__/ast-edit-final.js +11 -0
- package/dist/tools/codebase/ast/__tests__/ast-edit-final.js.map +1 -0
- package/dist/tools/codebase/ast/__tests__/ast-edit-test.d.ts +2 -0
- package/dist/tools/codebase/ast/__tests__/ast-edit-test.d.ts.map +1 -0
- package/dist/tools/codebase/ast/__tests__/ast-edit-test.js +9 -0
- package/dist/tools/codebase/ast/__tests__/ast-edit-test.js.map +1 -0
- package/dist/tools/codebase/ast/__tests__/integration.test.d.ts +2 -0
- package/dist/tools/codebase/ast/__tests__/integration.test.d.ts.map +1 -0
- package/dist/tools/codebase/ast/__tests__/integration.test.js +415 -0
- package/dist/tools/codebase/ast/__tests__/integration.test.js.map +1 -0
- package/dist/tools/codebase/ast/__tests__/tree-sitter.test.d.ts +2 -0
- package/dist/tools/codebase/ast/__tests__/tree-sitter.test.d.ts.map +1 -0
- package/dist/tools/codebase/ast/__tests__/tree-sitter.test.js +414 -0
- package/dist/tools/codebase/ast/__tests__/tree-sitter.test.js.map +1 -0
- package/dist/tools/codebase/ast/adapter.d.ts +23 -0
- package/dist/tools/codebase/ast/adapter.d.ts.map +1 -0
- package/dist/tools/codebase/ast/adapter.js +11 -0
- package/dist/tools/codebase/ast/adapter.js.map +1 -0
- package/dist/tools/codebase/ast/tools.d.ts +3 -0
- package/dist/tools/codebase/ast/tools.d.ts.map +1 -0
- package/dist/tools/codebase/ast/tools.js +286 -0
- package/dist/tools/codebase/ast/tools.js.map +1 -0
- package/dist/tools/codebase/ast/tree-sitter-adapter.d.ts +3 -0
- package/dist/tools/codebase/ast/tree-sitter-adapter.d.ts.map +1 -0
- package/dist/tools/codebase/ast/tree-sitter-adapter.js +412 -0
- package/dist/tools/codebase/ast/tree-sitter-adapter.js.map +1 -0
- package/dist/tools/codebase/ast/tree-sitter-converters.d.ts +4 -0
- package/dist/tools/codebase/ast/tree-sitter-converters.d.ts.map +1 -0
- package/dist/tools/codebase/ast/tree-sitter-converters.js +447 -0
- package/dist/tools/codebase/ast/tree-sitter-converters.js.map +1 -0
- package/dist/tools/codebase/ast/types.d.ts +103 -0
- package/dist/tools/codebase/ast/types.d.ts.map +1 -0
- package/dist/tools/codebase/ast/types.js +2 -0
- package/dist/tools/codebase/ast/types.js.map +1 -0
- package/dist/tools/codebase/ast/wasm/tree-sitter-python.wasm +0 -0
- package/dist/tools/codebase/ast/wasm/tree-sitter-tsx.wasm +0 -0
- package/dist/tools/codebase/ast/wasm/tree-sitter-typescript.wasm +0 -0
- package/dist/tools/codebase/ast/wasm/tree-sitter.wasm +0 -0
- package/dist/tools/codebase/ast/wasm/web-tree-sitter.wasm +0 -0
- package/dist/tools/codebase/find.d.ts +3 -0
- package/dist/tools/codebase/find.d.ts.map +1 -0
- package/dist/tools/codebase/find.js +61 -0
- package/dist/tools/codebase/find.js.map +1 -0
- package/dist/tools/codebase/glob.d.ts +3 -0
- package/dist/tools/codebase/glob.d.ts.map +1 -0
- package/dist/tools/codebase/glob.js +37 -0
- package/dist/tools/codebase/glob.js.map +1 -0
- package/dist/tools/codebase/grep.d.ts +3 -0
- package/dist/tools/codebase/grep.d.ts.map +1 -0
- package/dist/tools/codebase/grep.js +169 -0
- package/dist/tools/codebase/grep.js.map +1 -0
- package/dist/tools/codebase/ls.d.ts +3 -0
- package/dist/tools/codebase/ls.d.ts.map +1 -0
- package/dist/tools/codebase/ls.js +39 -0
- package/dist/tools/codebase/ls.js.map +1 -0
- package/dist/tools/codebase/read.d.ts +10 -0
- package/dist/tools/codebase/read.d.ts.map +1 -0
- package/dist/tools/codebase/read.js +81 -0
- package/dist/tools/codebase/read.js.map +1 -0
- package/dist/tools/delete_memory.d.ts +3 -0
- package/dist/tools/delete_memory.d.ts.map +1 -0
- package/dist/tools/delete_memory.js +46 -0
- package/dist/tools/delete_memory.js.map +1 -0
- package/dist/tools/edit/diff-compute.d.ts +4 -0
- package/dist/tools/edit/diff-compute.d.ts.map +1 -0
- package/dist/tools/edit/diff-compute.js +18 -0
- package/dist/tools/edit/diff-compute.js.map +1 -0
- package/dist/tools/edit/diff-render.d.ts +18 -0
- package/dist/tools/edit/diff-render.d.ts.map +1 -0
- package/dist/tools/edit/diff-render.js +128 -0
- package/dist/tools/edit/diff-render.js.map +1 -0
- package/dist/tools/edit/edit.d.ts +4 -0
- package/dist/tools/edit/edit.d.ts.map +1 -0
- package/dist/tools/edit/edit.js +440 -0
- package/dist/tools/edit/edit.js.map +1 -0
- package/dist/tools/edit/matcher.d.ts +24 -0
- package/dist/tools/edit/matcher.d.ts.map +1 -0
- package/dist/tools/edit/matcher.js +66 -0
- package/dist/tools/edit/matcher.js.map +1 -0
- package/dist/tools/edit/parser.d.ts +10 -0
- package/dist/tools/edit/parser.d.ts.map +1 -0
- package/dist/tools/edit/parser.js +120 -0
- package/dist/tools/edit/parser.js.map +1 -0
- package/dist/tools/edit/types.d.ts +16 -0
- package/dist/tools/edit/types.d.ts.map +1 -0
- package/dist/tools/edit/types.js +2 -0
- package/dist/tools/edit/types.js.map +1 -0
- package/dist/tools/edit/write_plan.d.ts +5 -0
- package/dist/tools/edit/write_plan.d.ts.map +1 -0
- package/dist/tools/edit/write_plan.js +98 -0
- package/dist/tools/edit/write_plan.js.map +1 -0
- package/dist/tools/edit.d.ts.map +1 -1
- package/dist/tools/edit.js +66 -1
- package/dist/tools/edit.js.map +1 -1
- package/dist/tools/execute/bash/constants.d.ts +24 -0
- package/dist/tools/execute/bash/constants.d.ts.map +1 -0
- package/dist/tools/execute/bash/constants.js +88 -0
- package/dist/tools/execute/bash/constants.js.map +1 -0
- package/dist/tools/execute/bash/errors.d.ts +39 -0
- package/dist/tools/execute/bash/errors.d.ts.map +1 -0
- package/dist/tools/execute/bash/errors.js +249 -0
- package/dist/tools/execute/bash/errors.js.map +1 -0
- package/dist/tools/execute/bash/execution.d.ts +18 -0
- package/dist/tools/execute/bash/execution.d.ts.map +1 -0
- package/dist/tools/execute/bash/execution.js +207 -0
- package/dist/tools/execute/bash/execution.js.map +1 -0
- package/dist/tools/execute/bash/index.d.ts +3 -0
- package/dist/tools/execute/bash/index.d.ts.map +1 -0
- package/dist/tools/execute/bash/index.js +117 -0
- package/dist/tools/execute/bash/index.js.map +1 -0
- package/dist/tools/execute/bash/platform.d.ts +6 -0
- package/dist/tools/execute/bash/platform.d.ts.map +1 -0
- package/dist/tools/execute/bash/platform.js +64 -0
- package/dist/tools/execute/bash/platform.js.map +1 -0
- package/dist/tools/execute/bash/security.d.ts +12 -0
- package/dist/tools/execute/bash/security.d.ts.map +1 -0
- package/dist/tools/execute/bash/security.js +26 -0
- package/dist/tools/execute/bash/security.js.map +1 -0
- package/dist/tools/execute/bash/validation.d.ts +16 -0
- package/dist/tools/execute/bash/validation.d.ts.map +1 -0
- package/dist/tools/execute/bash/validation.js +57 -0
- package/dist/tools/execute/bash/validation.js.map +1 -0
- package/dist/tools/execute/bash.d.ts +3 -0
- package/dist/tools/execute/bash.d.ts.map +1 -0
- package/dist/tools/execute/bash.js +728 -0
- package/dist/tools/execute/bash.js.map +1 -0
- package/dist/tools/execute/git.d.ts +3 -0
- package/dist/tools/execute/git.d.ts.map +1 -0
- package/dist/tools/execute/git.js +183 -0
- package/dist/tools/execute/git.js.map +1 -0
- package/dist/tools/execute/task_complete.d.ts +7 -0
- package/dist/tools/execute/task_complete.d.ts.map +1 -0
- package/dist/tools/execute/task_complete.js +38 -0
- package/dist/tools/execute/task_complete.js.map +1 -0
- package/dist/tools/fetch.d.ts.map +1 -1
- package/dist/tools/fetch.js +2 -0
- package/dist/tools/fetch.js.map +1 -1
- package/dist/tools/find.d.ts.map +1 -1
- package/dist/tools/find.js +2 -0
- package/dist/tools/find.js.map +1 -1
- package/dist/tools/git.d.ts.map +1 -1
- package/dist/tools/git.js +2 -0
- package/dist/tools/git.js.map +1 -1
- package/dist/tools/glob.d.ts.map +1 -1
- package/dist/tools/glob.js +2 -0
- package/dist/tools/glob.js.map +1 -1
- package/dist/tools/grep.d.ts.map +1 -1
- package/dist/tools/grep.js +2 -0
- package/dist/tools/grep.js.map +1 -1
- package/dist/tools/install/clawhub.d.ts +26 -0
- package/dist/tools/install/clawhub.d.ts.map +1 -0
- package/dist/tools/install/clawhub.js +182 -0
- package/dist/tools/install/clawhub.js.map +1 -0
- package/dist/tools/install/constants.d.ts +5 -0
- package/dist/tools/install/constants.d.ts.map +1 -0
- package/dist/tools/install/constants.js +5 -0
- package/dist/tools/install/constants.js.map +1 -0
- package/dist/tools/install/install_skill.d.ts +3 -0
- package/dist/tools/install/install_skill.d.ts.map +1 -0
- package/dist/tools/install/install_skill.js +132 -0
- package/dist/tools/install/install_skill.js.map +1 -0
- package/dist/tools/install/npm.d.ts +8 -0
- package/dist/tools/install/npm.d.ts.map +1 -0
- package/dist/tools/install/npm.js +52 -0
- package/dist/tools/install/npm.js.map +1 -0
- package/dist/tools/install/skill-content.d.ts +9 -0
- package/dist/tools/install/skill-content.d.ts.map +1 -0
- package/dist/tools/install/skill-content.js +89 -0
- package/dist/tools/install/skill-content.js.map +1 -0
- package/dist/tools/install_skill.d.ts.map +1 -1
- package/dist/tools/install_skill.js +2 -0
- package/dist/tools/install_skill.js.map +1 -1
- package/dist/tools/list_memory.d.ts +3 -0
- package/dist/tools/list_memory.d.ts.map +1 -0
- package/dist/tools/list_memory.js +55 -0
- package/dist/tools/list_memory.js.map +1 -0
- package/dist/tools/ls.d.ts.map +1 -1
- package/dist/tools/ls.js +2 -0
- package/dist/tools/ls.js.map +1 -1
- package/dist/tools/memory/delete_memory.d.ts +3 -0
- package/dist/tools/memory/delete_memory.d.ts.map +1 -0
- package/dist/tools/memory/delete_memory.js +46 -0
- package/dist/tools/memory/delete_memory.js.map +1 -0
- package/dist/tools/memory/list_memory.d.ts +3 -0
- package/dist/tools/memory/list_memory.d.ts.map +1 -0
- package/dist/tools/memory/list_memory.js +55 -0
- package/dist/tools/memory/list_memory.js.map +1 -0
- package/dist/tools/memory/save_memory.d.ts +3 -0
- package/dist/tools/memory/save_memory.d.ts.map +1 -0
- package/dist/tools/memory/save_memory.js +45 -0
- package/dist/tools/memory/save_memory.js.map +1 -0
- package/dist/tools/read.d.ts.map +1 -1
- package/dist/tools/read.js +2 -0
- package/dist/tools/read.js.map +1 -1
- package/dist/tools/registry.d.ts +7 -2
- package/dist/tools/registry.d.ts.map +1 -1
- package/dist/tools/registry.js +112 -17
- package/dist/tools/registry.js.map +1 -1
- package/dist/tools/save_memory.d.ts +3 -0
- package/dist/tools/save_memory.d.ts.map +1 -0
- package/dist/tools/save_memory.js +45 -0
- package/dist/tools/save_memory.js.map +1 -0
- package/dist/tools/search.d.ts.map +1 -1
- package/dist/tools/search.js +2 -0
- package/dist/tools/search.js.map +1 -1
- package/dist/tools/tree.d.ts +12 -0
- package/dist/tools/tree.d.ts.map +1 -0
- package/dist/tools/tree.js +87 -0
- package/dist/tools/tree.js.map +1 -0
- package/dist/tools/types.d.ts +12 -0
- package/dist/tools/types.d.ts.map +1 -1
- package/dist/tools/web/fetch.d.ts +3 -0
- package/dist/tools/web/fetch.d.ts.map +1 -0
- package/dist/tools/web/fetch.js +49 -0
- package/dist/tools/web/fetch.js.map +1 -0
- package/dist/tools/web/search.d.ts +3 -0
- package/dist/tools/web/search.d.ts.map +1 -0
- package/dist/tools/web/search.js +134 -0
- package/dist/tools/web/search.js.map +1 -0
- package/dist/tools/write_plan.d.ts.map +1 -1
- package/dist/tools/write_plan.js +2 -0
- package/dist/tools/write_plan.js.map +1 -1
- package/dist/tui/commands.d.ts +4 -0
- package/dist/tui/commands.d.ts.map +1 -0
- package/dist/tui/commands.js +118 -0
- package/dist/tui/commands.js.map +1 -0
- package/dist/tui/components/colors.d.ts +36 -0
- package/dist/tui/components/colors.d.ts.map +1 -0
- package/dist/tui/components/colors.js +48 -0
- package/dist/tui/components/colors.js.map +1 -0
- package/dist/tui/components/header-bar.d.ts +26 -0
- package/dist/tui/components/header-bar.d.ts.map +1 -0
- package/dist/tui/components/header-bar.js +78 -0
- package/dist/tui/components/header-bar.js.map +1 -0
- package/dist/tui/components/index.d.ts +9 -0
- package/dist/tui/components/index.d.ts.map +1 -0
- package/dist/tui/components/index.js +9 -0
- package/dist/tui/components/index.js.map +1 -0
- package/dist/tui/components/landing-screen.d.ts +12 -0
- package/dist/tui/components/landing-screen.d.ts.map +1 -0
- package/dist/tui/components/landing-screen.js +53 -0
- package/dist/tui/components/landing-screen.js.map +1 -0
- package/dist/tui/components/pixel-logo.d.ts +5 -0
- package/dist/tui/components/pixel-logo.d.ts.map +1 -0
- package/dist/tui/components/pixel-logo.js +27 -0
- package/dist/tui/components/pixel-logo.js.map +1 -0
- package/dist/tui/components/plan-utils.d.ts +11 -0
- package/dist/tui/components/plan-utils.d.ts.map +1 -0
- package/dist/tui/components/plan-utils.js +62 -0
- package/dist/tui/components/plan-utils.js.map +1 -0
- package/dist/tui/components/plans-list.d.ts +18 -0
- package/dist/tui/components/plans-list.d.ts.map +1 -0
- package/dist/tui/components/plans-list.js +46 -0
- package/dist/tui/components/plans-list.js.map +1 -0
- package/dist/tui/components/rich-footer.d.ts +29 -0
- package/dist/tui/components/rich-footer.d.ts.map +1 -0
- package/dist/tui/components/rich-footer.js +124 -0
- package/dist/tui/components/rich-footer.js.map +1 -0
- package/dist/tui/components/todo-panel.d.ts +14 -0
- package/dist/tui/components/todo-panel.d.ts.map +1 -0
- package/dist/tui/components/todo-panel.js +83 -0
- package/dist/tui/components/todo-panel.js.map +1 -0
- package/dist/tui/components.d.ts.map +1 -1
- package/dist/tui/components.js +14 -7
- package/dist/tui/components.js.map +1 -1
- package/dist/tui/index.d.ts +2 -1
- package/dist/tui/index.d.ts.map +1 -1
- package/dist/tui/index.js +99 -505
- package/dist/tui/index.js.map +1 -1
- package/dist/tui/overlays.d.ts +28 -0
- package/dist/tui/overlays.d.ts.map +1 -0
- package/dist/tui/overlays.js +154 -0
- package/dist/tui/overlays.js.map +1 -0
- package/dist/tui/themes.d.ts +5 -0
- package/dist/tui/themes.d.ts.map +1 -0
- package/dist/tui/themes.js +38 -0
- package/dist/tui/themes.js.map +1 -0
- package/dist/web/index.d.ts.map +1 -1
- package/dist/web/index.js +60 -122
- package/dist/web/index.js.map +1 -1
- package/dist/web/public/index.html +1 -1
- package/dist/web/public/messages.js +41 -25
- package/dist/web/public/state.js +72 -72
- package/dist/web/public/style.css +1126 -996
- package/dist/web/public/websocket.js +267 -267
- package/dist/web/session-bridge.d.ts.map +1 -1
- package/dist/web/session-bridge.js +82 -3
- package/dist/web/session-bridge.js.map +1 -1
- package/docs/ast-tools.md +117 -0
- package/package.json +60 -55
- package/scripts/check-line-count.mjs +53 -0
- package/scripts/copy-ast-wasm.mjs +32 -0
- package/src/agent/__tests__/session-restore.test.ts +284 -0
- package/src/agent/commands.ts +274 -0
- package/src/agent/compaction.ts +9 -2
- package/src/agent/index.ts +30 -25
- package/src/agent/memory.ts +60 -0
- package/src/agent/prompt-builder.ts +100 -26
- package/src/agent/providers/anthropic.ts +179 -179
- package/src/agent/providers/google.ts +210 -210
- package/src/agent/providers/ollama.ts +186 -186
- package/src/agent/providers/openai.ts +18 -3
- package/src/agent/session-chat.ts +480 -0
- package/src/agent/session-display.ts +195 -0
- package/src/agent/session-persistence.ts +146 -0
- package/src/agent/session-utils.ts +54 -0
- package/src/agent/session.ts +261 -718
- package/src/cli/index.ts +73 -4
- package/src/config/index.ts +342 -342
- package/src/index.ts +30 -3
- package/src/tools/__tests__/bash.test.ts +187 -1
- package/src/tools/__tests__/edit.test.ts +275 -11
- package/src/tools/__tests__/fetch.test.ts +1 -1
- package/src/tools/__tests__/glob.test.ts +1 -1
- package/src/tools/__tests__/grep.test.ts +1 -1
- package/src/tools/__tests__/ls.test.ts +1 -1
- package/src/tools/__tests__/read.test.ts +1 -1
- package/src/tools/__tests__/registry.test.ts +332 -0
- package/src/tools/__tests__/write_plan.test.ts +1 -1
- package/src/tools/codebase/ast/__tests__/integration.test.ts +452 -0
- package/src/tools/codebase/ast/__tests__/tree-sitter.test.ts +466 -0
- package/src/tools/codebase/ast/adapter.ts +44 -0
- package/src/tools/codebase/ast/tools.ts +322 -0
- package/src/tools/codebase/ast/tree-sitter-adapter.ts +468 -0
- package/src/tools/codebase/ast/tree-sitter-converters.ts +480 -0
- package/src/tools/codebase/ast/types.ts +102 -0
- package/src/tools/codebase/ast/wasm/tree-sitter-python.wasm +0 -0
- package/src/tools/codebase/ast/wasm/tree-sitter-tsx.wasm +0 -0
- package/src/tools/codebase/ast/wasm/tree-sitter-typescript.wasm +0 -0
- package/src/tools/codebase/ast/wasm/tree-sitter.wasm +0 -0
- package/src/tools/codebase/ast/wasm/web-tree-sitter.wasm +0 -0
- package/src/tools/{find.ts → codebase/find.ts} +4 -2
- package/src/tools/{glob.ts → codebase/glob.ts} +4 -2
- package/src/tools/{grep.ts → codebase/grep.ts} +4 -2
- package/src/tools/{ls.ts → codebase/ls.ts} +4 -2
- package/src/tools/{read.ts → codebase/read.ts} +5 -3
- package/src/tools/edit/diff-compute.ts +21 -0
- package/src/tools/edit/diff-render.ts +145 -0
- package/src/tools/edit/edit.ts +468 -0
- package/src/tools/edit/matcher.ts +70 -0
- package/src/tools/edit/parser.ts +131 -0
- package/src/tools/edit/types.ts +19 -0
- package/src/tools/{write_plan.ts → edit/write_plan.ts} +4 -2
- package/src/tools/execute/bash/constants.ts +93 -0
- package/src/tools/execute/bash/errors.ts +304 -0
- package/src/tools/execute/bash/execution.ts +211 -0
- package/src/tools/execute/bash/index.ts +131 -0
- package/src/tools/execute/bash/platform.ts +65 -0
- package/src/tools/execute/bash/security.ts +28 -0
- package/src/tools/execute/bash/validation.ts +78 -0
- package/src/tools/execute/git.ts +209 -0
- package/src/tools/execute/task_complete.ts +40 -0
- package/src/tools/install/clawhub.ts +222 -0
- package/src/tools/install/constants.ts +4 -0
- package/src/tools/install/install_skill.ts +150 -0
- package/src/tools/install/npm.ts +60 -0
- package/src/tools/install/skill-content.ts +108 -0
- package/src/tools/memory/delete_memory.ts +46 -0
- package/src/tools/memory/list_memory.ts +53 -0
- package/src/tools/memory/save_memory.ts +45 -0
- package/src/tools/registry.ts +119 -19
- package/src/tools/tree.ts +103 -0
- package/src/tools/types.ts +13 -0
- package/src/tools/{fetch.ts → web/fetch.ts} +4 -2
- package/src/tools/{search.ts → web/search.ts} +4 -2
- package/src/tui/commands.ts +135 -0
- package/src/tui/components/colors.ts +52 -0
- package/src/tui/components/header-bar.ts +88 -0
- package/src/tui/components/index.ts +8 -0
- package/src/tui/components/landing-screen.ts +67 -0
- package/src/tui/components/pixel-logo.ts +33 -0
- package/src/tui/components/plan-utils.ts +69 -0
- package/src/tui/components/plans-list.ts +55 -0
- package/src/tui/components/rich-footer.ts +146 -0
- package/src/tui/components/todo-panel.ts +95 -0
- package/src/tui/index.ts +335 -786
- package/src/tui/overlays.ts +203 -0
- package/src/tui/themes.ts +41 -0
- package/src/web/index.ts +64 -127
- package/src/web/public/index.html +1 -1
- package/src/web/public/messages.js +41 -25
- package/src/web/public/state.js +72 -72
- package/src/web/public/style.css +1126 -996
- package/src/web/public/websocket.js +267 -267
- package/src/web/session-bridge.ts +272 -193
- package/AGENT.md +0 -99
- package/src/tools/bash.ts +0 -125
- package/src/tools/edit.ts +0 -747
- package/src/tools/git.ts +0 -76
- package/src/tools/install_skill.ts +0 -539
- package/src/tui/components.ts +0 -635
|
@@ -0,0 +1,480 @@
|
|
|
1
|
+
import { saveTokenUsage } from '../config/tokens.js'
|
|
2
|
+
import { fmtErr } from '../tools/errors.js'
|
|
3
|
+
import type { ToolCall, ToolResult } from '../tools/types.js'
|
|
4
|
+
import { compact, shouldCompact } from './compaction.js'
|
|
5
|
+
import { EventChannels, getGlobalEventBus } from './event-bus.js'
|
|
6
|
+
import type { LLMMessage } from './llm.js'
|
|
7
|
+
import type { Session, SessionOutput } from './session.js'
|
|
8
|
+
import {
|
|
9
|
+
GY,
|
|
10
|
+
printTokenStats,
|
|
11
|
+
printToolInvocation,
|
|
12
|
+
printToolResult,
|
|
13
|
+
printUserMessage,
|
|
14
|
+
RE,
|
|
15
|
+
RS,
|
|
16
|
+
TH,
|
|
17
|
+
THINK_PREFIX_WIDTH,
|
|
18
|
+
termWidth,
|
|
19
|
+
thinkBottomBorder,
|
|
20
|
+
thinkTopBorder,
|
|
21
|
+
visibleWidth,
|
|
22
|
+
writeOut,
|
|
23
|
+
} from './session-display.js'
|
|
24
|
+
import { logToolError } from './session-persistence.js'
|
|
25
|
+
import { getContinuationMessage, isTaskCompleteMessage, sanitizeMessages } from './session-utils.js'
|
|
26
|
+
|
|
27
|
+
// ── Context compaction helper ─────────────────────────────────────────────────
|
|
28
|
+
|
|
29
|
+
function tryCompact(
|
|
30
|
+
session: Session,
|
|
31
|
+
out: SessionOutput | undefined,
|
|
32
|
+
bus: ReturnType<typeof getGlobalEventBus>,
|
|
33
|
+
): void {
|
|
34
|
+
if (shouldCompact(session.messages, session.config.contextWindow)) {
|
|
35
|
+
const before = session.messages.length
|
|
36
|
+
const result = compact(session.messages, session.config.contextWindow)
|
|
37
|
+
if (result.compressed) {
|
|
38
|
+
session.messages = result.messages
|
|
39
|
+
bus.emit(EventChannels.COMPACTION_TRIGGERED, { before, after: result.newCount })
|
|
40
|
+
if (out && !out.suppressToolOutput) {
|
|
41
|
+
out.write(
|
|
42
|
+
`\n ${GY}┃${RS} ${GY}📦 Compressed context: ${before} → ${result.newCount} messages${RS}\n`,
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// ── runChat ──────────────────────────────────────────────────────────────────
|
|
50
|
+
|
|
51
|
+
export async function runChat(session: Session, userPrompt: string): Promise<void> {
|
|
52
|
+
const bus = getGlobalEventBus()
|
|
53
|
+
const out = session.output
|
|
54
|
+
printUserMessage(userPrompt, out)
|
|
55
|
+
session.messages.push({ role: 'user', content: userPrompt })
|
|
56
|
+
session.save()
|
|
57
|
+
|
|
58
|
+
session.turnInputTokens = 0
|
|
59
|
+
session.turnOutputTokens = 0
|
|
60
|
+
session.turnApiCalls = 0
|
|
61
|
+
session.turnCacheHitTokens = 0
|
|
62
|
+
session.turnCacheMissTokens = 0
|
|
63
|
+
|
|
64
|
+
let iterations = 0
|
|
65
|
+
const maxIterations = session.config.mode === 'loop' ? 500 : 30
|
|
66
|
+
|
|
67
|
+
session.resetStopped()
|
|
68
|
+
session.abortController = new AbortController()
|
|
69
|
+
|
|
70
|
+
let toolCalls: ToolCall[] = []
|
|
71
|
+
|
|
72
|
+
while (iterations < maxIterations) {
|
|
73
|
+
if (session.isStopped()) {
|
|
74
|
+
bus.emit(EventChannels.TURN_END, { iterations, toolCallCount: toolCalls.length })
|
|
75
|
+
return
|
|
76
|
+
}
|
|
77
|
+
iterations++
|
|
78
|
+
session.turnApiCalls++
|
|
79
|
+
session.totalApiCalls++
|
|
80
|
+
toolCalls = []
|
|
81
|
+
let fullResponse = ''
|
|
82
|
+
let reasoningContent: string | undefined
|
|
83
|
+
let reasoningOutput = false
|
|
84
|
+
let reasoningLineStart = false
|
|
85
|
+
|
|
86
|
+
bus.emit(EventChannels.TURN_START, { prompt: userPrompt, iteration: iterations })
|
|
87
|
+
bus.emit(EventChannels.LLM_STREAM_START, { iteration: iterations })
|
|
88
|
+
|
|
89
|
+
session.messages = sanitizeMessages(session.messages)
|
|
90
|
+
const stream = session.provider.chat(
|
|
91
|
+
session.messages,
|
|
92
|
+
session.registry.getCoreDefinitions(),
|
|
93
|
+
session.abortController.signal,
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
try {
|
|
97
|
+
for await (const chunk of stream) {
|
|
98
|
+
if (chunk.reasoning_content) {
|
|
99
|
+
reasoningContent = chunk.reasoning_content
|
|
100
|
+
if (!chunk.text) {
|
|
101
|
+
bus.emit(EventChannels.THINKING, { text: chunk.reasoning_content })
|
|
102
|
+
if (!reasoningOutput) {
|
|
103
|
+
reasoningOutput = true
|
|
104
|
+
reasoningLineStart = true
|
|
105
|
+
if (!out?.suppressToolOutput) {
|
|
106
|
+
writeOut(thinkTopBorder(), out)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (!out?.suppressToolOutput) {
|
|
110
|
+
let thinkCol = 0
|
|
111
|
+
let remaining = chunk.reasoning_content
|
|
112
|
+
const maxContentWidth = termWidth() - THINK_PREFIX_WIDTH
|
|
113
|
+
while (remaining.length > 0) {
|
|
114
|
+
if (reasoningLineStart) {
|
|
115
|
+
writeOut(` ${GY}│${RS}${TH}`, out)
|
|
116
|
+
reasoningLineStart = false
|
|
117
|
+
thinkCol = 0
|
|
118
|
+
}
|
|
119
|
+
const nlIdx = remaining.indexOf('\n')
|
|
120
|
+
if (nlIdx === -1) {
|
|
121
|
+
while (remaining.length > 0) {
|
|
122
|
+
const segWidth = visibleWidth(remaining)
|
|
123
|
+
const avail = maxContentWidth - thinkCol
|
|
124
|
+
if (segWidth <= avail) {
|
|
125
|
+
writeOut(remaining, out)
|
|
126
|
+
thinkCol += segWidth
|
|
127
|
+
remaining = ''
|
|
128
|
+
} else if (avail <= 0) {
|
|
129
|
+
writeOut(`${RS}\n`, out)
|
|
130
|
+
writeOut(` ${GY}│${RS}${TH}`, out)
|
|
131
|
+
thinkCol = 0
|
|
132
|
+
} else {
|
|
133
|
+
let cut = avail
|
|
134
|
+
while (cut > 0 && visibleWidth(remaining.slice(0, cut)) > avail) cut--
|
|
135
|
+
if (cut <= 0) cut = 1
|
|
136
|
+
writeOut(remaining.slice(0, cut), out)
|
|
137
|
+
writeOut(`${RS}\n`, out)
|
|
138
|
+
writeOut(` ${GY}│${RS}${TH}`, out)
|
|
139
|
+
thinkCol = 0
|
|
140
|
+
remaining = remaining.slice(cut)
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
} else {
|
|
144
|
+
const segment = remaining.slice(0, nlIdx)
|
|
145
|
+
const segWidth = visibleWidth(segment)
|
|
146
|
+
const avail = maxContentWidth - thinkCol
|
|
147
|
+
if (segWidth <= avail) {
|
|
148
|
+
writeOut(segment, out)
|
|
149
|
+
writeOut(`${RS}\n`, out)
|
|
150
|
+
reasoningLineStart = true
|
|
151
|
+
thinkCol = 0
|
|
152
|
+
} else {
|
|
153
|
+
let rest = segment
|
|
154
|
+
if (avail > 0) {
|
|
155
|
+
let cut = avail
|
|
156
|
+
while (cut > 0 && visibleWidth(rest.slice(0, cut)) > avail) cut--
|
|
157
|
+
if (cut <= 0) cut = 1
|
|
158
|
+
writeOut(rest.slice(0, cut), out)
|
|
159
|
+
rest = rest.slice(cut)
|
|
160
|
+
}
|
|
161
|
+
writeOut(`${RS}\n`, out)
|
|
162
|
+
reasoningLineStart = true
|
|
163
|
+
thinkCol = 0
|
|
164
|
+
if (rest.length > 0) {
|
|
165
|
+
writeOut(` ${GY}│${RS}${TH}`, out)
|
|
166
|
+
reasoningLineStart = false
|
|
167
|
+
while (rest.length > 0) {
|
|
168
|
+
const rw = visibleWidth(rest)
|
|
169
|
+
if (rw <= maxContentWidth) {
|
|
170
|
+
writeOut(rest, out)
|
|
171
|
+
thinkCol = rw
|
|
172
|
+
rest = ''
|
|
173
|
+
} else {
|
|
174
|
+
let cut = maxContentWidth
|
|
175
|
+
while (cut > 0 && visibleWidth(rest.slice(0, cut)) > maxContentWidth)
|
|
176
|
+
cut--
|
|
177
|
+
if (cut <= 0) cut = 1
|
|
178
|
+
writeOut(rest.slice(0, cut), out)
|
|
179
|
+
writeOut(`${RS}\n`, out)
|
|
180
|
+
writeOut(` ${GY}│${RS}${TH}`, out)
|
|
181
|
+
rest = rest.slice(cut)
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
writeOut(`${RS}\n`, out)
|
|
186
|
+
reasoningLineStart = true
|
|
187
|
+
thinkCol = 0
|
|
188
|
+
}
|
|
189
|
+
remaining = remaining.slice(nlIdx + 1)
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (chunk.type === 'text' && chunk.text) {
|
|
196
|
+
if (reasoningOutput) {
|
|
197
|
+
bus.emit(EventChannels.THINKING_END, {})
|
|
198
|
+
if (!out?.suppressToolOutput) {
|
|
199
|
+
writeOut(`${RS}\n`, out)
|
|
200
|
+
writeOut(thinkBottomBorder(), out)
|
|
201
|
+
}
|
|
202
|
+
reasoningOutput = false
|
|
203
|
+
reasoningLineStart = false
|
|
204
|
+
}
|
|
205
|
+
fullResponse += chunk.text
|
|
206
|
+
writeOut(chunk.text, out)
|
|
207
|
+
} else if (chunk.type === 'tool_use' && chunk.tool_call) {
|
|
208
|
+
toolCalls.push(chunk.tool_call)
|
|
209
|
+
} else if (chunk.type === 'complete') {
|
|
210
|
+
if (chunk.usage) {
|
|
211
|
+
session.turnInputTokens += chunk.usage.input_tokens
|
|
212
|
+
session.turnOutputTokens += chunk.usage.output_tokens
|
|
213
|
+
session.totalInputTokens += chunk.usage.input_tokens
|
|
214
|
+
session.totalOutputTokens += chunk.usage.output_tokens
|
|
215
|
+
if (chunk.usage.prompt_cache_hit_tokens != null) {
|
|
216
|
+
session.turnCacheHitTokens += chunk.usage.prompt_cache_hit_tokens
|
|
217
|
+
session.totalCacheHitTokens += chunk.usage.prompt_cache_hit_tokens
|
|
218
|
+
}
|
|
219
|
+
if (chunk.usage.prompt_cache_miss_tokens != null) {
|
|
220
|
+
session.turnCacheMissTokens += chunk.usage.prompt_cache_miss_tokens
|
|
221
|
+
session.totalCacheMissTokens += chunk.usage.prompt_cache_miss_tokens
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
if (chunk.finish_reason === 'stop' || chunk.finish_reason === 'end_turn') {
|
|
225
|
+
if (toolCalls.length === 0) {
|
|
226
|
+
const finalAssistantMsg: LLMMessage = {
|
|
227
|
+
role: 'assistant',
|
|
228
|
+
content: fullResponse || null,
|
|
229
|
+
reasoning_content: reasoningContent,
|
|
230
|
+
}
|
|
231
|
+
session.messages.push(finalAssistantMsg)
|
|
232
|
+
printTokenStats(
|
|
233
|
+
session.turnInputTokens,
|
|
234
|
+
session.turnOutputTokens,
|
|
235
|
+
session.totalInputTokens,
|
|
236
|
+
session.totalOutputTokens,
|
|
237
|
+
session.turnApiCalls,
|
|
238
|
+
session.totalApiCalls,
|
|
239
|
+
out,
|
|
240
|
+
session.turnCacheHitTokens,
|
|
241
|
+
session.turnCacheMissTokens,
|
|
242
|
+
session.totalCacheHitTokens,
|
|
243
|
+
session.totalCacheMissTokens,
|
|
244
|
+
)
|
|
245
|
+
writeOut('\n\n', out)
|
|
246
|
+
saveTokenUsage(
|
|
247
|
+
session.config.cwd,
|
|
248
|
+
session.turnInputTokens,
|
|
249
|
+
session.turnOutputTokens,
|
|
250
|
+
session.turnApiCalls,
|
|
251
|
+
)
|
|
252
|
+
bus.emit(EventChannels.TURN_END, { iterations, toolCallCount: 0 })
|
|
253
|
+
session.save()
|
|
254
|
+
if (session.config.mode === 'loop' && !session.isStopped()) {
|
|
255
|
+
if (isTaskCompleteMessage(fullResponse)) {
|
|
256
|
+
session.save()
|
|
257
|
+
return
|
|
258
|
+
}
|
|
259
|
+
const contMsg = getContinuationMessage(session.messages)
|
|
260
|
+
session.messages.push({ role: 'user', content: contMsg })
|
|
261
|
+
session.turnInputTokens = 0
|
|
262
|
+
session.turnOutputTokens = 0
|
|
263
|
+
session.turnApiCalls = 0
|
|
264
|
+
session.turnCacheHitTokens = 0
|
|
265
|
+
session.turnCacheMissTokens = 0
|
|
266
|
+
continue
|
|
267
|
+
}
|
|
268
|
+
return
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
} catch (e) {
|
|
274
|
+
const errMsg = fmtErr(e)
|
|
275
|
+
if (!session.isStopped()) {
|
|
276
|
+
const partialContent = fullResponse ? fullResponse.slice(0, 500) : '(empty)'
|
|
277
|
+
if (!out?.suppressToolOutput) {
|
|
278
|
+
writeOut(`\n${RE}Stream error:${RS} ${errMsg}`, out)
|
|
279
|
+
writeOut(`\n ${GY}┃${RS} Partial response: ${partialContent}\n`, out)
|
|
280
|
+
}
|
|
281
|
+
console.error('[session] Stream error:', errMsg, '| Partial response:', partialContent)
|
|
282
|
+
if (reasoningOutput) {
|
|
283
|
+
bus.emit(EventChannels.THINKING_END, {})
|
|
284
|
+
if (!out?.suppressToolOutput) {
|
|
285
|
+
writeOut(`${RS}\n`, out)
|
|
286
|
+
writeOut(thinkBottomBorder(), out)
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
bus.emit(EventChannels.LLM_STREAM_END, { iteration: iterations, toolCallCount: 0 })
|
|
290
|
+
bus.emit(EventChannels.TURN_END, { iterations, toolCallCount: 0 })
|
|
291
|
+
|
|
292
|
+
if (toolCalls.length > 0) {
|
|
293
|
+
const interruptedMsg: LLMMessage = {
|
|
294
|
+
role: 'assistant',
|
|
295
|
+
content: null,
|
|
296
|
+
tool_calls: toolCalls,
|
|
297
|
+
reasoning_content: reasoningContent,
|
|
298
|
+
}
|
|
299
|
+
session.messages.push(interruptedMsg)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
saveTokenUsage(
|
|
303
|
+
session.config.cwd,
|
|
304
|
+
session.turnInputTokens,
|
|
305
|
+
session.turnOutputTokens,
|
|
306
|
+
session.turnApiCalls,
|
|
307
|
+
)
|
|
308
|
+
session.save()
|
|
309
|
+
}
|
|
310
|
+
return
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
bus.emit(EventChannels.LLM_STREAM_END, {
|
|
314
|
+
iteration: iterations,
|
|
315
|
+
toolCallCount: toolCalls.length,
|
|
316
|
+
})
|
|
317
|
+
|
|
318
|
+
if (reasoningOutput) {
|
|
319
|
+
bus.emit(EventChannels.THINKING_END, {})
|
|
320
|
+
if (!out?.suppressToolOutput) {
|
|
321
|
+
writeOut(`${RS}\n`, out)
|
|
322
|
+
writeOut(thinkBottomBorder(), out)
|
|
323
|
+
}
|
|
324
|
+
reasoningOutput = false
|
|
325
|
+
reasoningLineStart = false
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
if (toolCalls.length === 0) {
|
|
329
|
+
if (fullResponse) {
|
|
330
|
+
const finalAssistantMsg: LLMMessage = {
|
|
331
|
+
role: 'assistant',
|
|
332
|
+
content: fullResponse,
|
|
333
|
+
reasoning_content: reasoningContent,
|
|
334
|
+
}
|
|
335
|
+
session.messages.push(finalAssistantMsg)
|
|
336
|
+
printTokenStats(
|
|
337
|
+
session.turnInputTokens,
|
|
338
|
+
session.turnOutputTokens,
|
|
339
|
+
session.totalInputTokens,
|
|
340
|
+
session.totalOutputTokens,
|
|
341
|
+
session.turnApiCalls,
|
|
342
|
+
session.totalApiCalls,
|
|
343
|
+
out,
|
|
344
|
+
session.turnCacheHitTokens,
|
|
345
|
+
session.turnCacheMissTokens,
|
|
346
|
+
session.totalCacheHitTokens,
|
|
347
|
+
session.totalCacheMissTokens,
|
|
348
|
+
)
|
|
349
|
+
writeOut('\n\n', out)
|
|
350
|
+
}
|
|
351
|
+
saveTokenUsage(
|
|
352
|
+
session.config.cwd,
|
|
353
|
+
session.turnInputTokens,
|
|
354
|
+
session.turnOutputTokens,
|
|
355
|
+
session.turnApiCalls,
|
|
356
|
+
)
|
|
357
|
+
bus.emit(EventChannels.TURN_END, { iterations, toolCallCount: 0 })
|
|
358
|
+
session.save()
|
|
359
|
+
if (session.config.mode === 'loop' && !session.isStopped()) {
|
|
360
|
+
if (fullResponse && isTaskCompleteMessage(fullResponse)) {
|
|
361
|
+
session.save()
|
|
362
|
+
return
|
|
363
|
+
}
|
|
364
|
+
const contMsg = getContinuationMessage(session.messages)
|
|
365
|
+
session.messages.push({ role: 'user', content: contMsg })
|
|
366
|
+
session.turnInputTokens = 0
|
|
367
|
+
session.turnOutputTokens = 0
|
|
368
|
+
session.turnApiCalls = 0
|
|
369
|
+
session.turnCacheHitTokens = 0
|
|
370
|
+
session.turnCacheMissTokens = 0
|
|
371
|
+
continue
|
|
372
|
+
}
|
|
373
|
+
return
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const assistantMsg: LLMMessage = {
|
|
377
|
+
role: 'assistant',
|
|
378
|
+
content: fullResponse || null,
|
|
379
|
+
tool_calls: toolCalls,
|
|
380
|
+
reasoning_content: reasoningContent,
|
|
381
|
+
}
|
|
382
|
+
session.messages.push(assistantMsg)
|
|
383
|
+
|
|
384
|
+
if (!session.config.autoApprove && session.output?.confirmTool && toolCalls.length > 0) {
|
|
385
|
+
const writeTools = ['edit', 'bash', 'write_plan', 'install_skill']
|
|
386
|
+
const needsConfirm = toolCalls.filter(tc => writeTools.includes(tc.name))
|
|
387
|
+
if (needsConfirm.length > 0) {
|
|
388
|
+
const approved = await session.output.confirmTool(needsConfirm)
|
|
389
|
+
if (!approved) {
|
|
390
|
+
const rejectMsg: LLMMessage = {
|
|
391
|
+
role: 'tool',
|
|
392
|
+
content:
|
|
393
|
+
'USER_REJECTED: The user declined to execute the requested tool calls. Try a different approach.',
|
|
394
|
+
tool_call_id: needsConfirm[0].id,
|
|
395
|
+
name: 'user_feedback',
|
|
396
|
+
}
|
|
397
|
+
session.messages.push(rejectMsg)
|
|
398
|
+
bus.emit(EventChannels.TURN_END, { iterations, toolCallCount: toolCalls.length })
|
|
399
|
+
continue
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
for (let i = 0; i < toolCalls.length; i++) {
|
|
405
|
+
const tc = toolCalls[i]
|
|
406
|
+
if (session.isStopped()) {
|
|
407
|
+
const remainingToolCalls = toolCalls.slice(i)
|
|
408
|
+
if (remainingToolCalls.length > 0) {
|
|
409
|
+
const interruptedMsg: LLMMessage = {
|
|
410
|
+
role: 'assistant',
|
|
411
|
+
content: null,
|
|
412
|
+
tool_calls: remainingToolCalls,
|
|
413
|
+
}
|
|
414
|
+
session.messages.push(interruptedMsg)
|
|
415
|
+
}
|
|
416
|
+
bus.emit(EventChannels.TURN_END, { iterations, toolCallCount: toolCalls.length })
|
|
417
|
+
return
|
|
418
|
+
}
|
|
419
|
+
bus.emit(EventChannels.TOOL_CALL, { name: tc.name, input: tc.input, id: tc.id })
|
|
420
|
+
if (!out?.suppressToolOutput) {
|
|
421
|
+
printToolInvocation(tc, out)
|
|
422
|
+
}
|
|
423
|
+
const result: ToolResult = await session.registry.dispatch(tc)
|
|
424
|
+
if (result.success) {
|
|
425
|
+
bus.emit(EventChannels.TOOL_RESULT, { name: tc.name, id: tc.id, output: result.output })
|
|
426
|
+
} else {
|
|
427
|
+
bus.emit(EventChannels.TOOL_ERROR, { name: tc.name, id: tc.id, error: result.error })
|
|
428
|
+
logToolError(tc, result, session.sessionId)
|
|
429
|
+
}
|
|
430
|
+
if (!out?.suppressToolOutput) {
|
|
431
|
+
printToolResult(tc, result, out)
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const resultMsg: LLMMessage = {
|
|
435
|
+
role: 'tool',
|
|
436
|
+
content: result.success ? result.output : `ERROR: ${result.error}`,
|
|
437
|
+
tool_call_id: tc.id,
|
|
438
|
+
name: tc.name,
|
|
439
|
+
}
|
|
440
|
+
session.messages.push(resultMsg)
|
|
441
|
+
tryCompact(session, out, bus)
|
|
442
|
+
|
|
443
|
+
if (tc.name === 'task_complete') {
|
|
444
|
+
session.save()
|
|
445
|
+
session.stopped = true
|
|
446
|
+
break
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
session.save()
|
|
451
|
+
bus.emit(EventChannels.TURN_END, { iterations, toolCallCount: toolCalls.length })
|
|
452
|
+
|
|
453
|
+
tryCompact(session, out, bus)
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
if (iterations >= maxIterations) {
|
|
457
|
+
printTokenStats(
|
|
458
|
+
session.turnInputTokens,
|
|
459
|
+
session.turnOutputTokens,
|
|
460
|
+
session.totalInputTokens,
|
|
461
|
+
session.totalOutputTokens,
|
|
462
|
+
session.turnApiCalls,
|
|
463
|
+
session.totalApiCalls,
|
|
464
|
+
out,
|
|
465
|
+
session.turnCacheHitTokens,
|
|
466
|
+
session.turnCacheMissTokens,
|
|
467
|
+
session.totalCacheHitTokens,
|
|
468
|
+
session.totalCacheMissTokens,
|
|
469
|
+
)
|
|
470
|
+
writeOut('\nAgent reached maximum iterations. Stopping.\n', out)
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
saveTokenUsage(
|
|
474
|
+
session.config.cwd,
|
|
475
|
+
session.turnInputTokens,
|
|
476
|
+
session.turnOutputTokens,
|
|
477
|
+
session.turnApiCalls,
|
|
478
|
+
)
|
|
479
|
+
session.save()
|
|
480
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import type { ToolCall, ToolResult } from '../tools/types.js'
|
|
2
|
+
import { EventChannels, getGlobalEventBus } from './event-bus.js'
|
|
3
|
+
import type { SessionOutput } from './session.js'
|
|
4
|
+
|
|
5
|
+
// ── Colors ─────────────────────────────────────────────────────────────────
|
|
6
|
+
|
|
7
|
+
const CY = '\x1b[36m'
|
|
8
|
+
const GR = '\x1b[32m'
|
|
9
|
+
const YE = '\x1b[33m'
|
|
10
|
+
const RE = '\x1b[31m'
|
|
11
|
+
const MG = '\x1b[35m'
|
|
12
|
+
const GY = '\x1b[90m'
|
|
13
|
+
const RS = '\x1b[0m'
|
|
14
|
+
const BLD = '\x1b[1m'
|
|
15
|
+
const TH = '\x1b[48;2;22;22;32m\x1b[38;2;150;150;170m'
|
|
16
|
+
|
|
17
|
+
export { BLD, CY, GR, GY, MG, RE, RS, TH, YE }
|
|
18
|
+
|
|
19
|
+
/** Get terminal width (columns), default to 80. */
|
|
20
|
+
export function termWidth(): number {
|
|
21
|
+
return process.stdout.columns ?? 80
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Visible width of a string (strip ANSI codes). ASCII=1, CJK/non-ASCII=2. */
|
|
25
|
+
export function visibleWidth(s: string): number {
|
|
26
|
+
let w = 0
|
|
27
|
+
for (let i = 0; i < s.length; i++) {
|
|
28
|
+
if (s.charCodeAt(i) === 0x1b) {
|
|
29
|
+
while (i < s.length && s[i] !== 'm') i++
|
|
30
|
+
continue
|
|
31
|
+
}
|
|
32
|
+
w += s.charCodeAt(i) > 0x7e ? 2 : 1
|
|
33
|
+
}
|
|
34
|
+
return w
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Visible prefix width for thinking box lines: " │" = 3 */
|
|
38
|
+
export const THINK_PREFIX_WIDTH = 3
|
|
39
|
+
|
|
40
|
+
/** Build the top border of the thinking box */
|
|
41
|
+
export function thinkTopBorder(): string {
|
|
42
|
+
return `\n ${GY}╭───────${RS}${TH} Think ${GY}────────────────────${RS}\n`
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Build the bottom border of the thinking box */
|
|
46
|
+
export function thinkBottomBorder(): string {
|
|
47
|
+
return ` ${GY}╰${'─'.repeat(42)}${RS}\n\n`
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function writeOut(text: string, output?: SessionOutput): void {
|
|
51
|
+
if (output) {
|
|
52
|
+
output.write(text)
|
|
53
|
+
} else {
|
|
54
|
+
process.stdout.write(text)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function printUserMessage(prompt: string, output?: SessionOutput): void {
|
|
59
|
+
if (output?.suppressToolOutput) return
|
|
60
|
+
const line = ` ${GY}┃${RS} ${BLD}${CY}You${RS}`
|
|
61
|
+
writeOut(`\n${line} ${prompt}\n\n`, output)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function printToolInvocation(tc: ToolCall, output?: SessionOutput): void {
|
|
65
|
+
const detail = formatToolInput(tc)
|
|
66
|
+
const isWrite = tc.name === 'write_plan' || tc.name === 'edit'
|
|
67
|
+
const icon = isWrite ? `${YE}◆${RS}` : `${GR}◇${RS}`
|
|
68
|
+
const label = isWrite ? `${YE}${tc.name}${RS}` : `${GR}${tc.name}${RS}`
|
|
69
|
+
writeOut(`\n ${GY}│${RS} ${icon} ${label}${detail ? ` ${GY}${detail}${RS}` : ''}\n`, output)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function printToolResult(tc: ToolCall, result: ToolResult, output?: SessionOutput): void {
|
|
73
|
+
if (!result.success) {
|
|
74
|
+
writeOut(` ${GY}│${RS} ${RE}✖${RS} ${RE}${result.error}${RS}\n`, output)
|
|
75
|
+
return
|
|
76
|
+
}
|
|
77
|
+
if (tc.name === 'read') {
|
|
78
|
+
const fileCount = (result.output.match(/^=== /gm) || []).length
|
|
79
|
+
writeOut(` ${GY}│${RS} ${GR}✔${RS} read ${fileCount} file(s)\n`, output)
|
|
80
|
+
for (const line of result.output.split('\n')) {
|
|
81
|
+
if (line.startsWith('=== ')) {
|
|
82
|
+
const fp = line.slice(4, line.includes(' ===') ? line.indexOf(' ===') + 4 : undefined)
|
|
83
|
+
writeOut(` ${GY}│${RS} ${GY}${fp}${RS}\n`, output)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
} else if (tc.name === 'glob') {
|
|
87
|
+
const count = result.output.split('\n').filter(l => l && !l.startsWith('No')).length
|
|
88
|
+
writeOut(` ${GY}│${RS} ${GR}✔${RS} glob ${count} match(es)\n`, output)
|
|
89
|
+
} else if (tc.name === 'grep') {
|
|
90
|
+
const count = result.output.split('\n').filter(l => l && !l.startsWith('No')).length
|
|
91
|
+
writeOut(` ${GY}│${RS} ${GR}✔${RS} grep ${count} match(es)\n`, output)
|
|
92
|
+
} else if (tc.name === 'bash') {
|
|
93
|
+
const outLines = result.output.split('\n')
|
|
94
|
+
const summary = outLines.length > 1 ? `(${outLines.length} lines)` : ''
|
|
95
|
+
writeOut(` ${GY}│${RS} ${GR}✔${RS} bash ${summary}\n`, output)
|
|
96
|
+
} else if (tc.name === 'edit') {
|
|
97
|
+
writeOut(` ${GY}│${RS} ${GR}✔${RS} edit\n`, output)
|
|
98
|
+
if (result.output) {
|
|
99
|
+
for (const l of result.output.split('\n')) {
|
|
100
|
+
if (l.trim()) writeOut(` ${GY}│${RS} ${l.trim()}\n`, output)
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
} else if (tc.name === 'write_plan') {
|
|
104
|
+
writeOut(` ${GY}│${RS} ${GR}✔${RS} ${result.output || tc.name}\n`, output)
|
|
105
|
+
} else if (tc.name === 'search') {
|
|
106
|
+
writeOut(
|
|
107
|
+
` ${GY}│${RS} ${GR}✔${RS} search: ${String(tc.input.query || '').slice(0, 80)}\n`,
|
|
108
|
+
output,
|
|
109
|
+
)
|
|
110
|
+
} else {
|
|
111
|
+
writeOut(` ${GY}│${RS} ${GR}✔${RS} ${tc.name}\n`, output)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
interface SingleEditShape {
|
|
116
|
+
file_path: string
|
|
117
|
+
old_string: string
|
|
118
|
+
new_string: string
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function isSingleEditShape(v: unknown): v is SingleEditShape {
|
|
122
|
+
return (
|
|
123
|
+
typeof v === 'object' &&
|
|
124
|
+
v !== null &&
|
|
125
|
+
'file_path' in v &&
|
|
126
|
+
'old_string' in v &&
|
|
127
|
+
'new_string' in v
|
|
128
|
+
)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function formatToolInput(tc: ToolCall): string {
|
|
132
|
+
const parts: string[] = []
|
|
133
|
+
if (tc.name === 'read' && Array.isArray(tc.input.paths)) {
|
|
134
|
+
parts.push(tc.input.paths.join(', '))
|
|
135
|
+
} else if (tc.name === 'glob' && typeof tc.input.pattern === 'string') {
|
|
136
|
+
parts.push(tc.input.pattern)
|
|
137
|
+
} else if (tc.name === 'grep') {
|
|
138
|
+
if (typeof tc.input.pattern === 'string') parts.push(`/${tc.input.pattern}/`)
|
|
139
|
+
if (typeof tc.input.include === 'string') parts.push(`in:${tc.input.include}`)
|
|
140
|
+
} else if (tc.name === 'ls') {
|
|
141
|
+
parts.push(typeof tc.input.path === 'string' ? tc.input.path : '.')
|
|
142
|
+
} else if (tc.name === 'bash') {
|
|
143
|
+
const cmd = typeof tc.input.command === 'string' ? tc.input.command : ''
|
|
144
|
+
parts.push(cmd.length > 80 ? `${cmd.slice(0, 80)}\u2026` : cmd)
|
|
145
|
+
} else if (tc.name === 'search') {
|
|
146
|
+
if (typeof tc.input.query === 'string') parts.push(tc.input.query.slice(0, 120))
|
|
147
|
+
} else if (tc.name === 'write_plan') {
|
|
148
|
+
if (typeof tc.input.filename === 'string') parts.push(tc.input.filename)
|
|
149
|
+
} else if (tc.name === 'edit') {
|
|
150
|
+
if (Array.isArray(tc.input.edits) && tc.input.edits.every(isSingleEditShape)) {
|
|
151
|
+
const paths = tc.input.edits.map(e => e.file_path)
|
|
152
|
+
parts.push(paths.join(', '))
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return parts.join(' \u2502 ')
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function printTokenStats(
|
|
159
|
+
turnIn: number,
|
|
160
|
+
turnOut: number,
|
|
161
|
+
totalIn: number,
|
|
162
|
+
totalOut: number,
|
|
163
|
+
turnApi: number,
|
|
164
|
+
totalApi: number,
|
|
165
|
+
output?: SessionOutput,
|
|
166
|
+
turnCacheHit?: number,
|
|
167
|
+
turnCacheMiss?: number,
|
|
168
|
+
totalCacheHit?: number,
|
|
169
|
+
totalCacheMiss?: number,
|
|
170
|
+
): void {
|
|
171
|
+
const bus = getGlobalEventBus()
|
|
172
|
+
bus.emit(EventChannels.TOKEN_STATS, {
|
|
173
|
+
turnIn,
|
|
174
|
+
turnOut,
|
|
175
|
+
totalIn,
|
|
176
|
+
totalOut,
|
|
177
|
+
turnApi,
|
|
178
|
+
totalApi,
|
|
179
|
+
turnCacheHit,
|
|
180
|
+
turnCacheMiss,
|
|
181
|
+
totalCacheHit,
|
|
182
|
+
totalCacheMiss,
|
|
183
|
+
})
|
|
184
|
+
if (output?.suppressToolOutput) return
|
|
185
|
+
const total = totalIn + totalOut
|
|
186
|
+
let msg = ` ${GY}┃${RS} ${GY}${BLD}▴${RS}${GY}${turnIn}${RS} ${GY}${BLD}▾${RS}${GY}${turnOut}${RS} ${GY}total${RS} ${total} ${GY}calls${RS} ${turnApi}(${totalApi})`
|
|
187
|
+
const cacheHit = totalCacheHit ?? 0
|
|
188
|
+
const cacheMiss = totalCacheMiss ?? 0
|
|
189
|
+
const cacheTotal = cacheHit + cacheMiss
|
|
190
|
+
if (cacheTotal > 0) {
|
|
191
|
+
const pct = Math.round((cacheHit / cacheTotal) * 100)
|
|
192
|
+
msg += ` ${GY}cached${RS} ${pct}%`
|
|
193
|
+
}
|
|
194
|
+
writeOut(`\n${msg}\n`, output)
|
|
195
|
+
}
|