Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 179 additions & 0 deletions docs/claude-code-integration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
# Claude Code Integration

This document describes how to use Claude Code CLI integration with Roo Code.

## Overview

The Claude Code integration allows Roo Code to use the Claude Code CLI instead of directly calling the Anthropic API. This provides several benefits:

- **Local CLI Control**: Use your locally installed Claude Code CLI
- **Custom Configuration**: Configure Claude Code CLI path and settings
- **Consistent Experience**: Same interface as other providers
- **No API Key Required**: Uses Claude Code's authentication

## Prerequisites

1. **Install Claude Code CLI**

```bash
# Follow Claude Code installation instructions
# Ensure 'claude' command is available in PATH
```

2. **Verify Installation**
```bash
claude --version
```

## Configuration

### 1. Select Provider

1. Open Roo Code settings
2. Go to "Providers" section
3. Select "Claude Code" from the API Provider dropdown

### 2. Configure CLI Path

- **Default**: `claude` (uses system PATH)
- **Custom Path**: Specify full path to Claude Code CLI
```
/usr/local/bin/claude
/path/to/custom/claude
```

### 3. Select Model

Choose from available Claude Code models:

- `claude-sonnet-4-20250514` (default)
- `claude-opus-4-20250514`
- `claude-3-7-sonnet-20250219`
- `claude-3-5-sonnet-20241022`
- `claude-3-5-haiku-20241022`

## Usage

Once configured, Claude Code integration works seamlessly:

1. **Start Conversation**: Ask Roo Code any question
2. **CLI Execution**: Roo Code executes Claude Code CLI
3. **Streaming Response**: Receive real-time streaming responses
4. **Usage Tracking**: Monitor token usage and costs

## Verification

To verify Claude Code is being used:

### Console Logs (Development)

Open Developer Tools β†’ Console and look for:

```
Claude Code Handler: Starting Claude Code CLI execution
Claude Code CLI: Process started with PID: 12345
```

### System Process Monitoring

```bash
# Linux/macOS
ps aux | grep claude

# Windows
tasklist | findstr claude
```

### Test Script

Run the integration test:

```bash
npm test -- claude-code.spec.ts
```

## Troubleshooting

### Common Issues

1. **"claude: command not found"**

- Solution: Install Claude Code CLI or specify full path

2. **"Permission denied"**

- Solution: Make Claude Code CLI executable

```bash
chmod +x /path/to/claude
```

3. **Model not available**
- Solution: Check Claude Code CLI version and available models
```bash
claude --help
```

### Debug Mode

For development debugging, check console logs in Developer Tools.

## Implementation Details

### Architecture

```
Roo Code β†’ ClaudeCodeHandler β†’ runClaudeCode() β†’ Claude Code CLI
```

### Key Components

- **ClaudeCodeHandler**: Main API handler class
- **runClaudeCode()**: CLI execution function
- **ClaudeCodeMessage**: Type definitions for CLI output
- **Stream Processing**: Real-time response handling

### CLI Arguments

The integration uses these Claude Code CLI arguments:

```bash
claude -p <messages> --system-prompt <prompt> --verbose --output-format stream-json --max-turns 1 --model <model>
```

## API Compatibility

The Claude Code integration maintains full compatibility with Roo Code's provider interface:

- βœ… Streaming responses
- βœ… Token usage tracking
- βœ… Cost calculation
- βœ… Error handling
- βœ… Model selection
- βœ… System prompts

## Security Considerations

- Claude Code CLI runs locally with user permissions
- No API keys stored in Roo Code settings
- Authentication handled by Claude Code CLI
- Process isolation and error handling

## Contributing

To contribute to Claude Code integration:

1. **Tests**: Run `npm test -- claude-code.test.ts`
2. **Types**: Update types in `packages/types/src/providers/claude-code.ts`
3. **Handler**: Modify `src/api/providers/claude-code.ts`
4. **UI**: Update `webview-ui/src/components/settings/providers/ClaudeCode.tsx`

## Support

For issues with Claude Code integration:

