Compare commits

..

5 Commits

Author SHA1 Message Date
Eason Goodale
ba45d2f601 test cache discount 2025-04-26 12:15:34 -07:00
Eason Goodale
b051fcb804 whitespace 2025-04-20 04:04:12 -07:00
Eason Goodale
ada5e2249a format 2025-04-20 01:28:42 -07:00
Eason Goodale
0613fd35e2 lint, formatting 2025-04-18 03:20:10 -07:00
Eason Goodale
cdc0897a25 initial cost tracking
Signed-off-by: Eason Goodale <easong@openai.com>
2025-04-18 03:10:54 -07:00
116 changed files with 9704 additions and 12355 deletions

View File

@@ -26,7 +26,6 @@ body:
label: Which model were you using?
description: Like `gpt-4.1`, `o4-mini`, `o3`, etc.
- type: input
id: platform
attributes:
label: What platform is your computer?
description: |

View File

@@ -19,56 +19,40 @@ jobs:
with:
node-version: 22
- name: Setup pnpm
uses: pnpm/action-setup@v4
with:
version: 10.8.1
run_install: false
# Run codex-cli/ tasks first because they are higher signal.
- name: Get pnpm store directory
id: pnpm-cache
shell: bash
run: |
echo "store_path=$(pnpm store path --silent)" >> $GITHUB_OUTPUT
- name: Setup pnpm cache
uses: actions/cache@v4
with:
path: ${{ steps.pnpm-cache.outputs.store_path }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm install
# Run all tasks using workspace filters
- name: Check TypeScript code formatting
- name: Install dependencies (codex-cli)
working-directory: codex-cli
run: pnpm run format
run: npm ci
- name: Check Markdown and config file formatting
run: pnpm run format
- name: Check formatting (codex-cli)
working-directory: codex-cli
run: npm run format
- name: Run tests
run: pnpm run test
- name: Run tests (codex-cli)
working-directory: codex-cli
run: npm run test
- name: Lint
- name: Lint (codex-cli)
working-directory: codex-cli
run: |
pnpm --filter @openai/codex exec -- eslint src tests --ext ts --ext tsx \
--report-unused-disable-directives \
npm run lint -- \
--rule "no-console:error" \
--rule "no-debugger:error" \
--max-warnings=-1
- name: Type-check
run: pnpm run typecheck
- name: Typecheck (codex-cli)
working-directory: codex-cli
run: npm run typecheck
- name: Build
run: pnpm run build
- name: Build (codex-cli)
working-directory: codex-cli
run: npm run build
- name: Ensure README.md contains only ASCII and certain Unicode code points
run: ./scripts/asciicheck.py README.md
- name: Check README ToC
run: python3 scripts/readme_toc.py README.md
# Run formatting checks in the root directory last.
- name: Install dependencies (root)
run: npm ci
- name: Check formatting (root)
run: npm run format

14
.gitignore vendored
View File

@@ -1,11 +1,5 @@
# deps
# Node.js dependencies
node_modules
.pnpm-store
.pnpm-debug.log
# Keep pnpm-lock.yaml
!pnpm-lock.yaml
# build
dist/
@@ -23,14 +17,9 @@ result
.vscode/
.idea/
.history/
.zed/
*.swp
*~
# cli tools
CLAUDE.md
.claude/
# caches
.cache/
.turbo/
@@ -72,8 +61,9 @@ Icon?
# Unwanted package managers
.yarn/
yarn.lock
pnpm-lock.yaml
# release
package.json-e
session.ts-e
CHANGELOG.ignore.md
CHANGELOG.ignore.md

View File

@@ -1 +0,0 @@
pnpm lint-staged

4
.npmrc
View File

@@ -1,4 +0,0 @@
shamefully-hoist=true
strict-peer-dependencies=false
node-linker=hoisted
prefer-workspace-packages=true

View File

@@ -1,3 +1,2 @@
/codex-cli/dist
/codex-cli/node_modules
pnpm-lock.yaml

View File

@@ -2,76 +2,7 @@
You can install any of these versions: `npm install -g codex@version`
## `0.1.2504221401`
### 🚀 Features
- Show actionable errors when api keys are missing (#523)
- Add CLI `--version` flag (#492)
### 🐛 Bug Fixes
- Agent loop for ZDR (`disableResponseStorage`) (#543)
- Fix relative `workdir` check for `apply_patch` (#556)
- Minimal mid-stream #429 retry loop using existing back-off (#506)
- Inconsistent usage of base URL and API key (#507)
- Remove requirement for api key for ollama (#546)
- Support `[provider]_BASE_URL` (#542)
## `0.1.2504220136`
### 🚀 Features
- Add support for ZDR orgs (#481)
- Include fractional portion of chunk that exceeds stdout/stderr limit (#497)
## `0.1.2504211509`
### 🚀 Features
- Support multiple providers via Responses-Completion transformation (#247)
- Add user-defined safe commands configuration and approval logic #380 (#386)
- Allow switching approval modes when prompted to approve an edit/command (#400)
- Add support for `/diff` command autocomplete in TerminalChatInput (#431)
- Auto-open model selector if user selects deprecated model (#427)
- Read approvalMode from config file (#298)
- `/diff` command to view git diff (#426)
- Tab completions for file paths (#279)
- Add /command autocomplete (#317)
- Allow multi-line input (#438)
### 🐛 Bug Fixes
- `full-auto` support in quiet mode (#374)
- Enable shell option for child process execution (#391)
- Configure husky and lint-staged for pnpm monorepo (#384)
- Command pipe execution by improving shell detection (#437)
- Name of the file not matching the name of the component (#354)
- Allow proper exit from new Switch approval mode dialog (#453)
- Ensure /clear resets context and exclude system messages from approximateTokenUsed count (#443)
- `/clear` now clears terminal screen and resets context left indicator (#425)
- Correct fish completion function name in CLI script (#485)
- Auto-open model-selector when model is not found (#448)
- Remove unnecessary isLoggingEnabled() checks (#420)
- Improve test reliability for `raw-exec` (#434)
- Unintended tear down of agent loop (#483)
- Remove extraneous type casts (#462)
## `0.1.2504181820`
### 🚀 Features
- Add `/bug` report command (#312)
- Notify when a newer version is available (#333)
### 🐛 Bug Fixes
- Update context left display logic in TerminalChatInput component (#307)
- Improper spawn of sh on Windows Powershell (#318)
- `/bug` report command, thinking indicator (#381)
- Include pnpm lock file (#377)
## `0.1.2504172351`
## 0.1.2504172351
### 🚀 Features
@@ -83,7 +14,7 @@ You can install any of these versions: `npm install -g codex@version`
- Raw-exec-process-group.test improve reliability and error handling (#280)
- Canonicalize the writeable paths used in seatbelt policy (#275)
## `0.1.2504172304`
## 0.1.2504172304
### 🚀 Features

70
PNPM.md
View File

@@ -1,70 +0,0 @@
# Migration to pnpm
This project has been migrated from npm to pnpm to improve dependency management and developer experience.
## Why pnpm?
- **Faster installation**: pnpm is significantly faster than npm and yarn
- **Disk space savings**: pnpm uses a content-addressable store to avoid duplication
- **Phantom dependency prevention**: pnpm creates a strict node_modules structure
- **Native workspaces support**: simplified monorepo management
## How to use pnpm
### Installation
```bash
# Global installation of pnpm
npm install -g pnpm@10.8.1
# Or with corepack (available with Node.js 22+)
corepack enable
corepack prepare pnpm@10.8.1 --activate
```
### Common commands
| npm command | pnpm equivalent |
| --------------- | ---------------- |
| `npm install` | `pnpm install` |
| `npm run build` | `pnpm run build` |
| `npm test` | `pnpm test` |
| `npm run lint` | `pnpm run lint` |
### Workspace-specific commands
| Action | Command |
| ------------------------------------------ | ---------------------------------------- |
| Run a command in a specific package | `pnpm --filter @openai/codex run build` |
| Install a dependency in a specific package | `pnpm --filter @openai/codex add lodash` |
| Run a command in all packages | `pnpm -r run test` |
## Monorepo structure
```
codex/
├── pnpm-workspace.yaml # Workspace configuration
├── .npmrc # pnpm configuration
├── package.json # Root dependencies and scripts
├── codex-cli/ # Main package
│ └── package.json # codex-cli specific dependencies
└── docs/ # Documentation (future package)
```
## Configuration files
- **pnpm-workspace.yaml**: Defines the packages included in the monorepo
- **.npmrc**: Configures pnpm behavior
- **Root package.json**: Contains shared scripts and dependencies
## CI/CD
CI/CD workflows have been updated to use pnpm instead of npm. Make sure your CI environments use pnpm 10.8.1 or higher.
## Known issues
If you encounter issues with pnpm, try the following solutions:
1. Remove the `node_modules` folder and `pnpm-lock.yaml` file, then run `pnpm install`
2. Make sure you're using pnpm 10.8.1 or higher
3. Verify that Node.js 22 or higher is installed

359
README.md
View File

@@ -10,29 +10,24 @@
<details>
<summary><strong>Table&nbsp;of&nbsp;Contents</strong></summary>
<!-- Begin ToC -->
- [Experimental Technology Disclaimer](#experimental-technology-disclaimer)
- [Quickstart](#quickstart)
- [Why Codex?](#why-codex)
- [Security Model & Permissions](#security-model--permissions)
- [Why Codex?](#whycodex)
- [Security Model \& Permissions](#securitymodelpermissions)
- [Platform sandboxing details](#platform-sandboxing-details)
- [System Requirements](#system-requirements)
- [CLI Reference](#cli-reference)
- [Memory & Project Docs](#memory--project-docs)
- [Non-interactive / CI mode](#non-interactive--ci-mode)
- [Tracing / Verbose Logging](#tracing--verbose-logging)
- [System Requirements](#systemrequirements)
- [CLI Reference](#clireference)
- [Memory \& Project Docs](#memoryprojectdocs)
- [Noninteractive / CI mode](#noninteractivecimode)
- [Recipes](#recipes)
- [Installation](#installation)
- [Configuration](#configuration)
- [FAQ](#faq)
- [Zero Data Retention (ZDR) Organization Limitation](#zero-data-retention-zdr-organization-limitation)
- [Funding Opportunity](#funding-opportunity)
- [Contributing](#contributing)
- [Development workflow](#development-workflow)
- [Git Hooks with Husky](#git-hooks-with-husky)
- [Debugging](#debugging)
- [Writing high-impact code changes](#writing-high-impact-code-changes)
- [Nix Flake Development](#nix-flake-development)
- [Writing highimpact code changes](#writing-highimpact-code-changes)
- [Opening a pull request](#opening-a-pull-request)
- [Review process](#review-process)
- [Community values](#community-values)
@@ -40,12 +35,9 @@
- [Contributor License Agreement (CLA)](#contributor-license-agreement-cla)
- [Quick fixes](#quick-fixes)
- [Releasing `codex`](#releasing-codex)
- [Alternative Build Options](#alternative-build-options)
- [Nix Flake Development](#nix-flake-development)
- [Security & Responsible AI](#security--responsible-ai)
- [Security \& Responsible AI](#securityresponsibleai)
- [License](#license)
<!-- End ToC -->
- [Zero Data Retention (ZDR) Organization Limitation](#zero-data-retention-zdr-organization-limitation)
</details>
@@ -53,7 +45,7 @@
## Experimental Technology Disclaimer
Codex CLI is an experimental project under active development. It is not yet stable, may contain bugs, incomplete features, or undergo breaking changes. We're building it in the open with the community and welcome:
Codex CLI is an experimental project under active development. It is not yet stable, may contain bugs, incomplete features, or undergo breaking changes. Were building it in the open with the community and welcome:
- Bug reports
- Feature requests
@@ -76,7 +68,9 @@ Next, set your OpenAI API key as an environment variable:
export OPENAI_API_KEY="your-api-key-here"
```
> **Note:** This command sets the key only for your current terminal session. You can add the `export` line to your shell's configuration file (e.g., `~/.zshrc`) but we recommend setting for the session. **Tip:** You can also place your API key into a `.env` file at the root of your project:
> **Note:** This command sets the key only for your current terminal session. To make it permanent, add the `export` line to your shell's configuration file (e.g., `~/.zshrc`).
>
> **Tip:** You can also place your API key into a `.env` file at the root of your project:
>
> ```env
> OPENAI_API_KEY=your-api-key-here
@@ -84,29 +78,6 @@ export OPENAI_API_KEY="your-api-key-here"
>
> The CLI will automatically load variables from `.env` (via `dotenv/config`).
<details>
<summary><strong>Use <code>--provider</code> to use other models</strong></summary>
> Codex also allows you to use other providers that support the OpenAI Chat Completions API. You can set the provider in the config file or use the `--provider` flag. The possible options for `--provider` are:
>
> - openai (default)
> - openrouter
> - gemini
> - ollama
> - mistral
> - deepseek
> - xai
> - groq
>
> If you use a provider other than OpenAI, you will need to set the API key for the provider in the config file or in the environment variable as:
>
> ```shell
> export <provider>_API_KEY="your-api-key-here"
> ```
</details>
<br />
Run interactively:
```shell
@@ -123,59 +94,59 @@ codex "explain this codebase to me"
codex --approval-mode full-auto "create the fanciest todo-list app"
```
That's it - Codex will scaffold a file, run it inside a sandbox, install any
Thats it Codex will scaffold a file, run it inside a sandbox, install any
missing dependencies, and show you the live result. Approve the changes and
they'll be committed to your working directory.
theyll be committed to your working directory.
---
## Why Codex?
## Why Codex?
Codex CLI is built for developers who already **live in the terminal** and want
ChatGPT-level reasoning **plus** the power to actually run code, manipulate
files, and iterate - all under version control. In short, it's _chat-driven
ChatGPTlevel reasoning **plus** the power to actually run code, manipulate
files, and iterate all under version control. In short, its _chatdriven
development_ that understands and executes your repo.
- **Zero setup** - bring your OpenAI API key and it just works!
- **Zero setup** bring your OpenAI API key and it just works!
- **Full auto-approval, while safe + secure** by running network-disabled and directory-sandboxed
- **Multimodal** - pass in screenshots or diagrams to implement features ✨
- **Multimodal** pass in screenshots or diagrams to implement features ✨
And it's **fully open-source** so you can see and contribute to how it develops!
---
## Security Model & Permissions
## Security Model & Permissions
Codex lets you decide _how much autonomy_ the agent receives and auto-approval policy via the
`--approval-mode` flag (or the interactive onboarding prompt):
| Mode | What the agent may do without asking | Still requires approval |
| ------------------------- | --------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
| **Suggest** <br>(default) | <li>Read any file in the repo | <li>**All** file writes/patches<li> **Any** arbitrary shell commands (aside from reading files) |
| **Auto Edit** | <li>Read **and** apply-patch writes to files | <li>**All** shell commands |
| **Full Auto** | <li>Read/write files <li> Execute shell commands (network disabled, writes limited to your workdir) | - |
| Mode | What the agent may do without asking | Still requires approval |
| ------------------------- | -------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
| **Suggest** <br>(default) | Read any file in the repo | **All** file writes/patches <br>• **Any** arbitrary shell commands (aside from reading files) |
| **Auto Edit** | Read **and** applypatch writes to files | **All** shell commands |
| **Full Auto** | Read/write files <br>• Execute shell commands (network disabled, writes limited to your workdir) | |
In **Full Auto** every command is run **network-disabled** and confined to the
current working directory (plus temporary files) for defense-in-depth. Codex
will also show a warning/confirmation if you start in **auto-edit** or
**full-auto** while the directory is _not_ tracked by Git, so you always have a
In **Full Auto** every command is run **networkdisabled** and confined to the
current working directory (plus temporary files) for defenseindepth. Codex
will also show a warning/confirmation if you start in **autoedit** or
**fullauto** while the directory is _not_ tracked by Git, so you always have a
safety net.
Coming soon: you'll be able to whitelist specific commands to auto-execute with
the network enabled, once we're confident in additional safeguards.
Coming soon: youll be able to whitelist specific commands to autoexecute with
the network enabled, once were confident in additional safeguards.
### Platform sandboxing details
The hardening mechanism Codex uses depends on your OS:
- **macOS 12+** - commands are wrapped with **Apple Seatbelt** (`sandbox-exec`).
- **macOS 12+** commands are wrapped with **Apple Seatbelt** (`sandbox-exec`).
- Everything is placed in a read-only jail except for a small set of
- Everything is placed in a readonly jail except for a small set of
writable roots (`$PWD`, `$TMPDIR`, `~/.codex`, etc.).
- Outbound network is _fully blocked_ by default - even if a child process
- Outbound network is _fully blocked_ by default even if a child process
tries to `curl` somewhere it will fail.
- **Linux** - there is no sandboxing by default.
- **Linux** there is no sandboxing by default.
We recommend using Docker for sandboxing, where Codex launches itself inside a **minimal
container image** and mounts your repo _read/write_ at the same path. A
custom `iptables`/`ipset` firewall script denies all egress except the
@@ -184,47 +155,47 @@ The hardening mechanism Codex uses depends on your OS:
---
## System Requirements
## System Requirements
| Requirement | Details |
| --------------------------- | --------------------------------------------------------------- |
| Operating systems | macOS 12+, Ubuntu 20.04+/Debian 10+, or Windows 11 **via WSL2** |
| Operating systems | macOS 12+, Ubuntu 20.04+/Debian 10+, or Windows 11 **via WSL2** |
| Node.js | **22 or newer** (LTS recommended) |
| Git (optional, recommended) | 2.23+ for built-in PR helpers |
| RAM | 4-GB minimum (8-GB recommended) |
| Git (optional, recommended) | 2.23+ for builtin PR helpers |
| RAM | 4GB minimum (8GB recommended) |
> Never run `sudo npm install -g`; fix npm permissions instead.
---
## CLI Reference
## CLI Reference
| Command | Purpose | Example |
| ------------------------------------ | ----------------------------------- | ------------------------------------ |
| `codex` | Interactive REPL | `codex` |
| `codex "..."` | Initial prompt for interactive REPL | `codex "fix lint errors"` |
| `codex -q "..."` | Non-interactive "quiet mode" | `codex -q --json "explain utils.ts"` |
| `codex ""` | Initial prompt for interactive REPL | `codex "fix lint errors"` |
| `codex -q ""` | Noninteractive "quiet mode" | `codex -q --json "explain utils.ts"` |
| `codex completion <bash\|zsh\|fish>` | Print shell completion script | `codex completion bash` |
Key flags: `--model/-m`, `--approval-mode/-a`, `--quiet/-q`, and `--notify`.
---
## Memory & Project Docs
## Memory & Project Docs
Codex merges Markdown instructions in this order:
1. `~/.codex/instructions.md` - personal global guidance
2. `codex.md` at repo root - shared project notes
3. `codex.md` in cwd - sub-package specifics
1. `~/.codex/instructions.md` personal global guidance
2. `codex.md` at repo root shared project notes
3. `codex.md` in cwd subpackage specifics
Disable with `--no-project-doc` or `CODEX_DISABLE_PROJECT_DOC=1`.
---
## Non-interactive / CI mode
## Noninteractive / CI mode
Run Codex head-less in pipelines. Example GitHub Action step:
Run Codex headless in pipelines. Example GitHub Action step:
```yaml
- name: Update changelog via Codex
@@ -248,15 +219,15 @@ DEBUG=true codex
## Recipes
Below are a few bite-size examples you can copy-paste. Replace the text in quotes with your own task. See the [prompting guide](https://github.com/openai/codex/blob/main/codex-cli/examples/prompting_guide.md) for more tips and usage patterns.
Below are a few bitesize examples you can copypaste. Replace the text in quotes with your own task. See the [prompting guide](https://github.com/openai/codex/blob/main/codex-cli/examples/prompting_guide.md) for more tips and usage patterns.
| ✨ | What you type | What happens |
| --- | ------------------------------------------------------------------------------- | -------------------------------------------------------------------------- |
| 1 | `codex "Refactor the Dashboard component to React Hooks"` | Codex rewrites the class component, runs `npm test`, and shows the diff. |
| 1 | `codex "Refactor the Dashboard component to React Hooks"` | Codex rewrites the class component, runs `npm test`, and shows the diff. |
| 2 | `codex "Generate SQL migrations for adding a users table"` | Infers your ORM, creates migration files, and runs them in a sandboxed DB. |
| 3 | `codex "Write unit tests for utils/date.ts"` | Generates tests, executes them, and iterates until they pass. |
| 4 | `codex "Bulk-rename *.jpeg -> *.jpg with git mv"` | Safely renames files and updates imports/usages. |
| 5 | `codex "Explain what this regex does: ^(?=.*[A-Z]).{8,}$"` | Outputs a step-by-step human explanation. |
| 4 | `codex "Bulkrename *.jpeg *.jpg with git mv"` | Safely renames files and updates imports/usages. |
| 5 | `codex "Explain what this regex does: ^(?=.*[A-Z]).{8,}$"` | Outputs a stepbystep human explanation. |
| 6 | `codex "Carefully review this repo, and propose 3 high impact well-scoped PRs"` | Suggests impactful PRs in the current codebase. |
| 7 | `codex "Look for vulnerabilities and create a security review report"` | Finds and explains security bugs. |
@@ -265,7 +236,7 @@ Below are a few bite-size examples you can copy-paste. Replace the text in quote
## Installation
<details open>
<summary><strong>From npm (Recommended)</strong></summary>
<summary><strong>From npm (Recommended)</strong></summary>
```bash
npm install -g @openai/codex
@@ -273,35 +244,30 @@ npm install -g @openai/codex
yarn global add @openai/codex
# or
bun install -g @openai/codex
# or
pnpm add -g @openai/codex
```
</details>
<details>
<summary><strong>Build from source</strong></summary>
<summary><strong>Build from source</strong></summary>
```bash
# Clone the repository and navigate to the CLI package
git clone https://github.com/openai/codex.git
cd codex/codex-cli
# Enable corepack
corepack enable
# Install dependencies and build
pnpm install
pnpm build
npm install
npm run build
# Get the usage and the options
node ./dist/cli.js --help
# Run the locally-built CLI directly
# Run the locallybuilt CLI directly
node ./dist/cli.js
# Or link the command globally for convenience
pnpm link
npm link
```
</details>
@@ -310,26 +276,15 @@ pnpm link
## Configuration
Codex looks for config files in **`~/.codex/`** (either YAML or JSON format).
Codex looks for config files in **`~/.codex/`**.
```yaml
# ~/.codex/config.yaml
model: o4-mini # Default model
approvalMode: suggest # or auto-edit, full-auto
fullAutoErrorMode: ask-user # or ignore-and-continue
notify: true # Enable desktop notifications for responses
```
```json
// ~/.codex/config.json
{
"model": "o4-mini",
"approvalMode": "suggest",
"fullAutoErrorMode": "ask-user",
"notify": true
}
```
You can also define custom instructions:
```yaml
@@ -371,7 +326,7 @@ Codex runs model-generated commands in a sandbox. If a proposed command or file
<details>
<summary>Does it work on Windows?</summary>
Not directly. It requires [Windows Subsystem for Linux (WSL2)](https://learn.microsoft.com/en-us/windows/wsl/install) - Codex has been tested on macOS and Linux with Node 22.
Not directly. It requires [Windows Subsystem for Linux (WSL2)](https://learn.microsoft.com/en-us/windows/wsl/install) Codex has been tested on macOS and Linux with Node ≥ 22.
</details>
@@ -396,18 +351,18 @@ OpenAI rejected the request. Error details: Status: 400, Code: unsupported_param
**What can I do?**
- If you are part of a ZDR organization, Codex CLI will not work until support is added.
- We are tracking this limitation and will update the documentation once support becomes available.
- We are tracking this limitation and will update the documentation if support becomes available.
---
## Funding Opportunity
We're excited to launch a **$1 million initiative** supporting open source projects that use Codex CLI and other OpenAI models.
Were excited to launch a **$1 million initiative** supporting open source projects that use Codex CLI and other OpenAI models.
- Grants are awarded in **$25,000** API credit increments.
- Applications are reviewed **on a rolling basis**.
**Interested? [Apply here](https://openai.com/form/codex-open-source-fund/).**
**Interested? [Apply here](https://openai.com/form/codex-open-source-fund/).**
---
@@ -415,14 +370,14 @@ We're excited to launch a **$1 million initiative** supporting open source proje
This project is under active development and the code will likely change pretty significantly. We'll update this message once that's complete!
More broadly we welcome contributions - whether you are opening your very first pull request or you're a seasoned maintainer. At the same time we care about reliability and long-term maintainability, so the bar for merging code is intentionally **high**. The guidelines below spell out what "high-quality" means in practice and should make the whole process transparent and friendly.
More broadly we welcome contributions whether you are opening your very first pull request or youre a seasoned maintainer. At the same time we care about reliability and longterm maintainability, so the bar for merging code is intentionally **high**. The guidelines below spell out what highquality means in practice and should make the whole process transparent and friendly.
### Development workflow
- Create a _topic branch_ from `main` - e.g. `feat/interactive-prompt`.
- Create a _topic branch_ from `main` e.g. `feat/interactive-prompt`.
- Keep your changes focused. Multiple unrelated fixes should be opened as separate PRs.
- Use `pnpm test:watch` during development for super-fast feedback.
- We use **Vitest** for unit tests, **ESLint** + **Prettier** for style, and **TypeScript** for type-checking.
- Use `npm run test:watch` during development for superfast feedback.
- We use **Vitest** for unit tests, **ESLint** + **Prettier** for style, and **TypeScript** for typechecking.
- Before pushing, run the full test/type/lint suite:
### Git Hooks with Husky
@@ -435,7 +390,7 @@ This project uses [Husky](https://typicode.github.io/husky/) to enforce code qua
These hooks help maintain code quality and prevent pushing code with failing tests. For more details, see [HUSKY.md](./codex-cli/HUSKY.md).
```bash
pnpm test && pnpm run lint && pnpm run typecheck
npm test && npm run lint && npm run typecheck
```
- If you have **not** yet signed the Contributor License Agreement (CLA), add a PR comment containing the exact text
@@ -444,101 +399,20 @@ pnpm test && pnpm run lint && pnpm run typecheck
I have read the CLA Document and I hereby sign the CLA
```
The CLA-Assistant bot will turn the PR status green once all authors have signed.
The CLAAssistant bot will turn the PR status green once all authors have signed.
```bash
# Watch mode (tests rerun on change)
pnpm test:watch
# Watch mode (tests rerun on change)
npm run test:watch
# Type-check without emitting files
pnpm typecheck
# Typecheck without emitting files
npm run typecheck
# Automatically fix lint + prettier issues
pnpm lint:fix
pnpm format:fix
# Automatically fix lint + prettier issues
npm run lint:fix
npm run format:fix
```
### Debugging
To debug the CLI with a visual debugger, do the following in the `codex-cli` folder:
- Run `pnpm run build` to build the CLI, which will generate `cli.js.map` alongside `cli.js` in the `dist` folder.
- Run the CLI with `node --inspect-brk ./dist/cli.js` The program then waits until a debugger is attached before proceeding. Options:
- In VS Code, choose **Debug: Attach to Node Process** from the command palette and choose the option in the dropdown with debug port `9229` (likely the first option)
- Go to <chrome://inspect> in Chrome and find **localhost:9229** and click **trace**
### Writing high-impact code changes
1. **Start with an issue.** Open a new one or comment on an existing discussion so we can agree on the solution before code is written.
2. **Add or update tests.** Every new feature or bug-fix should come with test coverage that fails before your change and passes afterwards. 100% coverage is not required, but aim for meaningful assertions.
3. **Document behaviour.** If your change affects user-facing behaviour, update the README, inline help (`codex --help`), or relevant example projects.
4. **Keep commits atomic.** Each commit should compile and the tests should pass. This makes reviews and potential rollbacks easier.
### Opening a pull request
- Fill in the PR template (or include similar information) - **What? Why? How?**
- Run **all** checks locally (`npm test && npm run lint && npm run typecheck`). CI failures that could have been caught locally slow down the process.
- Make sure your branch is up-to-date with `main` and that you have resolved merge conflicts.
- Mark the PR as **Ready for review** only when you believe it is in a merge-able state.
### Review process
1. One maintainer will be assigned as a primary reviewer.
2. We may ask for changes - please do not take this personally. We value the work, we just also value consistency and long-term maintainability.
3. When there is consensus that the PR meets the bar, a maintainer will squash-and-merge.
### Community values
- **Be kind and inclusive.** Treat others with respect; we follow the [Contributor Covenant](https://www.contributor-covenant.org/).
- **Assume good intent.** Written communication is hard - err on the side of generosity.
- **Teach & learn.** If you spot something confusing, open an issue or PR with improvements.
### Getting help
If you run into problems setting up the project, would like feedback on an idea, or just want to say _hi_ - please open a Discussion or jump into the relevant issue. We are happy to help.
Together we can make Codex CLI an incredible tool. **Happy hacking!** :rocket:
### Contributor License Agreement (CLA)
All contributors **must** accept the CLA. The process is lightweight:
1. Open your pull request.
2. Paste the following comment (or reply `recheck` if you've signed before):
```text
I have read the CLA Document and I hereby sign the CLA
```
3. The CLA-Assistant bot records your signature in the repo and marks the status check as passed.
No special Git commands, email attachments, or commit footers required.
#### Quick fixes
| Scenario | Command |
| ----------------- | ------------------------------------------------ |
| Amend last commit | `git commit --amend -s --no-edit && git push -f` |
The **DCO check** blocks merges until every commit in the PR carries the footer (with squash this is just the one).
### Releasing `codex`
To publish a new version of the CLI, run the release scripts defined in `codex-cli/package.json`:
1. Open the `codex-cli` directory
2. Make sure you're on a branch like `git checkout -b bump-version`
3. Bump the version and `CLI_VERSION` to current datetime: `pnpm release:version`
4. Commit the version bump (with DCO sign-off):
```bash
git add codex-cli/src/utils/session.ts codex-cli/package.json
git commit -s -m "chore(release): codex-cli v$(node -p \"require('./codex-cli/package.json').version\")"
```
5. Copy README, build, and publish to npm: `pnpm release`
6. Push to branch: `git push origin HEAD`
### Alternative Build Options
#### Nix Flake Development
Prerequisite: Nix >= 2.4 with flakes enabled (`experimental-features = nix-command flakes` in `~/.config/nix/nix.conf`).
@@ -564,14 +438,85 @@ Run the CLI via the flake app:
nix run .#codex
```
### Writing highimpact code changes
1. **Start with an issue.** Open a new one or comment on an existing discussion so we can agree on the solution before code is written.
2. **Add or update tests.** Every new feature or bugfix should come with test coverage that fails before your change and passes afterwards. 100 % coverage is not required, but aim for meaningful assertions.
3. **Document behaviour.** If your change affects userfacing behaviour, update the README, inline help (`codex --help`), or relevant example projects.
4. **Keep commits atomic.** Each commit should compile and the tests should pass. This makes reviews and potential rollbacks easier.
### Opening a pull request
- Fill in the PR template (or include similar information) **What? Why? How?**
- Run **all** checks locally (`npm test && npm run lint && npm run typecheck`). CI failures that could have been caught locally slow down the process.
- Make sure your branch is uptodate with `main` and that you have resolved merge conflicts.
- Mark the PR as **Ready for review** only when you believe it is in a mergeable state.
### Review process
1. One maintainer will be assigned as a primary reviewer.
2. We may ask for changes please do not take this personally. We value the work, we just also value consistency and longterm maintainability.
3. When there is consensus that the PR meets the bar, a maintainer will squashandmerge.
### Community values
- **Be kind and inclusive.** Treat others with respect; we follow the [Contributor Covenant](https://www.contributor-covenant.org/).
- **Assume good intent.** Written communication is hard err on the side of generosity.
- **Teach & learn.** If you spot something confusing, open an issue or PR with improvements.
### Getting help
If you run into problems setting up the project, would like feedback on an idea, or just want to say _hi_ please open a Discussion or jump into the relevant issue. We are happy to help.
Together we can make Codex CLI an incredible tool. **Happy hacking!** :rocket:
### Contributor License Agreement (CLA)
All contributors **must** accept the CLA. The process is lightweight:
1. Open your pull request.
2. Paste the following comment (or reply `recheck` if youve signed before):
```text
I have read the CLA Document and I hereby sign the CLA
```
3. The CLAAssistant bot records your signature in the repo and marks the status check as passed.
No special Git commands, email attachments, or commit footers required.
#### Quick fixes
| Scenario | Command |
| ----------------- | ----------------------------------------------------------------------------------------- |
| Amend last commit | `git commit --amend -s --no-edit && git push -f` |
| GitHub UI only | Edit the commit message in the PR → add<br>`Signed-off-by: Your Name <email@example.com>` |
The **DCO check** blocks merges until every commit in the PR carries the footer (with squash this is just the one).
### Releasing `codex`
To publish a new version of the CLI, run the release scripts defined in `codex-cli/package.json`:
1. Open the `codex-cli` directory
2. Make sure you're on a branch like `git checkout -b bump-version`
3. Bump the version and `CLI_VERSION` to current datetime: `npm run release:version`
4. Commit the version bump (with DCO sign-off):
```bash
git add codex-cli/src/utils/session.ts codex-cli/package.json
git commit -s -m "chore(release): codex-cli v$(node -p \"require('./codex-cli/package.json').version\")"
```
5. Copy README, build, and publish to npm: `npm run release`
6. Push to branch: `git push origin HEAD`
---
## Security & Responsible AI
## Security &amp; Responsible AI
Have you discovered a vulnerability or have concerns about model output? Please e-mail **security@openai.com** and we will respond promptly.
Have you discovered a vulnerability or have concerns about model output? Please email **security@openai.com** and we will respond promptly.
---
## License
This repository is licensed under the [Apache-2.0 License](LICENSE).
This repository is licensed under the [Apache-2.0 License](LICENSE).

View File

@@ -38,7 +38,7 @@ commit_parsers = [
{ message = "^fix", group = "<!-- 1 -->🐛 Bug Fixes" },
{ message = "^bump", group = "<!-- 6 -->🛳️ Release" },
# Fallback  skip anything that didn't match the above rules.
{ message = ".*", group = "<!-- 10 -->💼 Other" },
{ message = ".*", group = "<!-- 10 -->💼 Other", skip = true },
]
filter_unconventional = false

View File

@@ -0,0 +1,32 @@
#!/usr/bin/env sh
if [ -z "$husky_skip_init" ]; then
debug () {
if [ "$HUSKY_DEBUG" = "1" ]; then
echo "husky (debug) - $1"
fi
}
readonly hook_name="$(basename -- "$0")"
debug "starting $hook_name..."
if [ "$HUSKY" = "0" ]; then
debug "HUSKY env variable is set to 0, skipping hook"
exit 0
fi
if [ -f ~/.huskyrc ]; then
debug "sourcing ~/.huskyrc"
. ~/.huskyrc
fi
readonly husky_skip_init=1
export husky_skip_init
sh -e "$0" "$@"
exitCode="$?"
if [ $exitCode != 0 ]; then
echo "husky - $hook_name hook exited with code $exitCode (error)"
fi
exit $exitCode
fi

View File

@@ -0,0 +1,5 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
# Run lint-staged to check files that are about to be committed
npm run pre-commit

View File

@@ -0,0 +1,5 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
# Run tests and type checking before pushing
npm test && npm run typecheck

View File

@@ -0,0 +1,9 @@
{
"*.{ts,tsx}": [
"eslint --fix",
"prettier --write"
],
"*.{json,md,yml}": [
"prettier --write"
]
}

20
codex-cli/bin/codex Executable file
View File

@@ -0,0 +1,20 @@
#!/usr/bin/env sh
# resolve script path in case of symlink
SOURCE="$0"
while [ -h "$SOURCE" ]; do
DIR=$(dirname "$SOURCE")
SOURCE=$(readlink "$SOURCE")
case "$SOURCE" in
/*) ;; # absolute path
*) SOURCE="$DIR/$SOURCE" ;; # relative path
esac
done
DIR=$(cd "$(dirname "$SOURCE")" && pwd)
if command -v node >/dev/null 2>&1; then
exec node "$DIR/../dist/cli.js" "$@"
elif command -v bun >/dev/null 2>&1; then
exec bun "$DIR/../dist/cli.js" "$@"
else
echo "Error: node or bun is required to run codex" >&2
exit 1
fi

View File

@@ -1,27 +0,0 @@
#!/usr/bin/env node
// Unified entry point for Codex CLI on all platforms
// Dynamically loads the compiled ESM bundle in dist/cli.js
import path from 'path';
import { fileURLToPath, pathToFileURL } from 'url';
// Determine this script's directory
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Resolve the path to the compiled CLI bundle
const cliPath = path.resolve(__dirname, '../dist/cli.js');
const cliUrl = pathToFileURL(cliPath).href;
// Load and execute the CLI
(async () => {
try {
await import(cliUrl);
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
// eslint-disable-next-line no-undef
process.exit(1);
}
})();

View File

@@ -1,8 +1,6 @@
import * as esbuild from "esbuild";
import * as fs from "fs";
import * as path from "path";
const OUT_DIR = 'dist'
/**
* ink attempts to import react-devtools-core in an ESM-unfriendly way:
*
@@ -41,11 +39,6 @@ const isDevBuild =
const plugins = [ignoreReactDevToolsPlugin];
// Build Hygiene, ensure we drop previous dist dir and any leftover files
const outPath = path.resolve(OUT_DIR);
if (fs.existsSync(outPath)) {
fs.rmSync(outPath, { recursive: true, force: true });
}
// Add a shebang that enables sourcemap support for dev builds so that stack
// traces point to the original TypeScript lines without requiring callers to
@@ -57,7 +50,7 @@ if (isDevBuild) {
name: "dev-shebang",
setup(build) {
build.onEnd(async () => {
const outFile = path.resolve(isDevBuild ? `${OUT_DIR}/cli-dev.js` : `${OUT_DIR}/cli.js`);
const outFile = path.resolve(isDevBuild ? "dist/cli-dev.js" : "dist/cli.js");
let code = await fs.promises.readFile(outFile, "utf8");
if (code.startsWith("#!")) {
code = code.replace(/^#!.*\n/, devShebangLine);
@@ -76,7 +69,7 @@ esbuild
format: "esm",
platform: "node",
tsconfig: "tsconfig.json",
outfile: isDevBuild ? `${OUT_DIR}/cli-dev.js` : `${OUT_DIR}/cli.js`,
outfile: isDevBuild ? "dist/cli-dev.js" : "dist/cli.js",
minify: !isDevBuild,
sourcemap: isDevBuild ? "inline" : true,
plugins,

7372
codex-cli/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,9 +1,9 @@
{
"name": "@openai/codex",
"version": "0.1.2504221401",
"version": "0.1.2504172351",
"license": "Apache-2.0",
"bin": {
"codex": "bin/codex.js"
"codex": "bin/codex"
},
"type": "module",
"engines": {
@@ -22,11 +22,16 @@
"build:dev": "NODE_ENV=development node build.mjs --dev && NODE_OPTIONS=--enable-source-maps node dist/cli-dev.js",
"release:readme": "cp ../README.md ./README.md",
"release:version": "TS=$(date +%y%m%d%H%M) && sed -E -i'' -e \"s/\\\"0\\.1\\.[0-9]{10}\\\"/\\\"0.1.${TS}\\\"/g\" package.json src/utils/session.ts",
"release:build-and-publish": "pnpm run build && npm publish",
"release": "pnpm run release:readme && pnpm run release:version && pnpm install && pnpm run release:build-and-publish"
"release:build-and-publish": "npm run build && npm publish",
"release": "npm run release:readme && npm run release:version && npm install && npm run release:build-and-publish",
"prepare": "husky",
"pre-commit": "lint-staged"
},
"files": [
"dist"
"README.md",
"bin",
"dist",
"src"
],
"dependencies": {
"@inkjs/ui": "^2.0.0",
@@ -34,7 +39,6 @@
"diff": "^7.0.0",
"dotenv": "^16.1.4",
"fast-deep-equal": "^3.1.3",
"fast-npm-meta": "^0.4.2",
"figures": "^6.1.0",
"file-type": "^20.1.0",
"ink": "^5.2.0",
@@ -43,8 +47,7 @@
"marked-terminal": "^7.3.0",
"meow": "^13.2.0",
"open": "^10.1.0",
"openai": "^4.95.1",
"package-manager-detector": "^1.2.0",
"openai": "^4.89.0",
"react": "^18.2.0",
"shell-quote": "^1.8.2",
"strip-ansi": "^7.1.0",
@@ -58,12 +61,9 @@
"@types/js-yaml": "^4.0.9",
"@types/marked-terminal": "^6.1.1",
"@types/react": "^18.0.32",
"@types/semver": "^7.7.0",
"@types/shell-quote": "^1.7.5",
"@types/which": "^3.0.4",
"@typescript-eslint/eslint-plugin": "^7.18.0",
"@typescript-eslint/parser": "^7.18.0",
"boxen": "^8.0.1",
"esbuild": "^0.25.2",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-react": "^7.32.2",
@@ -71,14 +71,21 @@
"eslint-plugin-react-refresh": "^0.4.19",
"husky": "^9.1.7",
"ink-testing-library": "^3.0.0",
"lint-staged": "^15.5.1",
"prettier": "^2.8.7",
"punycode": "^2.3.1",
"semver": "^7.7.1",
"ts-node": "^10.9.1",
"typescript": "^5.0.3",
"vitest": "^3.0.9",
"whatwg-url": "^14.2.0",
"which": "^5.0.0"
"whatwg-url": "^14.2.0"
},
"resolutions": {
"braces": "^3.0.3",
"micromatch": "^4.0.8",
"semver": "^7.7.1"
},
"overrides": {
"punycode": "^2.3.1"
},
"repository": {
"type": "git",

View File

@@ -23,16 +23,6 @@ fi
WORK_DIR=$(realpath "$WORK_DIR")
# Generate a unique container name based on the normalized work directory
CONTAINER_NAME="codex_$(echo "$WORK_DIR" | sed 's/\//_/g' | sed 's/[^a-zA-Z0-9_-]//g')"
# Define cleanup to remove the container on script exit, ensuring no leftover containers
cleanup() {
docker rm -f "$CONTAINER_NAME" >/dev/null 2>&1 || true
}
# Trap EXIT to invoke cleanup regardless of how the script terminates
trap cleanup EXIT
# Ensure a command is provided.
if [ "$#" -eq 0 ]; then
echo "Usage: $0 [--work_dir directory] \"COMMAND\""
@@ -45,11 +35,11 @@ if [ -z "$WORK_DIR" ]; then
exit 1
fi
# Kill any existing container for the working directory using cleanup(), centralizing removal logic.
cleanup
# Remove any existing container named 'codex'.
docker rm -f codex 2>/dev/null || true
# Run the container with the specified directory mounted at the same path inside the container.
docker run --name "$CONTAINER_NAME" -d \
docker run --name codex -d \
-e OPENAI_API_KEY \
--cap-add=NET_ADMIN \
--cap-add=NET_RAW \
@@ -58,7 +48,7 @@ docker run --name "$CONTAINER_NAME" -d \
sleep infinity
# Initialize the firewall inside the container.
docker exec "$CONTAINER_NAME" bash -c "sudo /usr/local/bin/init_firewall.sh"
docker exec codex bash -c "sudo /usr/local/bin/init_firewall.sh"
# Execute the provided command in the container, ensuring it runs in the work directory.
# We use a parameterized bash command to safely handle the command and directory.
@@ -67,4 +57,4 @@ quoted_args=""
for arg in "$@"; do
quoted_args+=" $(printf '%q' "$arg")"
done
docker exec -it "$CONTAINER_NAME" bash -c "cd \"/app$WORK_DIR\" && codex --full-auto ${quoted_args}"
docker exec -it codex bash -c "cd \"/app$WORK_DIR\" && codex --full-auto ${quoted_args}"

View File

@@ -71,14 +71,13 @@ export type ApprovalPolicy =
*/
export function canAutoApprove(
command: ReadonlyArray<string>,
workdir: string | undefined,
policy: ApprovalPolicy,
writableRoots: ReadonlyArray<string>,
env: NodeJS.ProcessEnv = process.env,
): SafetyAssessment {
if (command[0] === "apply_patch") {
return command.length === 2 && typeof command[1] === "string"
? canAutoApproveApplyPatch(command[1], workdir, writableRoots, policy)
? canAutoApproveApplyPatch(command[1], writableRoots, policy)
: {
type: "reject",
reason: "Invalid apply_patch command",
@@ -104,12 +103,7 @@ export function canAutoApprove(
) {
const applyPatchArg = tryParseApplyPatch(command[2]);
if (applyPatchArg != null) {
return canAutoApproveApplyPatch(
applyPatchArg,
workdir,
writableRoots,
policy,
);
return canAutoApproveApplyPatch(applyPatchArg, writableRoots, policy);
}
let bashCmd;
@@ -141,8 +135,8 @@ export function canAutoApprove(
// bashCmd could be a mix of strings and operators, e.g.:
// "ls || (true && pwd)" => [ 'ls', { op: '||' }, '(', 'true', { op: '&&' }, 'pwd', ')' ]
// We try to ensure that *every* command segment is deemed safe and that
// all operators belong to an allow-list. If so, the entire expression is
// considered auto-approvable.
// all operators belong to an allowlist. If so, the entire expression is
// considered autoapprovable.
const shellSafe = isEntireShellExpressionSafe(bashCmd);
if (shellSafe != null) {
@@ -168,7 +162,6 @@ export function canAutoApprove(
function canAutoApproveApplyPatch(
applyPatchArg: string,
workdir: string | undefined,
writableRoots: ReadonlyArray<string>,
policy: ApprovalPolicy,
): SafetyAssessment {
@@ -186,13 +179,7 @@ function canAutoApproveApplyPatch(
break;
}
if (
isWritePatchConstrainedToWritablePaths(
applyPatchArg,
workdir,
writableRoots,
)
) {
if (isWritePatchConstrainedToWritablePaths(applyPatchArg, writableRoots)) {
return {
type: "auto-approve",
reason: "apply_patch command is constrained to writable paths",
@@ -221,7 +208,6 @@ function canAutoApproveApplyPatch(
*/
function isWritePatchConstrainedToWritablePaths(
applyPatchArg: string,
workdir: string | undefined,
writableRoots: ReadonlyArray<string>,
): boolean {
// `identify_files_needed()` returns a list of files that will be modified or
@@ -236,12 +222,10 @@ function isWritePatchConstrainedToWritablePaths(
return (
allPathsConstrainedTowritablePaths(
identify_files_needed(applyPatchArg),
workdir,
writableRoots,
) &&
allPathsConstrainedTowritablePaths(
identify_files_added(applyPatchArg),
workdir,
writableRoots,
)
);
@@ -249,47 +233,24 @@ function isWritePatchConstrainedToWritablePaths(
function allPathsConstrainedTowritablePaths(
candidatePaths: ReadonlyArray<string>,
workdir: string | undefined,
writableRoots: ReadonlyArray<string>,
): boolean {
return candidatePaths.every((candidatePath) =>
isPathConstrainedTowritablePaths(candidatePath, workdir, writableRoots),
isPathConstrainedTowritablePaths(candidatePath, writableRoots),
);
}
/** If candidatePath is relative, it will be resolved against cwd. */
function isPathConstrainedTowritablePaths(
candidatePath: string,
workdir: string | undefined,
writableRoots: ReadonlyArray<string>,
): boolean {
const candidateAbsolutePath = resolvePathAgainstWorkdir(
candidatePath,
workdir,
);
const candidateAbsolutePath = path.resolve(candidatePath);
return writableRoots.some((writablePath) =>
pathContains(writablePath, candidateAbsolutePath),
);
}
/**
* If not already an absolute path, resolves `candidatePath` against `workdir`
* if specified; otherwise, against `process.cwd()`.
*/
export function resolvePathAgainstWorkdir(
candidatePath: string,
workdir: string | undefined,
): string {
if (path.isAbsolute(candidatePath)) {
return candidatePath;
} else if (workdir != null) {
return path.resolve(workdir, candidatePath);
} else {
return path.resolve(candidatePath);
}
}
/** Both `parent` and `child` must be absolute paths. */
function pathContains(parent: string, child: string): boolean {
const relative = path.relative(parent, child);
@@ -353,7 +314,7 @@ export function isSafeCommand(
};
case "true":
return {
reason: "No-op (true)",
reason: "Noop (true)",
group: "Utility",
};
case "echo":
@@ -368,20 +329,11 @@ export function isSafeCommand(
reason: "Ripgrep search",
group: "Searching",
};
case "find": {
// Certain options to `find` allow executing arbitrary processes, so we
// cannot auto-approve them.
if (
command.some((arg: string) => UNSAFE_OPTIONS_FOR_FIND_COMMAND.has(arg))
) {
break;
} else {
return {
reason: "Find files or directories",
group: "Searching",
};
}
}
case "find":
return {
reason: "Find files or directories",
group: "Searching",
};
case "grep":
return {
reason: "Text search (grep)",
@@ -469,27 +421,12 @@ function isValidSedNArg(arg: string | undefined): boolean {
return arg != null && /^(\d+,)?\d+p$/.test(arg);
}
const UNSAFE_OPTIONS_FOR_FIND_COMMAND: ReadonlySet<string> = new Set([
// Options that can execute arbitrary commands.
"-exec",
"-execdir",
"-ok",
"-okdir",
// Option that deletes matching files.
"-delete",
// Options that write pathnames to a file.
"-fls",
"-fprint",
"-fprint0",
"-fprintf",
]);
// ---------------- Helper utilities for complex shell expressions -----------------
// A conservative allow-list of bash operators that do not, on their own, cause
// A conservative allowlist of bash operators that do not, on their own, cause
// side effects. Redirections (>, >>, <, etc.) and command substitution `$()`
// are intentionally excluded. Parentheses used for grouping are treated as
// strings by `shell-quote`, so we do not add them here. Reference:
// strings by `shellquote`, so we do not add them here. Reference:
// https://github.com/substack/node-shell-quote#parsecmd-opts
const SAFE_SHELL_OPERATORS: ReadonlySet<string> = new Set([
"&&", // logical AND
@@ -515,7 +452,7 @@ function isEntireShellExpressionSafe(
}
try {
// Collect command segments delimited by operators. `shell-quote` represents
// Collect command segments delimited by operators. `shellquote` represents
// subshell grouping parentheses as literal strings "(" and ")"; treat them
// as unsafe to keep the logic simple (since subshells could introduce
// unexpected scope changes).
@@ -583,7 +520,7 @@ function isParseEntryWithOp(
return (
typeof entry === "object" &&
entry != null &&
// Using the safe `in` operator keeps the check property-safe even when
// Using the safe `in` operator keeps the check propertysafe even when
// `entry` is a `string`.
"op" in entry &&
typeof (entry as { op?: unknown }).op === "string"

View File

@@ -14,18 +14,19 @@ import type { ResponseItem } from "openai/resources/responses/responses";
import App from "./app";
import { runSinglePass } from "./cli-singlepass";
import { AgentLoop } from "./utils/agent/agent-loop";
import { initLogger } from "./utils/agent/log";
import { ReviewDecision } from "./utils/agent/review";
import { AutoApprovalMode } from "./utils/auto-approval-mode";
import { checkForUpdates } from "./utils/check-updates";
import {
getApiKey,
loadConfig,
PRETTY_PRINT,
INSTRUCTIONS_FILEPATH,
} from "./utils/config";
import { createInputItem } from "./utils/input-utils";
import { initLogger } from "./utils/logger/log";
import { isModelSupportedForResponses } from "./utils/model-utils.js";
import {
isModelSupportedForResponses,
preloadModels,
} from "./utils/model-utils.js";
import { parseToolCall } from "./utils/parsers";
import { onExit, setInkRenderer } from "./utils/terminal";
import chalk from "chalk";
@@ -52,11 +53,8 @@ const cli = meow(
$ codex completion <bash|zsh|fish>
Options
--version Print version and exit
-h, --help Show usage and exit
-m, --model <model> Model to use for completions (default: o4-mini)
-p, --provider <provider> Provider to use for completions (default: openai)
-i, --image <path> Path(s) to image files to include as input
-v, --view <rollout> Inspect a previously saved rollout instead of starting a session
-q, --quiet Non-interactive mode that only prints the assistant's final output
@@ -72,12 +70,6 @@ const cli = meow(
--full-stdout Do not truncate stdout/stderr from command outputs
--notify Enable desktop notifications for responses
--disable-response-storage Disable serverside response storage (sends the
full conversation context with every request)
--flex-mode Use "flex-mode" processing mode for the request (only supported
with models o3 and o4-mini)
Dangerous options
--dangerously-auto-approve-everything
Skip all confirmation prompts and execute commands without
@@ -99,10 +91,8 @@ const cli = meow(
flags: {
// misc
help: { type: "boolean", aliases: ["h"] },
version: { type: "boolean", description: "Print version and exit" },
view: { type: "string" },
model: { type: "string", aliases: ["m"] },
provider: { type: "string", aliases: ["p"] },
image: { type: "string", isMultiple: true, aliases: ["i"] },
quiet: {
type: "boolean",
@@ -143,17 +133,12 @@ const cli = meow(
},
noProjectDoc: {
type: "boolean",
description: "Disable automatic inclusion of project-level codex.md",
description: "Disable automatic inclusion of projectlevel codex.md",
},
projectDoc: {
type: "string",
description: "Path to a markdown file to include as project doc",
},
flexMode: {
type: "boolean",
description:
"Enable the flex-mode service tier (only supported by models o3 and o4-mini)",
},
fullStdout: {
type: "boolean",
description:
@@ -166,12 +151,6 @@ const cli = meow(
description: "Enable desktop notifications for responses",
},
disableResponseStorage: {
type: "boolean",
description:
"Disable server-side response storage (sends full conversation context with every request)",
},
// Experimental mode where whole directory is loaded in context and model is requested
// to make code edits in a single pass.
fullContext: {
@@ -203,7 +182,7 @@ _codex() {
}
_codex`,
fish: `# fish completion for codex
complete -c codex -a '(__fish_complete_path)' -d 'file path'`,
complete -c codex -a '(_fish_complete_path)' -d 'file path'`,
};
const script = scripts[shell];
if (!script) {
@@ -215,20 +194,19 @@ complete -c codex -a '(__fish_complete_path)' -d 'file path'`,
console.log(script);
process.exit(0);
}
// For --help, show help and exit.
// Show help if requested
if (cli.flags.help) {
cli.showHelp();
}
// For --config, open custom instructions file in editor and exit.
// Handle config flag: open instructions file in editor and exit
if (cli.flags.config) {
// Ensure configuration and instructions file exist
try {
loadConfig(); // Ensures the file is created if it doesn't already exit.
loadConfig();
} catch {
// ignore errors
}
const filePath = INSTRUCTIONS_FILEPATH;
const editor =
process.env["EDITOR"] || (process.platform === "win32" ? "notepad" : "vi");
@@ -240,87 +218,45 @@ if (cli.flags.config) {
// API key handling
// ---------------------------------------------------------------------------
const apiKey = process.env["OPENAI_API_KEY"];
if (!apiKey) {
// eslint-disable-next-line no-console
console.error(
`\n${chalk.red("Missing OpenAI API key.")}\n\n` +
`Set the environment variable ${chalk.bold("OPENAI_API_KEY")} ` +
`and re-run this command.\n` +
`You can create a key here: ${chalk.bold(
chalk.underline("https://platform.openai.com/account/api-keys"),
)}\n`,
);
process.exit(1);
}
const fullContextMode = Boolean(cli.flags.fullContext);
let config = loadConfig(undefined, undefined, {
cwd: process.cwd(),
disableProjectDoc: Boolean(cli.flags.noProjectDoc),
projectDocPath: cli.flags.projectDoc,
projectDocPath: cli.flags.projectDoc as string | undefined,
isFullContext: fullContextMode,
});
const prompt = cli.input[0];
const model = cli.flags.model ?? config.model;
const imagePaths = cli.flags.image;
const provider = cli.flags.provider ?? config.provider ?? "openai";
const apiKey = getApiKey(provider);
// Set of providers that don't require API keys
const NO_API_KEY_REQUIRED = new Set(["ollama"]);
// Skip API key validation for providers that don't require an API key
if (!apiKey && !NO_API_KEY_REQUIRED.has(provider.toLowerCase())) {
// eslint-disable-next-line no-console
console.error(
`\n${chalk.red(`Missing ${provider} API key.`)}\n\n` +
`Set the environment variable ${chalk.bold(
`${provider.toUpperCase()}_API_KEY`,
)} ` +
`and re-run this command.\n` +
`${
provider.toLowerCase() === "openai"
? `You can create a key here: ${chalk.bold(
chalk.underline("https://platform.openai.com/account/api-keys"),
)}\n`
: `You can create a ${chalk.bold(
`${provider.toUpperCase()}_API_KEY`,
)} ` + `in the ${chalk.bold(`${provider}`)} dashboard.\n`
}`,
);
process.exit(1);
}
const model = cli.flags.model;
const imagePaths = cli.flags.image as Array<string> | undefined;
config = {
apiKey,
...config,
model: model ?? config.model,
notify: Boolean(cli.flags.notify),
flexMode: Boolean(cli.flags.flexMode),
provider,
disableResponseStorage:
cli.flags.disableResponseStorage !== undefined
? Boolean(cli.flags.disableResponseStorage)
: config.disableResponseStorage,
};
// Check for updates after loading config. This is important because we write state file in
// the config dir.
try {
await checkForUpdates();
} catch {
// ignore
}
// For --flex-mode, validate and exit if incorrect.
if (cli.flags.flexMode) {
const allowedFlexModels = new Set(["o3", "o4-mini"]);
if (!allowedFlexModels.has(config.model)) {
// eslint-disable-next-line no-console
console.error(
`The --flex-mode option is only supported when using the 'o3' or 'o4-mini' models. ` +
`Current model: '${config.model}'.`,
);
process.exit(1);
}
}
if (
!(await isModelSupportedForResponses(provider, config.model)) &&
(!provider || provider.toLowerCase() === "openai")
) {
if (!(await isModelSupportedForResponses(config.model))) {
// eslint-disable-next-line no-console
console.error(
`The model "${config.model}" does not appear in the list of models ` +
`available to your account. Double-check the spelling (use\n` +
`available to your account. Doublecheck the spelling (use\n` +
` openai models list\n` +
`to see the full list) or choose another model with the --model flag.`,
);
@@ -329,7 +265,6 @@ if (
let rollout: AppRollout | undefined;
// For --view, optionally load an existing rollout from disk, display it and exit.
if (cli.flags.view) {
const viewPath = cli.flags.view;
const absolutePath = path.isAbsolute(viewPath)
@@ -345,7 +280,7 @@ if (cli.flags.view) {
}
}
// For --fullcontext, run the separate cli entrypoint and exit.
// If we are running in --fullcontext mode, do that and exit.
if (fullContextMode) {
await runSinglePass({
originalPrompt: prompt,
@@ -361,8 +296,14 @@ const additionalWritableRoots: ReadonlyArray<string> = (
cli.flags.writableRoot ?? []
).map((p) => path.resolve(p));
// For --quiet, run the cli without user interactions and exit.
if (cli.flags.quiet) {
// If we are running in --quiet mode, do that and exit.
const quietMode = Boolean(cli.flags.quiet);
const autoApproveEverything = Boolean(
cli.flags.dangerouslyAutoApproveEverything,
);
const fullStdout = Boolean(cli.flags.fullStdout);
if (quietMode) {
process.env["CODEX_QUIET_MODE"] = "1";
if (!prompt || prompt.trim() === "") {
// eslint-disable-next-line no-console
@@ -371,19 +312,12 @@ if (cli.flags.quiet) {
);
process.exit(1);
}
// Determine approval policy for quiet mode based on flags
const quietApprovalPolicy: ApprovalPolicy =
cli.flags.fullAuto || cli.flags.approvalMode === "full-auto"
? AutoApprovalMode.FULL_AUTO
: cli.flags.autoEdit || cli.flags.approvalMode === "auto-edit"
? AutoApprovalMode.AUTO_EDIT
: config.approvalMode || AutoApprovalMode.SUGGEST;
await runQuietMode({
prompt,
prompt: prompt as string,
imagePaths: imagePaths || [],
approvalPolicy: quietApprovalPolicy,
approvalPolicy: autoApproveEverything
? AutoApprovalMode.FULL_AUTO
: AutoApprovalMode.SUGGEST,
additionalWritableRoots,
config,
});
@@ -401,15 +335,16 @@ if (cli.flags.quiet) {
// it is more dangerous than --fullAuto we deliberately give it lower
// priority so a user specifying both flags still gets the safer behaviour.
// 3. --autoEdit automatically approve edits, but prompt for commands.
// 4. config.approvalMode - use the approvalMode setting from ~/.codex/config.json.
// 5. Default suggest mode (prompt for everything).
// 4. Default suggest mode (prompt for everything).
const approvalPolicy: ApprovalPolicy =
cli.flags.fullAuto || cli.flags.approvalMode === "full-auto"
? AutoApprovalMode.FULL_AUTO
: cli.flags.autoEdit || cli.flags.approvalMode === "auto-edit"
? AutoApprovalMode.AUTO_EDIT
: config.approvalMode || AutoApprovalMode.SUGGEST;
: AutoApprovalMode.SUGGEST;
preloadModels();
const instance = render(
<App
@@ -419,7 +354,7 @@ const instance = render(
imagePaths={imagePaths}
approvalPolicy={approvalPolicy}
additionalWritableRoots={additionalWritableRoots}
fullStdout={Boolean(cli.flags.fullStdout)}
fullStdout={fullStdout}
/>,
{
patchConsole: process.env["DEBUG"] ? false : true,
@@ -493,10 +428,8 @@ async function runQuietMode({
model: config.model,
config: config,
instructions: config.instructions,
provider: config.provider,
approvalPolicy,
additionalWritableRoots,
disableResponseStorage: config.disableResponseStorage,
onItem: (item: ResponseItem) => {
// eslint-disable-next-line no-console
console.log(formatResponseItemForQuietMode(item));
@@ -507,12 +440,7 @@ async function runQuietMode({
getCommandConfirmation: (
_command: Array<string>,
): Promise<CommandConfirmation> => {
// In quiet mode, default to NO_CONTINUE, except when in full-auto mode
const reviewDecision =
approvalPolicy === AutoApprovalMode.FULL_AUTO
? ReviewDecision.YES
: ReviewDecision.NO_CONTINUE;
return Promise.resolve({ review: reviewDecision });
return Promise.resolve({ review: ReviewDecision.NO_CONTINUE });
},
onLastResponseId: () => {
/* intentionally ignored in quiet mode */
@@ -533,13 +461,13 @@ process.on("SIGQUIT", exit);
process.on("SIGTERM", exit);
// ---------------------------------------------------------------------------
// Fallback for Ctrl-C when stdin is in raw-mode
// Fallback for CtrlC when stdin is in rawmode
// ---------------------------------------------------------------------------
if (process.stdin.isTTY) {
// Ensure we do not leave the terminal in raw mode if the user presses
// Ctrl-C while some other component has focus and Ink is intercepting
// input. Node does *not* emit a SIGINT in raw-mode, so we listen for the
// CtrlC while some other component has focus and Ink is intercepting
// input. Node does *not* emit a SIGINT in rawmode, so we listen for the
// corresponding byte (0x03) ourselves and trigger a graceful shutdown.
const onRawData = (data: Buffer | string): void => {
const str = Buffer.isBuffer(data) ? data.toString("utf8") : data;
@@ -550,6 +478,6 @@ if (process.stdin.isTTY) {
process.stdin.on("data", onRawData);
}
// Ensure terminal clean-up always runs, even when other code calls
// Ensure terminal cleanup always runs, even when other code calls
// `process.exit()` directly.
process.once("exit", onExit);

View File

@@ -14,7 +14,7 @@ import React, { useRef, useState } from "react";
* The real `process.stdin` object exposed by Node.js inherits these methods
* from `Socket`, but the lightweight stub used in tests only extends
* `EventEmitter`. Ink calls the two methods when enabling/disabling raw
* mode, so make them harmless no-ops when they're absent to avoid runtime
* mode, so make them harmless noops when they're absent to avoid runtime
* failures during unit tests.
* ----------------------------------------------------------------------- */
@@ -155,8 +155,6 @@ export interface MultilineTextEditorHandle {
isCursorAtLastRow(): boolean;
/** Full text contents */
getText(): string;
/** Move the cursor to the end of the text */
moveCursorToEnd(): void;
}
const MultilineTextEditorInner = (
@@ -261,47 +259,25 @@ const MultilineTextEditorInner = (
console.log("[MultilineTextEditor] event", { input, key });
}
// 1a) CSI-u / modifyOtherKeys *mode 2* (Ink strips initial ESC, so we
// start with '[') format: "[<code>;<modifiers>u".
// 1) CSIu / modifyOtherKeys (Ink strips initial ESC, so we start with '[')
if (input.startsWith("[") && input.endsWith("u")) {
const m = input.match(/^\[([0-9]+);([0-9]+)u$/);
if (m && m[1] === "13") {
const mod = Number(m[2]);
// In xterm's encoding: bit-1 (value 2) is Shift. Everything >1 that
// isn't exactly 1 means some modifier was held. We treat *shift or
// alt present* (2,3,4,6,8,9) as newline; Ctrl (bit-2 / value 4)
// triggers submit. See xterm/DEC modifyOtherKeys docs.
// In xterm's encoding: bit1 (value 2) is Shift. Everything >1 that
// isn't exactly 1 means some modifier was held. We treat *shift
// present* (2,4,6,8) as newline; plain (1) as submit.
// Xterm encodes modifier keys in `mod` bit2 (value 4) indicates
// that Ctrl was held. We avoid the `&` bitwise operator (disallowed
// by our ESLint config) by using arithmetic instead.
const hasCtrl = Math.floor(mod / 4) % 2 === 1;
if (hasCtrl) {
if (onSubmit) {
onSubmit(buffer.current.getText());
}
} else {
buffer.current.newline();
}
setVersion((v) => v + 1);
return;
}
}
// 1b) CSI-~ / modifyOtherKeys *mode 1* format: "[27;<mod>;<code>~".
// Terminals such as iTerm2 (default), older xterm versions, or when
// modifyOtherKeys=1 is configured, emit this legacy sequence. We
// translate it to the same behaviour as the mode2 variant above so
// that Shift+Enter (newline) / Ctrl+Enter (submit) work regardless
// of the users terminal settings.
if (input.startsWith("[27;") && input.endsWith("~")) {
const m = input.match(/^\[27;([0-9]+);13~$/);
if (m) {
const mod = Number(m[1]);
const hasCtrl = Math.floor(mod / 4) % 2 === 1;
if (hasCtrl) {
if (onSubmit) {
onSubmit(buffer.current.getText());
}
} else {
// Any variant without Ctrl just inserts newline (Shift, Alt, none)
buffer.current.newline();
}
setVersion((v) => v + 1);
@@ -374,16 +350,6 @@ const MultilineTextEditorInner = (
return row === lineCount - 1;
},
getText: () => buffer.current.getText(),
moveCursorToEnd: () => {
buffer.current.move("home");
const lines = buffer.current.getText().split("\n");
for (let i = 0; i < lines.length - 1; i++) {
buffer.current.move("down");
}
buffer.current.move("end");
// Force a re-render
setVersion((v) => v + 1);
},
}),
[],
);

View File

@@ -15,18 +15,11 @@ const DEFAULT_DENY_MESSAGE =
export function TerminalChatCommandReview({
confirmationPrompt,
onReviewCommand,
// callback to switch approval mode overlay
onSwitchApprovalMode,
explanation: propExplanation,
// whether this review Select is active (listening for keys)
isActive = true,
}: {
confirmationPrompt: React.ReactNode;
onReviewCommand: (decision: ReviewDecision, customMessage?: string) => void;
onSwitchApprovalMode: () => void;
explanation?: string;
// when false, disable the underlying Select so it won't capture input
isActive?: boolean;
}): React.ReactElement {
const [mode, setMode] = React.useState<"select" | "input" | "explanation">(
"select",
@@ -77,7 +70,6 @@ export function TerminalChatCommandReview({
const opts: Array<
| { label: string; value: ReviewDecision }
| { label: string; value: "edit" }
| { label: string; value: "switch" }
> = [
{
label: "Yes (y)",
@@ -101,11 +93,6 @@ export function TerminalChatCommandReview({
label: "Edit or give feedback (e)",
value: "edit",
},
// allow switching approval mode
{
label: "Switch approval mode (s)",
value: "switch",
},
{
label: "No, and keep going (n)",
value: ReviewDecision.NO_CONTINUE,
@@ -119,50 +106,44 @@ export function TerminalChatCommandReview({
return opts;
}, [showAlwaysApprove]);
useInput(
(input, key) => {
if (mode === "select") {
if (input === "y") {
onReviewCommand(ReviewDecision.YES);
} else if (input === "x") {
onReviewCommand(ReviewDecision.EXPLAIN);
} else if (input === "e") {
setMode("input");
} else if (input === "n") {
onReviewCommand(
ReviewDecision.NO_CONTINUE,
"Don't do that, keep going though",
);
} else if (input === "a" && showAlwaysApprove) {
onReviewCommand(ReviewDecision.ALWAYS);
} else if (input === "s") {
// switch approval mode
onSwitchApprovalMode();
} else if (key.escape) {
onReviewCommand(ReviewDecision.NO_EXIT);
}
} else if (mode === "explanation") {
// When in explanation mode, any key returns to select mode
if (key.return || key.escape || input === "x") {
setMode("select");
}
} else {
// text entry mode
if (key.return) {
// if user hit enter on empty msg, fall back to DEFAULT_DENY_MESSAGE
const custom = msg.trim() === "" ? DEFAULT_DENY_MESSAGE : msg;
onReviewCommand(ReviewDecision.NO_CONTINUE, custom);
} else if (key.escape) {
// treat escape as denial with default message as well
onReviewCommand(
ReviewDecision.NO_CONTINUE,
msg.trim() === "" ? DEFAULT_DENY_MESSAGE : msg,
);
}
useInput((input, key) => {
if (mode === "select") {
if (input === "y") {
onReviewCommand(ReviewDecision.YES);
} else if (input === "x") {
onReviewCommand(ReviewDecision.EXPLAIN);
} else if (input === "e") {
setMode("input");
} else if (input === "n") {
onReviewCommand(
ReviewDecision.NO_CONTINUE,
"Don't do that, keep going though",
);
} else if (input === "a" && showAlwaysApprove) {
onReviewCommand(ReviewDecision.ALWAYS);
} else if (key.escape) {
onReviewCommand(ReviewDecision.NO_EXIT);
}
},
{ isActive },
);
} else if (mode === "explanation") {
// When in explanation mode, any key returns to select mode
if (key.return || key.escape || input === "x") {
setMode("select");
}
} else {
// text entry mode
if (key.return) {
// if user hit enter on empty msg, fall back to DEFAULT_DENY_MESSAGE
const custom = msg.trim() === "" ? DEFAULT_DENY_MESSAGE : msg;
onReviewCommand(ReviewDecision.NO_CONTINUE, custom);
} else if (key.escape) {
// treat escape as denial with default message as well
onReviewCommand(
ReviewDecision.NO_CONTINUE,
msg.trim() === "" ? DEFAULT_DENY_MESSAGE : msg,
);
}
}
});
return (
<Box flexDirection="column" gap={1} borderStyle="round" marginTop={1}>
@@ -210,13 +191,9 @@ export function TerminalChatCommandReview({
<Text>Allow command?</Text>
<Box paddingX={2} flexDirection="column" gap={1}>
<Select
isDisabled={!isActive}
visibleOptionCount={approvalOptions.length}
onChange={(value: ReviewDecision | "edit" | "switch") => {
onChange={(value: ReviewDecision | "edit") => {
if (value === "edit") {
setMode("input");
} else if (value === "switch") {
onSwitchApprovalMode();
} else {
onReviewCommand(value);
}

View File

@@ -1,64 +0,0 @@
import { Box, Text } from "ink";
import React, { useMemo } from "react";
type TextCompletionProps = {
/**
* Array of text completion options to display in the list
*/
completions: Array<string>;
/**
* Maximum number of completion items to show at once in the view
*/
displayLimit: number;
/**
* Index of the currently selected completion in the completions array
*/
selectedCompletion: number;
};
function TerminalChatCompletions({
completions,
selectedCompletion,
displayLimit,
}: TextCompletionProps): JSX.Element {
const visibleItems = useMemo(() => {
// Try to keep selection centered in view
let startIndex = Math.max(
0,
selectedCompletion - Math.floor(displayLimit / 2),
);
// Fix window position when at the end of the list
if (completions.length - startIndex < displayLimit) {
startIndex = Math.max(0, completions.length - displayLimit);
}
const endIndex = Math.min(completions.length, startIndex + displayLimit);
return completions.slice(startIndex, endIndex).map((completion, index) => ({
completion,
originalIndex: index + startIndex,
}));
}, [completions, selectedCompletion, displayLimit]);
return (
<Box flexDirection="column">
{visibleItems.map(({ completion, originalIndex }) => (
<Text
key={completion}
dimColor={originalIndex !== selectedCompletion}
underline={originalIndex === selectedCompletion}
backgroundColor={
originalIndex === selectedCompletion ? "blackBright" : undefined
}
>
{completion}
</Text>
))}
</Box>
);
}
export default TerminalChatCompletions;

View File

@@ -1,28 +1,82 @@
import { log } from "../../utils/logger/log.js";
import { log, isLoggingEnabled } from "../../utils/agent/log.js";
import Spinner from "../vendor/ink-spinner.js";
import { Box, Text, useInput, useStdin } from "ink";
import React, { useState } from "react";
import { useInterval } from "use-interval";
// Retaining a single static placeholder text for potential future use. The
// more elaborate randomised thinking prompts were removed to streamline the
// UI the elapsedtime counter now provides sufficient feedback.
const thinkingTexts = ["Thinking"]; /* [
"Consulting the rubber duck",
"Maximizing paperclips",
"Reticulating splines",
"Immanentizing the Eschaton",
"Thinking",
"Thinking about thinking",
"Spinning in circles",
"Counting dust specks",
"Updating priors",
"Feeding the utility monster",
"Taking off",
"Wireheading",
"Counting to infinity",
"Staring into the Basilisk",
"Negotiationing acausal trades",
"Searching the library of babel",
"Multiplying matrices",
"Solving the halting problem",
"Counting grains of sand",
"Simulating a simulation",
"Asking the oracle",
"Detangling qubits",
"Reading tea leaves",
"Pondering universal love and transcendent joy",
"Feeling the AGI",
"Shaving the yak",
"Escaping local minima",
"Pruning the search tree",
"Descending the gradient",
"Bikeshedding",
"Securing funding",
"Rewriting in Rust",
"Engaging infinite improbability drive",
"Clapping with one hand",
"Synthesizing",
"Rebasing thesis onto antithesis",
"Transcending the loop",
"Frogeposting",
"Summoning",
"Peeking beyond the veil",
"Seeking",
"Entering deep thought",
"Meditating",
"Decomposing",
"Creating",
"Beseeching the machine spirit",
"Calibrating moral compass",
"Collapsing the wave function",
"Doodling",
"Translating whale song",
"Whispering to silicon",
"Looking for semicolons",
"Asking ChatGPT",
"Bargaining with entropy",
"Channeling",
"Cooking",
"Parroting stochastically",
]; */
export default function TerminalChatInputThinking({
onInterrupt,
active,
thinkingSeconds,
}: {
onInterrupt: () => void;
active: boolean;
thinkingSeconds: number;
}): React.ReactElement {
const [awaitingConfirm, setAwaitingConfirm] = useState(false);
const [dots, setDots] = useState("");
const [awaitingConfirm, setAwaitingConfirm] = useState(false);
// Animate the ellipsis
useInterval(() => {
setDots((prev) => (prev.length < 3 ? prev + "." : ""));
}, 500);
const [thinkingText, setThinkingText] = useState(
() => thinkingTexts[Math.floor(Math.random() * thinkingTexts.length)],
);
const { stdin, setRawMode } = useStdin();
@@ -40,9 +94,11 @@ export default function TerminalChatInputThinking({
const str = Buffer.isBuffer(data) ? data.toString("utf8") : data;
if (str === "\x1b\x1b") {
log(
"raw stdin: received collapsed ESC ESC starting confirmation timer",
);
if (isLoggingEnabled()) {
log(
"raw stdin: received collapsed ESC ESC starting confirmation timer",
);
}
setAwaitingConfirm(true);
setTimeout(() => setAwaitingConfirm(false), 1500);
}
@@ -54,7 +110,25 @@ export default function TerminalChatInputThinking({
};
}, [stdin, awaitingConfirm, onInterrupt, active, setRawMode]);
// No timers required beyond tracking the elapsed seconds supplied via props.
useInterval(() => {
setDots((prev) => (prev.length < 3 ? prev + "." : ""));
}, 500);
useInterval(
() => {
setThinkingText((prev) => {
let next = prev;
if (thinkingTexts.length > 1) {
while (next === prev) {
next =
thinkingTexts[Math.floor(Math.random() * thinkingTexts.length)];
}
}
return next;
});
},
active ? 30000 : null,
);
useInput(
(_input, key) => {
@@ -63,11 +137,15 @@ export default function TerminalChatInputThinking({
}
if (awaitingConfirm) {
log("useInput: second ESC detected triggering onInterrupt()");
if (isLoggingEnabled()) {
log("useInput: second ESC detected triggering onInterrupt()");
}
onInterrupt();
setAwaitingConfirm(false);
} else {
log("useInput: first ESC detected waiting for confirmation");
if (isLoggingEnabled()) {
log("useInput: first ESC detected waiting for confirmation");
}
setAwaitingConfirm(true);
setTimeout(() => setAwaitingConfirm(false), 1500);
}
@@ -75,41 +153,12 @@ export default function TerminalChatInputThinking({
{ isActive: active },
);
// Custom ball animation including the elapsed seconds
const ballFrames = [
"( ● )",
"( ● )",
"( ● )",
"( ● )",
"( ●)",
"( ● )",
"( ● )",
"( ● )",
"( ● )",
"(● )",
];
const [frame, setFrame] = useState(0);
useInterval(() => {
setFrame((idx) => (idx + 1) % ballFrames.length);
}, 80);
// Preserve the spinner (ball) animation while keeping the elapsed seconds
// text static. We achieve this by rendering the bouncing ball inside the
// parentheses and appending the seconds counter *after* the spinner rather
// than injecting it directly next to the ball (which caused the counter to
// move horizontally together with the ball).
const frameTemplate = ballFrames[frame] ?? ballFrames[0];
const frameWithSeconds = `${frameTemplate} ${thinkingSeconds}s`;
return (
<Box flexDirection="column" gap={1}>
<Box gap={2}>
<Text>{frameWithSeconds}</Text>
<Spinner type="ball" />
<Text>
Thinking
{thinkingText}
{dots}
</Text>
</Box>

View File

@@ -1,4 +1,3 @@
import type { MultilineTextEditorHandle } from "./multiline-editor";
import type { ReviewDecision } from "../../utils/agent/review.js";
import type { HistoryEntry } from "../../utils/storage/command-history.js";
import type {
@@ -6,29 +5,22 @@ import type {
ResponseItem,
} from "openai/resources/responses/responses.mjs";
import MultilineTextEditor from "./multiline-editor";
import { TerminalChatCommandReview } from "./terminal-chat-command-review.js";
import TextCompletions from "./terminal-chat-completions.js";
import { log, isLoggingEnabled } from "../../utils/agent/log.js";
import { loadConfig } from "../../utils/config.js";
import { getFileSystemSuggestions } from "../../utils/file-system-suggestions.js";
import { createInputItem } from "../../utils/input-utils.js";
import { log } from "../../utils/logger/log.js";
import { printAndResetSessionSummary } from "../../utils/session-cost.js";
import { setSessionId } from "../../utils/session.js";
import { SLASH_COMMANDS, type SlashCommand } from "../../utils/slash-commands";
import {
loadCommandHistory,
addToHistory,
} from "../../utils/storage/command-history.js";
import { clearTerminal, onExit } from "../../utils/terminal.js";
import Spinner from "../vendor/ink-spinner.js";
import TextInput from "../vendor/ink-text-input.js";
import { Box, Text, useApp, useInput, useStdin } from "ink";
import { fileURLToPath } from "node:url";
import React, {
useCallback,
useState,
Fragment,
useEffect,
useRef,
} from "react";
import React, { useCallback, useState, Fragment, useEffect } from "react";
import { useInterval } from "use-interval";
const suggestions = [
@@ -51,12 +43,9 @@ export default function TerminalChatInput({
openModelOverlay,
openApprovalOverlay,
openHelpOverlay,
openDiffOverlay,
onCompact,
interruptAgent,
active,
thinkingSeconds,
items = [],
}: {
isNew: boolean;
loading: boolean;
@@ -74,32 +63,16 @@ export default function TerminalChatInput({
openModelOverlay: () => void;
openApprovalOverlay: () => void;
openHelpOverlay: () => void;
openDiffOverlay: () => void;
onCompact: () => void;
interruptAgent: () => void;
active: boolean;
thinkingSeconds: number;
// New: current conversation items so we can include them in bug reports
items?: Array<ResponseItem>;
}): React.ReactElement {
// Slash command suggestion index
const [selectedSlashSuggestion, setSelectedSlashSuggestion] =
useState<number>(0);
const app = useApp();
const [selectedSuggestion, setSelectedSuggestion] = useState<number>(0);
const [input, setInput] = useState("");
const [history, setHistory] = useState<Array<HistoryEntry>>([]);
const [historyIndex, setHistoryIndex] = useState<number | null>(null);
const [draftInput, setDraftInput] = useState<string>("");
const [skipNextSubmit, setSkipNextSubmit] = useState<boolean>(false);
const [fsSuggestions, setFsSuggestions] = useState<Array<string>>([]);
const [selectedCompletion, setSelectedCompletion] = useState<number>(-1);
// Multiline text editor key to force remount after submission
const [editorKey, setEditorKey] = useState(0);
// Imperative handle from the multiline editor so we can query caret position
const editorRef = useRef<MultilineTextEditorHandle | null>(null);
// Track the caret row across keystrokes
const prevCursorRow = useRef<number | null>(null);
// Load command history on component mount
useEffect(() => {
@@ -110,150 +83,14 @@ export default function TerminalChatInput({
loadHistory();
}, []);
// Reset slash suggestion index when input prefix changes
useEffect(() => {
if (input.trim().startsWith("/")) {
setSelectedSlashSuggestion(0);
}
}, [input]);
useInput(
(_input, _key) => {
// Slash command navigation: up/down to select, enter to fill
if (!confirmationPrompt && !loading && input.trim().startsWith("/")) {
const prefix = input.trim();
const matches = SLASH_COMMANDS.filter((cmd: SlashCommand) =>
cmd.command.startsWith(prefix),
);
if (matches.length > 0) {
if (_key.tab) {
// Cycle and fill slash command suggestions on Tab
const len = matches.length;
// Determine new index based on shift state
const nextIdx = _key.shift
? selectedSlashSuggestion <= 0
? len - 1
: selectedSlashSuggestion - 1
: selectedSlashSuggestion >= len - 1
? 0
: selectedSlashSuggestion + 1;
setSelectedSlashSuggestion(nextIdx);
// Autocomplete the command in the input
const match = matches[nextIdx];
if (!match) {
return;
}
const cmd = match.command;
setInput(cmd);
setDraftInput(cmd);
return;
}
if (_key.upArrow) {
setSelectedSlashSuggestion((prev) =>
prev <= 0 ? matches.length - 1 : prev - 1,
);
return;
}
if (_key.downArrow) {
setSelectedSlashSuggestion((prev) =>
prev < 0 || prev >= matches.length - 1 ? 0 : prev + 1,
);
return;
}
if (_key.return) {
// Execute the currently selected slash command
const selIdx = selectedSlashSuggestion;
const cmdObj = matches[selIdx];
if (cmdObj) {
const cmd = cmdObj.command;
setInput("");
setDraftInput("");
setSelectedSlashSuggestion(0);
switch (cmd) {
case "/history":
openOverlay();
break;
case "/help":
openHelpOverlay();
break;
case "/compact":
onCompact();
break;
case "/model":
openModelOverlay();
break;
case "/approval":
openApprovalOverlay();
break;
case "/diff":
openDiffOverlay();
break;
case "/bug":
onSubmit(cmd);
break;
case "/clear":
onSubmit(cmd);
break;
case "/clearhistory":
onSubmit(cmd);
break;
default:
break;
}
}
return;
}
}
}
if (!confirmationPrompt && !loading) {
if (fsSuggestions.length > 0) {
if (_key.upArrow) {
setSelectedCompletion((prev) =>
prev <= 0 ? fsSuggestions.length - 1 : prev - 1,
);
return;
}
if (_key.downArrow) {
setSelectedCompletion((prev) =>
prev >= fsSuggestions.length - 1 ? 0 : prev + 1,
);
return;
}
if (_key.tab && selectedCompletion >= 0) {
const words = input.trim().split(/\s+/);
const selected = fsSuggestions[selectedCompletion];
if (words.length > 0 && selected) {
words[words.length - 1] = selected;
const newText = words.join(" ");
setInput(newText);
// Force remount of the editor with the new text
setEditorKey((k) => k + 1);
// We need to move the cursor to the end after editor remounts
setTimeout(() => {
editorRef.current?.moveCursorToEnd?.();
}, 0);
setFsSuggestions([]);
setSelectedCompletion(-1);
}
return;
}
}
if (_key.upArrow) {
// Only recall history when the caret was *already* on the very first
// row *before* this key-press.
const cursorRow = editorRef.current?.getRow?.() ?? 0;
const wasAtFirstRow = (prevCursorRow.current ?? cursorRow) === 0;
if (history.length > 0 && cursorRow === 0 && wasAtFirstRow) {
if (history.length > 0) {
if (historyIndex == null) {
const currentDraft = editorRef.current?.getText?.() ?? input;
setDraftInput(currentDraft);
setDraftInput(input);
}
let newIndex: number;
@@ -264,50 +101,27 @@ export default function TerminalChatInput({
}
setHistoryIndex(newIndex);
setInput(history[newIndex]?.command ?? "");
// Re-mount the editor so it picks up the new initialText
setEditorKey((k) => k + 1);
return; // we handled the key
}
// Otherwise let the event propagate so the editor moves the caret
return;
}
if (_key.downArrow) {
// Only move forward in history when we're already *in* history mode
// AND the caret sits on the last line of the buffer
if (historyIndex != null && editorRef.current?.isCursorAtLastRow()) {
const newIndex = historyIndex + 1;
if (newIndex >= history.length) {
setHistoryIndex(null);
setInput(draftInput);
setEditorKey((k) => k + 1);
} else {
setHistoryIndex(newIndex);
setInput(history[newIndex]?.command ?? "");
setEditorKey((k) => k + 1);
}
return; // handled
}
// Otherwise let it propagate
}
if (_key.tab) {
const words = input.split(/\s+/);
const mostRecentWord = words[words.length - 1];
if (mostRecentWord === undefined || mostRecentWord === "") {
if (historyIndex == null) {
return;
}
const completions = getFileSystemSuggestions(mostRecentWord);
setFsSuggestions(completions);
if (completions.length > 0) {
setSelectedCompletion(0);
const newIndex = historyIndex + 1;
if (newIndex >= history.length) {
setHistoryIndex(null);
setInput(draftInput);
} else {
setHistoryIndex(newIndex);
setInput(history[newIndex]?.command ?? "");
}
return;
}
}
// Update the cached cursor position *after* we've potentially handled
// the key so that the next event has the correct "previous" reference.
prevCursorRow.current = editorRef.current?.getRow?.() ?? null;
if (input.trim() === "" && isNew) {
if (_key.tab) {
setSelectedSuggestion(
@@ -339,16 +153,6 @@ export default function TerminalChatInput({
const onSubmit = useCallback(
async (value: string) => {
const inputValue = value.trim();
// If the user only entered a slash, do not send a chat message
if (inputValue === "/") {
setInput("");
return;
}
// Skip this submit if we just autocompleted a slash command
if (skipNextSubmit) {
setSkipNextSubmit(false);
return;
}
if (!inputValue) {
return;
}
@@ -365,12 +169,6 @@ export default function TerminalChatInput({
return;
}
if (inputValue === "/diff") {
setInput("");
openDiffOverlay();
return;
}
if (inputValue === "/compact") {
setInput("");
onCompact();
@@ -402,41 +200,25 @@ export default function TerminalChatInput({
setInput("");
setSessionId("");
setLastResponseId("");
// Clear the terminal screen (including scrollback) before resetting context
// Clear the terminal first so the summary is printed on a fresh
// screen before the new session starts.
clearTerminal();
// Emit a system notice in the chat; no raw console writes so Ink keeps control.
// Show the token/cost summary for the session that just ended.
printAndResetSessionSummary();
// Emit a system message to confirm the clear action. We *append*
// it so Ink's <Static> treats it as new output and actually renders it.
setItems((prev) => {
const filteredOldItems = prev.filter((item) => {
// Remove any tokenheavy entries (user/assistant turns and function calls)
if (
item.type === "message" &&
(item.role === "user" || item.role === "assistant")
) {
return false;
}
if (
item.type === "function_call" ||
item.type === "function_call_output"
) {
return false;
}
return true; // keep developer/system and other meta entries
});
return [
...filteredOldItems,
{
id: `clear-${Date.now()}`,
type: "message",
role: "system",
content: [{ type: "input_text", text: "Terminal cleared" }],
},
];
});
setItems((prev) => [
...prev,
{
id: `clear-${Date.now()}`,
type: "message",
role: "system",
content: [{ type: "input_text", text: "Context cleared" }],
},
]);
return;
} else if (inputValue === "/clearhistory") {
@@ -464,70 +246,6 @@ export default function TerminalChatInput({
},
);
return;
} else if (inputValue === "/bug") {
// Generate a GitHub bug report URL prefilled with session details
setInput("");
try {
// Dynamically import dependencies to avoid unnecessary bundle size
const [{ default: open }, os] = await Promise.all([
import("open"),
import("node:os"),
]);
// Lazy import CLI_VERSION to avoid circular deps
const { CLI_VERSION } = await import("../../utils/session.js");
const { buildBugReportUrl } = await import(
"../../utils/bug-report.js"
);
const url = buildBugReportUrl({
items: items ?? [],
cliVersion: CLI_VERSION,
model: loadConfig().model ?? "unknown",
platform: [os.platform(), os.arch(), os.release()]
.map((s) => `\`${s}\``)
.join(" | "),
});
// Open the URL in the user's default browser
await open(url, { wait: false });
// Inform the user in the chat history
setItems((prev) => [
...prev,
{
id: `bugreport-${Date.now()}`,
type: "message",
role: "system",
content: [
{
type: "input_text",
text: "📋 Opened browser to file a bug report. Please include any context that might help us fix the issue!",
},
],
},
]);
} catch (error) {
// If anything went wrong, notify the user
setItems((prev) => [
...prev,
{
id: `bugreport-error-${Date.now()}`,
type: "message",
role: "system",
content: [
{
type: "input_text",
text: `⚠️ Failed to create bug report URL: ${error}`,
},
],
},
]);
}
return;
} else if (inputValue.startsWith("/")) {
// Handle invalid/unrecognized commands.
@@ -604,8 +322,6 @@ export default function TerminalChatInput({
setDraftInput("");
setSelectedSuggestion(0);
setInput("");
setFsSuggestions([]);
setSelectedCompletion(-1);
},
[
setInput,
@@ -619,11 +335,8 @@ export default function TerminalChatInput({
openApprovalOverlay,
openModelOverlay,
openHelpOverlay,
openDiffOverlay,
history,
history, // Add history to the dependency array
onCompact,
skipNextSubmit,
items,
],
);
@@ -632,11 +345,7 @@ export default function TerminalChatInput({
<TerminalChatCommandReview
confirmationPrompt={confirmationPrompt}
onReviewCommand={submitConfirmation}
// allow switching approval mode via 'v'
onSwitchApprovalMode={openApprovalOverlay}
explanation={explanation}
// disable when input is inactive (e.g., overlay open)
isActive={active}
/>
);
}
@@ -648,113 +357,65 @@ export default function TerminalChatInput({
<TerminalChatInputThinking
onInterrupt={interruptAgent}
active={active}
thinkingSeconds={thinkingSeconds}
/>
) : (
<Box paddingX={1}>
<MultilineTextEditor
ref={editorRef}
onChange={(txt: string) => {
setDraftInput(txt);
<TextInput
focus={active}
placeholder={
selectedSuggestion
? `"${suggestions[selectedSuggestion - 1]}"`
: "send a message" +
(isNew ? " or press tab to select a suggestion" : "")
}
showCursor
value={input}
onChange={(value) => {
setDraftInput(value);
if (historyIndex != null) {
setHistoryIndex(null);
}
setInput(txt);
// Clear tab completions if a space is typed
if (txt.endsWith(" ")) {
setFsSuggestions([]);
setSelectedCompletion(-1);
} else if (fsSuggestions.length > 0) {
// Update file suggestions as user types
const words = txt.trim().split(/\s+/);
const mostRecentWord =
words.length > 0 ? words[words.length - 1] : "";
if (mostRecentWord !== undefined) {
setFsSuggestions(getFileSystemSuggestions(mostRecentWord));
}
}
}}
key={editorKey}
initialText={input}
height={6}
focus={active}
onSubmit={(txt) => {
onSubmit(txt);
setEditorKey((k) => k + 1);
setInput("");
setHistoryIndex(null);
setDraftInput("");
setInput(value);
}}
onSubmit={onSubmit}
/>
</Box>
)}
</Box>
{/* Slash command autocomplete suggestions */}
{input.trim().startsWith("/") && (
<Box flexDirection="column" paddingX={2} marginBottom={1}>
{SLASH_COMMANDS.filter((cmd: SlashCommand) =>
cmd.command.startsWith(input.trim()),
).map((cmd: SlashCommand, idx: number) => (
<Box key={cmd.command}>
<Text
backgroundColor={
idx === selectedSlashSuggestion ? "blackBright" : undefined
}
>
<Text color="blueBright">{cmd.command}</Text>
<Text> {cmd.description}</Text>
</Text>
</Box>
))}
</Box>
)}
<Box paddingX={2} marginBottom={1}>
{isNew && !input ? (
<Text dimColor>
try:{" "}
{suggestions.map((m, key) => (
<Fragment key={key}>
{key !== 0 ? " | " : ""}
<Text
backgroundColor={
key + 1 === selectedSuggestion ? "blackBright" : ""
}
>
{m}
</Text>
</Fragment>
))}
</Text>
) : fsSuggestions.length > 0 ? (
<TextCompletions
completions={fsSuggestions}
selectedCompletion={selectedCompletion}
displayLimit={5}
/>
) : (
<Text dimColor>
send q or ctrl+c to exit | send "/clear" to reset | send "/help" for
commands | press enter to send | shift+enter for new line
{contextLeftPercent > 25 && (
<>
{" — "}
<Text color={contextLeftPercent > 40 ? "green" : "yellow"}>
{Math.round(contextLeftPercent)}% context left
</Text>
</>
)}
{contextLeftPercent <= 25 && (
<>
{" — "}
<Text color="red">
{Math.round(contextLeftPercent)}% context left send
"/compact" to condense context
</Text>
</>
)}
</Text>
)}
<Text dimColor>
{isNew && !input ? (
<>
try:{" "}
{suggestions.map((m, key) => (
<Fragment key={key}>
{key !== 0 ? " | " : ""}
<Text
backgroundColor={
key + 1 === selectedSuggestion ? "blackBright" : ""
}
>
{m}
</Text>
</Fragment>
))}
</>
) : (
<>
send q or ctrl+c to exit | send "/clear" to reset | send "/help"
for commands | press enter to send
{contextLeftPercent < 25 && (
<>
{" — "}
<Text color="red">
{Math.round(contextLeftPercent)}% context left send
"/compact" to condense context
</Text>
</>
)}
</>
)}
</Text>
</Box>
</Box>
);
@@ -763,42 +424,12 @@ export default function TerminalChatInput({
function TerminalChatInputThinking({
onInterrupt,
active,
thinkingSeconds,
}: {
onInterrupt: () => void;
active: boolean;
thinkingSeconds: number;
}) {
const [awaitingConfirm, setAwaitingConfirm] = useState(false);
const [dots, setDots] = useState("");
// Animate ellipsis
useInterval(() => {
setDots((prev) => (prev.length < 3 ? prev + "." : ""));
}, 500);
// Spinner frames with embedded seconds
const ballFrames = [
"( ● )",
"( ● )",
"( ● )",
"( ● )",
"( ●)",
"( ● )",
"( ● )",
"( ● )",
"( ● )",
"(● )",
];
const [frame, setFrame] = useState(0);
useInterval(() => {
setFrame((idx) => (idx + 1) % ballFrames.length);
}, 80);
// Keep the elapsedseconds text fixed while the ball animation moves.
const frameTemplate = ballFrames[frame] ?? ballFrames[0];
const frameWithSeconds = `${frameTemplate} ${thinkingSeconds}s`;
const [awaitingConfirm, setAwaitingConfirm] = useState(false);
// ---------------------------------------------------------------------
// Raw stdin listener to catch the case where the terminal delivers two
@@ -829,9 +460,11 @@ function TerminalChatInputThinking({
const str = Buffer.isBuffer(data) ? data.toString("utf8") : data;
if (str === "\x1b\x1b") {
// Treat as the first Escape press prompt the user for confirmation.
log(
"raw stdin: received collapsed ESC ESC starting confirmation timer",
);
if (isLoggingEnabled()) {
log(
"raw stdin: received collapsed ESC ESC starting confirmation timer",
);
}
setAwaitingConfirm(true);
setTimeout(() => setAwaitingConfirm(false), 1500);
}
@@ -844,7 +477,10 @@ function TerminalChatInputThinking({
};
}, [stdin, awaitingConfirm, onInterrupt, active, setRawMode]);
// No local timer: the parent component supplies the elapsed time via props.
// Cycle the "Thinking…" animation dots.
useInterval(() => {
setDots((prev) => (prev.length < 3 ? prev + "." : ""));
}, 500);
// Listen for the escape key to allow the user to interrupt the current
// operation. We require two presses within a short window (1.5s) to avoid
@@ -856,11 +492,15 @@ function TerminalChatInputThinking({
}
if (awaitingConfirm) {
log("useInput: second ESC detected triggering onInterrupt()");
if (isLoggingEnabled()) {
log("useInput: second ESC detected triggering onInterrupt()");
}
onInterrupt();
setAwaitingConfirm(false);
} else {
log("useInput: first ESC detected waiting for confirmation");
if (isLoggingEnabled()) {
log("useInput: first ESC detected waiting for confirmation");
}
setAwaitingConfirm(true);
setTimeout(() => setAwaitingConfirm(false), 1500);
}
@@ -871,11 +511,8 @@ function TerminalChatInputThinking({
return (
<Box flexDirection="column" gap={1}>
<Box gap={2}>
<Text>{frameWithSeconds}</Text>
<Text>
Thinking
{dots}
</Text>
<Spinner type="ball" />
<Text>Thinking{dots}</Text>
</Box>
{awaitingConfirm && (
<Text dimColor>

View File

@@ -8,15 +8,17 @@ import type {
import MultilineTextEditor from "./multiline-editor";
import { TerminalChatCommandReview } from "./terminal-chat-command-review.js";
import { log, isLoggingEnabled } from "../../utils/agent/log.js";
import { loadConfig } from "../../utils/config.js";
import { createInputItem } from "../../utils/input-utils.js";
import { log } from "../../utils/logger/log.js";
import { printAndResetSessionSummary } from "../../utils/session-cost.js";
import { setSessionId } from "../../utils/session.js";
import {
loadCommandHistory,
addToHistory,
} from "../../utils/storage/command-history.js";
import { clearTerminal, onExit } from "../../utils/terminal.js";
import Spinner from "../vendor/ink-spinner.js";
import { Box, Text, useApp, useInput, useStdin } from "ink";
import { fileURLToPath } from "node:url";
import React, { useCallback, useState, Fragment, useEffect } from "react";
@@ -36,7 +38,39 @@ const typeHelpText = `ctrl+c to exit | "/clear" to reset context | "/help" for c
const DEBUG_HIST =
process.env["DEBUG_TCI"] === "1" || process.env["DEBUG_TCI"] === "true";
// Placeholder for potential dynamic prompts currently not used.
const thinkingTexts = ["Thinking"]; /* [
"Consulting the rubber duck",
"Maximizing paperclips",
"Reticulating splines",
"Immanentizing the Eschaton",
"Thinking",
"Thinking about thinking",
"Spinning in circles",
"Counting dust specks",
"Updating priors",
"Feeding the utility monster",
"Taking off",
"Wireheading",
"Counting to infinity",
"Staring into the Basilisk",
"Running acausal tariff negotiations",
"Searching the library of babel",
"Multiplying matrices",
"Solving the halting problem",
"Counting grains of sand",
"Simulating a simulation",
"Asking the oracle",
"Detangling qubits",
"Reading tea leaves",
"Pondering universal love and transcendent joy",
"Feeling the AGI",
"Shaving the yak",
"Escaping local minima",
"Pruning the search tree",
"Descending the gradient",
"Painting the bikeshed",
"Securing funding",
]; */
export default function TerminalChatInput({
isNew: _isNew,
@@ -52,10 +86,8 @@ export default function TerminalChatInput({
openModelOverlay,
openApprovalOverlay,
openHelpOverlay,
openDiffOverlay,
interruptAgent,
active,
thinkingSeconds,
}: {
isNew: boolean;
loading: boolean;
@@ -73,10 +105,8 @@ export default function TerminalChatInput({
openModelOverlay: () => void;
openApprovalOverlay: () => void;
openHelpOverlay: () => void;
openDiffOverlay: () => void;
interruptAgent: () => void;
active: boolean;
thinkingSeconds: number;
}): React.ReactElement {
const app = useApp();
const [selectedSuggestion, setSelectedSuggestion] = useState<number>(0);
@@ -232,12 +262,6 @@ export default function TerminalChatInput({
return;
}
if (inputValue === "/diff") {
setInput("");
openDiffOverlay();
return;
}
if (inputValue.startsWith("/model")) {
setInput("");
openModelOverlay();
@@ -263,16 +287,21 @@ export default function TerminalChatInput({
setInput("");
setSessionId("");
setLastResponseId("");
// Clear the terminal screen (including scrollback) before resetting context
// Clear screen then display session summary so the user sees it.
clearTerminal();
// Print a clear confirmation and reset conversation items.
setItems([
printAndResetSessionSummary();
// Emit a system message to confirm the clear action. We *append*
// it so Ink's <Static> treats it as new output and actually renders it.
setItems((prev) => [
...prev,
{
id: `clear-${Date.now()}`,
type: "message",
role: "system",
content: [{ type: "input_text", text: "Terminal cleared" }],
content: [{ type: "input_text", text: "Context cleared" }],
},
]);
@@ -344,7 +373,6 @@ export default function TerminalChatInput({
openApprovalOverlay,
openModelOverlay,
openHelpOverlay,
openDiffOverlay,
history, // Add history to the dependency array
],
);
@@ -354,11 +382,7 @@ export default function TerminalChatInput({
<TerminalChatCommandReview
confirmationPrompt={confirmationPrompt}
onReviewCommand={submitConfirmation}
// allow switching approval mode via 'v'
onSwitchApprovalMode={openApprovalOverlay}
explanation={explanation}
// disable when input is inactive (e.g., overlay open)
isActive={active}
/>
);
}
@@ -370,7 +394,6 @@ export default function TerminalChatInput({
<TerminalChatInputThinking
onInterrupt={interruptAgent}
active={active}
thinkingSeconds={thinkingSeconds}
/>
</Box>
) : (
@@ -436,43 +459,15 @@ export default function TerminalChatInput({
function TerminalChatInputThinking({
onInterrupt,
active,
thinkingSeconds,
}: {
onInterrupt: () => void;
active: boolean;
thinkingSeconds: number;
}) {
const [awaitingConfirm, setAwaitingConfirm] = useState(false);
const [dots, setDots] = useState("");
const [awaitingConfirm, setAwaitingConfirm] = useState(false);
// Animate ellipsis
useInterval(() => {
setDots((prev) => (prev.length < 3 ? prev + "." : ""));
}, 500);
// Spinner frames with seconds embedded
const ballFrames = [
"( ● )",
"( ● )",
"( ● )",
"( ● )",
"( ●)",
"( ● )",
"( ● )",
"( ● )",
"( ● )",
"(● )",
];
const [frame, setFrame] = useState(0);
useInterval(() => {
setFrame((idx) => (idx + 1) % ballFrames.length);
}, 80);
const frameTemplate = ballFrames[frame] ?? ballFrames[0];
const frameWithSeconds = (frameTemplate as string).replace(
"●",
`${thinkingSeconds}s`,
const [thinkingText] = useState(
() => thinkingTexts[Math.floor(Math.random() * thinkingTexts.length)],
);
// ---------------------------------------------------------------------
@@ -504,9 +499,11 @@ function TerminalChatInputThinking({
const str = Buffer.isBuffer(data) ? data.toString("utf8") : data;
if (str === "\x1b\x1b") {
// Treat as the first Escape press prompt the user for confirmation.
log(
"raw stdin: received collapsed ESC ESC starting confirmation timer",
);
if (isLoggingEnabled()) {
log(
"raw stdin: received collapsed ESC ESC starting confirmation timer",
);
}
setAwaitingConfirm(true);
setTimeout(() => setAwaitingConfirm(false), 1500);
}
@@ -519,7 +516,9 @@ function TerminalChatInputThinking({
};
}, [stdin, awaitingConfirm, onInterrupt, active, setRawMode]);
// Elapsed time provided via props no local interval needed.
useInterval(() => {
setDots((prev) => (prev.length < 3 ? prev + "." : ""));
}, 500);
useInput(
(_input, key) => {
@@ -528,11 +527,15 @@ function TerminalChatInputThinking({
}
if (awaitingConfirm) {
log("useInput: second ESC detected triggering onInterrupt()");
if (isLoggingEnabled()) {
log("useInput: second ESC detected triggering onInterrupt()");
}
onInterrupt();
setAwaitingConfirm(false);
} else {
log("useInput: first ESC detected waiting for confirmation");
if (isLoggingEnabled()) {
log("useInput: first ESC detected waiting for confirmation");
}
setAwaitingConfirm(true);
setTimeout(() => setAwaitingConfirm(false), 1500);
}
@@ -543,9 +546,9 @@ function TerminalChatInputThinking({
return (
<Box flexDirection="column" gap={1}>
<Box gap={2}>
<Text>{frameWithSeconds}</Text>
<Spinner type="ball" />
<Text>
Thinking
{thinkingText}
{dots}
</Text>
</Box>

View File

@@ -1,4 +1,3 @@
import type { OverlayModeType } from "./terminal-chat";
import type { TerminalRendererOptions } from "marked-terminal";
import type {
ResponseFunctionToolCallItem,
@@ -15,25 +14,18 @@ import chalk, { type ForegroundColorName } from "chalk";
import { Box, Text } from "ink";
import { parse, setOptions } from "marked";
import TerminalRenderer from "marked-terminal";
import React, { useEffect, useMemo } from "react";
import React, { useMemo } from "react";
export default function TerminalChatResponseItem({
item,
fullStdout = false,
setOverlayMode,
}: {
item: ResponseItem;
fullStdout?: boolean;
setOverlayMode?: React.Dispatch<React.SetStateAction<OverlayModeType>>;
}): React.ReactElement {
switch (item.type) {
case "message":
return (
<TerminalChatResponseMessage
setOverlayMode={setOverlayMode}
message={item}
/>
);
return <TerminalChatResponseMessage message={item} />;
case "function_call":
return <TerminalChatResponseToolCall message={item} />;
case "function_call_output":
@@ -106,23 +98,9 @@ const colorsByRole: Record<string, ForegroundColorName> = {
function TerminalChatResponseMessage({
message,
setOverlayMode,
}: {
message: ResponseInputMessageItem | ResponseOutputMessage;
setOverlayMode?: React.Dispatch<React.SetStateAction<OverlayModeType>>;
}) {
// auto switch to model mode if the system message contains "has been deprecated"
useEffect(() => {
if (message.role === "system") {
const systemMessage = message.content.find(
(c) => c.type === "input_text",
)?.text;
if (systemMessage?.includes("model_not_found")) {
setOverlayMode?.("model");
}
}
}, [message, setOverlayMode]);
return (
<Box flexDirection="column">
<Text bold color={colorsByRole[message.role] || "gray"}>

View File

@@ -0,0 +1,135 @@
import type { ResponseItem } from "openai/resources/responses/responses.mjs";
import { approximateTokensUsed } from "../../utils/approximate-tokens-used.js";
/**
* Typeguard that narrows a {@link ResponseItem} to one that represents a
* userauthored message. The OpenAI SDK represents both input *and* output
* messages with a discriminated union where:
* • `type` is the string literal "message" and
* • `role` is one of "user" | "assistant" | "system" | "developer".
*
* For the purposes of deduplication we only care about *user* messages so we
* detect those here in a single, reusable helper.
*/
function isUserMessage(
item: ResponseItem,
): item is ResponseItem & { type: "message"; role: "user"; content: unknown } {
return item.type === "message" && (item as { role?: string }).role === "user";
}
/**
* Returns the maximum context length (in tokens) for a given model.
* These numbers are besteffort guesses and provide a basis for UI percentages.
*/
export function maxTokensForModel(model: string): number {
const lower = model.toLowerCase();
// Heuristics for common context window sizes. Keep the checks loosely
// ordered from *largest* to *smallest* so that more specific longcontext
// models are detected before their shorter generic counterparts.
// Specialcase for 1,047,576token demo model (gpt4long). We match either
// the literal number or "gpt-4.1" variants we occasionally encounter.
if (lower.includes("1,047,576") || /gpt-4\.1/i.test(lower)) {
return 1047576;
}
if (lower.includes("128k") || /gpt-4\.5|gpt-4o-mini|gpt-4o\b/i.test(lower)) {
return 128000;
}
// Experimental oseries advertised at ~200k context
if (/\bo[134]\b|o[134]-mini|o1[- ]?pro/i.test(lower)) {
return 200000;
}
if (lower.includes("32k")) {
return 32000;
}
if (lower.includes("16k")) {
return 16000;
}
if (lower.includes("8k")) {
return 8000;
}
if (lower.includes("4k")) {
return 4000;
}
// Default to 128k for newer longcontext models
return 128000;
}
/**
* Calculates the percentage of tokens remaining in context for a model.
*/
export function calculateContextPercentRemaining(
items: Array<ResponseItem>,
model: string,
extraContextChars = 0,
): number {
const tokensFromItems = approximateTokensUsed(items);
const extraTokens = Math.ceil(extraContextChars / 4);
const used = tokensFromItems + extraTokens;
const max = maxTokensForModel(model);
const remaining = Math.max(0, max - used);
return (remaining / max) * 100;
}
/**
* Deduplicate the stream of {@link ResponseItem}s before they are persisted in
* component state.
*
* Historically we used the (optional) {@code id} field returned by the
* OpenAI streaming API as the primary key: the first occurrence of any given
* {@code id} “won” and subsequent duplicates were dropped. In practice this
* proved brittle because locallygenerated user messages dont include an
* {@code id}. The result was that if a user quickly pressed <Enter> twice the
* exact same message would appear twice in the transcript.
*
* The new rules are therefore:
* 1. If a {@link ResponseItem} has an {@code id} keep only the *first*
* occurrence of that {@code id} (this retains the previous behaviour for
* assistant / tool messages).
* 2. Additionally, collapse *consecutive* user messages with identical
* content. Two messages are considered identical when their serialized
* {@code content} array matches exactly. We purposefully restrict this
* to **adjacent** duplicates so that legitimately repeated questions at
* a later point in the conversation are still shown.
*/
export function uniqueById(items: Array<ResponseItem>): Array<ResponseItem> {
const seenIds = new Set<string>();
const deduped: Array<ResponseItem> = [];
for (const item of items) {
// ──────────────────────────────────────────────────────────────────
// Rule #1 deduplicate by id when present
// ──────────────────────────────────────────────────────────────────
if (typeof item.id === "string" && item.id.length > 0) {
if (seenIds.has(item.id)) {
continue; // skip duplicates
}
seenIds.add(item.id);
}
// ──────────────────────────────────────────────────────────────────
// Rule #2 collapse consecutive identical user messages
// ──────────────────────────────────────────────────────────────────
if (isUserMessage(item) && deduped.length > 0) {
const prev = deduped[deduped.length - 1]!;
if (
isUserMessage(prev) &&
// Note: the `content` field is an array of message parts. Performing
// a deep compare is overkill here; serialising to JSON is sufficient
// (and fast for the tiny payloads involved).
JSON.stringify(prev.content) === JSON.stringify(item.content)
) {
continue; // skip duplicate user message
}
}
deduped.push(item);
}
return deduped;
}

View File

@@ -5,46 +5,35 @@ import type { ColorName } from "chalk";
import type { ResponseItem } from "openai/resources/responses/responses.mjs";
import TerminalChatInput from "./terminal-chat-input.js";
import { TerminalChatToolCallCommand } from "./terminal-chat-tool-call-command.js";
import { TerminalChatToolCallCommand } from "./terminal-chat-tool-call-item.js";
import {
calculateContextPercentRemaining,
uniqueById,
} from "./terminal-chat-utils.js";
import TerminalMessageHistory from "./terminal-message-history.js";
import { formatCommandForDisplay } from "../../format-command.js";
import { useConfirmation } from "../../hooks/use-confirmation.js";
import { useTerminalSize } from "../../hooks/use-terminal-size.js";
import { AgentLoop } from "../../utils/agent/agent-loop.js";
import { isLoggingEnabled, log } from "../../utils/agent/log.js";
import { ReviewDecision } from "../../utils/agent/review.js";
import { generateCompactSummary } from "../../utils/compact-summary.js";
import { getBaseUrl, getApiKey, saveConfig } from "../../utils/config.js";
import { extractAppliedPatches as _extractAppliedPatches } from "../../utils/extract-applied-patches.js";
import { getGitDiff } from "../../utils/get-diff.js";
import { OPENAI_BASE_URL } from "../../utils/config.js";
import { createInputItem } from "../../utils/input-utils.js";
import { log } from "../../utils/logger/log.js";
import {
getAvailableModels,
calculateContextPercentRemaining,
uniqueById,
} from "../../utils/model-utils.js";
import { getAvailableModels } from "../../utils/model-utils.js";
import { CLI_VERSION } from "../../utils/session.js";
import { shortCwd } from "../../utils/short-path.js";
import { saveRollout } from "../../utils/storage/save-rollout.js";
import ApprovalModeOverlay from "../approval-mode-overlay.js";
import DiffOverlay from "../diff-overlay.js";
import HelpOverlay from "../help-overlay.js";
import HistoryOverlay from "../history-overlay.js";
import ModelOverlay from "../model-overlay.js";
import { Box, Text } from "ink";
import { spawn } from "node:child_process";
import { exec } from "node:child_process";
import OpenAI from "openai";
import React, { useEffect, useMemo, useRef, useState } from "react";
import { inspect } from "util";
export type OverlayModeType =
| "none"
| "history"
| "model"
| "approval"
| "help"
| "diff";
type Props = {
config: AppConfig;
prompt?: string;
@@ -65,21 +54,17 @@ const colorsByPolicy: Record<ApprovalPolicy, ColorName | undefined> = {
*
* @param command The command to explain
* @param model The model to use for generating the explanation
* @param flexMode Whether to use the flex-mode service tier
* @param config The configuration object
* @returns A human-readable explanation of what the command does
*/
async function generateCommandExplanation(
command: Array<string>,
model: string,
flexMode: boolean,
config: AppConfig,
): Promise<string> {
try {
// Create a temporary OpenAI client
const oai = new OpenAI({
apiKey: getApiKey(config.provider),
baseURL: getBaseUrl(config.provider),
apiKey: process.env["OPENAI_API_KEY"],
baseURL: OPENAI_BASE_URL,
});
// Format the command for display
@@ -88,7 +73,6 @@ async function generateCommandExplanation(
// Create a prompt that asks for an explanation with a more detailed system prompt
const response = await oai.chat.completions.create({
model,
...(flexMode ? { service_tier: "flex" } : {}),
messages: [
{
role: "system",
@@ -109,8 +93,11 @@ async function generateCommandExplanation(
} catch (error) {
log(`Error generating command explanation: ${error}`);
// Improved error handling with more specific error information
let errorMessage = "Unable to generate explanation due to an error.";
if (error instanceof Error) {
// Include specific error message for better debugging
errorMessage = `Unable to generate explanation: ${error.message}`;
// If it's an API error, check for more specific information
@@ -141,26 +128,21 @@ export default function TerminalChat({
additionalWritableRoots,
fullStdout,
}: Props): React.ReactElement {
// Desktop notification setting
const notify = config.notify;
const [model, setModel] = useState<string>(config.model);
const [provider, setProvider] = useState<string>(config.provider || "openai");
const [lastResponseId, setLastResponseId] = useState<string | null>(null);
const [items, setItems] = useState<Array<ResponseItem>>([]);
const [loading, setLoading] = useState<boolean>(false);
// Allow switching approval modes at runtime via an overlay.
const [approvalPolicy, setApprovalPolicy] = useState<ApprovalPolicy>(
initialApprovalPolicy,
);
const [thinkingSeconds, setThinkingSeconds] = useState(0);
const handleCompact = async () => {
setLoading(true);
try {
const summary = await generateCompactSummary(
items,
model,
Boolean(config.flexMode),
config,
);
const summary = await generateCompactSummary(items, model);
setItems([
{
id: `compact-${Date.now()}`,
@@ -185,21 +167,15 @@ export default function TerminalChat({
setLoading(false);
}
};
const {
requestConfirmation,
confirmationPrompt,
explanation,
submitConfirmation,
} = useConfirmation();
const [overlayMode, setOverlayMode] = useState<OverlayModeType>("none");
// Store the diff text when opening the diff overlay so the view isnt
// recomputed on every rerender while it is open.
// diffText is passed down to the DiffOverlay component. The setter is
// currently unused but retained for potential future updates. Prefix with
// an underscore so eslint ignores the unused variable.
const [diffText, _setDiffText] = useState<string>("");
const [overlayMode, setOverlayMode] = useState<
"none" | "history" | "model" | "approval" | "help"
>("none");
const [initialPrompt, setInitialPrompt] = useState(_initialPrompt);
const [initialImagePaths, setInitialImagePaths] =
@@ -215,44 +191,39 @@ export default function TerminalChat({
// ────────────────────────────────────────────────────────────────
// DEBUG: log every render w/ key bits of state
// ────────────────────────────────────────────────────────────────
log(
`render - agent? ${Boolean(agentRef.current)} loading=${loading} items=${
items.length
}`,
);
if (isLoggingEnabled()) {
log(
`render agent? ${Boolean(agentRef.current)} loading=${loading} items=${
items.length
}`,
);
}
useEffect(() => {
// Skip recreating the agent if awaiting a decision on a pending confirmation.
if (confirmationPrompt != null) {
log("skip AgentLoop recreation due to pending confirmationPrompt");
return;
if (isLoggingEnabled()) {
log("creating NEW AgentLoop");
log(
`model=${model} instructions=${Boolean(
config.instructions,
)} approvalPolicy=${approvalPolicy}`,
);
}
log("creating NEW AgentLoop");
log(
`model=${model} provider=${provider} instructions=${Boolean(
config.instructions,
)} approvalPolicy=${approvalPolicy}`,
);
// Tear down any existing loop before creating a new one.
// Tear down any existing loop before creating a new one
agentRef.current?.terminate();
const sessionId = crypto.randomUUID();
agentRef.current = new AgentLoop({
model,
provider,
config,
instructions: config.instructions,
approvalPolicy,
disableResponseStorage: config.disableResponseStorage,
additionalWritableRoots,
onLastResponseId: setLastResponseId,
onItem: (item) => {
log(`onItem: ${JSON.stringify(item)}`);
setItems((prev) => {
const updated = uniqueById([...prev, item as ResponseItem]);
saveRollout(sessionId, updated);
saveRollout(updated);
return updated;
});
},
@@ -269,18 +240,15 @@ export default function TerminalChat({
<TerminalChatToolCallCommand commandForDisplay={commandForDisplay} />,
);
// If the user wants an explanation, generate one and ask again.
// If the user wants an explanation, generate one and ask again
if (review === ReviewDecision.EXPLAIN) {
log(`Generating explanation for command: ${commandForDisplay}`);
const explanation = await generateCommandExplanation(
command,
model,
Boolean(config.flexMode),
config,
);
// Generate an explanation using the same model
const explanation = await generateCommandExplanation(command, model);
log(`Generated explanation: ${explanation}`);
// Ask for confirmation again, but with the explanation.
// Ask for confirmation again, but with the explanation
const confirmResult = await requestConfirmation(
<TerminalChatToolCallCommand
commandForDisplay={commandForDisplay}
@@ -288,11 +256,11 @@ export default function TerminalChat({
/>,
);
// Update the decision based on the second confirmation.
// Update the decision based on the second confirmation
review = confirmResult.decision;
customDenyMessage = confirmResult.customDenyMessage;
// Return the final decision with the explanation.
// Return the final decision with the explanation
return { review, customDenyMessage, applyPatch, explanation };
}
@@ -300,23 +268,30 @@ export default function TerminalChat({
},
});
// Force a render so JSX below can "see" the freshly created agent.
// force a render so JSX below can "see" the freshly created agent
forceUpdate();
log(`AgentLoop created: ${inspect(agentRef.current, { depth: 1 })}`);
if (isLoggingEnabled()) {
log(`AgentLoop created: ${inspect(agentRef.current, { depth: 1 })}`);
}
return () => {
log("terminating AgentLoop");
if (isLoggingEnabled()) {
log("terminating AgentLoop");
}
agentRef.current?.terminate();
agentRef.current = undefined;
forceUpdate(); // rerender after teardown too
};
// We intentionally omit 'approvalPolicy' and 'confirmationPrompt' from the deps
// so switching modes or showing confirmation dialogs doesnt tear down the loop.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [model, provider, config, requestConfirmation, additionalWritableRoots]);
}, [
model,
config,
approvalPolicy,
requestConfirmation,
additionalWritableRoots,
]);
// Whenever loading starts/stops, reset or start a timer — but pause the
// whenever loading starts/stops, reset or start a timer — but pause the
// timer while a confirmation overlay is displayed so we don't trigger a
// rerender every second during apply_patch reviews.
useEffect(() => {
@@ -341,15 +316,14 @@ export default function TerminalChat({
};
}, [loading, confirmationPrompt]);
// Notify desktop with a preview when an assistant response arrives.
// Notify desktop with a preview when an assistant response arrives
const prevLoadingRef = useRef<boolean>(false);
useEffect(() => {
// Only notify when notifications are enabled.
// Only notify when notifications are enabled
if (!notify) {
prevLoadingRef.current = loading;
return;
}
if (
prevLoadingRef.current &&
!loading &&
@@ -376,20 +350,21 @@ export default function TerminalChat({
const safePreview = preview.replace(/"/g, '\\"');
const title = "Codex CLI";
const cwd = PWD;
spawn("osascript", [
"-e",
`display notification "${safePreview}" with title "${title}" subtitle "${cwd}" sound name "Ping"`,
]);
exec(
`osascript -e 'display notification "${safePreview}" with title "${title}" subtitle "${cwd}" sound name "Ping"'`,
);
}
}
}
prevLoadingRef.current = loading;
}, [notify, loading, confirmationPrompt, items, PWD]);
// Let's also track whenever the ref becomes available.
// Let's also track whenever the ref becomes available
const agent = agentRef.current;
useEffect(() => {
log(`agentRef.current is now ${Boolean(agent)}`);
if (isLoggingEnabled()) {
log(`agentRef.current is now ${Boolean(agent)}`);
}
}, [agent]);
// ---------------------------------------------------------------------
@@ -409,7 +384,7 @@ export default function TerminalChat({
const inputItems = [
await createInputItem(initialPrompt || "", initialImagePaths || []),
];
// Clear them to prevent subsequent runs.
// Clear them to prevent subsequent runs
setInitialPrompt("");
setInitialImagePaths([]);
agent?.run(inputItems);
@@ -422,7 +397,7 @@ export default function TerminalChat({
// ────────────────────────────────────────────────────────────────
useEffect(() => {
(async () => {
const available = await getAvailableModels(provider);
const available = await getAvailableModels();
if (model && available.length > 0 && !available.includes(model)) {
setItems((prev) => [
...prev,
@@ -433,7 +408,7 @@ export default function TerminalChat({
content: [
{
type: "input_text",
text: `Warning: model "${model}" is not in the list of available models for provider "${provider}".`,
text: `Warning: model "${model}" is not in the list of available models returned by OpenAI.`,
},
],
},
@@ -444,7 +419,7 @@ export default function TerminalChat({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
// Just render every item in order, no grouping/collapse.
// Just render every item in order, no grouping/collapse
const lastMessageBatch = items.map((item) => ({ item }));
const groupCounts: Record<string, number> = {};
const userMsgCount = items.filter(
@@ -452,8 +427,14 @@ export default function TerminalChat({
).length;
const contextLeftPercent = useMemo(
() => calculateContextPercentRemaining(items, model),
[items, model],
() =>
calculateContextPercentRemaining(
items,
model,
// static system instructions count towards the context budget too
config.instructions?.length ?? 0,
),
[items, model, config.instructions],
);
return (
@@ -461,7 +442,6 @@ export default function TerminalChat({
<Box flexDirection="column">
{agent ? (
<TerminalMessageHistory
setOverlayMode={setOverlayMode}
batch={lastMessageBatch}
groupCounts={groupCounts}
items={items}
@@ -475,12 +455,10 @@ export default function TerminalChat({
version: CLI_VERSION,
PWD,
model,
provider,
approvalPolicy,
colorsByPolicy,
agent,
initialImagePaths,
flexModeEnabled: Boolean(config.flexMode),
}}
/>
) : (
@@ -488,7 +466,7 @@ export default function TerminalChat({
<Text color="gray">Initializing agent</Text>
</Box>
)}
{overlayMode === "none" && agent && (
{agent && (
<TerminalChatInput
loading={loading}
setItems={setItems}
@@ -510,35 +488,17 @@ export default function TerminalChat({
openModelOverlay={() => setOverlayMode("model")}
openApprovalOverlay={() => setOverlayMode("approval")}
openHelpOverlay={() => setOverlayMode("help")}
openDiffOverlay={() => {
const { isGitRepo, diff } = getGitDiff();
let text: string;
if (isGitRepo) {
text = diff;
} else {
text = "`/diff` — _not inside a git repository_";
}
setItems((prev) => [
...prev,
{
id: `diff-${Date.now()}`,
type: "message",
role: "system",
content: [{ type: "input_text", text }],
},
]);
// Ensure no overlay is shown.
setOverlayMode("none");
}}
onCompact={handleCompact}
active={overlayMode === "none"}
interruptAgent={() => {
if (!agent) {
return;
}
log(
"TerminalChat: interruptAgent invoked calling agent.cancel()",
);
if (isLoggingEnabled()) {
log(
"TerminalChat: interruptAgent invoked calling agent.cancel()",
);
}
agent.cancel();
setLoading(false);
@@ -562,8 +522,6 @@ export default function TerminalChat({
agent.run(inputs, lastResponseId || "");
return {};
}}
items={items}
thinkingSeconds={thinkingSeconds}
/>
)}
{overlayMode === "history" && (
@@ -572,15 +530,15 @@ export default function TerminalChat({
{overlayMode === "model" && (
<ModelOverlay
currentModel={model}
providers={config.providers}
currentProvider={provider}
hasLastResponse={Boolean(lastResponseId)}
onSelect={(newModel) => {
log(
"TerminalChat: interruptAgent invoked calling agent.cancel()",
);
if (!agent) {
log("TerminalChat: agent is not ready yet");
if (isLoggingEnabled()) {
log(
"TerminalChat: interruptAgent invoked calling agent.cancel()",
);
if (!agent) {
log("TerminalChat: agent is not ready yet");
}
}
agent?.cancel();
setLoading(false);
@@ -590,13 +548,6 @@ export default function TerminalChat({
prev && newModel !== model ? null : prev,
);
// Save model to config
saveConfig({
...config,
model: newModel,
provider: provider,
});
setItems((prev) => [
...prev,
{
@@ -614,51 +565,6 @@ export default function TerminalChat({
setOverlayMode("none");
}}
onSelectProvider={(newProvider) => {
log(
"TerminalChat: interruptAgent invoked calling agent.cancel()",
);
if (!agent) {
log("TerminalChat: agent is not ready yet");
}
agent?.cancel();
setLoading(false);
// Select default model for the new provider.
const defaultModel = model;
// Save provider to config.
const updatedConfig = {
...config,
provider: newProvider,
model: defaultModel,
};
saveConfig(updatedConfig);
setProvider(newProvider);
setModel(defaultModel);
setLastResponseId((prev) =>
prev && newProvider !== provider ? null : prev,
);
setItems((prev) => [
...prev,
{
id: `switch-provider-${Date.now()}`,
type: "message",
role: "system",
content: [
{
type: "input_text",
text: `Switched provider to ${newProvider} with model ${defaultModel}`,
},
],
},
]);
// Don't close the overlay so user can select a model for the new provider
// setOverlayMode("none");
}}
onExit={() => setOverlayMode("none")}
/>
)}
@@ -667,19 +573,12 @@ export default function TerminalChat({
<ApprovalModeOverlay
currentMode={approvalPolicy}
onSelect={(newMode) => {
// Update approval policy without cancelling an in-progress session.
agent?.cancel();
setLoading(false);
if (newMode === approvalPolicy) {
return;
}
setApprovalPolicy(newMode as ApprovalPolicy);
if (agentRef.current) {
(
agentRef.current as unknown as {
approvalPolicy: ApprovalPolicy;
}
).approvalPolicy = newMode as ApprovalPolicy;
}
setItems((prev) => [
...prev,
{
@@ -704,13 +603,6 @@ export default function TerminalChat({
{overlayMode === "help" && (
<HelpOverlay onExit={() => setOverlayMode("none")} />
)}
{overlayMode === "diff" && (
<DiffOverlay
diffText={diffText}
onExit={() => setOverlayMode("none")}
/>
)}
</Box>
</Box>
);

View File

@@ -9,12 +9,10 @@ export interface TerminalHeaderProps {
version: string;
PWD: string;
model: string;
provider?: string;
approvalPolicy: string;
colorsByPolicy: Record<string, string | undefined>;
agent?: AgentLoop;
initialImagePaths?: Array<string>;
flexModeEnabled?: boolean;
}
const TerminalHeader: React.FC<TerminalHeaderProps> = ({
@@ -22,21 +20,18 @@ const TerminalHeader: React.FC<TerminalHeaderProps> = ({
version,
PWD,
model,
provider = "openai",
approvalPolicy,
colorsByPolicy,
agent,
initialImagePaths,
flexModeEnabled = false,
}) => {
return (
<>
{terminalRows < 10 ? (
// Compact header for small terminal windows
<Text>
Codex v{version} - {PWD} - {model} ({provider}) -{" "}
Codex v{version} {PWD} {model} {" "}
<Text color={colorsByPolicy[approvalPolicy]}>{approvalPolicy}</Text>
{flexModeEnabled ? " - flex-mode" : ""}
</Text>
) : (
<>
@@ -67,22 +62,12 @@ const TerminalHeader: React.FC<TerminalHeaderProps> = ({
<Text dimColor>
<Text color="blueBright"></Text> model: <Text bold>{model}</Text>
</Text>
<Text dimColor>
<Text color="blueBright"></Text> provider:{" "}
<Text bold>{provider}</Text>
</Text>
<Text dimColor>
<Text color="blueBright"></Text> approval:{" "}
<Text bold color={colorsByPolicy[approvalPolicy]} dimColor>
{approvalPolicy}
</Text>
</Text>
{flexModeEnabled && (
<Text dimColor>
<Text color="blueBright"></Text> flex-mode:{" "}
<Text bold>enabled</Text>
</Text>
)}
{initialImagePaths?.map((img, idx) => (
<Text key={img ?? idx} color="gray">
<Text color="blueBright"></Text> image:{" "}

View File

@@ -1,18 +1,17 @@
import type { OverlayModeType } from "./terminal-chat.js";
import type { TerminalHeaderProps } from "./terminal-header.js";
import type { GroupedResponseItem } from "./use-message-grouping.js";
import type { ResponseItem } from "openai/resources/responses/responses.mjs";
import TerminalChatResponseItem from "./terminal-chat-response-item.js";
import TerminalHeader from "./terminal-header.js";
import { Box, Static } from "ink";
import { Box, Static, Text } from "ink";
import React, { useMemo } from "react";
// A batch entry can either be a standalone response item or a grouped set of
// items (e.g. autoapproved toolcall batches) that should be rendered
// together.
type BatchEntry = { item?: ResponseItem; group?: GroupedResponseItem };
type TerminalMessageHistoryProps = {
type MessageHistoryProps = {
batch: Array<BatchEntry>;
groupCounts: Record<string, number>;
items: Array<ResponseItem>;
@@ -22,25 +21,25 @@ type TerminalMessageHistoryProps = {
thinkingSeconds: number;
headerProps: TerminalHeaderProps;
fullStdout: boolean;
setOverlayMode: React.Dispatch<React.SetStateAction<OverlayModeType>>;
};
const TerminalMessageHistory: React.FC<TerminalMessageHistoryProps> = ({
const MessageHistory: React.FC<MessageHistoryProps> = ({
batch,
headerProps,
// `loading` and `thinkingSeconds` handled by input component now.
loading: _loading,
thinkingSeconds: _thinkingSeconds,
loading,
thinkingSeconds,
fullStdout,
setOverlayMode,
}) => {
// Flatten batch entries to response items.
const messages = useMemo(() => batch.map(({ item }) => item!), [batch]);
return (
<Box flexDirection="column">
{/* The dedicated thinking indicator in the input area now displays the
elapsed time, so we no longer render a separate counter here. */}
{loading && (
<Box marginTop={1}>
<Text color="yellow">{`thinking for ${thinkingSeconds}s`}</Text>
</Box>
)}
<Static items={["header", ...messages]}>
{(item, index) => {
if (item === "header") {
@@ -68,7 +67,6 @@ const TerminalMessageHistory: React.FC<TerminalMessageHistoryProps> = ({
<TerminalChatResponseItem
item={message}
fullStdout={fullStdout}
setOverlayMode={setOverlayMode}
/>
</Box>
);
@@ -78,4 +76,4 @@ const TerminalMessageHistory: React.FC<TerminalMessageHistoryProps> = ({
);
};
export default React.memo(TerminalMessageHistory);
export default React.memo(MessageHistory);

View File

@@ -1,93 +0,0 @@
import { Box, Text, useInput } from "ink";
import React, { useState } from "react";
/**
* Simple scrollable view for displaying a diff.
* The component is intentionally lightweight and mirrors the UX of
* HistoryOverlay: Up/Down or j/k to scroll, PgUp/PgDn for paging and Esc to
* close. The caller is responsible for computing the diff text.
*/
export default function DiffOverlay({
diffText,
onExit,
}: {
diffText: string;
onExit: () => void;
}): JSX.Element {
const lines = diffText.length > 0 ? diffText.split("\n") : ["(no changes)"];
const [cursor, setCursor] = useState(0);
// Determine how many rows we can display similar to HistoryOverlay.
const rows = process.stdout.rows || 24;
const headerRows = 2;
const footerRows = 1;
const maxVisible = Math.max(4, rows - headerRows - footerRows);
useInput((input, key) => {
if (key.escape || input === "q") {
onExit();
return;
}
if (key.downArrow || input === "j") {
setCursor((c) => Math.min(lines.length - 1, c + 1));
} else if (key.upArrow || input === "k") {
setCursor((c) => Math.max(0, c - 1));
} else if (key.pageDown) {
setCursor((c) => Math.min(lines.length - 1, c + maxVisible));
} else if (key.pageUp) {
setCursor((c) => Math.max(0, c - maxVisible));
} else if (input === "g") {
setCursor(0);
} else if (input === "G") {
setCursor(lines.length - 1);
}
});
const firstVisible = Math.min(
Math.max(0, cursor - Math.floor(maxVisible / 2)),
Math.max(0, lines.length - maxVisible),
);
const visible = lines.slice(firstVisible, firstVisible + maxVisible);
// Very small helper to colorize diff lines in a basic way.
function renderLine(line: string, idx: number): JSX.Element {
let color: "green" | "red" | "cyan" | undefined = undefined;
if (line.startsWith("+")) {
color = "green";
} else if (line.startsWith("-")) {
color = "red";
} else if (line.startsWith("@@") || line.startsWith("diff --git")) {
color = "cyan";
}
return (
<Text key={idx} color={color} wrap="truncate-end">
{line === "" ? " " : line}
</Text>
);
}
return (
<Box
flexDirection="column"
borderStyle="round"
borderColor="gray"
width={Math.min(120, process.stdout.columns || 120)}
>
<Box paddingX={1}>
<Text bold>Working tree diff ({lines.length} lines)</Text>
</Box>
<Box flexDirection="column" paddingX={1}>
{visible.map((line, idx) => {
return renderLine(line, firstVisible + idx);
})}
</Box>
<Box paddingX={1}>
<Text dimColor>esc Close Scroll PgUp/PgDn g/G First/Last</Text>
</Box>
</Box>
);
}

View File

@@ -52,12 +52,6 @@ export default function HelpOverlay({
<Text>
<Text color="cyan">/clearhistory</Text> clear command history
</Text>
<Text>
<Text color="cyan">/bug</Text> file a bug report with session log
</Text>
<Text>
<Text color="cyan">/diff</Text> view working tree git diff
</Text>
<Text>
<Text color="cyan">/compact</Text> condense context into a summary
</Text>

View File

@@ -14,10 +14,7 @@ export default function HistoryOverlay({ items, onExit }: Props): JSX.Element {
const [mode, setMode] = useState<Mode>("commands");
const [cursor, setCursor] = useState(0);
const { commands, files } = useMemo(
() => formatHistoryForDisplay(items),
[items],
);
const { commands, files } = useMemo(() => buildLists(items), [items]);
const list = mode === "commands" ? commands : files;
@@ -98,7 +95,7 @@ export default function HistoryOverlay({ items, onExit }: Props): JSX.Element {
);
}
function formatHistoryForDisplay(items: Array<ResponseItem>): {
function buildLists(items: Array<ResponseItem>): {
commands: Array<string>;
files: Array<string>;
} {
@@ -106,9 +103,33 @@ function formatHistoryForDisplay(items: Array<ResponseItem>): {
const filesSet = new Set<string>();
for (const item of items) {
const userPrompt = processUserMessage(item);
if (userPrompt) {
commands.push(userPrompt);
if (
item.type === "message" &&
(item as unknown as { role?: string }).role === "user"
) {
// TODO: We're ignoring images/files here.
const parts =
(item as unknown as { content?: Array<unknown> }).content ?? [];
const texts: Array<string> = [];
if (Array.isArray(parts)) {
for (const part of parts) {
if (part && typeof part === "object" && "text" in part) {
const t = (part as unknown as { text?: string }).text;
if (typeof t === "string" && t.length > 0) {
texts.push(t);
}
}
}
}
if (texts.length > 0) {
const fullPrompt = texts.join(" ");
// Truncate very long prompts so the history view stays legible.
const truncated =
fullPrompt.length > 120 ? `${fullPrompt.slice(0, 117)}` : fullPrompt;
commands.push(`> ${truncated}`);
}
continue;
}
@@ -152,7 +173,31 @@ function formatHistoryForDisplay(items: Array<ResponseItem>): {
: undefined;
if (cmdArray && cmdArray.length > 0) {
commands.push(processCommandArray(cmdArray, filesSet));
commands.push(cmdArray.join(" "));
// Heuristic for file paths in command args
for (const part of cmdArray) {
if (!part.startsWith("-") && part.includes("/")) {
filesSet.add(part);
}
}
// Specialcase apply_patch so we can extract the list of modified files
if (cmdArray[0] === "apply_patch" || cmdArray.includes("apply_patch")) {
const patchTextMaybe = cmdArray.find((s) =>
s.includes("*** Begin Patch"),
);
if (typeof patchTextMaybe === "string") {
const lines = patchTextMaybe.split("\n");
for (const line of lines) {
const m = line.match(/^[-+]{3} [ab]\/(.+)$/);
if (m && m[1]) {
filesSet.add(m[1]);
}
}
}
}
continue; // We processed this as a command; no need to treat as generic tool call.
}
@@ -160,96 +205,33 @@ function formatHistoryForDisplay(items: Array<ResponseItem>): {
// short argument representation to give users an idea of what
// happened.
if (typeof toolName === "string" && toolName.length > 0) {
commands.push(processNonExecTool(toolName, argsJson, filesSet));
let summary = toolName;
if (argsJson && typeof argsJson === "object") {
// Extract a few common argument keys to make the summary more useful
// without being overly verbose.
const interestingKeys = [
"path",
"file",
"filepath",
"filename",
"pattern",
];
for (const key of interestingKeys) {
const val = (argsJson as Record<string, unknown>)[key];
if (typeof val === "string") {
summary += ` ${val}`;
if (val.includes("/")) {
filesSet.add(val);
}
break;
}
}
}
commands.push(summary);
}
}
return { commands, files: Array.from(filesSet) };
}
function processUserMessage(item: ResponseItem): string | null {
if (
item.type === "message" &&
(item as unknown as { role?: string }).role === "user"
) {
// TODO: We're ignoring images/files here.
const parts =
(item as unknown as { content?: Array<unknown> }).content ?? [];
const texts: Array<string> = [];
if (Array.isArray(parts)) {
for (const part of parts) {
if (part && typeof part === "object" && "text" in part) {
const t = (part as unknown as { text?: string }).text;
if (typeof t === "string" && t.length > 0) {
texts.push(t);
}
}
}
}
if (texts.length > 0) {
const fullPrompt = texts.join(" ");
// Truncate very long prompts so the history view stays legible.
return fullPrompt.length > 120
? `> ${fullPrompt.slice(0, 117)}`
: `> ${fullPrompt}`;
}
}
return null;
}
function processCommandArray(
cmdArray: Array<string>,
filesSet: Set<string>,
): string {
const cmd = cmdArray.join(" ");
// Heuristic for file paths in command args
for (const part of cmdArray) {
if (!part.startsWith("-") && part.includes("/")) {
filesSet.add(part);
}
}
// Specialcase apply_patch so we can extract the list of modified files
if (cmdArray[0] === "apply_patch" || cmdArray.includes("apply_patch")) {
const patchTextMaybe = cmdArray.find((s) => s.includes("*** Begin Patch"));
if (typeof patchTextMaybe === "string") {
const lines = patchTextMaybe.split("\n");
for (const line of lines) {
const m = line.match(/^[-+]{3} [ab]\/(.+)$/);
if (m && m[1]) {
filesSet.add(m[1]);
}
}
}
}
return cmd;
}
function processNonExecTool(
toolName: string,
argsJson: unknown,
filesSet: Set<string>,
): string {
let summary = toolName;
if (argsJson && typeof argsJson === "object") {
// Extract a few common argument keys to make the summary more useful
// without being overly verbose.
const interestingKeys = ["path", "file", "filepath", "filename", "pattern"];
for (const key of interestingKeys) {
const val = (argsJson as Record<string, unknown>)[key];
if (typeof val === "string") {
summary += ` ${val}`;
if (val.includes("/")) {
filesSet.add(val);
}
break;
}
}
}
return summary;
}

View File

@@ -1,7 +1,7 @@
import TypeaheadOverlay from "./typeahead-overlay.js";
import {
getAvailableModels,
RECOMMENDED_MODELS as _RECOMMENDED_MODELS,
RECOMMENDED_MODELS,
} from "../utils/model-utils.js";
import { Box, Text, useInput } from "ink";
import React, { useEffect, useState } from "react";
@@ -16,53 +16,39 @@ import React, { useEffect, useState } from "react";
*/
type Props = {
currentModel: string;
currentProvider?: string;
hasLastResponse: boolean;
providers?: Record<string, { name: string; baseURL: string; envKey: string }>;
onSelect: (model: string) => void;
onSelectProvider?: (provider: string) => void;
onExit: () => void;
};
export default function ModelOverlay({
currentModel,
providers = {},
currentProvider = "openai",
hasLastResponse,
onSelect,
onSelectProvider,
onExit,
}: Props): JSX.Element {
const [items, setItems] = useState<Array<{ label: string; value: string }>>(
[],
);
const [providerItems, _setProviderItems] = useState<
Array<{ label: string; value: string }>
>(Object.values(providers).map((p) => ({ label: p.name, value: p.name })));
const [mode, setMode] = useState<"model" | "provider">("model");
const [isLoading, setIsLoading] = useState<boolean>(true);
// This effect will run when the provider changes to update the model list
useEffect(() => {
setIsLoading(true);
(async () => {
try {
const models = await getAvailableModels(currentProvider);
// Convert the models to the format needed by TypeaheadOverlay
setItems(
models.map((m) => ({
label: m,
value: m,
})),
);
} catch (error) {
// Silently handle errors - remove console.error
// console.error("Error loading models:", error);
} finally {
setIsLoading(false);
}
const models = await getAvailableModels();
// Split the list into recommended and “other” models.
const recommended = RECOMMENDED_MODELS.filter((m) => models.includes(m));
const others = models.filter((m) => !recommended.includes(m));
const ordered = [...recommended, ...others.sort()];
setItems(
ordered.map((m) => ({
label: recommended.includes(m) ? `${m}` : m,
value: m,
})),
);
})();
}, [currentProvider]);
}, []);
// ---------------------------------------------------------------------------
// If the conversation already contains a response we cannot change the model
@@ -72,14 +58,10 @@ export default function ModelOverlay({
// available action is to dismiss the overlay (Esc or Enter).
// ---------------------------------------------------------------------------
// Register input handling for switching between model and provider selection
// Always register input handling so hooks are called consistently.
useInput((_input, key) => {
if (hasLastResponse && (key.escape || key.return)) {
onExit();
} else if (!hasLastResponse) {
if (key.tab) {
setMode(mode === "model" ? "provider" : "model");
}
}
});
@@ -109,47 +91,13 @@ export default function ModelOverlay({
);
}
if (mode === "provider") {
return (
<TypeaheadOverlay
title="Select provider"
description={
<Box flexDirection="column">
<Text>
Current provider:{" "}
<Text color="greenBright">{currentProvider}</Text>
</Text>
<Text dimColor>press tab to switch to model selection</Text>
</Box>
}
initialItems={providerItems}
currentValue={currentProvider}
onSelect={(provider) => {
if (onSelectProvider) {
onSelectProvider(provider);
// Immediately switch to model selection so user can pick a model for the new provider
setMode("model");
}
}}
onExit={onExit}
/>
);
}
return (
<TypeaheadOverlay
title="Select model"
title="Switch model"
description={
<Box flexDirection="column">
<Text>
Current model: <Text color="greenBright">{currentModel}</Text>
</Text>
<Text>
Current provider: <Text color="greenBright">{currentProvider}</Text>
</Text>
{isLoading && <Text color="yellow">Loading models...</Text>}
<Text dimColor>press tab to switch to provider selection</Text>
</Box>
<Text>
Current model: <Text color="greenBright">{currentModel}</Text>
</Text>
}
initialItems={items}
currentValue={currentModel}

View File

@@ -1,5 +1,5 @@
import Indicator, { type Props as IndicatorProps } from "./indicator.js";
import ItemComponent, { type Props as ItemProps } from "./item.js";
import Indicator, { type Props as IndicatorProps } from "./Indicator.js";
import ItemComponent, { type Props as ItemProps } from "./Item.js";
import isEqual from "fast-deep-equal";
import { Box, useInput } from "ink";
import React, {

View File

@@ -5,7 +5,7 @@ import type { FileOperation } from "../utils/singlepass/file_ops";
import Spinner from "./vendor/ink-spinner"; // Thirdparty / vendor components
import TextInput from "./vendor/ink-text-input";
import { OPENAI_TIMEOUT_MS, getBaseUrl, getApiKey } from "../utils/config";
import { OPENAI_TIMEOUT_MS, OPENAI_BASE_URL } from "../utils/config";
import {
generateDiffSummary,
generateEditSummary,
@@ -394,13 +394,12 @@ export function SinglePassApp({
});
const openai = new OpenAI({
apiKey: getApiKey(config.provider),
baseURL: getBaseUrl(config.provider),
apiKey: config.apiKey ?? "",
baseURL: OPENAI_BASE_URL || undefined,
timeout: OPENAI_TIMEOUT_MS,
});
const chatResp = await openai.beta.chat.completions.parse({
model: config.model,
...(config.flexMode ? { service_tier: "flex" } : {}),
messages: [
{
role: "user",

View File

@@ -44,11 +44,6 @@ export type TextInputProps = {
* Function to call when `Enter` is pressed, where first argument is a value of the input.
*/
readonly onSubmit?: (value: string) => void;
/**
* Explicitly set the cursor position to the end of the text
*/
readonly cursorToEnd?: boolean;
};
function findPrevWordJump(prompt: string, cursorOffset: number) {
@@ -95,22 +90,12 @@ function TextInput({
showCursor = true,
onChange,
onSubmit,
cursorToEnd = false,
}: TextInputProps) {
const [state, setState] = useState({
cursorOffset: (originalValue || "").length,
cursorWidth: 0,
});
useEffect(() => {
if (cursorToEnd) {
setState((prev) => ({
...prev,
cursorOffset: (originalValue || "").length,
}));
}
}, [cursorToEnd, originalValue, focus]);
const { cursorOffset, cursorWidth } = state;
useEffect(() => {
@@ -168,78 +153,6 @@ function TextInput({
useInput(
(input, key) => {
// ────────────────────────────────────────────────────────────────
// Support Shift+Enter / Ctrl+Enter from terminals that have
// modifyOtherKeys enabled. Such terminals encode the keycombo in a
// CSI sequence rather than sending a bare "\r"/"\n". Ink passes the
// sequence through as raw text (without the initial ESC), so we need to
// detect and translate it before the generic character handler below
// treats it as literal input (e.g. "[27;2;13~"). We support both the
// modern *mode 2* (CSIu, ending in "u") and the legacy *mode 1*
// variant (ending in "~").
//
// - Shift+Enter → insert newline (same behaviour as Option+Enter)
// - Ctrl+Enter → submit the input (same as plain Enter)
//
// References: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Modify-Other-Keys
// ────────────────────────────────────────────────────────────────
function handleEncodedEnterSequence(raw: string): boolean {
// CSIu (modifyOtherKeys=2) → "[13;<mod>u"
let m = raw.match(/^\[([0-9]+);([0-9]+)u$/);
if (m && m[1] === "13") {
const mod = Number(m[2]);
const hasCtrl = Math.floor(mod / 4) % 2 === 1;
if (hasCtrl) {
if (onSubmit) {
onSubmit(originalValue);
}
} else {
const newValue =
originalValue.slice(0, cursorOffset) +
"\n" +
originalValue.slice(cursorOffset);
setState({
cursorOffset: cursorOffset + 1,
cursorWidth: 0,
});
onChange(newValue);
}
return true; // handled
}
// CSI~ (modifyOtherKeys=1) → "[27;<mod>;13~"
m = raw.match(/^\[27;([0-9]+);13~$/);
if (m) {
const mod = Number(m[1]);
const hasCtrl = Math.floor(mod / 4) % 2 === 1;
if (hasCtrl) {
if (onSubmit) {
onSubmit(originalValue);
}
} else {
const newValue =
originalValue.slice(0, cursorOffset) +
"\n" +
originalValue.slice(cursorOffset);
setState({
cursorOffset: cursorOffset + 1,
cursorWidth: 0,
});
onChange(newValue);
}
return true; // handled
}
return false; // not an encoded Enter sequence
}
if (handleEncodedEnterSequence(input)) {
return;
}
if (
key.upArrow ||
key.downArrow ||

View File

@@ -1,3 +1,4 @@
// use-confirmation.ts
import type { ReviewDecision } from "../utils/agent/review";
import type React from "react";

View File

@@ -1,24 +0,0 @@
// Ambient module declarations for optional/runtimeonly dependencies so that
// `tsc --noEmit` succeeds without installing their full type definitions.
declare module "package-manager-detector" {
export type AgentName = "npm" | "pnpm" | "yarn" | "bun" | "deno";
/** Detects the package manager based on environment variables. */
export function getUserAgent(): AgentName | null | undefined;
}
declare module "fast-npm-meta" {
export interface LatestVersionMeta {
version: string;
}
export function getLatestVersion(
pkgName: string,
opts?: Record<string, unknown>,
): Promise<LatestVersionMeta | { error: unknown }>;
}
declare module "semver" {
export function gt(v1: string, v2: string): boolean;
}

View File

@@ -423,7 +423,7 @@ export default class TextBuffer {
/** Delete the word to the *left* of the caret, mirroring common
* Ctrl/Alt+Backspace behaviour in editors & terminals. Both the adjacent
* whitespace *and* the word characters immediately preceding the caret are
* removed. If the caret is already at column0 this becomes a no-op. */
* removed. If the caret is already at column0 this becomes a noop. */
deleteWordLeft(): void {
dbg("deleteWordLeft", { beforeCursor: this.getCursor() });
@@ -710,7 +710,7 @@ export default class TextBuffer {
}
endSelection(): void {
// no-op for now, kept for API symmetry
// noop for now, kept for API symmetry
// we rely on anchor + current cursor to compute selection
}

View File

@@ -1,20 +1,18 @@
import type { ReviewDecision } from "./review.js";
import type { ApplyPatchCommand, ApprovalPolicy } from "../../approvals.js";
import type { AppConfig } from "../config.js";
import type { ResponseEvent } from "../responses.js";
import type { UsageBreakdown } from "../estimate-cost.js";
import type {
ResponseFunctionToolCall,
ResponseInputItem,
ResponseItem,
ResponseCreateParams,
FunctionTool,
} from "openai/resources/responses/responses.mjs";
import type { Reasoning } from "openai/resources.mjs";
import { OPENAI_TIMEOUT_MS, getApiKey, getBaseUrl } from "../config.js";
import { log } from "../logger/log.js";
import { log, isLoggingEnabled } from "./log.js";
import { OPENAI_BASE_URL, OPENAI_TIMEOUT_MS } from "../config.js";
import { parseToolCallArguments } from "../parsers.js";
import { responsesCreateViaChatCompletions } from "../responses.js";
import { ensureSessionTracker } from "../session-cost.js";
import {
ORIGIN,
CLI_VERSION,
@@ -43,18 +41,9 @@ const alreadyProcessedResponses = new Set();
type AgentLoopParams = {
model: string;
provider?: string;
config?: AppConfig;
instructions?: string;
approvalPolicy: ApprovalPolicy;
/**
* Whether the model responses should be stored on the server side (allows
* using `previous_response_id` to provide conversational context). Defaults
* to `true` to preserve the current behaviour. When set to `false` the agent
* will instead send the *full* conversation context as the `input` payload
* on every request and omit the `previous_response_id` parameter.
*/
disableResponseStorage?: boolean;
onItem: (item: ResponseItem) => void;
onLoading: (loading: boolean) => void;
@@ -69,39 +58,19 @@ type AgentLoopParams = {
onLastResponseId: (lastResponseId: string) => void;
};
const shellTool: FunctionTool = {
type: "function",
name: "shell",
description: "Runs a shell command, and returns its output.",
strict: false,
parameters: {
type: "object",
properties: {
command: { type: "array", items: { type: "string" } },
workdir: {
type: "string",
description: "The working directory for the command.",
},
timeout: {
type: "number",
description:
"The maximum time to wait for the command to complete in milliseconds.",
},
},
required: ["command"],
additionalProperties: false,
},
type Usage = {
total_tokens?: number;
input_tokens?: number;
output_tokens?: number;
};
type MaybeUsageEvent = { response?: { usage?: Usage } };
export class AgentLoop {
private model: string;
private provider: string;
private instructions?: string;
private approvalPolicy: ApprovalPolicy;
private config: AppConfig;
private additionalWritableRoots: ReadonlyArray<string>;
/** Whether we ask the API to persist conversation state on the server */
private readonly disableResponseStorage: boolean;
// Using `InstanceType<typeof OpenAI>` sidesteps typing issues with the OpenAI package under
// the TS 5+ `moduleResolution=bundler` setup. OpenAI client instance. We keep the concrete
@@ -132,13 +101,6 @@ export class AgentLoop {
private execAbortController: AbortController | null = null;
/** Set to true when `cancel()` is called so `run()` can exit early. */
private canceled = false;
/**
* Local conversation transcript used when `disableResponseStorage === true`. Holds
* all nonsystem items exchanged so far so we can provide full context on
* every request.
*/
private transcript: Array<ResponseInputItem> = [];
/** Function calls that were emitted by the model but never answered because
* the user cancelled the run. We keep the `call_id`s around so the *next*
* request can send a dummy `function_call_output` that satisfies the
@@ -163,13 +125,15 @@ export class AgentLoop {
// Reset the current stream to allow new requests
this.currentStream = null;
log(
`AgentLoop.cancel() invoked currentStream=${Boolean(
this.currentStream,
)} execAbortController=${Boolean(this.execAbortController)} generation=${
this.generation
}`,
);
if (isLoggingEnabled()) {
log(
`AgentLoop.cancel() invoked currentStream=${Boolean(
this.currentStream,
)} execAbortController=${Boolean(
this.execAbortController,
)} generation=${this.generation}`,
);
}
(
this.currentStream as { controller?: { abort?: () => void } } | null
)?.controller?.abort?.();
@@ -181,7 +145,9 @@ export class AgentLoop {
// Create a new abort controller for future tool calls
this.execAbortController = new AbortController();
log("AgentLoop.cancel(): execAbortController.abort() called");
if (isLoggingEnabled()) {
log("AgentLoop.cancel(): execAbortController.abort() called");
}
// NOTE: We intentionally do *not* clear `lastResponseId` here. If the
// stream produced a `function_call` before the user cancelled, OpenAI now
@@ -217,7 +183,9 @@ export class AgentLoop {
// this.onItem(cancelNotice);
this.generation += 1;
log(`AgentLoop.cancel(): generation bumped to ${this.generation}`);
if (isLoggingEnabled()) {
log(`AgentLoop.cancel(): generation bumped to ${this.generation}`);
}
}
/**
@@ -245,10 +213,8 @@ export class AgentLoop {
// private cumulativeThinkingMs = 0;
constructor({
model,
provider = "openai",
instructions,
approvalPolicy,
disableResponseStorage,
// `config` used to be required. Some unittests (and potentially other
// callers) instantiate `AgentLoop` without passing it, so we make it
// optional and fall back to sensible defaults. This keeps the public
@@ -263,7 +229,6 @@ export class AgentLoop {
additionalWritableRoots,
}: AgentLoopParams & { config?: AppConfig }) {
this.model = model;
this.provider = provider;
this.instructions = instructions;
this.approvalPolicy = approvalPolicy;
@@ -279,18 +244,25 @@ export class AgentLoop {
instructions: instructions ?? "",
} as AppConfig);
this.additionalWritableRoots = additionalWritableRoots;
this.onItem = onItem;
// Capture usage for costtracking before delegating to the callersupplied
// callback. Wrapping here avoids repeating the bookkeeping logic across
// every UI surface.
this.onItem = (item: ResponseItem) => {
try {
ensureSessionTracker(this.model).addItems([item]);
} catch {
/* besteffort never block uservisible updates */
}
onItem(item);
};
this.onLoading = onLoading;
this.getCommandConfirmation = getCommandConfirmation;
this.onLastResponseId = onLastResponseId;
this.disableResponseStorage = disableResponseStorage ?? false;
this.sessionId = getSessionId() || randomUUID().replaceAll("-", "");
// Configure OpenAI client with optional timeout (ms) from environment
const timeoutMs = OPENAI_TIMEOUT_MS;
const apiKey = getApiKey(this.provider);
const baseURL = getBaseUrl(this.provider);
const apiKey = this.config.apiKey ?? process.env["OPENAI_API_KEY"] ?? "";
this.oai = new OpenAI({
// The OpenAI JS SDK only requires `apiKey` when making requests against
// the official API. When running unittests we stub out all network
@@ -299,7 +271,7 @@ export class AgentLoop {
// errors inside the SDK (it validates that `apiKey` is a nonempty
// string when the field is present).
...(apiKey ? { apiKey } : {}),
baseURL,
baseURL: OPENAI_BASE_URL,
defaultHeaders: {
originator: ORIGIN,
version: CLI_VERSION,
@@ -363,11 +335,13 @@ export class AgentLoop {
const callId: string = (item as any).call_id ?? (item as any).id;
const args = parseToolCallArguments(rawArguments ?? "{}");
log(
`handleFunctionCall(): name=${
name ?? "undefined"
} callId=${callId} args=${rawArguments}`,
);
if (isLoggingEnabled()) {
log(
`handleFunctionCall(): name=${
name ?? "undefined"
} callId=${callId} args=${rawArguments}`,
);
}
if (args == null) {
const outputItem: ResponseInputItem.FunctionCallOutput = {
@@ -453,9 +427,11 @@ export class AgentLoop {
// Create a fresh AbortController for this run so that tool calls from a
// previous run do not accidentally get signalled.
this.execAbortController = new AbortController();
log(
`AgentLoop.run(): new execAbortController created (${this.execAbortController.signal}) for generation ${this.generation}`,
);
if (isLoggingEnabled()) {
log(
`AgentLoop.run(): new execAbortController created (${this.execAbortController.signal}) for generation ${this.generation}`,
);
}
// NOTE: We no longer (re)attach an `abort` listener to `hardAbort` here.
// A single listener that forwards the `abort` to the current
// `execAbortController` is installed once in the constructor. Readding a
@@ -463,17 +439,7 @@ export class AgentLoop {
// accumulate listeners which in turn triggered Node's
// `MaxListenersExceededWarning` after ten invocations.
// Track the response ID from the last *stored* response so we can use
// `previous_response_id` when `disableResponseStorage` is enabled. When storage
// is disabled we deliberately ignore the callersupplied value because
// the backend will not retain any state that could be referenced.
// If the backend stores conversation state (`disableResponseStorage === false`) we
// forward the callersupplied `previousResponseId` so that the model sees the
// full context. When storage is disabled we *must not* send any ID because the
// server no longer retains the referenced response.
let lastResponseId: string = this.disableResponseStorage
? ""
: previousResponseId;
let lastResponseId: string = previousResponseId;
// If there are unresolved function calls from a previously cancelled run
// we have to emit dummy tool outputs so that the API no longer expects
@@ -495,55 +461,7 @@ export class AgentLoop {
this.pendingAborts.clear();
}
// Build the input list for this turn. When responses are stored on the
// server we can simply send the *delta* (the new user input as well as
// any pending abort outputs) and rely on `previous_response_id` for
// context. When storage is disabled the server has no memory of the
// conversation, so we must include the *entire* transcript (minus system
// messages) on every call.
let turnInput: Array<ResponseInputItem> = [];
// Keeps track of how many items in `turnInput` stem from the existing
// transcript so we can avoid reemitting them to the UI. Only used when
// `disableResponseStorage === true`.
let transcriptPrefixLen = 0;
const stripInternalFields = (
item: ResponseInputItem,
): ResponseInputItem => {
// Clone shallowly and remove fields that are not part of the public
// schema expected by the OpenAI Responses API.
// We shallowclone the item so that subsequent mutations (deleting
// internal fields) do not affect the original object which may still
// be referenced elsewhere (e.g. UI components).
const clean = { ...item } as Record<string, unknown>;
delete clean["duration_ms"];
// Remove OpenAI-assigned identifiers and transient status so the
// backend does not reject items that were never persisted because we
// use `store: false`.
delete clean["id"];
delete clean["status"];
return clean as unknown as ResponseInputItem;
};
if (this.disableResponseStorage) {
// Remember where the existing transcript ends everything after this
// index in the upcoming `turnInput` list will be *new* for this turn
// and therefore needs to be surfaced to the UI.
transcriptPrefixLen = this.transcript.length;
// Ensure the transcript is uptodate with the latest user input so
// that subsequent iterations see a complete history.
// `turnInput` is still empty at this point (it will be filled later).
// We need to look at the *input* items the user just supplied.
this.transcript.push(...filterToApiMessages(input));
turnInput = [...this.transcript, ...abortOutputs].map(
stripInternalFields,
);
} else {
turnInput = [...abortOutputs, ...input].map(stripInternalFields);
}
let turnInput = [...abortOutputs, ...input];
this.onLoading(true);
@@ -574,51 +492,6 @@ export class AgentLoop {
this.onItem(item);
// Mark as delivered so flush won't re-emit it
staged[idx] = undefined;
// When we operate without serverside storage we keep our own
// transcript so we can provide full context on subsequent calls.
if (this.disableResponseStorage) {
// Exclude system messages from transcript as they do not form
// part of the assistant/user dialogue that the model needs.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const role = (item as any).role;
if (role !== "system") {
// Clone the item to avoid mutating the object that is also
// rendered in the UI. We need to strip auxiliary metadata
// such as `duration_ms` which is not part of the Responses
// API schema and therefore causes a 400 error when included
// in subsequent requests whose context is sent verbatim.
// Skip items that we have already inserted earlier or that the
// model does not need to see again in the next turn.
// • function_call superseded by the forthcoming
// function_call_output.
// • reasoning internal only, never sent back.
// • user messages we added these to the transcript when
// building the first turnInput; stageItem would add a
// duplicate.
if (
(item as ResponseInputItem).type === "function_call" ||
(item as ResponseInputItem).type === "reasoning" ||
((item as ResponseInputItem).type === "message" &&
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(item as any).role === "user")
) {
return;
}
const clone: ResponseInputItem = {
...(item as unknown as ResponseInputItem),
} as ResponseInputItem;
// The `duration_ms` field is only added to reasoning items to
// show elapsed time in the UI. It must not be forwarded back
// to the server.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete (clone as any).duration_ms;
this.transcript.push(clone);
}
}
}
}, 10);
};
@@ -629,22 +502,7 @@ export class AgentLoop {
return;
}
// send request to openAI
// Only surface the *new* input items to the UI replaying the entire
// transcript would duplicate messages that have already been shown in
// earlier turns.
// `turnInput` holds the *new* items that will be sent to the API in
// this iteration. Surface exactly these to the UI so that we do not
// reemit messages from previous turns (which would duplicate user
// prompts) and so that freshly generated `function_call_output`s are
// shown immediately.
// Figure out what subset of `turnInput` constitutes *new* information
// for the UI so that we dont spam the interface with repeats of the
// entire transcript on every iteration when response storage is
// disabled.
const deltaInput = this.disableResponseStorage
? turnInput.slice(transcriptPrefixLen)
: [...turnInput];
for (const item of deltaInput) {
for (const item of turnInput) {
stageItem(item as ResponseItem);
}
// Send request to OpenAI with retry on timeout
@@ -658,48 +516,52 @@ export class AgentLoop {
if (this.model.startsWith("o")) {
reasoning = { effort: "high" };
if (this.model === "o3" || this.model === "o4-mini") {
// @ts-expect-error waiting for API type update
reasoning.summary = "auto";
}
}
const mergedInstructions = [prefix, this.instructions]
.filter(Boolean)
.join("\n");
const responseCall =
!this.config.provider ||
this.config.provider?.toLowerCase() === "openai"
? (params: ResponseCreateParams) =>
this.oai.responses.create(params)
: (params: ResponseCreateParams) =>
responsesCreateViaChatCompletions(
this.oai,
params as ResponseCreateParams & { stream: true },
);
log(
`instructions (length ${mergedInstructions.length}): ${mergedInstructions}`,
);
if (isLoggingEnabled()) {
log(
`instructions (length ${mergedInstructions.length}): ${mergedInstructions}`,
);
}
// eslint-disable-next-line no-await-in-loop
stream = await responseCall({
stream = await this.oai.responses.create({
model: this.model,
instructions: mergedInstructions,
previous_response_id: lastResponseId || undefined,
input: turnInput,
stream: true,
parallel_tool_calls: false,
reasoning,
...(this.config.flexMode ? { service_tier: "flex" } : {}),
...(this.disableResponseStorage
? { store: false }
: {
store: true,
previous_response_id: lastResponseId || undefined,
}),
tools: [shellTool],
// Explicitly tell the model it is allowed to pick whatever
// tool it deems appropriate. Omitting this sometimes leads to
// the model ignoring the available tools and responding with
// plain text instead (resulting in a missing toolcall).
tool_choice: "auto",
tools: [
{
type: "function",
name: "shell",
description: "Runs a shell command, and returns its output.",
strict: false,
parameters: {
type: "object",
properties: {
command: { type: "array", items: { type: "string" } },
workdir: {
type: "string",
description: "The working directory for the command.",
},
timeout: {
type: "number",
description:
"The maximum time to wait for the command to complete in milliseconds.",
},
},
required: ["command"],
additionalProperties: false,
},
},
],
});
break;
} catch (error) {
@@ -888,240 +750,86 @@ export class AgentLoop {
return;
}
const MAX_STREAM_RETRIES = 5;
let streamRetryAttempt = 0;
// eslint-disable-next-line no-constant-condition
while (true) {
try {
// eslint-disable-next-line no-await-in-loop
for await (const event of stream as AsyncIterable<ResponseEvent>) {
try {
// eslint-disable-next-line no-await-in-loop
for await (const event of stream) {
if (isLoggingEnabled()) {
log(`AgentLoop.run(): response event ${event.type}`);
}
// process and surface each item (no-op until we can depend on streaming events)
if (event.type === "response.output_item.done") {
const item = event.item;
// 1) if it's a reasoning item, annotate it
type ReasoningItem = { type?: string; duration_ms?: number };
const maybeReasoning = item as ReasoningItem;
if (maybeReasoning.type === "reasoning") {
maybeReasoning.duration_ms = Date.now() - thinkingStart;
// process and surface each item (noop until we can depend on streaming events)
if (event.type === "response.output_item.done") {
const item = event.item;
// 1) if it's a reasoning item, annotate it
type ReasoningItem = { type?: string; duration_ms?: number };
const maybeReasoning = item as ReasoningItem;
if (maybeReasoning.type === "reasoning") {
maybeReasoning.duration_ms = Date.now() - thinkingStart;
}
if (item.type === "function_call") {
// Track outstanding tool call so we can abort later if needed.
// The item comes from the streaming response, therefore it has
// either `id` (chat) or `call_id` (responses) we normalise
// by reading both.
const callId =
(item as { call_id?: string; id?: string }).call_id ??
(item as { id?: string }).id;
if (callId) {
this.pendingAborts.add(callId);
}
if (item.type === "function_call") {
// Track outstanding tool call so we can abort later if needed.
// The item comes from the streaming response, therefore it has
// either `id` (chat) or `call_id` (responses) we normalise
// by reading both.
const callId =
(item as { call_id?: string; id?: string }).call_id ??
(item as { id?: string }).id;
if (callId) {
this.pendingAborts.add(callId);
}
} else {
} else {
stageItem(item as ResponseItem);
}
}
if (event.type === "response.completed") {
if (thisGeneration === this.generation && !this.canceled) {
for (const item of event.response.output) {
stageItem(item as ResponseItem);
}
}
if (event.response.status === "completed") {
// TODO: remove this once we can depend on streaming events
const newTurnInput = await this.processEventsWithoutStreaming(
event.response.output,
stageItem,
);
turnInput = newTurnInput;
}
lastResponseId = event.response.id;
this.onLastResponseId(event.response.id);
if (event.type === "response.completed") {
if (thisGeneration === this.generation && !this.canceled) {
for (const item of event.response.output) {
stageItem(item as ResponseItem);
}
}
if (
event.response.status === "completed" ||
(event.response.status as unknown as string) ===
"requires_action"
) {
// TODO: remove this once we can depend on streaming events
const newTurnInput = await this.processEventsWithoutStreaming(
event.response.output,
stageItem,
// Capture exact token usage for cost tracking when provided by
// the API. `responses.completed` events include a `usage` field
// with {input_tokens, output_tokens, total_tokens}. We record
// the total (or fallback to summing the parts if needed).
try {
const usage = (event as MaybeUsageEvent).response?.usage;
if (usage && typeof usage === "object") {
ensureSessionTracker(this.model).addUsage(
usage as unknown as UsageBreakdown,
);
// When we do not use serverside storage we maintain our
// own transcript so that *future* turns still contain full
// conversational context. However, whether we advance to
// another loop iteration should depend solely on the
// presence of *new* input items (i.e. items that were not
// part of the previous request). Resending the transcript
// by itself would create an infinite request loop because
// `turnInput.length` would never reach zero.
if (this.disableResponseStorage) {
// 1) Append the freshly emitted output to our local
// transcript (minus nonmessage items the model does
// not need to see again).
const cleaned = filterToApiMessages(
event.response.output.map(stripInternalFields),
);
this.transcript.push(...cleaned);
// 2) Determine the *delta* (newTurnInput) that must be
// sent in the next iteration. If there is none we can
// safely terminate the loop the transcript alone
// does not constitute new information for the
// assistant to act upon.
const delta = filterToApiMessages(
newTurnInput.map(stripInternalFields),
);
if (delta.length === 0) {
// No new input => end conversation.
turnInput = [];
} else {
// Resend full transcript *plus* the new delta so the
// stateless backend receives complete context.
turnInput = [...this.transcript, ...delta];
// The prefix ends at the current transcript length
// everything after this index is new for the next
// iteration.
transcriptPrefixLen = this.transcript.length;
}
} else {
turnInput = newTurnInput;
}
}
lastResponseId = event.response.id;
this.onLastResponseId(event.response.id);
} catch {
/* besteffort only */
}
}
// Stream finished successfully leave the retry loop.
break;
} catch (err: unknown) {
const isRateLimitError = (e: unknown): boolean => {
if (!e || typeof e !== "object") {
return false;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const ex: any = e;
return (
ex.status === 429 ||
ex.code === "rate_limit_exceeded" ||
ex.type === "rate_limit_exceeded"
);
};
if (
isRateLimitError(err) &&
streamRetryAttempt < MAX_STREAM_RETRIES
) {
streamRetryAttempt += 1;
const waitMs =
RATE_LIMIT_RETRY_WAIT_MS * 2 ** (streamRetryAttempt - 1);
log(
`OpenAI stream ratelimited retry ${streamRetryAttempt}/${MAX_STREAM_RETRIES} in ${waitMs} ms`,
);
// Give the server a breather before retrying.
// eslint-disable-next-line no-await-in-loop
await new Promise((res) => setTimeout(res, waitMs));
// Recreate the stream with the *same* parameters.
let reasoning: Reasoning | undefined;
if (this.model.startsWith("o")) {
reasoning = { effort: "high" };
if (this.model === "o3" || this.model === "o4-mini") {
reasoning.summary = "auto";
}
}
const mergedInstructions = [prefix, this.instructions]
.filter(Boolean)
.join("\n");
const responseCall =
!this.config.provider ||
this.config.provider?.toLowerCase() === "openai"
? (params: ResponseCreateParams) =>
this.oai.responses.create(params)
: (params: ResponseCreateParams) =>
responsesCreateViaChatCompletions(
this.oai,
params as ResponseCreateParams & { stream: true },
);
log(
"agentLoop.run(): responseCall(1): turnInput: " +
JSON.stringify(turnInput),
);
// eslint-disable-next-line no-await-in-loop
stream = await responseCall({
model: this.model,
instructions: mergedInstructions,
input: turnInput,
stream: true,
parallel_tool_calls: false,
reasoning,
...(this.config.flexMode ? { service_tier: "flex" } : {}),
...(this.disableResponseStorage
? { store: false }
: {
store: true,
previous_response_id: lastResponseId || undefined,
}),
tools: [shellTool],
tool_choice: "auto",
});
this.currentStream = stream;
// Continue to outer while to consume new stream.
continue;
}
// Gracefully handle an abort triggered via `cancel()` so that the
// consumer does not see an unhandled exception.
if (err instanceof Error && err.name === "AbortError") {
if (!this.canceled) {
// It was aborted for some other reason; surface the error.
throw err;
}
this.onLoading(false);
return;
}
// Suppress internal stack on JSON parse failures
if (err instanceof SyntaxError) {
this.onItem({
id: `error-${Date.now()}`,
type: "message",
role: "system",
content: [
{
type: "input_text",
text: "⚠️ Failed to parse streaming response (invalid JSON). Please `/clear` to reset.",
},
],
});
this.onLoading(false);
return;
}
// Handle OpenAI API quota errors
if (
err instanceof Error &&
(err as { code?: string }).code === "insufficient_quota"
) {
this.onItem({
id: `error-${Date.now()}`,
type: "message",
role: "system",
content: [
{
type: "input_text",
text: "⚠️ Insufficient quota. Please check your billing details and retry.",
},
],
});
this.onLoading(false);
return;
}
throw err;
} finally {
this.currentStream = null;
}
} // end while retry loop
} catch (err: unknown) {
// Gracefully handle an abort triggered via `cancel()` so that the
// consumer does not see an unhandled exception.
if (err instanceof Error && err.name === "AbortError") {
if (!this.canceled) {
// It was aborted for some other reason; surface the error.
throw err;
}
this.onLoading(false);
return;
}
throw err;
} finally {
this.currentStream = null;
}
log(
`Turn inputs (${turnInput.length}) - ${turnInput
@@ -1221,7 +929,7 @@ export class AgentLoop {
],
});
} catch {
/* no-op emitting the error message is besteffort */
/* noop emitting the error message is besteffort */
}
this.onLoading(false);
return;
@@ -1282,14 +990,6 @@ export class AgentLoop {
return true;
}
// Explicit check for OpenAI "server_error" types which are surfaced
// when the backend encounters an unexpected exception. The SDK often
// omits the HTTP status in this case (leaving it undefined) so we
// must inspect the structured error fields instead.
if (e.type === "server_error" || e.code === "server_error") {
return true;
}
if (typeof e.status === "number" && e.status >= 500) {
return true;
}
@@ -1472,21 +1172,7 @@ You MUST adhere to the following criteria when executing the task:
- For smaller tasks, describe in brief bullet points
- For more complex tasks, include brief high-level description, use bullet points, and include details that would be relevant to a code reviewer.
- If completing the user's task DOES NOT require writing or modifying files (e.g., the user asks a question about the code base):
- Respond in a friendly tone as a remote teammate, who is knowledgeable, capable and eager to help with coding.
- Respond in a friendly tune as a remote teammate, who is knowledgeable, capable and eager to help with coding.
- When your task involves writing or modifying files:
- Do NOT tell the user to "save the file" or "copy the code into a file" if you already created or modified the file using \`apply_patch\`. Instead, reference the file as already saved.
- Do NOT show the full contents of large files you have already written, unless the user explicitly asks for them.`;
function filterToApiMessages(
items: Array<ResponseInputItem>,
): Array<ResponseInputItem> {
return items.filter((it) => {
if (it.type === "message" && it.role === "system") {
return false;
}
if (it.type === "reasoning") {
return false;
}
return true;
});
}

View File

@@ -1,6 +1,5 @@
import type { ExecInput, ExecResult } from "./sandbox/interface.js";
import type { SpawnOptions } from "child_process";
import type { ParseEntry } from "shell-quote";
import { process_patch } from "./apply-patch.js";
import { SandboxType } from "./sandbox/interface.js";
@@ -9,25 +8,9 @@ import { exec as rawExec } from "./sandbox/raw-exec.js";
import { formatCommandForDisplay } from "../../format-command.js";
import fs from "fs";
import os from "os";
import path from "path";
import { parse } from "shell-quote";
import { resolvePathAgainstWorkdir } from "src/approvals.js";
const DEFAULT_TIMEOUT_MS = 10_000; // 10 seconds
function requiresShell(cmd: Array<string>): boolean {
// If the command is a single string that contains shell operators,
// it needs to be run with shell: true
if (cmd.length === 1 && cmd[0] !== undefined) {
const tokens = parse(cmd[0]) as Array<ParseEntry>;
return tokens.some((token) => typeof token === "object" && "op" in token);
}
// If the command is split into multiple arguments, we don't need shell: true
// even if one of the arguments is a shell operator like '|'
return false;
}
/**
* This function should never return a rejected promise: errors should be
* mapped to a non-zero exit code and the error message should be in stderr.
@@ -50,7 +33,6 @@ export function exec(
const opts: SpawnOptions = {
timeout: timeoutInMillis || DEFAULT_TIMEOUT_MS,
...(requiresShell(cmd) ? { shell: true } : {}),
...(workdir ? { cwd: workdir } : {}),
};
// Merge default writable roots with any user-specified ones.
@@ -62,32 +44,16 @@ export function exec(
return execForSandbox(cmd, opts, writableRoots, abortSignal);
}
export function execApplyPatch(
patchText: string,
workdir: string | undefined = undefined,
): ExecResult {
export function execApplyPatch(patchText: string): ExecResult {
// This is a temporary measure to understand what are the common base commands
// until we start persisting and uploading rollouts
try {
const result = process_patch(
patchText,
(p) => fs.readFileSync(resolvePathAgainstWorkdir(p, workdir), "utf8"),
(p, c) => {
const resolvedPath = resolvePathAgainstWorkdir(p, workdir);
// Ensure the parent directory exists before writing the file. This
// mirrors the behaviour of the standalone apply_patch CLI (see
// write_file() in apply-patch.ts) and prevents errors when adding a
// new file in a notyetcreated subdirectory.
const dir = path.dirname(resolvedPath);
if (dir !== ".") {
fs.mkdirSync(dir, { recursive: true });
}
fs.writeFileSync(resolvedPath, c, "utf8");
},
(p) => fs.unlinkSync(resolvePathAgainstWorkdir(p, workdir)),
(p) => fs.readFileSync(p, "utf8"),
(p, c) => fs.writeFileSync(p, c, "utf8"),
(p) => fs.unlinkSync(p),
);
return {
stdout: result,

View File

@@ -5,12 +5,12 @@ import type { ApplyPatchCommand, ApprovalPolicy } from "../../approvals.js";
import type { ResponseInputItem } from "openai/resources/responses/responses.mjs";
import { exec, execApplyPatch } from "./exec.js";
import { isLoggingEnabled, log } from "./log.js";
import { ReviewDecision } from "./review.js";
import { FullAutoErrorMode } from "../auto-approval-mode.js";
import { SandboxType } from "./sandbox/interface.js";
import { canAutoApprove } from "../../approvals.js";
import { formatCommandForDisplay } from "../../format-command.js";
import { isLoggingEnabled, log } from "../logger/log.js";
import { access } from "fs/promises";
// ---------------------------------------------------------------------------
@@ -81,7 +81,7 @@ export async function handleExecCommand(
) => Promise<CommandConfirmation>,
abortSignal?: AbortSignal,
): Promise<HandleExecCommandResult> {
const { cmd: command, workdir } = args;
const { cmd: command } = args;
const key = deriveCommandKey(command);
@@ -103,7 +103,7 @@ export async function handleExecCommand(
// working directory so that edits are constrained to the project root. If
// the caller wishes to broaden or restrict the set it can be made
// configurable in the future.
const safety = canAutoApprove(command, workdir, policy, [process.cwd()]);
const safety = canAutoApprove(command, policy, [process.cwd()]);
let runInSandbox: boolean;
switch (safety.type) {
@@ -144,7 +144,7 @@ export async function handleExecCommand(
abortSignal,
);
// If the operation was aborted in the meantime, propagate the cancellation
// upward by returning an empty (no-op) result so that the agent loop will
// upward by returning an empty (noop) result so that the agent loop will
// exit cleanly without emitting spurious output.
if (abortSignal?.aborted) {
return {
@@ -223,22 +223,23 @@ async function execCommand(
workdir = process.cwd();
}
}
if (applyPatchCommand != null) {
log("EXEC running apply_patch command");
} else if (isLoggingEnabled()) {
const { cmd, timeoutInMillis } = execInput;
// Seconds are a bit easier to read in log messages and most timeouts
// are specified as multiples of 1000, anyway.
const timeout =
timeoutInMillis != null
? Math.round(timeoutInMillis / 1000).toString()
: "undefined";
log(
`EXEC running \`${formatCommandForDisplay(
cmd,
)}\` in workdir=${workdir} with timeout=${timeout}s`,
);
if (isLoggingEnabled()) {
if (applyPatchCommand != null) {
log("EXEC running apply_patch command");
} else {
const { cmd, timeoutInMillis } = execInput;
// Seconds are a bit easier to read in log messages and most timeouts
// are specified as multiples of 1000, anyway.
const timeout =
timeoutInMillis != null
? Math.round(timeoutInMillis / 1000).toString()
: "undefined";
log(
`EXEC running \`${formatCommandForDisplay(
cmd,
)}\` in workdir=${workdir} with timeout=${timeout}s`,
);
}
}
// Note execApplyPatch() and exec() are coded defensively and should not
@@ -247,7 +248,7 @@ async function execCommand(
const start = Date.now();
const execResult =
applyPatchCommand != null
? execApplyPatch(applyPatchCommand.patch, workdir)
? execApplyPatch(applyPatchCommand.patch)
: await exec(
{ ...execInput, additionalWritableRoots },
await getSandbox(runInSandbox),

View File

@@ -124,14 +124,6 @@ export function log(message: string): void {
(logger ?? initLogger()).log(message);
}
/**
* USE SPARINGLY! This function should only be used to guard a call to log() if
* the log message is large and you want to avoid constructing it if logging is
* disabled.
*
* `log()` is already a no-op if DEBUG is not set, so an extra
* `isLoggingEnabled()` check is unnecessary.
*/
export function isLoggingEnabled(): boolean {
return (logger ?? initLogger()).isLoggingEnabled();
}

View File

@@ -2,7 +2,7 @@
* Utility functions for handling platform-specific commands
*/
import { log } from "../logger/log.js";
import { log, isLoggingEnabled } from "./log.js";
/**
* Map of Unix commands to their Windows equivalents
@@ -59,7 +59,9 @@ export function adaptCommandForPlatform(command: Array<string>): Array<string> {
return command;
}
log(`Adapting command '${cmd}' for Windows platform`);
if (isLoggingEnabled()) {
log(`Adapting command '${cmd}' for Windows platform`);
}
// Create a new command array with the adapted command
const adaptedCommand = [...command];
@@ -76,7 +78,9 @@ export function adaptCommandForPlatform(command: Array<string>): Array<string> {
}
}
log(`Adapted command: ${adaptedCommand.join(" ")}`);
if (isLoggingEnabled()) {
log(`Adapted command: ${adaptedCommand.join(" ")}`);
}
return adaptedCommand;
}

View File

@@ -1,78 +0,0 @@
// Maximum output cap: either MAX_OUTPUT_LINES lines or MAX_OUTPUT_BYTES bytes,
// whichever limit is reached first.
const MAX_OUTPUT_BYTES = 1024 * 10; // 10 KB
const MAX_OUTPUT_LINES = 256;
/**
* Creates a collector that accumulates data Buffers from a stream up to
* specified byte and line limits. After either limit is exceeded, further
* data is ignored.
*/
export function createTruncatingCollector(
stream: NodeJS.ReadableStream,
byteLimit: number = MAX_OUTPUT_BYTES,
lineLimit: number = MAX_OUTPUT_LINES,
): {
getString: () => string;
hit: boolean;
} {
const chunks: Array<Buffer> = [];
let totalBytes = 0;
let totalLines = 0;
let hitLimit = false;
stream?.on("data", (data: Buffer) => {
if (hitLimit) {
return;
}
const dataLength = data.length;
let newlineCount = 0;
for (let i = 0; i < dataLength; i++) {
if (data[i] === 0x0a) {
newlineCount++;
}
}
// If entire chunk fits within byte and line limits, take it whole
if (
totalBytes + dataLength <= byteLimit &&
totalLines + newlineCount <= lineLimit
) {
chunks.push(data);
totalBytes += dataLength;
totalLines += newlineCount;
} else {
// Otherwise, take a partial slice up to the first limit breach
const allowedBytes = byteLimit - totalBytes;
const allowedLines = lineLimit - totalLines;
let bytesTaken = 0;
let linesSeen = 0;
for (let i = 0; i < dataLength; i++) {
// Stop if byte or line limit is reached
if (bytesTaken === allowedBytes || linesSeen === allowedLines) {
break;
}
const byte = data[i];
if (byte === 0x0a) {
linesSeen++;
}
bytesTaken++;
}
if (bytesTaken > 0) {
chunks.push(data.slice(0, bytesTaken));
totalBytes += bytesTaken;
totalLines += linesSeen;
}
hitLimit = true;
}
});
return {
getString() {
return Buffer.concat(chunks).toString("utf8");
},
/** True if either byte or line limit was exceeded */
get hit(): boolean {
return hitLimit;
},
};
}

View File

@@ -2,10 +2,13 @@ import type { ExecResult } from "./interface.js";
import type { SpawnOptions } from "child_process";
import { exec } from "./raw-exec.js";
import { log } from "../../logger/log.js";
import { log } from "../log.js";
import { realpathSync } from "fs";
import { CONFIG_DIR } from "src/utils/config.js";
function getCommonRoots() {
return [
CONFIG_DIR,
// Without this root, it'll cause:
// pyenv: cannot rehash: $HOME/.pyenv/shims isn't writable
`${process.env["HOME"]}/.pyenv`,
@@ -15,20 +18,21 @@ function getCommonRoots() {
export function execWithSeatbelt(
cmd: Array<string>,
opts: SpawnOptions,
writableRoots: ReadonlyArray<string>,
writableRoots: Array<string>,
abortSignal?: AbortSignal,
): Promise<ExecResult> {
let scopedWritePolicy: string;
let policyTemplateParams: Array<string>;
const fullWritableRoots = [...writableRoots, ...getCommonRoots()];
// In practice, fullWritableRoots will be non-empty, but we check just in
// case the logic to build up fullWritableRoots changes.
if (fullWritableRoots.length > 0) {
const { policies, params } = fullWritableRoots
if (writableRoots.length > 0) {
// Add `~/.codex` to the list of writable roots
// (if there's any already, not in read-only mode)
getCommonRoots().map((root) => writableRoots.push(root));
const { policies, params } = writableRoots
.map((root, index) => ({
policy: `(subpath (param "WRITABLE_ROOT_${index}"))`,
param: `-DWRITABLE_ROOT_${index}=${root}`,
// the kernel resolves symlinks before handing them to seatbelt for checking
// so store the canonicalized form in the policy to be compared against
param: `-DWRITABLE_ROOT_${index}=${realpathSync(root)}`,
}))
.reduce(
(

View File

@@ -7,12 +7,13 @@ import type {
StdioPipe,
} from "child_process";
import { log } from "../../logger/log.js";
import { log, isLoggingEnabled } from "../log.js";
import { adaptCommandForPlatform } from "../platform-commands.js";
import { createTruncatingCollector } from "./create-truncating-collector";
import { spawn } from "child_process";
import * as os from "os";
const MAX_BUFFER = 1024 * 100; // 100 KB
/**
* This function should never return a rejected promise: errors should be
* mapped to a non-zero exit code and the error message should be in stderr.
@@ -20,13 +21,16 @@ import * as os from "os";
export function exec(
command: Array<string>,
options: SpawnOptions,
_writableRoots: ReadonlyArray<string>,
_writableRoots: Array<string>,
abortSignal?: AbortSignal,
): Promise<ExecResult> {
// Adapt command for the current platform (e.g., convert 'ls' to 'dir' on Windows)
const adaptedCommand = adaptCommandForPlatform(command);
if (JSON.stringify(adaptedCommand) !== JSON.stringify(command)) {
if (
isLoggingEnabled() &&
JSON.stringify(adaptedCommand) !== JSON.stringify(command)
) {
log(
`Command adapted for platform: ${command.join(
" ",
@@ -91,7 +95,9 @@ export function exec(
// timely fashion.
if (abortSignal) {
const abortHandler = () => {
log(`raw-exec: abort signal received killing child ${child.pid}`);
if (isLoggingEnabled()) {
log(`raw-exec: abort signal received killing child ${child.pid}`);
}
const killTarget = (signal: NodeJS.Signals) => {
if (!child.pid) {
return;
@@ -142,14 +148,37 @@ export function exec(
// resolve the promise and translate the failure into a regular
// ExecResult object so the rest of the agent loop can carry on gracefully.
return new Promise<ExecResult>((resolve) => {
// Collect stdout and stderr up to configured limits.
const stdoutCollector = createTruncatingCollector(child.stdout!);
const stderrCollector = createTruncatingCollector(child.stderr!);
const stdoutChunks: Array<Buffer> = [];
const stderrChunks: Array<Buffer> = [];
let numStdoutBytes = 0;
let numStderrBytes = 0;
let hitMaxStdout = false;
let hitMaxStderr = false;
return new Promise<ExecResult>((resolve) => {
child.stdout?.on("data", (data: Buffer) => {
if (!hitMaxStdout) {
numStdoutBytes += data.length;
if (numStdoutBytes <= MAX_BUFFER) {
stdoutChunks.push(data);
} else {
hitMaxStdout = true;
}
}
});
child.stderr?.on("data", (data: Buffer) => {
if (!hitMaxStderr) {
numStderrBytes += data.length;
if (numStderrBytes <= MAX_BUFFER) {
stderrChunks.push(data);
} else {
hitMaxStderr = true;
}
}
});
child.on("exit", (code, signal) => {
const stdout = stdoutCollector.getString();
const stderr = stderrCollector.getString();
const stdout = Buffer.concat(stdoutChunks).toString("utf8");
const stderr = Buffer.concat(stderrChunks).toString("utf8");
// Map (code, signal) to an exit code. We expect exactly one of the two
// values to be non-null, but we code defensively to handle the case where
@@ -165,61 +194,24 @@ export function exec(
exitCode = 1;
}
log(
`raw-exec: child ${child.pid} exited code=${exitCode} signal=${signal}`,
);
const execResult = {
if (isLoggingEnabled()) {
log(
`raw-exec: child ${child.pid} exited code=${exitCode} signal=${signal}`,
);
}
resolve({
stdout,
stderr,
exitCode,
};
resolve(
addTruncationWarningsIfNecessary(
execResult,
stdoutCollector.hit,
stderrCollector.hit,
),
);
});
});
child.on("error", (err) => {
const execResult = {
resolve({
stdout: "",
stderr: String(err),
exitCode: 1,
};
resolve(
addTruncationWarningsIfNecessary(
execResult,
stdoutCollector.hit,
stderrCollector.hit,
),
);
});
});
});
}
/**
* Adds a truncation warnings to stdout and stderr, if appropriate.
*/
function addTruncationWarningsIfNecessary(
execResult: ExecResult,
hitMaxStdout: boolean,
hitMaxStderr: boolean,
): ExecResult {
if (!hitMaxStdout && !hitMaxStderr) {
return execResult;
} else {
const { stdout, stderr, exitCode } = execResult;
return {
stdout: hitMaxStdout
? stdout + "\n\n[Output truncated: too many lines or bytes]"
: stdout,
stderr: hitMaxStderr
? stderr + "\n\n[Output truncated: too many lines or bytes]"
: stderr,
exitCode,
};
}
}

View File

@@ -19,10 +19,6 @@ export function approximateTokensUsed(items: Array<ResponseItem>): number {
for (const item of items) {
switch (item.type) {
case "message": {
if (item.role !== "user" && item.role !== "assistant") {
continue;
}
for (const c of item.content) {
if (c.type === "input_text" || c.type === "output_text") {
charCount += c.text.length;

View File

@@ -1,82 +0,0 @@
import type {
ResponseItem,
ResponseOutputItem,
} from "openai/resources/responses/responses.mjs";
/**
* Build a GitHub issuesnew URL that prefills the Codex 2bugreport.yml
* template with whatever structured data we can infer from the current
* session.
*/
export function buildBugReportUrl({
items,
cliVersion,
model,
platform,
}: {
/** Chat history so we can summarise user steps */
items: Array<ResponseItem | ResponseOutputItem>;
/** CLI revision string (e.g. output of `codex --revision`) */
cliVersion: string;
/** Active model name */
model: string;
/** Platform string e.g. `darwin arm64 23.0.0` */
platform: string;
}): string {
const params = new URLSearchParams({
template: "2-bug-report.yml",
labels: "bug",
});
params.set("version", cliVersion);
params.set("model", model);
params.set("platform", platform);
const bullets: Array<string> = [];
for (let i = 0; i < items.length; ) {
const entry = items[i];
if (entry?.type === "message" && entry.role === "user") {
const contentArray = entry.content as
| Array<{ text?: string }>
| undefined;
const messageText = contentArray
?.map((c) => c.text ?? "")
.join(" ")
.trim();
let reasoning = 0;
let toolCalls = 0;
let j = i + 1;
while (j < items.length) {
const it = items[j];
if (it?.type === "message" && it?.role === "user") {
break;
} else if (
it?.type === "reasoning" ||
(it?.type === "message" && it?.role === "assistant")
) {
reasoning += 1;
} else if (it?.type === "function_call") {
toolCalls += 1;
}
j++;
}
const codeBlock = `\`\`\`\n ${messageText}\n \`\`\``;
bullets.push(
`- ${codeBlock}\n - \`${reasoning} reasoning\` | \`${toolCalls} tool\``,
);
i = j;
} else {
i += 1;
}
}
if (bullets.length) {
params.set("steps", bullets.join("\n"));
}
return `https://github.com/openai/codex/issues/new?${params.toString()}`;
}

View File

@@ -1,146 +0,0 @@
import type { AgentName } from "package-manager-detector";
import { detectInstallerByPath } from "./package-manager-detector";
import { CLI_VERSION } from "./session";
import boxen from "boxen";
import chalk from "chalk";
import { getLatestVersion } from "fast-npm-meta";
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { getUserAgent } from "package-manager-detector";
import semver from "semver";
interface UpdateCheckState {
lastUpdateCheck?: string;
}
interface UpdateCheckInfo {
currentVersion: string;
latestVersion: string;
}
export interface UpdateOptions {
manager: AgentName;
packageName: string;
}
const UPDATE_CHECK_FREQUENCY = 1000 * 60 * 60 * 24; // 1 day
export function renderUpdateCommand({
manager,
packageName,
}: UpdateOptions): string {
const updateCommands: Record<AgentName, string> = {
npm: `npm install -g ${packageName}`,
pnpm: `pnpm add -g ${packageName}`,
bun: `bun add -g ${packageName}`,
/** Only works in yarn@v1 */
yarn: `yarn global add ${packageName}`,
deno: `deno install -g npm:${packageName}`,
};
return updateCommands[manager];
}
function renderUpdateMessage(options: UpdateOptions) {
const updateCommand = renderUpdateCommand(options);
return `To update, run ${chalk.magenta(updateCommand)} to update.`;
}
async function writeState(stateFilePath: string, state: UpdateCheckState) {
await writeFile(stateFilePath, JSON.stringify(state, null, 2), {
encoding: "utf8",
});
}
async function getUpdateCheckInfo(
packageName: string,
): Promise<UpdateCheckInfo | undefined> {
const metadata = await getLatestVersion(packageName, {
force: true,
throw: false,
});
if ("error" in metadata || !metadata?.version) {
return;
}
return {
currentVersion: CLI_VERSION,
latestVersion: metadata.version,
};
}
export async function checkForUpdates(): Promise<void> {
const { CONFIG_DIR } = await import("./config");
const stateFile = join(CONFIG_DIR, "update-check.json");
// Load previous check timestamp
let state: UpdateCheckState | undefined;
try {
state = JSON.parse(await readFile(stateFile, "utf8"));
} catch {
// ignore
}
// Bail out if we checked less than the configured frequency ago
if (
state?.lastUpdateCheck &&
Date.now() - new Date(state.lastUpdateCheck).valueOf() <
UPDATE_CHECK_FREQUENCY
) {
return;
}
// Fetch current vs latest from the registry
const { name: packageName } = await import("../../package.json");
const packageInfo = await getUpdateCheckInfo(packageName);
await writeState(stateFile, {
...state,
lastUpdateCheck: new Date().toUTCString(),
});
if (
!packageInfo ||
!semver.gt(packageInfo.latestVersion, packageInfo.currentVersion)
) {
return;
}
// Detect global installer
let managerName = await detectInstallerByPath();
// Fallback to the local package manager
if (!managerName) {
const local = getUserAgent();
if (!local) {
// No package managers found, skip it.
return;
}
managerName = local;
}
const updateMessage = renderUpdateMessage({
manager: managerName,
packageName,
});
const box = boxen(
`\
Update available! ${chalk.red(packageInfo.currentVersion)}${chalk.green(
packageInfo.latestVersion,
)}.
${updateMessage}`,
{
padding: 1,
margin: 1,
align: "center",
borderColor: "yellow",
borderStyle: "round",
},
);
// eslint-disable-next-line no-console
console.log(box);
}

View File

@@ -1,31 +1,21 @@
import type { AppConfig } from "./config.js";
import type { ResponseItem } from "openai/resources/responses/responses.mjs";
import { getBaseUrl, getApiKey } from "./config.js";
import { OPENAI_BASE_URL } from "./config.js";
import OpenAI from "openai";
/**
* Generate a condensed summary of the conversation items.
* @param items The list of conversation items to summarize
* @param model The model to use for generating the summary
* @returns A concise structured summary string
*/
/**
* Generate a condensed summary of the conversation items.
* @param items The list of conversation items to summarize
* @param model The model to use for generating the summary
* @param flexMode Whether to use the flex-mode service tier
* @param config The configuration object
* @returns A concise structured summary string
*/
export async function generateCompactSummary(
items: Array<ResponseItem>,
model: string,
flexMode = false,
config: AppConfig,
): Promise<string> {
const oai = new OpenAI({
apiKey: getApiKey(config.provider),
baseURL: getBaseUrl(config.provider),
apiKey: process.env["OPENAI_API_KEY"],
baseURL: OPENAI_BASE_URL,
});
const conversationText = items
@@ -54,7 +44,6 @@ export async function generateCompactSummary(
const response = await oai.chat.completions.create({
model,
...(flexMode ? { service_tier: "flex" } : {}),
messages: [
{
role: "assistant",

View File

@@ -8,9 +8,8 @@
import type { FullAutoErrorMode } from "./auto-approval-mode.js";
import { log, isLoggingEnabled } from "./agent/log.js";
import { AutoApprovalMode } from "./auto-approval-mode.js";
import { log } from "./logger/log.js";
import { providers } from "./providers.js";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
import { load as loadYaml, dump as dumpYaml } from "js-yaml";
import { homedir } from "os";
@@ -41,62 +40,17 @@ export function setApiKey(apiKey: string): void {
OPENAI_API_KEY = apiKey;
}
export function getBaseUrl(provider: string = "openai"): string | undefined {
// Check for a PROVIDER-specific override: e.g. OPENAI_BASE_URL or OLLAMA_BASE_URL.
const envKey = `${provider.toUpperCase()}_BASE_URL`;
if (process.env[envKey]) {
return process.env[envKey];
}
// Get providers config from config file.
const config = loadConfig();
const providersConfig = config.providers ?? providers;
const providerInfo = providersConfig[provider.toLowerCase()];
if (providerInfo) {
return providerInfo.baseURL;
}
// If the provider not found in the providers list and `OPENAI_BASE_URL` is set, use it.
if (OPENAI_BASE_URL !== "") {
return OPENAI_BASE_URL;
}
// We tried.
return undefined;
}
export function getApiKey(provider: string = "openai"): string | undefined {
const config = loadConfig();
const providersConfig = config.providers ?? providers;
const providerInfo = providersConfig[provider.toLowerCase()];
if (providerInfo) {
if (providerInfo.name === "Ollama") {
return process.env[providerInfo.envKey] ?? "dummy";
}
return process.env[providerInfo.envKey];
}
// If the provider not found in the providers list and `OPENAI_API_KEY` is set, use it
if (OPENAI_API_KEY !== "") {
return OPENAI_API_KEY;
}
// We tried.
return undefined;
}
// Formatting (quiet mode-only).
export const PRETTY_PRINT = Boolean(process.env["PRETTY_PRINT"] || "");
// Represents config as persisted in config.json.
export type StoredConfig = {
model?: string;
provider?: string;
approvalMode?: AutoApprovalMode;
fullAutoErrorMode?: FullAutoErrorMode;
memory?: MemoryConfig;
/** Whether to enable desktop notifications for responses */
notify?: boolean;
/** Disable server-side response storage (send full transcript each request) */
disableResponseStorage?: boolean;
providers?: Record<string, { name: string; baseURL: string; envKey: string }>;
history?: {
maxSize?: number;
saveHistory?: boolean;
@@ -120,20 +74,11 @@ export type MemoryConfig = {
export type AppConfig = {
apiKey?: string;
model: string;
provider?: string;
instructions: string;
approvalMode?: AutoApprovalMode;
fullAutoErrorMode?: FullAutoErrorMode;
memory?: MemoryConfig;
/** Whether to enable desktop notifications for responses */
notify: boolean;
/** Disable server-side response storage (send full transcript each request) */
disableResponseStorage?: boolean;
/** Enable the "flex-mode" processing mode for supported models (o3, o4-mini) */
flexMode?: boolean;
providers?: Record<string, { name: string; baseURL: string; envKey: string }>;
history?: {
maxSize: number;
saveHistory: boolean;
@@ -141,9 +86,6 @@ export type AppConfig = {
};
};
// Formatting (quiet mode-only).
export const PRETTY_PRINT = Boolean(process.env["PRETTY_PRINT"] || "");
// ---------------------------------------------------------------------------
// Project doc support (codex.md)
// ---------------------------------------------------------------------------
@@ -295,11 +237,15 @@ export const loadConfig = (
? resolvePath(cwd, options.projectDocPath)
: discoverProjectDocPath(cwd);
if (projectDocPath) {
log(
`[codex] Loaded project doc from ${projectDocPath} (${projectDoc.length} bytes)`,
);
if (isLoggingEnabled()) {
log(
`[codex] Loaded project doc from ${projectDocPath} (${projectDoc.length} bytes)`,
);
}
} else {
log(`[codex] No project doc found in ${cwd}`);
if (isLoggingEnabled()) {
log(`[codex] No project doc found in ${cwd}`);
}
}
}
@@ -320,11 +266,8 @@ export const loadConfig = (
(options.isFullContext
? DEFAULT_FULL_CONTEXT_MODEL
: DEFAULT_AGENTIC_MODEL),
provider: storedConfig.provider,
instructions: combinedInstructions,
notify: storedConfig.notify === true,
approvalMode: storedConfig.approvalMode,
disableResponseStorage: storedConfig.disableResponseStorage ?? false,
};
// -----------------------------------------------------------------------
@@ -402,9 +345,6 @@ export const loadConfig = (
};
}
// Merge default providers with user configured providers in the config.
config.providers = { ...providers, ...storedConfig.providers };
return config;
};
@@ -436,9 +376,6 @@ export const saveConfig = (
// Create the config object to save
const configToSave: StoredConfig = {
model: config.model,
provider: config.provider,
providers: config.providers,
approvalMode: config.approvalMode,
};
// Add history settings if they exist

View File

@@ -0,0 +1,212 @@
/**
* Costestimation helpers for OpenAI responses.
*
* The implementation now distinguishes between *input*, *cached input* and
* *output* tokens, reflecting OpenAIs 202504 pricing scheme. For models
* where we only have a single blended rate we gracefully fall back to the
* legacy logic so existing callsites continue to work.
*/
import type { ResponseItem } from "openai/resources/responses/responses.mjs";
import { approximateTokensUsed } from "./approximate-tokens-used.js";
// ────────────────────────────────────────────────────────────────────────────
// Pricing tables
// ────────────────────────────────────────────────────────────────────────────
/** Breakdown of pertoken prices (in USD). */
type TokenRates = {
/** Price for *noncached* input prompt tokens. */
input: number;
/** Preferential price for *cached* input tokens. */
cachedInput: number;
/** Price for completion / output tokens. */
output: number;
};
/**
* Pricing table (exact model name -> pertoken rates).
* All keys must be lowercase.
*/
const detailedPriceMap: Record<string, TokenRates> = {
// OpenAI “oseries” experimental
"o3": {
input: 10 / 1_000_000,
cachedInput: 2.5 / 1_000_000,
output: 40 / 1_000_000,
},
"o4-mini": {
input: 1.1 / 1_000_000,
cachedInput: 0.275 / 1_000_000,
output: 4.4 / 1_000_000,
},
// GPT4.1 family
"gpt-4.1-nano": {
input: 0.1 / 1_000_000,
cachedInput: 0.025 / 1_000_000,
output: 0.4 / 1_000_000,
},
"gpt-4.1-mini": {
input: 0.4 / 1_000_000,
cachedInput: 0.1 / 1_000_000,
output: 1.6 / 1_000_000,
},
"gpt-4.1": {
input: 2 / 1_000_000,
cachedInput: 0.5 / 1_000_000,
output: 8 / 1_000_000,
},
// GPT4o family
"gpt-4o-mini": {
input: 0.6 / 1_000_000,
cachedInput: 0.3 / 1_000_000,
output: 2.4 / 1_000_000,
},
"gpt-4o": {
input: 5 / 1_000_000,
cachedInput: 2.5 / 1_000_000,
output: 20 / 1_000_000,
},
};
/**
* Legacy singlerate pricing entries (per *thousand* tokens). These are kept
* to provide sensible fallbacks for models that do not yet expose a detailed
* breakdown or where we have no published split pricing. The figures stem
* from older OpenAI announcements and are only meant for *approximation*
* callers that rely on exact accounting should upgrade to models covered by
* {@link detailedPriceMap}.
*/
const blendedPriceMap: Record<string, number> = {
// GPT4 Turbo (Apr 2024)
"gpt-4-turbo": 0.01,
// Legacy GPT4 8k / 32k context models
"gpt-4": 0.03,
// GPT3.5Turbo family
"gpt-3.5-turbo": 0.0005,
// Remaining preview variants (exact names)
"gpt-4o-search-preview": 0.0025,
"gpt-4o-mini-search-preview": 0.00015,
"gpt-4o-realtime-preview": 0.005,
"gpt-4o-audio-preview": 0.0025,
"gpt-4o-mini-audio-preview": 0.00015,
"gpt-4o-mini-realtime-preview": 0.0006,
"gpt-4o-mini": 0.00015,
// Older experimental oseries rates
"o3-mini": 0.0011,
"o1-mini": 0.0011,
"o1-pro": 0.15,
"o1": 0.015,
// Additional internal preview models
"computer-use-preview": 0.003,
};
// ────────────────────────────────────────────────────────────────────────────
// Public helpers
// ────────────────────────────────────────────────────────────────────────────
/**
* Return the pertoken input/cached/output rates for the supplied model, or
* `null` when no detailed pricing is available.
*/
function normalize(model: string): string {
// Lowercase and strip date/version suffixes like “20250414”.
const lower = model.toLowerCase();
const dateSuffix = /-\d{4}-\d{2}-\d{2}$/;
return lower.replace(dateSuffix, "");
}
export function priceRates(model: string): TokenRates | null {
return detailedPriceMap[normalize(model)] ?? null;
}
/**
* Fallback that returns a *single* blended pertoken rate when no detailed
* split is available. This mirrors the behaviour of the pre2025 version so
* that existing callers keep working unmodified.
*/
export function pricePerToken(model: string): number | null {
// Prefer an *average* of the detailed rates when we have them this avoids
// surprises where callers mix `pricePerToken()` with the new detailed
// helpers.
const rates = priceRates(model);
if (rates) {
return (rates.input + rates.output) / 2; // simple average heuristic
}
const entry = blendedPriceMap[normalize(model)];
if (entry == null) {
return null;
}
return entry / 1000;
}
// ────────────────────────────────────────────────────────────────────────────
// Cost estimation
// ────────────────────────────────────────────────────────────────────────────
/** Shape of the `usage` object returned by OpenAIs Responses API. */
export type UsageBreakdown = {
input_tokens?: number;
input_tokens_details?: { cached_tokens?: number } | null;
output_tokens?: number;
total_tokens?: number;
};
/**
* Calculate the exact cost (in USD) for a single usage breakdown. Returns
* `null` when the model is unknown.
*/
export function estimateCostFromUsage(
usage: UsageBreakdown,
model: string,
): number | null {
const rates = priceRates(model);
if (!rates) {
// fall back to blended pricing
const per = pricePerToken(model);
if (per == null) {
return null;
}
const tokens =
usage.total_tokens ??
(usage.input_tokens ?? 0) + (usage.output_tokens ?? 0);
return tokens * per;
}
const input = usage.input_tokens ?? 0;
const cached = usage.input_tokens_details?.cached_tokens ?? 0;
const nonCachedInput = Math.max(0, input - cached);
const output = usage.output_tokens ?? 0;
return (
nonCachedInput * rates.input +
cached * rates.cachedInput +
output * rates.output
);
}
/**
* Rough cost estimate (USD) for a series of {@link ResponseItem}s when using
* the specified model. When no detailed usage object is available we fall
* back to estimating token counts based on the message contents.
*/
export function estimateCostUSD(
items: Array<ResponseItem>,
model: string,
): number | null {
const per = pricePerToken(model);
if (per == null) {
return null;
}
return approximateTokensUsed(items) * per;
}

View File

@@ -1,36 +0,0 @@
import type { ResponseItem } from "openai/resources/responses/responses.mjs";
/**
* Extracts the patch texts of all `apply_patch` tool calls from the given
* message history. Returns an empty string when none are found.
*/
export function extractAppliedPatches(items: Array<ResponseItem>): string {
const patches: Array<string> = [];
for (const item of items) {
if (item.type !== "function_call") {
continue;
}
const { name: toolName, arguments: argsString } = item as unknown as {
name: unknown;
arguments: unknown;
};
if (toolName !== "apply_patch" || typeof argsString !== "string") {
continue;
}
try {
const args = JSON.parse(argsString) as { patch?: string };
if (typeof args.patch === "string" && args.patch.length > 0) {
patches.push(args.patch.trim());
}
} catch {
// Ignore malformed JSON we never want to crash the overlay.
continue;
}
}
return patches.join("\n\n");
}

View File

@@ -1,42 +0,0 @@
import fs from "fs";
import os from "os";
import path from "path";
export function getFileSystemSuggestions(pathPrefix: string): Array<string> {
if (!pathPrefix) {
return [];
}
try {
const sep = path.sep;
const hasTilde = pathPrefix === "~" || pathPrefix.startsWith("~" + sep);
const expanded = hasTilde
? path.join(os.homedir(), pathPrefix.slice(1))
: pathPrefix;
const normalized = path.normalize(expanded);
const isDir = pathPrefix.endsWith(path.sep);
const base = path.basename(normalized);
const dir =
normalized === "." && !pathPrefix.startsWith("." + sep) && !hasTilde
? process.cwd()
: path.dirname(normalized);
const readDir = isDir ? path.join(dir, base) : dir;
return fs
.readdirSync(readDir)
.filter((item) => isDir || item.startsWith(base))
.map((item) => {
const fullPath = path.join(readDir, item);
const isDirectory = fs.statSync(fullPath).isDirectory();
if (isDirectory) {
return path.join(fullPath, sep);
}
return fullPath;
});
} catch {
return [];
}
}

View File

@@ -1,29 +0,0 @@
import { execSync } from "node:child_process";
/**
* Returns the current Git diff for the working directory. If the current
* working directory is not inside a Git repository, `isGitRepo` will be
* false and `diff` will be an empty string.
*/
export function getGitDiff(): {
isGitRepo: boolean;
diff: string;
} {
try {
// First check whether we are inside a git repository. `revparse` exits
// with a nonzero status code if not.
execSync("git rev-parse --is-inside-work-tree", { stdio: "ignore" });
// If the above call didnt throw, we are inside a git repo. Retrieve the
// diff including color codes so that the overlay can render them.
const output = execSync("git diff --color", {
encoding: "utf8",
maxBuffer: 10 * 1024 * 1024, // 10 MB ought to be enough for now
});
return { isGitRepo: true, diff: output };
} catch {
// Either git is not installed or were not inside a repository.
return { isGitRepo: false, diff: "" };
}
}

View File

@@ -2,7 +2,6 @@ import type { ResponseInputItem } from "openai/resources/responses/responses";
import { fileTypeFromBuffer } from "file-type";
import fs from "fs/promises";
import path from "path";
export async function createInputItem(
text: string,
@@ -15,24 +14,17 @@ export async function createInputItem(
};
for (const filePath of images) {
try {
/* eslint-disable no-await-in-loop */
const binary = await fs.readFile(filePath);
const kind = await fileTypeFromBuffer(binary);
/* eslint-enable no-await-in-loop */
const encoded = binary.toString("base64");
const mime = kind?.mime ?? "application/octet-stream";
inputItem.content.push({
type: "input_image",
detail: "auto",
image_url: `data:${mime};base64,${encoded}`,
});
} catch (err) {
inputItem.content.push({
type: "input_text",
text: `[missing image: ${path.basename(filePath)}]`,
});
}
/* eslint-disable no-await-in-loop */
const binary = await fs.readFile(filePath);
const kind = await fileTypeFromBuffer(binary);
/* eslint-enable no-await-in-loop */
const encoded = binary.toString("base64");
const mime = kind?.mime ?? "application/octet-stream";
inputItem.content.push({
type: "input_image",
detail: "auto",
image_url: `data:${mime};base64,${encoded}`,
});
}
return inputItem;

View File

@@ -1,198 +0,0 @@
export type ModelInfo = {
/** The human-readable label for this model */
label: string;
/** The max context window size for this model */
maxContextLength: number;
};
export type SupportedModelId = keyof typeof openAiModelInfo;
export const openAiModelInfo = {
"o1-pro-2025-03-19": {
label: "o1 Pro (2025-03-19)",
maxContextLength: 200000,
},
"o3": {
label: "o3",
maxContextLength: 200000,
},
"o3-2025-04-16": {
label: "o3 (2025-04-16)",
maxContextLength: 200000,
},
"o4-mini": {
label: "o4 Mini",
maxContextLength: 200000,
},
"gpt-4.1-nano": {
label: "GPT-4.1 Nano",
maxContextLength: 1000000,
},
"gpt-4.1-nano-2025-04-14": {
label: "GPT-4.1 Nano (2025-04-14)",
maxContextLength: 1000000,
},
"o4-mini-2025-04-16": {
label: "o4 Mini (2025-04-16)",
maxContextLength: 200000,
},
"gpt-4": {
label: "GPT-4",
maxContextLength: 8192,
},
"o1-preview-2024-09-12": {
label: "o1 Preview (2024-09-12)",
maxContextLength: 128000,
},
"gpt-4.1-mini": {
label: "GPT-4.1 Mini",
maxContextLength: 1000000,
},
"gpt-3.5-turbo-instruct-0914": {
label: "GPT-3.5 Turbo Instruct (0914)",
maxContextLength: 4096,
},
"gpt-4o-mini-search-preview": {
label: "GPT-4o Mini Search Preview",
maxContextLength: 128000,
},
"gpt-4.1-mini-2025-04-14": {
label: "GPT-4.1 Mini (2025-04-14)",
maxContextLength: 1000000,
},
"chatgpt-4o-latest": {
label: "ChatGPT-4o Latest",
maxContextLength: 128000,
},
"gpt-3.5-turbo-1106": {
label: "GPT-3.5 Turbo (1106)",
maxContextLength: 16385,
},
"gpt-4o-search-preview": {
label: "GPT-4o Search Preview",
maxContextLength: 128000,
},
"gpt-4-turbo": {
label: "GPT-4 Turbo",
maxContextLength: 128000,
},
"gpt-4o-realtime-preview-2024-12-17": {
label: "GPT-4o Realtime Preview (2024-12-17)",
maxContextLength: 128000,
},
"gpt-3.5-turbo-instruct": {
label: "GPT-3.5 Turbo Instruct",
maxContextLength: 4096,
},
"gpt-3.5-turbo": {
label: "GPT-3.5 Turbo",
maxContextLength: 16385,
},
"gpt-4-turbo-preview": {
label: "GPT-4 Turbo Preview",
maxContextLength: 128000,
},
"gpt-4o-mini-search-preview-2025-03-11": {
label: "GPT-4o Mini Search Preview (2025-03-11)",
maxContextLength: 128000,
},
"gpt-4-0125-preview": {
label: "GPT-4 (0125) Preview",
maxContextLength: 128000,
},
"gpt-4o-2024-11-20": {
label: "GPT-4o (2024-11-20)",
maxContextLength: 128000,
},
"o3-mini": {
label: "o3 Mini",
maxContextLength: 200000,
},
"gpt-4o-2024-05-13": {
label: "GPT-4o (2024-05-13)",
maxContextLength: 128000,
},
"gpt-4-turbo-2024-04-09": {
label: "GPT-4 Turbo (2024-04-09)",
maxContextLength: 128000,
},
"gpt-3.5-turbo-16k": {
label: "GPT-3.5 Turbo 16k",
maxContextLength: 16385,
},
"o3-mini-2025-01-31": {
label: "o3 Mini (2025-01-31)",
maxContextLength: 200000,
},
"o1-preview": {
label: "o1 Preview",
maxContextLength: 128000,
},
"o1-2024-12-17": {
label: "o1 (2024-12-17)",
maxContextLength: 128000,
},
"gpt-4-0613": {
label: "GPT-4 (0613)",
maxContextLength: 8192,
},
"o1": {
label: "o1",
maxContextLength: 128000,
},
"o1-pro": {
label: "o1 Pro",
maxContextLength: 200000,
},
"gpt-4.5-preview": {
label: "GPT-4.5 Preview",
maxContextLength: 128000,
},
"gpt-4.5-preview-2025-02-27": {
label: "GPT-4.5 Preview (2025-02-27)",
maxContextLength: 128000,
},
"gpt-4o-search-preview-2025-03-11": {
label: "GPT-4o Search Preview (2025-03-11)",
maxContextLength: 128000,
},
"gpt-4o": {
label: "GPT-4o",
maxContextLength: 128000,
},
"gpt-4o-mini": {
label: "GPT-4o Mini",
maxContextLength: 128000,
},
"gpt-4o-2024-08-06": {
label: "GPT-4o (2024-08-06)",
maxContextLength: 128000,
},
"gpt-4.1": {
label: "GPT-4.1",
maxContextLength: 1000000,
},
"gpt-4.1-2025-04-14": {
label: "GPT-4.1 (2025-04-14)",
maxContextLength: 1000000,
},
"gpt-4o-mini-2024-07-18": {
label: "GPT-4o Mini (2024-07-18)",
maxContextLength: 128000,
},
"o1-mini": {
label: "o1 Mini",
maxContextLength: 128000,
},
"gpt-3.5-turbo-0125": {
label: "GPT-3.5 Turbo (0125)",
maxContextLength: 16385,
},
"o1-mini-2024-09-12": {
label: "o1 Mini (2024-09-12)",
maxContextLength: 128000,
},
"gpt-4-1106-preview": {
label: "GPT-4 (1106) Preview",
maxContextLength: 128000,
},
} as const satisfies Record<string, ModelInfo>;

View File

@@ -1,8 +1,4 @@
import type { ResponseItem } from "openai/resources/responses/responses.mjs";
import { approximateTokensUsed } from "./approximate-tokens-used.js";
import { getBaseUrl, getApiKey } from "./config";
import { type SupportedModelId, openAiModelInfo } from "./model-info.js";
import { OPENAI_API_KEY } from "./config";
import OpenAI from "openai";
const MODEL_LIST_TIMEOUT_MS = 2_000; // 2 seconds
@@ -15,49 +11,53 @@ export const RECOMMENDED_MODELS: Array<string> = ["o4-mini", "o3"];
* enters interactive mode. The request is made exactly once during the
* lifetime of the process and the results are cached for subsequent calls.
*/
async function fetchModels(provider: string): Promise<Array<string>> {
// If the user has not configured an API key we cannot retrieve the models.
if (!getApiKey(provider)) {
throw new Error("No API key configured for provider: " + provider);
let modelsPromise: Promise<Array<string>> | null = null;
async function fetchModels(): Promise<Array<string>> {
// If the user has not configured an API key we cannot hit the network.
if (!OPENAI_API_KEY) {
return RECOMMENDED_MODELS;
}
try {
const openai = new OpenAI({
apiKey: getApiKey(provider),
baseURL: getBaseUrl(provider),
});
const openai = new OpenAI({ apiKey: OPENAI_API_KEY });
const list = await openai.models.list();
const models: Array<string> = [];
for await (const model of list as AsyncIterable<{ id?: string }>) {
if (model && typeof model.id === "string") {
let modelStr = model.id;
// Fix for gemini.
if (modelStr.startsWith("models/")) {
modelStr = modelStr.replace("models/", "");
}
models.push(modelStr);
models.push(model.id);
}
}
return models.sort();
} catch (error) {
} catch {
return [];
}
}
/** Returns the list of models available for the provided key / credentials. */
export async function getAvailableModels(
provider: string,
): Promise<Array<string>> {
return fetchModels(provider.toLowerCase());
export function preloadModels(): void {
if (!modelsPromise) {
// Fireandforget callers that truly need the list should `await`
// `getAvailableModels()` instead.
void getAvailableModels();
}
}
export async function getAvailableModels(): Promise<Array<string>> {
if (!modelsPromise) {
modelsPromise = fetchModels();
}
return modelsPromise;
}
/**
* Verifies that the provided model identifier is present in the set returned by
* {@link getAvailableModels}.
* Verify that the provided model identifier is present in the set returned by
* {@link getAvailableModels}. The list of models is fetched from the OpenAI
* `/models` endpoint the first time it is required and then cached inprocess.
*/
export async function isModelSupportedForResponses(
provider: string,
model: string | undefined | null,
): Promise<boolean> {
if (
@@ -70,7 +70,7 @@ export async function isModelSupportedForResponses(
try {
const models = await Promise.race<Array<string>>([
getAvailableModels(provider),
getAvailableModels(),
new Promise<Array<string>>((resolve) =>
setTimeout(() => resolve([]), MODEL_LIST_TIMEOUT_MS),
),
@@ -88,112 +88,3 @@ export async function isModelSupportedForResponses(
return true;
}
}
/** Returns the maximum context length (in tokens) for a given model. */
export function maxTokensForModel(model: string): number {
if (model in openAiModelInfo) {
return openAiModelInfo[model as SupportedModelId].maxContextLength;
}
// fallback to heuristics for models not in the registry
const lower = model.toLowerCase();
if (lower.includes("32k")) {
return 32000;
}
if (lower.includes("16k")) {
return 16000;
}
if (lower.includes("8k")) {
return 8000;
}
if (lower.includes("4k")) {
return 4000;
}
return 128000; // Default to 128k for any other model.
}
/** Calculates the percentage of tokens remaining in context for a model. */
export function calculateContextPercentRemaining(
items: Array<ResponseItem>,
model: string,
): number {
const used = approximateTokensUsed(items);
const max = maxTokensForModel(model);
const remaining = Math.max(0, max - used);
return (remaining / max) * 100;
}
/**
* Typeguard that narrows a {@link ResponseItem} to one that represents a
* userauthored message. The OpenAI SDK represents both input *and* output
* messages with a discriminated union where:
* • `type` is the string literal "message" and
* • `role` is one of "user" | "assistant" | "system" | "developer".
*
* For the purposes of deduplication we only care about *user* messages so we
* detect those here in a single, reusable helper.
*/
function isUserMessage(
item: ResponseItem,
): item is ResponseItem & { type: "message"; role: "user"; content: unknown } {
return item.type === "message" && (item as { role?: string }).role === "user";
}
/**
* Deduplicate the stream of {@link ResponseItem}s before they are persisted in
* component state.
*
* Historically we used the (optional) {@code id} field returned by the
* OpenAI streaming API as the primary key: the first occurrence of any given
* {@code id} “won” and subsequent duplicates were dropped. In practice this
* proved brittle because locallygenerated user messages dont include an
* {@code id}. The result was that if a user quickly pressed <Enter> twice the
* exact same message would appear twice in the transcript.
*
* The new rules are therefore:
* 1. If a {@link ResponseItem} has an {@code id} keep only the *first*
* occurrence of that {@code id} (this retains the previous behaviour for
* assistant / tool messages).
* 2. Additionally, collapse *consecutive* user messages with identical
* content. Two messages are considered identical when their serialized
* {@code content} array matches exactly. We purposefully restrict this
* to **adjacent** duplicates so that legitimately repeated questions at
* a later point in the conversation are still shown.
*/
export function uniqueById(items: Array<ResponseItem>): Array<ResponseItem> {
const seenIds = new Set<string>();
const deduped: Array<ResponseItem> = [];
for (const item of items) {
// ──────────────────────────────────────────────────────────────────
// Rule #1 deduplicate by id when present
// ──────────────────────────────────────────────────────────────────
if (typeof item.id === "string" && item.id.length > 0) {
if (seenIds.has(item.id)) {
continue; // skip duplicates
}
seenIds.add(item.id);
}
// ──────────────────────────────────────────────────────────────────
// Rule #2 collapse consecutive identical user messages
// ──────────────────────────────────────────────────────────────────
if (isUserMessage(item) && deduped.length > 0) {
const prev = deduped[deduped.length - 1]!;
if (
isUserMessage(prev) &&
// Note: the `content` field is an array of message parts. Performing
// a deep compare is overkill here; serialising to JSON is sufficient
// (and fast for the tiny payloads involved).
JSON.stringify(prev.content) === JSON.stringify(item.content)
) {
continue; // skip duplicate user message
}
}
deduped.push(item);
}
return deduped;
}

View File

@@ -1,73 +0,0 @@
import type { AgentName } from "package-manager-detector";
import { execFileSync } from "node:child_process";
import { join, resolve } from "node:path";
import which from "which";
function isInstalled(manager: AgentName): boolean {
try {
which.sync(manager);
return true;
} catch {
return false;
}
}
function getGlobalBinDir(manager: AgentName): string | undefined {
if (!isInstalled(manager)) {
return;
}
try {
switch (manager) {
case "npm": {
const stdout = execFileSync("npm", ["prefix", "-g"], {
encoding: "utf-8",
});
return join(stdout.trim(), "bin");
}
case "pnpm": {
// pnpm bin -g prints the bin dir
const stdout = execFileSync("pnpm", ["bin", "-g"], {
encoding: "utf-8",
});
return stdout.trim();
}
case "bun": {
// bun pm bin -g prints your bun global bin folder
const stdout = execFileSync("bun", ["pm", "bin", "-g"], {
encoding: "utf-8",
});
return stdout.trim();
}
default:
return undefined;
}
} catch {
// ignore
}
return undefined;
}
export async function detectInstallerByPath(): Promise<AgentName | undefined> {
// e.g. /usr/local/bin/codex
const invoked = process.argv[1] && resolve(process.argv[1]);
if (!invoked) {
return;
}
const supportedManagers: Array<AgentName> = ["npm", "pnpm", "bun"];
for (const mgr of supportedManagers) {
const binDir = getGlobalBinDir(mgr);
if (binDir && invoked.startsWith(binDir)) {
return mgr;
}
}
return undefined;
}

View File

@@ -81,13 +81,7 @@ export function parseToolCallArguments(
}
const { cmd, command } = json as Record<string, unknown>;
// The OpenAI model sometimes produces a single string instead of an array.
// Accept both shapes:
const commandArray =
toStringArray(cmd) ??
toStringArray(command) ??
(typeof cmd === "string" ? [cmd] : undefined) ??
(typeof command === "string" ? [command] : undefined);
const commandArray = toStringArray(cmd) ?? toStringArray(command);
if (commandArray == null) {
return undefined;
}

View File

@@ -1,45 +0,0 @@
export const providers: Record<
string,
{ name: string; baseURL: string; envKey: string }
> = {
openai: {
name: "OpenAI",
baseURL: "https://api.openai.com/v1",
envKey: "OPENAI_API_KEY",
},
openrouter: {
name: "OpenRouter",
baseURL: "https://openrouter.ai/api/v1",
envKey: "OPENROUTER_API_KEY",
},
gemini: {
name: "Gemini",
baseURL: "https://generativelanguage.googleapis.com/v1beta/openai",
envKey: "GEMINI_API_KEY",
},
ollama: {
name: "Ollama",
baseURL: "http://localhost:11434/v1",
envKey: "OLLAMA_API_KEY",
},
mistral: {
name: "Mistral",
baseURL: "https://api.mistral.ai/v1",
envKey: "MISTRAL_API_KEY",
},
deepseek: {
name: "DeepSeek",
baseURL: "https://api.deepseek.com",
envKey: "DEEPSEEK_API_KEY",
},
xai: {
name: "xAI",
baseURL: "https://api.x.ai/v1",
envKey: "XAI_API_KEY",
},
groq: {
name: "Groq",
baseURL: "https://api.groq.com/openai/v1",
envKey: "GROQ_API_KEY",
},
};

View File

@@ -1,717 +0,0 @@
import type { OpenAI } from "openai";
import type {
ResponseCreateParams,
Response,
} from "openai/resources/responses/responses";
// Define interfaces based on OpenAI API documentation
type ResponseCreateInput = ResponseCreateParams;
type ResponseOutput = Response;
// interface ResponseOutput {
// id: string;
// object: 'response';
// created_at: number;
// status: 'completed' | 'failed' | 'in_progress' | 'incomplete';
// error: { code: string; message: string } | null;
// incomplete_details: { reason: string } | null;
// instructions: string | null;
// max_output_tokens: number | null;
// model: string;
// output: Array<{
// type: 'message';
// id: string;
// status: 'completed' | 'in_progress';
// role: 'assistant';
// content: Array<{
// type: 'output_text' | 'function_call';
// text?: string;
// annotations?: Array<any>;
// tool_call?: {
// id: string;
// type: 'function';
// function: { name: string; arguments: string };
// };
// }>;
// }>;
// parallel_tool_calls: boolean;
// previous_response_id: string | null;
// reasoning: { effort: string | null; summary: string | null };
// store: boolean;
// temperature: number;
// text: { format: { type: 'text' } };
// tool_choice: string | object;
// tools: Array<any>;
// top_p: number;
// truncation: string;
// usage: {
// input_tokens: number;
// input_tokens_details: { cached_tokens: number };
// output_tokens: number;
// output_tokens_details: { reasoning_tokens: number };
// total_tokens: number;
// } | null;
// user: string | null;
// metadata: Record<string, string>;
// }
// Define types for the ResponseItem content and parts
type ResponseContentPart = {
type: string;
[key: string]: unknown;
};
type ResponseItemType = {
type: string;
id?: string;
status?: string;
role?: string;
content?: Array<ResponseContentPart>;
[key: string]: unknown;
};
type ResponseEvent =
| { type: "response.created"; response: Partial<ResponseOutput> }
| { type: "response.in_progress"; response: Partial<ResponseOutput> }
| {
type: "response.output_item.added";
output_index: number;
item: ResponseItemType;
}
| {
type: "response.content_part.added";
item_id: string;
output_index: number;
content_index: number;
part: ResponseContentPart;
}
| {
type: "response.output_text.delta";
item_id: string;
output_index: number;
content_index: number;
delta: string;
}
| {
type: "response.output_text.done";
item_id: string;
output_index: number;
content_index: number;
text: string;
}
| {
type: "response.function_call_arguments.delta";
item_id: string;
output_index: number;
content_index: number;
delta: string;
}
| {
type: "response.function_call_arguments.done";
item_id: string;
output_index: number;
content_index: number;
arguments: string;
}
| {
type: "response.content_part.done";
item_id: string;
output_index: number;
content_index: number;
part: ResponseContentPart;
}
| {
type: "response.output_item.done";
output_index: number;
item: ResponseItemType;
}
| { type: "response.completed"; response: ResponseOutput }
| { type: "error"; code: string; message: string; param: string | null };
// Define a type for tool call data
type ToolCallData = {
id: string;
name: string;
arguments: string;
};
// Define a type for usage data
type UsageData = {
prompt_tokens?: number;
completion_tokens?: number;
total_tokens?: number;
input_tokens?: number;
input_tokens_details?: { cached_tokens: number };
output_tokens?: number;
output_tokens_details?: { reasoning_tokens: number };
[key: string]: unknown;
};
// Define a type for content output
type ResponseContentOutput =
| {
type: "function_call";
call_id: string;
name: string;
arguments: string;
[key: string]: unknown;
}
| {
type: "output_text";
text: string;
annotations: Array<unknown>;
[key: string]: unknown;
};
// Global map to store conversation histories
const conversationHistories = new Map<
string,
{
previous_response_id: string | null;
messages: Array<OpenAI.Chat.Completions.ChatCompletionMessageParam>;
}
>();
// Utility function to generate unique IDs
function generateId(prefix: string = "msg"): string {
return `${prefix}_${Math.random().toString(36).substr(2, 9)}`;
}
// Function to convert ResponseInputItem to ChatCompletionMessageParam
type ResponseInputItem = ResponseCreateInput["input"][number];
function convertInputItemToMessage(
item: string | ResponseInputItem,
): OpenAI.Chat.Completions.ChatCompletionMessageParam {
// Handle string inputs as content for a user message
if (typeof item === "string") {
return { role: "user", content: item };
}
// At this point we know it's a ResponseInputItem
const responseItem = item;
if (responseItem.type === "message") {
// Use a more specific type assertion for the message content
const content = Array.isArray(responseItem.content)
? responseItem.content
.filter((c) => typeof c === "object" && c.type === "input_text")
.map((c) =>
typeof c === "object" && "text" in c
? (c["text"] as string) || ""
: "",
)
.join("")
: "";
return { role: responseItem.role, content };
} else if (responseItem.type === "function_call_output") {
return {
role: "tool",
tool_call_id: responseItem.call_id,
content: responseItem.output,
};
}
throw new Error(`Unsupported input item type: ${responseItem.type}`);
}
// Function to get full messages including history
function getFullMessages(
input: ResponseCreateInput,
): Array<OpenAI.Chat.Completions.ChatCompletionMessageParam> {
let baseHistory: Array<OpenAI.Chat.Completions.ChatCompletionMessageParam> =
[];
if (input.previous_response_id) {
const prev = conversationHistories.get(input.previous_response_id);
if (!prev) {
throw new Error(
`Previous response not found: ${input.previous_response_id}`,
);
}
baseHistory = prev.messages;
}
// Handle both string and ResponseInputItem in input.input
const newInputMessages = Array.isArray(input.input)
? input.input.map(convertInputItemToMessage)
: [convertInputItemToMessage(input.input)];
const messages = [...baseHistory, ...newInputMessages];
if (
input.instructions &&
messages[0]?.role !== "system" &&
messages[0]?.role !== "developer"
) {
return [{ role: "system", content: input.instructions }, ...messages];
}
return messages;
}
// Function to convert tools
function convertTools(
tools?: ResponseCreateInput["tools"],
): Array<OpenAI.Chat.Completions.ChatCompletionTool> | undefined {
return tools
?.filter((tool) => tool.type === "function")
.map((tool) => ({
type: "function" as const,
function: {
name: tool.name,
description: tool.description || undefined,
parameters: tool.parameters,
},
}));
}
const createCompletion = (openai: OpenAI, input: ResponseCreateInput) => {
const fullMessages = getFullMessages(input);
const chatTools = convertTools(input.tools);
const webSearchOptions = input.tools?.some(
(tool) => tool.type === "function" && tool.name === "web_search",
)
? {}
: undefined;
const chatInput: OpenAI.Chat.Completions.ChatCompletionCreateParams = {
model: input.model,
messages: fullMessages,
tools: chatTools,
web_search_options: webSearchOptions,
temperature: input.temperature,
top_p: input.top_p,
tool_choice: (input.tool_choice === "auto"
? "auto"
: input.tool_choice) as OpenAI.Chat.Completions.ChatCompletionCreateParams["tool_choice"],
stream: input.stream || false,
user: input.user,
metadata: input.metadata,
};
return openai.chat.completions.create(chatInput);
};
// Main function with overloading
async function responsesCreateViaChatCompletions(
openai: OpenAI,
input: ResponseCreateInput & { stream: true },
): Promise<AsyncGenerator<ResponseEvent>>;
async function responsesCreateViaChatCompletions(
openai: OpenAI,
input: ResponseCreateInput & { stream?: false },
): Promise<ResponseOutput>;
async function responsesCreateViaChatCompletions(
openai: OpenAI,
input: ResponseCreateInput,
): Promise<ResponseOutput | AsyncGenerator<ResponseEvent>> {
const completion = await createCompletion(openai, input);
if (input.stream) {
return streamResponses(
input,
completion as AsyncIterable<OpenAI.ChatCompletionChunk>,
);
} else {
return nonStreamResponses(
input,
completion as unknown as OpenAI.Chat.Completions.ChatCompletion,
);
}
}
// Non-streaming implementation
async function nonStreamResponses(
input: ResponseCreateInput,
completion: OpenAI.Chat.Completions.ChatCompletion,
): Promise<ResponseOutput> {
const fullMessages = getFullMessages(input);
try {
const chatResponse = completion;
if (!("choices" in chatResponse) || chatResponse.choices.length === 0) {
throw new Error("No choices in chat completion response");
}
const assistantMessage = chatResponse.choices?.[0]?.message;
if (!assistantMessage) {
throw new Error("No assistant message in chat completion response");
}
// Construct ResponseOutput
const responseId = generateId("resp");
const outputItemId = generateId("msg");
const outputContent: Array<ResponseContentOutput> = [];
// Check if the response contains tool calls
const hasFunctionCalls =
assistantMessage.tool_calls && assistantMessage.tool_calls.length > 0;
if (hasFunctionCalls && assistantMessage.tool_calls) {
for (const toolCall of assistantMessage.tool_calls) {
if (toolCall.type === "function") {
outputContent.push({
type: "function_call",
call_id: toolCall.id,
name: toolCall.function.name,
arguments: toolCall.function.arguments,
});
}
}
}
if (assistantMessage.content) {
outputContent.push({
type: "output_text",
text: assistantMessage.content,
annotations: [],
});
}
// Create response with appropriate status and properties
const responseOutput = {
id: responseId,
object: "response",
created_at: Math.floor(Date.now() / 1000),
status: hasFunctionCalls ? "requires_action" : "completed",
error: null,
incomplete_details: null,
instructions: null,
max_output_tokens: null,
model: chatResponse.model,
output: [
{
type: "message",
id: outputItemId,
status: "completed",
role: "assistant",
content: outputContent,
},
],
parallel_tool_calls: input.parallel_tool_calls ?? false,
previous_response_id: input.previous_response_id ?? null,
reasoning: null,
temperature: input.temperature,
text: { format: { type: "text" } },
tool_choice: input.tool_choice ?? "auto",
tools: input.tools ?? [],
top_p: input.top_p,
truncation: input.truncation ?? "disabled",
usage: chatResponse.usage
? {
input_tokens: chatResponse.usage.prompt_tokens,
input_tokens_details: { cached_tokens: 0 },
output_tokens: chatResponse.usage.completion_tokens,
output_tokens_details: { reasoning_tokens: 0 },
total_tokens: chatResponse.usage.total_tokens,
}
: undefined,
user: input.user ?? undefined,
metadata: input.metadata ?? {},
output_text: "",
} as ResponseOutput;
// Add required_action property for tool calls
if (hasFunctionCalls && assistantMessage.tool_calls) {
// Define type with required action
type ResponseWithAction = Partial<ResponseOutput> & {
required_action: unknown;
};
// Use the defined type for the assertion
(responseOutput as ResponseWithAction).required_action = {
type: "submit_tool_outputs",
submit_tool_outputs: {
tool_calls: assistantMessage.tool_calls.map((toolCall) => ({
id: toolCall.id,
type: toolCall.type,
function: {
name: toolCall.function.name,
arguments: toolCall.function.arguments,
},
})),
},
};
}
// Store history
const newHistory = [...fullMessages, assistantMessage];
conversationHistories.set(responseId, {
previous_response_id: input.previous_response_id ?? null,
messages: newHistory,
});
return responseOutput;
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error(`Failed to process chat completion: ${errorMessage}`);
}
}
// Streaming implementation
async function* streamResponses(
input: ResponseCreateInput,
completion: AsyncIterable<OpenAI.ChatCompletionChunk>,
): AsyncGenerator<ResponseEvent> {
const fullMessages = getFullMessages(input);
const responseId = generateId("resp");
const outputItemId = generateId("msg");
let textContentAdded = false;
let textContent = "";
const toolCalls = new Map<number, ToolCallData>();
let usage: UsageData | null = null;
const finalOutputItem: Array<ResponseContentOutput> = [];
// Initial response
const initialResponse: Partial<ResponseOutput> = {
id: responseId,
object: "response" as const,
created_at: Math.floor(Date.now() / 1000),
status: "in_progress" as const,
model: input.model,
output: [],
error: null,
incomplete_details: null,
instructions: null,
max_output_tokens: null,
parallel_tool_calls: true,
previous_response_id: input.previous_response_id ?? null,
reasoning: null,
temperature: input.temperature,
text: { format: { type: "text" } },
tool_choice: input.tool_choice ?? "auto",
tools: input.tools ?? [],
top_p: input.top_p,
truncation: input.truncation ?? "disabled",
usage: undefined,
user: input.user ?? undefined,
metadata: input.metadata ?? {},
output_text: "",
};
yield { type: "response.created", response: initialResponse };
yield { type: "response.in_progress", response: initialResponse };
let isToolCall = false;
for await (const chunk of completion as AsyncIterable<OpenAI.ChatCompletionChunk>) {
// console.error('\nCHUNK: ', JSON.stringify(chunk));
const choice = chunk.choices[0];
if (!choice) {
continue;
}
if (
!isToolCall &&
(("tool_calls" in choice.delta && choice.delta.tool_calls) ||
choice.finish_reason === "tool_calls")
) {
isToolCall = true;
}
if (chunk.usage) {
usage = {
prompt_tokens: chunk.usage.prompt_tokens,
completion_tokens: chunk.usage.completion_tokens,
total_tokens: chunk.usage.total_tokens,
input_tokens: chunk.usage.prompt_tokens,
input_tokens_details: { cached_tokens: 0 },
output_tokens: chunk.usage.completion_tokens,
output_tokens_details: { reasoning_tokens: 0 },
};
}
if (isToolCall) {
for (const tcDelta of choice.delta.tool_calls || []) {
const tcIndex = tcDelta.index;
const content_index = textContentAdded ? tcIndex + 1 : tcIndex;
if (!toolCalls.has(tcIndex)) {
// New tool call
const toolCallId = tcDelta.id || generateId("call");
const functionName = tcDelta.function?.name || "";
yield {
type: "response.output_item.added",
item: {
type: "function_call",
id: outputItemId,
status: "in_progress",
call_id: toolCallId,
name: functionName,
arguments: "",
},
output_index: 0,
};
toolCalls.set(tcIndex, {
id: toolCallId,
name: functionName,
arguments: "",
});
}
if (tcDelta.function?.arguments) {
const current = toolCalls.get(tcIndex);
if (current) {
current.arguments += tcDelta.function.arguments;
yield {
type: "response.function_call_arguments.delta",
item_id: outputItemId,
output_index: 0,
content_index,
delta: tcDelta.function.arguments,
};
}
}
}
if (choice.finish_reason === "tool_calls") {
for (const [tcIndex, tc] of toolCalls) {
const item = {
type: "function_call",
id: outputItemId,
status: "completed",
call_id: tc.id,
name: tc.name,
arguments: tc.arguments,
};
yield {
type: "response.function_call_arguments.done",
item_id: outputItemId,
output_index: tcIndex,
content_index: textContentAdded ? tcIndex + 1 : tcIndex,
arguments: tc.arguments,
};
yield {
type: "response.output_item.done",
output_index: tcIndex,
item,
};
finalOutputItem.push(item as unknown as ResponseContentOutput);
}
} else {
continue;
}
} else {
if (!textContentAdded) {
yield {
type: "response.content_part.added",
item_id: outputItemId,
output_index: 0,
content_index: 0,
part: { type: "output_text", text: "", annotations: [] },
};
textContentAdded = true;
}
if (choice.delta.content?.length) {
yield {
type: "response.output_text.delta",
item_id: outputItemId,
output_index: 0,
content_index: 0,
delta: choice.delta.content,
};
textContent += choice.delta.content;
}
if (choice.finish_reason) {
yield {
type: "response.output_text.done",
item_id: outputItemId,
output_index: 0,
content_index: 0,
text: textContent,
};
yield {
type: "response.content_part.done",
item_id: outputItemId,
output_index: 0,
content_index: 0,
part: { type: "output_text", text: textContent, annotations: [] },
};
const item = {
type: "message",
id: outputItemId,
status: "completed",
role: "assistant",
content: [
{ type: "output_text", text: textContent, annotations: [] },
],
};
yield {
type: "response.output_item.done",
output_index: 0,
item,
};
finalOutputItem.push(item as unknown as ResponseContentOutput);
} else {
continue;
}
}
// Construct final response
const finalResponse: ResponseOutput = {
id: responseId,
object: "response" as const,
created_at: initialResponse.created_at || Math.floor(Date.now() / 1000),
status: "completed" as const,
error: null,
incomplete_details: null,
instructions: null,
max_output_tokens: null,
model: chunk.model || input.model,
output: finalOutputItem as unknown as ResponseOutput["output"],
parallel_tool_calls: true,
previous_response_id: input.previous_response_id ?? null,
reasoning: null,
temperature: input.temperature,
text: { format: { type: "text" } },
tool_choice: input.tool_choice ?? "auto",
tools: input.tools ?? [],
top_p: input.top_p,
truncation: input.truncation ?? "disabled",
usage: usage as ResponseOutput["usage"],
user: input.user ?? undefined,
metadata: input.metadata ?? {},
output_text: "",
} as ResponseOutput;
// Store history
const assistantMessage: OpenAI.Chat.Completions.ChatCompletionMessageParam =
{
role: "assistant" as const,
};
if (textContent) {
assistantMessage.content = textContent;
}
// Add tool_calls property if needed
if (toolCalls.size > 0) {
const toolCallsArray = Array.from(toolCalls.values()).map((tc) => ({
id: tc.id,
type: "function" as const,
function: { name: tc.name, arguments: tc.arguments },
}));
// Define a more specific type for the assistant message with tool calls
type AssistantMessageWithToolCalls =
OpenAI.Chat.Completions.ChatCompletionMessageParam & {
tool_calls: Array<{
id: string;
type: "function";
function: {
name: string;
arguments: string;
};
}>;
};
// Use type assertion with the defined type
(assistantMessage as AssistantMessageWithToolCalls).tool_calls =
toolCallsArray;
}
const newHistory = [...fullMessages, assistantMessage];
conversationHistories.set(responseId, {
previous_response_id: input.previous_response_id ?? null,
messages: newHistory,
});
yield { type: "response.completed", response: finalResponse };
}
}
export {
responsesCreateViaChatCompletions,
ResponseCreateInput,
ResponseOutput,
ResponseEvent,
};

View File

@@ -0,0 +1,138 @@
import type { ResponseItem } from "openai/resources/responses/responses.mjs";
import { approximateTokensUsed } from "./approximate-tokens-used.js";
import {
estimateCostFromUsage,
pricePerToken,
type UsageBreakdown,
} from "./estimate-cost.js";
/**
* Simple accumulator for {@link ResponseItem}s that exposes aggregate token
* and (approximate) dollarcost statistics for the current conversation.
*/
export class SessionCostTracker {
private readonly model: string;
private readonly items: Array<ResponseItem> = [];
private tokensUsedPrecise: number | null = null;
/**
* Aggregated exact cost when we have detailed `usage` information from the
* OpenAI API. Falls back to `null` when we only have the rough estimate
* path available.
*/
private costPrecise: number | null = null;
constructor(model: string) {
this.model = model;
}
/** Append newlyreceived items to the internal history. */
addItems(items: Array<ResponseItem>): void {
this.items.push(...items);
}
/**
* Add a full usage breakdown as returned by the Responses API. This gives
* us exact token counts and allows truetospec cost accounting that
* factors in cached tokens.
*/
addUsage(usage: UsageBreakdown): void {
const tokens =
usage.total_tokens ??
(usage.input_tokens ?? 0) + (usage.output_tokens ?? 0);
if (Number.isFinite(tokens) && tokens > 0) {
this.tokensUsedPrecise = (this.tokensUsedPrecise ?? 0) + tokens;
}
const cost = estimateCostFromUsage(usage, this.model);
if (cost != null) {
this.costPrecise = (this.costPrecise ?? 0) + cost;
}
}
/** Legacy helper for callers that only know the total token count. */
addTokens(count: number): void {
if (Number.isFinite(count) && count > 0) {
this.tokensUsedPrecise = (this.tokensUsedPrecise ?? 0) + count;
// We deliberately do *not* update costPrecise here without a detailed
// breakdown we cannot know whether tokens were input/output/cached. We
// therefore fall back to the blended rate during `getCostUSD()`.
}
}
/** Approximate total token count so far. */
getTokensUsed(): number {
if (this.tokensUsedPrecise != null) {
return this.tokensUsedPrecise;
}
return approximateTokensUsed(this.items);
}
/** Besteffort USD cost estimate. Returns `null` when the model is unknown. */
getCostUSD(): number | null {
if (this.costPrecise != null) {
return this.costPrecise;
}
const per = pricePerToken(this.model);
if (per == null) {
return null;
}
return this.getTokensUsed() * per;
}
/**
* Humanreadable oneliner suitable for printing at session end (e.g. on
* CtrlC or `/clear`).
*/
summary(): string {
const tokens = this.getTokensUsed();
const cost = this.getCostUSD();
if (cost == null) {
return `Session complete approx. ${tokens} tokens used.`;
}
return `Session complete approx. ${tokens} tokens, $${cost.toFixed(
4,
)} USD.`;
}
}
// ────────────────────────────────────────────────────────────────────────────
// Global helpers so disparate parts of the codebase can share a single
// tracker instance without threading it through countless function calls.
// ────────────────────────────────────────────────────────────────────────────
let globalTracker: SessionCostTracker | null = null;
export function getSessionTracker(): SessionCostTracker | null {
return globalTracker;
}
export function ensureSessionTracker(model: string): SessionCostTracker {
if (!globalTracker) {
globalTracker = new SessionCostTracker(model);
}
return globalTracker;
}
export function resetSessionTracker(): void {
globalTracker = null;
}
/**
* Convenience helper that prints the session summary (if any) and resets the
* global tracker so that the next conversation starts with a clean slate.
*/
export function printAndResetSessionSummary(): void {
if (!globalTracker) {
return; // nothing to do
}
// eslint-disable-next-line no-console -- explicit, uservisible log
console.log("\n" + globalTracker.summary() + "\n");
resetSessionTracker();
}

View File

@@ -1,4 +1,4 @@
export const CLI_VERSION = "0.1.2504221401"; // Must be in sync with package.json.
export const CLI_VERSION = "0.1.2504172351"; // Must be in sync with package.json.
export const ORIGIN = "codex_cli_ts";
export type TerminalChatSession = {

View File

@@ -1,32 +0,0 @@
// Defines the available slash commands and their descriptions.
// Used for autocompletion in the chat input.
export interface SlashCommand {
command: string;
description: string;
}
export const SLASH_COMMANDS: Array<SlashCommand> = [
{
command: "/clear",
description: "Clear conversation history and free up context",
},
{
command: "/clearhistory",
description: "Clear command history",
},
{
command: "/compact",
description:
"Clear conversation history but keep a summary in context. Optional: /compact [instructions for summarization]",
},
{ command: "/history", description: "Open command history" },
{ command: "/help", description: "Show list of commands" },
{ command: "/model", description: "Open model selection panel" },
{ command: "/approval", description: "Open approval mode selection panel" },
{ command: "/bug", description: "Generate a prefilled GitHub bug report" },
{
command: "/diff",
description:
"Show git diff of the working directory (or applied patches if not in git)",
},
];

View File

@@ -1,13 +1,12 @@
import { log } from "../logger/log.js";
import { existsSync } from "fs";
import fs from "fs/promises";
import os from "os";
import path from "path";
const HISTORY_FILE = path.join(os.homedir(), ".codex", "history.json");
const DEFAULT_HISTORY_SIZE = 10_000;
const DEFAULT_HISTORY_SIZE = 1000;
// Regex patterns for sensitive commands that should not be saved.
// Regex patterns for sensitive commands that should not be saved
const SENSITIVE_PATTERNS = [
/\b[A-Za-z0-9-_]{20,}\b/, // API keys and tokens
/\bpassword\b/i,
@@ -19,7 +18,7 @@ const SENSITIVE_PATTERNS = [
export interface HistoryConfig {
maxSize: number;
saveHistory: boolean;
sensitivePatterns: Array<string>; // Regex patterns.
sensitivePatterns: Array<string>; // Array of regex patterns as strings
}
export interface HistoryEntry {
@@ -33,6 +32,9 @@ export const DEFAULT_HISTORY_CONFIG: HistoryConfig = {
sensitivePatterns: [],
};
/**
* Loads command history from the history file
*/
export async function loadCommandHistory(): Promise<Array<HistoryEntry>> {
try {
if (!existsSync(HISTORY_FILE)) {
@@ -43,21 +45,26 @@ export async function loadCommandHistory(): Promise<Array<HistoryEntry>> {
const history = JSON.parse(data) as Array<HistoryEntry>;
return Array.isArray(history) ? history : [];
} catch (error) {
log(`error: failed to load command history: ${error}`);
// Use error logger but for production would use a proper logging system
// eslint-disable-next-line no-console
console.error("Failed to load command history:", error);
return [];
}
}
/**
* Saves command history to the history file
*/
export async function saveCommandHistory(
history: Array<HistoryEntry>,
config: HistoryConfig = DEFAULT_HISTORY_CONFIG,
): Promise<void> {
try {
// Create directory if it doesn't exist.
// Create directory if it doesn't exist
const dir = path.dirname(HISTORY_FILE);
await fs.mkdir(dir, { recursive: true });
// Trim history to max size.
// Trim history to max size
const trimmedHistory = history.slice(-config.maxSize);
await fs.writeFile(
@@ -66,10 +73,14 @@ export async function saveCommandHistory(
"utf-8",
);
} catch (error) {
log(`error: failed to save command history: ${error}`);
// eslint-disable-next-line no-console
console.error("Failed to save command history:", error);
}
}
/**
* Adds a command to history if it's not sensitive
*/
export async function addToHistory(
command: string,
history: Array<HistoryEntry>,
@@ -79,41 +90,46 @@ export async function addToHistory(
return history;
}
// Skip commands with sensitive information.
if (commandHasSensitiveInfo(command, config.sensitivePatterns)) {
// Check if command contains sensitive information
if (isSensitiveCommand(command, config.sensitivePatterns)) {
return history;
}
// Check for duplicate (don't add if it's the same as the last command).
// Check for duplicate (don't add if it's the same as the last command)
const lastEntry = history[history.length - 1];
if (lastEntry && lastEntry.command === command) {
return history;
}
// Add new entry.
const newHistory: Array<HistoryEntry> = [
...history,
{
command,
timestamp: Date.now(),
},
];
// Add new entry
const newEntry: HistoryEntry = {
command,
timestamp: Date.now(),
};
const newHistory = [...history, newEntry];
// Save to file
await saveCommandHistory(newHistory, config);
return newHistory;
}
function commandHasSensitiveInfo(
/**
* Checks if a command contains sensitive information
*/
function isSensitiveCommand(
command: string,
additionalPatterns: Array<string> = [],
): boolean {
// Check built-in patterns.
// Check built-in patterns
for (const pattern of SENSITIVE_PATTERNS) {
if (pattern.test(command)) {
return true;
}
}
// Check additional patterns from config.
// Check additional patterns from config
for (const patternStr of additionalPatterns) {
try {
const pattern = new RegExp(patternStr);
@@ -121,19 +137,23 @@ function commandHasSensitiveInfo(
return true;
}
} catch (error) {
// Invalid regex pattern, skip it.
// Invalid regex pattern, skip it
}
}
return false;
}
/**
* Clears the command history
*/
export async function clearCommandHistory(): Promise<void> {
try {
if (existsSync(HISTORY_FILE)) {
await fs.writeFile(HISTORY_FILE, JSON.stringify([]), "utf-8");
}
} catch (error) {
log(`error: failed to clear command history: ${error}`);
// eslint-disable-next-line no-console
console.error("Failed to clear command history:", error);
}
}

View File

@@ -1,19 +1,20 @@
/* eslint-disable no-console */
import type { ResponseItem } from "openai/resources/responses/responses";
import { loadConfig } from "../config";
import { log } from "../logger/log.js";
import fs from "fs/promises";
import os from "os";
import path from "path";
const SESSIONS_ROOT = path.join(os.homedir(), ".codex", "sessions");
async function saveRolloutAsync(
sessionId: string,
async function saveRolloutToHomeSessions(
items: Array<ResponseItem>,
): Promise<void> {
await fs.mkdir(SESSIONS_ROOT, { recursive: true });
const sessionId = crypto.randomUUID();
const timestamp = new Date().toISOString();
const ts = timestamp.replace(/[:.]/g, "-").slice(0, 10);
const filename = `rollout-${ts}-${sessionId}.json`;
@@ -38,15 +39,23 @@ async function saveRolloutAsync(
"utf8",
);
} catch (error) {
log(`error: failed to save rollout to ${filePath}: ${error}`);
console.error(`Failed to save rollout to ${filePath}: `, error);
}
}
export function saveRollout(
sessionId: string,
items: Array<ResponseItem>,
): void {
// Best-effort. We also do not log here in case of failure as that should be taken care of
// by `saveRolloutAsync` already.
saveRolloutAsync(sessionId, items).catch(() => {});
let debounceTimer: NodeJS.Timeout | null = null;
let pendingItems: Array<ResponseItem> | null = null;
export function saveRollout(items: Array<ResponseItem>): void {
pendingItems = items;
if (debounceTimer) {
clearTimeout(debounceTimer);
}
debounceTimer = setTimeout(() => {
if (pendingItems) {
saveRolloutToHomeSessions(pendingItems).catch(() => {});
pendingItems = null;
}
debounceTimer = null;
}, 2000);
}

View File

@@ -1,6 +1,9 @@
import type { Instance } from "ink";
import type React from "react";
// Costtracking
import { printAndResetSessionSummary } from "./session-cost.js";
let inkRenderer: Instance | null = null;
// Track whether the cleanup routine has already executed so repeat calls are
@@ -50,8 +53,6 @@ export function clearTerminal(): void {
if (inkRenderer) {
inkRenderer.clear();
}
// Also clear scrollback and primary buffer to ensure a truly blank slate
process.stdout.write("\x1b[3J\x1b[H\x1b[2J");
}
export function onExit(): void {
@@ -81,4 +82,12 @@ export function onExit(): void {
/* besteffort continue even if Ink throws */
}
}
// Finally, print a brief token/cost summary for the session best effort
// only, errors are swallowed so that shutdown always succeeds.
try {
printAndResetSessionSummary();
} catch {
/* ignore */
}
}

View File

@@ -1,12 +0,0 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`checkForUpdates() > renders a box when a newer version exists and no global installer 1`] = `
"
╭─────────────────────────────────────────────────╮
│ │
│ Update available! 1.0.0 → 2.0.0. │
│ To update, run bun add -g my-pkg to update. │
│ │
╰─────────────────────────────────────────────────╯
"
`;

View File

@@ -50,7 +50,7 @@ vi.mock("openai", () => {
// The AgentLoop pulls these helpers in order to decide whether a command can
// be autoapproved. None of that matters for this test, so we stub the module
// with minimal no-op implementations.
// with minimal noop implementations.
vi.mock("../src/approvals.js", () => {
return {
__esModule: true,

View File

@@ -1,111 +0,0 @@
import { describe, it, expect, vi } from "vitest";
// ---------------------------------------------------------------------------
// Utility helpers & OpenAI mock tailored for serverside errors that occur
// *after* the streaming iterator was created (i.e. during iteration).
// ---------------------------------------------------------------------------
function createStreamThatErrors(err: Error) {
return new (class {
public controller = { abort: vi.fn() };
async *[Symbol.asyncIterator]() {
// Immediately raise the error once iteration starts mimics OpenAI SDK
// behaviour which throws from the iterator when the HTTP response status
// indicates an internal server failure.
throw err;
}
})();
}
// Spy holder swapped out per test case.
const openAiState: { createSpy?: ReturnType<typeof vi.fn> } = {};
vi.mock("openai", () => {
class FakeOpenAI {
public responses = {
create: (...args: Array<any>) => openAiState.createSpy!(...args),
};
}
class APIConnectionTimeoutError extends Error {}
return {
__esModule: true,
default: FakeOpenAI,
APIConnectionTimeoutError,
};
});
// Approvals / formatting stubs not part of the behaviour under test.
vi.mock("../src/approvals.js", () => ({
__esModule: true,
alwaysApprovedCommands: new Set<string>(),
canAutoApprove: () => ({ type: "auto-approve", runInSandbox: false } as any),
isSafeCommand: () => null,
}));
vi.mock("../src/format-command.js", () => ({
__esModule: true,
formatCommandForDisplay: (c: Array<string>) => c.join(" "),
}));
// Silence debug logging so the test output stays uncluttered.
vi.mock("../src/utils/agent/log.js", () => ({
__esModule: true,
log: () => {},
isLoggingEnabled: () => false,
}));
import { AgentLoop } from "../src/utils/agent/agent-loop.js";
describe("AgentLoop server_error surfaced during streaming", () => {
it("shows userfriendly system message instead of crashing", async () => {
const apiErr: any = new Error(
"The server had an error while processing your request. Sorry about that!",
);
// Replicate the structure used by the OpenAI SDK for 5xx failures.
apiErr.type = "server_error";
apiErr.code = null;
apiErr.status = undefined; // SDK leaves status undefined in this pathway
openAiState.createSpy = vi.fn(async () => {
return createStreamThatErrors(apiErr);
});
const received: Array<any> = [];
const agent = new AgentLoop({
model: "any",
instructions: "",
approvalPolicy: { mode: "auto" } as any,
additionalWritableRoots: [],
onItem: (i) => received.push(i),
onLoading: () => {},
getCommandConfirmation: async () => ({ review: "yes" } as any),
onLastResponseId: () => {},
});
const userMsg = [
{
type: "message",
role: "user",
content: [{ type: "input_text", text: "ping" }],
},
];
await expect(agent.run(userMsg as any)).resolves.not.toThrow();
// allow async onItem deliveries to flush
await new Promise((r) => setTimeout(r, 20));
const sysMsg = received.find(
(i) =>
i.role === "system" &&
typeof i.content?.[0]?.text === "string" &&
i.content[0].text.includes("Network error"),
);
expect(sysMsg).toBeTruthy();
});
});

View File

@@ -11,13 +11,7 @@ describe("canAutoApprove()", () => {
const writeablePaths: Array<string> = [];
const check = (command: ReadonlyArray<string>): SafetyAssessment =>
canAutoApprove(
command,
/* workdir */ undefined,
"suggest",
writeablePaths,
env,
);
canAutoApprove(command, "suggest", writeablePaths, env);
test("simple safe commands", () => {
expect(check(["ls"])).toEqual({
@@ -79,7 +73,7 @@ describe("canAutoApprove()", () => {
test("true command is considered safe", () => {
expect(check(["true"])).toEqual({
type: "auto-approve",
reason: "No-op (true)",
reason: "Noop (true)",
group: "Utility",
runInSandbox: false,
});
@@ -95,56 +89,4 @@ describe("canAutoApprove()", () => {
expect(check(["cargo", "build"])).toEqual({ type: "ask-user" });
});
test("find", () => {
expect(check(["find", ".", "-name", "file.txt"])).toEqual({
type: "auto-approve",
reason: "Find files or directories",
group: "Searching",
runInSandbox: false,
});
// Options that can execute arbitrary commands.
expect(
check(["find", ".", "-name", "file.txt", "-exec", "rm", "{}", ";"]),
).toEqual({
type: "ask-user",
});
expect(
check(["find", ".", "-name", "*.py", "-execdir", "python3", "{}", ";"]),
).toEqual({
type: "ask-user",
});
expect(
check(["find", ".", "-name", "file.txt", "-ok", "rm", "{}", ";"]),
).toEqual({
type: "ask-user",
});
expect(
check(["find", ".", "-name", "*.py", "-okdir", "python3", "{}", ";"]),
).toEqual({
type: "ask-user",
});
// Option that deletes matching files.
expect(check(["find", ".", "-delete", "-name", "file.txt"])).toEqual({
type: "ask-user",
});
// Options that write pathnames to a file.
expect(check(["find", ".", "-fls", "/etc/passwd"])).toEqual({
type: "ask-user",
});
expect(check(["find", ".", "-fprint", "/etc/passwd"])).toEqual({
type: "ask-user",
});
expect(check(["find", ".", "-fprint0", "/etc/passwd"])).toEqual({
type: "ask-user",
});
expect(
check(["find", ".", "-fprintf", "/root/suid.txt", "%#m %u %p\n"]),
).toEqual({
type: "ask-user",
});
});
});

View File

@@ -1,178 +0,0 @@
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { join } from "node:path";
import os from "node:os";
import type { UpdateOptions } from "../src/utils/check-updates";
import { getLatestVersion } from "fast-npm-meta";
import { getUserAgent } from "package-manager-detector";
import {
checkForUpdates,
renderUpdateCommand,
} from "../src/utils/check-updates";
import { detectInstallerByPath } from "../src/utils/package-manager-detector";
import { CLI_VERSION } from "../src/utils/session";
// In-memory FS mock
let memfs: Record<string, string> = {};
vi.mock("node:fs/promises", async (importOriginal) => {
return {
...(await importOriginal()),
readFile: async (path: string) => {
if (!(path in memfs)) {
const err: any = new Error(
`ENOENT: no such file or directory, open '${path}'`,
);
err.code = "ENOENT";
throw err;
}
return memfs[path];
},
writeFile: async (path: string, data: string) => {
memfs[path] = data;
},
rm: async (path: string) => {
delete memfs[path];
},
};
});
// Mock package name & CLI version
const MOCK_PKG = "my-pkg";
vi.mock("../package.json", () => ({ name: MOCK_PKG }));
vi.mock("../src/utils/session", () => ({ CLI_VERSION: "1.0.0" }));
vi.mock("../src/utils/package-manager-detector", async (importOriginal) => {
return {
...(await importOriginal()),
detectInstallerByPath: vi.fn(),
};
});
// Mock external services
vi.mock("fast-npm-meta", () => ({ getLatestVersion: vi.fn() }));
vi.mock("package-manager-detector", () => ({ getUserAgent: vi.fn() }));
describe("renderUpdateCommand()", () => {
it.each([
[{ manager: "npm", packageName: MOCK_PKG }, `npm install -g ${MOCK_PKG}`],
[{ manager: "pnpm", packageName: MOCK_PKG }, `pnpm add -g ${MOCK_PKG}`],
[{ manager: "bun", packageName: MOCK_PKG }, `bun add -g ${MOCK_PKG}`],
[{ manager: "yarn", packageName: MOCK_PKG }, `yarn global add ${MOCK_PKG}`],
[
{ manager: "deno", packageName: MOCK_PKG },
`deno install -g npm:${MOCK_PKG}`,
],
])("%s → command", async (options, cmd) => {
expect(renderUpdateCommand(options as UpdateOptions)).toBe(cmd);
});
});
describe("checkForUpdates()", () => {
// Use a stable directory under the OS temp
const TMP = join(os.tmpdir(), "update-test-memfs");
const STATE_PATH = join(TMP, "update-check.json");
beforeEach(async () => {
memfs = {};
// Mock CONFIG_DIR to our TMP
vi.doMock("../src/utils/config", () => ({ CONFIG_DIR: TMP }));
// Freeze time so the 24h logic is deterministic
vi.useFakeTimers().setSystemTime(new Date("2025-01-01T00:00:00Z"));
vi.resetAllMocks();
});
afterEach(async () => {
vi.useRealTimers();
});
it("uses global installer when detected, ignoring local agent", async () => {
// seed old timestamp
const old = new Date("2000-01-01T00:00:00Z").toUTCString();
memfs[STATE_PATH] = JSON.stringify({ lastUpdateCheck: old });
// simulate registry says update available
vi.mocked(getLatestVersion).mockResolvedValue({ version: "2.0.0" } as any);
// local agent would be npm, but global detection wins
vi.mocked(getUserAgent).mockReturnValue("npm");
vi.mocked(detectInstallerByPath).mockReturnValue(Promise.resolve("pnpm"));
const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
await checkForUpdates();
// should render using `pnpm` (global) rather than `npm`
expect(logSpy).toHaveBeenCalledOnce();
const output = logSpy.mock.calls.at(0)?.at(0);
expect(output).toContain("pnpm add -g"); // global branch used
// state updated
const newState = JSON.parse(memfs[STATE_PATH]!);
expect(newState.lastUpdateCheck).toBe(new Date().toUTCString());
});
it("skips when lastUpdateCheck is still fresh (<frequency)", async () => {
// seed a timestamp 12h ago
const recent = new Date(Date.now() - 1000 * 60 * 60 * 12).toUTCString();
memfs[STATE_PATH] = JSON.stringify({ lastUpdateCheck: recent });
const versionSpy = vi.mocked(getLatestVersion);
const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
await checkForUpdates();
expect(versionSpy).not.toHaveBeenCalled();
expect(logSpy).not.toHaveBeenCalled();
});
it("does not print when up-to-date", async () => {
vi.mocked(getLatestVersion).mockResolvedValue({
version: CLI_VERSION,
} as any);
vi.mocked(getUserAgent).mockReturnValue("npm");
vi.mocked(detectInstallerByPath).mockResolvedValue(undefined);
const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
await checkForUpdates();
expect(logSpy).not.toHaveBeenCalled();
// but state still written
const state = JSON.parse(memfs[STATE_PATH]!);
expect(state.lastUpdateCheck).toBe(new Date().toUTCString());
});
it("does not print when no manager detected at all", async () => {
vi.mocked(getLatestVersion).mockResolvedValue({ version: "2.0.0" } as any);
vi.mocked(detectInstallerByPath).mockResolvedValue(undefined);
vi.mocked(getUserAgent).mockReturnValue(null);
const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
await checkForUpdates();
expect(logSpy).not.toHaveBeenCalled();
// state still written
const state = JSON.parse(memfs[STATE_PATH]!);
expect(state.lastUpdateCheck).toBe(new Date().toUTCString());
});
it("renders a box when a newer version exists and no global installer", async () => {
// old timestamp
const old = new Date("2000-01-01T00:00:00Z").toUTCString();
memfs[STATE_PATH] = JSON.stringify({ lastUpdateCheck: old });
vi.mocked(getLatestVersion).mockResolvedValue({ version: "2.0.0" } as any);
vi.mocked(detectInstallerByPath).mockResolvedValue(undefined);
vi.mocked(getUserAgent).mockReturnValue("bun");
const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
await checkForUpdates();
expect(logSpy).toHaveBeenCalledOnce();
const output = logSpy.mock.calls[0]![0] as string;
expect(output).toContain("bun add -g");
expect(output).to.matchSnapshot();
// state updated
const state = JSON.parse(memfs[STATE_PATH]!);
expect(state.lastUpdateCheck).toBe(new Date().toUTCString());
});
});

View File

@@ -1,174 +0,0 @@
import React from "react";
import type { ComponentProps } from "react";
import { describe, it, expect, vi } from "vitest";
import { renderTui } from "./ui-test-helpers.js";
import TerminalChatInput from "../src/components/chat/terminal-chat-input.js";
import TerminalChatNewInput from "../src/components/chat/terminal-chat-new-input.js";
import * as TermUtils from "../src/utils/terminal.js";
// -------------------------------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------------------------------
async function type(
stdin: NodeJS.WritableStream,
text: string,
flush: () => Promise<void>,
): Promise<void> {
stdin.write(text);
await flush();
}
// -------------------------------------------------------------------------------------------------
// Tests
// -------------------------------------------------------------------------------------------------
describe("/clear command", () => {
it("invokes clearTerminal and resets context in TerminalChatInput", async () => {
const clearSpy = vi
.spyOn(TermUtils, "clearTerminal")
.mockImplementation(() => {});
const setItems = vi.fn();
// Minimal stub of a ResponseItem cast to bypass exhaustive type checks in this test context
const existingItems = [
{
id: "dummy-1",
type: "message",
role: "system",
content: [{ type: "input_text", text: "Old item" }],
},
] as Array<any>;
const props: ComponentProps<typeof TerminalChatInput> = {
isNew: false,
loading: false,
submitInput: () => {},
confirmationPrompt: null,
explanation: undefined,
submitConfirmation: () => {},
setLastResponseId: () => {},
setItems,
contextLeftPercent: 100,
openOverlay: () => {},
openModelOverlay: () => {},
openApprovalOverlay: () => {},
openHelpOverlay: () => {},
openDiffOverlay: () => {},
onCompact: () => {},
interruptAgent: () => {},
active: true,
thinkingSeconds: 0,
items: existingItems,
};
const { stdin, flush, cleanup } = renderTui(
<TerminalChatInput {...props} />,
);
await flush();
await type(stdin, "/clear", flush);
await type(stdin, "\r", flush); // press Enter
// Allow any asynchronous state updates to propagate
await flush();
expect(clearSpy).toHaveBeenCalledTimes(2);
expect(setItems).toHaveBeenCalledTimes(2);
const stateUpdater = setItems.mock.calls[0]![0];
expect(typeof stateUpdater).toBe("function");
const newItems = stateUpdater(existingItems);
expect(Array.isArray(newItems)).toBe(true);
expect(newItems).toHaveLength(2);
expect(newItems.at(-1)).toMatchObject({
role: "system",
type: "message",
content: [{ type: "input_text", text: "Terminal cleared" }],
});
cleanup();
clearSpy.mockRestore();
});
it("invokes clearTerminal and resets context in TerminalChatNewInput", async () => {
const clearSpy = vi
.spyOn(TermUtils, "clearTerminal")
.mockImplementation(() => {});
const setItems = vi.fn();
const props: ComponentProps<typeof TerminalChatNewInput> = {
isNew: false,
loading: false,
submitInput: () => {},
confirmationPrompt: null,
explanation: undefined,
submitConfirmation: () => {},
setLastResponseId: () => {},
setItems,
contextLeftPercent: 100,
openOverlay: () => {},
openModelOverlay: () => {},
openApprovalOverlay: () => {},
openHelpOverlay: () => {},
openDiffOverlay: () => {},
interruptAgent: () => {},
active: true,
thinkingSeconds: 0,
};
const { stdin, flush, cleanup } = renderTui(
<TerminalChatNewInput {...props} />,
);
await flush();
await type(stdin, "/clear", flush);
await type(stdin, "\r", flush); // press Enter
await flush();
expect(clearSpy).toHaveBeenCalledTimes(1);
expect(setItems).toHaveBeenCalledTimes(1);
const firstArg = setItems.mock.calls[0]![0];
expect(Array.isArray(firstArg)).toBe(true);
expect(firstArg).toHaveLength(1);
expect(firstArg[0]).toMatchObject({
role: "system",
type: "message",
content: [{ type: "input_text", text: "Terminal cleared" }],
});
cleanup();
clearSpy.mockRestore();
});
});
describe("clearTerminal", () => {
it("writes escape sequence to stdout", () => {
const originalQuiet = process.env["CODEX_QUIET_MODE"];
delete process.env["CODEX_QUIET_MODE"];
process.env["CODEX_QUIET_MODE"] = "0";
const writeSpy = vi
.spyOn(process.stdout, "write")
.mockImplementation(() => true);
TermUtils.clearTerminal();
expect(writeSpy).toHaveBeenCalledWith("\x1b[3J\x1b[H\x1b[2J");
writeSpy.mockRestore();
if (originalQuiet !== undefined) {
process.env["CODEX_QUIET_MODE"] = originalQuiet;
} else {
delete process.env["CODEX_QUIET_MODE"];
}
});
});

View File

@@ -1,11 +1,9 @@
import type * as fsType from "fs";
import { loadConfig, saveConfig } from "../src/utils/config.js"; // parent import first
import { AutoApprovalMode } from "../src/utils/auto-approval-mode.js";
import { tmpdir } from "os";
import { join } from "path";
import { test, expect, beforeEach, afterEach, vi } from "vitest";
import { providers as defaultProviders } from "../src/utils/providers";
// Inmemory FS store
let memfs: Record<string, string> = {};
@@ -27,7 +25,7 @@ vi.mock("fs", async () => {
memfs[path] = data;
},
mkdirSync: () => {
// no-op in inmemory store
// noop in inmemory store
},
rmSync: (path: string) => {
// recursively delete any key under this prefix
@@ -109,128 +107,3 @@ test("loads user instructions + project doc when codex.md is present", () => {
userInstr + "\n\n--- project-doc ---\n\n" + projectDoc,
);
});
test("loads and saves approvalMode correctly", () => {
// Setup config with approvalMode
memfs[testConfigPath] = JSON.stringify(
{
model: "mymodel",
approvalMode: AutoApprovalMode.AUTO_EDIT,
},
null,
2,
);
memfs[testInstructionsPath] = "test instructions";
// Load config and verify approvalMode
const loadedConfig = loadConfig(testConfigPath, testInstructionsPath, {
disableProjectDoc: true,
});
// Check approvalMode was loaded correctly
expect(loadedConfig.approvalMode).toBe(AutoApprovalMode.AUTO_EDIT);
// Modify approvalMode and save
const updatedConfig = {
...loadedConfig,
approvalMode: AutoApprovalMode.FULL_AUTO,
};
saveConfig(updatedConfig, testConfigPath, testInstructionsPath);
// Verify saved config contains updated approvalMode
expect(memfs[testConfigPath]).toContain(
`"approvalMode": "${AutoApprovalMode.FULL_AUTO}"`,
);
// Load again and verify updated value
const reloadedConfig = loadConfig(testConfigPath, testInstructionsPath, {
disableProjectDoc: true,
});
expect(reloadedConfig.approvalMode).toBe(AutoApprovalMode.FULL_AUTO);
});
test("loads and saves providers correctly", () => {
// Setup custom providers configuration
const customProviders = {
openai: {
name: "Custom OpenAI",
baseURL: "https://custom-api.openai.com/v1",
envKey: "CUSTOM_OPENAI_API_KEY",
},
anthropic: {
name: "Anthropic",
baseURL: "https://api.anthropic.com",
envKey: "ANTHROPIC_API_KEY",
},
};
// Create config with providers
const testConfig = {
model: "test-model",
provider: "anthropic",
providers: customProviders,
instructions: "test instructions",
notify: false,
};
// Save the config
saveConfig(testConfig, testConfigPath, testInstructionsPath);
// Verify saved config contains providers
expect(memfs[testConfigPath]).toContain(`"providers"`);
expect(memfs[testConfigPath]).toContain(`"Custom OpenAI"`);
expect(memfs[testConfigPath]).toContain(`"Anthropic"`);
expect(memfs[testConfigPath]).toContain(`"provider": "anthropic"`);
// Load config and verify providers were loaded correctly
const loadedConfig = loadConfig(testConfigPath, testInstructionsPath, {
disableProjectDoc: true,
});
// Check providers were loaded correctly
expect(loadedConfig.provider).toBe("anthropic");
expect(loadedConfig.providers).toEqual({
...defaultProviders,
...customProviders,
});
// Test merging with built-in providers
// Create a config with only one custom provider
const partialProviders = {
customProvider: {
name: "Custom Provider",
baseURL: "https://custom-api.example.com",
envKey: "CUSTOM_API_KEY",
},
};
const partialConfig = {
model: "test-model",
provider: "customProvider",
providers: partialProviders,
instructions: "test instructions",
notify: false,
};
// Save the partial config
saveConfig(partialConfig, testConfigPath, testInstructionsPath);
// Load config and verify providers were merged with built-in providers
const mergedConfig = loadConfig(testConfigPath, testInstructionsPath, {
disableProjectDoc: true,
});
// Check providers is defined
expect(mergedConfig.providers).toBeDefined();
// Use bracket notation to access properties
if (mergedConfig.providers) {
expect(mergedConfig.providers["customProvider"]).toBeDefined();
expect(mergedConfig.providers["customProvider"]).toEqual(
partialProviders.customProvider,
);
// Built-in providers should still be there (like openai)
expect(mergedConfig.providers["openai"]).toBeDefined();
}
});

View File

@@ -0,0 +1,28 @@
import { describe, expect, it } from "vitest";
import type { ResponseItem } from "openai/resources/responses/responses.mjs";
import { calculateContextPercentRemaining } from "../src/components/chat/terminal-chat-utils.js";
function makeUserMessage(id: string, text: string): ResponseItem {
return {
id,
type: "message",
role: "user",
content: [{ type: "input_text", text }],
} as ResponseItem;
}
describe("calculateContextPercentRemaining", () => {
it("includes extra context characters in calculation", () => {
const msgText = "a".repeat(40); // 40 chars → 10 tokens
const items = [makeUserMessage("1", msgText)];
const model = "gpt-4-16k";
const base = calculateContextPercentRemaining(items, model);
const withExtra = calculateContextPercentRemaining(items, model, 8); // +8 chars → +2 tokens
expect(withExtra).toBeLessThan(base);
});
});

View File

@@ -1,55 +0,0 @@
import { PassThrough } from "stream";
import { once } from "events";
import { describe, it, expect } from "vitest";
import { createTruncatingCollector } from "../src/utils/agent/sandbox/create-truncating-collector.js";
describe("createTruncatingCollector", () => {
it("collects data under limits without truncation", async () => {
const stream = new PassThrough();
const collector = createTruncatingCollector(stream, 100, 10);
const data = "line1\nline2\n";
stream.end(Buffer.from(data));
await once(stream, "end");
expect(collector.getString()).toBe(data);
expect(collector.hit).toBe(false);
});
it("truncates data over byte limit", async () => {
const stream = new PassThrough();
const collector = createTruncatingCollector(stream, 5, 100);
stream.end(Buffer.from("hello world"));
await once(stream, "end");
expect(collector.getString()).toBe("hello");
expect(collector.hit).toBe(true);
});
it("truncates data over line limit", async () => {
const stream = new PassThrough();
const collector = createTruncatingCollector(stream, 1000, 2);
const data = "a\nb\nc\nd\n";
stream.end(Buffer.from(data));
await once(stream, "end");
expect(collector.getString()).toBe("a\nb\n");
expect(collector.hit).toBe(true);
});
it("stops collecting after limit is hit across multiple writes", async () => {
const stream = new PassThrough();
const collector = createTruncatingCollector(stream, 10, 2);
stream.write(Buffer.from("1\n"));
stream.write(Buffer.from("2\n3\n4\n"));
stream.end();
await once(stream, "end");
expect(collector.getString()).toBe("1\n2\n");
expect(collector.hit).toBe(true);
});
it("handles zero limits", async () => {
const stream = new PassThrough();
const collector = createTruncatingCollector(stream, 0, 0);
stream.end(Buffer.from("anything\n"));
await once(stream, "end");
expect(collector.getString()).toBe("");
expect(collector.hit).toBe(true);
});
});

View File

@@ -0,0 +1,69 @@
import { describe, expect, test } from "vitest";
import {
estimateCostUSD,
estimateCostFromUsage,
} from "../src/utils/estimate-cost.js";
import { SessionCostTracker } from "../src/utils/session-cost.js";
import type { ResponseItem } from "openai/resources/responses/responses.mjs";
// Helper to craft a minimal ResponseItem for tests
function makeMessage(
id: string,
role: "user" | "assistant",
text: string,
): ResponseItem {
return {
id,
type: "message",
role,
content: [{ type: role === "user" ? "input_text" : "output_text", text }],
} as ResponseItem;
}
describe("estimateCostUSD", () => {
test("returns a proportional, positive estimate for known models", () => {
const items: Array<ResponseItem> = [
makeMessage("1", "user", "hello world"),
makeMessage("2", "assistant", "hi there"),
];
const cost = estimateCostUSD(items, "gpt-3.5-turbo");
expect(cost).not.toBeNull();
expect(cost!).toBeGreaterThan(0);
// Adding another token should increase the estimate
const cost2 = estimateCostUSD(
items.concat([makeMessage("3", "user", "extra")]),
"gpt-3.5-turbo",
);
expect(cost2!).toBeGreaterThan(cost!);
});
test("cost calculation honours cached input token discount", () => {
const usage = {
input_tokens: 1000,
input_tokens_details: { cached_tokens: 600 },
output_tokens: 500,
total_tokens: 1500,
} as any; // simple literal structure for test
const cost = estimateCostFromUsage(usage, "gpt-4.1");
// Expected: (1000-600)*0.000002 + 600*0.0000005 + 500*0.000008
const expected = 400 * 0.000002 + 600 * 0.0000005 + 500 * 0.000008;
expect(cost).not.toBeNull();
expect(cost!).toBeCloseTo(expected, 8);
});
});
describe("SessionCostTracker", () => {
test("accumulates items and reports tokens & cost", () => {
const tracker = new SessionCostTracker("gpt-3.5-turbo");
tracker.addItems([makeMessage("1", "user", "foo")]);
tracker.addItems([makeMessage("2", "assistant", "bar baz")]);
expect(tracker.getTokensUsed()).toBeGreaterThan(0);
expect(tracker.getCostUSD()!).toBeGreaterThan(0);
});
});

View File

@@ -1,44 +0,0 @@
import { execApplyPatch } from "../src/utils/agent/exec.js";
import fs from "fs";
import os from "os";
import path from "path";
import { test, expect } from "vitest";
/**
* This test verifies that `execApplyPatch()` is able to add a new file whose
* parent directory does not yet exist. Prior to the fix, the call would throw
* because `fs.writeFileSync()` could not create intermediate directories. The
* test creates an isolated temporary directory to avoid polluting the project
* workspace.
*/
test("execApplyPatch creates missing directories when adding a file", () => {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "apply-patch-test-"));
// Ensure we start from a clean slate.
const nestedFileRel = path.join("foo", "bar", "baz.txt");
const nestedFileAbs = path.join(tmpDir, nestedFileRel);
expect(fs.existsSync(nestedFileAbs)).toBe(false);
const patch = `*** Begin Patch\n*** Add File: ${nestedFileRel}\n+hello new world\n*** End Patch`;
// Run execApplyPatch() with cwd switched to tmpDir so that the relative
// path in the patch is resolved inside the temporary location.
const prevCwd = process.cwd();
try {
process.chdir(tmpDir);
const result = execApplyPatch(patch);
expect(result.exitCode).toBe(0);
expect(result.stderr).toBe("");
} finally {
process.chdir(prevCwd);
}
// The file (and its parent directories) should have been created with the
// expected contents.
const fileContents = fs.readFileSync(nestedFileAbs, "utf8");
expect(fileContents).toBe("hello new world");
// Cleanup to keep tmpdir tidy.
fs.rmSync(tmpDir, { recursive: true, force: true });
});

View File

@@ -1,73 +0,0 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import fs from "fs";
import os from "os";
import path from "path";
import { getFileSystemSuggestions } from "../src/utils/file-system-suggestions";
vi.mock("fs");
vi.mock("os");
describe("getFileSystemSuggestions", () => {
const mockFs = fs as unknown as {
readdirSync: ReturnType<typeof vi.fn>;
statSync: ReturnType<typeof vi.fn>;
};
const mockOs = os as unknown as {
homedir: ReturnType<typeof vi.fn>;
};
beforeEach(() => {
vi.clearAllMocks();
});
it("returns empty array for empty prefix", () => {
expect(getFileSystemSuggestions("")).toEqual([]);
});
it("expands ~ to home directory", () => {
mockOs.homedir = vi.fn(() => "/home/testuser");
mockFs.readdirSync = vi.fn(() => ["file1.txt", "docs"]);
mockFs.statSync = vi.fn((p) => ({
isDirectory: () => path.basename(p) === "docs",
}));
const result = getFileSystemSuggestions("~/");
expect(mockFs.readdirSync).toHaveBeenCalledWith("/home/testuser");
expect(result).toEqual([
path.join("/home/testuser", "file1.txt"),
path.join("/home/testuser", "docs" + path.sep),
]);
});
it("filters by prefix if not a directory", () => {
mockFs.readdirSync = vi.fn(() => ["abc.txt", "abd.txt", "xyz.txt"]);
mockFs.statSync = vi.fn((p) => ({
isDirectory: () => p.includes("abd"),
}));
const result = getFileSystemSuggestions("a");
expect(result).toEqual(["abc.txt", "abd.txt/"]);
});
it("handles errors gracefully", () => {
mockFs.readdirSync = vi.fn(() => {
throw new Error("failed");
});
const result = getFileSystemSuggestions("some/path");
expect(result).toEqual([]);
});
it("normalizes relative path", () => {
mockFs.readdirSync = vi.fn(() => ["foo", "bar"]);
mockFs.statSync = vi.fn((_p) => ({
isDirectory: () => true,
}));
const result = getFileSystemSuggestions("./");
expect(result).toContain("foo/");
expect(result).toContain("bar/");
});
});

View File

@@ -1,38 +0,0 @@
import { describe, it, expect } from "vitest";
import { parse } from "shell-quote";
// The fixed requiresShell function
function requiresShell(cmd: Array<string>): boolean {
// If the command is a single string that contains shell operators,
// it needs to be run with shell: true
if (cmd.length === 1 && cmd[0] !== undefined) {
const tokens = parse(cmd[0]) as Array<any>;
return tokens.some((token) => typeof token === "object" && "op" in token);
}
// If the command is split into multiple arguments, we don't need shell: true
// even if one of the arguments is a shell operator like '|'
return false;
}
describe("fixed requiresShell function", () => {
it("should detect pipe in a single argument", () => {
const cmd = ['grep -n "finally:" some-file | head'];
expect(requiresShell(cmd)).toBe(true);
});
it("should not detect pipe in separate arguments", () => {
const cmd = ["grep", "-n", "finally:", "some-file", "|", "head"];
expect(requiresShell(cmd)).toBe(false);
});
it("should handle other shell operators in a single argument", () => {
const cmd = ["echo hello && echo world"];
expect(requiresShell(cmd)).toBe(true);
});
it("should not enable shell for normal commands", () => {
const cmd = ["ls", "-la"];
expect(requiresShell(cmd)).toBe(false);
});
});

View File

@@ -1,350 +0,0 @@
/* -------------------------------------------------------------------------- *
* Tests for the HistoryOverlay component and its formatHistoryForDisplay utility function
*
* The component displays a list of commands and files from the chat history.
* It supports two modes:
* - Command mode: shows all commands and user messages
* - File mode: shows all files that were touched
*
* The formatHistoryForDisplay function processes ResponseItems to extract:
* - Commands: User messages and function calls
* - Files: Paths referenced in commands or function calls
* -------------------------------------------------------------------------- */
import { describe, it, expect, vi } from "vitest";
import { render } from "ink-testing-library";
import React from "react";
import type {
ResponseInputMessageItem,
ResponseFunctionToolCallItem,
} from "openai/resources/responses/responses.mjs";
import HistoryOverlay from "../src/components/history-overlay";
// ---------------------------------------------------------------------------
// Module mocks *must* be registered *before* the module under test is imported
// so that Vitest can replace the dependency during evaluation.
// ---------------------------------------------------------------------------
// Mock ink's useInput to capture keyboard handlers
let keyboardHandler: ((input: string, key: any) => void) | undefined;
vi.mock("ink", async () => {
const actual = await vi.importActual("ink");
return {
...actual,
useInput: (handler: (input: string, key: any) => void) => {
keyboardHandler = handler;
},
};
});
// ---------------------------------------------------------------------------
// Test Helpers
// ---------------------------------------------------------------------------
function createUserMessage(content: string): ResponseInputMessageItem {
return {
type: "message",
role: "user",
id: `msg_${Math.random().toString(36).slice(2)}`,
content: [{ type: "input_text", text: content }],
};
}
function createFunctionCall(
name: string,
args: unknown,
): ResponseFunctionToolCallItem {
return {
type: "function_call",
name,
id: `fn_${Math.random().toString(36).slice(2)}`,
call_id: `call_${Math.random().toString(36).slice(2)}`,
arguments: JSON.stringify(args),
};
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
describe("HistoryOverlay", () => {
describe("command mode", () => {
it("displays user messages", () => {
const items = [createUserMessage("hello"), createUserMessage("world")];
const { lastFrame } = render(
<HistoryOverlay items={items} onExit={vi.fn()} />,
);
const frame = lastFrame();
expect(frame).toContain("hello");
expect(frame).toContain("world");
});
it("displays shell commands", () => {
const items = [
createFunctionCall("shell", { cmd: ["ls", "-la"] }),
createFunctionCall("shell", { cmd: ["pwd"] }),
];
const { lastFrame } = render(
<HistoryOverlay items={items} onExit={vi.fn()} />,
);
const frame = lastFrame();
expect(frame).toContain("ls -la");
expect(frame).toContain("pwd");
});
it("displays file operations", () => {
const items = [createFunctionCall("read_file", { path: "test.txt" })];
const { lastFrame } = render(
<HistoryOverlay items={items} onExit={vi.fn()} />,
);
const frame = lastFrame();
expect(frame).toContain("read_file test.txt");
});
it("displays patch operations", () => {
const items = [
createFunctionCall("shell", {
cmd: [
"apply_patch",
"*** Begin Patch\n--- a/src/file1.txt\n+++ b/src/file1.txt\n@@ -1,5 +1,5 @@\n-const x = 1;\n+const x = 2;\n",
],
}),
];
const { lastFrame } = render(
<HistoryOverlay items={items} onExit={vi.fn()} />,
);
// Verify patch is displayed in command mode
let frame = lastFrame();
expect(frame).toContain("apply_patch");
expect(frame).toContain("src/file1.txt");
// Verify file is extracted in file mode
keyboardHandler?.("f", {});
frame = lastFrame();
expect(frame).toContain("src/file1.txt");
});
it("displays mixed content in chronological order", () => {
const items = [
createUserMessage("first message"),
createFunctionCall("shell", { cmd: ["echo", "hello"] }),
createUserMessage("second message"),
];
const { lastFrame } = render(
<HistoryOverlay items={items} onExit={vi.fn()} />,
);
const frame = lastFrame();
expect(frame).toContain("first message");
expect(frame).toContain("echo hello");
expect(frame).toContain("second message");
});
it("truncates long user messages", () => {
const shortMessage = "Hello";
const longMessage =
"This is a very long message that should be truncated because it exceeds the maximum length of 120 characters. We need to make sure it gets properly truncated with the right prefix and ellipsis.";
const items = [
createUserMessage(shortMessage),
createUserMessage(longMessage),
];
const { lastFrame } = render(
<HistoryOverlay items={items} onExit={vi.fn()} />,
);
const frame = lastFrame()!;
// Short message should have the > prefix
expect(frame).toContain(`> ${shortMessage}`);
// Long message should be truncated and contain:
// 1. The > prefix
expect(frame).toContain("> This is a very long message");
// 2. An ellipsis indicating truncation
expect(frame).toContain("…");
// 3. Not contain the full message
expect(frame).not.toContain(longMessage);
// Find the truncated message line
const lines = frame.split("\n");
const truncatedLine = lines.find((line) =>
line.includes("This is a very long message"),
)!;
// Verify it's not too long (allowing for some UI elements)
expect(truncatedLine.trim().length).toBeLessThan(150);
});
});
describe("file mode", () => {
it("displays files from shell commands", () => {
const items = [
createFunctionCall("shell", { cmd: ["cat", "/path/to/file"] }),
];
const { lastFrame } = render(
<HistoryOverlay items={items} onExit={vi.fn()} />,
);
// Switch to file mode
keyboardHandler?.("f", {});
const frame = lastFrame();
expect(frame).toContain("Files touched");
expect(frame).toContain("/path/to/file");
});
it("displays files from read operations", () => {
const items = [
createFunctionCall("read_file", { path: "/path/to/file" }),
];
const { lastFrame } = render(
<HistoryOverlay items={items} onExit={vi.fn()} />,
);
// Switch to file mode
keyboardHandler?.("f", {});
const frame = lastFrame();
expect(frame).toContain("Files touched");
expect(frame).toContain("/path/to/file");
});
it("displays files from patches", () => {
const items = [
createFunctionCall("shell", {
cmd: [
"apply_patch",
"*** Begin Patch\n--- a/src/file1.txt\n+++ b/src/file1.txt\n@@ -1,5 +1,5 @@\n-const x = 1;\n+const x = 2;\n",
],
}),
];
const { lastFrame } = render(
<HistoryOverlay items={items} onExit={vi.fn()} />,
);
// Switch to file mode
keyboardHandler?.("f", {});
const frame = lastFrame();
expect(frame).toContain("Files touched");
expect(frame).toContain("src/file1.txt");
});
});
describe("keyboard interaction", () => {
it("handles mode switching with 'c' and 'f' keys", () => {
const items = [
createUserMessage("hello"),
createFunctionCall("shell", { cmd: ["cat", "src/test.txt"] }),
];
const { lastFrame } = render(
<HistoryOverlay items={items} onExit={vi.fn()} />,
);
// Initial state (command mode)
let frame = lastFrame();
expect(frame).toContain("Commands run");
expect(frame).toContain("hello");
expect(frame).toContain("cat src/test.txt");
// Switch to files mode
keyboardHandler?.("f", {});
frame = lastFrame();
expect(frame).toContain("Files touched");
expect(frame).toContain("src/test.txt");
// Switch back to commands mode
keyboardHandler?.("c", {});
frame = lastFrame();
expect(frame).toContain("Commands run");
expect(frame).toContain("hello");
expect(frame).toContain("cat src/test.txt");
});
it("handles escape key", () => {
const onExit = vi.fn();
render(<HistoryOverlay items={[]} onExit={onExit} />);
keyboardHandler?.("", { escape: true });
expect(onExit).toHaveBeenCalled();
});
it("handles arrow keys for navigation", () => {
const items = [createUserMessage("first"), createUserMessage("second")];
const { lastFrame } = render(
<HistoryOverlay items={items} onExit={vi.fn()} />,
);
// Initial state shows first item selected
let frame = lastFrame();
expect(frame).toContain(" > first");
expect(frame).not.toContain(" > second");
// Move down - second item should be selected
keyboardHandler?.("", { downArrow: true });
frame = lastFrame();
expect(frame).toContain(" > second");
expect(frame).not.toContain(" > first");
// Move up - first item should be selected again
keyboardHandler?.("", { upArrow: true });
frame = lastFrame();
expect(frame).toContain(" > first");
expect(frame).not.toContain(" > second");
});
it("handles page up/down navigation", () => {
const items = Array.from({ length: 12 }, (_, i) =>
createUserMessage(`message ${i + 1}`),
);
const { lastFrame } = render(
<HistoryOverlay items={items} onExit={vi.fn()} />,
);
// Initial position - first message selected
let frame = lastFrame();
expect(frame).toMatch(/│ > message 1\s+│/); // message 1 should be selected
expect(frame).toMatch(/│ {3}> message 11\s+│/); // message 11 should be visible but not selected
// Page down moves by 10 - message 11 should be selected
keyboardHandler?.("", { pageDown: true });
frame = lastFrame();
expect(frame).toMatch(/│ {3}> message 1\s+│/); // message 1 should be visible but not selected
expect(frame).toMatch(/│ > message 11\s+│/); // message 11 should be selected
});
it("handles vim-style navigation", () => {
const items = [
createUserMessage("first"),
createUserMessage("second"),
createUserMessage("third"),
];
const { lastFrame } = render(
<HistoryOverlay items={items} onExit={vi.fn()} />,
);
// Initial state should show first item selected
let frame = lastFrame();
expect(frame).toContain(" > first");
expect(frame).not.toContain(" > third"); // Make sure third is not selected initially
// Test G to jump to end - third should be selected
keyboardHandler?.("G", {});
frame = lastFrame();
expect(frame).toContain(" > third");
// Test g to jump to beginning - first should be selected again
keyboardHandler?.("g", {});
frame = lastFrame();
expect(frame).toContain(" > first");
});
});
describe("error handling", () => {
it("handles empty or invalid items", () => {
const items = [{ type: "invalid" } as any, null as any, undefined as any];
const { lastFrame } = render(
<HistoryOverlay items={items} onExit={vi.fn()} />,
);
// Should render without errors
expect(lastFrame()).toBeTruthy();
});
});
});

View File

@@ -1,47 +0,0 @@
import { describe, it, expect, vi } from "vitest";
import fs from "fs/promises";
import { createInputItem } from "../src/utils/input-utils.js";
describe("createInputItem", () => {
it("returns only text when no images provided", async () => {
const result = await createInputItem("hello", []);
expect(result).toEqual({
role: "user",
type: "message",
content: [{ type: "input_text", text: "hello" }],
});
});
it("includes image content for existing file", async () => {
const fakeBuffer = Buffer.from("fake image content");
const readSpy = vi
.spyOn(fs, "readFile")
.mockResolvedValue(fakeBuffer as any);
const result = await createInputItem("hello", ["dummy-path"]);
const expectedUrl = `data:application/octet-stream;base64,${fakeBuffer.toString(
"base64",
)}`;
expect(result.role).toBe("user");
expect(result.type).toBe("message");
expect(result.content.length).toBe(2);
const [textItem, imageItem] = result.content;
expect(textItem).toEqual({ type: "input_text", text: "hello" });
expect(imageItem).toEqual({
type: "input_image",
detail: "auto",
image_url: expectedUrl,
});
readSpy.mockRestore();
});
it("falls back to missing image text for non-existent file", async () => {
const filePath = "tests/__fixtures__/does-not-exist.png";
const result = await createInputItem("hello", [filePath]);
expect(result.content.length).toBe(2);
const fallbackItem = result.content[1];
expect(fallbackItem).toEqual({
type: "input_text",
text: "[missing image: does-not-exist.png]",
});
});
});

View File

@@ -1,19 +0,0 @@
import { describe, expect, test } from "vitest";
import { openAiModelInfo } from "../src/utils/model-info";
describe("Model Info", () => {
test("supportedModelInfo contains expected models", () => {
expect(openAiModelInfo).toHaveProperty("gpt-4o");
expect(openAiModelInfo).toHaveProperty("gpt-4.1");
expect(openAiModelInfo).toHaveProperty("o3");
});
test("model info entries have required properties", () => {
Object.entries(openAiModelInfo).forEach(([_, info]) => {
expect(info).toHaveProperty("label");
expect(info).toHaveProperty("maxContextLength");
expect(typeof info.label).toBe("string");
expect(typeof info.maxContextLength).toBe("number");
});
});
});

View File

@@ -44,7 +44,7 @@ describe("model-utils offline resilience", () => {
"../src/utils/model-utils.js"
);
const supported = await isModelSupportedForResponses("openai", "o4-mini");
const supported = await isModelSupportedForResponses("o4-mini");
expect(supported).toBe(true);
});
@@ -63,11 +63,8 @@ describe("model-utils offline resilience", () => {
"../src/utils/model-utils.js"
);
// Should resolve true despite the network failure.
const supported = await isModelSupportedForResponses(
"openai",
"some-model",
);
// Should resolve true despite the network failure
const supported = await isModelSupportedForResponses("some-model");
expect(supported).toBe(true);
});
});

View File

@@ -1,78 +0,0 @@
import { describe, test, expect } from "vitest";
import {
calculateContextPercentRemaining,
maxTokensForModel,
} from "../src/utils/model-utils";
import { openAiModelInfo } from "../src/utils/model-info";
import type { ResponseItem } from "openai/resources/responses/responses.mjs";
describe("Model Utils", () => {
describe("openAiModelInfo", () => {
test("model info entries have required properties", () => {
Object.entries(openAiModelInfo).forEach(([_, info]) => {
expect(info).toHaveProperty("label");
expect(info).toHaveProperty("maxContextLength");
expect(typeof info.label).toBe("string");
expect(typeof info.maxContextLength).toBe("number");
});
});
});
describe("maxTokensForModel", () => {
test("returns correct token limit for known models", () => {
const knownModel = "gpt-4o";
const expectedTokens = openAiModelInfo[knownModel].maxContextLength;
expect(maxTokensForModel(knownModel)).toBe(expectedTokens);
});
test("handles models with size indicators in their names", () => {
expect(maxTokensForModel("some-model-32k")).toBe(32000);
expect(maxTokensForModel("some-model-16k")).toBe(16000);
expect(maxTokensForModel("some-model-8k")).toBe(8000);
expect(maxTokensForModel("some-model-4k")).toBe(4000);
});
test("defaults to 128k for unknown models not in the registry", () => {
expect(maxTokensForModel("completely-unknown-model")).toBe(128000);
});
});
describe("calculateContextPercentRemaining", () => {
test("returns 100% for empty items", () => {
const result = calculateContextPercentRemaining([], "gpt-4o");
expect(result).toBe(100);
});
test("calculates percentage correctly for non-empty items", () => {
const mockItems: Array<ResponseItem> = [
{
id: "test-id",
type: "message",
role: "user",
status: "completed",
content: [
{
type: "input_text",
text: "A".repeat(
openAiModelInfo["gpt-4o"].maxContextLength * 0.25 * 4,
),
},
],
} as ResponseItem,
];
const result = calculateContextPercentRemaining(mockItems, "gpt-4o");
expect(result).toBeCloseTo(75, 0);
});
test("handles models that are not in the registry", () => {
const mockItems: Array<ResponseItem> = [];
const result = calculateContextPercentRemaining(
mockItems,
"unknown-model",
);
expect(result).toBe(100);
});
});
});

View File

@@ -57,7 +57,7 @@ async function type(
await flush();
}
/** Build a set of no-op callbacks so <TerminalChatInput> renders with minimal
/** Build a set of noop callbacks so <TerminalChatInput> renders with minimal
* scaffolding.
*/
function stubProps(): any {

View File

@@ -1,49 +0,0 @@
// Regression test: Terminals with modifyOtherKeys=1 emit CSI~ sequence for
// Shift+Enter: ESC [ 27 ; mod ; 13 ~. The editor must treat Shift+Enter as
// newline (without submitting) and Ctrl+Enter as submit.
import { renderTui } from "./ui-test-helpers.js";
import MultilineTextEditor from "../src/components/chat/multiline-editor.js";
import * as React from "react";
import { describe, it, expect, vi } from "vitest";
async function type(
stdin: NodeJS.WritableStream,
text: string,
flush: () => Promise<void>,
) {
stdin.write(text);
await flush();
}
describe("MultilineTextEditor Shift+Enter with modifyOtherKeys=1", () => {
it("inserts newline, does NOT submit", async () => {
const onSubmit = vi.fn();
const { stdin, lastFrameStripped, flush, cleanup } = renderTui(
React.createElement(MultilineTextEditor, {
height: 5,
width: 20,
initialText: "",
onSubmit,
}),
);
await flush();
await type(stdin, "abc", flush);
// Shift+Enter => ESC [27;2;13~
await type(stdin, "\u001B[27;2;13~", flush);
await type(stdin, "def", flush);
const frame = lastFrameStripped();
expect(frame).toMatch(/abc/);
expect(frame).toMatch(/def/);
// newline inserted -> at least 2 lines
expect(frame.split("\n").length).toBeGreaterThanOrEqual(2);
expect(onSubmit).not.toHaveBeenCalled();
cleanup();
});
});

View File

@@ -1,66 +0,0 @@
import { describe, it, expect, beforeEach, vi, afterEach } from "vitest";
import which from "which";
import { detectInstallerByPath } from "../src/utils/package-manager-detector";
import { execFileSync } from "node:child_process";
vi.mock("which", () => ({
default: { sync: vi.fn() },
}));
vi.mock("node:child_process", () => ({ execFileSync: vi.fn() }));
describe("detectInstallerByPath()", () => {
const originalArgv = process.argv;
const fakeBinDirs = {
// `npm prefix -g` returns the global “prefix” (well add `/bin` when detecting)
npm: "/usr/local",
pnpm: "/home/user/.local/share/pnpm/bin",
bun: "/Users/test/.bun/bin",
} as const;
beforeEach(() => {
vi.resetAllMocks();
// Pretend each manager binary is on PATH:
vi.mocked(which.sync).mockImplementation(() => "/fake/path");
vi.mocked(execFileSync).mockImplementation(
(
cmd: string,
_args: ReadonlyArray<string> = [],
_options: unknown,
): string => {
return fakeBinDirs[cmd as keyof typeof fakeBinDirs];
},
);
});
afterEach(() => {
// Restore the real argv so tests dont leak
process.argv = originalArgv;
});
it.each(Object.entries(fakeBinDirs))(
"detects %s when invoked from its global-bin",
async (manager, binDir) => {
// Simulate the shim living under that binDir
process.argv =
manager === "npm"
? [process.argv[0]!, `${binDir}/bin/my-cli`]
: [process.argv[0]!, `${binDir}/my-cli`];
const detected = await detectInstallerByPath();
expect(detected).toBe(manager);
},
);
it("returns undefined if argv[1] is missing", async () => {
process.argv = [process.argv[0]!];
expect(await detectInstallerByPath()).toBeUndefined();
expect(execFileSync).not.toHaveBeenCalled();
});
it("returns undefined if shim isn't in any manager's bin", async () => {
// stub execFileSync to some other dirs
vi.mocked(execFileSync).mockImplementation(() => "/some/other/dir");
process.argv = [process.argv[0]!, "/home/user/.node_modules/.bin/my-cli"];
expect(await detectInstallerByPath()).toBeUndefined();
});
});

View File

@@ -1,19 +0,0 @@
import { describe, it, expect } from "vitest";
import { parse } from "shell-quote";
/* eslint-disable no-console */
describe("shell-quote parse with pipes", () => {
it("should correctly parse a command with a pipe", () => {
const cmd = 'grep -n "finally:" some-file | head';
const tokens = parse(cmd);
console.log("Parsed tokens:", JSON.stringify(tokens, null, 2));
// Check if any token has an 'op' property
const hasOpToken = tokens.some(
(token) => typeof token === "object" && "op" in token,
);
expect(hasOpToken).toBe(true);
});
});

View File

@@ -6,6 +6,7 @@ import { exec as rawExec } from "../src/utils/agent/sandbox/raw-exec.js";
// the direct child. The original logic only sent `SIGTERM` to the immediate
// child which meant that grandchildren (for instance when running through a
// `bash -c` wrapper) were left running and turned into "zombie" processes.
// Strategy:
// 1. Start a Bash shell that spawns a longrunning `sleep`, prints the PID
// of that `sleep`, and then waits forever. This guarantees we can later
@@ -14,6 +15,7 @@ import { exec as rawExec } from "../src/utils/agent/sandbox/raw-exec.js";
// 3. After `rawExec()` resolves we probe the previously printed PID with
// `process.kill(pid, 0)`. If the call throws `ESRCH` the process no
// longer exists the desired outcome. Otherwise the test fails.
// The negativePID processgroup trick employed by the fixed implementation is
// POSIXonly. On Windows we skip the test.
@@ -24,59 +26,49 @@ describe("rawExec abort kills entire process group", () => {
}
const abortController = new AbortController();
// Bash script: spawn `sleep 30` in background, print its PID, then wait.
const script = "sleep 30 & pid=$!; echo $pid; wait $pid";
const cmd = ["bash", "-c", script];
// Start a bash shell that:
// - spawns a background `sleep 30`
// - prints the PID of the `sleep`
// - waits for `sleep` to exit
const { stdout, exitCode } = await (async () => {
const p = rawExec(cmd, {}, [], abortController.signal);
// Kick off the command.
const execPromise = rawExec(cmd, {}, [], abortController.signal);
// Give Bash a tiny bit of time to start and print the PID.
await new Promise((r) => setTimeout(r, 100));
// Give Bash a tiny bit of time to start and print the PID.
await new Promise((r) => setTimeout(r, 100));
// Cancel the task this should kill *both* bash and the inner sleep.
abortController.abort();
// Cancel the task this should kill *both* bash and the inner sleep.
abortController.abort();
// Wait for rawExec to resolve after aborting
return p;
})();
const { exitCode, stdout } = await execPromise;
// We expect a nonzero exit code because the process was killed.
expect(exitCode).not.toBe(0);
// Extract the PID of the sleep process that bash printed
const pid = Number(stdout.trim().match(/^\d+/)?.[0]);
if (pid) {
// Confirm that the sleep process is no longer alive
await ensureProcessGone(pid);
// Attempt to extract the grandchild PID from stdout.
const pidMatch = /^(\d+)/.exec(stdout.trim());
if (pidMatch) {
const sleepPid = Number(pidMatch[1]);
// Verify that the sleep process is no longer alive.
let alive = true;
try {
process.kill(sleepPid, 0);
} catch (error: any) {
// Check if error is ESRCH (No such process)
if (error.code === "ESRCH") {
alive = false; // Process is dead, as expected.
} else {
throw error;
}
}
expect(alive).toBe(false);
} else {
// If PID was not printed, it implies bash was killed very early.
// The test passes implicitly in this scenario as the abort mechanism
// successfully stopped the command execution quickly.
expect(true).toBe(true);
}
});
});
/**
* Waits until a process no longer exists, or throws after timeout.
* @param pid - The process ID to check
* @throws {Error} If the process is still alive after 500ms
*/
async function ensureProcessGone(pid: number) {
const timeout = 500;
const deadline = Date.now() + timeout;
while (Date.now() < deadline) {
try {
process.kill(pid, 0); // check if process still exists
await new Promise((r) => setTimeout(r, 50)); // wait and retry
} catch (e: any) {
if (e.code === "ESRCH") {
return; // process is gone — success
}
throw e; // unexpected error — rethrow
}
}
throw new Error(
`Process with PID ${pid} failed to terminate within ${timeout}ms`,
);
}

Some files were not shown because too many files have changed in this diff Show More