Skip to content
Closed
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
33 changes: 33 additions & 0 deletions packages/opencode/src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ export namespace Auth {
expires: z.number(),
accountId: z.string().optional(),
enterpriseUrl: z.string().optional(),
label: z.string().optional(),
})
.meta({ ref: "OAuth" })

export const Api = z
.object({
type: z.literal("api"),
key: z.string(),
label: z.string().optional(),
})
.meta({ ref: "ApiAuth" })

Expand All @@ -29,6 +31,7 @@ export namespace Auth {
type: z.literal("wellknown"),
key: z.string(),
token: z.string(),
label: z.string().optional(),
})
.meta({ ref: "WellKnownAuth" })

Expand Down Expand Up @@ -70,4 +73,34 @@ export namespace Auth {
await Bun.write(file, JSON.stringify(data, null, 2))
await fs.chmod(file.name!, 0o600)
}

export function parseProviderKey(key: string): { base: string; alias?: string } {
const parts = key.split(":")
if (parts.length === 1) return { base: key }
return { base: parts[0], alias: parts.slice(1).join(":") }
}

export function createProviderKey(base: string, alias?: string): string {
if (!alias) return base
return `${base}:${alias}`
}

export async function listForProvider(providerID: string): Promise<Record<string, Info>> {
const data = await all()
const result: Record<string, Info> = {}
for (const [key, value] of Object.entries(data)) {
const parsed = parseProviderKey(key)
if (parsed.base === providerID) {
result[key] = value
}
}
return result
}

export async function generateAlias(base: string): Promise<string> {
const existing = await listForProvider(base)
const count = Object.keys(existing).length
if (count === 0) return base
return createProviderKey(base, String(count + 1))
}
}
Loading
Loading