Compare commits

..

3 Commits

Author SHA1 Message Date
Alisa 91fc6c748f Merge branch 'main' into package-lock-update 2026-01-29 13:11:26 -08:00
Alisa 690735c5d2 Merge branch 'main' into package-lock-update 2026-01-29 11:09:51 -08:00
Alisa Novikova 3a9338b404 Updating package-lock file 2026-01-29 11:06:30 -08:00
39 changed files with 520 additions and 1590 deletions
+79 -131
View File
@@ -10,14 +10,6 @@ Slash commands provide meta-level control over the CLI itself.
### Built-in Commands
- **`/about`**
- **Description:** Show version info. Please share this information when
filing issues.
- **`/auth`**
- **Description:** Open a dialog that lets you change the authentication
method.
- **`/bug`**
- **Description:** File an issue about Gemini CLI. By default, the issue is
filed within the GitHub repository for Gemini CLI. The string you enter
@@ -30,21 +22,10 @@ Slash commands provide meta-level control over the CLI itself.
conversation state interactively, or resuming a previous state from a later
session.
- **Sub-commands:**
- **`delete <tag>`**
- **Description:** Deletes a saved conversation checkpoint.
- **`list`**
- **Description:** Lists available tags for chat state resumption.
- **Note:** This command only lists chats saved within the current
project. Because chat history is project-scoped, chats saved in other
project directories will not be displayed.
- **`resume <tag>`**
- **Description:** Resumes a conversation from a previous save.
- **Note:** You can only resume chats that were saved within the current
project. To resume a chat from a different project, you must run the
Gemini CLI from that project's directory.
- **`save <tag>`**
- **`save`**
- **Description:** Saves the current conversation history. You must add a
`<tag>` for identifying the conversation state.
- **Usage:** `/chat save <tag>`
- **Details on checkpoint location:** The default locations for saved chat
checkpoints are:
- Linux/macOS: `~/.gemini/tmp/<project_hash>/`
@@ -56,11 +37,25 @@ Slash commands provide meta-level control over the CLI itself.
conversation states. For automatic checkpoints created before file
modifications, see the
[Checkpointing documentation](../cli/checkpointing.md).
- **`share [filename]`**
- **`resume`**
- **Description:** Resumes a conversation from a previous save.
- **Usage:** `/chat resume <tag>`
- **Note:** You can only resume chats that were saved within the current
project. To resume a chat from a different project, you must run the
Gemini CLI from that project's directory.
- **`list`**
- **Description:** Lists available tags for chat state resumption.
- **Note:** This command only lists chats saved within the current
project. Because chat history is project-scoped, chats saved in other
project directories will not be displayed.
- **`delete`**
- **Description:** Deletes a saved conversation checkpoint.
- **Usage:** `/chat delete <tag>`
- **`share`**
- **Description** Writes the current conversation to a provided Markdown
or JSON file. If no filename is provided, then the CLI will generate
one.
- **Usage** `/chat share file.md` or `/chat share file.json`.
or JSON file.
- **Usage** `/chat share file.md` or `/chat share file.json`. If no
filename is provided, then the CLI will generate one.
- **`/clear`**
- **Description:** Clear the terminal screen, including the visible session
@@ -103,9 +98,6 @@ Slash commands provide meta-level control over the CLI itself.
`--include-directories`.
- **Usage:** `/directory show`
- **`/docs`**
- **Description:** Open the Gemini CLI documentation in your browser.
- **`/editor`**
- **Description:** Open a dialog for selecting supported editors.
@@ -117,65 +109,30 @@ Slash commands provide meta-level control over the CLI itself.
- **Description:** Display help information about Gemini CLI, including
available commands and their usage.
- **`/hooks`**
- **Description:** Manage hooks, which allow you to intercept and customize
Gemini CLI behavior at specific lifecycle events.
- **Sub-commands:**
- **`disable-all`**:
- **Description:** Disable all enabled hooks.
- **`disable <hook-name>`**:
- **Description:** Disable a hook by name.
- **`enable-all`**:
- **Description:** Enable all disabled hooks.
- **`enable <hook-name>`**:
- **Description:** Enable a hook by name.
- **`list`** (or `show`, `panel`):
- **Description:** Display all registered hooks with their status.
- **`/ide`**
- **Description:** Manage IDE integration.
- **Sub-commands:**
- **`disable`**:
- **Description:** Disable IDE integration.
- **`enable`**:
- **Description:** Enable IDE integration.
- **`install`**:
- **Description:** Install required IDE companion.
- **`status`**:
- **Description:** Check status of IDE integration.
- **`/init`**
- **Description:** To help users easily create a `GEMINI.md` file, this
command analyzes the current directory and generates a tailored context
file, making it simpler for them to provide project-specific instructions to
the Gemini agent.
- **`/introspect`**
- **Description:** Provide debugging information about the current Gemini CLI
session, including the state of loaded sub-agents and active hooks. This
command is primarily for advanced users and developers.
- **`/mcp`**
- **Description:** Manage configured Model Context Protocol (MCP) servers.
- **Sub-commands:**
- **`list`** or **`ls`**:
- **Description:** List configured MCP servers and tools. This is the
default action if no subcommand is specified.
- **`desc`**
- **Description:** List configured MCP servers and tools with
descriptions.
- **`schema`**:
- **Description:** List configured MCP servers and tools with descriptions
and schemas.
- **`auth`**:
- **Description:** Authenticate with an OAuth-enabled MCP server.
- **Usage:** `/mcp auth <server-name>`
- **Details:** If `<server-name>` is provided, it initiates the OAuth flow
for that server. If no server name is provided, it lists all configured
servers that support OAuth authentication.
- **`desc`**
- **Description:** List configured MCP servers and tools with
descriptions.
- **`list`** or **`ls`**:
- **Description:** List configured MCP servers and tools. This is the
default action if no subcommand is specified.
- **`refresh`**:
- **Description:** Restarts all MCP servers and re-discovers their
available tools.
- **`schema`**:
- **Description:** List configured MCP servers and tools with descriptions
and schemas.
- [**`/model`**](./model.md)
- **Description:** Opens a dialog to choose your Gemini model.
- **`/memory`**
- **Description:** Manage the AI's instructional context (hierarchical memory
@@ -184,40 +141,23 @@ Slash commands provide meta-level control over the CLI itself.
- **`add`**:
- **Description:** Adds the following text to the AI's memory. Usage:
`/memory add <text to remember>`
- **`list`**:
- **Description:** Lists the paths of the GEMINI.md files in use for
hierarchical memory.
- **`refresh`**:
- **Description:** Reload the hierarchical instructional memory from all
`GEMINI.md` files found in the configured locations (global,
project/ancestors, and sub-directories). This command updates the model
with the latest `GEMINI.md` content.
- **`show`**:
- **Description:** Display the full, concatenated content of the current
hierarchical memory that has been loaded from all `GEMINI.md` files.
This lets you inspect the instructional context being provided to the
Gemini model.
- **`refresh`**:
- **Description:** Reload the hierarchical instructional memory from all
`GEMINI.md` files found in the configured locations (global,
project/ancestors, and sub-directories). This command updates the model
with the latest `GEMINI.md` content.
- **`list`**:
- **Description:** Lists the paths of the GEMINI.md files in use for
hierarchical memory.
- **Note:** For more details on how `GEMINI.md` files contribute to
hierarchical memory, see the
[CLI Configuration documentation](../get-started/configuration.md).
- [**`/model`**](./model.md)
- **Description:** Opens a dialog to choose your Gemini model.
- **`/policies`**
- **Description:** Manage policies.
- **Sub-commands:**
- **`list`**:
- **Description:** List all active policies grouped by mode.
- **`/privacy`**
- **Description:** Display the Privacy Notice and allow users to select
whether they consent to the collection of their data for service improvement
purposes.
- **`/quit`** (or **`/exit`**)
- **Description:** Exit Gemini CLI.
- **`/restore`**
- **Description:** Restores the project files to the state they were in just
before a tool was executed. This is particularly useful for undoing file
@@ -228,24 +168,18 @@ Slash commands provide meta-level control over the CLI itself.
[settings](../get-started/configuration.md). See
[Checkpointing documentation](../cli/checkpointing.md) for more details.
- **`/rewind`**
- **Description:** Navigates backward through the conversation history,
allowing you to review past interactions and potentially revert to a
previous state. This feature helps in managing complex or branched
conversations.
- **`/resume`**
- **Description:** Browse and resume previous conversation sessions. Opens an
interactive session browser where you can search, filter, and select from
automatically saved conversations.
- **Features:**
- **Management:** Delete unwanted sessions directly from the browser
- **Resume:** Select any session to resume and continue the conversation
- **Search:** Use `/` to search through conversation content across all
sessions
- **Session Browser:** Interactive interface showing all saved sessions with
timestamps, message counts, and first user message for context
- **Search:** Use `/` to search through conversation content across all
sessions
- **Sorting:** Sort sessions by date or message count
- **Management:** Delete unwanted sessions directly from the browser
- **Resume:** Select any session to resume and continue the conversation
- **Note:** All conversations are automatically saved as you chat - no manual
saving required. See [Session Management](../cli/session-management.md) for
complete details.
@@ -264,23 +198,19 @@ Slash commands provide meta-level control over the CLI itself.
modify them as desired. Changes to some settings are applied immediately,
while others require a restart.
- **`/setup-github`**
- **Description:** Set up GitHub Actions to triage issues and review PRs with
Gemini.
- [**`/skills`**](./skills.md)
- **Description:** Manage Agent Skills, which provide on-demand expertise and
specialized workflows.
- **Sub-commands:**
- **`disable <name>`**:
- **Description:** Disable a specific skill by name.
- **Usage:** `/skills disable <name>`
- **`enable <name>`**:
- **Description:** Enable a specific skill by name.
- **Usage:** `/skills enable <name>`
- **`list`**:
- **Description:** List all discovered skills and their current status
(enabled/disabled).
- **`enable`**:
- **Description:** Enable a specific skill by name.
- **Usage:** `/skills enable <name>`
- **`disable`**:
- **Description:** Disable a specific skill by name.
- **Usage:** `/skills disable <name>`
- **`reload`**:
- **Description:** Refresh the list of discovered skills from all tiers
(workspace, user, and extensions).
@@ -292,14 +222,18 @@ Slash commands provide meta-level control over the CLI itself.
cached tokens are being used, which occurs with API key authentication but
not with OAuth authentication at this time.
- **`/terminal-setup`**
- **Description:** Configure terminal keybindings for multiline input (VS
Code, Cursor, Windsurf).
- [**`/theme`**](./themes.md)
- **Description:** Open a dialog that lets you change the visual theme of
Gemini CLI.
- **`/auth`**
- **Description:** Open a dialog that lets you change the authentication
method.
- **`/about`**
- **Description:** Show version info. Please share this information when
filing issues.
- [**`/tools`**](../tools/index.md)
- **Description:** Display a list of tools that are currently available within
Gemini CLI.
@@ -311,23 +245,37 @@ Slash commands provide meta-level control over the CLI itself.
- **`nodesc`** or **`nodescriptions`**:
- **Description:** Hide tool descriptions, showing only the tool names.
- **`/privacy`**
- **Description:** Display the Privacy Notice and allow users to select
whether they consent to the collection of their data for service improvement
purposes.
- **`/quit`** (or **`/exit`**)
- **Description:** Exit Gemini CLI.
- **`/vim`**
- **Description:** Toggle vim mode on or off. When vim mode is enabled, the
input area supports vim-style navigation and editing commands in both NORMAL
and INSERT modes.
- **Features:**
- **Count support:** Prefix commands with numbers (e.g., `3h`, `5w`, `10G`)
- **Editing commands:** Delete with `x`, change with `c`, insert with `i`,
`a`, `o`, `O`; complex operations like `dd`, `cc`, `dw`, `cw`
- **INSERT mode:** Standard text input with escape to return to NORMAL mode
- **NORMAL mode:** Navigate with `h`, `j`, `k`, `l`; jump by words with `w`,
`b`, `e`; go to line start/end with `0`, `$`, `^`; go to specific lines
with `G` (or `gg` for first line)
- **INSERT mode:** Standard text input with escape to return to NORMAL mode
- **Editing commands:** Delete with `x`, change with `c`, insert with `i`,
`a`, `o`, `O`; complex operations like `dd`, `cc`, `dw`, `cw`
- **Count support:** Prefix commands with numbers (e.g., `3h`, `5w`, `10G`)
- **Repeat last command:** Use `.` to repeat the last editing operation
- **Persistent setting:** Vim mode preference is saved to
`~/.gemini/settings.json` and restored between sessions
- **Repeat last command:** Use `.` to repeat the last editing operation
- **Status indicator:** When enabled, shows `[NORMAL]` or `[INSERT]` in the
footer
- **Status indicator:** When enabled, shows `[NORMAL]` or `[INSERT]` in the
footer
- **`/init`**
- **Description:** To help users easily create a `GEMINI.md` file, this
command analyzes the current directory and generates a tailored context
file, making it simpler for them to provide project-specific instructions to
the Gemini agent.
### Custom commands
-80
View File
@@ -1,80 +0,0 @@
# Creating Agent Skills
This guide provides an overview of how to create your own Agent Skills to extend
the capabilities of Gemini CLI.
## Getting started: The `skill-creator` skill
The recommended way to create a new skill is to use the built-in `skill-creator`
skill. To use it, ask Gemini CLI to create a new skill for you.
**Example prompt:**
> "create a new skill called 'code-reviewer'"
Gemini CLI will then use the `skill-creator` to generate the skill:
1. Generate a new directory for your skill (e.g., `my-new-skill/`).
2. Create a `SKILL.md` file with the necessary YAML frontmatter (`name` and
`description`).
3. Create the standard resource directories: `scripts/`, `references/`, and
`assets/`.
## Manual skill creation
If you prefer to create skills manually:
1. **Create a directory** for your skill (e.g., `my-new-skill/`).
2. **Create a `SKILL.md` file** inside the new directory.
To add additional resources that support the skill, refer to the skill
structure.
## Skill structure
A skill is a directory containing a `SKILL.md` file at its root.
### Folder structure
While a `SKILL.md` file is the only required component, we recommend the
following structure for organizing your skill's resources:
```text
my-skill/
├── SKILL.md (Required) Instructions and metadata
├── scripts/ (Optional) Executable scripts
├── references/ (Optional) Static documentation
└── assets/ (Optional) Templates and other resources
```
### `SKILL.md` file
The `SKILL.md` file is the core of your skill. This file uses YAML frontmatter
for metadata and Markdown for instructions. For example:
```markdown
---
name: code-reviewer
description:
Use this skill to review code. It supports both local changes and remote Pull
Requests.
---
# Code Reviewer
This skill guides the agent in conducting thorough code reviews.
## Workflow
### 1. Determine Review Target
- **Remote PR**: If the user gives a PR number or URL, target that remote PR.
- **Local Changes**: If changes are local... ...
```
- **`name`**: A unique identifier for the skill. This should match the directory
name.
- **`description`**: A description of what the skill does and when Gemini should
use it.
- **Body**: The Markdown body of the file contains the instructions that guide
the agent's behavior when the skill is active.
+78 -5
View File
@@ -89,6 +89,84 @@ gemini skills enable my-expertise
gemini skills disable my-expertise --scope workspace
```
## Creating a Skill
A skill is a directory containing a `SKILL.md` file at its root. This file
serves as the entry point for your skill and uses YAML frontmatter for metadata
and Markdown for instructions.
### Folder Structure
Skills are self-contained directories. At a minimum, a skill requires a
`SKILL.md` file, but can include other resources:
```text
my-skill/
├── SKILL.md (Required) Instructions and metadata
├── scripts/ (Optional) Executable scripts/tools
├── references/ (Optional) Static documentation and examples
└── assets/ (Optional) Templates and binary resources
```
### Basic Structure (SKILL.md)
```markdown
---
name: <unique-name>
description: <what the skill does and when Gemini should use it>
---
<your instructions for how the agent should behave / use the skill>
```
- **`name`**: A unique identifier (lowercase, alphanumeric, and dashes).
- **`description`**: The most critical field. Gemini uses this to decide when
the skill is relevant. Be specific about the expertise provided.
- **Body**: Everything below the second `---` is injected as expert procedural
guidance for the model.
### Example: Team Code Reviewer
Create `~/.gemini/skills/code-reviewer/SKILL.md`:
```markdown
---
name: code-reviewer
description:
Expertise in reviewing code for style, security, and performance. Use when the
user asks for "feedback," a "review," or to "check" their changes.
---
# Code Reviewer
You are an expert code reviewer. When reviewing code, follow this workflow:
1. **Analyze**: Review the staged changes or specific files provided. Ensure
that the changes are scoped properly and represent minimal changes required
to address the issue.
2. **Style**: Ensure code follows the workspace's conventions and idiomatic
patterns as described in the `GEMINI.md` file.
3. **Security**: Flag any potential security vulnerabilities.
4. **Tests**: Verify that new logic has corresponding test coverage and that
the test coverage adequately validates the changes.
Provide your feedback as a concise bulleted list of "Strengths" and
"Opportunities."
```
### Resource Conventions
While you can structure your skill directory however you like, the Agent Skills
standard encourages these conventions:
- **`scripts/`**: Executable scripts (bash, python, node) the agent can run.
- **`references/`**: Static documentation, schemas, or example data for the
agent to consult.
- **`assets/`**: Code templates, boilerplate, or binary resources.
When a skill is activated, Gemini CLI provides the model with a tree view of the
entire skill directory, allowing it to discover and utilize these assets.
## How it Works (Security & Privacy)
1. **Discovery**: At the start of a session, Gemini CLI scans the discovery
@@ -105,8 +183,3 @@ gemini skills disable my-expertise --scope workspace
it permission to read any bundled assets.
5. **Execution**: The model proceeds with the specialized expertise active. It
is instructed to prioritize the skill's procedural guidance within reason.
## Creating your own skills
To create your own skills, see the
[Create Agent Skills](./guides/creating-skills.md) guide.
+28 -31
View File
@@ -24,29 +24,30 @@ the main agent's context or toolset.
## What are sub-agents?
Sub-agents are "specialists" that the main Gemini agent can hire for a specific
job.
Think of sub-agents as "specialists" that the main Gemini agent can hire for a
specific job.
- **Focused context:** Each sub-agent has its own system prompt and persona.
- **Specialized tools:** Sub-agents can have a restricted or specialized set of
tools.
- **Independent context window:** Interactions with a sub-agent happen in a
separate context loop, which saves tokens in your main conversation history.
separate context loop. The main agent only sees the final result, saving
tokens in your main conversation history.
Sub-agents are exposed to the main agent as a tool of the same name. When the
main agent calls the tool, it delegates the task to the sub-agent. Once the
sub-agent completes its task, it reports back to the main agent with its
findings.
Sub-agents are exposed to the main agent as a tool of the same name which
delegates to the sub-agent, when called. Once the sub-agent completes its task
(or fails), it reports back to the main agent with its findings (usually as a
text summary or structured report returned by the tool).
## Built-in sub-agents
Gemini CLI comes with the following built-in sub-agents:
Gemini CLI comes with powerful built-in sub-agents.
### Codebase Investigator
- **Name:** `codebase_investigator`
- **Purpose:** Analyze the codebase, reverse engineer, and understand complex
dependencies.
- **Purpose:** Deep analysis of the codebase, reverse engineering, and
understanding complex dependencies.
- **When to use:** "How does the authentication system work?", "Map out the
dependencies of the `AgentRegistry` class."
- **Configuration:** Enabled by default. You can configure it in
@@ -66,25 +67,20 @@ Gemini CLI comes with the following built-in sub-agents:
### CLI Help Agent
- **Name:** `cli_help`
- **Purpose:** Get expert knowledge about Gemini CLI itself, its commands,
- **Purpose:** Expert knowledge about Gemini CLI itself, its commands,
configuration, and documentation.
- **When to use:** "How do I configure a proxy?", "What does the `/rewind`
command do?"
- **Configuration:** Enabled by default.
### Generalist Agent
- **Name:** `generalist_agent`
- **Purpose:** Route tasks to the appropriate specialized sub-agent.
- **When to use:** Implicitly used by the main agent for routing. Not directly
invoked by the user.
- **Configuration:** Enabled by default. No specific configuration options.
## Creating custom sub-agents
You can create your own sub-agents to automate specific workflows or enforce
specific personas. To use custom sub-agents, you must enable them in your
`settings.json`:
specific personas.
### Prerequisites
To use custom sub-agents, you must enable them in your `settings.json`:
```json
{
@@ -127,10 +123,10 @@ vulnerabilities.
Focus on:
1. SQL Injection
2. XSS (Cross-Site Scripting)
3. Hardcoded credentials
4. Unsafe file operations
1. SQL Injection
2. XSS (Cross-Site Scripting)
3. Hardcoded credentials
4. Unsafe file operations
When you find a vulnerability, explain it clearly and suggest a fix. Do not fix
it yourself; just report it.
@@ -151,16 +147,17 @@ it yourself; just report it.
### Optimizing your sub-agent
The main agent's system prompt encourages it to use an expert sub-agent when one
is available. It decides whether an agent is a relevant expert based on the
agent's description. You can improve the reliability with which an agent is used
by updating the description to more clearly indicate:
The main agent system prompt contains language that encourages use of an expert
sub-agent when one is available for the task at hand. It decides whether an
agent is a relevant expert based on the agent's description. You can improve the
reliability with which an agent is used by updating the description to more
clearly indicate:
- Its area of expertise.
- When it should be used.
- Some example scenarios.
For example, the following sub-agent description should be called fairly
For example: the following sub-agent description should be called fairly
consistently for Git operations.
> Git expert agent which should be used for all local and remote git operations.
@@ -176,7 +173,7 @@ that your sub-agent was called with a specific prompt and the given description.
## Remote subagents (Agent2Agent) (experimental)
Gemini CLI can also delegate tasks to remote sub-agents using the Agent-to-Agent
Gemini CLI can also delegate tasks to remote subagents using the Agent-to-Agent
(A2A) protocol.
> **Note: Remote subagents are currently an experimental feature.**
-4
View File
@@ -1185,10 +1185,6 @@ the `advanced.excludedEnvVars` setting in your `settings.json` file.
- **Description:** The path to your Google Application Credentials JSON file.
- **Example:**
`export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/credentials.json"`
- **`GOOGLE_GENAI_API_VERSION`**:
- Specifies the API version to use for Gemini API requests.
- When set, overrides the default API version used by the SDK.
- Example: `export GOOGLE_GENAI_API_VERSION="v1"`
- **`OTLP_GOOGLE_CLOUD_PROJECT`**:
- Your Google Cloud Project ID for Telemetry in Google Cloud
- Example: `export OTLP_GOOGLE_CLOUD_PROJECT="YOUR_PROJECT_ID"`.
-4
View File
@@ -79,10 +79,6 @@
"label": "Ecosystem and extensibility",
"items": [
{ "label": "Agent skills", "slug": "docs/cli/skills" },
{
"label": "Creating Agent skills",
"slug": "docs/cli/creating-skills"
},
{
"label": "Sub-agents (experimental)",
"slug": "docs/core/subagents"
+3 -2
View File
@@ -13,7 +13,6 @@ import { mkdir, readdir, rm } from 'node:fs/promises';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { canUseRipgrep } from '../packages/core/src/tools/ripGrep.js';
import { disableMouseTracking } from '@google/gemini-cli-core';
const __dirname = dirname(fileURLToPath(import.meta.url));
const rootDir = join(__dirname, '..');
@@ -75,7 +74,9 @@ export async function setup() {
export async function teardown() {
// Disable mouse tracking
if (process.stdout.isTTY) {
disableMouseTracking();
process.stdout.write(
'\x1b[?1000l\x1b[?1003l\x1b[?1015l\x1b[?1006l\x1b[?1002l',
);
}
// Cleanup the test run directory unless KEEP_OUTPUT is set
+211 -24
View File
@@ -7,13 +7,16 @@
"": {
"name": "@google/gemini-cli",
"version": "0.28.0-nightly.20260128.adc8e11bb",
"hasInstallScript": true,
"workspaces": [
"packages/*"
],
"dependencies": {
"ink": "npm:@jrichman/ink@6.4.8",
"latest-version": "^9.0.0",
"simple-git": "^3.28.0"
"punycode": "^2.3.1",
"simple-git": "^3.28.0",
"whatwg-url": "^5.0.0"
},
"bin": {
"gemini": "bundle/gemini.js"
@@ -53,6 +56,7 @@
"mock-fs": "^5.5.0",
"msw": "^2.10.4",
"npm-run-all": "^4.1.5",
"patch-package": "^8.0.1",
"prettier": "^3.5.3",
"react-devtools-core": "^6.1.2",
"semver": "^7.7.2",
@@ -5361,6 +5365,13 @@
"integrity": "sha512-5xXB7kdQlFBP82ViMJTwwEc3gKCLGKR/eoxQm4zge7GPBl86tCdI0IdPJjoKd8mUSFXz5V7i/25sfsEkP4j46g==",
"license": "MIT"
},
"node_modules/@yarnpkg/lockfile": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@yarnpkg/lockfile/-/lockfile-1.1.0.tgz",
"integrity": "sha512-GpSwvyXOcOOlV70vbnzjj4fW5xW/FdUF6nQEt1ENy7m4ZCczi1+/buVUPAqmGfqznsORNFzUMjctTIp8a9tuCQ==",
"dev": true,
"license": "BSD-2-Clause"
},
"node_modules/abort-controller": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz",
@@ -6635,6 +6646,22 @@
"node": ">=18"
}
},
"node_modules/ci-info": {
"version": "3.9.0",
"resolved": "https://registry.npmjs.org/ci-info/-/ci-info-3.9.0.tgz",
"integrity": "sha512-NIxF55hv4nSqQswkAeiOi1r83xy8JldOFDTWiug55KBu9Jnblncd2U6ViHmYgHf01TPZS77NJBhBMKdWj9HQMQ==",
"dev": true,
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/sibiraj-s"
}
],
"license": "MIT",
"engines": {
"node": ">=8"
}
},
"node_modules/cjs-module-lexer": {
"version": "1.4.3",
"resolved": "https://registry.npmjs.org/cjs-module-lexer/-/cjs-module-lexer-1.4.3.tgz",
@@ -9287,6 +9314,16 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/find-yarn-workspace-root": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/find-yarn-workspace-root/-/find-yarn-workspace-root-2.0.0.tgz",
"integrity": "sha512-1IMnbjt4KzsQfnhnzNd8wUEgXZ44IzZaZmnLYx7D5FZlaHt2gW20Cri8Q+E/t5tIj4+epTBub+2Zxu/vNILzqQ==",
"dev": true,
"license": "Apache-2.0",
"dependencies": {
"micromatch": "^4.0.2"
}
},
"node_modules/findup-sync": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/findup-sync/-/findup-sync-5.0.0.tgz",
@@ -11511,6 +11548,26 @@
"integrity": "sha512-fQhoXdcvc3V28x7C7BMs4P5+kNlgUURe2jmUT1T//oBRMDrqy1QPelJimwZGo7Hg9VPV3EQV5Bnq4hbFy2vetA==",
"license": "BSD-2-Clause"
},
"node_modules/json-stable-stringify": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/json-stable-stringify/-/json-stable-stringify-1.3.0.tgz",
"integrity": "sha512-qtYiSSFlwot9XHtF9bD9c7rwKjr+RecWT//ZnPvSmEjpV5mmPOCN4j8UjY5hbjNkOwZ/jQv3J6R1/pL7RwgMsg==",
"dev": true,
"license": "MIT",
"dependencies": {
"call-bind": "^1.0.8",
"call-bound": "^1.0.4",
"isarray": "^2.0.5",
"jsonify": "^0.0.1",
"object-keys": "^1.1.1"
},
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/json-stable-stringify-without-jsonify": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz",
@@ -11559,6 +11616,16 @@
"node": ">= 10.0.0"
}
},
"node_modules/jsonify": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.1.tgz",
"integrity": "sha512-2/Ki0GcmuqSrgFyelQq9M05y7PS0mEwuIzrf3f1fPqkVDVRvZrPZtVSMHxdgo8Aq0sxAOb/cr2aqqA3LeWHVPg==",
"dev": true,
"license": "Public Domain",
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/jsonwebtoken": {
"version": "9.0.2",
"resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz",
@@ -11663,6 +11730,16 @@
"json-buffer": "3.0.1"
}
},
"node_modules/klaw-sync": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/klaw-sync/-/klaw-sync-6.0.0.tgz",
"integrity": "sha512-nIeuVSzdCCs6TDPTqI8w1Yre34sSq7AkZ4B3sfOBbI2CgVSB4Du4aLQijFU2+lhAFCwt9+42Hel6lQNIv6AntQ==",
"dev": true,
"license": "MIT",
"dependencies": {
"graceful-fs": "^4.1.11"
}
},
"node_modules/kleur": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/kleur/-/kleur-3.0.3.tgz",
@@ -12772,28 +12849,6 @@
}
}
},
"node_modules/node-fetch/node_modules/tr46": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
"integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==",
"license": "MIT"
},
"node_modules/node-fetch/node_modules/webidl-conversions": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
"integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==",
"license": "BSD-2-Clause"
},
"node_modules/node-fetch/node_modules/whatwg-url": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
"integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==",
"license": "MIT",
"dependencies": {
"tr46": "~0.0.3",
"webidl-conversions": "^3.0.0"
}
},
"node_modules/node-gyp-build": {
"version": "4.8.4",
"resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.8.4.tgz",
@@ -13687,6 +13742,117 @@
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
}
},
"node_modules/patch-package": {
"version": "8.0.1",
"resolved": "https://registry.npmjs.org/patch-package/-/patch-package-8.0.1.tgz",
"integrity": "sha512-VsKRIA8f5uqHQ7NGhwIna6Bx6D9s/1iXlA1hthBVBEbkq+t4kXD0HHt+rJhf/Z+Ci0F/HCB2hvn0qLdLG+Qxlw==",
"dev": true,
"license": "MIT",
"dependencies": {
"@yarnpkg/lockfile": "^1.1.0",
"chalk": "^4.1.2",
"ci-info": "^3.7.0",
"cross-spawn": "^7.0.3",
"find-yarn-workspace-root": "^2.0.0",
"fs-extra": "^10.0.0",
"json-stable-stringify": "^1.0.2",
"klaw-sync": "^6.0.0",
"minimist": "^1.2.6",
"open": "^7.4.2",
"semver": "^7.5.3",
"slash": "^2.0.0",
"tmp": "^0.2.4",
"yaml": "^2.2.2"
},
"bin": {
"patch-package": "index.js"
},
"engines": {
"node": ">=14",
"npm": ">5"
}
},
"node_modules/patch-package/node_modules/fs-extra": {
"version": "10.1.0",
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.1.0.tgz",
"integrity": "sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ==",
"dev": true,
"license": "MIT",
"dependencies": {
"graceful-fs": "^4.2.0",
"jsonfile": "^6.0.1",
"universalify": "^2.0.0"
},
"engines": {
"node": ">=12"
}
},
"node_modules/patch-package/node_modules/is-docker": {
"version": "2.2.1",
"resolved": "https://registry.npmjs.org/is-docker/-/is-docker-2.2.1.tgz",
"integrity": "sha512-F+i2BKsFrH66iaUFc0woD8sLy8getkwTwtOBjvs56Cx4CgJDeKQeqfz8wAYiSb8JOprWhHH5p77PbmYCvvUuXQ==",
"dev": true,
"license": "MIT",
"bin": {
"is-docker": "cli.js"
},
"engines": {
"node": ">=8"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/patch-package/node_modules/is-wsl": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/is-wsl/-/is-wsl-2.2.0.tgz",
"integrity": "sha512-fKzAra0rGJUUBwGBgNkHZuToZcn+TtXHpeCgmkMJMMYx1sQDYaCSyjJBSCa2nH1DGm7s3n1oBnohoVTBaN7Lww==",
"dev": true,
"license": "MIT",
"dependencies": {
"is-docker": "^2.0.0"
},
"engines": {
"node": ">=8"
}
},
"node_modules/patch-package/node_modules/open": {
"version": "7.4.2",
"resolved": "https://registry.npmjs.org/open/-/open-7.4.2.tgz",
"integrity": "sha512-MVHddDVweXZF3awtlAS+6pgKLlm/JgxZ90+/NBurBoQctVOOB/zDdVjcyPzQ+0laDGbsWgrRkflI65sQeOgT9Q==",
"dev": true,
"license": "MIT",
"dependencies": {
"is-docker": "^2.0.0",
"is-wsl": "^2.1.1"
},
"engines": {
"node": ">=8"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/patch-package/node_modules/slash": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/slash/-/slash-2.0.0.tgz",
"integrity": "sha512-ZYKh3Wh2z1PpEXWr0MpSBZ0V6mZHAQfYevttO11c51CaWjGTaadiKZ+wVt1PbMlDV5qhMFslpZCemhwOK7C89A==",
"dev": true,
"license": "MIT",
"engines": {
"node": ">=6"
}
},
"node_modules/patch-package/node_modules/universalify": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.1.tgz",
"integrity": "sha512-gptHNQghINnc/vTGIk0SOFGFNXw7JVrlRUtConJRlvaw6DuX0wO5Jeko9sWrMBhh+PsYAZ7oXAiOnf/UKogyiw==",
"dev": true,
"license": "MIT",
"engines": {
"node": ">= 10.0.0"
}
},
"node_modules/path-browserify": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/path-browserify/-/path-browserify-1.0.1.tgz",
@@ -14133,7 +14299,6 @@
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz",
"integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==",
"devOptional": true,
"license": "MIT",
"engines": {
"node": ">=6"
@@ -16621,6 +16786,12 @@
"node": ">=0.6"
}
},
"node_modules/tr46": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
"integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==",
"license": "MIT"
},
"node_modules/tree-dump": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/tree-dump/-/tree-dump-1.0.3.tgz",
@@ -17382,6 +17553,12 @@
}
}
},
"node_modules/webidl-conversions": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
"integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==",
"license": "BSD-2-Clause"
},
"node_modules/whatwg-encoding": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/whatwg-encoding/-/whatwg-encoding-3.1.1.tgz",
@@ -17405,6 +17582,16 @@
"node": ">=18"
}
},
"node_modules/whatwg-url": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
"integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==",
"license": "MIT",
"dependencies": {
"tr46": "~0.0.3",
"webidl-conversions": "^3.0.0"
}
},
"node_modules/which": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
+6 -2
View File
@@ -61,7 +61,8 @@
"telemetry": "node scripts/telemetry.js",
"check:lockfile": "node scripts/check-lockfile.js",
"clean": "node scripts/clean.js",
"pre-commit": "node scripts/pre-commit.js"
"pre-commit": "node scripts/pre-commit.js",
"postinstall": "patch-package"
},
"overrides": {
"ink": "npm:@jrichman/ink@6.4.8",
@@ -113,6 +114,7 @@
"mock-fs": "^5.5.0",
"msw": "^2.10.4",
"npm-run-all": "^4.1.5",
"patch-package": "^8.0.1",
"prettier": "^3.5.3",
"react-devtools-core": "^6.1.2",
"semver": "^7.7.2",
@@ -126,7 +128,9 @@
"dependencies": {
"ink": "npm:@jrichman/ink@6.4.8",
"latest-version": "^9.0.0",
"simple-git": "^3.28.0"
"punycode": "^2.3.1",
"simple-git": "^3.28.0",
"whatwg-url": "^5.0.0"
},
"optionalDependencies": {
"@lydell/node-pty": "1.1.0",
-9
View File
@@ -338,10 +338,6 @@ export class LoadedSettings {
}
setValue(scope: LoadableSettingScope, key: string, value: unknown): void {
coreEvents.emitFeedback(
'info',
'saving: ' + key + ', ' + value + ', scope: ' + scope,
);
const settingsFile = this.forScope(scope);
setNestedProperty(settingsFile.settings, key, value);
setNestedProperty(settingsFile.originalSettings, key, value);
@@ -731,10 +727,6 @@ export function migrateDeprecatedSettings(
}
if (modified) {
coreEvents.emitFeedback(
'info',
'setting new general for scope:' + scope,
);
loadedSettings.setValue(scope, 'general', newGeneral);
anyModified = true;
}
@@ -855,7 +847,6 @@ export function saveSettings(settingsFile: SettingsFile): void {
settingsToSave as Record<string, unknown>,
);
} catch (error) {
coreEvents.emitFeedback('error', 'here' + error, error);
coreEvents.emitFeedback(
'error',
'There was an error saving your latest settings changes.',
+2 -9
View File
@@ -32,10 +32,6 @@ import {
runExitCleanup,
registerTelemetryConfig,
} from './utils/cleanup.js';
import {
cleanupToolOutputFiles,
cleanupExpiredSessions,
} from './utils/sessionCleanup.js';
import {
type Config,
type ResumedSessionData,
@@ -75,6 +71,7 @@ import {
} from './core/initializer.js';
import { validateAuthMethod } from './config/auth.js';
import { runZedIntegration } from './zed-integration/zedIntegration.js';
import { cleanupExpiredSessions } from './utils/sessionCleanup.js';
import { validateNonInteractiveAuth } from './validateNonInterActiveAuth.js';
import { checkForUpdates } from './ui/utils/updateCheck.js';
import { handleAutoUpdate } from './utils/handleAutoUpdate.js';
@@ -323,10 +320,7 @@ export async function main() {
);
});
await Promise.all([
cleanupCheckpoints(),
cleanupToolOutputFiles(settings.merged),
]);
await cleanupCheckpoints();
const parseArgsHandle = startupProfiler.start('parse_arguments');
const argv = await parseArguments(settings.merged);
@@ -371,7 +365,6 @@ export async function main() {
process.env['CLOUD_SHELL'] === 'true' ||
process.env['GEMINI_CLI_USE_COMPUTE_ADC'] === 'true'
) {
coreEvents.emitFeedback('info', 'saving compute_adc');
settings.setValue(
SettingScope.User,
'security.auth.selectedType',
@@ -19,7 +19,7 @@ import { SettingsContext } from '../contexts/SettingsContext.js';
vi.mock('../contexts/VimModeContext.js', () => ({
useVimMode: vi.fn(() => ({
vimEnabled: false,
vimMode: 'INSERT',
vimMode: 'NORMAL',
})),
}));
import { ApprovalMode } from '@google/gemini-cli-core';
@@ -54,9 +54,7 @@ vi.mock('./DetailedMessagesDisplay.js', () => ({
}));
vi.mock('./InputPrompt.js', () => ({
InputPrompt: ({ placeholder }: { placeholder?: string }) => (
<Text>InputPrompt: {placeholder}</Text>
),
InputPrompt: () => <Text>InputPrompt</Text>,
calculatePromptWidths: vi.fn(() => ({
inputWidth: 80,
suggestionsWidth: 40,
@@ -489,40 +487,4 @@ describe('Composer', () => {
expect(lastFrame()).not.toContain('DetailedMessagesDisplay');
});
});
describe('Vim Mode Placeholders', () => {
it('shows correct placeholder in INSERT mode', async () => {
const uiState = createMockUIState({ isInputActive: true });
const { useVimMode } = await import('../contexts/VimModeContext.js');
vi.mocked(useVimMode).mockReturnValue({
vimEnabled: true,
vimMode: 'INSERT',
toggleVimEnabled: vi.fn(),
setVimMode: vi.fn(),
});
const { lastFrame } = renderComposer(uiState);
expect(lastFrame()).toContain(
"InputPrompt: Press 'Esc' for NORMAL mode.",
);
});
it('shows correct placeholder in NORMAL mode', async () => {
const uiState = createMockUIState({ isInputActive: true });
const { useVimMode } = await import('../contexts/VimModeContext.js');
vi.mocked(useVimMode).mockReturnValue({
vimEnabled: true,
vimMode: 'NORMAL',
toggleVimEnabled: vi.fn(),
setVimMode: vi.fn(),
});
const { lastFrame } = renderComposer(uiState);
expect(lastFrame()).toContain(
"InputPrompt: Press 'i' for INSERT mode.",
);
});
});
});
+2 -4
View File
@@ -35,7 +35,7 @@ export const Composer = ({ isFocused = true }: { isFocused?: boolean }) => {
const isScreenReaderEnabled = useIsScreenReaderEnabled();
const uiState = useUIState();
const uiActions = useUIActions();
const { vimEnabled, vimMode } = useVimMode();
const { vimEnabled } = useVimMode();
const terminalWidth = process.stdout.columns;
const isNarrow = isNarrowWidth(terminalWidth);
const debugConsoleMaxHeight = Math.floor(Math.max(terminalWidth * 0.2, 5));
@@ -143,9 +143,7 @@ export const Composer = ({ isFocused = true }: { isFocused?: boolean }) => {
popAllMessages={uiActions.popAllMessages}
placeholder={
vimEnabled
? vimMode === 'INSERT'
? " Press 'Esc' for NORMAL mode."
: " Press 'i' for INSERT mode."
? " Press 'i' for INSERT mode and 'Esc' for NORMAL mode."
: uiState.shellModeActive
? ' Type your shell command'
: ' Type your message or @path/to/file'
@@ -6,21 +6,11 @@
import { renderWithProviders } from '../../../test-utils/render.js';
import { HalfLinePaddedBox } from './HalfLinePaddedBox.js';
import { Text, useIsScreenReaderEnabled } from 'ink';
import { Text } from 'ink';
import { describe, it, expect, vi, afterEach } from 'vitest';
import { isITerm2 } from '../../utils/terminalUtils.js';
vi.mock('ink', async () => {
const actual = await vi.importActual('ink');
return {
...actual,
useIsScreenReaderEnabled: vi.fn(() => false),
};
});
describe('<HalfLinePaddedBox />', () => {
const mockUseIsScreenReaderEnabled = vi.mocked(useIsScreenReaderEnabled);
afterEach(() => {
vi.restoreAllMocks();
});
@@ -71,19 +61,4 @@ describe('<HalfLinePaddedBox />', () => {
unmount();
});
it('renders nothing when screen reader is enabled', async () => {
mockUseIsScreenReaderEnabled.mockReturnValue(true);
const { lastFrame, unmount } = renderWithProviders(
<HalfLinePaddedBox backgroundBaseColor="blue" backgroundOpacity={0.5}>
<Text>Content</Text>
</HalfLinePaddedBox>,
{ width: 10 },
);
expect(lastFrame()).toMatchSnapshot();
unmount();
});
});
@@ -6,7 +6,7 @@
import type React from 'react';
import { useMemo } from 'react';
import { Box, Text, useIsScreenReaderEnabled } from 'ink';
import { Box, Text } from 'ink';
import { useUIState } from '../../contexts/UIStateContext.js';
import {
interpolateColor,
@@ -39,8 +39,7 @@ export interface HalfLinePaddedBoxProps {
* at the top and bottom using block characters (▀/▄).
*/
export const HalfLinePaddedBox: React.FC<HalfLinePaddedBoxProps> = (props) => {
const isScreenReaderEnabled = useIsScreenReaderEnabled();
if (props.useBackgroundColor === false || isScreenReaderEnabled) {
if (props.useBackgroundColor === false) {
return <>{props.children}</>;
}
@@ -6,8 +6,6 @@ Content
▀▀▀▀▀▀▀▀▀▀"
`;
exports[`<HalfLinePaddedBox /> > renders nothing when screen reader is enabled 1`] = `"Content"`;
exports[`<HalfLinePaddedBox /> > renders nothing when useBackgroundColor is false 1`] = `"Content"`;
exports[`<HalfLinePaddedBox /> > renders standard background and blocks when not iTerm2 1`] = `
@@ -1418,7 +1418,7 @@ describe('useTextBuffer', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
act(() => {
act(() =>
result.current.handleInput({
name: 'h',
shift: false,
@@ -1427,9 +1427,9 @@ describe('useTextBuffer', () => {
cmd: false,
insertable: true,
sequence: 'h',
});
});
void act(() =>
}),
);
act(() =>
result.current.handleInput({
name: 'i',
shift: false,
@@ -1447,7 +1447,7 @@ describe('useTextBuffer', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
act(() => {
act(() =>
result.current.handleInput({
name: 'return',
shift: false,
@@ -1456,8 +1456,8 @@ describe('useTextBuffer', () => {
cmd: false,
insertable: true,
sequence: '\r',
});
});
}),
);
expect(getBufferState(result).lines).toEqual(['', '']);
});
@@ -1465,7 +1465,7 @@ describe('useTextBuffer', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
act(() => {
act(() =>
result.current.handleInput({
name: 'j',
shift: false,
@@ -1474,8 +1474,8 @@ describe('useTextBuffer', () => {
cmd: false,
insertable: false,
sequence: '\n',
});
});
}),
);
expect(getBufferState(result).lines).toEqual(['', '']);
});
@@ -1483,7 +1483,7 @@ describe('useTextBuffer', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
act(() => {
act(() =>
result.current.handleInput({
name: 'tab',
shift: false,
@@ -1492,8 +1492,8 @@ describe('useTextBuffer', () => {
cmd: false,
insertable: false,
sequence: '\t',
});
});
}),
);
expect(getBufferState(result).text).toBe('');
});
@@ -1501,7 +1501,7 @@ describe('useTextBuffer', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
act(() => {
act(() =>
result.current.handleInput({
name: 'tab',
shift: true,
@@ -1510,8 +1510,8 @@ describe('useTextBuffer', () => {
cmd: false,
insertable: false,
sequence: '\u001b[9;2u',
});
});
}),
);
expect(getBufferState(result).text).toBe('');
});
@@ -1524,7 +1524,7 @@ describe('useTextBuffer', () => {
}),
);
act(() => result.current.move('end'));
act(() => {
act(() =>
result.current.handleInput({
name: 'backspace',
shift: false,
@@ -1533,8 +1533,8 @@ describe('useTextBuffer', () => {
cmd: false,
insertable: false,
sequence: '\x7f',
});
});
}),
);
expect(getBufferState(result).text).toBe('');
});
@@ -1627,7 +1627,7 @@ describe('useTextBuffer', () => {
}),
);
act(() => result.current.move('end')); // cursor [0,2]
act(() => {
act(() =>
result.current.handleInput({
name: 'left',
shift: false,
@@ -1636,10 +1636,10 @@ describe('useTextBuffer', () => {
cmd: false,
insertable: false,
sequence: '\x1b[D',
});
});
}),
);
expect(getBufferState(result).cursor).toEqual([0, 1]);
act(() => {
act(() =>
result.current.handleInput({
name: 'right',
shift: false,
@@ -1648,8 +1648,8 @@ describe('useTextBuffer', () => {
cmd: false,
insertable: false,
sequence: '\x1b[C',
});
});
}),
);
expect(getBufferState(result).cursor).toEqual([0, 2]);
});
@@ -1659,7 +1659,7 @@ describe('useTextBuffer', () => {
);
const textWithAnsi = '\x1B[31mHello\x1B[0m \x1B[32mWorld\x1B[0m';
// Simulate pasting by calling handleInput with a string longer than 1 char
act(() => {
act(() =>
result.current.handleInput({
name: '',
shift: false,
@@ -1668,8 +1668,8 @@ describe('useTextBuffer', () => {
cmd: false,
insertable: true,
sequence: textWithAnsi,
});
});
}),
);
expect(getBufferState(result).text).toBe('Hello World');
});
@@ -1677,7 +1677,7 @@ describe('useTextBuffer', () => {
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
act(() => {
act(() =>
result.current.handleInput({
name: 'return',
shift: true,
@@ -1686,8 +1686,8 @@ describe('useTextBuffer', () => {
cmd: false,
insertable: true,
sequence: '\r',
});
}); // Simulates Shift+Enter in VSCode terminal
}),
); // Simulates Shift+Enter in VSCode terminal
expect(getBufferState(result).lines).toEqual(['', '']);
});
@@ -1927,9 +1927,7 @@ Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots
const { result } = renderHook(() =>
useTextBuffer({ viewport, isValidPath: () => false }),
);
act(() => {
result.current.handleInput(createInput(input));
});
act(() => result.current.handleInput(createInput(input)));
expect(getBufferState(result).text).toBe(expected);
});
@@ -1938,9 +1936,7 @@ Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots
useTextBuffer({ viewport, isValidPath: () => false }),
);
const validText = 'Hello World\nThis is a test.';
act(() => {
result.current.handleInput(createInput(validText));
});
act(() => result.current.handleInput(createInput(validText)));
expect(getBufferState(result).text).toBe(validText);
});
@@ -1954,7 +1950,7 @@ Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots
expect(largeTextWithUnsafe.length).toBeGreaterThan(5000);
act(() => {
act(() =>
result.current.handleInput({
name: '',
shift: false,
@@ -1963,8 +1959,8 @@ Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots
cmd: false,
insertable: true,
sequence: largeTextWithUnsafe,
});
});
}),
);
const resultText = getBufferState(result).text;
expect(resultText).not.toContain('\x07');
@@ -1989,7 +1985,7 @@ Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots
expect(largeTextWithAnsi.length).toBeGreaterThan(5000);
act(() => {
act(() =>
result.current.handleInput({
name: '',
shift: false,
@@ -1998,8 +1994,8 @@ Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots
cmd: false,
insertable: true,
sequence: largeTextWithAnsi,
});
});
}),
);
const resultText = getBufferState(result).text;
expect(resultText).not.toContain('\x1B[31m');
@@ -2014,7 +2010,7 @@ Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots
useTextBuffer({ viewport, isValidPath: () => false }),
);
const emojis = '🐍🐳🦀🦄';
act(() => {
act(() =>
result.current.handleInput({
name: '',
shift: false,
@@ -2023,8 +2019,8 @@ Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots
cmd: false,
insertable: true,
sequence: emojis,
});
});
}),
);
expect(getBufferState(result).text).toBe(emojis);
});
});
@@ -2206,7 +2202,7 @@ Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots
singleLine: true,
}),
);
act(() => {
act(() =>
result.current.handleInput({
name: 'return',
shift: false,
@@ -2215,8 +2211,8 @@ Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots
cmd: false,
insertable: true,
sequence: '\r',
});
});
}),
);
expect(getBufferState(result).lines).toEqual(['']);
});
@@ -2228,7 +2224,7 @@ Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots
singleLine: true,
}),
);
act(() => {
act(() =>
result.current.handleInput({
name: 'f1',
shift: false,
@@ -2237,8 +2233,8 @@ Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots
cmd: false,
insertable: false,
sequence: '\u001bOP',
});
});
}),
);
expect(getBufferState(result).lines).toEqual(['']);
});
@@ -3419,7 +3419,7 @@ export interface TextBuffer {
/**
* High level "handleInput" receives what Ink gives us.
*/
handleInput: (key: Key) => boolean;
handleInput: (key: Key) => void;
/**
* Opens the current buffer contents in the user's preferred terminal text
* editor ($VISUAL or $EDITOR, falling back to "vi"). The method blocks
@@ -34,24 +34,26 @@ export const VimModeProvider = ({
}) => {
const initialVimEnabled = settings.merged.general.vimMode;
const [vimEnabled, setVimEnabled] = useState(initialVimEnabled);
const [vimMode, setVimMode] = useState<VimMode>('INSERT');
const [vimMode, setVimMode] = useState<VimMode>(
initialVimEnabled ? 'NORMAL' : 'INSERT',
);
useEffect(() => {
// Initialize vimEnabled from settings on mount
const enabled = settings.merged.general.vimMode;
setVimEnabled(enabled);
// When vim mode is enabled, start in INSERT mode
// When vim mode is enabled, always start in NORMAL mode
if (enabled) {
setVimMode('INSERT');
setVimMode('NORMAL');
}
}, [settings.merged.general.vimMode]);
const toggleVimEnabled = useCallback(async () => {
const newValue = !vimEnabled;
setVimEnabled(newValue);
// When enabling vim mode, start in INSERT mode
// When enabling vim mode, start in NORMAL mode
if (newValue) {
setVimMode('INSERT');
setVimMode('NORMAL');
}
settings.setValue(SettingScope.User, 'general.vimMode', newValue);
return newValue;
@@ -1,85 +0,0 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { renderHook } from '../../test-utils/render.js';
import { act } from 'react';
import { useVim } from './vim.js';
import type { VimMode } from './vim.js';
import type { TextBuffer } from '../components/shared/text-buffer.js';
import type { Key } from './useKeypress.js';
// Mock the VimModeContext
const mockVimContext = {
vimEnabled: true,
vimMode: 'INSERT' as VimMode,
toggleVimEnabled: vi.fn(),
setVimMode: vi.fn(),
};
vi.mock('../contexts/VimModeContext.js', () => ({
useVimMode: () => mockVimContext,
VimModeProvider: ({ children }: { children: React.ReactNode }) => children,
}));
const createKey = (partial: Partial<Key>): Key => ({
name: partial.name || '',
sequence: partial.sequence || '',
shift: partial.shift || false,
alt: partial.alt || false,
ctrl: partial.ctrl || false,
cmd: partial.cmd || false,
insertable: partial.insertable || false,
...partial,
});
describe('useVim passthrough', () => {
let mockBuffer: Partial<TextBuffer>;
beforeEach(() => {
vi.clearAllMocks();
mockBuffer = {
text: 'hello',
handleInput: vi.fn().mockReturnValue(false),
vimEscapeInsertMode: vi.fn(),
setText: vi.fn(),
};
mockVimContext.vimEnabled = true;
});
it.each([
{
mode: 'INSERT' as VimMode,
name: 'F12',
key: createKey({ name: 'f12', sequence: '\u001b[24~' }),
},
{
mode: 'INSERT' as VimMode,
name: 'Ctrl-X',
key: createKey({ name: 'x', ctrl: true, sequence: '\x18' }),
},
{
mode: 'NORMAL' as VimMode,
name: 'F12',
key: createKey({ name: 'f12', sequence: '\u001b[24~' }),
},
{
mode: 'NORMAL' as VimMode,
name: 'Ctrl-X',
key: createKey({ name: 'x', ctrl: true, sequence: '\x18' }),
},
])('should pass through $name in $mode mode', ({ mode, key }) => {
mockVimContext.vimMode = mode;
const { result } = renderHook(() => useVim(mockBuffer as TextBuffer));
let handled = true;
act(() => {
handled = result.current.handleInput(key);
});
expect(handled).toBe(false);
});
});
+10 -84
View File
@@ -22,7 +22,7 @@ import { textBufferReducer } from '../components/shared/text-buffer.js';
// Mock the VimModeContext
const mockVimContext = {
vimEnabled: true,
vimMode: 'INSERT' as VimMode,
vimMode: 'NORMAL' as VimMode,
toggleVimEnabled: vi.fn(),
setVimMode: vi.fn(),
};
@@ -91,8 +91,6 @@ const TEST_SEQUENCES = {
LINE_END: createKey({ sequence: '$' }),
REPEAT: createKey({ sequence: '.' }),
CTRL_C: createKey({ sequence: '\x03', name: 'c', ctrl: true }),
CTRL_X: createKey({ sequence: '\x18', name: 'x', ctrl: true }),
F12: createKey({ sequence: '\u001b[24~', name: 'f12' }),
} as const;
describe('useVim hook', () => {
@@ -136,7 +134,6 @@ describe('useVim hook', () => {
replaceRangeByOffset: vi.fn(),
handleInput: vi.fn(),
setText: vi.fn(),
openInExternalEditor: vi.fn(),
// Vim-specific methods
vimDeleteWordForward: vi.fn(),
vimDeleteWordBackward: vi.fn(),
@@ -210,23 +207,20 @@ describe('useVim hook', () => {
mockBuffer = createMockBuffer();
// Reset mock context to default state
mockVimContext.vimEnabled = true;
mockVimContext.vimMode = 'INSERT';
mockVimContext.vimMode = 'NORMAL';
mockVimContext.toggleVimEnabled.mockClear();
mockVimContext.setVimMode.mockClear();
});
describe('Mode switching', () => {
it('should start in INSERT mode', () => {
it('should start in NORMAL mode', () => {
const { result } = renderVimHook();
expect(result.current.mode).toBe('INSERT');
expect(result.current.mode).toBe('NORMAL');
});
it('should switch to INSERT mode with i command', () => {
const { result } = renderVimHook();
exitInsertMode(result);
expect(result.current.mode).toBe('NORMAL');
act(() => {
result.current.handleInput(TEST_SEQUENCES.INSERT);
});
@@ -272,7 +266,6 @@ describe('useVim hook', () => {
describe('Navigation commands', () => {
it('should handle h (left movement)', () => {
const { result } = renderVimHook();
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'h' }));
@@ -283,7 +276,6 @@ describe('useVim hook', () => {
it('should handle l (right movement)', () => {
const { result } = renderVimHook();
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'l' }));
@@ -295,7 +287,6 @@ describe('useVim hook', () => {
it('should handle j (down movement)', () => {
const testBuffer = createMockBuffer('first line\nsecond line');
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'j' }));
@@ -307,7 +298,6 @@ describe('useVim hook', () => {
it('should handle k (up movement)', () => {
const testBuffer = createMockBuffer('first line\nsecond line');
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'k' }));
@@ -318,7 +308,6 @@ describe('useVim hook', () => {
it('should handle 0 (move to start of line)', () => {
const { result } = renderVimHook();
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: '0' }));
@@ -329,7 +318,6 @@ describe('useVim hook', () => {
it('should handle $ (move to end of line)', () => {
const { result } = renderVimHook();
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: '$' }));
@@ -342,7 +330,6 @@ describe('useVim hook', () => {
describe('Mode switching commands', () => {
it('should handle a (append after cursor)', () => {
const { result } = renderVimHook();
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'a' }));
@@ -354,7 +341,6 @@ describe('useVim hook', () => {
it('should handle A (append at end of line)', () => {
const { result } = renderVimHook();
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'A' }));
@@ -366,7 +352,6 @@ describe('useVim hook', () => {
it('should handle o (open line below)', () => {
const { result } = renderVimHook();
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'o' }));
@@ -378,7 +363,6 @@ describe('useVim hook', () => {
it('should handle O (open line above)', () => {
const { result } = renderVimHook();
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'O' }));
@@ -392,7 +376,6 @@ describe('useVim hook', () => {
describe('Edit commands', () => {
it('should handle x (delete character)', () => {
const { result } = renderVimHook();
exitInsertMode(result);
vi.clearAllMocks();
act(() => {
@@ -405,7 +388,6 @@ describe('useVim hook', () => {
it('should move cursor left when deleting last character on line (vim behavior)', () => {
const testBuffer = createMockBuffer('hello', [0, 4]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'x' }));
@@ -416,7 +398,6 @@ describe('useVim hook', () => {
it('should handle first d key (sets pending state)', () => {
const { result } = renderVimHook();
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'd' }));
@@ -429,7 +410,6 @@ describe('useVim hook', () => {
describe('Count handling', () => {
it('should handle count input and return to count 0 after command', () => {
const { result } = renderVimHook();
exitInsertMode(result);
act(() => {
const handled = result.current.handleInput(
@@ -451,7 +431,6 @@ describe('useVim hook', () => {
it('should only delete 1 character with x command when no count is specified', () => {
const testBuffer = createMockBuffer();
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'x' }));
@@ -467,7 +446,7 @@ describe('useVim hook', () => {
const { result } = renderVimHook(testBuffer);
expect(result.current.vimModeEnabled).toBe(true);
expect(result.current.mode).toBe('INSERT');
expect(result.current.mode).toBe('NORMAL');
expect(result.current.handleInput).toBeDefined();
});
@@ -479,7 +458,7 @@ describe('useVim hook', () => {
const { result } = renderVimHook(testBuffer);
expect(result.current.vimModeEnabled).toBe(true);
expect(result.current.mode).toBe('INSERT');
expect(result.current.mode).toBe('NORMAL');
expect(result.current.handleInput).toBeDefined();
expect(testBuffer.replaceRangeByOffset).toBeDefined();
expect(testBuffer.moveToOffset).toBeDefined();
@@ -488,7 +467,6 @@ describe('useVim hook', () => {
it('should handle w (next word)', () => {
const testBuffer = createMockBuffer('hello world test');
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'w' }));
@@ -500,7 +478,6 @@ describe('useVim hook', () => {
it('should handle b (previous word)', () => {
const testBuffer = createMockBuffer('hello world test', [0, 6]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'b' }));
@@ -512,7 +489,6 @@ describe('useVim hook', () => {
it('should handle e (end of word)', () => {
const testBuffer = createMockBuffer('hello world test');
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'e' }));
@@ -524,7 +500,6 @@ describe('useVim hook', () => {
it('should handle w when cursor is on the last word', () => {
const testBuffer = createMockBuffer('hello world', [0, 8]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'w' }));
@@ -535,7 +510,6 @@ describe('useVim hook', () => {
it('should handle first c key (sets pending change state)', () => {
const { result } = renderVimHook();
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'c' }));
@@ -589,7 +563,6 @@ describe('useVim hook', () => {
it('should repeat x command from current cursor position', () => {
const testBuffer = createMockBuffer('abcd\nefgh\nijkl', [0, 1]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'x' }));
@@ -607,7 +580,6 @@ describe('useVim hook', () => {
it('should repeat dd command from current position', () => {
const testBuffer = createMockBuffer('line1\nline2\nline3', [1, 0]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'd' }));
@@ -629,7 +601,6 @@ describe('useVim hook', () => {
it('should repeat ce command from current position', () => {
const testBuffer = createMockBuffer('word', [0, 0]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'c' }));
@@ -654,7 +625,6 @@ describe('useVim hook', () => {
it('should repeat cc command from current position', () => {
const testBuffer = createMockBuffer('line1\nline2\nline3', [1, 2]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'c' }));
@@ -679,7 +649,6 @@ describe('useVim hook', () => {
it('should repeat cw command from current position', () => {
const testBuffer = createMockBuffer('hello world test', [0, 6]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'c' }));
@@ -704,7 +673,6 @@ describe('useVim hook', () => {
it('should repeat D command from current position', () => {
const testBuffer = createMockBuffer('hello world test', [0, 6]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'D' }));
@@ -724,7 +692,6 @@ describe('useVim hook', () => {
it('should repeat C command from current position', () => {
const testBuffer = createMockBuffer('hello world test', [0, 6]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'C' }));
@@ -746,7 +713,6 @@ describe('useVim hook', () => {
it('should repeat command after cursor movement', () => {
const testBuffer = createMockBuffer('test text', [0, 0]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'x' }));
@@ -762,10 +728,8 @@ describe('useVim hook', () => {
});
it('should move cursor to the correct position after exiting INSERT mode with "a"', () => {
const testBuffer = createMockBuffer('hello world', [0, 11]);
const testBuffer = createMockBuffer('hello world', [0, 10]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
expect(testBuffer.cursor).toEqual([0, 10]);
act(() => {
result.current.handleInput(createKey({ sequence: 'a' }));
@@ -783,7 +747,6 @@ describe('useVim hook', () => {
it('should handle ^ (move to first non-whitespace character)', () => {
const testBuffer = createMockBuffer(' hello world', [0, 5]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: '^' }));
@@ -795,7 +758,6 @@ describe('useVim hook', () => {
it('should handle G without count (go to last line)', () => {
const testBuffer = createMockBuffer('line1\nline2\nline3', [0, 0]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'G' }));
@@ -807,7 +769,6 @@ describe('useVim hook', () => {
it('should handle gg (go to first line)', () => {
const testBuffer = createMockBuffer('line1\nline2\nline3', [2, 0]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
// First 'g' sets pending state
act(() => {
@@ -825,7 +786,6 @@ describe('useVim hook', () => {
it('should handle count with movement commands', () => {
const testBuffer = createMockBuffer('hello world test', [0, 0]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: '3' }));
@@ -844,7 +804,6 @@ describe('useVim hook', () => {
it('should delete from cursor to start of next word', () => {
const testBuffer = createMockBuffer('hello world test', [0, 0]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'd' }));
@@ -929,7 +888,6 @@ describe('useVim hook', () => {
it('should delete multiple words with count', () => {
const testBuffer = createMockBuffer('one two three four', [0, 0]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: '2' }));
@@ -947,7 +905,6 @@ describe('useVim hook', () => {
it('should record command for repeat with dot', () => {
const testBuffer = createMockBuffer('hello world test', [0, 0]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
// Execute dw
act(() => {
@@ -972,7 +929,6 @@ describe('useVim hook', () => {
it('should delete from cursor to end of current word', () => {
const testBuffer = createMockBuffer('hello world test', [0, 1]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'd' }));
@@ -987,7 +943,6 @@ describe('useVim hook', () => {
it('should handle count with de', () => {
const testBuffer = createMockBuffer('one two three four', [0, 0]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: '3' }));
@@ -1007,7 +962,6 @@ describe('useVim hook', () => {
it('should change from cursor to start of next word and enter INSERT mode', () => {
const testBuffer = createMockBuffer('hello world test', [0, 0]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'c' }));
@@ -1024,7 +978,6 @@ describe('useVim hook', () => {
it('should handle count with cw', () => {
const testBuffer = createMockBuffer('one two three four', [0, 0]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: '2' }));
@@ -1043,7 +996,6 @@ describe('useVim hook', () => {
it('should be repeatable with dot', () => {
const testBuffer = createMockBuffer('hello world test more', [0, 0]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
// Execute cw
act(() => {
@@ -1073,7 +1025,6 @@ describe('useVim hook', () => {
it('should change from cursor to end of word and enter INSERT mode', () => {
const testBuffer = createMockBuffer('hello world test', [0, 1]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'c' }));
@@ -1089,7 +1040,6 @@ describe('useVim hook', () => {
it('should handle count with ce', () => {
const testBuffer = createMockBuffer('one two three four', [0, 0]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: '2' }));
@@ -1110,7 +1060,6 @@ describe('useVim hook', () => {
it('should change entire line and enter INSERT mode', () => {
const testBuffer = createMockBuffer('hello world\nsecond line', [0, 5]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'c' }));
@@ -1129,7 +1078,6 @@ describe('useVim hook', () => {
[1, 0],
);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: '3' }));
@@ -1148,7 +1096,6 @@ describe('useVim hook', () => {
it('should be repeatable with dot', () => {
const testBuffer = createMockBuffer('line1\nline2\nline3', [0, 0]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
// Execute cc
act(() => {
@@ -1178,7 +1125,6 @@ describe('useVim hook', () => {
it('should delete from cursor to start of previous word', () => {
const testBuffer = createMockBuffer('hello world test', [0, 11]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'd' }));
@@ -1193,7 +1139,6 @@ describe('useVim hook', () => {
it('should handle count with db', () => {
const testBuffer = createMockBuffer('one two three four', [0, 18]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: '2' }));
@@ -1213,7 +1158,6 @@ describe('useVim hook', () => {
it('should change from cursor to start of previous word and enter INSERT mode', () => {
const testBuffer = createMockBuffer('hello world test', [0, 11]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: 'c' }));
@@ -1229,7 +1173,6 @@ describe('useVim hook', () => {
it('should handle count with cb', () => {
const testBuffer = createMockBuffer('one two three four', [0, 18]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
act(() => {
result.current.handleInput(createKey({ sequence: '3' }));
@@ -1250,7 +1193,6 @@ describe('useVim hook', () => {
it('should clear pending delete state after dw', () => {
const testBuffer = createMockBuffer('hello world', [0, 0]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
// Press 'd' to enter pending delete state
act(() => {
@@ -1278,7 +1220,6 @@ describe('useVim hook', () => {
it('should clear pending change state after cw', () => {
const testBuffer = createMockBuffer('hello world', [0, 0]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
// Execute cw
act(() => {
@@ -1305,7 +1246,6 @@ describe('useVim hook', () => {
it('should clear pending state with escape', () => {
const testBuffer = createMockBuffer('hello world', [0, 0]);
const { result } = renderVimHook(testBuffer);
exitInsertMode(result);
// Enter pending delete state
act(() => {
@@ -1681,7 +1621,7 @@ describe('useVim hook', () => {
beforeEach(() => {
mockBuffer = createMockBuffer('hello world');
mockVimContext.vimEnabled = true;
mockVimContext.vimMode = 'INSERT';
mockVimContext.vimMode = 'NORMAL';
mockHandleFinalSubmit = vi.fn();
vi.useFakeTimers();
});
@@ -1694,11 +1634,6 @@ describe('useVim hook', () => {
const { result } = renderHook(() =>
useVim(mockBuffer as TextBuffer, mockHandleFinalSubmit),
);
exitInsertMode(result);
// Wait to clear escape history
await act(async () => {
vi.advanceTimersByTime(600);
});
// First escape - should pass through (return false)
let handled: boolean;
@@ -1716,6 +1651,7 @@ describe('useVim hook', () => {
});
it('should clear buffer on double-escape in INSERT mode', async () => {
mockVimContext.vimMode = 'INSERT';
const { result } = renderHook(() =>
useVim(mockBuffer as TextBuffer, mockHandleFinalSubmit),
);
@@ -1740,11 +1676,6 @@ describe('useVim hook', () => {
const { result } = renderHook(() =>
useVim(mockBuffer as TextBuffer, mockHandleFinalSubmit),
);
exitInsertMode(result);
// Wait to clear escape history
await act(async () => {
vi.advanceTimersByTime(600);
});
// First escape
await act(async () => {
@@ -1770,11 +1701,6 @@ describe('useVim hook', () => {
const { result } = renderHook(() =>
useVim(mockBuffer as TextBuffer, mockHandleFinalSubmit),
);
exitInsertMode(result);
// Wait to clear escape history
await act(async () => {
vi.advanceTimersByTime(600);
});
// First escape
await act(async () => {
@@ -1804,7 +1730,6 @@ describe('useVim hook', () => {
const { result } = renderHook(() =>
useVim(mockBuffer as TextBuffer, mockHandleFinalSubmit),
);
exitInsertMode(result);
let handled: boolean;
await act(async () => {
@@ -1815,6 +1740,7 @@ describe('useVim hook', () => {
});
it('should pass Ctrl+C through to InputPrompt in INSERT mode', async () => {
mockVimContext.vimMode = 'INSERT';
const { result } = renderHook(() =>
useVim(mockBuffer as TextBuffer, mockHandleFinalSubmit),
);
+5 -5
View File
@@ -68,7 +68,7 @@ type VimAction =
| { type: 'ESCAPE_TO_NORMAL' };
const initialVimState: VimState = {
mode: 'INSERT',
mode: 'NORMAL',
count: 0,
pendingOperator: null,
lastCommand: null,
@@ -312,7 +312,9 @@ export function useVim(buffer: TextBuffer, onSubmit?: (value: string) => void) {
return true; // Handled by vim (even if no onSubmit callback)
}
return buffer.handleInput(normalizedKey);
// useKeypress already provides the correct format for TextBuffer
buffer.handleInput(normalizedKey);
return true; // Handled by vim
},
[buffer, dispatch, updateMode, onSubmit, checkDoubleEscape],
);
@@ -782,9 +784,7 @@ export function useVim(buffer: TextBuffer, onSubmit?: (value: string) => void) {
// Unknown command, clear count and pending states
dispatch({ type: 'CLEAR_PENDING_STATES' });
// Not handled by vim so allow other handlers to process it.
return false;
return true; // Still handled by vim to prevent other handlers
}
}
}
+1 -14
View File
@@ -17,24 +17,11 @@ import { cleanupExpiredSessions } from './sessionCleanup.js';
import { type SessionInfo, getAllSessionFiles } from './sessionUtils.js';
// Mock the fs module
vi.mock('node:fs/promises');
vi.mock('fs/promises');
vi.mock('./sessionUtils.js', () => ({
getAllSessionFiles: vi.fn(),
}));
vi.mock('@google/gemini-cli-core', async (importOriginal) => {
const actual =
await importOriginal<typeof import('@google/gemini-cli-core')>();
return {
...actual,
Storage: class MockStorage {
getProjectTempDir() {
return '/tmp/test-project';
}
},
};
});
const mockFs = vi.mocked(fs);
const mockGetAllSessionFiles = vi.mocked(getAllSessionFiles);
+1 -151
View File
@@ -6,12 +6,7 @@
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import {
debugLogger,
Storage,
TOOL_OUTPUT_DIR,
type Config,
} from '@google/gemini-cli-core';
import { debugLogger, type Config } from '@google/gemini-cli-core';
import type { Settings, SessionRetentionSettings } from '../config/settings.js';
import { getAllSessionFiles, type SessionFileEntry } from './sessionUtils.js';
@@ -314,148 +309,3 @@ function validateRetentionConfig(
return null;
}
/**
* Result of tool output cleanup operation
*/
export interface ToolOutputCleanupResult {
disabled: boolean;
scanned: number;
deleted: number;
failed: number;
}
/**
* Cleans up tool output files based on age and count limits.
* Uses the same retention settings as session cleanup.
*/
export async function cleanupToolOutputFiles(
settings: Settings,
debugMode: boolean = false,
projectTempDir?: string,
): Promise<ToolOutputCleanupResult> {
const result: ToolOutputCleanupResult = {
disabled: false,
scanned: 0,
deleted: 0,
failed: 0,
};
try {
// Early exit if cleanup is disabled
if (!settings.general?.sessionRetention?.enabled) {
return { ...result, disabled: true };
}
const retentionConfig = settings.general.sessionRetention;
const tempDir =
projectTempDir ?? new Storage(process.cwd()).getProjectTempDir();
const toolOutputDir = path.join(tempDir, TOOL_OUTPUT_DIR);
// Check if directory exists
try {
await fs.access(toolOutputDir);
} catch {
// Directory doesn't exist, nothing to clean up
return result;
}
// Get all files in the tool_output directory
const entries = await fs.readdir(toolOutputDir, { withFileTypes: true });
const files = entries.filter((e) => e.isFile());
result.scanned = files.length;
if (files.length === 0) {
return result;
}
// Get file stats for age-based cleanup (parallel for better performance)
const fileStatsResults = await Promise.all(
files.map(async (file) => {
try {
const filePath = path.join(toolOutputDir, file.name);
const stat = await fs.stat(filePath);
return { name: file.name, mtime: stat.mtime };
} catch (error) {
debugLogger.debug(
`Failed to stat file ${file.name}: ${error instanceof Error ? error.message : 'Unknown error'}`,
);
return null;
}
}),
);
const fileStats = fileStatsResults.filter(
(f): f is { name: string; mtime: Date } => f !== null,
);
// Sort by mtime (oldest first)
fileStats.sort((a, b) => a.mtime.getTime() - b.mtime.getTime());
const now = new Date();
const filesToDelete: string[] = [];
// Age-based cleanup: delete files older than maxAge
if (retentionConfig.maxAge) {
try {
const maxAgeMs = parseRetentionPeriod(retentionConfig.maxAge);
const cutoffDate = new Date(now.getTime() - maxAgeMs);
for (const file of fileStats) {
if (file.mtime < cutoffDate) {
filesToDelete.push(file.name);
}
}
} catch (error) {
debugLogger.debug(
`Invalid maxAge format, skipping age-based cleanup: ${error instanceof Error ? error.message : 'Unknown error'}`,
);
}
}
// Count-based cleanup: after age-based cleanup, if we still have more files
// than maxCount, delete the oldest ones to bring the count down.
// This ensures we keep at most maxCount files, preferring newer ones.
if (retentionConfig.maxCount !== undefined) {
// Filter out files already marked for deletion by age-based cleanup
const remainingFiles = fileStats.filter(
(f) => !filesToDelete.includes(f.name),
);
if (remainingFiles.length > retentionConfig.maxCount) {
// Calculate how many excess files need to be deleted
const excessCount = remainingFiles.length - retentionConfig.maxCount;
// remainingFiles is already sorted oldest first, so delete from the start
for (let i = 0; i < excessCount; i++) {
filesToDelete.push(remainingFiles[i].name);
}
}
}
// Delete the files
for (const fileName of filesToDelete) {
try {
const filePath = path.join(toolOutputDir, fileName);
await fs.unlink(filePath);
result.deleted++;
} catch (error) {
debugLogger.debug(
`Failed to delete file ${fileName}: ${error instanceof Error ? error.message : 'Unknown error'}`,
);
result.failed++;
}
}
if (debugMode && result.deleted > 0) {
debugLogger.debug(
`Tool output cleanup: deleted ${result.deleted}, failed ${result.failed}`,
);
}
} catch (error) {
// Global error handler - don't let cleanup failures break startup
const errorMessage =
error instanceof Error ? error.message : 'Unknown error';
debugLogger.warn(`Tool output cleanup failed: ${errorMessage}`);
result.failed++;
}
return result;
}
@@ -1,285 +0,0 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import * as os from 'node:os';
import { debugLogger, TOOL_OUTPUT_DIR } from '@google/gemini-cli-core';
import type { Settings } from '../config/settings.js';
import { cleanupToolOutputFiles } from './sessionCleanup.js';
describe('Tool Output Cleanup', () => {
let testTempDir: string;
beforeEach(async () => {
// Create a unique temp directory for each test
testTempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'tool-output-test-'));
vi.spyOn(debugLogger, 'error').mockImplementation(() => {});
vi.spyOn(debugLogger, 'warn').mockImplementation(() => {});
vi.spyOn(debugLogger, 'debug').mockImplementation(() => {});
});
afterEach(async () => {
vi.restoreAllMocks();
// Clean up the temp directory
try {
await fs.rm(testTempDir, { recursive: true, force: true });
} catch {
// Ignore cleanup errors
}
});
describe('cleanupToolOutputFiles', () => {
it('should return early when cleanup is disabled', async () => {
const settings: Settings = {
general: { sessionRetention: { enabled: false } },
};
const result = await cleanupToolOutputFiles(settings, false, testTempDir);
expect(result.disabled).toBe(true);
expect(result.scanned).toBe(0);
expect(result.deleted).toBe(0);
expect(result.failed).toBe(0);
});
it('should return early when sessionRetention is not configured', async () => {
const settings: Settings = {};
const result = await cleanupToolOutputFiles(settings, false, testTempDir);
expect(result.disabled).toBe(true);
expect(result.scanned).toBe(0);
expect(result.deleted).toBe(0);
});
it('should return early when tool_output directory does not exist', async () => {
const settings: Settings = {
general: {
sessionRetention: {
enabled: true,
maxAge: '7d',
},
},
};
// Don't create the tool_output directory
const result = await cleanupToolOutputFiles(settings, false, testTempDir);
expect(result.disabled).toBe(false);
expect(result.scanned).toBe(0);
expect(result.deleted).toBe(0);
expect(result.failed).toBe(0);
});
it('should delete files older than maxAge', async () => {
const settings: Settings = {
general: {
sessionRetention: {
enabled: true,
maxAge: '7d',
},
},
};
// Create tool_output directory and files
const toolOutputDir = path.join(testTempDir, TOOL_OUTPUT_DIR);
await fs.mkdir(toolOutputDir, { recursive: true });
const now = Date.now();
const fiveDaysAgo = now - 5 * 24 * 60 * 60 * 1000;
const tenDaysAgo = now - 10 * 24 * 60 * 60 * 1000;
// Create files with different ages
const recentFile = path.join(toolOutputDir, 'shell_recent.txt');
const oldFile = path.join(toolOutputDir, 'shell_old.txt');
await fs.writeFile(recentFile, 'recent content');
await fs.writeFile(oldFile, 'old content');
// Set file modification times
await fs.utimes(recentFile, fiveDaysAgo / 1000, fiveDaysAgo / 1000);
await fs.utimes(oldFile, tenDaysAgo / 1000, tenDaysAgo / 1000);
const result = await cleanupToolOutputFiles(settings, false, testTempDir);
expect(result.disabled).toBe(false);
expect(result.scanned).toBe(2);
expect(result.deleted).toBe(1); // Only the 10-day-old file should be deleted
expect(result.failed).toBe(0);
// Verify the old file was deleted and recent file remains
const remainingFiles = await fs.readdir(toolOutputDir);
expect(remainingFiles).toContain('shell_recent.txt');
expect(remainingFiles).not.toContain('shell_old.txt');
});
it('should delete oldest files when exceeding maxCount', async () => {
const settings: Settings = {
general: {
sessionRetention: {
enabled: true,
maxCount: 2,
},
},
};
// Create tool_output directory and files
const toolOutputDir = path.join(testTempDir, TOOL_OUTPUT_DIR);
await fs.mkdir(toolOutputDir, { recursive: true });
const now = Date.now();
const oneDayAgo = now - 1 * 24 * 60 * 60 * 1000;
const twoDaysAgo = now - 2 * 24 * 60 * 60 * 1000;
const threeDaysAgo = now - 3 * 24 * 60 * 60 * 1000;
// Create 3 files with different ages
const file1 = path.join(toolOutputDir, 'shell_1.txt');
const file2 = path.join(toolOutputDir, 'shell_2.txt');
const file3 = path.join(toolOutputDir, 'shell_3.txt');
await fs.writeFile(file1, 'content 1');
await fs.writeFile(file2, 'content 2');
await fs.writeFile(file3, 'content 3');
// Set file modification times (file3 is oldest)
await fs.utimes(file1, oneDayAgo / 1000, oneDayAgo / 1000);
await fs.utimes(file2, twoDaysAgo / 1000, twoDaysAgo / 1000);
await fs.utimes(file3, threeDaysAgo / 1000, threeDaysAgo / 1000);
const result = await cleanupToolOutputFiles(settings, false, testTempDir);
expect(result.disabled).toBe(false);
expect(result.scanned).toBe(3);
expect(result.deleted).toBe(1); // Should delete 1 file to get down to maxCount of 2
expect(result.failed).toBe(0);
// Verify the oldest file was deleted
const remainingFiles = await fs.readdir(toolOutputDir);
expect(remainingFiles).toHaveLength(2);
expect(remainingFiles).not.toContain('shell_3.txt');
});
it('should handle empty directory', async () => {
const settings: Settings = {
general: {
sessionRetention: {
enabled: true,
maxAge: '7d',
},
},
};
// Create empty tool_output directory
const toolOutputDir = path.join(testTempDir, TOOL_OUTPUT_DIR);
await fs.mkdir(toolOutputDir, { recursive: true });
const result = await cleanupToolOutputFiles(settings, false, testTempDir);
expect(result.disabled).toBe(false);
expect(result.scanned).toBe(0);
expect(result.deleted).toBe(0);
expect(result.failed).toBe(0);
});
it('should apply both maxAge and maxCount together', async () => {
const settings: Settings = {
general: {
sessionRetention: {
enabled: true,
maxAge: '3d',
maxCount: 2,
},
},
};
// Create tool_output directory and files
const toolOutputDir = path.join(testTempDir, TOOL_OUTPUT_DIR);
await fs.mkdir(toolOutputDir, { recursive: true });
const now = Date.now();
const oneDayAgo = now - 1 * 24 * 60 * 60 * 1000;
const twoDaysAgo = now - 2 * 24 * 60 * 60 * 1000;
const twoAndHalfDaysAgo = now - 2.5 * 24 * 60 * 60 * 1000;
const fiveDaysAgo = now - 5 * 24 * 60 * 60 * 1000;
const tenDaysAgo = now - 10 * 24 * 60 * 60 * 1000;
// Create 5 files with different ages
const file1 = path.join(toolOutputDir, 'shell_1.txt'); // 1 day old - keep
const file2 = path.join(toolOutputDir, 'shell_2.txt'); // 2 days old - keep
const file3 = path.join(toolOutputDir, 'shell_3.txt'); // 2.5 days old - delete by count
const file4 = path.join(toolOutputDir, 'shell_4.txt'); // 5 days old - delete by age
const file5 = path.join(toolOutputDir, 'shell_5.txt'); // 10 days old - delete by age
await fs.writeFile(file1, 'content 1');
await fs.writeFile(file2, 'content 2');
await fs.writeFile(file3, 'content 3');
await fs.writeFile(file4, 'content 4');
await fs.writeFile(file5, 'content 5');
// Set file modification times
await fs.utimes(file1, oneDayAgo / 1000, oneDayAgo / 1000);
await fs.utimes(file2, twoDaysAgo / 1000, twoDaysAgo / 1000);
await fs.utimes(
file3,
twoAndHalfDaysAgo / 1000,
twoAndHalfDaysAgo / 1000,
);
await fs.utimes(file4, fiveDaysAgo / 1000, fiveDaysAgo / 1000);
await fs.utimes(file5, tenDaysAgo / 1000, tenDaysAgo / 1000);
const result = await cleanupToolOutputFiles(settings, false, testTempDir);
expect(result.disabled).toBe(false);
expect(result.scanned).toBe(5);
// file4 and file5 deleted by maxAge, file3 deleted by maxCount
expect(result.deleted).toBe(3);
expect(result.failed).toBe(0);
// Verify only the 2 newest files remain
const remainingFiles = await fs.readdir(toolOutputDir);
expect(remainingFiles).toHaveLength(2);
expect(remainingFiles).toContain('shell_1.txt');
expect(remainingFiles).toContain('shell_2.txt');
expect(remainingFiles).not.toContain('shell_3.txt');
expect(remainingFiles).not.toContain('shell_4.txt');
expect(remainingFiles).not.toContain('shell_5.txt');
});
it('should log debug information when enabled', async () => {
const settings: Settings = {
general: {
sessionRetention: {
enabled: true,
maxAge: '1d',
},
},
};
// Create tool_output directory and an old file
const toolOutputDir = path.join(testTempDir, TOOL_OUTPUT_DIR);
await fs.mkdir(toolOutputDir, { recursive: true });
const tenDaysAgo = Date.now() - 10 * 24 * 60 * 60 * 1000;
const oldFile = path.join(toolOutputDir, 'shell_old.txt');
await fs.writeFile(oldFile, 'old content');
await fs.utimes(oldFile, tenDaysAgo / 1000, tenDaysAgo / 1000);
const debugSpy = vi
.spyOn(debugLogger, 'debug')
.mockImplementation(() => {});
await cleanupToolOutputFiles(settings, true, testTempDir);
expect(debugSpy).toHaveBeenCalledWith(
expect.stringContaining('Tool output cleanup: deleted'),
);
debugSpy.mockRestore();
});
});
});
@@ -338,152 +338,6 @@ describe('createContentGenerator', () => {
new LoggingContentGenerator(mockGenerator.models, mockConfig),
);
});
it('should pass apiVersion to GoogleGenAI when GOOGLE_GENAI_API_VERSION is set', async () => {
const mockConfig = {
getModel: vi.fn().mockReturnValue('gemini-pro'),
getProxy: vi.fn().mockReturnValue(undefined),
getUsageStatisticsEnabled: () => false,
getPreviewFeatures: vi.fn().mockReturnValue(false),
} as unknown as Config;
const mockGenerator = {
models: {},
} as unknown as GoogleGenAI;
vi.mocked(GoogleGenAI).mockImplementation(() => mockGenerator as never);
vi.stubEnv('GOOGLE_GENAI_API_VERSION', 'v1');
await createContentGenerator(
{
apiKey: 'test-api-key',
authType: AuthType.USE_GEMINI,
},
mockConfig,
);
expect(GoogleGenAI).toHaveBeenCalledWith({
apiKey: 'test-api-key',
vertexai: undefined,
httpOptions: {
headers: expect.objectContaining({
'User-Agent': expect.any(String),
}),
},
apiVersion: 'v1',
});
});
it('should not include apiVersion when GOOGLE_GENAI_API_VERSION is not set', async () => {
const mockConfig = {
getModel: vi.fn().mockReturnValue('gemini-pro'),
getProxy: vi.fn().mockReturnValue(undefined),
getUsageStatisticsEnabled: () => false,
getPreviewFeatures: vi.fn().mockReturnValue(false),
} as unknown as Config;
const mockGenerator = {
models: {},
} as unknown as GoogleGenAI;
vi.mocked(GoogleGenAI).mockImplementation(() => mockGenerator as never);
await createContentGenerator(
{
apiKey: 'test-api-key',
authType: AuthType.USE_GEMINI,
},
mockConfig,
);
expect(GoogleGenAI).toHaveBeenCalledWith({
apiKey: 'test-api-key',
vertexai: undefined,
httpOptions: {
headers: expect.objectContaining({
'User-Agent': expect.any(String),
}),
},
});
expect(GoogleGenAI).toHaveBeenCalledWith(
expect.not.objectContaining({
apiVersion: expect.any(String),
}),
);
});
it('should not include apiVersion when GOOGLE_GENAI_API_VERSION is an empty string', async () => {
const mockConfig = {
getModel: vi.fn().mockReturnValue('gemini-pro'),
getProxy: vi.fn().mockReturnValue(undefined),
getUsageStatisticsEnabled: () => false,
getPreviewFeatures: vi.fn().mockReturnValue(false),
} as unknown as Config;
const mockGenerator = {
models: {},
} as unknown as GoogleGenAI;
vi.mocked(GoogleGenAI).mockImplementation(() => mockGenerator as never);
vi.stubEnv('GOOGLE_GENAI_API_VERSION', '');
await createContentGenerator(
{
apiKey: 'test-api-key',
authType: AuthType.USE_GEMINI,
},
mockConfig,
);
expect(GoogleGenAI).toHaveBeenCalledWith({
apiKey: 'test-api-key',
vertexai: undefined,
httpOptions: {
headers: expect.objectContaining({
'User-Agent': expect.any(String),
}),
},
});
expect(GoogleGenAI).toHaveBeenCalledWith(
expect.not.objectContaining({
apiVersion: expect.any(String),
}),
);
});
it('should pass apiVersion for Vertex AI when GOOGLE_GENAI_API_VERSION is set', async () => {
const mockConfig = {
getModel: vi.fn().mockReturnValue('gemini-pro'),
getProxy: vi.fn().mockReturnValue(undefined),
getUsageStatisticsEnabled: () => false,
getPreviewFeatures: vi.fn().mockReturnValue(false),
} as unknown as Config;
const mockGenerator = {
models: {},
} as unknown as GoogleGenAI;
vi.mocked(GoogleGenAI).mockImplementation(() => mockGenerator as never);
vi.stubEnv('GOOGLE_GENAI_API_VERSION', 'v1alpha');
await createContentGenerator(
{
apiKey: 'test-api-key',
vertexai: true,
authType: AuthType.USE_VERTEX_AI,
},
mockConfig,
);
expect(GoogleGenAI).toHaveBeenCalledWith({
apiKey: 'test-api-key',
vertexai: true,
httpOptions: {
headers: expect.objectContaining({
'User-Agent': expect.any(String),
}),
},
apiVersion: 'v1alpha',
});
});
});
describe('createContentGeneratorConfig', () => {
@@ -132,7 +132,6 @@ export async function createContentGenerator(
const customHeadersMap = parseCustomHeaders(customHeadersEnv);
const apiKeyAuthMechanism =
process.env['GEMINI_API_KEY_AUTH_MECHANISM'] || 'x-goog-api-key';
const apiVersionEnv = process.env['GOOGLE_GENAI_API_VERSION'];
const baseHeaders: Record<string, string> = {
...customHeadersMap,
@@ -182,7 +181,6 @@ export async function createContentGenerator(
apiKey: config.apiKey === '' ? undefined : config.apiKey,
vertexai: config.vertexai,
httpOptions,
...(apiVersionEnv && { apiVersion: apiVersionEnv }),
});
return new LoggingContentGenerator(googleGenAI.models, gcConfig);
}
@@ -16,7 +16,6 @@ import type { BaseLlmClient } from '../core/baseLlmClient.js';
import type { GeminiChat } from '../core/geminiChat.js';
import type { Config } from '../config/config.js';
import * as fileUtils from '../utils/fileUtils.js';
import { TOOL_OUTPUT_DIR } from '../utils/fileUtils.js';
import { getInitialChatHistory } from '../utils/environmentContext.js';
import * as tokenCalculation from '../utils/tokenCalculation.js';
import { tokenLimit } from '../core/tokenLimits.js';
@@ -511,9 +510,8 @@ describe('ChatCompressionService', () => {
'Output too large.',
);
// Verify a file was actually created in the tool_output subdirectory
const toolOutputDir = path.join(testTempDir, TOOL_OUTPUT_DIR);
const files = fs.readdirSync(toolOutputDir);
// Verify a file was actually created
const files = fs.readdirSync(testTempDir);
expect(files.length).toBeGreaterThan(0);
expect(files[0]).toMatch(/grep_.*\.txt/);
});
+15 -9
View File
@@ -6,7 +6,6 @@
import * as fsPromises from 'node:fs/promises';
import * as path from 'node:path';
import * as os from 'node:os';
import * as crypto from 'node:crypto';
import * as Diff from 'diff';
import {
@@ -35,7 +34,7 @@ import {
} from './modifiable-tool.js';
import { IdeClient } from '../ide/ide-client.js';
import { FixLLMEditWithInstruction } from '../utils/llm-edit-fixer.js';
import { safeLiteralReplace, detectLineEnding } from '../utils/textUtils.js';
import { safeLiteralReplace } from '../utils/textUtils.js';
import { EditStrategyEvent } from '../telemetry/types.js';
import { logEditStrategy } from '../telemetry/loggers.js';
import { EditCorrectionEvent } from '../telemetry/types.js';
@@ -259,6 +258,17 @@ async function calculateRegexReplacement(
};
}
/**
* Detects the line ending style of a string.
* @param content The string content to analyze.
* @returns '\r\n' for Windows-style, '\n' for Unix-style.
*/
function detectLineEnding(content: string): '\r\n' | '\n' {
// If a Carriage Return is found, assume Windows-style endings.
// This is a simple but effective heuristic.
return content.includes('\r\n') ? '\r\n' : '\n';
}
export async function calculateReplacement(
config: Config,
context: ReplacementContext,
@@ -802,13 +812,9 @@ class EditToolInvocation
await this.ensureParentDirectoriesExistAsync(this.params.file_path);
let finalContent = editData.newContent;
// Restore original line endings if they were CRLF, or use OS default for new files
const useCRLF =
(!editData.isNewFile && editData.originalLineEnding === '\r\n') ||
(editData.isNewFile && os.EOL === '\r\n');
if (useCRLF) {
finalContent = finalContent.replace(/\r?\n/g, '\r\n');
// Restore original line endings if they were CRLF
if (!editData.isNewFile && editData.originalLineEnding === '\r\n') {
finalContent = finalContent.replace(/\n/g, '\r\n');
}
await this.config
.getFileSystemService()
@@ -1,282 +0,0 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {
describe,
it,
expect,
beforeEach,
afterEach,
vi,
type Mocked,
} from 'vitest';
import { detectLineEnding } from '../utils/textUtils.js';
import { WriteFileTool } from './write-file.js';
import { EditTool } from './edit.js';
import type { Config } from '../config/config.js';
import { ApprovalMode } from '../policy/types.js';
import { ToolConfirmationOutcome } from './tools.js';
import type { ToolRegistry } from './tool-registry.js';
import path from 'node:path';
import fs from 'node:fs';
import os from 'node:os';
import { GeminiClient } from '../core/client.js';
import type { BaseLlmClient } from '../core/baseLlmClient.js';
import {
ensureCorrectEdit,
ensureCorrectFileContent,
} from '../utils/editCorrector.js';
import { StandardFileSystemService } from '../services/fileSystemService.js';
import { WorkspaceContext } from '../utils/workspaceContext.js';
import {
createMockMessageBus,
getMockMessageBusInstance,
} from '../test-utils/mock-message-bus.js';
const rootDir = path.resolve(os.tmpdir(), 'gemini-cli-line-ending-test-root');
// --- MOCKS ---
vi.mock('../core/client.js');
vi.mock('../utils/editCorrector.js');
vi.mock('../ide/ide-client.js', () => ({
IdeClient: {
getInstance: vi.fn().mockResolvedValue({
openDiff: vi.fn(),
isDiffingEnabled: vi.fn().mockReturnValue(false),
}),
},
}));
let mockGeminiClientInstance: Mocked<GeminiClient>;
let mockBaseLlmClientInstance: Mocked<BaseLlmClient>;
const mockEnsureCorrectEdit = vi.fn<typeof ensureCorrectEdit>();
const mockEnsureCorrectFileContent = vi.fn<typeof ensureCorrectFileContent>();
// Mock Config
const fsService = new StandardFileSystemService();
const mockConfigInternal = {
getTargetDir: () => rootDir,
getApprovalMode: vi.fn(() => ApprovalMode.DEFAULT),
setApprovalMode: vi.fn(),
getGeminiClient: vi.fn(),
getBaseLlmClient: vi.fn(),
getFileSystemService: () => fsService,
getIdeMode: vi.fn(() => false),
getWorkspaceContext: () => new WorkspaceContext(rootDir),
getApiKey: () => 'test-key',
getModel: () => 'test-model',
getSandbox: () => false,
getDebugMode: () => false,
getQuestion: () => undefined,
getToolDiscoveryCommand: () => undefined,
getToolCallCommand: () => undefined,
getMcpServerCommand: () => undefined,
getMcpServers: () => undefined,
getUserAgent: () => 'test-agent',
getUserMemory: () => '',
setUserMemory: vi.fn(),
getGeminiMdFileCount: () => 0,
setGeminiMdFileCount: vi.fn(),
getDisableLLMCorrection: vi.fn(() => false),
validatePathAccess: vi.fn().mockReturnValue(null),
getToolRegistry: () =>
({
registerTool: vi.fn(),
discoverTools: vi.fn(),
}) as unknown as ToolRegistry,
isInteractive: () => false,
};
const mockConfig = mockConfigInternal as unknown as Config;
vi.mock('../telemetry/loggers.js', () => ({
logFileOperation: vi.fn(),
logEditStrategy: vi.fn(),
logEditCorrectionEvent: vi.fn(),
}));
// --- END MOCKS ---
describe('Line Ending Preservation', () => {
let tempDir: string;
beforeEach(() => {
vi.clearAllMocks();
tempDir = fs.mkdtempSync(
path.join(os.tmpdir(), 'line-ending-test-external-'),
);
if (!fs.existsSync(rootDir)) {
fs.mkdirSync(rootDir, { recursive: true });
}
mockGeminiClientInstance = new (vi.mocked(GeminiClient))(
mockConfig,
) as Mocked<GeminiClient>;
vi.mocked(GeminiClient).mockImplementation(() => mockGeminiClientInstance);
mockBaseLlmClientInstance = {
generateJson: vi.fn(),
} as unknown as Mocked<BaseLlmClient>;
vi.mocked(ensureCorrectEdit).mockImplementation(mockEnsureCorrectEdit);
vi.mocked(ensureCorrectFileContent).mockImplementation(
mockEnsureCorrectFileContent,
);
mockConfigInternal.getGeminiClient.mockReturnValue(
mockGeminiClientInstance,
);
mockConfigInternal.getBaseLlmClient.mockReturnValue(
mockBaseLlmClientInstance,
);
});
afterEach(() => {
if (fs.existsSync(tempDir)) {
fs.rmSync(tempDir, { recursive: true, force: true });
}
if (fs.existsSync(rootDir)) {
fs.rmSync(rootDir, { recursive: true, force: true });
}
vi.restoreAllMocks();
});
describe('detectLineEnding', () => {
it('should detect CRLF', () => {
expect(detectLineEnding('line1\r\nline2')).toBe('\r\n');
expect(detectLineEnding('line1\r\n')).toBe('\r\n');
});
it('should detect LF', () => {
expect(detectLineEnding('line1\nline2')).toBe('\n');
expect(detectLineEnding('line1\n')).toBe('\n');
expect(detectLineEnding('line1')).toBe('\n'); // Default to LF if no newline
});
});
describe('WriteFileTool', () => {
let tool: WriteFileTool;
const abortSignal = new AbortController().signal;
beforeEach(() => {
const bus = createMockMessageBus();
getMockMessageBusInstance(bus).defaultToolDecision = 'ask_user';
tool = new WriteFileTool(mockConfig, bus);
});
it('should preserve CRLF when overwriting an existing file', async () => {
const filePath = path.join(rootDir, 'crlf_file.txt');
const originalContent = 'line1\r\nline2\r\n';
fs.writeFileSync(filePath, originalContent); // Write with CRLF (or however Node writes binary buffer)
// Ensure strictly CRLF
fs.writeFileSync(filePath, Buffer.from('line1\r\nline2\r\n'));
// Proposed content from LLM (usually LF)
const proposedContent = 'line1\nline2\nline3\n';
// Mock corrections to return proposed content as-is (but usually normalized)
mockEnsureCorrectEdit.mockResolvedValue({
params: {
file_path: filePath,
old_string: originalContent,
new_string: proposedContent,
},
occurrences: 1,
});
const params = { file_path: filePath, content: proposedContent };
const invocation = tool.build(params);
// Force approval
const confirmDetails = await invocation.shouldConfirmExecute(abortSignal);
if (
confirmDetails &&
typeof confirmDetails === 'object' &&
'onConfirm' in confirmDetails
) {
await confirmDetails.onConfirm(ToolConfirmationOutcome.ProceedOnce);
}
await invocation.execute(abortSignal);
const writtenContent = fs.readFileSync(filePath, 'utf8');
// Expect all newlines to be CRLF
expect(writtenContent).toBe('line1\r\nline2\r\nline3\r\n');
});
it('should use OS EOL for new files', async () => {
const filePath = path.join(rootDir, 'new_os_eol_file.txt');
const proposedContent = 'line1\nline2\n';
mockEnsureCorrectFileContent.mockResolvedValue(proposedContent);
const params = { file_path: filePath, content: proposedContent };
const invocation = tool.build(params);
const confirmDetails = await invocation.shouldConfirmExecute(abortSignal);
if (
confirmDetails &&
typeof confirmDetails === 'object' &&
'onConfirm' in confirmDetails
) {
await confirmDetails.onConfirm(ToolConfirmationOutcome.ProceedOnce);
}
await invocation.execute(abortSignal);
const writtenContent = fs.readFileSync(filePath, 'utf8');
if (os.EOL === '\r\n') {
expect(writtenContent).toBe('line1\r\nline2\r\n');
} else {
expect(writtenContent).toBe('line1\nline2\n');
}
});
});
describe('EditTool', () => {
let tool: EditTool;
const abortSignal = new AbortController().signal;
beforeEach(() => {
const bus = createMockMessageBus();
getMockMessageBusInstance(bus).defaultToolDecision = 'ask_user';
tool = new EditTool(mockConfig, bus);
});
it('should preserve CRLF when editing a file', async () => {
const filePath = path.join(rootDir, 'edit_crlf.txt');
const originalContent = 'line1\r\nline2\r\nline3\r\n';
fs.writeFileSync(filePath, Buffer.from(originalContent));
const oldString = 'line2';
const newString = 'modified';
const params = {
file_path: filePath,
old_string: oldString,
new_string: newString,
instruction: 'Change line2 to modified',
};
const invocation = tool.build(params);
// Force approval
const confirmDetails = await invocation.shouldConfirmExecute(abortSignal);
if (
confirmDetails &&
typeof confirmDetails === 'object' &&
'onConfirm' in confirmDetails
) {
await confirmDetails.onConfirm(ToolConfirmationOutcome.ProceedOnce);
}
await invocation.execute(abortSignal);
const writtenContent = fs.readFileSync(filePath, 'utf8');
expect(writtenContent).toBe('line1\r\nmodified\r\nline3\r\n');
});
});
});
@@ -901,7 +901,6 @@ describe('WriteFileTool', () => {
errorMessage: 'Generic write error',
expectedMessagePrefix: 'Error writing to file',
mockFsExistsSync: false,
restoreAllMocks: false,
},
])(
'should return $errorType error when write fails with $errorCode',
+1 -13
View File
@@ -7,7 +7,6 @@
import fs from 'node:fs';
import fsPromises from 'node:fs/promises';
import path from 'node:path';
import os from 'node:os';
import * as Diff from 'diff';
import { WRITE_FILE_TOOL_NAME } from './tool-names.js';
import type { Config } from '../config/config.js';
@@ -34,7 +33,6 @@ import {
ensureCorrectEdit,
ensureCorrectFileContent,
} from '../utils/editCorrector.js';
import { detectLineEnding } from '../utils/textUtils.js';
import { DEFAULT_DIFF_OPTIONS, getDiffStat } from './diffOptions.js';
import type {
ModifiableDeclarativeTool,
@@ -303,19 +301,9 @@ class WriteFileToolInvocation extends BaseToolInvocation<
await fsPromises.mkdir(dirName, { recursive: true });
}
let finalContent = fileContent;
const useCRLF =
!isNewFile && originalContent
? detectLineEnding(originalContent) === '\r\n'
: os.EOL === '\r\n';
if (useCRLF) {
finalContent = finalContent.replace(/\r?\n/g, '\r\n');
}
await this.config
.getFileSystemService()
.writeTextFile(this.resolvedPath, finalContent);
.writeTextFile(this.resolvedPath, fileContent);
// Generate diff for display result
const fileName = path.basename(this.resolvedPath);
@@ -639,33 +639,6 @@ describe('editCorrector', () => {
expect(result.params).toEqual(originalParams);
});
});
describe('Scenario Group 7: Trimming with Newline Preservation', () => {
it('Test 7.1: should preserve trailing newlines in new_string when trimming is applied', async () => {
const currentContent = ' find me'; // Matches old_string initially
const originalParams = {
file_path: '/test/file.txt',
old_string: ' find me', // Matches, but has whitespace to trim
new_string: ' replaced\n\n', // Needs trimming but preserve newlines
};
const result = await ensureCorrectEdit(
'/test/file.txt',
currentContent,
originalParams,
mockGeminiClientInstance,
mockBaseLlmClientInstance,
abortSignal,
false,
);
// old_string should be trimmed to 'find me' because 'find me' also exists uniquely in ' find me'
expect(result.params.old_string).toBe('find me');
// new_string should be trimmed of spaces but keep ALL newlines
expect(result.params.new_string).toBe('replaced\n\n');
expect(result.occurrences).toBe(1);
});
});
});
describe('ensureCorrectFileContent', () => {
+2 -10
View File
@@ -689,20 +689,13 @@ Return ONLY the corrected string in the specified JSON format with the key 'corr
}
}
function trimPreservingTrailingNewline(str: string): string {
const trimmedEnd = str.trimEnd();
const trailingWhitespace = str.slice(trimmedEnd.length);
const trailingNewlines = trailingWhitespace.replace(/[^\r\n]/g, '');
return str.trim() + trailingNewlines;
}
function trimPairIfPossible(
target: string,
trimIfTargetTrims: string,
currentContent: string,
expectedReplacements: number,
) {
const trimmedTargetString = trimPreservingTrailingNewline(target);
const trimmedTargetString = target.trim();
if (target.length !== trimmedTargetString.length) {
const trimmedTargetOccurrences = countOccurrences(
currentContent,
@@ -710,8 +703,7 @@ function trimPairIfPossible(
);
if (trimmedTargetOccurrences === expectedReplacements) {
const trimmedReactiveString =
trimPreservingTrailingNewline(trimIfTargetTrims);
const trimmedReactiveString = trimIfTargetTrims.trim();
return {
targetString: trimmedTargetString,
pair: trimmedReactiveString,
+1 -7
View File
@@ -1076,11 +1076,7 @@ describe('fileUtils', () => {
tempRootDir,
);
const expectedOutputFile = path.join(
tempRootDir,
'tool_output',
'shell_123.txt',
);
const expectedOutputFile = path.join(tempRootDir, 'shell_123.txt');
expect(result.outputFile).toBe(expectedOutputFile);
expect(result.totalLines).toBe(1);
@@ -1106,7 +1102,6 @@ describe('fileUtils', () => {
// ../../dangerous/tool -> ______dangerous_tool
const expectedOutputFile = path.join(
tempRootDir,
'tool_output',
'______dangerous_tool_1.txt',
);
expect(result.outputFile).toBe(expectedOutputFile);
@@ -1127,7 +1122,6 @@ describe('fileUtils', () => {
// ../../etc/passwd -> ______etc_passwd
const expectedOutputFile = path.join(
tempRootDir,
'tool_output',
'shell_______etc_passwd.txt',
);
expect(result.outputFile).toBe(expectedOutputFile);
+1 -5
View File
@@ -566,8 +566,6 @@ ${processedLines.join('\n')}`;
/**
* Saves tool output to a temporary file for later retrieval.
*/
export const TOOL_OUTPUT_DIR = 'tool_output';
export async function saveTruncatedToolOutput(
content: string,
toolName: string,
@@ -580,10 +578,8 @@ export async function saveTruncatedToolOutput(
.replace(/[^a-z0-9]/gi, '_')
.toLowerCase();
const fileName = `${safeToolName}_${safeId}.txt`;
const toolOutputDir = path.join(projectTempDir, TOOL_OUTPUT_DIR);
const outputFile = path.join(toolOutputDir, fileName);
const outputFile = path.join(projectTempDir, fileName);
await fsPromises.mkdir(toolOutputDir, { recursive: true });
await fsPromises.writeFile(outputFile, content);
const lines = content.split('\n');
-15
View File
@@ -6,21 +6,6 @@
import { writeToStdout } from './stdio.js';
/**
* ANSI escape codes for disabling mouse tracking.
*/
export function disableMouseTracking() {
writeToStdout(
[
'\x1b[?1000l', // Normal tracking
'\x1b[?1003l', // Any-event tracking
'\x1b[?1015l', // urxvt extended mouse mode
'\x1b[?1006l', // SGR-style mouse tracking
'\x1b[?1002l', // Button-event tracking
].join(''),
);
}
export function enableMouseEvents() {
// Enable mouse tracking with SGR format
// ?1002h = button event tracking (clicks + drags + scroll wheel)
-11
View File
@@ -54,17 +54,6 @@ export function isBinary(
return false;
}
/**
* Detects the line ending style of a string.
* @param content The string content to analyze.
* @returns '\r\n' for Windows-style, '\n' for Unix-style.
*/
export function detectLineEnding(content: string): '\r\n' | '\n' {
// If a Carriage Return is found, assume Windows-style endings.
// This is a simple but effective heuristic.
return content.includes('\r\n') ? '\r\n' : '\n';
}
/**
* Truncates a string to a maximum length, appending a suffix if truncated.
* @param str The string to truncate.
+11
View File
@@ -0,0 +1,11 @@
diff --git a/node_modules/whatwg-url/lib/url-state-machine.js b/node_modules/whatwg-url/lib/url-state-machine.js
index 27d977a..2f5633a 100644
--- a/node_modules/whatwg-url/lib/url-state-machine.js
+++ b/node_modules/whatwg-url/lib/url-state-machine.js
@@ -1,5 +1,5 @@
"use strict";
-const punycode = require("punycode");
+const punycode = require("punycode/"); // Trailing slash ensures userland package usage
const tr46 = require("tr46");
const specialSchemes = {