Skip to content

Commit ac53a37

Browse files
committed
feat: use anthropic compat messages api for anthropic models through copilot
1 parent 7795cae commit ac53a37

3 files changed

Lines changed: 35 additions & 18 deletions

File tree

‎packages/opencode/src/plugin/copilot.ts‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export async function CopilotAuthPlugin(input: PluginInput): Promise<Hooks> {
2626
const info = await getAuth()
2727
if (!info || info.type !== "oauth") return {}
2828

29+
const enterpriseUrl = info.enterpriseUrl
30+
const baseURL = enterpriseUrl ? `https://copilot-api.${normalizeDomain(enterpriseUrl)}` : undefined
31+
2932
if (provider && provider.models) {
3033
for (const model of Object.values(provider.models)) {
3134
model.cost = {
@@ -36,16 +39,23 @@ export async function CopilotAuthPlugin(input: PluginInput): Promise<Hooks> {
3639
write: 0,
3740
},
3841
}
42+
43+
// TODO: move some of this hacky-ness to models.dev presets once we have better grasp of things here...
44+
const base = baseURL ?? model.api.url
45+
const claude = model.id.includes("claude")
46+
const url = iife(() => {
47+
if (!claude) return base
48+
if (base.endsWith("/v1")) return base
49+
if (base.endsWith("/")) return `${base}v1`
50+
return `${base}/v1`
51+
})
52+
53+
model.api.url = url
54+
model.api.npm = claude ? "@ai-sdk/anthropic" : "@ai-sdk/github-copilot"
3955
}
4056
}
4157

42-
const enterpriseUrl = info.enterpriseUrl
43-
const baseURL = enterpriseUrl
44-
? `https://copilot-api.${normalizeDomain(enterpriseUrl)}`
45-
: "https://api.githubcopilot.com"
46-
4758
return {
48-
baseURL,
4959
apiKey: "",
5060
async fetch(request: RequestInfo | URL, init?: RequestInit) {
5161
const info = await getAuth()

‎packages/opencode/src/provider/provider.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export namespace Provider {
132132
return {
133133
autoload: false,
134134
async getModel(sdk: any, modelID: string, _options?: Record<string, any>) {
135+
if (sdk.responses === undefined && sdk.chat === undefined) return sdk.languageModel(modelID)
135136
return shouldUseCopilotResponsesApi(modelID) ? sdk.responses(modelID) : sdk.chat(modelID)
136137
},
137138
options: {},
@@ -141,6 +142,7 @@ export namespace Provider {
141142
return {
142143
autoload: false,
143144
async getModel(sdk: any, modelID: string, _options?: Record<string, any>) {
145+
if (sdk.responses === undefined && sdk.chat === undefined) return sdk.languageModel(modelID)
144146
return shouldUseCopilotResponsesApi(modelID) ? sdk.responses(modelID) : sdk.chat(modelID)
145147
},
146148
options: {},
@@ -601,10 +603,7 @@ export namespace Provider {
601603
api: {
602604
id: model.id,
603605
url: provider.api!,
604-
npm: iife(() => {
605-
if (provider.id.startsWith("github-copilot")) return "@ai-sdk/github-copilot"
606-
return model.provider?.npm ?? provider.npm ?? "@ai-sdk/openai-compatible"
607-
}),
606+
npm: model.provider?.npm ?? provider.npm ?? "@ai-sdk/openai-compatible",
608607
},
609608
status: model.status ?? "active",
610609
headers: model.headers ?? {},
@@ -924,6 +923,8 @@ export namespace Provider {
924923
)
925924
delete provider.models[modelID]
926925

926+
model.variants = mapValues(ProviderTransform.variants(model), (v) => v)
927+
927928
// Filter out disabled variants from config
928929
const configVariants = configProvider?.models?.[modelID]?.variants
929930
if (configVariants && model.variants) {

‎packages/opencode/src/session/llm.ts‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,20 @@ export namespace LLM {
150150
},
151151
)
152152

153-
const maxOutputTokens = isCodex
154-
? undefined
155-
: ProviderTransform.maxOutputTokens(
156-
input.model.api.npm,
157-
params.options,
158-
input.model.limit.output,
159-
OUTPUT_TOKEN_MAX,
160-
)
153+
const maxOutputTokens = isCodex ? undefined : undefined
154+
log.info("max_output_tokens", {
155+
tokens: ProviderTransform.maxOutputTokens(
156+
input.model.api.npm,
157+
params.options,
158+
input.model.limit.output,
159+
OUTPUT_TOKEN_MAX,
160+
),
161+
modelOptions: params.options,
162+
outputLimit: input.model.limit.output,
163+
})
164+
// tokens = 32000
165+
// outputLimit = 64000
166+
// modelOptions={"reasoningEffort":"minimal"}
161167

162168
const tools = await resolveTools(input)
163169

0 commit comments

Comments
 (0)