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: 29 additions & 4 deletions ee/apps/den-api/src/routes/org/llm-providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,24 @@ function isOrganizationAdmin(payload: { currentMember: { isOwner: boolean; role:
return payload.currentMember.isOwner || memberHasRole(payload.currentMember.role, "admin")
}

export function getCredentialFlags(provider: Pick<LlmProviderRow, "credentialKind" | "apiKey" | "opencodeAuth">) {
const hasApiKey = Boolean(provider.apiKey && provider.apiKey.trim().length > 0)
const hasOpencodeAuth = Boolean(provider.opencodeAuth && provider.opencodeAuth.trim().length > 0)
return {
hasApiKey,
hasOpencodeAuth,
hasCredential: provider.credentialKind === "opencode_oauth" ? hasOpencodeAuth : hasApiKey,
}
}

export function redactLlmProviderCredentials<T extends { apiKey?: unknown; opencodeAuth?: unknown }>(provider: T): Omit<T, "apiKey" | "opencodeAuth"> & { apiKey: undefined; opencodeAuth: undefined } {
return {
...provider,
apiKey: undefined,
opencodeAuth: undefined,
}
}

function canManageLlmProvider(
payload: { currentMember: { id: MemberId; isOwner: boolean; role: string } },
provider: LlmProviderRow,
Expand Down Expand Up @@ -464,7 +482,7 @@ async function loadLlmProviders(input: {

return providers.map((provider) => ({
...provider,
hasApiKey: Boolean(provider.apiKey && provider.apiKey.trim().length > 0),
...getCredentialFlags(provider),
models: (modelsByProviderId.get(provider.id) ?? [])
.map((model) => ({
id: model.modelId,
Expand Down Expand Up @@ -607,8 +625,7 @@ export function registerOrgLlmProviderRoutes<T extends { Variables: OrgRouteVari

return c.json({
llmProviders: providers.map((provider) => ({
...provider,
apiKey: undefined,
...redactLlmProviderCredentials(provider),
canManage: canManageLlmProvider(payload, provider),
})),
})
Expand Down Expand Up @@ -678,6 +695,8 @@ export function registerOrgLlmProviderRoutes<T extends { Variables: OrgRouteVari
return c.json({
llmProvider: {
...provider,
opencodeAuth: undefined,
...getCredentialFlags(provider),
models: models
.map((model) => ({
id: model.modelId,
Expand Down Expand Up @@ -781,10 +800,13 @@ export function registerOrgLlmProviderRoutes<T extends { Variables: OrgRouteVari
organizationId: payload.organization.id,
createdByOrgMembershipId: payload.currentMember.id,
source: normalized.source,
credentialKind: "api_key",
providerId: normalized.providerId,
name: normalized.name,
providerConfig: normalized.providerConfig,
hasApiKey: Boolean(normalized.apiKey),
hasOpencodeAuth: false,
hasCredential: Boolean(normalized.apiKey),
createdAt: now,
updatedAt: now,
},
Expand Down Expand Up @@ -916,12 +938,15 @@ export function registerOrgLlmProviderRoutes<T extends { Variables: OrgRouteVari

return c.json({
llmProvider: {
...provider,
...redactLlmProviderCredentials(provider),
source: normalized.source,
providerId: normalized.providerId,
name: normalized.name,
providerConfig: normalized.providerConfig,
credentialKind: provider.credentialKind,
hasApiKey: input.apiKey === undefined ? Boolean(provider.apiKey) : Boolean(normalized.apiKey),
hasOpencodeAuth: Boolean(provider.opencodeAuth),
hasCredential: input.apiKey === undefined ? getCredentialFlags(provider).hasCredential : Boolean(normalized.apiKey),
updatedAt,
},
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE `llm_provider`
ADD COLUMN `credential_kind` enum('api_key','opencode_oauth') NOT NULL DEFAULT 'api_key' AFTER `provider_config`,
ADD COLUMN `opencode_auth` text AFTER `api_key`;
7 changes: 7 additions & 0 deletions ee/packages/den-db/drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@
"when": 1779380000000,
"tag": "0018_invited_org_members",
"breakpoints": true
},
{
"idx": 19,
"version": "5",
"when": 1777486400000,
"tag": "0019_llm_provider_opencode_oauth",
"breakpoints": true
}
]
}
4 changes: 4 additions & 0 deletions ee/packages/den-db/src/schema/sharables/llm-providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ export const LlmProviderTable = mysqlTable(
providerConfig: json("provider_config")
.$type<Record<string, unknown>>()
.notNull(),
credentialKind: mysqlEnum("credential_kind", ["api_key", "opencode_oauth"])
.notNull()
.default("api_key"),
apiKey: encryptedTextColumn("api_key"),
opencodeAuth: encryptedTextColumn("opencode_auth"),
createdAt: timestamp("created_at", { fsp: 3 }).notNull().defaultNow(),
updatedAt: timestamp("updated_at", { fsp: 3 })
.notNull()
Expand Down