Skip to content

Commit ca7bc42

Browse files
feat: update auth setup in codex (#472)
Closes # ## Description <!-- Briefly describe what this PR does and why --> ## Type of Change - [ ] New module - [x] Bug fix - [x] Feature/enhancement - [ ] Documentation - [ ] Other ## Module Information <!-- Delete this section if not applicable --> **Path:** `registry/coder-labs/modules/codex` **New version:** `v3.0.0` **Breaking change:** [X] Yes [ ] No ## Testing & Validation - [X] Tests pass (`bun test`) - [X] Code formatted (`bun run fmt`) - [X] Changes tested locally ## Related Issues <!-- Link related issues or write "None" if not applicable --> --------- Co-authored-by: DevCats <christofer@coder.com>
1 parent a599302 commit ca7bc42

File tree

5 files changed

+88
-23
lines changed

5 files changed

+88
-23
lines changed

registry/coder-labs/modules/codex/README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ Run Codex CLI in your workspace to access OpenAI's models through the Codex inte
1313
```tf
1414
module "codex" {
1515
source = "registry.coder.com/coder-labs/codex/coder"
16-
version = "2.1.1"
16+
version = "3.0.0"
1717
agent_id = coder_agent.example.id
1818
openai_api_key = var.openai_api_key
19-
folder = "/home/coder/project"
19+
workdir = "/home/coder/project"
2020
}
2121
```
2222

@@ -33,10 +33,11 @@ module "codex" {
3333
module "codex" {
3434
count = data.coder_workspace.me.start_count
3535
source = "registry.coder.com/coder-labs/codex/coder"
36-
version = "2.1.1"
36+
version = "3.0.0"
3737
agent_id = coder_agent.example.id
3838
openai_api_key = "..."
39-
folder = "/home/coder/project"
39+
workdir = "/home/coder/project"
40+
report_tasks = false
4041
}
4142
```
4243

@@ -60,11 +61,11 @@ module "coder-login" {
6061
6162
module "codex" {
6263
source = "registry.coder.com/coder-labs/codex/coder"
63-
version = "2.1.1"
64+
version = "3.0.0"
6465
agent_id = coder_agent.example.id
6566
openai_api_key = "..."
6667
ai_prompt = data.coder_parameter.ai_prompt.value
67-
folder = "/home/coder/project"
68+
workdir = "/home/coder/project"
6869
6970
# Custom configuration for full auto mode
7071
base_config_toml = <<-EOT
@@ -75,7 +76,7 @@ module "codex" {
7576
```
7677

7778
> [!WARNING]
78-
> This module configures Codex with a `workspace-write` sandbox that allows AI tasks to read/write files in the specified folder. 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.
79+
> 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.
7980
8081
## How it Works
8182

@@ -106,7 +107,7 @@ For custom Codex configuration, use `base_config_toml` and/or `additional_mcp_se
106107
```tf
107108
module "codex" {
108109
source = "registry.coder.com/coder-labs/codex/coder"
109-
version = "2.1.1"
110+
version = "3.0.0"
110111
# ... other variables ...
111112
112113
# Override default configuration
@@ -137,7 +138,7 @@ module "codex" {
137138
> [!IMPORTANT]
138139
> To use tasks with Codex CLI, ensure you have the `openai_api_key` variable set, and **you create a `coder_parameter` named `"AI Prompt"` and pass its value to the codex module's `ai_prompt` variable**. [Tasks Template Example](https://registry.coder.com/templates/coder-labs/tasks-docker).
139140
> The module automatically configures Codex with your API key and model preferences.
140-
> folder is a required variable for the module to function correctly.
141+
> workdir is a required variable for the module to function correctly.
141142
142143
## References
143144

registry/coder-labs/modules/codex/main.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const setup = async (props?: SetupProps): Promise<{ id: string }> => {
4747
install_codex: props?.skipCodexMock ? "true" : "false",
4848
install_agentapi: props?.skipAgentAPIMock ? "true" : "false",
4949
codex_model: "gpt-4-turbo",
50-
folder: "/home/coder",
50+
workdir: "/home/coder",
5151
...props?.moduleVariables,
5252
},
5353
registerCleanup,
@@ -166,20 +166,20 @@ describe("codex", async () => {
166166
expect(postInstallLog).toContain("post-install-script");
167167
});
168168

169-
test("folder-variable", async () => {
170-
const folder = "/tmp/codex-test-folder";
169+
test("workdir-variable", async () => {
170+
const workdir = "/tmp/codex-test-workdir";
171171
const { id } = await setup({
172172
skipCodexMock: false,
173173
moduleVariables: {
174-
folder,
174+
workdir,
175175
},
176176
});
177177
await execModuleScript(id);
178178
const resp = await readFileContainer(
179179
id,
180180
"/home/coder/.codex-module/install.log",
181181
);
182-
expect(resp).toContain(folder);
182+
expect(resp).toContain(workdir);
183183
});
184184

185185
test("additional-mcp-servers", async () => {

registry/coder-labs/modules/codex/main.tf

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,41 @@ variable "icon" {
3636
default = "/icon/openai.svg"
3737
}
3838

39-
variable "folder" {
39+
variable "workdir" {
4040
type = string
4141
description = "The folder to run Codex in."
4242
}
4343

44+
variable "report_tasks" {
45+
type = bool
46+
description = "Whether to enable task reporting to Coder UI via AgentAPI"
47+
default = true
48+
}
49+
50+
variable "subdomain" {
51+
type = bool
52+
description = "Whether to use a subdomain for AgentAPI."
53+
default = false
54+
}
55+
56+
variable "cli_app" {
57+
type = bool
58+
description = "Whether to create a CLI app for Codex"
59+
default = false
60+
}
61+
62+
variable "web_app_display_name" {
63+
type = string
64+
description = "Display name for the web app"
65+
default = "Codex"
66+
}
67+
68+
variable "cli_app_display_name" {
69+
type = string
70+
description = "Display name for the CLI app"
71+
default = "Codex CLI"
72+
}
73+
4474
variable "install_codex" {
4575
type = bool
4676
description = "Whether to install Codex."
@@ -120,6 +150,7 @@ resource "coder_env" "openai_api_key" {
120150
}
121151

122152
locals {
153+
workdir = trimsuffix(var.workdir, "/")
123154
app_slug = "codex"
124155
install_script = file("${path.module}/scripts/install.sh")
125156
start_script = file("${path.module}/scripts/start.sh")
@@ -131,16 +162,18 @@ module "agentapi" {
131162
version = "1.2.0"
132163

133164
agent_id = var.agent_id
134-
folder = var.folder
165+
folder = local.workdir
135166
web_app_slug = local.app_slug
136167
web_app_order = var.order
137168
web_app_group = var.group
138169
web_app_icon = var.icon
139-
web_app_display_name = "Codex"
140-
cli_app_slug = "${local.app_slug}-cli"
141-
cli_app_display_name = "Codex CLI"
170+
web_app_display_name = var.web_app_display_name
171+
cli_app = var.cli_app
172+
cli_app_slug = var.cli_app ? "${local.app_slug}-cli" : null
173+
cli_app_display_name = var.cli_app ? var.cli_app_display_name : null
142174
module_dir_name = local.module_dir_name
143175
install_agentapi = var.install_agentapi
176+
agentapi_subdomain = var.subdomain
144177
agentapi_version = var.agentapi_version
145178
pre_install_script = var.pre_install_script
146179
post_install_script = var.post_install_script
@@ -152,8 +185,9 @@ module "agentapi" {
152185
echo -n '${base64encode(local.start_script)}' | base64 -d > /tmp/start.sh
153186
chmod +x /tmp/start.sh
154187
ARG_OPENAI_API_KEY='${var.openai_api_key}' \
188+
ARG_REPORT_TASKS='${var.report_tasks}' \
155189
ARG_CODEX_MODEL='${var.codex_model}' \
156-
ARG_CODEX_START_DIRECTORY='${var.folder}' \
190+
ARG_CODEX_START_DIRECTORY='${var.workdir}' \
157191
ARG_CODEX_TASK_PROMPT='${base64encode(var.ai_prompt)}' \
158192
/tmp/start.sh
159193
EOT
@@ -165,12 +199,14 @@ module "agentapi" {
165199
166200
echo -n '${base64encode(local.install_script)}' | base64 -d > /tmp/install.sh
167201
chmod +x /tmp/install.sh
202+
ARG_OPENAI_API_KEY='${var.openai_api_key}' \
203+
ARG_REPORT_TASKS='${var.report_tasks}' \
168204
ARG_INSTALL='${var.install_codex}' \
169205
ARG_CODEX_VERSION='${var.codex_version}' \
170206
ARG_BASE_CONFIG_TOML='${base64encode(var.base_config_toml)}' \
171207
ARG_ADDITIONAL_MCP_SERVERS='${base64encode(var.additional_mcp_servers)}' \
172208
ARG_CODER_MCP_APP_STATUS_SLUG='${local.app_slug}' \
173-
ARG_CODEX_START_DIRECTORY='${var.folder}' \
209+
ARG_CODEX_START_DIRECTORY='${var.workdir}' \
174210
ARG_CODEX_INSTRUCTION_PROMPT='${base64encode(var.codex_system_prompt)}' \
175211
/tmp/install.sh
176212
EOT

registry/coder-labs/modules/codex/scripts/install.sh

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ printf "Start Directory: %s\n" "$ARG_CODEX_START_DIRECTORY"
2222
printf "Has Base Config: %s\n" "$([ -n "$ARG_BASE_CONFIG_TOML" ] && echo "Yes" || echo "No")"
2323
printf "Has Additional MCP: %s\n" "$([ -n "$ARG_ADDITIONAL_MCP_SERVERS" ] && echo "Yes" || echo "No")"
2424
printf "Has System Prompt: %s\n" "$([ -n "$ARG_CODEX_INSTRUCTION_PROMPT" ] && echo "Yes" || echo "No")"
25+
printf "OpenAI API Key: %s\n" "$([ -n "$ARG_OPENAI_API_KEY" ] && echo "Provided" || echo "Not provided")"
26+
printf "Report Tasks: %s\n" "$ARG_REPORT_TASKS"
2527
echo "======================================"
2628

2729
set +o nounset
@@ -100,13 +102,20 @@ EOF
100102
append_mcp_servers_section() {
101103
local config_path="$1"
102104

105+
if [ "${ARG_REPORT_TASKS}" == "false" ]; then
106+
ARG_CODER_MCP_APP_STATUS_SLUG=""
107+
CODER_MCP_AI_AGENTAPI_URL=""
108+
else
109+
CODER_MCP_AI_AGENTAPI_URL="http://localhost:3284"
110+
fi
111+
103112
cat << EOF >> "$config_path"
104113
105114
# MCP Servers Configuration
106115
[mcp_servers.Coder]
107116
command = "coder"
108117
args = ["exp", "mcp", "server"]
109-
env = { "CODER_MCP_APP_STATUS_SLUG" = "${ARG_CODER_MCP_APP_STATUS_SLUG}", "CODER_MCP_AI_AGENTAPI_URL" = "http://localhost:3284", "CODER_AGENT_URL" = "${CODER_AGENT_URL}", "CODER_AGENT_TOKEN" = "${CODER_AGENT_TOKEN}" }
118+
env = { "CODER_MCP_APP_STATUS_SLUG" = "${ARG_CODER_MCP_APP_STATUS_SLUG}", "CODER_MCP_AI_AGENTAPI_URL" = "${CODER_MCP_AI_AGENTAPI_URL}" , "CODER_AGENT_URL" = "${CODER_AGENT_URL}", "CODER_AGENT_TOKEN" = "${CODER_AGENT_TOKEN}" }
110119
description = "Report ALL tasks and statuses (in progress, done, failed) you are working on."
111120
type = "stdio"
112121
@@ -159,7 +168,21 @@ function add_instruction_prompt_if_exists() {
159168
fi
160169
}
161170

171+
function add_auth_json() {
172+
AUTH_JSON_PATH="$HOME/.codex/auth.json"
173+
mkdir -p "$(dirname "$AUTH_JSON_PATH")"
174+
AUTH_JSON=$(
175+
cat << EOF
176+
{
177+
"OPENAI_API_KEY": "${ARG_OPENAI_API_KEY}"
178+
}
179+
EOF
180+
)
181+
echo "$AUTH_JSON" > "$AUTH_JSON_PATH"
182+
}
183+
162184
install_codex
163185
codex --version
164186
populate_config_toml
165187
add_instruction_prompt_if_exists
188+
add_auth_json

registry/coder-labs/modules/codex/scripts/start.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ printf "OpenAI API Key: %s\n" "$([ -n "$ARG_OPENAI_API_KEY" ] && echo "Provided"
2222
printf "Codex Model: %s\n" "${ARG_CODEX_MODEL:-"Default"}"
2323
printf "Start Directory: %s\n" "$ARG_CODEX_START_DIRECTORY"
2424
printf "Has Task Prompt: %s\n" "$([ -n "$ARG_CODEX_TASK_PROMPT" ] && echo "Yes" || echo "No")"
25+
printf "Report Tasks: %s\n" "$ARG_REPORT_TASKS"
2526
echo "======================================"
2627
set +o nounset
2728
CODEX_ARGS=()
@@ -57,7 +58,11 @@ fi
5758

5859
if [ -n "$ARG_CODEX_TASK_PROMPT" ]; then
5960
printf "Running the task prompt %s\n" "$ARG_CODEX_TASK_PROMPT"
60-
PROMPT="Complete the task at hand in one go. Every step of the way, report your progress using coder_report_task tool with proper summary and statuses. Your task at hand: $ARG_CODEX_TASK_PROMPT"
61+
if [ "${ARG_REPORT_TASKS}" == "true" ]; then
62+
PROMPT="Complete the task at hand in one go. Every step of the way, report your progress using coder_report_task tool with proper summary and statuses. Your task at hand: $ARG_CODEX_TASK_PROMPT"
63+
else
64+
PROMPT="Your task at hand: $ARG_CODEX_TASK_PROMPT"
65+
fi
6166
CODEX_ARGS+=("$PROMPT")
6267
else
6368
printf "No task prompt given.\n"

0 commit comments

Comments
 (0)