Open History to see the last commit
1/**
2 * Deepgram — public OpenAPI at developers.deepgram.com/openapi.json
3 * (mirrored at github.com/deepgram/deepgram-api-specs). Models list
4 * requires DEEPGRAM_API_KEY. Auth is `Authorization: Token`.
5 */
6import type { Activity } from '#/db/schema.ts'
7import { deepgramModelPricing } from '../deepgram-pricing.ts'
8import { normalizeMediaModalities } from '../media-modalities.ts'
9import { fetchJson, fetchOpenApi, skippedResult } from '../types.ts'
10import type {
11 ListModelsResult,
12 ModelInfo,
13 ProviderConfig,
14 ProviderSecrets,
15 SpecFetchResult,
16} from '../types.ts'
17
18const DEEPGRAM_OPENAPI_URL = 'https://developers.deepgram.com/openapi.json'
19const DEEPGRAM_MODELS_URL = 'https://api.deepgram.com/v1/models'
20
21/**
22 * Listen (STT), Speak (TTS), and Voice Agent converse are audio
23 * generation. Project/key/auth management and `/v1/read` (text
24 * intelligence — no matching activity) classify to null.
25 */
26function classify(path: string): Activity | null {
27 const bare = path.split('?')[0] ?? path
28 if (
29 bare === '/v1/listen' ||
30 bare === '/v2/listen' ||
31 bare.startsWith('/v1/listen/') ||
32 bare.startsWith('/v2/listen/')
33 ) {
34 return 'audio'
35 }
36 if (
37 bare === '/v1/speak' ||
38 bare === '/v2/speak' ||
39 bare.startsWith('/v1/speak/') ||
40 bare.startsWith('/v2/speak/')
41 ) {
42 return 'audio'
43 }
44 if (bare === '/v1/agent/converse' || bare.startsWith('/v1/agent/converse/')) {
45 return 'audio'
46 }
47 return null
48}
49
50async function fetchSpec(_env: ProviderSecrets): Promise<SpecFetchResult> {
51 const { spec, hash } = await fetchOpenApi(DEEPGRAM_OPENAPI_URL)
52 return {
53 specs: [spec],
54 sources: [{ url: DEEPGRAM_OPENAPI_URL, hash }],
55 outputStrategy: 'post-200',
56 }
57}
58
59interface DeepgramListedModel {
60 name?: string
61 canonical_name?: string
62}
63
64interface DeepgramModelList {
65 stt?: Array<DeepgramListedModel>
66 tts?: Array<DeepgramListedModel>
67}
68
69function listedModel(
70 entry: DeepgramListedModel,
71 kind: 'stt' | 'tts',
72): ModelInfo | null {
73 const rawId = entry.canonical_name ?? entry.name
74 if (typeof rawId !== 'string' || rawId.length === 0) return null
75 const stt = kind === 'stt'
76 return {
77 rawId,
78 displayName: entry.name ?? rawId,
79 activity: 'audio',
80 modalities: normalizeMediaModalities({
81 activity: 'audio',
82 listingInput: stt ? ['audio'] : ['text'],
83 listingOutput: stt ? ['text'] : ['audio'],
84 }),
85 schemaEndpointId: stt ? 'v1/listen' : 'v1/speak',
86 }
87}
88
89async function listModels(
90 env: ProviderSecrets,
91 kv?: KVNamespace,
92): Promise<ListModelsResult> {
93 const key = env.DEEPGRAM_API_KEY
94 if (!key) {
95 return { models: [], ...skippedResult('deepgram', 'DEEPGRAM_API_KEY') }
96 }
97 const body = (await fetchJson(DEEPGRAM_MODELS_URL, {
98 headers: { Authorization: `Token ${key}` },
99 })) as DeepgramModelList
100 const models: Array<ModelInfo> = []
101 const seen = new Set<string>()
102 for (const entry of body.stt ?? []) {
103 const model = listedModel(entry, 'stt')
104 if (!model || seen.has(model.rawId)) continue
105 seen.add(model.rawId)
106 models.push(model)
107 }
108 for (const entry of body.tts ?? []) {
109 const model = listedModel(entry, 'tts')
110 if (!model || seen.has(model.rawId)) continue
111 seen.add(model.rawId)
112 models.push(model)
113 }
114 if (models.length === 0) return { models }
115 const pricing = await deepgramModelPricing(kv)
116 return {
117 models: models.map((model) => ({ ...model, ...pricing(model.rawId) })),
118 }
119}
120
121export const provider: ProviderConfig = {
122 id: 'deepgram',
123 displayName: 'Deepgram',
124 authEnvVar: 'DEEPGRAM_API_KEY',
125 specSourceUrl: DEEPGRAM_OPENAPI_URL,
126 modelsEndpoint: DEEPGRAM_MODELS_URL,
127 defaultDerivation: 'upstream-spec',
128 fetchSpec,
129 listModels,
130 classify,
131}