diff --git a/registry/coder/modules/agentapi/README.md b/registry/coder/modules/agentapi/README.md index 06897d6b..fe543aef 100644 --- a/registry/coder/modules/agentapi/README.md +++ b/registry/coder/modules/agentapi/README.md @@ -27,6 +27,7 @@ module "agentapi" { cli_app_slug = "goose-cli" cli_app_display_name = "Goose CLI" module_dir_name = local.module_dir_name + agentapi_server_type = "claude" install_agentapi = var.install_agentapi pre_install_script = var.pre_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 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 --term-width --term-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" +``` diff --git a/registry/coder/modules/agentapi/main.tf b/registry/coder/modules/agentapi/main.tf index 6914be77..e3e2e9a5 100644 --- a/registry/coder/modules/agentapi/main.tf +++ b/registry/coder/modules/agentapi/main.tf @@ -128,6 +128,29 @@ variable "agentapi_port" { 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" { type = bool 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_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_initial_prompt = var.agentapi_initial_prompt != null ? base64encode(var.agentapi_initial_prompt) : "" agentapi_start_script_b64 = base64encode(var.start_script) 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. @@ -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_POST_INSTALL_SCRIPT="$(echo -n '${local.encoded_post_install_script}' | base64 -d)" \ 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_TASK_ID='${try(data.coder_task.me.id, "")}' \ ARG_TASK_LOG_SNAPSHOT='${var.task_log_snapshot}' \ diff --git a/registry/coder/modules/agentapi/scripts/main.sh b/registry/coder/modules/agentapi/scripts/main.sh index 63e013eb..157e6060 100644 --- a/registry/coder/modules/agentapi/scripts/main.sh +++ b/registry/coder/modules/agentapi/scripts/main.sh @@ -13,6 +13,10 @@ START_SCRIPT="$ARG_START_SCRIPT" WAIT_FOR_START_SCRIPT="$ARG_WAIT_FOR_START_SCRIPT" POST_INSTALL_SCRIPT="$ARG_POST_INSTALL_SCRIPT" 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:-}" TASK_ID="${ARG_TASK_ID:-}" TASK_LOG_SNAPSHOT="${ARG_TASK_LOG_SNAPSHOT:-true}" @@ -106,5 +110,25 @@ cd "${WORKDIR}" export AGENTAPI_CHAT_BASE_PATH="${AGENTAPI_CHAT_BASE_PATH:-}" # Disable host header check since AgentAPI is proxied by Coder (which does its own validation) 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}" diff --git a/registry/coder/modules/agentapi/testdata/agentapi-start.sh b/registry/coder/modules/agentapi/testdata/agentapi-start.sh index 259eb0c9..f0119acd 100644 --- a/registry/coder/modules/agentapi/testdata/agentapi-start.sh +++ b/registry/coder/modules/agentapi/testdata/agentapi-start.sh @@ -2,21 +2,14 @@ set -o errexit set -o pipefail -use_prompt=${1:-false} -port=${2:-3284} +module_path=${1:-"$HOME/.agentapi-module"} -module_path="$HOME/.agentapi-module" -log_file_path="$module_path/agentapi.log" +# Write the agent command to agent-command.sh +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 -echo "using port: $port" >> /home/coder/test-agentapi-start.log +chmod +x "$module_path/agent-command.sh" -AGENTAPI_CHAT_BASE_PATH="${AGENTAPI_CHAT_BASE_PATH:-}" -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 +echo "Agent command written to $module_path/agent-command.sh"