diff --git a/.dockerignore b/.dockerignore index 6359978833..4579d61580 100644 --- a/.dockerignore +++ b/.dockerignore @@ -76,14 +76,18 @@ src/node_modules !pnpm-workspace.yaml !scripts/bootstrap.mjs !apps/web-evals/ +!apps/cli/ !src/ !webview-ui/ !packages/evals/.docker/entrypoints/runner.sh !packages/build/ !packages/config-eslint/ !packages/config-typescript/ +!packages/core/ !packages/evals/ !packages/ipc/ !packages/telemetry/ !packages/types/ +!packages/vscode-shim/ +!packages/cloud/ !locales/ diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml index 9e22303a35..443842c856 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yml +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -76,16 +76,14 @@ body: label: API Provider (optional) options: - Anthropic - - AWS Bedrock + - Amazon Bedrock - Chutes AI - DeepSeek - Featherless AI - Fireworks AI - - Glama - Google Gemini - Google Vertex AI - Groq - - Human Relay Provider - LiteLLM - LM Studio - Mistral AI diff --git a/.gitignore b/.gitignore index e044fc32a7..54cf66cee7 100644 --- a/.gitignore +++ b/.gitignore @@ -49,3 +49,6 @@ logs # Qdrant qdrant_storage/ + +# Architect plans +plans/ \ No newline at end of file diff --git a/.roo/commands/release.md b/.roo/commands/release.md index 8adf57e6f0..2e09783a58 100644 --- a/.roo/commands/release.md +++ b/.roo/commands/release.md @@ -1,6 +1,7 @@ --- description: "Create a new release of the Roo Code extension" argument-hint: patch | minor | major +mode: code --- 1. Identify the SHA corresponding to the most recent release using GitHub CLI: `gh release view --json tagName,targetCommitish,publishedAt` @@ -16,14 +17,14 @@ argument-hint: patch | minor | major [list of changes] ``` -- Always include contributor attribution using format: (thanks @username!) -- For PRs that close issues, also include the issue number and reporter: "- Fix: Description (#123 by @reporter, PR by @contributor)" -- For PRs without linked issues, use the standard format: "- Add support for feature (thanks @contributor!)" +- Always include contributor attribution and the PR number: use "(PR # by @username)". +- For PRs that close issues, include both the issue number and the PR number and authors: "- Fix: Description (#123 by @reporter, PR #456 by @contributor)" +- For PRs without linked issues, include the PR number and author: "- Add support for feature (PR #456 by @contributor)" - Provide brief descriptions of each item to explain the change - Order the list from most important to least important - Example formats: - - With issue: "- Fix: Resolve memory leak in extension (#456 by @issueReporter, PR by @prAuthor)" - - Without issue: "- Add support for Gemini 2.5 Pro caching (thanks @contributor!)" + - With issue: "- Fix: Resolve memory leak in extension (#456 by @issueReporter, PR #789 by @prAuthor)" + - Without issue: "- Add support for Gemini 2.5 Pro caching (PR #789 by @contributor)" - CRITICAL: Include EVERY SINGLE PR in the changeset - don't assume you know which ones are important. Count the total PRs to verify completeness and cross-reference the list to ensure nothing is missed. 6. If the generate_image tool is available, create a release image at `releases/[version]-release.png` diff --git a/.roo/roomotes.yml b/.roo/roomotes.yml index 33f6b3bd57..0ea30b93af 100644 --- a/.roo/roomotes.yml +++ b/.roo/roomotes.yml @@ -1,25 +1,6 @@ version: "1.0" commands: - - name: Pull latest changes - run: git pull - timeout: 60 - execution_phase: task_run - name: Install dependencies run: pnpm install timeout: 60 - execution_phase: task_run - -github_events: - - event: issues.opened - action: - name: github.issue.fix - - event: issue_comment.created - action: - name: github.issue.comment.respond - - event: pull_request.opened - action: - name: github.pr.review - - event: pull_request_review_comment.created - action: - name: github.pr.comment.respond diff --git a/.roo/rules-translate/instructions-zh-cn.md b/.roo/rules-translate/instructions-zh-cn.md index 241ae338dc..b166a1e6a8 100644 --- a/.roo/rules-translate/instructions-zh-cn.md +++ b/.roo/rules-translate/instructions-zh-cn.md @@ -16,7 +16,6 @@ | Auto-approve | 自动批准 | 始终批准 | 权限相关术语 | | Checkpoint | 存档点 | 检查点/快照 | 技术概念统一 | | MCP Server | MCP 服务 | MCP 服务器 | 技术组件 | -| Human Relay | 人工辅助模式 | 人工中继 | 功能描述清晰 | | Network Timeout | 请求超时 | 网络超时 | 更准确描述 | | Terminal | 终端 | 命令行 | 技术术语统一 | | diff | 差异更新 | 差分/补丁 | 代码变更 | @@ -115,7 +114,7 @@ - 保留英文品牌名 - 技术术语保持一致性 - - 保留英文专有名词:如"AWS Bedrock ARN" + - 保留英文专有名词:如"Amazon Bedrock ARN" 4. **用户操作** - 操作动词统一: diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e43a1e7b2..bf223708a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,557 @@ # Roo Code Changelog +## [3.38.3] - 2026-01-03 + +- Feat: Add option in Context settings to recursively load `.roo/rules` and `AGENTS.md` from subdirectories (PR #10446 by @mrubens) +- Fix: Stop frequent Claude Code sign-ins by hardening OAuth refresh token handling (PR #10410 by @hannesrudolph) +- Fix: Add `maxConcurrentFileReads` limit to native `read_file` tool schema (PR #10449 by @app/roomote) +- Fix: Add type check for `lastMessage.text` in TTS useEffect to prevent runtime errors (PR #10431 by @app/roomote) + +## [3.38.2] - 2025-12-31 + +![3.38.2 Release - Skill Alignment](/releases/3.38.2-release.png) + +- Align skills system with Agent Skills specification (PR #10409 by @hannesrudolph) +- Prevent write_to_file from creating files at truncated paths (PR #10415 by @mrubens and @daniel-lxs) +- Update Cerebras maxTokens to 16384 (PR #10387 by @sebastiand-cerebras) +- Fix rate limit wait display (PR #10389 by @hannesrudolph) +- Remove human-relay provider (PR #10388 by @hannesrudolph) +- Replace Todo Lists video with Context Management video in documentation (PR #10375 by @SannidhyaSah) + +## [3.38.1] - 2025-12-29 + +![3.38.1 Release - Bug Fixes and Stability](/releases/3.38.1-release.png) + +- Fix: Flush pending tool results before condensing context (PR #10379 by @daniel-lxs) +- Fix: Revert mergeToolResultText for OpenAI-compatible providers (PR #10381 by @hannesrudolph) +- Fix: Enforce maxConcurrentFileReads limit in read_file tool (PR #10363 by @roomote) +- Fix: Improve feedback message when read_file is used on a directory (PR #10371 by @roomote) +- Fix: Handle custom tool use similarly to MCP tools for IPC schema purposes (PR #10364 by @jr) +- Fix: Correct GitHub repository URL in marketing page (#10376 by @jishnuteegala, PR #10377 by @roomote) +- Docs: Clarify path to Security Settings in privacy policy (PR #10367 by @roomote) + +## [3.38.0] - 2025-12-27 + +![3.38.0 Release - Skills](/releases/3.38.0-release.png) + +- Add support for [Agent Skills](https://agentskills.io/), enabling reusable packages of prompts, tools, and resources to extend Roo's capabilities (PR #10335 by @mrubens) +- Add optional mode field to slash command front matter, allowing commands to automatically switch to a specific mode when triggered (PR #10344 by @app/roomote) +- Add support for npm packages and .env files to custom tools, allowing custom tools to import dependencies and access environment variables (PR #10336 by @cte) +- Remove simpleReadFileTool feature, streamlining the file reading experience (PR #10254 by @app/roomote) +- Remove OpenRouter Transforms feature (PR #10341 by @app/roomote) +- Fix mergeToolResultText handling in Roo provider (PR #10359 by @mrubens) + +## [3.37.1] - 2025-12-23 + +![3.37.1 Release - Tool Fixes and Provider Improvements](/releases/3.37.1-release.png) + +- Fix: Send native tool definitions by default for OpenAI to ensure proper tool usage (PR #10314 by @hannesrudolph) +- Fix: Preserve reasoning_details shape to prevent malformed responses when processing model output (PR #10313 by @hannesrudolph) +- Fix: Drain queued messages while waiting for ask to prevent message loss (PR #10315 by @hannesrudolph) +- Feat: Add grace retry for empty assistant messages to improve reliability (PR #10297 by @hannesrudolph) +- Feat: Enable mergeToolResultText for all OpenAI-compatible providers for better tool result handling (PR #10299 by @hannesrudolph) +- Feat: Enable mergeToolResultText for Roo Code Cloud provider (PR #10301 by @hannesrudolph) +- Feat: Strengthen native tool-use guidance in prompts for improved model behavior (PR #10311 by @hannesrudolph) +- UX: Account-centric signup flow for improved onboarding experience (PR #10306 by @brunobergher) + +## [3.37.0] - 2025-12-22 + +![3.37.0 Release - Custom Tool Calling](/releases/3.37.0-release.png) + +- Add MiniMax M2.1 and improve environment_details handling for Minimax thinking models (PR #10284 by @hannesrudolph) +- Add GLM-4.7 model with thinking mode support for Zai provider (PR #10282 by @hannesrudolph) +- Add experimental custom tool calling - define custom tools that integrate seamlessly with your AI workflow (PR #10083 by @cte) +- Deprecate XML tool protocol selection and force native tool format for new tasks (PR #10281 by @daniel-lxs) +- Fix: Emit tool_call_end events in OpenAI handler when streaming ends (#10275 by @torxeon, PR #10280 by @daniel-lxs) +- Fix: Emit tool_call_end events in BaseOpenAiCompatibleProvider (PR #10293 by @hannesrudolph) +- Fix: Disable strict mode for MCP tools to preserve optional parameters (PR #10220 by @daniel-lxs) +- Fix: Move array-specific properties into anyOf variant in normalizeToolSchema (PR #10276 by @daniel-lxs) +- Fix: Add CRLF line ending normalization to search_replace and search_and_replace tools (PR #10288 by @hannesrudolph) +- Fix: Add graceful fallback for model parsing in Chutes provider (PR #10279 by @hannesrudolph) +- Fix: Enable Requesty refresh models with credentials (PR #10273 by @daniel-lxs) +- Fix: Improve reasoning_details accumulation and serialization (PR #10285 by @hannesrudolph) +- Fix: Preserve reasoning_content in condense summary for DeepSeek-reasoner (PR #10292 by @hannesrudolph) +- Refactor Zai provider to merge environment_details into tool result instead of system message (PR #10289 by @hannesrudolph) +- Remove parallel_tool_calls parameter from litellm provider (PR #10274 by @roomote) +- Add Cloud Team page with comprehensive team management features (PR #10267 by @roomote) +- Add message log deduper utility for evals (PR #10286 by @hannesrudolph) + +## [3.36.16] - 2025-12-19 + +- Fix: Normalize tool schemas for VS Code LM API to resolve error 400 when using VS Code Language Model API providers (PR #10221 by @hannesrudolph) + +## [3.36.15] - 2025-12-19 + +![3.36.15 Release - 1M Context Window Support](/releases/3.36.15-release.png) + +- Add 1M context window beta support for Claude Sonnet 4 on Vertex AI, enabling significantly larger context for complex tasks (PR #10209 by @hannesrudolph) +- Add native tool calling support for LM Studio and Qwen-Code providers, improving compatibility with local models (PR #10208 by @hannesrudolph) +- Add native tool call defaults for OpenAI-compatible providers, expanding native function calling across more configurations (PR #10213 by @hannesrudolph) +- Enable native tool calls for Requesty provider (PR #10211 by @daniel-lxs) +- Improve API error handling and visibility with clearer error messages and better user feedback (PR #10204 by @brunobergher) +- Add downloadable error diagnostics from chat errors, making it easier to troubleshoot and report issues (PR #10188 by @brunobergher) +- Fix refresh models button not properly flushing the cache, ensuring model lists update correctly (#9682 by @tl-hbk, PR #9870 by @pdecat) +- Fix additionalProperties handling for strict mode compatibility, resolving schema validation issues with certain providers (PR #10210 by @daniel-lxs) + +## [3.36.14] - 2025-12-18 + +![3.36.14 Release - Native Tool Calling for Claude on Vertex AI](/releases/3.36.14-release.png) + +- Add native tool calling support for Claude models on Vertex AI, enabling more efficient and reliable tool interactions (PR #10197 by @hannesrudolph) +- Fix JSON Schema format value stripping for OpenAI compatibility, resolving issues with unsupported format values (PR #10198 by @daniel-lxs) +- Improve "no tools used" error handling with graceful retry mechanism for better reliability when tools fail to execute (PR #10196 by @hannesrudolph) + +## [3.36.13] - 2025-12-18 + +![3.36.13 Release - Native Tool Protocol](/releases/3.36.13-release.png) + +- Change default tool protocol from XML to native for improved reliability and performance (PR #10186 by @mrubens) +- Add native tool support for VS Code Language Model API providers (PR #10191 by @daniel-lxs) +- Lock task tool protocol for consistent task resumption, ensuring tasks resume with the same protocol they started with (PR #10192 by @daniel-lxs) +- Replace edit_file tool alias with actual edit_file tool for improved diff editing capabilities (PR #9983 by @hannesrudolph) +- Fix LiteLLM router models by merging default model info for native tool calling support (PR #10187 by @daniel-lxs) +- Add PostHog exception tracking for consecutive mistake errors to improve error monitoring (PR #10193 by @daniel-lxs) + +## [3.36.12] - 2025-12-18 + +![3.36.12 Release - Better telemetry and Bedrock fixes](/releases/3.36.12-release.png) + +- Fix: Add userAgentAppId to Bedrock embedder for code indexing (#10165 by @jackrein, PR #10166 by @roomote) +- Update OpenAI and Gemini tool preferences for improved model behavior (PR #10170 by @hannesrudolph) +- Extract error messages from JSON payloads for better PostHog error grouping (PR #10163 by @daniel-lxs) + +## [3.36.11] - 2025-12-17 + +![3.36.11 Release - Native Tool Calling Enhancements](/releases/3.36.11-release.png) + +- Add support for Claude Code Provider native tool calling, improving tool execution performance and reliability (PR #10077 by @hannesrudolph) +- Enable native tool calling by default for Z.ai models for better model compatibility (PR #10158 by @app/roomote) +- Enable native tools by default for OpenAI compatible provider to improve tool calling support (PR #10159 by @daniel-lxs) +- Fix: Normalize MCP tool schemas for Bedrock and OpenAI strict mode to ensure proper tool compatibility (PR #10148 by @daniel-lxs) +- Fix: Remove dots and colons from MCP tool names for Bedrock compatibility (PR #10152 by @daniel-lxs) +- Fix: Convert tool_result to XML text when native tools disabled for Bedrock (PR #10155 by @daniel-lxs) +- Fix: Refresh Roo models cache with session token on auth state change to resolve model list refresh issues (PR #10156 by @daniel-lxs) +- Fix: Support AWS GovCloud and China region ARNs in Bedrock provider for expanded regional support (PR #10157 by @app/roomote) + +## [3.36.10] - 2025-12-17 + +![3.36.10 Release - Gemini 3 Flash Preview](/releases/3.36.10-release.png) + +- Add support for Gemini 3 Flash Preview model in the Gemini provider (PR #10151 by @hannesrudolph) +- Implement interleaved thinking mode for DeepSeek Reasoner, enabling streaming reasoning output (PR #9969 by @hannesrudolph) +- Fix: Preserve reasoning_content during tool call sequences in DeepSeek (PR #10141 by @hannesrudolph) +- Fix: Correct token counting for context truncation display (PR #9961 by @hannesrudolph) +- Update Next.js dependency to ~15.2.8 (PR #10140 by @jr) + +## [3.36.9] - 2025-12-15 + +![3.36.9 Release - Cross-Provider Compatibility](/releases/3.36.9-release.png) + +- Fix: Normalize tool call IDs for cross-provider compatibility via OpenRouter, ensuring consistent handling across different AI providers (PR #10102 by @daniel-lxs) +- Fix: Add additionalProperties: false to nested MCP tool schemas, improving schema validation and preventing unexpected properties (PR #10109 by @daniel-lxs) +- Fix: Validate tool_result IDs in delegation resume flow, preventing errors when resuming delegated tasks (PR #10135 by @daniel-lxs) +- Feat: Add full error details to streaming failure dialog, providing more comprehensive information for debugging streaming issues (PR #10131 by @roomote) +- Feat: Improve evals UI with tool groups and duration fix, enhancing the evaluation interface organization and timing accuracy (PR #10133 by @hannesrudolph) + +## [3.36.8] - 2025-12-16 + +![3.36.8 Release - Native Tools Enabled by Default](/releases/3.36.8-release.png) + +- Implement incremental token-budgeted file reading for smarter, more efficient file content retrieval (PR #10052 by @jr) +- Enable native tools by default for multiple providers including OpenAI, Azure, Google, Vertex, and more (PR #10059 by @daniel-lxs) +- Enable native tools by default for Anthropic and add telemetry tracking for tool format usage (PR #10021 by @daniel-lxs) +- Fix: Prevent race condition from deleting wrong API messages during streaming (PR #10113 by @hannesrudolph) +- Fix: Prevent duplicate MCP tools error by deduplicating servers at source (PR #10096 by @daniel-lxs) +- Remove strict ARN validation for Bedrock custom ARN users allowing more flexibility (#10108 by @wisestmumbler, PR #10110 by @roomote) +- Add metadata to error details dialog for improved debugging (PR #10050 by @roomote) +- Add configuration to control public sharing feature (PR #10105 by @mrubens) +- Remove description from Bedrock service tiers for cleaner UI (PR #10118 by @mrubens) +- Fix: Correct link to provider pricing page on web (PR #10107 by @brunobergher) + +## [3.36.7] - 2025-12-15 + +- Improve tool configuration for OpenAI models in OpenRouter (PR #10082 by @hannesrudolph) +- Capture more detailed provider-specific error information from OpenRouter for better debugging (PR #10073 by @jr) +- Add Amazon Nova 2 Lite model to Bedrock provider (#9802 by @Smartsheet-JB-Brown, PR #9830 by @roomote) +- Add AWS Bedrock service tier support (#9874 by @Smartsheet-JB-Brown, PR #9955 by @roomote) +- Remove auto-approve toggles for to-do and retry actions to simplify the approval workflow (PR #10062 by @hannesrudolph) +- Move isToolAllowedForMode out of shared directory for better code organization (PR #10089 by @cte) +- Improve run logs and formatters in web-evals for better evaluation tracking (PR #10081 by @hannesrudolph) + +## [3.36.6] - 2025-12-12 + +![3.36.6 Release - Tool Alias Support](/releases/3.36.6-release.png) + +- Add tool alias support for model-specific tool customization, allowing users to configure how tools are presented to different AI models (PR #9989 by @daniel-lxs) +- Sanitize MCP server and tool names for API compatibility, ensuring special characters don't cause issues with API calls (PR #10054 by @daniel-lxs) +- Improve auto-approve timer visibility in follow-up suggestions for better user awareness of pending actions (PR #10048 by @brunobergher) +- Fix: Cancel auto-approval timeout when user starts typing, preventing accidental auto-approvals during user interaction (PR #9937 by @roomote) +- Add WorkspaceTaskVisibility type for organization cloud settings to support team visibility controls (PR #10020 by @roomote) +- Fix: Extract raw error message from OpenRouter metadata for clearer error reporting (PR #10039 by @daniel-lxs) +- Fix: Show tool protocol dropdown for LiteLLM provider, restoring missing configuration option (PR #10053 by @daniel-lxs) + +## [3.36.5] - 2025-12-11 + +![3.36.5 Release - GPT-5.2](/releases/3.36.5-release.png) + +- Add: GPT-5.2 model to openai-native provider (PR #10024 by @hannesrudolph) +- Add: Toggle for Enter key behavior in chat input allowing users to configure whether Enter sends or creates new line (#8555 by @lmtr0, PR #10002 by @hannesrudolph) +- Add: App version to telemetry exception captures and filter 402 errors (PR #9996 by @daniel-lxs) +- Fix: Handle empty Gemini responses and reasoning loops to prevent infinite retries (PR #10007 by @hannesrudolph) +- Fix: Add missing tool_result blocks to prevent API errors when tool results are expected (PR #10015 by @daniel-lxs) +- Fix: Filter orphaned tool_results when more results than tool_uses to prevent message validation errors (PR #10027 by @daniel-lxs) +- Fix: Add general API endpoints for Z.ai provider (#9879 by @richtong, PR #9894 by @roomote) +- Fix: Apply versioned settings on nightly builds (PR #9997 by @hannesrudolph) +- Remove: Glama provider (PR #9801 by @hannesrudolph) +- Remove: Deprecated list_code_definition_names tool (PR #10005 by @hannesrudolph) + +## [3.36.4] - 2025-12-10 + +![3.36.4 Release - Error Details Modal](/releases/3.36.4-release.png) + +- Add error details modal with on-demand display for improved error visibility when debugging issues (PR #9985 by @roomote) +- Fix: Prevent premature rawChunkTracker clearing for MCP tools, improving reliability of MCP tool streaming (PR #9993 by @daniel-lxs) +- Fix: Filter out 429 rate limit errors from API error telemetry for cleaner metrics (PR #9987 by @daniel-lxs) +- Fix: Correct TODO list display order in chat view to show items in proper sequence (PR #9991 by @roomote) + +## [3.36.3] - 2025-12-09 + +![3.36.3 Release](/releases/3.36.3-release.png) + +- Refactor: Unified context-management architecture with improved UX for better context control (PR #9795 by @hannesrudolph) +- Add new `search_replace` native tool for single-replacement operations with improved editing precision (PR #9918 by @hannesrudolph) +- Streaming tool stats and token usage throttling for better real-time feedback during generation (PR #9926 by @hannesrudolph) +- Add versioned settings support with minPluginVersion gating for Roo provider (PR #9934 by @hannesrudolph) +- Make Architect mode save plans to `/plans` directory and gitignore it (PR #9944 by @brunobergher) +- Add announcement support CTA and social icons to UI (PR #9945 by @hannesrudolph) +- Add ability to save screenshots from the browser tool (PR #9963 by @mrubens) +- Refactor: Decouple tools from system prompt for cleaner architecture (PR #9784 by @daniel-lxs) +- Update DeepSeek models to V3.2 with new pricing (PR #9962 by @hannesrudolph) +- Add minimal and medium reasoning effort levels for Gemini models (PR #9973 by @hannesrudolph) +- Update xAI models catalog with latest model options (PR #9872 by @hannesrudolph) +- Add DeepSeek V3-2 support for Baseten provider (PR #9861 by @AlexKer) +- Tweaks to Baseten model definitions for better defaults (PR #9866 by @mrubens) +- Fix: Add xhigh reasoning effort support for gpt-5.1-codex-max (#9891 by @andrewginns, PR #9900 by @andrewginns) +- Fix: Add Kimi, MiniMax, and Qwen model configurations for Bedrock (#9902 by @jbearak, PR #9905 by @app/roomote) +- Configure tool preferences for xAI models (PR #9923 by @hannesrudolph) +- Default to using native tools when supported on OpenRouter (PR #9878 by @mrubens) +- Fix: Exclude apply_diff from native tools when diffEnabled is false (#9919 by @denis-kudelin, PR #9920 by @app/roomote) +- Fix: Always show tool protocol selector for openai-compatible provider (#9965 by @bozoweed, PR #9966 by @hannesrudolph) +- Fix: Respect explicit supportsReasoningEffort array values for proper model configuration (PR #9970 by @hannesrudolph) +- Add timeout configuration to OpenAI Compatible Provider Client (PR #9898 by @dcbartlett) +- Revert default tool protocol change from xml to native for stability (PR #9956 by @mrubens) +- Remove defaultTemperature from Roo provider configuration (PR #9932 by @mrubens) +- Improve OpenAI error messages to be more useful for debugging (PR #9639 by @mrubens) +- Better error logs for parseToolCall exceptions (PR #9857 by @cte) +- Improve cloud job error logging for RCC provider errors (PR #9924 by @cte) +- Fix: Display actual API error message instead of generic text on retry (PR #9954 by @hannesrudolph) +- Add API error telemetry to OpenRouter provider for better diagnostics (PR #9953 by @daniel-lxs) +- Fix: Sanitize removed/invalid API providers to prevent infinite loop (PR #9869 by @hannesrudolph) +- Fix: Use foreground color for context-management icons (PR #9912 by @hannesrudolph) +- Fix: Suppress 'ask promise was ignored' error in handleError (PR #9914 by @daniel-lxs) +- Fix: Process finish_reason to emit tool_call_end events properly (PR #9927 by @daniel-lxs) +- Fix: Add finish_reason processing to xai.ts provider (PR #9929 by @daniel-lxs) +- Fix: Validate and fix tool_result IDs before API requests (PR #9952 by @daniel-lxs) +- Fix: Return undefined instead of 0 for disabled API timeout (PR #9960 by @hannesrudolph) +- Stop making unnecessary count_tokens requests for better performance (PR #9884 by @mrubens) +- Refactor: Consolidate ThinkingBudget components and fix disable handling (PR #9930 by @hannesrudolph) +- Forbid time estimates in architect mode for more focused planning (PR #9931 by @app/roomote) +- Web: Add product pages (PR #9865 by @brunobergher) +- Make eval runs deleteable in the web UI (PR #9909 by @mrubens) +- Feat: Change defaultToolProtocol default from xml to native (later reverted) (PR #9892 by @app/roomote) + +## [3.36.2] - 2025-12-04 + +![3.36.2 Release - Dynamic API Settings](/releases/3.36.2-release.png) + +- Restrict GPT-5 tool set to apply_patch for improved compatibility (PR #9853 by @hannesrudolph) +- Add dynamic settings support for Roo models from API, allowing model-specific configurations to be fetched dynamically (PR #9852 by @hannesrudolph) +- Fix: Resolve Chutes provider model fetching issue (PR #9854 by @cte) + +## [3.36.1] - 2025-12-04 + +![3.36.1 Release - Message Management & Stability Improvements](/releases/3.36.1-release.png) + +- Add MessageManager layer for centralized history coordination, fixing message synchronization issues (PR #9842 by @hannesrudolph) +- Fix: Prevent cascading truncation loop by only truncating visible messages (PR #9844 by @hannesrudolph) +- Fix: Handle unknown/invalid native tool calls to prevent extension freeze (PR #9834 by @daniel-lxs) +- Always enable reasoning for models that require it (PR #9836 by @cte) +- ChatView: Smoother stick-to-bottom behavior during streaming (PR #8999 by @hannesrudolph) +- UX: Improved error messages and documentation links (PR #9777 by @brunobergher) +- Fix: Overly round follow-up question suggestions styling (PR #9829 by @brunobergher) +- Add symlink support for slash commands in .roo/commands folder (PR #9838 by @mrubens) +- Ignore input to the execa terminal process for safer command execution (PR #9827 by @mrubens) +- Be safer about large file reads (PR #9843 by @jr) +- Add gpt-5.1-codex-max model to OpenAI provider (PR #9848 by @hannesrudolph) +- Evals UI: Add filtering, bulk delete, tool consolidation, and run notes (PR #9837 by @hannesrudolph) +- Evals UI: Add multi-model launch and UI improvements (PR #9845 by @hannesrudolph) +- Web: New pricing page (PR #9821 by @brunobergher) + +## [3.36.0] - 2025-12-04 + +![3.36.0 Release - Rewind Kangaroo](/releases/3.36.0-release.png) + +- Fix: Restore context when rewinding after condense (#8295 by @hannesrudolph, PR #9665 by @hannesrudolph) +- Add reasoning_details support to Roo provider for enhanced model reasoning visibility (PR #9796 by @app/roomote) +- Default to native tools for all models in the Roo provider for improved performance (PR #9811 by @mrubens) +- Enable search_and_replace for Minimax models (PR #9780 by @mrubens) +- Fix: Resolve Vercel AI Gateway model fetching issues (PR #9791 by @cte) +- Fix: Apply conservative max tokens for Cerebras provider (PR #9804 by @sebastiand-cerebras) +- Fix: Remove omission detection logic to eliminate false positives (#9785 by @Michaelzag, PR #9787 by @app/roomote) +- Refactor: Remove deprecated insert_content tool (PR #9751 by @daniel-lxs) +- Chore: Hide parallel tool calls experiment and disable feature (PR #9798 by @hannesrudolph) +- Update next.js documentation site dependencies (PR #9799 by @jr) +- Fix: Correct download count display on homepage (PR #9807 by @mrubens) + +## [3.35.5] - 2025-12-03 + +- Feat: Add provider routing selection for OpenRouter embeddings (#9144 by @SannidhyaSah, PR #9693 by @SannidhyaSah) +- Default Minimax M2 to native tool calling (PR #9778 by @mrubens) +- Sanitize the native tool calls to fix a bug with Gemini (PR #9769 by @mrubens) +- UX: Updates to CloudView (PR #9776 by @roomote) + +## [3.35.4] - 2025-12-02 + +- Fix: Handle malformed native tool calls to prevent hanging (PR #9758 by @daniel-lxs) +- Fix: Remove reasoning toggles for GLM-4.5 and GLM-4.6 on z.ai provider (PR #9752 by @roomote) +- Refactor: Remove line_count parameter from write_to_file tool (PR #9667 by @hannesrudolph) + +## [3.35.3] - 2025-12-02 + +- Switch to new welcome view for improved onboarding experience (PR #9741 by @mrubens) +- Update homepage with latest changes (PR #9675 by @brunobergher) +- Improve privacy for stealth models by adding vendor confidentiality section to system prompt (PR #9742 by @mrubens) + +## [3.35.2] - 2025-12-01 + +![3.35.2 Release - Model Default Temperatures](/releases/3.35.2-release.png) + +- Allow models to contain default temperature settings for provider-specific optimal defaults (PR #9734 by @mrubens) +- Add tag-based native tool calling detection for Roo provider models (PR #9735 by @mrubens) +- Enable native tool support for all LiteLLM models by default (PR #9736 by @mrubens) +- Pass app version to provider for improved request tracking (PR #9730 by @cte) + +## [3.35.1] - 2025-12-01 + +- Fix: Flush pending tool results before task delegation (PR #9726 by @daniel-lxs) +- Improve: Better IPC error logging for easier debugging (PR #9727 by @cte) + +## [3.35.0] - 2025-12-01 + +![3.35.0 Release - Subtasks & Native Tools](/releases/3.35.0-release.png) + +- Metadata-driven subtasks with automatic parent resume and single-open safety for improved task orchestration (#8081 by @hannesrudolph, PR #9090 by @hannesrudolph) +- Native tool calling support expanded across many providers: Bedrock (PR #9698 by @mrubens), Cerebras (PR #9692 by @mrubens), Chutes with auto-detection from API (PR #9715 by @daniel-lxs), DeepInfra (PR #9691 by @mrubens), DeepSeek and Doubao (PR #9671 by @daniel-lxs), Groq (PR #9673 by @daniel-lxs), LiteLLM (PR #9719 by @daniel-lxs), Ollama (PR #9696 by @mrubens), OpenAI-compatible providers (PR #9676 by @daniel-lxs), Requesty (PR #9672 by @daniel-lxs), Unbound (PR #9699 by @mrubens), Vercel AI Gateway (PR #9697 by @mrubens), Vertex Gemini (PR #9678 by @daniel-lxs), and xAI with new Grok 4 Fast and Grok 4.1 Fast models (PR #9690 by @mrubens) +- Fix: Preserve tool_use blocks in summary for parallel tool calls (#9700 by @SilentFlower, PR #9714 by @SilentFlower) +- Default Grok Code Fast to native tools for better performance (PR #9717 by @mrubens) +- UX improvements to the Roo Code Cloud provider-centric onboarding flow (PR #9709 by @brunobergher) +- UX toolbar cleanup and settings consolidation for a cleaner interface (PR #9710 by @brunobergher) +- Add model-specific tool customization via `excludedTools` and `includedTools` configuration (PR #9641 by @daniel-lxs) +- Add new `apply_patch` native tool for more efficient file editing operations (PR #9663 by @hannesrudolph) +- Add new `search_and_replace` tool for batch text replacements across files (PR #9549 by @hannesrudolph) +- Add debug buttons to view API and UI history for troubleshooting (PR #9684 by @hannesrudolph) +- Include tool format in environment details for better context awareness (PR #9661 by @mrubens) +- Fix: Display install count in millions instead of thousands (PR #9677 by @app/roomote) +- Web-evals improvements: add task log viewing, export failed logs, and new run options (PR #9637 by @hannesrudolph) +- Web-evals updates: add kill run functionality (PR #9681 by @hannesrudolph) +- Fix: Prevent navigation buttons from wrapping on smaller screens (PR #9721 by @app/roomote) + +## [3.34.8] - 2025-11-27 + +![3.34.8 Release - Race Condition Fix](/releases/3.34.8-release.png) + +- Fix: Race condition in new_task tool for native protocol (PR #9655 by @daniel-lxs) + +## [3.34.7] - 2025-11-27 + +![3.34.7 Release - More Native Tool Integrations](/releases/3.34.7-release.png) + +- Support native tools in the Anthropic provider for improved tool calling (PR #9644 by @mrubens) +- Enable native tool calling for z.ai models (PR #9645 by @mrubens) +- Enable native tool calling for Moonshot models (PR #9646 by @mrubens) +- Fix: OpenRouter tool calls handling improvements (PR #9642 by @mrubens) +- Fix: OpenRouter GPT-5 strict schema validation for read_file tool (PR #9633 by @daniel-lxs) +- Fix: Create parent directories early in write_to_file to prevent ENOENT errors (#9634 by @ivanenev, PR #9640 by @daniel-lxs) +- Fix: Disable native tools and temperature support for claude-code provider (PR #9643 by @hannesrudolph) +- Add 'taking you to cloud' screen after provider welcome for improved onboarding (PR #9652 by @mrubens) + +## [3.34.6] - 2025-11-26 + +![3.34.6 Release - Bedrock Embeddings](/releases/3.34.6-release.png) + +- Add support for AWS Bedrock embeddings in code indexing (#8658 by @kyle-hobbs, PR #9475 by @ggoranov-smar) +- Add native tool calling support for Mistral provider (PR #9625 by @hannesrudolph) +- Wire MULTIPLE_NATIVE_TOOL_CALLS experiment to OpenAI parallel_tool_calls for parallel tool execution (PR #9621 by @hannesrudolph) +- Add fine grained tool streaming for OpenRouter Anthropic (PR #9629 by @mrubens) +- Allow global inference selection for Bedrock when cross-region is enabled (PR #9616 by @roomote) +- Fix: Filter non-Anthropic content blocks before sending to Vertex API (#9583 by @cardil, PR #9618 by @hannesrudolph) +- Fix: Restore content undefined check in WriteToFileTool.handlePartial() (#9611 by @Lissanro, PR #9614 by @daniel-lxs) +- Fix: Prevent model cache from persisting empty API responses (#9597 by @zx2021210538, PR #9623 by @daniel-lxs) +- Fix: Exclude access_mcp_resource tool when MCP has no resources (PR #9615 by @daniel-lxs) +- Fix: Update default settings for inline terminal and codebase indexing (PR #9622 by @roomote) +- Fix: Convert line_ranges strings to lineRanges objects in native tool calls (PR #9627 by @daniel-lxs) +- Fix: Defer new_task tool_result until subtask completes for native protocol (PR #9628 by @daniel-lxs) + +## [3.34.5] - 2025-11-25 + +![3.34.5 Release - Experimental Parallel Tool Calling](/releases/3.34.5-release.png) + +- Experimental feature to enable multiple native tool calls per turn (PR #9273 by @daniel-lxs) +- Add Bedrock Opus 4.5 to global inference model list (PR #9595 by @roomote) +- Fix: Update API handler when toolProtocol changes (PR #9599 by @mrubens) +- Set native tools as default for minimax-m2 and claude-haiku-4.5 (PR #9586 by @daniel-lxs) +- Make single file read only apply to XML tools (PR #9600 by @mrubens) +- Enhance web-evals dashboard with dynamic tool columns and UX improvements (PR #9592 by @hannesrudolph) +- Revert "Add support for Roo Code Cloud as an embeddings provider" while we fix some issues (PR #9602 by @mrubens) + +## [3.34.4] - 2025-11-25 + +![3.34.4 Release - BFL Image Generation](/releases/3.34.4-release.png) + +- Add new Black Forest Labs image generation models, free on Roo Code Cloud and also available on OpenRouter (PR #9587 and #9589 by @mrubens) +- Fix: Preserve dynamic MCP tool names in native mode API history to prevent tool name mismatches (PR #9559 by @daniel-lxs) +- Fix: Preserve tool_use blocks in summary message during condensing with native tools to maintain conversation context (PR #9582 by @daniel-lxs) + +## [3.34.3] - 2025-11-25 + +![3.34.3 Release - Streaming and Opus 4.5](/releases/3.34.3-release.png) + +- Implement streaming for native tool calls, providing real-time feedback during tool execution (PR #9542 by @daniel-lxs) +- Add Claude Opus 4.5 model to Claude Code provider (PR #9560 by @mrubens) +- Add Claude Opus 4.5 model to Bedrock provider (#9571 by @pisicode, PR #9572 by @roomote) +- Enable caching for Opus 4.5 model to improve performance (#9567 by @iainRedro, PR #9568 by @roomote) +- Add support for Roo Code Cloud as an embeddings provider (PR #9543 by @mrubens) +- Fix ask_followup_question streaming issue and add missing tool cases (PR #9561 by @daniel-lxs) +- Add contact links to About Roo Code settings page (PR #9570 by @roomote) +- Switch from asdf to mise-en-place in bare-metal evals setup script (PR #9548 by @cte) + +## [3.34.2] - 2025-11-24 + +![3.34.2 Release - Opus Conductor](/releases/3.34.2-release.png) + +- Add support for Claude Opus 4.5 in Anthropic and Vertex providers (PR #9541 by @daniel-lxs) +- Add support for Claude Opus 4.5 in OpenRouter with prompt caching and reasoning budget (PR #9540 by @daniel-lxs) +- Add Roo Code Cloud as an image generation provider (PR #9528 by @mrubens) +- Fix: Gracefully skip unsupported content blocks in Gemini transformer (PR #9537 by @daniel-lxs) +- Fix: Flush LiteLLM cache when credentials change on refresh (PR #9536 by @daniel-lxs) +- Fix: Ensure XML parser state matches tool protocol on config update (PR #9535 by @daniel-lxs) +- Update Cerebras models (PR #9527 by @sebastiand-cerebras) +- Fix: Support reasoning_details format for Gemini 3 models (PR #9506 by @daniel-lxs) + +## [3.34.1] - 2025-11-23 + +- Show the prompt for image generation in the UI (PR #9505 by @mrubens) +- Fix double todo list display issue (PR #9517 by @mrubens) +- Add tracking for cloud synced messages (PR #9518 by @mrubens) +- Enable the Roo Code Cloud provider in evals (PR #9492 by @cte) + +## [3.34.0] - 2025-11-21 + +![3.34.0 Release - Browser Use 2.0](/releases/3.34.0-release.png) + +- Add Browser Use 2.0 with enhanced browser interaction capabilities (PR #8941 by @hannesrudolph) +- Add support for Baseten as a new AI provider (PR #9461 by @AlexKer) +- Improve base OpenAI compatible provider with better error handling and configuration (PR #9462 by @mrubens) +- Add provider-oriented welcome screen to improve onboarding experience (PR #9484 by @mrubens) +- Pin Roo provider to the top of the provider list for better discoverability (PR #9485 by @mrubens) +- Enhance native tool descriptions with examples and clarifications for better AI understanding (PR #9486 by @daniel-lxs) +- Fix: Make cancel button immediately responsive during streaming (#9435 by @jwadow, PR #9448 by @daniel-lxs) +- Fix: Resolve apply_diff performance regression from earlier changes (PR #9474 by @daniel-lxs) +- Fix: Implement model cache refresh to prevent stale disk cache issues (PR #9478 by @daniel-lxs) +- Fix: Copy model-level capabilities to OpenRouter endpoint models correctly (PR #9483 by @daniel-lxs) +- Fix: Add fallback to yield tool calls regardless of finish_reason (PR #9476 by @daniel-lxs) + +## [3.33.3] - 2025-11-20 + +![3.33.3 Release - Gemini 3 Pro Image Preview](/releases/3.33.3-release.png) + +- Add Google Gemini 3 Pro Image Preview to image generation models (PR #9440 by @app/roomote) +- Add support for Minimax as Anthropic-compatible provider (PR #9455 by @daniel-lxs) +- Store reasoning in conversation history for all providers (PR #9451 by @daniel-lxs) +- Fix: Improve preserveReasoning flag to control API reasoning inclusion (PR #9453 by @daniel-lxs) +- Fix: Prevent OpenAI Native parallel tool calls for native tool calling (PR #9433 by @hannesrudolph) +- Fix: Improve search and replace symbol parsing (PR #9456 by @daniel-lxs) +- Fix: Send tool_result blocks for skipped tools in native protocol (PR #9457 by @daniel-lxs) +- Fix: Improve markdown formatting and add reasoning support (PR #9458 by @daniel-lxs) +- Fix: Prevent duplicate environment_details when resuming cancelled tasks (PR #9442 by @daniel-lxs) +- Improve read_file tool description with examples (PR #9422 by @daniel-lxs) +- Update glob dependency to ^11.1.0 (PR #9449 by @jr) +- Update tar-fs to 3.1.1 via pnpm override (PR #9450 by @app/roomote) + +## [3.33.2] - 2025-11-19 + +- Enable native tool calling for Gemini provider (PR #9343 by @hannesrudolph) +- Add RCC credit balance display (PR #9386 by @jr) +- Fix: Preserve user images in native tool call results (PR #9401 by @daniel-lxs) +- Perf: Reduce excessive getModel() calls and implement disk cache fallback (PR #9410 by @daniel-lxs) +- Show zero price for free models (PR #9419 by @mrubens) + +## [3.33.1] - 2025-11-18 + +![3.33.1 Release - Native Tool Protocol Fixes](/releases/3.33.1-release.png) + +- Add native tool calling support to OpenAI-compatible (PR #9369 by @mrubens) +- Fix: Resolve native tool protocol race condition causing 400 errors (PR #9363 by @daniel-lxs) +- Fix: Update tools to return structured JSON for native protocol (PR #9373 by @daniel-lxs) +- Fix: Include nativeArgs in tool repetition detection (PR #9377 by @daniel-lxs) +- Fix: Ensure no XML parsing when protocol is native (PR #9371 by @daniel-lxs) +- Fix: Gemini maxOutputTokens and reasoning config (PR #9375 by @hannesrudolph) +- Fix: Gemini thought signature validation and token counting errors (PR #9380 by @hannesrudolph) +- Fix: Exclude XML tool examples from MODES section when native protocol enabled (PR #9367 by @daniel-lxs) +- Retry eval tasks if API instability detected (PR #9365 by @cte) +- Add toolProtocol property to PostHog tool usage telemetry (PR #9374 by @app/roomote) + +## [3.33.0] - 2025-11-18 + +![3.33.0 Release - Twin Kangaroos and the Gemini Constellation](/releases/3.33.0-release.png) + +- Add Gemini 3 Pro Preview model (PR #9357 by @hannesrudolph) +- Improve Google Gemini defaults with better temperature and cost reporting (PR #9327 by @hannesrudolph) +- Enable native tool calling for openai-native provider (PR #9348 by @hannesrudolph) +- Add git status information to environment details (PR #9310 by @daniel-lxs) +- Add tool protocol selector to advanced settings (PR #9324 by @daniel-lxs) +- Implement dynamic tool protocol resolution with proper precedence hierarchy (PR #9286 by @daniel-lxs) +- Move Import/Export functionality to Modes view toolbar and cleanup Mode Edit view (PR #9077 by @hannesrudolph) +- Update cloud agent CTA to point to setup page (PR #9338 by @app/roomote) +- Fix: Prevent duplicate tool_result blocks in native tool protocol (PR #9248 by @daniel-lxs) +- Fix: Format tool responses properly for native protocol (PR #9270 by @daniel-lxs) +- Fix: Centralize toolProtocol configuration checks (PR #9279 by @daniel-lxs) +- Fix: Preserve tool blocks for native protocol in conversation history (PR #9319 by @daniel-lxs) +- Fix: Prevent infinite loop when task_done succeeds (PR #9325 by @daniel-lxs) +- Fix: Sync parser state with profile/model changes (PR #9355 by @daniel-lxs) +- Fix: Pass tool protocol parameter to lineCountTruncationError (PR #9358 by @daniel-lxs) +- Use VSCode theme color for outline button borders (PR #9336 by @app/roomote) +- Replace broken badgen.net badges with shields.io (PR #9318 by @app/roomote) +- Add max git status files setting to evals (PR #9322 by @mrubens) +- Roo Code Cloud Provider pricing page and changes elsewhere (PR #9195 by @brunobergher) + +## [3.32.1] - 2025-11-14 + +![3.32.1 Release - Bug Fixes](/releases/3.32.1-release.png) + +- Fix: Add abort controller for request cancellation in OpenAI native protocol (PR #9276 by @daniel-lxs) +- Fix: Resolve duplicate tool blocks causing 'tool has already been used' error in native protocol mode (PR #9275 by @daniel-lxs) +- Fix: Prevent duplicate tool_result blocks in native protocol mode for read_file (PR #9272 by @daniel-lxs) +- Fix: Correct OpenAI Native handling of encrypted reasoning blocks to prevent errors during condensing (PR #9263 by @hannesrudolph) +- Fix: Disable XML parser for native tool protocol to prevent parsing conflicts (PR #9277 by @daniel-lxs) + +## [3.32.0] - 2025-11-14 + +![3.32.0 Release - GPT-5.1 models and OpenAI prompt caching](/releases/3.32.0-release.png) + +- Feature: Add GPT-5.1 models to OpenAI provider (PR #9252 by @hannesrudolph) +- Feature: Support for OpenAI Responses 24 hour prompt caching (PR #9259 by @hannesrudolph) +- Fix: Repair the share button in the UI (PR #9253 by @hannesrudolph) +- Docs: Include PR numbers in the release guide to improve traceability (PR #9236 by @hannesrudolph) + +## [3.31.3] - 2025-11-13 + +![3.31.3 Release - Kangaroo Decrypting a Message](/releases/3.31.3-release.png) + +- Fix: OpenAI Native encrypted_content handling and remove gpt-5-chat-latest verbosity flag (#9225 by @politsin, PR by @hannesrudolph) +- Fix: Roo Code Cloud provider Anthropic input token normalization to avoid double-counting (thanks @hannesrudolph!) +- Refactor: Rename sliding-window to context-management and truncateConversationIfNeeded to manageContext (thanks @hannesrudolph!) + ## [3.31.2] - 2025-11-12 - Fix: Apply updated API profile settings when provider/model unchanged (#9208 by @hannesrudolph, PR by @hannesrudolph) @@ -214,7 +766,7 @@ ## [3.28.11] - 2025-09-29 -- Fix: Correct AWS Bedrock Claude Sonnet 4.5 model identifier (#8371 by @sunhyung, PR by @app/roomote) +- Fix: Correct Amazon Bedrock Claude Sonnet 4.5 model identifier (#8371 by @sunhyung, PR by @app/roomote) - Fix: Correct Claude Sonnet 4.5 model ID format (thanks @daniel-lxs!) ## [3.28.10] - 2025-09-29 @@ -546,7 +1098,7 @@ ## [3.25.14] - 2025-08-13 - Fix: Only include verbosity parameter for models that support it (#7054 by @eastonmeth, PR by @app/roomote) -- Fix: AWS Bedrock 1M context - Move anthropic_beta to additionalModelRequestFields (thanks @daniel-lxs!) +- Fix: Amazon Bedrock 1M context - Move anthropic_beta to additionalModelRequestFields (thanks @daniel-lxs!) - Fix: Make cancelling requests more responsive by reverting recent changes ## [3.25.13] - 2025-08-12 @@ -911,7 +1463,7 @@ - Add user-configurable search score threshold slider for semantic search (thanks @hannesrudolph!) - Add default headers and testing for litellm fetcher (thanks @andrewshu2000!) - Fix consistent cancellation error messages for thinking vs streaming phases -- Fix AWS Bedrock cross-region inference profile mapping (thanks @KevinZhao!) +- Fix Amazon Bedrock cross-region inference profile mapping (thanks @KevinZhao!) - Fix URL loading timeout issues in @ mentions (thanks @MuriloFP!) - Fix API retry exponential backoff capped at 10 minutes (thanks @MuriloFP!) - Fix Qdrant URL field auto-filling with default value (thanks @SannidhyaSah!) @@ -925,7 +1477,7 @@ - Suppress Mermaid error rendering - Improve Mermaid buttons with light background in light mode (thanks @chrarnoldus!) - Add .vscode/ to write-protected files/directories -- Update AWS Bedrock cross-region inference profile mapping (thanks @KevinZhao!) +- Update Amazon Bedrock cross-region inference profile mapping (thanks @KevinZhao!) ## [3.22.5] - 2025-06-28 @@ -1549,7 +2101,7 @@ - Improved display of diff errors + easy copying for investigation - Fixes to .vscodeignore (thanks @franekp!) - Fix a zh-CN translation for model capabilities (thanks @zhangtony239!) -- Rename AWS Bedrock to Amazon Bedrock (thanks @ronyblum!) +- Rename Amazon Bedrock to Amazon Bedrock (thanks @ronyblum!) - Update extension title and description (thanks @StevenTCramer!) ## [3.11.12] - 2025-04-09 @@ -1798,12 +2350,12 @@ - PowerShell-specific command handling (thanks @KJ7LNW!) - OpenAI-compatible DeepSeek/QwQ reasoning support (thanks @lightrabbit!) - Anthropic-style prompt caching in the OpenAI-compatible provider (thanks @dleen!) -- Add Deepseek R1 for AWS Bedrock (thanks @ATempsch!) +- Add Deepseek R1 for Amazon Bedrock (thanks @ATempsch!) - Fix MarkdownBlock text color for Dark High Contrast theme (thanks @cannuri!) - Add gemini-2.0-pro-exp-02-05 model to vertex (thanks @shohei-ihaya!) - Bring back progress status for multi-diff edits (thanks @qdaxb!) - Refactor alert dialog styles to use the correct vscode theme (thanks @cannuri!) -- Custom ARNs in AWS Bedrock (thanks @Smartsheet-JB-Brown!) +- Custom ARNs in Amazon Bedrock (thanks @Smartsheet-JB-Brown!) - Update MCP servers directory path for platform compatibility (thanks @hannesrudolph!) - Fix browser system prompt inclusion rules (thanks @cannuri!) - Publish git tags to github from CI (thanks @pdecat!) @@ -1941,7 +2493,7 @@ ## [3.7.1] - 2025-02-24 -- Add AWS Bedrock support for Sonnet 3.7 and update some defaults to Sonnet 3.7 instead of 3.5 +- Add Amazon Bedrock support for Sonnet 3.7 and update some defaults to Sonnet 3.7 instead of 3.5 ## [3.7.0] - 2025-02-24 @@ -1958,7 +2510,7 @@ ## [3.3.24] - 2025-02-20 -- Fixed a bug with region selection preventing AWS Bedrock profiles from being saved (thanks @oprstchn!) +- Fixed a bug with region selection preventing Amazon Bedrock profiles from being saved (thanks @oprstchn!) - Updated the price of gpt-4o (thanks @marvijo-code!) ## [3.3.23] - 2025-02-20 @@ -2142,7 +2694,7 @@ - Reverts provider key entry back to checking onInput instead of onChange to hopefully address issues entering API keys (thanks @samhvw8!) - Added explicit checkbox to use Azure for OpenAI compatible providers (thanks @samhvw8!) - Fixed Glama usage reporting (thanks @punkpeye!) -- Added Llama 3.3 70B Instruct model to the AWS Bedrock provider options (thanks @Premshay!) +- Added Llama 3.3 70B Instruct model to the Amazon Bedrock provider options (thanks @Premshay!) ## [3.2.7] diff --git a/README.md b/README.md index d6a7d99c8e..ca85189562 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,5 @@

