@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
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import EventEmitter from "node:events";
|
|
2
|
+
import { readdir, readFile, writeFile } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { isString } from "@travisennis/stdlib/typeguards";
|
|
5
|
+
import {
|
|
6
|
+
type AssistantModelMessage,
|
|
7
|
+
generateText,
|
|
8
|
+
type ImagePart, // Added ImagePart
|
|
9
|
+
type ModelMessage,
|
|
10
|
+
type TextPart,
|
|
11
|
+
type ToolModelMessage,
|
|
12
|
+
type UserModelMessage,
|
|
13
|
+
} from "ai";
|
|
14
|
+
import type { ModelManager } from "./models/manager.ts";
|
|
15
|
+
import type { TokenTracker } from "./token-tracker.ts";
|
|
16
|
+
|
|
17
|
+
// Define a type for the items that can be passed in the first argument
|
|
18
|
+
export type UserMessageContentItem = string | ImagePart;
|
|
19
|
+
|
|
20
|
+
export function createUserMessage(
|
|
21
|
+
contentItems: UserMessageContentItem[],
|
|
22
|
+
prompt?: string,
|
|
23
|
+
): UserModelMessage {
|
|
24
|
+
const messageParts: (TextPart | ImagePart)[] = [];
|
|
25
|
+
|
|
26
|
+
// Process content items (images and pre-defined texts)
|
|
27
|
+
for (const item of contentItems) {
|
|
28
|
+
if (typeof item === "string") {
|
|
29
|
+
if (item.trim().length > 0) {
|
|
30
|
+
messageParts.push({ type: "text", text: item });
|
|
31
|
+
}
|
|
32
|
+
} else if (item.type === "image") {
|
|
33
|
+
messageParts.push(item);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Add the main prompt text if provided
|
|
38
|
+
if (prompt && prompt.trim().length > 0) {
|
|
39
|
+
messageParts.push({ type: "text", text: prompt });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
role: "user",
|
|
44
|
+
content: messageParts,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function createAssistantMessage(content: string): AssistantModelMessage {
|
|
49
|
+
return {
|
|
50
|
+
role: "assistant",
|
|
51
|
+
content: [
|
|
52
|
+
{
|
|
53
|
+
type: "text",
|
|
54
|
+
text: content,
|
|
55
|
+
},
|
|
56
|
+
],
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
A message that was generated during the generation process.
|
|
62
|
+
It can be either an assistant message or a tool message.
|
|
63
|
+
*/
|
|
64
|
+
type ResponseMessage = AssistantModelMessage | ToolModelMessage;
|
|
65
|
+
|
|
66
|
+
export type SavedMessageHistory = {
|
|
67
|
+
title: string;
|
|
68
|
+
createdAt: Date;
|
|
69
|
+
updatedAt: Date;
|
|
70
|
+
messages: ModelMessage[];
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
type RawMessageHistory = Omit<
|
|
74
|
+
SavedMessageHistory,
|
|
75
|
+
"createdAt" | "updatedAt"
|
|
76
|
+
> & {
|
|
77
|
+
createdAt?: string;
|
|
78
|
+
updatedAt?: string;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
interface MessageHistoryEvents {
|
|
82
|
+
"update-title": [string];
|
|
83
|
+
"clear-history": [];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export class MessageHistory extends EventEmitter<MessageHistoryEvents> {
|
|
87
|
+
private history: ModelMessage[];
|
|
88
|
+
private title: string;
|
|
89
|
+
private createdAt: Date;
|
|
90
|
+
private updatedAt: Date;
|
|
91
|
+
private stateDir: string;
|
|
92
|
+
private modelManager: ModelManager;
|
|
93
|
+
private tokenTracker: TokenTracker;
|
|
94
|
+
|
|
95
|
+
constructor({
|
|
96
|
+
stateDir,
|
|
97
|
+
modelManager,
|
|
98
|
+
tokenTracker,
|
|
99
|
+
}: {
|
|
100
|
+
stateDir: string;
|
|
101
|
+
modelManager: ModelManager;
|
|
102
|
+
tokenTracker: TokenTracker;
|
|
103
|
+
}) {
|
|
104
|
+
super();
|
|
105
|
+
this.history = [];
|
|
106
|
+
this.title = "";
|
|
107
|
+
this.createdAt = new Date();
|
|
108
|
+
this.updatedAt = new Date();
|
|
109
|
+
this.stateDir = stateDir;
|
|
110
|
+
this.modelManager = modelManager;
|
|
111
|
+
this.tokenTracker = tokenTracker;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
private validMessage(msg: ModelMessage) {
|
|
115
|
+
// Filter out messages with empty content arrays
|
|
116
|
+
if (Array.isArray(msg.content) && msg.content.length === 0) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Filter out assistant messages with empty text fields
|
|
121
|
+
if (
|
|
122
|
+
msg.role === "assistant" &&
|
|
123
|
+
Array.isArray(msg.content) &&
|
|
124
|
+
msg.content.length === 1 &&
|
|
125
|
+
msg.content[0]?.type === "text" &&
|
|
126
|
+
msg.content[0]?.text === ""
|
|
127
|
+
) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
get() {
|
|
135
|
+
return [...this.history].filter(this.validMessage);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
clear() {
|
|
139
|
+
this.history.length = 0;
|
|
140
|
+
this.emit("clear-history");
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
appendUserMessage(msg: string): void;
|
|
144
|
+
appendUserMessage(msg: UserModelMessage): void;
|
|
145
|
+
appendUserMessage(msg: string | UserModelMessage) {
|
|
146
|
+
const now = new Date();
|
|
147
|
+
const msgObj = isString(msg) ? createUserMessage([], msg) : msg;
|
|
148
|
+
if (
|
|
149
|
+
this.history.length === 0 &&
|
|
150
|
+
msgObj.content &&
|
|
151
|
+
msgObj.content.length > 0
|
|
152
|
+
) {
|
|
153
|
+
const textPart = msgObj.content.at(-1) as TextPart;
|
|
154
|
+
if (textPart?.text && textPart.text.trim() !== "") {
|
|
155
|
+
this.generateTitle(textPart.text);
|
|
156
|
+
}
|
|
157
|
+
this.createdAt = now;
|
|
158
|
+
}
|
|
159
|
+
this.updatedAt = now;
|
|
160
|
+
this.history.push(msgObj);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
appendAssistantMessage(msg: string): void;
|
|
164
|
+
appendAssistantMessage(msg: AssistantModelMessage): void;
|
|
165
|
+
appendAssistantMessage(msg: string | AssistantModelMessage) {
|
|
166
|
+
this.updatedAt = new Date();
|
|
167
|
+
const msgObj = isString(msg) ? createAssistantMessage(msg) : msg;
|
|
168
|
+
this.history.push(msgObj);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
appendResponseMessages(responseMessages: ResponseMessage[]) {
|
|
172
|
+
this.updatedAt = new Date();
|
|
173
|
+
// Filter out messages with empty content arrays
|
|
174
|
+
const validMessages = responseMessages.filter(this.validMessage);
|
|
175
|
+
this.history.push(...validMessages);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
isEmpty() {
|
|
179
|
+
return this.history.length === 0;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async save() {
|
|
183
|
+
const msgHistoryDir = this.stateDir;
|
|
184
|
+
const timestamp = new Date().toISOString().replace(/:/g, "-");
|
|
185
|
+
const fileName = `message-history-${timestamp}.json`;
|
|
186
|
+
const filePath = join(msgHistoryDir, fileName);
|
|
187
|
+
|
|
188
|
+
const output: SavedMessageHistory = {
|
|
189
|
+
title: this.title,
|
|
190
|
+
createdAt: this.createdAt,
|
|
191
|
+
updatedAt: this.updatedAt,
|
|
192
|
+
messages: this.history,
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
await writeFile(filePath, JSON.stringify(output, null, 2));
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
private async generateTitle(message: string) {
|
|
199
|
+
// Skip title generation if message is empty
|
|
200
|
+
if (!message || message.trim() === "") {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const app = "title-conversation";
|
|
205
|
+
|
|
206
|
+
const systemPrompt =
|
|
207
|
+
"You are an assistant who task is to analyze messages to generate a conversation topic that can be used as a conversation title. For each message, generate a 4-7 word title that captures the topic. Return only the title with no other text.\n\nExamples:\nMessage:\nHow do I implement authentication in my Express app?\nTitle: Express Authentication Implementation\n\nMessage:\nCan you help me debug this React component that isn't rendering correctly?\nTitle:React Component Rendering Debug";
|
|
208
|
+
try {
|
|
209
|
+
const { text, usage } = await generateText({
|
|
210
|
+
model: this.modelManager.getModel(app),
|
|
211
|
+
system: systemPrompt,
|
|
212
|
+
prompt: `Request:\n${message}\nTitle:`,
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
this.tokenTracker.trackUsage(app, usage);
|
|
216
|
+
|
|
217
|
+
if (text && text.split(" ").length < 10) {
|
|
218
|
+
this.title = text;
|
|
219
|
+
this.emit("update-title", this.title);
|
|
220
|
+
}
|
|
221
|
+
} catch (error) {
|
|
222
|
+
console.error(error);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
getFirstUserMessage(): UserModelMessage | undefined {
|
|
227
|
+
const firstUser = this.get().find(
|
|
228
|
+
(msg): msg is UserModelMessage => msg.role === "user",
|
|
229
|
+
);
|
|
230
|
+
return firstUser;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
static async load(
|
|
234
|
+
stateDir: string,
|
|
235
|
+
count = 10, // Add count parameter with default
|
|
236
|
+
): Promise<SavedMessageHistory[]> {
|
|
237
|
+
try {
|
|
238
|
+
const files = await readdir(stateDir);
|
|
239
|
+
const messageHistoryFiles = files
|
|
240
|
+
.filter(
|
|
241
|
+
(file) =>
|
|
242
|
+
file.startsWith("message-history-") && file.endsWith(".json"),
|
|
243
|
+
)
|
|
244
|
+
.sort((a, b) => {
|
|
245
|
+
// Extract timestamps and compare in reverse order (newest first)
|
|
246
|
+
const timeA = a.replace("message-history-", "").replace(".json", "");
|
|
247
|
+
const timeB = b.replace("message-history-", "").replace(".json", "");
|
|
248
|
+
return timeB.localeCompare(timeA); // Newest first
|
|
249
|
+
})
|
|
250
|
+
.slice(0, count); // Use the count parameter here
|
|
251
|
+
|
|
252
|
+
const fileReadPromises = messageHistoryFiles.map(async (fileName) => {
|
|
253
|
+
const filePath = join(stateDir, fileName);
|
|
254
|
+
try {
|
|
255
|
+
const content = await readFile(filePath, "utf-8");
|
|
256
|
+
const parsed = JSON.parse(content) as RawMessageHistory;
|
|
257
|
+
const result: SavedMessageHistory =
|
|
258
|
+
parsed as unknown as SavedMessageHistory;
|
|
259
|
+
// Basic validation - ensure messages array exists
|
|
260
|
+
if (parsed && Array.isArray(parsed.messages)) {
|
|
261
|
+
// Convert date strings back to Date objects
|
|
262
|
+
result.createdAt = new Date(parsed.createdAt ?? 0);
|
|
263
|
+
result.updatedAt = new Date(parsed.updatedAt ?? 0);
|
|
264
|
+
return result;
|
|
265
|
+
}
|
|
266
|
+
} catch (error) {
|
|
267
|
+
console.error(`Error reading or parsing file ${filePath}:`, error);
|
|
268
|
+
}
|
|
269
|
+
return null; // Return null for failed reads/parses
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
const results = await Promise.all(fileReadPromises);
|
|
273
|
+
// Filter out null results (failed reads/parses)
|
|
274
|
+
//sort results by result.updatedAt which is a Date
|
|
275
|
+
return results
|
|
276
|
+
.filter((result): result is SavedMessageHistory => result !== null)
|
|
277
|
+
.sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime());
|
|
278
|
+
} catch (error) {
|
|
279
|
+
// Handle cases where the directory might not exist or other readdir errors
|
|
280
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") {
|
|
281
|
+
console.error(`Error loading message history from ${stateDir}:`, error);
|
|
282
|
+
}
|
|
283
|
+
// Return empty array if directory doesn't exist or other read errors occur
|
|
284
|
+
return [];
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// Method to restore state from a SavedMessageHistory object
|
|
289
|
+
restore(savedHistory: SavedMessageHistory): void {
|
|
290
|
+
this.title = savedHistory.title;
|
|
291
|
+
// Ensure dates are Date objects, though load should handle this
|
|
292
|
+
this.createdAt =
|
|
293
|
+
typeof savedHistory.createdAt === "string"
|
|
294
|
+
? new Date(savedHistory.createdAt)
|
|
295
|
+
: savedHistory.createdAt;
|
|
296
|
+
this.updatedAt =
|
|
297
|
+
typeof savedHistory.updatedAt === "string"
|
|
298
|
+
? new Date(savedHistory.updatedAt)
|
|
299
|
+
: savedHistory.updatedAt;
|
|
300
|
+
this.history = [...savedHistory.messages]; // Use the correct internal property name and create a copy
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Normalizes an array of messages for API consumption by:
|
|
306
|
+
* 1. Filtering out progress-type messages
|
|
307
|
+
* 2. Processing user and assistant messages
|
|
308
|
+
* 3. Handling tool results by either:
|
|
309
|
+
* - Adding them as new messages if they're the first tool result
|
|
310
|
+
* - Adding them as new messages if the previous message wasn't a tool result
|
|
311
|
+
* - Merging them with the previous message if it was also a tool result
|
|
312
|
+
*
|
|
313
|
+
* This consolidation of sequential tool results into a single message
|
|
314
|
+
* ensures proper formatting for API consumption while maintaining the
|
|
315
|
+
* logical flow of the conversation.
|
|
316
|
+
*
|
|
317
|
+
* @param messages - Array of messages to normalize
|
|
318
|
+
* @returns Normalized array of user and assistant messages ready for API
|
|
319
|
+
*/
|
|
320
|
+
export function normalizeMessagesForApi(
|
|
321
|
+
messages: ModelMessage[],
|
|
322
|
+
): ModelMessage[] {
|
|
323
|
+
const result: ModelMessage[] = [];
|
|
324
|
+
for (const message of messages) {
|
|
325
|
+
switch (message.role) {
|
|
326
|
+
case "user": {
|
|
327
|
+
result.push(message);
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
case "tool": {
|
|
331
|
+
// If the last message is not a tool result, add it to the result
|
|
332
|
+
const lastMessage = result.at(-1);
|
|
333
|
+
if (
|
|
334
|
+
!lastMessage ||
|
|
335
|
+
lastMessage.role === "assistant" ||
|
|
336
|
+
!Array.isArray(lastMessage.content) ||
|
|
337
|
+
lastMessage.content[0]?.type !== "tool-result"
|
|
338
|
+
) {
|
|
339
|
+
result.push(message);
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// Otherwise, merge the current message with the last message
|
|
344
|
+
result[result.indexOf(lastMessage)] = {
|
|
345
|
+
...lastMessage,
|
|
346
|
+
// biome-ignore lint/suspicious/noExplicitAny: can't figure out type
|
|
347
|
+
content: [...lastMessage.content, ...message.content] as any, // #FIXME figure out what type this should be
|
|
348
|
+
};
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
case "assistant": {
|
|
352
|
+
result.push(message);
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
default:
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
return result;
|
|
360
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import type {
|
|
4
|
+
LanguageModelV2Middleware,
|
|
5
|
+
LanguageModelV2Prompt,
|
|
6
|
+
LanguageModelV2StreamPart,
|
|
7
|
+
LanguageModelV2TextPart,
|
|
8
|
+
} from "@ai-sdk/provider";
|
|
9
|
+
import type { LanguageModelUsage } from "ai";
|
|
10
|
+
|
|
11
|
+
interface AuditRecord {
|
|
12
|
+
model: string;
|
|
13
|
+
app: string;
|
|
14
|
+
messages: LanguageModelV2Prompt;
|
|
15
|
+
usage: LanguageModelUsage;
|
|
16
|
+
timestamp: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Writes an audit record to the specified file, overwriting any existing content.
|
|
21
|
+
* Ensures the directory exists before writing.
|
|
22
|
+
*
|
|
23
|
+
* @param filePath - The path to the file where the audit record will be saved.
|
|
24
|
+
* @param content - The audit record object to write.
|
|
25
|
+
*/
|
|
26
|
+
const writeAuditRecord = async (
|
|
27
|
+
app: string,
|
|
28
|
+
filePath: string,
|
|
29
|
+
content: AuditRecord,
|
|
30
|
+
): Promise<void> => {
|
|
31
|
+
try {
|
|
32
|
+
const now = new Date();
|
|
33
|
+
const path = join(filePath, `${now.toISOString()}-${app}-message.json`);
|
|
34
|
+
// Ensure directory exists
|
|
35
|
+
await mkdir(dirname(path), { recursive: true });
|
|
36
|
+
await writeFile(path, `${JSON.stringify(content, null, 2)}`);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
console.error("Error writing audit file:", error);
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const auditMessage = ({
|
|
44
|
+
filePath = "messages",
|
|
45
|
+
app = "default",
|
|
46
|
+
}: {
|
|
47
|
+
filePath: string;
|
|
48
|
+
app: string;
|
|
49
|
+
}) => {
|
|
50
|
+
const middleware: LanguageModelV2Middleware = {
|
|
51
|
+
wrapGenerate: async ({ doGenerate, params, model }) => {
|
|
52
|
+
const result = await doGenerate();
|
|
53
|
+
|
|
54
|
+
const msg: AuditRecord = {
|
|
55
|
+
model: model.modelId,
|
|
56
|
+
app,
|
|
57
|
+
messages: [...params.prompt].concat({
|
|
58
|
+
role: "assistant",
|
|
59
|
+
content: [
|
|
60
|
+
{
|
|
61
|
+
type: "text",
|
|
62
|
+
// biome-ignore lint/suspicious/noExplicitAny: work-around on type issue
|
|
63
|
+
text: (result as any).text,
|
|
64
|
+
} as LanguageModelV2TextPart,
|
|
65
|
+
],
|
|
66
|
+
}),
|
|
67
|
+
usage: result.usage,
|
|
68
|
+
timestamp: Date.now(),
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
await writeAuditRecord(app, filePath, msg);
|
|
72
|
+
|
|
73
|
+
return result;
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
wrapStream: async ({ doStream, params, model }) => {
|
|
77
|
+
const { stream, ...rest } = await doStream();
|
|
78
|
+
|
|
79
|
+
let generatedText = "";
|
|
80
|
+
let usage: LanguageModelUsage = {
|
|
81
|
+
inputTokens: 0,
|
|
82
|
+
outputTokens: 0,
|
|
83
|
+
totalTokens: 0,
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const transformStream = new TransformStream<
|
|
87
|
+
LanguageModelV2StreamPart,
|
|
88
|
+
LanguageModelV2StreamPart
|
|
89
|
+
>({
|
|
90
|
+
transform(chunk, controller) {
|
|
91
|
+
if (chunk.type === "text-delta") {
|
|
92
|
+
generatedText += chunk.delta;
|
|
93
|
+
}
|
|
94
|
+
if (chunk.type === "finish") {
|
|
95
|
+
usage = chunk.usage;
|
|
96
|
+
}
|
|
97
|
+
controller.enqueue(chunk);
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
async flush() {
|
|
101
|
+
const msg: AuditRecord = {
|
|
102
|
+
model: model.modelId,
|
|
103
|
+
app,
|
|
104
|
+
messages: generatedText
|
|
105
|
+
? [
|
|
106
|
+
...params.prompt,
|
|
107
|
+
{
|
|
108
|
+
role: "assistant",
|
|
109
|
+
content: [
|
|
110
|
+
{
|
|
111
|
+
type: "text",
|
|
112
|
+
text: generatedText,
|
|
113
|
+
},
|
|
114
|
+
],
|
|
115
|
+
},
|
|
116
|
+
]
|
|
117
|
+
: [...params.prompt],
|
|
118
|
+
usage,
|
|
119
|
+
timestamp: Date.now(),
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
await writeAuditRecord(app, filePath, msg);
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
stream: stream.pipeThrough(transformStream),
|
|
128
|
+
...rest,
|
|
129
|
+
};
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
return middleware;
|
|
133
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { LanguageModelV2Middleware } from "@ai-sdk/provider";
|
|
2
|
+
import pThrottle from "p-throttle";
|
|
3
|
+
|
|
4
|
+
export const createRateLimitMiddleware = ({
|
|
5
|
+
requestsPerMinute,
|
|
6
|
+
}: {
|
|
7
|
+
requestsPerMinute: number;
|
|
8
|
+
}): LanguageModelV2Middleware => {
|
|
9
|
+
const throttle = pThrottle({
|
|
10
|
+
limit: requestsPerMinute,
|
|
11
|
+
interval: 60 * 1000, // 1 minute
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
wrapGenerate: ({ doGenerate }) => {
|
|
16
|
+
const throttledGenerate = throttle(doGenerate);
|
|
17
|
+
return Promise.resolve(throttledGenerate());
|
|
18
|
+
},
|
|
19
|
+
wrapStream: ({ doStream }) => {
|
|
20
|
+
const throttledStream = throttle(doStream);
|
|
21
|
+
return throttledStream();
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { SharedV2ProviderMetadata } from "@ai-sdk/provider";
|
|
2
|
+
import type { ModelMetadata } from "./providers.ts";
|
|
3
|
+
|
|
4
|
+
type Effort = "none" | "low" | "medium" | "high";
|
|
5
|
+
|
|
6
|
+
const THINKING_TIERS: {
|
|
7
|
+
pattern: RegExp;
|
|
8
|
+
budget: number;
|
|
9
|
+
effort: Effort;
|
|
10
|
+
}[] = [
|
|
11
|
+
{
|
|
12
|
+
pattern:
|
|
13
|
+
/\b(ultrathink|think super hard|think really hard|think intensely)\b/i,
|
|
14
|
+
budget: 31999,
|
|
15
|
+
effort: "high",
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
pattern: /\b(megathink|think (very )?hard|think (a lot|more|about it))\b/i,
|
|
19
|
+
budget: 10000,
|
|
20
|
+
effort: "medium",
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
pattern: /\bthink\b/i, // Catch-all for standalone "think"
|
|
24
|
+
budget: 4000,
|
|
25
|
+
effort: "low",
|
|
26
|
+
},
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
function calculateThinkingLevel(userInput: string): {
|
|
30
|
+
tokenBudget: number;
|
|
31
|
+
effort: Effort;
|
|
32
|
+
} {
|
|
33
|
+
let tokenBudget = 0; // Default
|
|
34
|
+
let effort: Effort = "none";
|
|
35
|
+
for (const tier of THINKING_TIERS) {
|
|
36
|
+
if (tier.pattern.test(userInput)) {
|
|
37
|
+
tokenBudget = tier.budget;
|
|
38
|
+
effort = tier.effort;
|
|
39
|
+
break; // Use highest priority match
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return { tokenBudget, effort };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export class AiConfig {
|
|
46
|
+
private modelMetadata: ModelMetadata;
|
|
47
|
+
private prompt: string;
|
|
48
|
+
constructor({
|
|
49
|
+
modelMetadata,
|
|
50
|
+
prompt,
|
|
51
|
+
}: { modelMetadata: ModelMetadata; prompt: string }) {
|
|
52
|
+
this.modelMetadata = modelMetadata;
|
|
53
|
+
this.prompt = prompt;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
getMaxTokens() {
|
|
57
|
+
const modelConfig = this.modelMetadata;
|
|
58
|
+
const thinkingLevel = calculateThinkingLevel(this.prompt);
|
|
59
|
+
const maxTokens =
|
|
60
|
+
modelConfig.provider === "anthropic" && modelConfig.supportsReasoning
|
|
61
|
+
? modelConfig.maxOutputTokens - thinkingLevel.tokenBudget
|
|
62
|
+
: modelConfig.maxOutputTokens;
|
|
63
|
+
return maxTokens;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
getProviderOptions(): SharedV2ProviderMetadata {
|
|
67
|
+
const modelConfig = this.modelMetadata;
|
|
68
|
+
const thinkingLevel = calculateThinkingLevel(this.prompt);
|
|
69
|
+
|
|
70
|
+
if (modelConfig.supportsReasoning && thinkingLevel.effort !== "none") {
|
|
71
|
+
switch (modelConfig.provider) {
|
|
72
|
+
case "anthropic":
|
|
73
|
+
return {
|
|
74
|
+
anthropic: {
|
|
75
|
+
thinking: {
|
|
76
|
+
type: "enabled",
|
|
77
|
+
budgetTokens: thinkingLevel.tokenBudget,
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
case "openai":
|
|
82
|
+
return { openai: { reasoningEffort: thinkingLevel.effort } };
|
|
83
|
+
case "google": {
|
|
84
|
+
return {
|
|
85
|
+
google: {
|
|
86
|
+
thinkingConfig: {
|
|
87
|
+
thinkingBudget: thinkingLevel.tokenBudget,
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
case "openrouter": {
|
|
93
|
+
return {
|
|
94
|
+
openrouter: {
|
|
95
|
+
reasoning: {
|
|
96
|
+
enabled: true,
|
|
97
|
+
effort: thinkingLevel.effort,
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
default:
|
|
103
|
+
return {};
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
// If supportsReasoning is false, or no provider case matched
|
|
107
|
+
return {};
|
|
108
|
+
}
|
|
109
|
+
}
|