Open History to see the last commit
1import {
2 DEEPSEEK_CHAT_DOCS,
3 fetchNativeDeepseekSpec,
4} from '../deepseek-native-spec.ts'
5/** DeepSeek — native operation JSON from its own published API docs. */
6import {
7 applyReplay,
8 deepseekEffortFacts,
9 DEEPSEEK_THINKING_URL,
10 loadReplayDoc,
11 parseDeepseekReplay,
12} from '../provider-replay.ts'
13import {
14 DEEPSEEK_PRICING_URL,
15 deepseekModelPricing,
16} from '../deepseek-pricing.ts'
17import { docsReport, docsRun, tryDocs, unavailable } from '../model-facts.ts'
18import {
19 listOpenAiCompatibleModels,
20 openAiCompatModelFacts,
21} from '../openai-compat.ts'
22import { deepseekModelActivity } from '../model-meta.ts'
23import type {
24 ListModelsResult,
25 ModelFact,
26 ProviderConfig,
27 ProviderSecrets,
28 SpecFetchResult,
29} from '../types.ts'
30
31const DEEPSEEK_MODELS_URL = 'https://api.deepseek.com/models'
32
33function fetchSpec(_env: ProviderSecrets): Promise<SpecFetchResult> {
34 return fetchNativeDeepseekSpec()
35}
36
37export const provider: ProviderConfig = {
38 id: 'deepseek',
39 displayName: 'DeepSeek',
40 authEnvVar: 'DEEPSEEK_API_KEY',
41 specSourceUrl: DEEPSEEK_CHAT_DOCS,
42 modelsEndpoint: DEEPSEEK_MODELS_URL,
43 defaultDerivation: 'generated',
44 fetchSpec,
45 listModels: async (env, kv): Promise<ListModelsResult> => {
46 const listed = await listOpenAiCompatibleModels({
47 providerId: 'deepseek',
48 url: DEEPSEEK_MODELS_URL,
49 env,
50 envVar: 'DEEPSEEK_API_KEY',
51 activity: deepseekModelActivity,
52 extend: async (row) => {
53 const hit = deepseekEffortFacts(row.effort)
54 if (!hit) return {}
55 const capabilities = openAiCompatModelFacts(row).capabilities
56 return {
57 reasoning: hit.reasoning,
58 capabilities: [
59 ...new Set([
60 ...(Array.isArray(capabilities)
61 ? capabilities.filter(
62 (flag): flag is string => typeof flag === 'string',
63 )
64 : []),
65 'reasoning',
66 ]),
67 ],
68 factSources: {
69 reasoning: hit.source,
70 capabilities: { reasoning: hit.source },
71 },
72 }
73 },
74 })
75 if (listed.models.length === 0) return listed
76 const docs = docsRun()
77 const pricing = await tryDocs(docs, DEEPSEEK_PRICING_URL, (cached) =>
78 deepseekModelPricing(kv, cached),
79 )
80 // Same docs host as pricing. The second parse covers a KV entry cached
81 // before `verify` checked bodies.
82 const replay = listed.models.some((model) => model.reasoning != null)
83 ? await tryDocs(docs, DEEPSEEK_THINKING_URL, async (cached) => {
84 const doc = await loadReplayDoc(
85 DEEPSEEK_THINKING_URL,
86 kv,
87 cached,
88 parseDeepseekReplay,
89 )
90 parseDeepseekReplay(doc.text)
91 return doc
92 })
93 : null
94 return {
95 ...listed,
96 models: listed.models.map((model) => {
97 const lost: Array<ModelFact> = [
98 ...(pricing ? [] : ['pricing' as const]),
99 ...(model.reasoning != null && !replay
100 ? ['requestMap' as const]
101 : []),
102 ]
103 const row = {
104 ...model,
105 ...pricing?.(model.rawId),
106 ...(lost.length > 0
107 ? { absent: { ...model.absent, ...unavailable(...lost).absent } }
108 : {}),
109 }
110 return replay && model.reasoning != null
111 ? applyReplay(row, replay.source)
112 : row
113 }),
114 docsFailures: docsReport(docs),
115 }
116 },
117 classify: (_path, operation) =>
118 _path === '/chat/completions' &&
119 operation['x-modelschemas-deepseek-native'] === true
120 ? 'chat'
121 : null,
122 generationEndpointId: ({ activity }) =>
123 activity === 'chat' ? 'chat/completions' : null,
124}