Skip to content

Fix AI agent modules to use coder_env instead of coder_agent env #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions registry/coder/modules/aider/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
"
Expand All @@ -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
"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
20 changes: 7 additions & 13 deletions registry/coder/modules/claude-code/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
53 changes: 53 additions & 0 deletions registry/coder/modules/claude-code/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 13 additions & 21 deletions registry/coder/modules/goose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

Expand Down
41 changes: 41 additions & 0 deletions registry/coder/modules/goose/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
Loading