From ee0fba8dffd89229a939eca723946c2380754499 Mon Sep 17 00:00:00 2001 From: "blink-so[bot]" <211532188+blink-so[bot]@users.noreply.github.com> Date: Tue, 24 Jun 2025 02:34:45 +0000 Subject: [PATCH] Fix AI agent modules to use coder_env instead of coder_agent env This change addresses issue #128 by updating AI coding modules to use the coder_env resource for setting environment variables instead of recommending users set them on the coder_agent resource. Changes made: - Updated aider module to use coder_env for AI provider API keys - Updated claude-code module to add variables and coder_env resources for API key and prompts - Updated goose module to add variables and coder_env resources for prompts and API key - Updated documentation in README files to show the new approach - Removed export commands from shell scripts since env vars are now set by coder_env This reduces cognitive load for users and provides a cleaner, more consistent approach to environment variable management in AI modules. Co-authored-by: DevelopmentCats <176868952+DevelopmentCats@users.noreply.github.com> --- registry/coder/modules/aider/main.tf | 16 +++--- registry/coder/modules/claude-code/README.md | 20 +++----- registry/coder/modules/claude-code/main.tf | 53 ++++++++++++++++++++ registry/coder/modules/goose/README.md | 34 +++++-------- registry/coder/modules/goose/main.tf | 41 +++++++++++++++ 5 files changed, 124 insertions(+), 40 deletions(-) diff --git a/registry/coder/modules/aider/main.tf b/registry/coder/modules/aider/main.tf index e1f2eccd..9d9280a5 100644 --- a/registry/coder/modules/aider/main.tf +++ b/registry/coder/modules/aider/main.tf @@ -216,6 +216,13 @@ EOT model_flag = var.ai_provider == "ollama" ? "--ollama-model" : "--model" } +# Set environment variable for AI provider API key +resource "coder_env" "ai_api_key" { + agent_id = var.agent_id + name = local.env_var_name + value = var.ai_api_key +} + # Install and Initialize Aider resource "coder_script" "aider" { agent_id = var.agent_id @@ -387,7 +394,7 @@ EOL fi echo "Starting Aider using ${var.ai_provider} provider and model: ${var.ai_model}" - tmux new-session -d -s ${var.session_name} -c ${var.folder} "export ${local.env_var_name}=\"${var.ai_api_key}\"; aider --architect --yes-always ${local.model_flag} ${var.ai_model} --message \"${local.combined_prompt}\"" + tmux new-session -d -s ${var.session_name} -c ${var.folder} "aider --architect --yes-always ${local.model_flag} ${var.ai_model} --message \"${local.combined_prompt}\"" echo "Aider task started in tmux session '${var.session_name}'. Check the UI for progress." else # Configure tmux for shared sessions @@ -402,7 +409,7 @@ EOL fi echo "Starting Aider using ${var.ai_provider} provider and model: ${var.ai_model}" - tmux new-session -d -s ${var.session_name} -c ${var.folder} "export ${local.env_var_name}=\"${var.ai_api_key}\"; aider --architect --yes-always ${local.model_flag} ${var.ai_model} --message \"${var.system_prompt}\"" + tmux new-session -d -s ${var.session_name} -c ${var.folder} "aider --architect --yes-always ${local.model_flag} ${var.ai_model} --message \"${var.system_prompt}\"" echo "Tmux session '${var.session_name}' started. Access it by clicking the Aider button." fi else @@ -428,7 +435,6 @@ EOL screen -U -dmS ${var.session_name} bash -c " cd ${var.folder} export PATH=\"$HOME/bin:$HOME/.local/bin:$PATH\" - export ${local.env_var_name}=\"${var.ai_api_key}\" aider --architect --yes-always ${local.model_flag} ${var.ai_model} --message \"${local.combined_prompt}\" /bin/bash " @@ -455,7 +461,6 @@ EOL screen -U -dmS ${var.session_name} bash -c " cd ${var.folder} export PATH=\"$HOME/bin:$HOME/.local/bin:$PATH\" - export ${local.env_var_name}=\"${var.ai_api_key}\" aider --architect --yes-always ${local.model_flag} ${var.ai_model} --message \"${local.combined_prompt}\" /bin/bash " @@ -489,7 +494,7 @@ resource "coder_app" "aider_cli" { tmux attach-session -t ${var.session_name} else echo "Starting new Aider tmux session..." - tmux new-session -s ${var.session_name} -c ${var.folder} "export ${local.env_var_name}=\"${var.ai_api_key}\"; aider ${local.model_flag} ${var.ai_model} --message \"${local.combined_prompt}\"; exec bash" + tmux new-session -s ${var.session_name} -c ${var.folder} "aider ${local.model_flag} ${var.ai_model} --message \"${local.combined_prompt}\"; exec bash" fi elif [ "${var.use_screen}" = "true" ]; then if ! screen -list | grep -q "${var.session_name}"; then @@ -500,7 +505,6 @@ resource "coder_app" "aider_cli" { else cd "${var.folder}" echo "Starting Aider directly..." - export ${local.env_var_name}="${var.ai_api_key}" aider ${local.model_flag} ${var.ai_model} --message "${local.combined_prompt}" fi EOT diff --git a/registry/coder/modules/claude-code/README.md b/registry/coder/modules/claude-code/README.md index 21ff81f2..2a1adc46 100644 --- a/registry/coder/modules/claude-code/README.md +++ b/registry/coder/modules/claude-code/README.md @@ -72,19 +72,6 @@ data "coder_parameter" "ai_prompt" { mutable = true } -# Set the prompt and system prompt for Claude Code via environment variables -resource "coder_agent" "main" { - # ... - env = { - CODER_MCP_CLAUDE_API_KEY = var.anthropic_api_key # or use a coder_parameter - CODER_MCP_CLAUDE_TASK_PROMPT = data.coder_parameter.ai_prompt.value - CODER_MCP_APP_STATUS_SLUG = "claude-code" - CODER_MCP_CLAUDE_SYSTEM_PROMPT = <<-EOT - You are a helpful assistant that can help with code. - EOT - } -} - module "claude-code" { count = data.coder_workspace.me.start_count source = "registry.coder.com/coder/claude-code/coder" @@ -94,6 +81,13 @@ module "claude-code" { install_claude_code = true claude_code_version = "0.2.57" + # Set API key and prompts directly in the module + claude_api_key = var.anthropic_api_key # or use a coder_parameter + task_prompt = data.coder_parameter.ai_prompt.value + system_prompt = <<-EOT + You are a helpful assistant that can help with code. + EOT + # Enable experimental features experiment_use_screen = true # Or use experiment_use_tmux = true to use tmux instead experiment_report_tasks = true diff --git a/registry/coder/modules/claude-code/main.tf b/registry/coder/modules/claude-code/main.tf index d699b4f1..5176cc79 100644 --- a/registry/coder/modules/claude-code/main.tf +++ b/registry/coder/modules/claude-code/main.tf @@ -96,11 +96,64 @@ variable "experiment_tmux_session_save_interval" { default = "15" } +variable "claude_api_key" { + type = string + description = "Anthropic API key for Claude." + default = "" + sensitive = true +} + +variable "task_prompt" { + type = string + description = "Task prompt for Claude Code." + default = "" +} + +variable "system_prompt" { + type = string + description = "System prompt for Claude Code." + default = "" +} + +variable "app_status_slug" { + type = string + description = "App status slug for Claude Code." + default = "claude-code" +} + locals { encoded_pre_install_script = var.experiment_pre_install_script != null ? base64encode(var.experiment_pre_install_script) : "" encoded_post_install_script = var.experiment_post_install_script != null ? base64encode(var.experiment_post_install_script) : "" } +# Set environment variables for Claude Code +resource "coder_env" "claude_api_key" { + count = var.claude_api_key != "" ? 1 : 0 + agent_id = var.agent_id + name = "CODER_MCP_CLAUDE_API_KEY" + value = var.claude_api_key +} + +resource "coder_env" "claude_task_prompt" { + count = var.task_prompt != "" ? 1 : 0 + agent_id = var.agent_id + name = "CODER_MCP_CLAUDE_TASK_PROMPT" + value = var.task_prompt +} + +resource "coder_env" "claude_system_prompt" { + count = var.system_prompt != "" ? 1 : 0 + agent_id = var.agent_id + name = "CODER_MCP_CLAUDE_SYSTEM_PROMPT" + value = var.system_prompt +} + +resource "coder_env" "claude_app_status_slug" { + agent_id = var.agent_id + name = "CODER_MCP_APP_STATUS_SLUG" + value = var.app_status_slug +} + # Install and Initialize Claude Code resource "coder_script" "claude_code" { agent_id = var.agent_id diff --git a/registry/coder/modules/goose/README.md b/registry/coder/modules/goose/README.md index 268b1d00..c23604fb 100644 --- a/registry/coder/modules/goose/README.md +++ b/registry/coder/modules/goose/README.md @@ -66,27 +66,6 @@ data "coder_parameter" "ai_prompt" { mutable = true } -# Set the prompt and system prompt for Goose via environment variables -resource "coder_agent" "main" { - # ... - env = { - GOOSE_SYSTEM_PROMPT = <<-EOT - You are a helpful assistant that can help write code. - - Run all long running tasks (e.g. npm run dev) in the background and not in the foreground. - - Periodically check in on background tasks. - - Notify Coder of the status of the task before and after your steps. - EOT - GOOSE_TASK_PROMPT = data.coder_parameter.ai_prompt.value - - # An API key is required for experiment_auto_configure - # See https://block.github.io/goose/docs/getting-started/providers - ANTHROPIC_API_KEY = var.anthropic_api_key # or use a coder_parameter - } -} - module "goose" { count = data.coder_workspace.me.start_count source = "registry.coder.com/coder/goose/coder" @@ -96,6 +75,19 @@ module "goose" { install_goose = true goose_version = "v1.0.16" + # Set prompts and API key directly in the module + system_prompt = <<-EOT + You are a helpful assistant that can help write code. + + Run all long running tasks (e.g. npm run dev) in the background and not in the foreground. + + Periodically check in on background tasks. + + Notify Coder of the status of the task before and after your steps. + EOT + task_prompt = data.coder_parameter.ai_prompt.value + anthropic_api_key = var.anthropic_api_key # or use a coder_parameter + # Enable experimental features experiment_report_tasks = true diff --git a/registry/coder/modules/goose/main.tf b/registry/coder/modules/goose/main.tf index a159ca7b..11054889 100644 --- a/registry/coder/modules/goose/main.tf +++ b/registry/coder/modules/goose/main.tf @@ -114,6 +114,25 @@ variable "experiment_additional_extensions" { default = null } +variable "system_prompt" { + type = string + description = "System prompt for Goose." + default = "" +} + +variable "task_prompt" { + type = string + description = "Task prompt for Goose." + default = "" +} + +variable "anthropic_api_key" { + type = string + description = "Anthropic API key for Goose." + default = "" + sensitive = true +} + locals { base_extensions = <<-EOT coder: @@ -150,6 +169,28 @@ EOT encoded_post_install_script = var.experiment_post_install_script != null ? base64encode(var.experiment_post_install_script) : "" } +# Set environment variables for Goose +resource "coder_env" "goose_system_prompt" { + count = var.system_prompt != "" ? 1 : 0 + agent_id = var.agent_id + name = "GOOSE_SYSTEM_PROMPT" + value = var.system_prompt +} + +resource "coder_env" "goose_task_prompt" { + count = var.task_prompt != "" ? 1 : 0 + agent_id = var.agent_id + name = "GOOSE_TASK_PROMPT" + value = var.task_prompt +} + +resource "coder_env" "anthropic_api_key" { + count = var.anthropic_api_key != "" ? 1 : 0 + agent_id = var.agent_id + name = "ANTHROPIC_API_KEY" + value = var.anthropic_api_key +} + # Install and Initialize Goose resource "coder_script" "goose" { agent_id = var.agent_id