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

deepseek.ts

93 lines2.8 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 parseDeepseekReplay,
12} from '../provider-replay.ts'
13import { deepseekModelPricing } from '../deepseek-pricing.ts'
14import {
15 listOpenAiCompatibleModels,
16 openAiCompatModelFacts,
17} from '../openai-compat.ts'
18import { deepseekModelActivity } from '../model-meta.ts'
19import type {
20 ListModelsResult,
21 ProviderConfig,
22 ProviderSecrets,
23 SpecFetchResult,
24} from '../types.ts'
25​
26const DEEPSEEK_MODELS_URL = 'https://api.deepseek.com/models'
27​
28function fetchSpec(_env: ProviderSecrets): Promise<SpecFetchResult> {
29 return fetchNativeDeepseekSpec()
31​
32export const provider: ProviderConfig = {
33 id: 'deepseek',
34 displayName: 'DeepSeek',
35 authEnvVar: 'DEEPSEEK_API_KEY',
36 specSourceUrl: DEEPSEEK_CHAT_DOCS,
37 modelsEndpoint: DEEPSEEK_MODELS_URL,
38 defaultDerivation: 'generated',
39 fetchSpec,
40 listModels: async (env, kv): Promise<ListModelsResult> => {
41 const listed = await listOpenAiCompatibleModels({
42 providerId: 'deepseek',
43 url: DEEPSEEK_MODELS_URL,
44 env,
45 envVar: 'DEEPSEEK_API_KEY',
46 activity: deepseekModelActivity,
47 extend: async (row) => {
48 const hit = deepseekEffortFacts(row.effort)
49 if (!hit) return {}
50 const capabilities = openAiCompatModelFacts(row).capabilities
51 return {
52 reasoning: hit.reasoning,
53 capabilities: [
54 ...new Set([
55 ...(Array.isArray(capabilities)
56 ? capabilities.filter(
57 (flag): flag is string => typeof flag === 'string',
58 )
59 : []),
60 'reasoning',
61 ]),
62 ],
63 factSources: {
64 reasoning: hit.source,
65 capabilities: { reasoning: hit.source },
66 },
67 }
68 },
69 })
70 if (listed.models.length === 0) return listed
71 const pricing = await deepseekModelPricing(kv)
72 const replay = listed.models.some((model) => model.reasoning != null)
73 ? await loadReplayDoc(DEEPSEEK_THINKING_URL, kv)
74 : null
75 if (replay) parseDeepseekReplay(replay.text)
76 return {
77 ...listed,
78 models: listed.models.map((model) => {
79 const priced = { ...model, ...pricing(model.rawId) }
80 return replay && model.reasoning != null
81 ? applyReplay(priced, replay.source)
82 : priced
83 }),
84 }
85 },
86 classify: (_path, operation) =>
87 _path === '/chat/completions' &&
88 operation['x-modelschemas-deepseek-native'] === true
89 ? 'chat'
90 : null,
91 generationEndpointId: ({ activity }) =>
92 activity === 'chat' ? 'chat/completions' : null,