- VS Code - Installs - Rating + VS Code Marketplace X YouTube Join Discord @@ -68,10 +66,10 @@ Learn more: [Using Modes](https://docs.roocode.com/basic-usage/using-modes) •

-| | | | -| :-----------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------: | -|
Installing Roo Code |
Configuring Profiles |
Codebase Indexing | -|
Custom Modes |
Checkpoints |
Todo Lists | +| | | | +| :-----------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------: | +|
Installing Roo Code |
Configuring Profiles |
Codebase Indexing | +|
Custom Modes |
Checkpoints |
Context Management |

@@ -169,16 +167,6 @@ We love community contributions! Get started by reading our [CONTRIBUTING.md](CO --- -## Contributors - -Thanks to all our contributors who have helped make Roo Code better! - - - -[![Contributors](https://contrib.rocks/image?repo=RooCodeInc/roo-code&max=120&columns=12&cacheBust=0000000000)](https://github.com/RooCodeInc/roo-code/graphs/contributors) - - - ## License [Apache 2.0 © 2025 Roo Code, Inc.](./LICENSE) diff --git a/apps/cli/README.md b/apps/cli/README.md new file mode 100644 index 0000000000..3e78192d4e --- /dev/null +++ b/apps/cli/README.md @@ -0,0 +1,231 @@ +# @roo-code/cli + +Command Line Interface for Roo Code - Run the Roo Code agent from the terminal without VSCode. + +## Overview + +This CLI uses the `@roo-code/vscode-shim` package to provide a VSCode API compatibility layer, allowing the main Roo Code extension to run in a Node.js environment. + +## Installation + +### Quick Install (Recommended) + +Install the Roo Code CLI with a single command: + +```bash +curl -fsSL https://raw.githubusercontent.com/RooCodeInc/Roo-Code/main/apps/cli/install.sh | sh +``` + +**Requirements:** + +- Node.js 20 or higher +- macOS (Intel or Apple Silicon) or Linux (x64 or ARM64) + +**Custom installation directory:** + +```bash +ROO_INSTALL_DIR=/opt/roo-code ROO_BIN_DIR=/usr/local/bin curl -fsSL ... | sh +``` + +**Install a specific version:** + +```bash +ROO_VERSION=0.1.0 curl -fsSL https://raw.githubusercontent.com/RooCodeInc/Roo-Code/main/apps/cli/install.sh | sh +``` + +### Updating + +Re-run the install script to update to the latest version: + +```bash +curl -fsSL https://raw.githubusercontent.com/RooCodeInc/Roo-Code/main/apps/cli/install.sh | sh +``` + +### Uninstalling + +```bash +rm -rf ~/.roo/cli ~/.local/bin/roo +``` + +### Development Installation + +For contributing or development: + +```bash +# From the monorepo root. +pnpm install + +# Build the main extension first. +pnpm --filter roo-cline bundle + +# Build the cli. +pnpm --filter @roo-code/cli build +``` + +## Usage + +### Interactive Mode (Default) + +By default, the CLI prompts for approval before executing actions: + +```bash +export OPENROUTER_API_KEY=sk-or-v1-... + +roo "What is this project?" --workspace ~/Documents/my-project +``` + +In interactive mode: + +- Tool executions prompt for yes/no approval +- Commands prompt for yes/no approval +- Followup questions show suggestions and wait for user input +- Browser and MCP actions prompt for approval + +### Non-Interactive Mode (`-y`) + +For automation and scripts, use `-y` to auto-approve all actions: + +```bash +roo -y "Refactor the utils.ts file" --workspace ~/Documents/my-project +``` + +In non-interactive mode: + +- Tool, command, browser, and MCP actions are auto-approved +- Followup questions show a 10-second timeout, then auto-select the first suggestion +- Typing any key cancels the timeout and allows manual input + +## Options + +| Option | Description | Default | +| --------------------------------- | ------------------------------------------------------------------------------ | ----------------- | +| `-w, --workspace ` | Workspace path to operate in | Current directory | +| `-e, --extension ` | Path to the extension bundle directory | Auto-detected | +| `-v, --verbose` | Enable verbose output (show VSCode and extension logs) | `false` | +| `-d, --debug` | Enable debug output (includes detailed debug information, prompts, paths, etc) | `false` | +| `-x, --exit-on-complete` | Exit the process when task completes (useful for testing) | `false` | +| `-y, --yes` | Non-interactive mode: auto-approve all actions | `false` | +| `-k, --api-key ` | API key for the LLM provider | From env var | +| `-p, --provider ` | API provider (anthropic, openai, openrouter, etc.) | `openrouter` | +| `-m, --model ` | Model to use | Provider default | +| `-M, --mode ` | Mode to start in (code, architect, ask, debug, etc.) | `code` | +| `-r, --reasoning-effort ` | Reasoning effort level (none, minimal, low, medium, high, xhigh) | `medium` | + +By default, the CLI runs in quiet mode (suppressing VSCode/extension logs) and only shows assistant output. Use `-v` to see all logs, or `-d` for detailed debug information. + +## Environment Variables + +The CLI will look for API keys in environment variables if not provided via `--api-key`: + +| Provider | Environment Variable | +| ------------- | -------------------- | +| anthropic | `ANTHROPIC_API_KEY` | +| openai | `OPENAI_API_KEY` | +| openrouter | `OPENROUTER_API_KEY` | +| google/gemini | `GOOGLE_API_KEY` | +| mistral | `MISTRAL_API_KEY` | +| deepseek | `DEEPSEEK_API_KEY` | +| bedrock | `AWS_ACCESS_KEY_ID` | + +## Architecture + +``` +┌─────────────────┐ +│ CLI Entry │ +│ (index.ts) │ +└────────┬────────┘ + │ + ▼ +┌─────────────────┐ +│ ExtensionHost │ +│ (extension- │ +│ host.ts) │ +└────────┬────────┘ + │ + ┌────┴────┐ + │ │ + ▼ ▼ +┌───────┐ ┌──────────┐ +│vscode │ │Extension │ +│-shim │ │ Bundle │ +└───────┘ └──────────┘ +``` + +## How It Works + +1. **CLI Entry Point** (`index.ts`): Parses command line arguments and initializes the ExtensionHost + +2. **ExtensionHost** (`extension-host.ts`): + + - Creates a VSCode API mock using `@roo-code/vscode-shim` + - Intercepts `require('vscode')` to return the mock + - Loads and activates the extension bundle + - Manages bidirectional message flow + +3. **Message Flow**: + - CLI → Extension: `emit("webviewMessage", {...})` + - Extension → CLI: `emit("extensionWebviewMessage", {...})` + +## Current Limitations + +- **No TUI**: Output is plain text (no React/Ink UI yet) +- **No configuration file**: Settings are passed via command line flags +- **No persistence**: Each run is a fresh session + +## Development + +```bash +# Watch mode for development +pnpm dev + +# Run tests +pnpm test + +# Type checking +pnpm check-types + +# Linting +pnpm lint +``` + +## Releasing + +To create a new release, run the release script from the monorepo root: + +```bash +# Release using version from package.json +./apps/cli/scripts/release.sh + +# Release with a specific version +./apps/cli/scripts/release.sh 0.1.0 +``` + +The script will: + +1. Build the extension and CLI +2. Create a platform-specific tarball (for your current OS/architecture) +3. Create a GitHub release with the tarball attached + +**Prerequisites:** + +- GitHub CLI (`gh`) installed and authenticated (`gh auth login`) +- pnpm installed + +## Troubleshooting + +### Extension bundle not found + +Make sure you've built the main extension first: + +```bash +cd src +pnpm bundle +``` + +### Module resolution errors + +The CLI expects the extension to be a CommonJS bundle. Make sure the extension's esbuild config outputs CommonJS. + +### "vscode" module not found + +The CLI intercepts `require('vscode')` calls. If you see this error, the module resolution interception may have failed. diff --git a/apps/cli/eslint.config.mjs b/apps/cli/eslint.config.mjs new file mode 100644 index 0000000000..694bf73664 --- /dev/null +++ b/apps/cli/eslint.config.mjs @@ -0,0 +1,4 @@ +import { config } from "@roo-code/config-eslint/base" + +/** @type {import("eslint").Linter.Config} */ +export default [...config] diff --git a/apps/cli/install.sh b/apps/cli/install.sh new file mode 100755 index 0000000000..ca82ecfdd8 --- /dev/null +++ b/apps/cli/install.sh @@ -0,0 +1,287 @@ +#!/bin/sh +# Roo Code CLI Installer +# Usage: curl -fsSL https://raw.githubusercontent.com/RooCodeInc/Roo-Code/main/apps/cli/install.sh | sh +# +# Environment variables: +# ROO_INSTALL_DIR - Installation directory (default: ~/.roo/cli) +# ROO_BIN_DIR - Binary symlink directory (default: ~/.local/bin) +# ROO_VERSION - Specific version to install (default: latest) + +set -e + +# Configuration +INSTALL_DIR="${ROO_INSTALL_DIR:-$HOME/.roo/cli}" +BIN_DIR="${ROO_BIN_DIR:-$HOME/.local/bin}" +REPO="RooCodeInc/Roo-Code" +MIN_NODE_VERSION=20 + +# Color output (only if terminal supports it) +if [ -t 1 ]; then + RED='\033[0;31m' + GREEN='\033[0;32m' + YELLOW='\033[1;33m' + BLUE='\033[0;34m' + BOLD='\033[1m' + NC='\033[0m' +else + RED='' + GREEN='' + YELLOW='' + BLUE='' + BOLD='' + NC='' +fi + +info() { printf "${GREEN}==>${NC} %s\n" "$1"; } +warn() { printf "${YELLOW}Warning:${NC} %s\n" "$1"; } +error() { printf "${RED}Error:${NC} %s\n" "$1" >&2; exit 1; } + +# Check Node.js version +check_node() { + if ! command -v node >/dev/null 2>&1; then + error "Node.js is not installed. Please install Node.js $MIN_NODE_VERSION or higher. + +Install Node.js: + - macOS: brew install node + - Linux: https://nodejs.org/en/download/package-manager + - Or use a version manager like fnm, nvm, or mise" + fi + + NODE_VERSION=$(node -v | sed 's/v//' | cut -d. -f1) + if [ "$NODE_VERSION" -lt "$MIN_NODE_VERSION" ]; then + error "Node.js $MIN_NODE_VERSION+ required. Found: $(node -v) + +Please upgrade Node.js to version $MIN_NODE_VERSION or higher." + fi + + info "Found Node.js $(node -v)" +} + +# Detect OS and architecture +detect_platform() { + OS=$(uname -s | tr '[:upper:]' '[:lower:]') + ARCH=$(uname -m) + + case "$OS" in + darwin) OS="darwin" ;; + linux) OS="linux" ;; + mingw*|msys*|cygwin*) + error "Windows is not supported by this installer. Please use WSL or install manually." + ;; + *) error "Unsupported OS: $OS" ;; + esac + + case "$ARCH" in + x86_64|amd64) ARCH="x64" ;; + arm64|aarch64) ARCH="arm64" ;; + *) error "Unsupported architecture: $ARCH" ;; + esac + + PLATFORM="${OS}-${ARCH}" + info "Detected platform: $PLATFORM" +} + +# Get latest release version or use specified version +get_version() { + if [ -n "$ROO_VERSION" ]; then + VERSION="$ROO_VERSION" + info "Using specified version: $VERSION" + return + fi + + info "Fetching latest version..." + + # Try to get the latest cli release + RELEASES_JSON=$(curl -fsSL "https://api.github.com/repos/$REPO/releases" 2>/dev/null) || { + error "Failed to fetch releases from GitHub. Check your internet connection." + } + + # Extract the latest cli-v* tag + VERSION=$(echo "$RELEASES_JSON" | + grep -o '"tag_name": "cli-v[^"]*"' | + head -1 | + sed 's/"tag_name": "cli-v//' | + sed 's/"//') + + if [ -z "$VERSION" ]; then + error "Could not find any CLI releases. The CLI may not have been released yet." + fi + + info "Latest version: $VERSION" +} + +# Download and extract +download_and_install() { + TARBALL="roo-cli-${PLATFORM}.tar.gz" + URL="https://github.com/$REPO/releases/download/cli-v${VERSION}/${TARBALL}" + + info "Downloading from $URL..." + + # Create temp directory + TMP_DIR=$(mktemp -d) + trap "rm -rf $TMP_DIR" EXIT + + # Download with progress indicator + HTTP_CODE=$(curl -fsSL -w "%{http_code}" "$URL" -o "$TMP_DIR/$TARBALL" 2>/dev/null) || { + if [ "$HTTP_CODE" = "404" ]; then + error "Release not found for platform $PLATFORM version $VERSION. + +Available at: https://github.com/$REPO/releases" + fi + error "Download failed. HTTP code: $HTTP_CODE" + } + + # Verify we got something + if [ ! -s "$TMP_DIR/$TARBALL" ]; then + error "Downloaded file is empty. Please try again." + fi + + # Remove old installation if exists + if [ -d "$INSTALL_DIR" ]; then + info "Removing previous installation..." + rm -rf "$INSTALL_DIR" + fi + + mkdir -p "$INSTALL_DIR" + + # Extract + info "Extracting to $INSTALL_DIR..." + tar -xzf "$TMP_DIR/$TARBALL" -C "$INSTALL_DIR" --strip-components=1 || { + error "Failed to extract tarball. The download may be corrupted." + } + + # Save ripgrep binary before npm install (npm install will overwrite node_modules) + RIPGREP_BIN="" + if [ -f "$INSTALL_DIR/node_modules/@vscode/ripgrep/bin/rg" ]; then + RIPGREP_BIN="$TMP_DIR/rg" + cp "$INSTALL_DIR/node_modules/@vscode/ripgrep/bin/rg" "$RIPGREP_BIN" + fi + + # Install npm dependencies + info "Installing dependencies..." + cd "$INSTALL_DIR" + npm install --production --silent 2>/dev/null || { + warn "npm install failed, trying with --legacy-peer-deps..." + npm install --production --legacy-peer-deps --silent 2>/dev/null || { + error "Failed to install dependencies. Make sure npm is available." + } + } + cd - > /dev/null + + # Restore ripgrep binary after npm install + if [ -n "$RIPGREP_BIN" ] && [ -f "$RIPGREP_BIN" ]; then + mkdir -p "$INSTALL_DIR/node_modules/@vscode/ripgrep/bin" + cp "$RIPGREP_BIN" "$INSTALL_DIR/node_modules/@vscode/ripgrep/bin/rg" + chmod +x "$INSTALL_DIR/node_modules/@vscode/ripgrep/bin/rg" + fi + + # Make executable + chmod +x "$INSTALL_DIR/bin/roo" + + # Also make ripgrep executable if it exists + if [ -f "$INSTALL_DIR/bin/rg" ]; then + chmod +x "$INSTALL_DIR/bin/rg" + fi +} + +# Create symlink in bin directory +setup_bin() { + mkdir -p "$BIN_DIR" + + # Remove old symlink if exists + if [ -L "$BIN_DIR/roo" ] || [ -f "$BIN_DIR/roo" ]; then + rm -f "$BIN_DIR/roo" + fi + + ln -sf "$INSTALL_DIR/bin/roo" "$BIN_DIR/roo" + info "Created symlink: $BIN_DIR/roo" +} + +# Check if bin dir is in PATH and provide instructions +check_path() { + case ":$PATH:" in + *":$BIN_DIR:"*) + # Already in PATH + return 0 + ;; + esac + + warn "$BIN_DIR is not in your PATH" + echo "" + echo "Add this line to your shell profile:" + echo "" + + # Detect shell and provide specific instructions + SHELL_NAME=$(basename "$SHELL") + case "$SHELL_NAME" in + zsh) + echo " echo 'export PATH=\"$BIN_DIR:\$PATH\"' >> ~/.zshrc" + echo " source ~/.zshrc" + ;; + bash) + if [ -f "$HOME/.bashrc" ]; then + echo " echo 'export PATH=\"$BIN_DIR:\$PATH\"' >> ~/.bashrc" + echo " source ~/.bashrc" + else + echo " echo 'export PATH=\"$BIN_DIR:\$PATH\"' >> ~/.bash_profile" + echo " source ~/.bash_profile" + fi + ;; + fish) + echo " set -Ux fish_user_paths $BIN_DIR \$fish_user_paths" + ;; + *) + echo " export PATH=\"$BIN_DIR:\$PATH\"" + ;; + esac + echo "" +} + +# Verify installation +verify_install() { + if [ -x "$BIN_DIR/roo" ]; then + info "Verifying installation..." + # Just check if it runs without error + "$BIN_DIR/roo" --version >/dev/null 2>&1 || true + fi +} + +# Print success message +print_success() { + echo "" + printf "${GREEN}${BOLD}✓ Roo Code CLI installed successfully!${NC}\n" + echo "" + echo " Installation: $INSTALL_DIR" + echo " Binary: $BIN_DIR/roo" + echo " Version: $VERSION" + echo "" + echo " ${BOLD}Get started:${NC}" + echo " roo --help" + echo "" + echo " ${BOLD}Example:${NC}" + echo " export OPENROUTER_API_KEY=sk-or-v1-..." + echo " roo \"What is this project?\" --workspace ~/my-project" + echo "" +} + +# Main +main() { + echo "" + printf "${BLUE}${BOLD}" + echo " ╭─────────────────────────────────╮" + echo " │ Roo Code CLI Installer │" + echo " ╰─────────────────────────────────╯" + printf "${NC}" + echo "" + + check_node + detect_platform + get_version + download_and_install + setup_bin + check_path + verify_install + print_success +} + +main "$@" diff --git a/apps/cli/package.json b/apps/cli/package.json new file mode 100644 index 0000000000..f4c4a3bcb5 --- /dev/null +++ b/apps/cli/package.json @@ -0,0 +1,35 @@ +{ + "name": "@roo-code/cli", + "version": "0.1.0", + "description": "Roo Code CLI - Run the Roo Code agent from the command line", + "private": true, + "type": "module", + "main": "dist/index.js", + "bin": { + "roo": "dist/index.js" + }, + "scripts": { + "format": "prettier --write 'src/**/*.ts'", + "lint": "eslint src --ext .ts --max-warnings=0", + "check-types": "tsc --noEmit", + "test": "vitest run", + "build": "tsup", + "start": "node dist/index.js", + "clean": "rimraf dist .turbo" + }, + "dependencies": { + "@roo-code/types": "workspace:^", + "@roo-code/vscode-shim": "workspace:^", + "@vscode/ripgrep": "^1.15.9", + "commander": "^12.1.0" + }, + "devDependencies": { + "@roo-code/config-eslint": "workspace:^", + "@roo-code/config-typescript": "workspace:^", + "@types/node": "^24.1.0", + "rimraf": "^6.0.1", + "tsup": "^8.4.0", + "typescript": "5.8.3", + "vitest": "^3.2.3" + } +} diff --git a/apps/cli/scripts/release.sh b/apps/cli/scripts/release.sh new file mode 100755 index 0000000000..43d5298956 --- /dev/null +++ b/apps/cli/scripts/release.sh @@ -0,0 +1,363 @@ +#!/bin/bash +# Roo Code CLI Release Script +# +# Usage: +# ./apps/cli/scripts/release.sh [version] +# +# Examples: +# ./apps/cli/scripts/release.sh # Use version from package.json +# ./apps/cli/scripts/release.sh 0.1.0 # Specify version +# +# This script: +# 1. Builds the extension and CLI +# 2. Creates a tarball for the current platform +# 3. Creates a GitHub release and uploads the tarball +# +# Prerequisites: +# - GitHub CLI (gh) installed and authenticated +# - pnpm installed +# - Run from the monorepo root directory + +set -e + +# Colors +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +BOLD='\033[1m' +NC='\033[0m' + +info() { printf "${GREEN}==>${NC} %s\n" "$1"; } +warn() { printf "${YELLOW}Warning:${NC} %s\n" "$1"; } +error() { printf "${RED}Error:${NC} %s\n" "$1" >&2; exit 1; } +step() { printf "${BLUE}${BOLD}[%s]${NC} %s\n" "$1" "$2"; } + +# Get script directory and repo root +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +REPO_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)" +CLI_DIR="$REPO_ROOT/apps/cli" + +# Detect current platform +detect_platform() { + OS=$(uname -s | tr '[:upper:]' '[:lower:]') + ARCH=$(uname -m) + + case "$OS" in + darwin) OS="darwin" ;; + linux) OS="linux" ;; + *) error "Unsupported OS: $OS" ;; + esac + + case "$ARCH" in + x86_64|amd64) ARCH="x64" ;; + arm64|aarch64) ARCH="arm64" ;; + *) error "Unsupported architecture: $ARCH" ;; + esac + + PLATFORM="${OS}-${ARCH}" +} + +# Check prerequisites +check_prerequisites() { + step "1/7" "Checking prerequisites..." + + if ! command -v gh &> /dev/null; then + error "GitHub CLI (gh) is not installed. Install it with: brew install gh" + fi + + if ! gh auth status &> /dev/null; then + error "GitHub CLI is not authenticated. Run: gh auth login" + fi + + if ! command -v pnpm &> /dev/null; then + error "pnpm is not installed." + fi + + if ! command -v node &> /dev/null; then + error "Node.js is not installed." + fi + + info "Prerequisites OK" +} + +# Get version +get_version() { + if [ -n "$1" ]; then + VERSION="$1" + else + VERSION=$(node -p "require('$CLI_DIR/package.json').version") + fi + + # Validate semver format + if ! echo "$VERSION" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.]+)?$'; then + error "Invalid version format: $VERSION (expected semver like 0.1.0)" + fi + + TAG="cli-v$VERSION" + info "Version: $VERSION (tag: $TAG)" +} + +# Build everything +build() { + step "2/7" "Building extension bundle..." + cd "$REPO_ROOT" + pnpm bundle + + step "3/7" "Building CLI..." + pnpm --filter @roo-code/cli build + + info "Build complete" +} + +# Create release tarball +create_tarball() { + step "4/7" "Creating release tarball for $PLATFORM..." + + RELEASE_DIR="$REPO_ROOT/roo-cli-${PLATFORM}" + TARBALL="roo-cli-${PLATFORM}.tar.gz" + + # Clean up any previous build + rm -rf "$RELEASE_DIR" + rm -f "$REPO_ROOT/$TARBALL" + + # Create directory structure + mkdir -p "$RELEASE_DIR/bin" + mkdir -p "$RELEASE_DIR/lib" + mkdir -p "$RELEASE_DIR/extension" + + # Copy CLI dist files + info "Copying CLI files..." + cp -r "$CLI_DIR/dist/"* "$RELEASE_DIR/lib/" + + # Create package.json for npm install (only runtime dependencies) + info "Creating package.json..." + node -e " + const pkg = require('$CLI_DIR/package.json'); + const newPkg = { + name: '@roo-code/cli', + version: pkg.version, + type: 'module', + dependencies: { + commander: pkg.dependencies.commander + } + }; + console.log(JSON.stringify(newPkg, null, 2)); + " > "$RELEASE_DIR/package.json" + + # Copy extension bundle + info "Copying extension bundle..." + cp -r "$REPO_ROOT/src/dist/"* "$RELEASE_DIR/extension/" + + # Add package.json to extension directory to mark it as CommonJS + # This is necessary because the main package.json has "type": "module" + # but the extension bundle is CommonJS + echo '{"type": "commonjs"}' > "$RELEASE_DIR/extension/package.json" + + # Find and copy ripgrep binary + # The extension looks for ripgrep at: appRoot/node_modules/@vscode/ripgrep/bin/rg + # The CLI sets appRoot to the CLI package root, so we need to put ripgrep there + info "Looking for ripgrep binary..." + RIPGREP_PATH=$(find "$REPO_ROOT/node_modules" -path "*/@vscode/ripgrep/bin/rg" -type f 2>/dev/null | head -1) + if [ -n "$RIPGREP_PATH" ] && [ -f "$RIPGREP_PATH" ]; then + info "Found ripgrep at: $RIPGREP_PATH" + # Create the expected directory structure for the extension to find ripgrep + mkdir -p "$RELEASE_DIR/node_modules/@vscode/ripgrep/bin" + cp "$RIPGREP_PATH" "$RELEASE_DIR/node_modules/@vscode/ripgrep/bin/" + chmod +x "$RELEASE_DIR/node_modules/@vscode/ripgrep/bin/rg" + # Also keep a copy in bin/ for direct access + mkdir -p "$RELEASE_DIR/bin" + cp "$RIPGREP_PATH" "$RELEASE_DIR/bin/" + chmod +x "$RELEASE_DIR/bin/rg" + else + warn "ripgrep binary not found - users will need ripgrep installed" + fi + + # Create the wrapper script + info "Creating wrapper script..." + cat > "$RELEASE_DIR/bin/roo" << 'WRAPPER_EOF' +#!/usr/bin/env node + +import { fileURLToPath } from 'url'; +import { dirname, join } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +// Set environment variables for the CLI +process.env.ROO_EXTENSION_PATH = join(__dirname, '..', 'extension'); +process.env.ROO_RIPGREP_PATH = join(__dirname, 'rg'); + +// Import and run the actual CLI +await import(join(__dirname, '..', 'lib', 'index.js')); +WRAPPER_EOF + + chmod +x "$RELEASE_DIR/bin/roo" + + # Create version file + echo "$VERSION" > "$RELEASE_DIR/VERSION" + + # Create tarball + info "Creating tarball..." + cd "$REPO_ROOT" + tar -czvf "$TARBALL" "$(basename "$RELEASE_DIR")" + + # Clean up release directory + rm -rf "$RELEASE_DIR" + + # Show size + TARBALL_PATH="$REPO_ROOT/$TARBALL" + TARBALL_SIZE=$(ls -lh "$TARBALL_PATH" | awk '{print $5}') + info "Created: $TARBALL ($TARBALL_SIZE)" +} + +# Create checksum +create_checksum() { + step "5/7" "Creating checksum..." + cd "$REPO_ROOT" + + if command -v sha256sum &> /dev/null; then + sha256sum "$TARBALL" > "${TARBALL}.sha256" + elif command -v shasum &> /dev/null; then + shasum -a 256 "$TARBALL" > "${TARBALL}.sha256" + else + warn "No sha256sum or shasum found, skipping checksum" + return + fi + + info "Checksum: $(cat "${TARBALL}.sha256")" +} + +# Check if release already exists +check_existing_release() { + step "6/7" "Checking for existing release..." + + if gh release view "$TAG" &> /dev/null; then + warn "Release $TAG already exists" + read -p "Do you want to delete it and create a new one? [y/N] " -n 1 -r + echo + if [[ $REPLY =~ ^[Yy]$ ]]; then + info "Deleting existing release..." + gh release delete "$TAG" --yes + # Also delete the tag if it exists + git tag -d "$TAG" 2>/dev/null || true + git push origin ":refs/tags/$TAG" 2>/dev/null || true + else + error "Aborted. Use a different version or delete the existing release manually." + fi + fi +} + +# Create GitHub release +create_release() { + step "7/7" "Creating GitHub release..." + cd "$REPO_ROOT" + + RELEASE_NOTES=$(cat << EOF +## Installation + +\`\`\`bash +curl -fsSL https://raw.githubusercontent.com/RooCodeInc/Roo-Code/main/apps/cli/install.sh | sh +\`\`\` + +Or install a specific version: +\`\`\`bash +ROO_VERSION=$VERSION curl -fsSL https://raw.githubusercontent.com/RooCodeInc/Roo-Code/main/apps/cli/install.sh | sh +\`\`\` + +## Requirements + +- Node.js 20 or higher +- macOS (Intel or Apple Silicon) or Linux (x64 or ARM64) + +## Usage + +\`\`\`bash +# Set your API key +export OPENROUTER_API_KEY=sk-or-v1-... + +# Run a task +roo "What is this project?" --workspace ~/my-project + +# See all options +roo --help +\`\`\` + +## Platform Support + +This release includes: +- \`roo-cli-${PLATFORM}.tar.gz\` - Built on $(uname -s) $(uname -m) + +> **Note:** Additional platforms will be added as needed. If you need a different platform, please open an issue. + +## Checksum + +\`\`\` +$(cat "${TARBALL}.sha256" 2>/dev/null || echo "N/A") +\`\`\` +EOF +) + + # Get the current commit SHA for the release target + COMMIT_SHA=$(git rev-parse HEAD) + info "Creating release at commit: ${COMMIT_SHA:0:8}" + + # Create release (gh will create the tag automatically) + info "Creating release..." + RELEASE_FILES="$TARBALL" + if [ -f "${TARBALL}.sha256" ]; then + RELEASE_FILES="$RELEASE_FILES ${TARBALL}.sha256" + fi + + gh release create "$TAG" \ + --title "Roo Code CLI v$VERSION" \ + --notes "$RELEASE_NOTES" \ + --prerelease \ + --target "$COMMIT_SHA" \ + $RELEASE_FILES + + info "Release created!" +} + +# Cleanup +cleanup() { + info "Cleaning up..." + cd "$REPO_ROOT" + rm -f "$TARBALL" "${TARBALL}.sha256" +} + +# Print summary +print_summary() { + echo "" + printf "${GREEN}${BOLD}✓ Release v$VERSION created successfully!${NC}\n" + echo "" + echo " Release URL: https://github.com/RooCodeInc/Roo-Code/releases/tag/$TAG" + echo "" + echo " Install with:" + echo " curl -fsSL https://raw.githubusercontent.com/RooCodeInc/Roo-Code/main/apps/cli/install.sh | sh" + echo "" +} + +# Main +main() { + echo "" + printf "${BLUE}${BOLD}" + echo " ╭─────────────────────────────────╮" + echo " │ Roo Code CLI Release Script │" + echo " ╰─────────────────────────────────╯" + printf "${NC}" + echo "" + + detect_platform + check_prerequisites + get_version "$1" + build + create_tarball + create_checksum + check_existing_release + create_release + cleanup + print_summary +} + +main "$@" diff --git a/apps/cli/src/__tests__/extension-host.test.ts b/apps/cli/src/__tests__/extension-host.test.ts new file mode 100644 index 0000000000..509ad27d1e --- /dev/null +++ b/apps/cli/src/__tests__/extension-host.test.ts @@ -0,0 +1,1164 @@ +// pnpm --filter @roo-code/cli test src/__tests__/extension-host.test.ts + +import { ExtensionHost, type ExtensionHostOptions } from "../extension-host.js" +import { EventEmitter } from "events" +import type { ProviderName } from "@roo-code/types" + +vi.mock("@roo-code/vscode-shim", () => ({ + createVSCodeAPI: vi.fn(() => ({ + context: { extensionPath: "/test/extension" }, + })), +})) + +/** + * Create a test ExtensionHost with default options + */ +function createTestHost({ + mode = "code", + apiProvider = "openrouter", + model = "test-model", + ...options +}: Partial = {}): ExtensionHost { + return new ExtensionHost({ + mode, + apiProvider, + model, + workspacePath: "/test/workspace", + extensionPath: "/test/extension", + ...options, + }) +} + +// Type for accessing private members +type PrivateHost = Record + +/** + * Helper to access private members for testing + */ +function getPrivate(host: ExtensionHost, key: string): T { + return (host as unknown as PrivateHost)[key] as T +} + +/** + * Helper to call private methods for testing + */ +function callPrivate(host: ExtensionHost, method: string, ...args: unknown[]): T { + const fn = (host as unknown as PrivateHost)[method] as ((...a: unknown[]) => T) | undefined + if (!fn) throw new Error(`Method ${method} not found`) + return fn.apply(host, args) +} + +/** + * Helper to spy on private methods + * This uses a more permissive type to avoid TypeScript errors with vi.spyOn on private methods + */ +function spyOnPrivate(host: ExtensionHost, method: string) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + return vi.spyOn(host as any, method) +} + +describe("ExtensionHost", () => { + beforeEach(() => { + vi.resetAllMocks() + // Clean up globals + delete (global as Record).vscode + delete (global as Record).__extensionHost + }) + + describe("constructor", () => { + it("should store options correctly", () => { + const options: ExtensionHostOptions = { + mode: "code", + workspacePath: "/my/workspace", + extensionPath: "/my/extension", + verbose: true, + quiet: true, + apiKey: "test-key", + apiProvider: "openrouter", + model: "test-model", + } + + const host = new ExtensionHost(options) + + expect(getPrivate(host, "options")).toEqual(options) + }) + + it("should be an EventEmitter instance", () => { + const host = createTestHost() + expect(host).toBeInstanceOf(EventEmitter) + }) + + it("should initialize with default state values", () => { + const host = createTestHost() + + expect(getPrivate(host, "isWebviewReady")).toBe(false) + expect(getPrivate(host, "pendingMessages")).toEqual([]) + expect(getPrivate(host, "vscode")).toBeNull() + expect(getPrivate(host, "extensionModule")).toBeNull() + }) + }) + + describe("buildApiConfiguration", () => { + it.each([ + [ + "anthropic", + "test-key", + "test-model", + { apiProvider: "anthropic", apiKey: "test-key", apiModelId: "test-model" }, + ], + [ + "openrouter", + "or-key", + "or-model", + { + apiProvider: "openrouter", + openRouterApiKey: "or-key", + openRouterModelId: "or-model", + }, + ], + [ + "gemini", + "gem-key", + "gem-model", + { apiProvider: "gemini", geminiApiKey: "gem-key", apiModelId: "gem-model" }, + ], + [ + "openai-native", + "oai-key", + "oai-model", + { apiProvider: "openai-native", openAiNativeApiKey: "oai-key", apiModelId: "oai-model" }, + ], + [ + "openai", + "oai-key", + "oai-model", + { apiProvider: "openai", openAiApiKey: "oai-key", openAiModelId: "oai-model" }, + ], + [ + "mistral", + "mis-key", + "mis-model", + { apiProvider: "mistral", mistralApiKey: "mis-key", apiModelId: "mis-model" }, + ], + [ + "deepseek", + "ds-key", + "ds-model", + { apiProvider: "deepseek", deepSeekApiKey: "ds-key", apiModelId: "ds-model" }, + ], + ["xai", "xai-key", "xai-model", { apiProvider: "xai", xaiApiKey: "xai-key", apiModelId: "xai-model" }], + [ + "groq", + "groq-key", + "groq-model", + { apiProvider: "groq", groqApiKey: "groq-key", apiModelId: "groq-model" }, + ], + [ + "fireworks", + "fw-key", + "fw-model", + { apiProvider: "fireworks", fireworksApiKey: "fw-key", apiModelId: "fw-model" }, + ], + [ + "cerebras", + "cer-key", + "cer-model", + { apiProvider: "cerebras", cerebrasApiKey: "cer-key", apiModelId: "cer-model" }, + ], + [ + "sambanova", + "sn-key", + "sn-model", + { apiProvider: "sambanova", sambaNovaApiKey: "sn-key", apiModelId: "sn-model" }, + ], + [ + "ollama", + "oll-key", + "oll-model", + { apiProvider: "ollama", ollamaApiKey: "oll-key", ollamaModelId: "oll-model" }, + ], + ["lmstudio", undefined, "lm-model", { apiProvider: "lmstudio", lmStudioModelId: "lm-model" }], + [ + "litellm", + "lite-key", + "lite-model", + { apiProvider: "litellm", litellmApiKey: "lite-key", litellmModelId: "lite-model" }, + ], + [ + "huggingface", + "hf-key", + "hf-model", + { apiProvider: "huggingface", huggingFaceApiKey: "hf-key", huggingFaceModelId: "hf-model" }, + ], + ["chutes", "ch-key", "ch-model", { apiProvider: "chutes", chutesApiKey: "ch-key", apiModelId: "ch-model" }], + [ + "featherless", + "fl-key", + "fl-model", + { apiProvider: "featherless", featherlessApiKey: "fl-key", apiModelId: "fl-model" }, + ], + [ + "unbound", + "ub-key", + "ub-model", + { apiProvider: "unbound", unboundApiKey: "ub-key", unboundModelId: "ub-model" }, + ], + [ + "requesty", + "req-key", + "req-model", + { apiProvider: "requesty", requestyApiKey: "req-key", requestyModelId: "req-model" }, + ], + [ + "deepinfra", + "di-key", + "di-model", + { apiProvider: "deepinfra", deepInfraApiKey: "di-key", deepInfraModelId: "di-model" }, + ], + [ + "vercel-ai-gateway", + "vai-key", + "vai-model", + { + apiProvider: "vercel-ai-gateway", + vercelAiGatewayApiKey: "vai-key", + vercelAiGatewayModelId: "vai-model", + }, + ], + ["zai", "zai-key", "zai-model", { apiProvider: "zai", zaiApiKey: "zai-key", apiModelId: "zai-model" }], + [ + "baseten", + "bt-key", + "bt-model", + { apiProvider: "baseten", basetenApiKey: "bt-key", apiModelId: "bt-model" }, + ], + ["doubao", "db-key", "db-model", { apiProvider: "doubao", doubaoApiKey: "db-key", apiModelId: "db-model" }], + [ + "moonshot", + "ms-key", + "ms-model", + { apiProvider: "moonshot", moonshotApiKey: "ms-key", apiModelId: "ms-model" }, + ], + [ + "minimax", + "mm-key", + "mm-model", + { apiProvider: "minimax", minimaxApiKey: "mm-key", apiModelId: "mm-model" }, + ], + [ + "io-intelligence", + "io-key", + "io-model", + { apiProvider: "io-intelligence", ioIntelligenceApiKey: "io-key", ioIntelligenceModelId: "io-model" }, + ], + ])("should configure %s provider correctly", (provider, apiKey, model, expected) => { + const host = createTestHost({ + apiProvider: provider as ProviderName, + apiKey, + model, + }) + + const config = callPrivate>(host, "buildApiConfiguration") + + expect(config).toEqual(expected) + }) + + it("should use default provider when not specified", () => { + const host = createTestHost({ + apiKey: "test-key", + model: "test-model", + }) + + const config = callPrivate>(host, "buildApiConfiguration") + + expect(config.apiProvider).toBe("openrouter") + }) + + it("should handle missing apiKey gracefully", () => { + const host = createTestHost({ + apiProvider: "anthropic", + model: "test-model", + }) + + const config = callPrivate>(host, "buildApiConfiguration") + + expect(config.apiProvider).toBe("anthropic") + expect(config.apiKey).toBeUndefined() + expect(config.apiModelId).toBe("test-model") + }) + + it("should use default config for unknown providers", () => { + const host = createTestHost({ + apiProvider: "unknown-provider" as ProviderName, + apiKey: "test-key", + model: "test-model", + }) + + const config = callPrivate>(host, "buildApiConfiguration") + + expect(config.apiProvider).toBe("unknown-provider") + expect(config.apiKey).toBe("test-key") + expect(config.apiModelId).toBe("test-model") + }) + }) + + describe("webview provider registration", () => { + it("should register webview provider", () => { + const host = createTestHost() + const mockProvider = { resolveWebviewView: vi.fn() } + + host.registerWebviewProvider("test-view", mockProvider) + + const providers = getPrivate>(host, "webviewProviders") + expect(providers.get("test-view")).toBe(mockProvider) + }) + + it("should unregister webview provider", () => { + const host = createTestHost() + const mockProvider = { resolveWebviewView: vi.fn() } + + host.registerWebviewProvider("test-view", mockProvider) + host.unregisterWebviewProvider("test-view") + + const providers = getPrivate>(host, "webviewProviders") + expect(providers.has("test-view")).toBe(false) + }) + + it("should handle unregistering non-existent provider gracefully", () => { + const host = createTestHost() + + expect(() => { + host.unregisterWebviewProvider("non-existent") + }).not.toThrow() + }) + }) + + describe("webview ready state", () => { + describe("isInInitialSetup", () => { + it("should return true before webview is ready", () => { + const host = createTestHost() + expect(host.isInInitialSetup()).toBe(true) + }) + + it("should return false after markWebviewReady is called", () => { + const host = createTestHost() + host.markWebviewReady() + expect(host.isInInitialSetup()).toBe(false) + }) + }) + + describe("markWebviewReady", () => { + it("should set isWebviewReady to true", () => { + const host = createTestHost() + host.markWebviewReady() + expect(getPrivate(host, "isWebviewReady")).toBe(true) + }) + + it("should emit webviewReady event", () => { + const host = createTestHost() + const listener = vi.fn() + + host.on("webviewReady", listener) + host.markWebviewReady() + + expect(listener).toHaveBeenCalled() + }) + + it("should flush pending messages", () => { + const host = createTestHost() + const emitSpy = vi.spyOn(host, "emit") + + // Queue messages before ready + host.sendToExtension({ type: "test1" }) + host.sendToExtension({ type: "test2" }) + + // Mark ready (should flush) + host.markWebviewReady() + + // Check that webviewMessage events were emitted for pending messages + expect(emitSpy).toHaveBeenCalledWith("webviewMessage", { type: "test1" }) + expect(emitSpy).toHaveBeenCalledWith("webviewMessage", { type: "test2" }) + }) + }) + }) + + describe("sendToExtension", () => { + it("should queue message when webview not ready", () => { + const host = createTestHost() + const message = { type: "test" } + + host.sendToExtension(message) + + const pending = getPrivate(host, "pendingMessages") + expect(pending).toContain(message) + }) + + it("should emit webviewMessage event when webview is ready", () => { + const host = createTestHost() + const emitSpy = vi.spyOn(host, "emit") + const message = { type: "test" } + + host.markWebviewReady() + host.sendToExtension(message) + + expect(emitSpy).toHaveBeenCalledWith("webviewMessage", message) + }) + + it("should not queue message when webview is ready", () => { + const host = createTestHost() + + host.markWebviewReady() + host.sendToExtension({ type: "test" }) + + const pending = getPrivate(host, "pendingMessages") + expect(pending).toHaveLength(0) + }) + }) + + describe("handleExtensionMessage", () => { + it("should route state messages to handleStateMessage", () => { + const host = createTestHost() + const handleStateSpy = spyOnPrivate(host, "handleStateMessage") + + callPrivate(host, "handleExtensionMessage", { type: "state", state: {} }) + + expect(handleStateSpy).toHaveBeenCalled() + }) + + it("should route messageUpdated to handleMessageUpdated", () => { + const host = createTestHost() + const handleMsgUpdatedSpy = spyOnPrivate(host, "handleMessageUpdated") + + callPrivate(host, "handleExtensionMessage", { type: "messageUpdated", clineMessage: {} }) + + expect(handleMsgUpdatedSpy).toHaveBeenCalled() + }) + + it("should route action messages to handleActionMessage", () => { + const host = createTestHost() + const handleActionSpy = spyOnPrivate(host, "handleActionMessage") + + callPrivate(host, "handleExtensionMessage", { type: "action", action: "test" }) + + expect(handleActionSpy).toHaveBeenCalled() + }) + + it("should route invoke messages to handleInvokeMessage", () => { + const host = createTestHost() + const handleInvokeSpy = spyOnPrivate(host, "handleInvokeMessage") + + callPrivate(host, "handleExtensionMessage", { type: "invoke", invoke: "test" }) + + expect(handleInvokeSpy).toHaveBeenCalled() + }) + }) + + describe("handleSayMessage", () => { + let host: ExtensionHost + let outputSpy: ReturnType + let outputErrorSpy: ReturnType + + beforeEach(() => { + host = createTestHost() + // Mock process.stdout.write and process.stderr.write which are used by output() and outputError() + vi.spyOn(process.stdout, "write").mockImplementation(() => true) + vi.spyOn(process.stderr, "write").mockImplementation(() => true) + // Spy on the output methods + outputSpy = spyOnPrivate(host, "output") + outputErrorSpy = spyOnPrivate(host, "outputError") + }) + + afterEach(() => { + vi.restoreAllMocks() + }) + + it("should emit taskComplete for completion_result", () => { + const emitSpy = vi.spyOn(host, "emit") + + callPrivate(host, "handleSayMessage", 123, "completion_result", "Task done", false) + + expect(emitSpy).toHaveBeenCalledWith("taskComplete") + expect(outputSpy).toHaveBeenCalledWith("\n[task complete]", "Task done") + }) + + it("should output error messages without emitting taskError", () => { + const emitSpy = vi.spyOn(host, "emit") + + callPrivate(host, "handleSayMessage", 123, "error", "Something went wrong", false) + + // Errors are informational - they don't terminate the task + // The agent should decide what to do next + expect(emitSpy).not.toHaveBeenCalledWith("taskError", "Something went wrong") + expect(outputErrorSpy).toHaveBeenCalledWith("\n[error]", "Something went wrong") + }) + + it("should handle command_output messages", () => { + // Mock writeStream since command_output now uses it directly + const writeStreamSpy = spyOnPrivate(host, "writeStream") + + callPrivate(host, "handleSayMessage", 123, "command_output", "output text", false) + + // command_output now uses writeStream to bypass quiet mode + expect(writeStreamSpy).toHaveBeenCalledWith("\n[command output] ") + expect(writeStreamSpy).toHaveBeenCalledWith("output text") + expect(writeStreamSpy).toHaveBeenCalledWith("\n") + }) + + it("should handle tool messages", () => { + callPrivate(host, "handleSayMessage", 123, "tool", "tool usage", false) + + expect(outputSpy).toHaveBeenCalledWith("\n[tool]", "tool usage") + }) + + it("should skip already displayed complete messages", () => { + // First display + callPrivate(host, "handleSayMessage", 123, "completion_result", "Task done", false) + outputSpy.mockClear() + + // Second display should be skipped + callPrivate(host, "handleSayMessage", 123, "completion_result", "Task done", false) + + expect(outputSpy).not.toHaveBeenCalled() + }) + + it("should not output completion_result for partial messages", () => { + const emitSpy = vi.spyOn(host, "emit") + + // Partial message should not trigger output or taskComplete + callPrivate(host, "handleSayMessage", 123, "completion_result", "", true) + + expect(outputSpy).not.toHaveBeenCalled() + expect(emitSpy).not.toHaveBeenCalledWith("taskComplete") + }) + + it("should output completion_result text when complete message arrives after partial", () => { + const emitSpy = vi.spyOn(host, "emit") + + // First, a partial message with empty text (simulates streaming) + callPrivate(host, "handleSayMessage", 123, "completion_result", "", true) + outputSpy.mockClear() + emitSpy.mockClear() + + // Then, the complete message with the actual completion text + callPrivate(host, "handleSayMessage", 123, "completion_result", "Task completed successfully!", false) + + expect(outputSpy).toHaveBeenCalledWith("\n[task complete]", "Task completed successfully!") + expect(emitSpy).toHaveBeenCalledWith("taskComplete") + }) + + it("should track displayed messages", () => { + callPrivate(host, "handleSayMessage", 123, "tool", "test", false) + + const displayed = getPrivate>(host, "displayedMessages") + expect(displayed.has(123)).toBe(true) + }) + }) + + describe("handleAskMessage", () => { + let host: ExtensionHost + let outputSpy: ReturnType + + beforeEach(() => { + // Use nonInteractive mode for display-only behavior tests + host = createTestHost({ nonInteractive: true }) + // Mock process.stdout.write which is used by output() + vi.spyOn(process.stdout, "write").mockImplementation(() => true) + outputSpy = spyOnPrivate(host, "output") + }) + + afterEach(() => { + vi.restoreAllMocks() + }) + + it("should handle command type in non-interactive mode", () => { + callPrivate(host, "handleAskMessage", 123, "command", "ls -la", false) + + expect(outputSpy).toHaveBeenCalledWith("\n[command]", "ls -la") + }) + + it("should handle tool type with JSON parsing in non-interactive mode", () => { + const toolInfo = JSON.stringify({ tool: "write_file", path: "/test/file.txt" }) + + callPrivate(host, "handleAskMessage", 123, "tool", toolInfo, false) + + expect(outputSpy).toHaveBeenCalledWith("\n[tool] write_file") + expect(outputSpy).toHaveBeenCalledWith(" path: /test/file.txt") + }) + + it("should handle tool type with content preview in non-interactive mode", () => { + const toolInfo = JSON.stringify({ + tool: "write_file", + content: "This is the content that will be written to the file. It might be long.", + }) + + callPrivate(host, "handleAskMessage", 123, "tool", toolInfo, false) + + // Content is now shown (all tool parameters are displayed) + expect(outputSpy).toHaveBeenCalledWith("\n[tool] write_file") + expect(outputSpy).toHaveBeenCalledWith( + " content: This is the content that will be written to the file. It might be long.", + ) + }) + + it("should handle tool type with invalid JSON in non-interactive mode", () => { + callPrivate(host, "handleAskMessage", 123, "tool", "not json", false) + + expect(outputSpy).toHaveBeenCalledWith("\n[tool]", "not json") + }) + + it("should not display duplicate messages for same ts", () => { + const toolInfo = JSON.stringify({ tool: "read_file" }) + + // First call + callPrivate(host, "handleAskMessage", 123, "tool", toolInfo, false) + outputSpy.mockClear() + + // Same ts - should be duplicate (already displayed) + callPrivate(host, "handleAskMessage", 123, "tool", toolInfo, false) + + // Should not log again + expect(outputSpy).not.toHaveBeenCalled() + }) + + it("should handle other ask types in non-interactive mode", () => { + callPrivate(host, "handleAskMessage", 123, "question", "What is your name?", false) + + expect(outputSpy).toHaveBeenCalledWith("\n[question]", "What is your name?") + }) + + it("should skip partial messages", () => { + callPrivate(host, "handleAskMessage", 123, "command", "ls -la", true) + + // Partial messages should be skipped + expect(outputSpy).not.toHaveBeenCalled() + }) + }) + + describe("handleAskMessage - interactive mode", () => { + let host: ExtensionHost + let outputSpy: ReturnType + + beforeEach(() => { + // Default interactive mode + host = createTestHost({ nonInteractive: false }) + // Mock process.stdout.write which is used by output() + vi.spyOn(process.stdout, "write").mockImplementation(() => true) + outputSpy = spyOnPrivate(host, "output") + // Mock readline to prevent actual prompting + vi.spyOn(process.stdin, "on").mockImplementation(() => process.stdin) + }) + + afterEach(() => { + vi.restoreAllMocks() + }) + + it("should mark ask as pending in interactive mode", () => { + // This will try to prompt, but we're testing the pendingAsks tracking + callPrivate(host, "handleAskMessage", 123, "command", "ls -la", false) + + const pendingAsks = getPrivate>(host, "pendingAsks") + expect(pendingAsks.has(123)).toBe(true) + }) + + it("should skip already pending asks", () => { + // First call - marks as pending + callPrivate(host, "handleAskMessage", 123, "command", "ls -la", false) + const callCount1 = outputSpy.mock.calls.length + + // Second call - should skip + callPrivate(host, "handleAskMessage", 123, "command", "ls -la", false) + const callCount2 = outputSpy.mock.calls.length + + // Should not have logged again + expect(callCount2).toBe(callCount1) + }) + }) + + describe("handleFollowupQuestion", () => { + let host: ExtensionHost + let outputSpy: ReturnType + + beforeEach(() => { + host = createTestHost({ nonInteractive: false }) + // Mock process.stdout.write which is used by output() + vi.spyOn(process.stdout, "write").mockImplementation(() => true) + outputSpy = spyOnPrivate(host, "output") + // Mock readline to prevent actual prompting + vi.spyOn(process.stdin, "on").mockImplementation(() => process.stdin) + }) + + afterEach(() => { + vi.restoreAllMocks() + }) + + it("should parse followup question JSON with suggestion objects containing answer and mode", async () => { + // This is the format from AskFollowupQuestionTool + // { question: "...", suggest: [{ answer: "text", mode: "code" }, ...] } + const text = JSON.stringify({ + question: "What would you like to do?", + suggest: [ + { answer: "Write code", mode: "code" }, + { answer: "Debug issue", mode: "debug" }, + { answer: "Just explain", mode: null }, + ], + }) + + // Call the handler (it will try to prompt but we just want to test parsing) + callPrivate(host, "handleFollowupQuestion", 123, text) + + // Should display the question + expect(outputSpy).toHaveBeenCalledWith("\n[question]", "What would you like to do?") + + // Should display suggestions with answer text and mode hints + expect(outputSpy).toHaveBeenCalledWith("\nSuggested answers:") + expect(outputSpy).toHaveBeenCalledWith(" 1. Write code (mode: code)") + expect(outputSpy).toHaveBeenCalledWith(" 2. Debug issue (mode: debug)") + expect(outputSpy).toHaveBeenCalledWith(" 3. Just explain") + }) + + it("should handle followup question with suggestions that have no mode", async () => { + const text = JSON.stringify({ + question: "What path?", + suggest: [{ answer: "./src/file.ts" }, { answer: "./lib/other.ts" }], + }) + + callPrivate(host, "handleFollowupQuestion", 123, text) + + expect(outputSpy).toHaveBeenCalledWith("\n[question]", "What path?") + expect(outputSpy).toHaveBeenCalledWith(" 1. ./src/file.ts") + expect(outputSpy).toHaveBeenCalledWith(" 2. ./lib/other.ts") + }) + + it("should handle plain text (non-JSON) as the question", async () => { + callPrivate(host, "handleFollowupQuestion", 123, "What is your name?") + + expect(outputSpy).toHaveBeenCalledWith("\n[question]", "What is your name?") + }) + + it("should handle empty suggestions array", async () => { + const text = JSON.stringify({ + question: "Tell me more", + suggest: [], + }) + + callPrivate(host, "handleFollowupQuestion", 123, text) + + expect(outputSpy).toHaveBeenCalledWith("\n[question]", "Tell me more") + // Should not show "Suggested answers:" if array is empty + expect(outputSpy).not.toHaveBeenCalledWith("\nSuggested answers:") + }) + }) + + describe("handleFollowupQuestionWithTimeout", () => { + let host: ExtensionHost + let outputSpy: ReturnType + const originalIsTTY = process.stdin.isTTY + + beforeEach(() => { + // Non-interactive mode uses the timeout variant + host = createTestHost({ nonInteractive: true }) + // Mock process.stdout.write which is used by output() + vi.spyOn(process.stdout, "write").mockImplementation(() => true) + outputSpy = spyOnPrivate(host, "output") + // Mock stdin - set isTTY to false so setRawMode is not called + Object.defineProperty(process.stdin, "isTTY", { value: false, writable: true }) + vi.spyOn(process.stdin, "on").mockImplementation(() => process.stdin) + vi.spyOn(process.stdin, "resume").mockImplementation(() => process.stdin) + vi.spyOn(process.stdin, "pause").mockImplementation(() => process.stdin) + vi.spyOn(process.stdin, "removeListener").mockImplementation(() => process.stdin) + }) + + afterEach(() => { + vi.restoreAllMocks() + Object.defineProperty(process.stdin, "isTTY", { value: originalIsTTY, writable: true }) + }) + + it("should parse followup question JSON and display question with suggestions", () => { + const text = JSON.stringify({ + question: "What would you like to do?", + suggest: [ + { answer: "Option A", mode: "code" }, + { answer: "Option B", mode: null }, + ], + }) + + // Call the handler - it will display the question and start the timeout + callPrivate(host, "handleFollowupQuestionWithTimeout", 123, text) + + // Should display the question + expect(outputSpy).toHaveBeenCalledWith("\n[question]", "What would you like to do?") + + // Should display suggestions + expect(outputSpy).toHaveBeenCalledWith("\nSuggested answers:") + expect(outputSpy).toHaveBeenCalledWith(" 1. Option A (mode: code)") + expect(outputSpy).toHaveBeenCalledWith(" 2. Option B") + }) + + it("should handle non-JSON text as plain question", () => { + callPrivate(host, "handleFollowupQuestionWithTimeout", 123, "Plain question text") + + expect(outputSpy).toHaveBeenCalledWith("\n[question]", "Plain question text") + }) + + it("should include auto-select hint in prompt when suggestions exist", () => { + const stdoutWriteSpy = vi.spyOn(process.stdout, "write") + const text = JSON.stringify({ + question: "Choose one", + suggest: [{ answer: "First option" }], + }) + + callPrivate(host, "handleFollowupQuestionWithTimeout", 123, text) + + // Should show prompt with timeout hint + expect(stdoutWriteSpy).toHaveBeenCalledWith(expect.stringContaining("auto-select in 10s")) + }) + }) + + describe("handleAskMessageNonInteractive - followup handling", () => { + let host: ExtensionHost + let _outputSpy: ReturnType + let handleFollowupTimeoutSpy: ReturnType + const originalIsTTY = process.stdin.isTTY + + beforeEach(() => { + host = createTestHost({ nonInteractive: true }) + vi.spyOn(process.stdout, "write").mockImplementation(() => true) + _outputSpy = spyOnPrivate(host, "output") + handleFollowupTimeoutSpy = spyOnPrivate(host, "handleFollowupQuestionWithTimeout") + // Mock stdin - set isTTY to false so setRawMode is not called + Object.defineProperty(process.stdin, "isTTY", { value: false, writable: true }) + vi.spyOn(process.stdin, "on").mockImplementation(() => process.stdin) + vi.spyOn(process.stdin, "resume").mockImplementation(() => process.stdin) + vi.spyOn(process.stdin, "pause").mockImplementation(() => process.stdin) + vi.spyOn(process.stdin, "removeListener").mockImplementation(() => process.stdin) + }) + + afterEach(() => { + vi.restoreAllMocks() + Object.defineProperty(process.stdin, "isTTY", { value: originalIsTTY, writable: true }) + }) + + it("should call handleFollowupQuestionWithTimeout for followup asks in non-interactive mode", () => { + const text = JSON.stringify({ + question: "What to do?", + suggest: [{ answer: "Do something" }], + }) + + callPrivate(host, "handleAskMessageNonInteractive", 123, "followup", text) + + expect(handleFollowupTimeoutSpy).toHaveBeenCalledWith(123, text) + }) + + it("should add ts to pendingAsks for followup in non-interactive mode", () => { + const text = JSON.stringify({ + question: "What to do?", + suggest: [{ answer: "Do something" }], + }) + + callPrivate(host, "handleAskMessageNonInteractive", 123, "followup", text) + + const pendingAsks = getPrivate>(host, "pendingAsks") + expect(pendingAsks.has(123)).toBe(true) + }) + }) + + describe("streamContent", () => { + let host: ExtensionHost + let writeStreamSpy: ReturnType + + beforeEach(() => { + host = createTestHost() + // Mock process.stdout.write + vi.spyOn(process.stdout, "write").mockImplementation(() => true) + writeStreamSpy = spyOnPrivate(host, "writeStream") + }) + + afterEach(() => { + vi.restoreAllMocks() + }) + + it("should output header and text for new messages", () => { + callPrivate(host, "streamContent", 123, "Hello", "[Test]") + + expect(writeStreamSpy).toHaveBeenCalledWith("\n[Test] ") + expect(writeStreamSpy).toHaveBeenCalledWith("Hello") + }) + + it("should compute delta for growing text", () => { + // First call - establishes baseline + callPrivate(host, "streamContent", 123, "Hello", "[Test]") + writeStreamSpy.mockClear() + + // Second call - should only output delta + callPrivate(host, "streamContent", 123, "Hello World", "[Test]") + + expect(writeStreamSpy).toHaveBeenCalledWith(" World") + }) + + it("should skip when text has not grown", () => { + callPrivate(host, "streamContent", 123, "Hello", "[Test]") + writeStreamSpy.mockClear() + + callPrivate(host, "streamContent", 123, "Hello", "[Test]") + + expect(writeStreamSpy).not.toHaveBeenCalled() + }) + + it("should skip when text does not match prefix", () => { + callPrivate(host, "streamContent", 123, "Hello", "[Test]") + writeStreamSpy.mockClear() + + // Different text entirely + callPrivate(host, "streamContent", 123, "Goodbye", "[Test]") + + expect(writeStreamSpy).not.toHaveBeenCalled() + }) + + it("should track currently streaming ts", () => { + callPrivate(host, "streamContent", 123, "Hello", "[Test]") + + expect(getPrivate(host, "currentlyStreamingTs")).toBe(123) + }) + }) + + describe("finishStream", () => { + let host: ExtensionHost + let writeStreamSpy: ReturnType + + beforeEach(() => { + host = createTestHost() + vi.spyOn(process.stdout, "write").mockImplementation(() => true) + writeStreamSpy = spyOnPrivate(host, "writeStream") + }) + + afterEach(() => { + vi.restoreAllMocks() + }) + + it("should add newline when finishing current stream", () => { + // Set up streaming state + callPrivate(host, "streamContent", 123, "Hello", "[Test]") + writeStreamSpy.mockClear() + + callPrivate(host, "finishStream", 123) + + expect(writeStreamSpy).toHaveBeenCalledWith("\n") + expect(getPrivate(host, "currentlyStreamingTs")).toBeNull() + }) + + it("should not add newline for different ts", () => { + callPrivate(host, "streamContent", 123, "Hello", "[Test]") + writeStreamSpy.mockClear() + + callPrivate(host, "finishStream", 456) + + expect(writeStreamSpy).not.toHaveBeenCalled() + }) + }) + + describe("quiet mode", () => { + describe("setupQuietMode", () => { + it("should not modify console when quiet mode disabled", () => { + const host = createTestHost({ quiet: false }) + const originalLog = console.log + + callPrivate(host, "setupQuietMode") + + expect(console.log).toBe(originalLog) + }) + + it("should suppress console.log, warn, debug, info when enabled", () => { + const host = createTestHost({ quiet: true }) + const originalLog = console.log + + callPrivate(host, "setupQuietMode") + + // These should be no-ops now (different from original) + expect(console.log).not.toBe(originalLog) + + // Verify they are actually no-ops by calling them (should not throw) + expect(() => console.log("test")).not.toThrow() + expect(() => console.warn("test")).not.toThrow() + expect(() => console.debug("test")).not.toThrow() + expect(() => console.info("test")).not.toThrow() + + // Restore for other tests + callPrivate(host, "restoreConsole") + }) + + it("should preserve console.error", () => { + const host = createTestHost({ quiet: true }) + const originalError = console.error + + callPrivate(host, "setupQuietMode") + + expect(console.error).toBe(originalError) + + callPrivate(host, "restoreConsole") + }) + + it("should store original console methods", () => { + const host = createTestHost({ quiet: true }) + const originalLog = console.log + + callPrivate(host, "setupQuietMode") + + const stored = getPrivate<{ log: typeof console.log }>(host, "originalConsole") + expect(stored.log).toBe(originalLog) + + callPrivate(host, "restoreConsole") + }) + }) + + describe("restoreConsole", () => { + it("should restore original console methods", () => { + const host = createTestHost({ quiet: true }) + const originalLog = console.log + + callPrivate(host, "setupQuietMode") + callPrivate(host, "restoreConsole") + + expect(console.log).toBe(originalLog) + }) + + it("should handle case where console was not suppressed", () => { + const host = createTestHost({ quiet: false }) + + expect(() => { + callPrivate(host, "restoreConsole") + }).not.toThrow() + }) + }) + + describe("suppressNodeWarnings", () => { + it("should suppress process.emitWarning", () => { + const host = createTestHost() + const originalEmitWarning = process.emitWarning + + callPrivate(host, "suppressNodeWarnings") + + expect(process.emitWarning).not.toBe(originalEmitWarning) + + // Restore + callPrivate(host, "restoreConsole") + }) + }) + }) + + describe("dispose", () => { + let host: ExtensionHost + + beforeEach(() => { + host = createTestHost() + }) + + it("should remove message listener", async () => { + const listener = vi.fn() + ;(host as unknown as Record).messageListener = listener + host.on("extensionWebviewMessage", listener) + + await host.dispose() + + expect(getPrivate(host, "messageListener")).toBeNull() + }) + + it("should call extension deactivate if available", async () => { + const deactivateMock = vi.fn() + ;(host as unknown as Record).extensionModule = { + deactivate: deactivateMock, + } + + await host.dispose() + + expect(deactivateMock).toHaveBeenCalled() + }) + + it("should clear vscode reference", async () => { + ;(host as unknown as Record).vscode = { context: {} } + + await host.dispose() + + expect(getPrivate(host, "vscode")).toBeNull() + }) + + it("should clear extensionModule reference", async () => { + ;(host as unknown as Record).extensionModule = {} + + await host.dispose() + + expect(getPrivate(host, "extensionModule")).toBeNull() + }) + + it("should clear webviewProviders", async () => { + host.registerWebviewProvider("test", {}) + + await host.dispose() + + const providers = getPrivate>(host, "webviewProviders") + expect(providers.size).toBe(0) + }) + + it("should delete global vscode", async () => { + ;(global as Record).vscode = {} + + await host.dispose() + + expect((global as Record).vscode).toBeUndefined() + }) + + it("should delete global __extensionHost", async () => { + ;(global as Record).__extensionHost = {} + + await host.dispose() + + expect((global as Record).__extensionHost).toBeUndefined() + }) + + it("should restore console if it was suppressed", async () => { + const restoreConsoleSpy = spyOnPrivate(host, "restoreConsole") + + await host.dispose() + + expect(restoreConsoleSpy).toHaveBeenCalled() + }) + }) + + describe("waitForCompletion", () => { + it("should resolve when taskComplete is emitted", async () => { + const host = createTestHost() + + const promise = callPrivate>(host, "waitForCompletion") + + // Emit completion after a short delay + setTimeout(() => host.emit("taskComplete"), 10) + + await expect(promise).resolves.toBeUndefined() + }) + + it("should reject when taskError is emitted", async () => { + const host = createTestHost() + + const promise = callPrivate>(host, "waitForCompletion") + + setTimeout(() => host.emit("taskError", "Test error"), 10) + + await expect(promise).rejects.toThrow("Test error") + }) + + it("should timeout after configured duration", async () => { + const host = createTestHost() + + // Use fake timers for this test + vi.useFakeTimers() + + const promise = callPrivate>(host, "waitForCompletion") + + // Fast-forward past the timeout (10 minutes) + vi.advanceTimersByTime(10 * 60 * 1000 + 1) + + await expect(promise).rejects.toThrow("Task timed out") + + vi.useRealTimers() + }) + }) +}) diff --git a/apps/cli/src/__tests__/integration.test.ts b/apps/cli/src/__tests__/integration.test.ts new file mode 100644 index 0000000000..158438decb --- /dev/null +++ b/apps/cli/src/__tests__/integration.test.ts @@ -0,0 +1,144 @@ +/** + * Integration tests for CLI + * + * These tests require a valid OPENROUTER_API_KEY environment variable. + * They will be skipped if the API key is not available. + * + * Run with: OPENROUTER_API_KEY=sk-or-v1-... pnpm test + */ + +import { ExtensionHost } from "../extension-host.js" +import path from "path" +import fs from "fs" +import os from "os" +import { fileURLToPath } from "url" + +const __filename = fileURLToPath(import.meta.url) +const __dirname = path.dirname(__filename) + +const OPENROUTER_API_KEY = process.env.OPENROUTER_API_KEY +const hasApiKey = !!OPENROUTER_API_KEY + +// Find the extension path - we need a built extension for integration tests +function findExtensionPath(): string | null { + // From apps/cli/src/__tests__, go up to monorepo root then to src/dist + const monorepoPath = path.resolve(__dirname, "../../../../src/dist") + if (fs.existsSync(path.join(monorepoPath, "extension.js"))) { + return monorepoPath + } + // Also try from the apps/cli level + const altPath = path.resolve(__dirname, "../../../src/dist") + if (fs.existsSync(path.join(altPath, "extension.js"))) { + return altPath + } + return null +} + +const extensionPath = findExtensionPath() +const hasExtension = !!extensionPath + +// Create a temporary workspace directory for tests +function createTempWorkspace(): string { + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "roo-cli-test-")) + return tempDir +} + +// Clean up temporary workspace +function cleanupWorkspace(workspacePath: string): void { + try { + fs.rmSync(workspacePath, { recursive: true, force: true }) + } catch { + // Ignore cleanup errors + } +} + +describe.skipIf(!hasApiKey || !hasExtension)( + "CLI Integration Tests (requires OPENROUTER_API_KEY and built extension)", + () => { + let workspacePath: string + let host: ExtensionHost + + beforeAll(() => { + console.log("Integration tests running with:") + console.log(` - API Key: ${OPENROUTER_API_KEY?.substring(0, 12)}...`) + console.log(` - Extension Path: ${extensionPath}`) + }) + + beforeEach(() => { + workspacePath = createTempWorkspace() + }) + + afterEach(async () => { + if (host) { + await host.dispose() + } + cleanupWorkspace(workspacePath) + }) + + /** + * Main integration test - tests the complete end-to-end flow + * + * NOTE: Due to the extension using singletons (TelemetryService, etc.), + * only one integration test can run per process. This single test covers + * the main functionality: activation, task execution, completion, and disposal. + */ + it("should complete end-to-end task execution with proper lifecycle", async () => { + host = new ExtensionHost({ + mode: "code", + apiProvider: "openrouter", + apiKey: OPENROUTER_API_KEY!, + model: "anthropic/claude-haiku-4.5", // Use fast, cheap model for tests. + workspacePath, + extensionPath: extensionPath!, + quiet: true, + }) + + // Test activation + await host.activate() + + // Track state messages + const stateMessages: unknown[] = [] + host.on("extensionWebviewMessage", (msg: Record) => { + if (msg.type === "state") { + stateMessages.push(msg) + } + }) + + // Test task execution with completion + // Note: runTask internally waits for webview to be ready before sending messages + await expect(host.runTask("Say hello in exactly 5 words")).resolves.toBeUndefined() + + // After task completes, webview should have been ready + expect(host.isInInitialSetup()).toBe(false) + + // Verify we received state updates + expect(stateMessages.length).toBeGreaterThan(0) + + // Test disposal + await host.dispose() + expect((global as Record).vscode).toBeUndefined() + expect((global as Record).__extensionHost).toBeUndefined() + }, 120000) // 2 minute timeout + }, +) + +// Additional test to verify skip behavior +describe("Integration test skip behavior", () => { + it("should have OPENROUTER_API_KEY check", () => { + if (hasApiKey) { + console.log("OPENROUTER_API_KEY is set, integration tests will run") + } else { + console.log("OPENROUTER_API_KEY is not set, integration tests will be skipped") + } + expect(true).toBe(true) // Always passes + }) + + it("should have extension check", () => { + if (hasExtension) { + console.log(`Extension found at: ${extensionPath}`) + } else { + console.log("Extension not found, integration tests will be skipped") + } + expect(true).toBe(true) // Always passes + }) +}) diff --git a/apps/cli/src/__tests__/utils.test.ts b/apps/cli/src/__tests__/utils.test.ts new file mode 100644 index 0000000000..34ce825463 --- /dev/null +++ b/apps/cli/src/__tests__/utils.test.ts @@ -0,0 +1,119 @@ +/** + * Unit tests for CLI utility functions + */ + +import { getEnvVarName, getApiKeyFromEnv, getDefaultExtensionPath } from "../utils.js" +import fs from "fs" +import path from "path" + +// Mock fs module +vi.mock("fs") + +describe("getEnvVarName", () => { + it.each([ + ["anthropic", "ANTHROPIC_API_KEY"], + ["openai", "OPENAI_API_KEY"], + ["openrouter", "OPENROUTER_API_KEY"], + ["google", "GOOGLE_API_KEY"], + ["gemini", "GOOGLE_API_KEY"], + ["bedrock", "AWS_ACCESS_KEY_ID"], + ["ollama", "OLLAMA_API_KEY"], + ["mistral", "MISTRAL_API_KEY"], + ["deepseek", "DEEPSEEK_API_KEY"], + ])("should return %s for %s provider", (provider, expectedEnvVar) => { + expect(getEnvVarName(provider)).toBe(expectedEnvVar) + }) + + it("should handle case-insensitive provider names", () => { + expect(getEnvVarName("ANTHROPIC")).toBe("ANTHROPIC_API_KEY") + expect(getEnvVarName("Anthropic")).toBe("ANTHROPIC_API_KEY") + expect(getEnvVarName("OpenRouter")).toBe("OPENROUTER_API_KEY") + }) + + it("should return uppercase provider name with _API_KEY suffix for unknown providers", () => { + expect(getEnvVarName("custom")).toBe("CUSTOM_API_KEY") + expect(getEnvVarName("myProvider")).toBe("MYPROVIDER_API_KEY") + }) +}) + +describe("getApiKeyFromEnv", () => { + const originalEnv = process.env + + beforeEach(() => { + // Reset process.env before each test + process.env = { ...originalEnv } + }) + + afterEach(() => { + process.env = originalEnv + }) + + it("should return API key from environment variable for anthropic", () => { + process.env.ANTHROPIC_API_KEY = "test-anthropic-key" + expect(getApiKeyFromEnv("anthropic")).toBe("test-anthropic-key") + }) + + it("should return API key from environment variable for openrouter", () => { + process.env.OPENROUTER_API_KEY = "test-openrouter-key" + expect(getApiKeyFromEnv("openrouter")).toBe("test-openrouter-key") + }) + + it("should return API key from environment variable for openai", () => { + process.env.OPENAI_API_KEY = "test-openai-key" + expect(getApiKeyFromEnv("openai")).toBe("test-openai-key") + }) + + it("should return undefined when API key is not set", () => { + delete process.env.ANTHROPIC_API_KEY + expect(getApiKeyFromEnv("anthropic")).toBeUndefined() + }) + + it("should handle custom provider names", () => { + process.env.CUSTOM_API_KEY = "test-custom-key" + expect(getApiKeyFromEnv("custom")).toBe("test-custom-key") + }) + + it("should handle case-insensitive provider lookup", () => { + process.env.ANTHROPIC_API_KEY = "test-key" + expect(getApiKeyFromEnv("ANTHROPIC")).toBe("test-key") + }) +}) + +describe("getDefaultExtensionPath", () => { + beforeEach(() => { + vi.resetAllMocks() + }) + + it("should return monorepo path when extension.js exists there", () => { + const mockDirname = "/test/apps/cli/dist" + const expectedMonorepoPath = path.resolve(mockDirname, "../../../src/dist") + + vi.mocked(fs.existsSync).mockReturnValue(true) + + const result = getDefaultExtensionPath(mockDirname) + + expect(result).toBe(expectedMonorepoPath) + expect(fs.existsSync).toHaveBeenCalledWith(path.join(expectedMonorepoPath, "extension.js")) + }) + + it("should return package path when extension.js does not exist in monorepo path", () => { + const mockDirname = "/test/apps/cli/dist" + const expectedPackagePath = path.resolve(mockDirname, "../extension") + + vi.mocked(fs.existsSync).mockReturnValue(false) + + const result = getDefaultExtensionPath(mockDirname) + + expect(result).toBe(expectedPackagePath) + }) + + it("should check monorepo path first", () => { + const mockDirname = "/some/path" + vi.mocked(fs.existsSync).mockReturnValue(false) + + getDefaultExtensionPath(mockDirname) + + const expectedMonorepoPath = path.resolve(mockDirname, "../../../src/dist") + expect(fs.existsSync).toHaveBeenCalledWith(path.join(expectedMonorepoPath, "extension.js")) + }) +}) diff --git a/apps/cli/src/extension-host.ts b/apps/cli/src/extension-host.ts new file mode 100644 index 0000000000..3396386924 --- /dev/null +++ b/apps/cli/src/extension-host.ts @@ -0,0 +1,1663 @@ +/** + * ExtensionHost - Loads and runs the Roo Code extension in CLI mode + * + * This class is responsible for: + * 1. Creating the vscode-shim mock + * 2. Loading the extension bundle via require() + * 3. Activating the extension + * 4. Managing bidirectional message flow between CLI and extension + */ + +import { EventEmitter } from "events" +import { createRequire } from "module" +import path from "path" +import { fileURLToPath } from "url" +import fs from "fs" +import readline from "readline" + +import { createVSCodeAPI, setRuntimeConfigValues } from "@roo-code/vscode-shim" +import { ProviderName, ReasoningEffortExtended, RooCodeSettings } from "@roo-code/types" + +// Get the CLI package root directory (for finding node_modules/@vscode/ripgrep) +// When bundled, import.meta.url points to dist/index.js, so go up to package root +const __dirname = path.dirname(fileURLToPath(import.meta.url)) +const CLI_PACKAGE_ROOT = path.resolve(__dirname, "..") + +export interface ExtensionHostOptions { + mode: string + reasoningEffort?: ReasoningEffortExtended | "disabled" + apiProvider: ProviderName + apiKey?: string + model: string + workspacePath: string + extensionPath: string + verbose?: boolean + quiet?: boolean + nonInteractive?: boolean +} + +interface ExtensionModule { + activate: (context: unknown) => Promise + deactivate?: () => Promise +} + +/** + * Local interface for webview provider (matches VSCode API) + */ +interface WebviewViewProvider { + resolveWebviewView?(webviewView: unknown, context: unknown, token: unknown): void | Promise +} + +export class ExtensionHost extends EventEmitter { + private vscode: ReturnType | null = null + private extensionModule: ExtensionModule | null = null + private extensionAPI: unknown = null + private webviewProviders: Map = new Map() + private options: ExtensionHostOptions + private isWebviewReady = false + private pendingMessages: unknown[] = [] + private messageListener: ((message: unknown) => void) | null = null + + private originalConsole: { + log: typeof console.log + warn: typeof console.warn + error: typeof console.error + debug: typeof console.debug + info: typeof console.info + } | null = null + + private originalProcessEmitWarning: typeof process.emitWarning | null = null + + // Track pending asks that need a response (by ts) + private pendingAsks: Set = new Set() + + // Readline interface for interactive prompts + private rl: readline.Interface | null = null + + // Track displayed messages by ts to avoid duplicates and show updates + private displayedMessages: Map = new Map() + + // Track streamed content by ts for delta computation + private streamedContent: Map = new Map() + + // Track message processing for verbose debug output + private processedMessageCount = 0 + + // Track if we're currently streaming a message (to manage newlines) + private currentlyStreamingTs: number | null = null + + constructor(options: ExtensionHostOptions) { + super() + this.options = options + } + + private log(...args: unknown[]): void { + if (this.options.verbose) { + // Use original console if available to avoid quiet mode suppression + const logFn = this.originalConsole?.log || console.log + logFn("[ExtensionHost]", ...args) + } + } + + /** + * Suppress Node.js warnings (like MaxListenersExceededWarning) + * This is called regardless of quiet mode to prevent warnings from interrupting output + */ + private suppressNodeWarnings(): void { + // Suppress process warnings (like MaxListenersExceededWarning) + this.originalProcessEmitWarning = process.emitWarning + process.emitWarning = () => {} + + // Also suppress via the warning event handler + process.on("warning", () => {}) + } + + /** + * Suppress console output from the extension when quiet mode is enabled. + * This intercepts console.log, console.warn, console.info, console.debug + * but allows console.error through for critical errors. + */ + private setupQuietMode(): void { + if (!this.options.quiet) { + return + } + + // Save original console methods + this.originalConsole = { + log: console.log, + warn: console.warn, + error: console.error, + debug: console.debug, + info: console.info, + } + + // Replace with no-op functions (except error) + console.log = () => {} + console.warn = () => {} + console.debug = () => {} + console.info = () => {} + // Keep console.error for critical errors + } + + /** + * Restore original console methods and process.emitWarning + */ + private restoreConsole(): void { + if (this.originalConsole) { + console.log = this.originalConsole.log + console.warn = this.originalConsole.warn + console.error = this.originalConsole.error + console.debug = this.originalConsole.debug + console.info = this.originalConsole.info + this.originalConsole = null + } + + if (this.originalProcessEmitWarning) { + process.emitWarning = this.originalProcessEmitWarning + this.originalProcessEmitWarning = null + } + } + + async activate(): Promise { + this.log("Activating extension...") + + // Suppress Node.js warnings (like MaxListenersExceededWarning) before anything else + this.suppressNodeWarnings() + + // Set up quiet mode before loading extension + this.setupQuietMode() + + // Verify extension path exists + const bundlePath = path.join(this.options.extensionPath, "extension.js") + if (!fs.existsSync(bundlePath)) { + this.restoreConsole() + throw new Error(`Extension bundle not found at: ${bundlePath}`) + } + + // 1. Create VSCode API mock + this.log("Creating VSCode API mock...") + this.log("Using appRoot:", CLI_PACKAGE_ROOT) + this.vscode = createVSCodeAPI( + this.options.extensionPath, + this.options.workspacePath, + undefined, // identity + { appRoot: CLI_PACKAGE_ROOT }, // options - point appRoot to CLI package for ripgrep + ) + + // 2. Set global vscode reference for the extension + ;(global as Record).vscode = this.vscode + + // 3. Set up __extensionHost global for webview registration + // This is used by WindowAPI.registerWebviewViewProvider + ;(global as Record).__extensionHost = this + + // 4. Set up module resolution to intercept require('vscode') + const require = createRequire(import.meta.url) + const Module = require("module") + const originalResolve = Module._resolveFilename + + Module._resolveFilename = function (request: string, parent: unknown, isMain: boolean, options: unknown) { + if (request === "vscode") { + return "vscode-mock" + } + return originalResolve.call(this, request, parent, isMain, options) + } + + // Add the mock to require.cache + // Use 'as unknown as' to satisfy TypeScript's Module type requirements + require.cache["vscode-mock"] = { + id: "vscode-mock", + filename: "vscode-mock", + loaded: true, + exports: this.vscode, + children: [], + paths: [], + path: "", + isPreloading: false, + parent: null, + require: require, + } as unknown as NodeJS.Module + + this.log("Loading extension bundle from:", bundlePath) + + // 5. Load extension bundle + try { + this.extensionModule = require(bundlePath) as ExtensionModule + } catch (error) { + // Restore module resolution before throwing + Module._resolveFilename = originalResolve + throw new Error( + `Failed to load extension bundle: ${error instanceof Error ? error.message : String(error)}`, + ) + } + + // 6. Restore module resolution + Module._resolveFilename = originalResolve + + this.log("Activating extension...") + + // 7. Activate extension + try { + this.extensionAPI = await this.extensionModule.activate(this.vscode.context) + this.log("Extension activated successfully") + } catch (error) { + throw new Error(`Failed to activate extension: ${error instanceof Error ? error.message : String(error)}`) + } + } + + /** + * Called by WindowAPI.registerWebviewViewProvider + * This is triggered when the extension registers its sidebar webview provider + */ + registerWebviewProvider(viewId: string, provider: WebviewViewProvider): void { + this.log(`Webview provider registered: ${viewId}`) + this.webviewProviders.set(viewId, provider) + + // The WindowAPI will call resolveWebviewView automatically + // We don't need to do anything here + } + + /** + * Called when a webview provider is disposed + */ + unregisterWebviewProvider(viewId: string): void { + this.log(`Webview provider unregistered: ${viewId}`) + this.webviewProviders.delete(viewId) + } + + /** + * Returns true during initial extension setup + * Used to prevent the extension from aborting tasks during initialization + */ + isInInitialSetup(): boolean { + return !this.isWebviewReady + } + + /** + * Called by WindowAPI after resolveWebviewView completes + * This indicates the webview is ready to receive messages + */ + markWebviewReady(): void { + this.log("Webview marked as ready") + this.isWebviewReady = true + this.emit("webviewReady") + + // Flush any pending messages + this.flushPendingMessages() + } + + /** + * Send any messages that were queued before the webview was ready + */ + private flushPendingMessages(): void { + if (this.pendingMessages.length > 0) { + this.log(`Flushing ${this.pendingMessages.length} pending messages`) + for (const message of this.pendingMessages) { + this.emit("webviewMessage", message) + } + this.pendingMessages = [] + } + } + + /** + * Send a message to the extension (simulating webview -> extension communication). + */ + sendToExtension(message: unknown): void { + if (!this.isWebviewReady) { + this.log("Queueing message (webview not ready):", message) + this.pendingMessages.push(message) + return + } + + this.log("Sending message to extension:", message) + this.emit("webviewMessage", message) + } + + private applyRuntimeSettings(settings: RooCodeSettings): void { + if (this.options.mode) { + settings.mode = this.options.mode + } + + if (this.options.reasoningEffort) { + if (this.options.reasoningEffort === "disabled") { + settings.enableReasoningEffort = false + } else { + settings.enableReasoningEffort = true + settings.reasoningEffort = this.options.reasoningEffort + } + } + + // Update vscode-shim runtime configuration so + // vscode.workspace.getConfiguration() returns correct values. + setRuntimeConfigValues("roo-cline", settings as Record) + } + + /** + * Build the provider-specific API configuration + * Each provider uses different field names for API key and model + */ + private buildApiConfiguration(): RooCodeSettings { + const provider = this.options.apiProvider || "anthropic" + const apiKey = this.options.apiKey + const model = this.options.model + + // Base config with provider. + const config: RooCodeSettings = { apiProvider: provider } + + // Map provider to the correct API key and model field names. + switch (provider) { + case "anthropic": + if (apiKey) config.apiKey = apiKey + if (model) config.apiModelId = model + break + + case "openrouter": + if (apiKey) config.openRouterApiKey = apiKey + if (model) config.openRouterModelId = model + break + + case "gemini": + if (apiKey) config.geminiApiKey = apiKey + if (model) config.apiModelId = model + break + + case "openai-native": + if (apiKey) config.openAiNativeApiKey = apiKey + if (model) config.apiModelId = model + break + + case "openai": + if (apiKey) config.openAiApiKey = apiKey + if (model) config.openAiModelId = model + break + + case "mistral": + if (apiKey) config.mistralApiKey = apiKey + if (model) config.apiModelId = model + break + + case "deepseek": + if (apiKey) config.deepSeekApiKey = apiKey + if (model) config.apiModelId = model + break + + case "xai": + if (apiKey) config.xaiApiKey = apiKey + if (model) config.apiModelId = model + break + + case "groq": + if (apiKey) config.groqApiKey = apiKey + if (model) config.apiModelId = model + break + + case "fireworks": + if (apiKey) config.fireworksApiKey = apiKey + if (model) config.apiModelId = model + break + + case "cerebras": + if (apiKey) config.cerebrasApiKey = apiKey + if (model) config.apiModelId = model + break + + case "sambanova": + if (apiKey) config.sambaNovaApiKey = apiKey + if (model) config.apiModelId = model + break + + case "ollama": + if (apiKey) config.ollamaApiKey = apiKey + if (model) config.ollamaModelId = model + break + + case "lmstudio": + if (model) config.lmStudioModelId = model + break + + case "litellm": + if (apiKey) config.litellmApiKey = apiKey + if (model) config.litellmModelId = model + break + + case "huggingface": + if (apiKey) config.huggingFaceApiKey = apiKey + if (model) config.huggingFaceModelId = model + break + + case "chutes": + if (apiKey) config.chutesApiKey = apiKey + if (model) config.apiModelId = model + break + + case "featherless": + if (apiKey) config.featherlessApiKey = apiKey + if (model) config.apiModelId = model + break + + case "unbound": + if (apiKey) config.unboundApiKey = apiKey + if (model) config.unboundModelId = model + break + + case "requesty": + if (apiKey) config.requestyApiKey = apiKey + if (model) config.requestyModelId = model + break + + case "deepinfra": + if (apiKey) config.deepInfraApiKey = apiKey + if (model) config.deepInfraModelId = model + break + + case "vercel-ai-gateway": + if (apiKey) config.vercelAiGatewayApiKey = apiKey + if (model) config.vercelAiGatewayModelId = model + break + + case "zai": + if (apiKey) config.zaiApiKey = apiKey + if (model) config.apiModelId = model + break + + case "baseten": + if (apiKey) config.basetenApiKey = apiKey + if (model) config.apiModelId = model + break + + case "doubao": + if (apiKey) config.doubaoApiKey = apiKey + if (model) config.apiModelId = model + break + + case "moonshot": + if (apiKey) config.moonshotApiKey = apiKey + if (model) config.apiModelId = model + break + + case "minimax": + if (apiKey) config.minimaxApiKey = apiKey + if (model) config.apiModelId = model + break + + case "io-intelligence": + if (apiKey) config.ioIntelligenceApiKey = apiKey + if (model) config.ioIntelligenceModelId = model + break + + default: + // Default to apiKey and apiModelId for unknown providers. + if (apiKey) config.apiKey = apiKey + if (model) config.apiModelId = model + } + + return config + } + + /** + * Run a task with the given prompt + */ + async runTask(prompt: string): Promise { + this.log("Running task:", prompt) + + // Wait for webview to be ready + if (!this.isWebviewReady) { + this.log("Waiting for webview to be ready...") + await new Promise((resolve) => { + this.once("webviewReady", resolve) + }) + } + + // Set up message listener for extension responses + this.setupMessageListener() + + // Configure approval settings based on mode + // In non-interactive mode (-y flag), enable auto-approval for everything + // In interactive mode (default), we'll prompt the user for each action + if (this.options.nonInteractive) { + this.log("Non-interactive mode: enabling auto-approval settings...") + + const settings: RooCodeSettings = { + autoApprovalEnabled: true, + alwaysAllowReadOnly: true, + alwaysAllowReadOnlyOutsideWorkspace: true, + alwaysAllowWrite: true, + alwaysAllowWriteOutsideWorkspace: true, + alwaysAllowWriteProtected: false, // Keep protected files safe. + alwaysAllowBrowser: true, + alwaysAllowMcp: true, + alwaysAllowModeSwitch: true, + alwaysAllowSubtasks: true, + alwaysAllowExecute: true, + alwaysAllowFollowupQuestions: true, + // Allow all commands with wildcard (required for command auto-approval). + allowedCommands: ["*"], + commandExecutionTimeout: 20, + } + + this.applyRuntimeSettings(settings) + this.sendToExtension({ type: "updateSettings", updatedSettings: settings }) + await new Promise((resolve) => setTimeout(resolve, 100)) + } else { + this.log("Interactive mode: user will be prompted for approvals...") + const settings: RooCodeSettings = { autoApprovalEnabled: false } + this.applyRuntimeSettings(settings) + this.sendToExtension({ type: "updateSettings", updatedSettings: settings }) + await new Promise((resolve) => setTimeout(resolve, 100)) + } + + if (this.options.apiKey) { + this.sendToExtension({ type: "updateSettings", updatedSettings: this.buildApiConfiguration() }) + await new Promise((resolve) => setTimeout(resolve, 100)) + } + + this.sendToExtension({ type: "newTask", text: prompt }) + await this.waitForCompletion() + } + + /** + * Set up listener for messages from the extension + */ + private setupMessageListener(): void { + this.messageListener = (message: unknown) => { + this.handleExtensionMessage(message) + } + + this.on("extensionWebviewMessage", this.messageListener) + } + + /** + * Handle messages from the extension + */ + private handleExtensionMessage(message: unknown): void { + const msg = message as Record + + if (this.options.verbose) { + this.log("Received message from extension:", JSON.stringify(msg, null, 2)) + } + + // Handle different message types + switch (msg.type) { + case "state": + this.handleStateMessage(msg) + break + + case "messageUpdated": + // This is the streaming update - handle individual message updates + this.handleMessageUpdated(msg) + break + + case "action": + this.handleActionMessage(msg) + break + + case "invoke": + this.handleInvokeMessage(msg) + break + + default: + // Log unknown message types in verbose mode + if (this.options.verbose) { + this.log("Unknown message type:", msg.type) + } + } + } + + /** + * Output a message to the user (bypasses quiet mode) + * Use this for all user-facing output instead of console.log + */ + private output(...args: unknown[]): void { + const text = args.map((arg) => (typeof arg === "string" ? arg : JSON.stringify(arg))).join(" ") + process.stdout.write(text + "\n") + } + + /** + * Output an error message to the user (bypasses quiet mode) + * Use this for all user-facing errors instead of console.error + */ + private outputError(...args: unknown[]): void { + const text = args.map((arg) => (typeof arg === "string" ? arg : JSON.stringify(arg))).join(" ") + process.stderr.write(text + "\n") + } + + /** + * Handle state update messages from the extension + */ + private handleStateMessage(msg: Record): void { + const state = msg.state as Record | undefined + if (!state) return + + const clineMessages = state.clineMessages as Array> | undefined + + if (clineMessages && clineMessages.length > 0) { + // Track message processing for verbose debug output + this.processedMessageCount++ + + // Verbose: log state update summary + if (this.options.verbose) { + this.log(`State update #${this.processedMessageCount}: ${clineMessages.length} messages`) + } + + // Process all messages to find new or updated ones + for (const message of clineMessages) { + if (!message) continue + + const ts = message.ts as number | undefined + const isPartial = message.partial as boolean | undefined + const text = message.text as string + const type = message.type as string + const say = message.say as string | undefined + const ask = message.ask as string | undefined + + if (!ts) continue + + // Handle "say" type messages + if (type === "say" && say) { + this.handleSayMessage(ts, say, text, isPartial) + } + // Handle "ask" type messages + else if (type === "ask" && ask) { + this.handleAskMessage(ts, ask, text, isPartial) + } + } + } + } + + /** + * Handle messageUpdated - individual streaming updates for a single message + * This is where real-time streaming happens! + */ + private handleMessageUpdated(msg: Record): void { + const clineMessage = msg.clineMessage as Record | undefined + if (!clineMessage) return + + const ts = clineMessage.ts as number | undefined + const isPartial = clineMessage.partial as boolean | undefined + const text = clineMessage.text as string + const type = clineMessage.type as string + const say = clineMessage.say as string | undefined + const ask = clineMessage.ask as string | undefined + + if (!ts) return + + // Handle "say" type messages + if (type === "say" && say) { + this.handleSayMessage(ts, say, text, isPartial) + } + // Handle "ask" type messages + else if (type === "ask" && ask) { + this.handleAskMessage(ts, ask, text, isPartial) + } + } + + /** + * Write streaming output directly to stdout (bypassing quiet mode if needed) + */ + private writeStream(text: string): void { + process.stdout.write(text) + } + + /** + * Stream content with delta computation - only output new characters + */ + private streamContent(ts: number, text: string, header: string): void { + const previous = this.streamedContent.get(ts) + + if (!previous) { + // First time seeing this message - output header and initial text + this.writeStream(`\n${header} `) + this.writeStream(text) + this.streamedContent.set(ts, { text, headerShown: true }) + this.currentlyStreamingTs = ts + } else if (text.length > previous.text.length && text.startsWith(previous.text)) { + // Text has grown - output delta + const delta = text.slice(previous.text.length) + this.writeStream(delta) + this.streamedContent.set(ts, { text, headerShown: true }) + } + } + + /** + * Finish streaming a message (add newline) + */ + private finishStream(ts: number): void { + if (this.currentlyStreamingTs === ts) { + this.writeStream("\n") + this.currentlyStreamingTs = null + } + } + + /** + * Handle "say" type messages + */ + private handleSayMessage(ts: number, say: string, text: string, isPartial: boolean | undefined): void { + const previousDisplay = this.displayedMessages.get(ts) + const alreadyDisplayedComplete = previousDisplay && !previousDisplay.partial + + switch (say) { + case "text": + // Skip the initial user prompt echo (first message with no prior messages) + if (this.displayedMessages.size === 0 && !previousDisplay) { + this.displayedMessages.set(ts, { text, partial: !!isPartial }) + break + } + + if (isPartial && text) { + // Stream partial content + this.streamContent(ts, text, "[assistant]") + this.displayedMessages.set(ts, { text, partial: true }) + } else if (!isPartial && text && !alreadyDisplayedComplete) { + // Message complete - ensure all content is output + const streamed = this.streamedContent.get(ts) + if (streamed) { + // We were streaming - output any remaining delta and finish + if (text.length > streamed.text.length && text.startsWith(streamed.text)) { + const delta = text.slice(streamed.text.length) + this.writeStream(delta) + } + this.finishStream(ts) + } else { + // Not streamed yet - output complete message + this.output("\n[assistant]", text) + } + this.displayedMessages.set(ts, { text, partial: false }) + this.streamedContent.set(ts, { text, headerShown: true }) + } + break + + case "thinking": + case "reasoning": + // Stream reasoning content in real-time. + this.log(`Received ${say} message: partial=${isPartial}, textLength=${text?.length ?? 0}`) + if (isPartial && text) { + this.streamContent(ts, text, "[reasoning]") + this.displayedMessages.set(ts, { text, partial: true }) + } else if (!isPartial && text && !alreadyDisplayedComplete) { + // Reasoning complete - finish the stream. + const streamed = this.streamedContent.get(ts) + if (streamed) { + if (text.length > streamed.text.length && text.startsWith(streamed.text)) { + const delta = text.slice(streamed.text.length) + this.writeStream(delta) + } + this.finishStream(ts) + } else { + this.output("\n[reasoning]", text) + } + this.displayedMessages.set(ts, { text, partial: false }) + } + break + + case "command_output": + // Stream command output in real-time. + if (isPartial && text) { + this.streamContent(ts, text, "[command output]") + this.displayedMessages.set(ts, { text, partial: true }) + } else if (!isPartial && text && !alreadyDisplayedComplete) { + // Command output complete - finish the stream. + const streamed = this.streamedContent.get(ts) + if (streamed) { + if (text.length > streamed.text.length && text.startsWith(streamed.text)) { + const delta = text.slice(streamed.text.length) + this.writeStream(delta) + } + this.finishStream(ts) + } else { + this.writeStream("\n[command output] ") + this.writeStream(text) + this.writeStream("\n") + } + this.displayedMessages.set(ts, { text, partial: false }) + } + break + + case "completion_result": + // Only process when message is complete (not partial) + if (!isPartial && !alreadyDisplayedComplete) { + this.output("\n[task complete]", text || "") + this.displayedMessages.set(ts, { text: text || "", partial: false }) + this.emit("taskComplete") + } else if (isPartial) { + // Track partial messages but don't output yet - wait for complete message + this.displayedMessages.set(ts, { text: text || "", partial: true }) + } + break + + case "error": + // Display errors to the user but don't terminate the task + // Errors like command timeouts are informational - the agent should decide what to do next + if (!alreadyDisplayedComplete) { + this.outputError("\n[error]", text || "Unknown error") + this.displayedMessages.set(ts, { text: text || "", partial: false }) + } + break + + case "tool": + // Tool usage - show when complete + if (text && !alreadyDisplayedComplete) { + this.output("\n[tool]", text) + this.displayedMessages.set(ts, { text, partial: false }) + } + break + + case "api_req_started": + // API request started - log in verbose mode + if (this.options.verbose) { + this.log(`API request started: ts=${ts}`) + } + break + + default: + // Other say types - show in verbose mode + if (this.options.verbose) { + this.log(`Unknown say type: ${say}, text length: ${text?.length ?? 0}, partial: ${isPartial}`) + if (text && !alreadyDisplayedComplete) { + this.output(`\n[${say}]`, text || "") + this.displayedMessages.set(ts, { text: text || "", partial: false }) + } + } + } + } + + /** + * Handle "ask" type messages - these require user responses + * In interactive mode: prompt user for input + * In non-interactive mode: auto-approve (handled by extension settings) + */ + private handleAskMessage(ts: number, ask: string, text: string, isPartial: boolean | undefined): void { + // Special handling for command_output - stream it in real-time + // This needs to happen before the isPartial skip + if (ask === "command_output") { + this.handleCommandOutputAsk(ts, text, isPartial) + return + } + + // Skip partial messages - wait for the complete ask + if (isPartial) { + return + } + + // Check if we already handled this ask + if (this.pendingAsks.has(ts)) { + return + } + + // In non-interactive mode, the extension's auto-approval settings handle everything + // We just need to display the action being taken + if (this.options.nonInteractive) { + this.handleAskMessageNonInteractive(ts, ask, text) + return + } + + // Interactive mode - prompt user for input + this.handleAskMessageInteractive(ts, ask, text) + } + + /** + * Handle ask messages in non-interactive mode + * For followup questions: show prompt with 10s timeout, auto-select first option if no input + * For everything else: auto-approval handles responses + */ + private handleAskMessageNonInteractive(ts: number, ask: string, text: string): void { + const previousDisplay = this.displayedMessages.get(ts) + const alreadyDisplayed = !!previousDisplay + + switch (ask) { + case "followup": + if (!alreadyDisplayed) { + // In non-interactive mode, still prompt the user but with a 10s timeout + // that auto-selects the first option if no input is received + this.pendingAsks.add(ts) + this.handleFollowupQuestionWithTimeout(ts, text) + this.displayedMessages.set(ts, { text, partial: false }) + } + break + + case "command": + if (!alreadyDisplayed) { + this.output("\n[command]", text || "") + this.displayedMessages.set(ts, { text: text || "", partial: false }) + } + break + + // Note: command_output is handled separately in handleCommandOutputAsk + + case "tool": + if (!alreadyDisplayed && text) { + try { + const toolInfo = JSON.parse(text) + const toolName = toolInfo.tool || "unknown" + this.output(`\n[tool] ${toolName}`) + // Display all tool parameters (excluding 'tool' which is the name) + for (const [key, value] of Object.entries(toolInfo)) { + if (key === "tool") continue + // Format the value - truncate long strings + let displayValue: string + if (typeof value === "string") { + displayValue = value.length > 200 ? value.substring(0, 200) + "..." : value + } else if (typeof value === "object" && value !== null) { + const json = JSON.stringify(value) + displayValue = json.length > 200 ? json.substring(0, 200) + "..." : json + } else { + displayValue = String(value) + } + this.output(` ${key}: ${displayValue}`) + } + } catch { + this.output("\n[tool]", text) + } + this.displayedMessages.set(ts, { text, partial: false }) + } + break + + case "browser_action_launch": + if (!alreadyDisplayed) { + this.output("\n[browser action]", text || "") + this.displayedMessages.set(ts, { text: text || "", partial: false }) + } + break + + case "use_mcp_server": + if (!alreadyDisplayed) { + try { + const mcpInfo = JSON.parse(text) + this.output(`\n[mcp] ${mcpInfo.server_name || "unknown"}`) + } catch { + this.output("\n[mcp]", text || "") + } + this.displayedMessages.set(ts, { text: text || "", partial: false }) + } + break + + case "api_req_failed": + if (!alreadyDisplayed) { + this.output("\n[retrying api Request]") + this.displayedMessages.set(ts, { text: text || "", partial: false }) + } + break + + case "resume_task": + case "resume_completed_task": + if (!alreadyDisplayed) { + this.output("\n[continuing task]") + this.displayedMessages.set(ts, { text: text || "", partial: false }) + } + break + + case "completion_result": + // Task completion - no action needed + break + + default: + if (!alreadyDisplayed && text) { + this.output(`\n[${ask}]`, text) + this.displayedMessages.set(ts, { text, partial: false }) + } + } + } + + /** + * Handle ask messages in interactive mode - prompt user for input + */ + private handleAskMessageInteractive(ts: number, ask: string, text: string): void { + // Mark this ask as pending so we don't handle it again + this.pendingAsks.add(ts) + + switch (ask) { + case "followup": + this.handleFollowupQuestion(ts, text) + break + + case "command": + this.handleCommandApproval(ts, text) + break + + // Note: command_output is handled separately in handleCommandOutputAsk + + case "tool": + this.handleToolApproval(ts, text) + break + + case "browser_action_launch": + this.handleBrowserApproval(ts, text) + break + + case "use_mcp_server": + this.handleMcpApproval(ts, text) + break + + case "api_req_failed": + this.handleApiFailedRetry(ts, text) + break + + case "resume_task": + case "resume_completed_task": + this.handleResumeTask(ts, ask, text) + break + + case "completion_result": + // Task completion - handled by say message, no response needed + this.pendingAsks.delete(ts) + break + + default: + // Unknown ask type - try to handle as yes/no + this.handleGenericApproval(ts, ask, text) + } + } + + /** + * Handle followup questions - prompt for text input with suggestions + */ + private async handleFollowupQuestion(ts: number, text: string): Promise { + let question = text + // Suggestions are objects with { answer: string, mode?: string } + let suggestions: Array<{ answer: string; mode?: string | null }> = [] + + // Parse the followup question JSON + // Format: { question: "...", suggest: [{ answer: "text", mode: "code" }, ...] } + try { + const data = JSON.parse(text) + question = data.question || text + suggestions = Array.isArray(data.suggest) ? data.suggest : [] + } catch { + // Use raw text if not JSON + } + + this.output("\n[question]", question) + + // Show numbered suggestions + if (suggestions.length > 0) { + this.output("\nSuggested answers:") + suggestions.forEach((suggestion, index) => { + const suggestionText = suggestion.answer || String(suggestion) + const modeHint = suggestion.mode ? ` (mode: ${suggestion.mode})` : "" + this.output(` ${index + 1}. ${suggestionText}${modeHint}`) + }) + this.output("") + } + + try { + const answer = await this.promptForInput( + suggestions.length > 0 + ? "Enter number (1-" + suggestions.length + ") or type your answer: " + : "Your answer: ", + ) + + let responseText = answer.trim() + + // Check if user entered a number corresponding to a suggestion + const num = parseInt(responseText, 10) + if (!isNaN(num) && num >= 1 && num <= suggestions.length) { + const selectedSuggestion = suggestions[num - 1] + if (selectedSuggestion) { + responseText = selectedSuggestion.answer || String(selectedSuggestion) + this.output(`Selected: ${responseText}`) + } + } + + this.sendFollowupResponse(responseText) + // Don't delete from pendingAsks - keep it to prevent re-processing + // if the extension sends another state update before processing our response + } catch { + // If prompt fails (e.g., stdin closed), use first suggestion answer or empty + const firstSuggestion = suggestions.length > 0 ? suggestions[0] : null + const fallback = firstSuggestion?.answer ?? "" + this.output(`[Using default: ${fallback || "(empty)"}]`) + this.sendFollowupResponse(fallback) + } + // Note: We intentionally don't delete from pendingAsks here. + // The ts stays in the set to prevent duplicate handling if the extension + // sends another state update before it processes our response. + // The set is cleared when the task completes or the host is disposed. + } + + /** + * Handle followup questions with a timeout (for non-interactive mode) + * Shows the prompt but auto-selects the first option after 10 seconds + * if the user doesn't type anything. Cancels the timeout on any keypress. + */ + private async handleFollowupQuestionWithTimeout(ts: number, text: string): Promise { + let question = text + // Suggestions are objects with { answer: string, mode?: string } + let suggestions: Array<{ answer: string; mode?: string | null }> = [] + + // Parse the followup question JSON + try { + const data = JSON.parse(text) + question = data.question || text + suggestions = Array.isArray(data.suggest) ? data.suggest : [] + } catch { + // Use raw text if not JSON + } + + this.output("\n[question]", question) + + // Show numbered suggestions + if (suggestions.length > 0) { + this.output("\nSuggested answers:") + suggestions.forEach((suggestion, index) => { + const suggestionText = suggestion.answer || String(suggestion) + const modeHint = suggestion.mode ? ` (mode: ${suggestion.mode})` : "" + this.output(` ${index + 1}. ${suggestionText}${modeHint}`) + }) + this.output("") + } + + // Default to first suggestion or empty string + const firstSuggestion = suggestions.length > 0 ? suggestions[0] : null + const defaultAnswer = firstSuggestion?.answer ?? "" + + try { + const answer = await this.promptForInputWithTimeout( + suggestions.length > 0 + ? `Enter number (1-${suggestions.length}) or type your answer (auto-select in 10s): ` + : "Your answer (auto-select in 10s): ", + 10000, // 10 second timeout + defaultAnswer, + ) + + let responseText = answer.trim() + + // Check if user entered a number corresponding to a suggestion + const num = parseInt(responseText, 10) + if (!isNaN(num) && num >= 1 && num <= suggestions.length) { + const selectedSuggestion = suggestions[num - 1] + if (selectedSuggestion) { + responseText = selectedSuggestion.answer || String(selectedSuggestion) + this.output(`Selected: ${responseText}`) + } + } + + this.sendFollowupResponse(responseText) + } catch { + // If prompt fails, use default + this.output(`[Using default: ${defaultAnswer || "(empty)"}]`) + this.sendFollowupResponse(defaultAnswer) + } + } + + /** + * Prompt user for text input with a timeout + * Returns defaultValue if timeout expires before any input + * Cancels timeout as soon as any character is typed + */ + private promptForInputWithTimeout(prompt: string, timeoutMs: number, defaultValue: string): Promise { + return new Promise((resolve) => { + // Temporarily restore console for interactive prompts + const wasQuiet = this.options.quiet + if (wasQuiet) { + this.restoreConsole() + } + + // Put stdin in raw mode to detect individual keypresses + const wasRaw = process.stdin.isRaw + if (process.stdin.isTTY) { + process.stdin.setRawMode(true) + } + process.stdin.resume() + + let inputBuffer = "" + let timeoutCancelled = false + let resolved = false + + // Set up the timeout + const timeout = setTimeout(() => { + if (!resolved) { + resolved = true + cleanup() + this.output(`\n[Timeout - using default: ${defaultValue || "(empty)"}]`) + resolve(defaultValue) + } + }, timeoutMs) + + // Show the prompt + process.stdout.write(prompt) + + // Cleanup function + const cleanup = () => { + clearTimeout(timeout) + process.stdin.removeListener("data", onData) + if (process.stdin.isTTY && wasRaw !== undefined) { + process.stdin.setRawMode(wasRaw) + } + process.stdin.pause() + if (wasQuiet) { + this.setupQuietMode() + } + } + + // Handle keypress data + const onData = (data: Buffer) => { + const char = data.toString() + + // Check for Ctrl+C + if (char === "\x03") { + cleanup() + resolved = true + this.output("\n[cancelled]") + resolve(defaultValue) + return + } + + // Cancel timeout on first character + if (!timeoutCancelled) { + timeoutCancelled = true + clearTimeout(timeout) + } + + // Handle Enter key + if (char === "\r" || char === "\n") { + if (!resolved) { + resolved = true + cleanup() + process.stdout.write("\n") + resolve(inputBuffer) + } + return + } + + // Handle Backspace + if (char === "\x7f" || char === "\b") { + if (inputBuffer.length > 0) { + inputBuffer = inputBuffer.slice(0, -1) + // Erase character on screen: move back, write space, move back + process.stdout.write("\b \b") + } + return + } + + // Regular character - add to buffer and echo + inputBuffer += char + process.stdout.write(char) + } + + process.stdin.on("data", onData) + }) + } + + /** + * Handle command execution approval + */ + private async handleCommandApproval(ts: number, text: string): Promise { + this.output("\n[command request]") + this.output(` Command: ${text || "(no command specified)"}`) + + try { + const approved = await this.promptForYesNo("Execute this command? (y/n): ") + this.sendApprovalResponse(approved) + } catch { + this.output("[Defaulting to: no]") + this.sendApprovalResponse(false) + } + // Note: Don't delete from pendingAsks - see handleFollowupQuestion comment + } + + /** + * Handle tool execution approval + */ + private async handleToolApproval(ts: number, text: string): Promise { + let toolName = "unknown" + let toolInfo: Record = {} + + try { + toolInfo = JSON.parse(text) as Record + toolName = (toolInfo.tool as string) || "unknown" + } catch { + // Use raw text if not JSON + } + + this.output(`\n[Tool Request] ${toolName}`) + // Display all tool parameters (excluding 'tool' which is the name) + for (const [key, value] of Object.entries(toolInfo)) { + if (key === "tool") continue + // Format the value - truncate long strings + let displayValue: string + if (typeof value === "string") { + displayValue = value.length > 200 ? value.substring(0, 200) + "..." : value + } else if (typeof value === "object" && value !== null) { + const json = JSON.stringify(value) + displayValue = json.length > 200 ? json.substring(0, 200) + "..." : json + } else { + displayValue = String(value) + } + this.output(` ${key}: ${displayValue}`) + } + + try { + const approved = await this.promptForYesNo("Approve this action? (y/n): ") + this.sendApprovalResponse(approved) + } catch { + this.output("[Defaulting to: no]") + this.sendApprovalResponse(false) + } + // Note: Don't delete from pendingAsks - see handleFollowupQuestion comment + } + + /** + * Handle browser action approval + */ + private async handleBrowserApproval(ts: number, text: string): Promise { + this.output("\n[browser action request]") + if (text) this.output(` Action: ${text}`) + + try { + const approved = await this.promptForYesNo("Allow browser action? (y/n): ") + this.sendApprovalResponse(approved) + } catch { + this.output("[Defaulting to: no]") + this.sendApprovalResponse(false) + } + // Note: Don't delete from pendingAsks - see handleFollowupQuestion comment + } + + /** + * Handle MCP server access approval + */ + private async handleMcpApproval(ts: number, text: string): Promise { + let serverName = "unknown" + let toolName = "" + let resourceUri = "" + + try { + const mcpInfo = JSON.parse(text) + serverName = mcpInfo.server_name || "unknown" + if (mcpInfo.type === "use_mcp_tool") { + toolName = mcpInfo.tool_name || "" + } else if (mcpInfo.type === "access_mcp_resource") { + resourceUri = mcpInfo.uri || "" + } + } catch { + // Use raw text if not JSON + } + + this.output("\n[mcp request]") + this.output(` Server: ${serverName}`) + if (toolName) this.output(` Tool: ${toolName}`) + if (resourceUri) this.output(` Resource: ${resourceUri}`) + + try { + const approved = await this.promptForYesNo("Allow MCP access? (y/n): ") + this.sendApprovalResponse(approved) + } catch { + this.output("[Defaulting to: no]") + this.sendApprovalResponse(false) + } + // Note: Don't delete from pendingAsks - see handleFollowupQuestion comment + } + + /** + * Handle API request failed - retry prompt + */ + private async handleApiFailedRetry(ts: number, text: string): Promise { + this.output("\n[api request failed]") + this.output(` Error: ${text || "Unknown error"}`) + + try { + const retry = await this.promptForYesNo("Retry the request? (y/n): ") + this.sendApprovalResponse(retry) + } catch { + this.output("[Defaulting to: no]") + this.sendApprovalResponse(false) + } + // Note: Don't delete from pendingAsks - see handleFollowupQuestion comment + } + + /** + * Handle task resume prompt + */ + private async handleResumeTask(ts: number, ask: string, text: string): Promise { + const isCompleted = ask === "resume_completed_task" + this.output(`\n[Resume ${isCompleted ? "Completed " : ""}Task]`) + if (text) this.output(` ${text}`) + + try { + const resume = await this.promptForYesNo("Continue with this task? (y/n): ") + this.sendApprovalResponse(resume) + } catch { + this.output("[Defaulting to: no]") + this.sendApprovalResponse(false) + } + // Note: Don't delete from pendingAsks - see handleFollowupQuestion comment + } + + /** + * Handle generic approval prompts for unknown ask types + */ + private async handleGenericApproval(ts: number, ask: string, text: string): Promise { + this.output(`\n[${ask}]`) + if (text) this.output(` ${text}`) + + try { + const approved = await this.promptForYesNo("Approve? (y/n): ") + this.sendApprovalResponse(approved) + } catch { + this.output("[Defaulting to: no]") + this.sendApprovalResponse(false) + } + // Note: Don't delete from pendingAsks - see handleFollowupQuestion comment + } + + /** + * Handle command_output ask messages - stream the output in real-time + * This is called for both partial (streaming) and complete messages + */ + private handleCommandOutputAsk(ts: number, text: string, isPartial: boolean | undefined): void { + const previousDisplay = this.displayedMessages.get(ts) + const alreadyDisplayedComplete = previousDisplay && !previousDisplay.partial + + // Stream partial content + if (isPartial && text) { + this.streamContent(ts, text, "[command output]") + this.displayedMessages.set(ts, { text, partial: true }) + } else if (!isPartial) { + // Message complete - output any remaining content and send approval + if (text && !alreadyDisplayedComplete) { + const streamed = this.streamedContent.get(ts) + if (streamed) { + // We were streaming - output any remaining delta and finish. + if (text.length > streamed.text.length && text.startsWith(streamed.text)) { + const delta = text.slice(streamed.text.length) + this.writeStream(delta) + } + this.finishStream(ts) + } else { + this.writeStream("\n[command output] ") + this.writeStream(text) + this.writeStream("\n") + } + this.displayedMessages.set(ts, { text, partial: false }) + this.streamedContent.set(ts, { text, headerShown: true }) + } + + // Send approval response (only once per ts). + if (!this.pendingAsks.has(ts)) { + this.pendingAsks.add(ts) + this.sendApprovalResponse(true) + } + } + } + + /** + * Prompt user for text input via readline + */ + private promptForInput(prompt: string): Promise { + return new Promise((resolve, reject) => { + // Temporarily restore console for interactive prompts + const wasQuiet = this.options.quiet + if (wasQuiet) { + this.restoreConsole() + } + + const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, + }) + + rl.question(prompt, (answer) => { + rl.close() + + // Restore quiet mode if it was enabled + if (wasQuiet) { + this.setupQuietMode() + } + + resolve(answer) + }) + + // Handle stdin close (e.g., piped input ended) + rl.on("close", () => { + if (wasQuiet) { + this.setupQuietMode() + } + }) + + // Handle errors + rl.on("error", (err) => { + rl.close() + if (wasQuiet) { + this.setupQuietMode() + } + reject(err) + }) + }) + } + + /** + * Prompt user for yes/no input + */ + private async promptForYesNo(prompt: string): Promise { + const answer = await this.promptForInput(prompt) + const normalized = answer.trim().toLowerCase() + // Accept y, yes, Y, Yes, YES, etc. + return normalized === "y" || normalized === "yes" + } + + /** + * Send a followup response (text answer) to the extension + */ + private sendFollowupResponse(text: string): void { + this.sendToExtension({ + type: "askResponse", + askResponse: "messageResponse", + text, + }) + } + + /** + * Send an approval response (yes/no) to the extension + */ + private sendApprovalResponse(approved: boolean): void { + this.sendToExtension({ + type: "askResponse", + askResponse: approved ? "yesButtonClicked" : "noButtonClicked", + }) + } + + /** + * Handle action messages + */ + private handleActionMessage(msg: Record): void { + const action = msg.action as string + + if (this.options.verbose) { + this.log("Action:", action) + } + } + + /** + * Handle invoke messages + */ + private handleInvokeMessage(msg: Record): void { + const invoke = msg.invoke as string + + if (this.options.verbose) { + this.log("Invoke:", invoke) + } + } + + /** + * Wait for the task to complete + */ + private waitForCompletion(): Promise { + return new Promise((resolve, reject) => { + const completeHandler = () => { + cleanup() + resolve() + } + + const errorHandler = (error: string) => { + cleanup() + reject(new Error(error)) + } + + const cleanup = () => { + this.off("taskComplete", completeHandler) + this.off("taskError", errorHandler) + } + + this.once("taskComplete", completeHandler) + this.once("taskError", errorHandler) + + // Set a timeout (10 minutes by default) + const timeout = setTimeout( + () => { + cleanup() + reject(new Error("Task timed out")) + }, + 10 * 60 * 1000, + ) + + // Clear timeout on completion + this.once("taskComplete", () => clearTimeout(timeout)) + this.once("taskError", () => clearTimeout(timeout)) + }) + } + + /** + * Clean up resources + */ + async dispose(): Promise { + this.log("Disposing extension host...") + + // Clear pending asks + this.pendingAsks.clear() + + // Close readline interface if open + if (this.rl) { + this.rl.close() + this.rl = null + } + + // Remove message listener + if (this.messageListener) { + this.off("extensionWebviewMessage", this.messageListener) + this.messageListener = null + } + + // Deactivate extension if it has a deactivate function + if (this.extensionModule?.deactivate) { + try { + await this.extensionModule.deactivate() + } catch (error) { + this.log("Error deactivating extension:", error) + } + } + + // Clear references + this.vscode = null + this.extensionModule = null + this.extensionAPI = null + this.webviewProviders.clear() + + // Clear globals + delete (global as Record).vscode + delete (global as Record).__extensionHost + + // Restore console if it was suppressed + this.restoreConsole() + + this.log("Extension host disposed") + } +} diff --git a/apps/cli/src/index.ts b/apps/cli/src/index.ts new file mode 100644 index 0000000000..15a2786ef4 --- /dev/null +++ b/apps/cli/src/index.ts @@ -0,0 +1,163 @@ +/** + * @roo-code/cli - Command Line Interface for Roo Code + */ + +import { Command } from "commander" +import fs from "fs" +import path from "path" +import { fileURLToPath } from "url" + +import { + type ProviderName, + type ReasoningEffortExtended, + isProviderName, + reasoningEffortsExtended, +} from "@roo-code/types" +import { setLogger } from "@roo-code/vscode-shim" + +import { ExtensionHost } from "./extension-host.js" +import { getEnvVarName, getApiKeyFromEnv, getDefaultExtensionPath } from "./utils.js" + +const DEFAULTS = { + mode: "code", + reasoningEffort: "medium" as const, + model: "anthropic/claude-sonnet-4.5", +} + +const REASONING_EFFORTS = [...reasoningEffortsExtended, "unspecified", "disabled"] + +const __dirname = path.dirname(fileURLToPath(import.meta.url)) + +const program = new Command() + +program.name("roo").description("Roo Code CLI - Run the Roo Code agent from the command line").version("0.1.0") + +program + .argument("", "The prompt/task to execute") + .option("-w, --workspace ", "Workspace path to operate in", process.cwd()) + .option("-e, --extension ", "Path to the extension bundle directory") + .option("-v, --verbose", "Enable verbose output (show VSCode and extension logs)", false) + .option("-d, --debug", "Enable debug output (includes detailed debug information)", false) + .option("-x, --exit-on-complete", "Exit the process when the task completes (useful for testing)", false) + .option("-y, --yes", "Auto-approve all prompts (non-interactive mode)", false) + .option("-k, --api-key ", "API key for the LLM provider (defaults to ANTHROPIC_API_KEY env var)") + .option("-p, --provider ", "API provider (anthropic, openai, openrouter, etc.)", "openrouter") + .option("-m, --model ", "Model to use", DEFAULTS.model) + .option("-M, --mode ", "Mode to start in (code, architect, ask, debug, etc.)", DEFAULTS.mode) + .option( + "-r, --reasoning-effort ", + "Reasoning effort level (unspecified, disabled, none, minimal, low, medium, high, xhigh)", + DEFAULTS.reasoningEffort, + ) + .action( + async ( + prompt: string, + options: { + workspace: string + extension?: string + verbose: boolean + debug: boolean + exitOnComplete: boolean + yes: boolean + apiKey?: string + provider: ProviderName + model?: string + mode?: string + reasoningEffort?: ReasoningEffortExtended | "unspecified" | "disabled" + }, + ) => { + // Default is quiet mode - suppress VSCode shim logs unless verbose + // or debug is specified. + if (!options.verbose && !options.debug) { + setLogger({ + info: () => {}, + warn: () => {}, + error: () => {}, + debug: () => {}, + }) + } + + const extensionPath = options.extension || getDefaultExtensionPath(__dirname) + const apiKey = options.apiKey || getApiKeyFromEnv(options.provider) + const workspacePath = path.resolve(options.workspace) + + if (!apiKey) { + console.error( + `[CLI] Error: No API key provided. Use --api-key or set the appropriate environment variable.`, + ) + console.error(`[CLI] For ${options.provider}, set ${getEnvVarName(options.provider)}`) + process.exit(1) + } + + if (!fs.existsSync(workspacePath)) { + console.error(`[CLI] Error: Workspace path does not exist: ${workspacePath}`) + process.exit(1) + } + + if (!isProviderName(options.provider)) { + console.error(`[CLI] Error: Invalid provider: ${options.provider}`) + process.exit(1) + } + + if (options.reasoningEffort && !REASONING_EFFORTS.includes(options.reasoningEffort)) { + console.error( + `[CLI] Error: Invalid reasoning effort: ${options.reasoningEffort}, must be one of: ${REASONING_EFFORTS.join(", ")}`, + ) + process.exit(1) + } + + console.log(`[CLI] Mode: ${options.mode || "default"}`) + console.log(`[CLI] Reasoning Effort: ${options.reasoningEffort || "default"}`) + console.log(`[CLI] Provider: ${options.provider}`) + console.log(`[CLI] Model: ${options.model || "default"}`) + console.log(`[CLI] Workspace: ${workspacePath}`) + + const host = new ExtensionHost({ + mode: options.mode || DEFAULTS.mode, + reasoningEffort: options.reasoningEffort === "unspecified" ? undefined : options.reasoningEffort, + apiProvider: options.provider, + apiKey, + model: options.model || DEFAULTS.model, + workspacePath, + extensionPath: path.resolve(extensionPath), + verbose: options.debug, + quiet: !options.verbose && !options.debug, + nonInteractive: options.yes, + }) + + // Handle SIGINT (Ctrl+C) + process.on("SIGINT", async () => { + console.log("\n[CLI] Received SIGINT, shutting down...") + await host.dispose() + process.exit(130) + }) + + // Handle SIGTERM + process.on("SIGTERM", async () => { + console.log("\n[CLI] Received SIGTERM, shutting down...") + await host.dispose() + process.exit(143) + }) + + try { + await host.activate() + await host.runTask(prompt) + await host.dispose() + + if (options.exitOnComplete) { + process.exit(0) + } + } catch (error) { + console.error("[CLI] Error:", error instanceof Error ? error.message : String(error)) + + if (options.debug && error instanceof Error) { + console.error(error.stack) + } + + await host.dispose() + process.exit(1) + } + }, + ) + +program.parse() diff --git a/apps/cli/src/utils.ts b/apps/cli/src/utils.ts new file mode 100644 index 0000000000..5ea12e33b6 --- /dev/null +++ b/apps/cli/src/utils.ts @@ -0,0 +1,62 @@ +/** + * Utility functions for the Roo Code CLI + */ + +import path from "path" +import fs from "fs" + +/** + * Get the environment variable name for a provider's API key + */ +export function getEnvVarName(provider: string): string { + const envVarMap: Record = { + anthropic: "ANTHROPIC_API_KEY", + openai: "OPENAI_API_KEY", + openrouter: "OPENROUTER_API_KEY", + google: "GOOGLE_API_KEY", + gemini: "GOOGLE_API_KEY", + bedrock: "AWS_ACCESS_KEY_ID", + ollama: "OLLAMA_API_KEY", + mistral: "MISTRAL_API_KEY", + deepseek: "DEEPSEEK_API_KEY", + } + return envVarMap[provider.toLowerCase()] || `${provider.toUpperCase()}_API_KEY` +} + +/** + * Get API key from environment variable based on provider + */ +export function getApiKeyFromEnv(provider: string): string | undefined { + const envVar = getEnvVarName(provider) + return process.env[envVar] +} + +/** + * Get the default path to the extension bundle. + * This assumes the CLI is installed alongside the built extension. + * + * @param dirname - The __dirname equivalent for the calling module + */ +export function getDefaultExtensionPath(dirname: string): string { + // Check for environment variable first (set by install script) + if (process.env.ROO_EXTENSION_PATH) { + const envPath = process.env.ROO_EXTENSION_PATH + if (fs.existsSync(path.join(envPath, "extension.js"))) { + return envPath + } + } + + // __dirname is apps/cli/dist when bundled + // The extension is at src/dist (relative to monorepo root) + // So from apps/cli/dist, we need to go ../../../src/dist + const monorepoPath = path.resolve(dirname, "../../../src/dist") + + // Try monorepo path first (for development) + if (fs.existsSync(path.join(monorepoPath, "extension.js"))) { + return monorepoPath + } + + // Fallback: when installed via curl script, extension is at ../extension + const packagePath = path.resolve(dirname, "../extension") + return packagePath +} diff --git a/apps/cli/tsconfig.json b/apps/cli/tsconfig.json new file mode 100644 index 0000000000..9893fe2966 --- /dev/null +++ b/apps/cli/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "@roo-code/config-typescript/base.json", + "compilerOptions": { + "types": ["vitest/globals"], + "outDir": "dist" + }, + "include": ["src", "*.config.ts"], + "exclude": ["node_modules"] +} diff --git a/apps/cli/tsup.config.ts b/apps/cli/tsup.config.ts new file mode 100644 index 0000000000..f692148c3d --- /dev/null +++ b/apps/cli/tsup.config.ts @@ -0,0 +1,24 @@ +import { defineConfig } from "tsup" + +export default defineConfig({ + entry: ["src/index.ts"], + format: ["esm"], + dts: true, + clean: true, + sourcemap: true, + target: "node20", + platform: "node", + banner: { + js: "#!/usr/bin/env node", + }, + // Bundle workspace packages that export TypeScript + noExternal: ["@roo-code/types", "@roo-code/vscode-shim"], + external: [ + // Keep native modules external + "@anthropic-ai/sdk", + "@anthropic-ai/bedrock-sdk", + "@anthropic-ai/vertex-sdk", + // Keep @vscode/ripgrep external - we bundle the binary separately + "@vscode/ripgrep", + ], +}) diff --git a/apps/cli/vitest.config.ts b/apps/cli/vitest.config.ts new file mode 100644 index 0000000000..a558a62e83 --- /dev/null +++ b/apps/cli/vitest.config.ts @@ -0,0 +1,11 @@ +import { defineConfig } from "vitest/config" + +export default defineConfig({ + test: { + globals: true, + environment: "node", + watch: false, + testTimeout: 120_000, // 2m for integration tests. + include: ["src/**/*.test.ts"], + }, +}) diff --git a/apps/vscode-e2e/package.json b/apps/vscode-e2e/package.json index 1d19ffebf2..d366f72a2d 100644 --- a/apps/vscode-e2e/package.json +++ b/apps/vscode-e2e/package.json @@ -18,7 +18,7 @@ "@types/vscode": "^1.95.0", "@vscode/test-cli": "^0.0.11", "@vscode/test-electron": "^2.4.0", - "glob": "^11.0.1", + "glob": "^11.1.0", "mocha": "^11.1.0", "rimraf": "^6.0.1", "typescript": "5.8.3" diff --git a/apps/vscode-e2e/src/suite/extension.test.ts b/apps/vscode-e2e/src/suite/extension.test.ts index e7a92521cf..c5340a882d 100644 --- a/apps/vscode-e2e/src/suite/extension.test.ts +++ b/apps/vscode-e2e/src/suite/extension.test.ts @@ -15,16 +15,10 @@ suite("Roo Code Extension", function () { "SidebarProvider.removeView", "activationCompleted", "plusButtonClicked", - "mcpButtonClicked", - "promptsButtonClicked", "popoutButtonClicked", "openInNewTab", "settingsButtonClicked", "historyButtonClicked", - "showHumanRelayDialog", - "registerHumanRelayCallback", - "unregisterHumanRelayCallback", - "handleHumanRelayResponse", "newTask", "setCustomStoragePath", "focusInput", diff --git a/apps/vscode-e2e/src/suite/tools/insert-content.test.ts b/apps/vscode-e2e/src/suite/tools/insert-content.test.ts deleted file mode 100644 index a3a3abb186..0000000000 --- a/apps/vscode-e2e/src/suite/tools/insert-content.test.ts +++ /dev/null @@ -1,628 +0,0 @@ -import * as assert from "assert" -import * as fs from "fs/promises" -import * as path from "path" -import * as vscode from "vscode" - -import { RooCodeEventName, type ClineMessage } from "@roo-code/types" - -import { waitFor, sleep } from "../utils" -import { setDefaultSuiteTimeout } from "../test-utils" - -suite.skip("Roo Code insert_content Tool", function () { - setDefaultSuiteTimeout(this) - - let workspaceDir: string - - // Pre-created test files that will be used across tests - const testFiles = { - simpleText: { - name: `test-insert-simple-${Date.now()}.txt`, - content: "Line 1\nLine 2\nLine 3", - path: "", - }, - jsFile: { - name: `test-insert-js-${Date.now()}.js`, - content: `function hello() { - console.log("Hello World") -} - -function goodbye() { - console.log("Goodbye World") -}`, - path: "", - }, - emptyFile: { - name: `test-insert-empty-${Date.now()}.txt`, - content: "", - path: "", - }, - pythonFile: { - name: `test-insert-python-${Date.now()}.py`, - content: `def main(): - print("Start") - print("End")`, - path: "", - }, - } - - // Get the actual workspace directory that VSCode is using and create all test files - suiteSetup(async function () { - // Get the workspace folder from VSCode - const workspaceFolders = vscode.workspace.workspaceFolders - if (!workspaceFolders || workspaceFolders.length === 0) { - throw new Error("No workspace folder found") - } - workspaceDir = workspaceFolders[0]!.uri.fsPath - console.log("Using workspace directory:", workspaceDir) - - // Create all test files before any tests run - console.log("Creating test files in workspace...") - for (const [key, file] of Object.entries(testFiles)) { - file.path = path.join(workspaceDir, file.name) - await fs.writeFile(file.path, file.content) - console.log(`Created ${key} test file at:`, file.path) - } - - // Verify all files exist - for (const [key, file] of Object.entries(testFiles)) { - const exists = await fs - .access(file.path) - .then(() => true) - .catch(() => false) - if (!exists) { - throw new Error(`Failed to create ${key} test file at ${file.path}`) - } - } - }) - - // Clean up after all tests - suiteTeardown(async () => { - // Cancel any running tasks before cleanup - test("Should insert content at the beginning of a file (line 1)", async function () { - const api = globalThis.api - // Clean up before each test - setup(async () => { - // Cancel any previous task - try { - await globalThis.api.cancelCurrentTask() - } catch { - // Task might not be running - } - - // Small delay to ensure clean state - await sleep(100) - }) - - // Clean up after each test - teardown(async () => { - // Cancel the current task - try { - await globalThis.api.cancelCurrentTask() - } catch { - // Task might not be running - } - - // Small delay to ensure clean state - await sleep(100) - }) - const messages: ClineMessage[] = [] - const testFile = testFiles.simpleText - const insertContent = "New first line" - const expectedContent = `${insertContent} -${testFile.content}` - let taskStarted = false - let taskCompleted = false - let errorOccurred: string | null = null - let insertContentExecuted = false - - // Listen for messages - const messageHandler = ({ message }: { message: ClineMessage }) => { - messages.push(message) - - // Log important messages for debugging - if (message.type === "say" && message.say === "error") { - errorOccurred = message.text || "Unknown error" - console.error("Error:", message.text) - } - if (message.type === "ask" && message.ask === "tool") { - console.log("Tool request:", message.text?.substring(0, 200)) - } - if (message.type === "say" && (message.say === "completion_result" || message.say === "text")) { - console.log("AI response:", message.text?.substring(0, 200)) - } - - // Check for tool execution - if (message.type === "say" && message.say === "api_req_started" && message.text) { - console.log("API request started:", message.text.substring(0, 200)) - try { - const requestData = JSON.parse(message.text) - if (requestData.request && requestData.request.includes("insert_content")) { - insertContentExecuted = true - console.log("insert_content tool executed!") - } - } catch (e) { - console.log("Failed to parse api_req_started message:", e) - } - } - } - api.on(RooCodeEventName.Message, messageHandler) - - // Listen for task events - const taskStartedHandler = (id: string) => { - if (id === taskId) { - taskStarted = true - console.log("Task started:", id) - } - } - api.on(RooCodeEventName.TaskStarted, taskStartedHandler) - - const taskCompletedHandler = (id: string) => { - if (id === taskId) { - taskCompleted = true - console.log("Task completed:", id) - } - } - api.on(RooCodeEventName.TaskCompleted, taskCompletedHandler) - - let taskId: string - try { - // Start the task - taskId = await api.startNewTask({ - configuration: { - mode: "code", - autoApprovalEnabled: true, - alwaysAllowWrite: true, - alwaysAllowReadOnly: true, - alwaysAllowReadOnlyOutsideWorkspace: true, - }, - text: `Use insert_content to add "${insertContent}" at line 1 (beginning) of the file ${testFile.name}. The file already exists with this content: -${testFile.content} - -Assume the file exists and you can modify it directly.`, - }) - - console.log("Task ID:", taskId) - console.log("Test filename:", testFile.name) - - // Wait for task to start - await waitFor(() => taskStarted, { timeout: 45_000 }) - - // Check for early errors - if (errorOccurred) { - console.error("Early error detected:", errorOccurred) - } - - // Wait for task completion - await waitFor(() => taskCompleted, { timeout: 45_000 }) - - // Give extra time for file system operations - await sleep(2000) - - // Check if the file was modified correctly - const actualContent = await fs.readFile(testFile.path, "utf-8") - console.log("File content after insertion:", actualContent) - - // Verify tool was executed - assert.strictEqual(insertContentExecuted, true, "insert_content tool should have been executed") - - // Verify file content - assert.strictEqual( - actualContent.trim(), - expectedContent.trim(), - "Content should be inserted at the beginning of the file", - ) - - // Verify no errors occurred - assert.strictEqual( - errorOccurred, - null, - `Task should complete without errors, but got: ${errorOccurred}`, - ) - - console.log("Test passed! insert_content tool executed and content inserted at beginning successfully") - } finally { - api.off(RooCodeEventName.Message, messageHandler) - api.off(RooCodeEventName.TaskStarted, taskStartedHandler) - api.off(RooCodeEventName.TaskCompleted, taskCompletedHandler) - } - }) - try { - await globalThis.api.cancelCurrentTask() - } catch { - // Task might not be running - } - - // Clean up all test files - console.log("Cleaning up test files...") - for (const [key, file] of Object.entries(testFiles)) { - try { - await fs.unlink(file.path) - console.log(`Cleaned up ${key} test file`) - } catch (error) { - console.log(`Failed to clean up ${key} test file:`, error) - } - } - }) - - test("Should insert content at the end of a file (line 0)", async function () { - const api = globalThis.api - const messages: ClineMessage[] = [] - const testFile = testFiles.simpleText - const insertContent = "New last line" - const expectedContent = `${testFile.content} -${insertContent}` - let taskStarted = false - let taskCompleted = false - let errorOccurred: string | null = null - let insertContentExecuted = false - - // Listen for messages - const messageHandler = ({ message }: { message: ClineMessage }) => { - messages.push(message) - - // Log important messages for debugging - if (message.type === "say" && message.say === "error") { - errorOccurred = message.text || "Unknown error" - console.error("Error:", message.text) - } - if (message.type === "ask" && message.ask === "tool") { - console.log("Tool request:", message.text?.substring(0, 200)) - } - if (message.type === "say" && (message.say === "completion_result" || message.say === "text")) { - console.log("AI response:", message.text?.substring(0, 200)) - } - - // Check for tool execution - if (message.type === "say" && message.say === "api_req_started" && message.text) { - console.log("API request started:", message.text.substring(0, 200)) - try { - const requestData = JSON.parse(message.text) - if (requestData.request && requestData.request.includes("insert_content")) { - insertContentExecuted = true - console.log("insert_content tool executed!") - } - } catch (e) { - console.log("Failed to parse api_req_started message:", e) - } - } - } - api.on(RooCodeEventName.Message, messageHandler) - - // Listen for task events - const taskStartedHandler = (id: string) => { - if (id === taskId) { - taskStarted = true - console.log("Task started:", id) - } - } - api.on(RooCodeEventName.TaskStarted, taskStartedHandler) - - const taskCompletedHandler = (id: string) => { - if (id === taskId) { - taskCompleted = true - console.log("Task completed:", id) - } - } - api.on(RooCodeEventName.TaskCompleted, taskCompletedHandler) - - let taskId: string - try { - // Start the task - taskId = await api.startNewTask({ - configuration: { - mode: "code", - autoApprovalEnabled: true, - alwaysAllowWrite: true, - alwaysAllowReadOnly: true, - alwaysAllowReadOnlyOutsideWorkspace: true, - }, - text: `Use insert_content to add "${insertContent}" at line 0 (end of file) of the file ${testFile.name}. The file already exists with this content: -${testFile.content} - -Assume the file exists and you can modify it directly.`, - }) - - console.log("Task ID:", taskId) - console.log("Test filename:", testFile.name) - - // Wait for task to start - await waitFor(() => taskStarted, { timeout: 45_000 }) - - // Check for early errors - if (errorOccurred) { - console.error("Early error detected:", errorOccurred) - } - - // Wait for task completion - await waitFor(() => taskCompleted, { timeout: 45_000 }) - - // Give extra time for file system operations - await sleep(2000) - - // Check if the file was modified correctly - const actualContent = await fs.readFile(testFile.path, "utf-8") - console.log("File content after insertion:", actualContent) - - // Verify tool was executed - test("Should insert multiline content into a JavaScript file", async function () { - const api = globalThis.api - const messages: ClineMessage[] = [] - const testFile = testFiles.jsFile - const insertContent = `// New import statements -import { utils } from './utils' -import { helpers } from './helpers'` - const expectedContent = `${insertContent} -${testFile.content}` - let taskStarted = false - let taskCompleted = false - let errorOccurred: string | null = null - let insertContentExecuted = false - - // Listen for messages - const messageHandler = ({ message }: { message: ClineMessage }) => { - messages.push(message) - - // Log important messages for debugging - if (message.type === "say" && message.say === "error") { - errorOccurred = message.text || "Unknown error" - console.error("Error:", message.text) - } - if (message.type === "ask" && message.ask === "tool") { - console.log("Tool request:", message.text?.substring(0, 200)) - } - if (message.type === "say" && (message.say === "completion_result" || message.say === "text")) { - console.log("AI response:", message.text?.substring(0, 200)) - } - - // Check for tool execution - if (message.type === "say" && message.say === "api_req_started" && message.text) { - console.log("API request started:", message.text.substring(0, 200)) - try { - const requestData = JSON.parse(message.text) - if (requestData.request && requestData.request.includes("insert_content")) { - insertContentExecuted = true - console.log("insert_content tool executed!") - } - } catch (e) { - console.log("Failed to parse api_req_started message:", e) - } - } - } - api.on(RooCodeEventName.Message, messageHandler) - - // Listen for task events - const taskStartedHandler = (id: string) => { - if (id === taskId) { - taskStarted = true - console.log("Task started:", id) - } - } - api.on(RooCodeEventName.TaskStarted, taskStartedHandler) - - const taskCompletedHandler = (id: string) => { - if (id === taskId) { - taskCompleted = true - console.log("Task completed:", id) - } - } - api.on(RooCodeEventName.TaskCompleted, taskCompletedHandler) - - let taskId: string - try { - // Start the task - taskId = await api.startNewTask({ - configuration: { - mode: "code", - autoApprovalEnabled: true, - alwaysAllowWrite: true, - alwaysAllowReadOnly: true, - alwaysAllowReadOnlyOutsideWorkspace: true, - }, - text: `Use insert_content to add import statements at the beginning (line 1) of the JavaScript file ${testFile.name}. Add these lines: -${insertContent} - -The file already exists with this content: -${testFile.content} - -Assume the file exists and you can modify it directly.`, - }) - - console.log("Task ID:", taskId) - console.log("Test filename:", testFile.name) - - // Wait for task to start - await waitFor(() => taskStarted, { timeout: 45_000 }) - - // Check for early errors - if (errorOccurred) { - console.error("Early error detected:", errorOccurred) - } - - // Wait for task completion - await waitFor(() => taskCompleted, { timeout: 45_000 }) - - // Give extra time for file system operations - await sleep(2000) - - test("Should insert content into an empty file", async function () { - const api = globalThis.api - const messages: ClineMessage[] = [] - const testFile = testFiles.emptyFile - const insertContent = `# My New File -This is the first line of content -And this is the second line` - const expectedContent = insertContent - let taskStarted = false - let taskCompleted = false - let errorOccurred: string | null = null - let insertContentExecuted = false - - // Listen for messages - const messageHandler = ({ message }: { message: ClineMessage }) => { - messages.push(message) - - // Log important messages for debugging - if (message.type === "say" && message.say === "error") { - errorOccurred = message.text || "Unknown error" - console.error("Error:", message.text) - } - if (message.type === "ask" && message.ask === "tool") { - console.log("Tool request:", message.text?.substring(0, 200)) - } - if ( - message.type === "say" && - (message.say === "completion_result" || message.say === "text") - ) { - console.log("AI response:", message.text?.substring(0, 200)) - } - - // Check for tool execution - if (message.type === "say" && message.say === "api_req_started" && message.text) { - console.log("API request started:", message.text.substring(0, 200)) - try { - const requestData = JSON.parse(message.text) - if (requestData.request && requestData.request.includes("insert_content")) { - insertContentExecuted = true - console.log("insert_content tool executed!") - } - } catch (e) { - console.log("Failed to parse api_req_started message:", e) - } - } - } - api.on(RooCodeEventName.Message, messageHandler) - - // Listen for task events - const taskStartedHandler = (id: string) => { - if (id === taskId) { - taskStarted = true - console.log("Task started:", id) - } - } - api.on(RooCodeEventName.TaskStarted, taskStartedHandler) - - const taskCompletedHandler = (id: string) => { - if (id === taskId) { - taskCompleted = true - console.log("Task completed:", id) - } - } - api.on(RooCodeEventName.TaskCompleted, taskCompletedHandler) - - let taskId: string - try { - // Start the task - taskId = await api.startNewTask({ - configuration: { - mode: "code", - autoApprovalEnabled: true, - alwaysAllowWrite: true, - alwaysAllowReadOnly: true, - alwaysAllowReadOnlyOutsideWorkspace: true, - }, - text: `Use insert_content to add content to the empty file ${testFile.name}. Add this content at line 0 (end of file): -${insertContent} - -The file is currently empty. Assume the file exists and you can modify it directly.`, - }) - - console.log("Task ID:", taskId) - console.log("Test filename:", testFile.name) - - // Wait for task to start - await waitFor(() => taskStarted, { timeout: 45_000 }) - - // Check for early errors - if (errorOccurred) { - console.error("Early error detected:", errorOccurred) - } - - // Wait for task completion - await waitFor(() => taskCompleted, { timeout: 45_000 }) - - // Give extra time for file system operations - await sleep(2000) - - // Check if the file was modified correctly - const actualContent = await fs.readFile(testFile.path, "utf-8") - console.log("File content after insertion:", actualContent) - - // Verify tool was executed - assert.strictEqual( - insertContentExecuted, - true, - "insert_content tool should have been executed", - ) - - // Verify file content - assert.strictEqual( - actualContent.trim(), - expectedContent.trim(), - "Content should be inserted into the empty file", - ) - - // Verify no errors occurred - assert.strictEqual( - errorOccurred, - null, - `Task should complete without errors, but got: ${errorOccurred}`, - ) - - console.log( - "Test passed! insert_content tool executed and content inserted into empty file successfully", - ) - } finally { - api.off(RooCodeEventName.Message, messageHandler) - api.off(RooCodeEventName.TaskStarted, taskStartedHandler) - api.off(RooCodeEventName.TaskCompleted, taskCompletedHandler) - } - }) - // Check if the file was modified correctly - const actualContent = await fs.readFile(testFile.path, "utf-8") - console.log("File content after insertion:", actualContent) - - // Verify tool was executed - assert.strictEqual(insertContentExecuted, true, "insert_content tool should have been executed") - - // Verify file content - assert.strictEqual( - actualContent.trim(), - expectedContent.trim(), - "Multiline content should be inserted at the beginning of the JavaScript file", - ) - - // Verify no errors occurred - assert.strictEqual( - errorOccurred, - null, - `Task should complete without errors, but got: ${errorOccurred}`, - ) - - console.log("Test passed! insert_content tool executed and multiline content inserted successfully") - } finally { - api.off(RooCodeEventName.Message, messageHandler) - api.off(RooCodeEventName.TaskStarted, taskStartedHandler) - api.off(RooCodeEventName.TaskCompleted, taskCompletedHandler) - } - }) - assert.strictEqual(insertContentExecuted, true, "insert_content tool should have been executed") - - // Verify file content - assert.strictEqual( - actualContent.trim(), - expectedContent.trim(), - "Content should be inserted at the end of the file", - ) - - // Verify no errors occurred - assert.strictEqual(errorOccurred, null, `Task should complete without errors, but got: ${errorOccurred}`) - - console.log("Test passed! insert_content tool executed and content inserted at end successfully") - } finally { - api.off(RooCodeEventName.Message, messageHandler) - api.off(RooCodeEventName.TaskStarted, taskStartedHandler) - api.off(RooCodeEventName.TaskCompleted, taskCompletedHandler) - } - }) - // Tests will be added here one by one -}) diff --git a/apps/web-evals/package.json b/apps/web-evals/package.json index 3774016332..9ba2c98c2c 100644 --- a/apps/web-evals/package.json +++ b/apps/web-evals/package.json @@ -14,6 +14,7 @@ "dependencies": { "@hookform/resolvers": "^5.1.1", "@radix-ui/react-alert-dialog": "^1.1.7", + "@radix-ui/react-checkbox": "^1.1.5", "@radix-ui/react-dialog": "^1.1.6", "@radix-ui/react-dropdown-menu": "^2.1.7", "@radix-ui/react-label": "^2.1.2", @@ -28,12 +29,13 @@ "@roo-code/evals": "workspace:^", "@roo-code/types": "workspace:^", "@tanstack/react-query": "^5.69.0", + "archiver": "^7.0.1", "class-variance-authority": "^0.7.1", "clsx": "^2.1.1", "cmdk": "^1.1.0", "fuzzysort": "^3.1.0", "lucide-react": "^0.518.0", - "next": "^15.2.5", + "next": "~15.2.8", "next-themes": "^0.4.6", "p-map": "^7.0.3", "react": "^18.3.1", @@ -51,6 +53,7 @@ "@roo-code/config-eslint": "workspace:^", "@roo-code/config-typescript": "workspace:^", "@tailwindcss/postcss": "^4", + "@types/archiver": "^7.0.0", "@types/ps-tree": "^1.1.6", "@types/react": "^18.3.23", "@types/react-dom": "^18.3.5", diff --git a/apps/web-evals/src/actions/__tests__/killRun.spec.ts b/apps/web-evals/src/actions/__tests__/killRun.spec.ts new file mode 100644 index 0000000000..814d70d9fc --- /dev/null +++ b/apps/web-evals/src/actions/__tests__/killRun.spec.ts @@ -0,0 +1,207 @@ +// npx vitest run src/actions/__tests__/killRun.spec.ts + +import { execFileSync } from "child_process" + +// Mock child_process +vi.mock("child_process", () => ({ + execFileSync: vi.fn(), + spawn: vi.fn(), +})) + +// Mock next/cache +vi.mock("next/cache", () => ({ + revalidatePath: vi.fn(), +})) + +// Mock redis client +vi.mock("@/lib/server/redis", () => ({ + redisClient: vi.fn().mockResolvedValue({ + del: vi.fn().mockResolvedValue(1), + }), +})) + +// Mock @roo-code/evals +vi.mock("@roo-code/evals", () => ({ + createRun: vi.fn(), + deleteRun: vi.fn(), + createTask: vi.fn(), + exerciseLanguages: [], + getExercisesForLanguage: vi.fn().mockResolvedValue([]), +})) + +// Mock timers to speed up tests +vi.useFakeTimers() + +// Import after mocks +import { killRun } from "../runs" + +const mockExecFileSync = execFileSync as ReturnType + +describe("killRun", () => { + beforeEach(() => { + vi.clearAllMocks() + }) + + afterEach(() => { + vi.clearAllTimers() + }) + + it("should kill controller first, wait, then kill task containers", async () => { + const runId = 123 + + // execFileSync is used for all docker commands + mockExecFileSync + .mockReturnValueOnce("") // docker kill controller + .mockReturnValueOnce("evals-task-123-456.0\nevals-task-123-789.1\n") // docker ps + .mockReturnValueOnce("") // docker kill evals-task-123-456.0 + .mockReturnValueOnce("") // docker kill evals-task-123-789.1 + + const resultPromise = killRun(runId) + + // Fast-forward past the 10 second sleep + await vi.advanceTimersByTimeAsync(10000) + + const result = await resultPromise + + expect(result.success).toBe(true) + expect(result.killedContainers).toContain("evals-controller-123") + expect(result.killedContainers).toContain("evals-task-123-456.0") + expect(result.killedContainers).toContain("evals-task-123-789.1") + expect(result.errors).toHaveLength(0) + + // Verify execFileSync was called for docker kill + expect(mockExecFileSync).toHaveBeenNthCalledWith( + 1, + "docker", + ["kill", "evals-controller-123"], + expect.any(Object), + ) + // Verify execFileSync was called for docker ps with run-specific filter + expect(mockExecFileSync).toHaveBeenNthCalledWith( + 2, + "docker", + ["ps", "--format", "{{.Names}}", "--filter", "name=evals-task-123-"], + expect.any(Object), + ) + }) + + it("should continue killing runners even if controller is not running", async () => { + const runId = 456 + + mockExecFileSync + .mockImplementationOnce(() => { + throw new Error("No such container") + }) // controller kill fails + .mockReturnValueOnce("evals-task-456-100.0\n") // docker ps + .mockReturnValueOnce("") // docker kill task + + const resultPromise = killRun(runId) + await vi.advanceTimersByTimeAsync(10000) + const result = await resultPromise + + expect(result.success).toBe(true) + expect(result.killedContainers).toContain("evals-task-456-100.0") + // Controller not in list since it failed + expect(result.killedContainers).not.toContain("evals-controller-456") + }) + + it("should clear Redis state after killing containers", async () => { + const runId = 789 + + const mockDel = vi.fn().mockResolvedValue(1) + const { redisClient } = await import("@/lib/server/redis") + vi.mocked(redisClient).mockResolvedValue({ del: mockDel } as never) + + mockExecFileSync + .mockReturnValueOnce("") // controller kill + .mockReturnValueOnce("") // docker ps (no tasks) + + const resultPromise = killRun(runId) + await vi.advanceTimersByTimeAsync(10000) + await resultPromise + + expect(mockDel).toHaveBeenCalledWith("heartbeat:789") + expect(mockDel).toHaveBeenCalledWith("runners:789") + }) + + it("should handle docker ps failure gracefully", async () => { + const runId = 111 + + mockExecFileSync + .mockReturnValueOnce("") // controller kill succeeds + .mockImplementationOnce(() => { + throw new Error("Docker error") + }) // docker ps fails + + const resultPromise = killRun(runId) + await vi.advanceTimersByTimeAsync(10000) + const result = await resultPromise + + // Should still be successful because controller was killed + expect(result.success).toBe(true) + expect(result.killedContainers).toContain("evals-controller-111") + expect(result.errors).toContain("Failed to list Docker task containers") + }) + + it("should handle individual task kill failures", async () => { + const runId = 222 + + mockExecFileSync + .mockReturnValueOnce("") // controller kill + .mockReturnValueOnce("evals-task-222-300.0\nevals-task-222-400.0\n") // docker ps + .mockImplementationOnce(() => { + throw new Error("Kill failed") + }) // first task kill fails + .mockReturnValueOnce("") // second task kill succeeds + + const resultPromise = killRun(runId) + await vi.advanceTimersByTimeAsync(10000) + const result = await resultPromise + + expect(result.success).toBe(true) + expect(result.killedContainers).toContain("evals-controller-222") + expect(result.killedContainers).toContain("evals-task-222-400.0") + expect(result.errors.length).toBe(1) + expect(result.errors[0]).toContain("evals-task-222-300.0") + }) + + it("should return success with no containers when nothing is running", async () => { + const runId = 333 + + mockExecFileSync + .mockImplementationOnce(() => { + throw new Error("No such container") + }) // controller not running + .mockReturnValueOnce("") // no task containers + + const resultPromise = killRun(runId) + await vi.advanceTimersByTimeAsync(10000) + const result = await resultPromise + + expect(result.success).toBe(true) + expect(result.killedContainers).toHaveLength(0) + expect(result.errors).toHaveLength(0) + }) + + it("should only kill containers belonging to the specific run", async () => { + const runId = 555 + + mockExecFileSync + .mockReturnValueOnce("") // controller kill + .mockReturnValueOnce("evals-task-555-100.0\n") // docker ps + .mockReturnValueOnce("") // docker kill task + + const resultPromise = killRun(runId) + await vi.advanceTimersByTimeAsync(10000) + const result = await resultPromise + + expect(result.success).toBe(true) + // Verify execFileSync was called for docker ps with run-specific filter + expect(mockExecFileSync).toHaveBeenNthCalledWith( + 2, + "docker", + ["ps", "--format", "{{.Names}}", "--filter", "name=evals-task-555-"], + expect.any(Object), + ) + }) +}) diff --git a/apps/web-evals/src/actions/runs.ts b/apps/web-evals/src/actions/runs.ts index 2eae1f6804..f0c1578aed 100644 --- a/apps/web-evals/src/actions/runs.ts +++ b/apps/web-evals/src/actions/runs.ts @@ -3,7 +3,7 @@ import * as path from "path" import fs from "fs" import { fileURLToPath } from "url" -import { spawn } from "child_process" +import { spawn, execFileSync } from "child_process" import { revalidatePath } from "next/cache" import pMap from "p-map" @@ -13,19 +13,33 @@ import { exerciseLanguages, createRun as _createRun, deleteRun as _deleteRun, + updateRun as _updateRun, + getIncompleteRuns as _getIncompleteRuns, + deleteRunsByIds as _deleteRunsByIds, createTask, getExercisesForLanguage, } from "@roo-code/evals" import { CreateRun } from "@/lib/schemas" +import { redisClient } from "@/lib/server/redis" + +// Storage base path for eval logs +const EVALS_STORAGE_PATH = "/tmp/evals/runs" const EVALS_REPO_PATH = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../../../../evals") -// eslint-disable-next-line @typescript-eslint/no-unused-vars -export async function createRun({ suite, exercises = [], systemPrompt, timeout, ...values }: CreateRun) { +export async function createRun({ + suite, + exercises = [], + timeout, + iterations = 1, + executionMethod = "vscode", + ...values +}: CreateRun) { const run = await _createRun({ ...values, timeout, + executionMethod, socketPath: "", // TODO: Get rid of this. }) @@ -37,15 +51,34 @@ export async function createRun({ suite, exercises = [], systemPrompt, timeout, throw new Error("Invalid exercise path: " + path) } - await createTask({ ...values, runId: run.id, language: language as ExerciseLanguage, exercise }) + // Create multiple tasks for each iteration + for (let iteration = 1; iteration <= iterations; iteration++) { + await createTask({ + ...values, + runId: run.id, + language: language as ExerciseLanguage, + exercise, + iteration, + }) + } } } else { for (const language of exerciseLanguages) { - const exercises = await getExercisesForLanguage(EVALS_REPO_PATH, language) + const languageExercises = await getExercisesForLanguage(EVALS_REPO_PATH, language) - await pMap(exercises, (exercise) => createTask({ runId: run.id, language, exercise }), { - concurrency: 10, - }) + // Create tasks for all iterations of each exercise + const tasksToCreate: Array<{ language: ExerciseLanguage; exercise: string; iteration: number }> = [] + for (const exercise of languageExercises) { + for (let iteration = 1; iteration <= iterations; iteration++) { + tasksToCreate.push({ language, exercise, iteration }) + } + } + + await pMap( + tasksToCreate, + ({ language, exercise, iteration }) => createTask({ runId: run.id, language, exercise, iteration }), + { concurrency: 10 }, + ) } } @@ -98,3 +131,247 @@ export async function deleteRun(runId: number) { await _deleteRun(runId) revalidatePath("/runs") } + +export type KillRunResult = { + success: boolean + killedContainers: string[] + errors: string[] +} + +const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) + +/** + * Kill all Docker containers associated with a run (controller and task runners). + * Kills the controller first, waits 10 seconds, then kills runners. + * Also clears Redis state for heartbeat and runners. + * + * Container naming conventions: + * - Controller: evals-controller-{runId} + * - Task runners: evals-task-{runId}-{taskId}.{attempt} + */ +export async function killRun(runId: number): Promise { + const killedContainers: string[] = [] + const errors: string[] = [] + const controllerPattern = `evals-controller-${runId}` + const taskPattern = `evals-task-${runId}-` + + try { + // Step 1: Kill the controller first + console.log(`Killing controller: ${controllerPattern}`) + try { + execFileSync("docker", ["kill", controllerPattern], { encoding: "utf-8", timeout: 10000 }) + killedContainers.push(controllerPattern) + console.log(`Killed controller container: ${controllerPattern}`) + } catch (_error) { + // Controller might not be running - that's ok, continue to kill runners + console.log(`Controller ${controllerPattern} not running or already stopped`) + } + + // Step 2: Wait 10 seconds before killing runners + console.log("Waiting 10 seconds before killing runners...") + await sleep(10000) + + // Step 3: Find and kill all task runner containers for THIS run only + let taskContainerNames: string[] = [] + + try { + const output = execFileSync("docker", ["ps", "--format", "{{.Names}}", "--filter", `name=${taskPattern}`], { + encoding: "utf-8", + timeout: 10000, + }) + taskContainerNames = output + .split("\n") + .map((name) => name.trim()) + .filter((name) => name.length > 0 && name.startsWith(taskPattern)) + } catch (error) { + console.error("Failed to list task containers:", error) + errors.push("Failed to list Docker task containers") + } + + // Kill each task runner container + for (const containerName of taskContainerNames) { + try { + execFileSync("docker", ["kill", containerName], { encoding: "utf-8", timeout: 10000 }) + killedContainers.push(containerName) + console.log(`Killed task container: ${containerName}`) + } catch (error) { + // Container might have already stopped + console.error(`Failed to kill container ${containerName}:`, error) + errors.push(`Failed to kill container: ${containerName}`) + } + } + + // Step 4: Clear Redis state + try { + const redis = await redisClient() + const heartbeatKey = `heartbeat:${runId}` + const runnersKey = `runners:${runId}` + + await redis.del(heartbeatKey) + await redis.del(runnersKey) + console.log(`Cleared Redis keys: ${heartbeatKey}, ${runnersKey}`) + } catch (error) { + console.error("Failed to clear Redis state:", error) + errors.push("Failed to clear Redis state") + } + } catch (error) { + console.error("Error in killRun:", error) + errors.push("Unexpected error while killing containers") + } + + revalidatePath(`/runs/${runId}`) + revalidatePath("/runs") + + return { + success: killedContainers.length > 0 || errors.length === 0, + killedContainers, + errors, + } +} + +export type DeleteIncompleteRunsResult = { + success: boolean + deletedCount: number + deletedRunIds: number[] + storageErrors: string[] +} + +/** + * Delete all incomplete runs (runs without a taskMetricsId/final score). + * Removes both database records and storage folders. + */ +export async function deleteIncompleteRuns(): Promise { + const storageErrors: string[] = [] + + // Get all incomplete runs + const incompleteRuns = await _getIncompleteRuns() + const runIds = incompleteRuns.map((run) => run.id) + + if (runIds.length === 0) { + return { + success: true, + deletedCount: 0, + deletedRunIds: [], + storageErrors: [], + } + } + + // Delete storage folders for each run + for (const runId of runIds) { + const storagePath = path.join(EVALS_STORAGE_PATH, String(runId)) + try { + if (fs.existsSync(storagePath)) { + fs.rmSync(storagePath, { recursive: true, force: true }) + console.log(`Deleted storage folder: ${storagePath}`) + } + } catch (error) { + console.error(`Failed to delete storage folder ${storagePath}:`, error) + storageErrors.push(`Failed to delete storage for run ${runId}`) + } + + // Also try to clear Redis state for any potentially running incomplete runs + try { + const redis = await redisClient() + await redis.del(`heartbeat:${runId}`) + await redis.del(`runners:${runId}`) + } catch (error) { + // Non-critical error, just log it + console.error(`Failed to clear Redis state for run ${runId}:`, error) + } + } + + // Delete from database + await _deleteRunsByIds(runIds) + + revalidatePath("/runs") + + return { + success: true, + deletedCount: runIds.length, + deletedRunIds: runIds, + storageErrors, + } +} + +/** + * Get count of incomplete runs (for UI display) + */ +export async function getIncompleteRunsCount(): Promise { + const incompleteRuns = await _getIncompleteRuns() + return incompleteRuns.length +} + +/** + * Delete all runs older than 30 days. + * Removes both database records and storage folders. + */ +export async function deleteOldRuns(): Promise { + const storageErrors: string[] = [] + + // Get all runs older than 30 days + const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) + const { getRuns } = await import("@roo-code/evals") + const allRuns = await getRuns() + const oldRuns = allRuns.filter((run) => run.createdAt < thirtyDaysAgo) + const runIds = oldRuns.map((run) => run.id) + + if (runIds.length === 0) { + return { + success: true, + deletedCount: 0, + deletedRunIds: [], + storageErrors: [], + } + } + + // Delete storage folders for each run + for (const runId of runIds) { + const storagePath = path.join(EVALS_STORAGE_PATH, String(runId)) + try { + if (fs.existsSync(storagePath)) { + fs.rmSync(storagePath, { recursive: true, force: true }) + console.log(`Deleted storage folder: ${storagePath}`) + } + } catch (error) { + console.error(`Failed to delete storage folder ${storagePath}:`, error) + storageErrors.push(`Failed to delete storage for run ${runId}`) + } + + // Also try to clear Redis state + try { + const redis = await redisClient() + await redis.del(`heartbeat:${runId}`) + await redis.del(`runners:${runId}`) + } catch (error) { + // Non-critical error, just log it + console.error(`Failed to clear Redis state for run ${runId}:`, error) + } + } + + // Delete from database + await _deleteRunsByIds(runIds) + + revalidatePath("/runs") + + return { + success: true, + deletedCount: runIds.length, + deletedRunIds: runIds, + storageErrors, + } +} + +/** + * Update the description of a run. + */ +export async function updateRunDescription(runId: number, description: string | null): Promise<{ success: boolean }> { + try { + await _updateRun(runId, { description }) + revalidatePath("/runs") + revalidatePath(`/runs/${runId}`) + return { success: true } + } catch (error) { + console.error("Failed to update run description:", error) + return { success: false } + } +} diff --git a/apps/web-evals/src/app/api/health/route.ts b/apps/web-evals/src/app/api/health/route.ts deleted file mode 100644 index ca8a833942..0000000000 --- a/apps/web-evals/src/app/api/health/route.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { NextResponse } from "next/server" - -export async function GET() { - try { - return NextResponse.json( - { - status: "healthy", - timestamp: new Date().toISOString(), - uptime: process.uptime(), - environment: process.env.NODE_ENV || "production", - }, - { status: 200 }, - ) - } catch (error) { - return NextResponse.json( - { - status: "unhealthy", - timestamp: new Date().toISOString(), - error: error instanceof Error ? error.message : "Unknown error", - }, - { status: 503 }, - ) - } -} diff --git a/apps/web-evals/src/app/api/runs/[id]/logs/[taskId]/route.ts b/apps/web-evals/src/app/api/runs/[id]/logs/[taskId]/route.ts new file mode 100644 index 0000000000..e5ec8751ab --- /dev/null +++ b/apps/web-evals/src/app/api/runs/[id]/logs/[taskId]/route.ts @@ -0,0 +1,74 @@ +import { NextResponse } from "next/server" +import type { NextRequest } from "next/server" +import * as fs from "node:fs/promises" +import * as path from "node:path" + +import { findTask, findRun } from "@roo-code/evals" + +export const dynamic = "force-dynamic" + +const LOG_BASE_PATH = "/tmp/evals/runs" + +// Sanitize path components to prevent path traversal attacks +function sanitizePathComponent(component: string): string { + // Remove any path separators, null bytes, and other dangerous characters + return component.replace(/[/\\:\0*?"<>|]/g, "_") +} + +export async function GET(request: NextRequest, { params }: { params: Promise<{ id: string; taskId: string }> }) { + const { id, taskId } = await params + + try { + const runId = Number(id) + const taskIdNum = Number(taskId) + + if (isNaN(runId) || isNaN(taskIdNum)) { + return NextResponse.json({ error: "Invalid run ID or task ID" }, { status: 400 }) + } + + // Verify the run exists + await findRun(runId) + + // Get the task to find its language and exercise + const task = await findTask(taskIdNum) + + // Verify the task belongs to this run + if (task.runId !== runId) { + return NextResponse.json({ error: "Task does not belong to this run" }, { status: 404 }) + } + + // Sanitize language and exercise to prevent path traversal + const safeLanguage = sanitizePathComponent(task.language) + const safeExercise = sanitizePathComponent(task.exercise) + + // Construct the log file path + const logFileName = `${safeLanguage}-${safeExercise}.log` + const logFilePath = path.join(LOG_BASE_PATH, String(runId), logFileName) + + // Verify the resolved path is within the expected directory (defense in depth) + const resolvedPath = path.resolve(logFilePath) + const expectedBase = path.resolve(LOG_BASE_PATH) + if (!resolvedPath.startsWith(expectedBase)) { + return NextResponse.json({ error: "Invalid log path" }, { status: 400 }) + } + + // Check if the log file exists and read it (async) + try { + const logContent = await fs.readFile(logFilePath, "utf-8") + return NextResponse.json({ logContent }) + } catch (err) { + if ((err as NodeJS.ErrnoException).code === "ENOENT") { + return NextResponse.json({ error: "Log file not found", logContent: null }, { status: 200 }) + } + throw err + } + } catch (error) { + console.error("Error reading task log:", error) + + if (error instanceof Error && error.name === "RecordNotFoundError") { + return NextResponse.json({ error: "Task or run not found" }, { status: 404 }) + } + + return NextResponse.json({ error: "Failed to read log file" }, { status: 500 }) + } +} diff --git a/apps/web-evals/src/app/api/runs/[id]/logs/failed/route.ts b/apps/web-evals/src/app/api/runs/[id]/logs/failed/route.ts new file mode 100644 index 0000000000..8b2760df98 --- /dev/null +++ b/apps/web-evals/src/app/api/runs/[id]/logs/failed/route.ts @@ -0,0 +1,147 @@ +import { NextResponse } from "next/server" +import type { NextRequest } from "next/server" +import * as fs from "node:fs" +import * as path from "node:path" +import archiver from "archiver" + +import { findRun, getTasks } from "@roo-code/evals" + +export const dynamic = "force-dynamic" + +const LOG_BASE_PATH = "/tmp/evals/runs" + +// Sanitize path components to prevent path traversal attacks +function sanitizePathComponent(component: string): string { + // Remove any path separators, null bytes, and other dangerous characters + return component.replace(/[/\\:\0*?"<>|]/g, "_") +} + +export async function GET(request: NextRequest, { params }: { params: Promise<{ id: string }> }) { + const { id } = await params + + try { + const runId = Number(id) + + if (isNaN(runId)) { + return NextResponse.json({ error: "Invalid run ID" }, { status: 400 }) + } + + // Verify the run exists + await findRun(runId) + + // Get all tasks for this run + const tasks = await getTasks(runId) + + // Filter for failed tasks only + const failedTasks = tasks.filter((task) => task.passed === false) + + if (failedTasks.length === 0) { + return NextResponse.json({ error: "No failed tasks to export" }, { status: 400 }) + } + + // Create a zip archive + const archive = archiver("zip", { zlib: { level: 9 } }) + + // Collect chunks to build the response + const chunks: Buffer[] = [] + + archive.on("data", (chunk: Buffer) => { + chunks.push(chunk) + }) + + // Track archive errors + let archiveError: Error | null = null + archive.on("error", (err: Error) => { + archiveError = err + }) + + // Set up the end promise before finalizing (proper event listener ordering) + const archiveEndPromise = new Promise((resolve, reject) => { + archive.on("end", resolve) + archive.on("error", reject) + }) + + // Add each failed task's log file and history files to the archive + const logDir = path.join(LOG_BASE_PATH, String(runId)) + let filesAdded = 0 + + for (const task of failedTasks) { + // Sanitize language and exercise to prevent path traversal + const safeLanguage = sanitizePathComponent(task.language) + const safeExercise = sanitizePathComponent(task.exercise) + const expectedBase = path.resolve(LOG_BASE_PATH) + + // Add the log file + const logFileName = `${safeLanguage}-${safeExercise}.log` + const logFilePath = path.join(logDir, logFileName) + + // Verify the resolved path is within the expected directory (defense in depth) + const resolvedLogPath = path.resolve(logFilePath) + if (resolvedLogPath.startsWith(expectedBase) && fs.existsSync(logFilePath)) { + archive.file(logFilePath, { name: logFileName }) + filesAdded++ + } + + // Add the API conversation history file + // Format: {language}-{exercise}.{iteration}_api_conversation_history.json + const apiHistoryFileName = `${safeLanguage}-${safeExercise}.${task.iteration}_api_conversation_history.json` + const apiHistoryFilePath = path.join(logDir, apiHistoryFileName) + const resolvedApiHistoryPath = path.resolve(apiHistoryFilePath) + if (resolvedApiHistoryPath.startsWith(expectedBase) && fs.existsSync(apiHistoryFilePath)) { + archive.file(apiHistoryFilePath, { name: apiHistoryFileName }) + filesAdded++ + } + + // Add the UI messages file + // Format: {language}-{exercise}.{iteration}_ui_messages.json + const uiMessagesFileName = `${safeLanguage}-${safeExercise}.${task.iteration}_ui_messages.json` + const uiMessagesFilePath = path.join(logDir, uiMessagesFileName) + const resolvedUiMessagesPath = path.resolve(uiMessagesFilePath) + if (resolvedUiMessagesPath.startsWith(expectedBase) && fs.existsSync(uiMessagesFilePath)) { + archive.file(uiMessagesFilePath, { name: uiMessagesFileName }) + filesAdded++ + } + } + + // Check if any files were actually added + if (filesAdded === 0) { + archive.abort() + return NextResponse.json( + { error: "No log files found - they may have been cleared from disk" }, + { status: 404 }, + ) + } + + // Finalize the archive + await archive.finalize() + + // Wait for all data to be collected + await archiveEndPromise + + // Check for archive errors + if (archiveError) { + throw archiveError + } + + // Combine all chunks into a single buffer + const zipBuffer = Buffer.concat(chunks) + + // Return the zip file + return new NextResponse(zipBuffer, { + status: 200, + headers: { + "Content-Type": "application/zip", + "Content-Disposition": `attachment; filename="run-${runId}-failed-logs.zip"`, + "Content-Length": String(zipBuffer.length), + }, + }) + } catch (error) { + console.error("Error exporting failed logs:", error) + + if (error instanceof Error && error.name === "RecordNotFoundError") { + return NextResponse.json({ error: "Run not found" }, { status: 404 }) + } + + return NextResponse.json({ error: "Failed to export logs" }, { status: 500 }) + } +} diff --git a/apps/web-evals/src/app/runs/[id]/page.tsx b/apps/web-evals/src/app/runs/[id]/page.tsx index aae3fc70f9..8b993eec8a 100644 --- a/apps/web-evals/src/app/runs/[id]/page.tsx +++ b/apps/web-evals/src/app/runs/[id]/page.tsx @@ -7,7 +7,7 @@ export default async function Page({ params }: { params: Promise<{ id: string }> const run = await findRun(Number(id)) return ( -

+
) diff --git a/apps/web-evals/src/app/runs/[id]/run-status.tsx b/apps/web-evals/src/app/runs/[id]/run-status.tsx index 4b94ef14fa..e05b1b51eb 100644 --- a/apps/web-evals/src/app/runs/[id]/run-status.tsx +++ b/apps/web-evals/src/app/runs/[id]/run-status.tsx @@ -1,55 +1,79 @@ "use client" +import { Link2, Link2Off, CheckCircle2 } from "lucide-react" import type { RunStatus as _RunStatus } from "@/hooks/use-run-status" import { cn } from "@/lib/utils" +import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui" -export const RunStatus = ({ runStatus: { sseStatus, heartbeat, runners = [] } }: { runStatus: _RunStatus }) => ( -
-
-
-
Task Stream:
-
{sseStatus}
-
-
-
-
-
-
-
-
-
Task Controller:
-
{heartbeat ?? "dead"}
-
-
-
-
-
-
-
-
Task Runners:
- {runners.length > 0 &&
{runners?.join(", ")}
} -
-
-) +function StreamIcon({ status }: { status: "connected" | "waiting" | "error" }) { + if (status === "connected") { + return + } + return +} + +export const RunStatus = ({ + runStatus: { sseStatus, heartbeat, runners = [] }, + isComplete = false, +}: { + runStatus: _RunStatus + isComplete?: boolean +}) => { + // For completed runs, show a simple "Complete" badge + if (isComplete) { + return ( + + +
+ +
+
+ + Run complete + +
+ ) + } + + return ( + + +
+ {/* Task Stream status icon */} + + + {/* Task Controller ID */} + {heartbeat ?? "-"} + + {/* Task Runners count */} + 0 ? "text-green-500" : "text-rose-500"}> + {runners.length > 0 ? `${runners.length}r` : "0r"} + +
+
+ +
+
+ + Task Stream: {sseStatus} +
+
+ + Task Controller: {heartbeat ?? "dead"} +
+
+ 0 ? "text-green-500" : "text-rose-500"}>● + Task Runners: {runners.length > 0 ? runners.length : "none"} +
+ {runners.length > 0 && ( +
+ {runners.map((runner) => ( +
{runner}
+ ))} +
+ )} +
+
+
+ ) +} diff --git a/apps/web-evals/src/app/runs/[id]/run.tsx b/apps/web-evals/src/app/runs/[id]/run.tsx index b6c5290b13..badd77741e 100644 --- a/apps/web-evals/src/app/runs/[id]/run.tsx +++ b/apps/web-evals/src/app/runs/[id]/run.tsx @@ -1,112 +1,1058 @@ "use client" -import { useMemo } from "react" -import { LoaderCircle } from "lucide-react" +import { useMemo, useState, useCallback, useEffect, Fragment } from "react" +import { toast } from "sonner" +import { LoaderCircle, FileText, Copy, Check, StopCircle, List, Layers } from "lucide-react" -import type { Run, TaskMetrics as _TaskMetrics } from "@roo-code/evals" +import type { Run, TaskMetrics as _TaskMetrics, Task } from "@roo-code/evals" +import type { ToolName } from "@roo-code/types" -import { formatCurrency, formatDuration, formatTokens } from "@/lib/formatters" +import { formatCurrency, formatDuration, formatTokens, formatToolUsageSuccessRate } from "@/lib/formatters" import { useRunStatus } from "@/hooks/use-run-status" -import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui" +import { killRun } from "@/actions/runs" +import { + Table, + TableBody, + TableCell, + TableHead, + TableHeader, + TableRow, + Tooltip, + TooltipContent, + TooltipTrigger, + Dialog, + DialogContent, + DialogHeader, + DialogTitle, + ScrollArea, + Button, + AlertDialog, + AlertDialogAction, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle, +} from "@/components/ui" import { TaskStatus } from "./task-status" import { RunStatus } from "./run-status" type TaskMetrics = Pick<_TaskMetrics, "tokensIn" | "tokensOut" | "tokensContext" | "duration" | "cost"> +// Extended Task type with taskMetrics from useRunStatus +type TaskWithMetrics = Task & { taskMetrics: _TaskMetrics | null } + +type ToolUsageEntry = { attempts: number; failures: number } +type ToolUsage = Record + +// Generate abbreviation from tool name (e.g., "read_file" -> "RF", "list_code_definition_names" -> "LCDN") +function getToolAbbreviation(toolName: string): string { + return toolName + .split("_") + .map((word) => word[0]?.toUpperCase() ?? "") + .join("") +} + +// Pattern definitions for syntax highlighting +type HighlightPattern = { + pattern: RegExp + className: string + // If true, wraps the entire match; if a number, wraps that capture group + wrapGroup?: number +} + +const HIGHLIGHT_PATTERNS: HighlightPattern[] = [ + // Log levels - styled as badges + { pattern: /\|\s*(INFO)\s*\|/g, className: "text-green-400", wrapGroup: 1 }, + { pattern: /\|\s*(WARN|WARNING)\s*\|/g, className: "text-yellow-400", wrapGroup: 1 }, + { pattern: /\|\s*(ERROR)\s*\|/g, className: "text-red-400 font-semibold", wrapGroup: 1 }, + { pattern: /\|\s*(DEBUG)\s*\|/g, className: "text-gray-400", wrapGroup: 1 }, + // Task identifiers - important events + { + pattern: /(taskCreated|taskFocused|taskStarted|taskCompleted|taskAborted|taskResumable)/g, + className: "text-purple-400 font-medium", + }, + // Tool failures - highlight in red + { pattern: /(taskToolFailed)/g, className: "text-red-400 font-bold" }, + { pattern: /(Tool execution failed|tool.*failed|failed.*tool)/gi, className: "text-red-400" }, + { pattern: /(EvalPass)/g, className: "text-green-400 font-bold" }, + { pattern: /(EvalFail)/g, className: "text-red-400 font-bold" }, + // Message arrows + { pattern: /→/g, className: "text-cyan-400" }, + // Tool names in quotes + { pattern: /"(tool)":\s*"([^"]+)"/g, className: "text-orange-400" }, + // JSON keys + { pattern: /"([^"]+)":/g, className: "text-sky-300" }, + // Boolean values + { pattern: /:\s*(true|false)/g, className: "text-amber-400", wrapGroup: 1 }, + // Numbers + { pattern: /:\s*(-?\d+\.?\d*)/g, className: "text-emerald-400", wrapGroup: 1 }, +] + +// Extract timestamp from a log line and return elapsed time from baseline +function formatElapsedTime(timestamp: string, baselineMs: number): string { + const currentMs = new Date(timestamp).getTime() + const elapsedMs = currentMs - baselineMs + const totalSeconds = Math.floor(elapsedMs / 1000) + const minutes = Math.floor(totalSeconds / 60) + const seconds = totalSeconds % 60 + return `${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}` +} + +// Extract the first timestamp from the log to use as baseline +function extractFirstTimestamp(log: string): number | null { + // Match timestamp at start of line: [2025-11-28T09:35:23.187Z | ... or [2025-11-28T09:35:23.187Z] + const match = log.match(/\[(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z)[\s|\]]/) + const isoString = match?.[1] + if (!isoString) return null + return new Date(isoString).getTime() +} + +// Simplify log line by removing redundant metadata +function simplifyLogLine(line: string, baselineMs: number | null): { timestamp: string; simplified: string } { + // Extract timestamp - matches [2025-11-28T09:35:23.187Z | ... format + const timestampMatch = line.match(/\[(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z)[\s|\]]/) + const isoTimestamp = timestampMatch?.[1] + if (!isoTimestamp) { + return { timestamp: "", simplified: line } + } + + const timestamp = baselineMs !== null ? formatElapsedTime(isoTimestamp, baselineMs) : isoTimestamp.slice(11, 19) + + // Remove the timestamp from the line (handles both [timestamp] and [timestamp | formats) + let simplified = line.replace(/\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z\s*\|?\s*/, "") + + // Remove redundant metadata: pid, run, task IDs (they're same for entire log) + simplified = simplified.replace(/\|\s*pid:\d+\s*/g, "") + simplified = simplified.replace(/\|\s*run:\d+\s*/g, "") + simplified = simplified.replace(/\|\s*task:\d+\s*/g, "") + simplified = simplified.replace(/runTask\s*\|\s*/g, "") + + // Clean up extra pipes, spaces, and trailing brackets + simplified = simplified.replace(/\|\s*\|/g, "|") + simplified = simplified.replace(/^\s*\|\s*/, "") + simplified = simplified.replace(/\]\s*$/, "") // Remove trailing bracket if present + + return { timestamp, simplified } +} + +// Format a single line with syntax highlighting using React elements (XSS-safe) +function formatLine(line: string): React.ReactNode[] { + // Find all matches with their positions + type Match = { start: number; end: number; text: string; className: string } + const matches: Match[] = [] + + for (const { pattern, className, wrapGroup } of HIGHLIGHT_PATTERNS) { + // Reset regex state + pattern.lastIndex = 0 + let regexMatch + while ((regexMatch = pattern.exec(line)) !== null) { + const capturedText = wrapGroup !== undefined ? regexMatch[wrapGroup] : regexMatch[0] + // Skip if capture group didn't match + if (!capturedText) continue + const start = + wrapGroup !== undefined ? regexMatch.index + regexMatch[0].indexOf(capturedText) : regexMatch.index + matches.push({ + start, + end: start + capturedText.length, + text: capturedText, + className, + }) + } + } + + // Sort matches by position and filter overlapping ones + matches.sort((a, b) => a.start - b.start) + const filteredMatches: Match[] = [] + for (const m of matches) { + const lastMatch = filteredMatches[filteredMatches.length - 1] + if (!lastMatch || m.start >= lastMatch.end) { + filteredMatches.push(m) + } + } + + // Build result with highlighted spans + const result: React.ReactNode[] = [] + let currentPos = 0 + + for (const [i, m] of filteredMatches.entries()) { + // Add text before this match + if (m.start > currentPos) { + result.push(line.slice(currentPos, m.start)) + } + // Add highlighted match + result.push( + + {m.text} + , + ) + currentPos = m.end + } + + // Add remaining text + if (currentPos < line.length) { + result.push(line.slice(currentPos)) + } + + return result.length > 0 ? result : [line] +} + +// Determine the visual style for a log line based on its content +function getLineStyle(line: string): string { + if (line.includes("ERROR")) return "bg-red-950/30 border-l-2 border-red-500" + if (line.includes("WARN") || line.includes("WARNING")) return "bg-yellow-950/20 border-l-2 border-yellow-500" + if (line.includes("taskToolFailed")) return "bg-red-950/30 border-l-2 border-red-500" + if (line.includes("taskStarted") || line.includes("taskCreated")) return "bg-purple-950/20" + if (line.includes("EvalPass")) return "bg-green-950/30 border-l-2 border-green-500" + if (line.includes("EvalFail")) return "bg-red-950/30 border-l-2 border-red-500" + if (line.includes("taskCompleted") || line.includes("taskAborted")) return "bg-blue-950/20" + return "" +} + +// Format log content with basic highlighting (XSS-safe - no dangerouslySetInnerHTML) +function formatLogContent(log: string): React.ReactNode[] { + const lines = log.split("\n") + const baselineMs = extractFirstTimestamp(log) + + return lines.map((line, index) => { + if (!line.trim()) { + return ( +
+ {" "} +
+ ) + } + + const parsed = simplifyLogLine(line, baselineMs) + const lineStyle = getLineStyle(line) + + return ( +
+ {/* Elapsed time */} + + {parsed.timestamp} + + {/* Log content - pl-12 ensures wrapped lines are indented under the timestamp */} + + {formatLine(parsed.simplified)} + +
+ ) + }) +} + export function Run({ run }: { run: Run }) { const runStatus = useRunStatus(run) - const { tasks, tokenUsage, usageUpdatedAt } = runStatus + const { tasks, tokenUsage, toolUsage, usageUpdatedAt, heartbeat, runners } = runStatus + + const [selectedTask, setSelectedTask] = useState(null) + const [taskLog, setTaskLog] = useState(null) + const [isLoadingLog, setIsLoadingLog] = useState(false) + const [copied, setCopied] = useState(false) + const [showKillDialog, setShowKillDialog] = useState(false) + const [isKilling, setIsKilling] = useState(false) + const [groupByStatus, setGroupByStatus] = useState(() => { + // Initialize from localStorage if available (client-side only) + if (typeof window !== "undefined") { + const stored = localStorage.getItem("evals-group-by-status") + return stored === "true" + } + return false + }) + + // Persist groupByStatus to localStorage + useEffect(() => { + localStorage.setItem("evals-group-by-status", String(groupByStatus)) + }, [groupByStatus]) + + // Determine if run is still active (has heartbeat or runners) + const isRunActive = !run.taskMetricsId && (!!heartbeat || (runners && runners.length > 0)) + + const onKillRun = useCallback(async () => { + setIsKilling(true) + try { + const result = await killRun(run.id) + if (result.killedContainers.length > 0) { + toast.success(`Killed ${result.killedContainers.length} container(s)`) + } else if (result.errors.length === 0) { + toast.info("No running containers found") + } else { + toast.error(result.errors.join(", ")) + } + } catch (error) { + console.error("Failed to kill run:", error) + toast.error("Failed to kill run") + } finally { + setIsKilling(false) + setShowKillDialog(false) + } + }, [run.id]) + + const onCopyLog = useCallback(async () => { + if (!taskLog) return + + try { + await navigator.clipboard.writeText(taskLog) + setCopied(true) + toast.success("Log copied to clipboard") + setTimeout(() => setCopied(false), 2000) + } catch (error) { + console.error("Failed to copy log:", error) + toast.error("Failed to copy log") + } + }, [taskLog]) + + // Handle ESC key to close the dialog + useEffect(() => { + const handleKeyDown = (e: KeyboardEvent) => { + if (e.key === "Escape" && selectedTask) { + setSelectedTask(null) + } + } + + document.addEventListener("keydown", handleKeyDown) + return () => document.removeEventListener("keydown", handleKeyDown) + }, [selectedTask]) const taskMetrics: Record = useMemo(() => { + // Reference usageUpdatedAt to trigger recomputation when Map contents change + void usageUpdatedAt const metrics: Record = {} - tasks?.forEach((task) => { - const usage = tokenUsage.get(task.id) + // Helper to calculate duration from database timestamps when streaming duration + // is unavailable (e.g., page was loaded after TaskStarted event was published) + const calculateDurationFromTimestamps = (task: TaskWithMetrics): number => { + if (!task.startedAt) return 0 + const startTime = new Date(task.startedAt).getTime() + const endTime = task.finishedAt ? new Date(task.finishedAt).getTime() : Date.now() + return endTime - startTime + } - if (task.finishedAt && task.taskMetrics) { - metrics[task.id] = task.taskMetrics - } else if (usage) { + tasks?.forEach((task) => { + const streamingUsage = tokenUsage.get(task.id) + const dbMetrics = task.taskMetrics + + // For finished tasks, prefer DB values but fall back to streaming values + // This handles race conditions during timeout where DB might not have latest data + if (task.finishedAt) { + // Check if DB metrics have meaningful values (not just default/empty) + const dbHasData = dbMetrics && (dbMetrics.tokensIn > 0 || dbMetrics.tokensOut > 0 || dbMetrics.cost > 0) + if (dbHasData) { + // If DB duration is 0 but we have timestamps, calculate from timestamps + const duration = dbMetrics.duration || calculateDurationFromTimestamps(task) + metrics[task.id] = { ...dbMetrics, duration } + } else if (streamingUsage) { + // Fall back to streaming values if DB is empty/stale + // Use streaming duration, or calculate from timestamps if not available + const duration = streamingUsage.duration || calculateDurationFromTimestamps(task) + metrics[task.id] = { + tokensIn: streamingUsage.totalTokensIn, + tokensOut: streamingUsage.totalTokensOut, + tokensContext: streamingUsage.contextTokens, + duration, + cost: streamingUsage.totalCost, + } + } else { + // Task finished but no DB metrics and no streaming data + // (e.g., page loaded after task completed, metrics not persisted) + // Still provide duration calculated from timestamps + metrics[task.id] = { + tokensIn: 0, + tokensOut: 0, + tokensContext: 0, + duration: calculateDurationFromTimestamps(task), + cost: 0, + } + } + } else if (streamingUsage) { + // For running tasks, use streaming values + // Use streaming duration, or calculate from task.startedAt if not available + // (happens when page loads after TaskStarted event was already published) + const duration = streamingUsage.duration || calculateDurationFromTimestamps(task) metrics[task.id] = { - tokensIn: usage.totalTokensIn, - tokensOut: usage.totalTokensOut, - tokensContext: usage.contextTokens, - duration: usage.duration ?? 0, - cost: usage.totalCost, + tokensIn: streamingUsage.totalTokensIn, + tokensOut: streamingUsage.totalTokensOut, + tokensContext: streamingUsage.contextTokens, + duration, + cost: streamingUsage.totalCost, + } + } else if (task.startedAt) { + // Task has started (has startedAt in DB) but no streaming data yet + // This can happen when page loads after TaskStarted but before TokenUsageUpdated + metrics[task.id] = { + tokensIn: 0, + tokensOut: 0, + tokensContext: 0, + duration: calculateDurationFromTimestamps(task), + cost: 0, } } }) return metrics - // eslint-disable-next-line react-hooks/exhaustive-deps }, [tasks, tokenUsage, usageUpdatedAt]) + const onViewTaskLog = useCallback( + async (task: Task) => { + // Only allow viewing logs for tasks that have started. + // Note: we treat presence of derived metrics as evidence of a started task, + // since this page may be rendered without streaming `tokenUsage` populated. + const hasStarted = !!task.startedAt || !!tokenUsage.get(task.id) || !!taskMetrics[task.id] + if (!hasStarted) { + toast.error("Task has not started yet") + return + } + + setSelectedTask(task) + setIsLoadingLog(true) + setTaskLog(null) + + try { + const response = await fetch(`/api/runs/${run.id}/logs/${task.id}`) + + if (!response.ok) { + const error = await response.json() + toast.error(error.error || "Failed to load log") + setSelectedTask(null) + return + } + + const data = await response.json() + setTaskLog(data.logContent) + } catch (error) { + console.error("Error loading task log:", error) + toast.error("Failed to load log") + setSelectedTask(null) + } finally { + setIsLoadingLog(false) + } + }, + [run.id, tokenUsage, taskMetrics], + ) + + // Collect all unique tool names from all tasks and sort by total attempts + const toolColumns = useMemo(() => { + // Reference usageUpdatedAt to trigger recomputation when Map contents change + void usageUpdatedAt + if (!tasks) return [] + + const toolTotals = new Map() + + for (const task of tasks) { + // Get both DB and streaming values + const dbToolUsage = task.taskMetrics?.toolUsage + const streamingToolUsage = toolUsage.get(task.id) + + // For finished tasks, prefer DB values but fall back to streaming values + // For running tasks, use streaming values + // This handles race conditions during timeout where DB might not have latest data + const taskToolUsage = task.finishedAt + ? dbToolUsage && Object.keys(dbToolUsage).length > 0 + ? dbToolUsage + : streamingToolUsage + : streamingToolUsage + + if (taskToolUsage) { + for (const [toolName, usage] of Object.entries(taskToolUsage)) { + const tool = toolName as ToolName + const current = toolTotals.get(tool) ?? 0 + toolTotals.set(tool, current + usage.attempts) + } + } + } + + // Sort by total attempts descending + return Array.from(toolTotals.entries()) + .sort((a, b) => b[1] - a[1]) + .map(([name]): ToolName => name) + // toolUsage ref is stable; usageUpdatedAt triggers recomputation when Map contents change + }, [tasks, toolUsage, usageUpdatedAt]) + + // Compute aggregate stats + const stats = useMemo(() => { + // Reference usageUpdatedAt to trigger recomputation when Map contents change + void usageUpdatedAt + if (!tasks) return null + + const passed = tasks.filter((t) => t.passed === true).length + const failed = tasks.filter((t) => t.passed === false).length + const completed = passed + failed + + let totalTokensIn = 0 + let totalTokensOut = 0 + let totalCost = 0 + let totalDuration = 0 + + // Aggregate tool usage from all tasks (both finished and running) + const toolUsageAggregate: ToolUsage = {} + + for (const task of tasks) { + const metrics = taskMetrics[task.id] + if (metrics) { + totalTokensIn += metrics.tokensIn + totalTokensOut += metrics.tokensOut + totalCost += metrics.cost + totalDuration += metrics.duration + } + + // Aggregate tool usage: prefer DB values for finished tasks, fall back to streaming values + // This handles race conditions during timeout where DB might not have latest data + const dbToolUsage = task.taskMetrics?.toolUsage + const streamingToolUsage = toolUsage.get(task.id) + const taskToolUsage = task.finishedAt + ? dbToolUsage && Object.keys(dbToolUsage).length > 0 + ? dbToolUsage + : streamingToolUsage + : streamingToolUsage + + if (taskToolUsage) { + for (const [key, usage] of Object.entries(taskToolUsage)) { + const tool = key as keyof ToolUsage + if (!toolUsageAggregate[tool]) { + toolUsageAggregate[tool] = { attempts: 0, failures: 0 } + } + toolUsageAggregate[tool].attempts += usage.attempts + toolUsageAggregate[tool].failures += usage.failures + } + } + } + + const remaining = tasks.length - completed + + return { + passed, + failed, + completed, + remaining, + passRate: completed > 0 ? ((passed / completed) * 100).toFixed(1) : null, + totalTokensIn, + totalTokensOut, + totalCost, + totalDuration, + toolUsage: toolUsageAggregate, + } + // Map refs are stable; usageUpdatedAt triggers recomputation when Map contents change + }, [tasks, taskMetrics, toolUsage, usageUpdatedAt]) + + // Calculate elapsed time (wall-clock time from run creation to completion or now) + const elapsedTime = useMemo(() => { + // Reference usageUpdatedAt to trigger recomputation for live elapsed time updates + void usageUpdatedAt + if (!tasks || tasks.length === 0) return null + + const startTime = new Date(run.createdAt).getTime() + + // If run is complete, find the latest finishedAt from tasks + if (run.taskMetricsId) { + const latestFinish = tasks.reduce((latest, task) => { + if (task.finishedAt) { + const finishTime = new Date(task.finishedAt).getTime() + return finishTime > latest ? finishTime : latest + } + return latest + }, startTime) + return latestFinish - startTime + } + + // If still running, use current time + return Date.now() - startTime + }, [tasks, run.createdAt, run.taskMetricsId, usageUpdatedAt]) + + // Task status categories + type TaskStatusCategory = "failed" | "in_progress" | "passed" | "not_started" + + const getTaskStatusCategory = useCallback( + (task: TaskWithMetrics): TaskStatusCategory => { + if (task.passed === false) return "failed" + if (task.passed === true) return "passed" + // Check streaming data, DB metrics, or startedAt timestamp + const hasStarted = !!task.startedAt || !!tokenUsage.get(task.id) || !!taskMetrics[task.id] + if (hasStarted) return "in_progress" + return "not_started" + }, + [tokenUsage, taskMetrics], + ) + + // Group tasks by status while preserving original index + const groupedTasks = useMemo(() => { + if (!tasks || !groupByStatus) return null + + const groups: Record> = { + failed: [], + in_progress: [], + passed: [], + not_started: [], + } + + tasks.forEach((task, index) => { + const status = getTaskStatusCategory(task) + groups[status].push({ task, originalIndex: index }) + }) + + return groups + }, [tasks, groupByStatus, getTaskStatusCategory]) + + const statusLabels = useMemo( + (): Record => ({ + failed: { label: "Failed", className: "text-red-500", count: groupedTasks?.failed.length ?? 0 }, + in_progress: { + label: "In Progress", + className: "text-yellow-500", + count: groupedTasks?.in_progress.length ?? 0, + }, + passed: { label: "Passed", className: "text-green-500", count: groupedTasks?.passed.length ?? 0 }, + not_started: { + label: "Not Started", + className: "text-muted-foreground", + count: groupedTasks?.not_started.length ?? 0, + }, + }), + [groupedTasks], + ) + + const statusOrder: TaskStatusCategory[] = ["failed", "in_progress", "passed", "not_started"] + + // Helper to render a task row + const renderTaskRow = (task: TaskWithMetrics, originalIndex: number) => { + const hasStarted = !!task.startedAt || !!tokenUsage.get(task.id) || !!taskMetrics[task.id] + return ( + hasStarted && onViewTaskLog(task)}> + + {originalIndex + 1} + + +
+ +
+ + {task.language}/{task.exercise} + {task.iteration > 1 && ( + (#{task.iteration}) + )} + + {hasStarted && ( + + + + + Click to view log + + )} +
+
+
+ {taskMetrics[task.id] ? ( + <> + +
+
{formatTokens(taskMetrics[task.id]!.tokensIn)}
/ +
{formatTokens(taskMetrics[task.id]!.tokensOut)}
+
+
+ + {formatTokens(taskMetrics[task.id]!.tokensContext)} + + {toolColumns.map((toolName) => { + const dbUsage = task.taskMetrics?.toolUsage?.[toolName] + const streamingUsage = toolUsage.get(task.id)?.[toolName] + const usage = task.finishedAt ? (dbUsage ?? streamingUsage) : streamingUsage + + const successRate = + usage && usage.attempts > 0 + ? ((usage.attempts - usage.failures) / usage.attempts) * 100 + : 100 + const rateColor = + successRate === 100 + ? "text-muted-foreground" + : successRate >= 80 + ? "text-yellow-500" + : "text-red-500" + return ( + + {usage ? ( +
+ {usage.attempts} + {formatToolUsageSuccessRate(usage)} +
+ ) : ( + - + )} +
+ ) + })} + + {taskMetrics[task.id]!.duration ? formatDuration(taskMetrics[task.id]!.duration) : "-"} + + + {formatCurrency(taskMetrics[task.id]!.cost)} + + + ) : ( + + )} +
+ ) + } + return ( <>
-
-
-
{run.model}
- {run.description &&
{run.description}
} -
- {!run.taskMetricsId && } -
{!tasks ? ( ) : ( - - - - Exercise - Tokens In / Out - Context - Duration - Cost - - - - {tasks.map((task) => ( - - -
- -
- {task.language}/{task.exercise} + <> + {/* View Toggle */} +
+ + + + + + {groupByStatus ? "Show tasks in run order" : "Group tasks by status"} + + +
+
+ + {stats && ( + + + {/* Provider, Model title and status */} +
+ {run.settings?.apiProvider && ( + + {run.settings.apiProvider} + + )} +
{run.model}
+ + {run.description && ( + + - {run.description} + + )} + {isRunActive && ( + + + + + + Stop all containers for this run + + + )}
- - - {taskMetrics[task.id] ? ( - <> - -
-
{formatTokens(taskMetrics[task.id]!.tokensIn)}
/ -
{formatTokens(taskMetrics[task.id]!.tokensOut)}
+ {/* Main Stats Row */} +
+ {/* Pass Rate / Fail Rate / Remaining % */} +
+
+ + {stats.completed > 0 + ? `${((stats.passed / stats.completed) * 100).toFixed(1)}%` + : "-"} + + / + + {stats.completed > 0 + ? `${((stats.failed / stats.completed) * 100).toFixed(1)}%` + : "-"} + + / + + {tasks.length > 0 + ? `${((stats.remaining / tasks.length) * 100).toFixed(1)}%` + : "-"} + +
+
+ {stats.passed} + {" / "} + {stats.failed} + {" / "} + {stats.remaining} + {" of "} + {tasks.length} +
- - - {formatTokens(taskMetrics[task.id]!.tokensContext)} - - - {taskMetrics[task.id]!.duration - ? formatDuration(taskMetrics[task.id]!.duration) - : "-"} - - - {formatCurrency(taskMetrics[task.id]!.cost)} - - - ) : ( - - )} + + {/* Tokens */} +
+
+ {formatTokens(stats.totalTokensIn)} + / + {formatTokens(stats.totalTokensOut)} +
+
Tokens In / Out
+
+ + {/* Cost */} +
+
+ {formatCurrency(stats.totalCost)} +
+
Cost
+
+ + {/* Duration */} +
+
+ {stats.totalDuration > 0 + ? formatDuration(stats.totalDuration) + : "-"} +
+
Duration
+
+ + {/* Elapsed Time */} +
+
+ {elapsedTime !== null ? formatDuration(elapsedTime) : "-"} +
+
Elapsed
+
+ + {/* Estimated Time Remaining - only show if run is active and we have data */} + {!run.taskMetricsId && + elapsedTime !== null && + stats.completed > 0 && + stats.remaining > 0 && ( +
+
+ ~ + {formatDuration( + (elapsedTime / stats.completed) * stats.remaining, + )} +
+
+ Est. Remaining +
+
+ )} +
+ + {/* Tool Usage Row */} + {Object.keys(stats.toolUsage).length > 0 && ( +
+ {Object.entries(stats.toolUsage) + .sort(([, a], [, b]) => b.attempts - a.attempts) + .map(([toolName, usage]) => { + const abbr = getToolAbbreviation(toolName) + const successRate = + usage.attempts > 0 + ? ((usage.attempts - usage.failures) / + usage.attempts) * + 100 + : 100 + const rateColor = + successRate === 100 + ? "text-green-500" + : successRate >= 80 + ? "text-yellow-500" + : "text-red-500" + return ( + + +
+ + {abbr} + + + {usage.attempts} + + + {formatToolUsageSuccessRate(usage)} + +
+
+ + {toolName} + +
+ ) + })} +
+ )} + + + )} + + # + Exercise + Tokens In / Out + Context + {toolColumns.map((toolName) => ( + + + {getToolAbbreviation(toolName)} + {toolName} + + + ))} + Duration + Cost - ))} - -
+ + + {groupByStatus && groupedTasks + ? // Grouped view + statusOrder.map((status) => { + const group = groupedTasks[status] + if (group.length === 0) return null + const { label, className } = statusLabels[status] + return ( + + + + + {label} ({group.length}) + + + + {group.map(({ task, originalIndex }) => + renderTaskRow(task, originalIndex), + )} + + ) + }) + : // Default order view + tasks.map((task, index) => renderTaskRow(task, index))} + + + )}
+ + {/* Task Log Dialog - Full Screen */} + setSelectedTask(null)}> + + +
+ + + {selectedTask?.language}/{selectedTask?.exercise} + {selectedTask?.iteration && selectedTask.iteration > 1 && ( + (#{selectedTask.iteration}) + )} + + ( + {selectedTask?.passed === true + ? "Passed" + : selectedTask?.passed === false + ? "Failed" + : "Running"} + ) + + + {taskLog && ( + + )} +
+
+
+ {isLoadingLog ? ( +
+ +
+ ) : taskLog ? ( + +
+ {formatLogContent(taskLog)} +
+
+ ) : ( +
+ Log file not available (may have been cleared) +
+ )} +
+
+
+ + {/* Kill Run Confirmation Dialog */} + + + + Kill Run? + + This will stop the controller and all task runner containers for this run. Any running tasks + will be terminated immediately. This action cannot be undone. + + + + Cancel + + {isKilling ? ( + <> + + Killing... + + ) : ( + "Kill Run" + )} + + + + ) } diff --git a/apps/web-evals/src/app/runs/new/new-run.tsx b/apps/web-evals/src/app/runs/new/new-run.tsx index 41d35f3c4c..28fb4abfd5 100644 --- a/apps/web-evals/src/app/runs/new/new-run.tsx +++ b/apps/web-evals/src/app/runs/new/new-run.tsx @@ -1,39 +1,65 @@ "use client" -import { useCallback, useRef, useState } from "react" +import { useCallback, useEffect, useMemo, useState } from "react" import { useRouter } from "next/navigation" import { z } from "zod" import { useQuery } from "@tanstack/react-query" import { useForm, FormProvider } from "react-hook-form" import { zodResolver } from "@hookform/resolvers/zod" -import fuzzysort from "fuzzysort" import { toast } from "sonner" -import { X, Rocket, Check, ChevronsUpDown, SlidersHorizontal, CircleCheck } from "lucide-react" +import { + X, + Rocket, + Check, + ChevronsUpDown, + SlidersHorizontal, + Info, + Plus, + Minus, + Terminal, + MonitorPlay, +} from "lucide-react" -import { globalSettingsSchema, providerSettingsSchema, EVALS_SETTINGS, getModelId } from "@roo-code/types" +import { + type ProviderSettings, + type GlobalSettings, + globalSettingsSchema, + providerSettingsSchema, + getModelId, + EVALS_SETTINGS, +} from "@roo-code/types" import { createRun } from "@/actions/runs" import { getExercises } from "@/actions/exercises" + import { - createRunSchema, type CreateRun, - MODEL_DEFAULT, + type ExecutionMethod, + createRunSchema, CONCURRENCY_MIN, CONCURRENCY_MAX, CONCURRENCY_DEFAULT, TIMEOUT_MIN, TIMEOUT_MAX, TIMEOUT_DEFAULT, + ITERATIONS_MIN, + ITERATIONS_MAX, + ITERATIONS_DEFAULT, } from "@/lib/schemas" import { cn } from "@/lib/utils" + import { useOpenRouterModels } from "@/hooks/use-open-router-models" +import { useRooCodeCloudModels } from "@/hooks/use-roo-code-cloud-models" + import { Button, + Checkbox, FormControl, FormField, FormItem, FormLabel, FormMessage, + Input, Textarea, Tabs, TabsList, @@ -48,36 +74,75 @@ import { Popover, PopoverContent, PopoverTrigger, - ScrollArea, - ScrollBar, Slider, + Label, + Tooltip, + TooltipContent, + TooltipTrigger, } from "@/components/ui" import { SettingsDiff } from "./settings-diff" +type ImportedSettings = { + apiConfigs: Record + globalSettings: GlobalSettings + currentApiConfigName: string +} + +type ModelSelection = { + id: string + model: string + popoverOpen: boolean +} + +type ConfigSelection = { + id: string + configName: string + popoverOpen: boolean +} + export function NewRun() { const router = useRouter() - const [mode, setMode] = useState<"openrouter" | "settings">("openrouter") - const [modelSearchValue, setModelSearchValue] = useState("") - const [modelPopoverOpen, setModelPopoverOpen] = useState(false) + const [provider, setModelSource] = useState<"roo" | "openrouter" | "other">("other") + const [executionMethod, setExecutionMethod] = useState("vscode") + const [useNativeToolProtocol, setUseNativeToolProtocol] = useState(true) + const [commandExecutionTimeout, setCommandExecutionTimeout] = useState(20) + const [terminalShellIntegrationTimeout, setTerminalShellIntegrationTimeout] = useState(30) // seconds - const modelSearchResultsRef = useRef>(new Map()) - const modelSearchValueRef = useRef("") + const [modelSelections, setModelSelections] = useState([ + { id: crypto.randomUUID(), model: "", popoverOpen: false }, + ]) + + const [importedSettings, setImportedSettings] = useState(null) + const [configSelections, setConfigSelections] = useState([ + { id: crypto.randomUUID(), configName: "", popoverOpen: false }, + ]) + + const openRouter = useOpenRouterModels() + const rooCodeCloud = useRooCodeCloudModels() + const models = provider === "openrouter" ? openRouter.data : rooCodeCloud.data + const searchValue = provider === "openrouter" ? openRouter.searchValue : rooCodeCloud.searchValue + const setSearchValue = provider === "openrouter" ? openRouter.setSearchValue : rooCodeCloud.setSearchValue + const onFilter = provider === "openrouter" ? openRouter.onFilter : rooCodeCloud.onFilter - const models = useOpenRouterModels() const exercises = useQuery({ queryKey: ["getExercises"], queryFn: () => getExercises() }) + const [selectedExercises, setSelectedExercises] = useState([]) + const form = useForm({ resolver: zodResolver(createRunSchema), defaultValues: { - model: MODEL_DEFAULT, + model: "", description: "", suite: "full", exercises: [], settings: undefined, concurrency: CONCURRENCY_DEFAULT, timeout: TIMEOUT_DEFAULT, + iterations: ITERATIONS_DEFAULT, + jobToken: "", + executionMethod: "vscode", }, }) @@ -88,51 +153,287 @@ export function NewRun() { formState: { isSubmitting }, } = form - const [model, suite, settings] = watch(["model", "suite", "settings", "concurrency"]) + const [suite, settings] = watch(["suite", "settings", "concurrency"]) + + useEffect(() => { + const savedConcurrency = localStorage.getItem("evals-concurrency") + + if (savedConcurrency) { + const parsed = parseInt(savedConcurrency, 10) + + if (!isNaN(parsed) && parsed >= CONCURRENCY_MIN && parsed <= CONCURRENCY_MAX) { + setValue("concurrency", parsed) + } + } + + const savedTimeout = localStorage.getItem("evals-timeout") + + if (savedTimeout) { + const parsed = parseInt(savedTimeout, 10) + + if (!isNaN(parsed) && parsed >= TIMEOUT_MIN && parsed <= TIMEOUT_MAX) { + setValue("timeout", parsed) + } + } + + const savedCommandTimeout = localStorage.getItem("evals-command-execution-timeout") + + if (savedCommandTimeout) { + const parsed = parseInt(savedCommandTimeout, 10) + + if (!isNaN(parsed) && parsed >= 20 && parsed <= 60) { + setCommandExecutionTimeout(parsed) + } + } + + const savedShellTimeout = localStorage.getItem("evals-shell-integration-timeout") + + if (savedShellTimeout) { + const parsed = parseInt(savedShellTimeout, 10) + + if (!isNaN(parsed) && parsed >= 30 && parsed <= 60) { + setTerminalShellIntegrationTimeout(parsed) + } + } + + const savedSuite = localStorage.getItem("evals-suite") + + if (savedSuite === "partial") { + setValue("suite", "partial") + const savedExercises = localStorage.getItem("evals-exercises") + if (savedExercises) { + try { + const parsed = JSON.parse(savedExercises) as string[] + if (Array.isArray(parsed)) { + setSelectedExercises(parsed) + setValue("exercises", parsed) + } + } catch { + // Invalid JSON, ignore. + } + } + } + }, [setValue]) + + const languages = useMemo(() => { + if (!exercises.data) { + return [] + } + + const langs = new Set() + + for (const path of exercises.data) { + const lang = path.split("/")[0] + + if (lang) { + langs.add(lang) + } + } + + return Array.from(langs).sort() + }, [exercises.data]) + + const getExercisesForLanguage = useCallback( + (lang: string) => { + if (!exercises.data) { + return [] + } + + return exercises.data.filter((path) => path.startsWith(`${lang}/`)) + }, + [exercises.data], + ) + + const toggleLanguage = useCallback( + (lang: string) => { + const langExercises = getExercisesForLanguage(lang) + const allSelected = langExercises.every((ex) => selectedExercises.includes(ex)) + + let newSelected: string[] + + if (allSelected) { + newSelected = selectedExercises.filter((ex) => !ex.startsWith(`${lang}/`)) + } else { + const existing = new Set(selectedExercises) + + for (const ex of langExercises) { + existing.add(ex) + } + + newSelected = Array.from(existing) + } + + setSelectedExercises(newSelected) + setValue("exercises", newSelected) + localStorage.setItem("evals-exercises", JSON.stringify(newSelected)) + }, + [getExercisesForLanguage, selectedExercises, setValue], + ) + + const isLanguageSelected = useCallback( + (lang: string) => { + const langExercises = getExercisesForLanguage(lang) + return langExercises.length > 0 && langExercises.every((ex) => selectedExercises.includes(ex)) + }, + [getExercisesForLanguage, selectedExercises], + ) + + const isLanguagePartiallySelected = useCallback( + (lang: string) => { + const langExercises = getExercisesForLanguage(lang) + const selectedCount = langExercises.filter((ex) => selectedExercises.includes(ex)).length + return selectedCount > 0 && selectedCount < langExercises.length + }, + [getExercisesForLanguage, selectedExercises], + ) + + const addModelSelection = useCallback(() => { + setModelSelections((prev) => [...prev, { id: crypto.randomUUID(), model: "", popoverOpen: false }]) + }, []) + + const removeModelSelection = useCallback((id: string) => { + setModelSelections((prev) => prev.filter((s) => s.id !== id)) + }, []) + + const updateModelSelection = useCallback( + (id: string, model: string) => { + setModelSelections((prev) => prev.map((s) => (s.id === id ? { ...s, model, popoverOpen: false } : s))) + // Also set the form model field for validation (use first non-empty model). + setValue("model", model) + }, + [setValue], + ) + + const toggleModelPopover = useCallback((id: string, open: boolean) => { + setModelSelections((prev) => prev.map((s) => (s.id === id ? { ...s, popoverOpen: open } : s))) + }, []) + + const addConfigSelection = useCallback(() => { + setConfigSelections((prev) => [...prev, { id: crypto.randomUUID(), configName: "", popoverOpen: false }]) + }, []) + + const removeConfigSelection = useCallback((id: string) => { + setConfigSelections((prev) => prev.filter((s) => s.id !== id)) + }, []) + + const updateConfigSelection = useCallback( + (id: string, configName: string) => { + setConfigSelections((prev) => prev.map((s) => (s.id === id ? { ...s, configName, popoverOpen: false } : s))) + + // Also update the form settings for the first config (for validation). + if (importedSettings) { + const providerSettings = importedSettings.apiConfigs[configName] ?? {} + setValue("model", getModelId(providerSettings) ?? "") + setValue("settings", { ...EVALS_SETTINGS, ...providerSettings, ...importedSettings.globalSettings }) + } + }, + [importedSettings, setValue], + ) + + const toggleConfigPopover = useCallback((id: string, open: boolean) => { + setConfigSelections((prev) => prev.map((s) => (s.id === id ? { ...s, popoverOpen: open } : s))) + }, []) const onSubmit = useCallback( async (values: CreateRun) => { try { - if (mode === "openrouter") { - values.settings = { ...(values.settings || {}), openRouterModelId: model } + if (provider === "roo" && !values.jobToken?.trim()) { + toast.error("Roo Code Cloud Token is required") + return } - const { id } = await createRun(values) - router.push(`/runs/${id}`) + const selectionsToLaunch: { model: string; configName?: string }[] = [] + + if (provider === "other") { + for (const config of configSelections) { + if (config.configName) { + selectionsToLaunch.push({ model: "", configName: config.configName }) + } + } + } else { + for (const selection of modelSelections) { + if (selection.model) { + selectionsToLaunch.push({ model: selection.model }) + } + } + } + + if (selectionsToLaunch.length === 0) { + toast.error("Please select at least one model or config") + return + } + + const totalRuns = selectionsToLaunch.length + toast.info(totalRuns > 1 ? `Launching ${totalRuns} runs (every 20 seconds)...` : "Launching run...") + + for (let i = 0; i < selectionsToLaunch.length; i++) { + const selection = selectionsToLaunch[i]! + + // Wait 20 seconds between runs (except for the first one). + if (i > 0) { + await new Promise((resolve) => setTimeout(resolve, 20_000)) + } + + const runValues = { ...values } + runValues.executionMethod = executionMethod + + if (provider === "openrouter") { + runValues.model = selection.model + runValues.settings = { + ...(runValues.settings || {}), + apiProvider: "openrouter", + openRouterModelId: selection.model, + toolProtocol: useNativeToolProtocol ? "native" : "xml", + commandExecutionTimeout, + terminalShellIntegrationTimeout: terminalShellIntegrationTimeout * 1000, + } + } else if (provider === "roo") { + runValues.model = selection.model + runValues.settings = { + ...(runValues.settings || {}), + apiProvider: "roo", + apiModelId: selection.model, + toolProtocol: useNativeToolProtocol ? "native" : "xml", + commandExecutionTimeout, + terminalShellIntegrationTimeout: terminalShellIntegrationTimeout * 1000, + } + } else if (provider === "other" && selection.configName && importedSettings) { + const providerSettings = importedSettings.apiConfigs[selection.configName] ?? {} + runValues.model = getModelId(providerSettings) ?? "" + runValues.settings = { + ...EVALS_SETTINGS, + ...providerSettings, + ...importedSettings.globalSettings, + toolProtocol: useNativeToolProtocol ? "native" : "xml", + commandExecutionTimeout, + terminalShellIntegrationTimeout: terminalShellIntegrationTimeout * 1000, + } + } + + try { + await createRun(runValues) + toast.success(`Run ${i + 1}/${totalRuns} launched`) + } catch (e) { + toast.error(`Run ${i + 1} failed: ${e instanceof Error ? e.message : "Unknown error"}`) + } + } + + router.push("/") } catch (e) { toast.error(e instanceof Error ? e.message : "An unknown error occurred.") } }, - [mode, model, router], - ) - - const onFilterModels = useCallback( - (value: string, search: string) => { - if (modelSearchValueRef.current !== search) { - modelSearchValueRef.current = search - modelSearchResultsRef.current.clear() - - for (const { - obj: { id }, - score, - } of fuzzysort.go(search, models.data || [], { - key: "name", - })) { - modelSearchResultsRef.current.set(id, score) - } - } - - return modelSearchResultsRef.current.get(value) ?? 0 - }, - [models.data], - ) - - const onSelectModel = useCallback( - (model: string) => { - setValue("model", model) - setModelPopoverOpen(false) - }, - [setValue], + [ + provider, + executionMethod, + modelSelections, + configSelections, + importedSettings, + router, + useNativeToolProtocol, + commandExecutionTimeout, + terminalShellIntegrationTimeout, + ], ) const onImportSettings = useCallback( @@ -156,11 +457,18 @@ export function NewRun() { }) .parse(JSON.parse(await file.text())) - const providerSettings = providerProfiles.apiConfigs[providerProfiles.currentApiConfigName] ?? {} + setImportedSettings({ + apiConfigs: providerProfiles.apiConfigs, + globalSettings, + currentApiConfigName: providerProfiles.currentApiConfigName, + }) + const defaultConfigName = providerProfiles.currentApiConfigName + setConfigSelections([{ id: crypto.randomUUID(), configName: defaultConfigName, popoverOpen: false }]) + + const providerSettings = providerProfiles.apiConfigs[defaultConfigName] ?? {} setValue("model", getModelId(providerSettings) ?? "") setValue("settings", { ...EVALS_SETTINGS, ...providerSettings, ...globalSettings }) - setMode("settings") event.target.value = "" } catch (e) { @@ -177,97 +485,291 @@ export function NewRun() {
-
- {mode === "openrouter" && ( - ( - - - - - - - - - - No model found. - - {models.data?.map(({ id, name }) => ( - - {name} - - - ))} - - - - - - - - )} - /> - )} + ( + + setModelSource(value as "roo" | "openrouter" | "other")}> + + Import + Roo Code Cloud + OpenRouter + + - - - - {settings && ( - - <> -
- -
- Imported valid Roo Code settings. Showing differences from default - settings. + {provider === "other" ? ( +
+ + + + {importedSettings && Object.keys(importedSettings.apiConfigs).length > 0 && ( +
+ + {configSelections.map((selection, index) => ( +
+ + toggleConfigPopover(selection.id, open) + }> + + + + + + + + No config found. + + {Object.keys( + importedSettings.apiConfigs, + ).map((configName) => ( + + updateConfigSelection( + selection.id, + configName, + ) + }> + {configName} + {configName === + importedSettings.currentApiConfigName && ( + + (default) + + )} + + + ))} + + + + + + {index === configSelections.length - 1 ? ( + + ) : ( + + )} +
+ ))} +
+ )} + +
+ +
+ +
+
+ + {settings && ( + + )} +
+ ) : ( + <> +
+ {modelSelections.map((selection, index) => ( +
+ toggleModelPopover(selection.id, open)}> + + + + + + + + No model found. + + {models?.map(({ id, name }) => ( + + updateModelSelection( + selection.id, + id, + ) + }> + {name} + + + ))} + + + + + + {index === modelSelections.length - 1 ? ( + + ) : ( + + )} +
+ ))} +
+ +
+ +
+
- - - + )} + + + + )} + /> + + {provider === "roo" && ( + ( + +
+ Roo Code Cloud Token + + + + + +

+ If you have access to the Roo Code Cloud repository and the + decryption key for the .env.* files, generate a token with: +

+ + pnpm --filter @roo-code-cloud/auth production:create-auth-token + [email] [org] [ttl] + +
+
+
+ + + + +
)} - - -
+ /> + )} ( Exercises - setValue("suite", value as "full" | "partial")}> - - All - Some - - +
+ { + setValue("suite", value as "full" | "partial") + localStorage.setItem("evals-suite", value) + if (value === "full") { + setSelectedExercises([]) + setValue("exercises", []) + localStorage.removeItem("evals-exercises") + } + }}> + + All + Some + + + {suite === "partial" && languages.length > 0 && ( +
+ {languages.map((lang) => ( + + ))} +
+ )} +
{suite === "partial" && ( ({ value: path, label: path })) || []} - onValueChange={(value) => setValue("exercises", value)} + value={selectedExercises} + onValueChange={(value) => { + setSelectedExercises(value) + setValue("exercises", value) + localStorage.setItem("evals-exercises", JSON.stringify(value)) + }} placeholder="Select" variant="inverted" maxCount={4} @@ -297,47 +835,179 @@ export function NewRun() { )} /> - ( - - Concurrency - -
- field.onChange(value[0])} - /> -
{field.value}
-
-
- -
- )} - /> + {/* Concurrency, Timeout, and Iterations in a 3-column row */} +
+ ( + + Concurrency + +
+ { + field.onChange(value[0]) + localStorage.setItem("evals-concurrency", String(value[0])) + }} + /> +
{field.value}
+
+
+ +
+ )} + /> + ( + + Timeout (Minutes) + +
+ { + field.onChange(value[0]) + localStorage.setItem("evals-timeout", String(value[0])) + }} + /> +
{field.value}
+
+
+ +
+ )} + /> + + ( + + Iterations + +
+ { + field.onChange(value[0]) + }} + /> +
{field.value}
+
+
+ +
+ )} + /> +
+ + {/* Terminal timeouts in a 2-column row */} +
+ +
+ + + + + + +

