From 2ecf8945f10cb1adec7f2a20d3f609450e734d99 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 19 Nov 2025 17:36:58 +0000 Subject: [PATCH 01/42] feat: add rothnic namespace and OpenCode module This commit creates: - New rothnic namespace with contributor information - OpenCode module for AI-powered terminal coding assistance - Support for multiple AI providers (GitHub Copilot, Anthropic, OpenAI) - GitHub Copilot authentication through Coder external auth - AgentAPI integration for task reporting - Comprehensive tests for module functionality The OpenCode module is based on the copilot module pattern and provides: - Automatic GitHub authentication via Coder external auth - Multiple installation methods (npm, curl) - Session resumption support - Configurable AI providers - Task reporting to Coder UI --- registry/rothnic/.images/avatar.png | Bin 0 -> 17761 bytes registry/rothnic/README.md | 11 + registry/rothnic/modules/opencode/README.md | 225 ++++++++++++++++ registry/rothnic/modules/opencode/main.tf | 254 ++++++++++++++++++ .../modules/opencode/opencode.tftest.hcl | 231 ++++++++++++++++ .../modules/opencode/scripts/install.sh | 159 +++++++++++ .../rothnic/modules/opencode/scripts/start.sh | 113 ++++++++ 7 files changed, 993 insertions(+) create mode 100644 registry/rothnic/.images/avatar.png create mode 100644 registry/rothnic/README.md create mode 100644 registry/rothnic/modules/opencode/README.md create mode 100644 registry/rothnic/modules/opencode/main.tf create mode 100644 registry/rothnic/modules/opencode/opencode.tftest.hcl create mode 100644 registry/rothnic/modules/opencode/scripts/install.sh create mode 100644 registry/rothnic/modules/opencode/scripts/start.sh diff --git a/registry/rothnic/.images/avatar.png b/registry/rothnic/.images/avatar.png new file mode 100644 index 0000000000000000000000000000000000000000..be46cf0716dcf93276ff90e54070cb379cf09429 GIT binary patch literal 17761 zcmbumcQ~B?wl@BZ!RSJi!6-r0h%QFUAbRv(BYKJ6`yhmg-U%|%dy8HYq70(-o&H*1higUVj$;ECJ5}0(^W3J{|!C0wE+M zAR?h6B_Spzp`*M_PQ^&i%*03!gR!vR=VW2M#|DFOJ>b44ASfat!psSm78jDbFDxPi zA|xavAtr&6l0t>v?0ATR~!PamiN06=)4zhA&V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nEJfeB)7r&yhTHt@SQgT zr6UWwR3M~FIxPwLx(3aUrFzYG&PH-^7`7%g5{g3z73ei%cK2NnyqfE9N<5+h)Dht{ zTwr-qh>EGdL=zfD^(z;OS_rA^RAUyAcjLvSblLa4y1}Bn!4-mLkiM{6`}AVTU}NR_ z&b}j;Ha3_C;`UX`K%j5bvti9VZ$%pFT`9i{>W!TICa((fuES}!^ecpljL`+P-2Q&h ztCH4ygA_-&y>*JZp|8}L14xb5$|SdFZAR4#Q*E(OeyjV&0f$pR5e)ZbPA5PKsnDT4 zKS?_-Fet&UCvYrEdq z%`JBY^8s}sqnY~-IK@;uT7b;O>IKkND3XR<3R6*Z069owO;BVr+KVXQ&!_(d%#PvI literal 0 HcmV?d00001 diff --git a/registry/rothnic/README.md b/registry/rothnic/README.md new file mode 100644 index 000000000..64ddc6923 --- /dev/null +++ b/registry/rothnic/README.md @@ -0,0 +1,11 @@ +--- +display_name: "Nick Roth" +bio: "Software engineer building development tools and automation" +avatar: "./.images/avatar.png" +github: "rothnic" +status: "community" +--- + +# Nick Roth + +Software engineer building development tools and automation. diff --git a/registry/rothnic/modules/opencode/README.md b/registry/rothnic/modules/opencode/README.md new file mode 100644 index 000000000..14bd02b26 --- /dev/null +++ b/registry/rothnic/modules/opencode/README.md @@ -0,0 +1,225 @@ +--- +display_name: OpenCode +description: AI-powered terminal coding agent with support for multiple providers including GitHub Copilot +icon: ../../../../.icons/code.svg +verified: false +tags: [agent, ai, opencode, coding-assistant, copilot] +--- + +# OpenCode + +Run [OpenCode.ai](https://opencode.ai/) in your workspace for AI-powered coding assistance directly from the terminal. OpenCode is a powerful AI coding agent built for the terminal that supports multiple providers including GitHub Copilot, Anthropic, and OpenAI. This module integrates with [AgentAPI](https://github.com/coder/agentapi) for task reporting in the Coder UI. + +```tf +module "opencode" { + source = "registry.coder.com/rothnic/opencode/coder" + version = "1.0.0" + agent_id = coder_agent.example.id + workdir = "/home/coder/projects" +} +``` + +> [!IMPORTANT] +> This module assumes you have [Coder external authentication](https://coder.com/docs/admin/external-auth) configured with `id = "github"` if you want to use the GitHub Copilot provider. If not, you can provide a direct token using the `github_token` variable or configure authentication interactively using `opencode auth login`. + +> [!NOTE] +> By default, this module is configured to run the embedded chat interface as a path-based application. In production, we recommend that you configure a [wildcard access URL](https://coder.com/docs/admin/setup#wildcard-access-url) and set `subdomain = true`. See [here](https://coder.com/docs/tutorials/best-practices/security-best-practices#disable-path-based-apps) for more details. + +## Prerequisites + +- **Node.js v18+** and **npm** (when using npm install method) +- **AI Provider Access** (depending on which provider you choose): + - **GitHub Copilot**: Active [GitHub Copilot subscription](https://docs.github.com/en/copilot/about-github-copilot/subscription-plans-for-github-copilot) + - **Anthropic**: Anthropic API key + - **OpenAI**: OpenAI API key +- **GitHub authentication** (for Copilot provider) via one of: + - [Coder external authentication](https://coder.com/docs/admin/external-auth) (recommended) + - Direct token via `github_token` variable + - Interactive login via `opencode auth login` + +## Examples + +### Basic Usage with GitHub Copilot + +Use GitHub Copilot as the AI provider through Coder's external auth: + +```tf +module "opencode" { + source = "registry.coder.com/rothnic/opencode/coder" + version = "1.0.0" + agent_id = coder_agent.example.id + workdir = "/home/coder/projects" + + opencode_provider = "copilot" +} +``` + +### Usage with Tasks + +For development environments where you want OpenCode to automatically resume sessions and receive initial prompts: + +```tf +data "coder_parameter" "ai_prompt" { + type = "string" + name = "AI Prompt" + default = "" + description = "Initial task prompt for OpenCode." + mutable = true +} + +module "opencode" { + source = "registry.coder.com/rothnic/opencode/coder" + version = "1.0.0" + agent_id = coder_agent.example.id + workdir = "/home/coder/projects" + + ai_prompt = data.coder_parameter.ai_prompt.value + resume_session = true +} +``` + +### Using Different AI Providers + +OpenCode supports multiple AI providers. Configure the provider of your choice: + +```tf +module "opencode" { + source = "registry.coder.com/rothnic/opencode/coder" + version = "1.0.0" + agent_id = coder_agent.example.id + workdir = "/home/coder/projects" + + opencode_provider = "anthropic" # or "openai", "copilot" + + # Provider credentials can be configured via opencode_config + # or set up interactively with 'opencode auth login' +} +``` + +### Advanced Configuration + +Customize OpenCode settings and installation: + +```tf +module "opencode" { + source = "registry.coder.com/rothnic/opencode/coder" + version = "1.0.0" + agent_id = coder_agent.example.id + workdir = "/home/coder/projects" + + # Version pinning (defaults to "latest") + opencode_version = "0.1.5" + + # Installation method + install_method = "npm" # or "curl" + + # Custom OpenCode configuration + opencode_config = jsonencode({ + theme = "dark" + # Add other OpenCode config options + }) + + # Pre-install Node.js if needed + pre_install_script = <<-EOT + #!/bin/bash + curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - + sudo apt-get install -y nodejs + EOT +} +``` + +### Direct Token Authentication + +Use this example when you want to provide a GitHub Personal Access Token for Copilot instead of using Coder external auth: + +```tf +variable "github_token" { + type = string + description = "GitHub Personal Access Token" + sensitive = true +} + +module "opencode" { + source = "registry.coder.com/rothnic/opencode/coder" + version = "1.0.0" + agent_id = coder_agent.example.id + workdir = "/home/coder/projects" + github_token = var.github_token +} +``` + +### Standalone Mode + +Run OpenCode as a command-line tool without task reporting or web interface. This installs and configures OpenCode, making it available as a CLI app in the Coder agent bar that you can launch to interact with OpenCode directly from your terminal. Set `report_tasks = false` to disable integration with Coder Tasks. + +```tf +module "opencode" { + source = "registry.coder.com/rothnic/opencode/coder" + version = "1.0.0" + agent_id = coder_agent.example.id + workdir = "/home/coder" + report_tasks = false + cli_app = true +} +``` + +## Authentication + +The module supports multiple authentication methods for GitHub Copilot provider (in priority order): + +1. **[Coder External Auth](https://coder.com/docs/admin/external-auth) (Recommended)** - Automatic if GitHub external auth is configured in Coder +2. **Direct Token** - Pass `github_token` variable (OAuth or Personal Access Token) +3. **Interactive** - Configure providers via `opencode auth login` command + +For other providers (Anthropic, OpenAI), you can either: + +- Use `opencode auth login` to configure interactively +- Configure via `opencode_config` variable +- Set environment variables in your template + +> [!NOTE] +> OAuth tokens work best with GitHub Copilot. Personal Access Tokens may have limited functionality. + +## Session Resumption + +By default, the module resumes the latest OpenCode session when the workspace restarts. Set `resume_session = false` to always start fresh sessions. + +> [!NOTE] +> Session resumption requires persistent storage for the home directory or workspace volume. Without persistent storage, sessions will not resume across workspace restarts. + +## GitHub Copilot Integration + +OpenCode can use GitHub Copilot as an AI provider, giving you access to powerful models like Claude Sonnet and GPT-4 through your GitHub Copilot subscription. To enable this: + +1. Configure GitHub external auth in Coder (recommended) OR provide a GitHub token +2. Ensure you have an active GitHub Copilot subscription +3. Set `opencode_provider = "copilot"` (this is the default) + +OpenCode will automatically authenticate with GitHub Copilot using your credentials. See the [OpenCode GitHub documentation](https://opencode.ai/docs/github/) for more details on GitHub Copilot integration. + +## Troubleshooting + +If you encounter any issues, check the log files in the `~/.opencode-module` directory within your workspace for detailed information. + +```bash +# Installation logs +cat ~/.opencode-module/install.log + +# Startup logs +cat ~/.opencode-module/agentapi-start.log + +# Pre/post install script logs +cat ~/.opencode-module/pre_install.log +cat ~/.opencode-module/post_install.log +``` + +> [!NOTE] +> The `workdir` variable is required and specifies the directory where OpenCode will run. + +## References + +- [OpenCode.ai Documentation](https://opencode.ai/docs/) +- [OpenCode GitHub Repository](https://github.com/opencode-ai/opencode) +- [OpenCode GitHub Integration](https://opencode.ai/docs/github/) +- [AgentAPI Documentation](https://github.com/coder/agentapi) +- [Coder AI Agents Guide](https://coder.com/docs/tutorials/ai-agents) diff --git a/registry/rothnic/modules/opencode/main.tf b/registry/rothnic/modules/opencode/main.tf new file mode 100644 index 000000000..765990120 --- /dev/null +++ b/registry/rothnic/modules/opencode/main.tf @@ -0,0 +1,254 @@ +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." +} + +variable "workdir" { + type = string + description = "The folder to run OpenCode in." +} + +variable "external_auth_id" { + type = string + description = "ID of the GitHub external auth provider configured in Coder." + default = "github" +} + +variable "github_token" { + type = string + description = "GitHub OAuth token or Personal Access Token. If provided, this will be used instead of auto-detecting authentication." + default = "" + sensitive = true +} + +variable "opencode_provider" { + type = string + description = "AI provider to use with OpenCode. Supported values: anthropic, openai, copilot (default), etc." + default = "copilot" +} + +variable "opencode_config" { + type = string + description = "Custom OpenCode configuration as JSON string." + default = "" +} + +variable "ai_prompt" { + type = string + description = "Initial task prompt for programmatic mode." + default = "" +} + +variable "system_prompt" { + type = string + description = "The system prompt to use for OpenCode. Task reporting instructions are automatically added when report_tasks is enabled." + default = "You are a helpful coding assistant that helps developers write, debug, and understand code. Provide clear explanations, follow best practices, and help solve coding problems efficiently." +} + +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.10.0" +} + +variable "opencode_version" { + type = string + description = "The version of OpenCode to install. Use 'latest' for the latest version or specify a version." + default = "latest" +} + +variable "install_method" { + type = string + description = "Installation method for OpenCode: 'npm' (default) or 'curl'." + default = "npm" + validation { + condition = contains(["npm", "curl"], var.install_method) + error_message = "install_method must be either 'npm' or 'curl'." + } +} + +variable "report_tasks" { + type = bool + description = "Whether to enable task reporting to Coder UI via AgentAPI." + default = true +} + +variable "subdomain" { + type = bool + description = "Whether to use a subdomain for AgentAPI." + default = false +} + +variable "order" { + type = number + description = "The order determines the position of app in the UI presentation." + 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/code.svg" +} + +variable "web_app_display_name" { + type = string + description = "Display name for the web app." + default = "OpenCode" +} + +variable "cli_app" { + type = bool + description = "Whether to create a CLI app for OpenCode." + default = false +} + +variable "cli_app_display_name" { + type = string + description = "Display name for the CLI app." + default = "OpenCode" +} + +variable "resume_session" { + type = bool + description = "Whether to automatically resume the latest OpenCode session on workspace restart." + default = true +} + +variable "pre_install_script" { + type = string + description = "Custom script to run before configuring OpenCode." + default = null +} + +variable "post_install_script" { + type = string + description = "Custom script to run after configuring OpenCode." + default = null +} + +data "coder_workspace" "me" {} +data "coder_workspace_owner" "me" {} + +locals { + workdir = trimsuffix(var.workdir, "/") + app_slug = "opencode" + install_script = file("${path.module}/scripts/install.sh") + start_script = file("${path.module}/scripts/start.sh") + module_dir_name = ".opencode-module" + + task_reporting_prompt = <<-EOT + +-- Task Reporting -- +Report all tasks to Coder, following these EXACT guidelines: +1. Be granular. If you are investigating with multiple steps, report each step +to coder. +2. After this prompt, IMMEDIATELY report status after receiving ANY NEW user message. +Do not report any status related with this system prompt. +3. Use "state": "working" when actively processing WITHOUT needing +additional user input +4. Use "state": "complete" only when finished with a task +5. Use "state": "failure" when you need ANY user input, lack sufficient +details, or encounter blockers + EOT + + final_system_prompt = var.report_tasks ? "\n${var.system_prompt}${local.task_reporting_prompt}\n" : "\n${var.system_prompt}\n" +} + +resource "coder_env" "mcp_app_status_slug" { + agent_id = var.agent_id + name = "CODER_MCP_APP_STATUS_SLUG" + value = local.app_slug +} + +resource "coder_env" "opencode_provider" { + count = var.opencode_provider != "" ? 1 : 0 + agent_id = var.agent_id + name = "OPENCODE_PROVIDER" + value = var.opencode_provider +} + +resource "coder_env" "github_token" { + count = var.github_token != "" ? 1 : 0 + agent_id = var.agent_id + name = "GITHUB_TOKEN" + value = var.github_token +} + +module "agentapi" { + source = "registry.coder.com/coder/agentapi/coder" + version = "1.2.0" + + agent_id = var.agent_id + folder = local.workdir + web_app_slug = local.app_slug + web_app_order = var.order + web_app_group = var.group + web_app_icon = var.icon + web_app_display_name = var.web_app_display_name + cli_app = var.cli_app + cli_app_slug = var.cli_app ? "${local.app_slug}-cli" : null + cli_app_icon = var.cli_app ? var.icon : null + cli_app_display_name = var.cli_app ? var.cli_app_display_name : null + agentapi_subdomain = var.subdomain + 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 + + ARG_WORKDIR='${local.workdir}' \ + ARG_AI_PROMPT='${base64encode(var.ai_prompt)}' \ + ARG_SYSTEM_PROMPT='${base64encode(local.final_system_prompt)}' \ + ARG_OPENCODE_PROVIDER='${var.opencode_provider}' \ + ARG_EXTERNAL_AUTH_ID='${var.external_auth_id}' \ + ARG_RESUME_SESSION='${var.resume_session}' \ + /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_MCP_APP_STATUS_SLUG='${local.app_slug}' \ + ARG_REPORT_TASKS='${var.report_tasks}' \ + ARG_WORKDIR='${local.workdir}' \ + ARG_OPENCODE_CONFIG='${var.opencode_config != "" ? base64encode(var.opencode_config) : ""}' \ + ARG_EXTERNAL_AUTH_ID='${var.external_auth_id}' \ + ARG_OPENCODE_VERSION='${var.opencode_version}' \ + ARG_INSTALL_METHOD='${var.install_method}' \ + /tmp/install.sh + EOT +} diff --git a/registry/rothnic/modules/opencode/opencode.tftest.hcl b/registry/rothnic/modules/opencode/opencode.tftest.hcl new file mode 100644 index 000000000..7daff5838 --- /dev/null +++ b/registry/rothnic/modules/opencode/opencode.tftest.hcl @@ -0,0 +1,231 @@ +run "defaults_are_correct" { + command = plan + + variables { + agent_id = "test-agent" + workdir = "/home/coder" + } + + assert { + condition = var.opencode_provider == "copilot" + error_message = "Default provider should be 'copilot'" + } + + assert { + condition = var.report_tasks == true + error_message = "Task reporting should be enabled by default" + } + + assert { + condition = var.resume_session == true + error_message = "Session resumption should be enabled by default" + } + + assert { + condition = var.install_method == "npm" + error_message = "Default install method should be 'npm'" + } + + assert { + condition = resource.coder_env.mcp_app_status_slug.name == "CODER_MCP_APP_STATUS_SLUG" + error_message = "Status slug env var should be created" + } + + assert { + condition = resource.coder_env.mcp_app_status_slug.value == "opencode" + error_message = "Status slug value should be 'opencode'" + } +} + +run "github_token_creates_env_var" { + command = plan + + variables { + agent_id = "test-agent" + workdir = "/home/coder" + github_token = "test_github_token_abc123" + } + + assert { + condition = length(resource.coder_env.github_token) == 1 + error_message = "github_token env var should be created when token is provided" + } + + assert { + condition = resource.coder_env.github_token[0].name == "GITHUB_TOKEN" + error_message = "github_token env var name should be 'GITHUB_TOKEN'" + } + + assert { + condition = resource.coder_env.github_token[0].value == "test_github_token_abc123" + error_message = "github_token env var value should match input" + } +} + +run "github_token_not_created_when_empty" { + command = plan + + variables { + agent_id = "test-agent" + workdir = "/home/coder" + github_token = "" + } + + assert { + condition = length(resource.coder_env.github_token) == 0 + error_message = "github_token env var should not be created when empty" + } +} + +run "opencode_provider_env_var_created" { + command = plan + + variables { + agent_id = "test-agent" + workdir = "/home/coder" + opencode_provider = "anthropic" + } + + assert { + condition = length(resource.coder_env.opencode_provider) == 1 + error_message = "opencode_provider env var should be created when provider is specified" + } + + assert { + condition = resource.coder_env.opencode_provider[0].name == "OPENCODE_PROVIDER" + error_message = "opencode_provider env var name should be 'OPENCODE_PROVIDER'" + } + + assert { + condition = resource.coder_env.opencode_provider[0].value == "anthropic" + error_message = "opencode_provider env var value should match input" + } +} + +run "install_method_validation" { + command = plan + + variables { + agent_id = "test-agent" + workdir = "/home/coder" + install_method = "curl" + } + + assert { + condition = contains(["npm", "curl"], var.install_method) + error_message = "Install method should be either 'npm' or 'curl'" + } +} + +run "workdir_trimmed_of_trailing_slash" { + command = plan + + variables { + agent_id = "test-agent" + workdir = "/home/coder/project/" + } + + assert { + condition = local.workdir == "/home/coder/project" + error_message = "workdir should be trimmed of trailing slash" + } +} + +run "app_slug_is_consistent" { + command = plan + + variables { + agent_id = "test-agent" + workdir = "/home/coder" + } + + assert { + condition = local.app_slug == "opencode" + error_message = "app_slug should be 'opencode'" + } + + assert { + condition = local.module_dir_name == ".opencode-module" + error_message = "module_dir_name should be '.opencode-module'" + } +} + +run "custom_opencode_config" { + command = plan + + variables { + agent_id = "test-agent" + workdir = "/home/coder" + opencode_config = jsonencode({ + theme = "dark" + }) + } + + assert { + condition = var.opencode_config != "" + error_message = "Custom opencode config should be set" + } +} + +run "task_reporting_prompt_included" { + command = plan + + variables { + agent_id = "test-agent" + workdir = "/home/coder" + report_tasks = true + } + + assert { + condition = length(local.final_system_prompt) > 0 + error_message = "final_system_prompt should be computed" + } + + assert { + condition = can(regex("Task Reporting", local.final_system_prompt)) + error_message = "Task reporting prompt should be included when report_tasks is true" + } +} + +run "task_reporting_prompt_excluded" { + command = plan + + variables { + agent_id = "test-agent" + workdir = "/home/coder" + report_tasks = false + } + + assert { + condition = !can(regex("Task Reporting", local.final_system_prompt)) + error_message = "Task reporting prompt should not be included when report_tasks is false" + } +} + +run "version_defaults_to_latest" { + command = plan + + variables { + agent_id = "test-agent" + workdir = "/home/coder" + } + + assert { + condition = var.opencode_version == "latest" + error_message = "OpenCode version should default to 'latest'" + } +} + +run "agentapi_version_is_set" { + command = plan + + variables { + agent_id = "test-agent" + workdir = "/home/coder" + } + + assert { + condition = var.agentapi_version == "v0.10.0" + error_message = "AgentAPI version should be set to v0.10.0" + } +} diff --git a/registry/rothnic/modules/opencode/scripts/install.sh b/registry/rothnic/modules/opencode/scripts/install.sh new file mode 100644 index 000000000..75a3f1886 --- /dev/null +++ b/registry/rothnic/modules/opencode/scripts/install.sh @@ -0,0 +1,159 @@ +#!/bin/bash +set -euo pipefail + +source "$HOME"/.bashrc + +command_exists() { + command -v "$1" > /dev/null 2>&1 +} + +ARG_WORKDIR=${ARG_WORKDIR:-"$HOME"} +ARG_REPORT_TASKS=${ARG_REPORT_TASKS:-true} +ARG_MCP_APP_STATUS_SLUG=${ARG_MCP_APP_STATUS_SLUG:-} +ARG_OPENCODE_CONFIG=$(echo -n "${ARG_OPENCODE_CONFIG:-}" | base64 -d 2> /dev/null || echo "") +ARG_EXTERNAL_AUTH_ID=${ARG_EXTERNAL_AUTH_ID:-github} +ARG_OPENCODE_VERSION=${ARG_OPENCODE_VERSION:-latest} +ARG_INSTALL_METHOD=${ARG_INSTALL_METHOD:-npm} + +validate_prerequisites() { + if [ "$ARG_INSTALL_METHOD" = "npm" ]; then + if ! command_exists node; then + echo "ERROR: Node.js not found. OpenCode requires Node.js v18+." + echo "Install with: curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - && sudo apt-get install -y nodejs" + exit 1 + fi + + if ! command_exists npm; then + echo "ERROR: npm not found. OpenCode requires npm." + exit 1 + fi + + node_version=$(node --version | sed 's/v//' | cut -d. -f1) + if [ "$node_version" -lt 18 ]; then + echo "WARNING: Node.js v$node_version detected. OpenCode requires v18+." + fi + fi +} + +install_opencode() { + if ! command_exists opencode; then + echo "Installing OpenCode (version: ${ARG_OPENCODE_VERSION}, method: ${ARG_INSTALL_METHOD})..." + + if [ "$ARG_INSTALL_METHOD" = "curl" ]; then + curl -fsSL https://opencode.ai/install | bash + elif [ "$ARG_INSTALL_METHOD" = "npm" ]; then + if [ "$ARG_OPENCODE_VERSION" = "latest" ]; then + npm install -g opencode-ai@latest + else + npm install -g "opencode-ai@${ARG_OPENCODE_VERSION}" + fi + else + echo "ERROR: Unknown install method: $ARG_INSTALL_METHOD" + exit 1 + fi + + # Reload shell to get opencode in PATH + export PATH="$HOME/.local/bin:$PATH" + + if ! command_exists opencode; then + echo "ERROR: Failed to install OpenCode" + exit 1 + fi + + echo "OpenCode installed successfully" + else + echo "OpenCode already installed" + fi +} + +check_github_authentication() { + echo "Checking GitHub authentication..." + + if [ -n "${GITHUB_TOKEN:-}" ]; then + echo "✓ GitHub token provided via module configuration" + return 0 + fi + + if command_exists coder; then + if coder external-auth access-token "${ARG_EXTERNAL_AUTH_ID:-github}" > /dev/null 2>&1; then + echo "✓ GitHub OAuth authentication via Coder external auth" + return 0 + fi + fi + + if command_exists gh && gh auth status > /dev/null 2>&1; then + echo "✓ GitHub OAuth authentication via GitHub CLI" + return 0 + fi + + echo "⚠ No GitHub authentication detected" + echo " OpenCode can still work with other providers" + echo " For GitHub Copilot support, configure GitHub external auth or run 'opencode auth login'" + return 0 +} + +setup_opencode_configurations() { + mkdir -p "$ARG_WORKDIR" + + local module_path="$HOME/.opencode-module" + mkdir -p "$module_path" + + setup_opencode_config +} + +setup_opencode_config() { + export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}" + local opencode_data_dir="$XDG_DATA_HOME/opencode" + local auth_file="$opencode_data_dir/auth.json" + + mkdir -p "$opencode_data_dir" + + if [ -n "$ARG_OPENCODE_CONFIG" ]; then + echo "Setting up OpenCode configuration..." + local opencode_config_dir="$HOME/.config/opencode" + mkdir -p "$opencode_config_dir" + echo "$ARG_OPENCODE_CONFIG" > "$opencode_config_dir/opencode.json" + fi +} + +configure_github_copilot_provider() { + echo "Configuring GitHub Copilot provider for OpenCode..." + + local github_token="" + + if [ -n "${GITHUB_TOKEN:-}" ]; then + github_token="$GITHUB_TOKEN" + elif command_exists coder; then + github_token=$(coder external-auth access-token "${ARG_EXTERNAL_AUTH_ID:-github}" 2> /dev/null || echo "") + fi + + if [ -n "$github_token" ] && [ "$github_token" != "null" ]; then + echo "✓ GitHub token available for Copilot provider" + export GITHUB_TOKEN="$github_token" + export GH_TOKEN="$github_token" + else + echo "⚠ No GitHub token available. Run 'opencode auth login' to configure providers." + fi +} + +configure_coder_integration() { + if [ "$ARG_REPORT_TASKS" = "true" ] && [ -n "$ARG_MCP_APP_STATUS_SLUG" ]; then + echo "Configuring OpenCode task reporting..." + export CODER_MCP_APP_STATUS_SLUG="$ARG_MCP_APP_STATUS_SLUG" + export CODER_MCP_AI_AGENTAPI_URL="http://localhost:3284" + echo "✓ Coder integration configured for task reporting" + else + echo "Task reporting disabled or no app status slug provided." + export CODER_MCP_APP_STATUS_SLUG="" + export CODER_MCP_AI_AGENTAPI_URL="" + fi +} + +validate_prerequisites +install_opencode +check_github_authentication +setup_opencode_configurations +configure_github_copilot_provider +configure_coder_integration + +echo "OpenCode module setup completed." diff --git a/registry/rothnic/modules/opencode/scripts/start.sh b/registry/rothnic/modules/opencode/scripts/start.sh new file mode 100644 index 000000000..a88096c22 --- /dev/null +++ b/registry/rothnic/modules/opencode/scripts/start.sh @@ -0,0 +1,113 @@ +#!/bin/bash +set -euo pipefail + +source "$HOME"/.bashrc +export PATH="$HOME/.local/bin:$PATH" + +command_exists() { + command -v "$1" > /dev/null 2>&1 +} + +ARG_WORKDIR=${ARG_WORKDIR:-"$HOME"} +ARG_AI_PROMPT=$(echo -n "${ARG_AI_PROMPT:-}" | base64 -d 2> /dev/null || echo "") +ARG_SYSTEM_PROMPT=$(echo -n "${ARG_SYSTEM_PROMPT:-}" | base64 -d 2> /dev/null || echo "") +ARG_EXTERNAL_AUTH_ID=${ARG_EXTERNAL_AUTH_ID:-github} +ARG_RESUME_SESSION=${ARG_RESUME_SESSION:-true} +ARG_OPENCODE_PROVIDER=${ARG_OPENCODE_PROVIDER:-copilot} + +validate_opencode_installation() { + if ! command_exists opencode; then + echo "ERROR: OpenCode not installed." + exit 1 + fi +} + +build_initial_prompt() { + local initial_prompt="" + + if [ -n "$ARG_AI_PROMPT" ]; then + if [ -n "$ARG_SYSTEM_PROMPT" ]; then + initial_prompt="$ARG_SYSTEM_PROMPT + +$ARG_AI_PROMPT" + else + initial_prompt="$ARG_AI_PROMPT" + fi + fi + + echo "$initial_prompt" +} + +build_opencode_args() { + OPENCODE_ARGS=() + + # Add provider if specified + if [ -n "$ARG_OPENCODE_PROVIDER" ]; then + OPENCODE_ARGS+=(--provider "$ARG_OPENCODE_PROVIDER") + fi +} + +setup_github_authentication() { + export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}" + echo "Setting up GitHub authentication..." + + if [ -n "${GITHUB_TOKEN:-}" ]; then + export GH_TOKEN="$GITHUB_TOKEN" + echo "✓ Using GitHub token from module configuration" + return 0 + fi + + if command_exists coder; then + local github_token + if github_token=$(coder external-auth access-token "${ARG_EXTERNAL_AUTH_ID:-github}" 2> /dev/null); then + if [ -n "$github_token" ] && [ "$github_token" != "null" ]; then + export GITHUB_TOKEN="$github_token" + export GH_TOKEN="$github_token" + echo "✓ Using Coder external auth OAuth token" + return 0 + fi + fi + fi + + if command_exists gh && gh auth status > /dev/null 2>&1; then + echo "✓ Using GitHub CLI OAuth authentication" + return 0 + fi + + echo "⚠ No GitHub authentication available" + echo " OpenCode can still work with other providers" + echo " Use 'opencode auth login' to configure providers" + return 0 +} + +start_agentapi() { + echo "Starting in directory: $ARG_WORKDIR" + cd "$ARG_WORKDIR" + + build_opencode_args + + echo "Starting OpenCode..." + local initial_prompt + initial_prompt=$(build_initial_prompt) + + if [ -n "$initial_prompt" ]; then + echo "Using initial prompt with system context" + if [ ${#OPENCODE_ARGS[@]} -gt 0 ]; then + echo "OpenCode arguments: ${OPENCODE_ARGS[*]}" + agentapi server -I="$initial_prompt" --type opencode --term-width 120 --term-height 40 -- opencode "${OPENCODE_ARGS[@]}" + else + agentapi server -I="$initial_prompt" --type opencode --term-width 120 --term-height 40 -- opencode + fi + else + if [ ${#OPENCODE_ARGS[@]} -gt 0 ]; then + echo "OpenCode arguments: ${OPENCODE_ARGS[*]}" + agentapi server --type opencode --term-width 120 --term-height 40 -- opencode "${OPENCODE_ARGS[@]}" + else + agentapi server --type opencode --term-width 120 --term-height 40 -- opencode + fi + fi +} + +setup_github_authentication +validate_opencode_installation +start_agentapi From f0e75f433f4f2e7f5881d59497761ea9c4797b5c Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 19 Nov 2025 18:08:12 +0000 Subject: [PATCH 02/42] docs: enhance profile and OpenCode module documentation Enhanced rothnic namespace README: - Added comprehensive bio with professional background - Included LinkedIn and website links - Added expertise areas and notable projects - Improved formatting with sections for better readability Enhanced OpenCode module README: - Added "What is OpenCode?" section with detailed explanation - Included key features and benefits - Expanded authentication documentation - Added comprehensive troubleshooting section - Included configuration variables table - Added maintainer information and support links - Improved examples with better explanations - Added links to OpenCode documentation and resources --- registry/rothnic/README.md | 25 +- registry/rothnic/modules/opencode/README.md | 238 +++++++++++++++----- 2 files changed, 208 insertions(+), 55 deletions(-) diff --git a/registry/rothnic/README.md b/registry/rothnic/README.md index 64ddc6923..62039aeeb 100644 --- a/registry/rothnic/README.md +++ b/registry/rothnic/README.md @@ -1,11 +1,32 @@ --- display_name: "Nick Roth" -bio: "Software engineer building development tools and automation" +bio: "Software engineer specializing in data science infrastructure, containerization, and developer tooling. Based in Huntsville, AL, working on innovative solutions for autonomous development and productivity tools." avatar: "./.images/avatar.png" github: "rothnic" +linkedin: "http://www.linkedin.com/in/nicholasleeroth/" +website: "https://www.nickroth.com" status: "community" --- # Nick Roth -Software engineer building development tools and automation. +Software engineer specializing in data science infrastructure, containerization, and developer tooling. Based in Huntsville, AL, I focus on building tools that enhance developer productivity and streamline workflows. + +## Expertise + +- **Developer Tooling**: Creating innovative solutions for autonomous software development +- **Data Science Infrastructure**: Building containerized environments for Python, Jupyter, and data analysis +- **DevOps & Automation**: Docker, Kubernetes, and CI/CD pipeline development +- **AI/ML Integration**: Exploring AI-powered coding assistants and agent orchestration + +## Notable Projects + +- **OpenCode Agents**: Multi-agent orchestration system for autonomous software development +- **Docker-TinyConda**: Streamlined pattern for dockerizing Python applications with conda +- **Anaconda-Notebook**: Popular Docker image for Jupyter Notebooks with full Anaconda installation (86+ stars) + +## Connect + +- 🌐 [Website](https://www.nickroth.com) +- 💼 [LinkedIn](http://www.linkedin.com/in/nicholasleeroth/) +- 🐙 [GitHub](https://github.com/rothnic) diff --git a/registry/rothnic/modules/opencode/README.md b/registry/rothnic/modules/opencode/README.md index 14bd02b26..724326e89 100644 --- a/registry/rothnic/modules/opencode/README.md +++ b/registry/rothnic/modules/opencode/README.md @@ -1,14 +1,38 @@ --- display_name: OpenCode -description: AI-powered terminal coding agent with support for multiple providers including GitHub Copilot +description: AI-powered terminal coding agent with support for multiple providers including GitHub Copilot, Anthropic, and OpenAI icon: ../../../../.icons/code.svg +maintainer_github: rothnic verified: false -tags: [agent, ai, opencode, coding-assistant, copilot] +tags: [agent, ai, opencode, coding-assistant, copilot, terminal, automation] --- # OpenCode -Run [OpenCode.ai](https://opencode.ai/) in your workspace for AI-powered coding assistance directly from the terminal. OpenCode is a powerful AI coding agent built for the terminal that supports multiple providers including GitHub Copilot, Anthropic, and OpenAI. This module integrates with [AgentAPI](https://github.com/coder/agentapi) for task reporting in the Coder UI. +Integrate [OpenCode.ai](https://opencode.ai/) into your Coder workspace for AI-powered coding assistance directly from the terminal. OpenCode is a powerful, open-source AI coding agent built specifically for the terminal that brings the power of AI pair programming to your command line. + +## What is OpenCode? + +[OpenCode](https://github.com/opencode-ai/opencode) is an AI coding agent that runs in your terminal and helps you: + +- **Write code faster** with intelligent completions and suggestions +- **Debug issues** with AI-powered error analysis and fixes +- **Refactor code** with automated improvements and best practices +- **Learn new technologies** with contextual explanations and examples +- **Automate tasks** through natural language instructions + +Unlike traditional IDEs with AI extensions, OpenCode is designed for terminal-first workflows and integrates seamlessly with your existing command-line tools. + +## Key Features + +- 🤖 **Multiple AI Providers**: Choose from GitHub Copilot, Anthropic Claude, OpenAI GPT, and more +- 🔐 **Seamless Authentication**: Automatic GitHub authentication via Coder external auth +- 📊 **Task Reporting**: Integration with [AgentAPI](https://github.com/coder/agentapi) for real-time task tracking in Coder UI +- 💾 **Session Persistence**: Resume your AI conversations across workspace restarts +- 🎨 **Customizable**: Configure prompts, providers, and behavior to match your workflow +- 🚀 **Multiple Installation Methods**: NPM or direct curl installation + +## Quick Start ```tf module "opencode" { @@ -19,29 +43,31 @@ module "opencode" { } ``` +This basic configuration installs OpenCode with GitHub Copilot as the default provider and enables task reporting to your Coder UI. + > [!IMPORTANT] -> This module assumes you have [Coder external authentication](https://coder.com/docs/admin/external-auth) configured with `id = "github"` if you want to use the GitHub Copilot provider. If not, you can provide a direct token using the `github_token` variable or configure authentication interactively using `opencode auth login`. +> For GitHub Copilot integration, ensure you have [Coder external authentication](https://coder.com/docs/admin/external-auth) configured with `id = "github"`. Alternatively, provide a token via the `github_token` variable or configure authentication interactively with `opencode auth login`. > [!NOTE] -> By default, this module is configured to run the embedded chat interface as a path-based application. In production, we recommend that you configure a [wildcard access URL](https://coder.com/docs/admin/setup#wildcard-access-url) and set `subdomain = true`. See [here](https://coder.com/docs/tutorials/best-practices/security-best-practices#disable-path-based-apps) for more details. +> By default, this module uses path-based app access. In production, we recommend configuring a [wildcard access URL](https://coder.com/docs/admin/setup#wildcard-access-url) and setting `subdomain = true`. See [security best practices](https://coder.com/docs/tutorials/best-practices/security-best-practices#disable-path-based-apps) for details. ## Prerequisites - **Node.js v18+** and **npm** (when using npm install method) -- **AI Provider Access** (depending on which provider you choose): - - **GitHub Copilot**: Active [GitHub Copilot subscription](https://docs.github.com/en/copilot/about-github-copilot/subscription-plans-for-github-copilot) - - **Anthropic**: Anthropic API key - - **OpenAI**: OpenAI API key -- **GitHub authentication** (for Copilot provider) via one of: +- **AI Provider Access** (one of the following): + - **[GitHub Copilot](https://github.com/features/copilot)**: Active subscription (Individual, Pro, Business, or Enterprise) + - **[Anthropic](https://www.anthropic.com/)**: API key for Claude models + - **[OpenAI](https://platform.openai.com/)**: API key for GPT models +- **GitHub Authentication** (for Copilot provider): - [Coder external authentication](https://coder.com/docs/admin/external-auth) (recommended) - Direct token via `github_token` variable - Interactive login via `opencode auth login` -## Examples +## Usage Examples -### Basic Usage with GitHub Copilot +### Basic GitHub Copilot Setup -Use GitHub Copilot as the AI provider through Coder's external auth: +The simplest way to get started with GitHub Copilot: ```tf module "opencode" { @@ -54,9 +80,9 @@ module "opencode" { } ``` -### Usage with Tasks +### Task-Based Workflow -For development environments where you want OpenCode to automatically resume sessions and receive initial prompts: +Enable initial prompts and task tracking for automated workflows: ```tf data "coder_parameter" "ai_prompt" { @@ -78,9 +104,9 @@ module "opencode" { } ``` -### Using Different AI Providers +### Multiple AI Providers -OpenCode supports multiple AI providers. Configure the provider of your choice: +Switch between different AI providers based on your needs: ```tf module "opencode" { @@ -89,16 +115,17 @@ module "opencode" { agent_id = coder_agent.example.id workdir = "/home/coder/projects" - opencode_provider = "anthropic" # or "openai", "copilot" + # Choose your provider: "copilot", "anthropic", "openai", etc. + opencode_provider = "anthropic" - # Provider credentials can be configured via opencode_config + # Configure provider credentials via opencode_config # or set up interactively with 'opencode auth login' } ``` ### Advanced Configuration -Customize OpenCode settings and installation: +Full customization with version pinning and pre-installation scripts: ```tf module "opencode" { @@ -107,19 +134,19 @@ module "opencode" { agent_id = coder_agent.example.id workdir = "/home/coder/projects" - # Version pinning (defaults to "latest") + # Pin OpenCode version for stability opencode_version = "0.1.5" - # Installation method + # Choose installation method install_method = "npm" # or "curl" - # Custom OpenCode configuration + # Custom configuration opencode_config = jsonencode({ theme = "dark" - # Add other OpenCode config options + # Additional OpenCode config options }) - # Pre-install Node.js if needed + # Ensure Node.js is available pre_install_script = <<-EOT #!/bin/bash curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - @@ -130,7 +157,7 @@ module "opencode" { ### Direct Token Authentication -Use this example when you want to provide a GitHub Personal Access Token for Copilot instead of using Coder external auth: +Provide a GitHub token directly instead of using Coder external auth: ```tf variable "github_token" { @@ -148,9 +175,9 @@ module "opencode" { } ``` -### Standalone Mode +### Standalone CLI Mode -Run OpenCode as a command-line tool without task reporting or web interface. This installs and configures OpenCode, making it available as a CLI app in the Coder agent bar that you can launch to interact with OpenCode directly from your terminal. Set `report_tasks = false` to disable integration with Coder Tasks. +Install OpenCode as a standalone CLI tool without the web interface: ```tf module "opencode" { @@ -163,43 +190,90 @@ module "opencode" { } ``` +This makes OpenCode available as a CLI app in your Coder agent bar for direct terminal access. + ## Authentication -The module supports multiple authentication methods for GitHub Copilot provider (in priority order): +### GitHub Copilot Provider + +The module supports multiple authentication methods (in priority order): + +1. **[Coder External Auth](https://coder.com/docs/admin/external-auth) (Recommended)** + - Automatic OAuth token retrieval + - Best security and user experience + - Configured at the Coder deployment level + +2. **Direct Token** + - Pass via `github_token` variable + - Supports OAuth or Personal Access Tokens + - Good for testing or specific use cases -1. **[Coder External Auth](https://coder.com/docs/admin/external-auth) (Recommended)** - Automatic if GitHub external auth is configured in Coder -2. **Direct Token** - Pass `github_token` variable (OAuth or Personal Access Token) -3. **Interactive** - Configure providers via `opencode auth login` command +3. **Interactive Login** + - Run `opencode auth login` in the workspace + - Manual configuration of providers + - Useful for multiple provider setups -For other providers (Anthropic, OpenAI), you can either: +### Other Providers + +For Anthropic, OpenAI, and other providers: - Use `opencode auth login` to configure interactively -- Configure via `opencode_config` variable -- Set environment variables in your template +- Set via `opencode_config` variable with API keys +- Configure through environment variables in your template > [!NOTE] -> OAuth tokens work best with GitHub Copilot. Personal Access Tokens may have limited functionality. +> OAuth tokens work best with GitHub Copilot. Personal Access Tokens may have limited functionality depending on permissions. + +## Session Management + +OpenCode supports persistent sessions that survive workspace restarts: -## Session Resumption +- **Default behavior**: Automatically resumes the latest session +- **Disable resumption**: Set `resume_session = false` for fresh sessions +- **Requirements**: Persistent storage for home directory or workspace volume -By default, the module resumes the latest OpenCode session when the workspace restarts. Set `resume_session = false` to always start fresh sessions. +```tf +module "opencode" { + source = "registry.coder.com/rothnic/opencode/coder" + version = "1.0.0" + agent_id = coder_agent.example.id + workdir = "/home/coder/projects" + resume_session = false # Always start fresh +} +``` > [!NOTE] -> Session resumption requires persistent storage for the home directory or workspace volume. Without persistent storage, sessions will not resume across workspace restarts. +> Without persistent storage, sessions cannot resume across workspace restarts, and you'll start fresh each time. ## GitHub Copilot Integration -OpenCode can use GitHub Copilot as an AI provider, giving you access to powerful models like Claude Sonnet and GPT-4 through your GitHub Copilot subscription. To enable this: +OpenCode leverages GitHub Copilot to provide access to powerful AI models including: + +- **Claude Sonnet 4** and **Claude Sonnet 4.5** +- **GPT-4** and **GPT-5** +- Other models available through your GitHub Copilot subscription + +### Setup Steps -1. Configure GitHub external auth in Coder (recommended) OR provide a GitHub token -2. Ensure you have an active GitHub Copilot subscription -3. Set `opencode_provider = "copilot"` (this is the default) +1. **Configure Authentication** + - Set up [Coder external auth](https://coder.com/docs/admin/external-auth) for GitHub, OR + - Provide a GitHub token via `github_token` variable -OpenCode will automatically authenticate with GitHub Copilot using your credentials. See the [OpenCode GitHub documentation](https://opencode.ai/docs/github/) for more details on GitHub Copilot integration. +2. **Verify Copilot Subscription** + - Ensure you have an active [GitHub Copilot subscription](https://docs.github.com/en/copilot/about-github-copilot/subscription-plans-for-github-copilot) + - Individual, Pro+, Business, or Enterprise plans supported + +3. **Use Copilot Provider** + - Set `opencode_provider = "copilot"` (default) + - OpenCode automatically authenticates using your credentials + +For more details, see the [OpenCode GitHub Integration documentation](https://opencode.ai/docs/github/). ## Troubleshooting -If you encounter any issues, check the log files in the `~/.opencode-module` directory within your workspace for detailed information. +### Check Installation Logs + +All logs are stored in `~/.opencode-module/` within your workspace: ```bash # Installation logs @@ -213,13 +287,71 @@ cat ~/.opencode-module/pre_install.log cat ~/.opencode-module/post_install.log ``` -> [!NOTE] -> The `workdir` variable is required and specifies the directory where OpenCode will run. +### Common Issues + +**OpenCode not found after installation** +```bash +# Check if OpenCode is in PATH +which opencode + +# Verify installation +opencode --version + +# Reload shell +source ~/.bashrc +``` + +**GitHub authentication fails** +```bash +# Check Coder external auth +coder external-auth access-token github + +# Manually configure +opencode auth login +``` + +**Node.js version too old** +```bash +# Check Node version +node --version -## References +# Install Node.js 20+ +curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - +sudo apt-get install -y nodejs +``` + +## Configuration Variables + +| Variable | Type | Default | Description | +|----------|------|---------|-------------| +| `agent_id` | string | (required) | The ID of the Coder agent | +| `workdir` | string | (required) | Working directory for OpenCode | +| `opencode_provider` | string | `"copilot"` | AI provider to use | +| `github_token` | string | `""` | GitHub token for authentication | +| `opencode_version` | string | `"latest"` | OpenCode version to install | +| `install_method` | string | `"npm"` | Installation method: npm or curl | +| `report_tasks` | bool | `true` | Enable task reporting to Coder UI | +| `resume_session` | bool | `true` | Resume sessions on restart | +| `subdomain` | bool | `false` | Use subdomain for AgentAPI | +| `cli_app` | bool | `false` | Create CLI app in agent bar | + +See the full list of variables in [main.tf](./main.tf). + +## Learn More + +- 📚 [OpenCode Documentation](https://opencode.ai/docs/) +- 🐙 [OpenCode GitHub Repository](https://github.com/opencode-ai/opencode) +- 🔗 [OpenCode GitHub Integration](https://opencode.ai/docs/github/) +- 🔌 [OpenCode Providers](https://opencode.ai/docs/providers/) +- 🤖 [AgentAPI Documentation](https://github.com/coder/agentapi) +- 📖 [Coder AI Agents Guide](https://coder.com/docs/tutorials/ai-agents) + +## Support + +For issues specific to this module, please [open an issue](https://github.com/rothnic/coder-registry/issues) in the repository. + +For OpenCode-related questions, visit the [OpenCode GitHub Discussions](https://github.com/opencode-ai/opencode/discussions). + +--- -- [OpenCode.ai Documentation](https://opencode.ai/docs/) -- [OpenCode GitHub Repository](https://github.com/opencode-ai/opencode) -- [OpenCode GitHub Integration](https://opencode.ai/docs/github/) -- [AgentAPI Documentation](https://github.com/coder/agentapi) -- [Coder AI Agents Guide](https://coder.com/docs/tutorials/ai-agents) +**Maintained by** [Nick Roth](https://github.com/rothnic) | [Website](https://www.nickroth.com) | [LinkedIn](http://www.linkedin.com/in/nicholasleeroth/) From fbfda134865a77598c20a2e8ca49c1eb398f2f54 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 19 Nov 2025 19:38:49 +0000 Subject: [PATCH 03/42] feat: add Coder Tasks support to OpenCode module Adds task_app_id output to enable integration with Coder Tasks UI. This allows users to run tasks through OpenCode using the coder_ai_task resource in their templates. Changes: - Added task_app_id output that exposes the AgentAPI task app ID - Enables chat sidebar integration for Coder Tasks feature - Compatible with coder_task data source for prompt passing --- registry/rothnic/modules/opencode/main.tf | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/registry/rothnic/modules/opencode/main.tf b/registry/rothnic/modules/opencode/main.tf index 765990120..e4ebdb04f 100644 --- a/registry/rothnic/modules/opencode/main.tf +++ b/registry/rothnic/modules/opencode/main.tf @@ -252,3 +252,9 @@ module "agentapi" { /tmp/install.sh EOT } + +# Output for Coder Tasks integration +output "task_app_id" { + description = "The app ID for use with coder_ai_task resource" + value = module.agentapi.task_app_id +} From a8ad0f2d6bd5c454c21de0d492291bd624e074e4 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 19 Nov 2025 19:48:11 +0000 Subject: [PATCH 04/42] fix: correct task_app_id output construction The agentapi module doesn't expose task_app_id as an output. Instead, construct it manually using the Coder app ID pattern: {agent_id}_{app_slug} This matches how Coder internally constructs app IDs and will work correctly with the coder_ai_task resource. --- registry/rothnic/modules/opencode/main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/registry/rothnic/modules/opencode/main.tf b/registry/rothnic/modules/opencode/main.tf index e4ebdb04f..b0f23f1dc 100644 --- a/registry/rothnic/modules/opencode/main.tf +++ b/registry/rothnic/modules/opencode/main.tf @@ -256,5 +256,5 @@ module "agentapi" { # Output for Coder Tasks integration output "task_app_id" { description = "The app ID for use with coder_ai_task resource" - value = module.agentapi.task_app_id + value = "${var.agent_id}_${local.app_slug}" } From 240ae1b5cabc51a157141d85123e7a667950b632 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 19 Nov 2025 20:55:57 +0000 Subject: [PATCH 05/42] fix: remove task_app_id output - agentapi handles tasks internally The agentapi module (v1.2.0) creates the coder_ai_task resource internally, so modules should not expose task_app_id outputs. Following the pattern from copilot and goose modules, which have no outputs and let agentapi handle all task integration. This fixes the 'only one coder_ai_task resource can be provisioned' error when templates try to create coder_ai_task resources. --- registry/rothnic/modules/opencode/main.tf | 6 ------ 1 file changed, 6 deletions(-) diff --git a/registry/rothnic/modules/opencode/main.tf b/registry/rothnic/modules/opencode/main.tf index b0f23f1dc..765990120 100644 --- a/registry/rothnic/modules/opencode/main.tf +++ b/registry/rothnic/modules/opencode/main.tf @@ -252,9 +252,3 @@ module "agentapi" { /tmp/install.sh EOT } - -# Output for Coder Tasks integration -output "task_app_id" { - description = "The app ID for use with coder_ai_task resource" - value = "${var.agent_id}_${local.app_slug}" -} From ce86151ad578971af1b51df489216442eb318c18 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 19 Nov 2025 21:08:15 +0000 Subject: [PATCH 06/42] fix: use OpenCode ACP mode for agentapi integration OpenCode needs to be started with 'opencode acp' subcommand to enable Agent Communication Protocol (ACP) mode for stdio communication with agentapi. Changes: - Use 'opencode acp' instead of plain 'opencode' command - Remove '--type opencode' flag (not needed in ACP mode) - Update terminal dimensions to 67x1190 (matching goose/claude-code) - Add comment explaining ACP mode requirement This fixes the 'agentapi server not responding' error by properly enabling stdio/JSON-RPC communication between OpenCode and agentapi. --- registry/rothnic/modules/opencode/scripts/start.sh | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/registry/rothnic/modules/opencode/scripts/start.sh b/registry/rothnic/modules/opencode/scripts/start.sh index a88096c22..a0a722183 100644 --- a/registry/rothnic/modules/opencode/scripts/start.sh +++ b/registry/rothnic/modules/opencode/scripts/start.sh @@ -86,24 +86,26 @@ start_agentapi() { build_opencode_args - echo "Starting OpenCode..." + echo "Starting OpenCode with agentapi in ACP mode..." local initial_prompt initial_prompt=$(build_initial_prompt) + # OpenCode uses 'acp' subcommand for stdio/ACP communication with agentapi + # Following the goose pattern for terminal dimensions if [ -n "$initial_prompt" ]; then echo "Using initial prompt with system context" if [ ${#OPENCODE_ARGS[@]} -gt 0 ]; then echo "OpenCode arguments: ${OPENCODE_ARGS[*]}" - agentapi server -I="$initial_prompt" --type opencode --term-width 120 --term-height 40 -- opencode "${OPENCODE_ARGS[@]}" + agentapi server -I="$initial_prompt" --term-width 67 --term-height 1190 -- opencode acp "${OPENCODE_ARGS[@]}" else - agentapi server -I="$initial_prompt" --type opencode --term-width 120 --term-height 40 -- opencode + agentapi server -I="$initial_prompt" --term-width 67 --term-height 1190 -- opencode acp fi else if [ ${#OPENCODE_ARGS[@]} -gt 0 ]; then echo "OpenCode arguments: ${OPENCODE_ARGS[*]}" - agentapi server --type opencode --term-width 120 --term-height 40 -- opencode "${OPENCODE_ARGS[@]}" + agentapi server --term-width 67 --term-height 1190 -- opencode acp "${OPENCODE_ARGS[@]}" else - agentapi server --type opencode --term-width 120 --term-height 40 -- opencode + agentapi server --term-width 67 --term-height 1190 -- opencode acp fi fi } From 256d71b48f0d38ee996f518401a1d86cd1b62296 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 19 Nov 2025 21:24:14 +0000 Subject: [PATCH 07/42] fix: remove non-existent 'acp' subcommand from OpenCode OpenCode is a TUI application that runs directly, not an ACP/stdio agent. It doesn't have an 'acp' subcommand. This commit fixes the start script to run 'opencode' directly with --type opencode flag, following the same pattern as the copilot module. Changes: - Remove 'opencode acp' command (doesn't exist) - Run 'opencode' directly like other TUI agents - Add --type opencode flag for agentapi - Update comments to reflect TUI nature --- registry/rothnic/modules/opencode/scripts/start.sh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/registry/rothnic/modules/opencode/scripts/start.sh b/registry/rothnic/modules/opencode/scripts/start.sh index a0a722183..c379c0d32 100644 --- a/registry/rothnic/modules/opencode/scripts/start.sh +++ b/registry/rothnic/modules/opencode/scripts/start.sh @@ -86,26 +86,26 @@ start_agentapi() { build_opencode_args - echo "Starting OpenCode with agentapi in ACP mode..." + echo "Starting OpenCode with agentapi..." local initial_prompt initial_prompt=$(build_initial_prompt) - # OpenCode uses 'acp' subcommand for stdio/ACP communication with agentapi - # Following the goose pattern for terminal dimensions + # OpenCode runs as a TUI application, similar to copilot + # Use --type opencode for agentapi to recognize the agent type if [ -n "$initial_prompt" ]; then echo "Using initial prompt with system context" if [ ${#OPENCODE_ARGS[@]} -gt 0 ]; then echo "OpenCode arguments: ${OPENCODE_ARGS[*]}" - agentapi server -I="$initial_prompt" --term-width 67 --term-height 1190 -- opencode acp "${OPENCODE_ARGS[@]}" + agentapi server -I="$initial_prompt" --type opencode --term-width 67 --term-height 1190 -- opencode "${OPENCODE_ARGS[@]}" else - agentapi server -I="$initial_prompt" --term-width 67 --term-height 1190 -- opencode acp + agentapi server -I="$initial_prompt" --type opencode --term-width 67 --term-height 1190 -- opencode fi else if [ ${#OPENCODE_ARGS[@]} -gt 0 ]; then echo "OpenCode arguments: ${OPENCODE_ARGS[*]}" - agentapi server --term-width 67 --term-height 1190 -- opencode acp "${OPENCODE_ARGS[@]}" + agentapi server --type opencode --term-width 67 --term-height 1190 -- opencode "${OPENCODE_ARGS[@]}" else - agentapi server --term-width 67 --term-height 1190 -- opencode acp + agentapi server --type opencode --term-width 67 --term-height 1190 -- opencode fi fi } From 37dfc9deb320de23f2e84cca2fa14f32cb0d7c52 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 19 Nov 2025 21:29:43 +0000 Subject: [PATCH 08/42] revert: restore 'opencode acp' command - acp subcommand exists Reverting previous commit - OpenCode does have an 'acp' subcommand for starting the ACP (Agent Client Protocol) server. The original implementation was correct. --- registry/rothnic/modules/opencode/scripts/start.sh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/registry/rothnic/modules/opencode/scripts/start.sh b/registry/rothnic/modules/opencode/scripts/start.sh index c379c0d32..d2a1ea6d8 100644 --- a/registry/rothnic/modules/opencode/scripts/start.sh +++ b/registry/rothnic/modules/opencode/scripts/start.sh @@ -86,26 +86,26 @@ start_agentapi() { build_opencode_args - echo "Starting OpenCode with agentapi..." + echo "Starting OpenCode with agentapi in ACP mode..." local initial_prompt initial_prompt=$(build_initial_prompt) - # OpenCode runs as a TUI application, similar to copilot - # Use --type opencode for agentapi to recognize the agent type + # OpenCode uses 'acp' subcommand for ACP (Agent Client Protocol) server + # Following the goose pattern for terminal dimensions if [ -n "$initial_prompt" ]; then echo "Using initial prompt with system context" if [ ${#OPENCODE_ARGS[@]} -gt 0 ]; then echo "OpenCode arguments: ${OPENCODE_ARGS[*]}" - agentapi server -I="$initial_prompt" --type opencode --term-width 67 --term-height 1190 -- opencode "${OPENCODE_ARGS[@]}" + agentapi server -I="$initial_prompt" --term-width 67 --term-height 1190 -- opencode acp "${OPENCODE_ARGS[@]}" else - agentapi server -I="$initial_prompt" --type opencode --term-width 67 --term-height 1190 -- opencode + agentapi server -I="$initial_prompt" --term-width 67 --term-height 1190 -- opencode acp fi else if [ ${#OPENCODE_ARGS[@]} -gt 0 ]; then echo "OpenCode arguments: ${OPENCODE_ARGS[*]}" - agentapi server --type opencode --term-width 67 --term-height 1190 -- opencode "${OPENCODE_ARGS[@]}" + agentapi server --term-width 67 --term-height 1190 -- opencode acp "${OPENCODE_ARGS[@]}" else - agentapi server --type opencode --term-width 67 --term-height 1190 -- opencode + agentapi server --term-width 67 --term-height 1190 -- opencode acp fi fi } From 5ea4096f1b2c384c780729a77f7c771da98ec456 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 19 Nov 2025 21:36:02 +0000 Subject: [PATCH 09/42] fix: run opencode TUI directly, not acp subcommand AgentAPI wraps the TUI application and handles the protocol layer itself. The agent should run in normal TUI mode, not in ACP mode. Following the copilot pattern: agentapi wraps the interactive agent. Changes: - Run 'opencode' instead of 'opencode acp' - Add --type opencode flag for agent identification - Let agentapi handle stdio/protocol communication --- registry/rothnic/modules/opencode/scripts/start.sh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/registry/rothnic/modules/opencode/scripts/start.sh b/registry/rothnic/modules/opencode/scripts/start.sh index d2a1ea6d8..1aaf3d5e1 100644 --- a/registry/rothnic/modules/opencode/scripts/start.sh +++ b/registry/rothnic/modules/opencode/scripts/start.sh @@ -86,26 +86,26 @@ start_agentapi() { build_opencode_args - echo "Starting OpenCode with agentapi in ACP mode..." + echo "Starting OpenCode with agentapi..." local initial_prompt initial_prompt=$(build_initial_prompt) - # OpenCode uses 'acp' subcommand for ACP (Agent Client Protocol) server - # Following the goose pattern for terminal dimensions + # Run regular opencode TUI - agentapi wraps it and handles the protocol layer + # Following the copilot pattern for terminal dimensions and --type flag if [ -n "$initial_prompt" ]; then echo "Using initial prompt with system context" if [ ${#OPENCODE_ARGS[@]} -gt 0 ]; then echo "OpenCode arguments: ${OPENCODE_ARGS[*]}" - agentapi server -I="$initial_prompt" --term-width 67 --term-height 1190 -- opencode acp "${OPENCODE_ARGS[@]}" + agentapi server -I="$initial_prompt" --type opencode --term-width 67 --term-height 1190 -- opencode "${OPENCODE_ARGS[@]}" else - agentapi server -I="$initial_prompt" --term-width 67 --term-height 1190 -- opencode acp + agentapi server -I="$initial_prompt" --type opencode --term-width 67 --term-height 1190 -- opencode fi else if [ ${#OPENCODE_ARGS[@]} -gt 0 ]; then echo "OpenCode arguments: ${OPENCODE_ARGS[*]}" - agentapi server --term-width 67 --term-height 1190 -- opencode acp "${OPENCODE_ARGS[@]}" + agentapi server --type opencode --term-width 67 --term-height 1190 -- opencode "${OPENCODE_ARGS[@]}" else - agentapi server --term-width 67 --term-height 1190 -- opencode acp + agentapi server --type opencode --term-width 67 --term-height 1190 -- opencode fi fi } From fba18240aa0773d86d688fead2c43c6a19895909 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 19 Nov 2025 23:08:47 +0000 Subject: [PATCH 10/42] fix: automatically install Node.js if not present The install script now installs Node.js 20 automatically instead of exiting with an error when Node.js is not found. This enables the module to work in base Docker images without Node.js pre-installed. Changes: - Replace validate_prerequisites with install_nodejs function - Auto-install Node.js via apt-get, yum, apk, or nvm - Support multiple package managers and Linux distributions - Attempt to upgrade if Node.js version < 18 - Only install for npm method (curl method includes Node.js) --- .../modules/opencode/scripts/install.sh | 54 +++++++++++++++---- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/registry/rothnic/modules/opencode/scripts/install.sh b/registry/rothnic/modules/opencode/scripts/install.sh index 75a3f1886..9e655f216 100644 --- a/registry/rothnic/modules/opencode/scripts/install.sh +++ b/registry/rothnic/modules/opencode/scripts/install.sh @@ -15,22 +15,56 @@ ARG_EXTERNAL_AUTH_ID=${ARG_EXTERNAL_AUTH_ID:-github} ARG_OPENCODE_VERSION=${ARG_OPENCODE_VERSION:-latest} ARG_INSTALL_METHOD=${ARG_INSTALL_METHOD:-npm} -validate_prerequisites() { - if [ "$ARG_INSTALL_METHOD" = "npm" ]; then - if ! command_exists node; then - echo "ERROR: Node.js not found. OpenCode requires Node.js v18+." - echo "Install with: curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - && sudo apt-get install -y nodejs" - exit 1 +install_nodejs() { + if [ "$ARG_INSTALL_METHOD" != "npm" ]; then + # curl install method includes Node.js, skip + return 0 + fi + + if ! command_exists node; then + echo "Node.js not found. Installing Node.js 20..." + + # Try to install using package manager + if command_exists apt-get; then + echo "Installing Node.js via apt-get..." + curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - + sudo apt-get install -y nodejs + elif command_exists yum; then + echo "Installing Node.js via yum..." + curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash - + sudo yum install -y nodejs + elif command_exists apk; then + echo "Installing Node.js via apk..." + sudo apk add --no-cache nodejs npm + else + echo "WARNING: Could not detect package manager. Attempting to install via nvm..." + curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash + export NVM_DIR="$HOME/.nvm" + [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" + nvm install 20 + nvm use 20 fi - if ! command_exists npm; then - echo "ERROR: npm not found. OpenCode requires npm." + # Reload shell environment + source "$HOME"/.bashrc 2>/dev/null || true + + if ! command_exists node; then + echo "ERROR: Failed to install Node.js" exit 1 fi + echo "✓ Node.js installed successfully: $(node --version)" + else node_version=$(node --version | sed 's/v//' | cut -d. -f1) if [ "$node_version" -lt 18 ]; then - echo "WARNING: Node.js v$node_version detected. OpenCode requires v18+." + echo "WARNING: Node.js v$node_version detected. OpenCode requires v18+. Attempting upgrade..." + # Attempt to upgrade (best effort) + if command_exists apt-get; then + curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - + sudo apt-get install -y nodejs + fi + else + echo "✓ Node.js $(node --version) already installed" fi fi } @@ -149,7 +183,7 @@ configure_coder_integration() { fi } -validate_prerequisites +install_nodejs install_opencode check_github_authentication setup_opencode_configurations From 2cc8aa50b09bce404c49d4f0726a12f2e6f7b4fb Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 19 Nov 2025 23:26:35 +0000 Subject: [PATCH 11/42] fix: install OpenCode to user directory without sudo Fixed npm permission error by configuring npm to install packages to ~/.local instead of /usr/lib/node_modules which requires sudo. Changes: - Set npm prefix to $HOME/.local before installing - Create ~/.local/bin directory - Persist PATH to .bashrc for future sessions - Add version output to installation success messages This allows OpenCode to install in unprivileged containers without requiring sudo access. --- .../rothnic/modules/opencode/scripts/install.sh | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/registry/rothnic/modules/opencode/scripts/install.sh b/registry/rothnic/modules/opencode/scripts/install.sh index 9e655f216..6c20e509d 100644 --- a/registry/rothnic/modules/opencode/scripts/install.sh +++ b/registry/rothnic/modules/opencode/scripts/install.sh @@ -76,6 +76,16 @@ install_opencode() { if [ "$ARG_INSTALL_METHOD" = "curl" ]; then curl -fsSL https://opencode.ai/install | bash elif [ "$ARG_INSTALL_METHOD" = "npm" ]; then + # Configure npm to install to user directory to avoid permission issues + mkdir -p "$HOME/.local/bin" + npm config set prefix "$HOME/.local" + export PATH="$HOME/.local/bin:$PATH" + + # Persist PATH to shell profile + if ! grep -q 'PATH="$HOME/.local/bin:$PATH"' "$HOME/.bashrc" 2>/dev/null; then + echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$HOME/.bashrc" + fi + if [ "$ARG_OPENCODE_VERSION" = "latest" ]; then npm install -g opencode-ai@latest else @@ -94,9 +104,9 @@ install_opencode() { exit 1 fi - echo "OpenCode installed successfully" + echo "✓ OpenCode installed successfully: $(opencode --version 2>&1 | head -1)" else - echo "OpenCode already installed" + echo "✓ OpenCode already installed: $(opencode --version 2>&1 | head -1)" fi } From 965c7f20e461d0d45097840e9430919507f693e8 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 20 Nov 2025 17:11:13 +0000 Subject: [PATCH 12/42] fix: use OpenCode ACP mode for agentapi integration The default 'opencode' TUI mode exits with help text when run through agentapi's pseudo-terminal. Use 'opencode acp' (Agent Communication Protocol) mode instead, which is designed for stdio communication with agent wrappers. Changes: - Run 'opencode acp' instead of 'opencode' - Remove --type flag (not needed with acp mode) - ACP mode handles protocol communication properly This resolves the "non-zero exit code" error where OpenCode was printing help and exiting immediately. --- registry/rothnic/modules/opencode/scripts/start.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/registry/rothnic/modules/opencode/scripts/start.sh b/registry/rothnic/modules/opencode/scripts/start.sh index 1aaf3d5e1..d25f27595 100644 --- a/registry/rothnic/modules/opencode/scripts/start.sh +++ b/registry/rothnic/modules/opencode/scripts/start.sh @@ -90,22 +90,22 @@ start_agentapi() { local initial_prompt initial_prompt=$(build_initial_prompt) - # Run regular opencode TUI - agentapi wraps it and handles the protocol layer - # Following the copilot pattern for terminal dimensions and --type flag + # Use 'opencode acp' for Agent Communication Protocol mode + # ACP mode is designed for stdio communication with agent wrappers like agentapi if [ -n "$initial_prompt" ]; then echo "Using initial prompt with system context" if [ ${#OPENCODE_ARGS[@]} -gt 0 ]; then echo "OpenCode arguments: ${OPENCODE_ARGS[*]}" - agentapi server -I="$initial_prompt" --type opencode --term-width 67 --term-height 1190 -- opencode "${OPENCODE_ARGS[@]}" + agentapi server -I="$initial_prompt" --term-width 67 --term-height 1190 -- opencode acp "${OPENCODE_ARGS[@]}" else - agentapi server -I="$initial_prompt" --type opencode --term-width 67 --term-height 1190 -- opencode + agentapi server -I="$initial_prompt" --term-width 67 --term-height 1190 -- opencode acp fi else if [ ${#OPENCODE_ARGS[@]} -gt 0 ]; then echo "OpenCode arguments: ${OPENCODE_ARGS[*]}" - agentapi server --type opencode --term-width 67 --term-height 1190 -- opencode "${OPENCODE_ARGS[@]}" + agentapi server --term-width 67 --term-height 1190 -- opencode acp "${OPENCODE_ARGS[@]}" else - agentapi server --type opencode --term-width 67 --term-height 1190 -- opencode + agentapi server --term-width 67 --term-height 1190 -- opencode acp fi fi } From 9df216e2d3d7d234f43a42c86b730dad674f911b Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 20 Nov 2025 17:24:53 +0000 Subject: [PATCH 13/42] fix: use correct OpenCode integration per agentapi PR #79 Updated to use the correct OpenCode command format as implemented in agentapi. OpenCode requires --type=opencode flag and runs with plain 'opencode' command, not acp/serve/run subcommands. Changes: - Use --type=opencode flag (required per PR #79) - Run 'opencode' without subcommands - Remove incorrect 'acp' subcommand usage - Update comments to reference correct implementation Reference: https://github.com/coder/agentapi/pull/79 --- registry/rothnic/modules/opencode/scripts/start.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/registry/rothnic/modules/opencode/scripts/start.sh b/registry/rothnic/modules/opencode/scripts/start.sh index d25f27595..4ac71f7fd 100644 --- a/registry/rothnic/modules/opencode/scripts/start.sh +++ b/registry/rothnic/modules/opencode/scripts/start.sh @@ -90,22 +90,22 @@ start_agentapi() { local initial_prompt initial_prompt=$(build_initial_prompt) - # Use 'opencode acp' for Agent Communication Protocol mode - # ACP mode is designed for stdio communication with agent wrappers like agentapi + # OpenCode requires explicit --type=opencode flag per agentapi PR #79 + # Use plain 'opencode' command (not acp, serve, or run) if [ -n "$initial_prompt" ]; then echo "Using initial prompt with system context" if [ ${#OPENCODE_ARGS[@]} -gt 0 ]; then echo "OpenCode arguments: ${OPENCODE_ARGS[*]}" - agentapi server -I="$initial_prompt" --term-width 67 --term-height 1190 -- opencode acp "${OPENCODE_ARGS[@]}" + agentapi server -I="$initial_prompt" --type=opencode --term-width 67 --term-height 1190 -- opencode "${OPENCODE_ARGS[@]}" else - agentapi server -I="$initial_prompt" --term-width 67 --term-height 1190 -- opencode acp + agentapi server -I="$initial_prompt" --type=opencode --term-width 67 --term-height 1190 -- opencode fi else if [ ${#OPENCODE_ARGS[@]} -gt 0 ]; then echo "OpenCode arguments: ${OPENCODE_ARGS[*]}" - agentapi server --term-width 67 --term-height 1190 -- opencode acp "${OPENCODE_ARGS[@]}" + agentapi server --type=opencode --term-width 67 --term-height 1190 -- opencode "${OPENCODE_ARGS[@]}" else - agentapi server --term-width 67 --term-height 1190 -- opencode acp + agentapi server --type=opencode --term-width 67 --term-height 1190 -- opencode fi fi } From fb326994da244d76b743340cb60e4d6c7d3d8594 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 20 Nov 2025 17:48:26 +0000 Subject: [PATCH 14/42] fix: use opencode acp mode for stdio communication OpenCode's CLI changed since agentapi support was added. The default 'opencode' command now requires explicit subcommands. Use 'opencode acp' (Agent Communication Protocol) which is designed for stdio-based agent wrappers like agentapi. Changes: - Use 'opencode acp' subcommand for ACP protocol mode - Keep --type=opencode flag for agentapi - ACP mode handles interactive stdin/stdout communication - Pass provider and other args to acp subcommand The acp mode is specifically designed for agent protocol communication via stdin/stdout, which is what agentapi provides. --- .../rothnic/modules/opencode/scripts/start.sh | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/registry/rothnic/modules/opencode/scripts/start.sh b/registry/rothnic/modules/opencode/scripts/start.sh index 4ac71f7fd..065a0fd98 100644 --- a/registry/rothnic/modules/opencode/scripts/start.sh +++ b/registry/rothnic/modules/opencode/scripts/start.sh @@ -86,26 +86,26 @@ start_agentapi() { build_opencode_args - echo "Starting OpenCode with agentapi..." + echo "Starting OpenCode with agentapi in ACP mode..." local initial_prompt initial_prompt=$(build_initial_prompt) - # OpenCode requires explicit --type=opencode flag per agentapi PR #79 - # Use plain 'opencode' command (not acp, serve, or run) + # Use 'opencode acp' - Agent Communication Protocol mode for stdio communication + # ACP is designed for agent wrappers like agentapi that communicate via stdin/stdout if [ -n "$initial_prompt" ]; then echo "Using initial prompt with system context" if [ ${#OPENCODE_ARGS[@]} -gt 0 ]; then - echo "OpenCode arguments: ${OPENCODE_ARGS[*]}" - agentapi server -I="$initial_prompt" --type=opencode --term-width 67 --term-height 1190 -- opencode "${OPENCODE_ARGS[@]}" + echo "OpenCode ACP arguments: ${OPENCODE_ARGS[*]}" + agentapi server -I="$initial_prompt" --type=opencode --term-width 67 --term-height 1190 -- opencode acp "${OPENCODE_ARGS[@]}" else - agentapi server -I="$initial_prompt" --type=opencode --term-width 67 --term-height 1190 -- opencode + agentapi server -I="$initial_prompt" --type=opencode --term-width 67 --term-height 1190 -- opencode acp fi else if [ ${#OPENCODE_ARGS[@]} -gt 0 ]; then - echo "OpenCode arguments: ${OPENCODE_ARGS[*]}" - agentapi server --type=opencode --term-width 67 --term-height 1190 -- opencode "${OPENCODE_ARGS[@]}" + echo "OpenCode ACP arguments: ${OPENCODE_ARGS[*]}" + agentapi server --type=opencode --term-width 67 --term-height 1190 -- opencode acp "${OPENCODE_ARGS[@]}" else - agentapi server --type=opencode --term-width 67 --term-height 1190 -- opencode + agentapi server --type=opencode --term-width 67 --term-height 1190 -- opencode acp fi fi } From 5cf6e653ff2b6437489b9fd53ec031498044b0b7 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 20 Nov 2025 18:04:37 +0000 Subject: [PATCH 15/42] fix: configure GitHub Copilot via auth.json, remove unsupported --provider flag The 'opencode acp' subcommand doesn't accept --provider argument. Provider selection is configured via ~/.local/share/opencode/auth.json. Changes to install.sh: - Programmatically create auth.json with GitHub Copilot credentials - Use GitHub token from Coder external auth - Write proper JSON structure for OpenCode auth - Create empty auth.json if no token available Changes to start.sh: - Remove --provider argument (not supported by acp) - Simplify command to just 'opencode acp' - Provider is auto-selected from auth.json This enables automatic GitHub Copilot authentication without manual 'opencode auth login' interaction. --- .../modules/opencode/scripts/install.sh | 30 +++++++++++++++++-- .../rothnic/modules/opencode/scripts/start.sh | 23 ++++---------- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/registry/rothnic/modules/opencode/scripts/install.sh b/registry/rothnic/modules/opencode/scripts/install.sh index 6c20e509d..71e9e92db 100644 --- a/registry/rothnic/modules/opencode/scripts/install.sh +++ b/registry/rothnic/modules/opencode/scripts/install.sh @@ -163,6 +163,11 @@ setup_opencode_config() { configure_github_copilot_provider() { echo "Configuring GitHub Copilot provider for OpenCode..." + export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}" + local opencode_data_dir="$XDG_DATA_HOME/opencode" + local auth_file="$opencode_data_dir/auth.json" + mkdir -p "$opencode_data_dir" + local github_token="" if [ -n "${GITHUB_TOKEN:-}" ]; then @@ -172,11 +177,32 @@ configure_github_copilot_provider() { fi if [ -n "$github_token" ] && [ "$github_token" != "null" ]; then - echo "✓ GitHub token available for Copilot provider" + echo "✓ GitHub token available - configuring Copilot provider in auth.json" + + # Create auth.json with GitHub Copilot credentials + cat > "$auth_file" < "$auth_file" + fi fi } diff --git a/registry/rothnic/modules/opencode/scripts/start.sh b/registry/rothnic/modules/opencode/scripts/start.sh index 065a0fd98..f1dbe257f 100644 --- a/registry/rothnic/modules/opencode/scripts/start.sh +++ b/registry/rothnic/modules/opencode/scripts/start.sh @@ -41,10 +41,8 @@ $ARG_AI_PROMPT" build_opencode_args() { OPENCODE_ARGS=() - # Add provider if specified - if [ -n "$ARG_OPENCODE_PROVIDER" ]; then - OPENCODE_ARGS+=(--provider "$ARG_OPENCODE_PROVIDER") - fi + # ACP mode doesn't accept --provider argument + # Provider is configured via auth.json during installation } setup_github_authentication() { @@ -84,29 +82,18 @@ start_agentapi() { echo "Starting in directory: $ARG_WORKDIR" cd "$ARG_WORKDIR" - build_opencode_args - echo "Starting OpenCode with agentapi in ACP mode..." local initial_prompt initial_prompt=$(build_initial_prompt) # Use 'opencode acp' - Agent Communication Protocol mode for stdio communication # ACP is designed for agent wrappers like agentapi that communicate via stdin/stdout + # Provider selection is handled via auth.json (configured during installation) if [ -n "$initial_prompt" ]; then echo "Using initial prompt with system context" - if [ ${#OPENCODE_ARGS[@]} -gt 0 ]; then - echo "OpenCode ACP arguments: ${OPENCODE_ARGS[*]}" - agentapi server -I="$initial_prompt" --type=opencode --term-width 67 --term-height 1190 -- opencode acp "${OPENCODE_ARGS[@]}" - else - agentapi server -I="$initial_prompt" --type=opencode --term-width 67 --term-height 1190 -- opencode acp - fi + agentapi server -I="$initial_prompt" --type=opencode --term-width 67 --term-height 1190 -- opencode acp else - if [ ${#OPENCODE_ARGS[@]} -gt 0 ]; then - echo "OpenCode ACP arguments: ${OPENCODE_ARGS[*]}" - agentapi server --type=opencode --term-width 67 --term-height 1190 -- opencode acp "${OPENCODE_ARGS[@]}" - else - agentapi server --type=opencode --term-width 67 --term-height 1190 -- opencode acp - fi + agentapi server --type=opencode --term-width 67 --term-height 1190 -- opencode acp fi } From 8a6a0293dcaa03d450895ddf03260081768d110e Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 20 Nov 2025 18:31:03 +0000 Subject: [PATCH 16/42] fix: use opencode TUI mode, not acp mode for agentapi Agentapi sends plain text to stdin/stdout, NOT JSON-RPC protocol. The 'opencode acp' mode expects JSON-RPC messages and fails when receiving plain text from agentapi. Use the default OpenCode TUI mode with working directory as the project path argument, similar to how goose integration works. Changes: - Run 'opencode ' instead of 'opencode acp' - OpenCode TUI reads plain text from stdin (matches agentapi) - Remove ACP mode which expects JSON-RPC protocol - Pass working directory as positional argument to avoid help screen This matches the pattern used by goose and copilot modules. --- registry/rothnic/modules/opencode/scripts/start.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/registry/rothnic/modules/opencode/scripts/start.sh b/registry/rothnic/modules/opencode/scripts/start.sh index f1dbe257f..09efb4695 100644 --- a/registry/rothnic/modules/opencode/scripts/start.sh +++ b/registry/rothnic/modules/opencode/scripts/start.sh @@ -82,18 +82,18 @@ start_agentapi() { echo "Starting in directory: $ARG_WORKDIR" cd "$ARG_WORKDIR" - echo "Starting OpenCode with agentapi in ACP mode..." + echo "Starting OpenCode TUI with agentapi..." local initial_prompt initial_prompt=$(build_initial_prompt) - # Use 'opencode acp' - Agent Communication Protocol mode for stdio communication - # ACP is designed for agent wrappers like agentapi that communicate via stdin/stdout - # Provider selection is handled via auth.json (configured during installation) + # Run opencode TUI with working directory as project path + # Agentapi sends plain text to stdin/stdout, not JSON-RPC + # OpenCode's TUI mode reads plain text input, similar to copilot/goose if [ -n "$initial_prompt" ]; then echo "Using initial prompt with system context" - agentapi server -I="$initial_prompt" --type=opencode --term-width 67 --term-height 1190 -- opencode acp + agentapi server -I="$initial_prompt" --type=opencode --term-width 67 --term-height 1190 -- opencode "$ARG_WORKDIR" else - agentapi server --type=opencode --term-width 67 --term-height 1190 -- opencode acp + agentapi server --type=opencode --term-width 67 --term-height 1190 -- opencode "$ARG_WORKDIR" fi } From 57e13df37fdcd6b4a7ba7c1491d6a507f2ea70b1 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 20 Nov 2025 18:43:53 +0000 Subject: [PATCH 17/42] fix: configure auth.json in start script, not install script Moving GitHub Copilot authentication setup from install.sh to start.sh ensures the auth token is available when needed. The external auth token may not be accessible during initial workspace build but is available when the workspace starts. Changes: - Move auth.json configuration to setup_github_authentication() in start.sh - Write credentials to auth.json every time workspace starts - Ensures fresh token from Coder external auth on each start - Fallback to empty credentials if no token available This matches the pattern where authentication needs to happen at runtime, not build time. --- .../rothnic/modules/opencode/scripts/start.sh | 46 ++++++++++++++----- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/registry/rothnic/modules/opencode/scripts/start.sh b/registry/rothnic/modules/opencode/scripts/start.sh index 09efb4695..7aa3a1b34 100644 --- a/registry/rothnic/modules/opencode/scripts/start.sh +++ b/registry/rothnic/modules/opencode/scripts/start.sh @@ -49,22 +49,38 @@ setup_github_authentication() { export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}" echo "Setting up GitHub authentication..." + local opencode_data_dir="$XDG_DATA_HOME/opencode" + local auth_file="$opencode_data_dir/auth.json" + mkdir -p "$opencode_data_dir" + + local github_token="" + if [ -n "${GITHUB_TOKEN:-}" ]; then - export GH_TOKEN="$GITHUB_TOKEN" + github_token="$GITHUB_TOKEN" echo "✓ Using GitHub token from module configuration" - return 0 + elif command_exists coder; then + github_token=$(coder external-auth access-token "${ARG_EXTERNAL_AUTH_ID:-github}" 2> /dev/null || echo "") + if [ -n "$github_token" ] && [ "$github_token" != "null" ]; then + echo "✓ Using Coder external auth OAuth token" + fi fi - if command_exists coder; then - local github_token - if github_token=$(coder external-auth access-token "${ARG_EXTERNAL_AUTH_ID:-github}" 2> /dev/null); then - if [ -n "$github_token" ] && [ "$github_token" != "null" ]; then - export GITHUB_TOKEN="$github_token" - export GH_TOKEN="$github_token" - echo "✓ Using Coder external auth OAuth token" - return 0 - fi - fi + if [ -n "$github_token" ] && [ "$github_token" != "null" ]; then + # Create/update auth.json with GitHub Copilot credentials + cat > "$auth_file" < /dev/null 2>&1; then @@ -75,6 +91,12 @@ setup_github_authentication() { echo "⚠ No GitHub authentication available" echo " OpenCode can still work with other providers" echo " Use 'opencode auth login' to configure providers" + + # Ensure auth.json exists even without credentials + if [ ! -f "$auth_file" ]; then + echo '{"credentials":[]}' > "$auth_file" + fi + return 0 } From 44f08cadad24ab662e6b892f295ec17767375d0f Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 20 Nov 2025 20:36:02 +0000 Subject: [PATCH 18/42] feat: add opencode_auth_config variable for pre-configured authentication GitHub Copilot authentication in OpenCode requires a complex auth.json format with refresh tokens and session metadata that can't be generated from a simple OAuth token. The device flow ('opencode auth login') may also fail in restricted network environments. This adds support for providing a pre-configured auth.json obtained by running 'opencode auth login' on a local machine with proper network access. Changes: - Add opencode_auth_config variable (sensitive) to main.tf - Pass auth config to start script via ARG_OPENCODE_AUTH_CONFIG - Check for pre-configured auth first in setup_github_authentication() - Write provided auth.json directly to ~/.local/share/opencode/auth.json - Add helpful error messages for obtaining auth.json locally Usage: 1. Run 'opencode auth login' locally to authenticate 2. Copy ~/.local/share/opencode/auth.json content 3. Pass it to the module via opencode_auth_config variable --- registry/rothnic/modules/opencode/main.tf | 8 ++++++++ .../rothnic/modules/opencode/scripts/start.sh | 18 +++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/registry/rothnic/modules/opencode/main.tf b/registry/rothnic/modules/opencode/main.tf index 765990120..1611a6eac 100644 --- a/registry/rothnic/modules/opencode/main.tf +++ b/registry/rothnic/modules/opencode/main.tf @@ -43,6 +43,13 @@ variable "opencode_config" { default = "" } +variable "opencode_auth_config" { + type = string + description = "Pre-configured OpenCode auth.json content as JSON string. Use this to provide GitHub Copilot credentials obtained from running 'opencode auth login' locally." + default = "" + sensitive = true +} + variable "ai_prompt" { type = string description = "Initial task prompt for programmatic mode." @@ -232,6 +239,7 @@ module "agentapi" { ARG_OPENCODE_PROVIDER='${var.opencode_provider}' \ ARG_EXTERNAL_AUTH_ID='${var.external_auth_id}' \ ARG_RESUME_SESSION='${var.resume_session}' \ + ARG_OPENCODE_AUTH_CONFIG='${var.opencode_auth_config != "" ? base64encode(var.opencode_auth_config) : ""}' \ /tmp/start.sh EOT diff --git a/registry/rothnic/modules/opencode/scripts/start.sh b/registry/rothnic/modules/opencode/scripts/start.sh index 7aa3a1b34..acc0ba2a6 100644 --- a/registry/rothnic/modules/opencode/scripts/start.sh +++ b/registry/rothnic/modules/opencode/scripts/start.sh @@ -14,6 +14,7 @@ ARG_SYSTEM_PROMPT=$(echo -n "${ARG_SYSTEM_PROMPT:-}" | base64 -d 2> /dev/null || ARG_EXTERNAL_AUTH_ID=${ARG_EXTERNAL_AUTH_ID:-github} ARG_RESUME_SESSION=${ARG_RESUME_SESSION:-true} ARG_OPENCODE_PROVIDER=${ARG_OPENCODE_PROVIDER:-copilot} +ARG_OPENCODE_AUTH_CONFIG=$(echo -n "${ARG_OPENCODE_AUTH_CONFIG:-}" | base64 -d 2> /dev/null || echo "") validate_opencode_installation() { if ! command_exists opencode; then @@ -53,6 +54,14 @@ setup_github_authentication() { local auth_file="$opencode_data_dir/auth.json" mkdir -p "$opencode_data_dir" + # Check if pre-configured auth.json was provided + if [ -n "$ARG_OPENCODE_AUTH_CONFIG" ]; then + echo "✓ Using pre-configured auth.json from module variable" + echo "$ARG_OPENCODE_AUTH_CONFIG" > "$auth_file" + return 0 + fi + + # Try to get GitHub token from external auth local github_token="" if [ -n "${GITHUB_TOKEN:-}" ]; then @@ -66,7 +75,9 @@ setup_github_authentication() { fi if [ -n "$github_token" ] && [ "$github_token" != "null" ]; then - # Create/update auth.json with GitHub Copilot credentials + # Note: This creates a simplified auth.json that may not work with GitHub Copilot + # For full GitHub Copilot support, use the opencode_auth_config variable + echo "⚠ Warning: Using simplified auth format - GitHub Copilot may require device flow auth" cat > "$auth_file" < Date: Thu, 20 Nov 2025 22:40:20 +0000 Subject: [PATCH 19/42] docs: rewrite README with concise authentication guide Simplified README to focus on essential information: - Minimal quick start example - Clear GitHub Copilot authentication options (all optional) - Recommended approach: pre-configured auth.json via file() - Note about TUI limitations through AgentAPI - Concise configuration table Authentication is completely optional - module works without it and users can configure manually via 'opencode auth login'. --- registry/rothnic/modules/opencode/README.md | 361 +++----------------- 1 file changed, 49 insertions(+), 312 deletions(-) diff --git a/registry/rothnic/modules/opencode/README.md b/registry/rothnic/modules/opencode/README.md index 724326e89..857f08629 100644 --- a/registry/rothnic/modules/opencode/README.md +++ b/registry/rothnic/modules/opencode/README.md @@ -1,357 +1,94 @@ --- display_name: OpenCode -description: AI-powered terminal coding agent with support for multiple providers including GitHub Copilot, Anthropic, and OpenAI +description: AI-powered terminal coding agent with support for GitHub Copilot, Anthropic, and OpenAI icon: ../../../../.icons/code.svg maintainer_github: rothnic verified: false -tags: [agent, ai, opencode, coding-assistant, copilot, terminal, automation] +tags: [agent, ai, opencode, coding-assistant, copilot, terminal] --- # OpenCode -Integrate [OpenCode.ai](https://opencode.ai/) into your Coder workspace for AI-powered coding assistance directly from the terminal. OpenCode is a powerful, open-source AI coding agent built specifically for the terminal that brings the power of AI pair programming to your command line. - -## What is OpenCode? - -[OpenCode](https://github.com/opencode-ai/opencode) is an AI coding agent that runs in your terminal and helps you: - -- **Write code faster** with intelligent completions and suggestions -- **Debug issues** with AI-powered error analysis and fixes -- **Refactor code** with automated improvements and best practices -- **Learn new technologies** with contextual explanations and examples -- **Automate tasks** through natural language instructions - -Unlike traditional IDEs with AI extensions, OpenCode is designed for terminal-first workflows and integrates seamlessly with your existing command-line tools. - -## Key Features - -- 🤖 **Multiple AI Providers**: Choose from GitHub Copilot, Anthropic Claude, OpenAI GPT, and more -- 🔐 **Seamless Authentication**: Automatic GitHub authentication via Coder external auth -- 📊 **Task Reporting**: Integration with [AgentAPI](https://github.com/coder/agentapi) for real-time task tracking in Coder UI -- 💾 **Session Persistence**: Resume your AI conversations across workspace restarts -- 🎨 **Customizable**: Configure prompts, providers, and behavior to match your workflow -- 🚀 **Multiple Installation Methods**: NPM or direct curl installation +Integrate [OpenCode.ai](https://opencode.ai/) - an AI coding agent that runs in your terminal and integrates with Coder's task system via [AgentAPI](https://github.com/coder/agentapi). ## Quick Start ```tf module "opencode" { - source = "registry.coder.com/rothnic/opencode/coder" - version = "1.0.0" - agent_id = coder_agent.example.id - workdir = "/home/coder/projects" -} -``` - -This basic configuration installs OpenCode with GitHub Copilot as the default provider and enables task reporting to your Coder UI. - -> [!IMPORTANT] -> For GitHub Copilot integration, ensure you have [Coder external authentication](https://coder.com/docs/admin/external-auth) configured with `id = "github"`. Alternatively, provide a token via the `github_token` variable or configure authentication interactively with `opencode auth login`. - -> [!NOTE] -> By default, this module uses path-based app access. In production, we recommend configuring a [wildcard access URL](https://coder.com/docs/admin/setup#wildcard-access-url) and setting `subdomain = true`. See [security best practices](https://coder.com/docs/tutorials/best-practices/security-best-practices#disable-path-based-apps) for details. - -## Prerequisites - -- **Node.js v18+** and **npm** (when using npm install method) -- **AI Provider Access** (one of the following): - - **[GitHub Copilot](https://github.com/features/copilot)**: Active subscription (Individual, Pro, Business, or Enterprise) - - **[Anthropic](https://www.anthropic.com/)**: API key for Claude models - - **[OpenAI](https://platform.openai.com/)**: API key for GPT models -- **GitHub Authentication** (for Copilot provider): - - [Coder external authentication](https://coder.com/docs/admin/external-auth) (recommended) - - Direct token via `github_token` variable - - Interactive login via `opencode auth login` - -## Usage Examples - -### Basic GitHub Copilot Setup - -The simplest way to get started with GitHub Copilot: - -```tf -module "opencode" { - source = "registry.coder.com/rothnic/opencode/coder" - version = "1.0.0" - agent_id = coder_agent.example.id - workdir = "/home/coder/projects" - - opencode_provider = "copilot" -} -``` - -### Task-Based Workflow - -Enable initial prompts and task tracking for automated workflows: - -```tf -data "coder_parameter" "ai_prompt" { - type = "string" - name = "AI Prompt" - default = "" - description = "Initial task prompt for OpenCode." - mutable = true -} - -module "opencode" { - source = "registry.coder.com/rothnic/opencode/coder" - version = "1.0.0" - agent_id = coder_agent.example.id - workdir = "/home/coder/projects" + source = "git::https://github.com/rothnic/coder-registry.git//registry/rothnic/modules/opencode" - ai_prompt = data.coder_parameter.ai_prompt.value - resume_session = true + agent_id = coder_agent.main.id + workdir = "/workspaces" } ``` -### Multiple AI Providers +## Authentication (Optional) -Switch between different AI providers based on your needs: +### GitHub Copilot -```tf -module "opencode" { - source = "registry.coder.com/rothnic/opencode/coder" - version = "1.0.0" - agent_id = coder_agent.example.id - workdir = "/home/coder/projects" +OpenCode requires GitHub Copilot's special session token format. Three options: - # Choose your provider: "copilot", "anthropic", "openai", etc. - opencode_provider = "anthropic" +**Option 1: Pre-configured auth (Recommended)** - # Configure provider credentials via opencode_config - # or set up interactively with 'opencode auth login' -} -``` - -### Advanced Configuration - -Full customization with version pinning and pre-installation scripts: - -```tf -module "opencode" { - source = "registry.coder.com/rothnic/opencode/coder" - version = "1.0.0" - agent_id = coder_agent.example.id - workdir = "/home/coder/projects" - - # Pin OpenCode version for stability - opencode_version = "0.1.5" - - # Choose installation method - install_method = "npm" # or "curl" - - # Custom configuration - opencode_config = jsonencode({ - theme = "dark" - # Additional OpenCode config options - }) - - # Ensure Node.js is available - pre_install_script = <<-EOT - #!/bin/bash - curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - - sudo apt-get install -y nodejs - EOT -} +Run locally: +```bash +npm install -g opencode-ai +opencode auth login # Complete device flow +cat ~/.local/share/opencode/auth.json # Copy this ``` -### Direct Token Authentication - -Provide a GitHub token directly instead of using Coder external auth: - +In your template: ```tf -variable "github_token" { - type = string - description = "GitHub Personal Access Token" - sensitive = true -} - module "opencode" { - source = "registry.coder.com/rothnic/opencode/coder" - version = "1.0.0" - agent_id = coder_agent.example.id - workdir = "/home/coder/projects" - github_token = var.github_token -} -``` + source = "..." -### Standalone CLI Mode - -Install OpenCode as a standalone CLI tool without the web interface: - -```tf -module "opencode" { - source = "registry.coder.com/rothnic/opencode/coder" - version = "1.0.0" - agent_id = coder_agent.example.id - workdir = "/home/coder" - report_tasks = false - cli_app = true + opencode_auth_config = file("${path.module}/opencode-auth.json") # Store as file + # Or use variable: opencode_auth_config = var.opencode_auth_json } ``` -This makes OpenCode available as a CLI app in your Coder agent bar for direct terminal access. - -## Authentication - -### GitHub Copilot Provider - -The module supports multiple authentication methods (in priority order): +**Option 2: Coder external auth (May not work)** -1. **[Coder External Auth](https://coder.com/docs/admin/external-auth) (Recommended)** - - Automatic OAuth token retrieval - - Best security and user experience - - Configured at the Coder deployment level +Configure [external auth](https://coder.com/docs/admin/external-auth) with `id = "github"`. Note: Regular OAuth tokens may not work with Copilot's authentication. -2. **Direct Token** - - Pass via `github_token` variable - - Supports OAuth or Personal Access Tokens - - Good for testing or specific use cases +**Option 3: Manual login** -3. **Interactive Login** - - Run `opencode auth login` in the workspace - - Manual configuration of providers - - Useful for multiple provider setups +Users run `opencode auth login` in the workspace. Auth persists across restarts. ### Other Providers -For Anthropic, OpenAI, and other providers: - -- Use `opencode auth login` to configure interactively -- Set via `opencode_config` variable with API keys -- Configure through environment variables in your template - -> [!NOTE] -> OAuth tokens work best with GitHub Copilot. Personal Access Tokens may have limited functionality depending on permissions. - -## Session Management - -OpenCode supports persistent sessions that survive workspace restarts: - -- **Default behavior**: Automatically resumes the latest session -- **Disable resumption**: Set `resume_session = false` for fresh sessions -- **Requirements**: Persistent storage for home directory or workspace volume - -```tf -module "opencode" { - source = "registry.coder.com/rothnic/opencode/coder" - version = "1.0.0" - agent_id = coder_agent.example.id - workdir = "/home/coder/projects" - resume_session = false # Always start fresh -} -``` - -> [!NOTE] -> Without persistent storage, sessions cannot resume across workspace restarts, and you'll start fresh each time. - -## GitHub Copilot Integration - -OpenCode leverages GitHub Copilot to provide access to powerful AI models including: - -- **Claude Sonnet 4** and **Claude Sonnet 4.5** -- **GPT-4** and **GPT-5** -- Other models available through your GitHub Copilot subscription - -### Setup Steps - -1. **Configure Authentication** - - Set up [Coder external auth](https://coder.com/docs/admin/external-auth) for GitHub, OR - - Provide a GitHub token via `github_token` variable - -2. **Verify Copilot Subscription** - - Ensure you have an active [GitHub Copilot subscription](https://docs.github.com/en/copilot/about-github-copilot/subscription-plans-for-github-copilot) - - Individual, Pro+, Business, or Enterprise plans supported - -3. **Use Copilot Provider** - - Set `opencode_provider = "copilot"` (default) - - OpenCode automatically authenticates using your credentials +For Anthropic, OpenAI, etc., users authenticate via `opencode auth login` in the workspace. -For more details, see the [OpenCode GitHub Integration documentation](https://opencode.ai/docs/github/). +## Configuration -## Troubleshooting +| Variable | Description | Default | +|----------|-------------|---------| +| `agent_id` | Coder agent ID | Required | +| `workdir` | Working directory | Required | +| `opencode_auth_config` | Pre-configured auth.json content | `""` (optional) | +| `opencode_version` | OpenCode version | `"latest"` | +| `install_method` | Installation method: `npm` or `curl` | `"npm"` | +| `report_tasks` | Enable Coder task reporting | `true` | +| `subdomain` | Use subdomain for app access | `false` | -### Check Installation Logs +See [variables](./main.tf) for complete list. -All logs are stored in `~/.opencode-module/` within your workspace: +## Features -```bash -# Installation logs -cat ~/.opencode-module/install.log - -# Startup logs -cat ~/.opencode-module/agentapi-start.log +- 🤖 Multiple AI providers (Copilot, Claude, GPT) +- 📊 Task reporting to Coder UI +- 💾 Session persistence +- 🔐 Flexible authentication +- 🚀 Auto-installs Node.js and OpenCode -# Pre/post install script logs -cat ~/.opencode-module/pre_install.log -cat ~/.opencode-module/post_install.log -``` +## Notes -### Common Issues +- Requires Node.js 18+ (auto-installed) +- TUI may have limitations through AgentAPI (slash commands, menus) +- For production, use `subdomain = true` with [wildcard access URL](https://coder.com/docs/admin/setup#wildcard-access-url) -**OpenCode not found after installation** -```bash -# Check if OpenCode is in PATH -which opencode - -# Verify installation -opencode --version - -# Reload shell -source ~/.bashrc -``` - -**GitHub authentication fails** -```bash -# Check Coder external auth -coder external-auth access-token github - -# Manually configure -opencode auth login -``` - -**Node.js version too old** -```bash -# Check Node version -node --version - -# Install Node.js 20+ -curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - -sudo apt-get install -y nodejs -``` - -## Configuration Variables - -| Variable | Type | Default | Description | -|----------|------|---------|-------------| -| `agent_id` | string | (required) | The ID of the Coder agent | -| `workdir` | string | (required) | Working directory for OpenCode | -| `opencode_provider` | string | `"copilot"` | AI provider to use | -| `github_token` | string | `""` | GitHub token for authentication | -| `opencode_version` | string | `"latest"` | OpenCode version to install | -| `install_method` | string | `"npm"` | Installation method: npm or curl | -| `report_tasks` | bool | `true` | Enable task reporting to Coder UI | -| `resume_session` | bool | `true` | Resume sessions on restart | -| `subdomain` | bool | `false` | Use subdomain for AgentAPI | -| `cli_app` | bool | `false` | Create CLI app in agent bar | - -See the full list of variables in [main.tf](./main.tf). - -## Learn More - -- 📚 [OpenCode Documentation](https://opencode.ai/docs/) -- 🐙 [OpenCode GitHub Repository](https://github.com/opencode-ai/opencode) -- 🔗 [OpenCode GitHub Integration](https://opencode.ai/docs/github/) -- 🔌 [OpenCode Providers](https://opencode.ai/docs/providers/) -- 🤖 [AgentAPI Documentation](https://github.com/coder/agentapi) -- 📖 [Coder AI Agents Guide](https://coder.com/docs/tutorials/ai-agents) - -## Support - -For issues specific to this module, please [open an issue](https://github.com/rothnic/coder-registry/issues) in the repository. - -For OpenCode-related questions, visit the [OpenCode GitHub Discussions](https://github.com/opencode-ai/opencode/discussions). - ---- +## Resources -**Maintained by** [Nick Roth](https://github.com/rothnic) | [Website](https://www.nickroth.com) | [LinkedIn](http://www.linkedin.com/in/nicholasleeroth/) +- [OpenCode Documentation](https://opencode.ai/docs/) +- [OpenCode GitHub](https://github.com/opencode-ai/opencode) +- [AgentAPI](https://github.com/coder/agentapi) From e651da34b6e55dee8b736046ddc933e6761f3c5f Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 20 Nov 2025 22:50:49 +0000 Subject: [PATCH 20/42] fix: prevent chmod failures from marking workspace unhealthy The wrapper scripts use 'set -o errexit' which causes them to exit with code 1 if chmod fails. This marks the workspace as unhealthy even though the actual service starts fine. Changes: - Add '|| true' to chmod commands to prevent script failure - Use 'bash /tmp/script.sh' instead of '/tmp/script.sh' as fallback - Fix source .bashrc to not fail on missing file This ensures non-critical chmod failures don't prevent workspace startup. --- registry/rothnic/modules/opencode/main.tf | 8 ++++---- registry/rothnic/modules/opencode/scripts/start.sh | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/registry/rothnic/modules/opencode/main.tf b/registry/rothnic/modules/opencode/main.tf index 1611a6eac..b9e068541 100644 --- a/registry/rothnic/modules/opencode/main.tf +++ b/registry/rothnic/modules/opencode/main.tf @@ -231,7 +231,7 @@ module "agentapi" { set -o errexit set -o pipefail echo -n '${base64encode(local.start_script)}' | base64 -d > /tmp/start.sh - chmod +x /tmp/start.sh + chmod +x /tmp/start.sh || true ARG_WORKDIR='${local.workdir}' \ ARG_AI_PROMPT='${base64encode(var.ai_prompt)}' \ @@ -240,7 +240,7 @@ module "agentapi" { ARG_EXTERNAL_AUTH_ID='${var.external_auth_id}' \ ARG_RESUME_SESSION='${var.resume_session}' \ ARG_OPENCODE_AUTH_CONFIG='${var.opencode_auth_config != "" ? base64encode(var.opencode_auth_config) : ""}' \ - /tmp/start.sh + bash /tmp/start.sh EOT install_script = <<-EOT @@ -248,7 +248,7 @@ module "agentapi" { set -o errexit set -o pipefail echo -n '${base64encode(local.install_script)}' | base64 -d > /tmp/install.sh - chmod +x /tmp/install.sh + chmod +x /tmp/install.sh || true ARG_MCP_APP_STATUS_SLUG='${local.app_slug}' \ ARG_REPORT_TASKS='${var.report_tasks}' \ @@ -257,6 +257,6 @@ module "agentapi" { ARG_EXTERNAL_AUTH_ID='${var.external_auth_id}' \ ARG_OPENCODE_VERSION='${var.opencode_version}' \ ARG_INSTALL_METHOD='${var.install_method}' \ - /tmp/install.sh + bash /tmp/install.sh EOT } diff --git a/registry/rothnic/modules/opencode/scripts/start.sh b/registry/rothnic/modules/opencode/scripts/start.sh index acc0ba2a6..7003d0c38 100644 --- a/registry/rothnic/modules/opencode/scripts/start.sh +++ b/registry/rothnic/modules/opencode/scripts/start.sh @@ -1,7 +1,7 @@ #!/bin/bash set -euo pipefail -source "$HOME"/.bashrc +source "$HOME"/.bashrc 2>/dev/null || true export PATH="$HOME/.local/bin:$PATH" command_exists() { From 5d8541d98a118c5306e67d155e31aebeb1bdc949 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 20 Nov 2025 23:59:17 +0000 Subject: [PATCH 21/42] fix: add retry logic for apt operations to handle transient failures APT operations can fail transiently due to: - Lock contention from concurrent apt processes - Network timeouts - Repository unavailability Changes: - Add retry_apt() function with exponential backoff (5 attempts, 2s-32s delays) - Wait for existing apt locks before proceeding (up to 30s) - Retry NodeSource repo setup - Retry apt-get install operations - Retry yum and apk operations similarly This prevents workspace build failures from transient apt issues, especially common in task workspaces with concurrent installations. --- .../modules/opencode/scripts/install.sh | 52 ++++++++++++++++--- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/registry/rothnic/modules/opencode/scripts/install.sh b/registry/rothnic/modules/opencode/scripts/install.sh index 71e9e92db..689d5442a 100644 --- a/registry/rothnic/modules/opencode/scripts/install.sh +++ b/registry/rothnic/modules/opencode/scripts/install.sh @@ -7,6 +7,27 @@ command_exists() { command -v "$1" > /dev/null 2>&1 } +# Retry function for apt operations +retry_apt() { + local max_attempts=5 + local attempt=1 + local delay=2 + + while [ $attempt -le $max_attempts ]; do + if "$@"; then + return 0 + fi + + echo "Command failed (attempt $attempt/$max_attempts). Retrying in ${delay}s..." + sleep $delay + attempt=$((attempt + 1)) + delay=$((delay * 2)) + done + + echo "ERROR: Command failed after $max_attempts attempts: $*" + return 1 +} + ARG_WORKDIR=${ARG_WORKDIR:-"$HOME"} ARG_REPORT_TASKS=${ARG_REPORT_TASKS:-true} ARG_MCP_APP_STATUS_SLUG=${ARG_MCP_APP_STATUS_SLUG:-} @@ -27,15 +48,32 @@ install_nodejs() { # Try to install using package manager if command_exists apt-get; then echo "Installing Node.js via apt-get..." - curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - - sudo apt-get install -y nodejs + + # Wait for any existing apt processes to finish + local wait_count=0 + while sudo fuser /var/lib/dpkg/lock-frontend >/dev/null 2>&1 || \ + sudo fuser /var/lib/apt/lists/lock >/dev/null 2>&1; do + if [ $wait_count -ge 30 ]; then + echo "WARNING: Waited 30s for apt lock, proceeding anyway..." + break + fi + echo "Waiting for other apt processes to finish..." + sleep 1 + wait_count=$((wait_count + 1)) + done + + # Add NodeSource repository with retries + retry_apt bash -c "curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -" + + # Install Node.js with retries + retry_apt sudo apt-get install -y nodejs elif command_exists yum; then echo "Installing Node.js via yum..." - curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash - - sudo yum install -y nodejs + retry_apt bash -c "curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -" + retry_apt sudo yum install -y nodejs elif command_exists apk; then echo "Installing Node.js via apk..." - sudo apk add --no-cache nodejs npm + retry_apt sudo apk add --no-cache nodejs npm else echo "WARNING: Could not detect package manager. Attempting to install via nvm..." curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash @@ -60,8 +98,8 @@ install_nodejs() { echo "WARNING: Node.js v$node_version detected. OpenCode requires v18+. Attempting upgrade..." # Attempt to upgrade (best effort) if command_exists apt-get; then - curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - - sudo apt-get install -y nodejs + retry_apt bash -c "curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -" + retry_apt sudo apt-get install -y nodejs fi else echo "✓ Node.js $(node --version) already installed" From 1b70cc456a85306076d876fe032cbcb25372bc7b Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 21 Nov 2025 00:15:32 +0000 Subject: [PATCH 22/42] refactor: use nodejs module instead of installing Node.js directly Simplified the module to use the existing thezoker/nodejs module rather than managing Node.js installation ourselves. This removes all apt-get/yum/apk installation logic and retry mechanisms. Changes: - Replace install_nodejs() with validate_nodejs() - Check for Node.js and provide helpful error if missing - Update README to show using nodejs module as dependency - Add depends_on in example - Remove all apt-get retry logic (no longer needed) This follows the pattern used by other modules like codex and makes the module more maintainable. Users should add: module "nodejs" { source = "registry.coder.com/thezoker/nodejs/coder" agent_id = coder_agent.main.id } --- registry/rothnic/modules/opencode/README.md | 16 ++- .../modules/opencode/scripts/install.sh | 104 ++++-------------- 2 files changed, 34 insertions(+), 86 deletions(-) diff --git a/registry/rothnic/modules/opencode/README.md b/registry/rothnic/modules/opencode/README.md index 857f08629..6c5efff10 100644 --- a/registry/rothnic/modules/opencode/README.md +++ b/registry/rothnic/modules/opencode/README.md @@ -14,11 +14,19 @@ Integrate [OpenCode.ai](https://opencode.ai/) - an AI coding agent that runs in ## Quick Start ```tf +# Node.js 18+ required for npm install method +module "nodejs" { + source = "registry.coder.com/thezoker/nodejs/coder" + agent_id = coder_agent.main.id +} + module "opencode" { source = "git::https://github.com/rothnic/coder-registry.git//registry/rothnic/modules/opencode" agent_id = coder_agent.main.id workdir = "/workspaces" + + depends_on = [module.nodejs] } ``` @@ -79,11 +87,15 @@ See [variables](./main.tf) for complete list. - 📊 Task reporting to Coder UI - 💾 Session persistence - 🔐 Flexible authentication -- 🚀 Auto-installs Node.js and OpenCode +- 🚀 Uses nodejs module for installation + +## Prerequisites + +- **Node.js 18+**: Use the [nodejs module](https://github.com/coder/registry/tree/main/registry/thezoker/modules/nodejs) (npm install method only) ## Notes -- Requires Node.js 18+ (auto-installed) +- Uses [thezoker/nodejs](https://github.com/coder/registry/tree/main/registry/thezoker/modules/nodejs) module for Node.js installation - TUI may have limitations through AgentAPI (slash commands, menus) - For production, use `subdomain = true` with [wildcard access URL](https://coder.com/docs/admin/setup#wildcard-access-url) diff --git a/registry/rothnic/modules/opencode/scripts/install.sh b/registry/rothnic/modules/opencode/scripts/install.sh index 689d5442a..b79475fd8 100644 --- a/registry/rothnic/modules/opencode/scripts/install.sh +++ b/registry/rothnic/modules/opencode/scripts/install.sh @@ -7,27 +7,6 @@ command_exists() { command -v "$1" > /dev/null 2>&1 } -# Retry function for apt operations -retry_apt() { - local max_attempts=5 - local attempt=1 - local delay=2 - - while [ $attempt -le $max_attempts ]; do - if "$@"; then - return 0 - fi - - echo "Command failed (attempt $attempt/$max_attempts). Retrying in ${delay}s..." - sleep $delay - attempt=$((attempt + 1)) - delay=$((delay * 2)) - done - - echo "ERROR: Command failed after $max_attempts attempts: $*" - return 1 -} - ARG_WORKDIR=${ARG_WORKDIR:-"$HOME"} ARG_REPORT_TASKS=${ARG_REPORT_TASKS:-true} ARG_MCP_APP_STATUS_SLUG=${ARG_MCP_APP_STATUS_SLUG:-} @@ -36,75 +15,32 @@ ARG_EXTERNAL_AUTH_ID=${ARG_EXTERNAL_AUTH_ID:-github} ARG_OPENCODE_VERSION=${ARG_OPENCODE_VERSION:-latest} ARG_INSTALL_METHOD=${ARG_INSTALL_METHOD:-npm} -install_nodejs() { +validate_nodejs() { if [ "$ARG_INSTALL_METHOD" != "npm" ]; then - # curl install method includes Node.js, skip + # curl install method doesn't need Node.js return 0 fi if ! command_exists node; then - echo "Node.js not found. Installing Node.js 20..." - - # Try to install using package manager - if command_exists apt-get; then - echo "Installing Node.js via apt-get..." - - # Wait for any existing apt processes to finish - local wait_count=0 - while sudo fuser /var/lib/dpkg/lock-frontend >/dev/null 2>&1 || \ - sudo fuser /var/lib/apt/lists/lock >/dev/null 2>&1; do - if [ $wait_count -ge 30 ]; then - echo "WARNING: Waited 30s for apt lock, proceeding anyway..." - break - fi - echo "Waiting for other apt processes to finish..." - sleep 1 - wait_count=$((wait_count + 1)) - done - - # Add NodeSource repository with retries - retry_apt bash -c "curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -" - - # Install Node.js with retries - retry_apt sudo apt-get install -y nodejs - elif command_exists yum; then - echo "Installing Node.js via yum..." - retry_apt bash -c "curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -" - retry_apt sudo yum install -y nodejs - elif command_exists apk; then - echo "Installing Node.js via apk..." - retry_apt sudo apk add --no-cache nodejs npm - else - echo "WARNING: Could not detect package manager. Attempting to install via nvm..." - curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash - export NVM_DIR="$HOME/.nvm" - [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" - nvm install 20 - nvm use 20 - fi - - # Reload shell environment - source "$HOME"/.bashrc 2>/dev/null || true - - if ! command_exists node; then - echo "ERROR: Failed to install Node.js" - exit 1 - fi + echo "ERROR: Node.js not found. OpenCode requires Node.js 18+ when using npm install method." + echo "Please install Node.js using the nodejs module:" + echo "" + echo " module \"nodejs\" {" + echo " source = \"registry.coder.com/thezoker/nodejs/coder\"" + echo " agent_id = coder_agent.main.id" + echo " }" + echo "" + exit 1 + fi - echo "✓ Node.js installed successfully: $(node --version)" - else - node_version=$(node --version | sed 's/v//' | cut -d. -f1) - if [ "$node_version" -lt 18 ]; then - echo "WARNING: Node.js v$node_version detected. OpenCode requires v18+. Attempting upgrade..." - # Attempt to upgrade (best effort) - if command_exists apt-get; then - retry_apt bash -c "curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -" - retry_apt sudo apt-get install -y nodejs - fi - else - echo "✓ Node.js $(node --version) already installed" - fi + node_version=$(node --version | sed 's/v//' | cut -d. -f1) + if [ "$node_version" -lt 18 ]; then + echo "ERROR: Node.js v$node_version detected. OpenCode requires v18+." + echo "Please use the nodejs module with version >= 18." + exit 1 fi + + echo "✓ Node.js $(node --version) found" } install_opencode() { @@ -257,7 +193,7 @@ configure_coder_integration() { fi } -install_nodejs +validate_nodejs install_opencode check_github_authentication setup_opencode_configurations From 99eacefe7b0cad05f6c5b89b9a482fedb7d8749e Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 21 Nov 2025 14:54:01 +0000 Subject: [PATCH 23/42] refactor: replace nodejs module with NVM-based installation and pnpm - Install Node.js LTS via NVM instead of using thezoker/nodejs module - Add pnpm for faster, more efficient package management - Update install.sh to use pnpm instead of npm for OpenCode installation - Update start.sh to load NVM and ensure pnpm is in PATH - Update README to reflect automatic Node.js installation via NVM - Remove nodejs module dependency from documentation This change provides more reliable Node.js installation timing and improves package installation performance with pnpm. --- registry/rothnic/modules/opencode/README.md | 19 ++--- .../modules/opencode/scripts/install.sh | 76 +++++++++++-------- .../rothnic/modules/opencode/scripts/start.sh | 8 +- 3 files changed, 58 insertions(+), 45 deletions(-) diff --git a/registry/rothnic/modules/opencode/README.md b/registry/rothnic/modules/opencode/README.md index 6c5efff10..49478f654 100644 --- a/registry/rothnic/modules/opencode/README.md +++ b/registry/rothnic/modules/opencode/README.md @@ -14,22 +14,16 @@ Integrate [OpenCode.ai](https://opencode.ai/) - an AI coding agent that runs in ## Quick Start ```tf -# Node.js 18+ required for npm install method -module "nodejs" { - source = "registry.coder.com/thezoker/nodejs/coder" - agent_id = coder_agent.main.id -} - module "opencode" { source = "git::https://github.com/rothnic/coder-registry.git//registry/rothnic/modules/opencode" agent_id = coder_agent.main.id workdir = "/workspaces" - - depends_on = [module.nodejs] } ``` +Node.js LTS is automatically installed via NVM when using the `npm` install method (default). The module also installs pnpm for faster package management. + ## Authentication (Optional) ### GitHub Copilot @@ -87,15 +81,18 @@ See [variables](./main.tf) for complete list. - 📊 Task reporting to Coder UI - 💾 Session persistence - 🔐 Flexible authentication -- 🚀 Uses nodejs module for installation +- 🚀 Automatic Node.js installation via NVM +- ⚡ Fast package management with pnpm ## Prerequisites -- **Node.js 18+**: Use the [nodejs module](https://github.com/coder/registry/tree/main/registry/thezoker/modules/nodejs) (npm install method only) +- **Node.js 18+**: Automatically installed via NVM (npm install method only) +- **pnpm**: Automatically installed for faster package management ## Notes -- Uses [thezoker/nodejs](https://github.com/coder/registry/tree/main/registry/thezoker/modules/nodejs) module for Node.js installation +- Node.js LTS is automatically installed via NVM (if using npm install method) +- Uses pnpm for faster, more efficient package installation - TUI may have limitations through AgentAPI (slash commands, menus) - For production, use `subdomain = true` with [wildcard access URL](https://coder.com/docs/admin/setup#wildcard-access-url) diff --git a/registry/rothnic/modules/opencode/scripts/install.sh b/registry/rothnic/modules/opencode/scripts/install.sh index b79475fd8..87d106459 100644 --- a/registry/rothnic/modules/opencode/scripts/install.sh +++ b/registry/rothnic/modules/opencode/scripts/install.sh @@ -15,32 +15,53 @@ ARG_EXTERNAL_AUTH_ID=${ARG_EXTERNAL_AUTH_ID:-github} ARG_OPENCODE_VERSION=${ARG_OPENCODE_VERSION:-latest} ARG_INSTALL_METHOD=${ARG_INSTALL_METHOD:-npm} -validate_nodejs() { +install_nodejs() { if [ "$ARG_INSTALL_METHOD" != "npm" ]; then # curl install method doesn't need Node.js return 0 fi - if ! command_exists node; then - echo "ERROR: Node.js not found. OpenCode requires Node.js 18+ when using npm install method." - echo "Please install Node.js using the nodejs module:" - echo "" - echo " module \"nodejs\" {" - echo " source = \"registry.coder.com/thezoker/nodejs/coder\"" - echo " agent_id = coder_agent.main.id" - echo " }" - echo "" - exit 1 + if command_exists node; then + echo "✓ Node.js $(node --version) already installed" + return 0 fi - node_version=$(node --version | sed 's/v//' | cut -d. -f1) - if [ "$node_version" -lt 18 ]; then - echo "ERROR: Node.js v$node_version detected. OpenCode requires v18+." - echo "Please use the nodejs module with version >= 18." - exit 1 + echo "Installing Node.js via NVM..." + + # Install NVM + export NVM_DIR="$HOME/.nvm" + if [ ! -d "$NVM_DIR" ]; then + echo "Installing NVM..." + curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash fi - echo "✓ Node.js $(node --version) found" + # Load NVM + [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" + [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" + + # Install Node.js LTS + echo "Installing Node.js LTS..." + nvm install --lts + nvm alias default node + nvm use default + + echo "✓ Node.js $(node --version) installed successfully" +} + +install_pnpm() { + if [ "$ARG_INSTALL_METHOD" != "npm" ]; then + return 0 + fi + + if command_exists pnpm; then + echo "✓ pnpm $(pnpm --version) already installed" + return 0 + fi + + echo "Installing pnpm..." + npm install -g pnpm + + echo "✓ pnpm $(pnpm --version) installed successfully" } install_opencode() { @@ -50,29 +71,17 @@ install_opencode() { if [ "$ARG_INSTALL_METHOD" = "curl" ]; then curl -fsSL https://opencode.ai/install | bash elif [ "$ARG_INSTALL_METHOD" = "npm" ]; then - # Configure npm to install to user directory to avoid permission issues - mkdir -p "$HOME/.local/bin" - npm config set prefix "$HOME/.local" - export PATH="$HOME/.local/bin:$PATH" - - # Persist PATH to shell profile - if ! grep -q 'PATH="$HOME/.local/bin:$PATH"' "$HOME/.bashrc" 2>/dev/null; then - echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$HOME/.bashrc" - fi - + # Use pnpm for faster, more efficient installation if [ "$ARG_OPENCODE_VERSION" = "latest" ]; then - npm install -g opencode-ai@latest + pnpm install -g opencode-ai@latest else - npm install -g "opencode-ai@${ARG_OPENCODE_VERSION}" + pnpm install -g "opencode-ai@${ARG_OPENCODE_VERSION}" fi else echo "ERROR: Unknown install method: $ARG_INSTALL_METHOD" exit 1 fi - # Reload shell to get opencode in PATH - export PATH="$HOME/.local/bin:$PATH" - if ! command_exists opencode; then echo "ERROR: Failed to install OpenCode" exit 1 @@ -193,7 +202,8 @@ configure_coder_integration() { fi } -validate_nodejs +install_nodejs +install_pnpm install_opencode check_github_authentication setup_opencode_configurations diff --git a/registry/rothnic/modules/opencode/scripts/start.sh b/registry/rothnic/modules/opencode/scripts/start.sh index 7003d0c38..f5a030b4a 100644 --- a/registry/rothnic/modules/opencode/scripts/start.sh +++ b/registry/rothnic/modules/opencode/scripts/start.sh @@ -2,7 +2,13 @@ set -euo pipefail source "$HOME"/.bashrc 2>/dev/null || true -export PATH="$HOME/.local/bin:$PATH" + +# Load NVM if available +export NVM_DIR="$HOME/.nvm" +[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" + +# Ensure pnpm is in PATH +export PATH="$HOME/.local/share/pnpm:$PATH" command_exists() { command -v "$1" > /dev/null 2>&1 From 46da014ded3fa0b3cd2acc95de786c2290591cd4 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 21 Nov 2025 15:14:04 +0000 Subject: [PATCH 24/42] fix: add wait loop for OpenCode installation before starting server The start script was attempting to launch OpenCode before the install script completed, causing timeouts. Now the start script waits up to 2 minutes for OpenCode to be installed, checking every 2 seconds and reloading NVM/PATH on each check. This gives the install script time to: - Install NVM (~5-10s) - Install Node.js LTS (~15-30s) - Install pnpm (~5-10s) - Install OpenCode via pnpm (~20-40s) Total expected install time: ~45-90 seconds, well within timeout. --- .../rothnic/modules/opencode/scripts/start.sh | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/registry/rothnic/modules/opencode/scripts/start.sh b/registry/rothnic/modules/opencode/scripts/start.sh index f5a030b4a..b8e1c3c6a 100644 --- a/registry/rothnic/modules/opencode/scripts/start.sh +++ b/registry/rothnic/modules/opencode/scripts/start.sh @@ -23,10 +23,36 @@ ARG_OPENCODE_PROVIDER=${ARG_OPENCODE_PROVIDER:-copilot} ARG_OPENCODE_AUTH_CONFIG=$(echo -n "${ARG_OPENCODE_AUTH_CONFIG:-}" | base64 -d 2> /dev/null || echo "") validate_opencode_installation() { - if ! command_exists opencode; then - echo "ERROR: OpenCode not installed." - exit 1 - fi + local max_wait=120 # Wait up to 2 minutes for installation to complete + local wait_interval=2 + local elapsed=0 + + echo "Waiting for OpenCode installation to complete..." + + while [ $elapsed -lt $max_wait ]; do + # Reload NVM and PATH in case installation just completed + export NVM_DIR="$HOME/.nvm" + [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" + export PATH="$HOME/.local/share/pnpm:$PATH" + + if command_exists opencode; then + echo "✓ OpenCode is ready: $(opencode --version 2>&1 | head -1)" + return 0 + fi + + if [ $elapsed -eq 0 ]; then + echo "Waiting for OpenCode to be installed (this may take a minute)..." + elif [ $((elapsed % 10)) -eq 0 ]; then + echo "Still waiting... (${elapsed}s elapsed)" + fi + + sleep $wait_interval + elapsed=$((elapsed + wait_interval)) + done + + echo "ERROR: OpenCode not installed after ${max_wait} seconds." + echo "The installation script may have failed. Check the install logs above." + exit 1 } build_initial_prompt() { From 5a809ce47edafe681f50f1c32936361f56ef6b81 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 21 Nov 2025 15:45:41 +0000 Subject: [PATCH 25/42] debug: add comprehensive validation and debug logging for startup Changes: - Renamed validate_opencode_installation to validate_environment - Now checks for both Node.js AND OpenCode before proceeding - Shows status of each component during wait loop - Added detailed environment debug output before starting agentapi - Shows which node, npm, pnpm, opencode paths and versions - Tests opencode --version directly before handing to agentapi - Better error messages showing final status if timeout occurs This will help diagnose why agentapi server is timing out at port 3284. The most likely issue is that Node.js isn't in PATH when opencode runs under agentapi, even though it's installed. --- .../rothnic/modules/opencode/scripts/start.sh | 51 ++++++++++++++++--- 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/registry/rothnic/modules/opencode/scripts/start.sh b/registry/rothnic/modules/opencode/scripts/start.sh index b8e1c3c6a..666d59bd1 100644 --- a/registry/rothnic/modules/opencode/scripts/start.sh +++ b/registry/rothnic/modules/opencode/scripts/start.sh @@ -22,12 +22,12 @@ ARG_RESUME_SESSION=${ARG_RESUME_SESSION:-true} ARG_OPENCODE_PROVIDER=${ARG_OPENCODE_PROVIDER:-copilot} ARG_OPENCODE_AUTH_CONFIG=$(echo -n "${ARG_OPENCODE_AUTH_CONFIG:-}" | base64 -d 2> /dev/null || echo "") -validate_opencode_installation() { +validate_environment() { local max_wait=120 # Wait up to 2 minutes for installation to complete local wait_interval=2 local elapsed=0 - echo "Waiting for OpenCode installation to complete..." + echo "Waiting for installation to complete..." while [ $elapsed -lt $max_wait ]; do # Reload NVM and PATH in case installation just completed @@ -35,23 +35,45 @@ validate_opencode_installation() { [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" export PATH="$HOME/.local/share/pnpm:$PATH" + # Check for both Node.js and OpenCode + local node_ready=false + local opencode_ready=false + + if command_exists node; then + node_ready=true + fi + if command_exists opencode; then + opencode_ready=true + fi + + if [ "$node_ready" = true ] && [ "$opencode_ready" = true ]; then + echo "✓ Node.js is ready: $(node --version)" echo "✓ OpenCode is ready: $(opencode --version 2>&1 | head -1)" + echo "✓ Environment validated successfully" return 0 fi if [ $elapsed -eq 0 ]; then - echo "Waiting for OpenCode to be installed (this may take a minute)..." + echo "Waiting for installation (this may take a minute)..." + echo " Node.js: $( [ "$node_ready" = true ] && echo "✓" || echo "⏳" )" + echo " OpenCode: $( [ "$opencode_ready" = true ] && echo "✓" || echo "⏳" )" elif [ $((elapsed % 10)) -eq 0 ]; then echo "Still waiting... (${elapsed}s elapsed)" + echo " Node.js: $( [ "$node_ready" = true ] && echo "✓" || echo "⏳" )" + echo " OpenCode: $( [ "$opencode_ready" = true ] && echo "✓" || echo "⏳" )" fi sleep $wait_interval elapsed=$((elapsed + wait_interval)) done - echo "ERROR: OpenCode not installed after ${max_wait} seconds." - echo "The installation script may have failed. Check the install logs above." + echo "ERROR: Installation did not complete after ${max_wait} seconds." + echo "Final status:" + echo " Node.js: $( command_exists node && echo "✓ $(node --version)" || echo "✗ Not found" )" + echo " OpenCode: $( command_exists opencode && echo "✓ Installed" || echo "✗ Not found" )" + echo "" + echo "Check the install logs above for errors." exit 1 } @@ -148,6 +170,23 @@ start_agentapi() { echo "Starting in directory: $ARG_WORKDIR" cd "$ARG_WORKDIR" + # Debug: Show environment + echo "=== Environment Debug ===" + echo "Node.js: $(which node) -> $(node --version 2>&1)" + echo "npm: $(which npm) -> $(npm --version 2>&1)" + echo "pnpm: $(which pnpm) -> $(pnpm --version 2>&1)" + echo "OpenCode: $(which opencode)" + echo "PATH: $PATH" + echo "NVM_DIR: $NVM_DIR" + echo "========================" + + # Test OpenCode directly first + echo "Testing OpenCode command..." + if ! opencode --version; then + echo "ERROR: OpenCode command failed" + exit 1 + fi + echo "Starting OpenCode TUI with agentapi..." local initial_prompt initial_prompt=$(build_initial_prompt) @@ -164,5 +203,5 @@ start_agentapi() { } setup_github_authentication -validate_opencode_installation +validate_environment start_agentapi From 8065bb548cfc4b084e45d896026e4a844de57f83 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 21 Nov 2025 17:09:06 +0000 Subject: [PATCH 26/42] fix: use wrapper script to preserve environment for agentapi subprocess The issue was that when agentapi spawns the opencode subprocess, it doesn't inherit the NVM environment variables set in the start script. Solution: Create a wrapper script (/tmp/opencode-wrapper-$$.sh) that: - Loads NVM before running opencode - Adds pnpm to PATH - Uses exec to replace itself with opencode The agentapi server now runs this wrapper instead of opencode directly, ensuring Node.js and pnpm are available in the subprocess environment. This is similar to how other modules handle environment setup for subprocesses spawned by agentapi. --- .../rothnic/modules/opencode/scripts/start.sh | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/registry/rothnic/modules/opencode/scripts/start.sh b/registry/rothnic/modules/opencode/scripts/start.sh index 666d59bd1..741f6a305 100644 --- a/registry/rothnic/modules/opencode/scripts/start.sh +++ b/registry/rothnic/modules/opencode/scripts/start.sh @@ -187,18 +187,34 @@ start_agentapi() { exit 1 fi + # Create a wrapper script that ensures environment is loaded when opencode runs + # This is needed because agentapi spawns a subprocess that doesn't inherit our environment + local wrapper_script="/tmp/opencode-wrapper-$$.sh" + cat > "$wrapper_script" <<'WRAPPER_EOF' +#!/bin/bash +# Load NVM +export NVM_DIR="$HOME/.nvm" +[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" + +# Add pnpm to PATH +export PATH="$HOME/.local/share/pnpm:$PATH" + +# Run opencode with all arguments +exec opencode "$@" +WRAPPER_EOF + chmod +x "$wrapper_script" + echo "Starting OpenCode TUI with agentapi..." local initial_prompt initial_prompt=$(build_initial_prompt) - # Run opencode TUI with working directory as project path - # Agentapi sends plain text to stdin/stdout, not JSON-RPC - # OpenCode's TUI mode reads plain text input, similar to copilot/goose + # Use the wrapper script instead of calling opencode directly + # This ensures NVM and pnpm are in PATH when the subprocess runs if [ -n "$initial_prompt" ]; then echo "Using initial prompt with system context" - agentapi server -I="$initial_prompt" --type=opencode --term-width 67 --term-height 1190 -- opencode "$ARG_WORKDIR" + agentapi server -I="$initial_prompt" --type=opencode --term-width 67 --term-height 1190 -- "$wrapper_script" "$ARG_WORKDIR" else - agentapi server --type=opencode --term-width 67 --term-height 1190 -- opencode "$ARG_WORKDIR" + agentapi server --type=opencode --term-width 67 --term-height 1190 -- "$wrapper_script" "$ARG_WORKDIR" fi } From e5d08ddd5aac7e6ef0d621e2da5fecbf0f2b39a6 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 21 Nov 2025 20:24:09 +0000 Subject: [PATCH 27/42] refactor: follow codex module pattern for install/start separation Changes to install.sh: - Added final verification section that runs node --version, npm --version, pnpm --version, and opencode --version - This ensures the script doesn't exit until installation is verified Changes to start.sh: - Removed wait loop - install script should complete first - Simplified to match codex pattern: source NVM, verify, start - Quick verification with opencode --version at startup - Kept wrapper script for subprocess environment inheritance The codex module pattern: 1. install.sh does ALL installation and verifies at the end 2. start.sh just sources environment and starts immediately If start.sh can't find opencode, it means install.sh failed. --- .../modules/opencode/scripts/install.sh | 8 ++ .../rothnic/modules/opencode/scripts/start.sh | 110 ++++-------------- 2 files changed, 28 insertions(+), 90 deletions(-) diff --git a/registry/rothnic/modules/opencode/scripts/install.sh b/registry/rothnic/modules/opencode/scripts/install.sh index 87d106459..1bdc00539 100644 --- a/registry/rothnic/modules/opencode/scripts/install.sh +++ b/registry/rothnic/modules/opencode/scripts/install.sh @@ -210,4 +210,12 @@ setup_opencode_configurations configure_github_copilot_provider configure_coder_integration +# Final verification - like codex does +echo "=== Final Verification ===" +echo "Node.js: $(node --version)" +echo "npm: $(npm --version)" +echo "pnpm: $(pnpm --version)" +opencode --version +echo "===========================" + echo "OpenCode module setup completed." diff --git a/registry/rothnic/modules/opencode/scripts/start.sh b/registry/rothnic/modules/opencode/scripts/start.sh index 741f6a305..10b265fda 100644 --- a/registry/rothnic/modules/opencode/scripts/start.sh +++ b/registry/rothnic/modules/opencode/scripts/start.sh @@ -1,19 +1,26 @@ #!/bin/bash -set -euo pipefail +set -o errexit +set -o pipefail source "$HOME"/.bashrc 2>/dev/null || true -# Load NVM if available -export NVM_DIR="$HOME/.nvm" -[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" - -# Ensure pnpm is in PATH -export PATH="$HOME/.local/share/pnpm:$PATH" - command_exists() { command -v "$1" > /dev/null 2>&1 } +# Load NVM if available (like codex does) +if [ -f "$HOME/.nvm/nvm.sh" ]; then + source "$HOME/.nvm/nvm.sh" +else + export PATH="$HOME/.npm-global/bin:$PATH" +fi + +# Add pnpm to PATH +export PATH="$HOME/.local/share/pnpm:$PATH" + +# Quick verification - if this fails, install didn't complete +printf "OpenCode version: %s\n" "$(opencode --version 2>&1 | head -1)" + ARG_WORKDIR=${ARG_WORKDIR:-"$HOME"} ARG_AI_PROMPT=$(echo -n "${ARG_AI_PROMPT:-}" | base64 -d 2> /dev/null || echo "") ARG_SYSTEM_PROMPT=$(echo -n "${ARG_SYSTEM_PROMPT:-}" | base64 -d 2> /dev/null || echo "") @@ -22,61 +29,6 @@ ARG_RESUME_SESSION=${ARG_RESUME_SESSION:-true} ARG_OPENCODE_PROVIDER=${ARG_OPENCODE_PROVIDER:-copilot} ARG_OPENCODE_AUTH_CONFIG=$(echo -n "${ARG_OPENCODE_AUTH_CONFIG:-}" | base64 -d 2> /dev/null || echo "") -validate_environment() { - local max_wait=120 # Wait up to 2 minutes for installation to complete - local wait_interval=2 - local elapsed=0 - - echo "Waiting for installation to complete..." - - while [ $elapsed -lt $max_wait ]; do - # Reload NVM and PATH in case installation just completed - export NVM_DIR="$HOME/.nvm" - [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" - export PATH="$HOME/.local/share/pnpm:$PATH" - - # Check for both Node.js and OpenCode - local node_ready=false - local opencode_ready=false - - if command_exists node; then - node_ready=true - fi - - if command_exists opencode; then - opencode_ready=true - fi - - if [ "$node_ready" = true ] && [ "$opencode_ready" = true ]; then - echo "✓ Node.js is ready: $(node --version)" - echo "✓ OpenCode is ready: $(opencode --version 2>&1 | head -1)" - echo "✓ Environment validated successfully" - return 0 - fi - - if [ $elapsed -eq 0 ]; then - echo "Waiting for installation (this may take a minute)..." - echo " Node.js: $( [ "$node_ready" = true ] && echo "✓" || echo "⏳" )" - echo " OpenCode: $( [ "$opencode_ready" = true ] && echo "✓" || echo "⏳" )" - elif [ $((elapsed % 10)) -eq 0 ]; then - echo "Still waiting... (${elapsed}s elapsed)" - echo " Node.js: $( [ "$node_ready" = true ] && echo "✓" || echo "⏳" )" - echo " OpenCode: $( [ "$opencode_ready" = true ] && echo "✓" || echo "⏳" )" - fi - - sleep $wait_interval - elapsed=$((elapsed + wait_interval)) - done - - echo "ERROR: Installation did not complete after ${max_wait} seconds." - echo "Final status:" - echo " Node.js: $( command_exists node && echo "✓ $(node --version)" || echo "✗ Not found" )" - echo " OpenCode: $( command_exists opencode && echo "✓ Installed" || echo "✗ Not found" )" - echo "" - echo "Check the install logs above for errors." - exit 1 -} - build_initial_prompt() { local initial_prompt="" @@ -93,13 +45,6 @@ $ARG_AI_PROMPT" echo "$initial_prompt" } -build_opencode_args() { - OPENCODE_ARGS=() - - # ACP mode doesn't accept --provider argument - # Provider is configured via auth.json during installation -} - setup_github_authentication() { export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}" echo "Setting up GitHub authentication..." @@ -170,25 +115,12 @@ start_agentapi() { echo "Starting in directory: $ARG_WORKDIR" cd "$ARG_WORKDIR" - # Debug: Show environment - echo "=== Environment Debug ===" + echo "=== Environment ===" echo "Node.js: $(which node) -> $(node --version 2>&1)" - echo "npm: $(which npm) -> $(npm --version 2>&1)" - echo "pnpm: $(which pnpm) -> $(pnpm --version 2>&1)" echo "OpenCode: $(which opencode)" - echo "PATH: $PATH" - echo "NVM_DIR: $NVM_DIR" - echo "========================" - - # Test OpenCode directly first - echo "Testing OpenCode command..." - if ! opencode --version; then - echo "ERROR: OpenCode command failed" - exit 1 - fi + echo "===================" - # Create a wrapper script that ensures environment is loaded when opencode runs - # This is needed because agentapi spawns a subprocess that doesn't inherit our environment + # Create wrapper script to ensure environment is loaded when agentapi spawns opencode local wrapper_script="/tmp/opencode-wrapper-$$.sh" cat > "$wrapper_script" <<'WRAPPER_EOF' #!/bin/bash @@ -202,14 +134,13 @@ export PATH="$HOME/.local/share/pnpm:$PATH" # Run opencode with all arguments exec opencode "$@" WRAPPER_EOF - chmod +x "$wrapper_script" + chmod +x "$wrapper_script" || true echo "Starting OpenCode TUI with agentapi..." local initial_prompt initial_prompt=$(build_initial_prompt) - # Use the wrapper script instead of calling opencode directly - # This ensures NVM and pnpm are in PATH when the subprocess runs + # Use wrapper script to ensure environment is available in subprocess if [ -n "$initial_prompt" ]; then echo "Using initial prompt with system context" agentapi server -I="$initial_prompt" --type=opencode --term-width 67 --term-height 1190 -- "$wrapper_script" "$ARG_WORKDIR" @@ -219,5 +150,4 @@ WRAPPER_EOF } setup_github_authentication -validate_environment start_agentapi From e778aef02ca4ff54f40536b89232d6892872090c Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 21 Nov 2025 20:35:22 +0000 Subject: [PATCH 28/42] fix: use npm instead of pnpm, follow codex pattern exactly Changes: - Removed pnpm installation - using npm like codex does - Added setup_npm_global() to configure npm prefix to ~/.npm-global - Persist PATH to ~/.bashrc for subprocess discovery - Removed wrapper script from start.sh - run opencode directly - Updated README to remove pnpm references The codex module uses npm with a custom prefix, persists PATH to bashrc, then agentapi can find the binary in subprocesses. Following this exact pattern for reliability. --- registry/rothnic/modules/opencode/README.md | 5 +- .../modules/opencode/scripts/install.sh | 71 ++++++++++--------- .../rothnic/modules/opencode/scripts/start.sh | 33 ++------- 3 files changed, 43 insertions(+), 66 deletions(-) diff --git a/registry/rothnic/modules/opencode/README.md b/registry/rothnic/modules/opencode/README.md index 49478f654..8d60990ba 100644 --- a/registry/rothnic/modules/opencode/README.md +++ b/registry/rothnic/modules/opencode/README.md @@ -22,7 +22,7 @@ module "opencode" { } ``` -Node.js LTS is automatically installed via NVM when using the `npm` install method (default). The module also installs pnpm for faster package management. +Node.js LTS is automatically installed via NVM when using the `npm` install method (default). ## Authentication (Optional) @@ -82,17 +82,14 @@ See [variables](./main.tf) for complete list. - 💾 Session persistence - 🔐 Flexible authentication - 🚀 Automatic Node.js installation via NVM -- ⚡ Fast package management with pnpm ## Prerequisites - **Node.js 18+**: Automatically installed via NVM (npm install method only) -- **pnpm**: Automatically installed for faster package management ## Notes - Node.js LTS is automatically installed via NVM (if using npm install method) -- Uses pnpm for faster, more efficient package installation - TUI may have limitations through AgentAPI (slash commands, menus) - For production, use `subdomain = true` with [wildcard access URL](https://coder.com/docs/admin/setup#wildcard-access-url) diff --git a/registry/rothnic/modules/opencode/scripts/install.sh b/registry/rothnic/modules/opencode/scripts/install.sh index 1bdc00539..e9ebb94ff 100644 --- a/registry/rothnic/modules/opencode/scripts/install.sh +++ b/registry/rothnic/modules/opencode/scripts/install.sh @@ -48,49 +48,54 @@ install_nodejs() { echo "✓ Node.js $(node --version) installed successfully" } -install_pnpm() { +setup_npm_global() { if [ "$ARG_INSTALL_METHOD" != "npm" ]; then return 0 fi - if command_exists pnpm; then - echo "✓ pnpm $(pnpm --version) already installed" - return 0 + # Follow codex pattern: configure npm to use user directory + # This ensures global packages are accessible without sudo + if ! command_exists nvm; then + echo "Setting up npm global directory (non-NVM setup)..." + mkdir -p "$HOME/.npm-global/bin" + npm config set prefix "$HOME/.npm-global" + export PATH="$HOME/.npm-global/bin:$PATH" + + # Persist to bashrc like codex does + if ! grep -q 'export PATH="$HOME/.npm-global/bin:$PATH"' ~/.bashrc 2>/dev/null; then + echo 'export PATH="$HOME/.npm-global/bin:$PATH"' >> ~/.bashrc + fi fi - - echo "Installing pnpm..." - npm install -g pnpm - - echo "✓ pnpm $(pnpm --version) installed successfully" } install_opencode() { - if ! command_exists opencode; then - echo "Installing OpenCode (version: ${ARG_OPENCODE_VERSION}, method: ${ARG_INSTALL_METHOD})..." - - if [ "$ARG_INSTALL_METHOD" = "curl" ]; then - curl -fsSL https://opencode.ai/install | bash - elif [ "$ARG_INSTALL_METHOD" = "npm" ]; then - # Use pnpm for faster, more efficient installation - if [ "$ARG_OPENCODE_VERSION" = "latest" ]; then - pnpm install -g opencode-ai@latest - else - pnpm install -g "opencode-ai@${ARG_OPENCODE_VERSION}" - fi - else - echo "ERROR: Unknown install method: $ARG_INSTALL_METHOD" - exit 1 - fi + if command_exists opencode; then + echo "✓ OpenCode already installed: $(opencode --version 2>&1 | head -1)" + return 0 + fi - if ! command_exists opencode; then - echo "ERROR: Failed to install OpenCode" - exit 1 - fi + echo "Installing OpenCode (version: ${ARG_OPENCODE_VERSION}, method: ${ARG_INSTALL_METHOD})..." - echo "✓ OpenCode installed successfully: $(opencode --version 2>&1 | head -1)" + if [ "$ARG_INSTALL_METHOD" = "curl" ]; then + curl -fsSL https://opencode.ai/install | bash + elif [ "$ARG_INSTALL_METHOD" = "npm" ]; then + # Use npm like codex does (not pnpm) - simpler and PATH is already configured + if [ "$ARG_OPENCODE_VERSION" = "latest" ]; then + npm install -g opencode-ai@latest + else + npm install -g "opencode-ai@${ARG_OPENCODE_VERSION}" + fi else - echo "✓ OpenCode already installed: $(opencode --version 2>&1 | head -1)" + echo "ERROR: Unknown install method: $ARG_INSTALL_METHOD" + exit 1 fi + + if ! command_exists opencode; then + echo "ERROR: Failed to install OpenCode" + exit 1 + fi + + echo "✓ OpenCode installed successfully: $(opencode --version 2>&1 | head -1)" } check_github_authentication() { @@ -203,7 +208,7 @@ configure_coder_integration() { } install_nodejs -install_pnpm +setup_npm_global install_opencode check_github_authentication setup_opencode_configurations @@ -214,7 +219,7 @@ configure_coder_integration echo "=== Final Verification ===" echo "Node.js: $(node --version)" echo "npm: $(npm --version)" -echo "pnpm: $(pnpm --version)" +echo "which opencode: $(which opencode)" opencode --version echo "===========================" diff --git a/registry/rothnic/modules/opencode/scripts/start.sh b/registry/rothnic/modules/opencode/scripts/start.sh index 10b265fda..201e0b4c9 100644 --- a/registry/rothnic/modules/opencode/scripts/start.sh +++ b/registry/rothnic/modules/opencode/scripts/start.sh @@ -8,16 +8,13 @@ command_exists() { command -v "$1" > /dev/null 2>&1 } -# Load NVM if available (like codex does) +# Load NVM if available, otherwise use npm-global (like codex does) if [ -f "$HOME/.nvm/nvm.sh" ]; then source "$HOME/.nvm/nvm.sh" else export PATH="$HOME/.npm-global/bin:$PATH" fi -# Add pnpm to PATH -export PATH="$HOME/.local/share/pnpm:$PATH" - # Quick verification - if this fails, install didn't complete printf "OpenCode version: %s\n" "$(opencode --version 2>&1 | head -1)" @@ -74,8 +71,6 @@ setup_github_authentication() { fi if [ -n "$github_token" ] && [ "$github_token" != "null" ]; then - # Note: This creates a simplified auth.json that may not work with GitHub Copilot - # For full GitHub Copilot support, use the opencode_auth_config variable echo "⚠ Warning: Using simplified auth format - GitHub Copilot may require device flow auth" cat > "$auth_file" < "$wrapper_script" <<'WRAPPER_EOF' -#!/bin/bash -# Load NVM -export NVM_DIR="$HOME/.nvm" -[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" - -# Add pnpm to PATH -export PATH="$HOME/.local/share/pnpm:$PATH" - -# Run opencode with all arguments -exec opencode "$@" -WRAPPER_EOF - chmod +x "$wrapper_script" || true - echo "Starting OpenCode TUI with agentapi..." local initial_prompt initial_prompt=$(build_initial_prompt) - # Use wrapper script to ensure environment is available in subprocess + # Run opencode directly like codex does (no wrapper script) if [ -n "$initial_prompt" ]; then echo "Using initial prompt with system context" - agentapi server -I="$initial_prompt" --type=opencode --term-width 67 --term-height 1190 -- "$wrapper_script" "$ARG_WORKDIR" + agentapi server -I="$initial_prompt" --type=opencode --term-width 67 --term-height 1190 -- opencode "$ARG_WORKDIR" else - agentapi server --type=opencode --term-width 67 --term-height 1190 -- "$wrapper_script" "$ARG_WORKDIR" + agentapi server --type=opencode --term-width 67 --term-height 1190 -- opencode "$ARG_WORKDIR" fi } From 0f83fc9a243e95f0e962cdd5c882a8f1608989b4 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 21 Nov 2025 20:41:48 +0000 Subject: [PATCH 29/42] fix: use NVM for Node.js but install opencode to ~/.local/bin The key insight from the working commit (5d8541d): - npm prefix is set to ~/.local, so opencode goes to ~/.local/bin - start.sh just exports PATH="$HOME/.local/bin:$PATH" - no NVM loading! - agentapi can find opencode because PATH is simple and inherited What was wrong with previous NVM approach: - NVM puts npm packages in ~/.nvm/versions/node//bin/ - This requires sourcing NVM to find the binary - agentapi subprocess doesn't source NVM, so it can't find opencode The fix: - install.sh: Use NVM to install Node.js (avoids apt-get issues) - install.sh: Configure npm prefix to ~/.local (not NVM default) - install.sh: Add ~/.local/bin to PATH and persist to bashrc - start.sh: Just export PATH="$HOME/.local/bin:$PATH" (like working version) Now opencode binary is in a simple, predictable location that works in subprocess environments without NVM. --- .../modules/opencode/scripts/install.sh | 50 +++++++++++-------- .../rothnic/modules/opencode/scripts/start.sh | 44 +++++++++------- 2 files changed, 55 insertions(+), 39 deletions(-) diff --git a/registry/rothnic/modules/opencode/scripts/install.sh b/registry/rothnic/modules/opencode/scripts/install.sh index e9ebb94ff..76ea4d61d 100644 --- a/registry/rothnic/modules/opencode/scripts/install.sh +++ b/registry/rothnic/modules/opencode/scripts/install.sh @@ -1,7 +1,7 @@ #!/bin/bash set -euo pipefail -source "$HOME"/.bashrc +source "$HOME"/.bashrc 2>/dev/null || true command_exists() { command -v "$1" > /dev/null 2>&1 @@ -21,9 +21,14 @@ install_nodejs() { return 0 fi + # Check if node already exists and is adequate version if command_exists node; then - echo "✓ Node.js $(node --version) already installed" - return 0 + node_version=$(node --version | sed 's/v//' | cut -d. -f1) + if [ "$node_version" -ge 18 ]; then + echo "✓ Node.js $(node --version) already installed" + return 0 + fi + echo "Node.js v$node_version found but v18+ required, installing newer version..." fi echo "Installing Node.js via NVM..." @@ -48,27 +53,32 @@ install_nodejs() { echo "✓ Node.js $(node --version) installed successfully" } -setup_npm_global() { +setup_npm_prefix() { if [ "$ARG_INSTALL_METHOD" != "npm" ]; then return 0 fi - # Follow codex pattern: configure npm to use user directory - # This ensures global packages are accessible without sudo - if ! command_exists nvm; then - echo "Setting up npm global directory (non-NVM setup)..." - mkdir -p "$HOME/.npm-global/bin" - npm config set prefix "$HOME/.npm-global" - export PATH="$HOME/.npm-global/bin:$PATH" - - # Persist to bashrc like codex does - if ! grep -q 'export PATH="$HOME/.npm-global/bin:$PATH"' ~/.bashrc 2>/dev/null; then - echo 'export PATH="$HOME/.npm-global/bin:$PATH"' >> ~/.bashrc - fi + # Configure npm to install global packages to ~/.local/bin + # This is the KEY difference from broken NVM approach: + # - NVM puts npm packages in ~/.nvm/versions/node//bin/ (requires sourcing NVM) + # - This puts them in ~/.local/bin (simple PATH, works in subprocesses) + echo "Configuring npm prefix to ~/.local..." + mkdir -p "$HOME/.local/bin" + npm config set prefix "$HOME/.local" + export PATH="$HOME/.local/bin:$PATH" + + # Persist to bashrc (like working apt-get version did) + if ! grep -q 'PATH="$HOME/.local/bin:$PATH"' "$HOME/.bashrc" 2>/dev/null; then + echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$HOME/.bashrc" fi + + echo "✓ npm prefix configured to ~/.local" } install_opencode() { + # Make sure PATH is set before checking + export PATH="$HOME/.local/bin:$PATH" + if command_exists opencode; then echo "✓ OpenCode already installed: $(opencode --version 2>&1 | head -1)" return 0 @@ -79,7 +89,6 @@ install_opencode() { if [ "$ARG_INSTALL_METHOD" = "curl" ]; then curl -fsSL https://opencode.ai/install | bash elif [ "$ARG_INSTALL_METHOD" = "npm" ]; then - # Use npm like codex does (not pnpm) - simpler and PATH is already configured if [ "$ARG_OPENCODE_VERSION" = "latest" ]; then npm install -g opencode-ai@latest else @@ -167,7 +176,6 @@ configure_github_copilot_provider() { if [ -n "$github_token" ] && [ "$github_token" != "null" ]; then echo "✓ GitHub token available - configuring Copilot provider in auth.json" - # Create auth.json with GitHub Copilot credentials cat > "$auth_file" < "$auth_file" fi @@ -207,15 +214,16 @@ configure_coder_integration() { fi } +# Main execution install_nodejs -setup_npm_global +setup_npm_prefix install_opencode check_github_authentication setup_opencode_configurations configure_github_copilot_provider configure_coder_integration -# Final verification - like codex does +# Final verification echo "=== Final Verification ===" echo "Node.js: $(node --version)" echo "npm: $(npm --version)" diff --git a/registry/rothnic/modules/opencode/scripts/start.sh b/registry/rothnic/modules/opencode/scripts/start.sh index 201e0b4c9..7003d0c38 100644 --- a/registry/rothnic/modules/opencode/scripts/start.sh +++ b/registry/rothnic/modules/opencode/scripts/start.sh @@ -1,23 +1,13 @@ #!/bin/bash -set -o errexit -set -o pipefail +set -euo pipefail source "$HOME"/.bashrc 2>/dev/null || true +export PATH="$HOME/.local/bin:$PATH" command_exists() { command -v "$1" > /dev/null 2>&1 } -# Load NVM if available, otherwise use npm-global (like codex does) -if [ -f "$HOME/.nvm/nvm.sh" ]; then - source "$HOME/.nvm/nvm.sh" -else - export PATH="$HOME/.npm-global/bin:$PATH" -fi - -# Quick verification - if this fails, install didn't complete -printf "OpenCode version: %s\n" "$(opencode --version 2>&1 | head -1)" - ARG_WORKDIR=${ARG_WORKDIR:-"$HOME"} ARG_AI_PROMPT=$(echo -n "${ARG_AI_PROMPT:-}" | base64 -d 2> /dev/null || echo "") ARG_SYSTEM_PROMPT=$(echo -n "${ARG_SYSTEM_PROMPT:-}" | base64 -d 2> /dev/null || echo "") @@ -26,6 +16,13 @@ ARG_RESUME_SESSION=${ARG_RESUME_SESSION:-true} ARG_OPENCODE_PROVIDER=${ARG_OPENCODE_PROVIDER:-copilot} ARG_OPENCODE_AUTH_CONFIG=$(echo -n "${ARG_OPENCODE_AUTH_CONFIG:-}" | base64 -d 2> /dev/null || echo "") +validate_opencode_installation() { + if ! command_exists opencode; then + echo "ERROR: OpenCode not installed." + exit 1 + fi +} + build_initial_prompt() { local initial_prompt="" @@ -42,6 +39,13 @@ $ARG_AI_PROMPT" echo "$initial_prompt" } +build_opencode_args() { + OPENCODE_ARGS=() + + # ACP mode doesn't accept --provider argument + # Provider is configured via auth.json during installation +} + setup_github_authentication() { export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}" echo "Setting up GitHub authentication..." @@ -71,6 +75,8 @@ setup_github_authentication() { fi if [ -n "$github_token" ] && [ "$github_token" != "null" ]; then + # Note: This creates a simplified auth.json that may not work with GitHub Copilot + # For full GitHub Copilot support, use the opencode_auth_config variable echo "⚠ Warning: Using simplified auth format - GitHub Copilot may require device flow auth" cat > "$auth_file" < $(node --version 2>&1)" - echo "OpenCode: $(which opencode)" - echo "===================" - echo "Starting OpenCode TUI with agentapi..." local initial_prompt initial_prompt=$(build_initial_prompt) - # Run opencode directly like codex does (no wrapper script) + # Run opencode TUI with working directory as project path + # Agentapi sends plain text to stdin/stdout, not JSON-RPC + # OpenCode's TUI mode reads plain text input, similar to copilot/goose if [ -n "$initial_prompt" ]; then echo "Using initial prompt with system context" agentapi server -I="$initial_prompt" --type=opencode --term-width 67 --term-height 1190 -- opencode "$ARG_WORKDIR" @@ -125,4 +132,5 @@ start_agentapi() { } setup_github_authentication +validate_opencode_installation start_agentapi From b870304d4ccec661ec0d56f00bbd83839b39b445 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 21 Nov 2025 20:56:32 +0000 Subject: [PATCH 30/42] fix: background agentapi server and match codex pattern exactly Key changes based on ChatGPT analysis of logs: 1. Background agentapi with & (like codex does) - Codex: agentapi server ... -- codex "${CODEX_ARGS[@]}" & - Now: agentapi server ... -- opencode "$ARG_WORKDIR" & - This allows the start script to complete, which the agentapi module's wrapper expects 2. Use /tmp/start.sh directly instead of bash /tmp/start.sh - Matches codex pattern exactly - Removed "|| true" from chmod to fail fast if there's an issue 3. Made NVM installation more robust - Verify nvm.sh exists before sourcing - Verify nvm command available after sourcing - Verify node command available after nvm install - Better error messages if any step fails The ChatGPT analysis identified that the agentapi module's wrapper scripts expect the start script to return quickly so it can run its own wait-for-start loop AFTER the server begins starting. --- registry/rothnic/modules/opencode/main.tf | 12 +++++----- .../modules/opencode/scripts/install.sh | 23 ++++++++++++++++--- .../rothnic/modules/opencode/scripts/start.sh | 5 ++-- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/registry/rothnic/modules/opencode/main.tf b/registry/rothnic/modules/opencode/main.tf index b9e068541..e862b5457 100644 --- a/registry/rothnic/modules/opencode/main.tf +++ b/registry/rothnic/modules/opencode/main.tf @@ -230,9 +230,9 @@ module "agentapi" { #!/bin/bash set -o errexit set -o pipefail - echo -n '${base64encode(local.start_script)}' | base64 -d > /tmp/start.sh - chmod +x /tmp/start.sh || true + echo -n '${base64encode(local.start_script)}' | base64 -d > /tmp/start.sh + chmod +x /tmp/start.sh ARG_WORKDIR='${local.workdir}' \ ARG_AI_PROMPT='${base64encode(var.ai_prompt)}' \ ARG_SYSTEM_PROMPT='${base64encode(local.final_system_prompt)}' \ @@ -240,16 +240,16 @@ module "agentapi" { ARG_EXTERNAL_AUTH_ID='${var.external_auth_id}' \ ARG_RESUME_SESSION='${var.resume_session}' \ ARG_OPENCODE_AUTH_CONFIG='${var.opencode_auth_config != "" ? base64encode(var.opencode_auth_config) : ""}' \ - bash /tmp/start.sh + /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 || true + echo -n '${base64encode(local.install_script)}' | base64 -d > /tmp/install.sh + chmod +x /tmp/install.sh ARG_MCP_APP_STATUS_SLUG='${local.app_slug}' \ ARG_REPORT_TASKS='${var.report_tasks}' \ ARG_WORKDIR='${local.workdir}' \ @@ -257,6 +257,6 @@ module "agentapi" { ARG_EXTERNAL_AUTH_ID='${var.external_auth_id}' \ ARG_OPENCODE_VERSION='${var.opencode_version}' \ ARG_INSTALL_METHOD='${var.install_method}' \ - bash /tmp/install.sh + /tmp/install.sh EOT } diff --git a/registry/rothnic/modules/opencode/scripts/install.sh b/registry/rothnic/modules/opencode/scripts/install.sh index 76ea4d61d..f7a828728 100644 --- a/registry/rothnic/modules/opencode/scripts/install.sh +++ b/registry/rothnic/modules/opencode/scripts/install.sh @@ -40,9 +40,20 @@ install_nodejs() { curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash fi - # Load NVM - [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" - [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" + # Load NVM - must happen AFTER installation + # shellcheck source=/dev/null + if [ -s "$NVM_DIR/nvm.sh" ]; then + \. "$NVM_DIR/nvm.sh" + else + echo "ERROR: NVM installation failed - nvm.sh not found" + exit 1 + fi + + # Verify nvm command is available + if ! command_exists nvm; then + echo "ERROR: nvm command not found after sourcing nvm.sh" + exit 1 + fi # Install Node.js LTS echo "Installing Node.js LTS..." @@ -50,6 +61,12 @@ install_nodejs() { nvm alias default node nvm use default + # Verify node is now available + if ! command_exists node; then + echo "ERROR: node command not found after nvm install" + exit 1 + fi + echo "✓ Node.js $(node --version) installed successfully" } diff --git a/registry/rothnic/modules/opencode/scripts/start.sh b/registry/rothnic/modules/opencode/scripts/start.sh index 7003d0c38..c4c02da1d 100644 --- a/registry/rothnic/modules/opencode/scripts/start.sh +++ b/registry/rothnic/modules/opencode/scripts/start.sh @@ -123,11 +123,12 @@ start_agentapi() { # Run opencode TUI with working directory as project path # Agentapi sends plain text to stdin/stdout, not JSON-RPC # OpenCode's TUI mode reads plain text input, similar to copilot/goose + # Background with & like codex does if [ -n "$initial_prompt" ]; then echo "Using initial prompt with system context" - agentapi server -I="$initial_prompt" --type=opencode --term-width 67 --term-height 1190 -- opencode "$ARG_WORKDIR" + agentapi server -I="$initial_prompt" --type=opencode --term-width 67 --term-height 1190 -- opencode "$ARG_WORKDIR" & else - agentapi server --type=opencode --term-width 67 --term-height 1190 -- opencode "$ARG_WORKDIR" + agentapi server --type=opencode --term-width 67 --term-height 1190 -- opencode "$ARG_WORKDIR" & fi } From a1009fcfe775e0d179918be86e0b04e193431186 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 21 Nov 2025 21:44:53 +0000 Subject: [PATCH 31/42] refactor: switch to curl installer, remove all Node.js/NVM complexity Major simplification based on ChatGPT guidance: 1. Use official curl installer only - `curl -fsSL https://opencode.ai/install | bash` - OpenCode is now a Go CLI that handles its own runtime - No Node.js, npm, NVM, or apt-get needed 2. Simplified install.sh - Single install method: curl - Installs to ~/.local/bin with XDG_BIN_DIR - Persists PATH to bashrc - No auth.json fabrication (handled by start.sh) 3. Simplified start.sh - Uses pre-configured auth.json verbatim if provided - Does NOT try to synthesize auth.json from OAuth tokens - Clear warnings if no auth.json present - Backgrounds agentapi server with & so wrapper can run wait loop 4. Updated main.tf - Default install_method changed to "curl" - Removed validation (only curl supported now) 5. Updated README - Reflects curl-based installation - Notes that version pinning is not supported - Removed Node.js prerequisites Trade-off: Cannot pin OpenCode version (always installs latest). If version pinning is needed later, can add npm path back. --- registry/rothnic/modules/opencode/README.md | 9 +- registry/rothnic/modules/opencode/main.tf | 8 +- .../modules/opencode/scripts/install.sh | 204 +++++------------- .../rothnic/modules/opencode/scripts/start.sh | 89 +++----- 4 files changed, 88 insertions(+), 222 deletions(-) diff --git a/registry/rothnic/modules/opencode/README.md b/registry/rothnic/modules/opencode/README.md index 8d60990ba..8efd90f9e 100644 --- a/registry/rothnic/modules/opencode/README.md +++ b/registry/rothnic/modules/opencode/README.md @@ -22,7 +22,7 @@ module "opencode" { } ``` -Node.js LTS is automatically installed via NVM when using the `npm` install method (default). +OpenCode is automatically installed via the official curl installer (no Node.js required). ## Authentication (Optional) @@ -81,15 +81,16 @@ See [variables](./main.tf) for complete list. - 📊 Task reporting to Coder UI - 💾 Session persistence - 🔐 Flexible authentication -- 🚀 Automatic Node.js installation via NVM +- 🚀 Simple curl-based installation (no Node.js required) ## Prerequisites -- **Node.js 18+**: Automatically installed via NVM (npm install method only) +- None - OpenCode is installed via the official curl installer which handles its own runtime ## Notes -- Node.js LTS is automatically installed via NVM (if using npm install method) +- OpenCode is installed via `curl -fsSL https://opencode.ai/install | bash` +- Version pinning is not supported with curl installer (always installs latest) - TUI may have limitations through AgentAPI (slash commands, menus) - For production, use `subdomain = true` with [wildcard access URL](https://coder.com/docs/admin/setup#wildcard-access-url) diff --git a/registry/rothnic/modules/opencode/main.tf b/registry/rothnic/modules/opencode/main.tf index e862b5457..a55a1c010 100644 --- a/registry/rothnic/modules/opencode/main.tf +++ b/registry/rothnic/modules/opencode/main.tf @@ -82,12 +82,8 @@ variable "opencode_version" { variable "install_method" { type = string - description = "Installation method for OpenCode: 'npm' (default) or 'curl'." - default = "npm" - validation { - condition = contains(["npm", "curl"], var.install_method) - error_message = "install_method must be either 'npm' or 'curl'." - } + description = "Installation method for OpenCode. Currently only 'curl' is used (installs latest)." + default = "curl" } variable "report_tasks" { diff --git a/registry/rothnic/modules/opencode/scripts/install.sh b/registry/rothnic/modules/opencode/scripts/install.sh index f7a828728..2a33ef077 100644 --- a/registry/rothnic/modules/opencode/scripts/install.sh +++ b/registry/rothnic/modules/opencode/scripts/install.sh @@ -1,155 +1,95 @@ #!/bin/bash set -euo pipefail +# Load any existing PATH customizations source "$HOME"/.bashrc 2>/dev/null || true command_exists() { command -v "$1" > /dev/null 2>&1 } +# ---------- ARGUMENTS / CONFIG ---------- + ARG_WORKDIR=${ARG_WORKDIR:-"$HOME"} ARG_REPORT_TASKS=${ARG_REPORT_TASKS:-true} ARG_MCP_APP_STATUS_SLUG=${ARG_MCP_APP_STATUS_SLUG:-} ARG_OPENCODE_CONFIG=$(echo -n "${ARG_OPENCODE_CONFIG:-}" | base64 -d 2> /dev/null || echo "") ARG_EXTERNAL_AUTH_ID=${ARG_EXTERNAL_AUTH_ID:-github} +# Kept for future use, but NOT used in curl mode: ARG_OPENCODE_VERSION=${ARG_OPENCODE_VERSION:-latest} -ARG_INSTALL_METHOD=${ARG_INSTALL_METHOD:-npm} - -install_nodejs() { - if [ "$ARG_INSTALL_METHOD" != "npm" ]; then - # curl install method doesn't need Node.js - return 0 - fi +ARG_INSTALL_METHOD=${ARG_INSTALL_METHOD:-curl} - # Check if node already exists and is adequate version - if command_exists node; then - node_version=$(node --version | sed 's/v//' | cut -d. -f1) - if [ "$node_version" -ge 18 ]; then - echo "✓ Node.js $(node --version) already installed" - return 0 - fi - echo "Node.js v$node_version found but v18+ required, installing newer version..." - fi +# ---------- OPENCODE INSTALL (CURL SCRIPT ONLY) ---------- - echo "Installing Node.js via NVM..." - - # Install NVM - export NVM_DIR="$HOME/.nvm" - if [ ! -d "$NVM_DIR" ]; then - echo "Installing NVM..." - curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash - fi - - # Load NVM - must happen AFTER installation - # shellcheck source=/dev/null - if [ -s "$NVM_DIR/nvm.sh" ]; then - \. "$NVM_DIR/nvm.sh" - else - echo "ERROR: NVM installation failed - nvm.sh not found" - exit 1 - fi - - # Verify nvm command is available - if ! command_exists nvm; then - echo "ERROR: nvm command not found after sourcing nvm.sh" - exit 1 - fi - - # Install Node.js LTS - echo "Installing Node.js LTS..." - nvm install --lts - nvm alias default node - nvm use default - - # Verify node is now available - if ! command_exists node; then - echo "ERROR: node command not found after nvm install" - exit 1 - fi - - echo "✓ Node.js $(node --version) installed successfully" -} - -setup_npm_prefix() { - if [ "$ARG_INSTALL_METHOD" != "npm" ]; then +install_opencode_via_curl() { + if command_exists opencode; then + echo "✓ OpenCode already installed: $(opencode --version 2>&1 | head -1 || echo '')" return 0 fi - # Configure npm to install global packages to ~/.local/bin - # This is the KEY difference from broken NVM approach: - # - NVM puts npm packages in ~/.nvm/versions/node//bin/ (requires sourcing NVM) - # - This puts them in ~/.local/bin (simple PATH, works in subprocesses) - echo "Configuring npm prefix to ~/.local..." - mkdir -p "$HOME/.local/bin" - npm config set prefix "$HOME/.local" - export PATH="$HOME/.local/bin:$PATH" + echo "OpenCode not found on PATH. Installing via curl installer..." - # Persist to bashrc (like working apt-get version did) - if ! grep -q 'PATH="$HOME/.local/bin:$PATH"' "$HOME/.bashrc" 2>/dev/null; then - echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$HOME/.bashrc" - fi + # Ensure it ends up in a predictable user bin dir + mkdir -p "$HOME/.local/bin" + export XDG_BIN_DIR="$HOME/.local/bin" - echo "✓ npm prefix configured to ~/.local" -} + # This installs the latest release and handles its own runtime + curl -fsSL https://opencode.ai/install | bash -install_opencode() { - # Make sure PATH is set before checking - export PATH="$HOME/.local/bin:$PATH" - - if command_exists opencode; then - echo "✓ OpenCode already installed: $(opencode --version 2>&1 | head -1)" - return 0 + # Make sure $HOME/.local/bin is on PATH in future shells + if ! grep -q 'XDG_BIN_DIR="$HOME/.local/bin"' "$HOME/.bashrc" 2>/dev/null \ + && ! grep -q 'PATH="$HOME/.local/bin:$PATH"' "$HOME/.bashrc" 2>/dev/null; then + { + echo 'export XDG_BIN_DIR="$HOME/.local/bin"' + echo 'export PATH="$HOME/.local/bin:$PATH"' + } >> "$HOME/.bashrc" fi - echo "Installing OpenCode (version: ${ARG_OPENCODE_VERSION}, method: ${ARG_INSTALL_METHOD})..." - - if [ "$ARG_INSTALL_METHOD" = "curl" ]; then - curl -fsSL https://opencode.ai/install | bash - elif [ "$ARG_INSTALL_METHOD" = "npm" ]; then - if [ "$ARG_OPENCODE_VERSION" = "latest" ]; then - npm install -g opencode-ai@latest - else - npm install -g "opencode-ai@${ARG_OPENCODE_VERSION}" - fi - else - echo "ERROR: Unknown install method: $ARG_INSTALL_METHOD" - exit 1 - fi + export PATH="$HOME/.local/bin:$PATH" if ! command_exists opencode; then - echo "ERROR: Failed to install OpenCode" + echo "ERROR: OpenCode still not found after curl install." exit 1 fi - echo "✓ OpenCode installed successfully: $(opencode --version 2>&1 | head -1)" + echo "✓ OpenCode installed via curl: $(opencode --version 2>&1 | head -1 || echo '')" } +# ---------- GITHUB AUTH CHECK (for git/gh, NOT auth.json) ---------- + check_github_authentication() { - echo "Checking GitHub authentication..." + echo "Checking GitHub authentication for git/gh use (not Copilot tokens)..." if [ -n "${GITHUB_TOKEN:-}" ]; then - echo "✓ GitHub token provided via module configuration" + echo "✓ GITHUB_TOKEN already set via environment/module" return 0 fi if command_exists coder; then if coder external-auth access-token "${ARG_EXTERNAL_AUTH_ID:-github}" > /dev/null 2>&1; then - echo "✓ GitHub OAuth authentication via Coder external auth" - return 0 + local t + t=$(coder external-auth access-token "${ARG_EXTERNAL_AUTH_ID:-github}" 2>/dev/null || echo "") + if [ -n "$t" ] && [ "$t" != "null" ]; then + export GITHUB_TOKEN="$t" + export GH_TOKEN="$t" + echo "✓ Using Coder external auth token for GitHub" + return 0 + fi fi fi if command_exists gh && gh auth status > /dev/null 2>&1; then - echo "✓ GitHub OAuth authentication via GitHub CLI" + echo "✓ GitHub CLI OAuth authentication (gh auth status) is available" return 0 fi - echo "⚠ No GitHub authentication detected" - echo " OpenCode can still work with other providers" - echo " For GitHub Copilot support, configure GitHub external auth or run 'opencode auth login'" + echo "⚠ No GitHub authentication detected." + echo " This only affects Git operations / gh; OpenCode can still use other providers." return 0 } +# ---------- OPENCODE CONFIG (opencode.json, not auth.json) ---------- + setup_opencode_configurations() { mkdir -p "$ARG_WORKDIR" @@ -162,60 +102,27 @@ setup_opencode_configurations() { setup_opencode_config() { export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}" local opencode_data_dir="$XDG_DATA_HOME/opencode" - local auth_file="$opencode_data_dir/auth.json" mkdir -p "$opencode_data_dir" if [ -n "$ARG_OPENCODE_CONFIG" ]; then - echo "Setting up OpenCode configuration..." + echo "Setting up OpenCode configuration (opencode.json)..." local opencode_config_dir="$HOME/.config/opencode" mkdir -p "$opencode_config_dir" echo "$ARG_OPENCODE_CONFIG" > "$opencode_config_dir/opencode.json" fi } -configure_github_copilot_provider() { - echo "Configuring GitHub Copilot provider for OpenCode..." +# ---------- COPILOT / CODER INTEGRATION (NO auth.json FABRICATED) ---------- +configure_github_copilot_provider() { + # We do NOT fabricate auth.json here anymore. + # It is provided verbatim via opencode_auth_config in start.sh. export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}" local opencode_data_dir="$XDG_DATA_HOME/opencode" - local auth_file="$opencode_data_dir/auth.json" mkdir -p "$opencode_data_dir" - local github_token="" - - if [ -n "${GITHUB_TOKEN:-}" ]; then - github_token="$GITHUB_TOKEN" - elif command_exists coder; then - github_token=$(coder external-auth access-token "${ARG_EXTERNAL_AUTH_ID:-github}" 2> /dev/null || echo "") - fi - - if [ -n "$github_token" ] && [ "$github_token" != "null" ]; then - echo "✓ GitHub token available - configuring Copilot provider in auth.json" - - cat > "$auth_file" < "$auth_file" - fi - fi + echo "OpenCode auth.json will be provided via opencode_auth_config or created by 'opencode auth login'." } configure_coder_integration() { @@ -231,21 +138,12 @@ configure_coder_integration() { fi } -# Main execution -install_nodejs -setup_npm_prefix -install_opencode +# ---------- RUN IT ---------- + +install_opencode_via_curl check_github_authentication setup_opencode_configurations configure_github_copilot_provider configure_coder_integration -# Final verification -echo "=== Final Verification ===" -echo "Node.js: $(node --version)" -echo "npm: $(npm --version)" -echo "which opencode: $(which opencode)" -opencode --version -echo "===========================" - echo "OpenCode module setup completed." diff --git a/registry/rothnic/modules/opencode/scripts/start.sh b/registry/rothnic/modules/opencode/scripts/start.sh index c4c02da1d..0251e7aa6 100644 --- a/registry/rothnic/modules/opencode/scripts/start.sh +++ b/registry/rothnic/modules/opencode/scripts/start.sh @@ -18,9 +18,10 @@ ARG_OPENCODE_AUTH_CONFIG=$(echo -n "${ARG_OPENCODE_AUTH_CONFIG:-}" | base64 -d 2 validate_opencode_installation() { if ! command_exists opencode; then - echo "ERROR: OpenCode not installed." + echo "ERROR: OpenCode not found on PATH. Did install.sh fail?" exit 1 fi + echo "✓ OpenCode found: $(opencode --version 2>&1 | head -1 || echo '')" } build_initial_prompt() { @@ -39,77 +40,49 @@ $ARG_AI_PROMPT" echo "$initial_prompt" } -build_opencode_args() { - OPENCODE_ARGS=() - - # ACP mode doesn't accept --provider argument - # Provider is configured via auth.json during installation -} - setup_github_authentication() { export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}" - echo "Setting up GitHub authentication..." - local opencode_data_dir="$XDG_DATA_HOME/opencode" local auth_file="$opencode_data_dir/auth.json" + + echo "Setting up OpenCode / GitHub authentication..." mkdir -p "$opencode_data_dir" - # Check if pre-configured auth.json was provided + # 1) If the module is given a full auth.json blob, use it verbatim. if [ -n "$ARG_OPENCODE_AUTH_CONFIG" ]; then - echo "✓ Using pre-configured auth.json from module variable" + echo "✓ Using pre-configured OpenCode auth.json from module variable" echo "$ARG_OPENCODE_AUTH_CONFIG" > "$auth_file" - return 0 fi - # Try to get GitHub token from external auth - local github_token="" - - if [ -n "${GITHUB_TOKEN:-}" ]; then - github_token="$GITHUB_TOKEN" - echo "✓ Using GitHub token from module configuration" - elif command_exists coder; then - github_token=$(coder external-auth access-token "${ARG_EXTERNAL_AUTH_ID:-github}" 2> /dev/null || echo "") - if [ -n "$github_token" ] && [ "$github_token" != "null" ]; then - echo "✓ Using Coder external auth OAuth token" + # 2) For general GitHub use (git, gh), try to populate GITHUB_TOKEN / GH_TOKEN. + # We do NOT derive auth.json from these tokens. + if [ -z "${GITHUB_TOKEN:-}" ]; then + if command_exists coder; then + local t + t=$(coder external-auth access-token "${ARG_EXTERNAL_AUTH_ID:-github}" 2>/dev/null || echo "") + if [ -n "$t" ] && [ "$t" != "null" ]; then + export GITHUB_TOKEN="$t" + export GH_TOKEN="$t" + echo "✓ Using Coder external auth token for GitHub (GITHUB_TOKEN/GH_TOKEN)" + fi fi + else + export GH_TOKEN="$GITHUB_TOKEN" + echo "✓ Using GITHUB_TOKEN from module configuration" fi - if [ -n "$github_token" ] && [ "$github_token" != "null" ]; then - # Note: This creates a simplified auth.json that may not work with GitHub Copilot - # For full GitHub Copilot support, use the opencode_auth_config variable - echo "⚠ Warning: Using simplified auth format - GitHub Copilot may require device flow auth" - cat > "$auth_file" < /dev/null 2>&1; then - echo "✓ Using GitHub CLI OAuth authentication" - return 0 + # 3) If still no token env, fall back to gh CLI if it's logged in. + if [ -z "${GITHUB_TOKEN:-}" ] && command_exists gh && gh auth status >/dev/null 2>&1; then + echo "✓ GitHub CLI auth is available (gh auth status ok)" fi - echo "⚠ No GitHub authentication available" - echo " OpenCode can still work with other providers" - echo " To use GitHub Copilot:" - echo " 1. Run 'opencode auth login' locally to get auth.json" - echo " 2. Pass the auth.json content via opencode_auth_config variable" - - # Ensure auth.json exists even without credentials + # 4) If we still don't have an auth.json, warn, but don't fabricate one. if [ ! -f "$auth_file" ]; then - echo '{"credentials":[]}' > "$auth_file" + echo "⚠ No OpenCode auth.json present." + echo " Copilot / provider credentials must be set by:" + echo " - Running 'opencode auth login' and wiring that auth.json into opencode_auth_config" + echo " - Or using another provider via env vars / config" fi - - return 0 } start_agentapi() { @@ -120,10 +93,8 @@ start_agentapi() { local initial_prompt initial_prompt=$(build_initial_prompt) - # Run opencode TUI with working directory as project path - # Agentapi sends plain text to stdin/stdout, not JSON-RPC - # OpenCode's TUI mode reads plain text input, similar to copilot/goose - # Background with & like codex does + # Run opencode with agentapi, backgrounded so start script returns quickly + # The agentapi module's wrapper expects the script to exit so it can run its wait loop if [ -n "$initial_prompt" ]; then echo "Using initial prompt with system context" agentapi server -I="$initial_prompt" --type=opencode --term-width 67 --term-height 1190 -- opencode "$ARG_WORKDIR" & From 7841fd6bd5731b9febfb45b024b9cf0af9168b20 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 21 Nov 2025 22:28:44 +0000 Subject: [PATCH 32/42] refactor: use Node.js tarball instead of curl/apt/nvm The curl installer was hanging on its own NVM/code-server installation. Switch to direct Node.js tarball download + npm install for reliability. Key changes: 1. install.sh - Node tarball approach: - Downloads Node.js 20.18.0 tarball from nodejs.org - Installs to /workspaces/.coder-tools (shared across workspaces) - Uses npm to install opencode-ai (pinnable version) - No apt, nvm, or curl installer dependencies - Shared npm cache for faster subsequent installs 2. main.tf: - Default install_method back to "npm" 3. README: - Updated to reflect tarball-based installation - Notes version pinning is now supported Benefits: - Predictable startup (no opaque installer scripts) - Version pinning via opencode_version variable - Shared Node.js cache across workspaces on same host - No apt lock contention issues - No nvm sourcing complexity --- registry/rothnic/modules/opencode/README.md | 14 ++- registry/rothnic/modules/opencode/main.tf | 4 +- .../modules/opencode/scripts/install.sh | 104 +++++++++++++----- 3 files changed, 90 insertions(+), 32 deletions(-) diff --git a/registry/rothnic/modules/opencode/README.md b/registry/rothnic/modules/opencode/README.md index 8efd90f9e..ccee079fb 100644 --- a/registry/rothnic/modules/opencode/README.md +++ b/registry/rothnic/modules/opencode/README.md @@ -22,7 +22,7 @@ module "opencode" { } ``` -OpenCode is automatically installed via the official curl installer (no Node.js required). +OpenCode is installed via npm with a module-managed Node.js tarball (no apt/nvm required). The Node.js binary is cached in `/workspaces/.coder-tools` for fast subsequent installs. ## Authentication (Optional) @@ -81,16 +81,20 @@ See [variables](./main.tf) for complete list. - 📊 Task reporting to Coder UI - 💾 Session persistence - 🔐 Flexible authentication -- 🚀 Simple curl-based installation (no Node.js required) +- 🚀 Node.js tarball installation (no apt/nvm) +- 📌 Version pinning support via `opencode_version` +- 💾 Shared Node.js cache across workspaces ## Prerequisites -- None - OpenCode is installed via the official curl installer which handles its own runtime +- None - Node.js is installed via tarball to `/workspaces/.coder-tools` ## Notes -- OpenCode is installed via `curl -fsSL https://opencode.ai/install | bash` -- Version pinning is not supported with curl installer (always installs latest) +- Node.js 20 is installed via tarball (not apt/nvm) for reliability +- OpenCode is installed via `npm install -g opencode-ai@version` +- Node.js and npm cache are shared across workspaces in `/workspaces/.coder-tools` +- Version pinning is supported via `opencode_version` variable - TUI may have limitations through AgentAPI (slash commands, menus) - For production, use `subdomain = true` with [wildcard access URL](https://coder.com/docs/admin/setup#wildcard-access-url) diff --git a/registry/rothnic/modules/opencode/main.tf b/registry/rothnic/modules/opencode/main.tf index a55a1c010..bb7b6ab60 100644 --- a/registry/rothnic/modules/opencode/main.tf +++ b/registry/rothnic/modules/opencode/main.tf @@ -82,8 +82,8 @@ variable "opencode_version" { variable "install_method" { type = string - description = "Installation method for OpenCode. Currently only 'curl' is used (installs latest)." - default = "curl" + description = "Installation method for OpenCode. Use 'npm' with the module-managed Node tarball." + default = "npm" } variable "report_tasks" { diff --git a/registry/rothnic/modules/opencode/scripts/install.sh b/registry/rothnic/modules/opencode/scripts/install.sh index 2a33ef077..0f1ed87b5 100644 --- a/registry/rothnic/modules/opencode/scripts/install.sh +++ b/registry/rothnic/modules/opencode/scripts/install.sh @@ -15,47 +15,101 @@ ARG_REPORT_TASKS=${ARG_REPORT_TASKS:-true} ARG_MCP_APP_STATUS_SLUG=${ARG_MCP_APP_STATUS_SLUG:-} ARG_OPENCODE_CONFIG=$(echo -n "${ARG_OPENCODE_CONFIG:-}" | base64 -d 2> /dev/null || echo "") ARG_EXTERNAL_AUTH_ID=${ARG_EXTERNAL_AUTH_ID:-github} -# Kept for future use, but NOT used in curl mode: ARG_OPENCODE_VERSION=${ARG_OPENCODE_VERSION:-latest} -ARG_INSTALL_METHOD=${ARG_INSTALL_METHOD:-curl} +ARG_INSTALL_METHOD=${ARG_INSTALL_METHOD:-npm} -# ---------- OPENCODE INSTALL (CURL SCRIPT ONLY) ---------- +# Version pins for the toolchain +NODE_VERSION="${NODE_VERSION:-20.18.0}" -install_opencode_via_curl() { - if command_exists opencode; then - echo "✓ OpenCode already installed: $(opencode --version 2>&1 | head -1 || echo '')" - return 0 +# ---------- NODE INSTALL (NO APT/NVM, SHARED CACHE) ---------- + +install_nodejs() { + if [ "$ARG_INSTALL_METHOD" != "npm" ]; then + echo "ERROR: install_method=${ARG_INSTALL_METHOD} is not supported without the curl installer." + echo " Use install_method=\"npm\" in the module or Terraform." + exit 1 fi - echo "OpenCode not found on PATH. Installing via curl installer..." + # Shared tool root across workspaces on this host + local dev_root="${DEV_ROOT:-/workspaces}" + local tool_root="${TOOL_ROOT:-$dev_root/.coder-tools}" + local node_distro="linux-x64" + local node_tarball="node-v${NODE_VERSION}-${node_distro}.tar.xz" + local node_dir="${tool_root}/node-v${NODE_VERSION}-${node_distro}" - # Ensure it ends up in a predictable user bin dir - mkdir -p "$HOME/.local/bin" - export XDG_BIN_DIR="$HOME/.local/bin" + mkdir -p "$tool_root" + + if [ ! -d "$node_dir" ]; then + echo "Node.js ${NODE_VERSION} not found in cache. Downloading to ${tool_root}..." + curl -fsSL "https://nodejs.org/dist/v${NODE_VERSION}/${node_tarball}" \ + -o "${tool_root}/${node_tarball}" + + echo "Extracting Node.js..." + tar -xJf "${tool_root}/${node_tarball}" -C "$tool_root" + rm -f "${tool_root}/${node_tarball}" + else + echo "✓ Node.js ${NODE_VERSION} already cached at ${node_dir}" + fi + + # Make Node available now + export PATH="${node_dir}/bin:$HOME/.local/bin:$PATH" - # This installs the latest release and handles its own runtime - curl -fsSL https://opencode.ai/install | bash + # Shared npm cache to speed up repeated installs + local npm_cache_dir="${tool_root}/npm-cache" + mkdir -p "$npm_cache_dir" + export NPM_CONFIG_CACHE="$npm_cache_dir" - # Make sure $HOME/.local/bin is on PATH in future shells - if ! grep -q 'XDG_BIN_DIR="$HOME/.local/bin"' "$HOME/.bashrc" 2>/dev/null \ - && ! grep -q 'PATH="$HOME/.local/bin:$PATH"' "$HOME/.bashrc" 2>/dev/null; then + # Persist PATH + npm cache so start.sh and future shells see it + if ! grep -q "node-v${NODE_VERSION}-${node_distro}/bin" "$HOME/.bashrc" 2>/dev/null; then { - echo 'export XDG_BIN_DIR="$HOME/.local/bin"' - echo 'export PATH="$HOME/.local/bin:$PATH"' + echo "export PATH=\"${node_dir}/bin:\$HOME/.local/bin:\$PATH\"" + echo "export NPM_CONFIG_CACHE=\"${npm_cache_dir}\"" } >> "$HOME/.bashrc" fi + if ! command_exists node; then + echo "ERROR: Node.js still not on PATH after tarball install" + exit 1 + fi + + echo "✓ Node.js installed via tarball: $(node --version)" +} + +# ---------- OPENCODE INSTALL (NPM, PINNABLE VERSION) ---------- + +install_opencode() { + mkdir -p "$HOME/.local/bin" export PATH="$HOME/.local/bin:$PATH" if ! command_exists opencode; then - echo "ERROR: OpenCode still not found after curl install." - exit 1 - fi + echo "Installing OpenCode via npm (version: ${ARG_OPENCODE_VERSION})..." + + npm config set prefix "$HOME/.local" >/dev/null 2>&1 || true + + if ! grep -q 'PATH="$HOME/.local/bin:$PATH"' "$HOME/.bashrc" 2>/dev/null; then + echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$HOME/.bashrc" + fi - echo "✓ OpenCode installed via curl: $(opencode --version 2>&1 | head -1 || echo '')" + if [ "$ARG_OPENCODE_VERSION" = "latest" ]; then + npm install -g opencode-ai@latest + else + npm install -g "opencode-ai@${ARG_OPENCODE_VERSION}" + fi + + export PATH="$HOME/.local/bin:$PATH" + + if ! command_exists opencode; then + echo "ERROR: Failed to install OpenCode" + exit 1 + fi + + echo "✓ OpenCode installed successfully: $(opencode --version 2>&1 | head -1)" + else + echo "✓ OpenCode already installed: $(opencode --version 2>&1 | head -1)" + fi } -# ---------- GITHUB AUTH CHECK (for git/gh, NOT auth.json) ---------- +# ---------- GITHUB AUTH (for git/gh, NOT auth.json) ---------- check_github_authentication() { echo "Checking GitHub authentication for git/gh use (not Copilot tokens)..." @@ -117,7 +171,6 @@ setup_opencode_config() { configure_github_copilot_provider() { # We do NOT fabricate auth.json here anymore. - # It is provided verbatim via opencode_auth_config in start.sh. export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}" local opencode_data_dir="$XDG_DATA_HOME/opencode" mkdir -p "$opencode_data_dir" @@ -140,7 +193,8 @@ configure_coder_integration() { # ---------- RUN IT ---------- -install_opencode_via_curl +install_nodejs +install_opencode check_github_authentication setup_opencode_configurations configure_github_copilot_provider From 8e5cc8f06e30674dfdaec8da4c834bf5b1e159a3 Mon Sep 17 00:00:00 2001 From: Nick Roth Date: Sun, 23 Nov 2025 04:32:12 -0600 Subject: [PATCH 33/42] docs: update rothnic namespace and opencode module documentation - Update namespace README with product/engineering focus and agentic systems - Add comprehensive opencode module documentation with usage examples - Document authentication methods (file-based GitHub Copilot auth recommended) - Add complete template example for Coder task execution - Document all configuration variables in organized tables - Clarify task reporting and integration with Coder Tasks UI --- registry/rothnic/README.md | 25 +-- registry/rothnic/modules/opencode/README.md | 222 ++++++++++++++++---- 2 files changed, 194 insertions(+), 53 deletions(-) diff --git a/registry/rothnic/README.md b/registry/rothnic/README.md index 62039aeeb..a81fd1d3c 100644 --- a/registry/rothnic/README.md +++ b/registry/rothnic/README.md @@ -1,6 +1,6 @@ --- display_name: "Nick Roth" -bio: "Software engineer specializing in data science infrastructure, containerization, and developer tooling. Based in Huntsville, AL, working on innovative solutions for autonomous development and productivity tools." +bio: "Product leader and engineer focused on end-to-end product delivery and agentic AI systems. Based in Huntsville, AL." avatar: "./.images/avatar.png" github: "rothnic" linkedin: "http://www.linkedin.com/in/nicholasleeroth/" @@ -10,23 +10,16 @@ status: "community" # Nick Roth -Software engineer specializing in data science infrastructure, containerization, and developer tooling. Based in Huntsville, AL, I focus on building tools that enhance developer productivity and streamline workflows. +Product manager and engineer with 14 years of experience leading product strategy, engineering delivery, and platform initiatives. I focus on end-to-end product ownership—from discovery and design through implementation, deployment, and iterative improvement. My current work centers on building self-learning, agentic systems that automate content and operational workflows while keeping humans in the loop for quality and governance. ## Expertise -- **Developer Tooling**: Creating innovative solutions for autonomous software development -- **Data Science Infrastructure**: Building containerized environments for Python, Jupyter, and data analysis -- **DevOps & Automation**: Docker, Kubernetes, and CI/CD pipeline development -- **AI/ML Integration**: Exploring AI-powered coding assistants and agent orchestration +- **Product Leadership:** Cross-functional product strategy, roadmapping, team leadership, vendor and stakeholder management. +- **Agentic Systems & Automation:** Designing and delivering self-learning agent pipelines, human-in-the-loop workflows, and production-ready automation for content and operations. +- **Platform Delivery & Modernization:** Architecture, procurement, migrations, and shipping reliable systems from prototype to production. +- **Growth & Experimentation:** SEO-led content strategy, A/B testing, experimentation frameworks, and monetization optimization. +- **Systems Engineering:** Requirements, architecture, and integration across complex systems. -## Notable Projects +## Modules -- **OpenCode Agents**: Multi-agent orchestration system for autonomous software development -- **Docker-TinyConda**: Streamlined pattern for dockerizing Python applications with conda -- **Anaconda-Notebook**: Popular Docker image for Jupyter Notebooks with full Anaconda installation (86+ stars) - -## Connect - -- 🌐 [Website](https://www.nickroth.com) -- 💼 [LinkedIn](http://www.linkedin.com/in/nicholasleeroth/) -- 🐙 [GitHub](https://github.com/rothnic) +- [opencode](./modules/opencode/) - Execute AI-driven coding tasks and agentic workflows directly within Coder workspaces using OpenCode. diff --git a/registry/rothnic/modules/opencode/README.md b/registry/rothnic/modules/opencode/README.md index ccee079fb..2d88a674c 100644 --- a/registry/rothnic/modules/opencode/README.md +++ b/registry/rothnic/modules/opencode/README.md @@ -9,71 +9,144 @@ tags: [agent, ai, opencode, coding-assistant, copilot, terminal] # OpenCode -Integrate [OpenCode.ai](https://opencode.ai/) - an AI coding agent that runs in your terminal and integrates with Coder's task system via [AgentAPI](https://github.com/coder/agentapi). +Integrate [OpenCode.ai](https://opencode.ai/) - an AI coding agent that executes tasks in your terminal and reports progress to Coder's task system via [AgentAPI](https://github.com/coder/agentapi). Supports GitHub Copilot, Anthropic Claude, OpenAI, and other providers. ## Quick Start +**Minimal setup (manual auth):** + ```tf module "opencode" { - source = "git::https://github.com/rothnic/coder-registry.git//registry/rothnic/modules/opencode" + source = "registry.coder.com/rothnic/opencode/coder" + agent_id = coder_agent.main.id + workdir = "/workspaces" +} +``` + +**With pre-configured GitHub Copilot auth:** +```tf +module "opencode" { + source = "registry.coder.com/rothnic/opencode/coder" agent_id = coder_agent.main.id workdir = "/workspaces" + + # Pre-configured auth from local machine + opencode_auth_config = file("${path.module}/opencode-auth.json") } ``` -OpenCode is installed via npm with a module-managed Node.js tarball (no apt/nvm required). The Node.js binary is cached in `/workspaces/.coder-tools` for fast subsequent installs. +**With Coder Tasks integration:** + +```tf +data "coder_task" "me" {} + +module "opencode" { + source = "registry.coder.com/rothnic/opencode/coder" + agent_id = coder_agent.main.id + workdir = "/workspaces" + + # Pass task prompt from Coder Tasks UI + ai_prompt = data.coder_task.me.prompt -## Authentication (Optional) + # Enable task reporting for Coder UI integration + report_tasks = true +} +``` -### GitHub Copilot +## Authentication -OpenCode requires GitHub Copilot's special session token format. Three options: +### GitHub Copilot (Recommended) -**Option 1: Pre-configured auth (Recommended)** +GitHub Copilot requires a special session token format. The most reliable method is pre-configured auth: + +**Step 1: Generate auth on your local machine** -Run locally: ```bash npm install -g opencode-ai -opencode auth login # Complete device flow -cat ~/.local/share/opencode/auth.json # Copy this +opencode auth login # Complete GitHub device flow +cat ~/.local/share/opencode/auth.json # Copy this content ``` -In your template: +**Step 2: Add auth file to your template** + +Create `opencode-auth.json` in your template directory with the copied content, then reference it: + ```tf module "opencode" { - source = "..." - - opencode_auth_config = file("${path.module}/opencode-auth.json") # Store as file - # Or use variable: opencode_auth_config = var.opencode_auth_json + source = "registry.coder.com/rothnic/opencode/coder" + agent_id = coder_agent.main.id + workdir = "/workspaces" + opencode_auth_config = file("${path.module}/opencode-auth.json") } ``` -**Option 2: Coder external auth (May not work)** +**Alternative: Manual login per workspace** -Configure [external auth](https://coder.com/docs/admin/external-auth) with `id = "github"`. Note: Regular OAuth tokens may not work with Copilot's authentication. +Users can run `opencode auth login` inside the workspace. Auth persists across workspace restarts. -**Option 3: Manual login** +**Note on Coder external auth:** Standard OAuth tokens typically don't work with Copilot's authentication system. -Users run `opencode auth login` in the workspace. Auth persists across restarts. +### Other Providers (Claude, OpenAI, etc.) -### Other Providers - -For Anthropic, OpenAI, etc., users authenticate via `opencode auth login` in the workspace. +For Anthropic, OpenAI, and other providers, users authenticate via `opencode auth login` in the workspace. Provider is configured via `opencode_provider` variable. ## Configuration +### Core Variables + +| Variable | Description | Default | Required | +|----------|-------------|---------|----------| +| `agent_id` | Coder agent ID | - | Yes | +| `workdir` | Working directory for OpenCode | - | Yes | + +### Authentication & Provider + +| Variable | Description | Default | +|----------|-------------|---------|| +| `opencode_auth_config` | Pre-configured auth.json content (for GitHub Copilot) | `""` | +| `opencode_provider` | AI provider: `copilot`, `anthropic`, `openai`, etc. | `"copilot"` | +| `github_token` | GitHub token (alternative to auth_config) | `""` | +| `external_auth_id` | Coder external auth provider ID | `"github"` | + +### Task Integration + +| Variable | Description | Default | +|----------|-------------|---------|| +| `ai_prompt` | Initial task prompt (use `data.coder_task.me.prompt`) | `""` | +| `system_prompt` | Custom system prompt for the AI | Built-in prompt | +| `report_tasks` | Enable task reporting to Coder UI | `true` | +| `resume_session` | Auto-resume latest session on restart | `true` | + +### Installation & Versioning + | Variable | Description | Default | -|----------|-------------|---------| -| `agent_id` | Coder agent ID | Required | -| `workdir` | Working directory | Required | -| `opencode_auth_config` | Pre-configured auth.json content | `""` (optional) | -| `opencode_version` | OpenCode version | `"latest"` | -| `install_method` | Installation method: `npm` or `curl` | `"npm"` | -| `report_tasks` | Enable Coder task reporting | `true` | +|----------|-------------|---------|| +| `opencode_version` | OpenCode version (`latest` or specific version) | `"latest"` | +| `install_method` | Installation method (`npm` recommended) | `"npm"` | +| `install_agentapi` | Install AgentAPI | `true` | +| `agentapi_version` | AgentAPI version | `"v0.10.0"` | + +### UI & Apps + +| Variable | Description | Default | +|----------|-------------|---------|| +| `web_app_display_name` | Display name in Coder UI | `"OpenCode"` | +| `order` | App position in UI | `null` | +| `group` | App group name | `null` | +| `icon` | App icon path | `"/icon/code.svg"` | | `subdomain` | Use subdomain for app access | `false` | +| `cli_app` | Create CLI app entry | `false` | + +### Advanced -See [variables](./main.tf) for complete list. +| Variable | Description | Default | +|----------|-------------|---------|| +| `opencode_config` | Custom OpenCode config (JSON) | `""` | +| `pre_install_script` | Script to run before install | `null` | +| `post_install_script` | Script to run after install | `null` | + +See [main.tf](./main.tf) for complete variable definitions. ## Features @@ -85,18 +158,93 @@ See [variables](./main.tf) for complete list. - 📌 Version pinning support via `opencode_version` - 💾 Shared Node.js cache across workspaces +## Complete Template Example + +Here's a lightweight template for Coder task execution: + +```tf +terraform { + required_providers { + coder = { + source = "coder/coder" + } + docker = { + source = "kreuzwerker/docker" + } + } +} + +provider "docker" {} + +data "coder_workspace" "me" {} +data "coder_workspace_owner" "me" {} + +# Task data source - provides prompts from Coder Tasks UI +data "coder_task" "me" {} + +resource "coder_agent" "main" { + arch = "amd64" + os = "linux" + + startup_script = <<-EOT + set -e + mkdir -p /workspaces + EOT + + env = { + GIT_AUTHOR_NAME = coalesce(data.coder_workspace_owner.me.full_name, data.coder_workspace_owner.me.name) + GIT_AUTHOR_EMAIL = data.coder_workspace_owner.me.email + GIT_COMMITTER_NAME = coalesce(data.coder_workspace_owner.me.full_name, data.coder_workspace_owner.me.name) + GIT_COMMITTER_EMAIL = data.coder_workspace_owner.me.email + } +} + +# OpenCode AI coding agent +module "opencode" { + source = "registry.coder.com/rothnic/opencode/coder" + agent_id = coder_agent.main.id + workdir = "/workspaces" + + # Pre-configured GitHub Copilot authentication + opencode_auth_config = file("${path.module}/opencode-auth.json") + + # Pass task prompt from Coder Tasks UI + ai_prompt = data.coder_task.me.prompt + + # GitHub Copilot provider (default) + opencode_provider = "copilot" + + # Enable task reporting for Coder UI integration + report_tasks = true + + # Display settings + order = 1 + web_app_display_name = "OpenCode AI" +} + +resource "docker_container" "workspace" { + count = data.coder_workspace.me.start_count + image = "codercom/enterprise-base:ubuntu" + name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}" + + entrypoint = ["sh", "-c", coder_agent.main.init_script] + env = ["CODER_AGENT_TOKEN=${coder_agent.main.token}"] +} +``` + ## Prerequisites -- None - Node.js is installed via tarball to `/workspaces/.coder-tools` +- None - Node.js 20 is automatically installed via tarball ## Notes -- Node.js 20 is installed via tarball (not apt/nvm) for reliability -- OpenCode is installed via `npm install -g opencode-ai@version` -- Node.js and npm cache are shared across workspaces in `/workspaces/.coder-tools` -- Version pinning is supported via `opencode_version` variable -- TUI may have limitations through AgentAPI (slash commands, menus) -- For production, use `subdomain = true` with [wildcard access URL](https://coder.com/docs/admin/setup#wildcard-access-url) +- **Node.js installation**: Installed via tarball (not apt/nvm) to `/workspaces/.coder-tools` for reliability and workspace portability +- **OpenCode installation**: Installed via `npm install -g opencode-ai@{version}` +- **Caching**: Node.js and npm cache shared across workspaces in `/workspaces/.coder-tools` +- **Version pinning**: Use `opencode_version` variable to lock to specific versions +- **TUI limitations**: Some interactive features (slash commands, menus) may have limitations through AgentAPI +- **Subdomain access**: For production, use `subdomain = true` with [wildcard access URL](https://coder.com/docs/admin/setup#wildcard-access-url) +- **Task reporting**: When `report_tasks = true`, the module automatically configures system prompts for granular task status updates ## Resources From 161b8524bb33b032a326852e5e7fa96cb85e2914 Mon Sep 17 00:00:00 2001 From: Nick Roth Date: Sun, 23 Nov 2025 08:15:58 -0600 Subject: [PATCH 34/42] fix: use standard terminal dimensions for opencode TUI Change agentapi terminal size from 67x1190 to 120x40 to fix scrolling issues. Large terminal heights cause TUI apps to render huge interfaces, triggering browser auto-scroll behavior. Standard dimensions ensure the interface fits in the browser view with internal TUI scrolling (PgUp/PgDn) instead of browser scrollbar jumps. --- registry/rothnic/modules/opencode/scripts/start.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/registry/rothnic/modules/opencode/scripts/start.sh b/registry/rothnic/modules/opencode/scripts/start.sh index 0251e7aa6..489c5010f 100644 --- a/registry/rothnic/modules/opencode/scripts/start.sh +++ b/registry/rothnic/modules/opencode/scripts/start.sh @@ -95,11 +95,12 @@ start_agentapi() { # Run opencode with agentapi, backgrounded so start script returns quickly # The agentapi module's wrapper expects the script to exit so it can run its wait loop + # Use standard terminal dimensions (120x40) for TUI stability - prevents browser auto-scroll issues if [ -n "$initial_prompt" ]; then echo "Using initial prompt with system context" - agentapi server -I="$initial_prompt" --type=opencode --term-width 67 --term-height 1190 -- opencode "$ARG_WORKDIR" & + agentapi server -I="$initial_prompt" --type=opencode --term-width 120 --term-height 40 -- opencode "$ARG_WORKDIR" & else - agentapi server --type=opencode --term-width 67 --term-height 1190 -- opencode "$ARG_WORKDIR" & + agentapi server --type=opencode --term-width 120 --term-height 40 -- opencode "$ARG_WORKDIR" & fi } From d688c48fa64d294df304637161c7865e4359d7ec Mon Sep 17 00:00:00 2001 From: Nick Roth Date: Mon, 24 Nov 2025 17:39:07 -0600 Subject: [PATCH 35/42] feat(opencode): add MCP servers and model configuration support - Add mcp_servers variable for JSON MCP servers config (similar to claude-code) - Add opencode_model variable for model selection - Update install.sh to build opencode.json config with jq - Update README with Model & MCP Configuration section - Add MCP servers example with filesystem and github servers --- registry/rothnic/modules/opencode/README.md | 47 +++++++++++++++--- registry/rothnic/modules/opencode/main.tf | 16 ++++++- .../modules/opencode/scripts/install.sh | 48 +++++++++++++++++-- 3 files changed, 101 insertions(+), 10 deletions(-) diff --git a/registry/rothnic/modules/opencode/README.md b/registry/rothnic/modules/opencode/README.md index 2d88a674c..1ccb7f3ac 100644 --- a/registry/rothnic/modules/opencode/README.md +++ b/registry/rothnic/modules/opencode/README.md @@ -103,7 +103,7 @@ For Anthropic, OpenAI, and other providers, users authenticate via `opencode aut ### Authentication & Provider | Variable | Description | Default | -|----------|-------------|---------|| +|----------|-------------|---------| | `opencode_auth_config` | Pre-configured auth.json content (for GitHub Copilot) | `""` | | `opencode_provider` | AI provider: `copilot`, `anthropic`, `openai`, etc. | `"copilot"` | | `github_token` | GitHub token (alternative to auth_config) | `""` | @@ -112,7 +112,7 @@ For Anthropic, OpenAI, and other providers, users authenticate via `opencode aut ### Task Integration | Variable | Description | Default | -|----------|-------------|---------|| +|----------|-------------|---------| | `ai_prompt` | Initial task prompt (use `data.coder_task.me.prompt`) | `""` | | `system_prompt` | Custom system prompt for the AI | Built-in prompt | | `report_tasks` | Enable task reporting to Coder UI | `true` | @@ -121,7 +121,7 @@ For Anthropic, OpenAI, and other providers, users authenticate via `opencode aut ### Installation & Versioning | Variable | Description | Default | -|----------|-------------|---------|| +|----------|-------------|---------| | `opencode_version` | OpenCode version (`latest` or specific version) | `"latest"` | | `install_method` | Installation method (`npm` recommended) | `"npm"` | | `install_agentapi` | Install AgentAPI | `true` | @@ -130,7 +130,7 @@ For Anthropic, OpenAI, and other providers, users authenticate via `opencode aut ### UI & Apps | Variable | Description | Default | -|----------|-------------|---------|| +|----------|-------------|---------| | `web_app_display_name` | Display name in Coder UI | `"OpenCode"` | | `order` | App position in UI | `null` | | `group` | App group name | `null` | @@ -138,11 +138,44 @@ For Anthropic, OpenAI, and other providers, users authenticate via `opencode aut | `subdomain` | Use subdomain for app access | `false` | | `cli_app` | Create CLI app entry | `false` | +### Model & MCP Configuration + +| Variable | Description | Default | +|----------|-------------|---------| +| `opencode_model` | Model to use (e.g., `claude-3.7-sonnet`, `gpt-4o`). If empty, uses provider default. | `""` | +| `mcp_servers` | MCP servers configuration as JSON string (see example below) | `""` | + +**MCP Servers Example:** + +```tf +module "opencode" { + source = "registry.coder.com/rothnic/opencode/coder" + agent_id = coder_agent.main.id + workdir = "/workspaces" + + opencode_model = "claude-3.7-sonnet" + + mcp_servers = jsonencode({ + "filesystem" = { + command = "npx" + args = ["-y", "@modelcontextprotocol/server-filesystem", "/workspaces"] + } + "github" = { + command = "npx" + args = ["-y", "@modelcontextprotocol/server-github"] + env = { + GITHUB_TOKEN = var.github_token + } + } + }) +} +``` + ### Advanced | Variable | Description | Default | -|----------|-------------|---------|| -| `opencode_config` | Custom OpenCode config (JSON) | `""` | +|----------|-------------|---------| +| `opencode_config` | Full custom OpenCode config (JSON). Overrides other config options. | `""` | | `pre_install_script` | Script to run before install | `null` | | `post_install_script` | Script to run after install | `null` | @@ -151,6 +184,8 @@ See [main.tf](./main.tf) for complete variable definitions. ## Features - 🤖 Multiple AI providers (Copilot, Claude, GPT) +- 🔧 MCP servers configuration support +- 🎯 Model selection per deployment - 📊 Task reporting to Coder UI - 💾 Session persistence - 🔐 Flexible authentication diff --git a/registry/rothnic/modules/opencode/main.tf b/registry/rothnic/modules/opencode/main.tf index bb7b6ab60..9b1e066de 100644 --- a/registry/rothnic/modules/opencode/main.tf +++ b/registry/rothnic/modules/opencode/main.tf @@ -37,9 +37,21 @@ variable "opencode_provider" { default = "copilot" } +variable "opencode_model" { + type = string + description = "The model for OpenCode to use (e.g., 'claude-3.7-sonnet', 'gpt-4o'). If empty, uses provider default." + default = "" +} + +variable "mcp_servers" { + type = string + description = "MCP servers configuration as JSON string. Will be merged into the mcpServers section of opencode.json. Example: '{\"filesystem\":{\"type\":\"stdio\",\"command\":\"npx\",\"args\":[\"-y\",\"@modelcontextprotocol/server-filesystem\",\"/workspaces\"]}}'" + default = "" +} + variable "opencode_config" { type = string - description = "Custom OpenCode configuration as JSON string." + description = "Complete custom OpenCode configuration as JSON string. If provided, this overrides default config generation. For partial config (just MCP servers), use mcp_servers variable instead." default = "" } @@ -250,6 +262,8 @@ module "agentapi" { ARG_REPORT_TASKS='${var.report_tasks}' \ ARG_WORKDIR='${local.workdir}' \ ARG_OPENCODE_CONFIG='${var.opencode_config != "" ? base64encode(var.opencode_config) : ""}' \ + ARG_MCP_SERVERS='${var.mcp_servers != "" ? base64encode(var.mcp_servers) : ""}' \ + ARG_OPENCODE_MODEL='${var.opencode_model}' \ ARG_EXTERNAL_AUTH_ID='${var.external_auth_id}' \ ARG_OPENCODE_VERSION='${var.opencode_version}' \ ARG_INSTALL_METHOD='${var.install_method}' \ diff --git a/registry/rothnic/modules/opencode/scripts/install.sh b/registry/rothnic/modules/opencode/scripts/install.sh index 0f1ed87b5..4ea8852d5 100644 --- a/registry/rothnic/modules/opencode/scripts/install.sh +++ b/registry/rothnic/modules/opencode/scripts/install.sh @@ -14,6 +14,8 @@ ARG_WORKDIR=${ARG_WORKDIR:-"$HOME"} ARG_REPORT_TASKS=${ARG_REPORT_TASKS:-true} ARG_MCP_APP_STATUS_SLUG=${ARG_MCP_APP_STATUS_SLUG:-} ARG_OPENCODE_CONFIG=$(echo -n "${ARG_OPENCODE_CONFIG:-}" | base64 -d 2> /dev/null || echo "") +ARG_MCP_SERVERS=$(echo -n "${ARG_MCP_SERVERS:-}" | base64 -d 2> /dev/null || echo "") +ARG_OPENCODE_MODEL=${ARG_OPENCODE_MODEL:-} ARG_EXTERNAL_AUTH_ID=${ARG_EXTERNAL_AUTH_ID:-github} ARG_OPENCODE_VERSION=${ARG_OPENCODE_VERSION:-latest} ARG_INSTALL_METHOD=${ARG_INSTALL_METHOD:-npm} @@ -156,14 +158,54 @@ setup_opencode_configurations() { setup_opencode_config() { export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}" local opencode_data_dir="$XDG_DATA_HOME/opencode" + local opencode_config_dir="$HOME/.config/opencode" mkdir -p "$opencode_data_dir" + mkdir -p "$opencode_config_dir" + # If full custom config provided, use it directly if [ -n "$ARG_OPENCODE_CONFIG" ]; then - echo "Setting up OpenCode configuration (opencode.json)..." - local opencode_config_dir="$HOME/.config/opencode" - mkdir -p "$opencode_config_dir" + echo "Setting up OpenCode configuration (opencode.json) from custom config..." echo "$ARG_OPENCODE_CONFIG" > "$opencode_config_dir/opencode.json" + return 0 + fi + + # Otherwise, build config from individual options + echo "Building OpenCode configuration..." + + # Start with base config + local config='{}' + + # Add model config if specified + if [ -n "$ARG_OPENCODE_MODEL" ]; then + echo " Adding model configuration: $ARG_OPENCODE_MODEL" + config=$(echo "$config" | jq --arg model "$ARG_OPENCODE_MODEL" '. + { + "agents": { + "coder": {"model": $model}, + "task": {"model": $model} + } + }') + fi + + # Add MCP servers if specified + if [ -n "$ARG_MCP_SERVERS" ]; then + echo " Adding MCP servers configuration..." + # Merge MCP servers into config + local mcp_config + mcp_config=$(echo "$ARG_MCP_SERVERS" | jq '.') + if [ $? -eq 0 ] && [ -n "$mcp_config" ]; then + config=$(echo "$config" | jq --argjson mcp "$mcp_config" '. + {"mcpServers": $mcp}') + else + echo " ⚠ Warning: Invalid MCP servers JSON, skipping" + fi + fi + + # Only write config if we have something to configure + if [ "$config" != '{}' ]; then + echo "$config" | jq '.' > "$opencode_config_dir/opencode.json" + echo "✓ OpenCode config written to $opencode_config_dir/opencode.json" + else + echo " No custom configuration needed" fi } From 572d8bf3dae3c0dec4304835368e8c49e43200c0 Mon Sep 17 00:00:00 2001 From: Nick Roth Date: Mon, 24 Nov 2025 17:43:06 -0600 Subject: [PATCH 36/42] test(opencode): add MCP and model configuration tests, simplify Quick Start examples - Add test cases for mcp_servers and opencode_model variables - Add test for combined MCP + model configuration - Add test verifying default empty values - Simplify README Quick Start with cleaner examples - Use /home/coder as default workdir in examples (more typical) --- registry/rothnic/modules/opencode/README.md | 36 +++++---- .../modules/opencode/opencode.tftest.hcl | 80 +++++++++++++++++++ 2 files changed, 99 insertions(+), 17 deletions(-) diff --git a/registry/rothnic/modules/opencode/README.md b/registry/rothnic/modules/opencode/README.md index 1ccb7f3ac..db7cd11b1 100644 --- a/registry/rothnic/modules/opencode/README.md +++ b/registry/rothnic/modules/opencode/README.md @@ -13,44 +13,46 @@ Integrate [OpenCode.ai](https://opencode.ai/) - an AI coding agent that executes ## Quick Start -**Minimal setup (manual auth):** +**Basic setup:** ```tf module "opencode" { source = "registry.coder.com/rothnic/opencode/coder" agent_id = coder_agent.main.id - workdir = "/workspaces" + workdir = "/home/coder" } ``` -**With pre-configured GitHub Copilot auth:** +**With Coder Tasks (recommended for AI workspaces):** ```tf +data "coder_task" "me" {} + module "opencode" { source = "registry.coder.com/rothnic/opencode/coder" agent_id = coder_agent.main.id - workdir = "/workspaces" - - # Pre-configured auth from local machine - opencode_auth_config = file("${path.module}/opencode-auth.json") + workdir = "/home/coder" + + ai_prompt = data.coder_task.me.prompt + report_tasks = true } ``` -**With Coder Tasks integration:** +**With model and MCP servers:** ```tf -data "coder_task" "me" {} - module "opencode" { source = "registry.coder.com/rothnic/opencode/coder" agent_id = coder_agent.main.id - workdir = "/workspaces" - - # Pass task prompt from Coder Tasks UI - ai_prompt = data.coder_task.me.prompt - - # Enable task reporting for Coder UI integration - report_tasks = true + workdir = "/home/coder" + + opencode_model = "claude-3.7-sonnet" + mcp_servers = jsonencode({ + filesystem = { + command = "npx" + args = ["-y", "@modelcontextprotocol/server-filesystem", "/home/coder"] + } + }) } ``` diff --git a/registry/rothnic/modules/opencode/opencode.tftest.hcl b/registry/rothnic/modules/opencode/opencode.tftest.hcl index 7daff5838..c7d715556 100644 --- a/registry/rothnic/modules/opencode/opencode.tftest.hcl +++ b/registry/rothnic/modules/opencode/opencode.tftest.hcl @@ -229,3 +229,83 @@ run "agentapi_version_is_set" { error_message = "AgentAPI version should be set to v0.10.0" } } + +run "mcp_servers_config" { + command = plan + + variables { + agent_id = "test-agent" + workdir = "/home/coder" + mcp_servers = jsonencode({ + filesystem = { + command = "npx" + args = ["-y", "@modelcontextprotocol/server-filesystem", "/workspaces"] + } + }) + } + + assert { + condition = var.mcp_servers != "" + error_message = "MCP servers configuration should be provided" + } +} + +run "opencode_model_config" { + command = plan + + variables { + agent_id = "test-agent" + workdir = "/home/coder" + opencode_model = "claude-3.7-sonnet" + } + + assert { + condition = var.opencode_model == "claude-3.7-sonnet" + error_message = "OpenCode model should be set to 'claude-3.7-sonnet'" + } +} + +run "mcp_and_model_combined" { + command = plan + + variables { + agent_id = "test-agent" + workdir = "/home/coder" + opencode_model = "gpt-4o" + mcp_servers = jsonencode({ + github = { + command = "npx" + args = ["-y", "@modelcontextprotocol/server-github"] + } + }) + } + + assert { + condition = var.opencode_model == "gpt-4o" + error_message = "OpenCode model should be set" + } + + assert { + condition = var.mcp_servers != "" + error_message = "MCP servers should be configured" + } +} + +run "model_defaults_to_empty" { + command = plan + + variables { + agent_id = "test-agent" + workdir = "/home/coder" + } + + assert { + condition = var.opencode_model == "" + error_message = "OpenCode model should default to empty (provider default)" + } + + assert { + condition = var.mcp_servers == "" + error_message = "MCP servers should default to empty" + } +} From be8a20f73126d9a5edf2899c97626dea28288e95 Mon Sep 17 00:00:00 2001 From: Nick Roth Date: Mon, 24 Nov 2025 17:52:29 -0600 Subject: [PATCH 37/42] Clean up provider configuration and remove misleading env vars Changes: - Remove OPENCODE_PROVIDER env var (OpenCode doesn't use it - provider is configured via auth.json) - Remove empty configure_github_copilot_provider() function - Remove verbose development comments from install.sh - Update opencode_provider variable description to clarify it's documentation only - Update README with accurate provider authentication documentation - Clarify GitHub token is for git operations, not AI provider auth - Remove unused ARG_OPENCODE_PROVIDER from start.sh The key insight from OpenCode docs: authentication happens via 'opencode auth login' which creates ~/.local/share/opencode/auth.json. Standard GitHub tokens don't work for Copilot - it requires OAuth device flow. Tests: 15 passed --- bun.lock | 1 + registry/rothnic/modules/opencode/README.md | 122 +++++++++--------- registry/rothnic/modules/opencode/main.tf | 10 +- .../modules/opencode/opencode.tftest.hcl | 25 ---- .../modules/opencode/scripts/install.sh | 40 ++---- .../rothnic/modules/opencode/scripts/start.sh | 7 +- 6 files changed, 77 insertions(+), 128 deletions(-) diff --git a/bun.lock b/bun.lock index 16c21d09b..b8a3e27c9 100644 --- a/bun.lock +++ b/bun.lock @@ -1,5 +1,6 @@ { "lockfileVersion": 1, + "configVersion": 0, "workspaces": { "": { "name": "registry", diff --git a/registry/rothnic/modules/opencode/README.md b/registry/rothnic/modules/opencode/README.md index db7cd11b1..573e7021b 100644 --- a/registry/rothnic/modules/opencode/README.md +++ b/registry/rothnic/modules/opencode/README.md @@ -32,7 +32,7 @@ module "opencode" { source = "registry.coder.com/rothnic/opencode/coder" agent_id = coder_agent.main.id workdir = "/home/coder" - + ai_prompt = data.coder_task.me.prompt report_tasks = true } @@ -45,7 +45,7 @@ module "opencode" { source = "registry.coder.com/rothnic/opencode/coder" agent_id = coder_agent.main.id workdir = "/home/coder" - + opencode_model = "claude-3.7-sonnet" mcp_servers = jsonencode({ filesystem = { @@ -58,21 +58,24 @@ module "opencode" { ## Authentication -### GitHub Copilot (Recommended) +OpenCode supports many AI providers (GitHub Copilot, Anthropic Claude, OpenAI, etc.). Authentication is managed via `~/.local/share/opencode/auth.json`, created by running `opencode auth login`. + +### GitHub Copilot -GitHub Copilot requires a special session token format. The most reliable method is pre-configured auth: +GitHub Copilot requires OAuth device flow authentication - you cannot use standard GitHub tokens. -**Step 1: Generate auth on your local machine** +**Option 1: Pre-configured auth (recommended for automation)** + +Generate auth on your local machine, then embed in your template: ```bash +# On your local machine npm install -g opencode-ai -opencode auth login # Complete GitHub device flow -cat ~/.local/share/opencode/auth.json # Copy this content +opencode auth login # Select "GitHub Copilot", complete device flow +cat ~/.local/share/opencode/auth.json # Copy this content ``` -**Step 2: Add auth file to your template** - -Create `opencode-auth.json` in your template directory with the copied content, then reference it: +Create `opencode-auth.json` in your template directory, then reference it: ```tf module "opencode" { @@ -83,69 +86,75 @@ module "opencode" { } ``` -**Alternative: Manual login per workspace** +**Option 2: Manual login per workspace** -Users can run `opencode auth login` inside the workspace. Auth persists across workspace restarts. - -**Note on Coder external auth:** Standard OAuth tokens typically don't work with Copilot's authentication system. +Users run `opencode auth login` inside the workspace. Auth persists across restarts. ### Other Providers (Claude, OpenAI, etc.) -For Anthropic, OpenAI, and other providers, users authenticate via `opencode auth login` in the workspace. Provider is configured via `opencode_provider` variable. +For Anthropic, OpenAI, DeepSeek, Groq, and [other providers](https://opencode.ai/docs/providers/): + +1. Users run `opencode auth login` in the workspace +2. Select their provider and enter API key +3. Credentials persist in `~/.local/share/opencode/auth.json` + +Or provide API keys via environment variables (see provider docs). + +**Note:** The `opencode_provider` variable is for documentation only - it doesn't configure OpenCode. Provider selection happens via `opencode auth login`. ## Configuration ### Core Variables -| Variable | Description | Default | Required | -|----------|-------------|---------|----------| -| `agent_id` | Coder agent ID | - | Yes | -| `workdir` | Working directory for OpenCode | - | Yes | +| Variable | Description | Default | Required | +| ---------- | ------------------------------ | ------- | -------- | +| `agent_id` | Coder agent ID | - | Yes | +| `workdir` | Working directory for OpenCode | - | Yes | ### Authentication & Provider -| Variable | Description | Default | -|----------|-------------|---------| -| `opencode_auth_config` | Pre-configured auth.json content (for GitHub Copilot) | `""` | -| `opencode_provider` | AI provider: `copilot`, `anthropic`, `openai`, etc. | `"copilot"` | -| `github_token` | GitHub token (alternative to auth_config) | `""` | -| `external_auth_id` | Coder external auth provider ID | `"github"` | +| Variable | Description | Default | +| ---------------------- | -------------------------------------------------------------------------------------- | ----------- | +| `opencode_auth_config` | Pre-configured auth.json content (for GitHub Copilot or any provider) | `""` | +| `opencode_provider` | Intended provider (documentation only - actual provider set via `opencode auth login`) | `"copilot"` | +| `github_token` | GitHub token for git operations (not AI provider auth) | `""` | +| `external_auth_id` | Coder external auth provider ID for git operations | `"github"` | ### Task Integration -| Variable | Description | Default | -|----------|-------------|---------| -| `ai_prompt` | Initial task prompt (use `data.coder_task.me.prompt`) | `""` | -| `system_prompt` | Custom system prompt for the AI | Built-in prompt | -| `report_tasks` | Enable task reporting to Coder UI | `true` | -| `resume_session` | Auto-resume latest session on restart | `true` | +| Variable | Description | Default | +| ---------------- | ----------------------------------------------------- | --------------- | +| `ai_prompt` | Initial task prompt (use `data.coder_task.me.prompt`) | `""` | +| `system_prompt` | Custom system prompt for the AI | Built-in prompt | +| `report_tasks` | Enable task reporting to Coder UI | `true` | +| `resume_session` | Auto-resume latest session on restart | `true` | ### Installation & Versioning -| Variable | Description | Default | -|----------|-------------|---------| -| `opencode_version` | OpenCode version (`latest` or specific version) | `"latest"` | -| `install_method` | Installation method (`npm` recommended) | `"npm"` | -| `install_agentapi` | Install AgentAPI | `true` | -| `agentapi_version` | AgentAPI version | `"v0.10.0"` | +| Variable | Description | Default | +| ------------------ | ----------------------------------------------- | ----------- | +| `opencode_version` | OpenCode version (`latest` or specific version) | `"latest"` | +| `install_method` | Installation method (`npm` recommended) | `"npm"` | +| `install_agentapi` | Install AgentAPI | `true` | +| `agentapi_version` | AgentAPI version | `"v0.10.0"` | ### UI & Apps -| Variable | Description | Default | -|----------|-------------|---------| -| `web_app_display_name` | Display name in Coder UI | `"OpenCode"` | -| `order` | App position in UI | `null` | -| `group` | App group name | `null` | -| `icon` | App icon path | `"/icon/code.svg"` | -| `subdomain` | Use subdomain for app access | `false` | -| `cli_app` | Create CLI app entry | `false` | +| Variable | Description | Default | +| ---------------------- | ---------------------------- | ------------------ | +| `web_app_display_name` | Display name in Coder UI | `"OpenCode"` | +| `order` | App position in UI | `null` | +| `group` | App group name | `null` | +| `icon` | App icon path | `"/icon/code.svg"` | +| `subdomain` | Use subdomain for app access | `false` | +| `cli_app` | Create CLI app entry | `false` | ### Model & MCP Configuration -| Variable | Description | Default | -|----------|-------------|---------| -| `opencode_model` | Model to use (e.g., `claude-3.7-sonnet`, `gpt-4o`). If empty, uses provider default. | `""` | -| `mcp_servers` | MCP servers configuration as JSON string (see example below) | `""` | +| Variable | Description | Default | +| ---------------- | ------------------------------------------------------------------------------------ | ------- | +| `opencode_model` | Model to use (e.g., `claude-3.7-sonnet`, `gpt-4o`). If empty, uses provider default. | `""` | +| `mcp_servers` | MCP servers configuration as JSON string (see example below) | `""` | **MCP Servers Example:** @@ -175,11 +184,11 @@ module "opencode" { ### Advanced -| Variable | Description | Default | -|----------|-------------|---------| -| `opencode_config` | Full custom OpenCode config (JSON). Overrides other config options. | `""` | -| `pre_install_script` | Script to run before install | `null` | -| `post_install_script` | Script to run after install | `null` | +| Variable | Description | Default | +| --------------------- | ------------------------------------------------------------------- | ------- | +| `opencode_config` | Full custom OpenCode config (JSON). Overrides other config options. | `""` | +| `pre_install_script` | Script to run before install | `null` | +| `post_install_script` | Script to run after install | `null` | See [main.tf](./main.tf) for complete variable definitions. @@ -242,15 +251,12 @@ module "opencode" { agent_id = coder_agent.main.id workdir = "/workspaces" - # Pre-configured GitHub Copilot authentication + # Pre-configured authentication (from 'opencode auth login' output) opencode_auth_config = file("${path.module}/opencode-auth.json") # Pass task prompt from Coder Tasks UI ai_prompt = data.coder_task.me.prompt - # GitHub Copilot provider (default) - opencode_provider = "copilot" - # Enable task reporting for Coder UI integration report_tasks = true diff --git a/registry/rothnic/modules/opencode/main.tf b/registry/rothnic/modules/opencode/main.tf index 9b1e066de..2482a8c5b 100644 --- a/registry/rothnic/modules/opencode/main.tf +++ b/registry/rothnic/modules/opencode/main.tf @@ -33,7 +33,7 @@ variable "github_token" { variable "opencode_provider" { type = string - description = "AI provider to use with OpenCode. Supported values: anthropic, openai, copilot (default), etc." + description = "Intended AI provider (for documentation/defaults). Actual provider is configured via opencode_auth_config or by running 'opencode auth login' in the workspace." default = "copilot" } @@ -198,13 +198,6 @@ resource "coder_env" "mcp_app_status_slug" { value = local.app_slug } -resource "coder_env" "opencode_provider" { - count = var.opencode_provider != "" ? 1 : 0 - agent_id = var.agent_id - name = "OPENCODE_PROVIDER" - value = var.opencode_provider -} - resource "coder_env" "github_token" { count = var.github_token != "" ? 1 : 0 agent_id = var.agent_id @@ -244,7 +237,6 @@ module "agentapi" { ARG_WORKDIR='${local.workdir}' \ ARG_AI_PROMPT='${base64encode(var.ai_prompt)}' \ ARG_SYSTEM_PROMPT='${base64encode(local.final_system_prompt)}' \ - ARG_OPENCODE_PROVIDER='${var.opencode_provider}' \ ARG_EXTERNAL_AUTH_ID='${var.external_auth_id}' \ ARG_RESUME_SESSION='${var.resume_session}' \ ARG_OPENCODE_AUTH_CONFIG='${var.opencode_auth_config != "" ? base64encode(var.opencode_auth_config) : ""}' \ diff --git a/registry/rothnic/modules/opencode/opencode.tftest.hcl b/registry/rothnic/modules/opencode/opencode.tftest.hcl index c7d715556..5ece555a8 100644 --- a/registry/rothnic/modules/opencode/opencode.tftest.hcl +++ b/registry/rothnic/modules/opencode/opencode.tftest.hcl @@ -77,31 +77,6 @@ run "github_token_not_created_when_empty" { } } -run "opencode_provider_env_var_created" { - command = plan - - variables { - agent_id = "test-agent" - workdir = "/home/coder" - opencode_provider = "anthropic" - } - - assert { - condition = length(resource.coder_env.opencode_provider) == 1 - error_message = "opencode_provider env var should be created when provider is specified" - } - - assert { - condition = resource.coder_env.opencode_provider[0].name == "OPENCODE_PROVIDER" - error_message = "opencode_provider env var name should be 'OPENCODE_PROVIDER'" - } - - assert { - condition = resource.coder_env.opencode_provider[0].value == "anthropic" - error_message = "opencode_provider env var value should match input" - } -} - run "install_method_validation" { command = plan diff --git a/registry/rothnic/modules/opencode/scripts/install.sh b/registry/rothnic/modules/opencode/scripts/install.sh index 4ea8852d5..c3d249695 100644 --- a/registry/rothnic/modules/opencode/scripts/install.sh +++ b/registry/rothnic/modules/opencode/scripts/install.sh @@ -1,15 +1,13 @@ #!/bin/bash set -euo pipefail -# Load any existing PATH customizations -source "$HOME"/.bashrc 2>/dev/null || true +source "$HOME"/.bashrc 2> /dev/null || true command_exists() { command -v "$1" > /dev/null 2>&1 } -# ---------- ARGUMENTS / CONFIG ---------- - +# Configuration ARG_WORKDIR=${ARG_WORKDIR:-"$HOME"} ARG_REPORT_TASKS=${ARG_REPORT_TASKS:-true} ARG_MCP_APP_STATUS_SLUG=${ARG_MCP_APP_STATUS_SLUG:-} @@ -20,11 +18,8 @@ ARG_EXTERNAL_AUTH_ID=${ARG_EXTERNAL_AUTH_ID:-github} ARG_OPENCODE_VERSION=${ARG_OPENCODE_VERSION:-latest} ARG_INSTALL_METHOD=${ARG_INSTALL_METHOD:-npm} -# Version pins for the toolchain NODE_VERSION="${NODE_VERSION:-20.18.0}" -# ---------- NODE INSTALL (NO APT/NVM, SHARED CACHE) ---------- - install_nodejs() { if [ "$ARG_INSTALL_METHOD" != "npm" ]; then echo "ERROR: install_method=${ARG_INSTALL_METHOD} is not supported without the curl installer." @@ -62,7 +57,7 @@ install_nodejs() { export NPM_CONFIG_CACHE="$npm_cache_dir" # Persist PATH + npm cache so start.sh and future shells see it - if ! grep -q "node-v${NODE_VERSION}-${node_distro}/bin" "$HOME/.bashrc" 2>/dev/null; then + if ! grep -q "node-v${NODE_VERSION}-${node_distro}/bin" "$HOME/.bashrc" 2> /dev/null; then { echo "export PATH=\"${node_dir}/bin:\$HOME/.local/bin:\$PATH\"" echo "export NPM_CONFIG_CACHE=\"${npm_cache_dir}\"" @@ -77,8 +72,6 @@ install_nodejs() { echo "✓ Node.js installed via tarball: $(node --version)" } -# ---------- OPENCODE INSTALL (NPM, PINNABLE VERSION) ---------- - install_opencode() { mkdir -p "$HOME/.local/bin" export PATH="$HOME/.local/bin:$PATH" @@ -86,9 +79,9 @@ install_opencode() { if ! command_exists opencode; then echo "Installing OpenCode via npm (version: ${ARG_OPENCODE_VERSION})..." - npm config set prefix "$HOME/.local" >/dev/null 2>&1 || true + npm config set prefix "$HOME/.local" > /dev/null 2>&1 || true - if ! grep -q 'PATH="$HOME/.local/bin:$PATH"' "$HOME/.bashrc" 2>/dev/null; then + if ! grep -q 'PATH="$HOME/.local/bin:$PATH"' "$HOME/.bashrc" 2> /dev/null; then echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$HOME/.bashrc" fi @@ -111,10 +104,8 @@ install_opencode() { fi } -# ---------- GITHUB AUTH (for git/gh, NOT auth.json) ---------- - check_github_authentication() { - echo "Checking GitHub authentication for git/gh use (not Copilot tokens)..." + echo "Checking GitHub authentication..." if [ -n "${GITHUB_TOKEN:-}" ]; then echo "✓ GITHUB_TOKEN already set via environment/module" @@ -124,7 +115,7 @@ check_github_authentication() { if command_exists coder; then if coder external-auth access-token "${ARG_EXTERNAL_AUTH_ID:-github}" > /dev/null 2>&1; then local t - t=$(coder external-auth access-token "${ARG_EXTERNAL_AUTH_ID:-github}" 2>/dev/null || echo "") + t=$(coder external-auth access-token "${ARG_EXTERNAL_AUTH_ID:-github}" 2> /dev/null || echo "") if [ -n "$t" ] && [ "$t" != "null" ]; then export GITHUB_TOKEN="$t" export GH_TOKEN="$t" @@ -144,8 +135,6 @@ check_github_authentication() { return 0 } -# ---------- OPENCODE CONFIG (opencode.json, not auth.json) ---------- - setup_opencode_configurations() { mkdir -p "$ARG_WORKDIR" @@ -209,17 +198,6 @@ setup_opencode_config() { fi } -# ---------- COPILOT / CODER INTEGRATION (NO auth.json FABRICATED) ---------- - -configure_github_copilot_provider() { - # We do NOT fabricate auth.json here anymore. - export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}" - local opencode_data_dir="$XDG_DATA_HOME/opencode" - mkdir -p "$opencode_data_dir" - - echo "OpenCode auth.json will be provided via opencode_auth_config or created by 'opencode auth login'." -} - configure_coder_integration() { if [ "$ARG_REPORT_TASKS" = "true" ] && [ -n "$ARG_MCP_APP_STATUS_SLUG" ]; then echo "Configuring OpenCode task reporting..." @@ -233,13 +211,11 @@ configure_coder_integration() { fi } -# ---------- RUN IT ---------- - +# Main execution install_nodejs install_opencode check_github_authentication setup_opencode_configurations -configure_github_copilot_provider configure_coder_integration echo "OpenCode module setup completed." diff --git a/registry/rothnic/modules/opencode/scripts/start.sh b/registry/rothnic/modules/opencode/scripts/start.sh index 489c5010f..9f25d4058 100644 --- a/registry/rothnic/modules/opencode/scripts/start.sh +++ b/registry/rothnic/modules/opencode/scripts/start.sh @@ -1,7 +1,7 @@ #!/bin/bash set -euo pipefail -source "$HOME"/.bashrc 2>/dev/null || true +source "$HOME"/.bashrc 2> /dev/null || true export PATH="$HOME/.local/bin:$PATH" command_exists() { @@ -13,7 +13,6 @@ ARG_AI_PROMPT=$(echo -n "${ARG_AI_PROMPT:-}" | base64 -d 2> /dev/null || echo "" ARG_SYSTEM_PROMPT=$(echo -n "${ARG_SYSTEM_PROMPT:-}" | base64 -d 2> /dev/null || echo "") ARG_EXTERNAL_AUTH_ID=${ARG_EXTERNAL_AUTH_ID:-github} ARG_RESUME_SESSION=${ARG_RESUME_SESSION:-true} -ARG_OPENCODE_PROVIDER=${ARG_OPENCODE_PROVIDER:-copilot} ARG_OPENCODE_AUTH_CONFIG=$(echo -n "${ARG_OPENCODE_AUTH_CONFIG:-}" | base64 -d 2> /dev/null || echo "") validate_opencode_installation() { @@ -59,7 +58,7 @@ setup_github_authentication() { if [ -z "${GITHUB_TOKEN:-}" ]; then if command_exists coder; then local t - t=$(coder external-auth access-token "${ARG_EXTERNAL_AUTH_ID:-github}" 2>/dev/null || echo "") + t=$(coder external-auth access-token "${ARG_EXTERNAL_AUTH_ID:-github}" 2> /dev/null || echo "") if [ -n "$t" ] && [ "$t" != "null" ]; then export GITHUB_TOKEN="$t" export GH_TOKEN="$t" @@ -72,7 +71,7 @@ setup_github_authentication() { fi # 3) If still no token env, fall back to gh CLI if it's logged in. - if [ -z "${GITHUB_TOKEN:-}" ] && command_exists gh && gh auth status >/dev/null 2>&1; then + if [ -z "${GITHUB_TOKEN:-}" ] && command_exists gh && gh auth status > /dev/null 2>&1; then echo "✓ GitHub CLI auth is available (gh auth status ok)" fi From d6c79cc41520afc7ded108355c40c43dda1eca6c Mon Sep 17 00:00:00 2001 From: Nick Roth Date: Mon, 24 Nov 2025 18:12:44 -0600 Subject: [PATCH 38/42] Fix MCP server format to match OpenCode documentation Changes: - Use 'mcp' key instead of 'mcpServers' in opencode.json config - Update format: type='local' with command as array (not separate command/args) - Update format: 'environment' for env vars (not 'env') - Add remote MCP server example with type='remote' and url - Update main.tf variable description with correct format - Update README examples with correct OpenCode MCP format - Link to OpenCode MCP documentation OpenCode MCP format: - Local: {type: 'local', command: ['npx', '-y', 'server-name'], environment: {...}} - Remote: {type: 'remote', url: 'https://...'} Tests: 15 passed --- registry/rothnic/modules/opencode/README.md | 33 ++++++++++++++----- registry/rothnic/modules/opencode/main.tf | 2 +- .../modules/opencode/scripts/install.sh | 6 ++-- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/registry/rothnic/modules/opencode/README.md b/registry/rothnic/modules/opencode/README.md index 573e7021b..5a3fbef3e 100644 --- a/registry/rothnic/modules/opencode/README.md +++ b/registry/rothnic/modules/opencode/README.md @@ -49,8 +49,8 @@ module "opencode" { opencode_model = "claude-3.7-sonnet" mcp_servers = jsonencode({ filesystem = { - command = "npx" - args = ["-y", "@modelcontextprotocol/server-filesystem", "/home/coder"] + type = "local" + command = ["npx", "-y", "@modelcontextprotocol/server-filesystem", "/home/coder"] } }) } @@ -158,6 +158,8 @@ Or provide API keys via environment variables (see provider docs). **MCP Servers Example:** +OpenCode uses `type: "local"` for local MCP servers with `command` as an array (including all arguments): + ```tf module "opencode" { source = "registry.coder.com/rothnic/opencode/coder" @@ -167,14 +169,14 @@ module "opencode" { opencode_model = "claude-3.7-sonnet" mcp_servers = jsonencode({ - "filesystem" = { - command = "npx" - args = ["-y", "@modelcontextprotocol/server-filesystem", "/workspaces"] + filesystem = { + type = "local" + command = ["npx", "-y", "@modelcontextprotocol/server-filesystem", "/workspaces"] } - "github" = { - command = "npx" - args = ["-y", "@modelcontextprotocol/server-github"] - env = { + github = { + type = "local" + command = ["npx", "-y", "@modelcontextprotocol/server-github"] + environment = { GITHUB_TOKEN = var.github_token } } @@ -182,6 +184,19 @@ module "opencode" { } ``` +For remote MCP servers, use `type: "remote"` with a `url`: + +```tf +mcp_servers = jsonencode({ + context7 = { + type = "remote" + url = "https://mcp.context7.com/mcp" + } +}) +``` + +See [OpenCode MCP documentation](https://opencode.ai/docs/mcp-servers/) for more details. + ### Advanced | Variable | Description | Default | diff --git a/registry/rothnic/modules/opencode/main.tf b/registry/rothnic/modules/opencode/main.tf index 2482a8c5b..707d08aa8 100644 --- a/registry/rothnic/modules/opencode/main.tf +++ b/registry/rothnic/modules/opencode/main.tf @@ -45,7 +45,7 @@ variable "opencode_model" { variable "mcp_servers" { type = string - description = "MCP servers configuration as JSON string. Will be merged into the mcpServers section of opencode.json. Example: '{\"filesystem\":{\"type\":\"stdio\",\"command\":\"npx\",\"args\":[\"-y\",\"@modelcontextprotocol/server-filesystem\",\"/workspaces\"]}}'" + description = "MCP servers configuration as JSON string. Will be merged into the 'mcp' section of opencode.json. OpenCode format uses type='local' with command as array. Example: '{\"filesystem\":{\"type\":\"local\",\"command\":[\"npx\",\"-y\",\"@modelcontextprotocol/server-filesystem\",\"/workspaces\"]}}'" default = "" } diff --git a/registry/rothnic/modules/opencode/scripts/install.sh b/registry/rothnic/modules/opencode/scripts/install.sh index c3d249695..51e1997ab 100644 --- a/registry/rothnic/modules/opencode/scripts/install.sh +++ b/registry/rothnic/modules/opencode/scripts/install.sh @@ -176,14 +176,14 @@ setup_opencode_config() { }') fi - # Add MCP servers if specified + # Add MCP servers if specified (OpenCode uses 'mcp' key, not 'mcpServers') if [ -n "$ARG_MCP_SERVERS" ]; then echo " Adding MCP servers configuration..." - # Merge MCP servers into config + # Merge MCP servers into config under 'mcp' key local mcp_config mcp_config=$(echo "$ARG_MCP_SERVERS" | jq '.') if [ $? -eq 0 ] && [ -n "$mcp_config" ]; then - config=$(echo "$config" | jq --argjson mcp "$mcp_config" '. + {"mcpServers": $mcp}') + config=$(echo "$config" | jq --argjson mcp "$mcp_config" '. + {"mcp": $mcp}') else echo " ⚠ Warning: Invalid MCP servers JSON, skipping" fi From d07daaf1f7522e676fceabcb3dc85e9da07752d0 Mon Sep 17 00:00:00 2001 From: Nick Roth Date: Mon, 24 Nov 2025 18:23:45 -0600 Subject: [PATCH 39/42] Update README with subdomain wildcard DNS requirement - Add link to wildcard DNS setup docs in subdomain variable description - Add commented subdomain example in Complete Template Example - Clarifies that subdomain=true requires wildcard DNS configuration --- registry/rothnic/modules/opencode/README.md | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/registry/rothnic/modules/opencode/README.md b/registry/rothnic/modules/opencode/README.md index 5a3fbef3e..2c2f951af 100644 --- a/registry/rothnic/modules/opencode/README.md +++ b/registry/rothnic/modules/opencode/README.md @@ -140,14 +140,16 @@ Or provide API keys via environment variables (see provider docs). ### UI & Apps -| Variable | Description | Default | -| ---------------------- | ---------------------------- | ------------------ | -| `web_app_display_name` | Display name in Coder UI | `"OpenCode"` | -| `order` | App position in UI | `null` | -| `group` | App group name | `null` | -| `icon` | App icon path | `"/icon/code.svg"` | -| `subdomain` | Use subdomain for app access | `false` | -| `cli_app` | Create CLI app entry | `false` | +| Variable | Description | Default | +| ---------------------- | -------------------------------------------------------------------------- | ------------------ | +| `web_app_display_name` | Display name in Coder UI | `"OpenCode"` | +| `order` | App position in UI | `null` | +| `group` | App group name | `null` | +| `icon` | App icon path | `"/icon/code.svg"` | +| `subdomain` | Use subdomain for app access (requires [wildcard DNS][wildcard-dns-setup]) | `false` | +| `cli_app` | Create CLI app entry | `false` | + +[wildcard-dns-setup]: https://coder.com/docs/admin/setup#wildcard-access-url ### Model & MCP Configuration @@ -275,6 +277,9 @@ module "opencode" { # Enable task reporting for Coder UI integration report_tasks = true + # Use subdomain for better app routing (requires wildcard DNS) + # subdomain = true + # Display settings order = 1 web_app_display_name = "OpenCode AI" From cae8e163e4f74cc2b4432795be46bbf75f597627 Mon Sep 17 00:00:00 2001 From: Nick Roth Date: Mon, 24 Nov 2025 18:28:13 -0600 Subject: [PATCH 40/42] Add OpenCode icon and minimal agent task example - Add opencode.png icon from VS Code marketplace - Update icon default from code.svg to opencode.png - Add minimal-agent-task example for testing with current branch - Update README UI & Apps table with correct icon default --- .icons/opencode.png | Bin 0 -> 2989 bytes registry/rothnic/modules/opencode/README.md | 18 +-- .../examples/minimal-agent-task/main.tf | 135 ++++++++++++++++++ registry/rothnic/modules/opencode/main.tf | 2 +- 4 files changed, 145 insertions(+), 10 deletions(-) create mode 100644 .icons/opencode.png create mode 100644 registry/rothnic/modules/opencode/examples/minimal-agent-task/main.tf diff --git a/.icons/opencode.png b/.icons/opencode.png new file mode 100644 index 0000000000000000000000000000000000000000..b7436235d509af99aa36adc61ea815f8070effe6 GIT binary patch literal 2989 zcmeAS@N?(olHy`uVBq!ia0y~yV2S`?4mP03zO)&4fD~teM`SSr1Gg{;GcwGYBLNg- zEDmyaVpw-h<|UBBlJ4m1$iT3%pZiZDD^Qh&r;B4q1>@VB2XmDi1spEUn#x?jwuDJg zfJsTnMZ=}3Bj;N6w}W%KywITKNrZd{R?!VuB|2_M@PphoYrN<51T=S!_{cz&6R73w+_0M_l z7w(OV&)UA-AgBER6GI254nxC?QO0N>&?_a}xP5?scZ^=K)!eq{74`M!1?->*3E6Hk&$9thJUMXzcuSWp4@Oh`R5E!J^n+9Wi(%omPw-pGu~YAmo@O7l1<5)`gOoI hG+%0jXPU1sgBFm@0mLA1DR?r7@^tlcS?83{1OUsH*ZTkf literal 0 HcmV?d00001 diff --git a/registry/rothnic/modules/opencode/README.md b/registry/rothnic/modules/opencode/README.md index 2c2f951af..78bc3497e 100644 --- a/registry/rothnic/modules/opencode/README.md +++ b/registry/rothnic/modules/opencode/README.md @@ -1,7 +1,7 @@ --- display_name: OpenCode description: AI-powered terminal coding agent with support for GitHub Copilot, Anthropic, and OpenAI -icon: ../../../../.icons/code.svg +icon: ../../../../.icons/opencode.png maintainer_github: rothnic verified: false tags: [agent, ai, opencode, coding-assistant, copilot, terminal] @@ -140,14 +140,14 @@ Or provide API keys via environment variables (see provider docs). ### UI & Apps -| Variable | Description | Default | -| ---------------------- | -------------------------------------------------------------------------- | ------------------ | -| `web_app_display_name` | Display name in Coder UI | `"OpenCode"` | -| `order` | App position in UI | `null` | -| `group` | App group name | `null` | -| `icon` | App icon path | `"/icon/code.svg"` | -| `subdomain` | Use subdomain for app access (requires [wildcard DNS][wildcard-dns-setup]) | `false` | -| `cli_app` | Create CLI app entry | `false` | +| Variable | Description | Default | +| ---------------------- | -------------------------------------------------------------------------- | ---------------------- | +| `web_app_display_name` | Display name in Coder UI | `"OpenCode"` | +| `order` | App position in UI | `null` | +| `group` | App group name | `null` | +| `icon` | App icon path | `"/icon/opencode.png"` | +| `subdomain` | Use subdomain for app access (requires [wildcard DNS][wildcard-dns-setup]) | `false` | +| `cli_app` | Create CLI app entry | `false` | [wildcard-dns-setup]: https://coder.com/docs/admin/setup#wildcard-access-url diff --git a/registry/rothnic/modules/opencode/examples/minimal-agent-task/main.tf b/registry/rothnic/modules/opencode/examples/minimal-agent-task/main.tf new file mode 100644 index 000000000..69653e025 --- /dev/null +++ b/registry/rothnic/modules/opencode/examples/minimal-agent-task/main.tf @@ -0,0 +1,135 @@ +# Minimal OpenCode Agent Task Example +# +# This is a minimal template for testing the opencode module with agent tasks. +# Use this to test AI-driven coding workflows without additional tooling. +# +# Usage: +# coder templates push opencode-test +# coder create opencode-test --parameter git_repo="https://github.com/your/repo" +# # Then use the OpenCode web app or CLI to interact with the agent + +terraform { + required_providers { + coder = { + source = "coder/coder" + } + docker = { + source = "kreuzwerker/docker" + } + } +} + +provider "coder" {} +provider "docker" {} + +data "coder_provisioner" "me" {} +data "coder_workspace" "me" {} +data "coder_workspace_owner" "me" {} + +# Optional: Git repo to clone for context +variable "git_repo" { + description = "Git repository URL to clone (optional)" + type = string + default = "" +} + +# Agent task for AI prompts +data "coder_parameter" "ai_prompt" { + type = "string" + name = "AI Prompt" + default = "" + description = "Prompt for the AI coding agent (used for agent tasks)" + display_name = "AI Prompt" + mutable = true +} + +data "coder_workspace_tags" "custom_workspace_tags" { + tags = { + "cluster" = "dev" + } +} + +resource "coder_agent" "main" { + arch = data.coder_provisioner.me.arch + os = "linux" + dir = "/workspace" + + display_apps { + vscode = false + vscode_insiders = false + web_terminal = true + ssh_helper = true + } +} + +# OpenCode module from current branch +module "opencode" { + # For testing with the current branch: + source = "git::https://github.com/coder/registry.git//registry/rothnic/modules/opencode?ref=claude/review-module-guidelines-014yytiyG8n6Rj4V8BbxZb2B" + + # After merge, use: + # source = "registry.coder.com/rothnic/opencode/coder" + + agent_id = coder_agent.main.id + workdir = "/workspace" + + # Enable web app access + subdomain = true + + # Uncomment to configure a specific model: + # opencode_model = "claude-sonnet-4-20250514" + + # Uncomment to add MCP servers: + # mcp_servers = jsonencode({ + # filesystem = { + # type = "local" + # command = ["npx", "-y", "@anthropic/mcp-server-filesystem", "/workspace"] + # } + # }) +} + +# Optional: Clone git repo for context +module "git_clone" { + count = var.git_repo != "" ? 1 : 0 + source = "registry.coder.com/modules/git-clone/coder" + version = "~> 1.0" + agent_id = coder_agent.main.id + url = var.git_repo + path = "/workspace" +} + +resource "docker_image" "workspace" { + name = "codercom/enterprise-base:ubuntu" +} + +resource "docker_container" "workspace" { + count = data.coder_workspace.me.start_count + image = docker_image.workspace.image_id + name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}" + + hostname = data.coder_workspace.me.name + command = ["sh", "-c", coder_agent.main.init_script] + + env = [ + "CODER_AGENT_TOKEN=${coder_agent.main.token}", + ] + + host { + host = "host.docker.internal" + ip = "host-gateway" + } + + volumes { + container_path = "/workspace" + volume_name = docker_volume.workspace.name + read_only = false + } +} + +resource "docker_volume" "workspace" { + name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}" + + lifecycle { + ignore_changes = all + } +} diff --git a/registry/rothnic/modules/opencode/main.tf b/registry/rothnic/modules/opencode/main.tf index 707d08aa8..c87eaf037 100644 --- a/registry/rothnic/modules/opencode/main.tf +++ b/registry/rothnic/modules/opencode/main.tf @@ -125,7 +125,7 @@ variable "group" { variable "icon" { type = string description = "The icon to use for the app." - default = "/icon/code.svg" + default = "/icon/opencode.png" } variable "web_app_display_name" { From 632df5063caeedf67104d7e93cc463e1cddf05a1 Mon Sep 17 00:00:00 2001 From: Nick Roth Date: Mon, 24 Nov 2025 20:34:39 -0600 Subject: [PATCH 41/42] Update minimal agent task example to match Coder template structure - Add workspace presets with system_prompt - Include auth.json instructions for GitHub Copilot - Add proper Docker volume and container labels - Add agent metadata for CPU/RAM/disk monitoring - Configure Git from workspace owner - Use same container image parameter pattern as official templates --- .../examples/minimal-agent-task/main.tf | 239 +++++++++++------- 1 file changed, 153 insertions(+), 86 deletions(-) diff --git a/registry/rothnic/modules/opencode/examples/minimal-agent-task/main.tf b/registry/rothnic/modules/opencode/examples/minimal-agent-task/main.tf index 69653e025..580bb98de 100644 --- a/registry/rothnic/modules/opencode/examples/minimal-agent-task/main.tf +++ b/registry/rothnic/modules/opencode/examples/minimal-agent-task/main.tf @@ -1,13 +1,3 @@ -# Minimal OpenCode Agent Task Example -# -# This is a minimal template for testing the opencode module with agent tasks. -# Use this to test AI-driven coding workflows without additional tooling. -# -# Usage: -# coder templates push opencode-test -# coder create opencode-test --parameter git_repo="https://github.com/your/repo" -# # Then use the OpenCode web app or CLI to interact with the agent - terraform { required_providers { coder = { @@ -19,117 +9,194 @@ terraform { } } -provider "coder" {} +# This template requires a valid Docker socket +# You can reference Kubernetes/VM example templates and adapt: +# see: https://registry.coder.com/templates provider "docker" {} data "coder_provisioner" "me" {} data "coder_workspace" "me" {} data "coder_workspace_owner" "me" {} -# Optional: Git repo to clone for context -variable "git_repo" { - description = "Git repository URL to clone (optional)" - type = string - default = "" -} - -# Agent task for AI prompts -data "coder_parameter" "ai_prompt" { - type = "string" - name = "AI Prompt" - default = "" - description = "Prompt for the AI coding agent (used for agent tasks)" - display_name = "AI Prompt" - mutable = true -} - -data "coder_workspace_tags" "custom_workspace_tags" { - tags = { - "cluster" = "dev" - } -} - -resource "coder_agent" "main" { - arch = data.coder_provisioner.me.arch - os = "linux" - dir = "/workspace" - - display_apps { - vscode = false - vscode_insiders = false - web_terminal = true - ssh_helper = true - } -} - -# OpenCode module from current branch +# OpenCode module handles automatic task reporting via agentapi +# For testing with current branch: module "opencode" { - # For testing with the current branch: + count = data.coder_workspace.me.start_count source = "git::https://github.com/coder/registry.git//registry/rothnic/modules/opencode?ref=claude/review-module-guidelines-014yytiyG8n6Rj4V8BbxZb2B" # After merge, use: - # source = "registry.coder.com/rothnic/opencode/coder" - - agent_id = coder_agent.main.id - workdir = "/workspace" + # source = "registry.coder.com/rothnic/opencode/coder" + # version = "~> 1.0" - # Enable web app access + agent_id = coder_agent.main.id + workdir = "/home/coder/projects" + order = 999 + ai_prompt = data.coder_parameter.ai_prompt.value subdomain = true - # Uncomment to configure a specific model: + # GitHub Copilot Authentication: + # 1. Run `opencode auth login` locally and select GitHub Copilot + # 2. Copy ~/.local/share/opencode/auth.json to opencode-auth.json + # 3. Uncomment the line below: + # opencode_auth_config = file("${path.module}/opencode-auth.json") + + # Configure model (optional): # opencode_model = "claude-sonnet-4-20250514" - # Uncomment to add MCP servers: + # MCP Servers (optional): # mcp_servers = jsonencode({ # filesystem = { # type = "local" - # command = ["npx", "-y", "@anthropic/mcp-server-filesystem", "/workspace"] + # command = ["npx", "-y", "@anthropic/mcp-server-filesystem", "/home/coder/projects"] # } # }) } -# Optional: Clone git repo for context -module "git_clone" { - count = var.git_repo != "" ? 1 : 0 - source = "registry.coder.com/modules/git-clone/coder" - version = "~> 1.0" - agent_id = coder_agent.main.id - url = var.git_repo - path = "/workspace" +# Workspace presets for different use cases +# See https://coder.com/docs/admin/templates/extending-templates/parameters#workspace-presets +data "coder_workspace_preset" "default" { + name = "Default OpenCode Workspace" + default = true + parameters = { + "system_prompt" = <<-EOT + You are a helpful coding assistant running inside a Coder workspace. + Stay on track and feel free to debug, but when the original plan fails, + do not choose a different route/architecture without checking the user first. + EOT + "container_image" = "codercom/enterprise-base:ubuntu" + } } -resource "docker_image" "workspace" { - name = "codercom/enterprise-base:ubuntu" +# Parameters (set via preset or manually) +data "coder_parameter" "ai_prompt" { + type = "string" + name = "AI Prompt" + default = "" + description = "Write a prompt for OpenCode" + display_name = "AI Prompt" + mutable = true } -resource "docker_container" "workspace" { - count = data.coder_workspace.me.start_count - image = docker_image.workspace.image_id - name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}" +data "coder_parameter" "system_prompt" { + name = "system_prompt" + display_name = "System Prompt" + type = "string" + form_type = "textarea" + description = "System prompt for the agent with generalized instructions" + mutable = false + default = "" +} - hostname = data.coder_workspace.me.name - command = ["sh", "-c", coder_agent.main.init_script] +data "coder_parameter" "container_image" { + name = "container_image" + display_name = "Container Image" + type = "string" + default = "codercom/enterprise-base:ubuntu" + mutable = false +} - env = [ - "CODER_AGENT_TOKEN=${coder_agent.main.token}", - ] +resource "coder_agent" "main" { + arch = data.coder_provisioner.me.arch + os = "linux" + startup_script = <<-EOT + set -e + # Prepare user home with default files on first start + if [ ! -f ~/.init_done ]; then + cp -rT /etc/skel ~ + touch ~/.init_done + fi + # Create projects directory + mkdir -p /home/coder/projects + EOT + + # Git configuration from workspace owner + env = { + GIT_AUTHOR_NAME = coalesce(data.coder_workspace_owner.me.full_name, data.coder_workspace_owner.me.name) + GIT_AUTHOR_EMAIL = "${data.coder_workspace_owner.me.email}" + GIT_COMMITTER_NAME = coalesce(data.coder_workspace_owner.me.full_name, data.coder_workspace_owner.me.name) + GIT_COMMITTER_EMAIL = "${data.coder_workspace_owner.me.email}" + } - host { - host = "host.docker.internal" - ip = "host-gateway" + metadata { + display_name = "CPU Usage" + key = "0_cpu_usage" + script = "coder stat cpu" + interval = 10 + timeout = 1 } - volumes { - container_path = "/workspace" - volume_name = docker_volume.workspace.name - read_only = false + metadata { + display_name = "RAM Usage" + key = "1_ram_usage" + script = "coder stat mem" + interval = 10 + timeout = 1 } -} -resource "docker_volume" "workspace" { - name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}" + metadata { + display_name = "Home Disk" + key = "3_home_disk" + script = "coder stat disk --path $${HOME}" + interval = 60 + timeout = 1 + } +} +resource "docker_volume" "home_volume" { + name = "coder-${data.coder_workspace.me.id}-home" lifecycle { ignore_changes = all } + labels { + label = "coder.owner" + value = data.coder_workspace_owner.me.name + } + labels { + label = "coder.owner_id" + value = data.coder_workspace_owner.me.id + } + labels { + label = "coder.workspace_id" + value = data.coder_workspace.me.id + } + labels { + label = "coder.workspace_name_at_creation" + value = data.coder_workspace.me.name + } +} + +resource "docker_container" "workspace" { + count = data.coder_workspace.me.start_count + image = data.coder_parameter.container_image.value + name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}" + hostname = data.coder_workspace.me.name + user = "coder" + # Use the docker gateway if the access URL is 127.0.0.1 + entrypoint = ["sh", "-c", replace(coder_agent.main.init_script, "/localhost|127\\.0\\.0\\.1/", "host.docker.internal")] + env = ["CODER_AGENT_TOKEN=${coder_agent.main.token}"] + host { + host = "host.docker.internal" + ip = "host-gateway" + } + volumes { + container_path = "/home/coder" + volume_name = docker_volume.home_volume.name + read_only = false + } + labels { + label = "coder.owner" + value = data.coder_workspace_owner.me.name + } + labels { + label = "coder.owner_id" + value = data.coder_workspace_owner.me.id + } + labels { + label = "coder.workspace_id" + value = data.coder_workspace.me.id + } + labels { + label = "coder.workspace_name" + value = data.coder_workspace.me.name + } } From 6b2a9157f23cbba91f54a43822884abc6a7bb81f Mon Sep 17 00:00:00 2001 From: Nick Roth Date: Mon, 24 Nov 2025 21:07:34 -0600 Subject: [PATCH 42/42] Fix install.sh: Use HOME as fallback instead of /workspaces The DEV_ROOT default was /workspaces which may not exist or be writable. Changed to use $HOME as the fallback for tool installation directory. --- registry/rothnic/modules/opencode/scripts/install.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/registry/rothnic/modules/opencode/scripts/install.sh b/registry/rothnic/modules/opencode/scripts/install.sh index 51e1997ab..991daf95b 100644 --- a/registry/rothnic/modules/opencode/scripts/install.sh +++ b/registry/rothnic/modules/opencode/scripts/install.sh @@ -28,7 +28,8 @@ install_nodejs() { fi # Shared tool root across workspaces on this host - local dev_root="${DEV_ROOT:-/workspaces}" + # Use HOME as fallback since /workspaces may not exist or be writable + local dev_root="${DEV_ROOT:-$HOME}" local tool_root="${TOOL_ROOT:-$dev_root/.coder-tools}" local node_distro="linux-x64" local node_tarball="node-v${NODE_VERSION}-${node_distro}.tar.xz"