1. Check Claude Code CLI installation
2. Verify configuration settings
3. Review console logs for errors
4. Test with integration script
5. Report issues with detailed logs
7 changes: 7 additions & 0 deletions packages/types/src/provider-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { codebaseIndexProviderSchema } from "./codebase-index.js"

export const providerNames = [
"anthropic",
"claude-code",
"glama",
"openrouter",
"bedrock",
Expand Down Expand Up @@ -76,6 +77,10 @@ const anthropicSchema = apiModelIdProviderModelSchema.extend({
anthropicUseAuthToken: z.boolean().optional(),
})

const claudeCodeSchema = apiModelIdProviderModelSchema.extend({
claudeCodePath: z.string().optional(),
})

const glamaSchema = baseProviderSettingsSchema.extend({
glamaModelId: z.string().optional(),
glamaApiKey: z.string().optional(),
Expand Down Expand Up @@ -208,6 +213,7 @@ const defaultSchema = z.object({

export const providerSettingsSchemaDiscriminated = z.discriminatedUnion("apiProvider", [
anthropicSchema.merge(z.object({ apiProvider: z.literal("anthropic") })),
claudeCodeSchema.merge(z.object({ apiProvider: z.literal("claude-code") })),
glamaSchema.merge(z.object({ apiProvider: z.literal("glama") })),
openRouterSchema.merge(z.object({ apiProvider: z.literal("openrouter") })),
bedrockSchema.merge(z.object({ apiProvider: z.literal("bedrock") })),
Expand All @@ -234,6 +240,7 @@ export const providerSettingsSchemaDiscriminated = z.discriminatedUnion("apiProv
export const providerSettingsSchema = z.object({
apiProvider: providerNamesSchema.optional(),
...anthropicSchema.shape,
...claudeCodeSchema.shape,
...glamaSchema.shape,
...openRouterSchema.shape,
...bedrockSchema.shape,
Expand Down
15 changes: 15 additions & 0 deletions packages/types/src/providers/claude-code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { ModelInfo } from "../model.js"
import { anthropicModels } from "./anthropic.js"

// Claude Code models - subset of Anthropic models available through Claude Code CLI

export type ClaudeCodeModelId = keyof typeof claudeCodeModels
export const claudeCodeDefaultModelId: ClaudeCodeModelId = "claude-sonnet-4-20250514"

export const claudeCodeModels = {
"claude-sonnet-4-20250514": anthropicModels["claude-sonnet-4-20250514"],
"claude-opus-4-20250514": anthropicModels["claude-opus-4-20250514"],
"claude-3-7-sonnet-20250219": anthropicModels["claude-3-7-sonnet-20250219"],
"claude-3-5-sonnet-20241022": anthropicModels["claude-3-5-sonnet-20241022"],
"claude-3-5-haiku-20241022": anthropicModels["claude-3-5-haiku-20241022"],
} as const satisfies Record<string, ModelInfo>
1 change: 1 addition & 0 deletions packages/types/src/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from "./anthropic.js"
export * from "./bedrock.js"
export * from "./chutes.js"
export * from "./claude-code.js"
export * from "./deepseek.js"
export * from "./gemini.js"
export * from "./glama.js"
Expand Down
4 changes: 4 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
GroqHandler,
ChutesHandler,
LiteLLMHandler,
ClaudeCodeHandler,
} from "./providers"

export interface SingleCompletionHandler {
Expand All @@ -36,6 +37,7 @@ export interface SingleCompletionHandler {
export interface ApiHandlerCreateMessageMetadata {
mode?: string
taskId: string
signal?: AbortSignal
}

export interface ApiHandler {
Expand Down Expand Up @@ -64,6 +66,8 @@ export function buildApiHandler(configuration: ProviderSettings): ApiHandler {
switch (apiProvider) {
case "anthropic":
return new AnthropicHandler(options)
case "claude-code":
return new ClaudeCodeHandler(options)
case "glama":
return new GlamaHandler(options)
case "openrouter":
Expand Down
Loading