@@ -4,6 +4,7 @@ terraform {
4
4
required_providers {
5
5
coder = {
6
6
source = " coder/coder"
7
+ version = " >= 2.7.0"
7
8
}
8
9
}
9
10
}
@@ -53,6 +54,18 @@ variable "claude_code_version" {
53
54
default = " latest"
54
55
}
55
56
57
+ variable "install_agentapi" {
58
+ type = bool
59
+ description = " Whether to install AgentAPI."
60
+ default = true
61
+ }
62
+
63
+ variable "agentapi_version" {
64
+ type = string
65
+ description = " The version of AgentAPI to install."
66
+ default = " v0.2.2"
67
+ }
68
+
56
69
variable "experiment_use_screen" {
57
70
type = bool
58
71
description = " Whether to use screen for running Claude Code in the background."
@@ -86,6 +99,15 @@ variable "experiment_post_install_script" {
86
99
locals {
87
100
encoded_pre_install_script = var. experiment_pre_install_script != null ? base64encode (var. experiment_pre_install_script ) : " "
88
101
encoded_post_install_script = var. experiment_post_install_script != null ? base64encode (var. experiment_post_install_script ) : " "
102
+ agentapi_start_command = <<- EOT
103
+ #!/bin/bash
104
+ set -e
105
+
106
+ # use low width to fit in the tasks UI sidebar. height is adjusted to ~match the default 80k (80x1000) characters
107
+ # visible in the terminal screen.
108
+ agentapi server --term-width 67 --term-height 1190 -- bash -c "claude --dangerously-skip-permissions \"$(cat ~/.claude-code-prompt)\""
109
+ EOT
110
+ agentapi_start_command_base64 = base64encode (local. agentapi_start_command )
89
111
}
90
112
91
113
# Install and Initialize Claude Code
@@ -130,10 +152,31 @@ resource "coder_script" "claude_code" {
130
152
npm install -g @anthropic-ai/claude-code@${ var . claude_code_version }
131
153
fi
132
154
133
- # Hardcoded for now: install AgentAPI
134
- wget https://github.com/coder/agentapi/releases/download/preview/agentapi-linux-amd64
135
- chmod +x agentapi-linux-amd64
136
- sudo mv agentapi-linux-amd64 /usr/local/bin/agentapi
155
+ # Install AgentAPI if enabled
156
+ if [ "${ var . install_agentapi } " = "true" ]; then
157
+ echo "Installing AgentAPI..."
158
+ arch=$(uname -m)
159
+ if [ "$arch" = "x86_64" ]; then
160
+ binary_name="agentapi-linux-amd64"
161
+ elif [ "$arch" = "aarch64" ]; then
162
+ binary_name="agentapi-linux-arm64"
163
+ else
164
+ echo "Error: Unsupported architecture: $arch"
165
+ exit 1
166
+ fi
167
+ wget "https://github.com/coder/agentapi/releases/download/${ var . agentapi_version } /$binary_name"
168
+ chmod +x "$binary_name"
169
+ sudo mv "$binary_name" /usr/local/bin/agentapi
170
+ fi
171
+
172
+ if ! command_exists agentapi; then
173
+ echo "Error: AgentAPI is not installed. Please enable install_agentapi or install it manually."
174
+ exit 1
175
+ fi
176
+
177
+ echo -n "$CODER_MCP_CLAUDE_TASK_PROMPT" > ~/.claude-code-prompt
178
+ echo -n "${ local . agentapi_start_command_base64 } " | base64 -d > ~/.agentapi-start-command
179
+ chmod +x ~/.agentapi-start-command
137
180
138
181
if [ "${ var . experiment_report_tasks } " = "true" ]; then
139
182
echo "Configuring Claude Code to report tasks via Coder MCP..."
@@ -172,7 +215,7 @@ resource "coder_script" "claude_code" {
172
215
173
216
# use low width to fit in the tasks UI sidebar. height is adjusted to ~match the default 80k (80x1000) characters
174
217
# visible in the terminal screen.
175
- tmux new-session -d -s claude-code-agentapi -c ${ var . folder } 'agentapi server --term-width 67 --term-height 1190 -- bash -c "claude --dangerously-skip-permissions \"$CODER_MCP_CLAUDE_TASK_PROMPT\"" ; exec bash'
218
+ tmux new-session -d -s claude-code-agentapi -c ${ var . folder } '~/. agentapi-start-command ; exec bash'
176
219
echo "Waiting for agentapi server to start on port 3284..."
177
220
for i in $(seq 1 15); do
178
221
if lsof -i :3284 | grep -q 'LISTEN'; then
@@ -223,7 +266,7 @@ resource "coder_script" "claude_code" {
223
266
224
267
screen -U -dmS claude-code bash -c '
225
268
cd ${ var . folder }
226
- claude --dangerously-skip-permissions "$CODER_MCP_CLAUDE_TASK_PROMPT" | tee -a "$HOME/.claude-code.log"
269
+ ~/.agentapi-start-command
227
270
exec bash
228
271
'
229
272
else
@@ -266,9 +309,7 @@ resource "coder_app" "claude_code" {
266
309
267
310
if ! tmux has-session -t claude-code-agentapi 2>/dev/null; then
268
311
echo "Starting a new Claude Code agentapi tmux session." | tee -a "$HOME/.claude-code.log"
269
- # use low width to fit in the tasks UI sidebar. height is adjusted to ~match the default 80k (80x1000) characters
270
- # visible in the terminal screen.
271
- tmux new-session -d -s claude-code-agentapi -c ${ var . folder } 'agentapi server --term-width 67 --term-height 1190 -- bash -c "claude --dangerously-skip-permissions"; exec bash'
312
+ tmux new-session -d -s claude-code-agentapi -c ${ var . folder } '~/.agentapi-start-command; exec bash'
272
313
fi
273
314
274
315
if tmux has-session -t claude-code 2>/dev/null; then
0 commit comments