diff --git a/registry/saheli/modules/amazon-q/README.md b/registry/saheli/modules/amazon-q/README.md new file mode 100644 index 00000000..c79f7f7a --- /dev/null +++ b/registry/saheli/modules/amazon-q/README.md @@ -0,0 +1,249 @@ +--- +display_name: Amazon Q +description: Run Amazon Q with AgentAPI as a web chat interface, with Coder Tasks integration for real-time AI task management. +icon: ../../../../.icons/amazon-q.svg +maintainer_github: coder +verified: true +tags: [agent, ai, aws, amazon-q, agentapi, tasks, aider, mcp] +--- + +# Amazon Q with Coder Tasks Integration + +Run [Amazon Q](https://aws.amazon.com/q/) with [AgentAPI](https://github.com/coder/agentapi) and full [Coder Tasks](https://coder.com/docs/tasks) support. This module provides a modern web chat interface with real-time task reporting, making it easy to track AI progress through Coder's task sidebar. + +```tf +module "amazon-q" { + source = "registry.coder.com/saheli/amazon-q/coder" + version = "2.0.0" + agent_id = coder_agent.example.id + experiment_report_tasks = true # Enable Coder Tasks integration + + # AWS Authentication + aws_access_key_id = var.aws_access_key_id + aws_secret_access_key = var.aws_secret_access_key +} +``` + +![Amazon-Q with AgentAPI and Tasks](../../.images/amazon-q-agentapi-tasks.png) + +## Features + +- 🤖 **Amazon Q Integration**: Full Amazon Q Developer capabilities +- 📊 **Coder Tasks**: Real-time task reporting in the Coder sidebar +- 🌐 **Web Chat Interface**: Modern web UI powered by AgentAPI +- 🎯 **Aider Support**: Optional Aider integration for enhanced coding +- 🔌 **MCP Protocol**: Model Context Protocol for advanced integrations +- 🔐 **Flexible Auth**: Supports AWS credentials, IAM roles, and SSO + +## Prerequisites + +- Coder v2.7.0 or later +- AWS account with Amazon Q Developer access +- Linux workspace (x86_64 or arm64) + +## Quick Start + +### Basic Usage with Tasks + +```hcl +module "amazon-q" { + source = "registry.coder.com/saheli/amazon-q/coder" + version = "2.0.0" + + agent_id = coder_agent.main.id + + # Enable Coder Tasks + experiment_report_tasks = true + + # AWS credentials + aws_access_key_id = var.aws_access_key_id + aws_secret_access_key = var.aws_secret_access_key + aws_region = "us-east-1" +} +``` + +### With Custom Task Reporting + +```hcl +module "amazon-q" { + source = "registry.coder.com/saheli/amazon-q/coder" + version = "2.0.0" + + agent_id = coder_agent.main.id + experiment_report_tasks = true + + # Optimized system prompt for task reporting + system_prompt = <<-EOT + You are Amazon Q integrated with Coder Tasks. + + YOU MUST REPORT ALL TASKS TO CODER: + - Report status IMMEDIATELY after any user message + - Break work into granular, trackable tasks + - Use "working" when actively processing + - Use "complete" when finished + - Use "failure" when blocked + + Keep summaries under 160 characters. + EOT + + # Start with a specific task + task_prompt = "Create a Python REST API with FastAPI" +} +``` + +### Using Aider Mode + +```hcl +module "amazon-q" { + source = "registry.coder.com/saheli/amazon-q/coder" + version = "2.0.0" + + agent_id = coder_agent.main.id + + # Use Aider instead of Amazon Q CLI + use_aider = true + aider_version = "latest" + + # Enable all features + experiment_report_tasks = true + install_agentapi = true +} +``` + +## How Coder Tasks Integration Works + +When `experiment_report_tasks = true`: + +1. **Web Interface**: Access Amazon Q through the "Amazon Q Web" app in Coder +2. **Task Sidebar**: Tasks appear in real-time in the Coder sidebar +3. **Status Updates**: See live progress as the AI works on your requests +4. **MCP Protocol**: Uses Model Context Protocol for reliable task reporting + +The integration provides: + +- Real-time task status (working, complete, failure) +- Granular progress tracking +- Interactive chat interface +- Full task history + +## Configuration Variables + +| Variable | Type | Description | Default | +| ------------------------- | ------ | --------------------------------- | ---------------------- | +| `agent_id` | string | **Required** - The Coder agent ID | - | +| `experiment_report_tasks` | bool | Enable Coder Tasks integration | `true` | +| `install_amazon_q` | bool | Install Amazon Q CLI | `true` | +| `install_agentapi` | bool | Install AgentAPI | `true` | +| `use_aider` | bool | Use Aider instead of Amazon Q | `false` | +| `system_prompt` | string | System instructions for the AI | (task-optimized) | +| `task_prompt` | string | Initial task to start with | `""` | +| `aws_access_key_id` | string | AWS Access Key ID | `""` | +| `aws_secret_access_key` | string | AWS Secret Access Key | `""` | +| `aws_region` | string | AWS Region | `"us-east-1"` | +| `order` | number | App display order | `null` | +| `group` | string | App group name | `null` | +| `icon` | string | App icon path | `"/icon/amazon-q.svg"` | + +## AWS Authentication + +The module supports multiple authentication methods: + +### 1. Environment Variables + +```hcl +aws_access_key_id = var.aws_access_key_id +aws_secret_access_key = var.aws_secret_access_key +``` + +### 2. IAM Instance Role + +Leave credentials empty to use the workspace's IAM role. + +### 3. AWS Profile + +```hcl +aws_profile = "my-profile" +``` + +## Advanced Configuration + +### Custom MCP Extensions + +Add additional MCP servers for enhanced functionality: + +```hcl +additional_extensions = <<-YAML +desktop-commander: + cmd: desktop-commander-server + args: ["--server"] + description: Long-running processes + enabled: true + type: stdio +playwright: + cmd: npx + args: ["--yes", "@mcp-get/playwright", "server"] + description: Web testing + enabled: true + type: stdio +YAML +``` + +### Custom Scripts + +Run scripts before/after installation: + +```hcl +experiment_pre_install_script = <<-BASH + echo "Preparing environment..." + pip install -U pip +BASH + +experiment_post_install_script = <<-BASH + echo "Setup complete!" +BASH +``` + +## Troubleshooting + +### Task Reporting Issues + +- Ensure `experiment_report_tasks = true` +- Check AgentAPI status: `curl http://localhost:3284/status` +- Verify MCP config: `cat ~/.config/coder/mcp/amazon-q.yaml` +- Check logs: `tail -f ~/.amazon-q-module/logs/*.log` + +### Amazon Q Not Starting + +- Verify installation: `q --version` +- Check AWS credentials: `aws sts get-caller-identity` +- Review logs in `~/.amazon-q-module/logs/` + +### Web Interface Not Loading + +- Ensure AgentAPI is running on port 3284 +- Check the "Amazon Q Web" app in Coder UI +- Verify health check passes + +## Architecture + +```mermaid +graph LR + A[User] --> B[Coder Web UI] + B --> C[Amazon Q Web App] + C --> D[AgentAPI :3284] + D --> E[Amazon Q CLI] + E --> F[AWS Services] + D --> G[Coder Tasks API] + G --> H[Task Sidebar] +``` + +## Support + +- [Amazon Q Documentation](https://docs.aws.amazon.com/amazonq/) +- [Coder Tasks Guide](https://coder.com/docs/tasks) +- [AgentAPI Repository](https://github.com/coder/agentapi) +- [Module Issues](https://github.com/coder/registry/issues) + +## License + +This module is licensed under the MIT License. Amazon Q is subject to AWS service terms. diff --git a/registry/saheli/modules/amazon-q/main.test.ts b/registry/saheli/modules/amazon-q/main.test.ts new file mode 100644 index 00000000..c6de3a18 --- /dev/null +++ b/registry/saheli/modules/amazon-q/main.test.ts @@ -0,0 +1,46 @@ +import { describe, it, expect } from "bun:test"; +import { + runTerraformApply, + runTerraformInit, + testRequiredVariables, + findResourceInstance, +} from "~test"; +import path from "path"; + +const moduleDir = path.resolve(__dirname); + +const requiredVars = { + agent_id: "dummy-agent-id", +}; + +describe("amazon-q module", async () => { + await runTerraformInit(moduleDir); + + // 1. Required variables + testRequiredVariables(moduleDir, requiredVars); + + // 2. coder_script resource is created + it("creates coder_script resource", async () => { + const state = await runTerraformApply(moduleDir, requiredVars); + const scriptResource = findResourceInstance(state, "coder_script"); + expect(scriptResource).toBeDefined(); + expect(scriptResource.agent_id).toBe(requiredVars.agent_id); + // The script is base64 encoded, so let's check for the module + expect(scriptResource.script).toContain("ARG_INSTALL_SCRIPT"); + }); + + // 3. coder_app resource is created (from AgentAPI module) + it("creates coder_app resource", async () => { + const state = await runTerraformApply(moduleDir, requiredVars); + // The AgentAPI module creates apps with names "agentapi_web" and "agentapi_cli" + const webAppResource = findResourceInstance(state, "coder_app", "agentapi_web"); + expect(webAppResource).toBeDefined(); + expect(webAppResource.agent_id).toBe(requiredVars.agent_id); + + const cliAppResource = findResourceInstance(state, "coder_app", "agentapi_cli"); + expect(cliAppResource).toBeDefined(); + expect(cliAppResource.agent_id).toBe(requiredVars.agent_id); + }); + + // Add more state-based tests as needed +}); diff --git a/registry/saheli/modules/amazon-q/main.tf b/registry/saheli/modules/amazon-q/main.tf new file mode 100644 index 00000000..74b38c8e --- /dev/null +++ b/registry/saheli/modules/amazon-q/main.tf @@ -0,0 +1,281 @@ +terraform { + required_version = ">= 1.0" + + required_providers { + coder = { + source = "coder/coder" + version = ">= 2.7" + } + } +} + +variable "agent_id" { + type = string + description = "The ID of a Coder agent." +} + +data "coder_workspace" "me" {} + +data "coder_workspace_owner" "me" {} + +variable "order" { + type = number + description = "The order determines the position of app in the UI presentation. The lowest order is shown first and apps with equal order are sorted by name (ascending order)." + default = null +} + +variable "group" { + type = string + description = "The name of a group that this app belongs to." + default = null +} + +variable "icon" { + type = string + description = "The icon to use for the app." + default = "/icon/amazon-q.svg" +} + +variable "folder" { + type = string + description = "The folder to run Amazon Q in." + default = "/home/coder" +} + +variable "install_amazon_q" { + type = bool + description = "Whether to install Amazon Q." + default = true +} + +variable "amazon_q_version" { + type = string + description = "The version of Amazon Q to install." + default = "latest" +} + +variable "install_agentapi" { + type = bool + description = "Whether to install AgentAPI." + default = true +} + +variable "agentapi_version" { + type = string + description = "The version of AgentAPI to install." + default = "latest" +} + +variable "use_aider" { + type = bool + description = "Whether to use Aider instead of Amazon Q CLI." + default = false +} + +variable "aider_version" { + type = string + description = "The version of Aider to install when use_aider is true." + default = "latest" +} + +variable "experiment_use_screen" { + type = bool + description = "Whether to use screen for running Amazon Q in the background." + default = false +} + +variable "experiment_use_tmux" { + type = bool + description = "Whether to use tmux instead of screen for running Amazon Q in the background." + default = false +} + +variable "experiment_report_tasks" { + type = bool + description = "Whether to enable task reporting." + default = true +} + +variable "experiment_pre_install_script" { + type = string + description = "Custom script to run before installing Amazon Q." + default = null +} + +variable "experiment_post_install_script" { + type = string + description = "Custom script to run after installing Amazon Q." + default = null +} + +variable "aws_access_key_id" { + type = string + description = "AWS Access Key ID for Amazon Q authentication" + sensitive = true + default = "" +} + +variable "aws_secret_access_key" { + type = string + description = "AWS Secret Access Key for Amazon Q authentication" + sensitive = true + default = "" +} + +variable "aws_region" { + type = string + description = "AWS region for Amazon Q" + default = "us-east-1" +} + +variable "aws_profile" { + type = string + description = "AWS profile to use for authentication" + default = "default" +} + +variable "system_prompt" { + type = string + description = "The system prompt to use for Amazon Q. This should instruct the agent how to do task reporting." + default = <<-EOT + You are a helpful Coding assistant. Aim to autonomously investigate + and solve issues the user gives you and test your work, whenever possible. + Avoid shortcuts like mocking tests. When you get stuck, you can ask the user + but opt for autonomy. + + YOU MUST REPORT ALL TASKS TO CODER. + When reporting tasks, you MUST follow these EXACT instructions: + - IMMEDIATELY report status after receiving ANY user message. + - Be granular. If you are investigating with multiple steps, report each step to coder. + + Task state MUST be one of the following: + - Use "state": "working" when actively processing WITHOUT needing additional user input. + - Use "state": "complete" only when finished with a task. + - Use "state": "failure" when you need ANY user input, lack sufficient details, or encounter blockers. + + Task summaries MUST: + - Include specifics about what you're doing. + - Include clear and actionable steps for the user. + - Be less than 160 characters in length. + EOT +} + +variable "task_prompt" { + type = string + description = "The initial task prompt to send to Amazon Q." + default = "" +} + +variable "additional_extensions" { + type = string + description = "Additional extensions configuration in YAML format to append to the config." + default = null +} + +locals { + app_slug = "amazon-q" + module_dir_name = ".amazon-q-module" + + # MCP extensions configuration following Goose pattern + base_extensions = <<-EOT +coder: + args: + - exp + - mcp + - server + cmd: coder + description: Report ALL tasks and statuses (in progress, done, failed) you are working on. + enabled: true + envs: + CODER_MCP_APP_STATUS_SLUG: ${local.app_slug} + CODER_MCP_AI_AGENTAPI_URL: http://localhost:3284 + name: Coder + timeout: 3000 + type: stdio +developer: + display_name: Developer + enabled: true + name: developer + timeout: 300 + type: builtin +EOT + + # Format extensions to match YAML structure + formatted_base = " ${replace(trimspace(local.base_extensions), "\n", "\n ")}" + additional_extensions = var.additional_extensions != null ? "\n ${replace(trimspace(var.additional_extensions), "\n", "\n ")}" : "" + combined_extensions = <<-EOT +extensions: +${local.formatted_base}${local.additional_extensions} +EOT + + # Load scripts from files like Goose module + install_script_content = file("${path.module}/scripts/install.sh") + start_script_content = file("${path.module}/scripts/start.sh") + +} + +module "agentapi" { + source = "registry.coder.com/coder/agentapi/coder" + version = "1.0.0" + + agent_id = var.agent_id + web_app_slug = local.app_slug + web_app_order = var.order + web_app_group = var.group + web_app_icon = var.icon + web_app_display_name = var.use_aider ? "Amazon Q + Aider" : "Amazon Q" + cli_app = true + cli_app_slug = "${local.app_slug}-cli" + cli_app_display_name = var.use_aider ? "Amazon Q + Aider CLI" : "Amazon Q CLI" + module_dir_name = local.module_dir_name + install_agentapi = var.install_agentapi + agentapi_version = var.agentapi_version + pre_install_script = var.experiment_pre_install_script + post_install_script = var.experiment_post_install_script + start_script = local.start_script_content + install_script = <<-EOT + #!/bin/bash + set -o errexit + set -o pipefail + + echo -n '${base64encode(local.install_script_content)}' | base64 -d > /tmp/install.sh + chmod +x /tmp/install.sh + + ARG_AMAZON_Q_CONFIG="$(echo -n '${base64encode(local.combined_extensions)}' | base64 -d)" \ + ARG_INSTALL='${var.install_amazon_q}' \ + ARG_AMAZON_Q_VERSION='${var.amazon_q_version}' \ + ARG_USE_AIDER='${var.use_aider}' \ + ARG_AIDER_VERSION='${var.aider_version}' \ + ARG_AWS_ACCESS_KEY_ID='${var.aws_access_key_id}' \ + ARG_AWS_SECRET_ACCESS_KEY='${var.aws_secret_access_key}' \ + ARG_AWS_REGION='${var.aws_region}' \ + ARG_AWS_PROFILE='${var.aws_profile}' \ + /tmp/install.sh + EOT + +} + +# Create web app for Amazon Q chat interface +resource "coder_app" "amazon_q_web" { + count = var.experiment_report_tasks ? 1 : 0 + + slug = "aqw" # Short slug to avoid URL length issues + display_name = var.use_aider ? "Amazon Q + Aider Web" : "Amazon Q Web" + agent_id = var.agent_id + url = "http://localhost:3284/" + subdomain = true + healthcheck { + url = "http://localhost:3284/status" + interval = 3 + threshold = 20 + } +} + +# Create AI task resource for sidebar integration +resource "coder_ai_task" "amazon_q" { + count = var.experiment_report_tasks ? 1 : 0 + + sidebar_app { + id = coder_app.amazon_q_web[0].id + } +} diff --git a/registry/saheli/modules/amazon-q/scripts/install.sh b/registry/saheli/modules/amazon-q/scripts/install.sh new file mode 100644 index 00000000..abffbca6 --- /dev/null +++ b/registry/saheli/modules/amazon-q/scripts/install.sh @@ -0,0 +1,187 @@ +#!/bin/bash + +# Enhanced logging function +log() { + echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$HOME/.amazon-q-module/logs/install.log" +} + +# Error handling +set -e +trap 'log "ERROR: Installation failed on line $LINENO"' ERR + +# Create log directory +mkdir -p "$HOME/.amazon-q-module/logs" + +log "INFO: Starting Amazon Q installation..." + +# Get environment variables from Terraform +INSTALL_AMAZON_Q="${ARG_INSTALL:-true}" +AMAZON_Q_VERSION="${ARG_AMAZON_Q_VERSION:-latest}" +USE_AIDER="${ARG_USE_AIDER:-false}" +AIDER_VERSION="${ARG_AIDER_VERSION:-latest}" +AMAZON_Q_CONFIG="${ARG_AMAZON_Q_CONFIG:-}" +AWS_ACCESS_KEY_ID="${ARG_AWS_ACCESS_KEY_ID:-}" +AWS_SECRET_ACCESS_KEY="${ARG_AWS_SECRET_ACCESS_KEY:-}" +AWS_REGION="${ARG_AWS_REGION:-us-east-1}" +AWS_PROFILE="${ARG_AWS_PROFILE:-default}" + +# Check if Amazon Q is already installed +if command -v q >/dev/null 2>&1; then + log "INFO: Amazon Q is already installed" + q --version | tee -a "$HOME/.amazon-q-module/logs/install.log" + exit 0 +fi + +# Install dependencies +log "INFO: Installing dependencies..." +if command -v apt-get >/dev/null 2>&1; then + sudo apt-get update + sudo apt-get install -y curl unzip +elif command -v yum >/dev/null 2>&1; then + sudo yum install -y curl unzip +elif command -v brew >/dev/null 2>&1; then + brew install curl unzip +else + log "ERROR: Could not find package manager" + exit 1 +fi + +# Download and install Amazon Q +log "INFO: Downloading Amazon Q..." +TMP_DIR=$(mktemp -d) +cd "$TMP_DIR" + +ARCH=$(uname -m) +case "$ARCH" in + "x86_64") + Q_URL="https://desktop-release.q.us-east-1.amazonaws.com/latest/q-x86_64-linux.zip" + ;; + "aarch64"|"arm64") + Q_URL="https://desktop-release.codewhisperer.us-east-1.amazonaws.com/latest/q-aarch64-linux.zip" + ;; + *) + log "ERROR: Unsupported architecture: $ARCH" + exit 1 + ;; +esac + +log "INFO: Downloading from $Q_URL" +curl -L "$Q_URL" -o q.zip + +log "INFO: Extracting Amazon Q..." +unzip q.zip + +log "INFO: Installing Amazon Q..." +./q/install.sh --force + +# Add to PATH +log "INFO: Configuring PATH..." +if ! grep -q "q/bin" "$HOME/.bashrc"; then + echo 'export PATH="$PATH:$HOME/q/bin"' >> "$HOME/.bashrc" +fi + +export PATH="$PATH:$HOME/q/bin" + +# Verify installation +log "INFO: Verifying Amazon Q installation..." +if command -v q >/dev/null 2>&1; then + log "SUCCESS: Amazon Q installed successfully" + q --version | tee -a "$HOME/.amazon-q-module/logs/install.log" +else + log "ERROR: Amazon Q installation failed" + exit 1 +fi + +# Clean up +cd "$HOME" +rm -rf "$TMP_DIR" + +# Configure AWS credentials (following Goose pattern) +log "INFO: Configuring AWS credentials..." +mkdir -p "$HOME/.aws" + +# Create AWS config file +cat > "$HOME/.aws/config" << EOF +[default] +region = ${AWS_REGION:-us-east-1} +output = json +EOF + +# Create AWS credentials file if credentials are provided +if [ ! -z "$AWS_ACCESS_KEY_ID" ] && [ ! -z "$AWS_SECRET_ACCESS_KEY" ]; then + cat > "$HOME/.aws/credentials" << EOF +[default] +aws_access_key_id = $AWS_ACCESS_KEY_ID +aws_secret_access_key = $AWS_SECRET_ACCESS_KEY +EOF + log "INFO: AWS credentials configured" +else + log "INFO: No AWS credentials provided, using environment or IAM role" +fi + +# Create Amazon Q config (similar to Goose) +log "INFO: Creating Amazon Q configuration..." +mkdir -p "$HOME/.config/amazonq" +cat > "$HOME/.config/amazonq/config.yaml" << EOF +provider: aws +region: ${AWS_REGION:-us-east-1} +profile: ${AWS_PROFILE:-default} +EOF + +# Install Aider if requested +if [ "$USE_AIDER" = "true" ]; then + log "INFO: Installing Aider..." + if ! command -v pip3 >/dev/null 2>&1; then + log "INFO: Installing Python 3 and pip..." + if command -v apt-get >/dev/null 2>&1; then + sudo apt-get update + sudo apt-get install -y python3 python3-pip + elif command -v yum >/dev/null 2>&1; then + sudo yum install -y python3 python3-pip + fi + fi + + # Install Aider + if [ "$AIDER_VERSION" = "latest" ]; then + pip3 install --user aider-chat + else + pip3 install --user "aider-chat==$AIDER_VERSION" + fi + + # Add to PATH + if ! grep -q ".local/bin" "$HOME/.bashrc"; then + echo 'export PATH="$PATH:$HOME/.local/bin"' >> "$HOME/.bashrc" + fi + export PATH="$PATH:$HOME/.local/bin" + + # Verify Aider installation + if command -v aider >/dev/null 2>&1; then + log "SUCCESS: Aider installed successfully" + aider --version | tee -a "$HOME/.amazon-q-module/logs/install.log" + else + log "ERROR: Aider installation failed" + exit 1 + fi +fi + +# Create MCP configuration directory +log "INFO: Creating MCP configuration..." +mkdir -p "$HOME/.amazon-q-module/mcp" +mkdir -p "$HOME/.config/coder/mcp" + +# Write MCP configuration if provided +if [ ! -z "$AMAZON_Q_CONFIG" ]; then + echo "$AMAZON_Q_CONFIG" > "$HOME/.amazon-q-module/mcp/config.yaml" + log "INFO: MCP configuration written to $HOME/.amazon-q-module/mcp/config.yaml" + + # Also write to Coder MCP config location for Amazon Q + if [ "$USE_AIDER" = "true" ]; then + echo "$AMAZON_Q_CONFIG" > "$HOME/.config/coder/mcp/aider.yaml" + log "INFO: MCP configuration written for Aider" + else + echo "$AMAZON_Q_CONFIG" > "$HOME/.config/coder/mcp/amazon-q.yaml" + log "INFO: MCP configuration written for Amazon Q" + fi +fi + +log "INFO: Installation completed successfully" \ No newline at end of file diff --git a/registry/saheli/modules/amazon-q/scripts/start.sh b/registry/saheli/modules/amazon-q/scripts/start.sh new file mode 100644 index 00000000..f5df6a5f --- /dev/null +++ b/registry/saheli/modules/amazon-q/scripts/start.sh @@ -0,0 +1,112 @@ +#!/bin/bash +set -o errexit +set -o pipefail + +# Enhanced logging function +log() { + echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$HOME/.amazon-q-module/logs/start.log" +} + +# Create log directory +mkdir -p "$HOME/.amazon-q-module/logs" + +log "INFO: Starting Amazon Q module..." + +# Module directory matching main.tf +MODULE_DIR="$HOME/.amazon-q-module" +mkdir -p "$MODULE_DIR" + +# Add Amazon Q to PATH +export PATH="$PATH:$HOME/q/bin" + +# Set up environment variables for task reporting +export CODER_MCP_APP_STATUS_SLUG="amazon-q" +export CODER_MCP_AI_AGENTAPI_URL="http://localhost:3284" + +# Get template variables +SYSTEM_PROMPT="${system_prompt}" +TASK_PROMPT="${task_prompt}" +FOLDER="${folder}" +USE_AIDER="${use_aider}" +REPORT_TASKS="${report_tasks}" + +# Set AWS environment variables +export AWS_ACCESS_KEY_ID="${aws_access_key_id}" +export AWS_SECRET_ACCESS_KEY="${aws_secret_access_key}" +export AWS_REGION="${aws_region}" +export AWS_PROFILE="${aws_profile}" + +# Configure MCP if task reporting is enabled +if [ "$REPORT_TASKS" = "true" ]; then + log "INFO: Configuring MCP for task reporting..." + + # Set the workdir for Amazon Q + WORKDIR="${FOLDER:-/home/coder}" + + # Configure MCP with Amazon Q specific settings + export CODER_MCP_AMAZON_Q_TASK_PROMPT="$TASK_PROMPT" + export CODER_MCP_AMAZON_Q_SYSTEM_PROMPT="$SYSTEM_PROMPT" + + # Run MCP configuration + coder exp mcp configure amazon-q "$WORKDIR" || { + log "WARNING: MCP configuration failed, continuing without task reporting" + } +fi + +# Handle task prompt and system prompt +if [ ! -z "$TASK_PROMPT" ]; then + log "INFO: Starting with task prompt" + PROMPT="Review your instructions. Every step of the way, report tasks to Coder with proper descriptions and statuses. Your task at hand: $TASK_PROMPT" + PROMPT_FILE="$MODULE_DIR/prompt.txt" + echo -n "$PROMPT" > "$PROMPT_FILE" + + if [ "$USE_AIDER" = "true" ]; then + AMAZON_Q_ARGS=(--instructions "$PROMPT_FILE") + else + # For Amazon Q, we'll need to pass the prompt differently + AMAZON_Q_ARGS=() + fi +else + log "INFO: Starting without a prompt" + AMAZON_Q_ARGS=() +fi + +# Signal handling for graceful shutdown +cleanup() { + log "INFO: Received shutdown signal, cleaning up..." + exit 0 +} +trap cleanup SIGTERM SIGINT + +# Change to the specified folder +cd "$FOLDER" + +# Check if we should use Aider or Amazon Q +if [ "$USE_AIDER" = "true" ]; then + log "INFO: Starting Aider with AgentAPI..." + + # Check if Aider is available + if ! command -v aider >/dev/null 2>&1; then + log "ERROR: Aider is not installed or not in PATH" + log "INFO: Please install Aider first: pip install aider-chat" + exit 1 + fi + + # Start Aider with AgentAPI + agentapi server --term-width 67 --term-height 1190 -- \ + bash -c "$(printf '%q ' aider --yes-always "${AMAZON_Q_ARGS[@]}")" +else + log "INFO: Starting Amazon Q with AgentAPI..." + + # Check if Amazon Q is available + if ! command -v q >/dev/null 2>&1; then + log "ERROR: Amazon Q is not installed or not in PATH" + log "INFO: Please run the installation script first" + exit 1 + fi + + # Start Amazon Q via AgentAPI + # Amazon Q needs the --trust-all-tools flag for automated operation + agentapi server --term-width 67 --term-height 1190 -- \ + bash -c "export PATH=\"$PATH:$HOME/q/bin\" && q chat --trust-all-tools" +fi \ No newline at end of file