Skip to content

Commit bf39131

Browse files
authored
Merge branch 'main' into gemini-MCP-config
2 parents 6a02257 + 6b8d89d commit bf39131

8 files changed

Lines changed: 746 additions & 1303 deletions

File tree

Lines changed: 79 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,149 +1,107 @@
11
---
22
display_name: Codex CLI
33
icon: ../../../../.icons/openai.svg
4-
description: Run Codex CLI in your workspace with AgentAPI integration
4+
description: Install and configure the Codex CLI in your workspace.
55
verified: true
6-
tags: [agent, codex, ai, openai, tasks, aibridge]
6+
tags: [agent, codex, ai, openai, ai-gateway]
77
---
88

99
# Codex CLI
1010

11-
Run Codex CLI in your workspace to access OpenAI's models through the Codex interface, with custom pre/post install scripts. This module integrates with [AgentAPI](https://github.com/coder/agentapi) for Coder Tasks compatibility.
11+
Install and configure the [Codex CLI](https://github.com/openai/codex) in your workspace.
1212

1313
```tf
1414
module "codex" {
1515
source = "registry.coder.com/coder-labs/codex/coder"
16-
version = "4.3.1"
17-
agent_id = coder_agent.example.id
16+
version = "5.0.0"
17+
agent_id = coder_agent.main.id
1818
openai_api_key = var.openai_api_key
19-
workdir = "/home/coder/project"
2019
}
2120
```
2221

23-
## Prerequisites
24-
25-
- OpenAI API key for Codex access
22+
> [!WARNING]
23+
> If upgrading from v4.x.x of this module: v5 is a major refactor that drops support for [Coder Tasks](https://coder.com/docs/ai-coder/tasks) and [Boundary](https://coder.com/docs/ai-coder/agent-firewall). v5 also assumes npm is pre-installed; it no longer bootstraps Node.js. Keep using v4.x.x if you depend on them. See the [PR description](https://github.com/coder/registry/pull/879) for a full migration guide.
2624
2725
## Examples
2826

29-
### Run standalone
27+
### Standalone mode with a launcher app
3028

3129
```tf
32-
module "codex" {
33-
count = data.coder_workspace.me.start_count
34-
source = "registry.coder.com/coder-labs/codex/coder"
35-
version = "4.3.1"
36-
agent_id = coder_agent.example.id
37-
openai_api_key = "..."
38-
workdir = "/home/coder/project"
39-
report_tasks = false
30+
locals {
31+
codex_workdir = "/home/coder/project"
4032
}
41-
```
4233
43-
### Usage with AI Bridge
44-
45-
[AI Bridge](https://coder.com/docs/ai-coder/ai-bridge) is a Premium Coder feature that provides centralized LLM proxy management. To use AI Bridge, set `enable_aibridge = true`. Requires Coder version 2.30+
46-
47-
For tasks integration with AI Bridge, add `enable_aibridge = true` to the [Usage with Tasks](#usage-with-tasks) example below.
48-
49-
#### Standalone usage with AI Bridge
50-
51-
```tf
5234
module "codex" {
53-
source = "registry.coder.com/coder-labs/codex/coder"
54-
version = "4.3.1"
55-
agent_id = coder_agent.example.id
56-
workdir = "/home/coder/project"
57-
enable_aibridge = true
35+
source = "registry.coder.com/coder-labs/codex/coder"
36+
version = "5.0.0"
37+
agent_id = coder_agent.main.id
38+
workdir = local.codex_workdir
39+
openai_api_key = var.openai_api_key
5840
}
59-
```
60-
61-
When `enable_aibridge = true`, the module:
62-
63-
- Configures Codex to use the aibridge model_provider with `base_url` pointing to `${data.coder_workspace.me.access_url}/api/v2/aibridge/openai/v1` and `env_key` pointing to the workspace owner's session token
6441
65-
```toml
66-
model_provider = "aibridge"
67-
68-
[model_providers.aibridge]
69-
name = "AI Bridge"
70-
base_url = "https://example.coder.com/api/v2/aibridge/openai/v1"
71-
env_key = "CODER_AIBRIDGE_SESSION_TOKEN"
72-
wire_api = "responses"
42+
resource "coder_app" "codex" {
43+
agent_id = coder_agent.main.id
44+
slug = "codex"
45+
display_name = "Codex"
46+
icon = "/icon/openai.svg"
47+
open_in = "slim-window"
48+
command = <<-EOT
49+
#!/bin/bash
50+
set -e
51+
cd "${local.codex_workdir}"
52+
codex
53+
EOT
54+
}
7355
```
7456

75-
This allows Codex to route API requests through Coder's AI Bridge instead of directly to OpenAI's API.
76-
Template build will fail if `openai_api_key` is provided alongside `enable_aibridge = true`.
57+
> [!NOTE]
58+
> The `coder_app` command re-executes on every pane reconnect. This works for interactive `codex` (which stays alive), but one-shot commands like `codex exec` will re-run each time. For one-shot prompts, use a `coder_script` (runs once at startup) and a `coder_app` that attaches to the existing session (e.g. via tmux/screen).
7759
78-
### Usage with Tasks
60+
### Usage with AI Gateway
7961

80-
This example shows how to configure Codex with Coder tasks.
62+
[AI Gateway](https://coder.com/docs/ai-coder/ai-gateway) is a Premium Coder feature that provides centralized LLM proxy management. Requires Coder >= 2.30.0.
8163

8264
```tf
83-
resource "coder_ai_task" "task" {
84-
count = data.coder_workspace.me.start_count
85-
app_id = module.codex.task_app_id
86-
}
87-
88-
data "coder_task" "me" {}
89-
9065
module "codex" {
91-
source = "registry.coder.com/coder-labs/codex/coder"
92-
version = "4.3.1"
93-
agent_id = coder_agent.example.id
94-
openai_api_key = "..."
95-
ai_prompt = data.coder_task.me.prompt
96-
workdir = "/home/coder/project"
97-
98-
# Optional: route through AI Bridge (Premium feature)
99-
# enable_aibridge = true
66+
source = "registry.coder.com/coder-labs/codex/coder"
67+
version = "5.0.0"
68+
agent_id = coder_agent.main.id
69+
workdir = "/home/coder/project"
70+
enable_ai_gateway = true
10071
}
10172
```
10273

103-
### Usage with Agent Boundaries
104-
105-
This example shows how to configure the Codex module to run the agent behind a process-level boundary that restricts its network access.
74+
When `enable_ai_gateway = true`, the module configures Codex to use the `aigateway` model provider in `config.toml` with the workspace owner's session token for authentication.
10675

107-
By default, when `enable_boundary = true`, the module uses `coder boundary` subcommand (provided by Coder) without requiring any installation.
108-
109-
```tf
110-
module "codex" {
111-
source = "registry.coder.com/coder-labs/codex/coder"
112-
version = "4.3.1"
113-
agent_id = coder_agent.main.id
114-
openai_api_key = var.openai_api_key
115-
workdir = "/home/coder/project"
116-
enable_boundary = true
117-
}
118-
```
76+
> [!CAUTION]
77+
> `enable_ai_gateway = true` is mutually exclusive with `openai_api_key`. Setting both fails at plan time.
11978
12079
> [!NOTE]
121-
> For developers: The module also supports installing boundary from a release version (`use_boundary_directly = true`) or compiling from source (`compile_boundary_from_source = true`). These are escape hatches for development and testing purposes.
80+
> If you provide a custom `base_config_toml`, the module writes it verbatim and does not inject `model_provider = "aigateway"` automatically. Add it to your config yourself:
81+
>
82+
> ```toml
83+
> model_provider = "aigateway"
84+
> ```
12285
12386
### Advanced Configuration
12487
125-
This example shows additional configuration options for custom models, MCP servers, and base configuration.
126-
12788
```tf
12889
module "codex" {
12990
source = "registry.coder.com/coder-labs/codex/coder"
130-
version = "4.3.1"
131-
agent_id = coder_agent.example.id
132-
openai_api_key = "..."
91+
version = "5.0.0"
92+
agent_id = coder_agent.main.id
13393
workdir = "/home/coder/project"
94+
openai_api_key = var.openai_api_key
13495
135-
codex_version = "0.1.0" # Pin to a specific version
136-
codex_model = "gpt-4o" # Custom model
96+
codex_version = "0.128.0"
13797
138-
# Override default configuration
13998
base_config_toml = <<-EOT
14099
sandbox_mode = "danger-full-access"
141100
approval_policy = "never"
142101
preferred_auth_method = "apikey"
143102
EOT
144103
145-
# Add extra MCP servers
146-
additional_mcp_servers = <<-EOT
104+
mcp = <<-EOT
147105
[mcp_servers.GitHub]
148106
command = "npx"
149107
args = ["-y", "@modelcontextprotocol/server-github"]
@@ -152,61 +110,49 @@ module "codex" {
152110
}
153111
```
154112
155-
> [!WARNING]
156-
> This module configures Codex with a `workspace-write` sandbox that allows AI tasks to read/write files in the specified workdir. While the sandbox provides security boundaries, Codex can still modify files within the workspace. Use this module _only_ in trusted environments and be aware of the security implications.
157-
158-
## How it Works
159-
160-
- **Install**: The module installs Codex CLI and sets up the environment
161-
- **System Prompt**: If `codex_system_prompt` is set, writes the prompt to `AGENTS.md` in the `~/.codex/` directory
162-
- **Start**: Launches Codex CLI in the specified directory, wrapped by AgentAPI
163-
- **Configuration**: Sets `OPENAI_API_KEY` environment variable and passes `--model` flag to Codex CLI (if variables provided)
164-
- **Session Continuity**: When `continue = true` (default), the module automatically tracks task sessions in `~/.codex-module/.codex-task-session`. On workspace restart, it resumes the existing session with full conversation history. Set `continue = false` to always start fresh sessions.
165-
166-
## State Persistence
113+
### Serialize a downstream `coder_script` after the install pipeline
167114

168-
AgentAPI can save and restore its conversation state to disk across workspace restarts. This complements `continue` (which resumes the Codex CLI session) by also preserving the AgentAPI-level context. Enabled by default, requires agentapi >= v0.12.0 (older versions skip it with a warning).
169-
170-
To disable:
115+
The module exposes the `scripts` output: an ordered list of `coder exp sync` names for the scripts this module creates (pre_install, install, post_install). Scripts that were not configured are absent.
171116

172117
```tf
173118
module "codex" {
174-
# ... other config
175-
enable_state_persistence = false
119+
source = "registry.coder.com/coder-labs/codex/coder"
120+
version = "5.0.0"
121+
agent_id = coder_agent.main.id
122+
openai_api_key = var.openai_api_key
123+
}
124+
125+
resource "coder_script" "post_codex" {
126+
agent_id = coder_agent.main.id
127+
display_name = "Run after Codex install"
128+
run_on_start = true
129+
script = <<-EOT
130+
#!/bin/bash
131+
set -euo pipefail
132+
trap 'coder exp sync complete post-codex' EXIT
133+
coder exp sync want post-codex ${join(" ", module.codex.scripts)}
134+
coder exp sync start post-codex
135+
136+
codex --version
137+
EOT
176138
}
177139
```
178140

179141
## Configuration
180142

181-
### Default Configuration
182-
183-
When no custom `base_config_toml` is provided, the module uses these secure defaults:
184-
185-
```toml
186-
sandbox_mode = "workspace-write"
187-
approval_policy = "never"
188-
preferred_auth_method = "apikey"
189-
190-
[sandbox_workspace_write]
191-
network_access = true
192-
```
193-
194-
> [!NOTE]
195-
> If no custom configuration is provided, the module uses secure defaults. The Coder MCP server is always included automatically. For containerized workspaces (Docker/Kubernetes), you may need `sandbox_mode = "danger-full-access"` to avoid permission issues. For advanced options, see [Codex config docs](https://github.com/openai/codex/blob/main/codex-rs/config.md).
143+
When no custom `base_config_toml` is provided, the module uses a minimal default with `preferred_auth_method = "apikey"`. For advanced options, see [Codex config docs](https://developers.openai.com/codex/config-advanced).
196144

197145
## Troubleshooting
198146

199-
- Check installation and startup logs in `~/.codex-module/`
200-
- Ensure your OpenAI API key has access to the specified model
147+
Check the log files in `~/.coder-modules/coder-labs/codex/logs/` for detailed information.
201148

202-
> [!IMPORTANT]
203-
> To use tasks with Codex CLI, ensure you have the `openai_api_key` variable set. [Tasks Template Example](https://registry.coder.com/templates/coder-labs/tasks-docker).
204-
> The module automatically configures Codex with your API key and model preferences.
205-
> workdir is a required variable for the module to function correctly.
149+
```bash
150+
cat ~/.coder-modules/coder-labs/codex/logs/install.log
151+
cat ~/.coder-modules/coder-labs/codex/logs/pre_install.log
152+
cat ~/.coder-modules/coder-labs/codex/logs/post_install.log
153+
```
206154

207155
## References
208156

209157
- [Codex CLI Documentation](https://github.com/openai/codex)
210-
- [AgentAPI Documentation](https://github.com/coder/agentapi)
211-
- [Coder AI Agents Guide](https://coder.com/docs/tutorials/ai-agents)
212-
- [AI Bridge](https://coder.com/docs/ai-coder/ai-bridge)
158+
- [AI Gateway](https://coder.com/docs/ai-coder/ai-gateway)

0 commit comments

Comments
 (0)