feat(coder/modules/agentapi): enhance start script and configuration options for AgentAPI server

This commit is contained in:
35C4n0r 2026-02-03 18:58:38 +05:30
parent 66662db5aa
commit 6e18248731
No known key found for this signature in database
GPG Key ID: 5B71E5C9D18D5675
4 changed files with 100 additions and 16 deletions

View File

@ -27,6 +27,7 @@ module "agentapi" {
cli_app_slug = "goose-cli" cli_app_slug = "goose-cli"
cli_app_display_name = "Goose CLI" cli_app_display_name = "Goose CLI"
module_dir_name = local.module_dir_name module_dir_name = local.module_dir_name
agentapi_server_type = "claude"
install_agentapi = var.install_agentapi install_agentapi = var.install_agentapi
pre_install_script = var.pre_install_script pre_install_script = var.pre_install_script
post_install_script = var.post_install_script post_install_script = var.post_install_script
@ -62,6 +63,44 @@ module "agentapi" {
} }
``` ```
## AgentAPI server configuration
You can configure the AgentAPI server type, terminal dimensions, and initial prompt:
```tf
module "agentapi" {
# ... other config
agentapi_server_type = "claude" # required
agentapi_term_width = 67 # default: 67
agentapi_term_height = 1190 # default: 1190
agentapi_initial_prompt = "You are a helpful assistant." # optional
}
```
**Note:** The `agentapi_initial_prompt` is recommended only if the agent doesn't support initial prompt in interaction mode.
## For module developers ## For module developers
For a complete example of how to use this module, see the [Goose module](https://github.com/coder/registry/blob/main/registry/coder/modules/goose/main.tf). For a complete example of how to use this module, see the [Goose module](https://github.com/coder/registry/blob/main/registry/coder/modules/goose/main.tf).
### Start script behavior
The `start_script` should write the agent command to `$module_path/agent-command.sh` instead of starting the AgentAPI server directly. The module will start the server using:
```bash
agentapi server --type <type> --term-width <width> --term-height <height> -- ./agent-command.sh
```
Example start script:
```bash
#!/bin/bash
module_path=${1:-"$HOME/.my-module"}
cat > "$module_path/agent-command.sh" << 'EOF'
#!/bin/bash
exec my-agent-command
EOF
chmod +x "$module_path/agent-command.sh"
```

View File

@ -128,6 +128,29 @@ variable "agentapi_port" {
default = 3284 default = 3284
} }
variable "agentapi_server_type" {
type = string
description = "The server type for AgentAPI, passed using --agent flag."
}
variable "agentapi_term_width" {
type = number
description = "The terminal width for AgentAPI."
default = 67
}
variable "agentapi_term_height" {
type = number
description = "The terminal height for AgentAPI."
default = 1190
}
variable "agentapi_initial_prompt" {
type = string
description = "Initial prompt for the agent. Recommended only if the agent doesn't support initial prompt in interaction mode."
default = null
}
variable "task_log_snapshot" { variable "task_log_snapshot" {
type = bool type = bool
description = "Capture last 10 messages when workspace stops for offline viewing while task is paused." description = "Capture last 10 messages when workspace stops for offline viewing while task is paused."
@ -171,6 +194,7 @@ locals {
encoded_pre_install_script = var.pre_install_script != null ? base64encode(var.pre_install_script) : "" encoded_pre_install_script = var.pre_install_script != null ? base64encode(var.pre_install_script) : ""
encoded_install_script = var.install_script != null ? base64encode(var.install_script) : "" encoded_install_script = var.install_script != null ? base64encode(var.install_script) : ""
encoded_post_install_script = var.post_install_script != null ? base64encode(var.post_install_script) : "" encoded_post_install_script = var.post_install_script != null ? base64encode(var.post_install_script) : ""
encoded_initial_prompt = var.agentapi_initial_prompt != null ? base64encode(var.agentapi_initial_prompt) : ""
agentapi_start_script_b64 = base64encode(var.start_script) agentapi_start_script_b64 = base64encode(var.start_script)
agentapi_wait_for_start_script_b64 = base64encode(file("${path.module}/scripts/agentapi-wait-for-start.sh")) agentapi_wait_for_start_script_b64 = base64encode(file("${path.module}/scripts/agentapi-wait-for-start.sh"))
// Chat base path is only set if not using a subdomain. // Chat base path is only set if not using a subdomain.
@ -206,6 +230,10 @@ resource "coder_script" "agentapi" {
ARG_WAIT_FOR_START_SCRIPT="$(echo -n '${local.agentapi_wait_for_start_script_b64}' | base64 -d)" \ ARG_WAIT_FOR_START_SCRIPT="$(echo -n '${local.agentapi_wait_for_start_script_b64}' | base64 -d)" \
ARG_POST_INSTALL_SCRIPT="$(echo -n '${local.encoded_post_install_script}' | base64 -d)" \ ARG_POST_INSTALL_SCRIPT="$(echo -n '${local.encoded_post_install_script}' | base64 -d)" \
ARG_AGENTAPI_PORT='${var.agentapi_port}' \ ARG_AGENTAPI_PORT='${var.agentapi_port}' \
ARG_AGENTAPI_SERVER_TYPE='${var.agentapi_server_type}' \
ARG_AGENTAPI_TERM_WIDTH='${var.agentapi_term_width}' \
ARG_AGENTAPI_TERM_HEIGHT='${var.agentapi_term_height}' \
ARG_AGENTAPI_INITIAL_PROMPT="$(echo -n '${local.encoded_initial_prompt}' | base64 -d)" \
ARG_AGENTAPI_CHAT_BASE_PATH='${local.agentapi_chat_base_path}' \ ARG_AGENTAPI_CHAT_BASE_PATH='${local.agentapi_chat_base_path}' \
ARG_TASK_ID='${try(data.coder_task.me.id, "")}' \ ARG_TASK_ID='${try(data.coder_task.me.id, "")}' \
ARG_TASK_LOG_SNAPSHOT='${var.task_log_snapshot}' \ ARG_TASK_LOG_SNAPSHOT='${var.task_log_snapshot}' \

View File

@ -13,6 +13,10 @@ START_SCRIPT="$ARG_START_SCRIPT"
WAIT_FOR_START_SCRIPT="$ARG_WAIT_FOR_START_SCRIPT" WAIT_FOR_START_SCRIPT="$ARG_WAIT_FOR_START_SCRIPT"
POST_INSTALL_SCRIPT="$ARG_POST_INSTALL_SCRIPT" POST_INSTALL_SCRIPT="$ARG_POST_INSTALL_SCRIPT"
AGENTAPI_PORT="$ARG_AGENTAPI_PORT" AGENTAPI_PORT="$ARG_AGENTAPI_PORT"
AGENTAPI_SERVER_TYPE="$ARG_AGENTAPI_SERVER_TYPE"
AGENTAPI_TERM_WIDTH="$ARG_AGENTAPI_TERM_WIDTH"
AGENTAPI_TERM_HEIGHT="$ARG_AGENTAPI_TERM_HEIGHT"
AGENTAPI_INITIAL_PROMPT="${ARG_AGENTAPI_INITIAL_PROMPT:-}"
AGENTAPI_CHAT_BASE_PATH="${ARG_AGENTAPI_CHAT_BASE_PATH:-}" AGENTAPI_CHAT_BASE_PATH="${ARG_AGENTAPI_CHAT_BASE_PATH:-}"
TASK_ID="${ARG_TASK_ID:-}" TASK_ID="${ARG_TASK_ID:-}"
TASK_LOG_SNAPSHOT="${ARG_TASK_LOG_SNAPSHOT:-true}" TASK_LOG_SNAPSHOT="${ARG_TASK_LOG_SNAPSHOT:-true}"
@ -106,5 +110,25 @@ cd "${WORKDIR}"
export AGENTAPI_CHAT_BASE_PATH="${AGENTAPI_CHAT_BASE_PATH:-}" export AGENTAPI_CHAT_BASE_PATH="${AGENTAPI_CHAT_BASE_PATH:-}"
# Disable host header check since AgentAPI is proxied by Coder (which does its own validation) # Disable host header check since AgentAPI is proxied by Coder (which does its own validation)
export AGENTAPI_ALLOWED_HOSTS="*" export AGENTAPI_ALLOWED_HOSTS="*"
nohup "$module_path/scripts/agentapi-start.sh" true "${AGENTAPI_PORT}" &> "$module_path/agentapi-start.log" &
# Call agentapi-start.sh to write agent-command.sh
"$module_path/scripts/agentapi-start.sh" "$module_path"
# Build agentapi server command arguments
ARGS=(
"server"
"--type" "${AGENTAPI_SERVER_TYPE}"
"--port" "${AGENTAPI_PORT}"
"--term-width" "${AGENTAPI_TERM_WIDTH}"
"--term-height" "${AGENTAPI_TERM_HEIGHT}"
)
# Add optional initial prompt
if [ -n "${AGENTAPI_INITIAL_PROMPT}" ]; then
ARGS+=("--initial-prompt" "${AGENTAPI_INITIAL_PROMPT}")
fi
# Start agentapi server with the agent-command.sh script
nohup agentapi "${ARGS[@]}" -- "$module_path/agent-command.sh" &> "$module_path/agentapi-start.log" &
"$module_path/scripts/agentapi-wait-for-start.sh" "${AGENTAPI_PORT}" "$module_path/scripts/agentapi-wait-for-start.sh" "${AGENTAPI_PORT}"

View File

@ -2,21 +2,14 @@
set -o errexit set -o errexit
set -o pipefail set -o pipefail
use_prompt=${1:-false} module_path=${1:-"$HOME/.agentapi-module"}
port=${2:-3284}
module_path="$HOME/.agentapi-module" # Write the agent command to agent-command.sh
log_file_path="$module_path/agentapi.log" cat > "$module_path/agent-command.sh" << 'EOF'
#!/bin/bash
exec bash -c aiagent
EOF
echo "using prompt: $use_prompt" >> /home/coder/test-agentapi-start.log chmod +x "$module_path/agent-command.sh"
echo "using port: $port" >> /home/coder/test-agentapi-start.log
AGENTAPI_CHAT_BASE_PATH="${AGENTAPI_CHAT_BASE_PATH:-}" echo "Agent command written to $module_path/agent-command.sh"
if [ -n "$AGENTAPI_CHAT_BASE_PATH" ]; then
echo "Using AGENTAPI_CHAT_BASE_PATH: $AGENTAPI_CHAT_BASE_PATH" >> /home/coder/test-agentapi-start.log
export AGENTAPI_CHAT_BASE_PATH
fi
agentapi server --port "$port" --term-width 67 --term-height 1190 -- \
bash -c aiagent \
> "$log_file_path" 2>&1