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/README.md b/registry/harsh9485/modules/sourcegraph_amp/README.md new file mode 100644 index 00000000..ab62f8e9 --- /dev/null +++ b/registry/harsh9485/modules/sourcegraph_amp/README.md @@ -0,0 +1,81 @@ +--- + +display\_name: Sourcegraph AMP +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 + +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" { + 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 + +* 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 + +## Usage Example + +```tf +variable "sourcegraph_amp_api_key" { + type = string + description = "Sourcegraph AMP API key" + sensitive = true +} + +# Set system prompt for Sourcegraph Amp 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 + } +} + +module "sourcegraph_amp" { + 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 +} +``` + +## How it Works + +* **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 + +* 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 + +> \[!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. + +## References + +* [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/main.test.ts b/registry/harsh9485/modules/sourcegraph_amp/main.test.ts new file mode 100644 index 00000000..542ab521 --- /dev/null +++ b/registry/harsh9485/modules/sourcegraph_amp/main.test.ts @@ -0,0 +1,126 @@ +import { + 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); + +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"); + }); +}); diff --git a/registry/harsh9485/modules/sourcegraph_amp/main.tf b/registry/harsh9485/modules/sourcegraph_amp/main.tf new file mode 100644 index 00000000..0cd6d1d5 --- /dev/null +++ b/registry/harsh9485/modules/sourcegraph_amp/main.tf @@ -0,0 +1,160 @@ +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" +} + +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 + 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" + 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}' \ + 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..9ae6d850 --- /dev/null +++ b/registry/harsh9485/modules/sourcegraph_amp/scripts/install.sh @@ -0,0 +1,77 @@ +#!/bin/bash +set -euo pipefail + +# ANSI colors +BOLD='\033[1m' + +echo "--------------------------------" +echo "Install flag: $ARG_INSTALL_SOURCEGRAPH_AMP" +echo "Workspace: $SOURCEGRAPH_AMP_START_DIRECTORY" +echo "--------------------------------" + +# Helper function to check if a command exists +command_exists() { + command -v "$1" >/dev/null 2>&1 +} + +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 + 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 +} + +function setup_system_prompt() { + if [ -n "${SOURCEGRAPH_AMP_SYSTEM_PROMPT:-}" ]; then + echo "Setting Sourcegraph AMP system prompt..." + mkdir -p "$HOME/.sourcegraph-amp" + echo "$SOURCEGRAPH_AMP_SYSTEM_PROMPT" > "$HOME/.sourcegraph-amp/SYSTEM_PROMPT.md" + echo "System prompt saved to $HOME/.sourcegraph-amp/SYSTEM_PROMPT.md" + else + echo "No system prompt provided for Sourcegraph AMP." + fi +} + +install_sourcegraph_amp +setup_system_prompt 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..df8ec7ef --- /dev/null +++ b/registry/harsh9485/modules/sourcegraph_amp/scripts/start.sh @@ -0,0 +1,29 @@ +#!/bin/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" +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 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