-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(claude): map Claude Code model strings to OpenCode format when importing agents #2333
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
devxoul
wants to merge
4
commits into
code-yeongyu:dev
Choose a base branch
from
devxoul:feat/claude-model-mapper
base: dev
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.
+145
−0
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
77a2ab7
map Claude Code model strings to OpenCode format when importing agents
devxoul 5e25f55
add anthropic/ provider prefix for claude models, preserve date suffi…
devxoul 567f507
handle Claude Code official model aliases (sonnet, opus, haiku, inherit)
devxoul 96b5811
use Map for alias lookup to prevent prototype pollution, return undef…
devxoul 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
96 changes: 96 additions & 0 deletions
96
src/features/claude-code-agent-loader/claude-model-mapper.test.ts
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 |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import { describe, it, expect } from "bun:test" | ||
| import { mapClaudeModelToOpenCode } from "./claude-model-mapper" | ||
|
|
||
| describe("mapClaudeModelToOpenCode", () => { | ||
| describe("#given undefined or empty input", () => { | ||
| it("#when called with undefined #then returns undefined", () => { | ||
| expect(mapClaudeModelToOpenCode(undefined)).toBeUndefined() | ||
| }) | ||
|
|
||
| it("#when called with empty string #then returns undefined", () => { | ||
| expect(mapClaudeModelToOpenCode("")).toBeUndefined() | ||
| }) | ||
|
|
||
| it("#when called with whitespace-only string #then returns undefined", () => { | ||
| expect(mapClaudeModelToOpenCode(" ")).toBeUndefined() | ||
| }) | ||
| }) | ||
|
|
||
| describe("#given Claude Code alias", () => { | ||
| it("#when called with sonnet #then maps to anthropic/claude-sonnet-4-6", () => { | ||
| expect(mapClaudeModelToOpenCode("sonnet")).toBe("anthropic/claude-sonnet-4-6") | ||
| }) | ||
|
|
||
| it("#when called with opus #then maps to anthropic/claude-opus-4-6", () => { | ||
| expect(mapClaudeModelToOpenCode("opus")).toBe("anthropic/claude-opus-4-6") | ||
| }) | ||
|
|
||
| it("#when called with haiku #then maps to anthropic/claude-haiku-4-5", () => { | ||
| expect(mapClaudeModelToOpenCode("haiku")).toBe("anthropic/claude-haiku-4-5") | ||
| }) | ||
|
|
||
| it("#when called with Sonnet (capitalized) #then maps case-insensitively", () => { | ||
| expect(mapClaudeModelToOpenCode("Sonnet")).toBe("anthropic/claude-sonnet-4-6") | ||
| }) | ||
| }) | ||
|
|
||
| describe("#given inherit", () => { | ||
| it("#when called with inherit #then returns undefined", () => { | ||
| expect(mapClaudeModelToOpenCode("inherit")).toBeUndefined() | ||
| }) | ||
| }) | ||
|
|
||
| describe("#given bare Claude model name", () => { | ||
| it("#when called with claude-sonnet-4-5-20250514 #then adds anthropic prefix", () => { | ||
| expect(mapClaudeModelToOpenCode("claude-sonnet-4-5-20250514")).toBe("anthropic/claude-sonnet-4-5-20250514") | ||
| }) | ||
|
|
||
| it("#when called with claude-opus-4-6 #then adds anthropic prefix", () => { | ||
| expect(mapClaudeModelToOpenCode("claude-opus-4-6")).toBe("anthropic/claude-opus-4-6") | ||
| }) | ||
|
|
||
| it("#when called with claude-haiku-4-5-20251001 #then adds anthropic prefix", () => { | ||
| expect(mapClaudeModelToOpenCode("claude-haiku-4-5-20251001")).toBe("anthropic/claude-haiku-4-5-20251001") | ||
| }) | ||
|
|
||
| it("#when called with claude-3-5-sonnet-20241022 #then adds anthropic prefix", () => { | ||
| expect(mapClaudeModelToOpenCode("claude-3-5-sonnet-20241022")).toBe("anthropic/claude-3-5-sonnet-20241022") | ||
| }) | ||
| }) | ||
|
|
||
| describe("#given model with dot version numbers", () => { | ||
| it("#when called with claude-3.5-sonnet #then normalizes dots and adds prefix", () => { | ||
| expect(mapClaudeModelToOpenCode("claude-3.5-sonnet")).toBe("anthropic/claude-3-5-sonnet") | ||
| }) | ||
|
|
||
| it("#when called with claude-3.5-sonnet-20241022 #then normalizes dots and adds prefix", () => { | ||
| expect(mapClaudeModelToOpenCode("claude-3.5-sonnet-20241022")).toBe("anthropic/claude-3-5-sonnet-20241022") | ||
| }) | ||
| }) | ||
|
|
||
| describe("#given model already in provider/model format", () => { | ||
| it("#when called with anthropic/claude-sonnet-4-6 #then passes through unchanged", () => { | ||
| expect(mapClaudeModelToOpenCode("anthropic/claude-sonnet-4-6")).toBe("anthropic/claude-sonnet-4-6") | ||
| }) | ||
|
|
||
| it("#when called with openai/gpt-5.2 #then passes through unchanged", () => { | ||
| expect(mapClaudeModelToOpenCode("openai/gpt-5.2")).toBe("openai/gpt-5.2") | ||
| }) | ||
| }) | ||
|
|
||
| describe("#given non-Claude bare model", () => { | ||
| it("#when called with gpt-5.2 #then normalizes dots without adding prefix", () => { | ||
| expect(mapClaudeModelToOpenCode("gpt-5.2")).toBe("gpt-5-2") | ||
| }) | ||
|
|
||
| it("#when called with gemini-3-flash #then returns unchanged", () => { | ||
| expect(mapClaudeModelToOpenCode("gemini-3-flash")).toBe("gemini-3-flash") | ||
| }) | ||
| }) | ||
|
|
||
| describe("#given model with leading/trailing whitespace", () => { | ||
| it("#when called with padded string #then trims before mapping", () => { | ||
| expect(mapClaudeModelToOpenCode(" claude-sonnet-4-6 ")).toBe("anthropic/claude-sonnet-4-6") | ||
| }) | ||
| }) | ||
| }) |
31 changes: 31 additions & 0 deletions
31
src/features/claude-code-agent-loader/claude-model-mapper.ts
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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { normalizeModelID } from "../../shared/model-normalization" | ||
|
|
||
| const ANTHROPIC_PREFIX = "anthropic/" | ||
|
|
||
| const CLAUDE_CODE_ALIAS_MAP: Record<string, string> = { | ||
| sonnet: `${ANTHROPIC_PREFIX}claude-sonnet-4-6`, | ||
| opus: `${ANTHROPIC_PREFIX}claude-opus-4-6`, | ||
| haiku: `${ANTHROPIC_PREFIX}claude-haiku-4-5`, | ||
| } | ||
|
|
||
| export function mapClaudeModelToOpenCode(model: string | undefined): string | undefined { | ||
| if (!model) return undefined | ||
|
|
||
| const trimmed = model.trim() | ||
| if (trimmed.length === 0) return undefined | ||
|
|
||
| if (trimmed === "inherit") return undefined | ||
|
|
||
| const aliasResult = CLAUDE_CODE_ALIAS_MAP[trimmed.toLowerCase()] | ||
| if (aliasResult) return aliasResult | ||
|
|
||
| if (trimmed.includes("/")) return trimmed | ||
|
|
||
| const normalized = normalizeModelID(trimmed) | ||
|
|
||
| if (normalized.startsWith("claude-")) { | ||
| return `${ANTHROPIC_PREFIX}${normalized}` | ||
| } | ||
|
|
||
| return normalized | ||
| } | ||
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
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
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.