Skip to content
Merged
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
30 changes: 30 additions & 0 deletions packages/types/src/providers/claude-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export const claudeCodeModels = {
supportsReasoningEffort: false,
supportsReasoningBudget: false,
requiredReasoningBudget: false,
// Claude Code manages its own tools and temperature via the CLI
supportsNativeTools: false,
supportsTemperature: false,
},
"claude-sonnet-4-5-20250929[1m]": {
...anthropicModels["claude-sonnet-4-5"],
Expand All @@ -56,6 +59,9 @@ export const claudeCodeModels = {
supportsReasoningEffort: false,
supportsReasoningBudget: false,
requiredReasoningBudget: false,
// Claude Code manages its own tools and temperature via the CLI
supportsNativeTools: false,
supportsTemperature: false,
},
"claude-sonnet-4-20250514": {
...anthropicModels["claude-sonnet-4-20250514"],
Expand All @@ -64,6 +70,9 @@ export const claudeCodeModels = {
supportsReasoningEffort: false,
supportsReasoningBudget: false,
requiredReasoningBudget: false,
// Claude Code manages its own tools and temperature via the CLI
supportsNativeTools: false,
supportsTemperature: false,
},
"claude-opus-4-5-20251101": {
...anthropicModels["claude-opus-4-5-20251101"],
Expand All @@ -72,6 +81,9 @@ export const claudeCodeModels = {
supportsReasoningEffort: false,
supportsReasoningBudget: false,
requiredReasoningBudget: false,
// Claude Code manages its own tools and temperature via the CLI
supportsNativeTools: false,
supportsTemperature: false,
},
"claude-opus-4-1-20250805": {
...anthropicModels["claude-opus-4-1-20250805"],
Expand All @@ -80,6 +92,9 @@ export const claudeCodeModels = {
supportsReasoningEffort: false,
supportsReasoningBudget: false,
requiredReasoningBudget: false,
// Claude Code manages its own tools and temperature via the CLI
supportsNativeTools: false,
supportsTemperature: false,
},
"claude-opus-4-20250514": {
...anthropicModels["claude-opus-4-20250514"],
Expand All @@ -88,6 +103,9 @@ export const claudeCodeModels = {
supportsReasoningEffort: false,
supportsReasoningBudget: false,
requiredReasoningBudget: false,
// Claude Code manages its own tools and temperature via the CLI
supportsNativeTools: false,
supportsTemperature: false,
},
"claude-3-7-sonnet-20250219": {
...anthropicModels["claude-3-7-sonnet-20250219"],
Expand All @@ -96,6 +114,9 @@ export const claudeCodeModels = {
supportsReasoningEffort: false,
supportsReasoningBudget: false,
requiredReasoningBudget: false,
// Claude Code manages its own tools and temperature via the CLI
supportsNativeTools: false,
supportsTemperature: false,
},
"claude-3-5-sonnet-20241022": {
...anthropicModels["claude-3-5-sonnet-20241022"],
Expand All @@ -104,6 +125,9 @@ export const claudeCodeModels = {
supportsReasoningEffort: false,
supportsReasoningBudget: false,
requiredReasoningBudget: false,
// Claude Code manages its own tools and temperature via the CLI
supportsNativeTools: false,
supportsTemperature: false,
},
"claude-3-5-haiku-20241022": {
...anthropicModels["claude-3-5-haiku-20241022"],
Expand All @@ -112,6 +136,9 @@ export const claudeCodeModels = {
supportsReasoningEffort: false,
supportsReasoningBudget: false,
requiredReasoningBudget: false,
// Claude Code manages its own tools and temperature via the CLI
supportsNativeTools: false,
supportsTemperature: false,
},
"claude-haiku-4-5-20251001": {
...anthropicModels["claude-haiku-4-5-20251001"],
Expand All @@ -120,5 +147,8 @@ export const claudeCodeModels = {
supportsReasoningEffort: false,
supportsReasoningBudget: false,
requiredReasoningBudget: false,
// Claude Code manages its own tools and temperature via the CLI
supportsNativeTools: false,
supportsTemperature: false,
},
} as const satisfies Record<string, ModelInfo>
25 changes: 18 additions & 7 deletions src/api/providers/claude-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,26 @@ import {
type ModelInfo,
getClaudeCodeModelId,
} from "@roo-code/types"
import { type ApiHandler } from ".."
import { type ApiHandler, ApiHandlerCreateMessageMetadata } from ".."
import { ApiStreamUsageChunk, type ApiStream } from "../transform/stream"
import { runClaudeCode } from "../../integrations/claude-code/run"
import { filterMessagesForClaudeCode } from "../../integrations/claude-code/message-filter"
import { BaseProvider } from "./base-provider"
import { t } from "../../i18n"
import { ApiHandlerOptions } from "../../shared/api"
import { countTokens } from "../../utils/countTokens"

export class ClaudeCodeHandler extends BaseProvider implements ApiHandler {
export class ClaudeCodeHandler implements ApiHandler {
private options: ApiHandlerOptions

constructor(options: ApiHandlerOptions) {
super()
this.options = options
}

override async *createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream {
async *createMessage(
systemPrompt: string,
messages: Anthropic.Messages.MessageParam[],
_metadata?: ApiHandlerCreateMessageMetadata,
): ApiStream {
// Filter out image blocks since Claude Code doesn't support them
const filteredMessages = filterMessagesForClaudeCode(messages)

Expand All @@ -42,7 +45,7 @@ export class ClaudeCodeHandler extends BaseProvider implements ApiHandler {

// Usage is included with assistant messages,
// but cost is included in the result chunk
let usage: ApiStreamUsageChunk = {
const usage: ApiStreamUsageChunk = {
type: "usage",
inputTokens: 0,
outputTokens: 0,
Expand Down Expand Up @@ -121,6 +124,7 @@ export class ClaudeCodeHandler extends BaseProvider implements ApiHandler {
}
}

// Accumulate usage across streaming chunks
usage.inputTokens += message.usage.input_tokens
usage.outputTokens += message.usage.output_tokens
usage.cacheReadTokens = (usage.cacheReadTokens || 0) + (message.usage.cache_read_input_tokens || 0)
Expand All @@ -138,7 +142,7 @@ export class ClaudeCodeHandler extends BaseProvider implements ApiHandler {
}
}

getModel() {
getModel(): { id: string; info: ModelInfo } {
const modelId = this.options.apiModelId
if (modelId && modelId in claudeCodeModels) {
const id = modelId as ClaudeCodeModelId
Expand All @@ -165,6 +169,13 @@ export class ClaudeCodeHandler extends BaseProvider implements ApiHandler {
}
}

async countTokens(content: Anthropic.Messages.ContentBlockParam[]): Promise<number> {
if (content.length === 0) {
return 0
}
return countTokens(content, { useWorker: true })
}

private attemptParse(str: string) {
try {
return JSON.parse(str)
Expand Down
Loading