-
Notifications
You must be signed in to change notification settings - Fork 57
feat: refactor claude code to use agentapi module #402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
35C4n0r
wants to merge
13
commits into
coder:main
Choose a base branch
from
35C4n0r:feat-claude-agentapi
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+832
−666
Open
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
36cebbd
feat: refactor claude code to use agentapi module
35C4n0r 6c5fbee
ref: bun fmt
35C4n0r 02c8c4a
Merge branch 'main' into feat-claude-agentapi
35C4n0r 9411582
feat: additional documentation
35C4n0r 7fb8b86
ref: bun fmt
35C4n0r 2b23801
feat: need to test if this works
35C4n0r 4d53c76
feat: option to disable agentapi
35C4n0r fb2578c
revert changes
35C4n0r f5970dd
fix: fix tests
35C4n0r e51b46f
fix: fix tests
35C4n0r 76fbed1
feat: add default
35C4n0r da5a5ee
feat: remove bedrock flag
35C4n0r edbf22d
feat: remove bedrock flag
35C4n0r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,160 @@ | ||
--- | ||
display_name: Claude Code | ||
description: Run Claude Code in your workspace | ||
description: Run the Claude Code agent in your workspace to generate code and perform tasks. | ||
icon: ../../../../.icons/claude.svg | ||
verified: true | ||
tags: [agent, claude-code, ai, tasks] | ||
tags: [agent, claude-code, ai, tasks, anthropic] | ||
--- | ||
|
||
# Claude Code | ||
|
||
Run the [Claude Code](https://docs.anthropic.com/en/docs/agents-and-tools/claude-code/overview) agent in your workspace to generate code and perform tasks. | ||
Run the [Claude Code](https://docs.anthropic.com/en/docs/agents-and-tools/claude-code/overview) agent in your workspace to generate code and perform tasks. This module integrates with [AgentAPI](https://github.com/coder/agentapi) for task reporting in the Coder UI. | ||
|
||
```tf | ||
module "claude-code" { | ||
source = "registry.coder.com/coder/claude-code/coder" | ||
version = "2.2.0" | ||
agent_id = coder_agent.example.id | ||
folder = "/home/coder" | ||
install_claude_code = true | ||
claude_code_version = "latest" | ||
source = "registry.coder.com/coder/claude-code/coder" | ||
version = "3.0.0" | ||
agent_id = coder_agent.example.id | ||
workdir = "/home/coder/project" | ||
} | ||
``` | ||
|
||
> **Security Notice**: This module uses the [`--dangerously-skip-permissions`](https://docs.anthropic.com/en/docs/claude-code/cli-usage#cli-flags) flag when running Claude Code. This flag | ||
> bypasses standard permission checks and allows Claude Code broader access to your system than normally permitted. While | ||
> this enables more functionality, it also means Claude Code can potentially execute commands with the same privileges as | ||
> the user running it. Use this module _only_ in trusted environments and be aware of the security implications. | ||
> [!WARNING] | ||
> **Security Notice**: This module uses the `--dangerously-skip-permissions` flag when running Claude Code tasks. This flag bypasses standard permission checks and allows Claude Code broader access to your system than normally permitted. While this enables more functionality, it also means Claude Code can potentially execute commands with the same privileges as the user running it. Use this module _only_ in trusted environments and be aware of the security implications. | ||
## Prerequisites | ||
|
||
- You must add the [Coder Login](https://registry.coder.com/modules/coder-login) module to your template | ||
|
||
The `codercom/oss-dogfood:latest` container image can be used for testing on container-based workspaces. | ||
- You must add the [Coder Login](https://registry.coder.com/modules/coder-login) module to your template for task reporting features. | ||
35C4n0r marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- An **Anthropic API key** is required for tasks. You can get one from the [Anthropic Console](https://console.anthropic.com/dashboard). | ||
35C4n0r marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Examples | ||
|
||
### Run in the background and report tasks (Experimental) | ||
### Usage with Tasks and Advanced Configuration | ||
|
||
> This functionality is in early access as of Coder v2.21 and is still evolving. | ||
> For now, we recommend testing it in a demo or staging environment, | ||
> rather than deploying to production | ||
> | ||
> Learn more in [the Coder documentation](https://coder.com/docs/tutorials/ai-agents) | ||
> | ||
> Join our [Discord channel](https://discord.gg/coder) or | ||
> [contact us](https://coder.com/contact) to get help or share feedback. | ||
This example shows how to configure the Claude Code module with a task prompt, API key, and other custom settings. | ||
|
||
```tf | ||
variable "anthropic_api_key" { | ||
type = string | ||
description = "The Anthropic API key" | ||
description = "The Anthropic API key." | ||
sensitive = true | ||
} | ||
module "coder-login" { | ||
count = data.coder_workspace.me.start_count | ||
source = "registry.coder.com/coder/coder-login/coder" | ||
version = "1.0.15" | ||
agent_id = coder_agent.example.id | ||
} | ||
data "coder_parameter" "ai_prompt" { | ||
data "coder_parameter" "task_prompt" { | ||
type = "string" | ||
name = "AI Prompt" | ||
name = "AI Task Prompt" | ||
default = "" | ||
description = "Write a prompt for Claude Code" | ||
description = "Initial task prompt for Claude Code." | ||
mutable = true | ||
} | ||
35C4n0r marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Set the prompt and system prompt for Claude Code via environment variables | ||
resource "coder_agent" "main" { | ||
# ... | ||
env = { | ||
CODER_MCP_CLAUDE_API_KEY = var.anthropic_api_key # or use a coder_parameter | ||
CODER_MCP_CLAUDE_TASK_PROMPT = data.coder_parameter.ai_prompt.value | ||
CODER_MCP_APP_STATUS_SLUG = "claude-code" | ||
CODER_MCP_CLAUDE_SYSTEM_PROMPT = <<-EOT | ||
You are a helpful assistant that can help with code. | ||
EOT | ||
} | ||
module "coder-login" { | ||
count = data.coder_workspace.me.start_count | ||
source = "registry.coder.com/coder/coder-login/coder" | ||
version = "1.0.31" # Use a recent version | ||
agent_id = coder_agent.example.id | ||
35C4n0r marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
module "claude-code" { | ||
count = data.coder_workspace.me.start_count | ||
source = "registry.coder.com/coder/claude-code/coder" | ||
version = "2.2.0" | ||
agent_id = coder_agent.example.id | ||
folder = "/home/coder" | ||
install_claude_code = true | ||
claude_code_version = "1.0.40" | ||
# Enable experimental features | ||
experiment_report_tasks = true | ||
source = "registry.coder.com/coder/claude-code/coder" | ||
version = "3.0.0" | ||
agent_id = coder_agent.example.id | ||
workdir = "/home/coder/project" | ||
# --- Authentication --- | ||
claude_api_key = var.anthropic_api_key # required for tasks | ||
# --- Versioning --- | ||
claude_code_version = "1.0.82" # Pin to a specific version | ||
agentapi_version = "v0.6.1" | ||
# --- Task Configuration --- | ||
task_prompt = data.coder_parameter.task_prompt.value | ||
continue = true # will fail in a new workspace with no conversation/session to continue | ||
model = "sonnet" | ||
# --- Permissions & Tools --- | ||
permission_mode = "plan" | ||
# --- MCP Configuration --- | ||
mcp = <<-EOF | ||
{ | ||
"mcpServers": { | ||
"my-custom-tool": { | ||
"command": "my-tool-server" | ||
"args": ["--port", "8080"] | ||
} | ||
} | ||
} | ||
EOF | ||
} | ||
``` | ||
35C4n0r marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Run standalone | ||
### Standalone Mode | ||
|
||
Run Claude Code as a standalone app in your workspace. This will install Claude Code and run it without any task reporting to the Coder UI. | ||
Run Claude Code as a standalone CLI in your workspace without task reporting to the Coder UI. | ||
|
||
```tf | ||
module "claude-code" { | ||
source = "registry.coder.com/coder/claude-code/coder" | ||
version = "2.2.0" | ||
version = "3.0.0" | ||
agent_id = coder_agent.example.id | ||
folder = "/home/coder" | ||
workdir = "/home/coder" | ||
install_claude_code = true | ||
claude_code_version = "latest" | ||
report_tasks = false | ||
cli_app = true | ||
} | ||
``` | ||
|
||
# Icon is not available in Coder v2.20 and below, so we'll use a custom icon URL | ||
icon = "https://registry.npmmirror.com/@lobehub/icons-static-png/1.24.0/files/dark/claude-color.png" | ||
## Environment Variables | ||
|
||
The module can be further configured using environment variables set on the Coder agent. This allows for more advanced or dynamic setups. | ||
|
||
| Variable | Description | Default | | ||
| -------------------------------- | ----------------------------------------------------------------------------- | ------------------------------ | | ||
| `CLAUDE_API_KEY` | Your Anthropic API key. | `""` | | ||
| `CODER_MCP_CLAUDE_SYSTEM_PROMPT` | A custom system prompt for Claude. | "Send a task status update..." | | ||
| `CODER_MCP_CLAUDE_CODER_PROMPT` | A custom coder prompt for Claude. | `""` | | ||
| `CODER_MCP_CLAUDE_CONFIG_PATH` | Path to the Claude configuration file. | `~/.claude.json` | | ||
| `CODER_MCP_CLAUDE_MD_PATH` | Path to a `CLAUDE.md` file for project-specific instructions. | `~/.claude/CLAUDE.md` | | ||
| `CLAUDE_CODE_USE_BEDROCK` | Set to `"true"` to use Amazon Bedrock. Requires additional AWS configuration. | `""` | | ||
35C4n0r marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
An example of setting these on a `coder_agent` resource: | ||
|
||
```tf | ||
resource "coder_agent" "main" { | ||
# ... other agent config | ||
env = { | ||
CLAUDE_API_KEY = var.anthropic_api_key | ||
CODER_MCP_CLAUDE_SYSTEM_PROMPT = <<-EOT | ||
You are a helpful assistant that can help with code. | ||
EOT | ||
35C4n0r marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
``` | ||
|
||
## Troubleshooting | ||
|
||
The module will create log files in the workspace's `~/.claude-module` directory. If you run into any issues, look at them for more information. | ||
If you encounter any issues, check the log files in the `~/.claude-module` directory within your workspace for detailed information. | ||
|
||
```bash | ||
# Installation logs | ||
cat ~/.claude-module/install.log | ||
|
||
# Startup logs | ||
cat ~/.claude-module/agentapi-start.log | ||
|
||
# Pre/post install script logs | ||
cat ~/.claude-module/pre_install.log | ||
cat ~/.claude-module/post_install.log | ||
``` | ||
|
||
> [!NOTE] | ||
> To use tasks with Claude Code, you must provide an `anthropic_api_key`. It's recommended to use a `coder_parameter` for the `task_prompt` to allow users to input tasks from the Coder UI. The `workdir` variable is required and specifies the directory where Claude Code will run. | ||
## References | ||
|
||
- [Claude Code Documentation](https://docs.anthropic.com/en/docs/agents-and-tools/claude-code/overview) | ||
- [AgentAPI Documentation](https://github.com/coder/agentapi) | ||
- [Coder AI Agents Guide](https://coder.com/docs/tutorials/ai-agents) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.