+ Maximum time in seconds to wait for terminal command execution to complete + before timing out. This applies to commands run via the execute_command + tool. +

+
+
+
+
+ { + if (value !== undefined) { + setCommandExecutionTimeout(value) + localStorage.setItem("evals-command-execution-timeout", String(value)) + } + }} + /> +
{commandExecutionTimeout}
+
+
+ + +
+ + + + + + +

+ Maximum time in seconds to wait for shell integration to initialize when + opening a new terminal. +

+
+
+
+
+ { + if (value !== undefined) { + setTerminalShellIntegrationTimeout(value) + localStorage.setItem("evals-shell-integration-timeout", String(value)) + } + }} + /> +
{terminalShellIntegrationTimeout}
+
+
+
+ + {/* Execution Method */} ( + name="executionMethod" + render={() => ( - Timeout (Minutes) - -
- field.onChange(value[0])} - /> -
{field.value}
-
-
+ Execution Method + { + const newExecutionMethod = value as ExecutionMethod + setExecutionMethod(newExecutionMethod) + setValue("executionMethod", newExecutionMethod) + }}> + + + + VSCode + + + + CLI + + +
)} diff --git a/apps/web-evals/src/app/runs/new/settings-diff.tsx b/apps/web-evals/src/app/runs/new/settings-diff.tsx index e4a389ba8d..2761493d6b 100644 --- a/apps/web-evals/src/app/runs/new/settings-diff.tsx +++ b/apps/web-evals/src/app/runs/new/settings-diff.tsx @@ -1,12 +1,12 @@ -import { Fragment, HTMLAttributes } from "react" - import { type Keys, type RooCodeSettings, GLOBAL_SETTINGS_KEYS, PROVIDER_SETTINGS_KEYS } from "@roo-code/types" -import { cn } from "@/lib/utils" +import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui" -export const ROO_CODE_SETTINGS_KEYS = [...GLOBAL_SETTINGS_KEYS, ...PROVIDER_SETTINGS_KEYS] as Keys[] +export const ROO_CODE_SETTINGS_KEYS = [ + ...new Set([...GLOBAL_SETTINGS_KEYS, ...PROVIDER_SETTINGS_KEYS]), +] as Keys[] -type SettingsDiffProps = HTMLAttributes & { +type SettingsDiffProps = { defaultSettings: RooCodeSettings customSettings: RooCodeSettings } @@ -14,53 +14,45 @@ type SettingsDiffProps = HTMLAttributes & { export function SettingsDiff({ customSettings: { experiments: customExperiments, ...customSettings }, defaultSettings: { experiments: defaultExperiments, ...defaultSettings }, - className, - ...props }: SettingsDiffProps) { const defaults = { ...defaultSettings, ...defaultExperiments } const custom = { ...customSettings, ...customExperiments } return ( -
-
Setting
-
Default
-
Custom
- {ROO_CODE_SETTINGS_KEYS.map((key) => { - const defaultValue = defaults[key as keyof typeof defaults] - const customValue = custom[key as keyof typeof custom] - const isDefault = JSON.stringify(defaultValue) === JSON.stringify(customValue) +
+ + + + Setting + Default + Custom + + + + {ROO_CODE_SETTINGS_KEYS.map((key) => { + const defaultValue = JSON.stringify(defaults[key as keyof typeof defaults], null, 2) + const customValue = JSON.stringify(custom[key as keyof typeof custom], null, 2) - return isDefault ? null : ( - - ) - })} + return defaultValue === customValue || + (isEmpty(defaultValue) && isEmpty(customValue)) ? null : ( + + + {key} + + + {defaultValue} + + + {customValue} + + + ) + })} + +
) } -type SettingDiffProps = HTMLAttributes & { - name: string - defaultValue?: string - customValue?: string -} - -export function SettingDiff({ name, defaultValue, customValue, ...props }: SettingDiffProps) { - return ( - -
- {name} -
-
-				{defaultValue}
-			
-
-				{customValue}
-			
-
- ) -} +const isEmpty = (value: string | undefined) => + value === undefined || value === "" || value === "null" || value === '""' || value === "[]" || value === "{}" diff --git a/apps/web-evals/src/components/home/run.tsx b/apps/web-evals/src/components/home/run.tsx index c35673885c..379daf48a4 100644 --- a/apps/web-evals/src/components/home/run.tsx +++ b/apps/web-evals/src/components/home/run.tsx @@ -1,16 +1,29 @@ import { useCallback, useState, useRef } from "react" import Link from "next/link" -import { Ellipsis, ClipboardList, Copy, Check, LoaderCircle, Trash } from "lucide-react" +import { useRouter } from "next/navigation" +import { toast } from "sonner" +import { Ellipsis, ClipboardList, Copy, Check, LoaderCircle, Trash, Settings, FileDown, StickyNote } from "lucide-react" import type { Run as EvalsRun, TaskMetrics as EvalsTaskMetrics } from "@roo-code/evals" +import type { ToolName } from "@roo-code/types" -import { deleteRun } from "@/actions/runs" -import { formatCurrency, formatDuration, formatTokens, formatToolUsageSuccessRate } from "@/lib/formatters" +import { deleteRun, updateRunDescription } from "@/actions/runs" +import { + formatCurrency, + formatDateTime, + formatDuration, + formatTokens, + formatToolUsageSuccessRate, +} from "@/lib/formatters" import { useCopyRun } from "@/hooks/use-copy-run" import { Button, TableCell, TableRow, + Textarea, + Tooltip, + TooltipContent, + TooltipTrigger, DropdownMenu, DropdownMenuContent, DropdownMenuItem, @@ -23,18 +36,97 @@ import { AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, + Dialog, + DialogContent, + DialogFooter, + DialogHeader, + DialogTitle, + ScrollArea, } from "@/components/ui" +// Tool group type (same as in runs.tsx) +type ToolGroup = { + id: string + name: string + icon: string + tools: string[] +} + type RunProps = { run: EvalsRun taskMetrics: EvalsTaskMetrics | null + toolColumns: ToolName[] + toolGroups: ToolGroup[] } -export function Run({ run, taskMetrics }: RunProps) { +export function Run({ run, taskMetrics, toolColumns, toolGroups }: RunProps) { + const router = useRouter() const [deleteRunId, setDeleteRunId] = useState() + const [showSettings, setShowSettings] = useState(false) + const [isExportingLogs, setIsExportingLogs] = useState(false) + const [showNotesDialog, setShowNotesDialog] = useState(false) + const [editingDescription, setEditingDescription] = useState(run.description ?? "") + const [isSavingNotes, setIsSavingNotes] = useState(false) const continueRef = useRef(null) const { isPending, copyRun, copied } = useCopyRun(run.id) + const hasDescription = Boolean(run.description && run.description.trim().length > 0) + + const handleSaveDescription = useCallback(async () => { + setIsSavingNotes(true) + try { + const result = await updateRunDescription(run.id, editingDescription.trim() || null) + if (result.success) { + toast.success("Description saved") + setShowNotesDialog(false) + router.refresh() + } else { + toast.error("Failed to save description") + } + } catch (error) { + console.error("Error saving description:", error) + toast.error("Failed to save description") + } finally { + setIsSavingNotes(false) + } + }, [run.id, editingDescription, router]) + + const onExportFailedLogs = useCallback(async () => { + if (run.failed === 0) { + toast.error("No failed tasks to export") + return + } + + setIsExportingLogs(true) + try { + const response = await fetch(`/api/runs/${run.id}/logs/failed`) + + if (!response.ok) { + const error = await response.json() + toast.error(error.error || "Failed to export logs") + return + } + + // Download the zip file + const blob = await response.blob() + const url = window.URL.createObjectURL(blob) + const a = document.createElement("a") + a.href = url + a.download = `run-${run.id}-failed-logs.zip` + document.body.appendChild(a) + a.click() + window.URL.revokeObjectURL(url) + document.body.removeChild(a) + + toast.success("Failed logs exported successfully") + } catch (error) { + console.error("Error exporting logs:", error) + toast.error("Failed to export logs") + } finally { + setIsExportingLogs(false) + } + }, [run.id, run.failed]) + const onConfirmDelete = useCallback(async () => { if (!deleteRunId) { return @@ -48,86 +140,233 @@ export function Run({ run, taskMetrics }: RunProps) { } }, [deleteRunId]) + const handleRowClick = useCallback( + (e: React.MouseEvent) => { + // Don't navigate if clicking on the dropdown menu + if ((e.target as HTMLElement).closest("[data-dropdown-trigger]")) { + return + } + router.push(`/runs/${run.id}`) + }, + [router, run.id], + ) + + // Helper to render a tool group cell + const renderToolGroupCell = (group: ToolGroup) => { + if (!taskMetrics?.toolUsage) { + return - + } + + let totalAttempts = 0 + let totalFailures = 0 + const breakdown: Array<{ tool: string; attempts: number; rate: string }> = [] + + for (const toolName of group.tools) { + const usage = taskMetrics.toolUsage[toolName as ToolName] + if (usage) { + totalAttempts += usage.attempts + totalFailures += usage.failures + const rate = + usage.attempts > 0 + ? `${Math.round(((usage.attempts - usage.failures) / usage.attempts) * 100)}%` + : "0%" + breakdown.push({ tool: toolName, attempts: usage.attempts, rate }) + } + } + + if (totalAttempts === 0) { + return - + } + + const successRate = ((totalAttempts - totalFailures) / totalAttempts) * 100 + const rateColor = + successRate === 100 ? "text-muted-foreground" : successRate >= 80 ? "text-yellow-500" : "text-red-500" + + return ( + + +
+ {totalAttempts} + {Math.round(successRate)}% +
+
+ +
+
{group.name}
+ {breakdown.map(({ tool, attempts, rate }) => ( +
+ {tool}: + + {attempts} ({rate}) + +
+ ))} +
+
+
+ ) + } + return ( <> - - {run.model} + + {run.model} + {run.settings?.apiProvider ?? "-"} + + {formatDateTime(run.createdAt)} + {run.passed} {run.failed} - {run.passed + run.failed > 0 && ( - {((run.passed / (run.passed + run.failed)) * 100).toFixed(1)}% - )} + {run.passed + run.failed > 0 && + (() => { + const percent = (run.passed / (run.passed + run.failed)) * 100 + const colorClass = + percent === 100 ? "text-green-500" : percent >= 80 ? "text-yellow-500" : "text-red-500" + return {percent.toFixed(1)}% + })()} {taskMetrics && ( -
-
{formatTokens(taskMetrics.tokensIn)}
/ -
{formatTokens(taskMetrics.tokensOut)}
-
- )} -
- - {taskMetrics?.toolUsage?.apply_diff && ( -
-
{taskMetrics.toolUsage.apply_diff.attempts}
-
/
-
{formatToolUsageSuccessRate(taskMetrics.toolUsage.apply_diff)}
+
+ {formatTokens(taskMetrics.tokensIn)}/ + {formatTokens(taskMetrics.tokensOut)}
)} + {/* Tool Group Columns */} + {toolGroups.map((group) => ( + + {renderToolGroupCell(group)} + + ))} + {toolColumns.map((toolName) => { + const usage = taskMetrics?.toolUsage?.[toolName] + const successRate = + usage && usage.attempts > 0 ? ((usage.attempts - usage.failures) / usage.attempts) * 100 : 100 + const rateColor = + successRate === 100 + ? "text-muted-foreground" + : successRate >= 80 + ? "text-yellow-500" + : "text-red-500" + return ( + + {usage ? ( +
+ {usage.attempts} + {formatToolUsageSuccessRate(usage)} +
+ ) : ( + - + )} +
+ ) + })} {taskMetrics && formatCurrency(taskMetrics.cost)} {taskMetrics && formatDuration(taskMetrics.duration)} - - - - - - + e.stopPropagation()}> +
+ {/* Note Icon */} + + + + + + {hasDescription ? ( +
{run.description}
+ ) : ( +
No description. Click to add one.
+ )} +
+
+ + {/* More Actions Menu */} + + + + + +
+ +
View Tasks
+
+ +
+ {run.settings && ( + setShowSettings(true)}> +
+ +
View Settings
+
+
+ )} + {run.taskMetricsId && ( + copyRun()} disabled={isPending || copied}> +
+ {isPending ? ( + <> + + Copying... + + ) : copied ? ( + <> + + Copied! + + ) : ( + <> + + Copy to Production + + )} +
+
+ )} + {run.failed > 0 && ( + +
+ {isExportingLogs ? ( + <> + + Exporting... + + ) : ( + <> + + Export Failed Logs + + )} +
+
+ )} + { + setDeleteRunId(run.id) + setTimeout(() => continueRef.current?.focus(), 0) + }}>
- -
View Tasks
-
- -
- {run.taskMetricsId && ( - copyRun()} disabled={isPending || copied}> -
- {isPending ? ( - <> - - Copying... - - ) : copied ? ( - <> - - Copied! - - ) : ( - <> - - Copy to Production - - )} + +
Delete
- )} - { - setDeleteRunId(run.id) - setTimeout(() => continueRef.current?.focus(), 0) - }}> -
- -
Delete
-
-
-
-
+ + +
setDeleteRunId(undefined)}> @@ -144,6 +383,51 @@ export function Run({ run, taskMetrics }: RunProps) { + + + + Run Settings + + +
+							{JSON.stringify(run.settings, null, 2)}
+						
+
+
+
+ + {/* Notes/Description Dialog */} + + + + Run Description + +
+