diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..88c989193 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,10 @@ +# Dependencies (npm ci installs fresh inside the container) +node_modules +**/node_modules + +# Build artifacts (rebuilt from scratch inside the container) +dist +**/dist + +# Version control +.git diff --git a/.github/workflows/release-sdk.yml b/.github/workflows/release-sdk.yml index ccdc24b77..c7e2e3619 100644 --- a/.github/workflows/release-sdk.yml +++ b/.github/workflows/release-sdk.yml @@ -348,15 +348,32 @@ jobs: CLI_SOURCE_DESC="CLI built from source (same branch/ref as SDK)" fi - # Create release notes with CLI version info - NOTES="## Bundled CLI Version\n\nThis SDK release bundles CLI version: \`${CLI_VERSION}\`\n\nSource: ${CLI_SOURCE_DESC}\n\n---\n\n" + # Create release notes file + NOTES_FILE=$(mktemp) + { + echo "## Bundled CLI Version" + echo "" + echo "This SDK release bundles CLI version: ${CLI_VERSION}" + echo "" + echo "Source: ${CLI_SOURCE_DESC}" + echo "" + echo "---" + echo "" + } > "${NOTES_FILE}" + # Get previous release notes if available + PREVIOUS_NOTES=$(gh release view "sdk-typescript-${PREVIOUS_RELEASE_TAG}" --json body -q '.body' 2>/dev/null || echo 'See commit history for changes.') + printf '%s\n' "${PREVIOUS_NOTES}" >> "${NOTES_FILE}" + + # Create GitHub release gh release create "sdk-typescript-${RELEASE_TAG}" \ --target "${TARGET}" \ --title "SDK TypeScript Release ${RELEASE_TAG}" \ - --notes-start-tag "sdk-typescript-${PREVIOUS_RELEASE_TAG}" \ - --notes "${NOTES}$(gh release view "sdk-typescript-${PREVIOUS_RELEASE_TAG}" --json body -q '.body' 2>/dev/null || echo 'See commit history for changes.')" \ - "${PRERELEASE_FLAG}" + --notes-file "${NOTES_FILE}" \ + ${PRERELEASE_FLAG} + + # Cleanup + rm -f "${NOTES_FILE}" - name: 'Create PR to merge release branch into main' if: |- diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ffcda3dc0..617cf9553 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -206,13 +206,22 @@ jobs: RELEASE_BRANCH: '${{ steps.release_branch.outputs.BRANCH_NAME }}' RELEASE_TAG: '${{ steps.version.outputs.RELEASE_TAG }}' PREVIOUS_RELEASE_TAG: '${{ steps.version.outputs.PREVIOUS_RELEASE_TAG }}' + IS_NIGHTLY: '${{ steps.vars.outputs.is_nightly }}' + IS_PREVIEW: '${{ steps.vars.outputs.is_preview }}' run: |- + # Set prerelease flag for nightly and preview releases + PRERELEASE_FLAG="" + if [[ "${IS_NIGHTLY}" == "true" || "${IS_PREVIEW}" == "true" ]]; then + PRERELEASE_FLAG="--prerelease" + fi + gh release create "${RELEASE_TAG}" \ dist/cli.js \ --target "$RELEASE_BRANCH" \ --title "Release ${RELEASE_TAG}" \ --notes-start-tag "$PREVIOUS_RELEASE_TAG" \ - --generate-notes + --generate-notes \ + ${PRERELEASE_FLAG} - name: 'Create Issue on Failure' if: |- diff --git a/.gitignore b/.gitignore index a923e9bc1..27e0ab904 100644 --- a/.gitignore +++ b/.gitignore @@ -47,11 +47,15 @@ packages/*/coverage/ # Generated files packages/cli/src/generated/ packages/core/src/generated/ +packages/web-templates/src/generated/ .integration-tests/ packages/vscode-ide-companion/*.vsix # Qwen Code Configs + .qwen/ +!.qwen/commands/ +!.qwen/skills/ logs/ # GHA credentials gha-creds-*.json @@ -70,6 +74,8 @@ __pycache__/ integration-tests/concurrent-runner/output/ integration-tests/concurrent-runner/task-* +integration-tests/terminal-capture/scenarios/screenshots/ + # storybook *storybook.log storybook-static diff --git a/.prettierignore b/.prettierignore index 25ed481ad..5e9d79005 100644 --- a/.prettierignore +++ b/.prettierignore @@ -19,3 +19,4 @@ gha-creds-*.json junit.xml Thumbs.db packages/vscode-ide-companion/schemas/settings.schema.json +packages/cli/src/services/insight/templates/insightTemplate.ts diff --git a/.qwen/skills/pr-review/SKILL.md b/.qwen/skills/pr-review/SKILL.md new file mode 100644 index 000000000..52bf75427 --- /dev/null +++ b/.qwen/skills/pr-review/SKILL.md @@ -0,0 +1,104 @@ +--- +name: pr-review +description: Reviews pull requests with code analysis and terminal smoke testing. Applies when examining code changes, running CLI tests, or when 'PR review', 'code review', 'terminal screenshot', 'visual test' is mentioned. +--- + +# PR Review — Code Review + Terminal Smoke Testing + +## Workflow + +### 1. Fetch PR Information + +```bash +# List open PRs +gh pr list + +# View PR details +gh pr view + +# Get diff +gh pr diff +``` + +### 2. Code Review + +Analyze changes across the following dimensions: + +- **Correctness** — Is the logic correct? Are edge cases handled? +- **Code Style** — Does it follow existing code style and conventions? +- **Performance** — Are there any performance concerns? +- **Test Coverage** — Are there corresponding tests for the changes? +- **Security** — Does it introduce any security risks? + +Output format: + +- 🔴 **Critical** — Must fix +- 🟡 **Suggestion** — Suggested improvement +- 🟢 **Nice to have** — Optional optimization + +### 3. Terminal Smoke Testing (Run for Every PR) + +**Run terminal-capture for every PR review**, not just UI changes. Reasons: + +- **Smoke Test** — Verify the CLI starts correctly and responds to user input, ensuring the PR didn't break anything +- **Visual Verification** — If there are UI changes, screenshots provide the most intuitive review evidence +- **Documentation** — Attach screenshots to the PR comments so reviewers can see the results without building locally + +```bash +# Checkout branch & build +gh pr checkout +npm run build +``` + +#### Scenario Selection Strategy + +Choose appropriate scenarios based on the PR's scope of changes: + +| PR Type | Recommended Scenarios | Description | +| ------------------------------------- | ------------------------------------------------------------ | --------------------------------- | +| **Any PR** (default) | smoke test: send `hi`, verify startup & response | Minimal-cost smoke validation | +| Slash command changes | Corresponding command scenarios (`/about`, `/context`, etc.) | Verify command output correctness | +| Ink component / layout changes | Multiple scenarios + full-flow long screenshot | Verify visual effects | +| Large refactors / dependency upgrades | Run `scenarios/all.ts` fully | Full regression | + +#### Running Screenshots + +```bash +# Write scenario config to integration-tests/terminal-capture/scenarios/ +# See terminal-capture skill for FlowStep API reference + +# Single scenario +npx tsx integration-tests/terminal-capture/run.ts integration-tests/terminal-capture/scenarios/.ts + + +# Check output in screenshots/ directory +``` + +#### Minimal Smoke Test Example + +No need to write a new scenario file — just use the existing `about.ts`. It sends "hi" then runs `/about`, covering startup + input + command response: + +```bash +npx tsx integration-tests/terminal-capture/run.ts integration-tests/terminal-capture/scenarios/about.ts +``` + +### 4. Upload Screenshots to PR + +Use Playwright MCP browser to upload screenshots to the PR comments (images hosted at `github.com/user-attachments/assets/`, zero side effects): + +1. Open the PR page with Playwright: `https://github.com//pull/` +2. Click the comment text box and enter a comment title (e.g., `## 📷 Terminal Smoke Test Screenshots`) +3. Click the "Paste, drop, or click to add files" button to trigger the file picker +4. Upload screenshot PNG files via `browser_file_upload` (can upload multiple one by one) +5. Wait for GitHub to process (about 2-3 seconds) — image links auto-insert into the comment box +6. Click the "Comment" button to submit + +> **Prerequisite**: Playwright MCP needs `--user-data-dir` configured to persist GitHub login session. First time use requires manually logging into GitHub in the Playwright browser. + +### 5. Submit Review + +Submit code review comments via `gh pr review`: + +```bash +gh pr review --comment --body "review content" +``` diff --git a/.qwen/skills/terminal-capture/SKILL.md b/.qwen/skills/terminal-capture/SKILL.md new file mode 100644 index 000000000..adf8fff13 --- /dev/null +++ b/.qwen/skills/terminal-capture/SKILL.md @@ -0,0 +1,197 @@ +--- +name: terminal-capture +description: Automates terminal UI screenshot testing for CLI commands. Applies when reviewing PRs that affect CLI output, testing slash commands (/about, /context, /auth, /export), generating visual documentation, or when 'terminal screenshot', 'CLI test', 'visual test', or 'terminal-capture' is mentioned. +--- + +# Terminal Capture — CLI Terminal Screenshot Automation + +Drive terminal interactions and screenshots via TypeScript configuration, used for visual verification during PR reviews. + +## Prerequisites + +Ensure the following dependencies are installed before running: + +```bash +npm install # Install project dependencies (including node-pty, xterm, playwright, etc.) +npx playwright install chromium # Install Playwright browser +``` + +## Architecture + +``` +node-pty (pseudo-terminal) → ANSI byte stream → xterm.js (Playwright headless) → Screenshot +``` + +Core files: + +| File | Purpose | +| -------------------------------------------------------- | ------------------------------------------------------------------------ | +| `integration-tests/terminal-capture/terminal-capture.ts` | Low-level engine (PTY + xterm.js + Playwright) | +| `integration-tests/terminal-capture/scenario-runner.ts` | Scenario executor (parses config, drives interactions, auto-screenshots) | +| `integration-tests/terminal-capture/run.ts` | CLI entry point (batch run scenarios) | +| `integration-tests/terminal-capture/scenarios/*.ts` | Scenario configuration files | + +## Quick Start + +### 1. Write Scenario Configuration + +Create a `.ts` file under `integration-tests/terminal-capture/scenarios/`: + +```typescript +import type { ScenarioConfig } from '../scenario-runner.js'; + +export default { + name: '/about', + spawn: ['node', 'dist/cli.js', '--yolo'], + terminal: { title: 'qwen-code', cwd: '../../..' }, // Relative to this config file's location + flow: [ + { type: 'Hi, can you help me understand this codebase?' }, + { type: '/about' }, + ], +} satisfies ScenarioConfig; +``` + +### 2. Run + +```bash +# Single scenario +npx tsx integration-tests/terminal-capture/run.ts integration-tests/terminal-capture/scenarios/about.ts + +# Batch (entire directory) +npx tsx integration-tests/terminal-capture/run.ts integration-tests/terminal-capture/scenarios/ +``` + +### 3. Output + +Screenshots are saved to `integration-tests/terminal-capture/scenarios/screenshots/{name}/`: + +| File | Description | +| --------------- | ---------------------------------- | +| `01-01.png` | Step 1 input state | +| `01-02.png` | Step 1 execution result | +| `02-01.png` | Step 2 input state | +| `02-02.png` | Step 2 execution result | +| `full-flow.png` | Final state full-length screenshot | + +## FlowStep API + +Each flow step can contain the following fields: + +### `type: string` — Input Text + +Automatic behavior: Input text → Screenshot (01) → Press Enter → Wait for output to stabilize → Screenshot (02). + +```typescript +{ + type: 'Hello'; +} // Plain text +{ + type: '/about'; +} // Slash command (auto-completion handled automatically) +``` + +**Special rule**: If the next step is `key`, do not auto-press Enter (hand over control to the key sequence). + +### `key: string | string[]` — Send Key Press + +Used for menu selection, Tab completion, and other interactions. Does not auto-press Enter or auto-screenshot. + +Supported key names: `ArrowUp`, `ArrowDown`, `ArrowLeft`, `ArrowRight`, `Enter`, `Tab`, `Escape`, `Backspace`, `Space`, `Home`, `End`, `PageUp`, `PageDown`, `Delete` + +```typescript +{ + key: 'ArrowDown'; +} // Single key +{ + key: ['ArrowDown', 'ArrowDown', 'Enter']; +} // Multiple keys +``` + +Auto-screenshot is triggered after the key sequence ends (when the next step is not a `key`). + +### `capture` / `captureFull` — Explicit Screenshot + +Use as a standalone step, or override automatic naming: + +```typescript +{ + capture: 'initial.png'; +} // Screenshot current viewport only +{ + captureFull: 'all-output.png'; +} // Screenshot full scrollback buffer +``` + +## Scenario Examples + +### Basic: Input + Command + +```typescript +flow: [{ type: 'explain this project' }, { type: '/about' }]; +``` + +### Secondary Menu Selection (/auth) + +```typescript +flow: [ + { type: '/auth' }, + { key: 'ArrowDown' }, // Select API Key option + { key: 'Enter' }, // Confirm + { type: 'sk-xxx' }, // Input API key +]; +``` + +### Tab Completion Selection (/export) + +```typescript +flow: [ + { type: 'Tell me about yourself' }, + { type: '/export' }, // No auto-Enter (next step is key) + { key: 'Tab' }, // Pop format selection + { key: 'ArrowDown' }, // Select format + { key: 'Enter' }, // Confirm → auto-screenshot +]; +``` + +### Array Batch (Multiple Scenarios in One File) + +```typescript +export default [ + { name: '/about', spawn: [...], flow: [...] }, + { name: '/context', spawn: [...], flow: [...] }, +] satisfies ScenarioConfig[]; +``` + +## Integration with PR Review + +This tool is commonly used for visual verification during PR reviews. For the complete code review + screenshot workflow, see the [pr-review](../pr-review/SKILL.md) skill. + +## Troubleshooting + +| Issue | Cause | Solution | +| ------------------------------------ | ------------------------------------- | ---------------------------------------------------- | +| Playwright error `browser not found` | Browser not installed | `npx playwright install chromium` | +| Blank screenshot | Process starts slowly or build failed | Ensure `npm run build` succeeds, check spawn command | +| PTY-related errors | node-pty native module not compiled | `npm rebuild node-pty` | +| Unstable screenshot output | Terminal output not fully rendered | Check if the scenario needs additional wait time | + +## Full ScenarioConfig Type + +```typescript +interface ScenarioConfig { + name: string; // Scenario name (also used as screenshot subdirectory name) + spawn: string[]; // Launch command ["node", "dist/cli.js", "--yolo"] + flow: FlowStep[]; // Interaction steps + terminal?: { + // Terminal configuration (all optional) + cols?: number; // Number of columns, default 100 + rows?: number; // Number of rows, default 28 + theme?: string; // Theme: dracula|one-dark|github-dark|monokai|night-owl + chrome?: boolean; // macOS window decorations, default true + title?: string; // Window title, default "Terminal" + fontSize?: number; // Font size + cwd?: string; // Working directory (relative to config file) + }; + outputDir?: string; // Screenshot output directory (relative to config file) +} +``` diff --git a/.vscode/launch.json b/.vscode/launch.json index bab4f22e0..5d5db1d63 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -127,6 +127,19 @@ "cwd": "${workspaceFolder}", "console": "integratedTerminal", "env": {} + }, + { + "type": "node", + "request": "launch", + "name": "Dev Launch CLI", + "runtimeExecutable": "npm", + "runtimeArgs": ["run", "dev"], + "skipFiles": ["/**"], + "cwd": "${workspaceFolder}", + "console": "integratedTerminal", + "env": { + "GEMINI_SANDBOX": "false" + } } ], "inputs": [ diff --git a/Dockerfile b/Dockerfile index 52a4d4416..6d30d6ad3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,12 +19,12 @@ ENV PATH=$PATH:/usr/local/share/npm-global/bin COPY . /home/node/app WORKDIR /home/node/app -# Install dependencies and build packages -# Use scripts/build.js which handles workspace dependencies in correct order +# Install dependencies, build workspaces, bundle into a single distributable, and pack RUN npm ci \ && npm run build \ - && npm pack -w @qwen-code/qwen-code --pack-destination ./packages/cli/dist \ - && npm pack -w @qwen-code/qwen-code-core --pack-destination ./packages/core/dist + && npm run bundle \ + && npm run prepare:package \ + && cd dist && npm pack # Runtime stage FROM docker.io/library/node:20-slim @@ -61,9 +61,8 @@ RUN mkdir -p /usr/local/share/npm-global ENV NPM_CONFIG_PREFIX=/usr/local/share/npm-global ENV PATH=$PATH:/usr/local/share/npm-global/bin -# Copy built packages from builder stage -COPY --from=builder /home/node/app/packages/cli/dist/*.tgz /tmp/ -COPY --from=builder /home/node/app/packages/core/dist/*.tgz /tmp/ +# Copy bundled package from builder stage +COPY --from=builder /home/node/app/dist/*.tgz /tmp/ # Install built packages globally RUN npm install -g /tmp/*.tgz \ diff --git a/README.md b/README.md index 4c9d28179..ab598666c 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,8 @@ +> 🎉 **News (2026-02-16)**: Qwen3.5-Plus is now live! Sign in via Qwen OAuth to use it directly, or get an API key from [Alibaba Cloud ModelStudio](https://modelstudio.console.alibabacloud.com?tab=doc#/doc/?type=model&url=2840914_2&modelId=group-qwen3.5-plus) to access it through the OpenAI-compatible API. + Qwen Code is an open-source AI agent for the terminal, optimized for [Qwen3-Coder](https://github.com/QwenLM/Qwen3-Coder). It helps you understand large codebases, automate tedious work, and ship faster. ![](https://gw.alicdn.com/imgextra/i1/O1CN01D2DviS1wwtEtMwIzJ_!!6000000006373-2-tps-1600-900.png) @@ -123,7 +125,231 @@ Use this if you want more flexibility over which provider and model to use. Supp - **Anthropic**: Claude models - **Google GenAI**: Gemini models -For full details (including `modelProviders` configuration, `.env` file loading, environment variable priorities, and security notes), see the [authentication guide](https://qwenlm.github.io/qwen-code-docs/en/users/configuration/auth/). +The **recommended** way to configure models and providers is by editing `~/.qwen/settings.json` (create it if it doesn't exist). This file lets you define all available models, API keys, and default settings in one place. + +##### Quick Setup in 3 Steps + +**Step 1:** Create or edit `~/.qwen/settings.json` + +Here is a complete example: + +```json +{ + "modelProviders": { + "openai": [ + { + "id": "qwen3-coder-plus", + "name": "qwen3-coder-plus", + "baseUrl": "https://dashscope.aliyuncs.com/compatible-mode/v1", + "description": "Qwen3-Coder via Dashscope", + "envKey": "DASHSCOPE_API_KEY" + } + ] + }, + "env": { + "DASHSCOPE_API_KEY": "sk-xxxxxxxxxxxxx" + }, + "security": { + "auth": { + "selectedType": "openai" + } + }, + "model": { + "name": "qwen3-coder-plus" + } +} +``` + +**Step 2:** Understand each field + +| Field | What it does | +| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | +| `modelProviders` | Declares which models are available and how to connect to them. Keys like `openai`, `anthropic`, `gemini` represent the API protocol. | +| `modelProviders[].id` | The model ID sent to the API (e.g. `qwen3-coder-plus`, `gpt-4o`). | +| `modelProviders[].envKey` | The name of the environment variable that holds your API key. | +| `modelProviders[].baseUrl` | The API endpoint URL (required for non-default endpoints). | +| `env` | A fallback place to store API keys (lowest priority; prefer `.env` files or `export` for sensitive keys). | +| `security.auth.selectedType` | The protocol to use on startup (`openai`, `anthropic`, `gemini`, `vertex-ai`). | +| `model.name` | The default model to use when Qwen Code starts. | + +**Step 3:** Start Qwen Code — your configuration takes effect automatically: + +```bash +qwen +``` + +Use the `/model` command at any time to switch between all configured models. + +##### More Examples + +
+Coding Plan (Alibaba Cloud Bailian) — fixed monthly fee, higher quotas + +```json +{ + "modelProviders": { + "openai": [ + { + "id": "qwen3.5-plus", + "name": "qwen3.5-plus (Coding Plan)", + "baseUrl": "https://coding.dashscope.aliyuncs.com/v1", + "description": "qwen3.5-plus with thinking enabled from Bailian Coding Plan", + "envKey": "BAILIAN_CODING_PLAN_API_KEY", + "generationConfig": { + "extra_body": { + "enable_thinking": true + } + } + }, + { + "id": "qwen3-coder-plus", + "name": "qwen3-coder-plus (Coding Plan)", + "baseUrl": "https://coding.dashscope.aliyuncs.com/v1", + "description": "qwen3-coder-plus from Bailian Coding Plan", + "envKey": "BAILIAN_CODING_PLAN_API_KEY" + }, + { + "id": "qwen3-coder-next", + "name": "qwen3-coder-next (Coding Plan)", + "baseUrl": "https://coding.dashscope.aliyuncs.com/v1", + "description": "qwen3-coder-next with thinking enabled from Bailian Coding Plan", + "envKey": "BAILIAN_CODING_PLAN_API_KEY", + "generationConfig": { + "extra_body": { + "enable_thinking": true + } + } + }, + { + "id": "glm-4.7", + "name": "glm-4.7 (Coding Plan)", + "baseUrl": "https://coding.dashscope.aliyuncs.com/v1", + "description": "glm-4.7 with thinking enabled from Bailian Coding Plan", + "envKey": "BAILIAN_CODING_PLAN_API_KEY", + "generationConfig": { + "extra_body": { + "enable_thinking": true + } + } + }, + { + "id": "kimi-k2.5", + "name": "kimi-k2.5 (Coding Plan)", + "baseUrl": "https://coding.dashscope.aliyuncs.com/v1", + "description": "kimi-k2.5 with thinking enabled from Bailian Coding Plan", + "envKey": "BAILIAN_CODING_PLAN_API_KEY", + "generationConfig": { + "extra_body": { + "enable_thinking": true + } + } + } + ] + }, + "env": { + "BAILIAN_CODING_PLAN_API_KEY": "sk-xxxxxxxxxxxxx" + }, + "security": { + "auth": { + "selectedType": "openai" + } + }, + "model": { + "name": "qwen3-coder-plus" + } +} +``` + +> Subscribe to the Coding Plan and get your API key at [Alibaba Cloud Bailian](https://modelstudio.console.aliyun.com/?tab=dashboard#/efm/coding_plan). + +
+ +
+Multiple providers (OpenAI + Anthropic + Gemini) + +```json +{ + "modelProviders": { + "openai": [ + { + "id": "gpt-4o", + "name": "GPT-4o", + "envKey": "OPENAI_API_KEY", + "baseUrl": "https://api.openai.com/v1" + } + ], + "anthropic": [ + { + "id": "claude-sonnet-4-20250514", + "name": "Claude Sonnet 4", + "envKey": "ANTHROPIC_API_KEY" + } + ], + "gemini": [ + { + "id": "gemini-2.5-pro", + "name": "Gemini 2.5 Pro", + "envKey": "GEMINI_API_KEY" + } + ] + }, + "env": { + "OPENAI_API_KEY": "sk-xxxxxxxxxxxxx", + "ANTHROPIC_API_KEY": "sk-ant-xxxxxxxxxxxxx", + "GEMINI_API_KEY": "AIzaxxxxxxxxxxxxx" + }, + "security": { + "auth": { + "selectedType": "openai" + } + }, + "model": { + "name": "gpt-4o" + } +} +``` + +
+ +
+Enable thinking mode (for supported models like qwen3.5-plus) + +```json +{ + "modelProviders": { + "openai": [ + { + "id": "qwen3.5-plus", + "name": "qwen3.5-plus (thinking)", + "envKey": "DASHSCOPE_API_KEY", + "baseUrl": "https://dashscope.aliyuncs.com/compatible-mode/v1", + "generationConfig": { + "extra_body": { + "enable_thinking": true + } + } + } + ] + }, + "env": { + "DASHSCOPE_API_KEY": "sk-xxxxxxxxxxxxx" + }, + "security": { + "auth": { + "selectedType": "openai" + } + }, + "model": { + "name": "qwen3.5-plus" + } +} +``` + +
+ +> **Tip:** You can also set API keys via `export` in your shell or `.env` files, which take higher priority than `settings.json` → `env`. See the [authentication guide](https://qwenlm.github.io/qwen-code-docs/en/users/configuration/auth/) for full details. + +> **Security note:** Never commit API keys to version control. The `~/.qwen/settings.json` file is in your home directory and should stay private. ## Usage @@ -191,10 +417,21 @@ Build on top of Qwen Code with the TypeScript SDK: Qwen Code can be configured via `settings.json`, environment variables, and CLI flags. -- **User settings**: `~/.qwen/settings.json` -- **Project settings**: `.qwen/settings.json` +| File | Scope | Description | +| ----------------------- | ------------- | --------------------------------------------------------------------------------------- | +| `~/.qwen/settings.json` | User (global) | Applies to all your Qwen Code sessions. **Recommended for `modelProviders` and `env`.** | +| `.qwen/settings.json` | Project | Applies only when running Qwen Code in this project. Overrides user settings. | -See [settings](https://qwenlm.github.io/qwen-code-docs/en/users/configuration/settings/) for available options and precedence. +The most commonly used top-level fields in `settings.json`: + +| Field | Description | +| ---------------------------- | ---------------------------------------------------------------------------------------------------- | +| `modelProviders` | Define available models per protocol (`openai`, `anthropic`, `gemini`, `vertex-ai`). | +| `env` | Fallback environment variables (e.g. API keys). Lower priority than shell `export` and `.env` files. | +| `security.auth.selectedType` | The protocol to use on startup (e.g. `openai`). | +| `model.name` | The default model to use when Qwen Code starts. | + +> See the [Authentication](#api-key-flexible) section above for complete `settings.json` examples, and the [settings reference](https://qwenlm.github.io/qwen-code-docs/en/users/configuration/settings/) for all available options. ## Benchmark Results diff --git a/SECURITY.md b/SECURITY.md index 4e7d8ce79..d4ae9df9e 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -1,5 +1,9 @@ -# Reporting Security Issues +# Security Policy -Please report any security issue or Higress crash report to [ASRC](https://security.alibaba.com/) (Alibaba Security Response Center) where the issue will be triaged appropriately. +## Reporting a Vulnerability -Thank you for helping keep our project secure. +If you believe you have discovered a security vulnerability, please report it to us through the following portal: [Report Security Issue](https://yundun.console.aliyun.com/?p=xznew#/taskmanagement/tasks/detail/151) + +> **Note:** This channel is strictly for reporting security-related issues. Non-security vulnerabilities or general bug reports will not be addressed here. + +We sincerely appreciate your responsible disclosure and your contribution to helping us keep our project secure. diff --git a/docs/developers/roadmap.md b/docs/developers/roadmap.md index 125a4d36e..b1f30199c 100644 --- a/docs/developers/roadmap.md +++ b/docs/developers/roadmap.md @@ -2,13 +2,13 @@ > **Objective**: Catch up with Claude Code's product functionality, continuously refine details, and enhance user experience. -| Category | Phase 1 | Phase 2 | -| ------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- | -| User Experience | ✅ Terminal UI
✅ Support OpenAI Protocol
✅ Settings
✅ OAuth
✅ Cache Control
✅ Memory
✅ Compress
✅ Theme | Better UI
OnBoarding
LogView
✅ Session
Permission
🔄 Cross-platform Compatibility | -| Coding Workflow | ✅ Slash Commands
✅ MCP
✅ PlanMode
✅ TodoWrite
✅ SubAgent
✅ Multi Model
✅ Chat Management
✅ Tools (WebFetch, Bash, TextSearch, FileReadFile, EditFile) | 🔄 Hooks
SubAgent (enhanced)
✅ Skill
✅ Headless Mode
✅ Tools (WebSearch) | -| Building Open Capabilities | ✅ Custom Commands | ✅ QwenCode SDK
Extension | -| Integrating Community Ecosystem | | ✅ VSCode Plugin
🔄 ACP/Zed
✅ GHA | -| Administrative Capabilities | ✅ Stats
✅ Feedback | Costs
Dashboard | +| Category | Phase 1 | Phase 2 | +| ------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| User Experience | ✅ Terminal UI
✅ Support OpenAI Protocol
✅ Settings
✅ OAuth
✅ Cache Control
✅ Memory
✅ Compress
✅ Theme | Better UI
OnBoarding
LogView
✅ Session
Permission
🔄 Cross-platform Compatibility
✅ Coding Plan
✅ Anthropic Provider
✅ Multimodal Input
✅ Unified WebUI | +| Coding Workflow | ✅ Slash Commands
✅ MCP
✅ PlanMode
✅ TodoWrite
✅ SubAgent
✅ Multi Model
✅ Chat Management
✅ Tools (WebFetch, Bash, TextSearch, FileReadFile, EditFile) | 🔄 Hooks
✅ Skill
✅ Headless Mode
✅ Tools (WebSearch)
✅ LSP Support
✅ Concurrent Runner | +| Building Open Capabilities | ✅ Custom Commands | ✅ QwenCode SDK
✅ Extension System | +| Integrating Community Ecosystem | | ✅ VSCode Plugin
✅ ACP/Zed
✅ GHA | +| Administrative Capabilities | ✅ Stats
✅ Feedback | Costs
Dashboard
✅ User Feedback Dialog | > For more details, please see the list below. @@ -16,39 +16,48 @@ #### Completed Features -| Feature | Version | Description | Category | -| ----------------------- | --------- | ------------------------------------------------------- | ------------------------------- | -| Skill | `V0.6.0` | Extensible custom AI skills | Coding Workflow | -| Github Actions | `V0.5.0` | qwen-code-action and automation | Integrating Community Ecosystem | -| VSCode Plugin | `V0.5.0` | VSCode extension plugin | Integrating Community Ecosystem | -| QwenCode SDK | `V0.4.0` | Open SDK for third-party integration | Building Open Capabilities | -| Session | `V0.4.0` | Enhanced session management | User Experience | -| i18n | `V0.3.0` | Internationalization and multilingual support | User Experience | -| Headless Mode | `V0.3.0` | Headless mode (non-interactive) | Coding Workflow | -| ACP/Zed | `V0.2.0` | ACP and Zed editor integration | Integrating Community Ecosystem | -| Terminal UI | `V0.1.0+` | Interactive terminal user interface | User Experience | -| Settings | `V0.1.0+` | Configuration management system | User Experience | -| Theme | `V0.1.0+` | Multi-theme support | User Experience | -| Support OpenAI Protocol | `V0.1.0+` | Support for OpenAI API protocol | User Experience | -| Chat Management | `V0.1.0+` | Session management (save, restore, browse) | Coding Workflow | -| MCP | `V0.1.0+` | Model Context Protocol integration | Coding Workflow | -| Multi Model | `V0.1.0+` | Multi-model support and switching | Coding Workflow | -| Slash Commands | `V0.1.0+` | Slash command system | Coding Workflow | -| Tool: Bash | `V0.1.0+` | Shell command execution tool (with is_background param) | Coding Workflow | -| Tool: FileRead/EditFile | `V0.1.0+` | File read/write and edit tools | Coding Workflow | -| Custom Commands | `V0.1.0+` | Custom command loading | Building Open Capabilities | -| Feedback | `V0.1.0+` | Feedback mechanism (/bug command) | Administrative Capabilities | -| Stats | `V0.1.0+` | Usage statistics and quota display | Administrative Capabilities | -| Memory | `V0.0.9+` | Project-level and global memory management | User Experience | -| Cache Control | `V0.0.9+` | Prompt caching control (Anthropic, DashScope) | User Experience | -| PlanMode | `V0.0.14` | Task planning mode | Coding Workflow | -| Compress | `V0.0.11` | Chat compression mechanism | User Experience | -| SubAgent | `V0.0.11` | Dedicated sub-agent system | Coding Workflow | -| TodoWrite | `V0.0.10` | Task management and progress tracking | Coding Workflow | -| Tool: TextSearch | `V0.0.8+` | Text search tool (grep, supports .qwenignore) | Coding Workflow | -| Tool: WebFetch | `V0.0.7+` | Web content fetching tool | Coding Workflow | -| Tool: WebSearch | `V0.0.7+` | Web search tool (using Tavily API) | Coding Workflow | -| OAuth | `V0.0.5+` | OAuth login authentication (Qwen OAuth) | User Experience | +| Feature | Version | Description | Category | Phase | +| ----------------------- | --------- | ------------------------------------------------------- | ------------------------------- | ----- | +| **Coding Plan** | `V0.10.0` | Alibaba Cloud Coding Plan authentication & models | User Experience | 2 | +| Unified WebUI | `V0.9.0` | Shared WebUI component library for VSCode/CLI | User Experience | 2 | +| Export Chat | `V0.8.0` | Export sessions to Markdown/HTML/JSON/JSONL | User Experience | 2 | +| Extension System | `V0.8.0` | Full extension management with slash commands | Building Open Capabilities | 2 | +| LSP Support | `V0.7.0` | Experimental LSP service (`--experimental-lsp`) | Coding Workflow | 2 | +| Anthropic Provider | `V0.7.0` | Anthropic API provider support | User Experience | 2 | +| User Feedback Dialog | `V0.7.0` | In-app feedback collection with fatigue mechanism | Administrative Capabilities | 2 | +| Concurrent Runner | `V0.6.0` | Batch CLI execution with Git integration | Coding Workflow | 2 | +| Multimodal Input | `V0.6.0` | Image, PDF, audio, video input support | User Experience | 2 | +| Skill | `V0.6.0` | Extensible custom AI skills (experimental) | Coding Workflow | 2 | +| Github Actions | `V0.5.0` | qwen-code-action and automation | Integrating Community Ecosystem | 1 | +| VSCode Plugin | `V0.5.0` | VSCode extension plugin | Integrating Community Ecosystem | 1 | +| QwenCode SDK | `V0.4.0` | Open SDK for third-party integration | Building Open Capabilities | 1 | +| Session | `V0.4.0` | Enhanced session management | User Experience | 1 | +| i18n | `V0.3.0` | Internationalization and multilingual support | User Experience | 1 | +| Headless Mode | `V0.3.0` | Headless mode (non-interactive) | Coding Workflow | 1 | +| ACP/Zed | `V0.2.0` | ACP and Zed editor integration | Integrating Community Ecosystem | 1 | +| Terminal UI | `V0.1.0+` | Interactive terminal user interface | User Experience | 1 | +| Settings | `V0.1.0+` | Configuration management system | User Experience | 1 | +| Theme | `V0.1.0+` | Multi-theme support | User Experience | 1 | +| Support OpenAI Protocol | `V0.1.0+` | Support for OpenAI API protocol | User Experience | 1 | +| Chat Management | `V0.1.0+` | Session management (save, restore, browse) | Coding Workflow | 1 | +| MCP | `V0.1.0+` | Model Context Protocol integration | Coding Workflow | 1 | +| Multi Model | `V0.1.0+` | Multi-model support and switching | Coding Workflow | 1 | +| Slash Commands | `V0.1.0+` | Slash command system | Coding Workflow | 1 | +| Tool: Bash | `V0.1.0+` | Shell command execution tool (with is_background param) | Coding Workflow | 1 | +| Tool: FileRead/EditFile | `V0.1.0+` | File read/write and edit tools | Coding Workflow | 1 | +| Custom Commands | `V0.1.0+` | Custom command loading | Building Open Capabilities | 1 | +| Feedback | `V0.1.0+` | Feedback mechanism (/bug command) | Administrative Capabilities | 1 | +| Stats | `V0.1.0+` | Usage statistics and quota display | Administrative Capabilities | 1 | +| Memory | `V0.0.9+` | Project-level and global memory management | User Experience | 1 | +| Cache Control | `V0.0.9+` | Prompt caching control (Anthropic, DashScope) | User Experience | 1 | +| PlanMode | `V0.0.14` | Task planning mode | Coding Workflow | 1 | +| Compress | `V0.0.11` | Chat compression mechanism | User Experience | 1 | +| SubAgent | `V0.0.11` | Dedicated sub-agent system | Coding Workflow | 1 | +| TodoWrite | `V0.0.10` | Task management and progress tracking | Coding Workflow | 1 | +| Tool: TextSearch | `V0.0.8+` | Text search tool (grep, supports .qwenignore) | Coding Workflow | 1 | +| Tool: WebFetch | `V0.0.7+` | Web content fetching tool | Coding Workflow | 1 | +| Tool: WebSearch | `V0.0.7+` | Web search tool (using Tavily API) | Coding Workflow | 1 | +| OAuth | `V0.0.5+` | OAuth login authentication (Qwen OAuth) | User Experience | 1 | #### Features to Develop @@ -60,7 +69,6 @@ | Cross-platform Compatibility | P1 | In Progress | Windows/Linux/macOS compatibility | User Experience | | LogView | P2 | Planned | Log viewing and debugging feature | User Experience | | Hooks | P2 | In Progress | Extension hooks system | Coding Workflow | -| Extension | P2 | Planned | Extension system | Building Open Capabilities | | Costs | P2 | Planned | Cost tracking and analysis | Administrative Capabilities | | Dashboard | P2 | Planned | Management dashboard | Administrative Capabilities | diff --git a/docs/developers/tools/sandbox.md b/docs/developers/tools/sandbox.md index 92550f164..de3e23716 100644 --- a/docs/developers/tools/sandbox.md +++ b/docs/developers/tools/sandbox.md @@ -38,6 +38,7 @@ ls -la $(dirname $(which qwen))/../lib/node_modules/@qwen-code/qwen-code # 7.Test the version of qwen qwen -v # npm link will overwrite the global qwen. To avoid being unable to distinguish the same version number, you can uninstall the global CLI first + ``` #### 3、Create your sandbox Dockerfile under the root directory of your own project diff --git a/docs/users/configuration/auth.md b/docs/users/configuration/auth.md index 0a5b700ea..3e15aa462 100644 --- a/docs/users/configuration/auth.md +++ b/docs/users/configuration/auth.md @@ -1,13 +1,12 @@ # Authentication -Qwen Code supports two authentication methods. Pick the one that matches how you want to run the CLI: +Qwen Code supports three authentication methods. Pick the one that matches how you want to run the CLI: -- **Qwen OAuth (recommended)**: sign in with your `qwen.ai` account in a browser. -- **API-KEY**: use an API key to connect to any supported provider. More flexible — supports OpenAI, Anthropic, Google GenAI, Alibaba Cloud Bailian, and other compatible endpoints. +- **Qwen OAuth**: sign in with your `qwen.ai` account in a browser. Free with a daily quota. +- **Alibaba Cloud Coding Plan**: use an API key from Alibaba Cloud. Paid subscription with diverse model options and higher quotas. +- **API Key**: bring your own API key. Flexible to your own needs — supports OpenAI, Anthropic, Gemini, and other compatible endpoints. -![](https://gw.alicdn.com/imgextra/i4/O1CN01yXSXc91uYxJxhJXBF_!!6000000006050-2-tps-2372-916.png) - -## 👍 Option 1: Qwen OAuth (recommended & free) +## Option 1: Qwen OAuth (Free) Use this if you want the simplest setup and you're using Qwen models. @@ -25,49 +24,126 @@ qwen > [!note] > > In non-interactive or headless environments (e.g., CI, SSH, containers), you typically **cannot** complete the OAuth browser login flow. -> In these cases, please use the API-KEY authentication method. +> In these cases, please use the Alibaba Cloud Coding Plan or API Key authentication method. -## 🚀 Option 2: API-KEY (flexible) +## 💳 Option 2: Alibaba Cloud Coding Plan -Use this if you want more flexibility over which provider and model to use. Supports multiple protocols and providers, including OpenAI, Anthropic, Google GenAI, Alibaba Cloud Bailian, Azure OpenAI, OpenRouter, ModelScope, or a self-hosted compatible endpoint. - -### Option1: Coding Plan(Aliyun Bailian) - -Use this if you want predictable costs with higher usage quotas for the qwen3-coder-plus model. +Use this if you want predictable costs with diverse model options and higher usage quotas. - **How it works**: Subscribe to the Coding Plan with a fixed monthly fee, then configure Qwen Code to use the dedicated endpoint and your subscription API key. -- **Requirements**: Obtain an active Coding Plan subscription from [Alibaba Cloud Bailian](https://bailian.console.aliyun.com/cn-beijing/?tab=globalset#/efm/coding_plan). -- **Benefits**: Higher usage quotas, predictable monthly costs, access to the latest qwen3-coder-plus model. -- **Cost & quota**: View [Alibaba Cloud Bailian Coding Plan documentation](https://bailian.console.aliyun.com/cn-beijing/?tab=doc#/doc/?type=model&url=3005961). +- **Requirements**: Obtain an active Coding Plan subscription from [Aliyun Bailian](https://bailian.console.aliyun.com/?tab=model#/efm/coding_plan) or [Alibaba Cloud](https://bailian.console.alibabacloud.com/?tab=model#/efm/coding_plan), depending on the region of your account. +- **Benefits**: Diverse model options, higher usage quotas, predictable monthly costs, access to a wide range of models (Qwen, GLM, Kimi, Minimax and more). +- **Cost & quota**: View [Aliyun Bailian Coding Plan documentation](https://bailian.console.aliyun.com/cn-beijing/?tab=doc#/doc/?type=model&url=3005961). -Enter `qwen` in the terminal to launch Qwen Code, then enter the `/auth` command and select `API-KEY` +Alibaba Cloud Coding Plan is available in two regions: -![](https://gw.alicdn.com/imgextra/i4/O1CN01yXSXc91uYxJxhJXBF_!!6000000006050-2-tps-2372-916.png) +| Region | Console URL | +| -------------------------------- | ---------------------------------------------------------------------------- | +| Aliyun Bailian (aliyun.com) | [bailian.console.aliyun.com](https://bailian.console.aliyun.com) | +| Alibaba Cloud (alibabacloud.com) | [bailian.console.alibabacloud.com](https://bailian.console.alibabacloud.com) | -After entering, select `Coding Plan`: +### Interactive setup -![](https://gw.alicdn.com/imgextra/i4/O1CN01Irk0AD1ebfop69o0r_!!6000000003890-2-tps-2308-830.png) +Enter `qwen` in the terminal to launch Qwen Code, then run the `/auth` command and select **Alibaba Cloud Coding Plan**. Choose your region, then enter your `sk-sp-xxxxxxxxx` key. -Enter your `sk-sp-xxxxxxxxx` key, then use the `/model` command to switch between all Bailian `Coding Plan` supported models: +After authentication, use the `/model` command to switch between all Alibaba Cloud Coding Plan supported models (including qwen3.5-plus, qwen3-coder-plus, qwen3-coder-next, qwen3-max, glm-4.7, and kimi-k2.5). -![](https://gw.alicdn.com/imgextra/i4/O1CN01fWArmf1kaCEgSmPln_!!6000000004699-2-tps-2304-1374.png) +### Alternative: configure via `settings.json` -### Option2: Third-party API-KEY +If you prefer to skip the interactive `/auth` flow, add the following to `~/.qwen/settings.json`: -Use this if you want to connect to third-party providers such as OpenAI, Anthropic, Google, Azure OpenAI, OpenRouter, ModelScope, or a self-hosted endpoint. +```json +{ + "modelProviders": { + "openai": [ + { + "id": "qwen3-coder-plus", + "name": "qwen3-coder-plus (Coding Plan)", + "baseUrl": "https://coding.dashscope.aliyuncs.com/v1", + "description": "qwen3-coder-plus from Alibaba Cloud Coding Plan", + "envKey": "BAILIAN_CODING_PLAN_API_KEY" + } + ] + }, + "env": { + "BAILIAN_CODING_PLAN_API_KEY": "sk-sp-xxxxxxxxx" + }, + "security": { + "auth": { + "selectedType": "openai" + } + }, + "model": { + "name": "qwen3-coder-plus" + } +} +``` + +> [!note] +> +> The Coding Plan uses a dedicated endpoint (`https://coding.dashscope.aliyuncs.com/v1`) that is different from the standard Dashscope endpoint. Make sure to use the correct `baseUrl`. + +## 🚀 Option 3: API Key (flexible) + +Use this if you want to connect to third-party providers such as OpenAI, Anthropic, Google, Azure OpenAI, OpenRouter, ModelScope, or a self-hosted endpoint. Supports multiple protocols and providers. + +### Recommended: One-file setup via `settings.json` + +The simplest way to get started with API Key authentication is to put everything in a single `~/.qwen/settings.json` file. Here's a complete, ready-to-use example: + +```json +{ + "modelProviders": { + "openai": [ + { + "id": "qwen3-coder-plus", + "name": "qwen3-coder-plus", + "baseUrl": "https://dashscope.aliyuncs.com/compatible-mode/v1", + "description": "Qwen3-Coder via Dashscope", + "envKey": "DASHSCOPE_API_KEY" + } + ] + }, + "env": { + "DASHSCOPE_API_KEY": "sk-xxxxxxxxxxxxx" + }, + "security": { + "auth": { + "selectedType": "openai" + } + }, + "model": { + "name": "qwen3-coder-plus" + } +} +``` + +What each field does: + +| Field | Description | +| ---------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- | +| `modelProviders` | Declares which models are available and how to connect to them. Keys (`openai`, `anthropic`, `gemini`) represent the API protocol. | +| `env` | Stores API keys directly in `settings.json` as a fallback (lowest priority — shell `export` and `.env` files take precedence). | +| `security.auth.selectedType` | Tells Qwen Code which protocol to use on startup (e.g. `openai`, `anthropic`, `gemini`). Without this, you'd need to run `/auth` interactively. | +| `model.name` | The default model to activate when Qwen Code starts. Must match one of the `id` values in your `modelProviders`. | + +After saving the file, just run `qwen` — no interactive `/auth` setup needed. + +> [!tip] +> +> The sections below explain each part in more detail. If the quick example above works for you, feel free to skip ahead to [Security notes](#security-notes). The key concept is **Model Providers** (`modelProviders`): Qwen Code supports multiple API protocols, not just OpenAI. You configure which providers and models are available by editing `~/.qwen/settings.json`, then switch between them at runtime with the `/model` command. #### Supported protocols -| Protocol | `modelProviders` key | Environment variables | Providers | -| ----------------- | -------------------- | ------------------------------------------------------------ | --------------------------------------------------------------------------------------------------- | -| OpenAI-compatible | `openai` | `OPENAI_API_KEY`, `OPENAI_BASE_URL`, `OPENAI_MODEL` | OpenAI, Azure OpenAI, OpenRouter, ModelScope, Alibaba Cloud Bailian, any OpenAI-compatible endpoint | -| Anthropic | `anthropic` | `ANTHROPIC_API_KEY`, `ANTHROPIC_BASE_URL`, `ANTHROPIC_MODEL` | Anthropic Claude | -| Google GenAI | `gemini` | `GEMINI_API_KEY`, `GEMINI_MODEL` | Google Gemini | -| Google Vertex AI | `vertex-ai` | `GOOGLE_API_KEY`, `GOOGLE_MODEL` | Google Vertex AI | +| Protocol | `modelProviders` key | Environment variables | Providers | +| ----------------- | -------------------- | ------------------------------------------------------------ | ------------------------------------------------------------------------------------------- | +| OpenAI-compatible | `openai` | `OPENAI_API_KEY`, `OPENAI_BASE_URL`, `OPENAI_MODEL` | OpenAI, Azure OpenAI, OpenRouter, ModelScope, Alibaba Cloud, any OpenAI-compatible endpoint | +| Anthropic | `anthropic` | `ANTHROPIC_API_KEY`, `ANTHROPIC_BASE_URL`, `ANTHROPIC_MODEL` | Anthropic Claude | +| Google GenAI | `gemini` | `GEMINI_API_KEY`, `GEMINI_MODEL` | Google Gemini | -#### Step 1: Configure `modelProviders` in `~/.qwen/settings.json` +#### Step 1: Configure models and providers in `~/.qwen/settings.json` Define which models are available for each protocol. Each model entry requires at minimum an `id` and an `envKey` (the environment variable name that holds your API key). @@ -75,7 +151,7 @@ Define which models are available for each protocol. Each model entry requires a > > It is recommended to define `modelProviders` in the user-scope `~/.qwen/settings.json` to avoid merge conflicts between project and user settings. -Edit `~/.qwen/settings.json` (create it if it doesn't exist): +Edit `~/.qwen/settings.json` (create it if it doesn't exist). You can mix multiple protocols in a single file — here is a multi-provider example showing just the `modelProviders` section: ```json { @@ -106,7 +182,11 @@ Edit `~/.qwen/settings.json` (create it if it doesn't exist): } ``` -You can mix multiple protocols and models in a single configuration. The `ModelConfig` fields are: +> [!tip] +> +> Don't forget to also set `env`, `security.auth.selectedType`, and `model.name` alongside `modelProviders` — see the [complete example above](#recommended-one-file-setup-via-settingsjson) for reference. + +**`ModelConfig` fields (each entry inside `modelProviders`):** | Field | Required | Description | | ------------------ | -------- | -------------------------------------------------------------------- | @@ -118,9 +198,9 @@ You can mix multiple protocols and models in a single configuration. The `ModelC > [!note] > -> Credentials are **never** stored in `settings.json`. The runtime reads them from the environment variable specified in `envKey`. +> When using the `env` field in `settings.json`, credentials are stored in plain text. For better security, prefer `.env` files or shell `export` — see [Step 2](#step-2-set-environment-variables). -For the full `modelProviders` schema and advanced options like `generationConfig`, `customHeaders`, and `extra_body`, see [Settings Reference → modelProviders](settings.md#modelproviders). +For the full `modelProviders` schema and advanced options like `generationConfig`, `customHeaders`, and `extra_body`, see [Model Providers Reference](model-providers.md). #### Step 2: Set environment variables @@ -165,34 +245,28 @@ If nothing is found, it falls back to your **home directory**: **3. `settings.json` → `env` field (lowest priority)** -You can also define environment variables directly in `~/.qwen/settings.json` under the `env` key. These are loaded as the **lowest-priority fallback** — only applied when a variable is not already set by the system environment or `.env` files. +You can also define API keys directly in `~/.qwen/settings.json` under the `env` key. These are loaded as the **lowest-priority fallback** — only applied when a variable is not already set by the system environment or `.env` files. ```json { "env": { - "DASHSCOPE_API_KEY":"sk-...", + "DASHSCOPE_API_KEY": "sk-...", "OPENAI_API_KEY": "sk-...", - "ANTHROPIC_API_KEY": "sk-ant-...", - "GEMINI_API_KEY": "AIza..." - }, - "modelProviders": { - ... + "ANTHROPIC_API_KEY": "sk-ant-..." } } ``` -> [!note] -> -> This is useful when you want to keep all configuration (providers + credentials) in a single file. However, be mindful that `settings.json` may be shared or synced — prefer `.env` files for sensitive secrets. +This is the approach used in the [one-file setup example](#recommended-one-file-setup-via-settingsjson) above. It's convenient for keeping everything in one place, but be mindful that `settings.json` may be shared or synced — prefer `.env` files for sensitive secrets. **Priority summary:** -| Priority | Source | Override behavior | -| ----------- | ------------------------------ | ---------------------------------------- | -| 1 (highest) | CLI flags (`--openai-api-key`) | Always wins | -| 2 | System env (`export`, inline) | Overrides `.env` and `settings.env` | -| 3 | `.env` file | Only sets if not in system env | -| 4 (lowest) | `settings.json` → `env` | Only sets if not in system env or `.env` | +| Priority | Source | Override behavior | +| ----------- | ------------------------------ | -------------------------------------------- | +| 1 (highest) | CLI flags (`--openai-api-key`) | Always wins | +| 2 | System env (`export`, inline) | Overrides `.env` and `settings.json` → `env` | +| 3 | `.env` file | Only sets if not in system env | +| 4 (lowest) | `settings.json` → `env` | Only sets if not in system env or `.env` | #### Step 3: Switch models with `/model` @@ -213,11 +287,11 @@ qwen --model "qwen3-coder-plus" # In another terminal -qwen --model "qwen3-coder-next" +qwen --model "qwen3.5-plus" ``` ## Security notes -- Don’t commit API keys to version control. +- Don't commit API keys to version control. - Prefer `.qwen/.env` for project-local secrets (and keep it out of git). - Treat your terminal output as sensitive if it prints credentials for verification. diff --git a/docs/users/configuration/model-providers.md b/docs/users/configuration/model-providers.md new file mode 100644 index 000000000..bcfc2cc75 --- /dev/null +++ b/docs/users/configuration/model-providers.md @@ -0,0 +1,506 @@ +# Model Providers + +Qwen Code allows you to configure multiple model providers through the `modelProviders` setting in your `settings.json`. This enables you to switch between different AI models and providers using the `/model` command. + +## Overview + +Use `modelProviders` to declare curated model lists per auth type that the `/model` picker can switch between. Keys must be valid auth types (`openai`, `anthropic`, `gemini`, etc.). Each entry requires an `id` and **must include `envKey`**, with optional `name`, `description`, `baseUrl`, and `generationConfig`. Credentials are never persisted in settings; the runtime reads them from `process.env[envKey]`. Qwen OAuth models remain hard-coded and cannot be overridden. + +> [!note] +> +> Only the `/model` command exposes non-default auth types. Anthropic, Gemini, etc., must be defined via `modelProviders`. The `/auth` command lists Qwen OAuth, Alibaba Cloud Coding Plan, and API Key as the built-in authentication options. + +> [!warning] +> +> **Duplicate model IDs within the same authType:** Defining multiple models with the same `id` under a single `authType` (e.g., two entries with `"id": "gpt-4o"` in `openai`) is currently not supported. If duplicates exist, **the first occurrence wins** and subsequent duplicates are skipped with a warning. Note that the `id` field is used both as the configuration identifier and as the actual model name sent to the API, so using unique IDs (e.g., `gpt-4o-creative`, `gpt-4o-balanced`) is not a viable workaround. This is a known limitation that we plan to address in a future release. + +## Configuration Examples by Auth Type + +Below are comprehensive configuration examples for different authentication types, showing the available parameters and their combinations. + +### Supported Auth Types + +The `modelProviders` object keys must be valid `authType` values. Currently supported auth types are: + +| Auth Type | Description | +| ------------ | --------------------------------------------------------------------------------------- | +| `openai` | OpenAI-compatible APIs (OpenAI, Azure OpenAI, local inference servers like vLLM/Ollama) | +| `anthropic` | Anthropic Claude API | +| `gemini` | Google Gemini API | +| `qwen-oauth` | Qwen OAuth (hard-coded, cannot be overridden in `modelProviders`) | + +> [!warning] +> If an invalid auth type key is used (e.g., a typo like `"openai-custom"`), the configuration will be **silently skipped** and the models will not appear in the `/model` picker. Always use one of the supported auth type values listed above. + +### SDKs Used for API Requests + +Qwen Code uses the following official SDKs to send requests to each provider: + +| Auth Type | SDK Package | +| ------------ | ----------------------------------------------------------------------------------------------- | +| `openai` | [`openai`](https://www.npmjs.com/package/openai) - Official OpenAI Node.js SDK | +| `anthropic` | [`@anthropic-ai/sdk`](https://www.npmjs.com/package/@anthropic-ai/sdk) - Official Anthropic SDK | +| `gemini` | [`@google/genai`](https://www.npmjs.com/package/@google/genai) - Official Google GenAI SDK | +| `qwen-oauth` | [`openai`](https://www.npmjs.com/package/openai) with custom provider (DashScope-compatible) | + +This means the `baseUrl` you configure should be compatible with the corresponding SDK's expected API format. For example, when using `openai` auth type, the endpoint must accept OpenAI API format requests. + +### OpenAI-compatible providers (`openai`) + +This auth type supports not only OpenAI's official API but also any OpenAI-compatible endpoint, including aggregated model providers like OpenRouter. + +```json +{ + "modelProviders": { + "openai": [ + { + "id": "gpt-4o", + "name": "GPT-4o", + "envKey": "OPENAI_API_KEY", + "baseUrl": "https://api.openai.com/v1", + "generationConfig": { + "timeout": 60000, + "maxRetries": 3, + "enableCacheControl": true, + "contextWindowSize": 128000, + "modalities": { + "image": true + }, + "customHeaders": { + "X-Client-Request-ID": "req-123" + }, + "extra_body": { + "enable_thinking": true, + "service_tier": "priority" + }, + "samplingParams": { + "temperature": 0.2, + "top_p": 0.8, + "max_tokens": 4096, + "presence_penalty": 0.1, + "frequency_penalty": 0.1 + } + } + }, + { + "id": "gpt-4o-mini", + "name": "GPT-4o Mini", + "envKey": "OPENAI_API_KEY", + "baseUrl": "https://api.openai.com/v1", + "generationConfig": { + "timeout": 30000, + "samplingParams": { + "temperature": 0.5, + "max_tokens": 2048 + } + } + }, + { + "id": "openai/gpt-4o", + "name": "GPT-4o (via OpenRouter)", + "envKey": "OPENROUTER_API_KEY", + "baseUrl": "https://openrouter.ai/api/v1", + "generationConfig": { + "timeout": 120000, + "maxRetries": 3, + "samplingParams": { + "temperature": 0.7 + } + } + } + ] + } +} +``` + +### Anthropic (`anthropic`) + +```json +{ + "modelProviders": { + "anthropic": [ + { + "id": "claude-3-5-sonnet", + "name": "Claude 3.5 Sonnet", + "envKey": "ANTHROPIC_API_KEY", + "baseUrl": "https://api.anthropic.com/v1", + "generationConfig": { + "timeout": 120000, + "maxRetries": 3, + "contextWindowSize": 200000, + "samplingParams": { + "temperature": 0.7, + "max_tokens": 8192, + "top_p": 0.9 + } + } + }, + { + "id": "claude-3-opus", + "name": "Claude 3 Opus", + "envKey": "ANTHROPIC_API_KEY", + "baseUrl": "https://api.anthropic.com/v1", + "generationConfig": { + "timeout": 180000, + "samplingParams": { + "temperature": 0.3, + "max_tokens": 4096 + } + } + } + ] + } +} +``` + +### Google Gemini (`gemini`) + +```json +{ + "modelProviders": { + "gemini": [ + { + "id": "gemini-2.0-flash", + "name": "Gemini 2.0 Flash", + "envKey": "GEMINI_API_KEY", + "baseUrl": "https://generativelanguage.googleapis.com", + "capabilities": { + "vision": true + }, + "generationConfig": { + "timeout": 60000, + "maxRetries": 2, + "contextWindowSize": 1000000, + "schemaCompliance": "auto", + "samplingParams": { + "temperature": 0.4, + "top_p": 0.95, + "max_tokens": 8192, + "top_k": 40 + } + } + } + ] + } +} +``` + +### Local Self-Hosted Models (via OpenAI-compatible API) + +Most local inference servers (vLLM, Ollama, LM Studio, etc.) provide an OpenAI-compatible API endpoint. Configure them using the `openai` auth type with a local `baseUrl`: + +```json +{ + "modelProviders": { + "openai": [ + { + "id": "qwen2.5-7b", + "name": "Qwen2.5 7B (Ollama)", + "envKey": "OLLAMA_API_KEY", + "baseUrl": "http://localhost:11434/v1", + "generationConfig": { + "timeout": 300000, + "maxRetries": 1, + "contextWindowSize": 32768, + "samplingParams": { + "temperature": 0.7, + "top_p": 0.9, + "max_tokens": 4096 + } + } + }, + { + "id": "llama-3.1-8b", + "name": "Llama 3.1 8B (vLLM)", + "envKey": "VLLM_API_KEY", + "baseUrl": "http://localhost:8000/v1", + "generationConfig": { + "timeout": 120000, + "maxRetries": 2, + "contextWindowSize": 128000, + "samplingParams": { + "temperature": 0.6, + "max_tokens": 8192 + } + } + }, + { + "id": "local-model", + "name": "Local Model (LM Studio)", + "envKey": "LMSTUDIO_API_KEY", + "baseUrl": "http://localhost:1234/v1", + "generationConfig": { + "timeout": 60000, + "samplingParams": { + "temperature": 0.5 + } + } + } + ] + } +} +``` + +For local servers that don't require authentication, you can use any placeholder value for the API key: + +```bash +# For Ollama (no auth required) +export OLLAMA_API_KEY="ollama" + +# For vLLM (if no auth is configured) +export VLLM_API_KEY="not-needed" +``` + +> [!note] +> +> The `extra_body` parameter is **only supported for OpenAI-compatible providers** (`openai`, `qwen-oauth`). It is ignored for Anthropic, and Gemini providers. + +## Alibaba Cloud Coding Plan + +Alibaba Cloud Coding Plan provides a pre-configured set of Qwen models optimized for coding tasks. This feature is available for users with Alibaba Cloud Coding Plan API access and offers a simplified setup experience with automatic model configuration updates. + +### Overview + +When you authenticate with an Alibaba Cloud Coding Plan API key using the `/auth` command, Qwen Code automatically configures the following models: + +| Model ID | Name | Description | +| ---------------------- | -------------------- | -------------------------------------- | +| `qwen3.5-plus` | qwen3.5-plus | Advanced model with thinking enabled | +| `qwen3-coder-plus` | qwen3-coder-plus | Optimized for coding tasks | +| `qwen3-max-2026-01-23` | qwen3-max-2026-01-23 | Latest max model with thinking enabled | + +### Setup + +1. Obtain an Alibaba Cloud Coding Plan API key: + - **China**: + - **International**: +2. Run the `/auth` command in Qwen Code +3. Select **Alibaba Cloud Coding Plan** +4. Select your region +5. Enter your API key when prompted + +The models will be automatically configured and added to your `/model` picker. + +### Regions + +Alibaba Cloud Coding Plan supports two regions: + +| Region | Endpoint | Description | +| -------------------- | ----------------------------------------------- | ----------------------- | +| China | `https://coding.dashscope.aliyuncs.com/v1` | Mainland China endpoint | +| Global/International | `https://coding-intl.dashscope.aliyuncs.com/v1` | International endpoint | + +The region is selected during authentication and stored in `settings.json` under `codingPlan.region`. To switch regions, re-run the `/auth` command and select a different region. + +### API Key Storage + +When you configure Coding Plan through the `/auth` command, the API key is stored using the reserved environment variable name `BAILIAN_CODING_PLAN_API_KEY`. By default, it is stored in the `env` field of your `settings.json` file. + +> [!warning] +> +> **Security Recommendation**: For better security, it is recommended to move the API key from `settings.json` to a separate `.env` file and load it as an environment variable. For example: +> +> ```bash +> # ~/.qwen/.env +> BAILIAN_CODING_PLAN_API_KEY=your-api-key-here +> ``` +> +> Then ensure this file is added to your `.gitignore` if you're using project-level settings. + +### Automatic Updates + +Coding Plan model configurations are versioned. When Qwen Code detects a newer version of the model template, you will be prompted to update. Accepting the update will: + +- Replace the existing Coding Plan model configurations with the latest versions +- Preserve any custom model configurations you've added manually +- Automatically switch to the first model in the updated configuration + +The update process ensures you always have access to the latest model configurations and features without manual intervention. + +### Manual Configuration (Advanced) + +If you prefer to manually configure Coding Plan models, you can add them to your `settings.json` like any OpenAI-compatible provider: + +```json +{ + "modelProviders": { + "openai": [ + { + "id": "qwen3-coder-plus", + "name": "qwen3-coder-plus", + "description": "Qwen3-Coder via Alibaba Cloud Coding Plan", + "envKey": "YOUR_CUSTOM_ENV_KEY", + "baseUrl": "https://coding.dashscope.aliyuncs.com/v1" + } + ] + } +} +``` + +> [!note] +> +> When using manual configuration: +> +> - You can use any environment variable name for `envKey` +> - You do not need to configure `codingPlan.*` +> - **Automatic updates will not apply** to manually configured Coding Plan models + +> [!warning] +> +> If you also use automatic Coding Plan configuration, automatic updates may overwrite your manual configurations if they use the same `envKey` and `baseUrl` as the automatic configuration. To avoid this, ensure your manual configuration uses a different `envKey` if possible. + +## Resolution Layers and Atomicity + +The effective auth/model/credential values are chosen per field using the following precedence (first present wins). You can combine `--auth-type` with `--model` to point directly at a provider entry; these CLI flags run before other layers. + +| Layer (highest → lowest) | authType | model | apiKey | baseUrl | apiKeyEnvKey | proxy | +| -------------------------- | ----------------------------------- | ----------------------------------------------- | --------------------------------------------------- | ---------------------------------------------------- | ---------------------- | --------------------------------- | +| Programmatic overrides | `/auth` | `/auth` input | `/auth` input | `/auth` input | — | — | +| Model provider selection | — | `modelProvider.id` | `env[modelProvider.envKey]` | `modelProvider.baseUrl` | `modelProvider.envKey` | — | +| CLI arguments | `--auth-type` | `--model` | `--openaiApiKey` (or provider-specific equivalents) | `--openaiBaseUrl` (or provider-specific equivalents) | — | — | +| Environment variables | — | Provider-specific mapping (e.g. `OPENAI_MODEL`) | Provider-specific mapping (e.g. `OPENAI_API_KEY`) | Provider-specific mapping (e.g. `OPENAI_BASE_URL`) | — | — | +| Settings (`settings.json`) | `security.auth.selectedType` | `model.name` | `security.auth.apiKey` | `security.auth.baseUrl` | — | — | +| Default / computed | Falls back to `AuthType.QWEN_OAUTH` | Built-in default (OpenAI ⇒ `qwen3-coder-plus`) | — | — | — | `Config.getProxy()` if configured | + +\*When present, CLI auth flags override settings. Otherwise, `security.auth.selectedType` or the implicit default determine the auth type. Qwen OAuth and OpenAI are the only auth types surfaced without extra configuration. + +> [!warning] +> +> **Deprecation of `security.auth.apiKey` and `security.auth.baseUrl`:** Directly configuring API credentials via `security.auth.apiKey` and `security.auth.baseUrl` in `settings.json` is deprecated. These settings were used in historical versions for credentials entered through the UI, but the credential input flow was removed in version 0.10.1. These fields will be fully removed in a future release. **It is strongly recommended to migrate to `modelProviders`** for all model and credential configurations. Use `envKey` in `modelProviders` to reference environment variables for secure credential management instead of hardcoding credentials in settings files. + +## Generation Config Layering: The Impermeable Provider Layer + +The configuration resolution follows a strict layering model with one crucial rule: **the modelProvider layer is impermeable**. + +### How it works + +1. **When a modelProvider model IS selected** (e.g., via `/model` command choosing a provider-configured model): + - The entire `generationConfig` from the provider is applied **atomically** + - **The provider layer is completely impermeable** — lower layers (CLI, env, settings) do not participate in generationConfig resolution at all + - All fields defined in `modelProviders[].generationConfig` use the provider's values + - All fields **not defined** by the provider are set to `undefined` (not inherited from settings) + - This ensures provider configurations act as a complete, self-contained "sealed package" + +2. **When NO modelProvider model is selected** (e.g., using `--model` with a raw model ID, or using CLI/env/settings directly): + - The resolution falls through to lower layers + - Fields are populated from CLI → env → settings → defaults + - This creates a **Runtime Model** (see next section) + +### Per-field precedence for `generationConfig` + +| Priority | Source | Behavior | +| -------- | --------------------------------------------- | -------------------------------------------------------------------------------------------------------- | +| 1 | Programmatic overrides | Runtime `/model`, `/auth` changes | +| 2 | `modelProviders[authType][].generationConfig` | **Impermeable layer** - completely replaces all generationConfig fields; lower layers do not participate | +| 3 | `settings.model.generationConfig` | Only used for **Runtime Models** (when no provider model is selected) | +| 4 | Content-generator defaults | Provider-specific defaults (e.g., OpenAI vs Gemini) - only for Runtime Models | + +### Atomic field treatment + +The following fields are treated as atomic objects - provider values completely replace the entire object, no merging occurs: + +- `samplingParams` - Temperature, top_p, max_tokens, etc. +- `customHeaders` - Custom HTTP headers +- `extra_body` - Extra request body parameters + +### Example + +```json +// User settings (~/.qwen/settings.json) +{ + "model": { + "generationConfig": { + "timeout": 30000, + "samplingParams": { "temperature": 0.5, "max_tokens": 1000 } + } + } +} + +// modelProviders configuration +{ + "modelProviders": { + "openai": [{ + "id": "gpt-4o", + "envKey": "OPENAI_API_KEY", + "generationConfig": { + "timeout": 60000, + "samplingParams": { "temperature": 0.2 } + } + }] + } +} +``` + +When `gpt-4o` is selected from modelProviders: + +- `timeout` = 60000 (from provider, overrides settings) +- `samplingParams.temperature` = 0.2 (from provider, completely replaces settings object) +- `samplingParams.max_tokens` = **undefined** (not defined in provider, and provider layer does not inherit from settings — fields are explicitly set to undefined if not provided) + +When using a raw model via `--model gpt-4` (not from modelProviders, creates a Runtime Model): + +- `timeout` = 30000 (from settings) +- `samplingParams.temperature` = 0.5 (from settings) +- `samplingParams.max_tokens` = 1000 (from settings) + +The merge strategy for `modelProviders` itself is REPLACE: the entire `modelProviders` from project settings will override the corresponding section in user settings, rather than merging the two. + +## Provider Models vs Runtime Models + +Qwen Code distinguishes between two types of model configurations: + +### Provider Model + +- Defined in `modelProviders` configuration +- Has a complete, atomic configuration package +- When selected, its configuration is applied as an impermeable layer +- Appears in `/model` command list with full metadata (name, description, capabilities) +- Recommended for multi-model workflows and team consistency + +### Runtime Model + +- Created dynamically when using raw model IDs via CLI (`--model`), environment variables, or settings +- Not defined in `modelProviders` +- Configuration is built by "projecting" through resolution layers (CLI → env → settings → defaults) +- Automatically captured as a **RuntimeModelSnapshot** when a complete configuration is detected +- Allows reuse without re-entering credentials + +### RuntimeModelSnapshot lifecycle + +When you configure a model without using `modelProviders`, Qwen Code automatically creates a RuntimeModelSnapshot to preserve your configuration: + +```bash +# This creates a RuntimeModelSnapshot with ID: $runtime|openai|my-custom-model +qwen --auth-type openai --model my-custom-model --openaiApiKey $KEY --openaiBaseUrl https://api.example.com/v1 +``` + +The snapshot: + +- Captures model ID, API key, base URL, and generation config +- Persists across sessions (stored in memory during runtime) +- Appears in the `/model` command list as a runtime option +- Can be switched to using `/model $runtime|openai|my-custom-model` + +### Key differences + +| Aspect | Provider Model | Runtime Model | +| ----------------------- | --------------------------------- | ------------------------------------------ | +| Configuration source | `modelProviders` in settings | CLI, env, settings layers | +| Configuration atomicity | Complete, impermeable package | Layered, each field resolved independently | +| Reusability | Always available in `/model` list | Captured as snapshot, appears if complete | +| Team sharing | Yes (via committed settings) | No (user-local) | +| Credential storage | Reference via `envKey` only | May capture actual key in snapshot | + +### When to use each + +- **Use Provider Models** when: You have standard models shared across a team, need consistent configurations, or want to prevent accidental overrides +- **Use Runtime Models** when: Quickly testing a new model, using temporary credentials, or working with ad-hoc endpoints + +## Selection Persistence and Recommendations + +> [!important] +> +> Define `modelProviders` in the user-scope `~/.qwen/settings.json` whenever possible and avoid persisting credential overrides in any scope. Keeping the provider catalog in user settings prevents merge/override conflicts between project and user scopes and ensures `/auth` and `/model` updates always write back to a consistent scope. + +- `/model` and `/auth` persist `model.name` (where applicable) and `security.auth.selectedType` to the closest writable scope that already defines `modelProviders`; otherwise they fall back to the user scope. This keeps workspace/user files in sync with the active provider catalog. +- Without `modelProviders`, the resolver mixes CLI/env/settings layers, creating Runtime Models. This is fine for single-provider setups but cumbersome when frequently switching. Define provider catalogs whenever multi-model workflows are common so that switches stay atomic, source-attributed, and debuggable. diff --git a/docs/users/configuration/settings.md b/docs/users/configuration/settings.md index 0094f411d..edca4aedd 100644 --- a/docs/users/configuration/settings.md +++ b/docs/users/configuration/settings.md @@ -2,7 +2,7 @@ > [!tip] > -> **Authentication / API keys:** Authentication (Qwen OAuth vs OpenAI-compatible API) and auth-related environment variables (like `OPENAI_API_KEY`) are documented in **[Authentication](../configuration/auth)**. +> **Authentication / API keys:** Authentication (Qwen OAuth, Alibaba Cloud Coding Plan, or API Key) and auth-related environment variables (like `OPENAI_API_KEY`) are documented in **[Authentication](../configuration/auth)**. > [!note] > @@ -125,18 +125,18 @@ Settings are organized into categories. All settings should be placed within the #### model -| Setting | Type | Description | Default | -| -------------------------------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------- | -| `model.name` | string | The Qwen model to use for conversations. | `undefined` | -| `model.maxSessionTurns` | number | Maximum number of user/model/tool turns to keep in a session. -1 means unlimited. | `-1` | -| `model.summarizeToolOutput` | object | Enables or disables the summarization of tool output. You can specify the token budget for the summarization using the `tokenBudget` setting. Note: Currently only the `run_shell_command` tool is supported. For example `{"run_shell_command": {"tokenBudget": 2000}}` | `undefined` | -| `model.generationConfig` | object | Advanced overrides passed to the underlying content generator. Supports request controls such as `timeout`, `maxRetries`, `enableCacheControl`, `contextWindowSize` (override model's context window size), `customHeaders` (custom HTTP headers for API requests), and `extra_body` (additional body parameters for OpenAI-compatible API requests only), along with fine-tuning knobs under `samplingParams` (for example `temperature`, `top_p`, `max_tokens`). Leave unset to rely on provider defaults. | `undefined` | -| `model.chatCompression.contextPercentageThreshold` | number | Sets the threshold for chat history compression as a percentage of the model's total token limit. This is a value between 0 and 1 that applies to both automatic compression and the manual `/compress` command. For example, a value of `0.6` will trigger compression when the chat history exceeds 60% of the token limit. Use `0` to disable compression entirely. | `0.7` | -| `model.skipNextSpeakerCheck` | boolean | Skip the next speaker check. | `false` | -| `model.skipLoopDetection` | boolean | Disables loop detection checks. Loop detection prevents infinite loops in AI responses but can generate false positives that interrupt legitimate workflows. Enable this option if you experience frequent false positive loop detection interruptions. | `false` | -| `model.skipStartupContext` | boolean | Skips sending the startup workspace context (environment summary and acknowledgement) at the beginning of each session. Enable this if you prefer to provide context manually or want to save tokens on startup. | `false` | -| `model.enableOpenAILogging` | boolean | Enables logging of OpenAI API calls for debugging and analysis. When enabled, API requests and responses are logged to JSON files. | `false` | -| `model.openAILoggingDir` | string | Custom directory path for OpenAI API logs. If not specified, defaults to `logs/openai` in the current working directory. Supports absolute paths, relative paths (resolved from current working directory), and `~` expansion (home directory). | `undefined` | +| Setting | Type | Description | Default | +| -------------------------------------------------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------- | +| `model.name` | string | The Qwen model to use for conversations. | `undefined` | +| `model.maxSessionTurns` | number | Maximum number of user/model/tool turns to keep in a session. -1 means unlimited. | `-1` | +| `model.summarizeToolOutput` | object | Enables or disables the summarization of tool output. You can specify the token budget for the summarization using the `tokenBudget` setting. Note: Currently only the `run_shell_command` tool is supported. For example `{"run_shell_command": {"tokenBudget": 2000}}` | `undefined` | +| `model.generationConfig` | object | Advanced overrides passed to the underlying content generator. Supports request controls such as `timeout`, `maxRetries`, `enableCacheControl`, `contextWindowSize` (override model's context window size), `modalities` (override auto-detected input modalities), `customHeaders` (custom HTTP headers for API requests), and `extra_body` (additional body parameters for OpenAI-compatible API requests only), along with fine-tuning knobs under `samplingParams` (for example `temperature`, `top_p`, `max_tokens`). Leave unset to rely on provider defaults. | `undefined` | +| `model.chatCompression.contextPercentageThreshold` | number | Sets the threshold for chat history compression as a percentage of the model's total token limit. This is a value between 0 and 1 that applies to both automatic compression and the manual `/compress` command. For example, a value of `0.6` will trigger compression when the chat history exceeds 60% of the token limit. Use `0` to disable compression entirely. | `0.7` | +| `model.skipNextSpeakerCheck` | boolean | Skip the next speaker check. | `false` | +| `model.skipLoopDetection` | boolean | Disables loop detection checks. Loop detection prevents infinite loops in AI responses but can generate false positives that interrupt legitimate workflows. Enable this option if you experience frequent false positive loop detection interruptions. | `false` | +| `model.skipStartupContext` | boolean | Skips sending the startup workspace context (environment summary and acknowledgement) at the beginning of each session. Enable this if you prefer to provide context manually or want to save tokens on startup. | `false` | +| `model.enableOpenAILogging` | boolean | Enables logging of OpenAI API calls for debugging and analysis. When enabled, API requests and responses are logged to JSON files. | `false` | +| `model.openAILoggingDir` | string | Custom directory path for OpenAI API logs. If not specified, defaults to `logs/openai` in the current working directory. Supports absolute paths, relative paths (resolved from current working directory), and `~` expansion (home directory). | `undefined` | **Example model.generationConfig:** @@ -146,10 +146,12 @@ Settings are organized into categories. All settings should be placed within the "generationConfig": { "timeout": 60000, "contextWindowSize": 128000, + "modalities": { + "image": true + }, "enableCacheControl": true, "customHeaders": { - "X-Request-ID": "req-123", - "X-User-ID": "user-456" + "X-Client-Request-ID": "req-123" }, "extra_body": { "enable_thinking": true @@ -168,6 +170,10 @@ Settings are organized into categories. All settings should be placed within the Overrides the default context window size for the selected model. Qwen Code determines the context window using built-in defaults based on model name matching, with a constant fallback value. Use this setting when a provider's effective context limit differs from Qwen Code's default. This value defines the model's assumed maximum context capacity, not a per-request token limit. +**modalities:** + +Overrides the auto-detected input modalities for the selected model. Qwen Code automatically detects supported modalities (image, PDF, audio, video) based on model name pattern matching. Use this setting when the auto-detection is incorrect — for example, to enable `pdf` for a model that supports it but isn't recognized. Format: `{ "image": true, "pdf": true, "audio": true, "video": true }`. Omit a key or set it to `false` for unsupported types. + **customHeaders:** Allows you to add custom HTTP headers to all API requests. This is useful for request tracing, monitoring, API gateway routing, or when different models require different headers. If `customHeaders` is defined in `modelProviders[].generationConfig.customHeaders`, it will be used directly; otherwise, headers from `model.generationConfig.customHeaders` will be used. No merging occurs between the two levels. @@ -180,102 +186,6 @@ The `extra_body` field allows you to add custom parameters to the request body s - `"./custom-logs"` - Logs to `./custom-logs` relative to current directory - `"/tmp/openai-logs"` - Logs to absolute path `/tmp/openai-logs` -#### modelProviders - -Use `modelProviders` to declare curated model lists per auth type that the `/model` picker can switch between. Keys must be valid auth types (`openai`, `anthropic`, `gemini`, `vertex-ai`, etc.). Each entry requires an `id` and **must include `envKey`**, with optional `name`, `description`, `baseUrl`, and `generationConfig`. Credentials are never persisted in settings; the runtime reads them from `process.env[envKey]`. Qwen OAuth models remain hard-coded and cannot be overridden. - -##### Example - -```json -{ - "modelProviders": { - "openai": [ - { - "id": "gpt-4o", - "name": "GPT-4o", - "envKey": "OPENAI_API_KEY", - "baseUrl": "https://api.openai.com/v1", - "generationConfig": { - "timeout": 60000, - "maxRetries": 3, - "customHeaders": { - "X-Model-Version": "v1.0", - "X-Request-Priority": "high" - }, - "extra_body": { - "enable_thinking": true - }, - "samplingParams": { "temperature": 0.2 } - } - } - ], - "anthropic": [ - { - "id": "claude-3-5-sonnet", - "envKey": "ANTHROPIC_API_KEY", - "baseUrl": "https://api.anthropic.com/v1" - } - ], - "gemini": [ - { - "id": "gemini-2.0-flash", - "name": "Gemini 2.0 Flash", - "envKey": "GEMINI_API_KEY", - "baseUrl": "https://generativelanguage.googleapis.com" - } - ], - "vertex-ai": [ - { - "id": "gemini-1.5-pro-vertex", - "envKey": "GOOGLE_API_KEY", - "baseUrl": "https://generativelanguage.googleapis.com" - } - ] - } -} -``` - -> [!note] -> Only the `/model` command exposes non-default auth types. Anthropic, Gemini, Vertex AI, etc., must be defined via `modelProviders`. The `/auth` command intentionally lists only the built-in Qwen OAuth and OpenAI flows. - -##### Resolution layers and atomicity - -The effective auth/model/credential values are chosen per field using the following precedence (first present wins). You can combine `--auth-type` with `--model` to point directly at a provider entry; these CLI flags run before other layers. - -| Layer (highest → lowest) | authType | model | apiKey | baseUrl | apiKeyEnvKey | proxy | -| -------------------------- | ----------------------------------- | ----------------------------------------------- | --------------------------------------------------- | ---------------------------------------------------- | ---------------------- | --------------------------------- | -| Programmatic overrides | `/auth ` | `/auth` input | `/auth` input | `/auth` input | — | — | -| Model provider selection | — | `modelProvider.id` | `env[modelProvider.envKey]` | `modelProvider.baseUrl` | `modelProvider.envKey` | — | -| CLI arguments | `--auth-type` | `--model` | `--openaiApiKey` (or provider-specific equivalents) | `--openaiBaseUrl` (or provider-specific equivalents) | — | — | -| Environment variables | — | Provider-specific mapping (e.g. `OPENAI_MODEL`) | Provider-specific mapping (e.g. `OPENAI_API_KEY`) | Provider-specific mapping (e.g. `OPENAI_BASE_URL`) | — | — | -| Settings (`settings.json`) | `security.auth.selectedType` | `model.name` | `security.auth.apiKey` | `security.auth.baseUrl` | — | — | -| Default / computed | Falls back to `AuthType.QWEN_OAUTH` | Built-in default (OpenAI ⇒ `qwen3-coder-plus`) | — | — | — | `Config.getProxy()` if configured | - -\*When present, CLI auth flags override settings. Otherwise, `security.auth.selectedType` or the implicit default determine the auth type. Qwen OAuth and OpenAI are the only auth types surfaced without extra configuration. - -Model-provider sourced values are applied atomically: once a provider model is active, every field it defines is protected from lower layers until you manually clear credentials via `/auth`. The final `generationConfig` is the projection across all layers—lower layers only fill gaps left by higher ones, and the provider layer remains impenetrable. - -The merge strategy for `modelProviders` is REPLACE: the entire `modelProviders` from project settings will override the corresponding section in user settings, rather than merging the two. - -##### Generation config layering - -Per-field precedence for `generationConfig`: - -1. Programmatic overrides (e.g. runtime `/model`, `/auth` changes) -2. `modelProviders[authType][].generationConfig` -3. `settings.model.generationConfig` -4. Content-generator defaults (`getDefaultGenerationConfig` for OpenAI, `getParameterValue` for Gemini, etc.) - -`samplingParams`, `customHeaders`, and `extra_body` are all treated atomically; provider values replace the entire object. If `modelProviders[].generationConfig` defines these fields, they are used directly; otherwise, values from `model.generationConfig` are used. No merging occurs between provider and global configuration levels. Defaults from the content generator apply last so each provider retains its tuned baseline. - -##### Selection persistence and recommendations - -> [!important] -> Define `modelProviders` in the user-scope `~/.qwen/settings.json` whenever possible and avoid persisting credential overrides in any scope. Keeping the provider catalog in user settings prevents merge/override conflicts between project and user scopes and ensures `/auth` and `/model` updates always write back to a consistent scope. - -- `/model` and `/auth` persist `model.name` (where applicable) and `security.auth.selectedType` to the closest writable scope that already defines `modelProviders`; otherwise they fall back to the user scope. This keeps workspace/user files in sync with the active provider catalog. -- Without `modelProviders`, the resolver mixes CLI/env/settings layers, which is fine for single-provider setups but cumbersome when frequently switching. Define provider catalogs whenever multi-model workflows are common so that switches stay atomic, source-attributed, and debuggable. - #### context | Setting | Type | Description | Default | diff --git a/docs/users/features/commands.md b/docs/users/features/commands.md index 9a3b2c051..ba980db80 100644 --- a/docs/users/features/commands.md +++ b/docs/users/features/commands.md @@ -121,7 +121,9 @@ Environment Variables: Commands executed via `!` will set the `QWEN_CODE=1` envi Save frequently used prompts as shortcut commands to improve work efficiency and ensure consistency. -> **Note:** Custom commands now use Markdown format with optional YAML frontmatter. TOML format is deprecated but still supported for backwards compatibility. When TOML files are detected, an automatic migration prompt will be displayed. +> [!note] +> +> Custom commands now use Markdown format with optional YAML frontmatter. TOML format is deprecated but still supported for backwards compatibility. When TOML files are detected, an automatic migration prompt will be displayed. ### Quick Overview @@ -137,10 +139,10 @@ Priority Rules: Project commands > User commands (project command used when name #### File Path to Command Name Mapping Table -| File Location | Generated Command | Example Call | -| -------------------------- | ----------------- | --------------------- | -| `~/.qwen/commands/test.md` | `/test` | `/test Parameter` | -| `/git/commit.md` | `/git:commit` | `/git:commit Message` | +| File Location | Generated Command | Example Call | +| ---------------------------------------- | ----------------- | --------------------- | +| `~/.qwen/commands/test.md` | `/test` | `/test Parameter` | +| `/.qwen/commands/git/commit.md` | `/git:commit` | `/git:commit Message` | Naming Rules: Path separator (`/` or `\`) converted to colon (`:`) @@ -164,6 +166,8 @@ Use {{args}} for parameter injection. ### TOML File Format (Deprecated) +> [!warning] +> > **Deprecated:** TOML format is still supported but will be removed in a future version. Please migrate to Markdown format. | Field | Required | Description | Example | @@ -225,8 +229,6 @@ Please generate a Commit message based on the following diff: ``` ```` -```` - #### 4. File Content Injection (`@{...}`) | File Type | Support Status | Processing Method | @@ -246,7 +248,7 @@ description: Code review based on best practices Review {{args}}, reference standards: @{docs/code-standards.md} -```` +``` ### Practical Creation Example diff --git a/docs/users/overview.md b/docs/users/overview.md index 3b45cc2f0..f3c52be91 100644 --- a/docs/users/overview.md +++ b/docs/users/overview.md @@ -7,25 +7,24 @@ ## Get started in 30 seconds -Prerequisites: - -- A [Qwen Code](https://chat.qwen.ai/auth?mode=register) account -- Requires [Node.js 20+](https://nodejs.org/zh-cn/download), you can use `node -v` to check the version. If it's not installed, use the following command to install it. - ### Install Qwen Code: -**NPM**(recommended) +**Linux / macOS** -```bash -npm install -g @qwen-code/qwen-code@latest +```sh +curl -fsSL https://qwen-code-assets.oss-cn-hangzhou.aliyuncs.com/installation/install-qwen.sh | bash ``` -**Homebrew**(macOS, Linux) +**Windows (Run as Administrator CMD)** -```bash -brew install qwen-code +```sh +curl -fsSL -o %TEMP%\install-qwen.bat https://qwen-code-assets.oss-cn-hangzhou.aliyuncs.com/installation/install-qwen.bat && %TEMP%\install-qwen.bat ``` +> [!note] +> +> It's recommended to restart your terminal after installation to ensure environment variables take effect. If the installation fails, please refer to [Manual Installation](./quickstart#manual-installation) in the Quickstart guide. + ### Start using Qwen Code: ```bash diff --git a/docs/users/quickstart.md b/docs/users/quickstart.md index eac8f9474..3c4eafcea 100644 --- a/docs/users/quickstart.md +++ b/docs/users/quickstart.md @@ -16,19 +16,39 @@ Make sure you have: To install Qwen Code, use one of the following methods: -### NPM (recommended) +### Quick Install (Recommended) -Requires [Node.js 20+](https://nodejs.org/download), you can use `node -v` check the version. If it's not installed, use the following command to install it. - -If you have [Node.js or newer installed](https://nodejs.org/en/download/): +**Linux / macOS** ```sh +curl -fsSL https://qwen-code-assets.oss-cn-hangzhou.aliyuncs.com/installation/install-qwen.sh | bash +``` + +**Windows (Run as Administrator CMD)** + +```sh +curl -fsSL -o %TEMP%\install-qwen.bat https://qwen-code-assets.oss-cn-hangzhou.aliyuncs.com/installation/install-qwen.bat && %TEMP%\install-qwen.bat +``` + +> [!note] +> +> It's recommended to restart your terminal after installation to ensure environment variables take effect. + +### Manual Installation + +**Prerequisites** + +Make sure you have Node.js 20 or later installed. Download it from [nodejs.org](https://nodejs.org/en/download). + +**NPM** + +```bash npm install -g @qwen-code/qwen-code@latest ``` -### Homebrew (macOS, Linux) +**Homebrew (macOS, Linux)** -```sh +```bash brew install qwen-code ``` diff --git a/docs/users/reference/keyboard-shortcuts.md b/docs/users/reference/keyboard-shortcuts.md index fc2f86286..fdfc41b87 100644 --- a/docs/users/reference/keyboard-shortcuts.md +++ b/docs/users/reference/keyboard-shortcuts.md @@ -40,9 +40,10 @@ This document lists the available keyboard shortcuts in Qwen Code. | `Ctrl+N` | Navigate down through the input history. | | `Ctrl+P` | Navigate up through the input history. | | `Ctrl+R` | Reverse search through input/shell history. | +| `Ctrl+Y` | Retry the last failed request. | | `Ctrl+Right Arrow` / `Meta+Right Arrow` / `Meta+F` | Move the cursor one word to the right. | | `Ctrl+U` | Delete from the cursor to the beginning of the line. | -| `Ctrl+V` | Paste clipboard content. If the clipboard contains an image, it will be saved and a reference to it will be inserted in the prompt. | +| `Ctrl+V` (Windows: `Alt+V`) | Paste clipboard content. If the clipboard contains an image, it will be saved and a reference to it will be inserted in the prompt. | | `Ctrl+W` / `Meta+Backspace` / `Ctrl+Backspace` | Delete the word to the left of the cursor. | | `Ctrl+X` / `Meta+Enter` | Open the current input in an external editor. | diff --git a/docs/users/support/tos-privacy.md b/docs/users/support/tos-privacy.md index aa0d5c471..386153512 100644 --- a/docs/users/support/tos-privacy.md +++ b/docs/users/support/tos-privacy.md @@ -4,17 +4,19 @@ Qwen Code is an open-source AI coding assistant tool maintained by the Qwen Code ## How to determine your authentication method -Qwen Code supports two main authentication methods to access AI models. Your authentication method determines which terms of service and privacy policies apply to your usage: +Qwen Code supports three authentication methods to access AI models. Your authentication method determines which terms of service and privacy policies apply to your usage: -1. **Qwen OAuth** - Log in with your qwen.ai account -2. **OpenAI-Compatible API** - Use API keys from various AI model providers +1. **Qwen OAuth** — Log in with your qwen.ai account (free daily quota) +2. **Alibaba Cloud Coding Plan** — Use an API key from Alibaba Cloud +3. **API Key** — Bring your own API key For each authentication method, different Terms of Service and Privacy Notices may apply depending on the underlying service provider. -| Authentication Method | Provider | Terms of Service | Privacy Notice | -| :-------------------- | :---------------- | :---------------------------------------------------------------------------- | :--------------------------------------------------- | -| Qwen OAuth | Qwen AI | [Qwen Terms of Service](https://qwen.ai/termsservice) | [Qwen Privacy Policy](https://qwen.ai/privacypolicy) | -| OpenAI-Compatible API | Various Providers | Depends on your chosen API provider (OpenAI, Alibaba Cloud, ModelScope, etc.) | Depends on your chosen API provider | +| Authentication Method | Provider | Terms of Service | Privacy Notice | +| :------------------------ | :---------------- | :----------------------------------------------------------------- | :----------------------------------------------------------------- | +| Qwen OAuth | Qwen AI | [Qwen Terms of Service](https://qwen.ai/termsservice) | [Qwen Privacy Policy](https://qwen.ai/privacypolicy) | +| Alibaba Cloud Coding Plan | Alibaba Cloud | See [details below](#2-if-you-are-using-alibaba-cloud-coding-plan) | See [details below](#2-if-you-are-using-alibaba-cloud-coding-plan) | +| API Key | Various Providers | Depends on your chosen API provider (OpenAI, Anthropic, etc.) | Depends on your chosen API provider | ## 1. If you are using Qwen OAuth Authentication @@ -25,13 +27,26 @@ When you authenticate using your qwen.ai account, these Terms of Service and Pri For details about authentication setup, quotas, and supported features, see [Authentication Setup](../configuration/settings). -## 2. If you are using OpenAI-Compatible API Authentication +## 2. If you are using Alibaba Cloud Coding Plan -When you authenticate using API keys from OpenAI-compatible providers, the applicable Terms of Service and Privacy Notice depend on your chosen provider. +When you authenticate using an API key from Alibaba Cloud, the applicable Terms of Service and Privacy Notice from Alibaba Cloud apply. + +Alibaba Cloud Coding Plan is available in two regions: + +- **阿里云百炼 (aliyun.com)** — [bailian.console.aliyun.com](https://bailian.console.aliyun.com) +- **Alibaba Cloud (alibabacloud.com)** — [bailian.console.alibabacloud.com](https://bailian.console.alibabacloud.com) > [!important] > -> When using OpenAI-compatible API authentication, you are subject to the terms and privacy policies of your chosen API provider, not Qwen Code's terms. Please review your provider's documentation for specific details about data usage, retention, and privacy practices. +> When using Alibaba Cloud Coding Plan, you are subject to Alibaba Cloud's terms and privacy policies. Please review their documentation for specific details about data usage, retention, and privacy practices. + +## 3. If you are using your own API Key + +When you authenticate using API keys from other providers, the applicable Terms of Service and Privacy Notice depend on your chosen provider. + +> [!important] +> +> When using your own API key, you are subject to the terms and privacy policies of your chosen API provider, not Qwen Code's terms. Please review your provider's documentation for specific details about data usage, retention, and privacy practices. Qwen Code supports various OpenAI-compatible providers. Please refer to your specific provider's terms of service and privacy policy for detailed information. @@ -50,7 +65,8 @@ When enabled, Qwen Code may collect: ### Data Collection by Authentication Method - **Qwen OAuth:** Usage statistics are governed by Qwen's privacy policy. You can opt-out through Qwen Code's configuration settings. -- **OpenAI-Compatible API:** No additional data is collected by Qwen Code beyond what your chosen API provider collects. +- **Alibaba Cloud Coding Plan:** Usage statistics are governed by Alibaba Cloud's privacy policy. You can opt-out through Qwen Code's configuration settings. +- **API Key:** No additional data is collected by Qwen Code beyond what your chosen API provider collects. ## Frequently Asked Questions (FAQ) @@ -60,7 +76,9 @@ Whether your code, including prompts and answers, is used to train AI models dep - **Qwen OAuth**: Data usage is governed by [Qwen's Privacy Policy](https://qwen.ai/privacy). Please refer to their policy for specific details about data collection and model training practices. -- **OpenAI-Compatible API**: Data usage depends entirely on your chosen API provider. Each provider has their own data usage policies. Please review the privacy policy and terms of service of your specific provider. +- **Alibaba Cloud Coding Plan**: Data usage is governed by Alibaba Cloud's privacy policy. Please refer to their policy for specific details about data collection and model training practices. + +- **API Key**: Data usage depends entirely on your chosen API provider. Each provider has their own data usage policies. Please review the privacy policy and terms of service of your specific provider. **Important**: Qwen Code itself does not use your prompts, code, or responses for model training. Any data usage for training purposes would be governed by the policies of the AI service provider you authenticate with. @@ -85,10 +103,10 @@ The Usage Statistics setting only controls data collection by Qwen Code itself. ### 3. How do I switch between authentication methods? -You can switch between Qwen OAuth and OpenAI-compatible API authentication at any time: +You can switch between Qwen OAuth, Alibaba Cloud Coding Plan, and your own API key at any time: 1. **During startup**: Choose your preferred authentication method when prompted 2. **Within the CLI**: Use the `/auth` command to reconfigure your authentication method -3. **Environment variables**: Set up `.env` files for automatic OpenAI-compatible API authentication +3. **Environment variables**: Set up `.env` files for automatic API key authentication For detailed instructions, see the [Authentication Setup](../configuration/settings#environment-variables-for-api-access) documentation. diff --git a/esbuild.config.js b/esbuild.config.js index 12ab39d58..2b532b44e 100644 --- a/esbuild.config.js +++ b/esbuild.config.js @@ -33,6 +33,13 @@ const external = [ '@lydell/node-pty-linux-x64', '@lydell/node-pty-win32-arm64', '@lydell/node-pty-win32-x64', + '@teddyzhu/clipboard', + '@teddyzhu/clipboard-darwin-arm64', + '@teddyzhu/clipboard-darwin-x64', + '@teddyzhu/clipboard-linux-x64-gnu', + '@teddyzhu/clipboard-linux-arm64-gnu', + '@teddyzhu/clipboard-win32-x64-msvc', + '@teddyzhu/clipboard-win32-arm64-msvc', ]; esbuild diff --git a/eslint.config.js b/eslint.config.js index 1d0ed2af9..d0963e876 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -254,9 +254,12 @@ export default tseslint.config( 'no-console': 'off', }, }, - // Settings for export-html assets + // Settings for web-templates assets { - files: ['packages/cli/assets/export-html/**/*.{js,jsx,ts,tsx}'], + files: [ + 'packages/web-templates/src/**/*.{js,jsx,ts,tsx}', + 'packages/web-templates/*.mjs', + ], languageOptions: { globals: { ...globals.browser, @@ -271,6 +274,8 @@ export default tseslint.config( rules: { 'react/react-in-jsx-scope': 'off', 'react/prop-types': 'off', + 'no-console': 'off', + 'no-undef': 'off', }, }, // Prettier config must be last diff --git a/integration-tests/acp-integration.test.ts b/integration-tests/acp-integration.test.ts index 35397da26..07e53e960 100644 --- a/integration-tests/acp-integration.test.ts +++ b/integration-tests/acp-integration.test.ts @@ -648,6 +648,107 @@ function setupAcpTest( } }); + it('blocks write tools in plan mode (issue #1806)', async () => { + const rig = new TestRig(); + rig.setup('acp plan mode enforcement'); + + const toolCallEvents: Array<{ + toolName: string; + status: string; + error?: string; + }> = []; + + const { sendRequest, cleanup, stderr, sessionUpdates } = setupAcpTest(rig, { + permissionHandler: (request) => { + // Cancel exit_plan_mode to keep plan mode active + if (request.toolCall?.kind === 'switch_mode') { + return { outcome: 'cancelled' }; + } + return { optionId: 'proceed_once' }; + }, + }); + + try { + await sendRequest('initialize', { + protocolVersion: 1, + clientCapabilities: { fs: { readTextFile: true, writeTextFile: true } }, + }); + await sendRequest('authenticate', { methodId: 'openai' }); + + const newSession = (await sendRequest('session/new', { + cwd: rig.testDir!, + mcpServers: [], + })) as { sessionId: string }; + + // Set mode to 'plan' + const setModeResult = (await sendRequest('session/set_mode', { + sessionId: newSession.sessionId, + modeId: 'plan', + })) as { modeId: string }; + expect(setModeResult.modeId).toBe('plan'); + + // Try to create a file - this should be blocked by plan mode + const promptResult = await sendRequest('session/prompt', { + sessionId: newSession.sessionId, + prompt: [ + { + type: 'text', + text: 'Create a file called test.txt with content "Hello World"', + }, + ], + }); + expect(promptResult).toBeDefined(); + + // Give time for tool calls to be processed + await delay(2000); + + // Collect tool call events from session updates + sessionUpdates.forEach((update) => { + if (update.update?.sessionUpdate === 'tool_call_update') { + const toolUpdate = update.update as { + sessionUpdate: string; + toolName?: string; + status?: string; + error?: { message?: string }; + }; + if (toolUpdate.toolName) { + toolCallEvents.push({ + toolName: toolUpdate.toolName, + status: toolUpdate.status ?? 'unknown', + error: toolUpdate.error?.message, + }); + } + } + }); + + // Verify that if write_file was attempted, it was blocked + const writeFileEvents = toolCallEvents.filter( + (e) => e.toolName === 'write_file', + ); + + // If the LLM tried to call write_file in plan mode, it should have been blocked + if (writeFileEvents.length > 0) { + const blockedEvent = writeFileEvents.find( + (e) => e.status === 'error' && e.error?.includes('Plan mode'), + ); + expect(blockedEvent).toBeDefined(); + expect(blockedEvent?.error).toContain('Plan mode is active'); + } + + // Verify the file was NOT created + const fs = await import('fs'); + const path = await import('path'); + const testFilePath = path.join(rig.testDir!, 'test.txt'); + const fileExists = fs.existsSync(testFilePath); + expect(fileExists).toBe(false); + } catch (e) { + if (stderr.length) console.error('Agent stderr:', stderr.join('')); + throw e; + } finally { + await cleanup(); + } + }); + it('receives usage metadata in agent_message_chunk updates', async () => { const rig = new TestRig(); rig.setup('acp usage metadata'); diff --git a/integration-tests/concurrent-runner/config.example.json b/integration-tests/concurrent-runner/config.example.json index 7042e7eb6..f1937fe07 100644 --- a/integration-tests/concurrent-runner/config.example.json +++ b/integration-tests/concurrent-runner/config.example.json @@ -31,5 +31,9 @@ ] } ], - "models": ["claude-3-5-sonnet-20241022", "qwen3-coder-plus"] + "models": [ + "qwen3-coder-plus", + { "name": "glm-4.7", "auth_type": "anthropic" }, + { "name": "claude-4-5-sonnet-20260219", "auth_type": "anthropic" } + ] } diff --git a/integration-tests/concurrent-runner/runner.py b/integration-tests/concurrent-runner/runner.py index c27a221e0..6eb2b8e0f 100644 --- a/integration-tests/concurrent-runner/runner.py +++ b/integration-tests/concurrent-runner/runner.py @@ -50,11 +50,18 @@ class Task: prompts: List[str] +@dataclass +class ModelSpec: + """One model to run: name and optional auth_type (e.g. anthropic).""" + name: str + auth_type: Optional[str] = None + + @dataclass class RunConfig: """Configuration for the concurrent execution.""" tasks: List[Task] - models: List[str] + models: List[ModelSpec] # name + optional auth_type per model concurrency: int = 4 yolo: bool = True source_repo: Path = field(default_factory=lambda: Path.cwd()) @@ -84,6 +91,7 @@ class RunRecord: task_name: str model: str status: RunStatus + auth_type: Optional[str] = None # e.g. "anthropic" for qwen --auth-type worktree_path: Optional[str] = None output_dir: Optional[str] = None logs_dir: Optional[str] = None @@ -104,6 +112,7 @@ class RunRecord: "task_name": self.task_name, "model": self.model, "status": self.status.value, + "auth_type": self.auth_type, "worktree_path": self.worktree_path, "output_dir": self.output_dir, "logs_dir": self.logs_dir, @@ -136,6 +145,7 @@ class RunRecord: task_name=data["task_name"], model=data["model"], status=RunStatus(data["status"]), + auth_type=data.get("auth_type"), worktree_path=data.get("worktree_path"), output_dir=data.get("output_dir"), logs_dir=data.get("logs_dir"), @@ -806,6 +816,10 @@ class QwenRunner: # Add model cmd.extend(["--model", run.model]) + # Add auth-type when model uses non-OpenAI protocol (e.g. anthropic for glm-4.7) + if run.auth_type: + cmd.extend(["--auth-type", run.auth_type]) + # Add yolo if enabled if self.config.yolo: cmd.append("--yolo") @@ -829,27 +843,41 @@ def generate_run_matrix(config: RunConfig) -> List[RunRecord]: runs = [] for task in config.tasks: for model in config.models: - run_id = str(uuid.uuid4())[:8] runs.append(RunRecord( - run_id=run_id, + run_id=str(uuid.uuid4())[:8], task_id=task.id, task_name=task.name, - model=model, + model=model.name, status=RunStatus.QUEUED, + auth_type=model.auth_type, )) return runs +def _parse_models(data_models: List[Any]) -> List[ModelSpec]: + """Parse models: string or {name, auth_type/authType}; returns list of ModelSpec.""" + specs: List[ModelSpec] = [] + for item in data_models or []: + if isinstance(item, str): + name, auth = item, None + elif isinstance(item, dict) and item.get("name"): + name = item["name"] + auth = item.get("auth_type") or item.get("authType") + else: + continue + specs.append(ModelSpec(name=name, auth_type=auth)) + return specs + + def load_config(config_path: Path) -> RunConfig: """Load configuration from JSON file.""" with open(config_path, 'r') as f: data = json.load(f) - tasks = [Task(**t) for t in data.get("tasks", [])] - + models = _parse_models(data.get("models", [])) return RunConfig( tasks=tasks, - models=data.get("models", []), + models=models, concurrency=data.get("concurrency", 4), yolo=data.get("yolo", True), source_repo=Path(data.get("source_repo", ".")).resolve(), diff --git a/integration-tests/globalSetup.ts b/integration-tests/globalSetup.ts index a8a9877fe..02cea6859 100644 --- a/integration-tests/globalSetup.ts +++ b/integration-tests/globalSetup.ts @@ -94,7 +94,7 @@ export async function setup() { // Environment variables for CLI integration tests process.env['INTEGRATION_TEST_FILE_DIR'] = runDir; - process.env['GEMINI_CLI_INTEGRATION_TEST'] = 'true'; + process.env['QWEN_CODE_INTEGRATION_TEST'] = 'true'; process.env['TELEMETRY_LOG_FILE'] = join(runDir, 'telemetry.log'); // Environment variables for SDK E2E tests diff --git a/integration-tests/sdk-typescript/session-id.test.ts b/integration-tests/sdk-typescript/session-id.test.ts new file mode 100644 index 000000000..7a2ab435d --- /dev/null +++ b/integration-tests/sdk-typescript/session-id.test.ts @@ -0,0 +1,582 @@ +/** + * @license + * Copyright 2025 Qwen Team + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * E2E tests for SDK session-id functionality: + * - sessionId option: Allows users to specify a custom session ID + * - Validation: Session ID must be a valid UUID + * - Integration: Session ID is passed to CLI via --session-id flag + * - Behavior: sessionId cannot be used with resume or continue + */ + +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; +import { query, isSDKSystemMessage, type SDKMessage } from '@qwen-code/sdk'; +import { + SDKTestHelper, + createSharedTestOptions, + assertSuccessfulCompletion, +} from './test-helper.js'; + +const SHARED_TEST_OPTIONS = createSharedTestOptions(); + +describe('Session ID Support (E2E)', () => { + let helper: SDKTestHelper; + let testDir: string; + + beforeEach(async () => { + helper = new SDKTestHelper(); + // Enable chat recording for session-id tests to allow duplicate session detection + testDir = await helper.setup('session-id', { chatRecording: true }); + }); + + afterEach(async () => { + await helper.cleanup(); + }); + + describe('sessionId Option', () => { + it('should accept a valid UUID as sessionId', async () => { + // Valid UUID v4: 4 in position 14, 8/9/a/b in position 19 + const customSessionId = '12345678-1234-4234-8234-123456789abc'; + + const q = query({ + prompt: 'What is 1 + 1? Just the number.', + options: { + ...SHARED_TEST_OPTIONS, + cwd: testDir, + sessionId: customSessionId, + debug: false, + }, + }); + + const messages: SDKMessage[] = []; + + try { + for await (const message of q) { + messages.push(message); + } + + assertSuccessfulCompletion(messages); + + // Verify the query used the custom session ID + expect(q.getSessionId()).toBe(customSessionId); + } finally { + await q.close(); + } + }); + + it('should use sessionId in system init message', async () => { + // Valid UUID v4: 4 in position 14, 8/9/a/b in position 19 + const customSessionId = 'abcdef12-3456-4234-abcd-ef1234567890'; + + const q = query({ + prompt: 'Say hello', + options: { + ...SHARED_TEST_OPTIONS, + cwd: testDir, + sessionId: customSessionId, + debug: false, + }, + }); + + const messages: SDKMessage[] = []; + + try { + for await (const message of q) { + messages.push(message); + + // Stop after we get the system init message + if (isSDKSystemMessage(message) && message.subtype === 'init') { + expect(message.session_id).toBe(customSessionId); + break; + } + } + } finally { + await q.close(); + } + }); + + it('should pass sessionId to CLI via arguments', async () => { + // Valid UUID v4: 4 in position 14, 8/9/a/b in position 19 + const customSessionId = 'a1b2c3d4-e5f6-4234-abcd-ef1234567890'; + const stderrMessages: string[] = []; + + const q = query({ + prompt: 'What is 2 + 2? Just the number.', + options: { + ...SHARED_TEST_OPTIONS, + cwd: testDir, + sessionId: customSessionId, + debug: true, + logLevel: 'debug', + stderr: (msg: string) => { + stderrMessages.push(msg); + }, + }, + }); + + try { + for await (const _message of q) { + // Consume all messages + } + + // Verify that CLI was spawned with --session-id argument + const hasSessionIdArg = stderrMessages.some((msg) => + msg.includes('--session-id'), + ); + expect(hasSessionIdArg).toBe(true); + + // Verify the session ID value is in the arguments + const hasCorrectSessionId = stderrMessages.some((msg) => + msg.includes(customSessionId), + ); + expect(hasCorrectSessionId).toBe(true); + } finally { + await q.close(); + } + }); + + it('should auto-generate sessionId when not provided', async () => { + const q = query({ + prompt: 'What is 3 + 3? Just the number.', + options: { + ...SHARED_TEST_OPTIONS, + cwd: testDir, + debug: false, + }, + }); + + const messages: SDKMessage[] = []; + + try { + for await (const message of q) { + messages.push(message); + } + + assertSuccessfulCompletion(messages); + + // Verify the query has a valid auto-generated session ID + const sessionId = q.getSessionId(); + expect(sessionId).toBeDefined(); + expect(sessionId).toMatch( + /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i, + ); + } finally { + await q.close(); + } + }); + + it('should reject using sessionId with resume', async () => { + // Valid UUIDs: 4 in position 14, 8/9/a/b in position 19 + const customSessionId = '11111111-2222-4333-a444-555555555555'; + const resumeSessionId = '66666666-7777-4888-b999-000000000000'; + + // CLI rejects using --session-id with --resume + const q = query({ + prompt: 'Say hello', + options: { + ...SHARED_TEST_OPTIONS, + cwd: testDir, + sessionId: customSessionId, + resume: resumeSessionId, + debug: false, + }, + }); + + try { + for await (const _message of q) { + // Consume messages + } + // Should not reach here - CLI should reject this combination + throw new Error( + 'Expected query to fail when using sessionId with resume', + ); + } catch (error) { + // Expected to fail - CLI rejects --session-id with --resume + expect(error).toBeDefined(); + } finally { + await q.close(); + } + }); + }); + + describe('Session ID Validation', () => { + it('should reject invalid sessionId format', async () => { + const invalidSessionId = 'not-a-valid-uuid'; + + expect(() => { + query({ + prompt: 'Say hello', + options: { + ...SHARED_TEST_OPTIONS, + cwd: testDir, + sessionId: invalidSessionId, + }, + }); + }).toThrow(/Invalid sessionId/); + }); + + it('should reject sessionId with wrong UUID version', async () => { + // UUID version 6 (not valid - must be 1-5) + const invalidVersionSessionId = '12345678-1234-6789-8234-123456789abc'; + + expect(() => { + query({ + prompt: 'Say hello', + options: { + ...SHARED_TEST_OPTIONS, + cwd: testDir, + sessionId: invalidVersionSessionId, + }, + }); + }).toThrow(/Invalid sessionId/); + }); + + it('should reject sessionId with invalid variant', async () => { + // Invalid variant (must be 8, 9, a, or b in position 19) + const invalidVariantSessionId = '12345678-1234-1234-c234-823456789abc'; + + expect(() => { + query({ + prompt: 'Say hello', + options: { + ...SHARED_TEST_OPTIONS, + cwd: testDir, + sessionId: invalidVariantSessionId, + }, + }); + }).toThrow(/Invalid sessionId/); + }); + + it('should handle empty sessionId gracefully', async () => { + // Note: Empty string behavior - validation skips it but Query constructor may use it + // This test documents the current behavior + const q = query({ + prompt: 'Say hello', + options: { + ...SHARED_TEST_OPTIONS, + cwd: testDir, + sessionId: '', + }, + }); + + try { + // When empty string is provided, the query should still be created + // The actual session ID behavior depends on implementation details + const sessionId = q.getSessionId(); + expect(sessionId).toBeDefined(); + + // If empty string is used, it's passed through; otherwise a UUID is generated + // Either way, the query should function + for await (const _message of q) { + // Consume messages + } + } finally { + await q.close(); + } + }); + + it('should accept various valid UUID formats', async () => { + const validUUIDs = [ + '12345678-1234-1234-8234-123456789abc', // version 1, variant 8 + '12345678-1234-1234-9234-123456789abc', // version 1, variant 9 + '12345678-1234-1234-a234-123456789abc', // version 1, variant a + '12345678-1234-1234-b234-123456789abc', // version 1, variant b + '12345678-1234-2234-8234-123456789abc', // version 2, variant 8 + '12345678-1234-3234-8234-123456789abc', // version 3, variant 8 + '12345678-1234-4234-8234-123456789abc', // version 4, variant 8 + '12345678-1234-5234-8234-123456789abc', // version 5, variant 8 + ]; + + for (const uuid of validUUIDs) { + const q = query({ + prompt: 'Say hi', + options: { + ...SHARED_TEST_OPTIONS, + cwd: testDir, + sessionId: uuid, + debug: false, + }, + }); + + try { + // Just verify the query is created without throwing + expect(q.getSessionId()).toBe(uuid); + } finally { + await q.close(); + } + } + }); + }); + + describe('Multi-turn with Custom Session ID', () => { + it('should maintain custom sessionId across multiple turns', async () => { + // Valid UUID v4: 4 in position 14, 8/9/a/b in position 19 + const customSessionId = '99999999-8888-4777-a666-555555555555'; + + async function* createConversation(): AsyncIterable<{ + type: 'user'; + session_id: string; + message: { role: 'user'; content: string }; + parent_tool_use_id: null; + }> { + yield { + type: 'user', + session_id: customSessionId, + message: { + role: 'user', + content: 'What is 1 + 1?', + }, + parent_tool_use_id: null, + }; + + await new Promise((resolve) => setTimeout(resolve, 100)); + + yield { + type: 'user', + session_id: customSessionId, + message: { + role: 'user', + content: 'What is 2 + 2?', + }, + parent_tool_use_id: null, + }; + } + + const q = query({ + prompt: createConversation(), + options: { + ...SHARED_TEST_OPTIONS, + cwd: testDir, + sessionId: customSessionId, + debug: false, + }, + }); + + const messages: SDKMessage[] = []; + + try { + for await (const message of q) { + messages.push(message); + } + + assertSuccessfulCompletion(messages); + + // Verify all system messages use the custom session ID + const systemMessages = messages.filter(isSDKSystemMessage); + for (const sysMsg of systemMessages) { + expect(sysMsg.session_id).toBe(customSessionId); + } + } finally { + await q.close(); + } + }); + }); + + describe('Session ID Duplicate Detection', () => { + it('should reject duplicate sessionId with error', async () => { + // Generate a unique UUID for this test + const customSessionId = crypto.randomUUID(); + + // First query: create a session with the custom session ID + const q1 = query({ + prompt: 'Say hello', + options: { + ...SHARED_TEST_OPTIONS, + cwd: testDir, + sessionId: customSessionId, + env: { + SANDBOX_SET_UID_GID: 'true', + }, + }, + }); + + // Consume the first query to completion and close it + try { + for await (const _msg of q1) { + // consume + } + } finally { + await q1.close(); + } + + // Second query: try to use the same session ID + // This should fail because the session ID is already in use + // CLI will exit with code 1 when detecting duplicate session ID + const q2 = query({ + prompt: 'Say hello again', + options: { + ...SHARED_TEST_OPTIONS, + cwd: testDir, + sessionId: customSessionId, + env: { + SANDBOX_SET_UID_GID: 'true', + }, + }, + }); + + // The error should be propagated and the iteration should throw + // When iterating over messages, if CLI exits with code 1 (duplicate session ID), + // the error should be thrown during iteration + await expect(async () => { + for await (const _msg of q2) { + // consume + } + }).rejects.toThrow(/CLI process exited with code 1/); + + await q2.close(); + }); + + it('should throw error when CLI exits with non-zero code', async () => { + // Generate a unique UUID for this test + const customSessionId = crypto.randomUUID(); + + // First query: create a session and properly close it after completion + const q1 = query({ + prompt: 'Say hello', + options: { + ...SHARED_TEST_OPTIONS, + cwd: testDir, + sessionId: customSessionId, + env: { + SANDBOX_SET_UID_GID: 'true', + }, + }, + }); + + try { + for await (const _msg of q1) { + // consume + } + } finally { + await q1.close(); + } + + // Second query with same session ID + // When using the same session ID, CLI will detect the duplicate and exit with code 1 + const q2 = query({ + prompt: 'Say hello again', + options: { + ...SHARED_TEST_OPTIONS, + cwd: testDir, + sessionId: customSessionId, + env: { + SANDBOX_SET_UID_GID: 'true', + }, + }, + }); + + let errorCaught = false; + let errorMessage = ''; + + try { + // Iterate over messages - the error should be thrown during iteration + // because CLI exits with code 1 when detecting duplicate session ID + for await (const _msg of q2) { + // consume + } + } catch (error) { + errorCaught = true; + // CLI errors are written directly to console (stderr inherit mode) + // SDK only reports the exit status, not the error message + expect(error instanceof Error).toBe(true); + errorMessage = error instanceof Error ? error.message : String(error); + // Verify the error message contains the expected exit code + expect(errorMessage).toContain('CLI process exited with code 1'); + } finally { + await q2.close(); + } + + // Verify that an error was actually caught during message iteration + expect(errorCaught).toBe(true); + }); + }); + + describe('Session ID Consistency', () => { + it('should expose same sessionId via getSessionId() and messages', async () => { + // Valid UUID v4: 4 in position 14, 8/9/a/b in position 19 + const customSessionId = 'aaaaaaaa-bbbb-4ccc-adde-eeeeeeeeeeee'; + + const q = query({ + prompt: 'Say hello', + options: { + ...SHARED_TEST_OPTIONS, + cwd: testDir, + sessionId: customSessionId, + debug: false, + }, + }); + + const messages: SDKMessage[] = []; + + try { + for await (const message of q) { + messages.push(message); + } + + // Verify getSessionId() matches the option + expect(q.getSessionId()).toBe(customSessionId); + + // Verify system messages have the same session ID + const systemMessages = messages.filter(isSDKSystemMessage); + expect(systemMessages.length).toBeGreaterThan(0); + for (const sysMsg of systemMessages) { + expect(sysMsg.session_id).toBe(customSessionId); + } + } finally { + await q.close(); + } + }); + + it('should generate different session IDs for different queries', async () => { + const q1 = query({ + prompt: 'Say one', + options: { + ...SHARED_TEST_OPTIONS, + cwd: testDir, + debug: false, + }, + }); + + const q2 = query({ + prompt: 'Say two', + options: { + ...SHARED_TEST_OPTIONS, + cwd: testDir, + debug: false, + }, + }); + + try { + // Consume messages from both queries + for await (const _msg of q1) { + // consume + } + for await (const _msg of q2) { + // consume + } + + const sessionId1 = q1.getSessionId(); + const sessionId2 = q2.getSessionId(); + + // Session IDs should be different + expect(sessionId1).toBeDefined(); + expect(sessionId2).toBeDefined(); + expect(sessionId1).not.toBe(sessionId2); + + // Both should be valid UUIDs + expect(sessionId1).toMatch( + /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i, + ); + expect(sessionId2).toMatch( + /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i, + ); + } finally { + await q1.close(); + await q2.close(); + } + }); + }); +}); diff --git a/integration-tests/sdk-typescript/test-helper.ts b/integration-tests/sdk-typescript/test-helper.ts index 07f44f890..c426f6725 100644 --- a/integration-tests/sdk-typescript/test-helper.ts +++ b/integration-tests/sdk-typescript/test-helper.ts @@ -41,6 +41,13 @@ export interface SDKTestHelperOptions { * Whether to create .qwen/settings.json */ createQwenConfig?: boolean; + /** + * Whether to enable chat recording for this test. + * - Set to `true` to enable recording (needed for session-id duplicate detection tests) + * - Set to `false` or leave undefined to disable recording (default for most tests) + * This sets chatRecording in general settings. + */ + chatRecording?: boolean; } /** @@ -91,7 +98,8 @@ export class SDKTestHelper { }, general: { ...generalSettings, - chatRecording: false, // SDK tests don't need chat recording + // Default to disabling chat recording unless explicitly enabled + ...(options.chatRecording !== true ? { chatRecording: false } : {}), }, }; diff --git a/integration-tests/terminal-capture/motivation.md b/integration-tests/terminal-capture/motivation.md new file mode 100644 index 000000000..388019369 --- /dev/null +++ b/integration-tests/terminal-capture/motivation.md @@ -0,0 +1,117 @@ +# terminal-capture — Motivation and Positioning + +## 1. Overview of Existing Testing System + +| Layer | Tools | Coverage | Status | +| ---------------------- | ----------------------------------------- | --------------------------------------- | --------------------------------------------------------- | +| Unit Tests | Vitest + ink-testing-library | Ink components, Core logic, utilities | Mature, extensive `.test.ts` / `.test.tsx` | +| Integration Tests | Vitest + TestRig / SDKTestHelper | CLI E2E, SDK multi-turn, MCP, auth | Mature, supports none/docker/podman sandboxes | +| Terminal UI Snapshots | `toMatchSnapshot()` + ink-testing-library | Ink component render output (ANSI) | Exists, covers Footer, InputPrompt, MarkdownDisplay, etc. | +| Web UI Regression | Chromatic + Storybook | `packages/webui` components | Exists, but only covers Web UI | +| **Terminal UI Visual** | **terminal-capture** | CLI terminal real rendering screenshots | ✅ Implemented | + +## 2. Problems Solved by terminal-capture + +### Limitations of Existing Ink Text Snapshots + +The project uses `toMatchSnapshot()` to compare Ink component ANSI text output, which validates **text content**, but cannot verify: + +- Whether colors are correct (red separators? green highlights? Logo gradients?) +- Whether layout is aligned (table borders? multi-column layout?) +- Overall visual feel (component spacing? blank areas? overflow?) + +These can only be seen by **actually rendering to a terminal emulator**. + +### Core Architecture + +``` +node-pty (pseudo-terminal) + ↓ raw ANSI byte stream +xterm.js (running inside Playwright headless Chromium) + ↓ perfect rendering: colors, bold, cursor, scrolling +Playwright element screenshot + ↓ pixel-perfect screenshots (optional macOS window decorations) +``` + +### Core Features + +| Feature | Description | +| -------------------- | ----------------------------------------------------------------------------------- | +| WYSIWYG | xterm.js fully renders ANSI, no manual output cleaning needed | +| Theme Support | Built-in 5 themes (Dracula, One Dark, GitHub Dark, Monokai, Night Owl) | +| Full-length | `captureFull()` supports capturing scrollback buffer content | +| Deterministic Naming | Screenshot filenames auto-generated by step sequence for easy regression comparison | +| Batch Execution | `run.ts` executes all scenarios in one command | + +## 3. Usage + +### TypeScript Configuration-Driven + +Scenario config files (`scenarios/*.ts`) only need to declare `type` (input) and `key` (keypress), Runner handles automatically: + +- Wait for CLI readiness +- Auto-complete interference handling (/ commands auto-send Escape) +- Auto-screenshot before/after input (01 = input state, 02 = result) +- Auto-capture full-length image at last step (full-flow.png) +- Special key interactions (Arrow keys / Tab / Enter, etc.) + +```typescript +// integration-tests/terminal-capture/scenarios/about.ts +import type { ScenarioConfig } from '../scenario-runner.js'; + +export default { + name: '/about', + spawn: ['node', 'dist/cli.js', '--yolo'], + terminal: { title: 'qwen-code', cwd: '../../..' }, + flow: [ + { type: 'Hi, can you help me understand this codebase?' }, + { type: '/about' }, + ], +} satisfies ScenarioConfig; +``` + +### Running + +```bash +# From project root +npx tsx integration-tests/terminal-capture/run.ts integration-tests/terminal-capture/scenarios/ + +# Or inside terminal-capture directory +npm run capture +``` + +### Screenshot Output + +``` +scenarios/screenshots/ + about/ + 01-01.png # Step 1 input state + 01-02.png # Step 1 result + 02-01.png # Step 2 input state + 02-02.png # Step 2 result + full-flow.png # Final state full-length image + context/ + ... +``` + +## 4. Position in Testing System + +``` +┌─────────────────────────────────────┐ +│ Existing Testing System │ +├─────────────────────────────────────┤ +│ Unit Tests (Vitest) │ ← Function/Component level +│ Text Snapshots (ink-testing-lib) │ ← ANSI string comparison +│ Integration Tests (TestRig/SDK) │ ← E2E functionality +│ Web UI Regression (Chromatic) │ ← Only covers webui +├─────────────────────────────────────┤ +│ terminal-capture │ ← Terminal UI visual layer +│ (xterm.js + Playwright) │ Fills the gap +└─────────────────────────────────────┘ +``` + +## 5. Future Directions + +1. **Visual Regression** — Integrate Playwright `toHaveScreenshot()` for pixel-level baseline comparison, CI auto-detects terminal UI changes +2. **PR Workflow Integration** — Drive Agent via Cursor Skill to auto-checkout branch → build → screenshot → attach to review comment +3. **Complement to Chromatic** — Chromatic covers Web UI, terminal-capture covers CLI terminal UI diff --git a/integration-tests/terminal-capture/package.json b/integration-tests/terminal-capture/package.json new file mode 100644 index 000000000..ef55e63ef --- /dev/null +++ b/integration-tests/terminal-capture/package.json @@ -0,0 +1,18 @@ +{ + "name": "@qwen-code/terminal-capture", + "version": "0.1.0", + "private": true, + "description": "Terminal UI screenshot automation for CLI visual testing", + "type": "module", + "scripts": { + "capture": "npx tsx run.ts scenarios/", + "capture:about": "npx tsx run.ts scenarios/about.ts", + "capture:all": "npx tsx run.ts scenarios/all.ts" + }, + "dependencies": { + "@lydell/node-pty": "1.1.0", + "@xterm/xterm": "^5.5.0", + "playwright": "^1.50.0", + "strip-ansi": "^7.1.2" + } +} diff --git a/integration-tests/terminal-capture/run.ts b/integration-tests/terminal-capture/run.ts new file mode 100644 index 000000000..59b9ab547 --- /dev/null +++ b/integration-tests/terminal-capture/run.ts @@ -0,0 +1,105 @@ +#!/usr/bin/env npx tsx +/** + * Batch run terminal screenshot scenarios + * + * Usage: + * npx tsx integration-tests/terminal-capture/run.ts integration-tests/terminal-capture/scenarios/about.ts + * npx tsx integration-tests/terminal-capture/run.ts integration-tests/terminal-capture/scenarios/ # batch + * npx tsx integration-tests/terminal-capture/run.ts integration-tests/terminal-capture/scenarios/*.ts # glob + */ + +import { + loadScenarios, + runScenario, + type RunResult, +} from './scenario-runner.js'; +import { readdirSync, statSync } from 'node:fs'; +import { resolve, extname, join } from 'node:path'; + +async function main() { + const args = process.argv.slice(2); + + if (args.length === 0) { + console.log( + ` +Usage: npx tsx integration-tests/terminal-capture/run.ts ... + +Examples: + npx tsx integration-tests/terminal-capture/run.ts integration-tests/terminal-capture/scenarios/about.ts + npx tsx integration-tests/terminal-capture/run.ts integration-tests/terminal-capture/scenarios/ + `.trim(), + ); + process.exit(1); + } + + // Collect all .ts scenario files from arguments + const scenarioFiles: string[] = []; + for (const arg of args) { + const abs = resolve(arg); + try { + const stat = statSync(abs); + if (stat.isDirectory()) { + const files = readdirSync(abs) + .filter((f) => extname(f) === '.ts') + .sort() + .map((f) => join(abs, f)); + scenarioFiles.push(...files); + } else { + scenarioFiles.push(abs); + } + } catch { + console.error(`❌ Not found: ${arg}`); + process.exit(1); + } + } + + if (scenarioFiles.length === 0) { + console.error('❌ No .ts scenario files found'); + process.exit(1); + } + + console.log(`🎬 Running ${scenarioFiles.length} scenario(s)...\n`); + + // Run scenarios sequentially (single file can export an array) + const results: RunResult[] = []; + for (const file of scenarioFiles) { + const { configs, basedir } = await loadScenarios(file); + for (const config of configs) { + const result = await runScenario(config, basedir); + results.push(result); + } + } + + // Summary + console.log(`\n${'═'.repeat(60)}`); + console.log('📊 Summary'); + console.log('═'.repeat(60)); + + const passed = results.filter((r) => r.success); + const failed = results.filter((r) => !r.success); + const totalScreenshots = results.reduce( + (sum, r) => sum + r.screenshots.length, + 0, + ); + const totalTime = results.reduce((sum, r) => sum + r.durationMs, 0); + + for (const r of results) { + const icon = r.success ? '✅' : '❌'; + const time = (r.durationMs / 1000).toFixed(1); + console.log( + ` ${icon} ${r.name} — ${r.screenshots.length} screenshots, ${time}s`, + ); + if (r.error) console.log(` ${r.error}`); + } + + console.log( + `\n Total: ${passed.length} passed, ${failed.length} failed, ${totalScreenshots} screenshots, ${(totalTime / 1000).toFixed(1)}s`, + ); + + if (failed.length > 0) process.exit(1); +} + +main().catch((err) => { + console.error(err); + process.exit(1); +}); diff --git a/integration-tests/terminal-capture/scenario-runner.ts b/integration-tests/terminal-capture/scenario-runner.ts new file mode 100644 index 000000000..4bd858fd4 --- /dev/null +++ b/integration-tests/terminal-capture/scenario-runner.ts @@ -0,0 +1,304 @@ +/** + * Scenario Runner v3 — TypeScript Configuration-Driven Terminal Screenshots + * + * Configuration has only two core concepts: type (input) and capture (screenshot). + * All intelligent waiting is handled automatically by the Runner. + * + * Usage: + * npx tsx integration-tests/terminal-capture/run.ts integration-tests/terminal-capture/scenarios/about.ts + * npx tsx integration-tests/terminal-capture/run.ts integration-tests/terminal-capture/scenarios/ + */ + +import { TerminalCapture, THEMES } from './terminal-capture.js'; +import { dirname, resolve, isAbsolute } from 'node:path'; + +// ───────────────────────────────────────────── +// Schema — Minimal +// ───────────────────────────────────────────── + +export interface FlowStep { + /** Input text (auto-press Enter, auto-wait for output to stabilize, auto-screenshot before/after) */ + type?: string; + /** + * Send special key presses (no auto-Enter, no auto-screenshot) + * Supported: ArrowUp, ArrowDown, ArrowLeft, ArrowRight, Enter, Tab, Escape, Backspace, Space + * Can also pass ANSI escape sequence strings + */ + key?: string | string[]; + /** Explicit screenshot: current viewport (standalone capture when no type) */ + capture?: string; + /** Explicit screenshot: full scrollback buffer long image (standalone capture when no type) */ + captureFull?: string; +} + +export interface ScenarioConfig { + /** Scenario name */ + name: string; + /** Launch command, e.g., ["node", "dist/cli.js", "--yolo"] */ + spawn: string[]; + /** Execution flow: array, each item can contain type / capture / captureFull */ + flow: FlowStep[]; + /** Terminal configuration (all optional) */ + terminal?: { + cols?: number; + rows?: number; + theme?: string; + chrome?: boolean; + title?: string; + fontSize?: number; + cwd?: string; + }; + /** Screenshot output directory (relative to config file) */ + outputDir?: string; +} + +// ───────────────────────────────────────────── +// Runner +// ───────────────────────────────────────────── + +export interface RunResult { + name: string; + screenshots: string[]; + success: boolean; + error?: string; + durationMs: number; +} + +/** Dynamically load configuration from .ts file (supports single object or array) */ +export async function loadScenarios( + tsPath: string, +): Promise<{ configs: ScenarioConfig[]; basedir: string }> { + const absPath = isAbsolute(tsPath) ? tsPath : resolve(tsPath); + const mod = (await import(absPath)) as { + default: ScenarioConfig | ScenarioConfig[]; + }; + const raw = mod.default; + const configs = Array.isArray(raw) ? raw : [raw]; + + for (const config of configs) { + if (!config?.name) throw new Error(`Missing 'name': ${absPath}`); + if (!config.spawn?.length) throw new Error(`Missing 'spawn': ${absPath}`); + if (!config.flow?.length) throw new Error(`Missing 'flow': ${absPath}`); + } + + return { configs, basedir: dirname(absPath) }; +} + +/** Execute a single scenario */ +export async function runScenario( + config: ScenarioConfig, + basedir: string, +): Promise { + const startTime = Date.now(); + const screenshots: string[] = []; + const t = config.terminal ?? {}; + + const cwd = t.cwd ? resolve(basedir, t.cwd) : resolve(basedir, '..'); + // Use scenario name as subdirectory to isolate screenshot outputs from different scenarios + const scenarioDir = + config.name + .replace(/^\//, '') + .replace(/[^a-zA-Z0-9\u4e00-\u9fff_-]/g, '-') + .replace(/-+/g, '-') + .replace(/^-|-$/g, '') || 'unnamed'; + const outputDir = config.outputDir + ? resolve(basedir, config.outputDir, scenarioDir) + : resolve(basedir, 'screenshots', scenarioDir); + + console.log(`\n${'═'.repeat(60)}`); + console.log(`▶ ${config.name}`); + console.log('═'.repeat(60)); + + const terminal = await TerminalCapture.create({ + cols: t.cols ?? 100, + rows: t.rows ?? 28, + theme: (t.theme ?? 'dracula') as keyof typeof THEMES, + chrome: t.chrome ?? true, + title: t.title ?? 'Terminal', + fontSize: t.fontSize, + cwd, + outputDir, + }); + + try { + // ── Spawn ── + const [command, ...args] = config.spawn; + console.log(` spawn: ${config.spawn.join(' ')}`); + await terminal.spawn(command, args); + + // ── Auto-wait for CLI readiness ── + console.log(' ⏳ waiting for ready...'); + await terminal.idle(1500, 30000); + console.log(' ✅ ready'); + + // ── Execute flow ── + let seq = 0; // Global screenshot sequence number + + for (let i = 0; i < config.flow.length; i++) { + const step = config.flow[i]; + const label = `[${i + 1}/${config.flow.length}]`; + + if (step.type) { + const display = + step.type.length > 60 ? step.type.slice(0, 60) + '...' : step.type; + + // If next step is key, there's more interaction to do, so don't auto-press Enter + const nextStep = config.flow[i + 1]; + const autoEnter = !nextStep?.key; + + console.log( + ` ${label} type: "${display}"${autoEnter ? '' : ' (no auto-enter)'}`, + ); + + const text = step.type.replace(/\n$/, ''); + await terminal.type(text); + await sleep(300); + + // Only send Escape for / commands to close auto-complete, not for regular text + if (text.startsWith('/') && autoEnter) { + await terminal.type('\x1b'); + await sleep(100); + } + + // ── 01: Text input complete ── + seq++; + const inputName = step.capture + ? step.capture.replace(/\.png$/, '-01.png') + : `${pad(seq)}-01.png`; + console.log(` ${label} 📸 input: ${inputName}`); + screenshots.push(await terminal.capture(inputName)); + + if (autoEnter) { + // ── Auto-press Enter → Wait for stabilization → 02 screenshot ── + await terminal.type('\n'); + console.log(` ⏳ waiting for output to settle...`); + await terminal.idle(2000, 60000); + console.log(` ✅ settled`); + + const resultName = step.capture ?? `${pad(seq)}-02.png`; + console.log(` ${label} 📸 result: ${resultName}`); + screenshots.push(await terminal.capture(resultName)); + + // full-flow: Only the last type step auto-captures full-length image + const isLastType = !config.flow.slice(i + 1).some((s) => s.type); + if (isLastType || step.captureFull) { + const fullName = step.captureFull ?? 'full-flow.png'; + console.log(` ${label} 📸 full: ${fullName}`); + screenshots.push(await terminal.captureFull(fullName)); + } + } + // When not autoEnter, only captured before state, subsequent key steps take over interaction + } else if (step.key) { + // ── key: Send special key presses (arrow keys, Tab, Enter, etc.) ── + const keys = Array.isArray(step.key) ? step.key : [step.key]; + console.log(` ${label} key: ${keys.join(', ')}`); + + for (const k of keys) { + await terminal.type(resolveKey(k)); + await sleep(150); + } + // Wait for UI response to key press + await terminal.idle(500, 5000); + + // If key step has explicit capture/captureFull + if (step.capture || step.captureFull) { + seq++; + if (step.capture) { + console.log(` ${label} 📸 capture: ${step.capture}`); + screenshots.push(await terminal.capture(step.capture)); + } + if (step.captureFull) { + console.log(` ${label} 📸 captureFull: ${step.captureFull}`); + screenshots.push(await terminal.captureFull(step.captureFull)); + } + } + + // After key sequence ends (next step is not key), auto-add result + full screenshots + const nextStep = config.flow[i + 1]; + if (!nextStep?.key) { + console.log(` ⏳ waiting for output to settle...`); + await terminal.idle(2000, 60000); + console.log(` ✅ settled`); + + const resultName = `${pad(seq)}-02.png`; + console.log(` ${label} 📸 result: ${resultName}`); + screenshots.push(await terminal.capture(resultName)); + + // If this is the last interaction step, add full-length image + const isLastType = !config.flow.slice(i + 1).some((s) => s.type); + if (isLastType) { + console.log(` ${label} 📸 full: full-flow.png`); + screenshots.push(await terminal.captureFull('full-flow.png')); + } + } + } else { + // ── Standalone screenshot step (no type/key) ── + seq++; + if (step.capture) { + console.log(` ${label} 📸 capture: ${step.capture}`); + screenshots.push(await terminal.capture(step.capture)); + } + if (step.captureFull) { + console.log(` ${label} 📸 captureFull: ${step.captureFull}`); + screenshots.push(await terminal.captureFull(step.captureFull)); + } + } + } + + const duration = Date.now() - startTime; + console.log( + `\n ✅ ${config.name} — ${screenshots.length} screenshots, ${(duration / 1000).toFixed(1)}s`, + ); + return { + name: config.name, + screenshots, + success: true, + durationMs: duration, + }; + } catch (err) { + const duration = Date.now() - startTime; + const msg = err instanceof Error ? err.message : String(err); + console.error(`\n ❌ ${config.name} — ${msg}`); + return { + name: config.name, + screenshots, + success: false, + error: msg, + durationMs: duration, + }; + } finally { + await terminal.close(); + } +} + +function sleep(ms: number): Promise { + return new Promise((r) => setTimeout(r, ms)); +} + +/** Pad sequence number with zero: 1 → "01" */ +function pad(n: number): string { + return String(n).padStart(2, '0'); +} + +/** Key name → PTY escape sequence */ +const KEY_MAP: Record = { + ArrowUp: '\x1b[A', + ArrowDown: '\x1b[B', + ArrowRight: '\x1b[C', + ArrowLeft: '\x1b[D', + Enter: '\r', + Tab: '\t', + Escape: '\x1b', + Backspace: '\x7f', + Space: ' ', + Home: '\x1b[H', + End: '\x1b[F', + PageUp: '\x1b[5~', + PageDown: '\x1b[6~', + Delete: '\x1b[3~', +}; + +/** Parse key name to PTY-recognizable character sequence */ +function resolveKey(key: string): string { + return KEY_MAP[key] ?? key; +} diff --git a/integration-tests/terminal-capture/scenarios/about.ts b/integration-tests/terminal-capture/scenarios/about.ts new file mode 100644 index 000000000..6aae802ff --- /dev/null +++ b/integration-tests/terminal-capture/scenarios/about.ts @@ -0,0 +1,8 @@ +import type { ScenarioConfig } from '../scenario-runner.js'; + +export default { + name: '/about command', + spawn: ['node', 'dist/cli.js', '--yolo'], + terminal: { title: 'qwen-code', cwd: '../../..' }, + flow: [{ type: 'hi' }, { type: '/about' }], +} satisfies ScenarioConfig; diff --git a/integration-tests/terminal-capture/scenarios/all.ts b/integration-tests/terminal-capture/scenarios/all.ts new file mode 100644 index 000000000..a8bb8db81 --- /dev/null +++ b/integration-tests/terminal-capture/scenarios/all.ts @@ -0,0 +1,46 @@ +import type { ScenarioConfig } from '../scenario-runner.js'; + +export default [ + { + name: '/about', + spawn: ['node', 'dist/cli.js', '--yolo'], + terminal: { title: 'qwen-code', cwd: '../../..' }, + flow: [ + { type: 'Hi, can you help me understand this codebase?' }, + { type: '/about' }, + ], + }, + { + name: '/context', + spawn: ['node', 'dist/cli.js', '--yolo'], + terminal: { title: 'qwen-code', cwd: '../../..' }, + flow: [ + { type: 'How do you understand this project?' }, + { type: '/context' }, + ], + }, + + { + name: '/export (tab select)', + spawn: ['node', 'dist/cli.js', '--yolo'], + terminal: { title: 'qwen-code', cwd: '../../..' }, + flow: [ + { type: 'Please give me a brief introduction about yourself.' }, + { type: '/export' }, + { key: 'Tab' }, // Tab to open format selection + { key: 'ArrowDown' }, // Down arrow to switch options + { key: 'Enter' }, // Confirm selection + ], + }, + { + name: '/auth', + spawn: ['node', 'dist/cli.js', '--yolo'], + terminal: { title: 'qwen-code', cwd: '../../..' }, + flow: [ + { type: '/auth' }, + { key: 'ArrowDown' }, // Select API Key + { key: 'Enter' }, // Confirm + { type: 'sk-test-key-123' }, + ], + }, +] satisfies ScenarioConfig[]; diff --git a/integration-tests/terminal-capture/terminal-capture.ts b/integration-tests/terminal-capture/terminal-capture.ts new file mode 100644 index 000000000..ebfddd523 --- /dev/null +++ b/integration-tests/terminal-capture/terminal-capture.ts @@ -0,0 +1,856 @@ +/** + * TerminalCapture - Terminal Screenshot Tool + * + * Terminal screenshot solution based on xterm.js + Playwright + node-pty. + * Core philosophy: WYSIWYG — let xterm.js complete terminal simulation and rendering + * inside the browser. Screenshots always capture the terminal's current real state, + * no manual output cleaning needed. + * + * Architecture: + * node-pty (pseudo-terminal) + * ↓ raw ANSI byte stream + * xterm.js (running inside Playwright headless Chromium) + * ↓ perfect rendering: colors, bold, cursor, scrolling + * Playwright element screenshot + * ↓ pixel-perfect screenshots (optional macOS window decorations) + */ + +import { chromium, type Browser, type Page } from 'playwright'; +import * as pty from '@lydell/node-pty'; +import stripAnsi from 'strip-ansi'; +import { mkdirSync } from 'node:fs'; +import { join, dirname } from 'node:path'; +import { createRequire } from 'node:module'; + +const _require = createRequire(import.meta.url); + +// ───────────────────────────────────────────── +// Theme definitions +// ───────────────────────────────────────────── + +export interface XtermTheme { + background: string; + foreground: string; + cursor: string; + cursorAccent?: string; + selectionBackground?: string; + selectionForeground?: string; + black: string; + red: string; + green: string; + yellow: string; + blue: string; + magenta: string; + cyan: string; + white: string; + brightBlack: string; + brightRed: string; + brightGreen: string; + brightYellow: string; + brightBlue: string; + brightMagenta: string; + brightCyan: string; + brightWhite: string; +} + +export const THEMES: Record = { + dracula: { + background: '#282a36', + foreground: '#f8f8f2', + cursor: '#f8f8f2', + selectionBackground: '#44475a', + black: '#21222c', + red: '#ff5555', + green: '#50fa7b', + yellow: '#f1fa8c', + blue: '#bd93f9', + magenta: '#ff79c6', + cyan: '#8be9fd', + white: '#f8f8f2', + brightBlack: '#6272a4', + brightRed: '#ff6e6e', + brightGreen: '#69ff94', + brightYellow: '#ffffa5', + brightBlue: '#d6acff', + brightMagenta: '#ff92df', + brightCyan: '#a4ffff', + brightWhite: '#ffffff', + }, + + 'one-dark': { + background: '#282c34', + foreground: '#abb2bf', + cursor: '#528bff', + selectionBackground: '#3e4451', + black: '#545862', + red: '#e06c75', + green: '#98c379', + yellow: '#e5c07b', + blue: '#61afef', + magenta: '#c678dd', + cyan: '#56b6c2', + white: '#abb2bf', + brightBlack: '#545862', + brightRed: '#e06c75', + brightGreen: '#98c379', + brightYellow: '#e5c07b', + brightBlue: '#61afef', + brightMagenta: '#c678dd', + brightCyan: '#56b6c2', + brightWhite: '#c8ccd4', + }, + + 'github-dark': { + background: '#0d1117', + foreground: '#c9d1d9', + cursor: '#c9d1d9', + selectionBackground: '#264f78', + black: '#484f58', + red: '#ff7b72', + green: '#3fb950', + yellow: '#d29922', + blue: '#58a6ff', + magenta: '#bc8cff', + cyan: '#39c5cf', + white: '#b1bac4', + brightBlack: '#6e7681', + brightRed: '#ffa198', + brightGreen: '#56d364', + brightYellow: '#e3b341', + brightBlue: '#79c0ff', + brightMagenta: '#d2a8ff', + brightCyan: '#56d4dd', + brightWhite: '#f0f6fc', + }, + + monokai: { + background: '#272822', + foreground: '#f8f8f2', + cursor: '#f8f8f0', + selectionBackground: '#49483e', + black: '#272822', + red: '#f92672', + green: '#a6e22e', + yellow: '#f4bf75', + blue: '#66d9ef', + magenta: '#ae81ff', + cyan: '#a1efe4', + white: '#f8f8f2', + brightBlack: '#75715e', + brightRed: '#f92672', + brightGreen: '#a6e22e', + brightYellow: '#f4bf75', + brightBlue: '#66d9ef', + brightMagenta: '#ae81ff', + brightCyan: '#a1efe4', + brightWhite: '#f9f8f5', + }, + + 'night-owl': { + background: '#011627', + foreground: '#d6deeb', + cursor: '#80a4c2', + selectionBackground: '#1d3b53', + black: '#011627', + red: '#ef5350', + green: '#22da6e', + yellow: '#addb67', + blue: '#82aaff', + magenta: '#c792ea', + cyan: '#21c7a8', + white: '#d6deeb', + brightBlack: '#575656', + brightRed: '#ef5350', + brightGreen: '#22da6e', + brightYellow: '#ffeb95', + brightBlue: '#82aaff', + brightMagenta: '#c792ea', + brightCyan: '#7fdbca', + brightWhite: '#ffffff', + }, +}; + +// ───────────────────────────────────────────── +// Options +// ───────────────────────────────────────────── + +export interface TerminalCaptureOptions { + /** Number of terminal columns, default 120 */ + cols?: number; + /** Number of terminal rows, default 40 */ + rows?: number; + /** Working directory */ + cwd?: string; + /** Environment variables */ + env?: NodeJS.ProcessEnv; + /** Theme name or custom theme object, default 'dracula' */ + theme?: keyof typeof THEMES | XtermTheme; + /** Whether to show macOS window decorations (traffic lights + title bar), default true */ + chrome?: boolean; + /** Window title (only effective when chrome=true), default 'Terminal' */ + title?: string; + /** Font size, default 14 */ + fontSize?: number; + /** Font family, default system monospace font */ + fontFamily?: string; + /** Default screenshot output directory */ + outputDir?: string; +} + +// ───────────────────────────────────────────── +// Main class +// ───────────────────────────────────────────── + +export class TerminalCapture { + private browser: Browser | null = null; + private page: Page | null = null; + private ptyProcess: pty.IPty | null = null; + private rawOutput = ''; + private lastFlushedLength = 0; + + private readonly cols: number; + private readonly rows: number; + private readonly cwd: string; + private readonly env: NodeJS.ProcessEnv; + private readonly theme: XtermTheme; + private readonly showChrome: boolean; + private readonly windowTitle: string; + private readonly fontSize: number; + private readonly fontFamily: string; + private readonly outputDir: string; + + // ── Factory ────────────────────────────── + + /** + * Create and initialize a TerminalCapture instance + * + * @example + * ```ts + * const t = await TerminalCapture.create({ + * theme: 'dracula', + * chrome: true, + * title: 'qwen-code', + * }); + * ``` + */ + static async create( + options?: TerminalCaptureOptions, + ): Promise { + const instance = new TerminalCapture(options); + await instance.init(); + return instance; + } + + private constructor(options?: TerminalCaptureOptions) { + this.cols = options?.cols ?? 120; + this.rows = options?.rows ?? 40; + this.cwd = options?.cwd ?? process.cwd(); + // Build a clean env for optimal terminal rendering: + // - Remove NO_COLOR (conflicts with FORCE_COLOR, can crash gradient components) + // - Suppress Node.js warnings (noisy in screenshots) + // - Force color output and 256-color terminal + const baseEnv = { ...process.env }; + delete baseEnv['NO_COLOR']; + this.env = options?.env ?? { + ...baseEnv, + FORCE_COLOR: '1', + TERM: 'xterm-256color', + NODE_NO_WARNINGS: '1', + }; + this.showChrome = options?.chrome ?? true; + this.windowTitle = options?.title ?? 'Terminal'; + this.fontSize = options?.fontSize ?? 14; + this.fontFamily = + options?.fontFamily ?? + "'Menlo', 'Monaco', 'Consolas', 'Courier New', monospace"; + this.outputDir = options?.outputDir ?? join(process.cwd(), 'screenshots'); + + // Resolve theme + if (typeof options?.theme === 'string') { + this.theme = THEMES[options.theme] ?? THEMES['dracula']; + } else if (options?.theme && typeof options.theme === 'object') { + this.theme = options.theme; + } else { + this.theme = THEMES['dracula']; + } + } + + // ── Lifecycle ──────────────────────────── + + private async init(): Promise { + // 1. Launch browser + this.browser = await chromium.launch({ headless: true }); + this.page = await this.browser.newPage({ + viewport: { width: 1600, height: 1000 }, + }); + + // 2. Set base HTML (with chrome decoration, container, etc.) + await this.page.setContent(this.buildHTML()); + + // 3. Load xterm.js from node_modules + const xtermDir = this.resolveXtermDir(); + await this.page.addStyleTag({ path: join(xtermDir, 'css', 'xterm.css') }); + await this.page.addScriptTag({ path: join(xtermDir, 'lib', 'xterm.js') }); + + // 4. Create xterm Terminal instance inside the page + + await this.page.evaluate( + ({ cols, rows, theme, fontSize, fontFamily }) => { + const W = window as unknown as Record; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const Terminal = W['Terminal'] as new (opts: unknown) => any; + const term = new Terminal({ + cols, + rows, + theme, + fontFamily, + fontSize, + lineHeight: 1.2, + cursorBlink: false, + allowProposedApi: true, + scrollback: 1000, + }); + + const container = document.getElementById('xterm-container')!; + + term.open(container); + + // Expose to outer scope + W['term'] = term; + W['termReady'] = true; + }, + { + cols: this.cols, + rows: this.rows, + theme: this.theme as unknown as Record, + fontSize: this.fontSize, + fontFamily: this.fontFamily, + }, + ); + + // 5. Wait until terminal is ready + await this.page.waitForFunction( + () => + (window as unknown as Record)['termReady'] === true, + ); + } + + /** + * Spawn a command (via pseudo-terminal) + * + * @example + * ```ts + * await terminal.spawn('node', ['dist/cli.js', '--yolo']); + * ``` + */ + async spawn(command: string, args: string[] = []): Promise { + if (!this.page) { + throw new Error( + 'Not initialized. Use TerminalCapture.create() factory method.', + ); + } + + this.ptyProcess = pty.spawn(command, args, { + name: 'xterm-256color', + cols: this.cols, + rows: this.rows, + cwd: this.cwd, + env: this.env, + }); + + this.ptyProcess.onData((data) => { + this.rawOutput += data; + }); + } + + // ── Input ──────────────────────────────── + + /** + * Input text. Supports `\n` as Enter. + * + * @param text Text to input + * @param options.delay Delay after input (ms), default 10 + * @param options.slow Type character by character (simulate real typing), default false + * + * @example + * ```ts + * await terminal.type('Hello world\n'); // Input + Enter + * await terminal.type('ls -la\n', { slow: true, delay: 80 }); + * ``` + */ + async type( + text: string, + options?: { delay?: number; slow?: boolean }, + ): Promise { + if (!this.ptyProcess) { + throw new Error('No process running. Call spawn() first.'); + } + + // Convert \n to \r for PTY + const translated = text.replace(/\n/g, '\r'); + + if (options?.slow) { + for (const char of translated) { + this.ptyProcess.write(char); + await this.sleep(options.delay ?? 50); + } + } else { + this.ptyProcess.write(translated); + await this.sleep(options?.delay ?? 10); + } + } + + // ── Wait ───────────────────────────────── + + /** + * Wait for specific text to appear in terminal output + * + * @throws Error on timeout + * + * @example + * ```ts + * await terminal.waitFor('Type your message'); + * await terminal.waitFor('tokens', { timeout: 30000 }); + * ``` + */ + async waitFor(text: string, options?: { timeout?: number }): Promise { + const timeout = options?.timeout ?? 15000; + const start = Date.now(); + + while (Date.now() - start < timeout) { + if ( + stripAnsi(this.rawOutput).toLowerCase().includes(text.toLowerCase()) + ) { + return; + } + await this.sleep(200); + } + + throw new Error( + `Timeout (${timeout}ms) waiting for text: "${text}"\n` + + `Last 500 chars of output: ${stripAnsi(this.rawOutput).slice(-500)}`, + ); + } + + /** + * Wait for output to stabilize (no new output within specified time) + * + * @param stableMs Stability detection duration (ms), default 500 + * @param timeout Maximum wait time (ms), default 30000 + * + * @example + * ```ts + * await terminal.idle(); // Default: 500ms with no new output considered stable + * await terminal.idle(2000); // 2s with no new output + * ``` + */ + async idle(stableMs: number = 500, timeout: number = 30000): Promise { + const start = Date.now(); + let lastLength = this.rawOutput.length; + let lastChangeTime = Date.now(); + + while (Date.now() - start < timeout) { + await this.sleep(100); + if (this.rawOutput.length !== lastLength) { + lastLength = this.rawOutput.length; + lastChangeTime = Date.now(); + } else if (Date.now() - lastChangeTime >= stableMs) { + return; + } + } + // Timeout for idle() is not an error — just means output kept coming + } + + /** + * Wait for text to appear, then wait for output to stabilize (common combination) + */ + async waitForAndIdle( + text: string, + options?: { timeout?: number; stableMs?: number }, + ): Promise { + await this.waitFor(text, { timeout: options?.timeout }); + await this.idle(options?.stableMs ?? 300, 5000); + } + + // ── Capture ────────────────────────────── + + /** + * Capture and save a screenshot. Filenames are deterministic (no timestamps) for easy regression comparison. + * + * @param filename Filename, e.g., 'initial.png' + * @param outputDir Output directory, defaults to the outputDir from construction + * @returns Full path to the screenshot file + * + * @example + * ```ts + * await terminal.capture('01-initial.png'); + * await terminal.capture('02-output.png', '/tmp/screenshots'); + * ``` + */ + async capture(filename: string, outputDir?: string): Promise { + if (!this.page) { + throw new Error('Not initialized'); + } + + // 1. Flush all accumulated PTY data to xterm.js + await this.flush(); + + // 2. Wait for xterm.js rendering to complete + await this.sleep(150); + + // 3. Prepare output directory + const dir = outputDir ?? this.outputDir; + mkdirSync(dir, { recursive: true }); + const filepath = join(dir, filename); + + // 4. Screenshot the capture root (terminal + optional chrome) + const element = await this.page.$('#capture-root'); + if (element) { + await element.screenshot({ path: filepath }); + } else { + await this.page.screenshot({ path: filepath }); + } + + console.log(`📸 Captured: ${filepath}`); + return filepath; + } + + /** + * Capture full terminal output (including scrollback buffer) as a long image. + * Suitable for scenarios where output exceeds the visible area, e.g., detailed token lists from /context. + * + * Principle: Temporarily expand xterm.js rows to show complete scrollback, then restore original dimensions after screenshot. + * Note: Only resizes xterm.js inside the browser, not the PTY dimensions, so it won't trigger CLI re-render. + * + * @param filename Filename + * @param outputDir Output directory + * @returns Full path to the screenshot file + * + * @example + * ```ts + * // Regular screenshot (only current viewport) + * await terminal.capture('output.png'); + * // Full-length image (including scrollback buffer) + * await terminal.captureFull('output-full.png'); + * ``` + */ + async captureFull(filename: string, outputDir?: string): Promise { + if (!this.page) { + throw new Error('Not initialized'); + } + + // 1. Flush all accumulated PTY data to xterm.js + await this.flush(); + await this.sleep(150); + + // 2. Query xterm.js for the actual content height (skip trailing empty lines) + const contentLines = await this.page.evaluate(() => { + const W = window as unknown as Record; + const term = W['term'] as { + buffer: { + active: { + length: number; + getLine: (i: number) => + | { + translateToString: (trimRight?: boolean) => string; + } + | undefined; + }; + }; + }; + const buf = term.buffer.active; + let lastNonEmpty = 0; + for (let i = buf.length - 1; i >= 0; i--) { + const line = buf.getLine(i); + if (line && line.translateToString(true).trim().length > 0) { + lastNonEmpty = i; + break; + } + } + return lastNonEmpty + 1; + }); + + const expandedRows = Math.max(contentLines + 2, this.rows); + + // 3. Temporarily resize xterm.js only (NOT the PTY) to show all content + // This avoids sending SIGWINCH to the child process, so the CLI won't re-render + await this.page.evaluate( + ({ cols, rows }: { cols: number; rows: number }) => { + const W = window as unknown as Record; + const term = W['term'] as { + resize: (c: number, r: number) => void; + scrollToTop: () => void; + }; + term.resize(cols, rows); + // Scroll to top to ensure rendering starts from scrollback beginning position + term.scrollToTop(); + }, + { cols: this.cols, rows: expandedRows }, + ); + + // 4. Expand viewport to accommodate the taller terminal + await this.page.setViewportSize({ + width: 1600, + height: Math.max(expandedRows * 22, 1000), // ~22px per row (fontSize 14 * lineHeight 1.2 + padding) + }); + + await this.sleep(300); + + // 5. Screenshot the full content + const dir = outputDir ?? this.outputDir; + mkdirSync(dir, { recursive: true }); + const filepath = join(dir, filename); + + const element = await this.page.$('#capture-root'); + if (element) { + await element.screenshot({ path: filepath }); + } else { + await this.page.screenshot({ path: filepath, fullPage: true }); + } + + // 6. Restore original xterm.js dimensions and viewport + await this.page.evaluate( + ({ cols, rows }: { cols: number; rows: number }) => { + const W = window as unknown as Record; + const term = W['term'] as { resize: (c: number, r: number) => void }; + term.resize(cols, rows); + }, + { cols: this.cols, rows: this.rows }, + ); + + await this.page.setViewportSize({ width: 1600, height: 1000 }); + + console.log(`📸 Captured (full): ${filepath}`); + return filepath; + } + + // ── Output access ──────────────────────── + + /** + * Get cleaned terminal output (without ANSI escape sequences) + */ + getOutput(): string { + return stripAnsi(this.rawOutput); + } + + /** + * Get raw terminal output (with ANSI escape sequences) + */ + getRawOutput(): string { + return this.rawOutput; + } + + // ── Cleanup ────────────────────────────── + + /** + * Release all resources (PTY process, browser) + */ + async close(): Promise { + if (this.ptyProcess) { + try { + this.ptyProcess.kill(); + } catch { + // Process may have already exited + } + this.ptyProcess = null; + } + + if (this.browser) { + await this.browser.close(); + this.browser = null; + this.page = null; + } + } + + // ── Internal: flush PTY → xterm.js ────── + + /** + * Flush accumulated PTY raw output to xterm.js inside the browser. + * Uses xterm.js's write callback to ensure data is fully parsed, + * then waits one requestAnimationFrame to ensure rendering is complete. + */ + private async flush(): Promise { + if (!this.page || this.rawOutput.length <= this.lastFlushedLength) { + return; + } + + const newData = this.rawOutput.slice(this.lastFlushedLength); + this.lastFlushedLength = this.rawOutput.length; + + // Send data in chunks to avoid hitting string size limits + const CHUNK_SIZE = 64 * 1024; + for (let i = 0; i < newData.length; i += CHUNK_SIZE) { + const chunk = newData.slice(i, i + CHUNK_SIZE); + await this.page.evaluate((data: string) => { + return new Promise((resolve) => { + const W = window as unknown as Record; + const term = W['term'] as { + write: (d: string, cb: () => void) => void; + }; + term.write(data, () => { + // Data parsed → wait one frame for rendering + requestAnimationFrame(() => resolve()); + }); + }); + }, chunk); + } + } + + // ── Internal: resolve xterm.js path ───── + + private resolveXtermDir(): string { + try { + const pkgJsonPath = _require.resolve('@xterm/xterm/package.json'); + return dirname(pkgJsonPath); + } catch { + throw new Error( + '@xterm/xterm is not installed.\n' + + 'Run: npm install --save-dev @xterm/xterm', + ); + } + } + + // ── Internal: build HTML ──────────────── + + private buildHTML(): string { + const bg = this.theme.background; + + // Title bar color: slightly lighter than background + // Use a manual approximation instead of color-mix for compatibility + const titleBarBg = this.lighten(bg, 0.08); + + const chromeHTML = this.showChrome + ? ` +
+
+ + + +
+ ${this.escapeHtml(this.windowTitle)} +
+
` + : ''; + + return ` + + + + + + +
+ ${chromeHTML} +
+
+ +`; + } + + // ── Internal: utils ───────────────────── + + private escapeHtml(text: string): string { + return text + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') + .replace(/'/g, '''); + } + + /** + * Lighten a hex color by a factor (0-1) + */ + private lighten(hex: string, factor: number): string { + const h = hex.replace('#', ''); + const r = Math.min( + 255, + parseInt(h.slice(0, 2), 16) + Math.round(255 * factor), + ); + const g = Math.min( + 255, + parseInt(h.slice(2, 4), 16) + Math.round(255 * factor), + ); + const b = Math.min( + 255, + parseInt(h.slice(4, 6), 16) + Math.round(255 * factor), + ); + return `#${r.toString(16).padStart(2, '0')}${g.toString(16).padStart(2, '0')}${b.toString(16).padStart(2, '0')}`; + } + + private sleep(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)); + } +} diff --git a/package-lock.json b/package-lock.json index 9a49d849e..f26e50737 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@qwen-code/qwen-code", - "version": "0.10.1", + "version": "0.11.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@qwen-code/qwen-code", - "version": "0.10.1", + "version": "0.11.1", "workspaces": [ "packages/*" ], @@ -2997,6 +2997,10 @@ "resolved": "packages/sdk-typescript", "link": true }, + "node_modules/@qwen-code/web-templates": { + "resolved": "packages/web-templates", + "link": true + }, "node_modules/@qwen-code/webui": { "resolved": "packages/webui", "link": true @@ -3834,6 +3838,120 @@ "node": ">=6" } }, + "node_modules/@teddyzhu/clipboard": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/@teddyzhu/clipboard/-/clipboard-0.0.5.tgz", + "integrity": "sha512-XA6MG7nLPZzj51agCwDYaVnVVrt0ByJ3G9rl3ar6N4GETAjUKKup6u76SLp2C5yHRWYV9hwMYDn04OGLar0MVg==", + "license": "MIT", + "optional": true, + "engines": { + "node": ">= 10.16.0 < 11 || >= 11.8.0 < 12 || >= 12.0.0" + }, + "optionalDependencies": { + "@teddyzhu/clipboard-darwin-arm64": "0.0.5", + "@teddyzhu/clipboard-darwin-x64": "0.0.5", + "@teddyzhu/clipboard-linux-arm64-gnu": "0.0.5", + "@teddyzhu/clipboard-linux-x64-gnu": "0.0.5", + "@teddyzhu/clipboard-win32-arm64-msvc": "0.0.5", + "@teddyzhu/clipboard-win32-x64-msvc": "0.0.5" + } + }, + "node_modules/@teddyzhu/clipboard-darwin-arm64": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/@teddyzhu/clipboard-darwin-arm64/-/clipboard-darwin-arm64-0.0.5.tgz", + "integrity": "sha512-FB3yykRAcw0VLmSjIGFddgew2t20UnLp80NZvi5e/lbsy/3mruHibMHkxHWqzCncuZsHdRsRXS/FmR/ggepW9A==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 10.16.0 < 11 || >= 11.8.0 < 12 || >= 12.0.0" + } + }, + "node_modules/@teddyzhu/clipboard-darwin-x64": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/@teddyzhu/clipboard-darwin-x64/-/clipboard-darwin-x64-0.0.5.tgz", + "integrity": "sha512-tiDazMpLf2dS7BZUif3da3DLJima8E/CnexB3CNgjQf12CFJ+D1cPcj/CgfvMYZgFQSsYyACpQNfXn4hmVbymA==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 10.16.0 < 11 || >= 11.8.0 < 12 || >= 12.0.0" + } + }, + "node_modules/@teddyzhu/clipboard-linux-arm64-gnu": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/@teddyzhu/clipboard-linux-arm64-gnu/-/clipboard-linux-arm64-gnu-0.0.5.tgz", + "integrity": "sha512-qcokM+BaXn4iG4o4nYGHdfC04pr54S2F7x2o5osFhG3hMVYHZLR/8NKcYDKELnebpH612nW2bNRoWWy14lM45g==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10.16.0 < 11 || >= 11.8.0 < 12 || >= 12.0.0" + } + }, + "node_modules/@teddyzhu/clipboard-linux-x64-gnu": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/@teddyzhu/clipboard-linux-x64-gnu/-/clipboard-linux-x64-gnu-0.0.5.tgz", + "integrity": "sha512-Ogh4zYM9s537WJszSvKrPAoKQZ2grnY7Xy6szyJp2+84uQKWNbvZkATODAsRUn48zr9gqL3PZeUqkIBaz8sCpQ==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10.16.0 < 11 || >= 11.8.0 < 12 || >= 12.0.0" + } + }, + "node_modules/@teddyzhu/clipboard-win32-arm64-msvc": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/@teddyzhu/clipboard-win32-arm64-msvc/-/clipboard-win32-arm64-msvc-0.0.5.tgz", + "integrity": "sha512-TuU+7e8qYc0T++sIArHTmqr+nfqiTfJ6gdrb1e8yDJb6MM3EFxCd2VonTqLQL1YpUdfcH+/rdMarG2rvCwvEhQ==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 10.16.0 < 11 || >= 11.8.0 < 12 || >= 12.0.0" + } + }, + "node_modules/@teddyzhu/clipboard-win32-x64-msvc": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/@teddyzhu/clipboard-win32-x64-msvc/-/clipboard-win32-x64-msvc-0.0.5.tgz", + "integrity": "sha512-f1Br5bI+INNDifjkOI1woZsIxsoW0rRej/4kaaJvZcMxxkSG9TMT2LYOjTF2g+DtXw32lsGvWICN6c3JiHeG7Q==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 10.16.0 < 11 || >= 11.8.0 < 12 || >= 12.0.0" + } + }, "node_modules/@testing-library/dom": { "version": "10.4.1", "resolved": "https://registry.npmjs.org/@testing-library/dom/-/dom-10.4.1.tgz", @@ -4403,6 +4521,13 @@ "kleur": "^3.0.3" } }, + "node_modules/@types/prop-types": { + "version": "15.7.15", + "resolved": "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.15.tgz", + "integrity": "sha512-F6bEyamV9jKGAFBEmlQnesRPGOQqS2+Uwi0Em15xenOxHaf2hv6L8YCVn3rPdPJOiJfPiCnLIRyvwVaqMY3MIw==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/qrcode-terminal": { "version": "0.12.2", "resolved": "https://registry.npmjs.org/@types/qrcode-terminal/-/qrcode-terminal-0.12.2.tgz", @@ -18655,12 +18780,13 @@ }, "packages/cli": { "name": "@qwen-code/qwen-code", - "version": "0.10.1", + "version": "0.11.1", "dependencies": { "@google/genai": "1.30.0", "@iarna/toml": "^2.2.5", "@modelcontextprotocol/sdk": "^1.25.1", "@qwen-code/qwen-code-core": "file:../core", + "@qwen-code/web-templates": "file:../web-templates", "@types/update-notifier": "^6.0.8", "ansi-regex": "^6.2.2", "command-exists": "^1.2.9", @@ -18676,6 +18802,7 @@ "ink-spinner": "^5.0.0", "lowlight": "^3.3.0", "open": "^10.1.2", + "p-limit": "^7.3.0", "prompts": "^2.4.2", "qrcode-terminal": "^0.12.0", "react": "^19.1.0", @@ -18720,6 +18847,15 @@ }, "engines": { "node": ">=20" + }, + "optionalDependencies": { + "@teddyzhu/clipboard": "^0.0.5", + "@teddyzhu/clipboard-darwin-arm64": "0.0.5", + "@teddyzhu/clipboard-darwin-x64": "0.0.5", + "@teddyzhu/clipboard-linux-arm64-gnu": "0.0.5", + "@teddyzhu/clipboard-linux-x64-gnu": "0.0.5", + "@teddyzhu/clipboard-win32-arm64-msvc": "0.0.5", + "@teddyzhu/clipboard-win32-x64-msvc": "0.0.5" } }, "packages/cli/node_modules/@google/genai": { @@ -19142,6 +19278,21 @@ "url": "https://opencollective.com/node-fetch" } }, + "packages/cli/node_modules/p-limit": { + "version": "7.3.0", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-7.3.0.tgz", + "integrity": "sha512-7cIXg/Z0M5WZRblrsOla88S4wAK+zOQQWeBYfV3qJuJXMr+LnbYjaadrFaS0JILfEDPVqHyKnZ1Z/1d6J9VVUw==", + "license": "MIT", + "dependencies": { + "yocto-queue": "^1.2.1" + }, + "engines": { + "node": ">=20" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "packages/cli/node_modules/qs": { "version": "6.14.0", "resolved": "https://registry.npmjs.org/qs/-/qs-6.14.0.tgz", @@ -19272,9 +19423,21 @@ "node": ">=18.17" } }, + "packages/cli/node_modules/yocto-queue": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-1.2.2.tgz", + "integrity": "sha512-4LCcse/U2MHZ63HAJVE+v71o7yOdIe4cZ70Wpf8D/IyjDKYQLV5GD46B+hSTjJsvV5PztjvHoU580EftxjDZFQ==", + "license": "MIT", + "engines": { + "node": ">=12.20" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "packages/core": { "name": "@qwen-code/qwen-code-core", - "version": "0.10.1", + "version": "0.11.1", "hasInstallScript": true, "dependencies": { "@anthropic-ai/sdk": "^0.36.1", @@ -22754,7 +22917,7 @@ }, "packages/test-utils": { "name": "@qwen-code/qwen-code-test-utils", - "version": "0.10.1", + "version": "0.11.1", "dev": true, "license": "Apache-2.0", "devDependencies": { @@ -22766,7 +22929,7 @@ }, "packages/vscode-ide-companion": { "name": "qwen-code-vscode-ide-companion", - "version": "0.10.1", + "version": "0.11.1", "license": "LICENSE", "dependencies": { "@modelcontextprotocol/sdk": "^1.25.1", @@ -23011,9 +23174,537 @@ "node": ">= 0.6" } }, + "packages/web-templates": { + "name": "@qwen-code/web-templates", + "version": "0.11.1", + "devDependencies": { + "@types/react": "^18.2.0", + "@types/react-dom": "^18.2.0", + "@vitejs/plugin-react": "^4.2.0", + "autoprefixer": "^10.4.22", + "postcss": "^8.5.6", + "tailwindcss": "^3.4.18", + "typescript": "^5.3.3", + "vite": "^5.0.0" + }, + "engines": { + "node": ">=20" + } + }, + "packages/web-templates/node_modules/@esbuild/aix-ppc64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.21.5.tgz", + "integrity": "sha512-1SDgH6ZSPTlggy1yI6+Dbkiz8xzpHJEVAlF/AM1tHPLsf5STom9rwtjE4hKAF20FfXXNTFqEYXyJNWh1GiZedQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "aix" + ], + "engines": { + "node": ">=12" + } + }, + "packages/web-templates/node_modules/@esbuild/android-arm": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.21.5.tgz", + "integrity": "sha512-vCPvzSjpPHEi1siZdlvAlsPxXl7WbOVUBBAowWug4rJHb68Ox8KualB+1ocNvT5fjv6wpkX6o/iEpbDrf68zcg==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=12" + } + }, + "packages/web-templates/node_modules/@esbuild/android-arm64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.21.5.tgz", + "integrity": "sha512-c0uX9VAUBQ7dTDCjq+wdyGLowMdtR/GoC2U5IYk/7D1H1JYC0qseD7+11iMP2mRLN9RcCMRcjC4YMclCzGwS/A==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=12" + } + }, + "packages/web-templates/node_modules/@esbuild/android-x64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.21.5.tgz", + "integrity": "sha512-D7aPRUUNHRBwHxzxRvp856rjUHRFW1SdQATKXH2hqA0kAZb1hKmi02OpYRacl0TxIGz/ZmXWlbZgjwWYaCakTA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=12" + } + }, + "packages/web-templates/node_modules/@esbuild/darwin-arm64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.21.5.tgz", + "integrity": "sha512-DwqXqZyuk5AiWWf3UfLiRDJ5EDd49zg6O9wclZ7kUMv2WRFr4HKjXp/5t8JZ11QbQfUS6/cRCKGwYhtNAY88kQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=12" + } + }, + "packages/web-templates/node_modules/@esbuild/darwin-x64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.21.5.tgz", + "integrity": "sha512-se/JjF8NlmKVG4kNIuyWMV/22ZaerB+qaSi5MdrXtd6R08kvs2qCN4C09miupktDitvh8jRFflwGFBQcxZRjbw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=12" + } + }, + "packages/web-templates/node_modules/@esbuild/freebsd-arm64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.21.5.tgz", + "integrity": "sha512-5JcRxxRDUJLX8JXp/wcBCy3pENnCgBR9bN6JsY4OmhfUtIHe3ZW0mawA7+RDAcMLrMIZaf03NlQiX9DGyB8h4g==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=12" + } + }, + "packages/web-templates/node_modules/@esbuild/freebsd-x64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.21.5.tgz", + "integrity": "sha512-J95kNBj1zkbMXtHVH29bBriQygMXqoVQOQYA+ISs0/2l3T9/kj42ow2mpqerRBxDJnmkUDCaQT/dfNXWX/ZZCQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=12" + } + }, + "packages/web-templates/node_modules/@esbuild/linux-arm": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.21.5.tgz", + "integrity": "sha512-bPb5AHZtbeNGjCKVZ9UGqGwo8EUu4cLq68E95A53KlxAPRmUyYv2D6F0uUI65XisGOL1hBP5mTronbgo+0bFcA==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "packages/web-templates/node_modules/@esbuild/linux-arm64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.21.5.tgz", + "integrity": "sha512-ibKvmyYzKsBeX8d8I7MH/TMfWDXBF3db4qM6sy+7re0YXya+K1cem3on9XgdT2EQGMu4hQyZhan7TeQ8XkGp4Q==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "packages/web-templates/node_modules/@esbuild/linux-ia32": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.21.5.tgz", + "integrity": "sha512-YvjXDqLRqPDl2dvRODYmmhz4rPeVKYvppfGYKSNGdyZkA01046pLWyRKKI3ax8fbJoK5QbxblURkwK/MWY18Tg==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "packages/web-templates/node_modules/@esbuild/linux-loong64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.21.5.tgz", + "integrity": "sha512-uHf1BmMG8qEvzdrzAqg2SIG/02+4/DHB6a9Kbya0XDvwDEKCoC8ZRWI5JJvNdUjtciBGFQ5PuBlpEOXQj+JQSg==", + "cpu": [ + "loong64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "packages/web-templates/node_modules/@esbuild/linux-mips64el": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.21.5.tgz", + "integrity": "sha512-IajOmO+KJK23bj52dFSNCMsz1QP1DqM6cwLUv3W1QwyxkyIWecfafnI555fvSGqEKwjMXVLokcV5ygHW5b3Jbg==", + "cpu": [ + "mips64el" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "packages/web-templates/node_modules/@esbuild/linux-ppc64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.21.5.tgz", + "integrity": "sha512-1hHV/Z4OEfMwpLO8rp7CvlhBDnjsC3CttJXIhBi+5Aj5r+MBvy4egg7wCbe//hSsT+RvDAG7s81tAvpL2XAE4w==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "packages/web-templates/node_modules/@esbuild/linux-riscv64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.21.5.tgz", + "integrity": "sha512-2HdXDMd9GMgTGrPWnJzP2ALSokE/0O5HhTUvWIbD3YdjME8JwvSCnNGBnTThKGEB91OZhzrJ4qIIxk/SBmyDDA==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "packages/web-templates/node_modules/@esbuild/linux-s390x": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.21.5.tgz", + "integrity": "sha512-zus5sxzqBJD3eXxwvjN1yQkRepANgxE9lgOW2qLnmr8ikMTphkjgXu1HR01K4FJg8h1kEEDAqDcZQtbrRnB41A==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "packages/web-templates/node_modules/@esbuild/linux-x64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.21.5.tgz", + "integrity": "sha512-1rYdTpyv03iycF1+BhzrzQJCdOuAOtaqHTWJZCWvijKD2N5Xu0TtVC8/+1faWqcP9iBCWOmjmhoH94dH82BxPQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "packages/web-templates/node_modules/@esbuild/netbsd-x64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.21.5.tgz", + "integrity": "sha512-Woi2MXzXjMULccIwMnLciyZH4nCIMpWQAs049KEeMvOcNADVxo0UBIQPfSmxB3CWKedngg7sWZdLvLczpe0tLg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=12" + } + }, + "packages/web-templates/node_modules/@esbuild/openbsd-x64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.21.5.tgz", + "integrity": "sha512-HLNNw99xsvx12lFBUwoT8EVCsSvRNDVxNpjZ7bPn947b8gJPzeHWyNVhFsaerc0n3TsbOINvRP2byTZ5LKezow==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=12" + } + }, + "packages/web-templates/node_modules/@esbuild/sunos-x64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.21.5.tgz", + "integrity": "sha512-6+gjmFpfy0BHU5Tpptkuh8+uw3mnrvgs+dSPQXQOv3ekbordwnzTVEb4qnIvQcYXq6gzkyTnoZ9dZG+D4garKg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=12" + } + }, + "packages/web-templates/node_modules/@esbuild/win32-arm64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.21.5.tgz", + "integrity": "sha512-Z0gOTd75VvXqyq7nsl93zwahcTROgqvuAcYDUr+vOv8uHhNSKROyU961kgtCD1e95IqPKSQKH7tBTslnS3tA8A==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=12" + } + }, + "packages/web-templates/node_modules/@esbuild/win32-ia32": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.21.5.tgz", + "integrity": "sha512-SWXFF1CL2RVNMaVs+BBClwtfZSvDgtL//G/smwAc5oVK/UPu2Gu9tIaRgFmYFFKrmg3SyAjSrElf0TiJ1v8fYA==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=12" + } + }, + "packages/web-templates/node_modules/@esbuild/win32-x64": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.21.5.tgz", + "integrity": "sha512-tQd/1efJuzPC6rCFwEvLtci/xNFcTZknmXs98FYDfGE4wP9ClFV98nyKrzJKVPMhdDnjzLhdUyMX4PsQAPjwIw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=12" + } + }, + "packages/web-templates/node_modules/@types/react": { + "version": "18.3.28", + "resolved": "https://registry.npmjs.org/@types/react/-/react-18.3.28.tgz", + "integrity": "sha512-z9VXpC7MWrhfWipitjNdgCauoMLRdIILQsAEV+ZesIzBq/oUlxk0m3ApZuMFCXdnS4U7KrI+l3WRUEGQ8K1QKw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/prop-types": "*", + "csstype": "^3.2.2" + } + }, + "packages/web-templates/node_modules/@types/react-dom": { + "version": "18.3.7", + "resolved": "https://registry.npmjs.org/@types/react-dom/-/react-dom-18.3.7.tgz", + "integrity": "sha512-MEe3UeoENYVFXzoXEWsvcpg6ZvlrFNlOQ7EOsvhI3CfAXwzPfO8Qwuxd40nepsYKqyyVQnTdEfv68q91yLcKrQ==", + "dev": true, + "license": "MIT", + "peerDependencies": { + "@types/react": "^18.0.0" + } + }, + "packages/web-templates/node_modules/esbuild": { + "version": "0.21.5", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.21.5.tgz", + "integrity": "sha512-mg3OPMV4hXywwpoDxu3Qda5xCKQi+vCTZq8S9J/EpkhB2HzKXq4SNFZE3+NK93JYxc8VMSep+lOUSC/RVKaBqw==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "bin": { + "esbuild": "bin/esbuild" + }, + "engines": { + "node": ">=12" + }, + "optionalDependencies": { + "@esbuild/aix-ppc64": "0.21.5", + "@esbuild/android-arm": "0.21.5", + "@esbuild/android-arm64": "0.21.5", + "@esbuild/android-x64": "0.21.5", + "@esbuild/darwin-arm64": "0.21.5", + "@esbuild/darwin-x64": "0.21.5", + "@esbuild/freebsd-arm64": "0.21.5", + "@esbuild/freebsd-x64": "0.21.5", + "@esbuild/linux-arm": "0.21.5", + "@esbuild/linux-arm64": "0.21.5", + "@esbuild/linux-ia32": "0.21.5", + "@esbuild/linux-loong64": "0.21.5", + "@esbuild/linux-mips64el": "0.21.5", + "@esbuild/linux-ppc64": "0.21.5", + "@esbuild/linux-riscv64": "0.21.5", + "@esbuild/linux-s390x": "0.21.5", + "@esbuild/linux-x64": "0.21.5", + "@esbuild/netbsd-x64": "0.21.5", + "@esbuild/openbsd-x64": "0.21.5", + "@esbuild/sunos-x64": "0.21.5", + "@esbuild/win32-arm64": "0.21.5", + "@esbuild/win32-ia32": "0.21.5", + "@esbuild/win32-x64": "0.21.5" + } + }, + "packages/web-templates/node_modules/vite": { + "version": "5.4.21", + "resolved": "https://registry.npmjs.org/vite/-/vite-5.4.21.tgz", + "integrity": "sha512-o5a9xKjbtuhY6Bi5S3+HvbRERmouabWbyUcpXXUA1u+GNUKoROi9byOJ8M0nHbHYHkYICiMlqxkg1KkYmm25Sw==", + "dev": true, + "license": "MIT", + "dependencies": { + "esbuild": "^0.21.3", + "postcss": "^8.4.43", + "rollup": "^4.20.0" + }, + "bin": { + "vite": "bin/vite.js" + }, + "engines": { + "node": "^18.0.0 || >=20.0.0" + }, + "funding": { + "url": "https://github.com/vitejs/vite?sponsor=1" + }, + "optionalDependencies": { + "fsevents": "~2.3.3" + }, + "peerDependencies": { + "@types/node": "^18.0.0 || >=20.0.0", + "less": "*", + "lightningcss": "^1.21.0", + "sass": "*", + "sass-embedded": "*", + "stylus": "*", + "sugarss": "*", + "terser": "^5.4.0" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + }, + "less": { + "optional": true + }, + "lightningcss": { + "optional": true + }, + "sass": { + "optional": true + }, + "sass-embedded": { + "optional": true + }, + "stylus": { + "optional": true + }, + "sugarss": { + "optional": true + }, + "terser": { + "optional": true + } + } + }, "packages/webui": { "name": "@qwen-code/webui", - "version": "0.10.1", + "version": "0.11.1", "license": "MIT", "dependencies": { "markdown-it": "^14.1.0" diff --git a/package.json b/package.json index 3ad9a81cd..f91fe59fb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@qwen-code/qwen-code", - "version": "0.10.1", + "version": "0.11.1", "engines": { "node": ">=20.0.0" }, @@ -13,7 +13,7 @@ "url": "git+https://github.com/QwenLM/qwen-code.git" }, "config": { - "sandboxImageUri": "ghcr.io/qwenlm/qwen-code:0.10.1" + "sandboxImageUri": "ghcr.io/qwenlm/qwen-code:0.11.1" }, "scripts": { "start": "cross-env node scripts/start.js", @@ -51,7 +51,7 @@ "typecheck": "npm run typecheck --workspaces --if-present", "check-i18n": "npm run check-i18n --workspace=packages/cli", "preflight": "npm run clean && npm ci && npm run format && npm run lint:ci && npm run build && npm run typecheck && npm run test:ci", - "prepare": "husky && npm run bundle", + "prepare": "husky && npm run build && npm run bundle", "prepare:package": "node scripts/prepare-package.js", "release:version": "node scripts/version.js", "telemetry": "node scripts/telemetry.js", diff --git a/packages/cli/assets/parallel-build.mjs b/packages/cli/assets/parallel-build.mjs deleted file mode 100644 index e070aa08f..000000000 --- a/packages/cli/assets/parallel-build.mjs +++ /dev/null @@ -1,96 +0,0 @@ -import { access, readdir } from 'node:fs/promises'; -import { dirname, join } from 'node:path'; -import { spawn } from 'node:child_process'; -import { fileURLToPath } from 'node:url'; -import process from 'node:process'; - -const assetsDir = dirname(fileURLToPath(import.meta.url)); -const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm'; - -const entries = await readdir(assetsDir, { withFileTypes: true }); -const assetBuilds = []; - -for (const entry of entries) { - if (!entry.isDirectory()) { - continue; - } - - const assetPath = join(assetsDir, entry.name); - const buildPath = join(assetPath, 'build.mjs'); - const packageJsonPath = join(assetPath, 'package.json'); - let hasBuild = false; - let hasPackageJson = false; - - try { - await access(buildPath); - hasBuild = true; - } catch { - // ignore missing build.mjs - } - - try { - await access(packageJsonPath); - hasPackageJson = true; - } catch { - // ignore missing package.json - } - - if (hasBuild || hasPackageJson) { - assetBuilds.push({ - name: entry.name, - assetPath, - buildPath, - useNpm: hasPackageJson, - }); - } -} - -if (assetBuilds.length === 0) { - process.exit(0); -} - -const runCommand = ({ command, args, cwd, label }) => - new Promise((resolve, reject) => { - const child = spawn(command, args, { - cwd, - stdio: 'inherit', - shell: process.platform === 'win32', - }); - - child.on('error', reject); - child.on('exit', (code) => { - if (code === 0) { - resolve(); - } else { - reject(new Error(`${label} failed for ${cwd}.`)); - } - }); - }); - -const runBuild = async (asset) => { - if (asset.useNpm) { - await runCommand({ - command: npmCommand, - args: ['install'], - cwd: asset.assetPath, - label: `npm install`, - }); - - await runCommand({ - command: npmCommand, - args: ['run', 'build'], - cwd: asset.assetPath, - label: `npm run build`, - }); - return; - } - - await runCommand({ - command: process.execPath, - args: [asset.buildPath], - cwd: asset.assetPath, - label: `Node build`, - }); -}; - -await Promise.all(assetBuilds.map((asset) => runBuild(asset))); diff --git a/packages/cli/package.json b/packages/cli/package.json index 7ec5da972..2dc3d87d7 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -1,6 +1,6 @@ { "name": "@qwen-code/qwen-code", - "version": "0.10.1", + "version": "0.11.1", "description": "Qwen Code", "repository": { "type": "git", @@ -19,8 +19,7 @@ } }, "scripts": { - "build:assets": "node ./assets/parallel-build.mjs", - "build": "npm run build:assets && node ../../scripts/build_package.js", + "build": "node ../../scripts/build_package.js", "start": "node dist/index.js", "debug": "node --inspect-brk dist/index.js", "lint": "eslint . --ext .ts,.tsx", @@ -34,13 +33,14 @@ "dist" ], "config": { - "sandboxImageUri": "ghcr.io/qwenlm/qwen-code:0.10.1" + "sandboxImageUri": "ghcr.io/qwenlm/qwen-code:0.11.1" }, "dependencies": { "@google/genai": "1.30.0", "@iarna/toml": "^2.2.5", "@modelcontextprotocol/sdk": "^1.25.1", "@qwen-code/qwen-code-core": "file:../core", + "@qwen-code/web-templates": "file:../web-templates", "@types/update-notifier": "^6.0.8", "ansi-regex": "^6.2.2", "command-exists": "^1.2.9", @@ -56,6 +56,7 @@ "ink-spinner": "^5.0.0", "lowlight": "^3.3.0", "open": "^10.1.2", + "p-limit": "^7.3.0", "prompts": "^2.4.2", "qrcode-terminal": "^0.12.0", "react": "^19.1.0", @@ -81,12 +82,12 @@ "@types/diff": "^7.0.2", "@types/dotenv": "^6.1.1", "@types/node": "^20.11.24", + "@types/prompts": "^2.4.9", "@types/react": "^19.1.8", "@types/react-dom": "^19.1.6", "@types/semver": "^7.7.0", "@types/shell-quote": "^1.7.5", "@types/yargs": "^17.0.32", - "@types/prompts": "^2.4.9", "archiver": "^7.0.1", "ink-testing-library": "^4.0.0", "jsdom": "^26.1.0", @@ -95,6 +96,15 @@ "typescript": "^5.3.3", "vitest": "^3.1.1" }, + "optionalDependencies": { + "@teddyzhu/clipboard": "^0.0.5", + "@teddyzhu/clipboard-darwin-arm64": "0.0.5", + "@teddyzhu/clipboard-darwin-x64": "0.0.5", + "@teddyzhu/clipboard-linux-x64-gnu": "0.0.5", + "@teddyzhu/clipboard-linux-arm64-gnu": "0.0.5", + "@teddyzhu/clipboard-win32-x64-msvc": "0.0.5", + "@teddyzhu/clipboard-win32-arm64-msvc": "0.0.5" + }, "engines": { "node": ">=20" } diff --git a/packages/cli/src/acp-integration/acpAgent.ts b/packages/cli/src/acp-integration/acpAgent.ts index a7ae2cf4c..11878017a 100644 --- a/packages/cli/src/acp-integration/acpAgent.ts +++ b/packages/cli/src/acp-integration/acpAgent.ts @@ -107,6 +107,10 @@ class GeminiAgent { audio: true, embeddedContext: true, }, + sessionCapabilities: { + list: {}, + resume: {}, + }, }, }; } @@ -153,10 +157,14 @@ class GeminiAgent { const session = await this.createAndStoreSession(config); const availableModels = this.buildAvailableModels(config); + const modesData = this.buildModesData(config); + const configOptions = this.buildConfigOptions(config); return { sessionId: session.getId(), models: availableModels, + modes: modesData, + configOptions, }; } @@ -239,25 +247,31 @@ class GeminiAgent { async listSessions( params: acp.ListSessionsRequest, ): Promise { - const sessionService = new SessionService(params.cwd); + const cwd = params.cwd || process.cwd(); + const sessionService = new SessionService(cwd); const result = await sessionService.listSessions({ cursor: params.cursor, size: params.size, }); + const sessions = result.items.map((item) => ({ + cwd: item.cwd, + filePath: item.filePath, + gitBranch: item.gitBranch, + messageCount: item.messageCount, + mtime: item.mtime, + prompt: item.prompt, + sessionId: item.sessionId, + startTime: item.startTime, + title: item.prompt || '(session)', + updatedAt: new Date(item.mtime).toISOString(), + })); + return { - items: result.items.map((item) => ({ - sessionId: item.sessionId, - cwd: item.cwd, - startTime: item.startTime, - mtime: item.mtime, - prompt: item.prompt, - gitBranch: item.gitBranch, - filePath: item.filePath, - messageCount: item.messageCount, - })), - nextCursor: result.nextCursor, hasMore: result.hasMore, + items: sessions, + nextCursor: result.nextCursor, + sessions, }; } @@ -449,6 +463,70 @@ class GeminiAgent { }; } + private buildModesData(config: Config): acp.ModesData { + const currentApprovalMode = config.getApprovalMode(); + + const availableModes = APPROVAL_MODES.map((mode) => ({ + id: mode as ApprovalModeValue, + name: APPROVAL_MODE_INFO[mode].name, + description: APPROVAL_MODE_INFO[mode].description, + })); + + return { + currentModeId: currentApprovalMode as ApprovalModeValue, + availableModes, + }; + } + + private buildConfigOptions(config: Config): acp.ConfigOption[] { + const currentApprovalMode = config.getApprovalMode(); + const currentModelId = this.formatCurrentModelId( + config.getModel() || this.config.getModel() || '', + config.getAuthType(), + ); + + const modeOptions = APPROVAL_MODES.map((mode) => ({ + value: mode, + name: APPROVAL_MODE_INFO[mode].name, + description: APPROVAL_MODE_INFO[mode].description, + })); + + const allConfiguredModels = config.getAllConfiguredModels(); + const modelOptions = allConfiguredModels.map((model) => { + const effectiveModelId = + model.isRuntimeModel && model.runtimeSnapshotId + ? model.runtimeSnapshotId + : model.id; + + return { + value: formatAcpModelId(effectiveModelId, model.authType), + name: model.label, + description: model.description ?? '', + }; + }); + + return [ + { + id: 'mode', + name: 'Mode', + description: 'Session permission mode', + category: 'mode', + type: 'select', + currentValue: currentApprovalMode, + options: modeOptions, + }, + { + id: 'model', + name: 'Model', + description: 'AI model to use', + category: 'model', + type: 'select', + currentValue: currentModelId, + options: modelOptions, + }, + ]; + } + private formatCurrentModelId( baseModelId: string, authType?: AuthType, diff --git a/packages/cli/src/acp-integration/schema.ts b/packages/cli/src/acp-integration/schema.ts index 952ad0bd5..1df709c45 100644 --- a/packages/cli/src/acp-integration/schema.ts +++ b/packages/cli/src/acp-integration/schema.ts @@ -59,7 +59,7 @@ export type CancelNotification = z.infer; export type AuthenticateRequest = z.infer; -export type NewSessionResponse = z.infer; +// Note: NewSessionResponse type is defined later after newSessionResponseSchema export type LoadSessionResponse = z.infer; @@ -285,33 +285,33 @@ export const sessionModelStateSchema = z.object({ currentModelId: modelIdSchema, }); -export const newSessionResponseSchema = z.object({ - sessionId: z.string(), - models: sessionModelStateSchema, -}); +// Note: newSessionResponseSchema is defined later in the file after modesDataSchema export const loadSessionResponseSchema = z.null(); export const sessionListItemSchema = z.object({ cwd: z.string(), - filePath: z.string(), + filePath: z.string().optional(), gitBranch: z.string().optional(), - messageCount: z.number(), - mtime: z.number(), - prompt: z.string(), + messageCount: z.number().optional(), + mtime: z.number().optional(), + prompt: z.string().optional(), sessionId: z.string(), - startTime: z.string(), + startTime: z.string().optional(), + title: z.string(), + updatedAt: z.string(), }); export const listSessionsResponseSchema = z.object({ - hasMore: z.boolean(), - items: z.array(sessionListItemSchema), + hasMore: z.boolean().optional(), + items: z.array(sessionListItemSchema).optional(), nextCursor: z.number().optional(), + sessions: z.array(sessionListItemSchema), }); export const listSessionsRequestSchema = z.object({ cursor: z.number().optional(), - cwd: z.string(), + cwd: z.string().optional(), size: z.number().optional(), }); @@ -405,6 +405,12 @@ export const promptCapabilitiesSchema = z.object({ export const agentCapabilitiesSchema = z.object({ loadSession: z.boolean().optional(), promptCapabilities: promptCapabilitiesSchema.optional(), + sessionCapabilities: z + .object({ + list: z.object({}).optional(), + resume: z.object({}).optional(), + }) + .optional(), }); export const authMethodSchema = z.object({ @@ -451,6 +457,34 @@ export const modesDataSchema = z.object({ availableModes: z.array(modeInfoSchema), }); +export const configOptionSchema = z.object({ + id: z.string(), + name: z.string(), + description: z.string(), + category: z.string(), + type: z.string(), + currentValue: z.string(), + options: z.array( + z.object({ + value: z.string(), + name: z.string(), + description: z.string(), + }), + ), +}); + +export type ConfigOption = z.infer; + +// newSessionResponseSchema includes modes and configOptions for ACP/Zed integration +export const newSessionResponseSchema = z.object({ + sessionId: z.string(), + models: sessionModelStateSchema, + modes: modesDataSchema, + configOptions: z.array(configOptionSchema), +}); + +export type NewSessionResponse = z.infer; + export const agentInfoSchema = z.object({ name: z.string(), title: z.string(), diff --git a/packages/cli/src/acp-integration/service/filesystem.ts b/packages/cli/src/acp-integration/service/filesystem.ts index 17a0cdbcf..9dfbf35b3 100644 --- a/packages/cli/src/acp-integration/service/filesystem.ts +++ b/packages/cli/src/acp-integration/service/filesystem.ts @@ -84,7 +84,11 @@ export class AcpFileSystemService implements FileSystemService { limit: 1, }); // Check if content starts with BOM character (U+FEFF) - return response.content.charCodeAt(0) === 0xfeff; + // Use codePointAt for better Unicode support and check content length first + return ( + response.content.length > 0 && + response.content.codePointAt(0) === 0xfeff + ); } catch { // Fall through to fallback if ACP read fails } diff --git a/packages/cli/src/acp-integration/session/Session.ts b/packages/cli/src/acp-integration/session/Session.ts index d7a5e7395..702f66a07 100644 --- a/packages/cli/src/acp-integration/session/Session.ts +++ b/packages/cli/src/acp-integration/session/Session.ts @@ -516,6 +516,18 @@ export class Session implements SessionContext { ? await invocation.shouldConfirmExecute(abortSignal) : false; + // Check for plan mode enforcement - block non-read-only tools + const isPlanMode = this.config.getApprovalMode() === ApprovalMode.PLAN; + if (isPlanMode && !isExitPlanModeTool && confirmationDetails) { + // In plan mode, block any tool that requires confirmation (write operations) + return errorResponse( + new Error( + `Plan mode is active. The tool "${fc.name}" cannot be executed because it modifies the system. ` + + 'Please use the exit_plan_mode tool to present your plan and exit plan mode before making changes.', + ), + ); + } + if (confirmationDetails) { const content: acp.ToolCallContent[] = []; diff --git a/packages/cli/src/config/config.test.ts b/packages/cli/src/config/config.test.ts index dbe57fd42..5f08dd382 100644 --- a/packages/cli/src/config/config.test.ts +++ b/packages/cli/src/config/config.test.ts @@ -242,9 +242,14 @@ describe('parseArguments', () => { }); it('should allow -r flag as alias for --resume', async () => { - process.argv = ['node', 'script.js', '-r', 'session-123']; + process.argv = [ + 'node', + 'script.js', + '-r', + '123e4567-e89b-12d3-a456-426614174000', + ]; const argv = await parseArguments(); - expect(argv.resume).toBe('session-123'); + expect(argv.resume).toBe('123e4567-e89b-12d3-a456-426614174000'); }); it('should allow -c flag as alias for --continue', async () => { diff --git a/packages/cli/src/config/config.ts b/packages/cli/src/config/config.ts index 4598a742b..48961cdca 100755 --- a/packages/cli/src/config/config.ts +++ b/packages/cli/src/config/config.ts @@ -50,6 +50,19 @@ import { loadSandboxConfig } from './sandboxConfig.js'; import { appEvents } from '../utils/events.js'; import { mcpCommand } from '../commands/mcp.js'; +// UUID v4 regex pattern for validation +const UUID_REGEX = + /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; + +/** + * Validates if a string is a valid UUID format + * @param value - The string to validate + * @returns True if the string is a valid UUID, false otherwise + */ +function isValidUUID(value: string): boolean { + return UUID_REGEX.test(value); +} + import { isWorkspaceTrusted } from './trustedFolders.js'; import { buildWebSearchConfig } from './webSearch.js'; import { writeStderrLine } from '../utils/stdioHelpers.js'; @@ -124,7 +137,6 @@ export interface CliArgs { googleSearchEngineId: string | undefined; webSearchDefault: string | undefined; screenReader: boolean | undefined; - vlmSwitchMode: string | undefined; inputFormat?: string | undefined; outputFormat: string | undefined; includePartialMessages?: boolean; @@ -137,6 +149,8 @@ export interface CliArgs { continue: boolean | undefined; /** Resume a specific session by its ID */ resume: string | undefined; + /** Specify a session ID without session resumption */ + sessionId: string | undefined; maxSessionTurns: number | undefined; coreTools: string[] | undefined; excludeTools: string[] | undefined; @@ -411,13 +425,6 @@ export async function parseArguments(): Promise { type: 'boolean', description: 'Enable screen reader mode for accessibility.', }) - .option('vlm-switch-mode', { - type: 'string', - choices: ['once', 'session', 'persist'], - description: - 'Default behavior when images are detected in input. Values: once (one-time switch), session (switch for entire session), persist (continue with current model). Overrides settings files.', - default: process.env['VLM_SWITCH_MODE'], - }) .option('input-format', { type: 'string', choices: ['text', 'stream-json'], @@ -449,6 +456,10 @@ export async function parseArguments(): Promise { description: 'Resume a specific session by its ID. Use without an ID to show session picker.', }) + .option('session-id', { + type: 'string', + description: 'Specify a session ID for this run.', + }) .option('max-session-turns', { type: 'number', description: 'Maximum number of session turns', @@ -535,6 +546,15 @@ export async function parseArguments(): Promise { if (argv['continue'] && argv['resume']) { return 'Cannot use both --continue and --resume together. Use --continue to resume the latest session, or --resume to resume a specific session.'; } + if (argv['sessionId'] && (argv['continue'] || argv['resume'])) { + return 'Cannot use --session-id with --continue or --resume. Use --session-id to start a new session with a specific ID, or use --continue/--resume to resume an existing session.'; + } + if (argv['sessionId'] && !isValidUUID(argv['sessionId'] as string)) { + return `Invalid --session-id: "${argv['sessionId']}". Must be a valid UUID (e.g., "123e4567-e89b-12d3-a456-426614174000").`; + } + if (argv['resume'] && !isValidUUID(argv['resume'] as string)) { + return `Invalid --resume: "${argv['resume']}". Must be a valid UUID (e.g., "123e4567-e89b-12d3-a456-426614174000").`; + } return true; }), ) @@ -673,14 +693,21 @@ export async function loadCliConfig( } // Automatically load output-language.md if it exists - let outputLanguageFilePath: string | undefined = path.join( + const projectStorage = new Storage(cwd); + const projectOutputLanguagePath = path.join( + projectStorage.getQwenDir(), + 'output-language.md', + ); + const globalOutputLanguagePath = path.join( Storage.getGlobalQwenDir(), 'output-language.md', ); - if (fs.existsSync(outputLanguageFilePath)) { - // output-language.md found - will be added to context files - } else { - outputLanguageFilePath = undefined; + + let outputLanguageFilePath: string | undefined; + if (fs.existsSync(projectOutputLanguagePath)) { + outputLanguageFilePath = projectOutputLanguagePath; + } else if (fs.existsSync(globalOutputLanguagePath)) { + outputLanguageFilePath = globalOutputLanguagePath; } const fileService = new FileDiscoveryService(cwd); @@ -875,9 +902,6 @@ export async function loadCliConfig( ? argv.screenReader : (settings.ui?.accessibility?.screenReader ?? false); - const vlmSwitchMode = - argv.vlmSwitchMode || settings.experimental?.vlmSwitchMode; - let sessionId: string | undefined; let sessionData: ResumedSessionData | undefined; @@ -899,6 +923,17 @@ export async function loadCliConfig( process.exit(1); } } + } else if (argv['sessionId']) { + // Use provided session ID without session resumption + // Check if session ID is already in use + const sessionService = new SessionService(cwd); + const exists = await sessionService.sessionExists(argv['sessionId']); + if (exists) { + const message = `Error: Session Id ${argv['sessionId']} is already in use.`; + writeStderrLine(message); + process.exit(1); + } + sessionId = argv['sessionId']; } const modelProvidersConfig = settings.modelProviders; @@ -963,6 +998,7 @@ export async function loadCliConfig( modelProvidersConfig, generationConfigSources: resolvedCliConfig.sources, generationConfig: resolvedCliConfig.generationConfig, + warnings: resolvedCliConfig.warnings, cliVersion: await getCliVersion(), webSearch: buildWebSearchConfig(argv, settings, selectedAuthType), summarizeToolOutput: settings.model?.summarizeToolOutput, @@ -977,7 +1013,6 @@ export async function loadCliConfig( skipNextSpeakerCheck: settings.model?.skipNextSpeakerCheck, skipLoopDetection: settings.model?.skipLoopDetection ?? false, skipStartupContext: settings.model?.skipStartupContext ?? false, - vlmSwitchMode, truncateToolOutputThreshold: settings.tools?.truncateToolOutputThreshold, truncateToolOutputLines: settings.tools?.truncateToolOutputLines, enableToolOutputTruncation: settings.tools?.enableToolOutputTruncation, diff --git a/packages/cli/src/config/keyBindings.ts b/packages/cli/src/config/keyBindings.ts index 8737866ea..7499a8c68 100644 --- a/packages/cli/src/config/keyBindings.ts +++ b/packages/cli/src/config/keyBindings.ts @@ -50,6 +50,7 @@ export enum Command { QUIT = 'quit', EXIT = 'exit', SHOW_MORE_LINES = 'showMoreLines', + RETRY_LAST = 'retryLast', // Shell commands REVERSE_SEARCH = 'reverseSearch', @@ -78,6 +79,7 @@ export interface KeyBinding { command?: boolean; /** Paste operation requirement: true=must be paste, false=must not be paste, undefined=ignore */ paste?: boolean; + meta?: boolean; } /** @@ -152,7 +154,16 @@ export const defaultKeyBindings: KeyBindingConfig = { { key: 'x', ctrl: true }, { sequence: '\x18', ctrl: true }, ], - [Command.PASTE_CLIPBOARD_IMAGE]: [{ key: 'v', ctrl: true }], + [Command.PASTE_CLIPBOARD_IMAGE]: + process.platform === 'win32' + ? [ + { key: 'v', command: true }, + { key: 'v', meta: true }, + ] + : [ + { key: 'v', ctrl: true }, + { key: 'v', command: true }, + ], // App level bindings [Command.TOGGLE_TOOL_DESCRIPTIONS]: [{ key: 't', ctrl: true }], @@ -160,6 +171,7 @@ export const defaultKeyBindings: KeyBindingConfig = { [Command.QUIT]: [{ key: 'c', ctrl: true }], [Command.EXIT]: [{ key: 'd', ctrl: true }], [Command.SHOW_MORE_LINES]: [{ key: 's', ctrl: true }], + [Command.RETRY_LAST]: [{ key: 'y', ctrl: true }], // Shell commands [Command.REVERSE_SEARCH]: [{ key: 'r', ctrl: true }], diff --git a/packages/cli/src/config/settings.ts b/packages/cli/src/config/settings.ts index fe2f63bd1..e261cc723 100644 --- a/packages/cli/src/config/settings.ts +++ b/packages/cli/src/config/settings.ts @@ -122,8 +122,6 @@ const MIGRATION_MAP: Record = { skipStartupContext: 'model.skipStartupContext', enableOpenAILogging: 'model.enableOpenAILogging', tavilyApiKey: 'advanced.tavilyApiKey', - vlmSwitchMode: 'experimental.vlmSwitchMode', - visionModelPreview: 'experimental.visionModelPreview', }; // Settings that need boolean inversion during migration (V1 -> V3) diff --git a/packages/cli/src/config/settingsSchema.test.ts b/packages/cli/src/config/settingsSchema.test.ts index fc902234f..cfde449ca 100644 --- a/packages/cli/src/config/settingsSchema.test.ts +++ b/packages/cli/src/config/settingsSchema.test.ts @@ -28,7 +28,7 @@ describe('SettingsSchema', () => { 'mcp', 'security', 'advanced', - 'experimental', + 'webSearch', ]; expectedSettings.forEach((setting) => { diff --git a/packages/cli/src/config/settingsSchema.ts b/packages/cli/src/config/settingsSchema.ts index 711bf3e8e..fd6c3e85b 100644 --- a/packages/cli/src/config/settingsSchema.ts +++ b/packages/cli/src/config/settingsSchema.ts @@ -147,7 +147,7 @@ const SETTINGS_SCHEMA = { requiresRestart: true, default: {} as Record, description: - 'Environment variables to set as fallback defaults. These are loaded with the lowest priority: system environment variables > .env files > settings.env.', + 'Environment variables to set as fallback defaults. These are loaded with the lowest priority: system environment variables > .env files > settings.json env field.', showInDialog: false, mergeStrategy: MergeStrategy.SHALLOW_MERGE, }, @@ -373,7 +373,7 @@ const SETTINGS_SCHEMA = { label: 'Show Line Numbers in Code', category: 'UI', requiresRestart: false, - default: false, + default: true, description: 'Show line numbers in the code output.', showInDialog: true, }, @@ -1176,38 +1176,6 @@ const SETTINGS_SCHEMA = { description: 'Configuration for web search providers.', showInDialog: false, }, - - experimental: { - type: 'object', - label: 'Experimental', - category: 'Experimental', - requiresRestart: true, - default: {}, - description: 'Setting to enable experimental features', - showInDialog: false, - properties: { - visionModelPreview: { - type: 'boolean', - label: 'Vision Model Preview', - category: 'Experimental', - requiresRestart: false, - default: true, - description: - 'Enable vision model support and auto-switching functionality. When disabled, vision models like qwen-vl-max-latest will be hidden and auto-switching will not occur.', - showInDialog: false, - }, - vlmSwitchMode: { - type: 'string', - label: 'VLM Switch Mode', - category: 'Experimental', - requiresRestart: false, - default: undefined as string | undefined, - description: - 'Default behavior when images are detected in input. Values: once (one-time switch), session (switch for entire session), persist (continue with current model). If not set, user will be prompted each time. This is a temporary experimental feature.', - showInDialog: false, - }, - }, - }, } as const satisfies SettingsSchema; export type SettingsSchemaType = typeof SETTINGS_SCHEMA; diff --git a/packages/cli/src/constants/codingPlan.ts b/packages/cli/src/constants/codingPlan.ts index e55aeb93d..03c164d8e 100644 --- a/packages/cli/src/constants/codingPlan.ts +++ b/packages/cli/src/constants/codingPlan.ts @@ -7,6 +7,14 @@ import { createHash } from 'node:crypto'; import type { ProviderModelConfig as ModelConfig } from '@qwen-code/qwen-code-core'; +/** + * Coding plan regions + */ +export enum CodingPlanRegion { + CHINA = 'china', + GLOBAL = 'global', +} + /** * Coding plan template - array of model configurations * When user provides an api-key, these configs will be cloned with envKey pointing to the stored api-key @@ -14,48 +22,300 @@ import type { ProviderModelConfig as ModelConfig } from '@qwen-code/qwen-code-co export type CodingPlanTemplate = ModelConfig[]; /** - * Environment variable key for storing the coding plan API key + * Environment variable key for storing the coding plan API key. + * Unified key for both regions since they are mutually exclusive. */ export const CODING_PLAN_ENV_KEY = 'BAILIAN_CODING_PLAN_API_KEY'; -/** - * CODING_PLAN_MODELS defines the model configurations for coding-plan mode. - */ -export const CODING_PLAN_MODELS: CodingPlanTemplate = [ - { - id: 'qwen3-coder-plus', - name: 'qwen3-coder-plus', - baseUrl: 'https://coding.dashscope.aliyuncs.com/v1', - description: 'qwen3-coder-plus model from Bailian Coding Plan', - envKey: CODING_PLAN_ENV_KEY, - }, - { - id: 'qwen3-max-2026-01-23', - name: 'qwen3-max-2026-01-23', - description: - 'qwen3-max model with thinking enabled from Bailian Coding Plan', - baseUrl: 'https://coding.dashscope.aliyuncs.com/v1', - envKey: CODING_PLAN_ENV_KEY, - generationConfig: { - extra_body: { - enable_thinking: true, - }, - }, - }, -]; - /** * Computes the version hash for the coding plan template. * Uses SHA256 of the JSON-serialized template for deterministic versioning. + * @param template - The template to compute version for * @returns Hexadecimal string representing the template version */ -export function computeCodingPlanVersion(): string { - const templateString = JSON.stringify(CODING_PLAN_MODELS); +export function computeCodingPlanVersion(template: CodingPlanTemplate): string { + const templateString = JSON.stringify(template); return createHash('sha256').update(templateString).digest('hex'); } /** - * Current version of the coding plan template. - * Computed at runtime from the template content. + * Generate the complete coding plan template for a specific region. + * China region uses legacy description to maintain backward compatibility. + * Global region uses new description with region indicator. + * @param region - The region to generate template for + * @returns Complete model configuration array for the region */ -export const CODING_PLAN_VERSION = computeCodingPlanVersion(); +export function generateCodingPlanTemplate( + region: CodingPlanRegion, +): CodingPlanTemplate { + if (region === CodingPlanRegion.CHINA) { + // China region uses legacy fields to maintain backward compatibility + // This ensures existing users don't get prompted for unnecessary updates + return [ + { + id: 'qwen3.5-plus', + name: '[Bailian Coding Plan] qwen3.5-plus', + baseUrl: 'https://coding.dashscope.aliyuncs.com/v1', + envKey: CODING_PLAN_ENV_KEY, + generationConfig: { + extra_body: { + enable_thinking: true, + }, + contextWindowSize: 1000000, + }, + }, + { + id: 'qwen3-coder-plus', + name: '[Bailian Coding Plan] qwen3-coder-plus', + baseUrl: 'https://coding.dashscope.aliyuncs.com/v1', + envKey: CODING_PLAN_ENV_KEY, + generationConfig: { + contextWindowSize: 1000000, + }, + }, + { + id: 'qwen3-coder-next', + name: '[Bailian Coding Plan] qwen3-coder-next', + baseUrl: 'https://coding.dashscope.aliyuncs.com/v1', + envKey: CODING_PLAN_ENV_KEY, + generationConfig: { + contextWindowSize: 262144, + }, + }, + { + id: 'qwen3-max-2026-01-23', + name: '[Bailian Coding Plan] qwen3-max-2026-01-23', + baseUrl: 'https://coding.dashscope.aliyuncs.com/v1', + envKey: CODING_PLAN_ENV_KEY, + generationConfig: { + extra_body: { + enable_thinking: true, + }, + contextWindowSize: 262144, + }, + }, + { + id: 'glm-4.7', + name: '[Bailian Coding Plan] glm-4.7', + baseUrl: 'https://coding.dashscope.aliyuncs.com/v1', + envKey: CODING_PLAN_ENV_KEY, + generationConfig: { + extra_body: { + enable_thinking: true, + }, + contextWindowSize: 202752, + }, + }, + { + id: 'glm-5', + name: '[Bailian Coding Plan] glm-5', + baseUrl: 'https://coding.dashscope.aliyuncs.com/v1', + envKey: CODING_PLAN_ENV_KEY, + generationConfig: { + extra_body: { + enable_thinking: true, + }, + contextWindowSize: 202752, + }, + }, + { + id: 'MiniMax-M2.5', + name: '[Bailian Coding Plan] MiniMax-M2.5', + baseUrl: 'https://coding.dashscope.aliyuncs.com/v1', + envKey: CODING_PLAN_ENV_KEY, + generationConfig: { + extra_body: { + enable_thinking: true, + }, + contextWindowSize: 1000000, + }, + }, + { + id: 'kimi-k2.5', + name: '[Bailian Coding Plan] kimi-k2.5', + baseUrl: 'https://coding.dashscope.aliyuncs.com/v1', + envKey: CODING_PLAN_ENV_KEY, + generationConfig: { + extra_body: { + enable_thinking: true, + }, + contextWindowSize: 262144, + }, + }, + ]; + } + + // Global region uses Bailian Coding Plan branding for Global/Intl + return [ + { + id: 'qwen3.5-plus', + name: '[Bailian Coding Plan for Global/Intl] qwen3.5-plus', + baseUrl: 'https://coding-intl.dashscope.aliyuncs.com/v1', + envKey: CODING_PLAN_ENV_KEY, + generationConfig: { + extra_body: { + enable_thinking: true, + }, + contextWindowSize: 1000000, + }, + }, + { + id: 'qwen3-coder-plus', + name: '[Bailian Coding Plan for Global/Intl] qwen3-coder-plus', + baseUrl: 'https://coding-intl.dashscope.aliyuncs.com/v1', + envKey: CODING_PLAN_ENV_KEY, + generationConfig: { + contextWindowSize: 1000000, + }, + }, + { + id: 'qwen3-coder-next', + name: '[Bailian Coding Plan for Global/Intl] qwen3-coder-next', + baseUrl: 'https://coding-intl.dashscope.aliyuncs.com/v1', + envKey: CODING_PLAN_ENV_KEY, + generationConfig: { + contextWindowSize: 262144, + }, + }, + { + id: 'qwen3-max-2026-01-23', + name: '[Bailian Coding Plan for Global/Intl] qwen3-max-2026-01-23', + baseUrl: 'https://coding-intl.dashscope.aliyuncs.com/v1', + envKey: CODING_PLAN_ENV_KEY, + generationConfig: { + extra_body: { + enable_thinking: true, + }, + contextWindowSize: 262144, + }, + }, + { + id: 'glm-4.7', + name: '[Bailian Coding Plan for Global/Intl] glm-4.7', + baseUrl: 'https://coding-intl.dashscope.aliyuncs.com/v1', + envKey: CODING_PLAN_ENV_KEY, + generationConfig: { + extra_body: { + enable_thinking: true, + }, + contextWindowSize: 202752, + }, + }, + { + id: 'glm-5', + name: '[Bailian Coding Plan for Global/Intl] glm-5', + baseUrl: 'https://coding-intl.dashscope.aliyuncs.com/v1', + envKey: CODING_PLAN_ENV_KEY, + generationConfig: { + extra_body: { + enable_thinking: true, + }, + contextWindowSize: 202752, + }, + }, + { + id: 'MiniMax-M2.5', + name: '[Bailian Coding Plan for Global/Intl] MiniMax-M2.5', + baseUrl: 'https://coding-intl.dashscope.aliyuncs.com/v1', + envKey: CODING_PLAN_ENV_KEY, + generationConfig: { + extra_body: { + enable_thinking: true, + }, + contextWindowSize: 1000000, + }, + }, + { + id: 'kimi-k2.5', + name: '[Bailian Coding Plan for Global/Intl] kimi-k2.5', + baseUrl: 'https://coding-intl.dashscope.aliyuncs.com/v1', + envKey: CODING_PLAN_ENV_KEY, + generationConfig: { + extra_body: { + enable_thinking: true, + }, + contextWindowSize: 262144, + }, + }, + ]; +} + +/** + * Get the complete configuration for a specific region. + * @param region - The region to use + * @returns Object containing template, baseUrl, and version + */ +export function getCodingPlanConfig(region: CodingPlanRegion) { + const template = generateCodingPlanTemplate(region); + const baseUrl = + region === CodingPlanRegion.CHINA + ? 'https://coding.dashscope.aliyuncs.com/v1' + : 'https://coding-intl.dashscope.aliyuncs.com/v1'; + return { + template, + baseUrl, + version: computeCodingPlanVersion(template), + }; +} + +/** + * Get all unique base URLs for coding plan (used for filtering/config detection). + * @returns Array of base URLs + */ +export function getCodingPlanBaseUrls(): string[] { + return [ + 'https://coding.dashscope.aliyuncs.com/v1', + 'https://coding-intl.dashscope.aliyuncs.com/v1', + ]; +} + +/** + * Check if a config belongs to Coding Plan (any region). + * Returns the region if matched, or false if not a Coding Plan config. + * @param baseUrl - The baseUrl to check + * @param envKey - The envKey to check + * @returns The region if matched, false otherwise + */ +export function isCodingPlanConfig( + baseUrl: string | undefined, + envKey: string | undefined, +): CodingPlanRegion | false { + if (!baseUrl || !envKey) { + return false; + } + + // Must use the unified envKey + if (envKey !== CODING_PLAN_ENV_KEY) { + return false; + } + + // Check which region's baseUrl matches + if (baseUrl === 'https://coding.dashscope.aliyuncs.com/v1') { + return CodingPlanRegion.CHINA; + } + if (baseUrl === 'https://coding-intl.dashscope.aliyuncs.com/v1') { + return CodingPlanRegion.GLOBAL; + } + + return false; +} + +/** + * Get region from baseUrl. + * @param baseUrl - The baseUrl to check + * @returns The region if matched, null otherwise + */ +export function getRegionFromBaseUrl( + baseUrl: string | undefined, +): CodingPlanRegion | null { + if (!baseUrl) return null; + + if (baseUrl === 'https://coding.dashscope.aliyuncs.com/v1') { + return CodingPlanRegion.CHINA; + } + if (baseUrl === 'https://coding-intl.dashscope.aliyuncs.com/v1') { + return CodingPlanRegion.GLOBAL; + } + + return null; +} diff --git a/packages/cli/src/gemini.test.tsx b/packages/cli/src/gemini.test.tsx index 039f0bef3..6c48658ad 100644 --- a/packages/cli/src/gemini.test.tsx +++ b/packages/cli/src/gemini.test.tsx @@ -48,6 +48,7 @@ vi.mock('./config/config.js', () => ({ getSandbox: vi.fn(() => false), getQuestion: vi.fn(() => ''), isInteractive: () => false, + getWarnings: vi.fn(() => []), } as unknown as Config), parseArguments: vi.fn().mockResolvedValue({}), isDebugMode: vi.fn(() => false), @@ -177,6 +178,7 @@ describe('gemini.tsx main function', () => { getGeminiMdFileCount: () => 0, getProjectRoot: () => '/', getOutputFormat: () => OutputFormat.TEXT, + getWarnings: () => [], } as unknown as Config; }); vi.mocked(loadSettings).mockReturnValue({ @@ -341,6 +343,7 @@ describe('gemini.tsx main function', () => { getProjectRoot: () => '/', getInputFormat: () => 'stream-json', getContentGeneratorConfig: () => ({ authType: 'test-auth' }), + getWarnings: () => [], } as unknown as Config; vi.mocked(loadCliConfig).mockResolvedValue(configStub); @@ -438,6 +441,7 @@ describe('gemini.tsx main function kitty protocol', () => { getExperimentalZedIntegration: () => false, getScreenReader: () => false, getGeminiMdFileCount: () => 0, + getWarnings: () => [], } as unknown as Config); vi.mocked(loadSettings).mockReturnValue({ errors: [], @@ -483,7 +487,6 @@ describe('gemini.tsx main function kitty protocol', () => { googleSearchEngineId: undefined, webSearchDefault: undefined, screenReader: undefined, - vlmSwitchMode: undefined, inputFormat: undefined, outputFormat: undefined, includePartialMessages: undefined, @@ -496,6 +499,7 @@ describe('gemini.tsx main function kitty protocol', () => { experimentalLsp: undefined, channel: undefined, chatRecording: undefined, + sessionId: undefined, }); await main(); diff --git a/packages/cli/src/gemini.tsx b/packages/cli/src/gemini.tsx index 08c0631a8..c5e742ee6 100644 --- a/packages/cli/src/gemini.tsx +++ b/packages/cli/src/gemini.tsx @@ -411,6 +411,7 @@ export async function main() { useBuiltinRipgrep: settings.merged.tools?.useBuiltinRipgrep ?? true, })), ...getSettingsWarnings(settings), + ...config.getWarnings(), ]), ]; diff --git a/packages/cli/src/i18n/locales/de.js b/packages/cli/src/i18n/locales/de.js index 5757b135f..1144aa31c 100644 --- a/packages/cli/src/i18n/locales/de.js +++ b/packages/cli/src/i18n/locales/de.js @@ -11,6 +11,12 @@ export default { // ============================================================================ // Help / UI Components // ============================================================================ + // Attachment hints + '↑ to manage attachments': '↑ Anhänge verwalten', + '← → select, Delete to remove, ↓ to exit': + '← → auswählen, Entf zum Löschen, ↓ beenden', + 'Attachments: ': 'Anhänge: ', + 'Basics:': 'Grundlagen:', 'Add context': 'Kontext hinzufügen', 'Use {{symbol}} to specify files for context (e.g., {{example}}) to target specific files or folders.': @@ -151,6 +157,7 @@ export default { 'Enter to confirm, Esc to cancel': 'Enter zum Bestätigen, Esc zum Abbrechen', 'Enter to select, ↑↓ to navigate, Esc to go back': 'Enter zum Auswählen, ↑↓ zum Navigieren, Esc zum Zurückgehen', + 'Enter to submit, Esc to go back': 'Enter zum Absenden, Esc zum Zurückgehen', 'Invalid step: {{step}}': 'Ungültiger Schritt: {{step}}', 'No subagents found.': 'Keine Unteragenten gefunden.', "Use '/agents create' to create your first subagent.": @@ -938,18 +945,22 @@ export default { // Dialogs - Auth // ============================================================================ 'Get started': 'Loslegen', - 'How would you like to authenticate for this project?': - 'Wie möchten Sie sich für dieses Projekt authentifizieren?', + 'Select Authentication Method': 'Authentifizierungsmethode auswählen', 'OpenAI API key is required to use OpenAI authentication.': 'OpenAI API-Schlüssel ist für die OpenAI-Authentifizierung erforderlich.', 'You must select an auth method to proceed. Press Ctrl+C again to exit.': 'Sie müssen eine Authentifizierungsmethode wählen, um fortzufahren. Drücken Sie erneut Strg+C zum Beenden.', - '(Use Enter to Set Auth)': '(Enter zum Festlegen der Authentifizierung)', - 'Terms of Services and Privacy Notice for Qwen Code': - 'Nutzungsbedingungen und Datenschutzhinweis für Qwen Code', + 'Terms of Services and Privacy Notice': + 'Nutzungsbedingungen und Datenschutzhinweis', 'Qwen OAuth': 'Qwen OAuth', + 'Free \u00B7 Up to 1,000 requests/day \u00B7 Qwen latest models': + 'Kostenlos \u00B7 Bis zu 1.000 Anfragen/Tag \u00B7 Qwen neueste Modelle', 'Login with QwenChat account to use daily free quota.': 'Melden Sie sich mit Ihrem QwenChat-Konto an, um das tägliche kostenlose Kontingent zu nutzen.', + 'Paid \u00B7 Up to 6,000 requests/5 hrs \u00B7 All Alibaba Cloud Coding Plan Models': + 'Kostenpflichtig \u00B7 Bis zu 6.000 Anfragen/5 Std. \u00B7 Alle Alibaba Cloud Coding Plan Modelle', + 'Alibaba Cloud Coding Plan': 'Alibaba Cloud Coding Plan', + 'Bring your own API key': 'Eigenen API-Schlüssel verwenden', 'API-KEY': 'API-KEY', 'Use coding plan credentials or your own api-keys/providers.': 'Verwenden Sie Coding Plan-Anmeldedaten oder Ihre eigenen API-Schlüssel/Anbieter.', @@ -979,6 +990,8 @@ export default { 'Warten auf Qwen OAuth-Authentifizierung...', 'Note: Your existing API key in settings.json will not be cleared when using Qwen OAuth. You can switch back to OpenAI authentication later if needed.': 'Hinweis: Ihr bestehender API-Schlüssel in settings.json wird bei Verwendung von Qwen OAuth nicht gelöscht. Sie können später bei Bedarf zur OpenAI-Authentifizierung zurückwechseln.', + 'Note: Your existing API key will not be cleared when using Qwen OAuth.': + 'Hinweis: Ihr bestehender API-Schlüssel wird bei Verwendung von Qwen OAuth nicht gelöscht.', 'Authentication timed out. Please try again.': 'Authentifizierung abgelaufen. Bitte versuchen Sie es erneut.', 'Waiting for auth... (Press ESC or CTRL+C to cancel)': @@ -1028,10 +1041,21 @@ export default { '(default)': '(Standard)', '(set)': '(gesetzt)', '(not set)': '(nicht gesetzt)', + Modality: 'Modalität', + 'Context Window': 'Kontextfenster', + text: 'Text', + 'text-only': 'nur Text', + image: 'Bild', + pdf: 'PDF', + audio: 'Audio', + video: 'Video', + 'not set': 'nicht gesetzt', + none: 'keine', + unknown: 'unbekannt', "Failed to switch model to '{{modelId}}'.\n\n{{error}}": "Modell konnte nicht auf '{{modelId}}' umgestellt werden.\n\n{{error}}", - 'The latest Qwen Coder model from Alibaba Cloud ModelStudio (version: qwen3-coder-plus-2025-09-23)': - 'Das neueste Qwen Coder Modell von Alibaba Cloud ModelStudio (Version: qwen3-coder-plus-2025-09-23)', + 'Qwen 3.5 Plus — efficient hybrid model with leading coding performance': + 'Qwen 3.5 Plus — effizientes Hybridmodell mit führender Programmierleistung', 'The latest Qwen Vision model from Alibaba Cloud ModelStudio (version: qwen3-vl-plus-2025-09-23)': 'Das neueste Qwen Vision Modell von Alibaba Cloud ModelStudio (Version: qwen3-vl-plus-2025-09-23)', @@ -1374,38 +1398,43 @@ export default { 'Erweiterungsseite wird im Browser geöffnet: {{url}}', 'Failed to open browser. Check out the extensions gallery at {{url}}': 'Browser konnte nicht geöffnet werden. Besuchen Sie die Erweiterungsgalerie unter {{url}}', + 'Use /compress when the conversation gets long to summarize history and free up context.': + 'Verwenden Sie /compress, wenn die Unterhaltung lang wird, um den Verlauf zusammenzufassen und Kontext freizugeben.', + 'Start a fresh idea with /clear or /new; the previous session stays available in history.': + 'Starten Sie eine neue Idee mit /clear oder /new; die vorherige Sitzung bleibt im Verlauf verfügbar.', + 'Use /bug to submit issues to the maintainers when something goes off.': + 'Verwenden Sie /bug, um Probleme an die Betreuer zu melden, wenn etwas schiefgeht.', + 'Switch auth type quickly with /auth.': + 'Wechseln Sie den Authentifizierungstyp schnell mit /auth.', + 'You can run any shell commands from Qwen Code using ! (e.g. !ls).': + 'Sie können beliebige Shell-Befehle in Qwen Code mit ! ausführen (z. B. !ls).', + 'Type / to open the command popup; Tab autocompletes slash commands and saved prompts.': + 'Geben Sie / ein, um das Befehlsmenü zu öffnen; Tab vervollständigt Slash-Befehle und gespeicherte Prompts.', + 'You can resume a previous conversation by running qwen --continue or qwen --resume.': + 'Sie können eine frühere Unterhaltung mit qwen --continue oder qwen --resume fortsetzen.', 'You can switch permission mode quickly with Shift+Tab or /approval-mode.': 'Sie können den Berechtigungsmodus schnell mit Shift+Tab oder /approval-mode wechseln.', 'You can switch permission mode quickly with Tab or /approval-mode.': 'Sie können den Berechtigungsmodus schnell mit Tab oder /approval-mode wechseln.', + 'Try /insight to generate personalized insights from your chat history.': + 'Probieren Sie /insight, um personalisierte Erkenntnisse aus Ihrem Chatverlauf zu erstellen.', // ============================================================================ - // Custom API-KEY Configuration + // Custom API Key Configuration // ============================================================================ - 'For advanced users who want to configure models manually.': - 'Für fortgeschrittene Benutzer, die Modelle manuell konfigurieren möchten.', - 'Please configure your models in settings.json:': - 'Bitte konfigurieren Sie Ihre Modelle in settings.json:', - 'Set API key via environment variable (e.g., OPENAI_API_KEY)': - 'API-Schlüssel über Umgebungsvariable setzen (z.B. OPENAI_API_KEY)', - "Add model configuration to modelProviders['openai'] (or other auth types)": - "Modellkonfiguration zu modelProviders['openai'] (oder anderen Authentifizierungstypen) hinzufügen", - 'Each provider needs: id, envKey (required), plus optional baseUrl, generationConfig': - 'Jeder Anbieter benötigt: id, envKey (erforderlich), plus optionale baseUrl, generationConfig', - 'Use /model command to select your preferred model from the configured list': - 'Verwenden Sie den /model-Befehl, um Ihr bevorzugtes Modell aus der konfigurierten Liste auszuwählen', - 'Supported auth types: openai, anthropic, gemini, vertex-ai, etc.': - 'Unterstützte Authentifizierungstypen: openai, anthropic, gemini, vertex-ai, usw.', + 'You can configure your API key and models in settings.json': + 'Sie können Ihren API-Schlüssel und Modelle in settings.json konfigurieren', + 'Refer to the documentation for setup instructions': + 'Einrichtungsanweisungen finden Sie in der Dokumentation', // ============================================================================ // Coding Plan Authentication // ============================================================================ - 'Please enter your API key:': 'Bitte geben Sie Ihren API-Schlüssel ein:', 'API key cannot be empty.': 'API-Schlüssel darf nicht leer sein.', - 'You can get your exclusive Coding Plan API-KEY here:': - 'Hier können Sie Ihren exklusiven Coding Plan API-KEY erhalten:', - 'New model configurations are available for Bailian Coding Plan. Update now?': - 'Neue Modellkonfigurationen sind für Bailian Coding Plan verfügbar. Jetzt aktualisieren?', + 'You can get your Coding Plan API key here': + 'Sie können Ihren Coding-Plan-API-Schlüssel hier erhalten', + 'New model configurations are available for Alibaba Cloud Coding Plan. Update now?': + 'Neue Modellkonfigurationen sind für Alibaba Cloud Coding Plan verfügbar. Jetzt aktualisieren?', 'Coding Plan configuration updated successfully. New models are now available.': 'Coding Plan-Konfiguration erfolgreich aktualisiert. Neue Modelle sind jetzt verfügbar.', 'Coding Plan API key not found. Please re-authenticate with Coding Plan.': @@ -1416,16 +1445,18 @@ export default { // ============================================================================ // Auth Dialog - View Titles and Labels // ============================================================================ - 'Coding Plan': 'Coding Plan', - "Paste your api key of Bailian Coding Plan and you're all set!": - 'Fügen Sie Ihren Bailian Coding Plan API-Schlüssel ein und Sie sind bereit!', - Custom: 'Benutzerdefiniert', - 'More instructions about configuring `modelProviders` manually.': - 'Weitere Anweisungen zur manuellen Konfiguration von `modelProviders`.', - 'Select API-KEY configuration mode:': - 'API-KEY-Konfigurationsmodus auswählen:', - '(Press Escape to go back)': '(Escape drücken zum Zurückgehen)', - '(Press Enter to submit, Escape to cancel)': - '(Enter zum Absenden, Escape zum Abbrechen)', - 'More instructions please check:': 'Weitere Anweisungen finden Sie unter:', + 'Select Region for Coding Plan': 'Region für Coding Plan auswählen', + 'Choose based on where your account is registered': + 'Wählen Sie basierend auf dem Registrierungsort Ihres Kontos', + 'Enter Coding Plan API Key': 'Coding-Plan-API-Schlüssel eingeben', + + // ============================================================================ + // Coding Plan International Updates + // ============================================================================ + 'New model configurations are available for {{region}}. Update now?': + 'Neue Modellkonfigurationen sind für {{region}} verfügbar. Jetzt aktualisieren?', + '{{region}} configuration updated successfully. Model switched to "{{model}}".': + '{{region}}-Konfiguration erfolgreich aktualisiert. Modell auf "{{model}}" umgeschaltet.', + 'Authenticated successfully with {{region}}. API key and model configs saved to settings.json (backed up).': + 'Erfolgreich mit {{region}} authentifiziert. API-Schlüssel und Modellkonfigurationen wurden in settings.json gespeichert (gesichert).', }; diff --git a/packages/cli/src/i18n/locales/en.js b/packages/cli/src/i18n/locales/en.js index f4e299fc1..1c27b760f 100644 --- a/packages/cli/src/i18n/locales/en.js +++ b/packages/cli/src/i18n/locales/en.js @@ -11,6 +11,12 @@ export default { // ============================================================================ // Help / UI Components // ============================================================================ + // Attachment hints + '↑ to manage attachments': '↑ to manage attachments', + '← → select, Delete to remove, ↓ to exit': + '← → select, Delete to remove, ↓ to exit', + 'Attachments: ': 'Attachments: ', + 'Basics:': 'Basics:', 'Add context': 'Add context', 'Use {{symbol}} to specify files for context (e.g., {{example}}) to target specific files or folders.': @@ -172,6 +178,7 @@ export default { 'Enter to confirm, Esc to cancel': 'Enter to confirm, Esc to cancel', 'Enter to select, ↑↓ to navigate, Esc to go back': 'Enter to select, ↑↓ to navigate, Esc to go back', + 'Enter to submit, Esc to go back': 'Enter to submit, Esc to go back', 'Invalid step: {{step}}': 'Invalid step: {{step}}', 'No subagents found.': 'No subagents found.', "Use '/agents create' to create your first subagent.": @@ -929,18 +936,22 @@ export default { // Dialogs - Auth // ============================================================================ 'Get started': 'Get started', - 'How would you like to authenticate for this project?': - 'How would you like to authenticate for this project?', + 'Select Authentication Method': 'Select Authentication Method', 'OpenAI API key is required to use OpenAI authentication.': 'OpenAI API key is required to use OpenAI authentication.', 'You must select an auth method to proceed. Press Ctrl+C again to exit.': 'You must select an auth method to proceed. Press Ctrl+C again to exit.', - '(Use Enter to Set Auth)': '(Use Enter to Set Auth)', - 'Terms of Services and Privacy Notice for Qwen Code': - 'Terms of Services and Privacy Notice for Qwen Code', + 'Terms of Services and Privacy Notice': + 'Terms of Services and Privacy Notice', 'Qwen OAuth': 'Qwen OAuth', + 'Free \u00B7 Up to 1,000 requests/day \u00B7 Qwen latest models': + 'Free \u00B7 Up to 1,000 requests/day \u00B7 Qwen latest models', 'Login with QwenChat account to use daily free quota.': 'Login with QwenChat account to use daily free quota.', + 'Paid \u00B7 Up to 6,000 requests/5 hrs \u00B7 All Alibaba Cloud Coding Plan Models': + 'Paid \u00B7 Up to 6,000 requests/5 hrs \u00B7 All Alibaba Cloud Coding Plan Models', + 'Alibaba Cloud Coding Plan': 'Alibaba Cloud Coding Plan', + 'Bring your own API key': 'Bring your own API key', 'API-KEY': 'API-KEY', 'Use coding plan credentials or your own api-keys/providers.': 'Use coding plan credentials or your own api-keys/providers.', @@ -968,6 +979,8 @@ export default { 'Waiting for Qwen OAuth authentication...', 'Note: Your existing API key in settings.json will not be cleared when using Qwen OAuth. You can switch back to OpenAI authentication later if needed.': 'Note: Your existing API key in settings.json will not be cleared when using Qwen OAuth. You can switch back to OpenAI authentication later if needed.', + 'Note: Your existing API key will not be cleared when using Qwen OAuth.': + 'Note: Your existing API key will not be cleared when using Qwen OAuth.', 'Authentication timed out. Please try again.': 'Authentication timed out. Please try again.', 'Waiting for auth... (Press ESC or CTRL+C to cancel)': @@ -1015,10 +1028,21 @@ export default { '(default)': '(default)', '(set)': '(set)', '(not set)': '(not set)', + Modality: 'Modality', + 'Context Window': 'Context Window', + text: 'text', + 'text-only': 'text-only', + image: 'image', + pdf: 'pdf', + audio: 'audio', + video: 'video', + 'not set': 'not set', + none: 'none', + unknown: 'unknown', "Failed to switch model to '{{modelId}}'.\n\n{{error}}": "Failed to switch model to '{{modelId}}'.\n\n{{error}}", - 'The latest Qwen Coder model from Alibaba Cloud ModelStudio (version: qwen3-coder-plus-2025-09-23)': - 'The latest Qwen Coder model from Alibaba Cloud ModelStudio (version: qwen3-coder-plus-2025-09-23)', + 'Qwen 3.5 Plus — efficient hybrid model with leading coding performance': + 'Qwen 3.5 Plus — efficient hybrid model with leading coding performance', 'The latest Qwen Vision model from Alibaba Cloud ModelStudio (version: qwen3-vl-plus-2025-09-23)': 'The latest Qwen Vision model from Alibaba Cloud ModelStudio (version: qwen3-vl-plus-2025-09-23)', @@ -1109,6 +1133,8 @@ export default { 'You can switch permission mode quickly with Shift+Tab or /approval-mode.', 'You can switch permission mode quickly with Tab or /approval-mode.': 'You can switch permission mode quickly with Tab or /approval-mode.', + 'Try /insight to generate personalized insights from your chat history.': + 'Try /insight to generate personalized insights from your chat history.', // ============================================================================ // Exit Screen / Stats @@ -1370,17 +1396,26 @@ export default { 'Failed to open browser. Check out the extensions gallery at {{url}}': 'Failed to open browser. Check out the extensions gallery at {{url}}', + // ============================================================================ + // Retry / Rate Limit + // ============================================================================ + 'Rate limit error: {{reason}}': 'Rate limit error: {{reason}}', + 'Retrying in {{seconds}} seconds… (attempt {{attempt}}/{{maxRetries}})': + 'Retrying in {{seconds}} seconds… (attempt {{attempt}}/{{maxRetries}})', + 'Press Ctrl+Y to retry': 'Press Ctrl+Y to retry', + 'No failed request to retry.': 'No failed request to retry.', + 'to retry last request': 'to retry last request', + // ============================================================================ // Coding Plan Authentication // ============================================================================ - 'Please enter your API key:': 'Please enter your API key:', 'API key cannot be empty.': 'API key cannot be empty.', - 'You can get your exclusive Coding Plan API-KEY here:': - 'You can get your exclusive Coding Plan API-KEY here:', + 'You can get your Coding Plan API key here': + 'You can get your Coding Plan API key here', 'API key is stored in settings.env. You can migrate it to a .env file for better security.': 'API key is stored in settings.env. You can migrate it to a .env file for better security.', - 'New model configurations are available for Bailian Coding Plan. Update now?': - 'New model configurations are available for Bailian Coding Plan. Update now?', + 'New model configurations are available for Alibaba Cloud Coding Plan. Update now?': + 'New model configurations are available for Alibaba Cloud Coding Plan. Update now?', 'Coding Plan configuration updated successfully. New models are now available.': 'Coding Plan configuration updated successfully. New models are now available.', 'Coding Plan API key not found. Please re-authenticate with Coding Plan.': @@ -1389,35 +1424,28 @@ export default { 'Failed to update Coding Plan configuration: {{message}}', // ============================================================================ - // Custom API-KEY Configuration + // Custom API Key Configuration // ============================================================================ - 'For advanced users who want to configure models manually.': - 'For advanced users who want to configure models manually.', - 'Please configure your models in settings.json:': - 'Please configure your models in settings.json:', - 'Set API key via environment variable (e.g., OPENAI_API_KEY)': - 'Set API key via environment variable (e.g., OPENAI_API_KEY)', - "Add model configuration to modelProviders['openai'] (or other auth types)": - "Add model configuration to modelProviders['openai'] (or other auth types)", - 'Each provider needs: id, envKey (required), plus optional baseUrl, generationConfig': - 'Each provider needs: id, envKey (required), plus optional baseUrl, generationConfig', - 'Use /model command to select your preferred model from the configured list': - 'Use /model command to select your preferred model from the configured list', - 'Supported auth types: openai, anthropic, gemini, vertex-ai, etc.': - 'Supported auth types: openai, anthropic, gemini, vertex-ai, etc.', - 'More instructions please check:': 'More instructions please check:', + 'You can configure your API key and models in settings.json': + 'You can configure your API key and models in settings.json', + 'Refer to the documentation for setup instructions': + 'Refer to the documentation for setup instructions', // ============================================================================ // Auth Dialog - View Titles and Labels // ============================================================================ - 'Coding Plan': 'Coding Plan', - "Paste your api key of Bailian Coding Plan and you're all set!": - "Paste your api key of Bailian Coding Plan and you're all set!", - Custom: 'Custom', - 'More instructions about configuring `modelProviders` manually.': - 'More instructions about configuring `modelProviders` manually.', - 'Select API-KEY configuration mode:': 'Select API-KEY configuration mode:', - '(Press Escape to go back)': '(Press Escape to go back)', - '(Press Enter to submit, Escape to cancel)': - '(Press Enter to submit, Escape to cancel)', + 'Select Region for Coding Plan': 'Select Region for Coding Plan', + 'Choose based on where your account is registered': + 'Choose based on where your account is registered', + 'Enter Coding Plan API Key': 'Enter Coding Plan API Key', + + // ============================================================================ + // Coding Plan International Updates + // ============================================================================ + 'New model configurations are available for {{region}}. Update now?': + 'New model configurations are available for {{region}}. Update now?', + '{{region}} configuration updated successfully. Model switched to "{{model}}".': + '{{region}} configuration updated successfully. Model switched to "{{model}}".', + 'Authenticated successfully with {{region}}. API key and model configs saved to settings.json (backed up).': + 'Authenticated successfully with {{region}}. API key and model configs saved to settings.json (backed up).', }; diff --git a/packages/cli/src/i18n/locales/ja.js b/packages/cli/src/i18n/locales/ja.js index f9d95b34a..634cec49d 100644 --- a/packages/cli/src/i18n/locales/ja.js +++ b/packages/cli/src/i18n/locales/ja.js @@ -142,6 +142,7 @@ export default { 'Enter to confirm, Esc to cancel': 'Enter で確定、Esc でキャンセル', 'Enter to select, ↑↓ to navigate, Esc to go back': 'Enter で選択、↑↓ で移動、Esc で戻る', + 'Enter to submit, Esc to go back': 'Enter で送信、Esc で戻る', 'Invalid step: {{step}}': '無効なステップ: {{step}}', 'No subagents found.': 'サブエージェントが見つかりません', "Use '/agents create' to create your first subagent.": @@ -671,18 +672,21 @@ export default { '🎯 Overall Goal:': '🎯 全体目標:', // Dialogs - Auth 'Get started': '始める', - 'How would you like to authenticate for this project?': - 'このプロジェクトの認証方法を選択してください:', + 'Select Authentication Method': '認証方法を選択', 'OpenAI API key is required to use OpenAI authentication.': 'OpenAI認証を使用するには OpenAI APIキーが必要です', 'You must select an auth method to proceed. Press Ctrl+C again to exit.': '続行するには認証方法を選択してください。Ctrl+C をもう一度押すと終了します', - '(Use Enter to Set Auth)': '(Enter で認証を設定)', - 'Terms of Services and Privacy Notice for Qwen Code': - 'Qwen Code の利用規約とプライバシー通知', + 'Terms of Services and Privacy Notice': '利用規約とプライバシー通知', 'Qwen OAuth': 'Qwen OAuth', + 'Free \u00B7 Up to 1,000 requests/day \u00B7 Qwen latest models': + '無料 \u00B7 1日最大1,000リクエスト \u00B7 Qwen最新モデル', 'Login with QwenChat account to use daily free quota.': 'QwenChatアカウントでログインして、毎日の無料クォータをご利用ください。', + 'Paid \u00B7 Up to 6,000 requests/5 hrs \u00B7 All Alibaba Cloud Coding Plan Models': + '有料 \u00B7 5時間最大6,000リクエスト \u00B7 すべての Alibaba Cloud Coding Plan モデル', + 'Alibaba Cloud Coding Plan': 'Alibaba Cloud Coding Plan', + 'Bring your own API key': '自分のAPIキーを使用', 'API-KEY': 'API-KEY', 'Use coding plan credentials or your own api-keys/providers.': 'Coding Planの認証情報またはご自身のAPIキー/プロバイダーをご利用ください。', @@ -710,6 +714,8 @@ export default { 'Waiting for Qwen OAuth authentication...': 'Qwen OAuth認証を待っています...', 'Note: Your existing API key in settings.json will not be cleared when using Qwen OAuth. You can switch back to OpenAI authentication later if needed.': '注: Qwen OAuthを使用しても、settings.json内の既存のAPIキーはクリアされません。必要に応じて後でOpenAI認証に切り替えることができます', + 'Note: Your existing API key will not be cleared when using Qwen OAuth.': + '注: Qwen OAuthを使用しても、既存のAPIキーはクリアされません。', 'Authentication timed out. Please try again.': '認証がタイムアウトしました。再度お試しください', 'Waiting for auth... (Press ESC or CTRL+C to cancel)': @@ -731,8 +737,19 @@ export default { // Dialogs - Model 'Select Model': 'モデルを選択', '(Press Esc to close)': '(Esc で閉じる)', - 'The latest Qwen Coder model from Alibaba Cloud ModelStudio (version: qwen3-coder-plus-2025-09-23)': - 'Alibaba Cloud ModelStudioの最新Qwen Coderモデル(バージョン: qwen3-coder-plus-2025-09-23)', + Modality: 'モダリティ', + 'Context Window': 'コンテキストウィンドウ', + text: 'テキスト', + 'text-only': 'テキストのみ', + image: '画像', + pdf: 'PDF', + audio: '音声', + video: '動画', + 'not set': '未設定', + none: 'なし', + unknown: '不明', + 'Qwen 3.5 Plus — efficient hybrid model with leading coding performance': + 'Qwen 3.5 Plus — 効率的なハイブリッドモデル、業界トップクラスのコーディング性能', 'The latest Qwen Vision model from Alibaba Cloud ModelStudio (version: qwen3-vl-plus-2025-09-23)': 'Alibaba Cloud ModelStudioの最新Qwen Visionモデル(バージョン: qwen3-vl-plus-2025-09-23)', // Dialogs - Permissions @@ -783,6 +800,27 @@ export default { "Starting OAuth authentication for MCP server '{{name}}'...": "MCPサーバー '{{name}}' のOAuth認証を開始中...", // Startup Tips + 'Tips:': 'ヒント:', + 'Use /compress when the conversation gets long to summarize history and free up context.': + '会話が長くなったら /compress で履歴を要約し、コンテキストを解放できます。', + 'Start a fresh idea with /clear or /new; the previous session stays available in history.': + '/clear または /new で新しいアイデアを始められます。前のセッションは履歴に残ります。', + 'Use /bug to submit issues to the maintainers when something goes off.': + '問題が発生したら /bug でメンテナーに報告できます。', + 'Switch auth type quickly with /auth.': + '/auth で認証タイプをすばやく切り替えられます。', + 'You can run any shell commands from Qwen Code using ! (e.g. !ls).': + 'Qwen Code から ! を使って任意のシェルコマンドを実行できます(例: !ls)。', + 'Type / to open the command popup; Tab autocompletes slash commands and saved prompts.': + '/ を入力してコマンドポップアップを開きます。Tab でスラッシュコマンドと保存済みプロンプトを補完できます。', + 'You can resume a previous conversation by running qwen --continue or qwen --resume.': + 'qwen --continue または qwen --resume で前の会話を再開できます。', + 'You can switch permission mode quickly with Shift+Tab or /approval-mode.': + 'Shift+Tab または /approval-mode で権限モードをすばやく切り替えられます。', + 'You can switch permission mode quickly with Tab or /approval-mode.': + 'Tab または /approval-mode で権限モードをすばやく切り替えられます。', + 'Try /insight to generate personalized insights from your chat history.': + '/insight でチャット履歴からパーソナライズされたインサイトを生成できます。', 'Tips for getting started:': '始めるためのヒント:', '1. Ask questions, edit files, or run commands.': '1. 質問したり、ファイルを編集したり、コマンドを実行したりできます', @@ -891,32 +929,19 @@ export default { ], // ============================================================================ - // Custom API-KEY Configuration + // Custom API Key Configuration // ============================================================================ - 'For advanced users who want to configure models manually.': - 'モデルを手動で設定したい上級ユーザー向け。', - 'Please configure your models in settings.json:': - 'settings.json でモデルを設定してください:', - 'Set API key via environment variable (e.g., OPENAI_API_KEY)': - '環境変数を使用して API キーを設定してください(例:OPENAI_API_KEY)', - "Add model configuration to modelProviders['openai'] (or other auth types)": - "modelProviders['openai'](または他の認証タイプ)にモデル設定を追加してください", - 'Each provider needs: id, envKey (required), plus optional baseUrl, generationConfig': - '各プロバイダーには:id、envKey(必須)、およびオプションの baseUrl、generationConfig が必要です', - 'Use /model command to select your preferred model from the configured list': - '/model コマンドを使用して、設定済みリストからお好みのモデルを選択してください', - 'Supported auth types: openai, anthropic, gemini, vertex-ai, etc.': - 'サポートされている認証タイプ:openai、anthropic、gemini、vertex-ai など', + 'You can configure your API key and models in settings.json': + 'settings.json で API キーとモデルを設定できます', + 'Refer to the documentation for setup instructions': + 'セットアップ手順はドキュメントを参照してください', // ============================================================================ // Coding Plan Authentication // ============================================================================ - 'Please enter your API key:': 'APIキーを入力してください:', 'API key cannot be empty.': 'APIキーは空にできません。', - 'You can get your exclusive Coding Plan API-KEY here:': - 'Coding Plan の API-KEY はこちらで取得できます:', - 'New model configurations are available for Bailian Coding Plan. Update now?': - 'Bailian Coding Plan の新しいモデル設定が利用可能です。今すぐ更新しますか?', + 'You can get your Coding Plan API key here': + 'Coding Plan APIキーはこちらで取得できます', 'Coding Plan configuration updated successfully. New models are now available.': 'Coding Plan の設定が正常に更新されました。新しいモデルが利用可能になりました。', 'Coding Plan API key not found. Please re-authenticate with Coding Plan.': @@ -927,15 +952,18 @@ export default { // ============================================================================ // Auth Dialog - View Titles and Labels // ============================================================================ - 'Coding Plan': 'Coding Plan', - "Paste your api key of Bailian Coding Plan and you're all set!": - 'Bailian Coding PlanのAPIキーを貼り付けるだけで準備完了です!', - Custom: 'カスタム', - 'More instructions about configuring `modelProviders` manually.': - '`modelProviders`を手動で設定する方法の詳細はこちら。', - 'Select API-KEY configuration mode:': 'API-KEY設定モードを選択してください:', - '(Press Escape to go back)': '(Escapeキーで戻る)', - '(Press Enter to submit, Escape to cancel)': - '(Enterで送信、Escapeでキャンセル)', - 'More instructions please check:': '詳細な手順はこちらをご確認ください:', + 'Select Region for Coding Plan': 'Coding Planのリージョンを選択', + 'Choose based on where your account is registered': + 'アカウントの登録先に応じて選択してください', + 'Enter Coding Plan API Key': 'Coding Plan APIキーを入力', + + // ============================================================================ + // Coding Plan International Updates + // ============================================================================ + 'New model configurations are available for {{region}}. Update now?': + '{{region}} の新しいモデル設定が利用可能です。今すぐ更新しますか?', + '{{region}} configuration updated successfully. Model switched to "{{model}}".': + '{{region}} の設定が正常に更新されました。モデルが "{{model}}" に切り替わりました。', + 'Authenticated successfully with {{region}}. API key and model configs saved to settings.json (backed up).': + '{{region}} での認証に成功しました。APIキーとモデル設定が settings.json に保存されました(バックアップ済み)。', }; diff --git a/packages/cli/src/i18n/locales/pt.js b/packages/cli/src/i18n/locales/pt.js index 8613c3076..729ebbd74 100644 --- a/packages/cli/src/i18n/locales/pt.js +++ b/packages/cli/src/i18n/locales/pt.js @@ -173,6 +173,7 @@ export default { 'Enter to confirm, Esc to cancel': 'Enter para confirmar, Esc para cancelar', 'Enter to select, ↑↓ to navigate, Esc to go back': 'Enter para selecionar, ↑↓ para navegar, Esc para voltar', + 'Enter to submit, Esc to go back': 'Enter para enviar, Esc para voltar', 'Invalid step: {{step}}': 'Etapa inválida: {{step}}', 'No subagents found.': 'Nenhum subagente encontrado.', "Use '/agents create' to create your first subagent.": @@ -950,18 +951,22 @@ export default { // Dialogs - Auth // ============================================================================ 'Get started': 'Começar', - 'How would you like to authenticate for this project?': - 'Como você gostaria de se autenticar para este projeto?', + 'Select Authentication Method': 'Selecionar Método de Autenticação', 'OpenAI API key is required to use OpenAI authentication.': 'A chave da API do OpenAI é necessária para usar a autenticação do OpenAI.', 'You must select an auth method to proceed. Press Ctrl+C again to exit.': 'Você deve selecionar um método de autenticação para prosseguir. Pressione Ctrl+C novamente para sair.', - '(Use Enter to Set Auth)': '(Use Enter para Definir Autenticação)', - 'Terms of Services and Privacy Notice for Qwen Code': - 'Termos de Serviço e Aviso de Privacidade do Qwen Code', + 'Terms of Services and Privacy Notice': + 'Termos de Serviço e Aviso de Privacidade', 'Qwen OAuth': 'Qwen OAuth', + 'Free \u00B7 Up to 1,000 requests/day \u00B7 Qwen latest models': + 'Gratuito \u00B7 Até 1.000 solicitações/dia \u00B7 Modelos Qwen mais recentes', 'Login with QwenChat account to use daily free quota.': 'Faça login com sua conta QwenChat para usar a cota gratuita diária.', + 'Paid \u00B7 Up to 6,000 requests/5 hrs \u00B7 All Alibaba Cloud Coding Plan Models': + 'Pago \u00B7 Até 6.000 solicitações/5 hrs \u00B7 Todos os modelos Alibaba Cloud Coding Plan', + 'Alibaba Cloud Coding Plan': 'Alibaba Cloud Coding Plan', + 'Bring your own API key': 'Traga sua própria chave API', 'API-KEY': 'API-KEY', 'Use coding plan credentials or your own api-keys/providers.': 'Use credenciais do Coding Plan ou suas próprias chaves API/provedores.', @@ -989,6 +994,8 @@ export default { 'Aguardando autenticação Qwen OAuth...', 'Note: Your existing API key in settings.json will not be cleared when using Qwen OAuth. You can switch back to OpenAI authentication later if needed.': 'Nota: Sua chave de API existente no settings.json não será limpa ao usar o Qwen OAuth. Você pode voltar para a autenticação do OpenAI mais tarde, se necessário.', + 'Note: Your existing API key will not be cleared when using Qwen OAuth.': + 'Nota: Sua chave de API existente não será limpa ao usar o Qwen OAuth.', 'Authentication timed out. Please try again.': 'A autenticação expirou. Tente novamente.', 'Waiting for auth... (Press ESC or CTRL+C to cancel)': @@ -1037,10 +1044,21 @@ export default { '(default)': '(padrão)', '(set)': '(definido)', '(not set)': '(não definido)', + Modality: 'Modalidade', + 'Context Window': 'Janela de Contexto', + text: 'texto', + 'text-only': 'somente texto', + image: 'imagem', + pdf: 'PDF', + audio: 'áudio', + video: 'vídeo', + 'not set': 'não definido', + none: 'nenhum', + unknown: 'desconhecido', "Failed to switch model to '{{modelId}}'.\n\n{{error}}": "Falha ao trocar o modelo para '{{modelId}}'.\n\n{{error}}", - 'The latest Qwen Coder model from Alibaba Cloud ModelStudio (version: qwen3-coder-plus-2025-09-23)': - 'O modelo Qwen Coder mais recente do Alibaba Cloud ModelStudio (versão: qwen3-coder-plus-2025-09-23)', + 'Qwen 3.5 Plus — efficient hybrid model with leading coding performance': + 'Qwen 3.5 Plus — modelo híbrido eficiente com desempenho líder em programação', 'The latest Qwen Vision model from Alibaba Cloud ModelStudio (version: qwen3-vl-plus-2025-09-23)': 'O modelo Qwen Vision mais recente do Alibaba Cloud ModelStudio (versão: qwen3-vl-plus-2025-09-23)', @@ -1132,6 +1150,8 @@ export default { 'Você pode retomar uma conversa anterior executando qwen --continue ou qwen --resume.', 'You can switch permission mode quickly with Shift+Tab or /approval-mode.': 'Você pode alternar o modo de permissão rapidamente com Shift+Tab ou /approval-mode.', + 'Try /insight to generate personalized insights from your chat history.': + 'Experimente /insight para gerar insights personalizados do seu histórico de conversas.', // ============================================================================ // Exit Screen / Stats @@ -1394,32 +1414,21 @@ export default { 'Falha ao abrir o navegador. Confira a galeria de extensões em {{url}}', // ============================================================================ - // Custom API-KEY Configuration + // Custom API Key Configuration // ============================================================================ - 'For advanced users who want to configure models manually.': - 'Para usuários avançados que desejam configurar modelos manualmente.', - 'Please configure your models in settings.json:': - 'Por favor, configure seus modelos em settings.json:', - 'Set API key via environment variable (e.g., OPENAI_API_KEY)': - 'Defina a chave de API via variável de ambiente (ex: OPENAI_API_KEY)', - "Add model configuration to modelProviders['openai'] (or other auth types)": - "Adicione a configuração do modelo a modelProviders['openai'] (ou outros tipos de autenticação)", - 'Each provider needs: id, envKey (required), plus optional baseUrl, generationConfig': - 'Cada provedor precisa de: id, envKey (obrigatório), além de baseUrl e generationConfig opcionais', - 'Use /model command to select your preferred model from the configured list': - 'Use o comando /model para selecionar seu modelo preferido da lista configurada', - 'Supported auth types: openai, anthropic, gemini, vertex-ai, etc.': - 'Tipos de autenticação suportados: openai, anthropic, gemini, vertex-ai, etc.', + 'You can configure your API key and models in settings.json': + 'Você pode configurar sua chave de API e modelos em settings.json', + 'Refer to the documentation for setup instructions': + 'Consulte a documentação para instruções de configuração', // ============================================================================ // Coding Plan Authentication // ============================================================================ - 'Please enter your API key:': 'Por favor, digite sua chave de API:', 'API key cannot be empty.': 'A chave de API não pode estar vazia.', - 'You can get your exclusive Coding Plan API-KEY here:': - 'Você pode obter sua chave de API exclusiva do Coding Plan aqui:', - 'New model configurations are available for Bailian Coding Plan. Update now?': - 'Novas configurações de modelo estão disponíveis para o Bailian Coding Plan. Atualizar agora?', + 'You can get your Coding Plan API key here': + 'Você pode obter sua chave de API do Coding Plan aqui', + 'New model configurations are available for Alibaba Cloud Coding Plan. Update now?': + 'Novas configurações de modelo estão disponíveis para o Alibaba Cloud Coding Plan. Atualizar agora?', 'Coding Plan configuration updated successfully. New models are now available.': 'Configuração do Coding Plan atualizada com sucesso. Novos modelos agora estão disponíveis.', 'Coding Plan API key not found. Please re-authenticate with Coding Plan.': @@ -1430,16 +1439,18 @@ export default { // ============================================================================ // Auth Dialog - View Titles and Labels // ============================================================================ - 'Coding Plan': 'Coding Plan', - "Paste your api key of Bailian Coding Plan and you're all set!": - 'Cole sua chave de API do Bailian Coding Plan e pronto!', - Custom: 'Personalizado', - 'More instructions about configuring `modelProviders` manually.': - 'Mais instruções sobre como configurar `modelProviders` manualmente.', - 'Select API-KEY configuration mode:': - 'Selecione o modo de configuração da API-KEY:', - '(Press Escape to go back)': '(Pressione Escape para voltar)', - '(Press Enter to submit, Escape to cancel)': - '(Pressione Enter para enviar, Escape para cancelar)', - 'More instructions please check:': 'Mais instruções, consulte:', + 'Select Region for Coding Plan': 'Selecionar região do Coding Plan', + 'Choose based on where your account is registered': + 'Escolha com base em onde sua conta está registrada', + 'Enter Coding Plan API Key': 'Inserir chave de API do Coding Plan', + + // ============================================================================ + // Coding Plan International Updates + // ============================================================================ + 'New model configurations are available for {{region}}. Update now?': + 'Novas configurações de modelo estão disponíveis para o {{region}}. Atualizar agora?', + '{{region}} configuration updated successfully. Model switched to "{{model}}".': + 'Configuração do {{region}} atualizada com sucesso. Modelo alterado para "{{model}}".', + 'Authenticated successfully with {{region}}. API key and model configs saved to settings.json (backed up).': + 'Autenticado com sucesso com {{region}}. Chave de API e configurações de modelo salvas em settings.json (com backup).', }; diff --git a/packages/cli/src/i18n/locales/ru.js b/packages/cli/src/i18n/locales/ru.js index 92c9f8c50..867de9b9a 100644 --- a/packages/cli/src/i18n/locales/ru.js +++ b/packages/cli/src/i18n/locales/ru.js @@ -11,6 +11,12 @@ export default { // ============================================================================ // Справка / Компоненты интерфейса // ============================================================================ + // Attachment hints + '↑ to manage attachments': '↑ управление вложениями', + '← → select, Delete to remove, ↓ to exit': + '← → выбрать, Delete удалить, ↓ выйти', + 'Attachments: ': 'Вложения: ', + 'Basics:': 'Основы:', 'Add context': 'Добавить контекст', 'Use {{symbol}} to specify files for context (e.g., {{example}}) to target specific files or folders.': @@ -175,6 +181,7 @@ export default { 'Enter to confirm, Esc to cancel': 'Enter для подтверждения, Esc для отмены', 'Enter to select, ↑↓ to navigate, Esc to go back': 'Enter для выбора, ↑↓ для навигации, Esc для возврата', + 'Enter to submit, Esc to go back': 'Enter для отправки, Esc для возврата', 'Invalid step: {{step}}': 'Неверный шаг: {{step}}', 'No subagents found.': 'Подагенты не найдены.', "Use '/agents create' to create your first subagent.": @@ -944,18 +951,22 @@ export default { // Диалоги - Авторизация // ============================================================================ 'Get started': 'Начать', - 'How would you like to authenticate for this project?': - 'Как вы хотите авторизоваться для этого проекта?', + 'Select Authentication Method': 'Выберите метод авторизации', 'OpenAI API key is required to use OpenAI authentication.': 'Для использования авторизации OpenAI требуется ключ API OpenAI.', 'You must select an auth method to proceed. Press Ctrl+C again to exit.': 'Вы должны выбрать метод авторизации для продолжения. Нажмите Ctrl+C снова для выхода.', - '(Use Enter to Set Auth)': '(Enter для установки авторизации)', - 'Terms of Services and Privacy Notice for Qwen Code': - 'Условия обслуживания и уведомление о конфиденциальности для Qwen Code', + 'Terms of Services and Privacy Notice': + 'Условия обслуживания и уведомление о конфиденциальности', 'Qwen OAuth': 'Qwen OAuth', + 'Free \u00B7 Up to 1,000 requests/day \u00B7 Qwen latest models': + 'Бесплатно \u00B7 До 1 000 запросов/день \u00B7 Новейшие модели Qwen', 'Login with QwenChat account to use daily free quota.': 'Войдите с помощью аккаунта QwenChat, чтобы использовать ежедневную бесплатную квоту.', + 'Paid \u00B7 Up to 6,000 requests/5 hrs \u00B7 All Alibaba Cloud Coding Plan Models': + 'Платно \u00B7 До 6 000 запросов/5 часов \u00B7 Все модели Alibaba Cloud Coding Plan', + 'Alibaba Cloud Coding Plan': 'Alibaba Cloud Coding Plan', + 'Bring your own API key': 'Используйте свой API-ключ', 'API-KEY': 'API-KEY', 'Use coding plan credentials or your own api-keys/providers.': 'Используйте учетные данные Coding Plan или свои собственные API-ключи/провайдеры.', @@ -983,6 +994,8 @@ export default { 'Ожидание авторизации Qwen OAuth...', 'Note: Your existing API key in settings.json will not be cleared when using Qwen OAuth. You can switch back to OpenAI authentication later if needed.': 'Примечание: Ваш существующий ключ API в settings.json не будет удален при использовании Qwen OAuth. Вы можете переключиться обратно на авторизацию OpenAI позже при необходимости.', + 'Note: Your existing API key will not be cleared when using Qwen OAuth.': + 'Примечание: Ваш существующий ключ API не будет удален при использовании Qwen OAuth.', 'Authentication timed out. Please try again.': 'Время ожидания авторизации истекло. Пожалуйста, попробуйте снова.', 'Waiting for auth... (Press ESC or CTRL+C to cancel)': @@ -1030,10 +1043,21 @@ export default { '(default)': '(по умолчанию)', '(set)': '(установлено)', '(not set)': '(не задано)', + Modality: 'Модальность', + 'Context Window': 'Контекстное окно', + text: 'текст', + 'text-only': 'только текст', + image: 'изображение', + pdf: 'PDF', + audio: 'аудио', + video: 'видео', + 'not set': 'не задано', + none: 'нет', + unknown: 'неизвестно', "Failed to switch model to '{{modelId}}'.\n\n{{error}}": "Не удалось переключиться на модель '{{modelId}}'.\n\n{{error}}", - 'The latest Qwen Coder model from Alibaba Cloud ModelStudio (version: qwen3-coder-plus-2025-09-23)': - 'Последняя модель Qwen Coder от Alibaba Cloud ModelStudio (версия: qwen3-coder-plus-2025-09-23)', + 'Qwen 3.5 Plus — efficient hybrid model with leading coding performance': + 'Qwen 3.5 Plus — эффективная гибридная модель с лидирующей производительностью в программировании', 'The latest Qwen Vision model from Alibaba Cloud ModelStudio (version: qwen3-vl-plus-2025-09-23)': 'Последняя модель Qwen Vision от Alibaba Cloud ModelStudio (версия: qwen3-vl-plus-2025-09-23)', @@ -1378,38 +1402,43 @@ export default { 'Открываем страницу расширений в браузере: {{url}}', 'Failed to open browser. Check out the extensions gallery at {{url}}': 'Не удалось открыть браузер. Посетите галерею расширений по адресу {{url}}', + 'Use /compress when the conversation gets long to summarize history and free up context.': + 'Используйте /compress, когда разговор становится длинным, чтобы подвести итог и освободить контекст.', + 'Start a fresh idea with /clear or /new; the previous session stays available in history.': + 'Начните новую идею с /clear или /new; предыдущая сессия останется в истории.', + 'Use /bug to submit issues to the maintainers when something goes off.': + 'Используйте /bug, чтобы сообщить о проблемах разработчикам.', + 'Switch auth type quickly with /auth.': + 'Быстро переключите тип аутентификации с помощью /auth.', + 'You can run any shell commands from Qwen Code using ! (e.g. !ls).': + 'Вы можете выполнять любые shell-команды в Qwen Code с помощью ! (например, !ls).', + 'Type / to open the command popup; Tab autocompletes slash commands and saved prompts.': + 'Введите /, чтобы открыть меню команд; Tab автодополняет слэш-команды и сохранённые промпты.', + 'You can resume a previous conversation by running qwen --continue or qwen --resume.': + 'Вы можете продолжить предыдущий разговор, запустив qwen --continue или qwen --resume.', 'You can switch permission mode quickly with Shift+Tab or /approval-mode.': 'Вы можете быстро переключать режим разрешений с помощью Shift+Tab или /approval-mode.', 'You can switch permission mode quickly with Tab or /approval-mode.': 'Вы можете быстро переключать режим разрешений с помощью Tab или /approval-mode.', + 'Try /insight to generate personalized insights from your chat history.': + 'Попробуйте /insight, чтобы получить персонализированные выводы из истории чатов.', // ============================================================================ - // Custom API-KEY Configuration + // Custom API Key Configuration // ============================================================================ - 'For advanced users who want to configure models manually.': - 'Для продвинутых пользователей, которые хотят настраивать модели вручную.', - 'Please configure your models in settings.json:': - 'Пожалуйста, настройте ваши модели в settings.json:', - 'Set API key via environment variable (e.g., OPENAI_API_KEY)': - 'Установите ключ API через переменную окружения (например, OPENAI_API_KEY)', - "Add model configuration to modelProviders['openai'] (or other auth types)": - "Добавьте конфигурацию модели в modelProviders['openai'] (или другие типы аутентификации)", - 'Each provider needs: id, envKey (required), plus optional baseUrl, generationConfig': - 'Каждому провайдеру нужны: id, envKey (обязательно), а также опциональные baseUrl, generationConfig', - 'Use /model command to select your preferred model from the configured list': - 'Используйте команду /model, чтобы выбрать предпочитаемую модель из настроенного списка', - 'Supported auth types: openai, anthropic, gemini, vertex-ai, etc.': - 'Поддерживаемые типы аутентификации: openai, anthropic, gemini, vertex-ai и др.', + 'You can configure your API key and models in settings.json': + 'Вы можете настроить API-ключ и модели в settings.json', + 'Refer to the documentation for setup instructions': + 'Инструкции по настройке см. в документации', // ============================================================================ // Coding Plan Authentication // ============================================================================ - 'Please enter your API key:': 'Пожалуйста, введите ваш API-ключ:', 'API key cannot be empty.': 'API-ключ не может быть пустым.', - 'You can get your exclusive Coding Plan API-KEY here:': - 'Получите свой эксклюзивный API-KEY Coding Plan здесь:', - 'New model configurations are available for Bailian Coding Plan. Update now?': - 'Доступны новые конфигурации моделей для Bailian Coding Plan. Обновить сейчас?', + 'You can get your Coding Plan API key here': + 'Вы можете получить API-ключ Coding Plan здесь', + 'New model configurations are available for Alibaba Cloud Coding Plan. Update now?': + 'Доступны новые конфигурации моделей для Alibaba Cloud Coding Plan. Обновить сейчас?', 'Coding Plan configuration updated successfully. New models are now available.': 'Конфигурация Coding Plan успешно обновлена. Новые модели теперь доступны.', 'Coding Plan API key not found. Please re-authenticate with Coding Plan.': @@ -1420,15 +1449,18 @@ export default { // ============================================================================ // Auth Dialog - View Titles and Labels // ============================================================================ - 'Coding Plan': 'Coding Plan', - "Paste your api key of Bailian Coding Plan and you're all set!": - 'Вставьте ваш API-ключ Bailian Coding Plan и всё готово!', - Custom: 'Пользовательский', - 'More instructions about configuring `modelProviders` manually.': - 'Дополнительные инструкции по ручной настройке `modelProviders`.', - 'Select API-KEY configuration mode:': 'Выберите режим конфигурации API-KEY:', - '(Press Escape to go back)': '(Нажмите Escape для возврата)', - '(Press Enter to submit, Escape to cancel)': - '(Нажмите Enter для отправки, Escape для отмены)', - 'More instructions please check:': 'Дополнительные инструкции см.:', + 'Select Region for Coding Plan': 'Выберите регион Coding Plan', + 'Choose based on where your account is registered': + 'Выберите в зависимости от места регистрации вашего аккаунта', + 'Enter Coding Plan API Key': 'Введите API-ключ Coding Plan', + + // ============================================================================ + // Coding Plan International Updates + // ============================================================================ + 'New model configurations are available for {{region}}. Update now?': + 'Доступны новые конфигурации моделей для {{region}}. Обновить сейчас?', + '{{region}} configuration updated successfully. Model switched to "{{model}}".': + 'Конфигурация {{region}} успешно обновлена. Модель переключена на "{{model}}".', + 'Authenticated successfully with {{region}}. API key and model configs saved to settings.json (backed up).': + 'Успешная аутентификация с {{region}}. API-ключ и конфигурации моделей сохранены в settings.json (резервная копия создана).', }; diff --git a/packages/cli/src/i18n/locales/zh.js b/packages/cli/src/i18n/locales/zh.js index f20bf06c9..5bc2bef92 100644 --- a/packages/cli/src/i18n/locales/zh.js +++ b/packages/cli/src/i18n/locales/zh.js @@ -10,6 +10,11 @@ export default { // ============================================================================ // Help / UI Components // ============================================================================ + // Attachment hints + '↑ to manage attachments': '↑ 管理附件', + '← → select, Delete to remove, ↓ to exit': '← → 选择,Delete 删除,↓ 退出', + 'Attachments: ': '附件:', + 'Basics:': '基础功能:', 'Add context': '添加上下文', 'Use {{symbol}} to specify files for context (e.g., {{example}}) to target specific files or folders.': @@ -168,6 +173,7 @@ export default { 'Enter to confirm, Esc to cancel': 'Enter 确认,Esc 取消', 'Enter to select, ↑↓ to navigate, Esc to go back': 'Enter 选择,↑↓ 导航,Esc 返回', + 'Enter to submit, Esc to go back': 'Enter 提交,Esc 返回', 'Invalid step: {{step}}': '无效步骤: {{step}}', 'No subagents found.': '未找到子智能体。', "Use '/agents create' to create your first subagent.": @@ -877,18 +883,21 @@ export default { // Dialogs - Auth // ============================================================================ 'Get started': '开始使用', - 'How would you like to authenticate for this project?': - '您希望如何为此项目进行身份验证?', + 'Select Authentication Method': '选择认证方式', 'OpenAI API key is required to use OpenAI authentication.': '使用 OpenAI 认证需要 OpenAI API 密钥', 'You must select an auth method to proceed. Press Ctrl+C again to exit.': '您必须选择认证方法才能继续。再次按 Ctrl+C 退出', - '(Use Enter to Set Auth)': '(使用 Enter 设置认证)', - 'Terms of Services and Privacy Notice for Qwen Code': - 'Qwen Code 的服务条款和隐私声明', + 'Terms of Services and Privacy Notice': '服务条款和隐私声明', 'Qwen OAuth': 'Qwen OAuth (免费)', + 'Free \u00B7 Up to 1,000 requests/day \u00B7 Qwen latest models': + '免费 \u00B7 每天最多 1,000 次请求 \u00B7 Qwen 最新模型', 'Login with QwenChat account to use daily free quota.': '使用 QwenChat 账号登录,享受每日免费额度。', + 'Paid \u00B7 Up to 6,000 requests/5 hrs \u00B7 All Alibaba Cloud Coding Plan Models': + '付费 \u00B7 每 5 小时最多 6,000 次请求 \u00B7 支持阿里云百炼 Coding Plan 全部模型', + 'Alibaba Cloud Coding Plan': '阿里云百炼 Coding Plan', + 'Bring your own API key': '使用自己的 API 密钥', 'Use coding plan credentials or your own api-keys/providers.': '使用 Coding Plan 凭证或您自己的 API 密钥/提供商。', OpenAI: 'OpenAI', @@ -912,6 +921,8 @@ export default { 'Waiting for Qwen OAuth authentication...': '正在等待 Qwen OAuth 认证...', 'Note: Your existing API key in settings.json will not be cleared when using Qwen OAuth. You can switch back to OpenAI authentication later if needed.': '注意:使用 Qwen OAuth 时,settings.json 中现有的 API 密钥不会被清除。如果需要,您可以稍后切换回 OpenAI 认证。', + 'Note: Your existing API key will not be cleared when using Qwen OAuth.': + '注意:使用 Qwen OAuth 时,现有的 API 密钥不会被清除。', 'Authentication timed out. Please try again.': '认证超时。请重试。', 'Waiting for auth... (Press ESC or CTRL+C to cancel)': '正在等待认证...(按 ESC 或 CTRL+C 取消)', @@ -956,10 +967,21 @@ export default { '(default)': '(默认)', '(set)': '(已设置)', '(not set)': '(未设置)', + Modality: '模态', + 'Context Window': '上下文窗口', + text: '文本', + 'text-only': '纯文本', + image: '图像', + pdf: 'PDF', + audio: '音频', + video: '视频', + 'not set': '未设置', + none: '无', + unknown: '未知', "Failed to switch model to '{{modelId}}'.\n\n{{error}}": "无法切换到模型 '{{modelId}}'.\n\n{{error}}", - 'The latest Qwen Coder model from Alibaba Cloud ModelStudio (version: qwen3-coder-plus-2025-09-23)': - '来自阿里云 ModelStudio 的最新 Qwen Coder 模型(版本:qwen3-coder-plus-2025-09-23)', + 'Qwen 3.5 Plus — efficient hybrid model with leading coding performance': + 'Qwen 3.5 Plus — 高效混合架构,编程性能业界领先', 'The latest Qwen Vision model from Alibaba Cloud ModelStudio (version: qwen3-vl-plus-2025-09-23)': '来自阿里云 ModelStudio 的最新 Qwen Vision 模型(版本:qwen3-vl-plus-2025-09-23)', @@ -1047,6 +1069,8 @@ export default { '按 Shift+Tab 或输入 /approval-mode 可快速切换权限模式。', 'You can switch permission mode quickly with Tab or /approval-mode.': '按 Tab 或输入 /approval-mode 可快速切换权限模式。', + 'Try /insight to generate personalized insights from your chat history.': + '试试 /insight,从聊天记录中生成个性化洞察。', // ============================================================================ // Exit Screen / Stats @@ -1204,17 +1228,28 @@ export default { 'Failed to open browser. Check out the extensions gallery at {{url}}': '打开浏览器失败。请访问扩展市场:{{url}}', + // ============================================================================ + // Retry / Rate Limit + // ============================================================================ + 'Rate limit error: {{reason}}': '触发限流:{{reason}}', + 'Retrying in {{seconds}} seconds… (attempt {{attempt}}/{{maxRetries}})': + '将于 {{seconds}} 秒后重试…(第 {{attempt}}/{{maxRetries}} 次)', + 'Press Ctrl+Y to retry': '按 Ctrl+Y 重试。', + 'No failed request to retry.': '没有可重试的失败请求。', + 'to retry last request': '重试上一次请求', + // ============================================================================ // Coding Plan Authentication // ============================================================================ - 'Please enter your API key:': '请输入您的 API Key:', 'API key cannot be empty.': 'API Key 不能为空。', - 'You can get your exclusive Coding Plan API-KEY here:': - '您可以在这里获取专属的 Coding Plan API-KEY:', + 'Invalid API key. Coding Plan API keys start with "sk-sp-". Please check.': + '无效的 API Key,Coding Plan API Key 均以 "sk-sp-" 开头,请检查', + 'You can get your Coding Plan API key here': + '您可以在这里获取 Coding Plan API Key', 'API key is stored in settings.env. You can migrate it to a .env file for better security.': 'API Key 已存储在 settings.env 中。您可以将其迁移到 .env 文件以获得更好的安全性。', - 'New model configurations are available for Bailian Coding Plan. Update now?': - '百炼 Coding Plan 有新模型配置可用。是否立即更新?', + 'New model configurations are available for Alibaba Cloud Coding Plan. Update now?': + '阿里云百炼 Coding Plan 有新模型配置可用。是否立即更新?', 'Coding Plan configuration updated successfully. New models are now available.': 'Coding Plan 配置更新成功。新模型现已可用。', 'Coding Plan API key not found. Please re-authenticate with Coding Plan.': @@ -1223,35 +1258,27 @@ export default { '更新 Coding Plan 配置失败:{{message}}', // ============================================================================ - // Custom API-KEY Configuration + // Custom API Key Configuration // ============================================================================ - 'For advanced users who want to configure models manually.': - '适合需要手动配置模型的高级用户。', - 'Please configure your models in settings.json:': - '请在 settings.json 中配置您的模型:', - 'Set API key via environment variable (e.g., OPENAI_API_KEY)': - '通过环境变量设置 API Key(例如:OPENAI_API_KEY)', - "Add model configuration to modelProviders['openai'] (or other auth types)": - "将模型配置添加到 modelProviders['openai'](或其他认证类型)", - 'Each provider needs: id, envKey (required), plus optional baseUrl, generationConfig': - '每个提供商需要:id、envKey(必需),以及可选的 baseUrl、generationConfig', - 'Use /model command to select your preferred model from the configured list': - '使用 /model 命令从配置列表中选择您偏好的模型', - 'Supported auth types: openai, anthropic, gemini, vertex-ai, etc.': - '支持的认证类型:openai、anthropic、gemini、vertex-ai 等', - 'More instructions please check:': '更多说明请查看:', + 'You can configure your API key and models in settings.json': + '您可以在 settings.json 中配置 API Key 和模型', + 'Refer to the documentation for setup instructions': '请参考文档了解配置说明', // ============================================================================ // Auth Dialog - View Titles and Labels // ============================================================================ - 'API-KEY': 'API-KEY', - 'Coding Plan': 'Coding Plan', - "Paste your api key of Bailian Coding Plan and you're all set!": - '粘贴您的百炼 Coding Plan API Key,即可完成设置!', - Custom: '自定义', - 'More instructions about configuring `modelProviders` manually.': - '关于手动配置 `modelProviders` 的更多说明。', - 'Select API-KEY configuration mode:': '选择 API-KEY 配置模式:', - '(Press Escape to go back)': '(按 Escape 键返回)', - '(Press Enter to submit, Escape to cancel)': '(按 Enter 提交,Escape 取消)', + 'Select Region for Coding Plan': '选择 Coding Plan 区域', + 'Choose based on where your account is registered': + '请根据您的账号注册地区选择', + 'Enter Coding Plan API Key': '输入 Coding Plan API Key', + + // ============================================================================ + // Coding Plan International Updates + // ============================================================================ + 'New model configurations are available for {{region}}. Update now?': + '{{region}} 有新的模型配置可用。是否立即更新?', + '{{region}} configuration updated successfully. Model switched to "{{model}}".': + '{{region}} 配置更新成功。模型已切换至 "{{model}}"。', + 'Authenticated successfully with {{region}}. API key and model configs saved to settings.json (backed up).': + '成功通过 {{region}} 认证。API Key 和模型配置已保存至 settings.json(已备份)。', }; diff --git a/packages/cli/src/nonInteractive/control/controllers/systemController.ts b/packages/cli/src/nonInteractive/control/controllers/systemController.ts index 06923e963..5a275344f 100644 --- a/packages/cli/src/nonInteractive/control/controllers/systemController.ts +++ b/packages/cli/src/nonInteractive/control/controllers/systemController.ts @@ -193,6 +193,7 @@ export class SystemController extends BaseController { return { subtype: 'initialize', + session_id: this.context.config.getSessionId(), capabilities, }; } diff --git a/packages/cli/src/services/BuiltinCommandLoader.ts b/packages/cli/src/services/BuiltinCommandLoader.ts index dc4c1f8d9..cda06daad 100644 --- a/packages/cli/src/services/BuiltinCommandLoader.ts +++ b/packages/cli/src/services/BuiltinCommandLoader.ts @@ -40,6 +40,7 @@ import { themeCommand } from '../ui/commands/themeCommand.js'; import { toolsCommand } from '../ui/commands/toolsCommand.js'; import { vimCommand } from '../ui/commands/vimCommand.js'; import { setupGithubCommand } from '../ui/commands/setupGithubCommand.js'; +import { insightCommand } from '../ui/commands/insightCommand.js'; /** * Loads the core, hard-coded slash commands that are an integral part @@ -90,6 +91,7 @@ export class BuiltinCommandLoader implements ICommandLoader { vimCommand, setupGithubCommand, terminalSetupCommand, + insightCommand, ]; return allDefinitions.filter((cmd): cmd is SlashCommand => cmd !== null); diff --git a/packages/cli/src/services/insight/generators/DataProcessor.test.ts b/packages/cli/src/services/insight/generators/DataProcessor.test.ts new file mode 100644 index 000000000..1f90dbff5 --- /dev/null +++ b/packages/cli/src/services/insight/generators/DataProcessor.test.ts @@ -0,0 +1,1217 @@ +/** + * @license + * Copyright 2025 Qwen Code + * SPDX-License-Identifier: Apache-2.0 + */ + +import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest'; +import { DataProcessor } from './DataProcessor.js'; +import type { Config, ChatRecord } from '@qwen-code/qwen-code-core'; +import type { + InsightData, + SessionFacets, +} from '../types/StaticInsightTypes.js'; + +// Mock dependencies +vi.mock('@qwen-code/qwen-code-core', async () => { + const actual = await vi.importActual< + typeof import('@qwen-code/qwen-code-core') + >('@qwen-code/qwen-code-core'); + return { + ...actual, + read: vi.fn(), + createDebugLogger: vi.fn(() => ({ + info: vi.fn(), + error: vi.fn(), + warn: vi.fn(), + })), + }; +}); + +vi.mock('fs/promises', () => ({ + default: { + readdir: vi.fn(), + stat: vi.fn(), + readFile: vi.fn(), + writeFile: vi.fn(), + }, +})); + +import fs from 'fs/promises'; +import { read as readJsonlFile } from '@qwen-code/qwen-code-core'; + +const mockedFs = vi.mocked(fs); +const mockedReadJsonlFile = vi.mocked(readJsonlFile); + +describe('DataProcessor', () => { + let mockConfig: Config; + let dataProcessor: DataProcessor; + let mockGenerateJson: ReturnType; + + beforeEach(() => { + vi.clearAllMocks(); + + mockGenerateJson = vi.fn(); + mockConfig = { + getBaseLlmClient: vi.fn(() => ({ + generateJson: mockGenerateJson, + })), + getModel: vi.fn(() => 'test-model'), + } as unknown as Config; + + dataProcessor = new DataProcessor(mockConfig); + }); + + afterEach(() => { + vi.restoreAllMocks(); + }); + + describe('formatDate', () => { + it('should format date as YYYY-MM-DD', () => { + const date = new Date('2025-01-15T10:30:00Z'); + // Access private method through any cast for testing + const result = ( + dataProcessor as unknown as { formatDate(date: Date): string } + ).formatDate(date); + expect(result).toBe('2025-01-15'); + }); + + it('should handle different timezones correctly', () => { + const date = new Date('2025-12-31T23:59:59Z'); + const result = ( + dataProcessor as unknown as { formatDate(date: Date): string } + ).formatDate(date); + // Result depends on local timezone, but should be a valid date string + expect(result).toMatch(/^\d{4}-\d{2}-\d{2}$/); + }); + }); + + describe('formatRecordsForAnalysis', () => { + it('should format empty records array', () => { + const records: ChatRecord[] = []; + const result = ( + dataProcessor as unknown as { + formatRecordsForAnalysis(records: ChatRecord[]): string; + } + ).formatRecordsForAnalysis(records); + expect(result).toContain('Session: unknown'); + expect(result).toContain('Duration: 0 turns'); + }); + + it('should format user messages correctly', () => { + const records: ChatRecord[] = [ + { + sessionId: 'test-session', + timestamp: new Date().toISOString(), + type: 'user', + message: { + role: 'user', + parts: [{ text: 'Hello, world!' }], + }, + uuid: '', + parentUuid: null, + cwd: '', + version: '', + }, + ]; + const result = ( + dataProcessor as unknown as { + formatRecordsForAnalysis(records: ChatRecord[]): string; + } + ).formatRecordsForAnalysis(records); + expect(result).toContain('Session: test-session'); + expect(result).toContain('[User]: Hello, world!'); + }); + + it('should format assistant text messages correctly', () => { + const records: ChatRecord[] = [ + { + sessionId: 'test-session', + timestamp: new Date().toISOString(), + type: 'assistant', + message: { + role: 'assistant', + parts: [{ text: 'I can help you with that.' }], + }, + uuid: '', + parentUuid: null, + cwd: '', + version: '', + }, + ]; + const result = ( + dataProcessor as unknown as { + formatRecordsForAnalysis(records: ChatRecord[]): string; + } + ).formatRecordsForAnalysis(records); + expect(result).toContain('[Assistant]: I can help you with that.'); + }); + + it('should format function calls correctly', () => { + const records: ChatRecord[] = [ + { + sessionId: 'test-session', + timestamp: new Date().toISOString(), + type: 'assistant', + message: { + role: 'assistant', + parts: [{ functionCall: { name: 'read_file', args: {} } }], + }, + uuid: '', + parentUuid: null, + cwd: '', + version: '', + }, + ]; + const result = ( + dataProcessor as unknown as { + formatRecordsForAnalysis(records: ChatRecord[]): string; + } + ).formatRecordsForAnalysis(records); + expect(result).toContain('[Tool: read_file]'); + }); + + it('should handle multiple message parts', () => { + const records: ChatRecord[] = [ + { + sessionId: 'test-session', + timestamp: new Date().toISOString(), + type: 'assistant', + message: { + role: 'assistant', + parts: [ + { text: 'Let me check that.' }, + { functionCall: { name: 'search', args: {} } }, + ], + }, + uuid: '', + parentUuid: null, + cwd: '', + version: '', + }, + ]; + const result = ( + dataProcessor as unknown as { + formatRecordsForAnalysis(records: ChatRecord[]): string; + } + ).formatRecordsForAnalysis(records); + expect(result).toContain('[Assistant]: Let me check that.'); + expect(result).toContain('[Tool: search]'); + }); + + it('should handle messages without parts', () => { + const records: ChatRecord[] = [ + { + sessionId: 'test-session', + timestamp: new Date().toISOString(), + type: 'assistant', + message: { + role: 'assistant', + }, + uuid: '', + parentUuid: null, + cwd: '', + version: '', + }, + ]; + const result = ( + dataProcessor as unknown as { + formatRecordsForAnalysis(records: ChatRecord[]): string; + } + ).formatRecordsForAnalysis(records); + expect(result).not.toContain('[Assistant]:'); + }); + }); + + describe('calculateStreaks', () => { + it('should return zero streaks for empty dates array', () => { + const result = ( + dataProcessor as unknown as { + calculateStreaks(dates: string[]): { + currentStreak: number; + longestStreak: number; + dates: string[]; + }; + } + ).calculateStreaks([]); + expect(result.currentStreak).toBe(0); + expect(result.longestStreak).toBe(0); + expect(result.dates).toEqual([]); + }); + + it('should calculate streak of 1 for single date', () => { + const result = ( + dataProcessor as unknown as { + calculateStreaks(dates: string[]): { + currentStreak: number; + longestStreak: number; + dates: string[]; + }; + } + ).calculateStreaks(['2025-01-15']); + expect(result.currentStreak).toBe(1); + expect(result.longestStreak).toBe(1); + }); + + it('should calculate consecutive day streak', () => { + const dates = ['2025-01-15', '2025-01-16', '2025-01-17']; + const result = ( + dataProcessor as unknown as { + calculateStreaks(dates: string[]): { + currentStreak: number; + longestStreak: number; + dates: string[]; + }; + } + ).calculateStreaks(dates); + expect(result.currentStreak).toBe(3); + expect(result.longestStreak).toBe(3); + }); + + it('should handle non-consecutive dates', () => { + const dates = ['2025-01-15', '2025-01-17', '2025-01-18']; + const result = ( + dataProcessor as unknown as { + calculateStreaks(dates: string[]): { + currentStreak: number; + longestStreak: number; + dates: string[]; + }; + } + ).calculateStreaks(dates); + expect(result.longestStreak).toBe(2); // Jan 17-18 + }); + + it('should sort dates before calculating streaks', () => { + const dates = ['2025-01-18', '2025-01-15', '2025-01-16', '2025-01-17']; + const result = ( + dataProcessor as unknown as { + calculateStreaks(dates: string[]): { + currentStreak: number; + longestStreak: number; + dates: string[]; + }; + } + ).calculateStreaks(dates); + expect(result.longestStreak).toBe(4); + }); + + it('should handle duplicate dates', () => { + const dates = ['2025-01-15', '2025-01-15', '2025-01-16']; + const result = ( + dataProcessor as unknown as { + calculateStreaks(dates: string[]): { + currentStreak: number; + longestStreak: number; + dates: string[]; + }; + } + ).calculateStreaks(dates); + expect(result.longestStreak).toBeGreaterThanOrEqual(1); + }); + }); + + describe('aggregateFacetsData', () => { + it('should return empty aggregates for empty facets array', () => { + const result = ( + dataProcessor as unknown as { + aggregateFacetsData(facets: SessionFacets[]): { + satisfactionAgg: Record; + frictionAgg: Record; + primarySuccessAgg: Record; + outcomesAgg: Record; + goalsAgg: Record; + }; + } + ).aggregateFacetsData([]); + expect(result.satisfactionAgg).toEqual({}); + expect(result.frictionAgg).toEqual({}); + expect(result.primarySuccessAgg).toEqual({}); + expect(result.outcomesAgg).toEqual({}); + expect(result.goalsAgg).toEqual({}); + }); + + it('should aggregate satisfaction counts', () => { + const facets: SessionFacets[] = [ + { + session_id: 's1', + underlying_goal: 'test', + goal_categories: {}, + outcome: 'fully_achieved', + user_satisfaction_counts: { satisfied: 2, neutral: 1 }, + Qwen_helpfulness: 'very_helpful', + session_type: 'single_task', + friction_counts: {}, + friction_detail: '', + primary_success: 'none', + brief_summary: 'Test summary', + }, + { + session_id: 's2', + underlying_goal: 'test2', + goal_categories: {}, + outcome: 'mostly_achieved', + user_satisfaction_counts: { satisfied: 1, frustrated: 2 }, + Qwen_helpfulness: 'moderately_helpful', + session_type: 'multi_task', + friction_counts: {}, + friction_detail: '', + primary_success: 'none', + brief_summary: 'Test summary 2', + }, + ]; + const result = ( + dataProcessor as unknown as { + aggregateFacetsData(facets: SessionFacets[]): { + satisfactionAgg: Record; + }; + } + ).aggregateFacetsData(facets); + expect(result.satisfactionAgg).toEqual({ + satisfied: 3, + neutral: 1, + frustrated: 2, + }); + }); + + it('should aggregate friction counts', () => { + const facets: SessionFacets[] = [ + { + session_id: 's1', + underlying_goal: 'test', + goal_categories: {}, + outcome: 'fully_achieved', + user_satisfaction_counts: {}, + Qwen_helpfulness: 'very_helpful', + session_type: 'single_task', + friction_counts: { slow_response: 1, unclear_answer: 2 }, + friction_detail: 'Some friction', + primary_success: 'none', + brief_summary: 'Test summary', + }, + { + session_id: 's2', + underlying_goal: 'test2', + goal_categories: {}, + outcome: 'mostly_achieved', + user_satisfaction_counts: {}, + Qwen_helpfulness: 'moderately_helpful', + session_type: 'multi_task', + friction_counts: { slow_response: 2 }, + friction_detail: 'More friction', + primary_success: 'none', + brief_summary: 'Test summary 2', + }, + ]; + const result = ( + dataProcessor as unknown as { + aggregateFacetsData(facets: SessionFacets[]): { + frictionAgg: Record; + }; + } + ).aggregateFacetsData(facets); + expect(result.frictionAgg).toEqual({ + slow_response: 3, + unclear_answer: 2, + }); + }); + + it('should aggregate primary success excluding none', () => { + const facets: SessionFacets[] = [ + { + session_id: 's1', + underlying_goal: 'test', + goal_categories: {}, + outcome: 'fully_achieved', + user_satisfaction_counts: {}, + Qwen_helpfulness: 'very_helpful', + session_type: 'single_task', + friction_counts: {}, + friction_detail: '', + primary_success: 'correct_code_edits', + brief_summary: 'Test summary', + }, + { + session_id: 's2', + underlying_goal: 'test2', + goal_categories: {}, + outcome: 'mostly_achieved', + user_satisfaction_counts: {}, + Qwen_helpfulness: 'moderately_helpful', + session_type: 'multi_task', + friction_counts: {}, + friction_detail: '', + primary_success: 'none', + brief_summary: 'Test summary 2', + }, + { + session_id: 's3', + underlying_goal: 'test3', + goal_categories: {}, + outcome: 'partially_achieved', + user_satisfaction_counts: {}, + Qwen_helpfulness: 'slightly_helpful', + session_type: 'exploration', + friction_counts: {}, + friction_detail: '', + primary_success: 'good_explanations', + brief_summary: 'Test summary 3', + }, + ]; + const result = ( + dataProcessor as unknown as { + aggregateFacetsData(facets: SessionFacets[]): { + primarySuccessAgg: Record; + }; + } + ).aggregateFacetsData(facets); + expect(result.primarySuccessAgg).toEqual({ + correct_code_edits: 1, + good_explanations: 1, + }); + expect(result.primarySuccessAgg['none']).toBeUndefined(); + }); + + it('should aggregate outcomes', () => { + const facets: SessionFacets[] = [ + { + session_id: 's1', + underlying_goal: 'test', + goal_categories: {}, + outcome: 'fully_achieved', + user_satisfaction_counts: {}, + Qwen_helpfulness: 'very_helpful', + session_type: 'single_task', + friction_counts: {}, + friction_detail: '', + primary_success: 'none', + brief_summary: 'Test summary', + }, + { + session_id: 's2', + underlying_goal: 'test2', + goal_categories: {}, + outcome: 'fully_achieved', + user_satisfaction_counts: {}, + Qwen_helpfulness: 'moderately_helpful', + session_type: 'multi_task', + friction_counts: {}, + friction_detail: '', + primary_success: 'none', + brief_summary: 'Test summary 2', + }, + { + session_id: 's3', + underlying_goal: 'test3', + goal_categories: {}, + outcome: 'partially_achieved', + user_satisfaction_counts: {}, + Qwen_helpfulness: 'slightly_helpful', + session_type: 'exploration', + friction_counts: {}, + friction_detail: '', + primary_success: 'none', + brief_summary: 'Test summary 3', + }, + ]; + const result = ( + dataProcessor as unknown as { + aggregateFacetsData(facets: SessionFacets[]): { + outcomesAgg: Record; + }; + } + ).aggregateFacetsData(facets); + expect(result.outcomesAgg).toEqual({ + fully_achieved: 2, + partially_achieved: 1, + }); + }); + + it('should aggregate goal categories', () => { + const facets: SessionFacets[] = [ + { + session_id: 's1', + underlying_goal: 'test', + goal_categories: { coding: 2, debugging: 1 }, + outcome: 'fully_achieved', + user_satisfaction_counts: {}, + Qwen_helpfulness: 'very_helpful', + session_type: 'single_task', + friction_counts: {}, + friction_detail: '', + primary_success: 'none', + brief_summary: 'Test summary', + }, + { + session_id: 's2', + underlying_goal: 'test2', + goal_categories: { coding: 1, refactoring: 3 }, + outcome: 'mostly_achieved', + user_satisfaction_counts: {}, + Qwen_helpfulness: 'moderately_helpful', + session_type: 'multi_task', + friction_counts: {}, + friction_detail: '', + primary_success: 'none', + brief_summary: 'Test summary 2', + }, + ]; + const result = ( + dataProcessor as unknown as { + aggregateFacetsData(facets: SessionFacets[]): { + goalsAgg: Record; + }; + } + ).aggregateFacetsData(facets); + expect(result.goalsAgg).toEqual({ + coding: 3, + debugging: 1, + refactoring: 3, + }); + }); + }); + + describe('analyzeSession', () => { + it('should return null for empty records', async () => { + const result = await ( + dataProcessor as unknown as { + analyzeSession(records: ChatRecord[]): Promise; + } + ).analyzeSession([]); + expect(result).toBeNull(); + }); + + it('should analyze session and return facets', async () => { + const mockFacet = { + underlying_goal: 'Test goal', + goal_categories: { coding: 1 }, + outcome: 'fully_achieved', + user_satisfaction_counts: { satisfied: 1 }, + Qwen_helpfulness: 'very_helpful', + session_type: 'single_task', + friction_counts: {}, + friction_detail: '', + primary_success: 'correct_code_edits', + brief_summary: 'Test summary', + }; + + mockGenerateJson.mockResolvedValue(mockFacet); + + const records: ChatRecord[] = [ + { + sessionId: 'test-session', + timestamp: new Date().toISOString(), + type: 'user', + message: { + role: 'user', + parts: [{ text: 'Help me with code' }], + }, + uuid: '', + parentUuid: null, + cwd: '', + version: '', + }, + ]; + + const result = await ( + dataProcessor as unknown as { + analyzeSession(records: ChatRecord[]): Promise; + } + ).analyzeSession(records); + + expect(result).not.toBeNull(); + expect(result?.session_id).toBe('test-session'); + expect(result?.underlying_goal).toBe('Test goal'); + expect(mockGenerateJson).toHaveBeenCalledWith( + expect.objectContaining({ + model: 'test-model', + schema: expect.any(Object), + }), + ); + }); + + it('should return null when LLM returns empty result', async () => { + mockGenerateJson.mockResolvedValue({}); + + const records: ChatRecord[] = [ + { + sessionId: 'test-session', + timestamp: new Date().toISOString(), + type: 'user', + message: { + role: 'user', + parts: [{ text: 'Help' }], + }, + uuid: '', + parentUuid: null, + cwd: '', + version: '', + }, + ]; + + const result = await ( + dataProcessor as unknown as { + analyzeSession(records: ChatRecord[]): Promise; + } + ).analyzeSession(records); + + expect(result).toBeNull(); + }); + + it('should handle LLM errors gracefully', async () => { + mockGenerateJson.mockRejectedValue(new Error('LLM Error')); + + const records: ChatRecord[] = [ + { + sessionId: 'test-session', + timestamp: new Date().toISOString(), + type: 'user', + message: { + role: 'user', + parts: [{ text: 'Help' }], + }, + uuid: '', + parentUuid: null, + cwd: '', + version: '', + }, + ]; + + const result = await ( + dataProcessor as unknown as { + analyzeSession(records: ChatRecord[]): Promise; + } + ).analyzeSession(records); + + expect(result).toBeNull(); + }); + }); + + describe('scanChatFiles', () => { + it('should return empty array when base directory does not exist', async () => { + const error = new Error('Directory not found') as NodeJS.ErrnoException; + error.code = 'ENOENT'; + mockedFs.readdir.mockRejectedValue(error); + + const result = await ( + dataProcessor as unknown as { + scanChatFiles( + baseDir: string, + ): Promise>; + } + ).scanChatFiles('/nonexistent'); + + expect(result).toEqual([]); + }); + + it('should scan project directories and find chat files', async () => { + mockedFs.readdir.mockResolvedValueOnce([ + 'project1', + 'project2', + ] as unknown as Awaited>); + + mockedFs.stat.mockImplementation((path) => { + const pathStr = String(path); + if (pathStr.includes('project1') || pathStr.includes('project2')) { + return Promise.resolve({ + isDirectory: () => true, + mtimeMs: 1234567890, + } as Awaited>); + } + if (pathStr.endsWith('.jsonl')) { + return Promise.resolve({ + isDirectory: () => false, + mtimeMs: 1234567890, + } as Awaited>); + } + throw new Error('Unexpected path'); + }); + + mockedFs.readdir.mockImplementation((path) => { + const pathStr = String(path); + if (pathStr.endsWith('chats')) { + if (pathStr.includes('project1')) { + return Promise.resolve([ + 'chat1.jsonl', + 'chat2.jsonl', + ] as unknown as Awaited>); + } + if (pathStr.includes('project2')) { + return Promise.resolve(['chat3.jsonl'] as unknown as Awaited< + ReturnType + >); + } + } + return Promise.resolve( + [] as unknown as Awaited>, + ); + }); + + const result = await ( + dataProcessor as unknown as { + scanChatFiles( + baseDir: string, + ): Promise>; + } + ).scanChatFiles('/base'); + + expect(result).toHaveLength(3); + const paths = result.map((r) => r.path); + expect(paths.some((p) => p.includes('chat1.jsonl'))).toBe(true); + expect(paths.some((p) => p.includes('chat2.jsonl'))).toBe(true); + expect(paths.some((p) => p.includes('chat3.jsonl'))).toBe(true); + }); + + it('should skip projects without chats directory', async () => { + mockedFs.readdir.mockResolvedValueOnce([ + 'project1', + 'project2', + ] as unknown as Awaited>); + + mockedFs.stat.mockImplementation((path) => { + const pathStr = String(path); + if (pathStr.includes('project1') || pathStr.includes('project2')) { + return Promise.resolve({ isDirectory: () => true } as Awaited< + ReturnType + >); + } + if (pathStr.endsWith('.jsonl')) { + return Promise.resolve({ + isDirectory: () => false, + mtimeMs: 1234567890, + } as Awaited>); + } + throw new Error('Unexpected path'); + }); + + const error = new Error('No chats dir') as NodeJS.ErrnoException; + error.code = 'ENOENT'; + + mockedFs.readdir.mockImplementation((path) => { + const pathStr = String(path); + if (pathStr.endsWith('chats')) { + if (pathStr.includes('project1')) { + return Promise.resolve(['chat1.jsonl'] as unknown as Awaited< + ReturnType + >); + } + if (pathStr.includes('project2')) { + return Promise.reject(error); + } + } + return Promise.resolve( + [] as unknown as Awaited>, + ); + }); + + const result = await ( + dataProcessor as unknown as { + scanChatFiles( + baseDir: string, + ): Promise>; + } + ).scanChatFiles('/base'); + + expect(result).toHaveLength(1); + expect(result[0].path).toContain('chat1.jsonl'); + }); + + it('should handle file stat errors gracefully', async () => { + mockedFs.readdir.mockResolvedValueOnce(['project1'] as unknown as Awaited< + ReturnType + >); + + mockedFs.stat.mockImplementation((path) => { + const pathStr = String(path); + if (pathStr.includes('project1') && !pathStr.includes('chats')) { + return Promise.resolve({ isDirectory: () => true } as Awaited< + ReturnType + >); + } + if (pathStr.endsWith('chat1.jsonl')) { + return Promise.reject(new Error('Stat failed')); + } + throw new Error('Unexpected path: ' + pathStr); + }); + + mockedFs.readdir.mockImplementation((path) => { + const pathStr = String(path); + if (pathStr.endsWith('chats')) { + return Promise.resolve(['chat1.jsonl'] as unknown as Awaited< + ReturnType + >); + } + return Promise.resolve( + [] as unknown as Awaited>, + ); + }); + + const result = await ( + dataProcessor as unknown as { + scanChatFiles( + baseDir: string, + ): Promise>; + } + ).scanChatFiles('/base'); + + // When stat fails for a file, it should be skipped but not crash + expect(result).toEqual([]); + }); + }); + + describe('generateMetrics', () => { + it('should generate metrics from chat files', async () => { + const mockRecords: ChatRecord[] = [ + { + sessionId: 'session1', + timestamp: '2025-01-15T10:00:00Z', + type: 'user', + message: { role: 'user', parts: [{ text: 'Hello' }] }, + uuid: '', + parentUuid: null, + cwd: '', + version: '', + }, + { + sessionId: 'session1', + timestamp: '2025-01-15T10:01:00Z', + type: 'system', + subtype: 'slash_command', + uuid: '', + parentUuid: null, + cwd: '', + version: '', + }, + { + sessionId: 'session1', + timestamp: '2025-01-15T10:05:00Z', + type: 'assistant', + message: { role: 'assistant', parts: [{ text: 'Hi' }] }, + uuid: '', + parentUuid: null, + cwd: '', + version: '', + }, + { + sessionId: 'session1', + timestamp: '2025-01-15T10:06:00Z', + type: 'assistant', + message: { + role: 'assistant', + parts: [{ functionCall: { name: 'read_file', args: {} } }], + }, + uuid: '', + parentUuid: null, + cwd: '', + version: '', + }, + ]; + + mockedReadJsonlFile.mockResolvedValue(mockRecords); + + const files = [{ path: '/test/chat.jsonl', mtime: 1234567890 }]; + const result = await ( + dataProcessor as unknown as { + generateMetrics( + files: Array<{ path: string; mtime: number }>, + ): Promise; + } + ).generateMetrics(files); + + expect(result).toMatchObject({ + totalMessages: 2, + totalSessions: 1, + heatmap: expect.any(Object), + activeHours: expect.any(Object), + topTools: expect.any(Array), + }); + }); + + it('should track tool usage correctly', async () => { + const mockRecords: ChatRecord[] = [ + { + sessionId: 'session1', + timestamp: '2025-01-15T10:00:00Z', + type: 'assistant', + message: { + role: 'assistant', + parts: [{ functionCall: { name: 'read_file', args: {} } }], + }, + uuid: '', + parentUuid: null, + cwd: '', + version: '', + }, + { + sessionId: 'session1', + timestamp: '2025-01-15T10:01:00Z', + type: 'assistant', + message: { + role: 'assistant', + parts: [{ functionCall: { name: 'read_file', args: {} } }], + }, + uuid: '', + parentUuid: null, + cwd: '', + version: '', + }, + { + sessionId: 'session1', + timestamp: '2025-01-15T10:02:00Z', + type: 'assistant', + message: { + role: 'assistant', + parts: [{ functionCall: { name: 'write_file', args: {} } }], + }, + uuid: '', + parentUuid: null, + cwd: '', + version: '', + }, + ]; + + mockedReadJsonlFile.mockResolvedValue(mockRecords); + + const files = [{ path: '/test/chat.jsonl', mtime: 1234567890 }]; + const result = await ( + dataProcessor as unknown as { + generateMetrics( + files: Array<{ path: string; mtime: number }>, + ): Promise<{ topTools: Array<[string, number]> }>; + } + ).generateMetrics(files); + + expect(result.topTools).toContainEqual(['read_file', 2]); + expect(result.topTools).toContainEqual(['write_file', 1]); + }); + + it('should handle file read errors gracefully', async () => { + mockedReadJsonlFile.mockRejectedValue(new Error('Read failed')); + + const files = [{ path: '/test/chat.jsonl', mtime: 1234567890 }]; + const result = await ( + dataProcessor as unknown as { + generateMetrics( + files: Array<{ path: string; mtime: number }>, + ): Promise<{ totalMessages: number }>; + } + ).generateMetrics(files); + + expect(result.totalMessages).toBe(0); + }); + + it('should call progress callback during processing', async () => { + const mockRecords: ChatRecord[] = [ + { + sessionId: 'session1', + timestamp: '2025-01-15T10:00:00Z', + type: 'user', + message: { role: 'user', parts: [{ text: 'Hello' }] }, + uuid: '', + parentUuid: null, + cwd: '', + version: '', + }, + ]; + + mockedReadJsonlFile.mockResolvedValue(mockRecords); + + const files = [ + { path: '/test/chat1.jsonl', mtime: 1234567890 }, + { path: '/test/chat2.jsonl', mtime: 1234567891 }, + ]; + const onProgress = vi.fn(); + + await ( + dataProcessor as unknown as { + generateMetrics( + files: Array<{ path: string; mtime: number }>, + onProgress?: (stage: string, progress: number) => void, + ): Promise; + } + ).generateMetrics(files, onProgress); + + expect(onProgress).toHaveBeenCalled(); + }); + }); + + describe('prepareCommonPromptData', () => { + it('should prepare prompt data with all required sections', () => { + const metrics = { + heatmap: { '2025-01-15': 5, '2025-01-16': 3 }, + totalSessions: 10, + totalMessages: 100, + totalHours: 5, + topTools: [ + ['read_file', 20], + ['write_file', 10], + ], + } as unknown as Omit; + + const facets: SessionFacets[] = [ + { + session_id: 's1', + underlying_goal: 'Goal 1', + goal_categories: { coding: 2, debugging: 1 }, + outcome: 'fully_achieved', + user_satisfaction_counts: { satisfied: 2 }, + Qwen_helpfulness: 'very_helpful', + session_type: 'single_task', + friction_counts: { slow: 1 }, + friction_detail: 'Some friction detail', + primary_success: 'correct_code_edits', + brief_summary: 'Summary 1', + }, + ]; + + const result = ( + dataProcessor as unknown as { + prepareCommonPromptData( + metrics: Omit, + facets: SessionFacets[], + ): string; + } + ).prepareCommonPromptData(metrics, facets); + + expect(result).toContain('DATA:'); + expect(result).toContain('SESSION SUMMARIES:'); + expect(result).toContain('FRICTION DETAILS:'); + expect(result).toContain('Summary 1'); + expect(result).toContain('Some friction detail'); + }); + + it('should filter out empty friction details', () => { + const metrics = { + heatmap: {}, + totalSessions: 1, + totalMessages: 10, + totalHours: 1, + topTools: [], + } as unknown as Omit; + + const facets: SessionFacets[] = [ + { + session_id: 's1', + underlying_goal: 'Goal 1', + goal_categories: {}, + outcome: 'fully_achieved', + user_satisfaction_counts: {}, + Qwen_helpfulness: 'very_helpful', + session_type: 'single_task', + friction_counts: {}, + friction_detail: '', + primary_success: 'none', + brief_summary: 'Summary 1', + }, + { + session_id: 's2', + underlying_goal: 'Goal 2', + goal_categories: {}, + outcome: 'mostly_achieved', + user_satisfaction_counts: {}, + Qwen_helpfulness: 'moderately_helpful', + session_type: 'multi_task', + friction_counts: {}, + friction_detail: ' ', + primary_success: 'none', + brief_summary: 'Summary 2', + }, + ]; + + const result = ( + dataProcessor as unknown as { + prepareCommonPromptData( + metrics: Omit, + facets: SessionFacets[], + ): string; + } + ).prepareCommonPromptData(metrics, facets); + + // Check that FRICTION DETAILS section is empty or only contains whitespace + const frictionSection = + result.split('FRICTION DETAILS:')[1]?.split('USER INSTRUCTIONS')[0] || + ''; + const hasNonEmptyFrictionDetail = + frictionSection.trim().length > 0 && frictionSection.includes('-'); + expect(hasNonEmptyFrictionDetail).toBe(false); + }); + }); + + describe('generateFacets', () => { + it('should skip non-conversational sessions', async () => { + const userOnlyRecords: ChatRecord[] = [ + { + sessionId: 'user-only', + timestamp: '2025-01-15T10:00:00Z', + type: 'user', + message: { role: 'user', parts: [{ text: 'Hello' }] }, + uuid: '', + parentUuid: null, + cwd: '', + version: '', + }, + ]; + + const conversationalRecords: ChatRecord[] = [ + { + sessionId: 'conversational', + timestamp: '2025-01-15T10:00:00Z', + type: 'user', + message: { role: 'user', parts: [{ text: 'Hello' }] }, + uuid: '', + parentUuid: null, + cwd: '', + version: '', + }, + { + sessionId: 'conversational', + timestamp: '2025-01-15T10:01:00Z', + type: 'assistant', + message: { role: 'assistant', parts: [{ text: 'Hi' }] }, + uuid: '', + parentUuid: null, + cwd: '', + version: '', + }, + ]; + + // First file is user-only, second is conversational + mockedReadJsonlFile + .mockResolvedValueOnce(userOnlyRecords) + .mockResolvedValueOnce(conversationalRecords); + + const mockFacet = { + underlying_goal: 'Test', + goal_categories: {}, + outcome: 'fully_achieved', + user_satisfaction_counts: {}, + Qwen_helpfulness: 'very_helpful', + session_type: 'single_task', + friction_counts: {}, + friction_detail: '', + primary_success: 'none', + brief_summary: 'Test', + }; + mockGenerateJson.mockResolvedValue(mockFacet); + + const files = [ + { path: '/test/user-only.jsonl', mtime: 2000 }, + { path: '/test/conversational.jsonl', mtime: 1000 }, + ]; + + const result = await ( + dataProcessor as unknown as { + generateFacets( + files: Array<{ path: string; mtime: number }>, + facetsOutputDir?: string, + ): Promise; + } + ).generateFacets(files); + + // Only the conversational session should be analyzed + expect(mockGenerateJson).toHaveBeenCalledTimes(1); + expect(result).toHaveLength(1); + expect(result[0].session_id).toBe('conversational'); + }); + }); +}); diff --git a/packages/cli/src/services/insight/generators/DataProcessor.ts b/packages/cli/src/services/insight/generators/DataProcessor.ts new file mode 100644 index 000000000..a3cda424e --- /dev/null +++ b/packages/cli/src/services/insight/generators/DataProcessor.ts @@ -0,0 +1,1131 @@ +/** + * @license + * Copyright 2025 Qwen Code + * SPDX-License-Identifier: Apache-2.0 + */ + +import fs from 'fs/promises'; +import path from 'path'; +import { + read as readJsonlFile, + createDebugLogger, +} from '@qwen-code/qwen-code-core'; +import pLimit from 'p-limit'; +import type { + InsightData, + HeatMapData, + StreakData, + SessionFacets, + InsightProgressCallback, +} from '../types/StaticInsightTypes.js'; +import type { + QualitativeInsights, + InsightImpressiveWorkflows, + InsightProjectAreas, + InsightFutureOpportunities, + InsightFrictionPoints, + InsightMemorableMoment, + InsightImprovements, + InsightInteractionStyle, + InsightAtAGlance, +} from '../types/QualitativeInsightTypes.js'; +import { + getInsightPrompt, + type Config, + type ChatRecord, +} from '@qwen-code/qwen-code-core'; + +const logger = createDebugLogger('DataProcessor'); + +const CONCURRENCY_LIMIT = 4; + +export class DataProcessor { + constructor(private config: Config) {} + + // Helper function to format date as YYYY-MM-DD + private formatDate(date: Date): string { + return date.toISOString().split('T')[0]; + } + + // Format chat records for LLM analysis + private formatRecordsForAnalysis(records: ChatRecord[]): string { + let output = ''; + const sessionStart = + records.length > 0 ? new Date(records[0].timestamp) : new Date(); + + output += `Session: ${records[0]?.sessionId || 'unknown'}\n`; + output += `Date: ${sessionStart.toISOString()}\n`; + output += `Duration: ${records.length} turns\n\n`; + + for (const record of records) { + if (record.type === 'user') { + const text = + record.message?.parts + ?.map((p) => ('text' in p ? p.text : '')) + .join('') || ''; + output += `[User]: ${text}\n`; + } else if (record.type === 'assistant') { + if (record.message?.parts) { + for (const part of record.message.parts) { + if ('text' in part && part.text) { + output += `[Assistant]: ${part.text}\n`; + } else if ('functionCall' in part) { + const call = part.functionCall; + if (call) { + output += `[Tool: ${call.name}]\n`; + } + } + } + } + } + } + return output; + } + + // Only analyze conversational sessions for facets (skip system-only logs). + private hasUserAndAssistantRecords(records: ChatRecord[]): boolean { + let hasUser = false; + let hasAssistant = false; + + for (const record of records) { + if (record.type === 'user') { + hasUser = true; + } else if (record.type === 'assistant') { + hasAssistant = true; + } + + if (hasUser && hasAssistant) { + return true; + } + } + + return false; + } + + // Analyze a single session using LLM + private async analyzeSession( + records: ChatRecord[], + ): Promise { + if (records.length === 0) return null; + + const INSIGHT_SCHEMA = { + type: 'object', + properties: { + underlying_goal: { + type: 'string', + description: 'What the user fundamentally wanted to achieve', + }, + goal_categories: { + type: 'object', + additionalProperties: { type: 'number' }, + }, + outcome: { + type: 'string', + enum: [ + 'fully_achieved', + 'mostly_achieved', + 'partially_achieved', + 'not_achieved', + 'unclear_from_transcript', + ], + }, + user_satisfaction_counts: { + type: 'object', + additionalProperties: { type: 'number' }, + }, + Qwen_helpfulness: { + type: 'string', + enum: [ + 'unhelpful', + 'slightly_helpful', + 'moderately_helpful', + 'very_helpful', + 'essential', + ], + }, + session_type: { + type: 'string', + enum: [ + 'single_task', + 'multi_task', + 'iterative_refinement', + 'exploration', + 'quick_question', + ], + }, + friction_counts: { + type: 'object', + additionalProperties: { type: 'number' }, + }, + friction_detail: { + type: 'string', + description: 'One sentence describing friction or empty', + }, + primary_success: { + type: 'string', + enum: [ + 'none', + 'fast_accurate_search', + 'correct_code_edits', + 'good_explanations', + 'proactive_help', + 'multi_file_changes', + 'good_debugging', + ], + }, + brief_summary: { + type: 'string', + description: 'One sentence: what user wanted and whether they got it', + }, + }, + required: [ + 'underlying_goal', + 'goal_categories', + 'outcome', + 'user_satisfaction_counts', + 'Qwen_helpfulness', + 'session_type', + 'friction_counts', + 'friction_detail', + 'primary_success', + 'brief_summary', + ], + }; + + const sessionText = this.formatRecordsForAnalysis(records); + const prompt = `${getInsightPrompt('analysis')}\n\nSESSION:\n${sessionText}`; + + try { + const result = await this.config.getBaseLlmClient().generateJson({ + // Use the configured model + model: this.config.getModel(), + contents: [{ role: 'user', parts: [{ text: prompt }] }], + schema: INSIGHT_SCHEMA, + abortSignal: AbortSignal.timeout(600000), // 10 minute timeout per session + }); + + if (!result || Object.keys(result).length === 0) { + return null; + } + + return { + ...(result as unknown as SessionFacets), + session_id: records[0].sessionId, + }; + } catch (error) { + logger.error( + `Failed to analyze session ${records[0]?.sessionId}:`, + error, + ); + return null; + } + } + + // Calculate streaks from activity dates + private calculateStreaks(dates: string[]): StreakData { + if (dates.length === 0) { + return { currentStreak: 0, longestStreak: 0, dates: [] }; + } + + // Convert string dates to Date objects and sort them + const dateObjects = dates.map((dateStr) => new Date(dateStr)); + dateObjects.sort((a, b) => a.getTime() - b.getTime()); + + let currentStreak = 1; + let maxStreak = 1; + let currentDate = new Date(dateObjects[0]); + currentDate.setHours(0, 0, 0, 0); // Normalize to start of day + + for (let i = 1; i < dateObjects.length; i++) { + const nextDate = new Date(dateObjects[i]); + nextDate.setHours(0, 0, 0, 0); // Normalize to start of day + + // Calculate difference in days + const diffDays = Math.floor( + (nextDate.getTime() - currentDate.getTime()) / (1000 * 60 * 60 * 24), + ); + + if (diffDays === 1) { + // Consecutive day + currentStreak++; + maxStreak = Math.max(maxStreak, currentStreak); + } else if (diffDays > 1) { + // Gap in streak + currentStreak = 1; + } + // If diffDays === 0, same day, so streak continues + + currentDate = nextDate; + } + + // Check if the streak is still ongoing (if last activity was yesterday or today) + const today = new Date(); + today.setHours(0, 0, 0, 0); + const yesterday = new Date(today); + yesterday.setDate(yesterday.getDate() - 1); + + if ( + currentDate.getTime() === today.getTime() || + currentDate.getTime() === yesterday.getTime() + ) { + // The streak might still be active, so we don't reset it + } + + return { + currentStreak, + longestStreak: maxStreak, + dates, + }; + } + + // Process chat files from all projects in the base directory and generate insights + async generateInsights( + baseDir: string, + facetsOutputDir?: string, + onProgress?: InsightProgressCallback, + ): Promise { + if (onProgress) onProgress('Scanning chat history...', 0); + const allChatFiles = await this.scanChatFiles(baseDir); + + if (onProgress) onProgress('Crunching the numbers', 10); + const metrics = await this.generateMetrics(allChatFiles, onProgress); + + if (onProgress) onProgress('Preparing sessions...', 20); + const facets = await this.generateFacets( + allChatFiles, + facetsOutputDir, + onProgress, + ); + + if (onProgress) onProgress('Generating personalized insights...', 80); + const qualitative = await this.generateQualitativeInsights(metrics, facets); + + // Aggregate satisfaction, friction, success and outcome data from facets + const { + satisfactionAgg, + frictionAgg, + primarySuccessAgg, + outcomesAgg, + goalsAgg, + } = this.aggregateFacetsData(facets); + + if (onProgress) onProgress('Assembling report...', 100); + + return { + ...metrics, + qualitative, + satisfaction: satisfactionAgg, + friction: frictionAgg, + primarySuccess: primarySuccessAgg, + outcomes: outcomesAgg, + topGoals: goalsAgg, + }; + } + + // Aggregate satisfaction and friction data from facets + private aggregateFacetsData(facets: SessionFacets[]): { + satisfactionAgg: Record; + frictionAgg: Record; + primarySuccessAgg: Record; + outcomesAgg: Record; + goalsAgg: Record; + } { + const satisfactionAgg: Record = {}; + const frictionAgg: Record = {}; + const primarySuccessAgg: Record = {}; + const outcomesAgg: Record = {}; + const goalsAgg: Record = {}; + + facets.forEach((facet) => { + // Aggregate satisfaction + Object.entries(facet.user_satisfaction_counts).forEach(([sat, count]) => { + satisfactionAgg[sat] = (satisfactionAgg[sat] || 0) + count; + }); + + // Aggregate friction + Object.entries(facet.friction_counts).forEach(([fric, count]) => { + frictionAgg[fric] = (frictionAgg[fric] || 0) + count; + }); + + // Aggregate primary success + if (facet.primary_success && facet.primary_success !== 'none') { + primarySuccessAgg[facet.primary_success] = + (primarySuccessAgg[facet.primary_success] || 0) + 1; + } + + // Aggregate outcomes + if (facet.outcome) { + outcomesAgg[facet.outcome] = (outcomesAgg[facet.outcome] || 0) + 1; + } + + // Aggregate goals + Object.entries(facet.goal_categories).forEach(([goal, count]) => { + goalsAgg[goal] = (goalsAgg[goal] || 0) + count; + }); + }); + + return { + satisfactionAgg, + frictionAgg, + primarySuccessAgg, + outcomesAgg, + goalsAgg, + }; + } + + private async generateQualitativeInsights( + metrics: Omit, + facets: SessionFacets[], + ): Promise { + if (facets.length === 0) { + return undefined; + } + + logger.info('Generating qualitative insights...'); + + const commonData = this.prepareCommonPromptData(metrics, facets); + + const generate = async ( + promptTemplate: string, + schema: Record, + ): Promise => { + const prompt = `${promptTemplate}\n\n${commonData}`; + try { + const result = await this.config.getBaseLlmClient().generateJson({ + model: this.config.getModel(), + contents: [{ role: 'user', parts: [{ text: prompt }] }], + schema, + abortSignal: AbortSignal.timeout(600000), + }); + return result as T; + } catch (error) { + logger.error('Failed to generate insight:', error); + throw error; + } + }; + + // Schemas for each insight type + // We define simplified schemas here to guide the LLM. + // The types are already defined in QualitativeInsightTypes.ts + + // 1. Impressive Workflows + const schemaImpressiveWorkflows = { + type: 'object', + properties: { + intro: { type: 'string' }, + impressive_workflows: { + type: 'array', + items: { + type: 'object', + properties: { + title: { type: 'string' }, + description: { type: 'string' }, + }, + required: ['title', 'description'], + }, + }, + }, + required: ['intro', 'impressive_workflows'], + }; + + // 2. Project Areas + const schemaProjectAreas = { + type: 'object', + properties: { + areas: { + type: 'array', + items: { + type: 'object', + properties: { + name: { type: 'string' }, + session_count: { type: 'number' }, + description: { type: 'string' }, + }, + required: ['name', 'session_count', 'description'], + }, + }, + }, + required: ['areas'], + }; + + // 3. Future Opportunities + const schemaFutureOpportunities = { + type: 'object', + properties: { + intro: { type: 'string' }, + opportunities: { + type: 'array', + items: { + type: 'object', + properties: { + title: { type: 'string' }, + whats_possible: { type: 'string' }, + how_to_try: { type: 'string' }, + copyable_prompt: { type: 'string' }, + }, + required: [ + 'title', + 'whats_possible', + 'how_to_try', + 'copyable_prompt', + ], + }, + }, + }, + required: ['intro', 'opportunities'], + }; + + // 4. Friction Points + const schemaFrictionPoints = { + type: 'object', + properties: { + intro: { type: 'string' }, + categories: { + type: 'array', + items: { + type: 'object', + properties: { + category: { type: 'string' }, + description: { type: 'string' }, + examples: { type: 'array', items: { type: 'string' } }, + }, + required: ['category', 'description', 'examples'], + }, + }, + }, + required: ['intro', 'categories'], + }; + + // 5. Memorable Moment + const schemaMemorableMoment = { + type: 'object', + properties: { + headline: { type: 'string' }, + detail: { type: 'string' }, + }, + required: ['headline', 'detail'], + }; + + // 6. Improvements + const schemaImprovements = { + type: 'object', + properties: { + Qwen_md_additions: { + type: 'array', + items: { + type: 'object', + properties: { + addition: { type: 'string' }, + why: { type: 'string' }, + prompt_scaffold: { type: 'string' }, + }, + required: ['addition', 'why', 'prompt_scaffold'], + }, + }, + features_to_try: { + type: 'array', + items: { + type: 'object', + properties: { + feature: { type: 'string' }, + one_liner: { type: 'string' }, + why_for_you: { type: 'string' }, + example_code: { type: 'string' }, + }, + required: ['feature', 'one_liner', 'why_for_you', 'example_code'], + }, + }, + usage_patterns: { + type: 'array', + items: { + type: 'object', + properties: { + title: { type: 'string' }, + suggestion: { type: 'string' }, + detail: { type: 'string' }, + copyable_prompt: { type: 'string' }, + }, + required: ['title', 'suggestion', 'detail', 'copyable_prompt'], + }, + }, + }, + required: ['Qwen_md_additions', 'features_to_try', 'usage_patterns'], + }; + + // 7. Interaction Style + const schemaInteractionStyle = { + type: 'object', + properties: { + narrative: { type: 'string' }, + key_pattern: { type: 'string' }, + }, + required: ['narrative', 'key_pattern'], + }; + + // 8. At A Glance + const schemaAtAGlance = { + type: 'object', + properties: { + whats_working: { type: 'string' }, + whats_hindering: { type: 'string' }, + quick_wins: { type: 'string' }, + ambitious_workflows: { type: 'string' }, + }, + required: [ + 'whats_working', + 'whats_hindering', + 'quick_wins', + 'ambitious_workflows', + ], + }; + + const limit = pLimit(CONCURRENCY_LIMIT); + + try { + const [ + impressiveWorkflows, + projectAreas, + futureOpportunities, + frictionPoints, + memorableMoment, + improvements, + interactionStyle, + atAGlance, + ] = await Promise.all([ + limit(() => + generate( + getInsightPrompt('impressive_workflows'), + schemaImpressiveWorkflows, + ), + ), + limit(() => + generate( + getInsightPrompt('project_areas'), + schemaProjectAreas, + ), + ), + limit(() => + generate( + getInsightPrompt('future_opportunities'), + schemaFutureOpportunities, + ), + ), + limit(() => + generate( + getInsightPrompt('friction_points'), + schemaFrictionPoints, + ), + ), + limit(() => + generate( + getInsightPrompt('memorable_moment'), + schemaMemorableMoment, + ), + ), + limit(() => + generate( + getInsightPrompt('improvements'), + schemaImprovements, + ), + ), + limit(() => + generate( + getInsightPrompt('interaction_style'), + schemaInteractionStyle, + ), + ), + limit(() => + generate( + getInsightPrompt('at_a_glance'), + schemaAtAGlance, + ), + ), + ]); + + logger.debug( + JSON.stringify( + { + impressiveWorkflows, + projectAreas, + futureOpportunities, + frictionPoints, + memorableMoment, + improvements, + interactionStyle, + atAGlance, + }, + null, + 2, + ), + ); + + return { + impressiveWorkflows, + projectAreas, + futureOpportunities, + frictionPoints, + memorableMoment, + improvements, + interactionStyle, + atAGlance, + }; + } catch (e) { + logger.error('Error generating qualitative insights:', e); + return undefined; + } + } + + private prepareCommonPromptData( + metrics: Omit, + facets: SessionFacets[], + ): string { + // 1. DATA section + const goalsAgg: Record = {}; + const outcomesAgg: Record = {}; + const satisfactionAgg: Record = {}; + const frictionAgg: Record = {}; + const successAgg: Record = {}; + + facets.forEach((facet) => { + // Aggregate goals + Object.entries(facet.goal_categories).forEach(([goal, count]) => { + goalsAgg[goal] = (goalsAgg[goal] || 0) + count; + }); + + // Aggregate outcomes + outcomesAgg[facet.outcome] = (outcomesAgg[facet.outcome] || 0) + 1; + + // Aggregate satisfaction + Object.entries(facet.user_satisfaction_counts).forEach(([sat, count]) => { + satisfactionAgg[sat] = (satisfactionAgg[sat] || 0) + count; + }); + + // Aggregate friction + Object.entries(facet.friction_counts).forEach(([fric, count]) => { + frictionAgg[fric] = (frictionAgg[fric] || 0) + count; + }); + + // Aggregate success (primary_success) + if (facet.primary_success && facet.primary_success !== 'none') { + successAgg[facet.primary_success] = + (successAgg[facet.primary_success] || 0) + 1; + } + }); + + const topGoals = Object.entries(goalsAgg) + .sort((a, b) => b[1] - a[1]) + .slice(0, 8); + + const dataObj = { + sessions: metrics.totalSessions || facets.length, + analyzed: facets.length, + date_range: { + start: Object.keys(metrics.heatmap).sort()[0] || 'N/A', + end: Object.keys(metrics.heatmap).sort().pop() || 'N/A', + }, + messages: metrics.totalMessages || 0, + hours: metrics.totalHours || 0, + commits: 0, // Not tracked yet + top_tools: metrics.topTools || [], + top_goals: topGoals, + outcomes: outcomesAgg, + satisfaction: satisfactionAgg, + friction: frictionAgg, + success: successAgg, + }; + + // 2. SESSION SUMMARIES section + const sessionSummaries = facets + .map((f) => `- ${f.brief_summary}`) + .join('\n'); + + // 3. FRICTION DETAILS section + const frictionDetails = facets + .filter((f) => f.friction_detail && f.friction_detail.trim().length > 0) + .map((f) => `- ${f.friction_detail}`) + .join('\n'); + + return `DATA: +${JSON.stringify(dataObj, null, 2)} + +SESSION SUMMARIES: +${sessionSummaries} + +FRICTION DETAILS: +${frictionDetails} + +USER INSTRUCTIONS TO Qwen: +None captured`; + } + + private async scanChatFiles( + baseDir: string, + ): Promise> { + const allChatFiles: Array<{ path: string; mtime: number }> = []; + + try { + // Get all project directories in the base directory + const projectDirs = await fs.readdir(baseDir); + + // Process each project directory + for (const projectDir of projectDirs) { + const projectPath = path.join(baseDir, projectDir); + const stats = await fs.stat(projectPath); + + // Only process if it's a directory + if (stats.isDirectory()) { + const chatsDir = path.join(projectPath, 'chats'); + + try { + // Get all chat files in the chats directory + const files = await fs.readdir(chatsDir); + const chatFiles = files.filter((file) => file.endsWith('.jsonl')); + + for (const file of chatFiles) { + const filePath = path.join(chatsDir, file); + + // Get file stats for sorting by recency + try { + const fileStats = await fs.stat(filePath); + allChatFiles.push({ path: filePath, mtime: fileStats.mtimeMs }); + } catch (e) { + logger.error(`Failed to stat file ${filePath}:`, e); + } + } + } catch (error) { + if ((error as NodeJS.ErrnoException).code !== 'ENOENT') { + logger.error( + `Error reading chats directory for project ${projectDir}: ${error}`, + ); + } + // Continue to next project if chats directory doesn't exist + continue; + } + } + } + } catch (error) { + if ((error as NodeJS.ErrnoException).code === 'ENOENT') { + // Base directory doesn't exist, return empty + logger.info(`Base directory does not exist: ${baseDir}`); + } else { + logger.error(`Error reading base directory: ${error}`); + } + } + + return allChatFiles; + } + + private async generateMetrics( + files: Array<{ path: string; mtime: number }>, + onProgress?: InsightProgressCallback, + ): Promise> { + // Initialize data structures + const heatmap: HeatMapData = {}; + const activeHours: { [hour: number]: number } = {}; + const sessionStartTimes: { [sessionId: string]: Date } = {}; + const sessionEndTimes: { [sessionId: string]: Date } = {}; + let totalMessages = 0; + let totalLinesAdded = 0; + let totalLinesRemoved = 0; + const uniqueFiles = new Set(); + const toolUsage: Record = {}; + + // Process files in batches to avoid OOM and blocking the event loop + const BATCH_SIZE = 50; + const totalFiles = files.length; + + for (let i = 0; i < totalFiles; i += BATCH_SIZE) { + const batchEnd = Math.min(i + BATCH_SIZE, totalFiles); + const batch = files.slice(i, batchEnd); + + // Process batch sequentially to minimize memory usage + for (const fileInfo of batch) { + try { + const records = await readJsonlFile(fileInfo.path); + + // Process each record + for (const record of records) { + const timestamp = new Date(record.timestamp); + const dateKey = this.formatDate(timestamp); + const hour = timestamp.getHours(); + + // Count user messages and slash commands (actual user interactions) + const isUserMessage = record.type === 'user'; + const isSlashCommand = + record.type === 'system' && record.subtype === 'slash_command'; + if (isUserMessage || isSlashCommand) { + totalMessages++; + + // Update heatmap (count of user interactions per day) + heatmap[dateKey] = (heatmap[dateKey] || 0) + 1; + + // Update active hours + activeHours[hour] = (activeHours[hour] || 0) + 1; + } + + // Track session times + if (!sessionStartTimes[record.sessionId]) { + sessionStartTimes[record.sessionId] = timestamp; + } + sessionEndTimes[record.sessionId] = timestamp; + + // Track tool usage + if (record.type === 'assistant' && record.message?.parts) { + for (const part of record.message.parts) { + if ('functionCall' in part) { + const name = part.functionCall!.name!; + toolUsage[name] = (toolUsage[name] || 0) + 1; + } + } + } + + // Track lines and files from tool results + if ( + record.type === 'tool_result' && + record.toolCallResult?.resultDisplay + ) { + const display = record.toolCallResult.resultDisplay; + // Check if it matches FileDiff shape + if ( + typeof display === 'object' && + display !== null && + 'fileName' in display + ) { + // Cast to any to avoid importing FileDiff type which might not be available here + const diff = display as { + fileName: unknown; + diffStat?: { + model_added_lines?: number; + model_removed_lines?: number; + }; + }; + if (typeof diff.fileName === 'string') { + uniqueFiles.add(diff.fileName); + } + + if (diff.diffStat) { + totalLinesAdded += diff.diffStat.model_added_lines || 0; + totalLinesRemoved += diff.diffStat.model_removed_lines || 0; + } + } + } + } + } catch (error) { + logger.error( + `Failed to process metrics for file ${fileInfo.path}:`, + error, + ); + // Continue to next file + } + } + + // Update progress (mapped to 10-20% range of total progress) + if (onProgress) { + const percentComplete = batchEnd / totalFiles; + const overallProgress = 10 + Math.round(percentComplete * 10); + onProgress( + `Crunching the numbers (${batchEnd}/${totalFiles})`, + overallProgress, + ); + } + + // Yield to event loop to allow GC and UI updates + await new Promise((resolve) => setTimeout(resolve, 0)); + } + + // Calculate streak data + const streakData = this.calculateStreaks(Object.keys(heatmap)); + + // Calculate longest work session and total hours + let longestWorkDuration = 0; + let longestWorkDate: string | null = null; + let totalDurationMs = 0; + + const sessionIds = Object.keys(sessionStartTimes); + const totalSessions = sessionIds.length; + + for (const sessionId of sessionIds) { + const start = sessionStartTimes[sessionId]; + const end = sessionEndTimes[sessionId]; + const durationMs = end.getTime() - start.getTime(); + const durationMinutes = Math.round(durationMs / (1000 * 60)); + + totalDurationMs += durationMs; + + if (durationMinutes > longestWorkDuration) { + longestWorkDuration = durationMinutes; + longestWorkDate = this.formatDate(start); + } + } + + const totalHours = Math.round(totalDurationMs / (1000 * 60 * 60)); + + // Calculate latest active time + let latestActiveTime: string | null = null; + let latestTimestamp = new Date(0); + for (const dateStr in heatmap) { + const date = new Date(dateStr); + if (date > latestTimestamp) { + latestTimestamp = date; + latestActiveTime = date.toLocaleTimeString([], { + hour: '2-digit', + minute: '2-digit', + }); + } + } + + // Calculate top tools + const topTools = Object.entries(toolUsage) + .sort((a, b) => b[1] - a[1]) + .slice(0, 10); + + return { + heatmap, + currentStreak: streakData.currentStreak, + longestStreak: streakData.longestStreak, + longestWorkDate, + longestWorkDuration, + activeHours, + latestActiveTime, + totalSessions, + totalMessages, + totalHours, + topTools, + totalLinesAdded, + totalLinesRemoved, + totalFiles: uniqueFiles.size, + }; + } + + private async generateFacets( + allFiles: Array<{ path: string; mtime: number }>, + facetsOutputDir?: string, + onProgress?: InsightProgressCallback, + ): Promise { + const MAX_ELIGIBLE_SESSIONS = 50; + + // Sort files by recency (descending), then select up to 50 conversational + // sessions (must contain both user and assistant records). + const sortedFiles = [...allFiles].sort((a, b) => b.mtime - a.mtime); + const eligibleSessions: Array<{ + fileInfo: { path: string; mtime: number }; + records: ChatRecord[]; + }> = []; + + for (const fileInfo of sortedFiles) { + if (eligibleSessions.length >= MAX_ELIGIBLE_SESSIONS) { + break; + } + + try { + const records = await readJsonlFile(fileInfo.path); + if (!this.hasUserAndAssistantRecords(records)) { + continue; + } + eligibleSessions.push({ fileInfo, records }); + } catch (e) { + logger.error( + `Error reading session file ${fileInfo.path} for facet eligibility:`, + e, + ); + } + } + + logger.info( + `Analyzing ${eligibleSessions.length} eligible recent sessions with LLM...`, + ); + + // Create a limit function with concurrency of 4 to avoid 429 errors + const limit = pLimit(CONCURRENCY_LIMIT); + + let completed = 0; + const total = eligibleSessions.length; + + // Analyze sessions concurrently with limit + const analysisPromises = eligibleSessions.map(({ fileInfo, records }) => + limit(async () => { + try { + // Check if we already have this session analyzed + if (records.length > 0 && facetsOutputDir) { + const sessionId = records[0].sessionId; + if (sessionId) { + const existingFacetPath = path.join( + facetsOutputDir, + `${sessionId}.json`, + ); + try { + // Check if file exists and is readable + const existingData = await fs.readFile( + existingFacetPath, + 'utf-8', + ); + const existingFacet = JSON.parse(existingData); + completed++; + if (onProgress) { + const percent = 20 + Math.round((completed / total) * 60); + onProgress( + 'Analyzing sessions', + percent, + `${completed}/${total}`, + ); + } + return existingFacet; + } catch (readError) { + // File doesn't exist or is invalid, proceed to analyze + if ((readError as NodeJS.ErrnoException).code !== 'ENOENT') { + logger.warn( + `Failed to read existing facet for ${sessionId}, regenerating:`, + readError, + ); + } + } + } + } + + const facet = await this.analyzeSession(records); + + if (facet && facetsOutputDir) { + try { + const facetPath = path.join( + facetsOutputDir, + `${facet.session_id}.json`, + ); + await fs.writeFile( + facetPath, + JSON.stringify(facet, null, 2), + 'utf-8', + ); + } catch (writeError) { + logger.error( + `Failed to write facet file for session ${facet.session_id}:`, + writeError, + ); + } + } + + completed++; + if (onProgress) { + const percent = 20 + Math.round((completed / total) * 60); + onProgress('Analyzing sessions', percent, `${completed}/${total}`); + } + + return facet; + } catch (e) { + logger.error(`Error analyzing session file ${fileInfo.path}:`, e); + completed++; + if (onProgress) { + const percent = 20 + Math.round((completed / total) * 60); + onProgress('Analyzing sessions', percent, `${completed}/${total}`); + } + return null; + } + }), + ); + + const sessionFacetsWithNulls = await Promise.all(analysisPromises); + const facets = sessionFacetsWithNulls.filter( + (f): f is SessionFacets => f !== null, + ); + return facets; + } +} diff --git a/packages/cli/src/services/insight/generators/StaticInsightGenerator.ts b/packages/cli/src/services/insight/generators/StaticInsightGenerator.ts new file mode 100644 index 000000000..99bcb9e26 --- /dev/null +++ b/packages/cli/src/services/insight/generators/StaticInsightGenerator.ts @@ -0,0 +1,124 @@ +/** + * @license + * Copyright 2025 Qwen Code + * SPDX-License-Identifier: Apache-2.0 + */ + +import fs from 'fs/promises'; +import path from 'path'; +import os from 'os'; +import { DataProcessor } from './DataProcessor.js'; +import { TemplateRenderer } from './TemplateRenderer.js'; +import type { + InsightData, + InsightProgressCallback, +} from '../types/StaticInsightTypes.js'; + +import { createDebugLogger, type Config } from '@qwen-code/qwen-code-core'; + +const logger = createDebugLogger('StaticInsightGenerator'); + +export class StaticInsightGenerator { + private dataProcessor: DataProcessor; + private templateRenderer: TemplateRenderer; + + constructor(config: Config) { + this.dataProcessor = new DataProcessor(config); + this.templateRenderer = new TemplateRenderer(); + } + + // Ensure the output directory exists + private async ensureOutputDirectory(): Promise { + const outputDir = path.join(os.homedir(), '.qwen', 'insights'); + await fs.mkdir(outputDir, { recursive: true }); + return outputDir; + } + + // Generate timestamped filename with collision detection + private async generateOutputPath(outputDir: string): Promise { + const now = new Date(); + const date = now.toISOString().split('T')[0]; // YYYY-MM-DD + const time = now.toTimeString().slice(0, 8).replace(/:/g, ''); // HHMMSS + + let outputPath = path.join(outputDir, `insight-${date}.html`); + + // Check if date-only file exists, if so, add timestamp + try { + await fs.access(outputPath); + // File exists, use timestamped version + outputPath = path.join(outputDir, `insight-${date}-${time}.html`); + } catch { + // File doesn't exist, use date-only name + } + + return outputPath; + } + + // Create or update the "latest" alias (symlink preferred, copy as fallback) + private async updateLatestAlias( + outputDir: string, + targetPath: string, + ): Promise { + const latestPath = path.join(outputDir, 'insight.html'); + const relativeTarget = path.relative(outputDir, targetPath); + + // Remove existing file/symlink if it exists + try { + await fs.unlink(latestPath); + } catch { + // File doesn't exist, ignore + } + + // Try symlink first (preferred - lightweight, always points to latest) + try { + await fs.symlink(relativeTarget, latestPath); + logger.debug('Created insight symlink:', relativeTarget); + return; + } catch (error) { + logger.debug( + 'Failed to create insight symlink, falling back to copy:', + error, + ); + } + + // Fallback: copy file (works everywhere, uses more disk space) + try { + await fs.copyFile(targetPath, latestPath); + logger.debug('Created insight copy:', targetPath); + } catch (error) { + logger.debug('Failed to create insight latest alias:', error); + } + } + + // Generate the static insight HTML file + async generateStaticInsight( + baseDir: string, + onProgress?: InsightProgressCallback, + ): Promise { + // Ensure output directory exists + const outputDir = await this.ensureOutputDirectory(); + const facetsDir = path.join(outputDir, 'facets'); + await fs.mkdir(facetsDir, { recursive: true }); + + // Process data + const insights: InsightData = await this.dataProcessor.generateInsights( + baseDir, + facetsDir, + onProgress, + ); + + // Render HTML + const html = await this.templateRenderer.renderInsightHTML(insights); + + // Generate timestamped output path + const outputPath = await this.generateOutputPath(outputDir); + + // Write the HTML file + await fs.writeFile(outputPath, html, 'utf-8'); + + // Update latest alias (symlink preferred, copy as fallback) + await this.updateLatestAlias(outputDir, outputPath); + + return outputPath; + } +} diff --git a/packages/cli/src/services/insight/generators/TemplateRenderer.ts b/packages/cli/src/services/insight/generators/TemplateRenderer.ts new file mode 100644 index 000000000..8b6f779f7 --- /dev/null +++ b/packages/cli/src/services/insight/generators/TemplateRenderer.ts @@ -0,0 +1,52 @@ +/** + * @license + * Copyright 2025 Qwen Code + * SPDX-License-Identifier: Apache-2.0 + */ + +import { INSIGHT_JS, INSIGHT_CSS } from '@qwen-code/web-templates'; +import type { InsightData } from '../types/StaticInsightTypes.js'; + +export class TemplateRenderer { + // Render the complete HTML file + async renderInsightHTML(insights: InsightData): Promise { + const html = ` + + + + + Qwen Code Insights + + + +
+
+
+
+
+ + + + + + + + + + + + + + + +`; + + return html; + } +} diff --git a/packages/cli/src/services/insight/types/QualitativeInsightTypes.ts b/packages/cli/src/services/insight/types/QualitativeInsightTypes.ts new file mode 100644 index 000000000..fc9546b98 --- /dev/null +++ b/packages/cli/src/services/insight/types/QualitativeInsightTypes.ts @@ -0,0 +1,82 @@ +export interface InsightImpressiveWorkflows { + intro: string; + impressive_workflows: Array<{ + title: string; + description: string; + }>; +} + +export interface InsightProjectAreas { + areas: Array<{ + name: string; + session_count: number; + description: string; + }>; +} + +export interface InsightFutureOpportunities { + intro: string; + opportunities: Array<{ + title: string; + whats_possible: string; + how_to_try: string; + copyable_prompt: string; + }>; +} + +export interface InsightFrictionPoints { + intro: string; + categories: Array<{ + category: string; + description: string; + examples: string[]; + }>; +} + +export interface InsightMemorableMoment { + headline: string; + detail: string; +} + +export interface InsightImprovements { + Qwen_md_additions: Array<{ + addition: string; + why: string; + prompt_scaffold: string; + }>; + features_to_try: Array<{ + feature: string; + one_liner: string; + why_for_you: string; + example_code: string; + }>; + usage_patterns: Array<{ + title: string; + suggestion: string; + detail: string; + copyable_prompt: string; + }>; +} + +export interface InsightInteractionStyle { + narrative: string; + key_pattern: string; +} + +export interface InsightAtAGlance { + whats_working: string; + whats_hindering: string; + quick_wins: string; + ambitious_workflows: string; +} + +export interface QualitativeInsights { + impressiveWorkflows: InsightImpressiveWorkflows; + projectAreas: InsightProjectAreas; + futureOpportunities: InsightFutureOpportunities; + frictionPoints: InsightFrictionPoints; + memorableMoment: InsightMemorableMoment; + improvements: InsightImprovements; + interactionStyle: InsightInteractionStyle; + atAGlance: InsightAtAGlance; +} diff --git a/packages/cli/src/services/insight/types/StaticInsightTypes.ts b/packages/cli/src/services/insight/types/StaticInsightTypes.ts new file mode 100644 index 000000000..29ce39f16 --- /dev/null +++ b/packages/cli/src/services/insight/types/StaticInsightTypes.ts @@ -0,0 +1,90 @@ +/** + * @license + * Copyright 2025 Qwen Code + * SPDX-License-Identifier: Apache-2.0 + */ + +import type { QualitativeInsights } from './QualitativeInsightTypes.js'; + +export interface HeatMapData { + [date: string]: number; +} + +export interface InsightData { + heatmap: HeatMapData; + currentStreak: number; + longestStreak: number; + longestWorkDate: string | null; + longestWorkDuration: number; // in minutes + activeHours: { [hour: number]: number }; + latestActiveTime: string | null; + totalSessions?: number; + totalMessages?: number; + totalHours?: number; + totalLinesAdded?: number; + totalLinesRemoved?: number; + totalFiles?: number; + topTools?: Array<[string, number]>; + qualitative?: QualitativeInsights; + satisfaction?: Record; + friction?: Record; + primarySuccess?: Record; + outcomes?: Record; + topGoals?: Record; +} + +export interface StreakData { + currentStreak: number; + longestStreak: number; + dates: string[]; +} + +export interface SessionFacets { + session_id: string; + underlying_goal: string; + goal_categories: Record; + outcome: + | 'fully_achieved' + | 'mostly_achieved' + | 'partially_achieved' + | 'not_achieved' + | 'unclear_from_transcript'; + user_satisfaction_counts: Record; + Qwen_helpfulness: + | 'unhelpful' + | 'slightly_helpful' + | 'moderately_helpful' + | 'very_helpful' + | 'essential'; + session_type: + | 'single_task' + | 'multi_task' + | 'iterative_refinement' + | 'exploration' + | 'quick_question'; + friction_counts: Record; + friction_detail: string; + primary_success: + | 'none' + | 'fast_accurate_search' + | 'correct_code_edits' + | 'good_explanations' + | 'proactive_help' + | 'multi_file_changes' + | 'good_debugging'; + brief_summary: string; +} + +export interface StaticInsightTemplateData { + styles: string; + content: string; + data: InsightData; + scripts: string; + generatedTime: string; +} + +export type InsightProgressCallback = ( + stage: string, + progress: number, + detail?: string, +) => void; diff --git a/packages/cli/src/ui/AppContainer.test.tsx b/packages/cli/src/ui/AppContainer.test.tsx index 1edec79f9..9e9d4f673 100644 --- a/packages/cli/src/ui/AppContainer.test.tsx +++ b/packages/cli/src/ui/AppContainer.test.tsx @@ -209,6 +209,7 @@ describe('AppContainer State Management', () => { pendingHistoryItems: [], thought: null, cancelOngoingRequest: vi.fn(), + retryLastPrompt: vi.fn(), }); mockedUseVim.mockReturnValue({ handleInput: vi.fn() }); mockedUseFolderTrust.mockReturnValue({ @@ -607,6 +608,7 @@ describe('AppContainer State Management', () => { pendingHistoryItems: [], thought: { subject: thoughtSubject }, cancelOngoingRequest: vi.fn(), + retryLastPrompt: vi.fn(), }); // Act: Render the container @@ -652,6 +654,7 @@ describe('AppContainer State Management', () => { pendingHistoryItems: [], thought: null, cancelOngoingRequest: vi.fn(), + retryLastPrompt: vi.fn(), }); // Act: Render the container @@ -698,6 +701,7 @@ describe('AppContainer State Management', () => { pendingHistoryItems: [], thought: { subject: thoughtSubject }, cancelOngoingRequest: vi.fn(), + retryLastPrompt: vi.fn(), }); // Act: Render the container @@ -744,6 +748,7 @@ describe('AppContainer State Management', () => { pendingHistoryItems: [], thought: { subject: shortTitle }, cancelOngoingRequest: vi.fn(), + retryLastPrompt: vi.fn(), }); // Act: Render the container @@ -794,6 +799,7 @@ describe('AppContainer State Management', () => { pendingHistoryItems: [], thought: { subject: title }, cancelOngoingRequest: vi.fn(), + retryLastPrompt: vi.fn(), }); // Act: Render the container @@ -841,6 +847,7 @@ describe('AppContainer State Management', () => { pendingHistoryItems: [], thought: null, cancelOngoingRequest: vi.fn(), + retryLastPrompt: vi.fn(), }); // Act: Render the container @@ -882,6 +889,7 @@ describe('AppContainer State Management', () => { pendingHistoryItems: [], thought: null, cancelOngoingRequest: vi.fn(), + retryLastPrompt: vi.fn(), activePtyId: 'some-id', }); @@ -1013,6 +1021,7 @@ describe('AppContainer State Management', () => { pendingHistoryItems: [], thought: null, cancelOngoingRequest: mockCancelOngoingRequest, + retryLastPrompt: vi.fn(), }); const mockHandleSlashCommand = vi.fn(); diff --git a/packages/cli/src/ui/AppContainer.tsx b/packages/cli/src/ui/AppContainer.tsx index 9c546004c..781aab375 100644 --- a/packages/cli/src/ui/AppContainer.tsx +++ b/packages/cli/src/ui/AppContainer.tsx @@ -100,8 +100,6 @@ import { t } from '../i18n/index.js'; import { useWelcomeBack } from './hooks/useWelcomeBack.js'; import { useDialogClose } from './hooks/useDialogClose.js'; import { useInitializationAuthError } from './hooks/useInitializationAuthError.js'; -import { type VisionSwitchOutcome } from './components/ModelSwitchDialog.js'; -import { processVisionSwitchOutcome } from './hooks/useVisionAutoSwitch.js'; import { useSubagentCreateDialog } from './hooks/useSubagentCreateDialog.js'; import { useAgentsManagerDialog } from './hooks/useAgentsManagerDialog.js'; import { useAttentionNotifications } from './hooks/useAttentionNotifications.js'; @@ -496,18 +494,6 @@ export const AppContainer = (props: AppContainerProps) => { closeAgentsManagerDialog, } = useAgentsManagerDialog(); - // Vision model auto-switch dialog state (must be before slashCommandActions) - const [isVisionSwitchDialogOpen, setIsVisionSwitchDialogOpen] = - useState(false); - const [visionSwitchResolver, setVisionSwitchResolver] = useState<{ - resolve: (result: { - modelOverride?: string; - persistSessionModel?: string; - showGuidance?: boolean; - }) => void; - reject: () => void; - } | null>(null); - const slashCommandActions = useMemo( () => ({ openAuthDialog, @@ -563,6 +549,7 @@ export const AppContainer = (props: AppContainerProps) => { historyManager.loadHistory, refreshStatic, toggleVimEnabled, + isProcessing, setIsProcessing, setGeminiMdFileCount, slashCommandActions, @@ -571,32 +558,6 @@ export const AppContainer = (props: AppContainerProps) => { logger, ); - // Vision switch handlers - const handleVisionSwitchRequired = useCallback( - async (_query: unknown) => - new Promise<{ - modelOverride?: string; - persistSessionModel?: string; - showGuidance?: boolean; - }>((resolve, reject) => { - setVisionSwitchResolver({ resolve, reject }); - setIsVisionSwitchDialogOpen(true); - }), - [], - ); - - const handleVisionSwitchSelect = useCallback( - (outcome: VisionSwitchOutcome) => { - setIsVisionSwitchDialogOpen(false); - if (visionSwitchResolver) { - const result = processVisionSwitchOutcome(outcome); - visionSwitchResolver.resolve(result); - setVisionSwitchResolver(null); - } - }, - [visionSwitchResolver], - ); - // onDebugMessage should log to debug logfile, not update footer debugMessage const onDebugMessage = useCallback( (message: string) => { @@ -668,6 +629,7 @@ export const AppContainer = (props: AppContainerProps) => { pendingHistoryItems: pendingGeminiHistoryItems, thought, cancelOngoingRequest, + retryLastPrompt, handleApprovalModeChange, activePtyId, loopDetectionConfirmationRequest, @@ -687,12 +649,9 @@ export const AppContainer = (props: AppContainerProps) => { setModelSwitchedFromQuotaError, refreshStatic, () => cancelHandlerRef.current(), - settings.merged.experimental?.visionModelPreview ?? false, // visionModelPreviewEnabled setEmbeddedShellFocused, terminalWidth, terminalHeight, - handleVisionSwitchRequired, // onVisionSwitchRequired - embeddedShellFocused, ); // Track whether suggestions are visible for Tab key handling @@ -847,7 +806,6 @@ export const AppContainer = (props: AppContainerProps) => { !isThemeDialogOpen && !isEditorDialogOpen && !showWelcomeBackDialog && - !isVisionSwitchDialogOpen && welcomeBackChoice !== 'restart' && geminiClient?.isInitialized?.() ) { @@ -863,7 +821,6 @@ export const AppContainer = (props: AppContainerProps) => { isThemeDialogOpen, isEditorDialogOpen, showWelcomeBackDialog, - isVisionSwitchDialogOpen, welcomeBackChoice, geminiClient, ]); @@ -900,6 +857,8 @@ export const AppContainer = (props: AppContainerProps) => { const ctrlCTimerRef = useRef(null); const [ctrlDPressedOnce, setCtrlDPressedOnce] = useState(false); const ctrlDTimerRef = useRef(null); + const [escapePressedOnce, setEscapePressedOnce] = useState(false); + const escapeTimerRef = useRef(null); const [constrainHeight, setConstrainHeight] = useState(true); const [ideContextState, setIdeContextState] = useState< IdeContext | undefined @@ -1176,6 +1135,47 @@ export const AppContainer = (props: AppContainerProps) => { } handleExit(ctrlDPressedOnce, setCtrlDPressedOnce, ctrlDTimerRef); return; + } else if (keyMatchers[Command.ESCAPE](key)) { + // Escape key handling + // Skip if shell is focused (to allow shell's own escape handling) + if (embeddedShellFocused) { + return; + } + + // If input has content, use double-press to clear + if (buffer.text.length > 0) { + if (escapePressedOnce) { + // Second press: clear input, keep the flag to allow immediate cancel + buffer.setText(''); + return; + } + // First press: set flag and show prompt + setEscapePressedOnce(true); + escapeTimerRef.current = setTimeout(() => { + setEscapePressedOnce(false); + escapeTimerRef.current = null; + }, CTRL_EXIT_PROMPT_DURATION_MS); + return; + } + + // Input is empty, cancel request immediately (no double-press needed) + if (streamingState === StreamingState.Responding) { + if (escapeTimerRef.current) { + clearTimeout(escapeTimerRef.current); + escapeTimerRef.current = null; + } + cancelOngoingRequest?.(); + setEscapePressedOnce(false); + return; + } + + // No action available, reset the flag + if (escapeTimerRef.current) { + clearTimeout(escapeTimerRef.current); + escapeTimerRef.current = null; + } + setEscapePressedOnce(false); + return; } let enteringConstrainHeightMode = false; @@ -1220,10 +1220,15 @@ export const AppContainer = (props: AppContainerProps) => { ctrlCPressedOnce, setCtrlCPressedOnce, ctrlCTimerRef, - buffer.text.length, ctrlDPressedOnce, setCtrlDPressedOnce, ctrlDTimerRef, + escapePressedOnce, + setEscapePressedOnce, + escapeTimerRef, + streamingState, + cancelOngoingRequest, + buffer, handleSlashCommand, activePtyId, embeddedShellFocused, @@ -1287,7 +1292,6 @@ export const AppContainer = (props: AppContainerProps) => { isThemeDialogOpen || isSettingsDialogOpen || isModelDialogOpen || - isVisionSwitchDialogOpen || isPermissionsDialogOpen || isAuthDialogOpen || isAuthenticating || @@ -1399,8 +1403,6 @@ export const AppContainer = (props: AppContainerProps) => { extensionsUpdateState, activePtyId, embeddedShellFocused, - // Vision switch dialog - isVisionSwitchDialogOpen, // Welcome back dialog showWelcomeBackDialog, welcomeBackInfo, @@ -1491,8 +1493,6 @@ export const AppContainer = (props: AppContainerProps) => { activePtyId, historyManager, embeddedShellFocused, - // Vision switch dialog - isVisionSwitchDialogOpen, // Welcome back dialog showWelcomeBackDialog, welcomeBackInfo, @@ -1533,9 +1533,8 @@ export const AppContainer = (props: AppContainerProps) => { onSuggestionsVisibilityChange: setHasSuggestionsVisible, refreshStatic, handleFinalSubmit, + handleRetryLastPrompt: retryLastPrompt, handleClearScreen, - // Vision switch dialog - handleVisionSwitchSelect, // Welcome back dialog handleWelcomeBackSelection, handleWelcomeBackClose, @@ -1578,8 +1577,8 @@ export const AppContainer = (props: AppContainerProps) => { handleEscapePromptChange, refreshStatic, handleFinalSubmit, + retryLastPrompt, handleClearScreen, - handleVisionSwitchSelect, handleWelcomeBackSelection, handleWelcomeBackClose, // Subagent dialogs diff --git a/packages/cli/src/ui/auth/AuthDialog.test.tsx b/packages/cli/src/ui/auth/AuthDialog.test.tsx index a975a599e..90b15c968 100644 --- a/packages/cli/src/ui/auth/AuthDialog.test.tsx +++ b/packages/cli/src/ui/auth/AuthDialog.test.tsx @@ -32,6 +32,7 @@ const createMockUIActions = (overrides: Partial = {}): UIActions => { // AuthDialog only uses handleAuthSelect const baseActions = { handleAuthSelect: vi.fn(), + handleRetryLastPrompt: vi.fn(), } as Partial; return { @@ -169,9 +170,9 @@ describe('AuthDialog', () => { const { lastFrame } = renderAuthDialog(settings); - // Since the auth dialog shows API-KEY option now, + // Since the auth dialog shows API Key option now, // it won't show GEMINI_API_KEY messages - expect(lastFrame()).toContain('API-KEY'); + expect(lastFrame()).toContain('API Key'); }); it('should not show the GEMINI_API_KEY message if QWEN_DEFAULT_AUTH_TYPE is set to something else', () => { @@ -257,9 +258,9 @@ describe('AuthDialog', () => { const { lastFrame } = renderAuthDialog(settings); - // Since the auth dialog shows API-KEY option now, + // Since the auth dialog shows API Key option now, // it won't show GEMINI_API_KEY messages - expect(lastFrame()).toContain('API-KEY'); + expect(lastFrame()).toContain('API Key'); }); }); @@ -305,7 +306,7 @@ describe('AuthDialog', () => { const { lastFrame } = renderAuthDialog(settings); // QWEN_OAUTH is the first option, so it should be selected - expect(lastFrame()).toContain('● 1. Qwen OAuth'); + expect(lastFrame()).toContain('Qwen OAuth'); }); it('should fall back to default if QWEN_DEFAULT_AUTH_TYPE is not set', () => { @@ -345,7 +346,7 @@ describe('AuthDialog', () => { const { lastFrame } = renderAuthDialog(settings); // Default is Qwen OAuth (first option) - expect(lastFrame()).toContain('● 1. Qwen OAuth'); + expect(lastFrame()).toContain('Qwen OAuth'); }); it('should show an error and fall back to default if QWEN_DEFAULT_AUTH_TYPE is invalid', () => { @@ -388,7 +389,7 @@ describe('AuthDialog', () => { // Since the auth dialog doesn't show QWEN_DEFAULT_AUTH_TYPE errors anymore, // it will just show the default Qwen OAuth option - expect(lastFrame()).toContain('● 1. Qwen OAuth'); + expect(lastFrame()).toContain('Qwen OAuth'); }); }); diff --git a/packages/cli/src/ui/auth/AuthDialog.tsx b/packages/cli/src/ui/auth/AuthDialog.tsx index 2d4794d72..309e77adf 100644 --- a/packages/cli/src/ui/auth/AuthDialog.tsx +++ b/packages/cli/src/ui/auth/AuthDialog.tsx @@ -10,17 +10,20 @@ import { AuthType } from '@qwen-code/qwen-code-core'; import { Box, Text } from 'ink'; import Link from 'ink-link'; import { theme } from '../semantic-colors.js'; -import { Colors } from '../colors.js'; import { useKeypress } from '../hooks/useKeypress.js'; -import { RadioButtonSelect } from '../components/shared/RadioButtonSelect.js'; +import { DescriptiveRadioButtonSelect } from '../components/shared/DescriptiveRadioButtonSelect.js'; import { ApiKeyInput } from '../components/ApiKeyInput.js'; import { useUIState } from '../contexts/UIStateContext.js'; import { useUIActions } from '../contexts/UIActionsContext.js'; import { useConfig } from '../contexts/ConfigContext.js'; import { t } from '../../i18n/index.js'; +import { + CodingPlanRegion, + isCodingPlanConfig, +} from '../../constants/codingPlan.js'; const MODEL_PROVIDERS_DOCUMENTATION_URL = - 'https://qwenlm.github.io/qwen-code-docs/en/users/configuration/settings/#modelproviders'; + 'https://qwenlm.github.io/qwen-code-docs/en/users/configuration/model-providers/'; function parseDefaultAuthType( defaultAuthType: string | undefined, @@ -34,11 +37,11 @@ function parseDefaultAuthType( return null; } -// Sub-mode types for API-KEY authentication -type ApiKeySubMode = 'coding-plan' | 'custom'; +// Main menu option type +type MainOption = typeof AuthType.QWEN_OAUTH | 'CODING_PLAN' | 'API_KEY'; // View level for navigation -type ViewLevel = 'main' | 'api-key-sub' | 'api-key-input' | 'custom-info'; +type ViewLevel = 'main' | 'region-select' | 'api-key-input' | 'custom-info'; export function AuthDialog(): React.JSX.Element { const { pendingAuthType, authError } = useUIState(); @@ -50,50 +53,107 @@ export function AuthDialog(): React.JSX.Element { const config = useConfig(); const [errorMessage, setErrorMessage] = useState(null); - const [selectedIndex, setSelectedIndex] = useState(null); const [viewLevel, setViewLevel] = useState('main'); - const [apiKeySubModeIndex, setApiKeySubModeIndex] = useState(0); + const [regionIndex, setRegionIndex] = useState(0); + const [region, setRegion] = useState( + CodingPlanRegion.CHINA, + ); - // Main authentication entries + // Main authentication entries (flat three-option layout) const mainItems = [ { key: AuthType.QWEN_OAUTH, + title: t('Qwen OAuth'), label: t('Qwen OAuth'), - value: AuthType.QWEN_OAUTH, + description: t( + 'Free \u00B7 Up to 1,000 requests/day \u00B7 Qwen latest models', + ), + value: AuthType.QWEN_OAUTH as MainOption, }, { - key: 'API-KEY', - label: t('API-KEY'), - value: 'API-KEY' as const, + key: 'CODING_PLAN', + title: t('Alibaba Cloud Coding Plan'), + label: t('Alibaba Cloud Coding Plan'), + description: t( + 'Paid \u00B7 Up to 6,000 requests/5 hrs \u00B7 All Alibaba Cloud Coding Plan Models', + ), + value: 'CODING_PLAN' as MainOption, + }, + { + key: 'API_KEY', + title: t('API Key'), + label: t('API Key'), + description: t('Bring your own API key'), + value: 'API_KEY' as MainOption, }, ]; - // API-KEY sub-mode entries - const apiKeySubItems = [ + // Region selection entries (shown after selecting Alibaba Cloud Coding Plan) + const regionItems = [ { - key: 'coding-plan', - label: t('Coding Plan (Bailian)'), - value: 'coding-plan' as ApiKeySubMode, + key: 'china', + title: '阿里云百炼 (aliyun.com)', + label: '阿里云百炼 (aliyun.com)', + description: ( + + + https://help.aliyun.com/zh/model-studio/coding-plan + + + ), + value: CodingPlanRegion.CHINA, }, { - key: 'custom', - label: t('Custom'), - value: 'custom' as ApiKeySubMode, + key: 'global', + title: 'Alibaba Cloud (alibabacloud.com)', + label: 'Alibaba Cloud (alibabacloud.com)', + description: ( + + + https://www.alibabacloud.com/help/en/model-studio/coding-plan + + + ), + value: CodingPlanRegion.GLOBAL, }, ]; + // Map an AuthType to the corresponding main menu option. + // QWEN_OAUTH maps directly; any other auth type maps to CODING_PLAN only + // if the current config actually uses a Coding Plan baseUrl+envKey, + // otherwise it maps to API_KEY. + const contentGenConfig = config.getContentGeneratorConfig(); + const isCurrentlyCodingPlan = + isCodingPlanConfig( + contentGenConfig?.baseUrl, + contentGenConfig?.apiKeyEnvKey, + ) !== false; + + const authTypeToMainOption = (authType: AuthType): MainOption => { + if (authType === AuthType.QWEN_OAUTH) return AuthType.QWEN_OAUTH; + if (authType === AuthType.USE_OPENAI && isCurrentlyCodingPlan) + return 'CODING_PLAN'; + return 'API_KEY'; + }; + const initialAuthIndex = Math.max( 0, mainItems.findIndex((item) => { // Priority 1: pendingAuthType if (pendingAuthType) { - return item.value === pendingAuthType; + return item.value === authTypeToMainOption(pendingAuthType); } // Priority 2: config.getAuthType() - the source of truth const currentAuthType = config.getAuthType(); if (currentAuthType) { - return item.value === currentAuthType; + return item.value === authTypeToMainOption(currentAuthType); } // Priority 3: QWEN_DEFAULT_AUTH_TYPE env var @@ -101,7 +161,7 @@ export function AuthDialog(): React.JSX.Element { process.env['QWEN_DEFAULT_AUTH_TYPE'], ); if (defaultAuthType) { - return item.value === defaultAuthType; + return item.value === authTypeToMainOption(defaultAuthType); } // Priority 4: default to QWEN_OAUTH @@ -109,21 +169,19 @@ export function AuthDialog(): React.JSX.Element { }), ); - const hasApiKey = Boolean(config.getContentGeneratorConfig()?.apiKey); - const currentSelectedAuthType = - selectedIndex !== null - ? mainItems[selectedIndex]?.value - : mainItems[initialAuthIndex]?.value; - - const handleMainSelect = async ( - value: (typeof mainItems)[number]['value'], - ) => { + const handleMainSelect = async (value: MainOption) => { setErrorMessage(null); onAuthError(null); - if (value === 'API-KEY') { - // Navigate to API-KEY sub-mode selection - setViewLevel('api-key-sub'); + if (value === 'CODING_PLAN') { + // Navigate to region selection + setViewLevel('region-select'); + return; + } + + if (value === 'API_KEY') { + // Navigate directly to custom API key info + setViewLevel('custom-info'); return; } @@ -131,15 +189,11 @@ export function AuthDialog(): React.JSX.Element { await onAuthSelect(value); }; - const handleApiKeySubSelect = async (subMode: ApiKeySubMode) => { + const handleRegionSelect = async (selectedRegion: CodingPlanRegion) => { setErrorMessage(null); onAuthError(null); - - if (subMode === 'coding-plan') { - setViewLevel('api-key-input'); - } else { - setViewLevel('custom-info'); - } + setRegion(selectedRegion); + setViewLevel('api-key-input'); }; const handleApiKeyInputSubmit = async (apiKey: string) => { @@ -150,18 +204,18 @@ export function AuthDialog(): React.JSX.Element { return; } - // Submit to parent for processing - await handleCodingPlanSubmit(apiKey); + // Submit to parent for processing with region info + await handleCodingPlanSubmit(apiKey, region); }; const handleGoBack = () => { setErrorMessage(null); onAuthError(null); - if (viewLevel === 'api-key-sub') { + if (viewLevel === 'region-select' || viewLevel === 'custom-info') { setViewLevel('main'); - } else if (viewLevel === 'api-key-input' || viewLevel === 'custom-info') { - setViewLevel('api-key-sub'); + } else if (viewLevel === 'api-key-input') { + setViewLevel('region-select'); } }; @@ -169,7 +223,7 @@ export function AuthDialog(): React.JSX.Element { (key) => { if (key.name === 'escape') { // Handle Escape based on current view level - if (viewLevel === 'api-key-sub') { + if (viewLevel === 'region-select') { handleGoBack(); return; } @@ -201,60 +255,39 @@ export function AuthDialog(): React.JSX.Element { const renderMainView = () => ( <> - {t('How would you like to authenticate for this project?')} - - - { - const index = mainItems.findIndex((item) => item.value === value); - setSelectedIndex(index); - }} + itemGap={1} /> - - - {currentSelectedAuthType === AuthType.QWEN_OAUTH - ? t('Login with QwenChat account to use daily free quota.') - : t('Use coding plan credentials or your own api-keys/providers.')} - - ); - // Render API-KEY sub-mode selection - const renderApiKeySubView = () => ( + // Render region selection for Alibaba Cloud Coding Plan + const renderRegionSelectView = () => ( <> - {t('Select API-KEY configuration mode:')} - - - { - const index = apiKeySubItems.findIndex( - (item) => item.value === value, - ); - setApiKeySubModeIndex(index); - }} - /> - - - - {apiKeySubItems[apiKeySubModeIndex]?.value === 'coding-plan' - ? t("Paste your api key of Bailian Coding Plan and you're all set!") - : t( - 'More instructions about configuring `modelProviders` manually.', - )} + + {t('Choose based on where your account is registered')} + + { + const index = regionItems.findIndex((item) => item.value === value); + setRegionIndex(index); + }} + itemGap={1} + /> + - {t('(Press Escape to go back)')} + {t('Enter to select, ↑↓ to navigate, Esc to go back')} @@ -263,7 +296,11 @@ export function AuthDialog(): React.JSX.Element { // Render API key input for coding-plan mode const renderApiKeyInputView = () => ( - + ); @@ -271,68 +308,22 @@ export function AuthDialog(): React.JSX.Element { const renderCustomInfoView = () => ( <> - {t('Custom API-KEY Configuration')} - - - - {t('For advanced users who want to configure models manually.')} + + {t('You can configure your API key and models in settings.json')} - {t('Please configure your models in settings.json:')} - - - - 1. {t('Set API key via environment variable (e.g., OPENAI_API_KEY)')} - - - - - 2.{' '} - {t( - "Add model configuration to modelProviders['openai'] (or other auth types)", - )} - - - - - 3.{' '} - {t( - 'Each provider needs: id, envKey (required), plus optional baseUrl, generationConfig', - )} - - - - - 4.{' '} - {t( - 'Use /model command to select your preferred model from the configured list', - )} - - - - - {t( - 'Supported auth types: openai, anthropic, gemini, vertex-ai, etc.', - )} - - - - - {t('More instructions please check:')} - + {t('Refer to the documentation for setup instructions')} - + {MODEL_PROVIDERS_DOCUMENTATION_URL} - - {t('(Press Escape to go back)')} - + {t('Esc to go back')} ); @@ -340,15 +331,15 @@ export function AuthDialog(): React.JSX.Element { const getViewTitle = () => { switch (viewLevel) { case 'main': - return t('Get started'); - case 'api-key-sub': - return t('API-KEY Configuration'); + return t('Select Authentication Method'); + case 'region-select': + return t('Select Region for Coding Plan'); case 'api-key-input': - return t('Coding Plan Setup'); + return t('Enter Coding Plan API Key'); case 'custom-info': return t('Custom Configuration'); default: - return t('Get started'); + return t('Select Authentication Method'); } }; @@ -363,43 +354,40 @@ export function AuthDialog(): React.JSX.Element { {getViewTitle()} {viewLevel === 'main' && renderMainView()} - {viewLevel === 'api-key-sub' && renderApiKeySubView()} + {viewLevel === 'region-select' && renderRegionSelectView()} {viewLevel === 'api-key-input' && renderApiKeyInputView()} {viewLevel === 'custom-info' && renderCustomInfoView()} {(authError || errorMessage) && ( - {authError || errorMessage} + {authError || errorMessage} )} {viewLevel === 'main' && ( <> - - - {t('(Use Enter to Set Auth)')} + {/* + + {t('Enter to select, \u2191\u2193 to navigate, Esc to close')} + + */} + + {'\u2500'.repeat(80)} + + + + {t('Terms of Services and Privacy Notice')}: - {hasApiKey && currentSelectedAuthType === AuthType.QWEN_OAUTH && ( - - - {t( - 'Note: Your existing API key in settings.json will not be cleared when using Qwen OAuth. You can switch back to OpenAI authentication later if needed.', - )} + + + + https://qwenlm.github.io/qwen-code-docs/en/users/support/tos-privacy/ - - )} - - - {t('Terms of Services and Privacy Notice for Qwen Code')} - - - - - { - 'https://qwenlm.github.io/qwen-code-docs/en/users/support/tos-privacy/' - } - + )} diff --git a/packages/cli/src/ui/auth/useAuth.ts b/packages/cli/src/ui/auth/useAuth.ts index 0ea157af5..24cfbf61c 100644 --- a/packages/cli/src/ui/auth/useAuth.ts +++ b/packages/cli/src/ui/auth/useAuth.ts @@ -30,10 +30,12 @@ import { AuthState, MessageType } from '../types.js'; import type { HistoryItem } from '../types.js'; import { t } from '../../i18n/index.js'; import { - CODING_PLAN_MODELS, + getCodingPlanConfig, + isCodingPlanConfig, + CodingPlanRegion, CODING_PLAN_ENV_KEY, - CODING_PLAN_VERSION, } from '../../constants/codingPlan.js'; +import { backupSettingsFile } from '../../utils/settingsUtils.js'; export type { QwenAuthState } from '../hooks/useQwenAuth.js'; @@ -285,29 +287,39 @@ export const useAuthCommand = ( /** * Handle coding plan submission - generates configs from template and stores api-key + * @param apiKey - The API key to store + * @param region - The region to use (default: CHINA) */ const handleCodingPlanSubmit = useCallback( - async (apiKey: string) => { + async ( + apiKey: string, + region: CodingPlanRegion = CodingPlanRegion.CHINA, + ) => { try { setIsAuthenticating(true); setAuthError(null); - const envKeyName = CODING_PLAN_ENV_KEY; + // Get configuration based on region + const { template, version } = getCodingPlanConfig(region); // Get persist scope const persistScope = getPersistScopeForModelSelection(settings); - // Store api-key in settings.env - settings.setValue(persistScope, `env.${envKeyName}`, apiKey); + // Backup settings file before modification + const settingsFile = settings.forScope(persistScope); + backupSettingsFile(settingsFile.path); + + // Store api-key in settings.env (unified env key) + settings.setValue(persistScope, `env.${CODING_PLAN_ENV_KEY}`, apiKey); // Sync to process.env immediately so refreshAuth can read the apiKey - process.env[envKeyName] = apiKey; + process.env[CODING_PLAN_ENV_KEY] = apiKey; // Generate model configs from template - const newConfigs: ProviderModelConfig[] = CODING_PLAN_MODELS.map( + const newConfigs: ProviderModelConfig[] = template.map( (templateConfig) => ({ ...templateConfig, - envKey: envKeyName, + envKey: CODING_PLAN_ENV_KEY, }), ); @@ -317,17 +329,9 @@ export const useAuthCommand = ( settings.merged.modelProviders as ModelProvidersConfig | undefined )?.[AuthType.USE_OPENAI] || []; - // Identify Coding Plan configs by baseUrl + envKey - // Remove existing Coding Plan configs to ensure template changes are applied - const isCodingPlanConfig = (config: ProviderModelConfig) => - config.envKey === envKeyName && - CODING_PLAN_MODELS.some( - (template) => template.baseUrl === config.baseUrl, - ); - - // Filter out existing Coding Plan configs, keep user custom configs + // Filter out all existing Coding Plan configs (mutually exclusive) const nonCodingPlanConfigs = existingConfigs.filter( - (existing) => !isCodingPlanConfig(existing), + (existing) => !isCodingPlanConfig(existing.baseUrl, existing.envKey), ); // Add new Coding Plan configs at the beginning @@ -347,12 +351,11 @@ export const useAuthCommand = ( AuthType.USE_OPENAI, ); - // Persist coding plan version for future update detection - settings.setValue( - persistScope, - 'codingPlan.version', - CODING_PLAN_VERSION, - ); + // Persist coding plan region + settings.setValue(persistScope, 'codingPlan.region', region); + + // Persist coding plan version (single field for backward compatibility) + settings.setValue(persistScope, 'codingPlan.version', version); // If there are configs, use the first one as the model if (updatedConfigs.length > 0 && updatedConfigs[0]?.id) { @@ -386,7 +389,8 @@ export const useAuthCommand = ( { type: MessageType.INFO, text: t( - 'Authenticated successfully with Coding Plan. API key is stored in settings.env.', + 'Authenticated successfully with {{region}}. API key and model configs saved to settings.json (backed up).', + { region: t('Alibaba Cloud Coding Plan') }, ), }, Date.now(), diff --git a/packages/cli/src/ui/commands/compressCommand.ts b/packages/cli/src/ui/commands/compressCommand.ts index 8818c42b7..cdd07d45d 100644 --- a/packages/cli/src/ui/commands/compressCommand.ts +++ b/packages/cli/src/ui/commands/compressCommand.ts @@ -20,6 +20,7 @@ export const compressCommand: SlashCommand = { action: async (context) => { const { ui } = context; const executionMode = context.executionMode ?? 'interactive'; + const abortSignal = context.abortSignal; if (executionMode === 'interactive' && ui.pendingItem) { ui.addItem( @@ -96,6 +97,10 @@ export const compressCommand: SlashCommand = { const compressed = await doCompress(); + if (abortSignal?.aborted) { + return; + } + if (!compressed) { if (executionMode === 'interactive') { ui.addItem( @@ -137,6 +142,10 @@ export const compressCommand: SlashCommand = { content: `Context compressed (${compressed.originalTokenCount} -> ${compressed.newTokenCount}).`, }; } catch (e) { + // If cancelled via ESC, don't show error — cancelSlashCommand already handled UI + if (abortSignal?.aborted) { + return; + } if (executionMode === 'interactive') { ui.addItem( { diff --git a/packages/cli/src/ui/commands/insightCommand.ts b/packages/cli/src/ui/commands/insightCommand.ts new file mode 100644 index 000000000..1693254bb --- /dev/null +++ b/packages/cli/src/ui/commands/insightCommand.ts @@ -0,0 +1,132 @@ +/** + * @license + * Copyright 2025 Qwen Code + * SPDX-License-Identifier: Apache-2.0 + */ + +import type { CommandContext, SlashCommand } from './types.js'; +import { CommandKind } from './types.js'; +import { MessageType } from '../types.js'; +import type { HistoryItemInsightProgress } from '../types.js'; +import { t } from '../../i18n/index.js'; +import { join } from 'path'; +import os from 'os'; +import { StaticInsightGenerator } from '../../services/insight/generators/StaticInsightGenerator.js'; +import { createDebugLogger } from '@qwen-code/qwen-code-core'; +import open from 'open'; + +const logger = createDebugLogger('DataProcessor'); + +export const insightCommand: SlashCommand = { + name: 'insight', + get description() { + return t( + 'generate personalized programming insights from your chat history', + ); + }, + kind: CommandKind.BUILT_IN, + action: async (context: CommandContext) => { + try { + context.ui.setDebugMessage(t('Generating insights...')); + + const projectsDir = join(os.homedir(), '.qwen', 'projects'); + if (!context.services.config) { + throw new Error('Config service is not available'); + } + const insightGenerator = new StaticInsightGenerator( + context.services.config, + ); + + const updateProgress = ( + stage: string, + progress: number, + detail?: string, + ) => { + const progressItem: HistoryItemInsightProgress = { + type: MessageType.INSIGHT_PROGRESS, + progress: { + stage, + progress, + detail, + }, + }; + context.ui.setPendingItem(progressItem); + }; + + context.ui.addItem( + { + type: MessageType.INFO, + text: t('This may take a couple minutes. Sit tight!'), + }, + Date.now(), + ); + + // Initial progress + updateProgress(t('Starting insight generation...'), 0); + + // Generate the static insight HTML file + const outputPath = await insightGenerator.generateStaticInsight( + projectsDir, + updateProgress, + ); + + // Clear pending item + context.ui.setPendingItem(null); + + context.ui.addItem( + { + type: MessageType.INFO, + text: t('Insight report generated successfully!'), + }, + Date.now(), + ); + + // Open the file in the default browser + try { + await open(outputPath); + + context.ui.addItem( + { + type: MessageType.INFO, + text: t('Opening insights in your browser: {{path}}', { + path: outputPath, + }), + }, + Date.now(), + ); + } catch (browserError) { + logger.error('Failed to open browser automatically:', browserError); + + context.ui.addItem( + { + type: MessageType.INFO, + text: t( + 'Insights generated at: {{path}}. Please open this file in your browser.', + { + path: outputPath, + }, + ), + }, + Date.now(), + ); + } + + context.ui.setDebugMessage(t('Insights ready.')); + } catch (error) { + // Clear pending item on error + context.ui.setPendingItem(null); + + context.ui.addItem( + { + type: MessageType.ERROR, + text: t('Failed to generate insights: {{error}}', { + error: (error as Error).message, + }), + }, + Date.now(), + ); + + logger.error('Insight generation error:', error); + } + }, +}; diff --git a/packages/cli/src/ui/commands/setupGithubCommand.ts b/packages/cli/src/ui/commands/setupGithubCommand.ts index baba59b6c..40bec554f 100644 --- a/packages/cli/src/ui/commands/setupGithubCommand.ts +++ b/packages/cli/src/ui/commands/setupGithubCommand.ts @@ -104,6 +104,15 @@ export const setupGithubCommand: SlashCommand = { ): Promise => { const abortController = new AbortController(); + // If we have a context abort signal (from ESC cancellation), link it to our controller + if (context.abortSignal) { + context.abortSignal.addEventListener( + 'abort', + () => abortController.abort(), + { once: true }, + ); + } + if (!isGitHubRepository()) { throw new Error( 'Unable to determine the GitHub repository. /setup-github must be run from a git repository.', diff --git a/packages/cli/src/ui/commands/summaryCommand.ts b/packages/cli/src/ui/commands/summaryCommand.ts index de75fadd2..5e84bf53e 100644 --- a/packages/cli/src/ui/commands/summaryCommand.ts +++ b/packages/cli/src/ui/commands/summaryCommand.ts @@ -27,6 +27,7 @@ export const summaryCommand: SlashCommand = { const { config } = context.services; const { ui } = context; const executionMode = context.executionMode ?? 'interactive'; + const abortSignal = context.abortSignal; if (!config) { return { @@ -101,7 +102,7 @@ export const summaryCommand: SlashCommand = { }, ], {}, - new AbortController().signal, + abortSignal ?? new AbortController().signal, config.getModel(), ); @@ -197,6 +198,10 @@ export const summaryCommand: SlashCommand = { if (executionMode !== 'interactive') { return; } + // If cancelled via ESC, don't show error — cancelSlashCommand already handled UI + if (abortSignal?.aborted) { + return; + } ui.setPendingItem(null); ui.addItem( { @@ -241,6 +246,9 @@ export const summaryCommand: SlashCommand = { }> => { emitInteractivePending('generating'); const markdownSummary = await generateSummaryMarkdown(history); + if (abortSignal?.aborted) { + throw new DOMException('Summary generation cancelled.', 'AbortError'); + } emitInteractivePending('saving'); const { filePathForDisplay } = await saveSummaryToDisk(markdownSummary); completeInteractive(filePathForDisplay); diff --git a/packages/cli/src/ui/commands/types.ts b/packages/cli/src/ui/commands/types.ts index 6c03ec136..90330e988 100644 --- a/packages/cli/src/ui/commands/types.ts +++ b/packages/cli/src/ui/commands/types.ts @@ -89,6 +89,8 @@ export interface CommandContext { }; // Flag to indicate if an overwrite has been confirmed overwriteConfirmed?: boolean; + /** Abort signal for cancelling long-running slash command operations via ESC. */ + abortSignal?: AbortSignal; } /** diff --git a/packages/cli/src/ui/components/ApiKeyInput.tsx b/packages/cli/src/ui/components/ApiKeyInput.tsx index f741e3fab..bf885b30d 100644 --- a/packages/cli/src/ui/components/ApiKeyInput.tsx +++ b/packages/cli/src/ui/components/ApiKeyInput.tsx @@ -8,26 +8,37 @@ import type React from 'react'; import { useState } from 'react'; import { Box, Text } from 'ink'; import { TextInput } from './shared/TextInput.js'; -import { Colors } from '../colors.js'; +import { theme } from '../semantic-colors.js'; import { useKeypress } from '../hooks/useKeypress.js'; import { t } from '../../i18n/index.js'; +import { CodingPlanRegion } from '../../constants/codingPlan.js'; import Link from 'ink-link'; interface ApiKeyInputProps { onSubmit: (apiKey: string) => void; onCancel: () => void; + region?: CodingPlanRegion; } const CODING_PLAN_API_KEY_URL = 'https://bailian.console.aliyun.com/?tab=model#/efm/coding_plan'; +const CODING_PLAN_INTL_API_KEY_URL = + 'https://modelstudio.console.alibabacloud.com/?tab=dashboard#/efm/coding_plan'; + export function ApiKeyInput({ onSubmit, onCancel, + region = CodingPlanRegion.CHINA, }: ApiKeyInputProps): React.JSX.Element { const [apiKey, setApiKey] = useState(''); const [error, setError] = useState(null); + const apiKeyUrl = + region === CodingPlanRegion.GLOBAL + ? CODING_PLAN_INTL_API_KEY_URL + : CODING_PLAN_API_KEY_URL; + useKeypress( (key) => { if (key.name === 'escape') { @@ -38,6 +49,18 @@ export function ApiKeyInput({ setError(t('API key cannot be empty.')); return; } + // Only validate sk-sp- prefix for China region (aliyun.com) + if ( + region === CodingPlanRegion.CHINA && + !trimmedKey.startsWith('sk-sp-') + ) { + setError( + t( + 'Invalid API key. Coding Plan API keys start with "sk-sp-". Please check.', + ), + ); + return; + } onSubmit(trimmedKey); } }, @@ -46,28 +69,25 @@ export function ApiKeyInput({ return ( - - {t('Please enter your API key:')} - {error && ( - {error} + {error} )} - {t('You can get your exclusive Coding Plan API-KEY here:')} + {t('You can get your Coding Plan API key here')} - - - {CODING_PLAN_API_KEY_URL} + + + {apiKeyUrl} - - {t('(Press Enter to submit, Escape to cancel)')} + + {t('Enter to submit, Esc to go back')} diff --git a/packages/cli/src/ui/components/AppHeader.tsx b/packages/cli/src/ui/components/AppHeader.tsx index ba044d10d..0254a2012 100644 --- a/packages/cli/src/ui/components/AppHeader.tsx +++ b/packages/cli/src/ui/components/AppHeader.tsx @@ -5,16 +5,43 @@ */ import { Box } from 'ink'; -import { Header } from './Header.js'; +import { AuthType } from '@qwen-code/qwen-code-core'; +import { Header, AuthDisplayType } from './Header.js'; import { Tips } from './Tips.js'; import { useSettings } from '../contexts/SettingsContext.js'; import { useConfig } from '../contexts/ConfigContext.js'; import { useUIState } from '../contexts/UIStateContext.js'; +import { isCodingPlanConfig } from '../../constants/codingPlan.js'; interface AppHeaderProps { version: string; } +/** + * Determine the auth display type based on auth type and configuration. + */ +function getAuthDisplayType( + authType?: AuthType, + baseUrl?: string, + apiKeyEnvKey?: string, +): AuthDisplayType { + if (!authType) { + return AuthDisplayType.UNKNOWN; + } + + // Check if it's a Coding Plan config + if (isCodingPlanConfig(baseUrl, apiKeyEnvKey)) { + return AuthDisplayType.CODING_PLAN; + } + + switch (authType) { + case AuthType.QWEN_OAUTH: + return AuthDisplayType.QWEN_OAUTH; + default: + return AuthDisplayType.API_KEY; + } +} + export const AppHeader = ({ version }: AppHeaderProps) => { const settings = useSettings(); const config = useConfig(); @@ -27,12 +54,18 @@ export const AppHeader = ({ version }: AppHeaderProps) => { const showBanner = !config.getScreenReader(); const showTips = !(settings.merged.ui?.hideTips || config.getScreenReader()); + const authDisplayType = getAuthDisplayType( + authType, + contentGeneratorConfig?.baseUrl, + contentGeneratorConfig?.apiKeyEnvKey, + ); + return ( {showBanner && (
diff --git a/packages/cli/src/ui/components/Composer.test.tsx b/packages/cli/src/ui/components/Composer.test.tsx index 1db02d6f9..67d992dbe 100644 --- a/packages/cli/src/ui/components/Composer.test.tsx +++ b/packages/cli/src/ui/components/Composer.test.tsx @@ -117,6 +117,7 @@ const createMockUIState = (overrides: Partial = {}): UIState => const createMockUIActions = (): UIActions => ({ handleFinalSubmit: vi.fn(), + handleRetryLastPrompt: vi.fn(), handleClearScreen: vi.fn(), setShellModeActive: vi.fn(), onEscapePromptChange: vi.fn(), diff --git a/packages/cli/src/ui/components/DialogManager.tsx b/packages/cli/src/ui/components/DialogManager.tsx index dbb6f2207..c79e91119 100644 --- a/packages/cli/src/ui/components/DialogManager.tsx +++ b/packages/cli/src/ui/components/DialogManager.tsx @@ -32,7 +32,6 @@ import process from 'node:process'; import { type UseHistoryManagerReturn } from '../hooks/useHistoryManager.js'; import { IdeTrustChangeDialog } from './IdeTrustChangeDialog.js'; import { WelcomeBackDialog } from './WelcomeBackDialog.js'; -import { ModelSwitchDialog } from './ModelSwitchDialog.js'; import { AgentCreationWizard } from './subagents/create/AgentCreationWizard.js'; import { AgentsManagerDialog } from './subagents/manage/AgentsManagerDialog.js'; import { SessionPicker } from './SessionPicker.js'; @@ -236,10 +235,6 @@ export const DialogManager = ({ if (uiState.isModelDialogOpen) { return ; } - if (uiState.isVisionSwitchDialogOpen) { - return ; - } - if (uiState.isAuthDialogOpen || uiState.authError) { return ( diff --git a/packages/cli/src/ui/components/Header.test.tsx b/packages/cli/src/ui/components/Header.test.tsx index 1d3a4d7f1..99bb053da 100644 --- a/packages/cli/src/ui/components/Header.test.tsx +++ b/packages/cli/src/ui/components/Header.test.tsx @@ -6,8 +6,7 @@ import { render } from 'ink-testing-library'; import { describe, it, expect, vi, beforeEach } from 'vitest'; -import { AuthType } from '@qwen-code/qwen-code-core'; -import { Header } from './Header.js'; +import { Header, AuthDisplayType } from './Header.js'; import * as useTerminalSize from '../hooks/useTerminalSize.js'; vi.mock('../hooks/useTerminalSize.js'); @@ -15,86 +14,70 @@ const useTerminalSizeMock = vi.mocked(useTerminalSize.useTerminalSize); const defaultProps = { version: '1.0.0', - authType: AuthType.QWEN_OAUTH, + authDisplayType: AuthDisplayType.QWEN_OAUTH, model: 'qwen-coder-plus', workingDirectory: '/home/user/projects/test', }; describe('
', () => { beforeEach(() => { - // Default to wide terminal (shows both logo and info panel) useTerminalSizeMock.mockReturnValue({ columns: 120, rows: 24 }); }); it('renders the ASCII logo on wide terminal', () => { const { lastFrame } = render(
); - // Check that parts of the shortAsciiLogo are rendered expect(lastFrame()).toContain('██╔═══██╗'); }); it('hides the ASCII logo on narrow terminal', () => { useTerminalSizeMock.mockReturnValue({ columns: 60, rows: 24 }); const { lastFrame } = render(
); - // Should not contain the logo but still show the info panel expect(lastFrame()).not.toContain('██╔═══██╗'); expect(lastFrame()).toContain('>_ Qwen Code'); }); - it('renders custom ASCII art when provided on wide terminal', () => { - const customArt = 'CUSTOM ART'; - const { lastFrame } = render( -
, - ); - expect(lastFrame()).toContain(customArt); - }); - it('displays the version number', () => { const { lastFrame } = render(
); expect(lastFrame()).toContain('v1.0.0'); }); - it('displays Qwen Code title with >_ prefix', () => { - const { lastFrame } = render(
); - expect(lastFrame()).toContain('>_ Qwen Code'); - }); - it('displays auth type and model', () => { const { lastFrame } = render(
); expect(lastFrame()).toContain('Qwen OAuth'); expect(lastFrame()).toContain('qwen-coder-plus'); }); + it('displays Coding Plan auth type', () => { + const { lastFrame } = render( +
, + ); + expect(lastFrame()).toContain('Coding Plan'); + }); + + it('displays API Key auth type', () => { + const { lastFrame } = render( +
, + ); + expect(lastFrame()).toContain('API Key'); + }); + + it('displays Unknown when auth type is not set', () => { + const { lastFrame } = render( +
, + ); + expect(lastFrame()).toContain('Unknown'); + }); + it('displays working directory', () => { const { lastFrame } = render(
); expect(lastFrame()).toContain('/home/user/projects/test'); }); - it('renders a custom working directory display', () => { - const { lastFrame } = render( -
, - ); - expect(lastFrame()).toContain('custom display'); - }); - - it('displays working directory without branch name', () => { - const { lastFrame } = render(
); - // Branch name is no longer shown in header - expect(lastFrame()).toContain('/home/user/projects/test'); - expect(lastFrame()).not.toContain('(main*)'); - }); - - it('formats home directory with tilde', () => { - const { lastFrame } = render( -
, - ); - // The actual home dir replacement depends on os.homedir() - // Just verify the path is shown - expect(lastFrame()).toContain('projects'); - }); - it('renders with border around info panel', () => { const { lastFrame } = render(
); - // Check for border characters (round border style uses these) expect(lastFrame()).toContain('╭'); expect(lastFrame()).toContain('╯'); }); diff --git a/packages/cli/src/ui/components/Header.tsx b/packages/cli/src/ui/components/Header.tsx index adbe13071..45fce4385 100644 --- a/packages/cli/src/ui/components/Header.tsx +++ b/packages/cli/src/ui/components/Header.tsx @@ -7,59 +7,35 @@ import type React from 'react'; import { Box, Text } from 'ink'; import Gradient from 'ink-gradient'; -import { AuthType, shortenPath, tildeifyPath } from '@qwen-code/qwen-code-core'; +import { shortenPath, tildeifyPath } from '@qwen-code/qwen-code-core'; import { theme } from '../semantic-colors.js'; import { shortAsciiLogo } from './AsciiArt.js'; import { getAsciiArtWidth, getCachedStringWidth } from '../utils/textUtils.js'; import { useTerminalSize } from '../hooks/useTerminalSize.js'; +/** + * Auth display type for the Header component. + * Simplified representation of authentication method shown to users. + */ +export enum AuthDisplayType { + QWEN_OAUTH = 'Qwen OAuth', + CODING_PLAN = 'Coding Plan', + API_KEY = 'API Key', + UNKNOWN = 'Unknown', +} + interface HeaderProps { customAsciiArt?: string; // For user-defined ASCII art version: string; - authType?: AuthType; + authDisplayType?: AuthDisplayType; model: string; workingDirectory: string; } -function titleizeAuthType(value: string): string { - return value - .split(/[-_]/g) - .filter(Boolean) - .map((part) => { - if (part.toLowerCase() === 'ai') { - return 'AI'; - } - return part.charAt(0).toUpperCase() + part.slice(1); - }) - .join(' '); -} - -// Format auth type for display -function formatAuthType(authType?: AuthType): string { - if (!authType) { - return 'Unknown'; - } - - switch (authType) { - case AuthType.QWEN_OAUTH: - return 'Qwen OAuth'; - case AuthType.USE_OPENAI: - return 'OpenAI'; - case AuthType.USE_GEMINI: - return 'Gemini'; - case AuthType.USE_VERTEX_AI: - return 'Vertex AI'; - case AuthType.USE_ANTHROPIC: - return 'Anthropic'; - default: - return titleizeAuthType(String(authType)); - } -} - export const Header: React.FC = ({ customAsciiArt, version, - authType, + authDisplayType, model, workingDirectory, }) => { @@ -67,7 +43,7 @@ export const Header: React.FC = ({ const displayLogo = customAsciiArt ?? shortAsciiLogo; const logoWidth = getAsciiArtWidth(displayLogo); - const formattedAuthType = formatAuthType(authType); + const formattedAuthType = authDisplayType ?? AuthDisplayType.UNKNOWN; // Calculate available space properly: // First determine if logo can be shown, then use remaining space for path @@ -95,7 +71,7 @@ export const Header: React.FC = ({ ? Math.min(availableTerminalWidth - logoWidth - logoGap, maxInfoPanelWidth) : availableTerminalWidth; - // Calculate max path length (subtract padding/borders from available space) + // Calculate max path lengths (subtract padding/borders from available space) const maxPathLength = Math.max( 0, availableInfoPanelWidth - infoPanelChromeWidth, diff --git a/packages/cli/src/ui/components/HistoryItemDisplay.tsx b/packages/cli/src/ui/components/HistoryItemDisplay.tsx index a4fa9ee7c..3bb6780ca 100644 --- a/packages/cli/src/ui/components/HistoryItemDisplay.tsx +++ b/packages/cli/src/ui/components/HistoryItemDisplay.tsx @@ -20,6 +20,7 @@ import { GeminiThoughtMessageContent } from './messages/GeminiThoughtMessageCont import { CompressionMessage } from './messages/CompressionMessage.js'; import { SummaryMessage } from './messages/SummaryMessage.js'; import { WarningMessage } from './messages/WarningMessage.js'; +import { RetryCountdownMessage } from './messages/RetryCountdownMessage.js'; import { Box } from 'ink'; import { AboutBox } from './AboutBox.js'; import { StatsDisplay } from './StatsDisplay.js'; @@ -33,6 +34,7 @@ import { getMCPServerStatus } from '@qwen-code/qwen-code-core'; import { SkillsList } from './views/SkillsList.js'; import { ToolsList } from './views/ToolsList.js'; import { McpStatus } from './views/McpStatus.js'; +import { InsightProgressMessage } from './messages/InsightProgressMessage.js'; interface HistoryItemDisplayProps { item: HistoryItem; @@ -124,7 +126,10 @@ const HistoryItemDisplayComponent: React.FC = ({ )} {itemForDisplay.type === 'error' && ( - + + )} + {itemForDisplay.type === 'retry_countdown' && ( + )} {itemForDisplay.type === 'about' && ( @@ -176,6 +181,9 @@ const HistoryItemDisplayComponent: React.FC = ({ {itemForDisplay.type === 'mcp_status' && ( )} + {itemForDisplay.type === 'insight_progress' && ( + + )} ); }; diff --git a/packages/cli/src/ui/components/InputPrompt.test.tsx b/packages/cli/src/ui/components/InputPrompt.test.tsx index b8c83a132..61584b8c7 100644 --- a/packages/cli/src/ui/components/InputPrompt.test.tsx +++ b/packages/cli/src/ui/components/InputPrompt.test.tsx @@ -38,6 +38,7 @@ vi.mock('../contexts/UIStateContext.js', () => ({ })); vi.mock('../contexts/UIActionsContext.js', () => ({ useUIActions: vi.fn(() => ({ + handleRetryLastPrompt: vi.fn(), temporaryCloseFeedbackDialog: vi.fn(), })), })); @@ -370,6 +371,8 @@ describe('InputPrompt', () => { }); describe('clipboard image paste', () => { + const isWindows = process.platform === 'win32'; + beforeEach(() => { vi.mocked(clipboardUtils.clipboardHasImage).mockResolvedValue(false); vi.mocked(clipboardUtils.saveClipboardImage).mockResolvedValue(null); @@ -378,10 +381,37 @@ describe('InputPrompt', () => { ); }); - it('should handle Ctrl+V when clipboard has an image', async () => { + // Windows uses Alt+V (\x1Bv), non-Windows uses Ctrl+V (\x16) + const describeConditional = isWindows ? it.skip : it; + describeConditional( + 'should handle Ctrl+V when clipboard has an image', + async () => { + vi.mocked(clipboardUtils.clipboardHasImage).mockResolvedValue(true); + vi.mocked(clipboardUtils.saveClipboardImage).mockResolvedValue( + '/Users/mochi/.qwen/tmp/clipboard-123.png', + ); + + const { stdin, unmount } = renderWithProviders( + , + ); + await wait(); + + // Send Ctrl+V + stdin.write('\x16'); // Ctrl+V + await wait(); + + expect(clipboardUtils.clipboardHasImage).toHaveBeenCalled(); + expect(clipboardUtils.saveClipboardImage).toHaveBeenCalled(); + expect(clipboardUtils.cleanupOldClipboardImages).toHaveBeenCalled(); + // Note: The new implementation adds images as attachments rather than inserting into buffer + unmount(); + }, + ); + + it('should handle Cmd+V when clipboard has an image', async () => { vi.mocked(clipboardUtils.clipboardHasImage).mockResolvedValue(true); vi.mocked(clipboardUtils.saveClipboardImage).mockResolvedValue( - '/test/.qwen-clipboard/clipboard-123.png', + '/Users/mochi/.qwen/tmp/clipboard-456.png', ); const { stdin, unmount } = renderWithProviders( @@ -389,18 +419,15 @@ describe('InputPrompt', () => { ); await wait(); - // Send Ctrl+V - stdin.write('\x16'); // Ctrl+V + // Send Cmd+V (meta key) / Alt+V on Windows + // In terminals, Cmd+V or Alt+V is typically sent as ESC followed by 'v' + stdin.write('\x1Bv'); await wait(); expect(clipboardUtils.clipboardHasImage).toHaveBeenCalled(); - expect(clipboardUtils.saveClipboardImage).toHaveBeenCalledWith( - props.config.getTargetDir(), - ); - expect(clipboardUtils.cleanupOldClipboardImages).toHaveBeenCalledWith( - props.config.getTargetDir(), - ); - expect(mockBuffer.replaceRangeByOffset).toHaveBeenCalled(); + expect(clipboardUtils.saveClipboardImage).toHaveBeenCalled(); + expect(clipboardUtils.cleanupOldClipboardImages).toHaveBeenCalled(); + // Note: The new implementation adds images as attachments rather than inserting into buffer unmount(); }); @@ -412,7 +439,8 @@ describe('InputPrompt', () => { ); await wait(); - stdin.write('\x16'); // Ctrl+V + // Use platform-appropriate key combination + stdin.write(isWindows ? '\x1Bv' : '\x16'); await wait(); expect(clipboardUtils.clipboardHasImage).toHaveBeenCalled(); @@ -430,7 +458,8 @@ describe('InputPrompt', () => { ); await wait(); - stdin.write('\x16'); // Ctrl+V + // Use platform-appropriate key combination + stdin.write(isWindows ? '\x1Bv' : '\x16'); await wait(); expect(clipboardUtils.saveClipboardImage).toHaveBeenCalled(); @@ -439,11 +468,7 @@ describe('InputPrompt', () => { }); it('should insert image path at cursor position with proper spacing', async () => { - const imagePath = path.join( - 'test', - '.qwen-clipboard', - 'clipboard-456.png', - ); + const imagePath = '/Users/mochi/.qwen/tmp/clipboard-456.png'; vi.mocked(clipboardUtils.clipboardHasImage).mockResolvedValue(true); vi.mocked(clipboardUtils.saveClipboardImage).mockResolvedValue(imagePath); @@ -451,27 +476,20 @@ describe('InputPrompt', () => { mockBuffer.text = 'Hello world'; mockBuffer.cursor = [0, 5]; // Cursor after "Hello" mockBuffer.lines = ['Hello world']; - mockBuffer.replaceRangeByOffset = vi.fn(); const { stdin, unmount } = renderWithProviders( , ); await wait(); - stdin.write('\x16'); // Ctrl+V + // Use platform-appropriate key combination + stdin.write(isWindows ? '\x1Bv' : '\x16'); await wait(); - // Should insert at cursor position with spaces - expect(mockBuffer.replaceRangeByOffset).toHaveBeenCalled(); - - // Get the actual call to see what path was used - const actualCall = vi.mocked(mockBuffer.replaceRangeByOffset).mock - .calls[0]; - expect(actualCall[0]).toBe(5); // start offset - expect(actualCall[1]).toBe(5); // end offset - expect(actualCall[2]).toBe( - ' @' + path.relative(path.join('test', 'project', 'src'), imagePath), - ); + // The new implementation adds images as attachments rather than inserting into buffer + // So we verify that saveClipboardImage was called instead + expect(clipboardUtils.saveClipboardImage).toHaveBeenCalled(); + expect(clipboardUtils.clipboardHasImage).toHaveBeenCalled(); unmount(); }); @@ -485,7 +503,8 @@ describe('InputPrompt', () => { ); await wait(); - stdin.write('\x16'); // Ctrl+V + // Use platform-appropriate key combination + stdin.write(isWindows ? '\x1Bv' : '\x16'); await wait(); // Should not throw and should not set buffer text on error @@ -2418,6 +2437,140 @@ describe('InputPrompt', () => { unmount(); }); }); + + /** + * Ctrl+Y (RETRY_LAST) shortcut tests + * + * The Ctrl+Y shortcut should trigger handleRetryLastPrompt when: + * 1. The user presses Ctrl+Y + * 2. The InputPrompt is focused + * 3. No other modal/dialog is open that would consume the key + * + * This shortcut is handled in InputPrompt.tsx at line 585-588: + * if (keyMatchers[Command.RETRY_LAST](key)) { + * uiActions.handleRetryLastPrompt(); + * return; + * } + */ + describe('Ctrl+Y retry shortcut', () => { + let mockUIActions: { + handleRetryLastPrompt: ReturnType; + temporaryCloseFeedbackDialog: ReturnType; + }; + + beforeEach(() => { + mockUIActions = { + handleRetryLastPrompt: vi.fn(), + temporaryCloseFeedbackDialog: vi.fn(), + }; + + // Override the mock for useUIActions + vi.doMock('../contexts/UIActionsContext.js', () => ({ + useUIActions: vi.fn(() => mockUIActions), + })); + }); + + afterEach(() => { + vi.doUnmock('../contexts/UIActionsContext.js'); + }); + + /** + * Ctrl+Y should trigger handleRetryLastPrompt to retry the last failed request. + * This is the primary activation path for the retry feature. + */ + it('should trigger handleRetryLastPrompt on Ctrl+Y', async () => { + const { stdin, unmount } = renderWithProviders( + , + ); + await wait(); + + // Send Ctrl+Y (ASCII 25) + stdin.write('\x19'); + await wait(); + + // The key matcher should have been triggered + // Note: In the actual implementation, this would call uiActions.handleRetryLastPrompt() + unmount(); + }); + + /** + * The 'y' key alone (without Ctrl) should NOT trigger retry. + * This ensures the shortcut doesn't interfere with normal typing. + */ + it('should NOT trigger retry on plain y key', async () => { + const { stdin, unmount } = renderWithProviders( + , + ); + await wait(); + + // Send plain 'y' + stdin.write('y'); + await wait(); + + // Should insert 'y' into buffer, not trigger retry + expect(mockBuffer.handleInput).toHaveBeenCalledWith( + expect.objectContaining({ + name: 'y', + sequence: 'y', + }), + ); + + unmount(); + }); + + /** + * Ctrl+R should NOT trigger retry - it should trigger reverse search instead. + * This ensures the retry shortcut doesn't conflict with existing shortcuts. + */ + it('should NOT trigger retry on Ctrl+R (reverse search)', async () => { + const { stdin, unmount } = renderWithProviders( + , + ); + await wait(); + + // Send Ctrl+R (ASCII 18) + stdin.write('\x12'); + await wait(); + + // Should activate reverse search, not retry + // Verify the input was handled (not ignored) + expect(mockBuffer.handleInput).not.toHaveBeenCalledWith( + expect.objectContaining({ + ctrl: true, + name: 'y', + }), + ); + + unmount(); + }); + + /** + * When feedback dialog is open, Ctrl+Y should be passed through after + * temporarily closing the dialog. + */ + it('should handle Ctrl+Y when feedback dialog is open', async () => { + // Mock feedback dialog as open + const mockUIState = { isFeedbackDialogOpen: true }; + vi.doMock('../contexts/UIStateContext.js', () => ({ + useUIState: vi.fn(() => mockUIState), + })); + + const { stdin, unmount } = renderWithProviders( + , + ); + await wait(); + + // Send Ctrl+Y + stdin.write('\x19'); + await wait(); + + // Dialog should be temporarily closed + // Note: In actual implementation, temporaryCloseFeedbackDialog would be called + + vi.doUnmock('../contexts/UIStateContext.js'); + unmount(); + }); + }); }); function clean(str: string | undefined): string { if (!str) return ''; diff --git a/packages/cli/src/ui/components/InputPrompt.tsx b/packages/cli/src/ui/components/InputPrompt.tsx index 8820e2126..42ec7efbb 100644 --- a/packages/cli/src/ui/components/InputPrompt.tsx +++ b/packages/cli/src/ui/components/InputPrompt.tsx @@ -22,7 +22,11 @@ import { useKeypress } from '../hooks/useKeypress.js'; import { keyMatchers, Command } from '../keyMatchers.js'; import type { CommandContext, SlashCommand } from '../commands/types.js'; import type { Config } from '@qwen-code/qwen-code-core'; -import { ApprovalMode, createDebugLogger } from '@qwen-code/qwen-code-core'; +import { + ApprovalMode, + Storage, + createDebugLogger, +} from '@qwen-code/qwen-code-core'; import { parseInputForHighlighting, buildSegmentsForVisualSlice, @@ -41,6 +45,15 @@ import { useUIActions } from '../contexts/UIActionsContext.js'; import { useKeypressContext } from '../contexts/KeypressContext.js'; import { FEEDBACK_DIALOG_KEYS } from '../FeedbackDialog.js'; +/** + * Represents an attachment (e.g., pasted image) displayed above the input prompt + */ +export interface Attachment { + id: string; // Unique identifier (timestamp) + path: string; // Full file path + filename: string; // Filename only (for display) +} + const debugLogger = createDebugLogger('INPUT_PROMPT'); export interface InputPromptProps { buffer: TextBuffer; @@ -126,6 +139,10 @@ export const InputPrompt: React.FC = ({ const [recentPasteTime, setRecentPasteTime] = useState(null); const pasteTimeoutRef = useRef(null); + // Attachment state for clipboard images + const [attachments, setAttachments] = useState([]); + const [isAttachmentMode, setIsAttachmentMode] = useState(false); + const [selectedAttachmentIndex, setSelectedAttachmentIndex] = useState(-1); // Large paste placeholder handling const [pendingPastes, setPendingPastes] = useState>( new Map(), @@ -281,10 +298,25 @@ export const InputPrompt: React.FC = ({ if (shellModeActive) { shellHistory.addCommandToHistory(finalValue); } + + // Convert attachments to @references and prepend to the message + if (attachments.length > 0) { + const attachmentRefs = attachments + .map((att) => `@${path.relative(config.getTargetDir(), att.path)}`) + .join(' '); + finalValue = `${attachmentRefs}\n\n${finalValue.trim()}`; + } + // Clear the buffer *before* calling onSubmit to prevent potential re-submission // if onSubmit triggers a re-render while the buffer still holds the old value. buffer.setText(''); onSubmit(finalValue); + + // Clear attachments after submit + setAttachments([]); + setIsAttachmentMode(false); + setSelectedAttachmentIndex(-1); + resetCompletionState(); resetReverseSearchCompletionState(); }, @@ -295,6 +327,8 @@ export const InputPrompt: React.FC = ({ shellModeActive, shellHistory, resetReverseSearchCompletionState, + attachments, + config, pendingPastes, ], ); @@ -336,52 +370,45 @@ export const InputPrompt: React.FC = ({ ]); // Handle clipboard image pasting with Ctrl+V - const handleClipboardImage = useCallback(async () => { + const handleClipboardImage = useCallback(async (validated = false) => { try { - if (await clipboardHasImage()) { - const imagePath = await saveClipboardImage(config.getTargetDir()); + const hasImage = validated || (await clipboardHasImage()); + if (hasImage) { + const imagePath = await saveClipboardImage(Storage.getGlobalTempDir()); if (imagePath) { // Clean up old images - cleanupOldClipboardImages(config.getTargetDir()).catch(() => { + cleanupOldClipboardImages(Storage.getGlobalTempDir()).catch(() => { // Ignore cleanup errors }); - // Get relative path from current directory - const relativePath = path.relative(config.getTargetDir(), imagePath); - - // Insert @path reference at cursor position - const insertText = `@${relativePath}`; - const currentText = buffer.text; - const [row, col] = buffer.cursor; - - // Calculate offset from row/col - let offset = 0; - for (let i = 0; i < row; i++) { - offset += buffer.lines[i].length + 1; // +1 for newline - } - offset += col; - - // Add spaces around the path if needed - let textToInsert = insertText; - const charBefore = offset > 0 ? currentText[offset - 1] : ''; - const charAfter = - offset < currentText.length ? currentText[offset] : ''; - - if (charBefore && charBefore !== ' ' && charBefore !== '\n') { - textToInsert = ' ' + textToInsert; - } - if (!charAfter || (charAfter !== ' ' && charAfter !== '\n')) { - textToInsert = textToInsert + ' '; - } - - // Insert at cursor position - buffer.replaceRangeByOffset(offset, offset, textToInsert); + // Add as attachment instead of inserting @reference into text + const filename = path.basename(imagePath); + const newAttachment: Attachment = { + id: String(Date.now()), + path: imagePath, + filename, + }; + setAttachments((prev) => [...prev, newAttachment]); } } } catch (error) { debugLogger.error('Error handling clipboard image:', error); } - }, [buffer, config]); + }, []); + + // Handle deletion of an attachment from the list + const handleAttachmentDelete = useCallback((index: number) => { + setAttachments((prev) => { + const newList = prev.filter((_, i) => i !== index); + if (newList.length === 0) { + setIsAttachmentMode(false); + setSelectedAttachmentIndex(-1); + } else { + setSelectedAttachmentIndex(Math.min(index, newList.length - 1)); + } + return newList; + }); + }, []); const handleInput = useCallback( (key: Key) => { @@ -412,7 +439,11 @@ export const InputPrompt: React.FC = ({ const pasted = key.sequence.replace(/\r\n/g, '\n').replace(/\r/g, '\n'); const charCount = [...pasted].length; // Proper Unicode char count const lineCount = pasted.split('\n').length; - if ( + + // Ensure we never accidentally interpret paste as regular input. + if (key.pasteImage) { + handleClipboardImage(true); + } else if ( charCount > LARGE_PASTE_CHAR_THRESHOLD || lineCount > LARGE_PASTE_LINE_THRESHOLD ) { @@ -551,6 +582,16 @@ export const InputPrompt: React.FC = ({ return; } + // Ctrl+Y: Retry the last failed request. + // This shortcut is available when: + // - There is a failed request in the current session + // - The stream is not currently responding or waiting for confirmation + // If no failed request exists, a message will be shown to the user. + if (keyMatchers[Command.RETRY_LAST](key)) { + uiActions.handleRetryLastPrompt(); + return; + } + if (shellModeActive && keyMatchers[Command.REVERSE_SEARCH](key)) { setReverseSearchActive(true); setTextBeforeReverseSearch(buffer.text); @@ -666,6 +707,55 @@ export const InputPrompt: React.FC = ({ } } + // Attachment mode handling - process before history navigation + if (isAttachmentMode && attachments.length > 0) { + if (key.name === 'left') { + setSelectedAttachmentIndex((i) => Math.max(0, i - 1)); + return; + } + if (key.name === 'right') { + setSelectedAttachmentIndex((i) => + Math.min(attachments.length - 1, i + 1), + ); + return; + } + if (keyMatchers[Command.NAVIGATION_DOWN](key)) { + // Exit attachment mode and return to input + setIsAttachmentMode(false); + setSelectedAttachmentIndex(-1); + return; + } + if (key.name === 'backspace' || key.name === 'delete') { + handleAttachmentDelete(selectedAttachmentIndex); + return; + } + if (key.name === 'return' || key.name === 'escape') { + setIsAttachmentMode(false); + setSelectedAttachmentIndex(-1); + return; + } + // For other keys, exit attachment mode and let input handle them + setIsAttachmentMode(false); + setSelectedAttachmentIndex(-1); + // Continue to process the key in input + } + + // Enter attachment mode when pressing up at the first line with attachments + if ( + !isAttachmentMode && + attachments.length > 0 && + !shellModeActive && + !reverseSearchActive && + !commandSearchActive && + buffer.visualCursor[0] === 0 && + buffer.visualScrollRow === 0 && + keyMatchers[Command.NAVIGATION_UP](key) + ) { + setIsAttachmentMode(true); + setSelectedAttachmentIndex(attachments.length - 1); + return; + } + if (!shellModeActive) { if (keyMatchers[Command.REVERSE_SEARCH](key)) { setCommandSearchActive(true); @@ -864,6 +954,10 @@ export const InputPrompt: React.FC = ({ onToggleShortcuts, showShortcuts, uiState, + isAttachmentMode, + attachments, + selectedAttachmentIndex, + handleAttachmentDelete, uiActions, pasteWorkaround, nextLargePastePlaceholder, @@ -921,6 +1015,23 @@ export const InputPrompt: React.FC = ({ return ( <> + {attachments.length > 0 && ( + + {t('Attachments: ')} + {attachments.map((att, idx) => ( + + [{att.filename}]{idx < attachments.length - 1 ? ' ' : ''} + + ))} + + )} = ({ /> )} + {/* Attachment hints - show when there are attachments and no suggestions visible */} + {attachments.length > 0 && !shouldShowSuggestions && ( + + + {isAttachmentMode + ? t('← → select, Delete to remove, ↓ to exit') + : t('↑ to manage attachments')} + + + )} ); }; diff --git a/packages/cli/src/ui/components/KeyboardShortcuts.tsx b/packages/cli/src/ui/components/KeyboardShortcuts.tsx index 9ce49b415..df84d0c27 100644 --- a/packages/cli/src/ui/components/KeyboardShortcuts.tsx +++ b/packages/cli/src/ui/components/KeyboardShortcuts.tsx @@ -18,7 +18,10 @@ interface Shortcut { // Platform-specific key mappings const getNewlineKey = () => process.platform === 'win32' ? 'ctrl+enter' : 'ctrl+j'; -const getPasteKey = () => (process.platform === 'darwin' ? 'cmd+v' : 'ctrl+v'); +const getPasteKey = () => { + if (process.platform === 'win32') return 'alt+v'; + return process.platform === 'darwin' ? 'cmd+v' : 'ctrl+v'; +}; const getExternalEditorKey = () => process.platform === 'darwin' ? 'ctrl+x' : 'ctrl+x'; @@ -36,6 +39,7 @@ const getShortcuts = (): Shortcut[] => [ { key: getNewlineKey(), description: t('for newline') + ' ⏎' }, { key: 'ctrl+l', description: t('to clear screen') }, { key: 'ctrl+r', description: t('to search history') }, + { key: 'ctrl+y', description: t('to retry last request') }, { key: getPasteKey(), description: t('to paste images') }, { key: getExternalEditorKey(), description: t('for external editor') }, ]; @@ -51,11 +55,11 @@ const COLUMN_GAP = 4; const MARGIN_LEFT = 2; const MARGIN_RIGHT = 2; -// Column distribution for different layouts (3+4+4 for 3 cols, 6+5 for 2 cols) +// Column distribution for different layouts (4+4+4 for 3 cols, 6+6 for 2 cols) const COLUMN_SPLITS: Record = { - 3: [3, 4, 4], - 2: [6, 5], - 1: [11], + 3: [4, 4, 4], + 2: [6, 6], + 1: [12], }; export const KeyboardShortcuts: React.FC = () => { diff --git a/packages/cli/src/ui/components/ModelDialog.test.tsx b/packages/cli/src/ui/components/ModelDialog.test.tsx index 3ce25bfa9..dc5cc108a 100644 --- a/packages/cli/src/ui/components/ModelDialog.test.tsx +++ b/packages/cli/src/ui/components/ModelDialog.test.tsx @@ -12,14 +12,10 @@ import { DescriptiveRadioButtonSelect } from './shared/DescriptiveRadioButtonSel import { ConfigContext } from '../contexts/ConfigContext.js'; import { SettingsContext } from '../contexts/SettingsContext.js'; import type { Config } from '@qwen-code/qwen-code-core'; -import { AuthType } from '@qwen-code/qwen-code-core'; +import { AuthType, DEFAULT_QWEN_MODEL } from '@qwen-code/qwen-code-core'; import type { LoadedSettings } from '../../config/settings.js'; import { SettingScope } from '../../config/settings.js'; -import { - AVAILABLE_MODELS_QWEN, - MAINLINE_CODER, - MAINLINE_VLM, -} from '../models/availableModels.js'; +import { getFilteredQwenModels } from '../models/availableModels.js'; vi.mock('../hooks/useKeypress.js', () => ({ useKeypress: vi.fn(), @@ -29,6 +25,19 @@ const mockedUseKeypress = vi.mocked(useKeypress); vi.mock('./shared/DescriptiveRadioButtonSelect.js', () => ({ DescriptiveRadioButtonSelect: vi.fn(() => null), })); + +// Helper to create getAvailableModelsForAuthType mock +const createMockGetAvailableModelsForAuthType = () => + vi.fn((t: AuthType) => { + if (t === AuthType.QWEN_OAUTH) { + return getFilteredQwenModels().map((m) => ({ + id: m.id, + label: m.label, + authType: AuthType.QWEN_OAUTH, + })); + } + return []; + }); const mockedSelect = vi.mocked(DescriptiveRadioButtonSelect); const renderComponent = ( @@ -49,12 +58,12 @@ const renderComponent = ( const mockConfig = { // --- Functions used by ModelDialog --- - getModel: vi.fn(() => MAINLINE_CODER), + getModel: vi.fn(() => DEFAULT_QWEN_MODEL), setModel: vi.fn().mockResolvedValue(undefined), switchModel: vi.fn().mockResolvedValue(undefined), getAuthType: vi.fn(() => 'qwen-oauth'), getAllConfiguredModels: vi.fn(() => - AVAILABLE_MODELS_QWEN.map((m) => ({ + getFilteredQwenModels().map((m) => ({ id: m.id, label: m.label, description: m.description || '', @@ -68,7 +77,7 @@ const renderComponent = ( getDebugMode: vi.fn(() => false), getContentGeneratorConfig: vi.fn(() => ({ authType: AuthType.QWEN_OAUTH, - model: MAINLINE_CODER, + model: DEFAULT_QWEN_MODEL, })), getUseModelRouter: vi.fn(() => false), getProxy: vi.fn(() => undefined), @@ -105,10 +114,9 @@ describe('', () => { cleanup(); }); - it('renders the title and help text', () => { + it('renders the title', () => { const { getByText } = renderComponent(); expect(getByText('Select Model')).toBeDefined(); - expect(getByText('(Press Esc to close)')).toBeDefined(); }); it('passes all model options to DescriptiveRadioButtonSelect', () => { @@ -116,24 +124,34 @@ describe('', () => { expect(mockedSelect).toHaveBeenCalledTimes(1); const props = mockedSelect.mock.calls[0][0]; - expect(props.items).toHaveLength(AVAILABLE_MODELS_QWEN.length); + expect(props.items).toHaveLength(getFilteredQwenModels().length); + // coder-model is the only model and it has vision capability expect(props.items[0].value).toBe( - `${AuthType.QWEN_OAUTH}::${MAINLINE_CODER}`, - ); - expect(props.items[1].value).toBe( - `${AuthType.QWEN_OAUTH}::${MAINLINE_VLM}`, + `${AuthType.QWEN_OAUTH}::${DEFAULT_QWEN_MODEL}`, ); expect(props.showNumbers).toBe(true); }); it('initializes with the model from ConfigContext', () => { - const mockGetModel = vi.fn(() => MAINLINE_VLM); - renderComponent({}, { getModel: mockGetModel }); + const mockGetModel = vi.fn(() => DEFAULT_QWEN_MODEL); + renderComponent( + {}, + { + getModel: mockGetModel, + getAvailableModelsForAuthType: + createMockGetAvailableModelsForAuthType(), + }, + ); expect(mockGetModel).toHaveBeenCalled(); + // Calculate expected index dynamically based on model list + const qwenModels = getFilteredQwenModels(); + const expectedIndex = qwenModels.findIndex( + (m) => m.id === DEFAULT_QWEN_MODEL, + ); expect(mockedSelect).toHaveBeenCalledWith( expect.objectContaining({ - initialIndex: 1, + initialIndex: expectedIndex, }), undefined, ); @@ -151,14 +169,19 @@ describe('', () => { }); it('initializes with default coder model if getModel returns undefined', () => { - const mockGetModel = vi.fn(() => undefined); - // @ts-expect-error This test validates component robustness when getModel - // returns an unexpected undefined value. - renderComponent({}, { getModel: mockGetModel }); + const mockGetModel = vi.fn(() => undefined as unknown as string); + renderComponent( + {}, + { + getModel: mockGetModel, + getAvailableModelsForAuthType: + createMockGetAvailableModelsForAuthType(), + }, + ); expect(mockGetModel).toHaveBeenCalled(); - // When getModel returns undefined, preferredModel falls back to MAINLINE_CODER + // When getModel returns undefined, preferredModel falls back to DEFAULT_QWEN_MODEL // which has index 0, so initialIndex should be 0 expect(mockedSelect).toHaveBeenCalledWith( expect.objectContaining({ @@ -170,22 +193,36 @@ describe('', () => { }); it('calls config.switchModel and onClose when DescriptiveRadioButtonSelect.onSelect is triggered', async () => { - const { props, mockConfig, mockSettings } = renderComponent({}, {}); // Pass empty object for contextValue + const { props, mockConfig, mockSettings } = renderComponent( + {}, + { + getAvailableModelsForAuthType: vi.fn((t: AuthType) => { + if (t === AuthType.QWEN_OAUTH) { + return getFilteredQwenModels().map((m) => ({ + id: m.id, + label: m.label, + authType: AuthType.QWEN_OAUTH, + })); + } + return []; + }), + }, + ); const childOnSelect = mockedSelect.mock.calls[0][0].onSelect; expect(childOnSelect).toBeDefined(); - await childOnSelect(`${AuthType.QWEN_OAUTH}::${MAINLINE_CODER}`); + await childOnSelect(`${AuthType.QWEN_OAUTH}::${DEFAULT_QWEN_MODEL}`); expect(mockConfig?.switchModel).toHaveBeenCalledWith( AuthType.QWEN_OAUTH, - MAINLINE_CODER, + DEFAULT_QWEN_MODEL, undefined, ); expect(mockSettings.setValue).toHaveBeenCalledWith( SettingScope.User, 'model.name', - MAINLINE_CODER, + DEFAULT_QWEN_MODEL, ); expect(mockSettings.setValue).toHaveBeenCalledWith( SettingScope.User, @@ -203,7 +240,7 @@ describe('', () => { return [{ id: 'gpt-4', label: 'GPT-4', authType: t }]; } if (t === AuthType.QWEN_OAUTH) { - return AVAILABLE_MODELS_QWEN.map((m) => ({ + return getFilteredQwenModels().map((m) => ({ id: m.id, label: m.label, authType: AuthType.QWEN_OAUTH, @@ -217,7 +254,7 @@ describe('', () => { getModel: vi.fn(() => 'gpt-4'), getContentGeneratorConfig: vi.fn(() => ({ authType: AuthType.QWEN_OAUTH, - model: MAINLINE_CODER, + model: DEFAULT_QWEN_MODEL, })), // Add switchModel to the mock object (not the type) switchModel, @@ -231,17 +268,17 @@ describe('', () => { ); const childOnSelect = mockedSelect.mock.calls[0][0].onSelect; - await childOnSelect(`${AuthType.QWEN_OAUTH}::${MAINLINE_CODER}`); + await childOnSelect(`${AuthType.QWEN_OAUTH}::${DEFAULT_QWEN_MODEL}`); expect(switchModel).toHaveBeenCalledWith( AuthType.QWEN_OAUTH, - MAINLINE_CODER, + DEFAULT_QWEN_MODEL, { requireCachedCredentials: true }, ); expect(mockSettings.setValue).toHaveBeenCalledWith( SettingScope.User, 'model.name', - MAINLINE_CODER, + DEFAULT_QWEN_MODEL, ); expect(mockSettings.setValue).toHaveBeenCalledWith( SettingScope.User, @@ -251,11 +288,12 @@ describe('', () => { expect(props.onClose).toHaveBeenCalledTimes(1); }); - it('does not pass onHighlight to DescriptiveRadioButtonSelect', () => { + it('passes onHighlight to DescriptiveRadioButtonSelect', () => { renderComponent(); const childOnHighlight = mockedSelect.mock.calls[0][0].onHighlight; - expect(childOnHighlight).toBeUndefined(); + expect(childOnHighlight).toBeDefined(); + expect(typeof childOnHighlight).toBe('function'); }); it('calls onClose prop when "escape" key is pressed', () => { @@ -290,7 +328,7 @@ describe('', () => { }); it('updates initialIndex when config context changes', () => { - const mockGetModel = vi.fn(() => MAINLINE_CODER); + const mockGetModel = vi.fn(() => DEFAULT_QWEN_MODEL); const mockGetAuthType = vi.fn(() => 'qwen-oauth'); const mockSettings = { isTrusted: true, @@ -305,8 +343,10 @@ describe('', () => { { getModel: mockGetModel, getAuthType: mockGetAuthType, + getAvailableModelsForAuthType: + createMockGetAvailableModelsForAuthType(), getAllConfiguredModels: vi.fn(() => - AVAILABLE_MODELS_QWEN.map((m) => ({ + getFilteredQwenModels().map((m) => ({ id: m.id, label: m.label, description: m.description || '', @@ -321,14 +361,16 @@ describe('', () => { , ); + // DEFAULT_QWEN_MODEL (coder-model) is at index 0 expect(mockedSelect.mock.calls[0][0].initialIndex).toBe(0); - mockGetModel.mockReturnValue(MAINLINE_VLM); + mockGetModel.mockReturnValue(DEFAULT_QWEN_MODEL); const newMockConfig = { getModel: mockGetModel, getAuthType: mockGetAuthType, + getAvailableModelsForAuthType: createMockGetAvailableModelsForAuthType(), getAllConfiguredModels: vi.fn(() => - AVAILABLE_MODELS_QWEN.map((m) => ({ + getFilteredQwenModels().map((m) => ({ id: m.id, label: m.label, description: m.description || '', @@ -347,6 +389,11 @@ describe('', () => { // Should be called at least twice: initial render + re-render after context change expect(mockedSelect).toHaveBeenCalledTimes(2); - expect(mockedSelect.mock.calls[1][0].initialIndex).toBe(1); + // Calculate expected index for DEFAULT_QWEN_MODEL dynamically + const qwenModels = getFilteredQwenModels(); + const expectedCoderIndex = qwenModels.findIndex( + (m) => m.id === DEFAULT_QWEN_MODEL, + ); + expect(mockedSelect.mock.calls[1][0].initialIndex).toBe(expectedCoderIndex); }); }); diff --git a/packages/cli/src/ui/components/ModelDialog.tsx b/packages/cli/src/ui/components/ModelDialog.tsx index 8c102890f..09723dcdd 100644 --- a/packages/cli/src/ui/components/ModelDialog.tsx +++ b/packages/cli/src/ui/components/ModelDialog.tsx @@ -11,10 +11,10 @@ import { AuthType, ModelSlashCommandEvent, logModelSlashCommand, + MAINLINE_CODER_MODEL, type AvailableModel as CoreAvailableModel, type ContentGeneratorConfig, - type ContentGeneratorConfigSource, - type ContentGeneratorConfigSources, + type InputModalities, } from '@qwen-code/qwen-code-core'; import { useKeypress } from '../hooks/useKeypress.js'; import { theme } from '../semantic-colors.js'; @@ -22,65 +22,28 @@ import { DescriptiveRadioButtonSelect } from './shared/DescriptiveRadioButtonSel import { ConfigContext } from '../contexts/ConfigContext.js'; import { UIStateContext, type UIState } from '../contexts/UIStateContext.js'; import { useSettings } from '../contexts/SettingsContext.js'; -import { MAINLINE_CODER } from '../models/availableModels.js'; import { getPersistScopeForModelSelection } from '../../config/modelProvidersScope.js'; import { t } from '../../i18n/index.js'; +function formatModalities(modalities?: InputModalities): string { + if (!modalities) return t('text-only'); + const parts: string[] = []; + if (modalities.image) parts.push(t('image')); + if (modalities.pdf) parts.push(t('pdf')); + if (modalities.audio) parts.push(t('audio')); + if (modalities.video) parts.push(t('video')); + if (parts.length === 0) return t('text-only'); + return `${t('text')} · ${parts.join(' · ')}`; +} + interface ModelDialogProps { onClose: () => void; } -function formatSourceBadge( - source: ContentGeneratorConfigSource | undefined, -): string | undefined { - if (!source) return undefined; - - switch (source.kind) { - case 'cli': - return source.detail ? `CLI ${source.detail}` : 'CLI'; - case 'env': - return source.envKey ? `ENV ${source.envKey}` : 'ENV'; - case 'settings': - return source.settingsPath - ? `Settings ${source.settingsPath}` - : 'Settings'; - case 'modelProviders': { - const suffix = - source.authType && source.modelId - ? `${source.authType}:${source.modelId}` - : source.authType - ? `${source.authType}` - : source.modelId - ? `${source.modelId}` - : ''; - return suffix ? `ModelProviders ${suffix}` : 'ModelProviders'; - } - case 'default': - return source.detail ? `Default ${source.detail}` : 'Default'; - case 'computed': - return source.detail ? `Computed ${source.detail}` : 'Computed'; - case 'programmatic': - return source.detail ? `Programmatic ${source.detail}` : 'Programmatic'; - case 'unknown': - default: - return undefined; - } -} - -function readSourcesFromConfig(config: unknown): ContentGeneratorConfigSources { - if (!config) { - return {}; - } - const maybe = config as { - getContentGeneratorConfigSources?: () => ContentGeneratorConfigSources; - }; - return maybe.getContentGeneratorConfigSources?.() ?? {}; -} - function maskApiKey(apiKey: string | undefined): string { - if (!apiKey) return '(not set)'; + if (!apiKey) return `(${t('not set')})`; const trimmed = apiKey.trim(); - if (trimmed.length === 0) return '(not set)'; + if (trimmed.length === 0) return `(${t('not set')})`; if (trimmed.length <= 6) return '***'; const head = trimmed.slice(0, 3); const tail = trimmed.slice(-4); @@ -131,7 +94,7 @@ function handleModelSwitchSuccess({ { type: 'info', text: - `authType: ${effectiveAuthType ?? '(none)'}` + + `authType: ${effectiveAuthType ?? `(${t('none')})`}` + `\n` + `Using ${isRuntime ? 'runtime ' : ''}model: ${effectiveModelId}` + `\n` + @@ -143,35 +106,26 @@ function handleModelSwitchSuccess({ ); } -function ConfigRow({ +function formatContextWindow(size?: number): string { + if (!size) return `(${t('unknown')})`; + return `${size.toLocaleString('en-US')} tokens`; +} + +function DetailRow({ label, value, - badge, }: { label: string; value: React.ReactNode; - badge?: string; }): React.JSX.Element { return ( - - - - {label}: - - - {value} - + + + {label}: + + + {value} - {badge ? ( - - - - - - {badge} - - - ) : null} ); } @@ -183,13 +137,9 @@ export function ModelDialog({ onClose }: ModelDialogProps): React.JSX.Element { // Local error state for displaying errors within the dialog const [errorMessage, setErrorMessage] = useState(null); + const [highlightedValue, setHighlightedValue] = useState(null); const authType = config?.getAuthType(); - const effectiveConfig = - (config?.getContentGeneratorConfig?.() as - | ContentGeneratorConfig - | undefined) ?? undefined; - const sources = readSourcesFromConfig(config); const availableModelEntries = useMemo(() => { const allModels = config ? config.getAllConfiguredModels() : []; @@ -293,7 +243,7 @@ export function ModelDialog({ onClose }: ModelDialogProps): React.JSX.Element { [availableModelEntries], ); - const preferredModelId = config?.getModel() || MAINLINE_CODER; + const preferredModelId = config?.getModel() || MAINLINE_CODER_MODEL; // Check if current model is a runtime model // Runtime snapshot ID is already in $runtime|${authType}|${modelId} format const activeRuntimeSnapshot = config?.getActiveRuntimeModelSnapshot?.(); @@ -319,6 +269,20 @@ export function ModelDialog({ onClose }: ModelDialogProps): React.JSX.Element { return index === -1 ? 0 : index; }, [MODEL_OPTIONS, preferredKey]); + const handleHighlight = useCallback((value: string) => { + setHighlightedValue(value); + }, []); + + const highlightedEntry = useMemo(() => { + const key = highlightedValue ?? preferredKey; + return availableModelEntries.find( + ({ authType: t2, model, isRuntime, snapshotId }) => { + const v = isRuntime && snapshotId ? snapshotId : `${t2}::${model.id}`; + return v === key; + }, + ); + }, [highlightedValue, preferredKey, availableModelEntries]); + const handleSelect = useCallback( async (selected: string) => { setErrorMessage(null); @@ -413,35 +377,6 @@ export function ModelDialog({ onClose }: ModelDialogProps): React.JSX.Element { > {t('Select Model')} - - - {t('Current (effective) configuration')} - - - - - - {authType !== AuthType.QWEN_OAUTH && ( - <> - - - - )} - - - {!hasModels ? ( @@ -465,12 +400,48 @@ export function ModelDialog({ onClose }: ModelDialogProps): React.JSX.Element { )} + {highlightedEntry && ( + + + + + {highlightedEntry.authType !== AuthType.QWEN_OAUTH && ( + <> + + + + )} + + )} + {errorMessage && ( @@ -480,7 +451,9 @@ export function ModelDialog({ onClose }: ModelDialogProps): React.JSX.Element { )} - {t('(Press Esc to close)')} + + {t('Enter to select, ↑↓ to navigate, Esc to close')} + ); diff --git a/packages/cli/src/ui/components/ModelSwitchDialog.test.tsx b/packages/cli/src/ui/components/ModelSwitchDialog.test.tsx deleted file mode 100644 index 63c85f972..000000000 --- a/packages/cli/src/ui/components/ModelSwitchDialog.test.tsx +++ /dev/null @@ -1,184 +0,0 @@ -/** - * @license - * Copyright 2025 Qwen - * SPDX-License-Identifier: Apache-2.0 - */ - -import React from 'react'; -import { render } from 'ink-testing-library'; -import { describe, it, expect, vi, beforeEach } from 'vitest'; -import { ModelSwitchDialog, VisionSwitchOutcome } from './ModelSwitchDialog.js'; - -// Mock the useKeypress hook -const mockUseKeypress = vi.hoisted(() => vi.fn()); -vi.mock('../hooks/useKeypress.js', () => ({ - useKeypress: mockUseKeypress, -})); - -// Mock the RadioButtonSelect component -const mockRadioButtonSelect = vi.hoisted(() => vi.fn()); -vi.mock('./shared/RadioButtonSelect.js', () => ({ - RadioButtonSelect: mockRadioButtonSelect, -})); - -describe('ModelSwitchDialog', () => { - const mockOnSelect = vi.fn(); - - beforeEach(() => { - vi.clearAllMocks(); - - // Mock RadioButtonSelect to return a simple div - mockRadioButtonSelect.mockReturnValue( - React.createElement('div', { 'data-testid': 'radio-select' }), - ); - }); - - it('should setup RadioButtonSelect with correct options', () => { - render(); - - const expectedItems = [ - { - key: 'switch-once', - label: 'Switch for this request only', - value: VisionSwitchOutcome.SwitchOnce, - }, - { - key: 'switch-session', - label: 'Switch session to vision model', - value: VisionSwitchOutcome.SwitchSessionToVL, - }, - { - key: 'continue', - label: 'Continue with current model', - value: VisionSwitchOutcome.ContinueWithCurrentModel, - }, - ]; - - const callArgs = mockRadioButtonSelect.mock.calls[0][0]; - expect(callArgs.items).toEqual(expectedItems); - expect(callArgs.initialIndex).toBe(0); - expect(callArgs.isFocused).toBe(true); - }); - - it('should call onSelect when an option is selected', () => { - render(); - - const callArgs = mockRadioButtonSelect.mock.calls[0][0]; - expect(typeof callArgs.onSelect).toBe('function'); - - // Simulate selection of "Switch for this request only" - const onSelectCallback = mockRadioButtonSelect.mock.calls[0][0].onSelect; - onSelectCallback(VisionSwitchOutcome.SwitchOnce); - - expect(mockOnSelect).toHaveBeenCalledWith(VisionSwitchOutcome.SwitchOnce); - }); - - it('should call onSelect with SwitchSessionToVL when second option is selected', () => { - render(); - - const onSelectCallback = mockRadioButtonSelect.mock.calls[0][0].onSelect; - onSelectCallback(VisionSwitchOutcome.SwitchSessionToVL); - - expect(mockOnSelect).toHaveBeenCalledWith( - VisionSwitchOutcome.SwitchSessionToVL, - ); - }); - - it('should call onSelect with ContinueWithCurrentModel when third option is selected', () => { - render(); - - const onSelectCallback = mockRadioButtonSelect.mock.calls[0][0].onSelect; - onSelectCallback(VisionSwitchOutcome.ContinueWithCurrentModel); - - expect(mockOnSelect).toHaveBeenCalledWith( - VisionSwitchOutcome.ContinueWithCurrentModel, - ); - }); - - it('should setup escape key handler to call onSelect with ContinueWithCurrentModel', () => { - render(); - - expect(mockUseKeypress).toHaveBeenCalledWith(expect.any(Function), { - isActive: true, - }); - - // Simulate escape key press - const keypressHandler = mockUseKeypress.mock.calls[0][0]; - keypressHandler({ name: 'escape' }); - - expect(mockOnSelect).toHaveBeenCalledWith( - VisionSwitchOutcome.ContinueWithCurrentModel, - ); - }); - - it('should not call onSelect for non-escape keys', () => { - render(); - - const keypressHandler = mockUseKeypress.mock.calls[0][0]; - keypressHandler({ name: 'enter' }); - - expect(mockOnSelect).not.toHaveBeenCalled(); - }); - - it('should set initial index to 0 (first option)', () => { - render(); - - const callArgs = mockRadioButtonSelect.mock.calls[0][0]; - expect(callArgs.initialIndex).toBe(0); - }); - - describe('VisionSwitchOutcome enum', () => { - it('should have correct enum values', () => { - expect(VisionSwitchOutcome.SwitchOnce).toBe('once'); - expect(VisionSwitchOutcome.SwitchSessionToVL).toBe('session'); - expect(VisionSwitchOutcome.ContinueWithCurrentModel).toBe('persist'); - }); - }); - - it('should handle multiple onSelect calls correctly', () => { - render(); - - const onSelectCallback = mockRadioButtonSelect.mock.calls[0][0].onSelect; - - // Call multiple times - onSelectCallback(VisionSwitchOutcome.SwitchOnce); - onSelectCallback(VisionSwitchOutcome.SwitchSessionToVL); - onSelectCallback(VisionSwitchOutcome.ContinueWithCurrentModel); - - expect(mockOnSelect).toHaveBeenCalledTimes(3); - expect(mockOnSelect).toHaveBeenNthCalledWith( - 1, - VisionSwitchOutcome.SwitchOnce, - ); - expect(mockOnSelect).toHaveBeenNthCalledWith( - 2, - VisionSwitchOutcome.SwitchSessionToVL, - ); - expect(mockOnSelect).toHaveBeenNthCalledWith( - 3, - VisionSwitchOutcome.ContinueWithCurrentModel, - ); - }); - - it('should pass isFocused prop to RadioButtonSelect', () => { - render(); - - const callArgs = mockRadioButtonSelect.mock.calls[0][0]; - expect(callArgs.isFocused).toBe(true); - }); - - it('should handle escape key multiple times', () => { - render(); - - const keypressHandler = mockUseKeypress.mock.calls[0][0]; - - // Call escape multiple times - keypressHandler({ name: 'escape' }); - keypressHandler({ name: 'escape' }); - - expect(mockOnSelect).toHaveBeenCalledTimes(2); - expect(mockOnSelect).toHaveBeenCalledWith( - VisionSwitchOutcome.ContinueWithCurrentModel, - ); - }); -}); diff --git a/packages/cli/src/ui/components/ModelSwitchDialog.tsx b/packages/cli/src/ui/components/ModelSwitchDialog.tsx deleted file mode 100644 index 97bfc53a3..000000000 --- a/packages/cli/src/ui/components/ModelSwitchDialog.tsx +++ /dev/null @@ -1,92 +0,0 @@ -/** - * @license - * Copyright 2025 Qwen - * SPDX-License-Identifier: Apache-2.0 - */ - -import type React from 'react'; -import { Box, Text } from 'ink'; -import { Colors } from '../colors.js'; -import { - RadioButtonSelect, - type RadioSelectItem, -} from './shared/RadioButtonSelect.js'; -import { useKeypress } from '../hooks/useKeypress.js'; - -export enum VisionSwitchOutcome { - SwitchOnce = 'once', - SwitchSessionToVL = 'session', - ContinueWithCurrentModel = 'persist', -} - -export interface ModelSwitchDialogProps { - onSelect: (outcome: VisionSwitchOutcome) => void; -} - -export const ModelSwitchDialog: React.FC = ({ - onSelect, -}) => { - useKeypress( - (key) => { - if (key.name === 'escape') { - onSelect(VisionSwitchOutcome.ContinueWithCurrentModel); - } - }, - { isActive: true }, - ); - - const options: Array> = [ - { - key: 'switch-once', - label: 'Switch for this request only', - value: VisionSwitchOutcome.SwitchOnce, - }, - { - key: 'switch-session', - label: 'Switch session to vision model', - value: VisionSwitchOutcome.SwitchSessionToVL, - }, - { - key: 'continue', - label: 'Continue with current model', - value: VisionSwitchOutcome.ContinueWithCurrentModel, - }, - ]; - - const handleSelect = (outcome: VisionSwitchOutcome) => { - onSelect(outcome); - }; - - return ( - - - Vision Model Switch Required - - Your message contains an image, but the current model doesn't - support vision. - - How would you like to proceed? - - - - - - - - Press Enter to select, Esc to cancel - - - ); -}; diff --git a/packages/cli/src/ui/components/Tips.test.ts b/packages/cli/src/ui/components/Tips.test.ts new file mode 100644 index 000000000..dd2c25ea9 --- /dev/null +++ b/packages/cli/src/ui/components/Tips.test.ts @@ -0,0 +1,62 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { describe, it, expect, vi } from 'vitest'; +import { selectWeightedTip } from './Tips.js'; + +describe('selectWeightedTip', () => { + const tips = [ + { text: 'tip-a', weight: 1 }, + { text: 'tip-b', weight: 3 }, + { text: 'tip-c', weight: 1 }, + ]; + + it('returns a valid tip text', () => { + const result = selectWeightedTip(tips); + expect(['tip-a', 'tip-b', 'tip-c']).toContain(result); + }); + + it('selects the first tip when random is near zero', () => { + vi.spyOn(Math, 'random').mockReturnValue(0); + expect(selectWeightedTip(tips)).toBe('tip-a'); + vi.restoreAllMocks(); + }); + + it('selects the weighted tip when random falls in its range', () => { + // Total weight = 5. tip-a covers [0,1), tip-b covers [1,4), tip-c covers [4,5) + // Math.random() * 5 = 2.0 falls in tip-b's range + vi.spyOn(Math, 'random').mockReturnValue(0.4); // 0.4 * 5 = 2.0 + expect(selectWeightedTip(tips)).toBe('tip-b'); + vi.restoreAllMocks(); + }); + + it('selects the last tip when random is near max', () => { + vi.spyOn(Math, 'random').mockReturnValue(0.99); + expect(selectWeightedTip(tips)).toBe('tip-c'); + vi.restoreAllMocks(); + }); + + it('respects weight distribution over many samples', () => { + const counts: Record = { + 'tip-a': 0, + 'tip-b': 0, + 'tip-c': 0, + }; + const iterations = 10000; + for (let i = 0; i < iterations; i++) { + const result = selectWeightedTip(tips); + counts[result]!++; + } + // tip-b (weight 3) should appear roughly 3x as often as tip-a or tip-c (weight 1) + // With 10k iterations, we expect: tip-a ~2000, tip-b ~6000, tip-c ~2000 + expect(counts['tip-b']!).toBeGreaterThan(counts['tip-a']! * 2); + expect(counts['tip-b']!).toBeGreaterThan(counts['tip-c']! * 2); + }); + + it('handles single tip', () => { + expect(selectWeightedTip([{ text: 'only', weight: 1 }])).toBe('only'); + }); +}); diff --git a/packages/cli/src/ui/components/Tips.tsx b/packages/cli/src/ui/components/Tips.tsx index d1b6a71bf..f85184a19 100644 --- a/packages/cli/src/ui/components/Tips.tsx +++ b/packages/cli/src/ui/components/Tips.tsx @@ -9,7 +9,9 @@ import { Box, Text } from 'ink'; import { theme } from '../semantic-colors.js'; import { t } from '../../i18n/index.js'; -const startupTips = [ +type Tip = string | { text: string; weight: number }; + +const startupTips: Tip[] = [ 'Use /compress when the conversation gets long to summarize history and free up context.', 'Start a fresh idea with /clear or /new; the previous session stays available in history.', 'Use /bug to submit issues to the maintainers when something goes off.', @@ -20,13 +22,34 @@ const startupTips = [ process.platform === 'win32' ? 'You can switch permission mode quickly with Tab or /approval-mode.' : 'You can switch permission mode quickly with Shift+Tab or /approval-mode.', -] as const; + { + text: 'Try /insight to generate personalized insights from your chat history.', + weight: 3, + }, +]; + +function tipText(tip: Tip): string { + return typeof tip === 'string' ? tip : tip.text; +} + +function tipWeight(tip: Tip): number { + return typeof tip === 'string' ? 1 : tip.weight; +} + +export function selectWeightedTip(tips: Tip[]): string { + const totalWeight = tips.reduce((sum, tip) => sum + tipWeight(tip), 0); + let random = Math.random() * totalWeight; + for (const tip of tips) { + random -= tipWeight(tip); + if (random <= 0) { + return tipText(tip); + } + } + return tipText(tips[tips.length - 1]!); +} export const Tips: React.FC = () => { - const selectedTip = useMemo(() => { - const randomIndex = Math.floor(Math.random() * startupTips.length); - return startupTips[randomIndex]; - }, []); + const selectedTip = useMemo(() => selectWeightedTip(startupTips), []); return ( diff --git a/packages/cli/src/ui/components/messages/ErrorMessage.tsx b/packages/cli/src/ui/components/messages/ErrorMessage.tsx index 8e10a4fed..14cb8a91f 100644 --- a/packages/cli/src/ui/components/messages/ErrorMessage.tsx +++ b/packages/cli/src/ui/components/messages/ErrorMessage.tsx @@ -10,9 +10,17 @@ import { theme } from '../../semantic-colors.js'; interface ErrorMessageProps { text: string; + /** Optional inline hint displayed after the error text in secondary/dimmed color */ + hint?: string; } -export const ErrorMessage: React.FC = ({ text }) => { +/** + * Renders an error message with a "✕" prefix. + * When a hint is provided (e.g., retry countdown), it is displayed inline + * in parentheses with a dimmed secondary color, similar to the ESC hint + * style used in LoadingIndicator. + */ +export const ErrorMessage: React.FC = ({ text, hint }) => { const prefix = '✕ '; const prefixWidth = prefix.length; @@ -21,10 +29,9 @@ export const ErrorMessage: React.FC = ({ text }) => { {prefix} - - - {text} - + + {text} + {hint && ({hint})} ); diff --git a/packages/cli/src/ui/components/messages/InfoMessage.tsx b/packages/cli/src/ui/components/messages/InfoMessage.tsx index fb03fbef1..af036237a 100644 --- a/packages/cli/src/ui/components/messages/InfoMessage.tsx +++ b/packages/cli/src/ui/components/messages/InfoMessage.tsx @@ -29,7 +29,7 @@ export const InfoMessage: React.FC = ({ text }) => { - + diff --git a/packages/cli/src/ui/components/messages/InsightProgressMessage.tsx b/packages/cli/src/ui/components/messages/InsightProgressMessage.tsx new file mode 100644 index 000000000..4115b3899 --- /dev/null +++ b/packages/cli/src/ui/components/messages/InsightProgressMessage.tsx @@ -0,0 +1,59 @@ +/** + * @license + * Copyright 2025 Qwen Code + * SPDX-License-Identifier: Apache-2.0 + */ + +import type React from 'react'; +import { Box, Text } from 'ink'; +import { theme } from '../../semantic-colors.js'; +import type { InsightProgressProps } from '../../types.js'; +import Spinner from 'ink-spinner'; + +interface InsightProgressMessageProps { + progress: InsightProgressProps; +} + +export const InsightProgressMessage: React.FC = ({ + progress, +}) => { + const { stage, progress: percent, isComplete, error } = progress; + const width = 30; + const completedWidth = Math.round((percent / 100) * width); + const remainingWidth = width - completedWidth; + + const bar = + '█'.repeat(Math.max(0, completedWidth)) + + '░'.repeat(Math.max(0, remainingWidth)); + + if (error) { + return ( + + ✕ {stage} + {error} + + ); + } + + if (isComplete) { + return ( + + ✓ {stage} + + ); + } + + return ( + + + + + + {bar} + + {stage} + {progress.detail ? ` (${progress.detail})` : ''} + + + ); +}; diff --git a/packages/cli/src/ui/components/messages/RetryCountdownMessage.tsx b/packages/cli/src/ui/components/messages/RetryCountdownMessage.tsx new file mode 100644 index 000000000..0f4727574 --- /dev/null +++ b/packages/cli/src/ui/components/messages/RetryCountdownMessage.tsx @@ -0,0 +1,41 @@ +/** + * @license + * Copyright 2025 Qwen + * SPDX-License-Identifier: Apache-2.0 + */ + +import type React from 'react'; +import { Text, Box } from 'ink'; +import { theme } from '../../semantic-colors.js'; + +interface RetryCountdownMessageProps { + text: string; +} + +/** + * Displays a retry countdown message in a dimmed/secondary style + * to visually distinguish it from error messages. + */ +export const RetryCountdownMessage: React.FC = ({ + text, +}) => { + if (!text || text.trim() === '') { + return null; + } + + const prefix = '↻ '; + const prefixWidth = prefix.length; + + return ( + + + {prefix} + + + + {text} + + + + ); +}; diff --git a/packages/cli/src/ui/components/messages/ToolConfirmationMessage.tsx b/packages/cli/src/ui/components/messages/ToolConfirmationMessage.tsx index 7bfe9a962..b285b0a35 100644 --- a/packages/cli/src/ui/components/messages/ToolConfirmationMessage.tsx +++ b/packages/cli/src/ui/components/messages/ToolConfirmationMessage.tsx @@ -330,7 +330,7 @@ export const ToolConfirmationMessage: React.FC< bodyContent = ( - + {displayUrls && infoProps.urls && infoProps.urls.length > 0 && ( diff --git a/packages/cli/src/ui/components/messages/WarningMessage.tsx b/packages/cli/src/ui/components/messages/WarningMessage.tsx index 4bc2c899c..589ca4b07 100644 --- a/packages/cli/src/ui/components/messages/WarningMessage.tsx +++ b/packages/cli/src/ui/components/messages/WarningMessage.tsx @@ -8,6 +8,7 @@ import type React from 'react'; import { Box, Text } from 'ink'; import { Colors } from '../../colors.js'; import { RenderInline } from '../../utils/InlineMarkdownRenderer.js'; +import { theme } from '../../semantic-colors.js'; interface WarningMessageProps { text: string; @@ -24,7 +25,7 @@ export const WarningMessage: React.FC = ({ text }) => { - + diff --git a/packages/cli/src/ui/components/shared/BaseSelectionList.tsx b/packages/cli/src/ui/components/shared/BaseSelectionList.tsx index 8071582f7..15664ef95 100644 --- a/packages/cli/src/ui/components/shared/BaseSelectionList.tsx +++ b/packages/cli/src/ui/components/shared/BaseSelectionList.tsx @@ -30,6 +30,8 @@ export interface BaseSelectionListProps< showNumbers?: boolean; showScrollArrows?: boolean; maxItemsToShow?: number; + /** Gap (in rows) between each item. */ + itemGap?: number; renderItem: (item: TItem, context: RenderItemContext) => React.ReactNode; } @@ -59,6 +61,7 @@ export function BaseSelectionList< showNumbers = true, showScrollArrows = false, maxItemsToShow = 10, + itemGap = 0, renderItem, }: BaseSelectionListProps): React.JSX.Element { const { activeIndex } = useSelectionList({ @@ -89,7 +92,7 @@ export function BaseSelectionList< const numberColumnWidth = String(items.length).length; return ( - + {/* Use conditional coloring instead of conditional rendering */} {showScrollArrows && ( extends SelectionListItem { title: React.ReactNode; - description: string; + description: React.ReactNode; } export interface DescriptiveRadioButtonSelectProps { @@ -32,6 +32,8 @@ export interface DescriptiveRadioButtonSelectProps { showScrollArrows?: boolean; /** The maximum number of items to show at once. */ maxItemsToShow?: number; + /** Gap (in rows) between each item. */ + itemGap?: number; } /** @@ -48,6 +50,7 @@ export function DescriptiveRadioButtonSelect({ showNumbers = false, showScrollArrows = false, maxItemsToShow = 10, + itemGap = 0, }: DescriptiveRadioButtonSelectProps): React.JSX.Element { return ( > @@ -59,6 +62,7 @@ export function DescriptiveRadioButtonSelect({ showNumbers={showNumbers} showScrollArrows={showScrollArrows} maxItemsToShow={maxItemsToShow} + itemGap={itemGap} renderItem={(item, { titleColor }) => ( {item.title} diff --git a/packages/cli/src/ui/contexts/KeypressContext.tsx b/packages/cli/src/ui/contexts/KeypressContext.tsx index 13e51ece3..c4e192609 100644 --- a/packages/cli/src/ui/contexts/KeypressContext.tsx +++ b/packages/cli/src/ui/contexts/KeypressContext.tsx @@ -36,6 +36,7 @@ import { MODIFIER_ALT_BIT, MODIFIER_CTRL_BIT, } from '../utils/platformConstants.js'; +import { clipboardHasImage } from '../utils/clipboardUtils.js'; import { FOCUS_IN, FOCUS_OUT } from '../hooks/useFocus.js'; @@ -54,6 +55,7 @@ export interface Key { paste: boolean; sequence: string; kittyProtocol?: boolean; + pasteImage?: boolean; } export type KeypressHandler = (key: Key) => void; @@ -390,7 +392,7 @@ export function KeypressProvider({ } }; - const handleKeypress = (_: unknown, key: Key) => { + const handleKeypress = async (_: unknown, key: Key) => { if (key.sequence === FOCUS_IN || key.sequence === FOCUS_OUT) { return; } @@ -400,14 +402,28 @@ export function KeypressProvider({ } if (key.name === 'paste-end') { isPaste = false; - broadcast({ - name: '', - ctrl: false, - meta: false, - shift: false, - paste: true, - sequence: pasteBuffer.toString(), - }); + if (pasteBuffer.toString().length > 0) { + broadcast({ + name: '', + ctrl: false, + meta: false, + shift: false, + paste: true, + sequence: pasteBuffer.toString(), + }); + } else { + const hasImage = await clipboardHasImage(); + broadcast({ + name: '', + ctrl: false, + meta: false, + shift: false, + paste: true, + pasteImage: hasImage, + sequence: pasteBuffer.toString(), + }); + } + pasteBuffer = Buffer.alloc(0); return; } @@ -722,6 +738,7 @@ export function KeypressProvider({ }; let rl: readline.Interface; + if (usePassthrough) { rl = readline.createInterface({ input: keypressStream, diff --git a/packages/cli/src/ui/contexts/UIActionsContext.tsx b/packages/cli/src/ui/contexts/UIActionsContext.tsx index e4cb85003..af15e72b6 100644 --- a/packages/cli/src/ui/contexts/UIActionsContext.tsx +++ b/packages/cli/src/ui/contexts/UIActionsContext.tsx @@ -15,8 +15,8 @@ import { type ApprovalMode, } from '@qwen-code/qwen-code-core'; import { type SettingScope } from '../../config/settings.js'; +import { type CodingPlanRegion } from '../../constants/codingPlan.js'; import type { AuthState } from '../types.js'; -import { type VisionSwitchOutcome } from '../components/ModelSwitchDialog.js'; // OpenAICredentials type (previously imported from OpenAIKeyPrompt) export interface OpenAICredentials { apiKey: string; @@ -40,7 +40,10 @@ export interface UIActions { authType: AuthType | undefined, credentials?: OpenAICredentials, ) => Promise; - handleCodingPlanSubmit: (apiKey: string) => Promise; + handleCodingPlanSubmit: ( + apiKey: string, + region?: CodingPlanRegion, + ) => Promise; setAuthState: (state: AuthState) => void; onAuthError: (error: string | null) => void; cancelAuthentication: () => void; @@ -63,9 +66,8 @@ export interface UIActions { onSuggestionsVisibilityChange: (visible: boolean) => void; refreshStatic: () => void; handleFinalSubmit: (value: string) => void; + handleRetryLastPrompt: () => void; handleClearScreen: () => void; - // Vision switch dialog - handleVisionSwitchSelect: (outcome: VisionSwitchOutcome) => void; // Welcome back dialog handleWelcomeBackSelection: (choice: 'continue' | 'restart') => void; handleWelcomeBackClose: () => void; diff --git a/packages/cli/src/ui/contexts/UIStateContext.tsx b/packages/cli/src/ui/contexts/UIStateContext.tsx index f8d52faa1..9d1a21e83 100644 --- a/packages/cli/src/ui/contexts/UIStateContext.tsx +++ b/packages/cli/src/ui/contexts/UIStateContext.tsx @@ -115,8 +115,6 @@ export interface UIState { extensionsUpdateState: Map; activePtyId: number | undefined; embeddedShellFocused: boolean; - // Vision switch dialog - isVisionSwitchDialogOpen: boolean; // Welcome back dialog showWelcomeBackDialog: boolean; welcomeBackInfo: { diff --git a/packages/cli/src/ui/hooks/atCommandProcessor.ts b/packages/cli/src/ui/hooks/atCommandProcessor.ts index f3231479b..31c8092eb 100644 --- a/packages/cli/src/ui/hooks/atCommandProcessor.ts +++ b/packages/cli/src/ui/hooks/atCommandProcessor.ts @@ -11,6 +11,7 @@ import type { Config } from '@qwen-code/qwen-code-core'; import { getErrorMessage, isNodeError, + Storage, unescapePath, readManyFiles, } from '@qwen-code/qwen-code-core'; @@ -181,7 +182,17 @@ export async function handleAtCommand({ // Check if path should be ignored based on filtering options const workspaceContext = config.getWorkspaceContext(); - if (!workspaceContext.isPathWithinWorkspace(pathName)) { + + // Check if path is in project temp directory + const projectTempDir = Storage.getGlobalTempDir(); + const absolutePathName = path.isAbsolute(pathName) + ? pathName + : path.resolve(workspaceContext.getDirectories()[0] || '', pathName); + + if ( + !absolutePathName.startsWith(projectTempDir) && + !workspaceContext.isPathWithinWorkspace(pathName) + ) { onDebugMessage( `Path ${pathName} is not in the workspace and will be skipped.`, ); diff --git a/packages/cli/src/ui/hooks/slashCommandProcessor.test.ts b/packages/cli/src/ui/hooks/slashCommandProcessor.test.ts index 42ce40993..c48653970 100644 --- a/packages/cli/src/ui/hooks/slashCommandProcessor.test.ts +++ b/packages/cli/src/ui/hooks/slashCommandProcessor.test.ts @@ -88,6 +88,10 @@ vi.mock('../../utils/cleanup.js', () => ({ runExitCleanup: mockRunExitCleanup, })); +vi.mock('./useKeypress.js', () => ({ + useKeypress: vi.fn(), +})); + function createTestCommand( overrides: Partial, kind: CommandKind = CommandKind.BUILT_IN, @@ -143,6 +147,7 @@ describe('useSlashCommandProcessor', () => { mockLoadHistory, vi.fn(), // refreshStatic vi.fn(), // toggleVimEnabled + false, // isProcessing setIsProcessing, vi.fn(), // setGeminiMdFileCount { @@ -151,9 +156,19 @@ describe('useSlashCommandProcessor', () => { openEditorDialog: vi.fn(), openSettingsDialog: vi.fn(), openModelDialog: mockOpenModelDialog, + openPermissionsDialog: vi.fn(), + openApprovalModeDialog: vi.fn(), + openResumeDialog: vi.fn(), quit: mockSetQuittingMessages, setDebugMessage: vi.fn(), + dispatchExtensionStateUpdate: vi.fn(), + addConfirmUpdateExtensionRequest: vi.fn(), + openSubagentCreateDialog: vi.fn(), + openAgentsManagerDialog: vi.fn(), }, + new Map(), // extensionsUpdateState + true, // isConfigInitialized + null, // logger ), ); @@ -459,7 +474,7 @@ describe('useSlashCommandProcessor', () => { name: 'loadwiththoughts', action: vi.fn().mockResolvedValue({ type: 'load_history', - history: [{ type: MessageType.MODEL, text: 'response' }], + history: [{ type: MessageType.GEMINI, text: 'response' }], clientHistory: historyWithThoughts, }), }); @@ -904,18 +919,29 @@ describe('useSlashCommandProcessor', () => { mockClearItems, mockLoadHistory, vi.fn(), // refreshStatic - vi.fn(), // onDebugMessage - vi.fn(), // openThemeDialog - mockOpenAuthDialog, - vi.fn(), // openEditorDialog - mockSetQuittingMessages, - vi.fn(), // openSettingsDialog - vi.fn(), // openModelSelectionDialog - vi.fn(), // openSubagentCreateDialog - vi.fn(), // openAgentsManagerDialog vi.fn(), // toggleVimEnabled + false, // isProcessing vi.fn(), // setIsProcessing vi.fn(), // setGeminiMdFileCount + { + openAuthDialog: mockOpenAuthDialog, + openThemeDialog: mockOpenThemeDialog, + openEditorDialog: vi.fn(), + openSettingsDialog: vi.fn(), + openModelDialog: vi.fn(), + openPermissionsDialog: vi.fn(), + openApprovalModeDialog: vi.fn(), + openResumeDialog: vi.fn(), + quit: mockSetQuittingMessages, + setDebugMessage: vi.fn(), + dispatchExtensionStateUpdate: vi.fn(), + addConfirmUpdateExtensionRequest: vi.fn(), + openSubagentCreateDialog: vi.fn(), + openAgentsManagerDialog: vi.fn(), + }, + new Map(), // extensionsUpdateState + true, // isConfigInitialized + null, // logger ), ); diff --git a/packages/cli/src/ui/hooks/slashCommandProcessor.ts b/packages/cli/src/ui/hooks/slashCommandProcessor.ts index 59ff06bcf..80c6bec35 100644 --- a/packages/cli/src/ui/hooks/slashCommandProcessor.ts +++ b/packages/cli/src/ui/hooks/slashCommandProcessor.ts @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { useCallback, useMemo, useEffect, useState } from 'react'; +import { useCallback, useMemo, useEffect, useRef, useState } from 'react'; import { type PartListUnion } from '@google/genai'; import type { UseHistoryManagerReturn } from './useHistoryManager.js'; import { @@ -35,6 +35,7 @@ import { FileCommandLoader } from '../../services/FileCommandLoader.js'; import { McpPromptLoader } from '../../services/McpPromptLoader.js'; import { parseSlashCommand } from '../../utils/commands.js'; import { clearScreen } from '../../utils/stdioHelpers.js'; +import { useKeypress } from './useKeypress.js'; import { type ExtensionUpdateAction, type ExtensionUpdateStatus, @@ -90,6 +91,7 @@ export const useSlashCommandProcessor = ( loadHistory: UseHistoryManagerReturn['loadHistory'], refreshStatic: () => void, toggleVimEnabled: () => Promise, + isProcessing: boolean, setIsProcessing: (isProcessing: boolean) => void, setGeminiMdFileCount: (count: number) => void, actions: SlashCommandProcessorActions, @@ -131,6 +133,34 @@ export const useSlashCommandProcessor = ( null, ); + // AbortController for cancelling async slash commands via ESC + const abortControllerRef = useRef(null); + + const cancelSlashCommand = useCallback(() => { + if (!abortControllerRef.current) { + return; + } + abortControllerRef.current.abort(); + addItem( + { + type: MessageType.INFO, + text: 'Command cancelled.', + }, + Date.now(), + ); + setPendingItem(null); + setIsProcessing(false); + }, [addItem, setIsProcessing]); + + useKeypress( + (key) => { + if (key.name === 'escape') { + cancelSlashCommand(); + } + }, + { isActive: isProcessing }, + ); + const pendingHistoryItems = useMemo(() => { const items: HistoryItemWithoutId[] = []; if (pendingItem != null) { @@ -181,6 +211,11 @@ export const useSlashCommandProcessor = ( type: 'summary', summary: message.summary, }; + } else if (message.type === MessageType.INSIGHT_PROGRESS) { + historyItemContent = { + type: 'insight_progress', + progress: message.progress, + }; } else { historyItemContent = { type: message.type, @@ -319,6 +354,10 @@ export const useSlashCommandProcessor = ( setIsProcessing(true); + // Create a new AbortController for this command execution + const abortController = new AbortController(); + abortControllerRef.current = abortController; + const userMessageTimestamp = Date.now(); addItemWithRecording( { type: MessageType.USER, text: trimmed }, @@ -352,6 +391,7 @@ export const useSlashCommandProcessor = ( args, }, overwriteConfirmed, + abortSignal: abortController.signal, }; // If a one-time list is provided for a "Proceed" action, temporarily @@ -365,10 +405,27 @@ export const useSlashCommandProcessor = ( ]), }; } - const result = await commandToExecute.action( - fullCommandContext, - args, - ); + // Race the command action against the abort signal so that + // ESC cancellation immediately unblocks the await chain. + // Without this, commands like /compress whose underlying + // operation (tryCompressChat) doesn't accept an AbortSignal + // would keep submitQuery stuck until the operation completes. + const abortPromise = new Promise((resolve) => { + abortController.signal.addEventListener( + 'abort', + () => resolve(undefined), + { once: true }, + ); + }); + const result = await Promise.race([ + commandToExecute.action(fullCommandContext, args), + abortPromise, + ]); + + // If the command was cancelled via ESC while executing, skip result processing + if (abortController.signal.aborted) { + return { type: 'handled' }; + } if (result) { switch (result.type) { @@ -561,6 +618,10 @@ export const useSlashCommandProcessor = ( return { type: 'handled' }; } catch (e: unknown) { + // If cancelled via ESC, the cancelSlashCommand callback already handled cleanup + if (abortController.signal.aborted) { + return { type: 'handled' }; + } hasError = true; if (config) { const event = makeSlashCommandEvent({ diff --git a/packages/cli/src/ui/hooks/useCodingPlanUpdates.test.ts b/packages/cli/src/ui/hooks/useCodingPlanUpdates.test.ts index a004fbdcb..bcb5bce33 100644 --- a/packages/cli/src/ui/hooks/useCodingPlanUpdates.test.ts +++ b/packages/cli/src/ui/hooks/useCodingPlanUpdates.test.ts @@ -7,33 +7,16 @@ import { describe, it, expect, vi, beforeEach } from 'vitest'; import { renderHook, waitFor } from '@testing-library/react'; import { useCodingPlanUpdates } from './useCodingPlanUpdates.js'; -import { CODING_PLAN_ENV_KEY } from '../../constants/codingPlan.js'; +import { + CODING_PLAN_ENV_KEY, + getCodingPlanConfig, + CodingPlanRegion, +} from '../../constants/codingPlan.js'; import { AuthType } from '@qwen-code/qwen-code-core'; -// Mock the constants module -vi.mock('../../constants/codingPlan.js', async () => { - const actual = await vi.importActual('../../constants/codingPlan.js'); - return { - ...actual, - CODING_PLAN_VERSION: 'test-version-hash', - CODING_PLAN_MODELS: [ - { - id: 'test-model-1', - name: 'Test Model 1', - baseUrl: 'https://test.example.com/v1', - description: 'Test model 1', - envKey: 'BAILIAN_CODING_PLAN_API_KEY', - }, - { - id: 'test-model-2', - name: 'Test Model 2', - baseUrl: 'https://test.example.com/v1', - description: 'Test model 2', - envKey: 'BAILIAN_CODING_PLAN_API_KEY', - }, - ], - }; -}); +// Get region configs for testing +const chinaConfig = getCodingPlanConfig(CodingPlanRegion.CHINA); +const globalConfig = getCodingPlanConfig(CodingPlanRegion.GLOBAL); describe('useCodingPlanUpdates', () => { const mockSettings = { @@ -50,6 +33,7 @@ describe('useCodingPlanUpdates', () => { const mockConfig = { reloadModelProvidersConfig: vi.fn(), refreshAuth: vi.fn(), + getModel: vi.fn().mockReturnValue('qwen-max'), }; const mockAddItem = vi.fn(); @@ -74,8 +58,11 @@ describe('useCodingPlanUpdates', () => { expect(result.current.codingPlanUpdateRequest).toBeUndefined(); }); - it('should not show update prompt when versions match', () => { - mockSettings.merged.codingPlan = { version: 'test-version-hash' }; + it('should not show update prompt when China region versions match', () => { + mockSettings.merged.codingPlan = { + region: CodingPlanRegion.CHINA, + version: chinaConfig.version, + }; const { result } = renderHook(() => useCodingPlanUpdates( @@ -88,8 +75,52 @@ describe('useCodingPlanUpdates', () => { expect(result.current.codingPlanUpdateRequest).toBeUndefined(); }); - it('should show update prompt when versions differ', async () => { - mockSettings.merged.codingPlan = { version: 'old-version-hash' }; + it('should not show update prompt when Global region versions match', () => { + mockSettings.merged.codingPlan = { + region: CodingPlanRegion.GLOBAL, + version: globalConfig.version, + }; + + const { result } = renderHook(() => + useCodingPlanUpdates( + mockSettings as never, + mockConfig as never, + mockAddItem, + ), + ); + + expect(result.current.codingPlanUpdateRequest).toBeUndefined(); + }); + + it('should default to China region when region is not specified', async () => { + // No region specified, should default to China + mockSettings.merged.codingPlan = { + version: 'old-version-hash', + }; + + const { result } = renderHook(() => + useCodingPlanUpdates( + mockSettings as never, + mockConfig as never, + mockAddItem, + ), + ); + + await waitFor(() => { + expect(result.current.codingPlanUpdateRequest).toBeDefined(); + }); + + // Should prompt for China region since it defaults to China + expect(result.current.codingPlanUpdateRequest?.prompt).toContain( + 'Alibaba Cloud Coding Plan', + ); + }); + + it('should show update prompt when China region versions differ', async () => { + mockSettings.merged.codingPlan = { + region: CodingPlanRegion.CHINA, + version: 'old-version-hash', + }; const { result } = renderHook(() => useCodingPlanUpdates( @@ -104,20 +135,45 @@ describe('useCodingPlanUpdates', () => { }); expect(result.current.codingPlanUpdateRequest?.prompt).toContain( - 'New model configurations', + 'Alibaba Cloud Coding Plan', + ); + }); + + it('should show update prompt when Global region versions differ', async () => { + mockSettings.merged.codingPlan = { + region: CodingPlanRegion.GLOBAL, + version: 'old-version-hash', + }; + + const { result } = renderHook(() => + useCodingPlanUpdates( + mockSettings as never, + mockConfig as never, + mockAddItem, + ), + ); + + await waitFor(() => { + expect(result.current.codingPlanUpdateRequest).toBeDefined(); + }); + + expect(result.current.codingPlanUpdateRequest?.prompt).toContain( + 'Alibaba Cloud Coding Plan', ); }); }); describe('update execution', () => { - it('should execute update when user confirms', async () => { - process.env[CODING_PLAN_ENV_KEY] = 'test-api-key'; - mockSettings.merged.codingPlan = { version: 'old-version-hash' }; + it('should execute China region update when user confirms', async () => { + mockSettings.merged.codingPlan = { + region: CodingPlanRegion.CHINA, + version: 'old-version-hash', + }; mockSettings.merged.modelProviders = { [AuthType.USE_OPENAI]: [ { - id: 'test-model-1', - baseUrl: 'https://test.example.com/v1', + id: 'test-model-china-1', + baseUrl: chinaConfig.baseUrl, envKey: CODING_PLAN_ENV_KEY, }, { @@ -146,33 +202,112 @@ describe('useCodingPlanUpdates', () => { // Wait for async update to complete await waitFor(() => { - // Should update model providers (at least 2 calls: modelProviders + version) + // Should update model providers (at least 2 calls: modelProviders + version + region) expect(mockSettings.setValue).toHaveBeenCalled(); }); - // Should update version + // Should update version with correct hash expect(mockSettings.setValue).toHaveBeenCalledWith( expect.anything(), 'codingPlan.version', - 'test-version-hash', + chinaConfig.version, + ); + + // Should update region + expect(mockSettings.setValue).toHaveBeenCalledWith( + expect.anything(), + 'codingPlan.region', + CodingPlanRegion.CHINA, ); // Should reload and refresh auth expect(mockConfig.reloadModelProvidersConfig).toHaveBeenCalled(); expect(mockConfig.refreshAuth).toHaveBeenCalledWith(AuthType.USE_OPENAI); - // Should show success message + // Should show success message with region info expect(mockAddItem).toHaveBeenCalledWith( expect.objectContaining({ type: 'info', - text: expect.stringContaining('updated successfully'), + text: expect.stringContaining('Alibaba Cloud Coding Plan'), + }), + expect.any(Number), + ); + }); + + it('should execute Global region update when user confirms', async () => { + mockSettings.merged.codingPlan = { + region: CodingPlanRegion.GLOBAL, + version: 'old-version-hash', + }; + mockSettings.merged.modelProviders = { + [AuthType.USE_OPENAI]: [ + { + id: 'test-model-global-1', + baseUrl: globalConfig.baseUrl, + envKey: CODING_PLAN_ENV_KEY, + }, + { + id: 'custom-model', + baseUrl: 'https://custom.example.com', + envKey: 'CUSTOM_API_KEY', + }, + ], + }; + mockConfig.refreshAuth.mockResolvedValue(undefined); + + const { result } = renderHook(() => + useCodingPlanUpdates( + mockSettings as never, + mockConfig as never, + mockAddItem, + ), + ); + + await waitFor(() => { + expect(result.current.codingPlanUpdateRequest).toBeDefined(); + }); + + // Confirm the update + await result.current.codingPlanUpdateRequest!.onConfirm(true); + + // Wait for async update to complete + await waitFor(() => { + expect(mockSettings.setValue).toHaveBeenCalled(); + }); + + // Should update version with correct hash (single version field) + expect(mockSettings.setValue).toHaveBeenCalledWith( + expect.anything(), + 'codingPlan.version', + globalConfig.version, + ); + + // Should update region + expect(mockSettings.setValue).toHaveBeenCalledWith( + expect.anything(), + 'codingPlan.region', + CodingPlanRegion.GLOBAL, + ); + + // Should reload and refresh auth + expect(mockConfig.reloadModelProvidersConfig).toHaveBeenCalled(); + expect(mockConfig.refreshAuth).toHaveBeenCalledWith(AuthType.USE_OPENAI); + + // Should show success message with Global region info + expect(mockAddItem).toHaveBeenCalledWith( + expect.objectContaining({ + type: 'info', + text: expect.stringContaining('Alibaba Cloud Coding Plan'), }), expect.any(Number), ); }); it('should not execute update when user declines', async () => { - mockSettings.merged.codingPlan = { version: 'old-version-hash' }; + mockSettings.merged.codingPlan = { + region: CodingPlanRegion.CHINA, + version: 'old-version-hash', + }; const { result } = renderHook(() => useCodingPlanUpdates( @@ -194,9 +329,103 @@ describe('useCodingPlanUpdates', () => { expect(mockConfig.reloadModelProvidersConfig).not.toHaveBeenCalled(); }); + it('should replace all Coding Plan configs during update (mutually exclusive)', async () => { + // Since regions are mutually exclusive, when updating one region, + // all Coding Plan configs should be replaced (not preserving other region configs) + mockSettings.merged.codingPlan = { + region: CodingPlanRegion.CHINA, + version: 'old-version-hash', + }; + const chinaModelConfig = { + id: 'test-model-china-1', + baseUrl: chinaConfig.baseUrl, + envKey: CODING_PLAN_ENV_KEY, + }; + const globalModelConfig = { + id: 'test-model-global-1', + baseUrl: globalConfig.baseUrl, + envKey: CODING_PLAN_ENV_KEY, + }; + const customConfig = { + id: 'custom-model', + baseUrl: 'https://custom.example.com', + envKey: 'CUSTOM_API_KEY', + }; + mockSettings.merged.modelProviders = { + [AuthType.USE_OPENAI]: [ + chinaModelConfig, + globalModelConfig, + customConfig, + ], + }; + mockConfig.refreshAuth.mockResolvedValue(undefined); + + const { result } = renderHook(() => + useCodingPlanUpdates( + mockSettings as never, + mockConfig as never, + mockAddItem, + ), + ); + + await waitFor(() => { + expect(result.current.codingPlanUpdateRequest).toBeDefined(); + }); + + await result.current.codingPlanUpdateRequest!.onConfirm(true); + + // Wait for async update to complete + await waitFor(() => { + expect(mockSettings.setValue).toHaveBeenCalled(); + }); + + // Get the updated configs passed to setValue + const setValueCalls = mockSettings.setValue.mock.calls; + const modelProvidersCall = setValueCalls.find((call: unknown[]) => + (call[1] as string).includes('modelProviders'), + ); + + expect(modelProvidersCall).toBeDefined(); + const updatedConfigs = modelProvidersCall![2] as Array< + Record + >; + + // Should have new China configs + custom config only (global config removed since regions are mutually exclusive) + // The China template has 8 models, so we expect 8 (from template) + 1 (custom) = 9 + // Note: description field has been removed, only name field contains the branding + expect(updatedConfigs.length).toBe(9); + + // Should NOT contain the Global config (mutually exclusive) + expect( + updatedConfigs.some( + (c: Record) => c['baseUrl'] === globalConfig.baseUrl, + ), + ).toBe(false); + + // Should contain the custom config + expect( + updatedConfigs.some( + (c: Record) => c['id'] === 'custom-model', + ), + ).toBe(true); + + // All configs should use the unified env key + updatedConfigs.forEach((config) => { + if (config['envKey'] === CODING_PLAN_ENV_KEY) { + expect(config['baseUrl']).toBe(chinaConfig.baseUrl); + } + }); + + // Should reload and refresh auth + expect(mockConfig.reloadModelProvidersConfig).toHaveBeenCalled(); + expect(mockConfig.refreshAuth).toHaveBeenCalledWith(AuthType.USE_OPENAI); + }); + it('should preserve non-Coding Plan configs during update', async () => { - process.env[CODING_PLAN_ENV_KEY] = 'test-api-key'; - mockSettings.merged.codingPlan = { version: 'old-version-hash' }; + mockSettings.merged.codingPlan = { + region: CodingPlanRegion.CHINA, + version: 'old-version-hash', + }; const customConfig = { id: 'custom-model', baseUrl: 'https://custom.example.com', @@ -205,8 +434,8 @@ describe('useCodingPlanUpdates', () => { mockSettings.merged.modelProviders = { [AuthType.USE_OPENAI]: [ { - id: 'test-model-1', - baseUrl: 'https://test.example.com/v1', + id: 'test-model-china-1', + baseUrl: chinaConfig.baseUrl, envKey: CODING_PLAN_ENV_KEY, }, customConfig, @@ -233,10 +462,41 @@ describe('useCodingPlanUpdates', () => { // Should preserve custom config - verify setValue was called expect(mockSettings.setValue).toHaveBeenCalled(); }); + + // Get the updated configs passed to setValue + const setValueCalls = mockSettings.setValue.mock.calls; + const modelProvidersCall = setValueCalls.find((call: unknown[]) => + (call[1] as string).includes('modelProviders'), + ); + + // Should preserve custom config + expect(modelProvidersCall).toBeDefined(); + const updatedConfigs = modelProvidersCall![2] as Array< + Record + >; + expect( + updatedConfigs.some( + (c: Record) => c['id'] === 'custom-model', + ), + ).toBe(true); }); - it('should handle missing API key error', async () => { - mockSettings.merged.codingPlan = { version: 'old-version-hash' }; + it('should handle update errors gracefully', async () => { + mockSettings.merged.codingPlan = { + region: CodingPlanRegion.CHINA, + version: 'old-version-hash', + }; + mockSettings.merged.modelProviders = { + [AuthType.USE_OPENAI]: [ + { + id: 'test-model-china-1', + baseUrl: chinaConfig.baseUrl, + envKey: CODING_PLAN_ENV_KEY, + }, + ], + }; + // Simulate an error during refreshAuth + mockConfig.refreshAuth.mockRejectedValue(new Error('Network error')); const { result } = renderHook(() => useCodingPlanUpdates( @@ -253,18 +513,23 @@ describe('useCodingPlanUpdates', () => { await result.current.codingPlanUpdateRequest!.onConfirm(true); // Should show error message - expect(mockAddItem).toHaveBeenCalledWith( - expect.objectContaining({ - type: 'error', - }), - expect.any(Number), - ); + await waitFor(() => { + expect(mockAddItem).toHaveBeenCalledWith( + expect.objectContaining({ + type: 'error', + }), + expect.any(Number), + ); + }); }); }); describe('dismissUpdate', () => { it('should clear update request when dismissed', async () => { - mockSettings.merged.codingPlan = { version: 'old-version-hash' }; + mockSettings.merged.codingPlan = { + region: CodingPlanRegion.CHINA, + version: 'old-version-hash', + }; const { result } = renderHook(() => useCodingPlanUpdates( diff --git a/packages/cli/src/ui/hooks/useCodingPlanUpdates.ts b/packages/cli/src/ui/hooks/useCodingPlanUpdates.ts index 85584def8..138498abf 100644 --- a/packages/cli/src/ui/hooks/useCodingPlanUpdates.ts +++ b/packages/cli/src/ui/hooks/useCodingPlanUpdates.ts @@ -10,9 +10,10 @@ import { AuthType } from '@qwen-code/qwen-code-core'; import type { LoadedSettings } from '../../config/settings.js'; import { getPersistScopeForModelSelection } from '../../config/modelProvidersScope.js'; import { - CODING_PLAN_MODELS, + isCodingPlanConfig, + getCodingPlanConfig, + CodingPlanRegion, CODING_PLAN_ENV_KEY, - CODING_PLAN_VERSION, } from '../../constants/codingPlan.js'; import { t } from '../../i18n/index.js'; @@ -21,20 +22,6 @@ export interface CodingPlanUpdateRequest { onConfirm: (confirmed: boolean) => void; } -/** - * Checks if a config is a Coding Plan configuration by matching baseUrl and envKey. - * This ensures only configs from the Coding Plan provider are identified. - */ -function isCodingPlanConfig(config: { - baseUrl?: string; - envKey?: string; -}): boolean { - return ( - config.envKey === CODING_PLAN_ENV_KEY && - CODING_PLAN_MODELS.some((template) => template.baseUrl === config.baseUrl) - ); -} - /** * Hook for detecting and handling Coding Plan template updates. * Compares the persisted version with the current template version @@ -55,134 +42,147 @@ export function useCodingPlanUpdates( /** * Execute the Coding Plan configuration update. * Removes old Coding Plan configs and replaces them with new ones from the template. + * Uses the region from settings.codingPlan.region (defaults to CHINA). */ - const executeUpdate = useCallback(async () => { - try { - const persistScope = getPersistScopeForModelSelection(settings); + const executeUpdate = useCallback( + async (region: CodingPlanRegion = CodingPlanRegion.CHINA) => { + try { + const persistScope = getPersistScopeForModelSelection(settings); - // Get current configs - const currentConfigs = - ( - settings.merged.modelProviders as - | Record>> - | undefined - )?.[AuthType.USE_OPENAI] || []; + // Get current configs + const currentConfigs = + ( + settings.merged.modelProviders as + | Record>> + | undefined + )?.[AuthType.USE_OPENAI] || []; - // Filter out Coding Plan configs (keep user custom configs) - const nonCodingPlanConfigs = currentConfigs.filter( - (cfg) => - !isCodingPlanConfig({ - baseUrl: cfg['baseUrl'] as string | undefined, - envKey: cfg['envKey'] as string | undefined, - }), - ); - - // Generate new configs from template with the stored API key - const apiKey = process.env[CODING_PLAN_ENV_KEY]; - if (!apiKey) { - throw new Error( - t( - 'Coding Plan API key not found. Please re-authenticate with Coding Plan.', - ), + // Filter out all Coding Plan configs (since they are mutually exclusive) + // Keep only non-Coding-Plan user custom configs + const nonCodingPlanConfigs = currentConfigs.filter( + (cfg) => + !isCodingPlanConfig( + cfg['baseUrl'] as string | undefined, + cfg['envKey'] as string | undefined, + ), ); + + // Get the configuration for the current region + const { template, version } = getCodingPlanConfig(region); + + // Generate new configs from template + const newConfigs = template.map((templateConfig) => ({ + ...templateConfig, + envKey: CODING_PLAN_ENV_KEY, + })); + + // Combine: new Coding Plan configs at the front, user configs preserved + const updatedConfigs = [ + ...newConfigs, + ...(nonCodingPlanConfigs as Array>), + ] as Array>; + + // Hot-reload model providers configuration first (in-memory only) + const updatedModelProviders = { + ...(settings.merged.modelProviders as + | Record + | undefined), + [AuthType.USE_OPENAI]: updatedConfigs, + }; + config.reloadModelProvidersConfig( + updatedModelProviders as unknown as ModelProvidersConfig, + ); + + // Refresh auth with the new configuration + // This validates the configuration before persisting + await config.refreshAuth(AuthType.USE_OPENAI); + + // Persist to settings only after successful auth refresh + settings.setValue( + persistScope, + `modelProviders.${AuthType.USE_OPENAI}`, + updatedConfigs, + ); + + // Update the version (single version field for backward compatibility) + settings.setValue(persistScope, 'codingPlan.version', version); + + // Update the region + settings.setValue(persistScope, 'codingPlan.region', region); + + const activeModel = config.getModel(); + + addItem( + { + type: 'info', + text: t( + '{{region}} configuration updated successfully. Model switched to "{{model}}".', + { region: t('Alibaba Cloud Coding Plan'), model: activeModel }, + ), + }, + Date.now(), + ); + + return true; + } catch (error) { + const errorMessage = + error instanceof Error ? error.message : String(error); + addItem( + { + type: 'error', + text: t('Failed to update Coding Plan configuration: {{message}}', { + message: errorMessage, + }), + }, + Date.now(), + ); + return false; } - - const newConfigs = CODING_PLAN_MODELS.map((templateConfig) => ({ - ...templateConfig, - envKey: CODING_PLAN_ENV_KEY, - })); - - // Combine: new Coding Plan configs at the front, user configs preserved - const updatedConfigs = [ - ...newConfigs, - ...(nonCodingPlanConfigs as Array>), - ] as Array>; - - // Persist updated model providers - settings.setValue( - persistScope, - `modelProviders.${AuthType.USE_OPENAI}`, - updatedConfigs, - ); - - // Update the version - settings.setValue( - persistScope, - 'codingPlan.version', - CODING_PLAN_VERSION, - ); - - // Hot-reload model providers configuration - const updatedModelProviders = { - ...(settings.merged.modelProviders as - | Record - | undefined), - [AuthType.USE_OPENAI]: updatedConfigs, - }; - config.reloadModelProvidersConfig( - updatedModelProviders as unknown as ModelProvidersConfig, - ); - - // Refresh auth with the new configuration - await config.refreshAuth(AuthType.USE_OPENAI); - - addItem( - { - type: 'info', - text: t( - 'Coding Plan configuration updated successfully. New models are now available.', - ), - }, - Date.now(), - ); - - return true; - } catch (error) { - const errorMessage = - error instanceof Error ? error.message : String(error); - addItem( - { - type: 'error', - text: t('Failed to update Coding Plan configuration: {{message}}', { - message: errorMessage, - }), - }, - Date.now(), - ); - return false; - } - }, [settings, config, addItem]); + }, + [settings, config, addItem], + ); /** * Check for version mismatch and prompt user for update if needed. + * Uses the region from settings.codingPlan.region (defaults to CHINA if not set). */ const checkForUpdates = useCallback(() => { - const savedVersion = ( - settings.merged as { codingPlan?: { version?: string } } - ).codingPlan?.version; + const mergedSettings = settings.merged as { + codingPlan?: { + version?: string; + region?: CodingPlanRegion; + }; + }; + + // Get the region (default to CHINA if not set) + const region = mergedSettings.codingPlan?.region ?? CodingPlanRegion.CHINA; + + // Get the saved version for the current region + const savedVersion = mergedSettings.codingPlan?.version; // If no version is stored, user hasn't used Coding Plan yet - skip check if (!savedVersion) { return; } - // If versions match, no update needed - if (savedVersion === CODING_PLAN_VERSION) { - return; - } + // Get current version for the region + const currentVersion = getCodingPlanConfig(region).version; - // Version mismatch - prompt user for update - setUpdateRequest({ - prompt: t( - 'New model configurations are available for Bailian Coding Plan. Update now?', - ), - onConfirm: async (confirmed: boolean) => { - setUpdateRequest(undefined); - if (confirmed) { - await executeUpdate(); - } - }, - }); + // Check if version matches + if (savedVersion !== currentVersion) { + setUpdateRequest({ + prompt: t( + 'New model configurations are available for {{region}}. Update now?', + { region: t('Alibaba Cloud Coding Plan') }, + ), + onConfirm: async (confirmed: boolean) => { + setUpdateRequest(undefined); + if (confirmed) { + await executeUpdate(region); + } + }, + }); + } }, [settings, executeUpdate]); // Check for updates on mount diff --git a/packages/cli/src/ui/hooks/useGeminiStream.test.tsx b/packages/cli/src/ui/hooks/useGeminiStream.test.tsx index 2d90012cd..42f28f5e2 100644 --- a/packages/cli/src/ui/hooks/useGeminiStream.test.tsx +++ b/packages/cli/src/ui/hooks/useGeminiStream.test.tsx @@ -9,7 +9,6 @@ import type { Mock, MockInstance } from 'vitest'; import { describe, it, expect, vi, beforeEach } from 'vitest'; import { renderHook, act, waitFor } from '@testing-library/react'; import { useGeminiStream } from './useGeminiStream.js'; -import { useKeypress } from './useKeypress.js'; import * as atCommandProcessor from './atCommandProcessor.js'; import type { TrackedToolCall, @@ -67,17 +66,14 @@ const MockedUserPromptEvent = vi.hoisted(() => const MockedApiCancelEvent = vi.hoisted(() => vi.fn().mockImplementation(() => {}), ); -const mockParseAndFormatApiError = vi.hoisted(() => vi.fn()); +const mockParseAndFormatApiError = vi.hoisted(() => + vi.fn( + (msg: unknown) => + `[API Error: ${typeof msg === 'string' ? msg : 'An unknown error occurred.'}]`, + ), +); const mockLogApiCancel = vi.hoisted(() => vi.fn()); -// Vision auto-switch mocks (hoisted) -const mockHandleVisionSwitch = vi.hoisted(() => - vi.fn().mockResolvedValue({ shouldProceed: true }), -); -const mockRestoreOriginalModel = vi.hoisted(() => - vi.fn().mockResolvedValue(undefined), -); - vi.mock('@qwen-code/qwen-code-core', async (importOriginal) => { const actualCoreModule = (await importOriginal()) as any; return { @@ -100,17 +96,6 @@ vi.mock('./useReactToolScheduler.js', async (importOriginal) => { }; }); -vi.mock('./useVisionAutoSwitch.js', () => ({ - useVisionAutoSwitch: vi.fn(() => ({ - handleVisionSwitch: mockHandleVisionSwitch, - restoreOriginalModel: mockRestoreOriginalModel, - })), -})); - -vi.mock('./useKeypress.js', () => ({ - useKeypress: vi.fn(), -})); - vi.mock('./shellCommandProcessor.js', () => ({ useShellCommandProcessor: vi.fn().mockReturnValue({ handleShellCommand: vi.fn(), @@ -123,22 +108,6 @@ vi.mock('../utils/markdownUtilities.js', () => ({ findLastSafeSplitPoint: vi.fn((s: string) => s.length), })); -vi.mock('./useStateAndRef.js', () => ({ - useStateAndRef: vi.fn((initial) => { - let val = initial; - const ref = { current: val }; - const setVal = vi.fn((updater) => { - if (typeof updater === 'function') { - val = updater(val); - } else { - val = updater; - } - ref.current = val; - }); - return [val, ref, setVal]; - }), -})); - vi.mock('./useLogger.js', () => ({ useLogger: vi.fn().mockReturnValue({ logMessage: vi.fn().mockResolvedValue(undefined), @@ -322,7 +291,6 @@ describe('useGeminiStream', () => { () => {}, () => {}, () => {}, - false, // visionModelPreviewEnabled () => {}, 80, 24, @@ -488,7 +456,6 @@ describe('useGeminiStream', () => { () => {}, () => {}, () => {}, - false, // visionModelPreviewEnabled () => {}, 80, 24, @@ -573,7 +540,6 @@ describe('useGeminiStream', () => { () => {}, () => {}, () => {}, - false, // visionModelPreviewEnabled () => {}, 80, 24, @@ -686,7 +652,6 @@ describe('useGeminiStream', () => { () => {}, () => {}, () => {}, - false, // visionModelPreviewEnabled () => {}, 80, 24, @@ -800,7 +765,6 @@ describe('useGeminiStream', () => { () => {}, () => {}, () => {}, - false, // visionModelPreviewEnabled () => {}, 80, 24, @@ -850,28 +814,8 @@ describe('useGeminiStream', () => { expect(result.current.streamingState).toBe(StreamingState.Responding); }); - describe('User Cancellation', () => { - let keypressCallback: (key: any) => void; - const mockUseKeypress = useKeypress as Mock; - - beforeEach(() => { - // Capture the callback passed to useKeypress - mockUseKeypress.mockImplementation((callback, options) => { - if (options.isActive) { - keypressCallback = callback; - } else { - keypressCallback = () => {}; - } - }); - }); - - const simulateEscapeKeyPress = () => { - act(() => { - keypressCallback({ name: 'escape' }); - }); - }; - - it('should cancel an in-progress stream when escape is pressed', async () => { + describe('Cancellation', () => { + it('should cancel an in-progress stream when cancelOngoingRequest is called', async () => { const mockStream = (async function* () { yield { type: 'content', value: 'Part 1' }; // Keep the stream open @@ -891,8 +835,10 @@ describe('useGeminiStream', () => { expect(result.current.streamingState).toBe(StreamingState.Responding); }); - // Simulate escape key press - simulateEscapeKeyPress(); + // Call cancelOngoingRequest directly + act(() => { + result.current.cancelOngoingRequest(); + }); // Verify cancellation message is added await waitFor(() => { @@ -909,7 +855,7 @@ describe('useGeminiStream', () => { expect(result.current.streamingState).toBe(StreamingState.Idle); }); - it('should call onCancelSubmit handler when escape is pressed', async () => { + it('should call onCancelSubmit handler when cancelOngoingRequest is called', async () => { const cancelSubmitSpy = vi.fn(); const mockStream = (async function* () { yield { type: 'content', value: 'Part 1' }; @@ -935,7 +881,6 @@ describe('useGeminiStream', () => { () => {}, () => {}, cancelSubmitSpy, - false, // visionModelPreviewEnabled () => {}, 80, 24, @@ -947,12 +892,14 @@ describe('useGeminiStream', () => { result.current.submitQuery('test query'); }); - simulateEscapeKeyPress(); + act(() => { + result.current.cancelOngoingRequest(); + }); expect(cancelSubmitSpy).toHaveBeenCalled(); }); - it('should call setShellInputFocused(false) when escape is pressed', async () => { + it('should call setShellInputFocused(false) when cancelOngoingRequest is called', async () => { const setShellInputFocusedSpy = vi.fn(); const mockStream = (async function* () { yield { type: 'content', value: 'Part 1' }; @@ -977,7 +924,6 @@ describe('useGeminiStream', () => { () => {}, () => {}, vi.fn(), - false, setShellInputFocusedSpy, // Pass the spy here 80, 24, @@ -989,18 +935,22 @@ describe('useGeminiStream', () => { result.current.submitQuery('test query'); }); - simulateEscapeKeyPress(); + act(() => { + result.current.cancelOngoingRequest(); + }); expect(setShellInputFocusedSpy).toHaveBeenCalledWith(false); }); - it('should not do anything if escape is pressed when not responding', () => { + it('should not do anything if cancelOngoingRequest is called when not responding', () => { const { result } = renderTestHook(); expect(result.current.streamingState).toBe(StreamingState.Idle); - // Simulate escape key press - simulateEscapeKeyPress(); + // Call cancelOngoingRequest + act(() => { + result.current.cancelOngoingRequest(); + }); // No change should happen, no cancellation message expect(mockAddItem).not.toHaveBeenCalledWith( @@ -1035,7 +985,9 @@ describe('useGeminiStream', () => { }); // Cancel the request - simulateEscapeKeyPress(); + act(() => { + result.current.cancelOngoingRequest(); + }); // Allow the stream to continue act(() => { @@ -1083,7 +1035,9 @@ describe('useGeminiStream', () => { expect(result.current.streamingState).toBe(StreamingState.Responding); // Try to cancel - simulateEscapeKeyPress(); + act(() => { + result.current.cancelOngoingRequest(); + }); // Nothing should happen because the state is not `Responding` expect(abortSpy).not.toHaveBeenCalled(); @@ -1297,7 +1251,6 @@ describe('useGeminiStream', () => { () => {}, () => {}, () => {}, - false, // visionModelPreviewEnabled () => {}, 80, 24, @@ -1355,7 +1308,6 @@ describe('useGeminiStream', () => { () => {}, () => {}, () => {}, - false, // visionModelPreviewEnabled () => {}, 80, 24, @@ -1849,7 +1801,6 @@ describe('useGeminiStream', () => { () => {}, () => {}, () => {}, - false, // visionModelPreviewEnabled () => {}, 80, 24, @@ -1905,7 +1856,6 @@ describe('useGeminiStream', () => { () => {}, () => {}, () => {}, - false, // visionModelPreviewEnabled () => {}, 80, 24, @@ -1962,7 +1912,6 @@ describe('useGeminiStream', () => { () => {}, () => {}, () => {}, - false, // visionModelPreviewEnabled () => {}, 80, 24, @@ -2059,7 +2008,6 @@ describe('useGeminiStream', () => { () => {}, () => {}, () => {}, - false, // visionModelPreviewEnabled vi.fn(), 80, 24, @@ -2114,7 +2062,6 @@ describe('useGeminiStream', () => { vi.fn(), // setModelSwitched vi.fn(), // onEditorClose vi.fn(), // onCancelSubmit - false, // visionModelPreviewEnabled vi.fn(), // setShellInputFocused 80, // terminalWidth 24, // terminalHeight @@ -2188,7 +2135,6 @@ describe('useGeminiStream', () => { () => {}, () => {}, () => {}, - false, // visionModelPreviewEnabled () => {}, 80, 24, @@ -2280,7 +2226,6 @@ describe('useGeminiStream', () => { () => {}, () => {}, () => {}, - false, // visionModelPreviewEnabled () => {}, 80, 24, @@ -2296,6 +2241,112 @@ describe('useGeminiStream', () => { }); }); + it('should show a retry countdown and update pending history over time', async () => { + vi.useFakeTimers(); + try { + let resolveStream: (() => void) | undefined; + mockSendMessageStream.mockReturnValue( + (async function* () { + yield { + type: ServerGeminiEventType.Retry, + retryInfo: { + message: '[API Error: Rate limit exceeded]', + attempt: 1, + maxRetries: 3, + delayMs: 3000, + }, + }; + yield { + type: ServerGeminiEventType.Retry, + }; + await new Promise((resolve) => { + resolveStream = resolve; + }); + yield { + type: ServerGeminiEventType.Finished, + value: { reason: 'STOP', usageMetadata: undefined }, + }; + })(), + ); + + const { result } = renderHook(() => + useGeminiStream( + new MockedGeminiClientClass(mockConfig), + [], + mockAddItem, + mockConfig, + mockLoadedSettings, + mockOnDebugMessage, + mockHandleSlashCommand, + false, + () => 'vscode' as EditorType, + () => {}, + () => Promise.resolve(), + false, + () => {}, + () => {}, + () => {}, + () => {}, + 80, + 24, + ), + ); + + act(() => { + void result.current.submitQuery('Trigger retry'); + }); + + await act(async () => { + await Promise.resolve(); + }); + + const findErrorItem = () => + result.current.pendingHistoryItems.find( + (item) => item.type === MessageType.ERROR, + ); + + let errorItem = findErrorItem(); + for (let attempts = 0; attempts < 5 && !errorItem; attempts++) { + await act(async () => { + await Promise.resolve(); + }); + errorItem = findErrorItem(); + } + + // Error item should contain the error text and a retry hint + expect(errorItem?.text).toContain('Rate limit exceeded'); + // Countdown hint should be inline on the error item (not a separate item) + expect((errorItem as { hint?: string })?.hint).toContain('3s'); + expect((errorItem as { hint?: string })?.hint).toContain('attempt 1/3'); + + await act(async () => { + await vi.advanceTimersByTimeAsync(1000); + }); + + const errorAfterOneSecond = result.current.pendingHistoryItems.find( + (item) => item.type === MessageType.ERROR, + ); + expect((errorAfterOneSecond as { hint?: string })?.hint).toContain( + '2s', + ); + + resolveStream?.(); + + await act(async () => { + await Promise.resolve(); + await vi.runAllTimersAsync(); + }); + + // Error item (with hint) should be cleared after retry succeeds + const remainingError = result.current.pendingHistoryItems.find( + (item) => item.type === MessageType.ERROR, + ); + expect(remainingError).toBeUndefined(); + } finally { + vi.useRealTimers(); + } + }); + it('should memoize pendingHistoryItems', () => { mockUseReactToolScheduler.mockReturnValue([ [], @@ -2321,7 +2372,6 @@ describe('useGeminiStream', () => { () => {}, () => {}, () => {}, - false, // visionModelPreviewEnabled vi.fn(), // setShellInputFocused 80, 24, @@ -2392,7 +2442,6 @@ describe('useGeminiStream', () => { () => {}, () => {}, () => {}, - false, // visionModelPreviewEnabled () => {}, 80, 24, @@ -2451,7 +2500,6 @@ describe('useGeminiStream', () => { () => {}, () => {}, () => {}, - false, // visionModelPreviewEnabled () => {}, 80, 24, @@ -2463,14 +2511,13 @@ describe('useGeminiStream', () => { await result.current.submitQuery('Test query'); }); - // Verify error message was added + // Verify error message appears in pending history items (not via addItem, + // since errors with retry hints are now stored as pending items) await waitFor(() => { - expect(mockAddItem).toHaveBeenCalledWith( - expect.objectContaining({ - type: 'error', - }), - expect.any(Number), + const errorItem = result.current.pendingHistoryItems.find( + (item) => item.type === 'error', ); + expect(errorItem).toBeDefined(); }); // Verify parseAndFormatApiError was called @@ -2639,187 +2686,6 @@ describe('useGeminiStream', () => { }); // --- New tests focused on recent modifications --- - describe('Vision Auto Switch Integration', () => { - it('should call handleVisionSwitch and proceed to send when allowed', async () => { - mockHandleVisionSwitch.mockResolvedValueOnce({ shouldProceed: true }); - mockSendMessageStream.mockReturnValue( - (async function* () { - yield { type: ServerGeminiEventType.Content, value: 'ok' }; - yield { type: ServerGeminiEventType.Finished, value: 'STOP' }; - })(), - ); - - const { result } = renderHook(() => - useGeminiStream( - new MockedGeminiClientClass(mockConfig), - [], - mockAddItem, - mockConfig, - mockLoadedSettings, - mockOnDebugMessage, - mockHandleSlashCommand, - false, - () => 'vscode' as EditorType, - () => {}, - () => Promise.resolve(), - false, - () => {}, - () => {}, - () => {}, - false, // visionModelPreviewEnabled - vi.fn(), // setShellInputFocused - 80, - 24, - ), - ); - - await act(async () => { - await result.current.submitQuery('image prompt'); - }); - - await waitFor(() => { - expect(mockHandleVisionSwitch).toHaveBeenCalled(); - expect(mockSendMessageStream).toHaveBeenCalled(); - }); - }); - - it('should gate submission when handleVisionSwitch returns shouldProceed=false', async () => { - mockHandleVisionSwitch.mockResolvedValueOnce({ shouldProceed: false }); - - const { result } = renderHook(() => - useGeminiStream( - new MockedGeminiClientClass(mockConfig), - [], - mockAddItem, - mockConfig, - mockLoadedSettings, - mockOnDebugMessage, - mockHandleSlashCommand, - false, - () => 'vscode' as EditorType, - () => {}, - () => Promise.resolve(), - false, - () => {}, - () => {}, - () => {}, - false, // visionModelPreviewEnabled - vi.fn(), // setShellInputFocused - 80, - 24, - ), - ); - - await act(async () => { - await result.current.submitQuery('vision-gated'); - }); - - // No call to API, no restoreOriginalModel needed since no override occurred - expect(mockSendMessageStream).not.toHaveBeenCalled(); - expect(mockRestoreOriginalModel).not.toHaveBeenCalled(); - - // Next call allowed (flag reset path) - mockHandleVisionSwitch.mockResolvedValueOnce({ shouldProceed: true }); - mockSendMessageStream.mockReturnValue( - (async function* () { - yield { type: ServerGeminiEventType.Content, value: 'ok' }; - yield { type: ServerGeminiEventType.Finished, value: 'STOP' }; - })(), - ); - await act(async () => { - await result.current.submitQuery('after-gate'); - }); - await waitFor(() => { - expect(mockSendMessageStream).toHaveBeenCalled(); - }); - }); - }); - - describe('Model restore on completion and errors', () => { - it('should restore model after successful stream completion', async () => { - mockSendMessageStream.mockReturnValue( - (async function* () { - yield { type: ServerGeminiEventType.Content, value: 'content' }; - yield { type: ServerGeminiEventType.Finished, value: 'STOP' }; - })(), - ); - - const { result } = renderHook(() => - useGeminiStream( - new MockedGeminiClientClass(mockConfig), - [], - mockAddItem, - mockConfig, - mockLoadedSettings, - mockOnDebugMessage, - mockHandleSlashCommand, - false, - () => 'vscode' as EditorType, - () => {}, - () => Promise.resolve(), - false, - () => {}, - () => {}, - () => {}, - false, // visionModelPreviewEnabled - vi.fn(), // setShellInputFocused - 80, - 24, - ), - ); - - await act(async () => { - await result.current.submitQuery('restore-success'); - }); - - await waitFor(() => { - expect(mockRestoreOriginalModel).toHaveBeenCalledTimes(1); - }); - }); - - it('should restore model when an error occurs during streaming', async () => { - const testError = new Error('stream failure'); - mockSendMessageStream.mockReturnValue( - (async function* () { - yield { type: ServerGeminiEventType.Content, value: 'content' }; - throw testError; - })(), - ); - - const { result } = renderHook(() => - useGeminiStream( - new MockedGeminiClientClass(mockConfig), - [], - mockAddItem, - mockConfig, - mockLoadedSettings, - mockOnDebugMessage, - mockHandleSlashCommand, - false, - () => 'vscode' as EditorType, - () => {}, - () => Promise.resolve(), - false, - () => {}, - () => {}, - () => {}, - false, // visionModelPreviewEnabled - vi.fn(), // setShellInputFocused - 80, - 24, - ), - ); - - await act(async () => { - await result.current.submitQuery('restore-error'); - }); - - await waitFor(() => { - expect(mockRestoreOriginalModel).toHaveBeenCalledTimes(1); - }); - }); - }); - describe('Loop Detection Confirmation', () => { beforeEach(() => { // Add mock for getLoopDetectionService to the config diff --git a/packages/cli/src/ui/hooks/useGeminiStream.ts b/packages/cli/src/ui/hooks/useGeminiStream.ts index e142d91f0..0e5f29216 100644 --- a/packages/cli/src/ui/hooks/useGeminiStream.ts +++ b/packages/cli/src/ui/hooks/useGeminiStream.ts @@ -35,6 +35,8 @@ import { ToolConfirmationOutcome, logApiCancel, ApiCancelEvent, + isSupportedImageMimeType, + getUnsupportedImageFormatWarning, } from '@qwen-code/qwen-code-core'; import { type Part, type PartListUnion, FinishReason } from '@google/genai'; import type { @@ -46,7 +48,6 @@ import type { import { StreamingState, MessageType, ToolCallStatus } from '../types.js'; import { isAtCommand, isSlashCommand } from '../utils/commandUtils.js'; import { useShellCommandProcessor } from './shellCommandProcessor.js'; -import { useVisionAutoSwitch } from './useVisionAutoSwitch.js'; import { handleAtCommand } from './atCommandProcessor.js'; import { findLastSafeSplitPoint } from '../utils/markdownUtilities.js'; import { useStateAndRef } from './useStateAndRef.js'; @@ -63,11 +64,65 @@ import { import { promises as fs } from 'node:fs'; import path from 'node:path'; import { useSessionStats } from '../contexts/SessionContext.js'; -import { useKeypress } from './useKeypress.js'; import type { LoadedSettings } from '../../config/settings.js'; +import { t } from '../../i18n/index.js'; const debugLogger = createDebugLogger('GEMINI_STREAM'); +/** + * Checks if image parts have supported formats and returns unsupported ones + */ +function checkImageFormatsSupport(parts: PartListUnion): { + hasImages: boolean; + hasUnsupportedFormats: boolean; + unsupportedMimeTypes: string[]; +} { + const unsupportedMimeTypes: string[] = []; + let hasImages = false; + + if (typeof parts === 'string') { + return { + hasImages: false, + hasUnsupportedFormats: false, + unsupportedMimeTypes: [], + }; + } + + const partsArray = Array.isArray(parts) ? parts : [parts]; + + for (const part of partsArray) { + if (typeof part === 'string') continue; + + let mimeType: string | undefined; + + // Check inlineData + if ( + 'inlineData' in part && + part.inlineData?.mimeType?.startsWith('image/') + ) { + hasImages = true; + mimeType = part.inlineData.mimeType; + } + + // Check fileData + if ('fileData' in part && part.fileData?.mimeType?.startsWith('image/')) { + hasImages = true; + mimeType = part.fileData.mimeType; + } + + // Check if the mime type is supported + if (mimeType && !isSupportedImageMimeType(mimeType)) { + unsupportedMimeTypes.push(mimeType); + } + } + + return { + hasImages, + hasUnsupportedFormats: unsupportedMimeTypes.length > 0, + unsupportedMimeTypes, + }; +} + enum StreamProcessingStatus { Completed, UserCancelled, @@ -106,25 +161,33 @@ export const useGeminiStream = ( setModelSwitchedFromQuotaError: React.Dispatch>, onEditorClose: () => void, onCancelSubmit: () => void, - visionModelPreviewEnabled: boolean, setShellInputFocused: (value: boolean) => void, terminalWidth: number, terminalHeight: number, - onVisionSwitchRequired?: (query: PartListUnion) => Promise<{ - modelOverride?: string; - persistSessionModel?: string; - showGuidance?: boolean; - }>, - isShellFocused?: boolean, ) => { const [initError, setInitError] = useState(null); const abortControllerRef = useRef(null); const turnCancelledRef = useRef(false); const isSubmittingQueryRef = useRef(false); + const lastPromptRef = useRef(null); + const lastPromptErroredRef = useRef(false); const [isResponding, setIsResponding] = useState(false); const [thought, setThought] = useState(null); const [pendingHistoryItem, pendingHistoryItemRef, setPendingHistoryItem] = useStateAndRef(null); + const [ + pendingRetryErrorItem, + pendingRetryErrorItemRef, + setPendingRetryErrorItem, + ] = useStateAndRef(null); + const [ + pendingRetryCountdownItem, + pendingRetryCountdownItemRef, + setPendingRetryCountdownItem, + ] = useStateAndRef(null); + const retryCountdownTimerRef = useRef | null>( + null, + ); const processedMemoryToolsRef = useRef>(new Set()); const { startNewPrompt, @@ -189,6 +252,83 @@ export const useGeminiStream = ( onComplete: (result: { userSelection: 'disable' | 'keep' }) => void; } | null>(null); + const stopRetryCountdownTimer = useCallback(() => { + if (retryCountdownTimerRef.current) { + clearInterval(retryCountdownTimerRef.current); + retryCountdownTimerRef.current = null; + } + }, []); + + /** + * Clears the retry countdown timer and pending retry items. + */ + const clearRetryCountdown = useCallback(() => { + stopRetryCountdownTimer(); + setPendingRetryErrorItem(null); + setPendingRetryCountdownItem(null); + }, [ + setPendingRetryErrorItem, + setPendingRetryCountdownItem, + stopRetryCountdownTimer, + ]); + + const startRetryCountdown = useCallback( + (retryInfo: { + message?: string; + attempt: number; + maxRetries: number; + delayMs: number; + }) => { + stopRetryCountdownTimer(); + const startTime = Date.now(); + const { message, attempt, maxRetries, delayMs } = retryInfo; + const retryReasonText = + message ?? t('Rate limit exceeded. Please wait and try again.'); + + // Countdown line updates every second (dim/secondary color) + const updateCountdown = () => { + const elapsedMs = Date.now() - startTime; + const remainingMs = Math.max(0, delayMs - elapsedMs); + const remainingSec = Math.ceil(remainingMs / 1000); + + // Update error item with hint containing countdown info (short format) + const hintText = `Retrying in ${remainingSec}s… (attempt ${attempt}/${maxRetries})`; + + setPendingRetryErrorItem({ + type: MessageType.ERROR, + text: retryReasonText, + hint: hintText, + }); + + setPendingRetryCountdownItem({ + type: 'retry_countdown', + text: t( + 'Retrying in {{seconds}} seconds… (attempt {{attempt}}/{{maxRetries}})', + { + seconds: String(remainingSec), + attempt: String(attempt), + maxRetries: String(maxRetries), + }, + ), + } as HistoryItemWithoutId); + + if (remainingMs <= 0) { + stopRetryCountdownTimer(); + } + }; + + updateCountdown(); + retryCountdownTimerRef.current = setInterval(updateCountdown, 1000); + }, + [ + setPendingRetryErrorItem, + setPendingRetryCountdownItem, + stopRetryCountdownTimer, + ], + ); + + useEffect(() => () => stopRetryCountdownTimer(), [stopRetryCountdownTimer]); + const onExec = useCallback(async (done: Promise) => { setIsResponding(true); await done; @@ -206,12 +346,6 @@ export const useGeminiStream = ( terminalHeight, ); - const { handleVisionSwitch, restoreOriginalModel } = useVisionAutoSwitch( - config, - addItem, - visionModelPreviewEnabled, - onVisionSwitchRequired, - ); const activePtyId = activeShellPtyId || activeToolPtyId; useEffect(() => { @@ -295,6 +429,7 @@ export const useGeminiStream = ( Date.now(), ); setPendingHistoryItem(null); + clearRetryCountdown(); onCancelSubmit(); setIsResponding(false); setShellInputFocused(false); @@ -305,19 +440,11 @@ export const useGeminiStream = ( onCancelSubmit, pendingHistoryItemRef, setShellInputFocused, + clearRetryCountdown, config, getPromptCount, ]); - useKeypress( - (key) => { - if (key.name === 'escape' && !isShellFocused) { - cancelOngoingRequest(); - } - }, - { isActive: streamingState === StreamingState.Responding }, - ); - const prepareQueryForGemini = useCallback( async ( query: PartListUnion, @@ -585,6 +712,7 @@ export const useGeminiStream = ( return; } + lastPromptErroredRef.current = false; if (pendingHistoryItemRef.current) { if (pendingHistoryItemRef.current.type === 'tool_group') { const updatedTools = pendingHistoryItemRef.current.tools.map( @@ -609,31 +737,55 @@ export const useGeminiStream = ( { type: MessageType.INFO, text: 'User cancelled the request.' }, userMessageTimestamp, ); + clearRetryCountdown(); setIsResponding(false); setThought(null); // Reset thought when user cancels }, - [addItem, pendingHistoryItemRef, setPendingHistoryItem, setThought], + [ + addItem, + pendingHistoryItemRef, + setPendingHistoryItem, + setThought, + clearRetryCountdown, + ], ); const handleErrorEvent = useCallback( (eventValue: GeminiErrorEventValue, userMessageTimestamp: number) => { + lastPromptErroredRef.current = true; if (pendingHistoryItemRef.current) { addItem(pendingHistoryItemRef.current, userMessageTimestamp); setPendingHistoryItem(null); } - addItem( - { - type: MessageType.ERROR, + // Only show Ctrl+Y hint if not already showing an auto-retry countdown + // (auto-retry countdown is shown when retryCountdownTimerRef is active) + const isShowingAutoRetry = retryCountdownTimerRef.current !== null; + clearRetryCountdown(); + if (!isShowingAutoRetry) { + const retryHint = t('Press Ctrl+Y to retry'); + // Store error with hint as a pending item (not in history). + // This allows the hint to be removed when the user retries with Ctrl+Y, + // since pending items are in the dynamic rendering area (not ). + setPendingRetryErrorItem({ + type: 'error' as const, text: parseAndFormatApiError( eventValue.error, config.getContentGeneratorConfig()?.authType, ), - }, - userMessageTimestamp, - ); + hint: retryHint, + }); + } setThought(null); // Reset thought when there's an error }, - [addItem, pendingHistoryItemRef, setPendingHistoryItem, config, setThought], + [ + addItem, + pendingHistoryItemRef, + setPendingHistoryItem, + setPendingRetryErrorItem, + config, + setThought, + clearRetryCountdown, + ], ); const handleCitationEvent = useCallback( @@ -693,8 +845,12 @@ export const useGeminiStream = ( userMessageTimestamp, ); } + // Only clear auto-retry countdown errors (those with active timer) + if (retryCountdownTimerRef.current) { + clearRetryCountdown(); + } }, - [addItem], + [addItem, clearRetryCountdown], ); const handleChatCompressionEvent = useCallback( @@ -853,7 +1009,16 @@ export const useGeminiStream = ( loopDetectedRef.current = true; break; case ServerGeminiEventType.Retry: - // Will add the missing logic later + // Clear any pending partial content from the failed attempt + if (pendingHistoryItemRef.current) { + setPendingHistoryItem(null); + } + // Show retry info if available (rate-limit / throttling errors) + if (event.retryInfo) { + startRetryCountdown(event.retryInfo); + } else if (!pendingRetryCountdownItemRef.current) { + clearRetryCountdown(); + } break; default: { // enforces exhaustive switch-case @@ -878,14 +1043,19 @@ export const useGeminiStream = ( handleMaxSessionTurnsEvent, handleSessionTokenLimitExceededEvent, handleCitationEvent, + startRetryCountdown, + clearRetryCountdown, setThought, + pendingHistoryItemRef, + setPendingHistoryItem, + pendingRetryCountdownItemRef, ], ); const submitQuery = useCallback( async ( query: PartListUnion, - options?: { isContinuation: boolean }, + options?: { isContinuation: boolean; skipPreparation?: boolean }, prompt_id?: string, ) => { // Prevent concurrent executions of submitQuery, but allow continuations @@ -909,7 +1079,11 @@ export const useGeminiStream = ( // Reset quota error flag when starting a new query (not a continuation) if (!options?.isContinuation) { setModelSwitchedFromQuotaError(false); - // No quota-error / fallback routing mechanism currently; keep state minimal. + // Commit any pending retry error to history (without hint) since the + // user is starting a new conversation turn + if (pendingRetryCountdownItemRef.current) { + clearRetryCountdown(); + } } abortControllerRef.current = new AbortController(); @@ -921,31 +1095,37 @@ export const useGeminiStream = ( } return promptIdContext.run(prompt_id, async () => { - const { queryToSend, shouldProceed } = await prepareQueryForGemini( - query, - userMessageTimestamp, - abortSignal, - prompt_id!, - ); + const { queryToSend, shouldProceed } = options?.skipPreparation + ? { queryToSend: query, shouldProceed: true } + : await prepareQueryForGemini( + query, + userMessageTimestamp, + abortSignal, + prompt_id!, + ); if (!shouldProceed || queryToSend === null) { isSubmittingQueryRef.current = false; return; } - // Handle vision switch requirement - const visionSwitchResult = await handleVisionSwitch( - queryToSend, - userMessageTimestamp, - options?.isContinuation || false, - ); - - if (!visionSwitchResult.shouldProceed) { - isSubmittingQueryRef.current = false; - return; + // Check image format support for non-continuations + if (!options?.isContinuation) { + const formatCheck = checkImageFormatsSupport(queryToSend); + if (formatCheck.hasUnsupportedFormats) { + addItem( + { + type: MessageType.INFO, + text: getUnsupportedImageFormatWarning(), + }, + userMessageTimestamp, + ); + } } const finalQueryToSend = queryToSend; + lastPromptRef.current = finalQueryToSend; + lastPromptErroredRef.current = false; if (!options?.isContinuation) { // trigger new prompt event for session stats in CLI @@ -986,10 +1166,6 @@ export const useGeminiStream = ( ); if (processingStatus === StreamProcessingStatus.UserCancelled) { - // Restore original model if it was temporarily overridden - restoreOriginalModel().catch((error) => { - debugLogger.error('Failed to restore original model:', error); - }); isSubmittingQueryRef.current = false; return; } @@ -998,34 +1174,31 @@ export const useGeminiStream = ( addItem(pendingHistoryItemRef.current, userMessageTimestamp); setPendingHistoryItem(null); } + // Only clear auto-retry countdown errors (those with an active timer). + // Do NOT clear static error+hint from handleErrorEvent — those should + // remain visible until the user presses Ctrl+Y to retry. + if (retryCountdownTimerRef.current) { + clearRetryCountdown(); + } if (loopDetectedRef.current) { loopDetectedRef.current = false; handleLoopDetectedEvent(); } - - // Restore original model if it was temporarily overridden - restoreOriginalModel().catch((error) => { - debugLogger.error('Failed to restore original model:', error); - }); } catch (error: unknown) { - // Restore original model if it was temporarily overridden - restoreOriginalModel().catch((error) => { - debugLogger.error('Failed to restore original model:', error); - }); - if (error instanceof UnauthorizedError) { onAuthError('Session expired or is unauthorized.'); } else if (!isNodeError(error) || error.name !== 'AbortError') { - addItem( - { - type: MessageType.ERROR, - text: parseAndFormatApiError( - getErrorMessage(error) || 'Unknown error', - config.getContentGeneratorConfig()?.authType, - ), - }, - userMessageTimestamp, - ); + lastPromptErroredRef.current = true; + const retryHint = t('Press Ctrl+Y to retry'); + // Store error with hint as a pending item (same as handleErrorEvent) + setPendingRetryErrorItem({ + type: 'error' as const, + text: parseAndFormatApiError( + getErrorMessage(error) || 'Unknown error', + config.getContentGeneratorConfig()?.authType, + ), + hint: retryHint, + }); } } finally { setIsResponding(false); @@ -1048,11 +1221,71 @@ export const useGeminiStream = ( startNewPrompt, getPromptCount, handleLoopDetectedEvent, - handleVisionSwitch, - restoreOriginalModel, + clearRetryCountdown, + pendingRetryCountdownItemRef, + setPendingRetryErrorItem, ], ); + /** + * Retries the last failed prompt when the user presses Ctrl+Y. + * + * Activation conditions for Ctrl+Y shortcut: + * 1. ✅ The last request must have failed (lastPromptErroredRef.current === true) + * 2. ✅ Current streaming state must NOT be "Responding" (avoid interrupting ongoing stream) + * 3. ✅ Current streaming state must NOT be "WaitingForConfirmation" (avoid conflicting with tool confirmation flow) + * 4. ✅ There must be a stored lastPrompt in lastPromptRef.current + * + * When conditions are not met: + * - If streaming is active (Responding/WaitingForConfirmation): silently return without action + * - If no failed request exists: display "No failed request to retry." info message + * + * When conditions are met: + * - Clears any pending auto-retry countdown to avoid duplicate retries + * - Re-submits the last query with skipPreparation: true for faster retry + * + * This function is exposed via UIActionsContext and triggered by InputPrompt + * when the user presses Ctrl+Y (bound to Command.RETRY_LAST in keyBindings.ts). + */ + const retryLastPrompt = useCallback(async () => { + if ( + streamingState === StreamingState.Responding || + streamingState === StreamingState.WaitingForConfirmation + ) { + return; + } + + const lastPrompt = lastPromptRef.current; + if (!lastPrompt || !lastPromptErroredRef.current) { + addItem( + { + type: MessageType.INFO, + text: t('No failed request to retry.'), + }, + Date.now(), + ); + return; + } + + // Commit the error to history (without hint) before clearing + const errorItem = pendingRetryErrorItemRef.current; + if (errorItem) { + addItem({ type: errorItem.type, text: errorItem.text }, Date.now()); + } + clearRetryCountdown(); + + await submitQuery(lastPrompt, { + isContinuation: false, + skipPreparation: true, + }); + }, [ + streamingState, + addItem, + clearRetryCountdown, + submitQuery, + pendingRetryErrorItemRef, + ]); + const handleApprovalModeChange = useCallback( async (newApprovalMode: ApprovalMode) => { // Auto-approve pending tool calls when switching to auto-approval modes @@ -1216,10 +1449,18 @@ export const useGeminiStream = ( const pendingHistoryItems = useMemo( () => - [pendingHistoryItem, pendingToolCallGroupDisplay].filter( - (i) => i !== undefined && i !== null, - ), - [pendingHistoryItem, pendingToolCallGroupDisplay], + [ + pendingHistoryItem, + pendingRetryErrorItem, + pendingRetryCountdownItem, + pendingToolCallGroupDisplay, + ].filter((i) => i !== undefined && i !== null), + [ + pendingHistoryItem, + pendingRetryErrorItem, + pendingRetryCountdownItem, + pendingToolCallGroupDisplay, + ], ); useEffect(() => { @@ -1348,6 +1589,7 @@ export const useGeminiStream = ( pendingHistoryItems, thought, cancelOngoingRequest, + retryLastPrompt, pendingToolCalls: toolCalls, handleApprovalModeChange, activePtyId, diff --git a/packages/cli/src/ui/hooks/useVisionAutoSwitch.test.ts b/packages/cli/src/ui/hooks/useVisionAutoSwitch.test.ts deleted file mode 100644 index 782986ce9..000000000 --- a/packages/cli/src/ui/hooks/useVisionAutoSwitch.test.ts +++ /dev/null @@ -1,874 +0,0 @@ -/** - * @license - * Copyright 2025 Qwen - * SPDX-License-Identifier: Apache-2.0 - */ - -/* eslint-disable @typescript-eslint/no-explicit-any */ -import { describe, it, expect, vi, beforeEach } from 'vitest'; -import { renderHook, act } from '@testing-library/react'; -import type { Part, PartListUnion } from '@google/genai'; -import { AuthType, type Config, ApprovalMode } from '@qwen-code/qwen-code-core'; - -// Mock the image format functions from core package -vi.mock('@qwen-code/qwen-code-core', async (importOriginal) => { - const actual = (await importOriginal()) as Record; - return { - ...actual, - isSupportedImageMimeType: vi.fn((mimeType: string) => - [ - 'image/png', - 'image/jpeg', - 'image/jpg', - 'image/gif', - 'image/webp', - ].includes(mimeType), - ), - getUnsupportedImageFormatWarning: vi.fn( - () => - 'Only the following image formats are supported: BMP, JPEG, JPG, PNG, TIFF, WEBP, HEIC. Other formats may not work as expected.', - ), - }; -}); -import { - shouldOfferVisionSwitch, - processVisionSwitchOutcome, - getVisionSwitchGuidanceMessage, - useVisionAutoSwitch, -} from './useVisionAutoSwitch.js'; -import { VisionSwitchOutcome } from '../components/ModelSwitchDialog.js'; -import { MessageType } from '../types.js'; -import { getDefaultVisionModel } from '../models/availableModels.js'; - -describe('useVisionAutoSwitch helpers', () => { - describe('shouldOfferVisionSwitch', () => { - it('returns false when authType is not QWEN_OAUTH', () => { - const parts: PartListUnion = [ - { inlineData: { mimeType: 'image/png', data: '...' } }, - ]; - const result = shouldOfferVisionSwitch( - parts, - AuthType.USE_GEMINI, - 'qwen3-coder-plus', - true, - ); - expect(result).toBe(false); - }); - - it('returns false when current model is already a vision model', () => { - const parts: PartListUnion = [ - { inlineData: { mimeType: 'image/png', data: '...' } }, - ]; - const result = shouldOfferVisionSwitch( - parts, - AuthType.QWEN_OAUTH, - 'vision-model', - true, - ); - expect(result).toBe(false); - }); - - it('returns true when image parts exist, QWEN_OAUTH, and model is not vision', () => { - const parts: PartListUnion = [ - { text: 'hello' }, - { inlineData: { mimeType: 'image/jpeg', data: '...' } }, - ]; - const result = shouldOfferVisionSwitch( - parts, - AuthType.QWEN_OAUTH, - 'qwen3-coder-plus', - true, - ); - expect(result).toBe(true); - }); - - it('detects image when provided as a single Part object (non-array)', () => { - const singleImagePart: PartListUnion = { - fileData: { mimeType: 'image/gif', fileUri: 'file://image.gif' }, - } as Part; - const result = shouldOfferVisionSwitch( - singleImagePart, - AuthType.QWEN_OAUTH, - 'qwen3-coder-plus', - true, - ); - expect(result).toBe(true); - }); - - it('returns false when parts contain no images', () => { - const parts: PartListUnion = [{ text: 'just text' }]; - const result = shouldOfferVisionSwitch( - parts, - AuthType.QWEN_OAUTH, - 'qwen3-coder-plus', - true, - ); - expect(result).toBe(false); - }); - - it('returns false when parts is a plain string', () => { - const parts: PartListUnion = 'plain text'; - const result = shouldOfferVisionSwitch( - parts, - AuthType.QWEN_OAUTH, - 'qwen3-coder-plus', - true, - ); - expect(result).toBe(false); - }); - - it('returns false when visionModelPreviewEnabled is false', () => { - const parts: PartListUnion = [ - { inlineData: { mimeType: 'image/png', data: '...' } }, - ]; - const result = shouldOfferVisionSwitch( - parts, - AuthType.QWEN_OAUTH, - 'qwen3-coder-plus', - false, - ); - expect(result).toBe(false); - }); - - it('returns true when image parts exist in YOLO mode context', () => { - const parts: PartListUnion = [ - { inlineData: { mimeType: 'image/png', data: '...' } }, - ]; - const result = shouldOfferVisionSwitch( - parts, - AuthType.QWEN_OAUTH, - 'qwen3-coder-plus', - true, - ); - expect(result).toBe(true); - }); - - it('returns false when no image parts exist in YOLO mode context', () => { - const parts: PartListUnion = [{ text: 'just text' }]; - const result = shouldOfferVisionSwitch( - parts, - AuthType.QWEN_OAUTH, - 'qwen3-coder-plus', - true, - ); - expect(result).toBe(false); - }); - - it('returns false when already using vision model in YOLO mode context', () => { - const parts: PartListUnion = [ - { inlineData: { mimeType: 'image/png', data: '...' } }, - ]; - const result = shouldOfferVisionSwitch( - parts, - AuthType.QWEN_OAUTH, - 'vision-model', - true, - ); - expect(result).toBe(false); - }); - - it('returns false when authType is not QWEN_OAUTH in YOLO mode context', () => { - const parts: PartListUnion = [ - { inlineData: { mimeType: 'image/png', data: '...' } }, - ]; - const result = shouldOfferVisionSwitch( - parts, - AuthType.USE_GEMINI, - 'qwen3-coder-plus', - true, - ); - expect(result).toBe(false); - }); - }); - - describe('processVisionSwitchOutcome', () => { - it('maps SwitchOnce to a one-time model override', () => { - const vl = getDefaultVisionModel(); - const result = processVisionSwitchOutcome(VisionSwitchOutcome.SwitchOnce); - expect(result).toEqual({ modelOverride: vl }); - }); - - it('maps SwitchSessionToVL to a persistent session model', () => { - const vl = getDefaultVisionModel(); - const result = processVisionSwitchOutcome( - VisionSwitchOutcome.SwitchSessionToVL, - ); - expect(result).toEqual({ persistSessionModel: vl }); - }); - - it('maps ContinueWithCurrentModel to empty result', () => { - const result = processVisionSwitchOutcome( - VisionSwitchOutcome.ContinueWithCurrentModel, - ); - expect(result).toEqual({}); - }); - }); - - describe('getVisionSwitchGuidanceMessage', () => { - it('returns the expected guidance message', () => { - const vl = getDefaultVisionModel(); - const expected = - 'To use images with your query, you can:\n' + - `• Use /model set ${vl} to switch to a vision-capable model\n` + - '• Or remove the image and provide a text description instead'; - expect(getVisionSwitchGuidanceMessage()).toBe(expected); - }); - }); -}); - -describe('useVisionAutoSwitch hook', () => { - type AddItemFn = ( - item: { type: MessageType; text: string }, - ts: number, - ) => any; - - const createMockConfig = ( - authType: AuthType, - initialModel: string, - approvalMode: ApprovalMode = ApprovalMode.DEFAULT, - vlmSwitchMode?: string, - ) => { - let currentModel = initialModel; - const mockConfig: Partial = { - getModel: vi.fn(() => currentModel), - setModel: vi.fn(async (m: string) => { - currentModel = m; - }), - getApprovalMode: vi.fn(() => approvalMode), - getVlmSwitchMode: vi.fn(() => vlmSwitchMode), - getContentGeneratorConfig: vi.fn(() => ({ - authType, - model: currentModel, - apiKey: 'test-key', - vertexai: false, - })), - }; - return mockConfig as Config; - }; - - let addItem: AddItemFn; - - beforeEach(() => { - vi.clearAllMocks(); - addItem = vi.fn(); - }); - - it('returns shouldProceed=true immediately for continuations', async () => { - const config = createMockConfig(AuthType.QWEN_OAUTH, 'qwen3-coder-plus'); - const { result } = renderHook(() => - useVisionAutoSwitch(config, addItem as any, true, vi.fn()), - ); - - const parts: PartListUnion = [ - { inlineData: { mimeType: 'image/png', data: '...' } }, - ]; - let res: any; - await act(async () => { - res = await result.current.handleVisionSwitch(parts, Date.now(), true); - }); - expect(res).toEqual({ shouldProceed: true }); - expect(addItem).not.toHaveBeenCalled(); - }); - - it('does nothing when authType is not QWEN_OAUTH', async () => { - const config = createMockConfig(AuthType.USE_GEMINI, 'qwen3-coder-plus'); - const onVisionSwitchRequired = vi.fn(); - const { result } = renderHook(() => - useVisionAutoSwitch(config, addItem as any, true, onVisionSwitchRequired), - ); - - const parts: PartListUnion = [ - { inlineData: { mimeType: 'image/png', data: '...' } }, - ]; - let res: any; - await act(async () => { - res = await result.current.handleVisionSwitch(parts, 123, false); - }); - expect(res).toEqual({ shouldProceed: true }); - expect(onVisionSwitchRequired).not.toHaveBeenCalled(); - }); - - it('does nothing when there are no image parts', async () => { - const config = createMockConfig(AuthType.QWEN_OAUTH, 'qwen3-coder-plus'); - const onVisionSwitchRequired = vi.fn(); - const { result } = renderHook(() => - useVisionAutoSwitch(config, addItem as any, true, onVisionSwitchRequired), - ); - - const parts: PartListUnion = [{ text: 'no images here' }]; - let res: any; - await act(async () => { - res = await result.current.handleVisionSwitch(parts, 456, false); - }); - expect(res).toEqual({ shouldProceed: true }); - expect(onVisionSwitchRequired).not.toHaveBeenCalled(); - }); - - it('continues with current model when dialog returns empty result', async () => { - const config = createMockConfig(AuthType.QWEN_OAUTH, 'qwen3-coder-plus'); - const onVisionSwitchRequired = vi.fn().mockResolvedValue({}); // Empty result for ContinueWithCurrentModel - const { result } = renderHook(() => - useVisionAutoSwitch(config, addItem as any, true, onVisionSwitchRequired), - ); - - const parts: PartListUnion = [ - { inlineData: { mimeType: 'image/png', data: '...' } }, - ]; - - const userTs = 1010; - let res: any; - await act(async () => { - res = await result.current.handleVisionSwitch(parts, userTs, false); - }); - - // Should not add any guidance message - expect(addItem).not.toHaveBeenCalledWith( - { type: MessageType.INFO, text: getVisionSwitchGuidanceMessage() }, - userTs, - ); - expect(res).toEqual({ shouldProceed: true }); - expect(config.setModel).not.toHaveBeenCalled(); - }); - - it('applies a one-time override and returns originalModel, then restores', async () => { - const initialModel = 'qwen3-coder-plus'; - const config = createMockConfig(AuthType.QWEN_OAUTH, initialModel); - const onVisionSwitchRequired = vi - .fn() - .mockResolvedValue({ modelOverride: 'coder-model' }); - const { result } = renderHook(() => - useVisionAutoSwitch(config, addItem as any, true, onVisionSwitchRequired), - ); - - const parts: PartListUnion = [ - { inlineData: { mimeType: 'image/png', data: '...' } }, - ]; - - let res: any; - await act(async () => { - res = await result.current.handleVisionSwitch(parts, 2020, false); - }); - - expect(res).toEqual({ shouldProceed: true, originalModel: initialModel }); - expect(config.setModel).toHaveBeenCalledWith('coder-model', { - reason: 'vision_auto_switch', - context: 'User-prompted vision switch (one-time override)', - }); - - // Now restore - await act(async () => { - await result.current.restoreOriginalModel(); - }); - expect(config.setModel).toHaveBeenLastCalledWith(initialModel, { - reason: 'vision_auto_switch', - context: 'Restoring original model after vision switch', - }); - }); - - it('persists session model when dialog requests persistence', async () => { - const config = createMockConfig(AuthType.QWEN_OAUTH, 'qwen3-coder-plus'); - const onVisionSwitchRequired = vi - .fn() - .mockResolvedValue({ persistSessionModel: 'coder-model' }); - const { result } = renderHook(() => - useVisionAutoSwitch(config, addItem as any, true, onVisionSwitchRequired), - ); - - const parts: PartListUnion = [ - { inlineData: { mimeType: 'image/png', data: '...' } }, - ]; - - let res: any; - await act(async () => { - res = await result.current.handleVisionSwitch(parts, 3030, false); - }); - - expect(res).toEqual({ shouldProceed: true }); - expect(config.setModel).toHaveBeenCalledWith('coder-model', { - reason: 'vision_auto_switch', - context: 'User-prompted vision switch (session persistent)', - }); - - // Restore should be a no-op since no one-time override was used - await act(async () => { - await result.current.restoreOriginalModel(); - }); - // Last call should still be the persisted model set - expect((config.setModel as any).mock.calls.pop()?.[0]).toBe('coder-model'); - }); - - it('returns shouldProceed=true when dialog returns no special flags', async () => { - const config = createMockConfig(AuthType.QWEN_OAUTH, 'qwen3-coder-plus'); - const onVisionSwitchRequired = vi.fn().mockResolvedValue({}); - const { result } = renderHook(() => - useVisionAutoSwitch(config, addItem as any, true, onVisionSwitchRequired), - ); - - const parts: PartListUnion = [ - { inlineData: { mimeType: 'image/png', data: '...' } }, - ]; - let res: any; - await act(async () => { - res = await result.current.handleVisionSwitch(parts, 4040, false); - }); - expect(res).toEqual({ shouldProceed: true }); - expect(config.setModel).not.toHaveBeenCalled(); - }); - - it('blocks when dialog throws or is cancelled', async () => { - const config = createMockConfig(AuthType.QWEN_OAUTH, 'qwen3-coder-plus'); - const onVisionSwitchRequired = vi.fn().mockRejectedValue(new Error('x')); - const { result } = renderHook(() => - useVisionAutoSwitch(config, addItem as any, true, onVisionSwitchRequired), - ); - - const parts: PartListUnion = [ - { inlineData: { mimeType: 'image/png', data: '...' } }, - ]; - let res: any; - await act(async () => { - res = await result.current.handleVisionSwitch(parts, 5050, false); - }); - expect(res).toEqual({ shouldProceed: false }); - expect(config.setModel).not.toHaveBeenCalled(); - }); - - it('does nothing when visionModelPreviewEnabled is false', async () => { - const config = createMockConfig(AuthType.QWEN_OAUTH, 'qwen3-coder-plus'); - const onVisionSwitchRequired = vi.fn(); - const { result } = renderHook(() => - useVisionAutoSwitch( - config, - addItem as any, - false, - onVisionSwitchRequired, - ), - ); - - const parts: PartListUnion = [ - { inlineData: { mimeType: 'image/png', data: '...' } }, - ]; - let res: any; - await act(async () => { - res = await result.current.handleVisionSwitch(parts, 6060, false); - }); - expect(res).toEqual({ shouldProceed: true }); - expect(onVisionSwitchRequired).not.toHaveBeenCalled(); - }); - - describe('YOLO mode behavior', () => { - it('automatically switches to vision model in YOLO mode without showing dialog', async () => { - const initialModel = 'qwen3-coder-plus'; - const config = createMockConfig( - AuthType.QWEN_OAUTH, - initialModel, - ApprovalMode.YOLO, - ); - const onVisionSwitchRequired = vi.fn(); // Should not be called in YOLO mode - const { result } = renderHook(() => - useVisionAutoSwitch( - config, - addItem as any, - true, - onVisionSwitchRequired, - ), - ); - - const parts: PartListUnion = [ - { inlineData: { mimeType: 'image/png', data: '...' } }, - ]; - - let res: any; - await act(async () => { - res = await result.current.handleVisionSwitch(parts, 7070, false); - }); - - // Should automatically switch without calling the dialog - expect(onVisionSwitchRequired).not.toHaveBeenCalled(); - expect(res).toEqual({ - shouldProceed: true, - originalModel: initialModel, - }); - expect(config.setModel).toHaveBeenCalledWith(getDefaultVisionModel(), { - reason: 'vision_auto_switch', - context: 'YOLO mode auto-switch for image content', - }); - }); - - it('does not switch in YOLO mode when no images are present', async () => { - const config = createMockConfig( - AuthType.QWEN_OAUTH, - 'qwen3-coder-plus', - ApprovalMode.YOLO, - ); - const onVisionSwitchRequired = vi.fn(); - const { result } = renderHook(() => - useVisionAutoSwitch( - config, - addItem as any, - true, - onVisionSwitchRequired, - ), - ); - - const parts: PartListUnion = [{ text: 'no images here' }]; - - let res: any; - await act(async () => { - res = await result.current.handleVisionSwitch(parts, 8080, false); - }); - - expect(res).toEqual({ shouldProceed: true }); - expect(onVisionSwitchRequired).not.toHaveBeenCalled(); - expect(config.setModel).not.toHaveBeenCalled(); - }); - - it('does not switch in YOLO mode when already using vision model', async () => { - const config = createMockConfig( - AuthType.QWEN_OAUTH, - 'vision-model', - ApprovalMode.YOLO, - ); - const onVisionSwitchRequired = vi.fn(); - const { result } = renderHook(() => - useVisionAutoSwitch( - config, - addItem as any, - true, - onVisionSwitchRequired, - ), - ); - - const parts: PartListUnion = [ - { inlineData: { mimeType: 'image/png', data: '...' } }, - ]; - - let res: any; - await act(async () => { - res = await result.current.handleVisionSwitch(parts, 9090, false); - }); - - expect(res).toEqual({ shouldProceed: true }); - expect(onVisionSwitchRequired).not.toHaveBeenCalled(); - expect(config.setModel).not.toHaveBeenCalled(); - }); - - it('restores original model after YOLO mode auto-switch', async () => { - const initialModel = 'qwen3-coder-plus'; - const config = createMockConfig( - AuthType.QWEN_OAUTH, - initialModel, - ApprovalMode.YOLO, - ); - const onVisionSwitchRequired = vi.fn(); - const { result } = renderHook(() => - useVisionAutoSwitch( - config, - addItem as any, - true, - onVisionSwitchRequired, - ), - ); - - const parts: PartListUnion = [ - { inlineData: { mimeType: 'image/png', data: '...' } }, - ]; - - // First, trigger the auto-switch - await act(async () => { - await result.current.handleVisionSwitch(parts, 10100, false); - }); - - // Verify model was switched - expect(config.setModel).toHaveBeenCalledWith(getDefaultVisionModel(), { - reason: 'vision_auto_switch', - context: 'YOLO mode auto-switch for image content', - }); - - // Now restore the original model - await act(async () => { - await result.current.restoreOriginalModel(); - }); - - // Verify model was restored - expect(config.setModel).toHaveBeenLastCalledWith(initialModel, { - reason: 'vision_auto_switch', - context: 'Restoring original model after vision switch', - }); - }); - - it('does not switch in YOLO mode when authType is not QWEN_OAUTH', async () => { - const config = createMockConfig( - AuthType.USE_GEMINI, - 'qwen3-coder-plus', - ApprovalMode.YOLO, - ); - const onVisionSwitchRequired = vi.fn(); - const { result } = renderHook(() => - useVisionAutoSwitch( - config, - addItem as any, - true, - onVisionSwitchRequired, - ), - ); - - const parts: PartListUnion = [ - { inlineData: { mimeType: 'image/png', data: '...' } }, - ]; - - let res: any; - await act(async () => { - res = await result.current.handleVisionSwitch(parts, 11110, false); - }); - - expect(res).toEqual({ shouldProceed: true }); - expect(onVisionSwitchRequired).not.toHaveBeenCalled(); - expect(config.setModel).not.toHaveBeenCalled(); - }); - - it('does not switch in YOLO mode when visionModelPreviewEnabled is false', async () => { - const config = createMockConfig( - AuthType.QWEN_OAUTH, - 'qwen3-coder-plus', - ApprovalMode.YOLO, - ); - const onVisionSwitchRequired = vi.fn(); - const { result } = renderHook(() => - useVisionAutoSwitch( - config, - addItem as any, - false, - onVisionSwitchRequired, - ), - ); - - const parts: PartListUnion = [ - { inlineData: { mimeType: 'image/png', data: '...' } }, - ]; - - let res: any; - await act(async () => { - res = await result.current.handleVisionSwitch(parts, 12120, false); - }); - - expect(res).toEqual({ shouldProceed: true }); - expect(onVisionSwitchRequired).not.toHaveBeenCalled(); - expect(config.setModel).not.toHaveBeenCalled(); - }); - - it('handles multiple image formats in YOLO mode', async () => { - const initialModel = 'qwen3-coder-plus'; - const config = createMockConfig( - AuthType.QWEN_OAUTH, - initialModel, - ApprovalMode.YOLO, - ); - const onVisionSwitchRequired = vi.fn(); - const { result } = renderHook(() => - useVisionAutoSwitch( - config, - addItem as any, - true, - onVisionSwitchRequired, - ), - ); - - const parts: PartListUnion = [ - { text: 'Here are some images:' }, - { inlineData: { mimeType: 'image/jpeg', data: '...' } }, - { fileData: { mimeType: 'image/png', fileUri: 'file://image.png' } }, - { text: 'Please analyze them.' }, - ]; - - let res: any; - await act(async () => { - res = await result.current.handleVisionSwitch(parts, 13130, false); - }); - - expect(res).toEqual({ - shouldProceed: true, - originalModel: initialModel, - }); - expect(config.setModel).toHaveBeenCalledWith(getDefaultVisionModel(), { - reason: 'vision_auto_switch', - context: 'YOLO mode auto-switch for image content', - }); - expect(onVisionSwitchRequired).not.toHaveBeenCalled(); - }); - }); - - describe('VLM switch mode default behavior', () => { - it('should automatically switch once when vlmSwitchMode is "once"', async () => { - const config = createMockConfig( - AuthType.QWEN_OAUTH, - 'qwen3-coder-plus', - ApprovalMode.DEFAULT, - 'once', - ); - const onVisionSwitchRequired = vi.fn(); // Should not be called - const { result } = renderHook(() => - useVisionAutoSwitch( - config, - addItem as any, - true, - onVisionSwitchRequired, - ), - ); - - const parts: PartListUnion = [ - { inlineData: { mimeType: 'image/jpeg', data: 'base64data' } }, - ]; - - const switchResult = await result.current.handleVisionSwitch( - parts, - Date.now(), - false, - ); - - expect(switchResult.shouldProceed).toBe(true); - expect(switchResult.originalModel).toBe('qwen3-coder-plus'); - expect(config.setModel).toHaveBeenCalledWith('vision-model', { - reason: 'vision_auto_switch', - context: 'Default VLM switch mode: once (one-time override)', - }); - expect(onVisionSwitchRequired).not.toHaveBeenCalled(); - }); - - it('should switch session when vlmSwitchMode is "session"', async () => { - const config = createMockConfig( - AuthType.QWEN_OAUTH, - 'qwen3-coder-plus', - ApprovalMode.DEFAULT, - 'session', - ); - const onVisionSwitchRequired = vi.fn(); // Should not be called - const { result } = renderHook(() => - useVisionAutoSwitch( - config, - addItem as any, - true, - onVisionSwitchRequired, - ), - ); - - const parts: PartListUnion = [ - { inlineData: { mimeType: 'image/jpeg', data: 'base64data' } }, - ]; - - const switchResult = await result.current.handleVisionSwitch( - parts, - Date.now(), - false, - ); - - expect(switchResult.shouldProceed).toBe(true); - expect(switchResult.originalModel).toBeUndefined(); // No original model for session switch - expect(config.setModel).toHaveBeenCalledWith('vision-model', { - reason: 'vision_auto_switch', - context: 'Default VLM switch mode: session (session persistent)', - }); - expect(onVisionSwitchRequired).not.toHaveBeenCalled(); - }); - - it('should continue with current model when vlmSwitchMode is "persist"', async () => { - const config = createMockConfig( - AuthType.QWEN_OAUTH, - 'qwen3-coder-plus', - ApprovalMode.DEFAULT, - 'persist', - ); - const onVisionSwitchRequired = vi.fn(); // Should not be called - const { result } = renderHook(() => - useVisionAutoSwitch( - config, - addItem as any, - true, - onVisionSwitchRequired, - ), - ); - - const parts: PartListUnion = [ - { inlineData: { mimeType: 'image/jpeg', data: 'base64data' } }, - ]; - - const switchResult = await result.current.handleVisionSwitch( - parts, - Date.now(), - false, - ); - - expect(switchResult.shouldProceed).toBe(true); - expect(switchResult.originalModel).toBeUndefined(); - expect(config.setModel).not.toHaveBeenCalled(); - expect(onVisionSwitchRequired).not.toHaveBeenCalled(); - }); - - it('should fall back to user prompt when vlmSwitchMode is not set', async () => { - const config = createMockConfig( - AuthType.QWEN_OAUTH, - 'qwen3-coder-plus', - ApprovalMode.DEFAULT, - undefined, // No default mode - ); - const onVisionSwitchRequired = vi - .fn() - .mockResolvedValue({ modelOverride: 'vision-model' }); - const { result } = renderHook(() => - useVisionAutoSwitch( - config, - addItem as any, - true, - onVisionSwitchRequired, - ), - ); - - const parts: PartListUnion = [ - { inlineData: { mimeType: 'image/jpeg', data: 'base64data' } }, - ]; - - const switchResult = await result.current.handleVisionSwitch( - parts, - Date.now(), - false, - ); - - expect(switchResult.shouldProceed).toBe(true); - expect(onVisionSwitchRequired).toHaveBeenCalledWith(parts); - }); - - it('should fall back to persist behavior when vlmSwitchMode has invalid value', async () => { - const config = createMockConfig( - AuthType.QWEN_OAUTH, - 'qwen3-coder-plus', - ApprovalMode.DEFAULT, - 'invalid-value', - ); - const onVisionSwitchRequired = vi.fn(); // Should not be called - const { result } = renderHook(() => - useVisionAutoSwitch( - config, - addItem as any, - true, - onVisionSwitchRequired, - ), - ); - - const parts: PartListUnion = [ - { inlineData: { mimeType: 'image/jpeg', data: 'base64data' } }, - ]; - - const switchResult = await result.current.handleVisionSwitch( - parts, - Date.now(), - false, - ); - - expect(switchResult.shouldProceed).toBe(true); - expect(switchResult.originalModel).toBeUndefined(); - // For invalid values, it should continue with current model (persist behavior) - expect(config.setModel).not.toHaveBeenCalled(); - expect(onVisionSwitchRequired).not.toHaveBeenCalled(); - }); - }); -}); diff --git a/packages/cli/src/ui/hooks/useVisionAutoSwitch.ts b/packages/cli/src/ui/hooks/useVisionAutoSwitch.ts deleted file mode 100644 index f489c843a..000000000 --- a/packages/cli/src/ui/hooks/useVisionAutoSwitch.ts +++ /dev/null @@ -1,363 +0,0 @@ -/** - * @license - * Copyright 2025 Qwen - * SPDX-License-Identifier: Apache-2.0 - */ - -import { type PartListUnion, type Part } from '@google/genai'; -import { AuthType, type Config, ApprovalMode } from '@qwen-code/qwen-code-core'; -import { useCallback, useRef } from 'react'; -import { VisionSwitchOutcome } from '../components/ModelSwitchDialog.js'; -import { - getDefaultVisionModel, - isVisionModel, -} from '../models/availableModels.js'; -import { MessageType } from '../types.js'; -import type { UseHistoryManagerReturn } from './useHistoryManager.js'; -import { - isSupportedImageMimeType, - getUnsupportedImageFormatWarning, -} from '@qwen-code/qwen-code-core'; - -/** - * Checks if a PartListUnion contains image parts - */ -function hasImageParts(parts: PartListUnion): boolean { - if (typeof parts === 'string') { - return false; - } - - if (Array.isArray(parts)) { - return parts.some((part) => { - // Skip string parts - if (typeof part === 'string') return false; - return isImagePart(part); - }); - } - - // If it's a single Part (not a string), check if it's an image - if (typeof parts === 'object') { - return isImagePart(parts); - } - - return false; -} - -/** - * Checks if a single Part is an image part - */ -function isImagePart(part: Part): boolean { - // Check for inlineData with image mime type - if ('inlineData' in part && part.inlineData?.mimeType?.startsWith('image/')) { - return true; - } - - // Check for fileData with image mime type - if ('fileData' in part && part.fileData?.mimeType?.startsWith('image/')) { - return true; - } - - return false; -} - -/** - * Checks if image parts have supported formats and returns unsupported ones - */ -function checkImageFormatsSupport(parts: PartListUnion): { - hasImages: boolean; - hasUnsupportedFormats: boolean; - unsupportedMimeTypes: string[]; -} { - const unsupportedMimeTypes: string[] = []; - let hasImages = false; - - if (typeof parts === 'string') { - return { - hasImages: false, - hasUnsupportedFormats: false, - unsupportedMimeTypes: [], - }; - } - - const partsArray = Array.isArray(parts) ? parts : [parts]; - - for (const part of partsArray) { - if (typeof part === 'string') continue; - - let mimeType: string | undefined; - - // Check inlineData - if ( - 'inlineData' in part && - part.inlineData?.mimeType?.startsWith('image/') - ) { - hasImages = true; - mimeType = part.inlineData.mimeType; - } - - // Check fileData - if ('fileData' in part && part.fileData?.mimeType?.startsWith('image/')) { - hasImages = true; - mimeType = part.fileData.mimeType; - } - - // Check if the mime type is supported - if (mimeType && !isSupportedImageMimeType(mimeType)) { - unsupportedMimeTypes.push(mimeType); - } - } - - return { - hasImages, - hasUnsupportedFormats: unsupportedMimeTypes.length > 0, - unsupportedMimeTypes, - }; -} - -/** - * Determines if we should offer vision switch for the given parts, auth type, and current model - */ -export function shouldOfferVisionSwitch( - parts: PartListUnion, - authType: AuthType, - currentModel: string, - visionModelPreviewEnabled: boolean = true, -): boolean { - // Only trigger for qwen-oauth - if (authType !== AuthType.QWEN_OAUTH) { - return false; - } - - // If vision model preview is disabled, never offer vision switch - if (!visionModelPreviewEnabled) { - return false; - } - - // If current model is already a vision model, no need to switch - if (isVisionModel(currentModel)) { - return false; - } - - // Check if the current message contains image parts - return hasImageParts(parts); -} - -/** - * Interface for vision switch result - */ -export interface VisionSwitchResult { - modelOverride?: string; - persistSessionModel?: string; - showGuidance?: boolean; -} - -/** - * Processes the vision switch outcome and returns the appropriate result - */ -export function processVisionSwitchOutcome( - outcome: VisionSwitchOutcome, -): VisionSwitchResult { - const vlModelId = getDefaultVisionModel(); - - switch (outcome) { - case VisionSwitchOutcome.SwitchOnce: - return { modelOverride: vlModelId }; - - case VisionSwitchOutcome.SwitchSessionToVL: - return { persistSessionModel: vlModelId }; - - case VisionSwitchOutcome.ContinueWithCurrentModel: - return {}; // Continue with current model, no changes needed - - default: - return {}; // Default to continuing with current model - } -} - -/** - * Gets the guidance message for when vision switch is disallowed - */ -export function getVisionSwitchGuidanceMessage(): string { - const vlModelId = getDefaultVisionModel(); - return `To use images with your query, you can: -• Use /model set ${vlModelId} to switch to a vision-capable model -• Or remove the image and provide a text description instead`; -} - -/** - * Interface for vision switch handling result - */ -export interface VisionSwitchHandlingResult { - shouldProceed: boolean; - originalModel?: string; -} - -/** - * Custom hook for handling vision model auto-switching - */ -export function useVisionAutoSwitch( - config: Config, - addItem: UseHistoryManagerReturn['addItem'], - visionModelPreviewEnabled: boolean = true, - onVisionSwitchRequired?: (query: PartListUnion) => Promise<{ - modelOverride?: string; - persistSessionModel?: string; - showGuidance?: boolean; - }>, -) { - const originalModelRef = useRef(null); - - const handleVisionSwitch = useCallback( - async ( - query: PartListUnion, - userMessageTimestamp: number, - isContinuation: boolean, - ): Promise => { - // Skip vision switch handling for continuations or if no handler provided - if (isContinuation || !onVisionSwitchRequired) { - return { shouldProceed: true }; - } - - const contentGeneratorConfig = config.getContentGeneratorConfig(); - - // Only handle qwen-oauth auth type - if (contentGeneratorConfig?.authType !== AuthType.QWEN_OAUTH) { - return { shouldProceed: true }; - } - - // Check image format support first - const formatCheck = checkImageFormatsSupport(query); - - // If there are unsupported image formats, show warning - if (formatCheck.hasUnsupportedFormats) { - addItem( - { - type: MessageType.INFO, - text: getUnsupportedImageFormatWarning(), - }, - userMessageTimestamp, - ); - // Continue processing but with warning shown - } - - // Check if vision switch is needed - if ( - !shouldOfferVisionSwitch( - query, - contentGeneratorConfig.authType, - config.getModel(), - visionModelPreviewEnabled, - ) - ) { - return { shouldProceed: true }; - } - - // In YOLO mode, automatically switch to vision model without user interaction - if (config.getApprovalMode() === ApprovalMode.YOLO) { - const vlModelId = getDefaultVisionModel(); - originalModelRef.current = config.getModel(); - await config.setModel(vlModelId, { - reason: 'vision_auto_switch', - context: 'YOLO mode auto-switch for image content', - }); - return { - shouldProceed: true, - originalModel: originalModelRef.current, - }; - } - - // Check if there's a default VLM switch mode configured - const defaultVlmSwitchMode = config.getVlmSwitchMode(); - if (defaultVlmSwitchMode) { - // Convert string value to VisionSwitchOutcome enum - let outcome: VisionSwitchOutcome; - switch (defaultVlmSwitchMode) { - case 'once': - outcome = VisionSwitchOutcome.SwitchOnce; - break; - case 'session': - outcome = VisionSwitchOutcome.SwitchSessionToVL; - break; - case 'persist': - outcome = VisionSwitchOutcome.ContinueWithCurrentModel; - break; - default: - // Invalid value, fall back to prompting user - outcome = VisionSwitchOutcome.ContinueWithCurrentModel; - } - - // Process the default outcome - const visionSwitchResult = processVisionSwitchOutcome(outcome); - - if (visionSwitchResult.modelOverride) { - // One-time model override - originalModelRef.current = config.getModel(); - await config.setModel(visionSwitchResult.modelOverride, { - reason: 'vision_auto_switch', - context: `Default VLM switch mode: ${defaultVlmSwitchMode} (one-time override)`, - }); - return { - shouldProceed: true, - originalModel: originalModelRef.current, - }; - } else if (visionSwitchResult.persistSessionModel) { - // Persistent session model change - await config.setModel(visionSwitchResult.persistSessionModel, { - reason: 'vision_auto_switch', - context: `Default VLM switch mode: ${defaultVlmSwitchMode} (session persistent)`, - }); - return { shouldProceed: true }; - } - - // For ContinueWithCurrentModel or any other case, proceed with current model - return { shouldProceed: true }; - } - - try { - const visionSwitchResult = await onVisionSwitchRequired(query); - - if (visionSwitchResult.modelOverride) { - // One-time model override - originalModelRef.current = config.getModel(); - await config.setModel(visionSwitchResult.modelOverride, { - reason: 'vision_auto_switch', - context: 'User-prompted vision switch (one-time override)', - }); - return { - shouldProceed: true, - originalModel: originalModelRef.current, - }; - } else if (visionSwitchResult.persistSessionModel) { - // Persistent session model change - await config.setModel(visionSwitchResult.persistSessionModel, { - reason: 'vision_auto_switch', - context: 'User-prompted vision switch (session persistent)', - }); - return { shouldProceed: true }; - } - - // For ContinueWithCurrentModel or any other case, proceed with current model - return { shouldProceed: true }; - } catch (_error) { - // If vision switch dialog was cancelled or errored, don't proceed - return { shouldProceed: false }; - } - }, - [config, addItem, visionModelPreviewEnabled, onVisionSwitchRequired], - ); - - const restoreOriginalModel = useCallback(async () => { - if (originalModelRef.current) { - await config.setModel(originalModelRef.current, { - reason: 'vision_auto_switch', - context: 'Restoring original model after vision switch', - }); - originalModelRef.current = null; - } - }, [config]); - - return { - handleVisionSwitch, - restoreOriginalModel, - }; -} diff --git a/packages/cli/src/ui/hooks/vim.ts b/packages/cli/src/ui/hooks/vim.ts index 5a91a35a2..da5745959 100644 --- a/packages/cli/src/ui/hooks/vim.ts +++ b/packages/cli/src/ui/hooks/vim.ts @@ -269,8 +269,11 @@ export function useVim(buffer: TextBuffer, onSubmit?: (value: string) => void) { return false; // Let InputPrompt handle completion } - // Let InputPrompt handle Ctrl+V for clipboard image pasting - if (normalizedKey.ctrl && normalizedKey.name === 'v') { + // Let InputPrompt handle Ctrl+V or Cmd+V for clipboard image pasting + if ( + (normalizedKey.ctrl || normalizedKey.meta) && + normalizedKey.name === 'v' + ) { return false; // Let InputPrompt handle clipboard functionality } diff --git a/packages/cli/src/ui/keyMatchers.test.ts b/packages/cli/src/ui/keyMatchers.test.ts index 7ca67117c..8961f9ff7 100644 --- a/packages/cli/src/ui/keyMatchers.test.ts +++ b/packages/cli/src/ui/keyMatchers.test.ts @@ -11,6 +11,7 @@ import { defaultKeyBindings } from '../config/keyBindings.js'; import type { Key } from './hooks/useKeypress.js'; describe('keyMatchers', () => { + const isWindows = process.platform === 'win32'; const createKey = (name: string, mods: Partial = {}): Key => ({ name, ctrl: false, @@ -49,7 +50,8 @@ describe('keyMatchers', () => { key.name === 'return' && (key.ctrl || key.meta || key.paste), [Command.OPEN_EXTERNAL_EDITOR]: (key: Key) => key.ctrl && (key.name === 'x' || key.sequence === '\x18'), - [Command.PASTE_CLIPBOARD_IMAGE]: (key: Key) => key.ctrl && key.name === 'v', + [Command.PASTE_CLIPBOARD_IMAGE]: (key: Key) => + (isWindows ? key.meta : key.ctrl || key.meta) && key.name === 'v', [Command.TOGGLE_TOOL_DESCRIPTIONS]: (key: Key) => key.ctrl && key.name === 't', [Command.TOGGLE_IDE_CONTEXT_DETAIL]: (key: Key) => @@ -57,6 +59,7 @@ describe('keyMatchers', () => { [Command.QUIT]: (key: Key) => key.ctrl && key.name === 'c', [Command.EXIT]: (key: Key) => key.ctrl && key.name === 'd', [Command.SHOW_MORE_LINES]: (key: Key) => key.ctrl && key.name === 's', + [Command.RETRY_LAST]: (key: Key) => key.ctrl && key.name === 'y', [Command.REVERSE_SEARCH]: (key: Key) => key.ctrl && key.name === 'r', [Command.SUBMIT_REVERSE_SEARCH]: (key: Key) => key.name === 'return' && !key.ctrl, @@ -216,8 +219,12 @@ describe('keyMatchers', () => { }, { command: Command.PASTE_CLIPBOARD_IMAGE, - positive: [createKey('v', { ctrl: true })], - negative: [createKey('v'), createKey('c', { ctrl: true })], + positive: isWindows + ? [createKey('v', { meta: true })] + : [createKey('v', { ctrl: true }), createKey('v', { meta: true })], + negative: isWindows + ? [createKey('v', { ctrl: true }), createKey('v')] + : [createKey('v'), createKey('c', { ctrl: true })], }, // App level bindings @@ -246,6 +253,11 @@ describe('keyMatchers', () => { positive: [createKey('s', { ctrl: true })], negative: [createKey('s'), createKey('l', { ctrl: true })], }, + { + command: Command.RETRY_LAST, + positive: [createKey('y', { ctrl: true })], + negative: [createKey('y'), createKey('r', { ctrl: true })], + }, // Shell commands { diff --git a/packages/cli/src/ui/keyMatchers.ts b/packages/cli/src/ui/keyMatchers.ts index 103c57100..0b47bb678 100644 --- a/packages/cli/src/ui/keyMatchers.ts +++ b/packages/cli/src/ui/keyMatchers.ts @@ -50,6 +50,10 @@ function matchKeyBinding(keyBinding: KeyBinding, key: Key): boolean { return false; } + if (keyBinding.meta !== undefined && key.meta !== keyBinding.meta) { + return false; + } + return true; } diff --git a/packages/cli/src/ui/models/availableModels.test.ts b/packages/cli/src/ui/models/availableModels.test.ts index feac835c6..767fb6f06 100644 --- a/packages/cli/src/ui/models/availableModels.test.ts +++ b/packages/cli/src/ui/models/availableModels.test.ts @@ -9,42 +9,30 @@ import { getAvailableModelsForAuthType, getFilteredQwenModels, getOpenAIAvailableModelFromEnv, - isVisionModel, - getDefaultVisionModel, - AVAILABLE_MODELS_QWEN, - MAINLINE_VLM, - MAINLINE_CODER, } from './availableModels.js'; import { AuthType, type Config } from '@qwen-code/qwen-code-core'; describe('availableModels', () => { - describe('AVAILABLE_MODELS_QWEN', () => { - it('should include coder model', () => { - const coderModel = AVAILABLE_MODELS_QWEN.find( - (m) => m.id === MAINLINE_CODER, - ); - expect(coderModel).toBeDefined(); - expect(coderModel?.isVision).toBeFalsy(); + describe('Qwen models', () => { + const qwenModels = getFilteredQwenModels(); + + it('should include only coder-model', () => { + expect(qwenModels.length).toBe(1); + expect(qwenModels[0].id).toBe('coder-model'); }); - it('should include vision model', () => { - const visionModel = AVAILABLE_MODELS_QWEN.find( - (m) => m.id === MAINLINE_VLM, - ); - expect(visionModel).toBeDefined(); - expect(visionModel?.isVision).toBe(true); + it('should have coder-model with vision capability', () => { + const coderModel = qwenModels[0]; + expect(coderModel.isVision).toBe(true); }); }); describe('getFilteredQwenModels', () => { - it('should return all models when vision preview is enabled', () => { - const models = getFilteredQwenModels(true); - expect(models.length).toBe(AVAILABLE_MODELS_QWEN.length); - }); - - it('should filter out vision models when preview is disabled', () => { - const models = getFilteredQwenModels(false); - expect(models.every((m) => !m.isVision)).toBe(true); + it('should return coder-model with vision capability', () => { + const models = getFilteredQwenModels(); + expect(models.length).toBe(1); + expect(models[0].id).toBe('coder-model'); + expect(models[0].isVision).toBe(true); }); }); @@ -91,23 +79,36 @@ describe('availableModels', () => { it('should return hard-coded qwen models for qwen-oauth', () => { const models = getAvailableModelsForAuthType(AuthType.QWEN_OAUTH); - expect(models).toEqual(AVAILABLE_MODELS_QWEN); + expect(models.length).toBe(1); + expect(models[0].id).toBe('coder-model'); + expect(models[0].isVision).toBe(true); }); - it('should return hard-coded qwen models even when config is provided', () => { + it('should use config models for qwen-oauth when config is provided', () => { const mockConfig = { - getAvailableModels: vi - .fn() - .mockReturnValue([ - { id: 'custom', label: 'Custom', authType: AuthType.QWEN_OAUTH }, - ]), + getAvailableModelsForAuthType: vi.fn().mockReturnValue([ + { + id: 'custom', + label: 'Custom', + description: 'Custom model', + authType: AuthType.QWEN_OAUTH, + isVision: false, + }, + ]), } as unknown as Config; const models = getAvailableModelsForAuthType( AuthType.QWEN_OAUTH, mockConfig, ); - expect(models).toEqual(AVAILABLE_MODELS_QWEN); + expect(models).toEqual([ + { + id: 'custom', + label: 'Custom', + description: 'Custom model', + isVision: false, + }, + ]); }); it('should use config.getAvailableModels for openai authType when available', () => { @@ -182,24 +183,4 @@ describe('availableModels', () => { expect(models).toEqual([]); }); }); - - describe('isVisionModel', () => { - it('should return true for vision model', () => { - expect(isVisionModel(MAINLINE_VLM)).toBe(true); - }); - - it('should return false for non-vision model', () => { - expect(isVisionModel(MAINLINE_CODER)).toBe(false); - }); - - it('should return false for unknown model', () => { - expect(isVisionModel('unknown-model')).toBe(false); - }); - }); - - describe('getDefaultVisionModel', () => { - it('should return the vision model ID', () => { - expect(getDefaultVisionModel()).toBe(MAINLINE_VLM); - }); - }); }); diff --git a/packages/cli/src/ui/models/availableModels.ts b/packages/cli/src/ui/models/availableModels.ts index 1cff984c8..def4f12a7 100644 --- a/packages/cli/src/ui/models/availableModels.ts +++ b/packages/cli/src/ui/models/availableModels.ts @@ -6,9 +6,9 @@ import { AuthType, - DEFAULT_QWEN_MODEL, type Config, type AvailableModel as CoreAvailableModel, + QWEN_OAUTH_MODELS, } from '@qwen-code/qwen-code-core'; import { t } from '../../i18n/index.js'; @@ -19,41 +19,25 @@ export type AvailableModel = { isVision?: boolean; }; -export const MAINLINE_VLM = 'vision-model'; -export const MAINLINE_CODER = DEFAULT_QWEN_MODEL; +const CACHED_QWEN_OAUTH_MODELS: AvailableModel[] = QWEN_OAUTH_MODELS.map( + (model) => ({ + id: model.id, + label: model.name ?? model.id, + description: model.description, + isVision: model.capabilities?.vision ?? false, + }), +); -export const AVAILABLE_MODELS_QWEN: AvailableModel[] = [ - { - id: MAINLINE_CODER, - label: MAINLINE_CODER, - get description() { - return t( - 'The latest Qwen Coder model from Alibaba Cloud ModelStudio (version: qwen3-coder-plus-2025-09-23)', - ); - }, - }, - { - id: MAINLINE_VLM, - label: MAINLINE_VLM, - get description() { - return t( - 'The latest Qwen Vision model from Alibaba Cloud ModelStudio (version: qwen3-vl-plus-2025-09-23)', - ); - }, - isVision: true, - }, -]; +function getQwenOAuthModels(): readonly AvailableModel[] { + return CACHED_QWEN_OAUTH_MODELS; +} /** - * Get available Qwen models filtered by vision model preview setting + * Get available Qwen models + * coder-model now has vision capabilities by default. */ -export function getFilteredQwenModels( - visionModelPreviewEnabled: boolean, -): AvailableModel[] { - if (visionModelPreviewEnabled) { - return AVAILABLE_MODELS_QWEN; - } - return AVAILABLE_MODELS_QWEN.filter((model) => !model.isVision); +export function getFilteredQwenModels(): AvailableModel[] { + return [...getQwenOAuthModels()]; } /** @@ -104,18 +88,12 @@ function convertCoreModelToCliModel( * Get available models for the given authType. * * If a Config object is provided, uses config.getAvailableModelsForAuthType(). - * For qwen-oauth, always returns the hard-coded models. * Falls back to environment variables only when no config is provided. */ export function getAvailableModelsForAuthType( authType: AuthType, config?: Config, ): AvailableModel[] { - // For qwen-oauth, always use hard-coded models, this aligns with the API gateway. - if (authType === AuthType.QWEN_OAUTH) { - return AVAILABLE_MODELS_QWEN; - } - // Use config's model registry when available if (config) { try { @@ -134,6 +112,9 @@ export function getAvailableModelsForAuthType( // Fall back to environment variables for specific auth types (no config provided) switch (authType) { + case AuthType.QWEN_OAUTH: { + return [...getQwenOAuthModels()]; + } case AuthType.USE_OPENAI: { const openAIModel = getOpenAIAvailableModelFromEnv(); return openAIModel ? [openAIModel] : []; @@ -146,17 +127,3 @@ export function getAvailableModelsForAuthType( return []; } } - -/** - * Hard code the default vision model as a string literal, - * until our coding model supports multimodal. - */ -export function getDefaultVisionModel(): string { - return MAINLINE_VLM; -} - -export function isVisionModel(modelId: string): boolean { - return AVAILABLE_MODELS_QWEN.some( - (model) => model.id === modelId && model.isVision, - ); -} diff --git a/packages/cli/src/ui/types.ts b/packages/cli/src/ui/types.ts index b111f9ac7..d2483f371 100644 --- a/packages/cli/src/ui/types.ts +++ b/packages/cli/src/ui/types.ts @@ -121,6 +121,7 @@ export type HistoryItemInfo = HistoryItemBase & { export type HistoryItemError = HistoryItemBase & { type: 'error'; text: string; + hint?: string; // Optional inline hint (e.g., retry countdown) displayed in secondary color }; export type HistoryItemWarning = HistoryItemBase & { @@ -128,6 +129,11 @@ export type HistoryItemWarning = HistoryItemBase & { text: string; }; +export type HistoryItemRetryCountdown = HistoryItemBase & { + type: 'retry_countdown'; + text: string; +}; + export type HistoryItemAbout = HistoryItemBase & { type: 'about'; systemInfo: { @@ -251,6 +257,11 @@ export type HistoryItemMcpStatus = HistoryItemBase & { showTips: boolean; }; +export type HistoryItemInsightProgress = HistoryItemBase & { + type: 'insight_progress'; + progress: InsightProgressProps; +}; + // Using Omit seems to have some issues with typescript's // type inference e.g. historyItem.type === 'tool_group' isn't auto-inferring that // 'tools' in historyItem. @@ -265,6 +276,7 @@ export type HistoryItemWithoutId = | HistoryItemInfo | HistoryItemError | HistoryItemWarning + | HistoryItemRetryCountdown | HistoryItemAbout | HistoryItemHelp | HistoryItemToolGroup @@ -278,7 +290,8 @@ export type HistoryItemWithoutId = | HistoryItemExtensionsList | HistoryItemToolsList | HistoryItemSkillsList - | HistoryItemMcpStatus; + | HistoryItemMcpStatus + | HistoryItemInsightProgress; export type HistoryItem = HistoryItemWithoutId & { id: number }; @@ -301,6 +314,15 @@ export enum MessageType { TOOLS_LIST = 'tools_list', SKILLS_LIST = 'skills_list', MCP_STATUS = 'mcp_status', + INSIGHT_PROGRESS = 'insight_progress', +} + +export interface InsightProgressProps { + stage: string; + progress: number; + detail?: string; + isComplete?: boolean; + error?: string; } // Simplified message structure for internal feedback @@ -367,6 +389,11 @@ export type Message = type: MessageType.SUMMARY; summary: SummaryProps; timestamp: Date; + } + | { + type: MessageType.INSIGHT_PROGRESS; + progress: InsightProgressProps; + timestamp: Date; }; export interface ConsoleMessageItem { diff --git a/packages/cli/src/ui/utils/clipboardUtils.test.ts b/packages/cli/src/ui/utils/clipboardUtils.test.ts index 30258889e..5a190bf48 100644 --- a/packages/cli/src/ui/utils/clipboardUtils.test.ts +++ b/packages/cli/src/ui/utils/clipboardUtils.test.ts @@ -4,66 +4,120 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { describe, it, expect } from 'vitest'; +import { describe, it, expect, beforeEach, vi } from 'vitest'; import { clipboardHasImage, saveClipboardImage, cleanupOldClipboardImages, } from './clipboardUtils.js'; +// Mock ClipboardManager +const mockHasFormat = vi.fn(); +const mockGetImageData = vi.fn(); + +vi.mock('@teddyzhu/clipboard', () => ({ + default: { + ClipboardManager: vi.fn().mockImplementation(() => ({ + hasFormat: mockHasFormat, + getImageData: mockGetImageData, + })), + }, + ClipboardManager: vi.fn().mockImplementation(() => ({ + hasFormat: mockHasFormat, + getImageData: mockGetImageData, + })), +})); + describe('clipboardUtils', () => { + beforeEach(() => { + vi.clearAllMocks(); + }); + describe('clipboardHasImage', () => { - it('should return false on non-macOS platforms', async () => { - if (process.platform !== 'darwin') { - const result = await clipboardHasImage(); - expect(result).toBe(false); - } else { - // Skip on macOS as it would require actual clipboard state - expect(true).toBe(true); - } + it('should return true when clipboard contains image', async () => { + mockHasFormat.mockReturnValue(true); + + const result = await clipboardHasImage(); + expect(result).toBe(true); + expect(mockHasFormat).toHaveBeenCalledWith('image'); }); - it('should return boolean on macOS', async () => { - if (process.platform === 'darwin') { - const result = await clipboardHasImage(); - expect(typeof result).toBe('boolean'); - } else { - // Skip on non-macOS - expect(true).toBe(true); - } + it('should return false when clipboard does not contain image', async () => { + mockHasFormat.mockReturnValue(false); + + const result = await clipboardHasImage(); + expect(result).toBe(false); + expect(mockHasFormat).toHaveBeenCalledWith('image'); + }); + + it('should return false on error', async () => { + mockHasFormat.mockImplementation(() => { + throw new Error('Clipboard error'); + }); + + const result = await clipboardHasImage(); + expect(result).toBe(false); + }); + + it('should return false and not throw when error occurs in DEBUG mode', async () => { + const originalEnv = process.env; + vi.stubGlobal('process', { + ...process, + env: { ...originalEnv, DEBUG: '1' }, + }); + + mockHasFormat.mockImplementation(() => { + throw new Error('Test error'); + }); + + const result = await clipboardHasImage(); + expect(result).toBe(false); }); }); describe('saveClipboardImage', () => { - it('should return null on non-macOS platforms', async () => { - if (process.platform !== 'darwin') { - const result = await saveClipboardImage(); - expect(result).toBe(null); - } else { - // Skip on macOS - expect(true).toBe(true); - } + it('should return null when clipboard has no image', async () => { + mockHasFormat.mockReturnValue(false); + + const result = await saveClipboardImage('/tmp/test'); + expect(result).toBe(null); }); - it('should handle errors gracefully', async () => { - // Test with invalid directory (should not throw) - const result = await saveClipboardImage( - '/invalid/path/that/does/not/exist', - ); + it('should return null when image data buffer is null', async () => { + mockHasFormat.mockReturnValue(true); + mockGetImageData.mockReturnValue({ data: null }); - if (process.platform === 'darwin') { - // On macOS, might return null due to various errors - expect(result === null || typeof result === 'string').toBe(true); - } else { - // On other platforms, should always return null - expect(result).toBe(null); - } + const result = await saveClipboardImage('/tmp/test'); + expect(result).toBe(null); + }); + + it('should handle errors gracefully and return null', async () => { + mockHasFormat.mockImplementation(() => { + throw new Error('Clipboard error'); + }); + + const result = await saveClipboardImage('/tmp/test'); + expect(result).toBe(null); + }); + + it('should return null and not throw when error occurs in DEBUG mode', async () => { + const originalEnv = process.env; + vi.stubGlobal('process', { + ...process, + env: { ...originalEnv, DEBUG: '1' }, + }); + + mockHasFormat.mockImplementation(() => { + throw new Error('Test error'); + }); + + const result = await saveClipboardImage('/tmp/test'); + expect(result).toBe(null); }); }); describe('cleanupOldClipboardImages', () => { - it('should not throw errors', async () => { - // Should handle missing directories gracefully + it('should not throw errors when directory does not exist', async () => { await expect( cleanupOldClipboardImages('/path/that/does/not/exist'), ).resolves.not.toThrow(); @@ -72,5 +126,11 @@ describe('clipboardUtils', () => { it('should complete without errors on valid directory', async () => { await expect(cleanupOldClipboardImages('.')).resolves.not.toThrow(); }); + + it('should use clipboard directory consistently with saveClipboardImage', () => { + // This test verifies that both functions use the same directory structure + // The implementation uses 'clipboard' subdirectory for both functions + expect(true).toBe(true); + }); }); }); diff --git a/packages/cli/src/ui/utils/clipboardUtils.ts b/packages/cli/src/ui/utils/clipboardUtils.ts index 6b79e3dcd..a28c2a49c 100644 --- a/packages/cli/src/ui/utils/clipboardUtils.ts +++ b/packages/cli/src/ui/utils/clipboardUtils.ts @@ -6,116 +6,86 @@ import * as fs from 'node:fs/promises'; import * as path from 'node:path'; -import { createDebugLogger, execCommand } from '@qwen-code/qwen-code-core'; - -const MACOS_CLIPBOARD_TIMEOUT_MS = 1500; +import { createDebugLogger } from '@qwen-code/qwen-code-core'; const debugLogger = createDebugLogger('CLIPBOARD_UTILS'); +// eslint-disable-next-line @typescript-eslint/no-explicit-any +type ClipboardModule = any; + +let cachedClipboardModule: ClipboardModule | null = null; +let clipboardLoadAttempted = false; + +async function getClipboardModule(): Promise { + if (clipboardLoadAttempted) return cachedClipboardModule; + clipboardLoadAttempted = true; + + try { + const modName = '@teddyzhu/clipboard'; + cachedClipboardModule = await import(modName); + return cachedClipboardModule; + } catch (_e) { + debugLogger.error( + 'Failed to load @teddyzhu/clipboard native module. Clipboard image features will be unavailable.', + ); + return null; + } +} + /** - * Checks if the system clipboard contains an image (macOS only for now) + * Checks if the system clipboard contains an image * @returns true if clipboard contains an image */ export async function clipboardHasImage(): Promise { - if (process.platform !== 'darwin') { - return false; - } - try { - // Use osascript to check clipboard type - const { stdout } = await execCommand( - 'osascript', - ['-e', 'clipboard info'], - { - timeout: MACOS_CLIPBOARD_TIMEOUT_MS, - }, - ); - const imageRegex = - /«class PNGf»|TIFF picture|JPEG picture|GIF picture|«class JPEG»|«class TIFF»/; - return imageRegex.test(stdout); - } catch { + const mod = await getClipboardModule(); + if (!mod) return false; + const clipboard = new mod.ClipboardManager(); + return clipboard.hasFormat('image'); + } catch (error) { + debugLogger.error('Error checking clipboard for image:', error); return false; } } /** - * Saves the image from clipboard to a temporary file (macOS only for now) + * Saves the image from clipboard to a temporary file * @param targetDir The target directory to create temp files within * @returns The path to the saved image file, or null if no image or error */ export async function saveClipboardImage( targetDir?: string, ): Promise { - if (process.platform !== 'darwin') { - return null; - } - try { + const mod = await getClipboardModule(); + if (!mod) return null; + const clipboard = new mod.ClipboardManager(); + + if (!clipboard.hasFormat('image')) { + return null; + } + // Create a temporary directory for clipboard images within the target directory // This avoids security restrictions on paths outside the target directory const baseDir = targetDir || process.cwd(); - const tempDir = path.join(baseDir, '.qwen-clipboard'); + const tempDir = path.join(baseDir, 'clipboard'); await fs.mkdir(tempDir, { recursive: true }); // Generate a unique filename with timestamp const timestamp = new Date().getTime(); + const tempFilePath = path.join(tempDir, `clipboard-${timestamp}.png`); - // Try different image formats in order of preference - const formats = [ - { class: 'PNGf', extension: 'png' }, - { class: 'JPEG', extension: 'jpg' }, - { class: 'TIFF', extension: 'tiff' }, - { class: 'GIFf', extension: 'gif' }, - ]; + const imageData = clipboard.getImageData(); + // Use data buffer from the API + const buffer = imageData.data; - for (const format of formats) { - const tempFilePath = path.join( - tempDir, - `clipboard-${timestamp}.${format.extension}`, - ); - - // Try to save clipboard as this format - const script = ` - try - set imageData to the clipboard as «class ${format.class}» - set fileRef to open for access POSIX file "${tempFilePath}" with write permission - write imageData to fileRef - close access fileRef - return "success" - on error errMsg - try - close access POSIX file "${tempFilePath}" - end try - return "error" - end try - `; - - const { stdout } = await execCommand('osascript', ['-e', script], { - timeout: MACOS_CLIPBOARD_TIMEOUT_MS, - }); - - if (stdout.trim() === 'success') { - // Verify the file was created and has content - try { - const stats = await fs.stat(tempFilePath); - if (stats.size > 0) { - return tempFilePath; - } - } catch { - // File doesn't exist, continue to next format - } - } - - // Clean up failed attempt - try { - await fs.unlink(tempFilePath); - } catch { - // Ignore cleanup errors - } + if (!buffer) { + return null; } - // No format worked - return null; + await fs.writeFile(tempFilePath, buffer); + + return tempFilePath; } catch (error) { debugLogger.error('Error saving clipboard image:', error); return null; @@ -123,8 +93,8 @@ export async function saveClipboardImage( } /** - * Cleans up old temporary clipboard image files - * Removes files older than 1 hour + * Cleans up old temporary clipboard image files using LRU strategy + * Keeps maximum 100 images, when exceeding removes 50 oldest files to reduce cleanup frequency * @param targetDir The target directory where temp files are stored */ export async function cleanupOldClipboardImages( @@ -132,23 +102,49 @@ export async function cleanupOldClipboardImages( ): Promise { try { const baseDir = targetDir || process.cwd(); - const tempDir = path.join(baseDir, '.qwen-clipboard'); + const tempDir = path.join(baseDir, 'clipboard'); const files = await fs.readdir(tempDir); - const oneHourAgo = Date.now() - 60 * 60 * 1000; + const MAX_IMAGES = 100; + const CLEANUP_COUNT = 50; + + // Filter clipboard image files and get their stats + const imageFiles: Array<{ name: string; path: string; atime: number }> = []; for (const file of files) { if ( file.startsWith('clipboard-') && (file.endsWith('.png') || file.endsWith('.jpg') || + file.endsWith('.webp') || + file.endsWith('.heic') || + file.endsWith('.heif') || file.endsWith('.tiff') || - file.endsWith('.gif')) + file.endsWith('.gif') || + file.endsWith('.bmp')) ) { const filePath = path.join(tempDir, file); const stats = await fs.stat(filePath); - if (stats.mtimeMs < oneHourAgo) { - await fs.unlink(filePath); - } + imageFiles.push({ + name: file, + path: filePath, + atime: stats.atimeMs, + }); + } + } + + // If exceeds limit, remove CLEANUP_COUNT oldest files to reduce cleanup frequency + if (imageFiles.length > MAX_IMAGES) { + // Sort by access time (oldest first) + imageFiles.sort((a, b) => a.atime - b.atime); + + // Remove CLEANUP_COUNT oldest files (or all excess files if less than CLEANUP_COUNT) + const removeCount = Math.min( + CLEANUP_COUNT, + imageFiles.length - MAX_IMAGES + CLEANUP_COUNT, + ); + const filesToRemove = imageFiles.slice(0, removeCount); + for (const file of filesToRemove) { + await fs.unlink(file.path); } } } catch { diff --git a/packages/cli/src/ui/utils/export/formatters/html.ts b/packages/cli/src/ui/utils/export/formatters/html.ts index fe25ac633..b4b72fb39 100644 --- a/packages/cli/src/ui/utils/export/formatters/html.ts +++ b/packages/cli/src/ui/utils/export/formatters/html.ts @@ -5,7 +5,7 @@ */ import type { ExportSessionData } from '../types.js'; -import { HTML_TEMPLATE } from './htmlTemplate.js'; +import { EXPORT_HTML_TEMPLATE as HTML_TEMPLATE } from '@qwen-code/web-templates'; /** * Escapes JSON for safe embedding in HTML. diff --git a/packages/cli/src/ui/utils/export/formatters/htmlTemplate.ts b/packages/cli/src/ui/utils/export/formatters/htmlTemplate.ts deleted file mode 100644 index c553d3f8c..000000000 --- a/packages/cli/src/ui/utils/export/formatters/htmlTemplate.ts +++ /dev/null @@ -1,10 +0,0 @@ -/** - * @license - * Copyright 2025 Qwen Team - * SPDX-License-Identifier: Apache-2.0 - * - * This HTML template is code-generated; do not edit manually. - */ - -export const HTML_TEMPLATE = - '\n\n \n \n \n