Skip to content

Commit 9db64de

Browse files
janaki-sasidharclaudedaniel-lxs
authored
Feature/vertex ai model name conversion (#5728)
Co-authored-by: Claude <[email protected]> Co-authored-by: Daniel Riccio <[email protected]>
1 parent 9b6fb36 commit 9db64de

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { describe, test, expect } from "vitest"
2+
import { convertModelNameForVertex, getClaudeCodeModelId } from "../claude-code.js"
3+
4+
describe("convertModelNameForVertex", () => {
5+
test("should convert hyphen-date format to @date format", () => {
6+
expect(convertModelNameForVertex("claude-sonnet-4-20250514")).toBe("claude-sonnet-4@20250514")
7+
expect(convertModelNameForVertex("claude-opus-4-20250514")).toBe("claude-opus-4@20250514")
8+
expect(convertModelNameForVertex("claude-3-7-sonnet-20250219")).toBe("claude-3-7-sonnet@20250219")
9+
expect(convertModelNameForVertex("claude-3-5-sonnet-20241022")).toBe("claude-3-5-sonnet@20241022")
10+
expect(convertModelNameForVertex("claude-3-5-haiku-20241022")).toBe("claude-3-5-haiku@20241022")
11+
})
12+
13+
test("should not modify models without date pattern", () => {
14+
expect(convertModelNameForVertex("some-other-model")).toBe("some-other-model")
15+
expect(convertModelNameForVertex("claude-model")).toBe("claude-model")
16+
expect(convertModelNameForVertex("model-with-short-date-123")).toBe("model-with-short-date-123")
17+
})
18+
19+
test("should only convert 8-digit date patterns at the end", () => {
20+
expect(convertModelNameForVertex("claude-20250514-sonnet")).toBe("claude-20250514-sonnet")
21+
expect(convertModelNameForVertex("model-20250514-with-more")).toBe("model-20250514-with-more")
22+
})
23+
})
24+
25+
describe("getClaudeCodeModelId", () => {
26+
test("should return original model when useVertex is false", () => {
27+
expect(getClaudeCodeModelId("claude-sonnet-4-20250514", false)).toBe("claude-sonnet-4-20250514")
28+
expect(getClaudeCodeModelId("claude-opus-4-20250514", false)).toBe("claude-opus-4-20250514")
29+
expect(getClaudeCodeModelId("claude-3-7-sonnet-20250219", false)).toBe("claude-3-7-sonnet-20250219")
30+
})
31+
32+
test("should return converted model when useVertex is true", () => {
33+
expect(getClaudeCodeModelId("claude-sonnet-4-20250514", true)).toBe("claude-sonnet-4@20250514")
34+
expect(getClaudeCodeModelId("claude-opus-4-20250514", true)).toBe("claude-opus-4@20250514")
35+
expect(getClaudeCodeModelId("claude-3-7-sonnet-20250219", true)).toBe("claude-3-7-sonnet@20250219")
36+
})
37+
38+
test("should default to useVertex false when parameter not provided", () => {
39+
expect(getClaudeCodeModelId("claude-sonnet-4-20250514")).toBe("claude-sonnet-4-20250514")
40+
})
41+
})

packages/types/src/providers/claude-code.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,44 @@
11
import type { ModelInfo } from "../model.js"
22
import { anthropicModels } from "./anthropic.js"
33

4+
// Regex pattern to match 8-digit date at the end of model names
5+
const VERTEX_DATE_PATTERN = /-(\d{8})$/
6+
7+
/**
8+
* Converts Claude model names from hyphen-date format to Vertex AI's @-date format.
9+
*
10+
* @param modelName - The original model name (e.g., "claude-sonnet-4-20250514")
11+
* @returns The converted model name for Vertex AI (e.g., "claude-sonnet-4@20250514")
12+
*
13+
* @example
14+
* convertModelNameForVertex("claude-sonnet-4-20250514") // returns "claude-sonnet-4@20250514"
15+
* convertModelNameForVertex("claude-model") // returns "claude-model" (no change)
16+
*/
17+
export function convertModelNameForVertex(modelName: string): string {
18+
// Convert hyphen-date format to @date format for Vertex AI
19+
return modelName.replace(VERTEX_DATE_PATTERN, "@$1")
20+
}
21+
422
// Claude Code
523
export type ClaudeCodeModelId = keyof typeof claudeCodeModels
624
export const claudeCodeDefaultModelId: ClaudeCodeModelId = "claude-sonnet-4-20250514"
725
export const CLAUDE_CODE_DEFAULT_MAX_OUTPUT_TOKENS = 8000
26+
27+
/**
28+
* Gets the appropriate model ID based on whether Vertex AI is being used.
29+
*
30+
* @param baseModelId - The base Claude Code model ID
31+
* @param useVertex - Whether to format the model ID for Vertex AI (default: false)
32+
* @returns The model ID, potentially formatted for Vertex AI
33+
*
34+
* @example
35+
* getClaudeCodeModelId("claude-sonnet-4-20250514", true) // returns "claude-sonnet-4@20250514"
36+
* getClaudeCodeModelId("claude-sonnet-4-20250514", false) // returns "claude-sonnet-4-20250514"
37+
*/
38+
export function getClaudeCodeModelId(baseModelId: ClaudeCodeModelId, useVertex = false): string {
39+
return useVertex ? convertModelNameForVertex(baseModelId) : baseModelId
40+
}
41+
842
export const claudeCodeModels = {
943
"claude-sonnet-4-20250514": {
1044
...anthropicModels["claude-sonnet-4-20250514"],

src/api/providers/claude-code.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import type { Anthropic } from "@anthropic-ai/sdk"
2-
import { claudeCodeDefaultModelId, type ClaudeCodeModelId, claudeCodeModels, type ModelInfo } from "@roo-code/types"
2+
import {
3+
claudeCodeDefaultModelId,
4+
type ClaudeCodeModelId,
5+
claudeCodeModels,
6+
type ModelInfo,
7+
getClaudeCodeModelId,
8+
} from "@roo-code/types"
39
import { type ApiHandler } from ".."
410
import { ApiStreamUsageChunk, type ApiStream } from "../transform/stream"
511
import { runClaudeCode } from "../../integrations/claude-code/run"
@@ -20,11 +26,17 @@ export class ClaudeCodeHandler extends BaseProvider implements ApiHandler {
2026
// Filter out image blocks since Claude Code doesn't support them
2127
const filteredMessages = filterMessagesForClaudeCode(messages)
2228

29+
const useVertex = process.env.CLAUDE_CODE_USE_VERTEX === "1"
30+
const model = this.getModel()
31+
32+
// Validate that the model ID is a valid ClaudeCodeModelId
33+
const modelId = model.id in claudeCodeModels ? (model.id as ClaudeCodeModelId) : claudeCodeDefaultModelId
34+
2335
const claudeProcess = runClaudeCode({
2436
systemPrompt,
2537
messages: filteredMessages,
2638
path: this.options.claudeCodePath,
27-
modelId: this.getModel().id,
39+
modelId: getClaudeCodeModelId(modelId, useVertex),
2840
maxOutputTokens: this.options.claudeCodeMaxOutputTokens,
2941
})
3042

0 commit comments

Comments
 (0)