Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 15 additions & 27 deletions registry/coder-labs/modules/sourcegraph-amp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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" {
Expand All @@ -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

Expand All @@ -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
Expand Down
46 changes: 39 additions & 7 deletions registry/coder-labs/modules/sourcegraph-amp/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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({
Expand All @@ -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);
});
Expand All @@ -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",
Expand All @@ -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",
Expand Down
31 changes: 25 additions & 6 deletions registry/coder-labs/modules/sourcegraph-amp/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -54,6 +53,24 @@ 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 = ""
}

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"
Expand All @@ -69,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" {
Expand Down Expand Up @@ -151,7 +168,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
Expand All @@ -173,8 +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}' \
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

Expand All @@ -186,8 +204,9 @@ 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}' \
ARG_SOURCEGRAPH_AMP_SYSTEM_PROMPT='${var.system_prompt}' \
/tmp/install.sh
EOT
}
Expand Down
91 changes: 48 additions & 43 deletions registry/coder-labs/modules/sourcegraph-amp/scripts/install.sh
Original file line number Diff line number Diff line change
@@ -1,74 +1,79 @@
#!/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:-}
ARG_SOURCEGRAPH_AMP_SYSTEM_PROMPT=${ARG_SOURCEGRAPH_AMP_SYSTEM_PROMPT:-}

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"
printf "System Prompt: %s\n" "$ARG_SOURCEGRAPH_AMP_SYSTEM_PROMPT"
echo "--------------------------------"

# Helper function to check if a command exists
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
Comment on lines +26 to 32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's switch to using the install script.

curl -fsSL https://ampcode.com/install.sh | bash

https://ampcode.com/manual#getting-started-command-line-interface

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/[email protected]
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
}

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."
Expand Down
Loading