Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

deepseek.ts

157 lines4.9 KB

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 parseDeepseekNonThinking,
12 parseDeepseekReplay,
13} from '../provider-replay.ts'
14import {
15 DEEPSEEK_PRICING_URL,
16 deepseekModelPricing,
17} from '../deepseek-pricing.ts'
18import { docsReport, docsRun, tryDocs, unavailable } from '../model-facts.ts'
19import {
20 listOpenAiCompatibleModels,
21 openAiCompatModelFacts,
22} from '../openai-compat.ts'
23import { deepseekModelActivity } from '../model-meta.ts'
24import { ingestFailedEvent, noteIngest } from '../../ingest/ingest-signals.ts'
25import type {
26 ListModelsResult,
27 ModelFact,
28 ProviderConfig,
29 ProviderSecrets,
30 SpecFetchResult,
31} from '../types.ts'
32import { bearerConnect } from '../connect.ts'
33​
34const DEEPSEEK_MODELS_URL = 'https://api.deepseek.com/models'
35​
36function fetchSpec(_env: ProviderSecrets): Promise<SpecFetchResult> {
37 return fetchNativeDeepseekSpec()
39​
40export const provider: ProviderConfig = {
41 id: 'deepseek',
42 displayName: 'DeepSeek',
43 authEnvVar: 'DEEPSEEK_API_KEY',
44 specSourceUrl: DEEPSEEK_CHAT_DOCS,
45 modelsEndpoint: DEEPSEEK_MODELS_URL,
46 defaultDerivation: 'generated',
47 connect: bearerConnect('https://api.deepseek.com'),
48 fetchSpec,
49 listModels: async (env, kv): Promise<ListModelsResult> => {
50 const listed = await listOpenAiCompatibleModels({
51 providerId: 'deepseek',
52 url: DEEPSEEK_MODELS_URL,
53 env,
54 envVar: 'DEEPSEEK_API_KEY',
55 activity: deepseekModelActivity,
56 extend: async (row) => {
57 const hit = deepseekEffortFacts(row.effort)
58 if (!hit) return {}
59 const capabilities = openAiCompatModelFacts(row).capabilities
60 return {
61 reasoning: hit.reasoning,
62 capabilities: [
63 ...new Set([
64 ...(Array.isArray(capabilities)
65 ? capabilities.filter(
66 (flag): flag is string => typeof flag === 'string',
67 )
68 : []),
69 'reasoning',
70 ]),
71 ],
72 factSources: {
73 reasoning: hit.source,
74 capabilities: { reasoning: hit.source },
75 },
76 }
77 },
78 })
79 if (listed.models.length === 0) return listed
80 const docs = docsRun()
81 const pricing = await tryDocs(docs, DEEPSEEK_PRICING_URL, (cached) =>
82 deepseekModelPricing(kv, cached),
83 )
84 // Same docs host as pricing. The second parse covers a KV entry cached
85 // before `verify` checked bodies.
86 const replay = listed.models.some((model) => model.reasoning != null)
87 ? await tryDocs(docs, DEEPSEEK_THINKING_URL, async (cached) => {
88 const doc = await loadReplayDoc(
89 DEEPSEEK_THINKING_URL,
90 kv,
91 cached,
92 parseDeepseekReplay,
93 )
94 parseDeepseekReplay(doc.text)
95 return doc
96 })
97 : null
98 const switchable = new Set(
99 replay ? parseDeepseekNonThinking(replay.text) : [],
100 )
101 // The guide names its models' non-thinking modes; reading none means
102 // the page changed shape, so the stored `mandatory` stays.
103 const unreadMandatory = !replay || switchable.size === 0
104 if (replay && switchable.size === 0)
105 noteIngest(
106 ingestFailedEvent(
107 'models-poll',
108 'deepseek',
109 'thinking guide: no model names its non-thinking mode',
110 ),
111 )
112 return {
113 ...listed,
114 models: listed.models.map((model) => {
115 const lost: Array<ModelFact> = [
116 ...(pricing ? [] : ['pricing' as const]),
117 ...(model.reasoning != null && !replay
118 ? ['requestMap' as const]
119 : []),
120 ...(model.reasoning != null && unreadMandatory
121 ? ['reasoning' as const]
122 : []),
123 ]
124 const row = {
125 ...model,
126 ...pricing?.(model.rawId),
127 ...(lost.length > 0
128 ? { absent: { ...model.absent, ...unavailable(...lost).absent } }
129 : {}),
130 }
131 if (!replay || model.reasoning == null) return row
132 const replayed = applyReplay(row, replay.source)
133 return switchable.has(model.rawId)
134 ? {
135 ...replayed,
136 reasoning: { ...model.reasoning, mandatory: false },
137 factSources: {
138 ...replayed.factSources,
139 reasoning: {
140 ...replay.source,
141 path: 'top_p bullet names its non-thinking mode (mandatory); efforts are the listing',
142 },
143 },
144 }
145 : replayed
146 }),
147 docsFailures: docsReport(docs),
148 }
149 },
150 classify: (_path, operation) =>
151 _path === '/chat/completions' &&
152 operation['x-modelschemas-deepseek-native'] === true
153 ? 'chat'
154 : null,
155 generationEndpointId: ({ activity }) =>
156 activity === 'chat' ? 'chat/completions' : null,