@travisennis/acai 0.0.1
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/.acai/acai.json +9 -0
- package/.acai/prompts/add-openrouter-model.md +13 -0
- package/.acai/prompts/project-status.md +4 -0
- package/.acai/prompts/update-architecture-document.md +9 -0
- package/.acai/rules/learned-rules.md +9 -0
- package/.ai/docs/available-tools.txt +3 -0
- package/.ai/docs/cognitive_complexity_refactoring_progress.md +65 -0
- package/.ai/docs/deleted_tools.md +168 -0
- package/.ai/docs/deleted_tools_88ced9ef.md +56 -0
- package/.ai/docs/image-pasting.md +46 -0
- package/.ai/docs/initialize-app.md +117 -0
- package/.ai/docs/issue-4-plan.md +44 -0
- package/.ai/docs/marked-renderer-debug.md +15 -0
- package/.ai/docs/marked-renderer-refactor-plan.md +64 -0
- package/.ai/docs/memory-use-cases.md +55 -0
- package/.ai/docs/prompt-consistency.md +31 -0
- package/.ai/docs/refactoring-tools.md +98 -0
- package/.ai/docs/system-prompt-update.md +174 -0
- package/.ai/docs/system_prompt.txt +210 -0
- package/.ai/docs/tasks.md +49 -0
- package/.ai/plan.md +131 -0
- package/.ai/prompt.md +1 -0
- package/.ai/scripts/fetch_models.js +27 -0
- package/.ai/scripts/generateSystemPrompt.ts +15 -0
- package/.ai/scripts/list-tools.mjs +4 -0
- package/.ai/scripts/p5_geometric_shapes.js +149 -0
- package/.husky/commit-msg +1 -0
- package/.husky/pre-commit +3 -0
- package/.husky/pre-push +1 -0
- package/.ignore +4 -0
- package/AGENTS.md +25 -0
- package/ARCHITECTURE.md +304 -0
- package/LICENSE +21 -0
- package/README.md +392 -0
- package/TODO.md +2 -0
- package/biome.json +61 -0
- package/commitlint.config.js +3 -0
- package/dist/cli.d.ts +19 -0
- package/dist/cli.js +116 -0
- package/dist/commands/application-log-command.d.ts +2 -0
- package/dist/commands/application-log-command.js +43 -0
- package/dist/commands/clear-command.d.ts +2 -0
- package/dist/commands/clear-command.js +12 -0
- package/dist/commands/compact-command.d.ts +2 -0
- package/dist/commands/compact-command.js +51 -0
- package/dist/commands/copy-command.d.ts +2 -0
- package/dist/commands/copy-command.js +51 -0
- package/dist/commands/edit-command.d.ts +2 -0
- package/dist/commands/edit-command.js +53 -0
- package/dist/commands/edit-prompt-command.d.ts +2 -0
- package/dist/commands/edit-prompt-command.js +25 -0
- package/dist/commands/exit-command.d.ts +2 -0
- package/dist/commands/exit-command.js +14 -0
- package/dist/commands/files-command.d.ts +2 -0
- package/dist/commands/files-command.js +63 -0
- package/dist/commands/generate-rules-command.d.ts +2 -0
- package/dist/commands/generate-rules-command.js +61 -0
- package/dist/commands/help-command.d.ts +2 -0
- package/dist/commands/help-command.js +19 -0
- package/dist/commands/init-command.d.ts +2 -0
- package/dist/commands/init-command.js +40 -0
- package/dist/commands/last-log-command.d.ts +2 -0
- package/dist/commands/last-log-command.js +76 -0
- package/dist/commands/manager.d.ts +22 -0
- package/dist/commands/manager.js +123 -0
- package/dist/commands/model-command.d.ts +2 -0
- package/dist/commands/model-command.js +84 -0
- package/dist/commands/paste-command.d.ts +2 -0
- package/dist/commands/paste-command.js +40 -0
- package/dist/commands/prompt-command.d.ts +2 -0
- package/dist/commands/prompt-command.js +111 -0
- package/dist/commands/reset-command.d.ts +2 -0
- package/dist/commands/reset-command.js +16 -0
- package/dist/commands/rules-command.d.ts +2 -0
- package/dist/commands/rules-command.js +68 -0
- package/dist/commands/save-command.d.ts +2 -0
- package/dist/commands/save-command.js +14 -0
- package/dist/commands/types.d.ts +26 -0
- package/dist/commands/types.js +1 -0
- package/dist/commands/usage-command.d.ts +2 -0
- package/dist/commands/usage-command.js +21 -0
- package/dist/config.d.ts +60 -0
- package/dist/config.js +193 -0
- package/dist/conversation-analyzer.d.ts +10 -0
- package/dist/conversation-analyzer.js +88 -0
- package/dist/dedent.d.ts +3 -0
- package/dist/dedent.js +38 -0
- package/dist/formatting.d.ts +17 -0
- package/dist/formatting.js +103 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +213 -0
- package/dist/logger.d.ts +2 -0
- package/dist/logger.js +24 -0
- package/dist/mentions.d.ts +9 -0
- package/dist/mentions.js +182 -0
- package/dist/messages.d.ts +69 -0
- package/dist/messages.js +261 -0
- package/dist/middleware/audit-message.d.ts +5 -0
- package/dist/middleware/audit-message.js +95 -0
- package/dist/middleware/index.d.ts +2 -0
- package/dist/middleware/index.js +2 -0
- package/dist/middleware/rate-limit.d.ts +4 -0
- package/dist/middleware/rate-limit.js +17 -0
- package/dist/models/ai-config.d.ts +12 -0
- package/dist/models/ai-config.js +87 -0
- package/dist/models/anthropic-provider.d.ts +25 -0
- package/dist/models/anthropic-provider.js +184 -0
- package/dist/models/deepseek-provider.d.ts +20 -0
- package/dist/models/deepseek-provider.js +42 -0
- package/dist/models/google-provider.d.ts +19 -0
- package/dist/models/google-provider.js +56 -0
- package/dist/models/manager.d.ts +15 -0
- package/dist/models/manager.js +48 -0
- package/dist/models/openai-provider.d.ts +22 -0
- package/dist/models/openai-provider.js +70 -0
- package/dist/models/openrouter-provider.d.ts +36 -0
- package/dist/models/openrouter-provider.js +276 -0
- package/dist/models/providers.d.ts +33 -0
- package/dist/models/providers.js +116 -0
- package/dist/models/xai-provider.d.ts +20 -0
- package/dist/models/xai-provider.js +47 -0
- package/dist/parsing.d.ts +2 -0
- package/dist/parsing.js +18 -0
- package/dist/prompts/manager.d.ts +19 -0
- package/dist/prompts/manager.js +71 -0
- package/dist/prompts.d.ts +4 -0
- package/dist/prompts.js +158 -0
- package/dist/repl-prompt.d.ts +14 -0
- package/dist/repl-prompt.js +147 -0
- package/dist/repl.d.ts +27 -0
- package/dist/repl.js +431 -0
- package/dist/source/cli.d.ts +19 -0
- package/dist/source/cli.js +116 -0
- package/dist/source/commands/application-log-command.d.ts +2 -0
- package/dist/source/commands/application-log-command.js +43 -0
- package/dist/source/commands/clear-command.d.ts +2 -0
- package/dist/source/commands/clear-command.js +12 -0
- package/dist/source/commands/compact-command.d.ts +2 -0
- package/dist/source/commands/compact-command.js +51 -0
- package/dist/source/commands/copy-command.d.ts +2 -0
- package/dist/source/commands/copy-command.js +51 -0
- package/dist/source/commands/edit-command.d.ts +2 -0
- package/dist/source/commands/edit-command.js +53 -0
- package/dist/source/commands/edit-prompt-command.d.ts +2 -0
- package/dist/source/commands/edit-prompt-command.js +25 -0
- package/dist/source/commands/exit-command.d.ts +2 -0
- package/dist/source/commands/exit-command.js +14 -0
- package/dist/source/commands/files-command.d.ts +2 -0
- package/dist/source/commands/files-command.js +63 -0
- package/dist/source/commands/generate-rules-command.d.ts +2 -0
- package/dist/source/commands/generate-rules-command.js +61 -0
- package/dist/source/commands/help-command.d.ts +2 -0
- package/dist/source/commands/help-command.js +19 -0
- package/dist/source/commands/init-command.d.ts +2 -0
- package/dist/source/commands/init-command.js +40 -0
- package/dist/source/commands/last-log-command.d.ts +2 -0
- package/dist/source/commands/last-log-command.js +76 -0
- package/dist/source/commands/manager.d.ts +22 -0
- package/dist/source/commands/manager.js +123 -0
- package/dist/source/commands/model-command.d.ts +2 -0
- package/dist/source/commands/model-command.js +84 -0
- package/dist/source/commands/paste-command.d.ts +2 -0
- package/dist/source/commands/paste-command.js +40 -0
- package/dist/source/commands/prompt-command.d.ts +2 -0
- package/dist/source/commands/prompt-command.js +111 -0
- package/dist/source/commands/reset-command.d.ts +2 -0
- package/dist/source/commands/reset-command.js +16 -0
- package/dist/source/commands/rules-command.d.ts +2 -0
- package/dist/source/commands/rules-command.js +68 -0
- package/dist/source/commands/save-command.d.ts +2 -0
- package/dist/source/commands/save-command.js +14 -0
- package/dist/source/commands/types.d.ts +26 -0
- package/dist/source/commands/types.js +1 -0
- package/dist/source/commands/usage-command.d.ts +2 -0
- package/dist/source/commands/usage-command.js +21 -0
- package/dist/source/config.d.ts +60 -0
- package/dist/source/config.js +193 -0
- package/dist/source/conversation-analyzer.d.ts +10 -0
- package/dist/source/conversation-analyzer.js +88 -0
- package/dist/source/dedent.d.ts +3 -0
- package/dist/source/dedent.js +38 -0
- package/dist/source/formatting.d.ts +17 -0
- package/dist/source/formatting.js +103 -0
- package/dist/source/index.d.ts +18 -0
- package/dist/source/index.js +213 -0
- package/dist/source/logger.d.ts +2 -0
- package/dist/source/logger.js +24 -0
- package/dist/source/mentions.d.ts +9 -0
- package/dist/source/mentions.js +182 -0
- package/dist/source/messages.d.ts +69 -0
- package/dist/source/messages.js +261 -0
- package/dist/source/middleware/audit-message.d.ts +5 -0
- package/dist/source/middleware/audit-message.js +95 -0
- package/dist/source/middleware/index.d.ts +2 -0
- package/dist/source/middleware/index.js +2 -0
- package/dist/source/middleware/rate-limit.d.ts +4 -0
- package/dist/source/middleware/rate-limit.js +17 -0
- package/dist/source/models/ai-config.d.ts +12 -0
- package/dist/source/models/ai-config.js +87 -0
- package/dist/source/models/anthropic-provider.d.ts +25 -0
- package/dist/source/models/anthropic-provider.js +184 -0
- package/dist/source/models/deepseek-provider.d.ts +20 -0
- package/dist/source/models/deepseek-provider.js +42 -0
- package/dist/source/models/google-provider.d.ts +19 -0
- package/dist/source/models/google-provider.js +56 -0
- package/dist/source/models/manager.d.ts +15 -0
- package/dist/source/models/manager.js +48 -0
- package/dist/source/models/openai-provider.d.ts +22 -0
- package/dist/source/models/openai-provider.js +70 -0
- package/dist/source/models/openrouter-provider.d.ts +36 -0
- package/dist/source/models/openrouter-provider.js +276 -0
- package/dist/source/models/providers.d.ts +33 -0
- package/dist/source/models/providers.js +116 -0
- package/dist/source/models/xai-provider.d.ts +20 -0
- package/dist/source/models/xai-provider.js +47 -0
- package/dist/source/parsing.d.ts +2 -0
- package/dist/source/parsing.js +18 -0
- package/dist/source/prompts/manager.d.ts +19 -0
- package/dist/source/prompts/manager.js +71 -0
- package/dist/source/prompts.d.ts +4 -0
- package/dist/source/prompts.js +158 -0
- package/dist/source/repl-prompt.d.ts +14 -0
- package/dist/source/repl-prompt.js +147 -0
- package/dist/source/repl.d.ts +27 -0
- package/dist/source/repl.js +431 -0
- package/dist/source/terminal/formatting.d.ts +37 -0
- package/dist/source/terminal/formatting.js +106 -0
- package/dist/source/terminal/index.d.ts +94 -0
- package/dist/source/terminal/index.js +420 -0
- package/dist/source/terminal/markdown-utils.d.ts +2 -0
- package/dist/source/terminal/markdown-utils.js +81 -0
- package/dist/source/terminal/markdown.d.ts +1 -0
- package/dist/source/terminal/markdown.js +111 -0
- package/dist/source/terminal/types.d.ts +71 -0
- package/dist/source/terminal/types.js +1 -0
- package/dist/source/terminal-output.d.ts +8 -0
- package/dist/source/terminal-output.js +213 -0
- package/dist/source/terminal-output.test.d.ts +8 -0
- package/dist/source/terminal-output.test.js +213 -0
- package/dist/source/token-tracker.d.ts +14 -0
- package/dist/source/token-tracker.js +53 -0
- package/dist/source/token-utils.d.ts +7 -0
- package/dist/source/token-utils.js +13 -0
- package/dist/source/tools/agent.d.ts +17 -0
- package/dist/source/tools/agent.js +87 -0
- package/dist/source/tools/bash.d.ts +19 -0
- package/dist/source/tools/bash.js +294 -0
- package/dist/source/tools/code-interpreter.d.ts +12 -0
- package/dist/source/tools/code-interpreter.js +131 -0
- package/dist/source/tools/command-validation.d.ts +8 -0
- package/dist/source/tools/command-validation.js +69 -0
- package/dist/source/tools/delete-file.d.ts +12 -0
- package/dist/source/tools/delete-file.js +56 -0
- package/dist/source/tools/directory-tree.d.ts +12 -0
- package/dist/source/tools/directory-tree.js +38 -0
- package/dist/source/tools/edit-file.d.ts +19 -0
- package/dist/source/tools/edit-file.js +107 -0
- package/dist/source/tools/filesystem-utils.d.ts +22 -0
- package/dist/source/tools/filesystem-utils.js +191 -0
- package/dist/source/tools/git-utils.d.ts +14 -0
- package/dist/source/tools/git-utils.js +64 -0
- package/dist/source/tools/grep.d.ts +17 -0
- package/dist/source/tools/grep.js +138 -0
- package/dist/source/tools/index.d.ts +161 -0
- package/dist/source/tools/index.js +209 -0
- package/dist/source/tools/memory-read.d.ts +13 -0
- package/dist/source/tools/memory-read.js +135 -0
- package/dist/source/tools/memory-write.d.ts +12 -0
- package/dist/source/tools/memory-write.js +83 -0
- package/dist/source/tools/move-file.d.ts +13 -0
- package/dist/source/tools/move-file.js +44 -0
- package/dist/source/tools/read-file.d.ts +17 -0
- package/dist/source/tools/read-file.js +86 -0
- package/dist/source/tools/read-multiple-files.d.ts +14 -0
- package/dist/source/tools/read-multiple-files.js +55 -0
- package/dist/source/tools/save-file.d.ts +17 -0
- package/dist/source/tools/save-file.js +98 -0
- package/dist/source/tools/think.d.ts +11 -0
- package/dist/source/tools/think.js +45 -0
- package/dist/source/tools/types.d.ts +29 -0
- package/dist/source/tools/types.js +14 -0
- package/dist/source/tools/web-fetch.d.ts +47 -0
- package/dist/source/tools/web-fetch.js +246 -0
- package/dist/source/tools/web-search.d.ts +13 -0
- package/dist/source/tools/web-search.js +80 -0
- package/dist/source/utils/process.d.ts +36 -0
- package/dist/source/utils/process.js +75 -0
- package/dist/source/version.d.ts +1 -0
- package/dist/source/version.js +21 -0
- package/dist/terminal/formatting.d.ts +37 -0
- package/dist/terminal/formatting.js +106 -0
- package/dist/terminal/index.d.ts +94 -0
- package/dist/terminal/index.js +420 -0
- package/dist/terminal/markdown-utils.d.ts +2 -0
- package/dist/terminal/markdown-utils.js +81 -0
- package/dist/terminal/markdown.d.ts +1 -0
- package/dist/terminal/markdown.js +111 -0
- package/dist/terminal/types.d.ts +71 -0
- package/dist/terminal/types.js +1 -0
- package/dist/terminal-output.d.ts +8 -0
- package/dist/terminal-output.js +213 -0
- package/dist/token-tracker.d.ts +14 -0
- package/dist/token-tracker.js +53 -0
- package/dist/token-utils.d.ts +7 -0
- package/dist/token-utils.js +13 -0
- package/dist/tools/agent.d.ts +17 -0
- package/dist/tools/agent.js +87 -0
- package/dist/tools/bash.d.ts +19 -0
- package/dist/tools/bash.js +294 -0
- package/dist/tools/code-interpreter.d.ts +12 -0
- package/dist/tools/code-interpreter.js +131 -0
- package/dist/tools/command-validation.d.ts +8 -0
- package/dist/tools/command-validation.js +69 -0
- package/dist/tools/delete-file.d.ts +12 -0
- package/dist/tools/delete-file.js +56 -0
- package/dist/tools/directory-tree.d.ts +12 -0
- package/dist/tools/directory-tree.js +38 -0
- package/dist/tools/edit-file.d.ts +19 -0
- package/dist/tools/edit-file.js +107 -0
- package/dist/tools/filesystem-utils.d.ts +22 -0
- package/dist/tools/filesystem-utils.js +191 -0
- package/dist/tools/git-utils.d.ts +14 -0
- package/dist/tools/git-utils.js +64 -0
- package/dist/tools/grep.d.ts +17 -0
- package/dist/tools/grep.js +138 -0
- package/dist/tools/index.d.ts +161 -0
- package/dist/tools/index.js +209 -0
- package/dist/tools/memory-read.d.ts +13 -0
- package/dist/tools/memory-read.js +135 -0
- package/dist/tools/memory-write.d.ts +12 -0
- package/dist/tools/memory-write.js +83 -0
- package/dist/tools/move-file.d.ts +13 -0
- package/dist/tools/move-file.js +44 -0
- package/dist/tools/read-file.d.ts +17 -0
- package/dist/tools/read-file.js +86 -0
- package/dist/tools/read-multiple-files.d.ts +14 -0
- package/dist/tools/read-multiple-files.js +55 -0
- package/dist/tools/save-file.d.ts +17 -0
- package/dist/tools/save-file.js +98 -0
- package/dist/tools/think.d.ts +11 -0
- package/dist/tools/think.js +45 -0
- package/dist/tools/types.d.ts +29 -0
- package/dist/tools/types.js +14 -0
- package/dist/tools/web-fetch.d.ts +47 -0
- package/dist/tools/web-fetch.js +246 -0
- package/dist/tools/web-search.d.ts +13 -0
- package/dist/tools/web-search.js +80 -0
- package/dist/utils/process.d.ts +36 -0
- package/dist/utils/process.js +75 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +21 -0
- package/knip.json +5 -0
- package/package.json +83 -0
- package/source/cli.ts +172 -0
- package/source/commands/application-log-command.ts +53 -0
- package/source/commands/clear-command.ts +14 -0
- package/source/commands/compact-command.ts +64 -0
- package/source/commands/copy-command.ts +55 -0
- package/source/commands/edit-command.ts +63 -0
- package/source/commands/edit-prompt-command.ts +31 -0
- package/source/commands/exit-command.ts +18 -0
- package/source/commands/files-command.ts +85 -0
- package/source/commands/generate-rules-command.ts +82 -0
- package/source/commands/help-command.ts +27 -0
- package/source/commands/init-command.ts +48 -0
- package/source/commands/last-log-command.ts +88 -0
- package/source/commands/manager.ts +151 -0
- package/source/commands/model-command.ts +123 -0
- package/source/commands/paste-command.ts +62 -0
- package/source/commands/prompt-command.ts +150 -0
- package/source/commands/reset-command.ts +22 -0
- package/source/commands/rules-command.ts +76 -0
- package/source/commands/save-command.ts +20 -0
- package/source/commands/types.ts +28 -0
- package/source/commands/usage-command.ts +26 -0
- package/source/config.ts +223 -0
- package/source/conversation-analyzer.ts +115 -0
- package/source/dedent.ts +53 -0
- package/source/formatting.ts +132 -0
- package/source/index.ts +240 -0
- package/source/logger.ts +29 -0
- package/source/mentions.ts +227 -0
- package/source/messages.ts +360 -0
- package/source/middleware/audit-message.ts +133 -0
- package/source/middleware/index.ts +2 -0
- package/source/middleware/rate-limit.ts +24 -0
- package/source/models/ai-config.ts +109 -0
- package/source/models/anthropic-provider.ts +199 -0
- package/source/models/deepseek-provider.ts +53 -0
- package/source/models/google-provider.ts +68 -0
- package/source/models/manager.ts +84 -0
- package/source/models/openai-provider.ts +81 -0
- package/source/models/openrouter-provider.ts +288 -0
- package/source/models/providers.ts +197 -0
- package/source/models/xai-provider.ts +59 -0
- package/source/parsing.ts +20 -0
- package/source/prompts/manager.ts +90 -0
- package/source/prompts.ts +172 -0
- package/source/repl-prompt.ts +196 -0
- package/source/repl.ts +572 -0
- package/source/terminal/formatting.ts +121 -0
- package/source/terminal/index.ts +518 -0
- package/source/terminal/markdown-utils.ts +89 -0
- package/source/terminal/markdown.ts +155 -0
- package/source/terminal/types.ts +84 -0
- package/source/terminal-output.test.ts +266 -0
- package/source/token-tracker.ts +78 -0
- package/source/token-utils.ts +17 -0
- package/source/tools/agent.ts +107 -0
- package/source/tools/bash.ts +367 -0
- package/source/tools/code-interpreter.ts +172 -0
- package/source/tools/command-validation.ts +81 -0
- package/source/tools/delete-file.ts +71 -0
- package/source/tools/directory-tree.ts +54 -0
- package/source/tools/edit-file.ts +155 -0
- package/source/tools/filesystem-utils.ts +265 -0
- package/source/tools/git-utils.ts +70 -0
- package/source/tools/grep.ts +184 -0
- package/source/tools/index.ts +278 -0
- package/source/tools/memory-read.ts +174 -0
- package/source/tools/memory-write.ts +105 -0
- package/source/tools/move-file.ts +59 -0
- package/source/tools/read-file.ts +129 -0
- package/source/tools/read-multiple-files.ts +80 -0
- package/source/tools/save-file.ts +147 -0
- package/source/tools/think.ts +51 -0
- package/source/tools/types.ts +58 -0
- package/source/tools/web-fetch.ts +327 -0
- package/source/tools/web-search.ts +101 -0
- package/source/utils/process.ts +121 -0
- package/source/version.ts +21 -0
- package/test/commands/copy-command.test.ts +69 -0
- package/test/config.test.ts +200 -0
- package/test/terminal/markdown-utils.test.ts +124 -0
- package/test/tools/bash-tool.test.ts +58 -0
- package/test/tools/code-interpreter.test.ts +91 -0
- package/test/tools/command-validation.test.ts +48 -0
- package/tsconfig.build.json +9 -0
- package/tsconfig.json +30 -0
package/source/repl.ts
ADDED
|
@@ -0,0 +1,572 @@
|
|
|
1
|
+
import { isNumber, isRecord } from "@travisennis/stdlib/typeguards";
|
|
2
|
+
import type { AsyncReturnType } from "@travisennis/stdlib/types";
|
|
3
|
+
import {
|
|
4
|
+
generateObject,
|
|
5
|
+
NoSuchToolError,
|
|
6
|
+
type StepResult,
|
|
7
|
+
stepCountIs,
|
|
8
|
+
streamText,
|
|
9
|
+
type ToolCallRepairFunction,
|
|
10
|
+
type ToolSet,
|
|
11
|
+
} from "ai";
|
|
12
|
+
import chalk, { type ChalkInstance } from "chalk";
|
|
13
|
+
import type z from "zod";
|
|
14
|
+
import type { CommandManager } from "./commands/manager.ts";
|
|
15
|
+
import { config as configManager } from "./config.ts";
|
|
16
|
+
import { logger } from "./logger.ts";
|
|
17
|
+
import { processPrompt } from "./mentions.ts";
|
|
18
|
+
import type { MessageHistory } from "./messages.ts";
|
|
19
|
+
import { AiConfig } from "./models/ai-config.ts";
|
|
20
|
+
import type { ModelManager } from "./models/manager.js";
|
|
21
|
+
import type { PromptManager } from "./prompts/manager.ts";
|
|
22
|
+
import { systemPrompt } from "./prompts.ts";
|
|
23
|
+
import { ReplPrompt } from "./repl-prompt.ts";
|
|
24
|
+
import type { Terminal } from "./terminal/index.ts";
|
|
25
|
+
import { isMarkdown } from "./terminal/markdown-utils.ts";
|
|
26
|
+
import type { TokenTracker } from "./token-tracker.ts";
|
|
27
|
+
import type { TokenCounter } from "./token-utils.ts";
|
|
28
|
+
import {
|
|
29
|
+
getCurrentBranch,
|
|
30
|
+
getDiffStat,
|
|
31
|
+
hasUncommittedChanges,
|
|
32
|
+
inGitDirectory,
|
|
33
|
+
} from "./tools/git-utils.ts"; // Modified import
|
|
34
|
+
import { initAgents, initTools } from "./tools/index.ts";
|
|
35
|
+
import type { Message } from "./tools/types.ts";
|
|
36
|
+
|
|
37
|
+
interface ReplOptions {
|
|
38
|
+
messageHistory: MessageHistory;
|
|
39
|
+
promptManager: PromptManager;
|
|
40
|
+
modelManager: ModelManager;
|
|
41
|
+
tokenTracker: TokenTracker;
|
|
42
|
+
terminal: Terminal;
|
|
43
|
+
commands: CommandManager;
|
|
44
|
+
config: Record<PropertyKey, unknown>;
|
|
45
|
+
tokenCounter: TokenCounter;
|
|
46
|
+
toolEvents: Map<string, Message[]>;
|
|
47
|
+
autoAcceptAll: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
type CompleteToolSet = AsyncReturnType<typeof initTools> &
|
|
51
|
+
AsyncReturnType<typeof initAgents>;
|
|
52
|
+
|
|
53
|
+
type OnFinishResult<Tools extends ToolSet = CompleteToolSet> = Omit<
|
|
54
|
+
StepResult<Tools>,
|
|
55
|
+
"stepType" | "isContinued"
|
|
56
|
+
> & {
|
|
57
|
+
/**
|
|
58
|
+
Details for all steps.
|
|
59
|
+
*/
|
|
60
|
+
readonly steps: StepResult<Tools>[];
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export class Repl {
|
|
64
|
+
private options: ReplOptions;
|
|
65
|
+
constructor(options: ReplOptions) {
|
|
66
|
+
this.options = options;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async run() {
|
|
70
|
+
const {
|
|
71
|
+
config,
|
|
72
|
+
promptManager,
|
|
73
|
+
terminal,
|
|
74
|
+
modelManager,
|
|
75
|
+
tokenTracker,
|
|
76
|
+
messageHistory,
|
|
77
|
+
commands,
|
|
78
|
+
tokenCounter,
|
|
79
|
+
toolEvents,
|
|
80
|
+
autoAcceptAll,
|
|
81
|
+
} = this.options;
|
|
82
|
+
|
|
83
|
+
logger.info(config, "Config:");
|
|
84
|
+
|
|
85
|
+
terminal.displayWelcome();
|
|
86
|
+
|
|
87
|
+
const promptHistory: string[] = [];
|
|
88
|
+
|
|
89
|
+
let currentContextWindow = 0;
|
|
90
|
+
messageHistory.on("clear-history", () => {
|
|
91
|
+
currentContextWindow = 0;
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
let prevCb: (() => void) | null = null;
|
|
95
|
+
|
|
96
|
+
while (true) {
|
|
97
|
+
const abortController = new AbortController();
|
|
98
|
+
const { signal } = abortController;
|
|
99
|
+
|
|
100
|
+
const cb = () => {
|
|
101
|
+
abortController.abort();
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
if (prevCb) {
|
|
105
|
+
process.removeListener("SIGINT", prevCb);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Handle Ctrl+C (SIGINT)
|
|
109
|
+
process.on("SIGINT", cb);
|
|
110
|
+
prevCb = cb;
|
|
111
|
+
|
|
112
|
+
const langModel = modelManager.getModel("repl");
|
|
113
|
+
const modelConfig = modelManager.getModelMetadata("repl");
|
|
114
|
+
|
|
115
|
+
const currentDir = process.cwd().split("/").pop() || process.cwd();
|
|
116
|
+
const branch = await getCurrentBranch();
|
|
117
|
+
|
|
118
|
+
let branchDisplay = "";
|
|
119
|
+
if (branch) {
|
|
120
|
+
const hasChanges = await hasUncommittedChanges();
|
|
121
|
+
const asterisk = hasChanges ? "*" : "";
|
|
122
|
+
branchDisplay = ` ${chalk.gray(branch + asterisk)}`;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
terminal.hr();
|
|
126
|
+
terminal.writeln(`${chalk.blue(currentDir)}${branchDisplay}`);
|
|
127
|
+
terminal.writeln(chalk.dim(langModel.modelId));
|
|
128
|
+
terminal.displayProgressBar(
|
|
129
|
+
currentContextWindow,
|
|
130
|
+
modelConfig.contextWindow,
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
if (!promptManager.isPending()) {
|
|
134
|
+
// For interactive input
|
|
135
|
+
const prompt = new ReplPrompt({ commands, history: promptHistory });
|
|
136
|
+
const userInput = await prompt.input();
|
|
137
|
+
prompt.close();
|
|
138
|
+
|
|
139
|
+
// see if the userInput contains a command
|
|
140
|
+
const commandResult = await commands.handle({ userInput });
|
|
141
|
+
if (commandResult.break) {
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
if (commandResult.continue) {
|
|
145
|
+
terminal.lineBreak();
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (!userInput.trim()) {
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// if there is no pending prompt then use the user's input. otherwise, the prompt was loaded from a command
|
|
154
|
+
if (!promptManager.isPending()) {
|
|
155
|
+
const processedPrompt = await processPrompt(userInput, {
|
|
156
|
+
baseDir: process.cwd(),
|
|
157
|
+
model: modelConfig,
|
|
158
|
+
});
|
|
159
|
+
for (const context of processedPrompt.context) {
|
|
160
|
+
promptManager.addContext(context);
|
|
161
|
+
}
|
|
162
|
+
promptManager.set(processedPrompt.message);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
terminal.lineBreak();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// flag to see if the user prompt has added context
|
|
169
|
+
const hasAddedContext = promptManager.hasContext();
|
|
170
|
+
|
|
171
|
+
if (hasAddedContext) {
|
|
172
|
+
terminal.info("Context will be added to prompt.");
|
|
173
|
+
terminal.lineBreak();
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const userPrompt = promptManager.get();
|
|
177
|
+
|
|
178
|
+
const userMsg = promptManager.getUserMessage();
|
|
179
|
+
|
|
180
|
+
messageHistory.appendUserMessage(userMsg);
|
|
181
|
+
|
|
182
|
+
const finalSystemPrompt = await systemPrompt({
|
|
183
|
+
supportsToolCalling: modelConfig.supportsToolCalling,
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
const aiConfig = new AiConfig({
|
|
187
|
+
modelMetadata: modelConfig,
|
|
188
|
+
prompt: userPrompt,
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
const maxTokens = aiConfig.getMaxTokens();
|
|
192
|
+
|
|
193
|
+
const tools = modelConfig.supportsToolCalling
|
|
194
|
+
? {
|
|
195
|
+
...(await initTools({
|
|
196
|
+
terminal,
|
|
197
|
+
tokenCounter,
|
|
198
|
+
autoAcceptAll,
|
|
199
|
+
events: toolEvents,
|
|
200
|
+
})),
|
|
201
|
+
...(await initAgents({
|
|
202
|
+
terminal,
|
|
203
|
+
modelManager,
|
|
204
|
+
tokenTracker,
|
|
205
|
+
tokenCounter,
|
|
206
|
+
events: toolEvents,
|
|
207
|
+
})),
|
|
208
|
+
}
|
|
209
|
+
: undefined;
|
|
210
|
+
|
|
211
|
+
try {
|
|
212
|
+
const result = streamText({
|
|
213
|
+
model: langModel,
|
|
214
|
+
maxOutputTokens: maxTokens,
|
|
215
|
+
messages: [
|
|
216
|
+
{
|
|
217
|
+
role: "system",
|
|
218
|
+
content: finalSystemPrompt,
|
|
219
|
+
providerOptions: {
|
|
220
|
+
anthropic: { cacheControl: { type: "ephemeral" } },
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
...messageHistory.get(),
|
|
224
|
+
],
|
|
225
|
+
temperature: modelConfig.defaultTemperature,
|
|
226
|
+
stopWhen: stepCountIs(60),
|
|
227
|
+
maxRetries: 2,
|
|
228
|
+
providerOptions: aiConfig.getProviderOptions(),
|
|
229
|
+
tools,
|
|
230
|
+
// biome-ignore lint/style/useNamingConvention: third-party controlled
|
|
231
|
+
experimental_repairToolCall: modelConfig.supportsToolCalling
|
|
232
|
+
? toolCallRepair(modelManager, terminal)
|
|
233
|
+
: undefined,
|
|
234
|
+
abortSignal: signal,
|
|
235
|
+
onFinish: async (result) => {
|
|
236
|
+
logger.debug("onFinish called");
|
|
237
|
+
if (result.response.messages.length > 0) {
|
|
238
|
+
messageHistory.appendResponseMessages(result.response.messages);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
terminal.hr();
|
|
242
|
+
|
|
243
|
+
// Notify if configured in project config (acai.json)
|
|
244
|
+
const projectConfig = await configManager.readProjectConfig();
|
|
245
|
+
if (projectConfig.notify) {
|
|
246
|
+
terminal.alert();
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// Create a more visual representation of steps/tool usage
|
|
250
|
+
this.displayToolUse(result, terminal);
|
|
251
|
+
|
|
252
|
+
if (await inGitDirectory()) {
|
|
253
|
+
// Added check
|
|
254
|
+
const stats = await getDiffStat();
|
|
255
|
+
terminal.writeln(
|
|
256
|
+
`${chalk.dim("Files changed:")} ${chalk.yellow(stats.filesChanged)} ` +
|
|
257
|
+
`${chalk.green(`+${stats.insertions}`)} ` + // Insertions first (green)
|
|
258
|
+
`${chalk.red(`-${stats.deletions}`)}`, // Deletions last (red)
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const total =
|
|
263
|
+
(result as { totalUsage?: typeof result.usage }).totalUsage ??
|
|
264
|
+
result.usage;
|
|
265
|
+
const outgoingTokens = isNumber(total.inputTokens)
|
|
266
|
+
? total.inputTokens
|
|
267
|
+
: 0;
|
|
268
|
+
const incomingTokens = isNumber(total.outputTokens)
|
|
269
|
+
? total.outputTokens
|
|
270
|
+
: 0;
|
|
271
|
+
const tokenSummary = `Tokens: ↑ ${outgoingTokens} ↓ ${incomingTokens}`;
|
|
272
|
+
terminal.writeln(chalk.dim(tokenSummary));
|
|
273
|
+
|
|
274
|
+
// Track aggregate usage across all steps when available
|
|
275
|
+
tokenTracker.trackUsage("repl", total);
|
|
276
|
+
|
|
277
|
+
// Derive current context window from final step usage
|
|
278
|
+
const finalTotalTokens = result.usage.totalTokens;
|
|
279
|
+
if (isNumber(finalTotalTokens)) {
|
|
280
|
+
currentContextWindow = finalTotalTokens ?? 0;
|
|
281
|
+
} else {
|
|
282
|
+
// Fallback: find the stopped step
|
|
283
|
+
for (const step of result.steps) {
|
|
284
|
+
if (step.finishReason === "stop") {
|
|
285
|
+
const usage = step.usage;
|
|
286
|
+
currentContextWindow = Number.isNaN(usage.totalTokens)
|
|
287
|
+
? 0
|
|
288
|
+
: (usage.totalTokens ?? 0);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// comment out auto-summarization for now. it's been causing issues.
|
|
294
|
+
// if (currentContextWindow > 70000) {
|
|
295
|
+
// logger.info(
|
|
296
|
+
// `Condensing history from ${currentContextWindow} to 0`,
|
|
297
|
+
// );
|
|
298
|
+
// await messageHistory.summarizeAndReset();
|
|
299
|
+
// }
|
|
300
|
+
|
|
301
|
+
terminal.hr();
|
|
302
|
+
},
|
|
303
|
+
onError: ({ error }) => {
|
|
304
|
+
logger.error(
|
|
305
|
+
error, // Log the full error object
|
|
306
|
+
"Error on REPL streamText",
|
|
307
|
+
);
|
|
308
|
+
terminal.error(
|
|
309
|
+
(error as Error).message.length > 100
|
|
310
|
+
? `${(error as Error).message.slice(0, 100)}...`
|
|
311
|
+
: (error as Error).message,
|
|
312
|
+
);
|
|
313
|
+
},
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
let accumulatedText = "";
|
|
317
|
+
let lastType: "reasoning" | "text" | null = null;
|
|
318
|
+
|
|
319
|
+
for await (const chunk of result.fullStream) {
|
|
320
|
+
// Handle text-related chunks (reasoning or text-delta)
|
|
321
|
+
if (chunk.type === "reasoning-delta" || chunk.type === "text-delta") {
|
|
322
|
+
if (chunk.type === "reasoning-delta") {
|
|
323
|
+
if (lastType !== "reasoning") {
|
|
324
|
+
terminal.writeln(chalk.dim("<think>"));
|
|
325
|
+
}
|
|
326
|
+
terminal.write(chalk.dim(chunk.text)); // Stream reasoning directly
|
|
327
|
+
lastType = "reasoning";
|
|
328
|
+
} else if (chunk.type === "text-delta") {
|
|
329
|
+
if (lastType === "reasoning") {
|
|
330
|
+
// Finishing reasoning: Print </think>
|
|
331
|
+
terminal.writeln(chalk.dim("\n</think>\n"));
|
|
332
|
+
}
|
|
333
|
+
accumulatedText += chunk.text;
|
|
334
|
+
lastType = "text";
|
|
335
|
+
}
|
|
336
|
+
} else if (chunk.type === "tool-result") {
|
|
337
|
+
const messages = toolEvents.get(chunk.toolCallId);
|
|
338
|
+
if (messages) {
|
|
339
|
+
displayToolMessages(messages, terminal);
|
|
340
|
+
toolEvents.delete(chunk.toolCallId);
|
|
341
|
+
} else {
|
|
342
|
+
logger.warn(`No tool events found for ${chunk.toolCallId}`);
|
|
343
|
+
}
|
|
344
|
+
} else {
|
|
345
|
+
// Close thinking tags when moving from reasoning to any other chunk type
|
|
346
|
+
if (lastType === "reasoning") {
|
|
347
|
+
terminal.write(chalk.dim("\n</think>\n\n"));
|
|
348
|
+
}
|
|
349
|
+
// if there is accumulatedText, display it
|
|
350
|
+
if (accumulatedText) {
|
|
351
|
+
terminal.writeln(`${chalk.blue.bold("●")} Response:`);
|
|
352
|
+
terminal.display(accumulatedText, true);
|
|
353
|
+
terminal.lineBreak();
|
|
354
|
+
}
|
|
355
|
+
accumulatedText = "";
|
|
356
|
+
lastType = null;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// Ensure the final closing tag for reasoning is written if it was the last type
|
|
361
|
+
if (lastType === "reasoning") {
|
|
362
|
+
terminal.write(chalk.gray("\n</think>\n\n"));
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// if there is accumulatedText, display it
|
|
366
|
+
if (accumulatedText) {
|
|
367
|
+
terminal.writeln(`${chalk.green.bold("●")} Response:`);
|
|
368
|
+
terminal.display(accumulatedText, true);
|
|
369
|
+
terminal.lineBreak();
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
terminal.lineBreak(); // Add a final newline for clarity
|
|
373
|
+
|
|
374
|
+
await result.consumeStream();
|
|
375
|
+
} catch (e) {
|
|
376
|
+
if (isRecord(e) && isRecord(e["data"]) && "error" in e["data"]) {
|
|
377
|
+
terminal.error(
|
|
378
|
+
(e["data"]["error"] as Record<"message", string>).message,
|
|
379
|
+
);
|
|
380
|
+
} else {
|
|
381
|
+
terminal.error(
|
|
382
|
+
(e as Error).message.length > 100
|
|
383
|
+
? `${(e as Error).message.slice(0, 100)}...`
|
|
384
|
+
: (e as Error).message,
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
terminal.lineBreak();
|
|
388
|
+
if (e instanceof Error) {
|
|
389
|
+
logger.error(e);
|
|
390
|
+
} else {
|
|
391
|
+
logger.error(JSON.stringify(e, null, 2));
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
private displayToolUse(result: OnFinishResult, terminal: Terminal) {
|
|
398
|
+
const toolsCalled: string[] = [];
|
|
399
|
+
const toolColors = new Map<string, ChalkInstance>();
|
|
400
|
+
|
|
401
|
+
const chalkColors = [
|
|
402
|
+
"red",
|
|
403
|
+
"green",
|
|
404
|
+
"yellow",
|
|
405
|
+
"blue",
|
|
406
|
+
"magenta",
|
|
407
|
+
"cyan",
|
|
408
|
+
"white",
|
|
409
|
+
"gray",
|
|
410
|
+
"redBright",
|
|
411
|
+
"greenBright",
|
|
412
|
+
"yellowBright",
|
|
413
|
+
"blueBright",
|
|
414
|
+
"magentaBright",
|
|
415
|
+
"cyanBright",
|
|
416
|
+
"whiteBright",
|
|
417
|
+
"blackBright",
|
|
418
|
+
] as const;
|
|
419
|
+
|
|
420
|
+
terminal.writeln(chalk.dim(`Steps: ${result.steps.length}`));
|
|
421
|
+
|
|
422
|
+
for (const step of result.steps) {
|
|
423
|
+
let currentToolCalls: Array<{ toolName: string }> = [];
|
|
424
|
+
|
|
425
|
+
if (step.toolResults.length > 0) {
|
|
426
|
+
currentToolCalls = step.toolResults;
|
|
427
|
+
} else if (step.toolCalls.length > 0) {
|
|
428
|
+
currentToolCalls = step.toolCalls;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
for (const toolCallOrResult of currentToolCalls) {
|
|
432
|
+
const toolName = toolCallOrResult.toolName;
|
|
433
|
+
if (!toolColors.has(toolName)) {
|
|
434
|
+
const availableColors = chalkColors.filter(
|
|
435
|
+
(color) =>
|
|
436
|
+
!Array.from(toolColors.values()).some((c) => c === chalk[color]),
|
|
437
|
+
);
|
|
438
|
+
const color =
|
|
439
|
+
availableColors.length > 0
|
|
440
|
+
? (availableColors[
|
|
441
|
+
Math.floor(Math.random() * availableColors.length)
|
|
442
|
+
] ?? "white")
|
|
443
|
+
: "white";
|
|
444
|
+
toolColors.set(toolName, chalk[color]);
|
|
445
|
+
}
|
|
446
|
+
toolsCalled.push(toolName);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
if (toolsCalled.length > 0) {
|
|
451
|
+
terminal.writeln(chalk.dim("Tools:"));
|
|
452
|
+
for (const toolCalled of toolsCalled) {
|
|
453
|
+
const colorFn = toolColors.get(toolCalled) ?? chalk.white;
|
|
454
|
+
terminal.write(`${colorFn("██")} `);
|
|
455
|
+
}
|
|
456
|
+
terminal.lineBreak();
|
|
457
|
+
|
|
458
|
+
const uniqueTools = new Set(toolsCalled);
|
|
459
|
+
for (const [index, toolCalled] of Array.from(uniqueTools).entries()) {
|
|
460
|
+
const colorFn = toolColors.get(toolCalled) ?? chalk.white;
|
|
461
|
+
terminal.write(colorFn(toolCalled));
|
|
462
|
+
if (index < new Set(toolsCalled).size - 1) {
|
|
463
|
+
terminal.write(" - ");
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
terminal.lineBreak();
|
|
467
|
+
terminal.lineBreak();
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
function displayToolMessages(messages: Message[], terminal: Terminal) {
|
|
473
|
+
const isError = messages[messages.length - 1]?.event === "tool-error";
|
|
474
|
+
const indicator = isError ? chalk.red.bold("●") : chalk.blue.bold("●");
|
|
475
|
+
const initMessage =
|
|
476
|
+
messages.find((m) => m.event === "tool-init")?.data ?? "Tool Execution";
|
|
477
|
+
|
|
478
|
+
terminal.write(`${indicator} `); // Write indicator without newline (sync)
|
|
479
|
+
terminal.display(initMessage); // Display initial message (async)
|
|
480
|
+
|
|
481
|
+
for (const msg of messages) {
|
|
482
|
+
switch (msg.event) {
|
|
483
|
+
case "tool-update":
|
|
484
|
+
_handleToolUpdateMessage(msg.data, terminal);
|
|
485
|
+
break;
|
|
486
|
+
case "tool-completion":
|
|
487
|
+
_handleToolCompletionMessage(msg.data, terminal);
|
|
488
|
+
break;
|
|
489
|
+
case "tool-error":
|
|
490
|
+
_handleToolErrorMessage(msg.data, terminal);
|
|
491
|
+
break;
|
|
492
|
+
// 'tool-init' is handled before the loop, so no case needed here.
|
|
493
|
+
default:
|
|
494
|
+
// Optional: Log an unexpected event type for debugging, or do nothing.
|
|
495
|
+
logger.debug(`Unhandled tool message event: ${msg.event}`);
|
|
496
|
+
break;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
terminal.lineBreak();
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// Helper function to handle tool update messages
|
|
503
|
+
function _handleToolUpdateMessage(
|
|
504
|
+
data: { primary: string; secondary?: string[] },
|
|
505
|
+
terminal: Terminal,
|
|
506
|
+
) {
|
|
507
|
+
if (data.secondary && data.secondary.length > 0) {
|
|
508
|
+
const content = data.secondary.join("\n");
|
|
509
|
+
if (content.trim().length !== 0) {
|
|
510
|
+
terminal.display(`└── ${data.primary}`);
|
|
511
|
+
terminal.hr();
|
|
512
|
+
if (isMarkdown(content)) {
|
|
513
|
+
terminal.display(content, true);
|
|
514
|
+
} else {
|
|
515
|
+
terminal.write(chalk.green(content));
|
|
516
|
+
terminal.lineBreak();
|
|
517
|
+
}
|
|
518
|
+
terminal.hr();
|
|
519
|
+
}
|
|
520
|
+
} else {
|
|
521
|
+
terminal.display(`└── ${data.primary}`);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// Helper function to handle tool completion messages
|
|
526
|
+
function _handleToolCompletionMessage(data: string, terminal: Terminal) {
|
|
527
|
+
terminal.display(`└── ${data}`);
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
// Helper function to handle tool error messages
|
|
531
|
+
function _handleToolErrorMessage(data: string, terminal: Terminal) {
|
|
532
|
+
terminal.write("└── ");
|
|
533
|
+
terminal.error(data);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
const toolCallRepair = (modelManager: ModelManager, terminal: Terminal) => {
|
|
537
|
+
const fn: ToolCallRepairFunction<CompleteToolSet> = async ({
|
|
538
|
+
toolCall,
|
|
539
|
+
tools,
|
|
540
|
+
inputSchema,
|
|
541
|
+
error,
|
|
542
|
+
}) => {
|
|
543
|
+
if (NoSuchToolError.isInstance(error)) {
|
|
544
|
+
return null; // do not attempt to fix invalid tool names
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
terminal.warn(`Attempting to repair tool call: ${toolCall.toolName}.`);
|
|
548
|
+
terminal.lineBreak();
|
|
549
|
+
|
|
550
|
+
const tool = tools[toolCall.toolName as keyof typeof tools];
|
|
551
|
+
|
|
552
|
+
try {
|
|
553
|
+
const { object: repairedArgs } = await generateObject({
|
|
554
|
+
model: modelManager.getModel("tool-repair"),
|
|
555
|
+
schema: tool.inputSchema as z.ZodSchema<unknown>,
|
|
556
|
+
prompt: [
|
|
557
|
+
`The model tried to call the tool "${toolCall.toolName}" with the following arguments:`,
|
|
558
|
+
JSON.stringify(toolCall.input),
|
|
559
|
+
"The tool accepts the following schema:",
|
|
560
|
+
JSON.stringify(inputSchema(toolCall)),
|
|
561
|
+
"Please fix the arguments.",
|
|
562
|
+
].join("\n"),
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
return { ...toolCall, args: JSON.stringify(repairedArgs) };
|
|
566
|
+
} catch (err) {
|
|
567
|
+
logger.error(err, `Failed to repair tool call: ${toolCall.toolName}.`);
|
|
568
|
+
return null;
|
|
569
|
+
}
|
|
570
|
+
};
|
|
571
|
+
return fn;
|
|
572
|
+
};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Terminal Formatting Utilities
|
|
3
|
+
*
|
|
4
|
+
* Provides functions for formatting and displaying text in the terminal.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import chalk from "chalk";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Clear the terminal screen
|
|
11
|
+
*/
|
|
12
|
+
export function clearScreen(): void {
|
|
13
|
+
// Clear screen and move cursor to top-left
|
|
14
|
+
process.stdout.write("\x1b[2J\x1b[0f");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Clear the terminal screen including scrollback buffer
|
|
19
|
+
*
|
|
20
|
+
* Unlike clearScreen, this function:
|
|
21
|
+
* 1. Clears the entire screen (\x1b[2J)
|
|
22
|
+
* 2. Clears the scrollback buffer (\x1b[3J)
|
|
23
|
+
* 3. Moves cursor to home position (\x1b[H)
|
|
24
|
+
* 4. Returns a Promise that resolves when the write operation completes
|
|
25
|
+
*
|
|
26
|
+
* @returns Promise that resolves when the terminal has been cleared
|
|
27
|
+
*/
|
|
28
|
+
export function clearTerminal(): Promise<void> {
|
|
29
|
+
return new Promise((resolve) => {
|
|
30
|
+
process.stdout.write("\x1b[2J\x1b[3J\x1b[H", () => {
|
|
31
|
+
resolve();
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Sets the terminal title
|
|
38
|
+
*/
|
|
39
|
+
export function setTerminalTitle(title: string): void {
|
|
40
|
+
if (process.platform === "win32") {
|
|
41
|
+
process.title = title ? `✳✳ ${title}` : title;
|
|
42
|
+
} else {
|
|
43
|
+
process.stdout.write(`\x1b]0;${title ? `✳✳ ${title}` : ""}\x07`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Get the terminal size (rows and columns)
|
|
49
|
+
*/
|
|
50
|
+
export function getTerminalSize(): { rows: number; columns: number } {
|
|
51
|
+
// Default to a reasonable size if we can't determine the actual size
|
|
52
|
+
const defaultSize = { rows: 24, columns: 80 };
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
if (process.stdout.isTTY) {
|
|
56
|
+
return {
|
|
57
|
+
rows: process.stdout.rows || defaultSize.rows,
|
|
58
|
+
columns: process.stdout.columns || defaultSize.columns,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
} catch (_error) {
|
|
62
|
+
// Ignore errors
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return defaultSize;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Word wrap text to the specified width
|
|
70
|
+
*/
|
|
71
|
+
export function wordWrap(text: string, width: number): string {
|
|
72
|
+
const lines = text.split("\n");
|
|
73
|
+
|
|
74
|
+
return lines.map((line) => wrapLine(line, width)).join("\n");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function wrapLine(line: string, width: number): string {
|
|
78
|
+
// If the line is a code block or already shorter than the width, leave it as is
|
|
79
|
+
if (line.trim().startsWith("┃") || line.length <= width) {
|
|
80
|
+
return line;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Word wrap the line
|
|
84
|
+
const words = line.split(" ");
|
|
85
|
+
const wrappedLines: string[] = [];
|
|
86
|
+
let currentLine = "";
|
|
87
|
+
|
|
88
|
+
for (const word of words) {
|
|
89
|
+
// If adding this word would exceed the width
|
|
90
|
+
if (currentLine.length + word.length + 1 > width) {
|
|
91
|
+
// Add the current line to wrapped lines if it's not empty
|
|
92
|
+
if (currentLine) {
|
|
93
|
+
wrappedLines.push(currentLine);
|
|
94
|
+
currentLine = word;
|
|
95
|
+
} else {
|
|
96
|
+
// If the current line is empty, it means the word itself is longer than the width
|
|
97
|
+
wrappedLines.push(word);
|
|
98
|
+
}
|
|
99
|
+
} else {
|
|
100
|
+
// Add the word to the current line
|
|
101
|
+
currentLine = currentLine ? `${currentLine} ${word}` : word;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Add the last line if it's not empty
|
|
106
|
+
if (currentLine) {
|
|
107
|
+
wrappedLines.push(currentLine);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return wrappedLines.join("\n");
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// const ESC = "\u001B[";
|
|
114
|
+
const OSC = "\u001B]";
|
|
115
|
+
const BEL = "\u0007";
|
|
116
|
+
const SEP = ";";
|
|
117
|
+
|
|
118
|
+
export const link = (text: string, url: string) =>
|
|
119
|
+
chalk.underline.blue(
|
|
120
|
+
[OSC, "8", SEP, SEP, url, BEL, text, OSC, "8", SEP, SEP, BEL].join(""),
|
|
121
|
+
);
|