返回源码地图

packages/llm/llm-deepseek-api-key/src/index.ts

main snapshot · da00f7f5358f · 正文引用章节 09 / 09;完整原文可核对,不声称全文件人工逐行审计

完整原文供逐行核对;页面收录不代表每行都经过人工语义审核。MIT 许可见 许可证。

1/** API-key authentication and discovery for the official DeepSeek route. */
2import type { Context } from '@deepseek-ai/cordis'
3import { assertUsableApiKey, LlmError } from '@deepseek-ai/dsh-llm'
4import type {} from '@deepseek-ai/cordis-plugin-loader'
5import { launchEnvironmentOf } from '@deepseek-ai/dsh-launch-environment'
6import { registerDeepSeekProvider, catalogModelInfo } from '@deepseek-ai/dsh-llm-deepseek'
7import { Config, plainOptions, resolveAdapterOptions } from './config.ts'
8import type { ResolvedDeepSeekOptions } from './config.ts'
9
10export { Config, plainOptions, resolveAdapterOptions } from './config.ts'
11export type { Options, ResolvedDeepSeekOptions } from './config.ts'
12export const name = 'llm-deepseek-api-key'
13export const inject = ['llm']
14
15const PROVIDER = 'deepseek-official'
16
17export 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.apiKeyEnv
22 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}