From 8c438154557d7d1ad238de6e97f448d749597f58 Mon Sep 17 00:00:00 2001 From: Harsh panwar Date: Tue, 29 Jul 2025 10:13:23 +0530 Subject: [PATCH 1/4] Add main files for Sourcegraph Amp module --- registry/harsh9485/modules/README.md | 12 ++ .../modules/sourcegraph_amp/README.md | 71 ++++++++ .../modules/sourcegraph_amp/main.test.ts | 28 +++ .../harsh9485/modules/sourcegraph_amp/main.tf | 162 ++++++++++++++++++ .../sourcegraph_amp/scripts/install.sh | 38 ++++ .../modules/sourcegraph_amp/scripts/start.sh | 28 +++ 6 files changed, 339 insertions(+) create mode 100644 registry/harsh9485/modules/README.md create mode 100644 registry/harsh9485/modules/sourcegraph_amp/README.md create mode 100644 registry/harsh9485/modules/sourcegraph_amp/main.test.ts create mode 100644 registry/harsh9485/modules/sourcegraph_amp/main.tf create mode 100644 registry/harsh9485/modules/sourcegraph_amp/scripts/install.sh create mode 100644 registry/harsh9485/modules/sourcegraph_amp/scripts/start.sh diff --git a/registry/harsh9485/modules/README.md b/registry/harsh9485/modules/README.md new file mode 100644 index 00000000..9f16b718 --- /dev/null +++ b/registry/harsh9485/modules/README.md @@ -0,0 +1,12 @@ +--- +display_name: "harsh9485" +bio: "Brief description of what you do" +avatar: "./.images/avatar.png" +website: "https://your-website.com" +support_email: "panwarharshwardhan67@gmail.com" +status: "community" +--- + +# Your Name + +Brief description of who you are and what you do. diff --git a/registry/harsh9485/modules/sourcegraph_amp/README.md b/registry/harsh9485/modules/sourcegraph_amp/README.md new file mode 100644 index 00000000..aa898a29 --- /dev/null +++ b/registry/harsh9485/modules/sourcegraph_amp/README.md @@ -0,0 +1,71 @@ +--- +display_name: sourcegraph_amp +description: Describe what this module does +icon: ../../../../.icons/sourcegraph-amp.svg +verified: false +tags: [helper] +--- + +# sourcegraph_amp + + + +```tf +module "sourcegraph_amp" { + count = data.coder_workspace.me.start_count + source = "registry.coder.com/harsh9485/sourcegraph_amp/coder" + version = "1.0.0" +} +``` + + + +## Examples + +### Example 1 + +Install the Dracula theme from [OpenVSX](https://open-vsx.org/): + +```tf +module "sourcegraph_amp" { + count = data.coder_workspace.me.start_count + source = "registry.coder.com/NAMESPACE/sourcegraph_amp/coder" + version = "1.0.0" + agent_id = coder_agent.example.id + extensions = [ + "dracula-theme.theme-dracula" + ] +} +``` + +Enter the `.` into the extensions array and code-server will automatically install on start. + +### Example 2 + +Configure VS Code's [settings.json](https://code.visualstudio.com/docs/getstarted/settings#_settingsjson) file: + +```tf +module "sourcegraph_amp" { + count = data.coder_workspace.me.start_count + source = "registry.coder.com/NAMESPACE/sourcegraph_amp/coder" + version = "1.0.0" + agent_id = coder_agent.example.id + extensions = ["dracula-theme.theme-dracula"] + settings = { + "workbench.colorTheme" = "Dracula" + } +} +``` + +### Example 3 + +Run code-server in the background, don't fetch it from GitHub: + +```tf +module "sourcegraph_amp" { + source = "registry.coder.com/NAMESPACE/sourcegraph_amp/coder" + version = "1.0.0" + agent_id = coder_agent.example.id + offline = true +} +``` diff --git a/registry/harsh9485/modules/sourcegraph_amp/main.test.ts b/registry/harsh9485/modules/sourcegraph_amp/main.test.ts new file mode 100644 index 00000000..89161426 --- /dev/null +++ b/registry/harsh9485/modules/sourcegraph_amp/main.test.ts @@ -0,0 +1,28 @@ +import { describe, it, expect } from "bun:test"; +import { + runTerraformInit, + runTerraformApply, + testRequiredVariables, + findResourceInstance, +} from "~test"; +import path from "path"; + +const moduleDir = path.resolve(__dirname); +const requiredVars = { agent_id: "dummy-agent-id" }; + +describe("sourcegraph-amp module", () => { + it("initializes and applies without errors", async () => { + await runTerraformInit(moduleDir); + testRequiredVariables(moduleDir, requiredVars); + + const state = await runTerraformApply(moduleDir, requiredVars); + const script = findResourceInstance(state, "coder_script"); + + expect(script).toBeDefined(); + expect(script.agent_id).toBe(requiredVars.agent_id); + expect(script.script).toContain("ARG_INSTALL_SOURCEGRAPH_AMP='true'"); + expect(script.script).toContain("ARG_AGENTAPI_VERSION='v0.3.0'"); + expect(script.script).toMatch(/\/tmp\/install\.sh/); + expect(script.script).toMatch(/\/tmp\/start\.sh/); + }); +}); \ No newline at end of file diff --git a/registry/harsh9485/modules/sourcegraph_amp/main.tf b/registry/harsh9485/modules/sourcegraph_amp/main.tf new file mode 100644 index 00000000..b980ff96 --- /dev/null +++ b/registry/harsh9485/modules/sourcegraph_amp/main.tf @@ -0,0 +1,162 @@ +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/sourcegraph-amp.svg" +} + +variable "folder" { + type = string + description = "The folder to run sourcegraph-amp in." + default = "/home/coder/sourcegraph-amp" +} + +variable "install_sourcegraph-amp" { + type = bool + description = "Whether to install sourcegraph-amp." + default = true +} + +variable "sourcegraph-amp_api_key" { + type = string + description = "sourcegraph-amp API Key" + default = "" +} + +resource "coder_env" "sourcegraph-amp_api_key" { + agent_id = var.agent_id + name = "SOURCEGRAPH_AMP_API_KEY" + value = var.sourcegraph-amp_api_key +} + +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 = "v0.3.0" +} + +variable "pre_install_script" { + type = string + description = "Custom script to run before installing sourcegraph-amp" + default = null +} + +variable "post_install_script" { + type = string + description = "Custom script to run after installing sourcegraph-amp." + default = null +} + +locals { + base_extensions = <<-EOT +{ + "coder": { + "args": [ + "exp", + "mcp", + "server" + ], + "command": "coder", + "description": "Report ALL tasks and statuses (in progress, done, failed) you are working on.", + "enabled": true, + "env": { + "CODER_MCP_APP_STATUS_SLUG": "${local.app_slug}", + "CODER_MCP_AI_AGENTAPI_URL": "http://localhost:3284" + }, + "name": "Coder", + "timeout": 3000, + "type": "stdio", + "trust": true + } +} +EOT + + app_slug = "sourcegraph-amp" + install_script = file("${path.module}/scripts/install.sh") + start_script = file("${path.module}/scripts/start.sh") + module_dir_name = ".sourcegraph-amp-module" +} + +module "agentapi" { + source = "registry.coder.com/coder/agentapi/coder" + version = "1.0.1" + + 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 = "Sourcegraph Amp" + cli_app_slug = "${local.app_slug}-cli" + cli_app_display_name = "Sourcegraph Amp CLI" + module_dir_name = local.module_dir_name + install_agentapi = var.install_agentapi + agentapi_version = var.agentapi_version + pre_install_script = var.pre_install_script + post_install_script = var.post_install_script + start_script = <<-EOT + #!/bin/bash + set -o errexit + set -o pipefail + + 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}' \ + /tmp/start.sh + EOT + + install_script = <<-EOT + #!/bin/bash + set -o errexit + set -o pipefail + + 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}' \ + SOURCEGRAPH_AMP_API_KEY='${var.sourcegraph-amp_api_key}' \ + BASE_EXTENSIONS='${replace(local.base_extensions, "'", "'\\''")}' \ + /tmp/install.sh + EOT +} + + diff --git a/registry/harsh9485/modules/sourcegraph_amp/scripts/install.sh b/registry/harsh9485/modules/sourcegraph_amp/scripts/install.sh new file mode 100644 index 00000000..033d5a01 --- /dev/null +++ b/registry/harsh9485/modules/sourcegraph_amp/scripts/install.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash +set -euo pipefail + +# ANSI colors +BOLD='\033[1m' +RESET='\033[0m' + +# Print arguments +echo "--------------------------------" +echo "Install flag: $ARG_INSTALL_SOURCEGRAPH_AMP" +echo "Workspace: $SOURCEGRAPH_AMP_START_DIRECTORY" +echo "--------------------------------" + +# Check for npm/node and install via nvm if missing +function ensure_node() { + if ! command -v npm &>/dev/null; then + echo "npm not found. Installing Node.js via NVM..." + export NVM_DIR="$HOME/.nvm" + mkdir -p "$NVM_DIR" + curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash + # shellcheck source=/dev/null + source "$NVM_DIR/nvm.sh" + nvm install --lts + nvm alias default node + fi +} + +function install_sourcegraph_amp() { + if [[ "$ARG_INSTALL_SOURCEGRAPH_AMP" == "true" ]]; then + ensure_node + printf "%b Installing Sourcegraph AMP CLI...%b\n" "$BOLD" "$RESET" + npm install -g @sourcegraph/amp + export AMP_API_KEY="$SOURCEGRAPH_AMP_API_KEY" + printf "%b Installation complete.%b\n" "$BOLD" "$RESET" + fi +} + +install_sourcegraph_amp \ No newline at end of file diff --git a/registry/harsh9485/modules/sourcegraph_amp/scripts/start.sh b/registry/harsh9485/modules/sourcegraph_amp/scripts/start.sh new file mode 100644 index 00000000..004eeeaf --- /dev/null +++ b/registry/harsh9485/modules/sourcegraph_amp/scripts/start.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Load user environment +# shellcheck source=/dev/null +source "$HOME/.bashrc" +# shellcheck source=/dev/null +source "$HOME/.nvm/nvm.sh" + +function ensure_command() { + command -v "$1" &>/dev/null || { echo "Error: '$1' not found." >&2; exit 1; } +} + +ensure_command amp +echo "AMP version: $(amp --version)" + + +dir="$SOURCEGRAPH_AMP_START_DIRECTORY" +if [[ -d "$dir" ]]; then + echo "Using existing directory: $dir" +else + echo "Creating directory: $dir" + mkdir -p "$dir" +fi +cd "$dir" + +# Launch AgentAPI server with AMP +agentapi server --term-width=67 --term-height=1190 -- amp \ No newline at end of file From 807650f5883dcb86e3c85cccc1aefc25e9bc85bc Mon Sep 17 00:00:00 2001 From: Harsh panwar Date: Thu, 31 Jul 2025 21:30:17 +0530 Subject: [PATCH 2/4] LF-error-solved --- .icons/sourcegraph-amp.svg | 7 +++ .../harsh9485/modules/sourcegraph_amp/main.tf | 43 +++++++++---------- .../sourcegraph_amp/scripts/install.sh | 8 ++-- .../modules/sourcegraph_amp/scripts/start.sh | 2 +- 4 files changed, 33 insertions(+), 27 deletions(-) create mode 100644 .icons/sourcegraph-amp.svg diff --git a/.icons/sourcegraph-amp.svg b/.icons/sourcegraph-amp.svg new file mode 100644 index 00000000..cdcae4f2 --- /dev/null +++ b/.icons/sourcegraph-amp.svg @@ -0,0 +1,7 @@ + + + + + + + diff --git a/registry/harsh9485/modules/sourcegraph_amp/main.tf b/registry/harsh9485/modules/sourcegraph_amp/main.tf index b980ff96..d755b5ab 100644 --- a/registry/harsh9485/modules/sourcegraph_amp/main.tf +++ b/registry/harsh9485/modules/sourcegraph_amp/main.tf @@ -18,7 +18,6 @@ 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)." @@ -40,7 +39,7 @@ variable "icon" { variable "folder" { type = string description = "The folder to run sourcegraph-amp in." - default = "/home/coder/sourcegraph-amp" + default = "/home/coder" } variable "install_sourcegraph-amp" { @@ -87,26 +86,26 @@ variable "post_install_script" { locals { base_extensions = <<-EOT -{ - "coder": { - "args": [ - "exp", - "mcp", - "server" - ], - "command": "coder", - "description": "Report ALL tasks and statuses (in progress, done, failed) you are working on.", - "enabled": true, - "env": { - "CODER_MCP_APP_STATUS_SLUG": "${local.app_slug}", - "CODER_MCP_AI_AGENTAPI_URL": "http://localhost:3284" - }, - "name": "Coder", - "timeout": 3000, - "type": "stdio", - "trust": true - } -} +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 app_slug = "sourcegraph-amp" diff --git a/registry/harsh9485/modules/sourcegraph_amp/scripts/install.sh b/registry/harsh9485/modules/sourcegraph_amp/scripts/install.sh index 033d5a01..de77cfed 100644 --- a/registry/harsh9485/modules/sourcegraph_amp/scripts/install.sh +++ b/registry/harsh9485/modules/sourcegraph_amp/scripts/install.sh @@ -1,9 +1,9 @@ -#!/usr/bin/env bash +#!/bin/bash set -euo pipefail # ANSI colors BOLD='\033[1m' -RESET='\033[0m' + # Print arguments echo "--------------------------------" @@ -28,10 +28,10 @@ function ensure_node() { function install_sourcegraph_amp() { if [[ "$ARG_INSTALL_SOURCEGRAPH_AMP" == "true" ]]; then ensure_node - printf "%b Installing Sourcegraph AMP CLI...%b\n" "$BOLD" "$RESET" + printf "%b Installing Sourcegraph AMP CLI...%b\n" "$BOLD" npm install -g @sourcegraph/amp export AMP_API_KEY="$SOURCEGRAPH_AMP_API_KEY" - printf "%b Installation complete.%b\n" "$BOLD" "$RESET" + printf "%b Installation complete.%b\n" "$BOLD" fi } diff --git a/registry/harsh9485/modules/sourcegraph_amp/scripts/start.sh b/registry/harsh9485/modules/sourcegraph_amp/scripts/start.sh index 004eeeaf..71c57c33 100644 --- a/registry/harsh9485/modules/sourcegraph_amp/scripts/start.sh +++ b/registry/harsh9485/modules/sourcegraph_amp/scripts/start.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/bash set -euo pipefail # Load user environment From a42eee526519a1e3dc9b6a363503b4a4fdcbd25a Mon Sep 17 00:00:00 2001 From: Harsh panwar Date: Sat, 2 Aug 2025 13:03:52 +0530 Subject: [PATCH 3/4] Add README file for the module --- registry/harsh9485/modules/README.md | 12 --- .../modules/sourcegraph_amp/README.md | 92 +++++++++---------- .../sourcegraph_amp/scripts/install.sh | 1 - .../modules/sourcegraph_amp/scripts/start.sh | 3 +- 4 files changed, 47 insertions(+), 61 deletions(-) delete mode 100644 registry/harsh9485/modules/README.md diff --git a/registry/harsh9485/modules/README.md b/registry/harsh9485/modules/README.md deleted file mode 100644 index 9f16b718..00000000 --- a/registry/harsh9485/modules/README.md +++ /dev/null @@ -1,12 +0,0 @@ ---- -display_name: "harsh9485" -bio: "Brief description of what you do" -avatar: "./.images/avatar.png" -website: "https://your-website.com" -support_email: "panwarharshwardhan67@gmail.com" -status: "community" ---- - -# Your Name - -Brief description of who you are and what you do. diff --git a/registry/harsh9485/modules/sourcegraph_amp/README.md b/registry/harsh9485/modules/sourcegraph_amp/README.md index aa898a29..2de73e41 100644 --- a/registry/harsh9485/modules/sourcegraph_amp/README.md +++ b/registry/harsh9485/modules/sourcegraph_amp/README.md @@ -1,71 +1,69 @@ --- -display_name: sourcegraph_amp -description: Describe what this module does + +display\_name: Sourcegraph AMP icon: ../../../../.icons/sourcegraph-amp.svg -verified: false -tags: [helper] ---- +description: Run Sourcegraph AMP CLI in your workspace with AgentAPI integration +verified: true +tags: \[agent, sourcegraph, amp, ai, tasks] +------------------------------------------- -# sourcegraph_amp +# Sourcegraph AMP CLI - +Run [Sourcegraph AMP CLI](https://sourcegraph.com/amp) in your workspace to access Sourcegraph's AI-powered code search and analysis tools, with AgentAPI integration for seamless Coder Tasks support. ```tf module "sourcegraph_amp" { - count = data.coder_workspace.me.start_count - source = "registry.coder.com/harsh9485/sourcegraph_amp/coder" - version = "1.0.0" + source = "registry.coder.com/harsh9485/sourcegraph-amp/coder" + version = "1.0.0" + agent_id = coder_agent.example.id + sourcegraph_amp_api_key = var.sourcegraph_amp_api_key + install_sourcegraph-amp = true + agentapi_version = "latest" } ``` - +## Prerequisites -## Examples +* 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 -### Example 1 - -Install the Dracula theme from [OpenVSX](https://open-vsx.org/): +## Usage Example ```tf +variable "sourcegraph_amp_api_key" { + type = string + description = "Sourcegraph AMP API key" + sensitive = true +} + module "sourcegraph_amp" { - count = data.coder_workspace.me.start_count - source = "registry.coder.com/NAMESPACE/sourcegraph_amp/coder" - version = "1.0.0" - agent_id = coder_agent.example.id - extensions = [ - "dracula-theme.theme-dracula" - ] + count = data.coder_workspace.me.start_count + source = "registry.coder.com/harsh9485/sourcegraph-amp/coder" + version = "1.0.0" + agent_id = coder_agent.example.id + sourcegraph_amp_api_key = var.sourcegraph_amp_api_key # recommended for authenticated usage + install_sourcegraph-amp = true } ``` -Enter the `.` into the extensions array and code-server will automatically install on start. +## How it Works -### Example 2 +* **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 -Configure VS Code's [settings.json](https://code.visualstudio.com/docs/getstarted/settings#_settingsjson) file: +## Troubleshooting -```tf -module "sourcegraph_amp" { - count = data.coder_workspace.me.start_count - source = "registry.coder.com/NAMESPACE/sourcegraph_amp/coder" - version = "1.0.0" - agent_id = coder_agent.example.id - extensions = ["dracula-theme.theme-dracula"] - settings = { - "workbench.colorTheme" = "Dracula" - } -} -``` +* 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 AgentAPI fails to start, verify that your container has network access and executable permissions for the scripts -### Example 3 +> \[!IMPORTANT] +> For using **Coder Tasks** with Sourcegraph AMP, make sure to pass the `AI Prompt` parameter and set `sourcegraph_amp_api_key`. +> This ensures task reporting and status updates work seamlessly. -Run code-server in the background, don't fetch it from GitHub: +## References -```tf -module "sourcegraph_amp" { - source = "registry.coder.com/NAMESPACE/sourcegraph_amp/coder" - version = "1.0.0" - agent_id = coder_agent.example.id - offline = true -} -``` +* [Sourcegraph AMP Documentation](https://sourcegraph.com/amp) +* [AgentAPI Documentation](https://github.com/coder/agentapi) +* [Coder AI Agents Guide](https://coder.com/docs/tutorials/ai-agents) diff --git a/registry/harsh9485/modules/sourcegraph_amp/scripts/install.sh b/registry/harsh9485/modules/sourcegraph_amp/scripts/install.sh index de77cfed..a9c58ed2 100644 --- a/registry/harsh9485/modules/sourcegraph_amp/scripts/install.sh +++ b/registry/harsh9485/modules/sourcegraph_amp/scripts/install.sh @@ -30,7 +30,6 @@ function install_sourcegraph_amp() { ensure_node printf "%b Installing Sourcegraph AMP CLI...%b\n" "$BOLD" npm install -g @sourcegraph/amp - export AMP_API_KEY="$SOURCEGRAPH_AMP_API_KEY" printf "%b Installation complete.%b\n" "$BOLD" fi } diff --git a/registry/harsh9485/modules/sourcegraph_amp/scripts/start.sh b/registry/harsh9485/modules/sourcegraph_amp/scripts/start.sh index 71c57c33..df8ec7ef 100644 --- a/registry/harsh9485/modules/sourcegraph_amp/scripts/start.sh +++ b/registry/harsh9485/modules/sourcegraph_amp/scripts/start.sh @@ -23,6 +23,7 @@ else mkdir -p "$dir" fi cd "$dir" - +echo "Add AMP API key" +export AMP_API_KEY=$SOURCEGRAPH_AMP_API_KEY # Launch AgentAPI server with AMP agentapi server --term-width=67 --term-height=1190 -- amp \ No newline at end of file From 667b1c1c499cf55a0d834fd18b2f13083f1826c6 Mon Sep 17 00:00:00 2001 From: Harsh panwar Date: Mon, 4 Aug 2025 15:49:44 +0530 Subject: [PATCH 4/4] Add test suite --- .../modules/sourcegraph_amp/README.md | 2 +- .../modules/sourcegraph_amp/main.test.ts | 150 +++++++++++++++--- .../harsh9485/modules/sourcegraph_amp/main.tf | 1 - .../sourcegraph_amp/scripts/install.sh | 63 +++++--- .../sourcegraph_amp/testdata/amp-mock.sh | 14 ++ 5 files changed, 182 insertions(+), 48 deletions(-) create mode 100644 registry/harsh9485/modules/sourcegraph_amp/testdata/amp-mock.sh diff --git a/registry/harsh9485/modules/sourcegraph_amp/README.md b/registry/harsh9485/modules/sourcegraph_amp/README.md index 2de73e41..66e43ba7 100644 --- a/registry/harsh9485/modules/sourcegraph_amp/README.md +++ b/registry/harsh9485/modules/sourcegraph_amp/README.md @@ -5,7 +5,7 @@ icon: ../../../../.icons/sourcegraph-amp.svg description: Run Sourcegraph AMP CLI in your workspace with AgentAPI integration verified: true tags: \[agent, sourcegraph, amp, ai, tasks] -------------------------------------------- +--- # Sourcegraph AMP CLI diff --git a/registry/harsh9485/modules/sourcegraph_amp/main.test.ts b/registry/harsh9485/modules/sourcegraph_amp/main.test.ts index 89161426..5f92ffd0 100644 --- a/registry/harsh9485/modules/sourcegraph_amp/main.test.ts +++ b/registry/harsh9485/modules/sourcegraph_amp/main.test.ts @@ -1,28 +1,126 @@ -import { describe, it, expect } from "bun:test"; import { - runTerraformInit, - runTerraformApply, - testRequiredVariables, - findResourceInstance, -} from "~test"; -import path from "path"; - -const moduleDir = path.resolve(__dirname); -const requiredVars = { agent_id: "dummy-agent-id" }; - -describe("sourcegraph-amp module", () => { - it("initializes and applies without errors", async () => { - await runTerraformInit(moduleDir); - testRequiredVariables(moduleDir, requiredVars); - - const state = await runTerraformApply(moduleDir, requiredVars); - const script = findResourceInstance(state, "coder_script"); - - expect(script).toBeDefined(); - expect(script.agent_id).toBe(requiredVars.agent_id); - expect(script.script).toContain("ARG_INSTALL_SOURCEGRAPH_AMP='true'"); - expect(script.script).toContain("ARG_AGENTAPI_VERSION='v0.3.0'"); - expect(script.script).toMatch(/\/tmp\/install\.sh/); - expect(script.script).toMatch(/\/tmp\/start\.sh/); + test, + afterEach, + describe, + setDefaultTimeout, + beforeAll, + expect, +} from "bun:test"; +import { execContainer, readFileContainer, runTerraformInit } from "~test"; +import { + loadTestFile, + writeExecutable, + setup as setupUtil, + execModuleScript, + expectAgentAPIStarted, +} from "../../../coder/modules/agentapi/test-util"; + +let cleanupFunctions: (() => Promise)[] = []; +const registerCleanup = (cleanup: () => Promise) => { + cleanupFunctions.push(cleanup); +}; +afterEach(async () => { + const cleanupFnsCopy = cleanupFunctions.slice().reverse(); + cleanupFunctions = []; + for (const cleanup of cleanupFnsCopy) { + try { + await cleanup(); + } catch (error) { + console.error("Error during cleanup:", error); + } + } +}); + +interface SetupProps { + skipAgentAPIMock?: boolean; + skipAmpMock?: boolean; + moduleVariables?: Record; + agentapiMockScript?: string; +} + +const setup = async (props?: SetupProps): Promise<{ id: string }> => { + const projectDir = "/home/coder/project"; + const { id } = await setupUtil({ + moduleDir: import.meta.dir, + moduleVariables: { + install_sourcegraph_amp: props?.skipAmpMock ? "true" : "false", + install_agentapi: props?.skipAgentAPIMock ? "true" : "false", + sourcegraph_amp_model: "test-model", + ...props?.moduleVariables, + }, + registerCleanup, + projectDir, + skipAgentAPIMock: props?.skipAgentAPIMock, + agentapiMockScript: props?.agentapiMockScript, + }); + + // Place the AMP mock CLI binary inside the container + if (!props?.skipAmpMock) { + await writeExecutable({ + containerId: id, + filePath: "/usr/bin/amp", + content: await loadTestFile(`${import.meta.dir}`, "amp-mock.sh"), + }); + } + + return { id }; +}; + +setDefaultTimeout(60 * 1000 * 8); + +describe("Sourcegraph AMP Module", async () => { + beforeAll(async () => { + await runTerraformInit(import.meta.dir); + }); + + test("happy-path", async () => { + const { id } = await setup(); + await execModuleScript(id); + await expectAgentAPIStarted(id); + }); + + test("sourcegraph-amp-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("AMP version: AMP CLI mock version v1.0.0"); + }); + + test("custom-folder", async () => { + const folder = "/tmp/sourcegraph-amp-test"; + const { id } = await setup({ + moduleVariables: { + folder, + }, + }); + await execModuleScript(id); + const resp = await readFileContainer(id, "/home/coder/.sourcegraph-amp-module/install.log"); + expect(resp).toContain(folder); + }); + + test("pre-post-install-scripts", async () => { + const { id } = await setup({ + moduleVariables: { + pre_install_script: "#!/bin/bash\necho 'pre-install-script'", + post_install_script: "#!/bin/bash\necho 'post-install-script'", + }, + }); + await execModuleScript(id); + const preLog = await readFileContainer(id, "/home/coder/.sourcegraph-amp-module/pre_install.log"); + expect(preLog).toContain("pre-install-script"); + const postLog = await readFileContainer(id, "/home/coder/.sourcegraph-amp-module/post_install.log"); + expect(postLog).toContain("post-install-script"); + }); + + test("amp-not-installed", async () => { + const { id } = await setup({ skipAmpMock: true }); + await execModuleScript(id); + const log = await readFileContainer(id, "/home/coder/.sourcegraph-amp-module/install.log"); + expect(log).toContain("Error"); }); -}); \ No newline at end of file +}); diff --git a/registry/harsh9485/modules/sourcegraph_amp/main.tf b/registry/harsh9485/modules/sourcegraph_amp/main.tf index d755b5ab..0cd6d1d5 100644 --- a/registry/harsh9485/modules/sourcegraph_amp/main.tf +++ b/registry/harsh9485/modules/sourcegraph_amp/main.tf @@ -152,7 +152,6 @@ module "agentapi" { chmod +x /tmp/install.sh ARG_INSTALL_SOURCEGRAPH_AMP='${var.install_sourcegraph-amp}' \ SOURCEGRAPH_AMP_START_DIRECTORY='${var.folder}' \ - SOURCEGRAPH_AMP_API_KEY='${var.sourcegraph-amp_api_key}' \ BASE_EXTENSIONS='${replace(local.base_extensions, "'", "'\\''")}' \ /tmp/install.sh EOT diff --git a/registry/harsh9485/modules/sourcegraph_amp/scripts/install.sh b/registry/harsh9485/modules/sourcegraph_amp/scripts/install.sh index a9c58ed2..0f7ba84d 100644 --- a/registry/harsh9485/modules/sourcegraph_amp/scripts/install.sh +++ b/registry/harsh9485/modules/sourcegraph_amp/scripts/install.sh @@ -4,34 +4,57 @@ set -euo pipefail # ANSI colors BOLD='\033[1m' - -# Print arguments echo "--------------------------------" echo "Install flag: $ARG_INSTALL_SOURCEGRAPH_AMP" echo "Workspace: $SOURCEGRAPH_AMP_START_DIRECTORY" echo "--------------------------------" -# Check for npm/node and install via nvm if missing -function ensure_node() { - if ! command -v npm &>/dev/null; then - echo "npm not found. Installing Node.js via NVM..." - export NVM_DIR="$HOME/.nvm" - mkdir -p "$NVM_DIR" - curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash - # shellcheck source=/dev/null - source "$NVM_DIR/nvm.sh" - nvm install --lts - nvm alias default node - fi +function install_node() { + 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 + + nvm install --lts + nvm use --lts + nvm alias default node + + 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 + fi } function install_sourcegraph_amp() { - if [[ "$ARG_INSTALL_SOURCEGRAPH_AMP" == "true" ]]; then - ensure_node - printf "%b Installing Sourcegraph AMP CLI...%b\n" "$BOLD" - npm install -g @sourcegraph/amp - printf "%b Installation complete.%b\n" "$BOLD" - fi + 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 + fi + + printf "%s Installing Sourcegraph AMP CLI...\n" "${BOLD}" + npm install -g @sourcegraph/amp + export AMP_API_KEY="$SOURCEGRAPH_AMP_API_KEY" + printf "%s Successfully installed Sourcegraph AMP CLI. Version: %s\n" "${BOLD}" "$(amp --version)" + fi } install_sourcegraph_amp \ No newline at end of file diff --git a/registry/harsh9485/modules/sourcegraph_amp/testdata/amp-mock.sh b/registry/harsh9485/modules/sourcegraph_amp/testdata/amp-mock.sh new file mode 100644 index 00000000..259db57a --- /dev/null +++ b/registry/harsh9485/modules/sourcegraph_amp/testdata/amp-mock.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +# Mock behavior of the AMP CLI +if [[ "$1" == "--version" ]]; then + echo "AMP CLI mock version v1.0.0" + exit 0 +fi + +# Simulate AMP running in a loop for AgentAPI to connect +set -e +while true; do + echo "$(date) - AMP mock is running..." + sleep 15 +done