Skip to content
Open
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
3 changes: 3 additions & 0 deletions packages/opencode/src/config/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ export const Info = Schema.Struct({
),
),
models: Schema.optional(Schema.Record(Schema.String, Model)),
auth_provider: Schema.optional(Schema.String).annotate({
description: "Reuse auth flow (OAuth/credentials) from another provider while storing credentials under this provider ID",
}),
}).annotate({ identifier: "ProviderConfig" })
export type Info = Schema.Schema.Type<typeof Info>

Expand Down
26 changes: 26 additions & 0 deletions packages/opencode/src/provider/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,32 @@ export const layer = Layer.effect(
const opts = options ?? {}
const patch: Partial<Info> = providers[providerID] ? { options: opts } : { source: "custom", options: opts }
mergeProvider(providerID, patch)

// auth provider aliases - config providers that reuse this plugin's auth flow
for (const [aliasID, aliasCfg] of configProviders) {
if (!aliasCfg.auth_provider) continue
if (ProviderID.make(aliasCfg.auth_provider) !== providerID) continue

const aliasProviderID = ProviderID.make(aliasID)
if (disabled.has(aliasProviderID)) continue

const aliasStored = yield* auth.get(aliasProviderID).pipe(
Effect.orDie,
Effect.map((x) => x ?? stored),
)

const aliasOptions = yield* Effect.promise(() =>
plugin.auth!.loader!(
() => bridge.promise(auth.get(aliasProviderID).pipe(Effect.orDie)) as any,
toPublicInfo(database[aliasProviderID] ?? database[providerID]),
),
)
const aliasOpts = aliasOptions ?? {}
const aliasPatch: Partial<Info> = providers[aliasProviderID]
? { options: aliasOpts }
: { source: "custom", options: aliasOpts }
mergeProvider(aliasProviderID, aliasPatch)
}
}

for (const [id, fn] of Object.entries(custom(dep))) {
Expand Down
27 changes: 27 additions & 0 deletions packages/opencode/test/config/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2278,3 +2278,30 @@ test("parseManagedPlist handles empty config", async () => {
)
expect(config.$schema).toBe("https://opencode.ai/config.json")
})

test("parseManagedPlist parses auth_provider in provider config", async () => {
const config = ConfigParse.schema(
Config.Info,
ConfigParse.jsonc(
await ConfigManaged.parseManagedPlist(
JSON.stringify({
$schema: "https://opencode.ai/config.json",
provider: {
"custom-github-copilot": {
name: "Custom GitHub Copilot",
auth_provider: "github-copilot",
env: [],
models: {
"gpt-4o": { name: "GPT-4o", limit: { context: 128000, output: 4096 } },
},
},
},
}),
),
"test:mobileconfig",
),
"test:mobileconfig",
)
expect(config.provider?.["custom-github-copilot"]?.name).toBe("Custom GitHub Copilot")
expect(config.provider?.["custom-github-copilot"]?.auth_provider).toBe("github-copilot")
})
38 changes: 38 additions & 0 deletions packages/opencode/test/provider/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2612,6 +2612,44 @@ test("opencode loader keeps paid models when config apiKey is present", async ()
expect(keyedCount).toBeGreaterThan(0)
})

test("provider with auth_provider is parsed and listed without errors", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
provider: {
"my-custom-provider": {
name: "My Custom Provider",
auth_provider: "openai",
env: [],
models: {
"my-model": {
name: "My Model",
tool_call: true,
limit: { context: 8192, output: 2048 },
},
},
options: { apiKey: "test-key" },
},
},
}),
)
},
})
await withTestInstance({
directory: tmp.path,
fn: async (ctx) => {
const providers = await list(ctx)
const pid = ProviderID.make("my-custom-provider")
expect(providers[pid]).toBeDefined()
expect(providers[pid].name).toBe("My Custom Provider")
expect(providers[pid].models["my-model"]).toBeDefined()
},
})
})

test("opencode loader keeps paid models when auth exists", async () => {
await using base = await tmpdir({
init: async (dir) => {
Expand Down
Loading