Skip to content

Commit b1ad2d5

Browse files
committed
fix: add validation for model ID type assertion and improve code documentation
- Add validation before type assertion in claude-code.ts to ensure model ID is valid - Add comprehensive JSDoc comments for convertModelNameForVertex and getClaudeCodeModelId functions - Extract regex pattern as named constant VERTEX_DATE_PATTERN for better maintainability
1 parent a4f70c3 commit b1ad2d5

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

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

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

4-
// Utility function to convert model names for Vertex AI format
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+
*/
517
export function convertModelNameForVertex(modelName: string): string {
618
// Convert hyphen-date format to @date format for Vertex AI
7-
return modelName.replace(/-(\d{8})$/, "@$1")
19+
return modelName.replace(VERTEX_DATE_PATTERN, "@$1")
820
}
921

1022
// Claude Code
1123
export type ClaudeCodeModelId = keyof typeof claudeCodeModels
1224
export const claudeCodeDefaultModelId: ClaudeCodeModelId = "claude-sonnet-4-20250514"
1325
export const CLAUDE_CODE_DEFAULT_MAX_OUTPUT_TOKENS = 8000
14-
// Helper function to get model ID based on environment
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+
*/
1538
export function getClaudeCodeModelId(baseModelId: ClaudeCodeModelId, useVertex = false): string {
1639
return useVertex ? convertModelNameForVertex(baseModelId) : baseModelId
1740
}

src/api/providers/claude-code.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,16 @@ export class ClaudeCodeHandler extends BaseProvider implements ApiHandler {
2727
const filteredMessages = filterMessagesForClaudeCode(messages)
2828

2929
const useVertex = process.env.CLAUDE_CODE_USE_VERTEX?.toLowerCase() === "true"
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+
3035
const claudeProcess = runClaudeCode({
3136
systemPrompt,
3237
messages: filteredMessages,
3338
path: this.options.claudeCodePath,
34-
modelId: getClaudeCodeModelId(this.getModel().id as ClaudeCodeModelId, useVertex),
39+
modelId: getClaudeCodeModelId(modelId, useVertex),
3540
maxOutputTokens: this.options.claudeCodeMaxOutputTokens,
3641
})
3742

0 commit comments

Comments
 (0)