From 0914e92896b737443d9f488476cc2d47473debf0 Mon Sep 17 00:00:00 2001 From: 35C4n0r Date: Tue, 26 Aug 2025 14:51:29 +0530 Subject: [PATCH 01/11] feat: qol updates for amp --- .../modules/sourcegraph-amp/main.tf | 18 +++- .../sourcegraph-amp/scripts/install.sh | 85 ++++++++++--------- .../modules/sourcegraph-amp/scripts/start.sh | 16 +++- 3 files changed, 72 insertions(+), 47 deletions(-) diff --git a/registry/coder-labs/modules/sourcegraph-amp/main.tf b/registry/coder-labs/modules/sourcegraph-amp/main.tf index 033fc84ed..8442acc29 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/main.tf +++ b/registry/coder-labs/modules/sourcegraph-amp/main.tf @@ -39,7 +39,6 @@ variable "icon" { variable "folder" { type = string description = "The folder to run sourcegraph_amp in." - default = "/home/coder" } variable "install_sourcegraph_amp" { @@ -54,6 +53,18 @@ variable "sourcegraph_amp_api_key" { default = "" } +variable "sourcegraph_amp_version" { + type = string + description = "The version of sourcegraph-amp to install." + default = "" +} + +variable "ai_prompt" { + type = string + description = "Task prompt for the Amp CLI" + default = "" +} + resource "coder_env" "sourcegraph_amp_api_key" { agent_id = var.agent_id name = "SOURCEGRAPH_AMP_API_KEY" @@ -151,7 +162,7 @@ locals { module "agentapi" { source = "registry.coder.com/coder/agentapi/coder" - version = "1.0.1" + version = "1.1.1" agent_id = var.agent_id web_app_slug = local.app_slug @@ -175,6 +186,7 @@ module "agentapi" { chmod +x /tmp/start.sh SOURCEGRAPH_AMP_API_KEY='${var.sourcegraph_amp_api_key}' \ SOURCEGRAPH_AMP_START_DIRECTORY='${var.folder}' \ + SOURCEGRAPH_AMP_TASK_PROMPT='${var.ai_prompt}' \ /tmp/start.sh EOT @@ -186,8 +198,8 @@ module "agentapi" { echo -n '${base64encode(local.install_script)}' | base64 -d > /tmp/install.sh chmod +x /tmp/install.sh ARG_INSTALL_SOURCEGRAPH_AMP='${var.install_sourcegraph_amp}' \ - SOURCEGRAPH_AMP_START_DIRECTORY='${var.folder}' \ ARG_AMP_CONFIG="$(echo -n '${base64encode(jsonencode(local.final_config))}' | base64 -d)" \ + ARG_AMP_VERSION='${var.sourcegraph_amp_version}' \ /tmp/install.sh EOT } diff --git a/registry/coder-labs/modules/sourcegraph-amp/scripts/install.sh b/registry/coder-labs/modules/sourcegraph-amp/scripts/install.sh index 61e498b7b..b1be662d7 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/scripts/install.sh +++ b/registry/coder-labs/modules/sourcegraph-amp/scripts/install.sh @@ -1,12 +1,19 @@ #!/bin/bash set -euo pipefail +source "$HOME"/.bashrc + # ANSI colors BOLD='\033[1m' +ARG_INSTALL_SOURCEGRAPH_AMP=${ARG_INSTALL_SOURCEGRAPH_AMP:-true} +ARG_AMP_VERSION=${ARG_AMP_VERSION:-} +ARG_AMP_CONFIG=${ARG_AMP_CONFIG:-} + echo "--------------------------------" -echo "Install flag: $ARG_INSTALL_SOURCEGRAPH_AMP" -echo "Workspace: $SOURCEGRAPH_AMP_START_DIRECTORY" +printf "Install flag: %s\n" "$ARG_INSTALL_SOURCEGRAPH_AMP" +printf "Amp Version: %s\n" "$ARG_AMP_VERSION" +printf "AMP Config: %s\n" "$ARG_AMP_CONFIG" echo "--------------------------------" # Helper function to check if a command exists @@ -14,53 +21,49 @@ command_exists() { command -v "$1" > /dev/null 2>&1 } -function install_node() { +function check_dependencies() { + if ! command_exists node; then + printf "Error: Node.js is not installed. Please install Node.js manually or use the pre_install_script to install it.\n" + exit 1 + fi + if ! command_exists npm; then - printf "npm not found, checking for Node.js installation...\n" - if ! command_exists node; then - printf "Node.js not found, installing Node.js via NVM...\n" - export NVM_DIR="$HOME/.nvm" - if [ ! -d "$NVM_DIR" ]; then - mkdir -p "$NVM_DIR" - curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash - [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" - else - [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" - fi - - # Temporarily disable nounset (-u) for nvm to avoid PROVIDED_VERSION error - set +u - nvm install --lts - nvm use --lts - nvm alias default node - set -u - - printf "Node.js installed: %s\n" "$(node --version)" - printf "npm installed: %s\n" "$(npm --version)" - else - printf "Node.js is installed but npm is not available. Please install npm manually.\n" - exit 1 - fi + printf "Error: npm is not installed. Please install npm manually or use the pre_install_script to install it.\n" + exit 1 fi + + printf "Node.js version: %s\n" "$(node --version)" + printf "npm version: %s\n" "$(npm --version)" } function install_sourcegraph_amp() { if [ "${ARG_INSTALL_SOURCEGRAPH_AMP}" = "true" ]; then - install_node - - # If nvm is not used, set up user npm global directory - if ! command_exists nvm; then - mkdir -p "$HOME/.npm-global" - npm config set prefix "$HOME/.npm-global" - export PATH="$HOME/.npm-global/bin:$PATH" - if ! grep -q "export PATH=$HOME/.npm-global/bin:\$PATH" ~/.bashrc; then - echo "export PATH=$HOME/.npm-global/bin:\$PATH" >> ~/.bashrc - fi + check_dependencies + + printf "%s Installing Sourcegraph amp\n" "${BOLD}" + + NPM_GLOBAL_PREFIX="${HOME}/.npm-global" + if [ ! -d "$NPM_GLOBAL_PREFIX" ]; then + mkdir -p "$NPM_GLOBAL_PREFIX" fi - printf "%s Installing Sourcegraph AMP CLI...\n" "${BOLD}" - npm install -g @sourcegraph/amp@0.0.1754179307-gba1f97 - printf "%s Successfully installed Sourcegraph AMP CLI. Version: %s\n" "${BOLD}" "$(amp --version)" + npm config set prefix "$NPM_GLOBAL_PREFIX" + + export PATH="$NPM_GLOBAL_PREFIX/bin:$PATH" + + if [ -n "$ARG_AMP_VERSION" ]; then + npm install -g "@sourcegraph/amp@$ARG_AMP_VERSION" + else + npm install -g "@sourcegraph/amp" + fi + + if ! grep -q "export PATH=\"\$HOME/.npm-global/bin:\$PATH\"" "$HOME/.bashrc"; then + echo 'export PATH="$HOME/.npm-global/bin:$PATH"' >> "$HOME/.bashrc" + fi + + printf "%s Successfully installed Sourcegraph Amp CLI. Version: %s\n" "${BOLD}" "$(amp --version)" + else + printf "Skipping Sourcegraph Amp CLI installation (install_sourcegraph_amp=false)\n" fi } diff --git a/registry/coder-labs/modules/sourcegraph-amp/scripts/start.sh b/registry/coder-labs/modules/sourcegraph-amp/scripts/start.sh index 252b343f8..1facee4d7 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/scripts/start.sh +++ b/registry/coder-labs/modules/sourcegraph-amp/scripts/start.sh @@ -18,6 +18,16 @@ function ensure_command() { } } +SOURCEGRAPH_AMP_START_DIRECTORY=${SOURCEGRAPH_AMP_START_DIRECTORY:-"$HOME"} +SOURCEGRAPH_AMP_API_KEY=${SOURCEGRAPH_AMP_API_KEY:-} +SOURCEGRAPH_AMP_TASK_PROMPT=${SOURCEGRAPH_AMP_TASK_PROMPT:-} + +echo "--------------------------------" +printf "API Key: %s\n" "$SOURCEGRAPH_AMP_API_KEY" +printf "Workspace: %s\n" "$SOURCEGRAPH_AMP_START_DIRECTORY" +printf "Task Prompt: %s\n" "$SOURCEGRAPH_AMP_TASK_PROMPT" +echo "--------------------------------" + ensure_command amp echo "AMP version: $(amp --version)" @@ -38,12 +48,12 @@ else fi if [ -n "${SOURCEGRAPH_AMP_TASK_PROMPT:-}" ]; then - printf "sourcegraph amp task prompt provided : $SOURCEGRAPH_AMP_TASK_PROMPT" + printf "sourcegraph amp task prompt provided : %s" "$SOURCEGRAPH_AMP_TASK_PROMPT" PROMPT="Every step of the way, report tasks to Coder with proper descriptions and statuses. Your task at hand: $SOURCEGRAPH_AMP_TASK_PROMPT" # Pipe the prompt into amp, which will be run inside agentapi - agentapi server --term-width=67 --term-height=1190 -- bash -c "echo \"$PROMPT\" | amp" + agentapi server --type amp --term-width=67 --term-height=1190 -- bash -c "echo \"$PROMPT\" | amp" else printf "No task prompt given.\n" - agentapi server --term-width=67 --term-height=1190 -- amp + agentapi server --type amp --term-width=67 --term-height=1190 -- amp fi From a830a79f14d979702a5d0f8db73f77199f0fdbc0 Mon Sep 17 00:00:00 2001 From: 35C4n0r Date: Tue, 26 Aug 2025 15:49:05 +0530 Subject: [PATCH 02/11] feat: update readme and agentapi version --- .../modules/sourcegraph-amp/README.md | 42 ++++++----------- .../modules/sourcegraph-amp/main.test.ts | 46 ++++++++++++++++--- .../modules/sourcegraph-amp/main.tf | 15 ++++-- .../sourcegraph-amp/scripts/install.sh | 6 ++- .../modules/sourcegraph-amp/scripts/start.sh | 24 +++++----- 5 files changed, 81 insertions(+), 52 deletions(-) diff --git a/registry/coder-labs/modules/sourcegraph-amp/README.md b/registry/coder-labs/modules/sourcegraph-amp/README.md index 6d3c8dfff..4af6797c2 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/README.md +++ b/registry/coder-labs/modules/sourcegraph-amp/README.md @@ -13,18 +13,17 @@ Run [Amp CLI](https://ampcode.com/) in your workspace to access Sourcegraph's AI ```tf module "amp-cli" { source = "registry.coder.com/coder-labs/sourcegraph-amp/coder" - version = "1.0.2" + version = "2.0.0" agent_id = coder_agent.example.id sourcegraph_amp_api_key = var.sourcegraph_amp_api_key - install_sourcegraph_amp = true - agentapi_version = "latest" + folder = "/home/coder/project" } ``` ## Prerequisites - Include the [Coder Login](https://registry.coder.com/modules/coder-login/coder) module in your template -- Node.js and npm are automatically installed (via NVM) if not already available +- **Node.js and npm must be sourced/available before the amp cli installs** - ensure they are installed in your workspace image or via earlier provisioning steps ## Usage Example @@ -35,20 +34,6 @@ data "coder_parameter" "ai_prompt" { type = "string" default = "" mutable = true - -} - -# Set system prompt for Amp CLI via environment variables -resource "coder_agent" "main" { - # ... - env = { - SOURCEGRAPH_AMP_SYSTEM_PROMPT = <<-EOT - You are an Amp assistant that helps developers debug and write code efficiently. - - Always log task status to Coder. - EOT - SOURCEGRAPH_AMP_TASK_PROMPT = data.coder_parameter.ai_prompt.value - } } variable "sourcegraph_amp_api_key" { @@ -60,18 +45,20 @@ variable "sourcegraph_amp_api_key" { module "amp-cli" { count = data.coder_workspace.me.start_count source = "registry.coder.com/coder-labs/sourcegraph-amp/coder" - version = "1.0.2" + sourcegraph_amp_version = "2.0.0" agent_id = coder_agent.example.id - sourcegraph_amp_api_key = var.sourcegraph_amp_api_key # recommended for authenticated usage + sourcegraph_amp_api_key = var.sourcegraph_amp_api_key # recommended for tasks usage install_sourcegraph_amp = true -} -``` + folder = "/home/coder/project" + system_prompt = <<-EOT + You are an Amp assistant that helps developers debug and write code efficiently. -## How it Works + Always log task status to Coder. +EOT + ai_prompt = data.coder_parameter.ai_prompt.value -- **Install**: Installs Sourcegraph Amp CLI using npm (installs Node.js via NVM if required) -- **Start**: Launches Amp CLI in the specified directory, wrapped with AgentAPI to enable tasks and AI interactions -- **Environment Variables**: Sets `SOURCEGRAPH_AMP_API_KEY` and `SOURCEGRAPH_AMP_START_DIRECTORY` for the CLI execution +} +``` ## Troubleshooting @@ -80,7 +67,8 @@ module "amp-cli" { - If AgentAPI fails to start, verify that your container has network access and executable permissions for the scripts > [!IMPORTANT] -> For using **Coder Tasks** with Amp CLI, make sure to pass the `AI Prompt` parameter and set `sourcegraph_amp_api_key`. +> To use tasks with Amp CLI, create a `coder_parameter` named `"AI Prompt"` and pass its value to the amp-cli module's `ai_prompt` variable. The `folder` variable is required for the module to function correctly. +> For using **Coder Tasks** with Amp CLI, make sure to set `sourcegraph_amp_api_key`. > This ensures task reporting and status updates work seamlessly. ## References diff --git a/registry/coder-labs/modules/sourcegraph-amp/main.test.ts b/registry/coder-labs/modules/sourcegraph-amp/main.test.ts index a08497087..82930cc21 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/main.test.ts +++ b/registry/coder-labs/modules/sourcegraph-amp/main.test.ts @@ -43,6 +43,7 @@ const setup = async (props?: SetupProps): Promise<{ id: string }> => { const { id } = await setupUtil({ moduleDir: import.meta.dir, moduleVariables: { + folder: "/home/coder", install_sourcegraph_amp: props?.skipAmpMock ? "true" : "false", install_agentapi: props?.skipAgentAPIMock ? "true" : "false", sourcegraph_amp_model: "test-model", @@ -94,6 +95,33 @@ describe("sourcegraph-amp", async () => { expect(resp).toContain("sourcegraph_amp_api_key provided !"); }); + test("install-latest-version", async () => { + const { id } = await setup({ + skipAmpMock: true, + skipAgentAPIMock: true, + moduleVariables: { + sourcegraph_amp_version: "", + }, + }); + await execModuleScript(id); + await expectAgentAPIStarted(id); + }); + + test("install-specific-version", async () => { + const { id } = await setup({ + skipAmpMock: true, + moduleVariables: { + sourcegraph_amp_version: "0.0.1755964909-g31e083", + }, + }); + await execModuleScript(id); + const resp = await readFileContainer( + id, + "/home/coder/.sourcegraph-amp-module/agentapi-start.log", + ); + expect(resp).toContain("0.0.1755964909-g31e08"); + }); + test("custom-folder", async () => { const folder = "/tmp/sourcegraph-amp-test"; const { id } = await setup({ @@ -104,7 +132,7 @@ describe("sourcegraph-amp", async () => { await execModuleScript(id); const resp = await readFileContainer( id, - "/home/coder/.sourcegraph-amp-module/install.log", + "/home/coder/.sourcegraph-amp-module/agentapi-start.log", ); expect(resp).toContain(folder); }); @@ -131,10 +159,12 @@ describe("sourcegraph-amp", async () => { test("system-prompt", async () => { const prompt = "this is a system prompt for AMP"; - const { id } = await setup(); - await execModuleScript(id, { - SOURCEGRAPH_AMP_SYSTEM_PROMPT: prompt, + const { id } = await setup({ + moduleVariables: { + system_prompt: prompt + } }); + await execModuleScript(id); const resp = await readFileContainer( id, "/home/coder/.sourcegraph-amp-module/SYSTEM_PROMPT.md", @@ -144,10 +174,12 @@ describe("sourcegraph-amp", async () => { test("task-prompt", async () => { const prompt = "this is a task prompt for AMP"; - const { id } = await setup(); - await execModuleScript(id, { - SOURCEGRAPH_AMP_TASK_PROMPT: prompt, + const { id } = await setup({ + moduleVariables: { + ai_prompt: prompt + } }); + await execModuleScript(id); const resp = await readFileContainer( id, "/home/coder/.sourcegraph-amp-module/agentapi-start.log", diff --git a/registry/coder-labs/modules/sourcegraph-amp/main.tf b/registry/coder-labs/modules/sourcegraph-amp/main.tf index 8442acc29..16af380a6 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/main.tf +++ b/registry/coder-labs/modules/sourcegraph-amp/main.tf @@ -65,6 +65,12 @@ variable "ai_prompt" { default = "" } +variable "system_prompt" { + type = string + description = "System prompt for the Amp CLI" + default = "" +} + resource "coder_env" "sourcegraph_amp_api_key" { agent_id = var.agent_id name = "SOURCEGRAPH_AMP_API_KEY" @@ -80,7 +86,7 @@ variable "install_agentapi" { variable "agentapi_version" { type = string description = "The version of AgentAPI to install." - default = "v0.3.0" + default = "v0.6.1" } variable "pre_install_script" { @@ -184,9 +190,9 @@ module "agentapi" { echo -n '${base64encode(local.start_script)}' | base64 -d > /tmp/start.sh chmod +x /tmp/start.sh - SOURCEGRAPH_AMP_API_KEY='${var.sourcegraph_amp_api_key}' \ - SOURCEGRAPH_AMP_START_DIRECTORY='${var.folder}' \ - SOURCEGRAPH_AMP_TASK_PROMPT='${var.ai_prompt}' \ + ARG_SOURCEGRAPH_AMP_API_KEY='${var.sourcegraph_amp_api_key}' \ + ARG_SOURCEGRAPH_AMP_START_DIRECTORY='${var.folder}' \ + ARG_SOURCEGRAPH_AMP_TASK_PROMPT='${var.ai_prompt}' \ /tmp/start.sh EOT @@ -200,6 +206,7 @@ module "agentapi" { ARG_INSTALL_SOURCEGRAPH_AMP='${var.install_sourcegraph_amp}' \ ARG_AMP_CONFIG="$(echo -n '${base64encode(jsonencode(local.final_config))}' | base64 -d)" \ ARG_AMP_VERSION='${var.sourcegraph_amp_version}' \ + ARG_SOURCEGRAPH_AMP_SYSTEM_PROMPT='${var.system_prompt}' \ /tmp/install.sh EOT } diff --git a/registry/coder-labs/modules/sourcegraph-amp/scripts/install.sh b/registry/coder-labs/modules/sourcegraph-amp/scripts/install.sh index b1be662d7..fe1458b29 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/scripts/install.sh +++ b/registry/coder-labs/modules/sourcegraph-amp/scripts/install.sh @@ -9,11 +9,13 @@ BOLD='\033[1m' ARG_INSTALL_SOURCEGRAPH_AMP=${ARG_INSTALL_SOURCEGRAPH_AMP:-true} ARG_AMP_VERSION=${ARG_AMP_VERSION:-} ARG_AMP_CONFIG=${ARG_AMP_CONFIG:-} +ARG_SOURCEGRAPH_AMP_SYSTEM_PROMPT=${ARG_SOURCEGRAPH_AMP_SYSTEM_PROMPT:-} echo "--------------------------------" printf "Install flag: %s\n" "$ARG_INSTALL_SOURCEGRAPH_AMP" printf "Amp Version: %s\n" "$ARG_AMP_VERSION" printf "AMP Config: %s\n" "$ARG_AMP_CONFIG" +printf "System Prompt: %s\n" "$ARG_SOURCEGRAPH_AMP_SYSTEM_PROMPT" echo "--------------------------------" # Helper function to check if a command exists @@ -68,10 +70,10 @@ function install_sourcegraph_amp() { } function setup_system_prompt() { - if [ -n "${SOURCEGRAPH_AMP_SYSTEM_PROMPT:-}" ]; then + if [ -n "${ARG_SOURCEGRAPH_AMP_SYSTEM_PROMPT:-}" ]; then echo "Setting Sourcegraph AMP system prompt..." mkdir -p "$HOME/.sourcegraph-amp-module" - echo "$SOURCEGRAPH_AMP_SYSTEM_PROMPT" > "$HOME/.sourcegraph-amp-module/SYSTEM_PROMPT.md" + echo "$ARG_SOURCEGRAPH_AMP_SYSTEM_PROMPT" > "$HOME/.sourcegraph-amp-module/SYSTEM_PROMPT.md" echo "System prompt saved to $HOME/.sourcegraph-amp-module/SYSTEM_PROMPT.md" else echo "No system prompt provided for Sourcegraph AMP." diff --git a/registry/coder-labs/modules/sourcegraph-amp/scripts/start.sh b/registry/coder-labs/modules/sourcegraph-amp/scripts/start.sh index 1facee4d7..48a5c8fe5 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/scripts/start.sh +++ b/registry/coder-labs/modules/sourcegraph-amp/scripts/start.sh @@ -18,20 +18,20 @@ function ensure_command() { } } -SOURCEGRAPH_AMP_START_DIRECTORY=${SOURCEGRAPH_AMP_START_DIRECTORY:-"$HOME"} -SOURCEGRAPH_AMP_API_KEY=${SOURCEGRAPH_AMP_API_KEY:-} -SOURCEGRAPH_AMP_TASK_PROMPT=${SOURCEGRAPH_AMP_TASK_PROMPT:-} +ARG_SOURCEGRAPH_AMP_START_DIRECTORY=${ARG_SOURCEGRAPH_AMP_START_DIRECTORY:-"$HOME"} +ARG_SOURCEGRAPH_AMP_API_KEY=${ARG_SOURCEGRAPH_AMP_API_KEY:-} +ARG_SOURCEGRAPH_AMP_TASK_PROMPT=${ARG_SOURCEGRAPH_AMP_TASK_PROMPT:-} echo "--------------------------------" -printf "API Key: %s\n" "$SOURCEGRAPH_AMP_API_KEY" -printf "Workspace: %s\n" "$SOURCEGRAPH_AMP_START_DIRECTORY" -printf "Task Prompt: %s\n" "$SOURCEGRAPH_AMP_TASK_PROMPT" +printf "API Key: %s\n" "$ARG_SOURCEGRAPH_AMP_API_KEY" +printf "Workspace: %s\n" "$ARG_SOURCEGRAPH_AMP_START_DIRECTORY" +printf "Task Prompt: %s\n" "$ARG_SOURCEGRAPH_AMP_TASK_PROMPT" echo "--------------------------------" ensure_command amp echo "AMP version: $(amp --version)" -dir="$SOURCEGRAPH_AMP_START_DIRECTORY" +dir="$ARG_SOURCEGRAPH_AMP_START_DIRECTORY" if [[ -d "$dir" ]]; then echo "Using existing directory: $dir" else @@ -40,16 +40,16 @@ else fi cd "$dir" -if [ -n "$SOURCEGRAPH_AMP_API_KEY" ]; then +if [ -n "$ARG_SOURCEGRAPH_AMP_API_KEY" ]; then printf "sourcegraph_amp_api_key provided !\n" - export AMP_API_KEY=$SOURCEGRAPH_AMP_API_KEY + export AMP_API_KEY=$ARG_SOURCEGRAPH_AMP_API_KEY else printf "sourcegraph_amp_api_key not provided\n" fi -if [ -n "${SOURCEGRAPH_AMP_TASK_PROMPT:-}" ]; then - printf "sourcegraph amp task prompt provided : %s" "$SOURCEGRAPH_AMP_TASK_PROMPT" - PROMPT="Every step of the way, report tasks to Coder with proper descriptions and statuses. Your task at hand: $SOURCEGRAPH_AMP_TASK_PROMPT" +if [ -n "${ARG_SOURCEGRAPH_AMP_TASK_PROMPT:-}" ]; then + printf "sourcegraph amp task prompt provided : %s" "$ARG_SOURCEGRAPH_AMP_TASK_PROMPT" + PROMPT="Every step of the way, report tasks to Coder with proper descriptions and statuses. Your task at hand: $ARG_SOURCEGRAPH_AMP_TASK_PROMPT" # Pipe the prompt into amp, which will be run inside agentapi agentapi server --type amp --term-width=67 --term-height=1190 -- bash -c "echo \"$PROMPT\" | amp" From 0cf9f3e948c5a9c2e17c4335a2def2cd4969cd76 Mon Sep 17 00:00:00 2001 From: 35C4n0r Date: Sun, 7 Sep 2025 00:57:06 +0530 Subject: [PATCH 03/11] feat: refactor --- .../modules/sourcegraph-amp/README.md | 40 +++--- .../modules/sourcegraph-amp/main.test.ts | 83 ++++++------ .../modules/sourcegraph-amp/main.tf | 126 +++++++++++------- .../sourcegraph-amp/scripts/install.sh | 42 +++--- .../modules/sourcegraph-amp/scripts/start.sh | 34 +++-- 5 files changed, 185 insertions(+), 140 deletions(-) diff --git a/registry/coder-labs/modules/sourcegraph-amp/README.md b/registry/coder-labs/modules/sourcegraph-amp/README.md index 4af6797c2..528a3e708 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/README.md +++ b/registry/coder-labs/modules/sourcegraph-amp/README.md @@ -1,5 +1,5 @@ --- -display_name: Amp CLI +display_name: Amp icon: ../../../../.icons/sourcegraph-amp.svg description: Sourcegraph's AI coding agent with deep codebase understanding and intelligent code search capabilities verified: false @@ -12,17 +12,16 @@ Run [Amp CLI](https://ampcode.com/) in your workspace to access Sourcegraph's AI ```tf module "amp-cli" { - source = "registry.coder.com/coder-labs/sourcegraph-amp/coder" - version = "2.0.0" - agent_id = coder_agent.example.id - sourcegraph_amp_api_key = var.sourcegraph_amp_api_key - folder = "/home/coder/project" + source = "registry.coder.com/coder-labs/sourcegraph-amp/coder" + version = "2.0.0" + agent_id = coder_agent.example.id + amp_api_key = var.amp_api_key + workdir = "/home/coder/project" } ``` ## Prerequisites -- Include the [Coder Login](https://registry.coder.com/modules/coder-login/coder) module in your template - **Node.js and npm must be sourced/available before the amp cli installs** - ensure they are installed in your workspace image or via earlier provisioning steps ## Usage Example @@ -36,39 +35,40 @@ data "coder_parameter" "ai_prompt" { mutable = true } -variable "sourcegraph_amp_api_key" { +variable "amp_api_key" { type = string description = "Sourcegraph Amp API key. Get one at https://ampcode.com/settings" sensitive = true } module "amp-cli" { - count = data.coder_workspace.me.start_count - source = "registry.coder.com/coder-labs/sourcegraph-amp/coder" - sourcegraph_amp_version = "2.0.0" - agent_id = coder_agent.example.id - sourcegraph_amp_api_key = var.sourcegraph_amp_api_key # recommended for tasks usage - install_sourcegraph_amp = true - folder = "/home/coder/project" - system_prompt = <<-EOT + count = data.coder_workspace.me.start_count + source = "registry.coder.com/coder-labs/sourcegraph-amp/coder" + amp_version = "2.0.0" + agent_id = coder_agent.example.id + amp_api_key = var.amp_api_key # recommended for tasks usage + install_amp = true + workdir = "/home/coder/project" + instruction_prompt = <<-EOT You are an Amp assistant that helps developers debug and write code efficiently. Always log task status to Coder. EOT - ai_prompt = data.coder_parameter.ai_prompt.value + ai_prompt = data.coder_parameter.ai_prompt.value + report_tasks = true } ``` ## Troubleshooting -- If `amp` is not found, ensure `install_sourcegraph_amp = true` and your API key is valid -- Logs are written under `/home/coder/.sourcegraph-amp-module/` (`install.log`, `agentapi-start.log`) for debugging +- If `amp` is not found, ensure `install_amp = true` and your API key is valid +- Logs are written under `/home/coder/.amp-module/` (`install.log`, `agentapi-start.log`) for debugging - If AgentAPI fails to start, verify that your container has network access and executable permissions for the scripts > [!IMPORTANT] > To use tasks with Amp CLI, create a `coder_parameter` named `"AI Prompt"` and pass its value to the amp-cli module's `ai_prompt` variable. The `folder` variable is required for the module to function correctly. -> For using **Coder Tasks** with Amp CLI, make sure to set `sourcegraph_amp_api_key`. +> For using **Coder Tasks** with Amp CLI, make sure to set `amp_api_key`. > This ensures task reporting and status updates work seamlessly. ## References diff --git a/registry/coder-labs/modules/sourcegraph-amp/main.test.ts b/registry/coder-labs/modules/sourcegraph-amp/main.test.ts index 82930cc21..4639bcd1d 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/main.test.ts +++ b/registry/coder-labs/modules/sourcegraph-amp/main.test.ts @@ -43,10 +43,9 @@ const setup = async (props?: SetupProps): Promise<{ id: string }> => { const { id } = await setupUtil({ moduleDir: import.meta.dir, moduleVariables: { - folder: "/home/coder", - install_sourcegraph_amp: props?.skipAmpMock ? "true" : "false", + workdir: "/home/coder", + install_amp: props?.skipAmpMock ? "true" : "false", install_agentapi: props?.skipAgentAPIMock ? "true" : "false", - sourcegraph_amp_model: "test-model", ...props?.moduleVariables, }, registerCleanup, @@ -69,38 +68,38 @@ const setup = async (props?: SetupProps): Promise<{ id: string }> => { setDefaultTimeout(60 * 1000); -describe("sourcegraph-amp", async () => { +describe("amp", async () => { beforeAll(async () => { await runTerraformInit(import.meta.dir); }); - test("happy-path", async () => { - const { id } = await setup(); - await execModuleScript(id); - await expectAgentAPIStarted(id); - }); - - test("api-key", async () => { - const apiKey = "test-api-key-123"; - const { id } = await setup({ - moduleVariables: { - sourcegraph_amp_api_key: apiKey, - }, - }); - await execModuleScript(id); - const resp = await readFileContainer( - id, - "/home/coder/.sourcegraph-amp-module/agentapi-start.log", - ); - expect(resp).toContain("sourcegraph_amp_api_key provided !"); - }); - + // test("happy-path", async () => { + // const { id } = await setup(); + // await execModuleScript(id); + // await expectAgentAPIStarted(id); + // }); + // + // test("api-key", async () => { + // const apiKey = "test-api-key-123"; + // const { id } = await setup({ + // moduleVariables: { + // amp_api_key: apiKey, + // }, + // }); + // await execModuleScript(id); + // const resp = await readFileContainer( + // id, + // "/home/coder/.amp-module/agentapi-start.log", + // ); + // expect(resp).toContain("amp_api_key provided !"); + // }); + // test("install-latest-version", async () => { const { id } = await setup({ skipAmpMock: true, skipAgentAPIMock: true, moduleVariables: { - sourcegraph_amp_version: "", + amp_version: "", }, }); await execModuleScript(id); @@ -111,30 +110,30 @@ describe("sourcegraph-amp", async () => { const { id } = await setup({ skipAmpMock: true, moduleVariables: { - sourcegraph_amp_version: "0.0.1755964909-g31e083", + amp_version: "0.0.1755964909-g31e083", }, }); await execModuleScript(id); const resp = await readFileContainer( id, - "/home/coder/.sourcegraph-amp-module/agentapi-start.log", + "/home/coder/.amp-module/agentapi-start.log", ); expect(resp).toContain("0.0.1755964909-g31e08"); }); - test("custom-folder", async () => { - const folder = "/tmp/sourcegraph-amp-test"; + test("custom-workdir", async () => { + const workdir = "/tmp/amp-test"; const { id } = await setup({ moduleVariables: { - folder, + workdir, }, }); await execModuleScript(id); const resp = await readFileContainer( id, - "/home/coder/.sourcegraph-amp-module/agentapi-start.log", + "/home/coder/.amp-module/agentapi-start.log", ); - expect(resp).toContain(folder); + expect(resp).toContain(workdir); }); test("pre-post-install-scripts", async () => { @@ -147,32 +146,32 @@ describe("sourcegraph-amp", async () => { await execModuleScript(id); const preLog = await readFileContainer( id, - "/home/coder/.sourcegraph-amp-module/pre_install.log", + "/home/coder/.amp-module/pre_install.log", ); expect(preLog).toContain("pre-install-script"); const postLog = await readFileContainer( id, - "/home/coder/.sourcegraph-amp-module/post_install.log", + "/home/coder/.amp-module/post_install.log", ); expect(postLog).toContain("post-install-script"); }); - test("system-prompt", async () => { - const prompt = "this is a system prompt for AMP"; + test("instruction-prompt", async () => { + const prompt = "this is a instruction prompt for AMP"; const { id } = await setup({ moduleVariables: { - system_prompt: prompt + instruction_prompt: prompt } }); await execModuleScript(id); const resp = await readFileContainer( id, - "/home/coder/.sourcegraph-amp-module/SYSTEM_PROMPT.md", + "/home/coder/.config/AGENTS.md", ); expect(resp).toContain(prompt); }); - test("task-prompt", async () => { + test("ai-prompt", async () => { const prompt = "this is a task prompt for AMP"; const { id } = await setup({ moduleVariables: { @@ -182,8 +181,8 @@ describe("sourcegraph-amp", async () => { await execModuleScript(id); const resp = await readFileContainer( id, - "/home/coder/.sourcegraph-amp-module/agentapi-start.log", + "/home/coder/.amp-module/agentapi-start.log", ); - expect(resp).toContain(`sourcegraph amp task prompt provided : ${prompt}`); + expect(resp).toContain(`amp task prompt provided : ${prompt}`); }); }); diff --git a/registry/coder-labs/modules/sourcegraph-amp/main.tf b/registry/coder-labs/modules/sourcegraph-amp/main.tf index 16af380a6..e9a3693ca 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/main.tf +++ b/registry/coder-labs/modules/sourcegraph-amp/main.tf @@ -36,69 +36,95 @@ variable "icon" { default = "/icon/sourcegraph-amp.svg" } -variable "folder" { +variable "workdir" { type = string - description = "The folder to run sourcegraph_amp in." + description = "The folder to run AMP CLI in." } -variable "install_sourcegraph_amp" { +variable "install_agentapi" { type = bool - description = "Whether to install sourcegraph-amp." + description = "Whether to install AgentAPI." default = true } -variable "sourcegraph_amp_api_key" { +variable "agentapi_version" { type = string - description = "sourcegraph-amp API Key" - default = "" + description = "The version of AgentAPI to install." + default = "v0.6.1" } -variable "sourcegraph_amp_version" { +variable "cli_app" { + type = bool + description = "Whether to create a CLI app for Claude Code" + default = false +} + +variable "web_app_display_name" { type = string - description = "The version of sourcegraph-amp to install." - default = "" + description = "Display name for the web app" + default = "Amp" } -variable "ai_prompt" { +variable "cli_app_display_name" { type = string - description = "Task prompt for the Amp CLI" - default = "" + description = "Display name for the CLI app" + default = "Amp CLI" } -variable "system_prompt" { +variable "pre_install_script" { type = string - description = "System prompt for the Amp CLI" - default = "" + description = "Custom script to run before installing amp cli" + default = null } -resource "coder_env" "sourcegraph_amp_api_key" { - agent_id = var.agent_id - name = "SOURCEGRAPH_AMP_API_KEY" - value = var.sourcegraph_amp_api_key +variable "post_install_script" { + type = string + description = "Custom script to run after installing amp cli." + default = null } -variable "install_agentapi" { +variable "report_tasks" { type = bool - description = "Whether to install AgentAPI." + description = "Whether to enable task reporting to Coder UI" default = true } -variable "agentapi_version" { +# ------------------------------------------ + +variable "install_amp" { + type = bool + description = "Whether to install amp cli." + default = true +} + +variable "amp_api_key" { type = string - description = "The version of AgentAPI to install." - default = "v0.6.1" + description = "amp cli API Key" + default = "" } -variable "pre_install_script" { +variable "amp_version" { type = string - description = "Custom script to run before installing sourcegraph_amp" - default = null + description = "The version of amp cli to install." + default = "" } -variable "post_install_script" { +variable "ai_prompt" { type = string - description = "Custom script to run after installing sourcegraph_amp." - default = null + description = "Task prompt for the Amp CLI" + default = "" +} + +variable "instruction_prompt" { + type = string + description = "Instruction prompt for the Amp CLI. https://ampcode.com/manual#AGENTS.md" + default = "" +} + +resource "coder_env" "amp_api_key" { + agent_id = var.agent_id + name = "AMP_API_KEY" + value = var.amp_api_key } variable "base_amp_config" { @@ -119,12 +145,16 @@ variable "base_amp_config" { default = "" } -variable "additional_mcp_servers" { +variable "mcp" { type = string description = "Additional MCP servers configuration in JSON format to append to amp.mcpServers." default = null } +data "external" "env" { + program = ["sh", "-c", "echo '{\"CODER_AGENT_TOKEN\":\"'$CODER_AGENT_TOKEN'\",\"CODER_AGENT_URL\":\"'$CODER_AGENT_URL'\"}'"] +} + locals { app_slug = "amp" @@ -142,14 +172,16 @@ locals { "command" = "coder" "args" = ["exp", "mcp", "server"] "env" = { - "CODER_MCP_APP_STATUS_SLUG" = local.app_slug - "CODER_MCP_AI_AGENTAPI_URL" = "http://localhost:3284" + "CODER_MCP_APP_STATUS_SLUG" = var.report_tasks == true ? local.app_slug : "" + "CODER_MCP_AI_AGENTAPI_URL" = var.report_tasks == true ? "http://localhost:3284" : "" + "CODER_AGENT_TOKEN" = data.external.env.result.CODER_AGENT_TOKEN + "CODER_AGENT_URL" = data.external.env.result.CODER_AGENT_URL } "type" = "stdio" } } - additional_mcp = var.additional_mcp_servers != null ? jsondecode(var.additional_mcp_servers) : {} + additional_mcp = var.mcp != null ? jsondecode(var.mcp) : {} merged_mcp_servers = merge( lookup(local.user_config, "amp.mcpServers", {}), @@ -163,7 +195,7 @@ locals { install_script = file("${path.module}/scripts/install.sh") start_script = file("${path.module}/scripts/start.sh") - module_dir_name = ".sourcegraph-amp-module" + module_dir_name = ".amp-module" } module "agentapi" { @@ -175,9 +207,10 @@ module "agentapi" { web_app_order = var.order web_app_group = var.group web_app_icon = var.icon - web_app_display_name = "Sourcegraph Amp" - cli_app_slug = "${local.app_slug}-cli" - cli_app_display_name = "Sourcegraph Amp CLI" + web_app_display_name = var.web_app_display_name + cli_app = var.cli_app + cli_app_slug = var.cli_app ? "${local.app_slug}-cli" : null + cli_app_display_name = var.cli_app ? var.cli_app_display_name : null module_dir_name = local.module_dir_name install_agentapi = var.install_agentapi agentapi_version = var.agentapi_version @@ -190,9 +223,10 @@ module "agentapi" { echo -n '${base64encode(local.start_script)}' | base64 -d > /tmp/start.sh chmod +x /tmp/start.sh - ARG_SOURCEGRAPH_AMP_API_KEY='${var.sourcegraph_amp_api_key}' \ - ARG_SOURCEGRAPH_AMP_START_DIRECTORY='${var.folder}' \ - ARG_SOURCEGRAPH_AMP_TASK_PROMPT='${var.ai_prompt}' \ + ARG_AMP_API_KEY='${var.amp_api_key}' \ + ARG_AMP_START_DIRECTORY='${var.workdir}' \ + ARG_AMP_TASK_PROMPT='${base64encode(var.ai_prompt)}' \ + ARG_REPORT_TASKS='${var.report_tasks}' \ /tmp/start.sh EOT @@ -203,10 +237,10 @@ module "agentapi" { echo -n '${base64encode(local.install_script)}' | base64 -d > /tmp/install.sh chmod +x /tmp/install.sh - ARG_INSTALL_SOURCEGRAPH_AMP='${var.install_sourcegraph_amp}' \ - ARG_AMP_CONFIG="$(echo -n '${base64encode(jsonencode(local.final_config))}' | base64 -d)" \ - ARG_AMP_VERSION='${var.sourcegraph_amp_version}' \ - ARG_SOURCEGRAPH_AMP_SYSTEM_PROMPT='${var.system_prompt}' \ + ARG_INSTALL_AMP='${var.install_amp}' \ + ARG_AMP_CONFIG="${base64encode(jsonencode(local.final_config))}" \ + ARG_AMP_VERSION='${var.amp_version}' \ + ARG_AMP_INSTRUCTION_PROMPT='${base64encode(var.instruction_prompt)}' \ /tmp/install.sh EOT } diff --git a/registry/coder-labs/modules/sourcegraph-amp/scripts/install.sh b/registry/coder-labs/modules/sourcegraph-amp/scripts/install.sh index fe1458b29..e88b2d989 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/scripts/install.sh +++ b/registry/coder-labs/modules/sourcegraph-amp/scripts/install.sh @@ -6,16 +6,16 @@ source "$HOME"/.bashrc # ANSI colors BOLD='\033[1m' -ARG_INSTALL_SOURCEGRAPH_AMP=${ARG_INSTALL_SOURCEGRAPH_AMP:-true} +ARG_INSTALL_AMP=${ARG_INSTALL_AMP:-true} ARG_AMP_VERSION=${ARG_AMP_VERSION:-} -ARG_AMP_CONFIG=${ARG_AMP_CONFIG:-} -ARG_SOURCEGRAPH_AMP_SYSTEM_PROMPT=${ARG_SOURCEGRAPH_AMP_SYSTEM_PROMPT:-} +ARG_AMP_INSTRUCTION_PROMPT=$(echo -n "${ARG_AMP_INSTRUCTION_PROMPT:-}" | base64 -d) +ARG_AMP_CONFIG=$(echo -n "${ARG_AMP_CONFIG:-}" | base64 -d) echo "--------------------------------" -printf "Install flag: %s\n" "$ARG_INSTALL_SOURCEGRAPH_AMP" +printf "Install flag: %s\n" "$ARG_INSTALL_AMP" printf "Amp Version: %s\n" "$ARG_AMP_VERSION" printf "AMP Config: %s\n" "$ARG_AMP_CONFIG" -printf "System Prompt: %s\n" "$ARG_SOURCEGRAPH_AMP_SYSTEM_PROMPT" +printf "Instruction Prompt: %s\n" "$ARG_AMP_INSTRUCTION_PROMPT" echo "--------------------------------" # Helper function to check if a command exists @@ -38,8 +38,8 @@ function check_dependencies() { printf "npm version: %s\n" "$(npm --version)" } -function install_sourcegraph_amp() { - if [ "${ARG_INSTALL_SOURCEGRAPH_AMP}" = "true" ]; then +function install_amp() { + if [ "${ARG_INSTALL_AMP}" = "true" ]; then check_dependencies printf "%s Installing Sourcegraph amp\n" "${BOLD}" @@ -65,18 +65,18 @@ function install_sourcegraph_amp() { printf "%s Successfully installed Sourcegraph Amp CLI. Version: %s\n" "${BOLD}" "$(amp --version)" else - printf "Skipping Sourcegraph Amp CLI installation (install_sourcegraph_amp=false)\n" + printf "Skipping Sourcegraph Amp CLI installation (install_amp=false)\n" fi } -function setup_system_prompt() { - if [ -n "${ARG_SOURCEGRAPH_AMP_SYSTEM_PROMPT:-}" ]; then - echo "Setting Sourcegraph AMP system prompt..." - mkdir -p "$HOME/.sourcegraph-amp-module" - echo "$ARG_SOURCEGRAPH_AMP_SYSTEM_PROMPT" > "$HOME/.sourcegraph-amp-module/SYSTEM_PROMPT.md" - echo "System prompt saved to $HOME/.sourcegraph-amp-module/SYSTEM_PROMPT.md" +function setup_instruction_prompt() { + if [ -n "${ARG_AMP_INSTRUCTION_PROMPT:-}" ]; then + echo "Setting AMP instruction prompt..." + mkdir -p "$HOME/.config" + echo "$ARG_AMP_INSTRUCTION_PROMPT" > "$HOME/.config/AGENTS.md" + echo "Instruction prompt saved to $HOME/.config/AGENTS.md" else - echo "No system prompt provided for Sourcegraph AMP." + echo "No instruction prompt provided for Sourcegraph AMP." fi } @@ -91,11 +91,17 @@ function configure_amp_settings() { fi echo "Writing AMP configuration to $SETTINGS_PATH" - printf '%s\n' "$ARG_AMP_CONFIG" > "$SETTINGS_PATH" + UPDATED_CONFIG=$(echo "$ARG_AMP_CONFIG" | jq --arg token "$CODER_AGENT_TOKEN" --arg url "$CODER_AGENT_URL" \ + ".[\"amp.mcpServers\"].coder.env += { + \"CODER_AGENT_TOKEN\": \"$CODER_AGENT_TOKEN\", + \"CODER_AGENT_URL\": \"$CODER_AGENT_URL\" + }") + printf "UPDATED_CONFIG: %s\n" "$UPDATED_CONFIG" + printf '%s\n' "$UPDATED_CONFIG" > "$SETTINGS_PATH" echo "AMP configuration complete" } -install_sourcegraph_amp -setup_system_prompt +install_amp +setup_instruction_prompt configure_amp_settings diff --git a/registry/coder-labs/modules/sourcegraph-amp/scripts/start.sh b/registry/coder-labs/modules/sourcegraph-amp/scripts/start.sh index 48a5c8fe5..7df5fa4f4 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/scripts/start.sh +++ b/registry/coder-labs/modules/sourcegraph-amp/scripts/start.sh @@ -18,20 +18,22 @@ function ensure_command() { } } -ARG_SOURCEGRAPH_AMP_START_DIRECTORY=${ARG_SOURCEGRAPH_AMP_START_DIRECTORY:-"$HOME"} -ARG_SOURCEGRAPH_AMP_API_KEY=${ARG_SOURCEGRAPH_AMP_API_KEY:-} -ARG_SOURCEGRAPH_AMP_TASK_PROMPT=${ARG_SOURCEGRAPH_AMP_TASK_PROMPT:-} +ARG_AMP_START_DIRECTORY=${ARG_AMP_START_DIRECTORY:-"$HOME"} +ARG_AMP_API_KEY=${ARG_AMP_API_KEY:-} +ARG_AMP_TASK_PROMPT=$(echo -n "${ARG_AMP_TASK_PROMPT:-}" | base64 -d) +ARG_REPORT_TASKS=${ARG_REPORT_TASKS:-true} echo "--------------------------------" -printf "API Key: %s\n" "$ARG_SOURCEGRAPH_AMP_API_KEY" -printf "Workspace: %s\n" "$ARG_SOURCEGRAPH_AMP_START_DIRECTORY" -printf "Task Prompt: %s\n" "$ARG_SOURCEGRAPH_AMP_TASK_PROMPT" +printf "API Key: %s\n" "$ARG_AMP_API_KEY" +printf "Workspace: %s\n" "$ARG_AMP_START_DIRECTORY" +printf "Task Prompt: %s\n" "$ARG_AMP_TASK_PROMPT" +printf "ARG_REPORT_TASKS: %s\n" "$ARG_REPORT_TASKS" echo "--------------------------------" ensure_command amp echo "AMP version: $(amp --version)" -dir="$ARG_SOURCEGRAPH_AMP_START_DIRECTORY" +dir="$ARG_AMP_START_DIRECTORY" if [[ -d "$dir" ]]; then echo "Using existing directory: $dir" else @@ -40,17 +42,21 @@ else fi cd "$dir" -if [ -n "$ARG_SOURCEGRAPH_AMP_API_KEY" ]; then - printf "sourcegraph_amp_api_key provided !\n" - export AMP_API_KEY=$ARG_SOURCEGRAPH_AMP_API_KEY +if [ -n "$ARG_AMP_API_KEY" ]; then + printf "amp_api_key provided !\n" + export AMP_API_KEY=$ARG_AMP_API_KEY else - printf "sourcegraph_amp_api_key not provided\n" + printf "amp_api_key not provided\n" fi -if [ -n "${ARG_SOURCEGRAPH_AMP_TASK_PROMPT:-}" ]; then - printf "sourcegraph amp task prompt provided : %s" "$ARG_SOURCEGRAPH_AMP_TASK_PROMPT" - PROMPT="Every step of the way, report tasks to Coder with proper descriptions and statuses. Your task at hand: $ARG_SOURCEGRAPH_AMP_TASK_PROMPT" +if [ -n "$ARG_AMP_TASK_PROMPT" ]; then + if [ "$ARG_REPORT_TASKS" == "true" ]; then + printf "amp task prompt provided : %s" "$ARG_AMP_TASK_PROMPT\n" + PROMPT="Every step of the way, report your progress using coder_report_task tool with proper summary and statuses. Your task at hand: $ARG_AMP_TASK_PROMPT" + else + PROMPT="$ARG_AMP_TASK_PROMPT" + fi # Pipe the prompt into amp, which will be run inside agentapi agentapi server --type amp --term-width=67 --term-height=1190 -- bash -c "echo \"$PROMPT\" | amp" else From 8ba00b3ae97ce18af337a4571c3d8c56ed2f2bbe Mon Sep 17 00:00:00 2001 From: 35C4n0r Date: Sun, 7 Sep 2025 11:18:27 +0530 Subject: [PATCH 04/11] chore: refactor --- .../coder-labs/modules/sourcegraph-amp/main.test.ts | 13 +++++-------- .../modules/sourcegraph-amp/scripts/install.sh | 2 +- .../modules/sourcegraph-amp/scripts/start.sh | 1 - 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/registry/coder-labs/modules/sourcegraph-amp/main.test.ts b/registry/coder-labs/modules/sourcegraph-amp/main.test.ts index 4639bcd1d..0ac8bb84b 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/main.test.ts +++ b/registry/coder-labs/modules/sourcegraph-amp/main.test.ts @@ -160,14 +160,11 @@ describe("amp", async () => { const prompt = "this is a instruction prompt for AMP"; const { id } = await setup({ moduleVariables: { - instruction_prompt: prompt - } + instruction_prompt: prompt, + }, }); await execModuleScript(id); - const resp = await readFileContainer( - id, - "/home/coder/.config/AGENTS.md", - ); + const resp = await readFileContainer(id, "/home/coder/.config/AGENTS.md"); expect(resp).toContain(prompt); }); @@ -175,8 +172,8 @@ describe("amp", async () => { const prompt = "this is a task prompt for AMP"; const { id } = await setup({ moduleVariables: { - ai_prompt: prompt - } + ai_prompt: prompt, + }, }); await execModuleScript(id); const resp = await readFileContainer( diff --git a/registry/coder-labs/modules/sourcegraph-amp/scripts/install.sh b/registry/coder-labs/modules/sourcegraph-amp/scripts/install.sh index e88b2d989..c4724efb2 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/scripts/install.sh +++ b/registry/coder-labs/modules/sourcegraph-amp/scripts/install.sh @@ -96,7 +96,7 @@ function configure_amp_settings() { \"CODER_AGENT_TOKEN\": \"$CODER_AGENT_TOKEN\", \"CODER_AGENT_URL\": \"$CODER_AGENT_URL\" }") - printf "UPDATED_CONFIG: %s\n" "$UPDATED_CONFIG" + printf "UPDATED_CONFIG: %s\n" "$UPDATED_CONFIG" printf '%s\n' "$UPDATED_CONFIG" > "$SETTINGS_PATH" echo "AMP configuration complete" diff --git a/registry/coder-labs/modules/sourcegraph-amp/scripts/start.sh b/registry/coder-labs/modules/sourcegraph-amp/scripts/start.sh index 7df5fa4f4..aa07a6184 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/scripts/start.sh +++ b/registry/coder-labs/modules/sourcegraph-amp/scripts/start.sh @@ -49,7 +49,6 @@ else printf "amp_api_key not provided\n" fi - if [ -n "$ARG_AMP_TASK_PROMPT" ]; then if [ "$ARG_REPORT_TASKS" == "true" ]; then printf "amp task prompt provided : %s" "$ARG_AMP_TASK_PROMPT\n" From c5aeb392e04582817524beb45b71fac6a6c241a7 Mon Sep 17 00:00:00 2001 From: 35C4n0r Date: Fri, 12 Sep 2025 18:07:11 +0530 Subject: [PATCH 05/11] chore: resolve amp comments --- registry/coder-labs/modules/sourcegraph-amp/README.md | 2 -- registry/coder-labs/modules/sourcegraph-amp/main.tf | 1 - registry/coder-labs/modules/sourcegraph-amp/scripts/start.sh | 1 - 3 files changed, 4 deletions(-) diff --git a/registry/coder-labs/modules/sourcegraph-amp/README.md b/registry/coder-labs/modules/sourcegraph-amp/README.md index f7bcbf328..7b232cd11 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/README.md +++ b/registry/coder-labs/modules/sourcegraph-amp/README.md @@ -47,14 +47,12 @@ module "amp-cli" { amp_version = "2.0.0" agent_id = coder_agent.example.id amp_api_key = var.amp_api_key # recommended for tasks usage - install_amp = true workdir = "/home/coder/project" instruction_prompt = <<-EOT # Instructions - You are an Amp assistant that helps developers debug and write code efficiently. EOT ai_prompt = data.coder_parameter.ai_prompt.value - report_tasks = true } ``` diff --git a/registry/coder-labs/modules/sourcegraph-amp/main.tf b/registry/coder-labs/modules/sourcegraph-amp/main.tf index e9a3693ca..dfd1ff6d6 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/main.tf +++ b/registry/coder-labs/modules/sourcegraph-amp/main.tf @@ -89,7 +89,6 @@ variable "report_tasks" { default = true } -# ------------------------------------------ variable "install_amp" { type = bool diff --git a/registry/coder-labs/modules/sourcegraph-amp/scripts/start.sh b/registry/coder-labs/modules/sourcegraph-amp/scripts/start.sh index aa07a6184..617abc3d4 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/scripts/start.sh +++ b/registry/coder-labs/modules/sourcegraph-amp/scripts/start.sh @@ -24,7 +24,6 @@ ARG_AMP_TASK_PROMPT=$(echo -n "${ARG_AMP_TASK_PROMPT:-}" | base64 -d) ARG_REPORT_TASKS=${ARG_REPORT_TASKS:-true} echo "--------------------------------" -printf "API Key: %s\n" "$ARG_AMP_API_KEY" printf "Workspace: %s\n" "$ARG_AMP_START_DIRECTORY" printf "Task Prompt: %s\n" "$ARG_AMP_TASK_PROMPT" printf "ARG_REPORT_TASKS: %s\n" "$ARG_REPORT_TASKS" From a6737cba7abb802e564995c6d8b0b6dc4ed21355 Mon Sep 17 00:00:00 2001 From: 35C4n0r <70096901+35C4n0r@users.noreply.github.com> Date: Sat, 13 Sep 2025 21:46:53 +0530 Subject: [PATCH 06/11] Update registry/coder-labs/modules/sourcegraph-amp/README.md Co-authored-by: Atif Ali --- registry/coder-labs/modules/sourcegraph-amp/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/registry/coder-labs/modules/sourcegraph-amp/README.md b/registry/coder-labs/modules/sourcegraph-amp/README.md index 7b232cd11..d649052a0 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/README.md +++ b/registry/coder-labs/modules/sourcegraph-amp/README.md @@ -22,7 +22,7 @@ module "amp-cli" { ## Prerequisites -- **Node.js and npm must be sourced/available before the amp cli installs** - ensure they are installed in your workspace image or via earlier provisioning steps +- **Node.js and npm must be sourced/available before the Amp cli installs** - ensure they are installed in your workspace image or via earlier provisioning steps ## Usage Example From a3566b8345d2e92b4e02ee0075d8b9489ddffd17 Mon Sep 17 00:00:00 2001 From: 35C4n0r Date: Sat, 13 Sep 2025 22:18:46 +0530 Subject: [PATCH 07/11] chore: address comments --- registry/coder-labs/modules/sourcegraph-amp/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/registry/coder-labs/modules/sourcegraph-amp/README.md b/registry/coder-labs/modules/sourcegraph-amp/README.md index d649052a0..8a9e8b8d9 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/README.md +++ b/registry/coder-labs/modules/sourcegraph-amp/README.md @@ -50,7 +50,7 @@ module "amp-cli" { workdir = "/home/coder/project" instruction_prompt = <<-EOT # Instructions - - You are an Amp assistant that helps developers debug and write code efficiently. + - Start every response with `amp > ` EOT ai_prompt = data.coder_parameter.ai_prompt.value From 195b17ae793d1e9a04dbd9b2e79c3401d42ff999 Mon Sep 17 00:00:00 2001 From: DevelopmentCats Date: Thu, 2 Oct 2025 17:03:05 -0500 Subject: [PATCH 08/11] chore: add folder to agentapi --- registry/coder-labs/modules/sourcegraph-amp/main.tf | 1 + 1 file changed, 1 insertion(+) diff --git a/registry/coder-labs/modules/sourcegraph-amp/main.tf b/registry/coder-labs/modules/sourcegraph-amp/main.tf index dfd1ff6d6..559dbce67 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/main.tf +++ b/registry/coder-labs/modules/sourcegraph-amp/main.tf @@ -202,6 +202,7 @@ module "agentapi" { version = "1.1.1" agent_id = var.agent_id + folder = var.workdir web_app_slug = local.app_slug web_app_order = var.order web_app_group = var.group From 2e88f2fc12708c3d9b9346a86038619809683fff Mon Sep 17 00:00:00 2001 From: DevelopmentCats Date: Fri, 3 Oct 2025 15:18:30 -0500 Subject: [PATCH 09/11] fix: simplify user_config assignment in sourcegraph-amp module --- registry/coder-labs/modules/sourcegraph-amp/main.tf | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/registry/coder-labs/modules/sourcegraph-amp/main.tf b/registry/coder-labs/modules/sourcegraph-amp/main.tf index 559dbce67..96b820aa8 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/main.tf +++ b/registry/coder-labs/modules/sourcegraph-amp/main.tf @@ -157,13 +157,12 @@ data "external" "env" { locals { app_slug = "amp" - default_base_config = { + default_base_config = jsonencode({ "amp.anthropic.thinking.enabled" = true "amp.todos.enabled" = true - } + }) - # Use provided config or default, then extract base settings (excluding mcpServers) - user_config = var.base_amp_config != "" ? jsondecode(var.base_amp_config) : local.default_base_config + user_config = jsondecode(var.base_amp_config != "" ? var.base_amp_config : local.default_base_config) base_amp_settings = { for k, v in local.user_config : k => v if k != "amp.mcpServers" } coder_mcp = { From 831a50866c568fae2b40dbd4660af84ba3e888cf Mon Sep 17 00:00:00 2001 From: DevelopmentCats Date: Fri, 3 Oct 2025 15:18:46 -0500 Subject: [PATCH 10/11] test: add tests for custom and default AMP configurations in sourcegraph-amp module --- .../modules/sourcegraph-amp/main.test.ts | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/registry/coder-labs/modules/sourcegraph-amp/main.test.ts b/registry/coder-labs/modules/sourcegraph-amp/main.test.ts index 0ac8bb84b..d8f26e868 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/main.test.ts +++ b/registry/coder-labs/modules/sourcegraph-amp/main.test.ts @@ -182,4 +182,68 @@ describe("amp", async () => { ); expect(resp).toContain(`amp task prompt provided : ${prompt}`); }); + + test("custom-base-config", async () => { + const customConfig = JSON.stringify({ + "amp.anthropic.thinking.enabled": false, + "amp.todos.enabled": false, + "amp.tools.stopTimeout": 900, + "amp.git.commit.ampThread.enabled": true, + }); + const customMcp = JSON.stringify({ + "test-server": { + command: "/usr/bin/test-mcp", + args: ["--test-arg"], + type: "stdio", + }, + }); + const { id } = await setup({ + moduleVariables: { + base_amp_config: customConfig, + mcp: customMcp, + }, + }); + await execModuleScript(id, { + CODER_AGENT_TOKEN: "test-token", + CODER_AGENT_URL: "http://test-url:3000", + }); + const settingsContent = await readFileContainer( + id, + "/home/coder/.config/amp/settings.json", + ); + const settings = JSON.parse(settingsContent); + + expect(settings["amp.anthropic.thinking.enabled"]).toBe(false); + expect(settings["amp.todos.enabled"]).toBe(false); + expect(settings["amp.tools.stopTimeout"]).toBe(900); + expect(settings["amp.git.commit.ampThread.enabled"]).toBe(true); + expect(settings["amp.mcpServers"]).toBeDefined(); + expect(settings["amp.mcpServers"].coder).toBeDefined(); + expect(settings["amp.mcpServers"]["test-server"]).toBeDefined(); + expect(settings["amp.mcpServers"]["test-server"].command).toBe( + "/usr/bin/test-mcp", + ); + expect(settings["amp.mcpServers"]["test-server"].args).toEqual([ + "--test-arg", + ]); + }); + + test("default-base-config", async () => { + const { id } = await setup(); + await execModuleScript(id, { + CODER_AGENT_TOKEN: "test-token", + CODER_AGENT_URL: "http://test-url:3000", + }); + const settingsContent = await readFileContainer( + id, + "/home/coder/.config/amp/settings.json", + ); + const settings = JSON.parse(settingsContent); + + expect(settings["amp.anthropic.thinking.enabled"]).toBe(true); + expect(settings["amp.todos.enabled"]).toBe(true); + expect(settings["amp.mcpServers"]).toBeDefined(); + expect(settings["amp.mcpServers"].coder).toBeDefined(); + expect(settings["amp.mcpServers"].coder.command).toBe("coder"); + }); }); From 6dd1088f749f995932095dd77470f7277972ba25 Mon Sep 17 00:00:00 2001 From: DevelopmentCats Date: Fri, 3 Oct 2025 15:19:06 -0500 Subject: [PATCH 11/11] feat: add base AMP configuration to sourcegraph-amp module README --- .../modules/sourcegraph-amp/README.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/registry/coder-labs/modules/sourcegraph-amp/README.md b/registry/coder-labs/modules/sourcegraph-amp/README.md index 8a9e8b8d9..9f8c03f0f 100644 --- a/registry/coder-labs/modules/sourcegraph-amp/README.md +++ b/registry/coder-labs/modules/sourcegraph-amp/README.md @@ -53,7 +53,23 @@ module "amp-cli" { - Start every response with `amp > ` EOT ai_prompt = data.coder_parameter.ai_prompt.value - + base_amp_config = jsonencode({ + "amp.anthropic.thinking.enabled" = true + "amp.todos.enabled" = true + "amp.tools.stopTimeout" = 600 + "amp.git.commit.ampThread.enabled" = true + "amp.git.commit.coauthor.enabled" = true + "amp.terminal.commands.nodeSpawn.loadProfile" = "daily" + "amp.permissions" = [ + { "tool" : "mcp__coder__*", "action" : "allow" }, + { "tool" : "Bash", "action" : "allow", "context" : "thread" }, + { "tool" : "Bash", "matches" : { "cmd" : ["rm -rf /*", "rm -rf ~/*"] }, "action" : "reject", "context" : "subagent" }, + { "tool" : "edit_file", "action" : "allow" }, + { "tool" : "write_file", "action" : "allow" }, + { "tool" : "read_file", "action" : "allow" }, + { "tool" : "Grep", "action" : "allow" } + ] + }) } ```