1
/** API-key authentication and discovery for the official DeepSeek route. */2
import type { Context } from '@deepseek-ai/cordis'3
import { assertUsableApiKey, LlmError } from '@deepseek-ai/dsh-llm'4
import type {} from '@deepseek-ai/cordis-plugin-loader'5
import { launchEnvironmentOf } from '@deepseek-ai/dsh-launch-environment'6
import { registerDeepSeekProvider, catalogModelInfo } from '@deepseek-ai/dsh-llm-deepseek'7
import { Config, plainOptions, resolveAdapterOptions } from './config.ts'8
import type { ResolvedDeepSeekOptions } from './config.ts'10
export { Config, plainOptions, resolveAdapterOptions } from './config.ts'11
export type { Options, ResolvedDeepSeekOptions } from './config.ts'12
export const name = 'llm-deepseek-api-key'13
export const inject = ['llm']15
const PROVIDER = 'deepseek-official'17
export function apply(ctx: Context, config: Config): void {18
const options = () => resolveAdapterOptions(plainOptions(config), launchEnvironmentOf(ctx))19
options()20
const resolveApiKey = async (connection: ResolvedDeepSeekOptions): Promise<string> => {21
const ref = connection.apiKeyEnv22
const credentials = ctx.get('credentials')23
if (credentials !== undefined) {24
const hit = await credentials.resolve(ref)25
if (hit !== undefined) return assertUsableApiKey(hit.value, 'llm-deepseek', ref)26
} else {27
const ambient = launchEnvironmentOf(ctx).get(ref)28
if (ambient !== undefined && ambient.value.length > 0) return assertUsableApiKey(ambient.value, 'llm-deepseek', ref)29
}30
throw new LlmError(31
`llm-deepseek: no API key for provider route "${PROVIDER}"; store ${ref} through the credentials`32
+ ` service (the web Models page writes it), or export ${ref} in the launching environment`,33
'MISSING_CREDENTIAL',34
)35
}36
ctx.llm.registerConfigurableProviders([37
{ provider: PROVIDER, displayName: 'DeepSeek', settingsNs: ctx.fiber.entry?.options.id ?? name, settingsPath: [] },38
])39
registerDeepSeekProvider(ctx, PROVIDER, {40
options, providerName: 'DeepSeek',41
resolveAuth: async connection => ({ headers: { 'x-api-key': await resolveApiKey(connection) } }),42
discoverModels: (provider) => {43
const connection = options()44
return Promise.resolve(connection.models.map(model => catalogModelInfo(provider, model)))45
},46
})47
}