Open History to see the last commit
1/**
2 * SambaNova Cloud — OpenAI-compatible chat API. hints.json's docs URL
3 * 404s and the Stainless SDK publishes no openapi_spec_url, so endpoint
4 * schemas stay generated from the OpenAI document. That document is not
5 * the request map. Modalities and capability flags are read from
6 * SambaNova's models page, function-calling page, and inference OpenAPI.
7 */
8import { perTokenListingCard } from '../catalog-prices.ts'
9import { overlayModelFacts } from '../reasoning-config.ts'
10import { assertParsed, docsReport, docsRun, tryDocs } from '../model-facts.ts'
11import {
12 SAMBANOVA_MODELS_DOCS_URL,
13 SAMBANOVA_SPEC_URL,
14 SAMBANOVA_TOOLS_DOCS_URL,
15 parseSambanovaChatFlags,
16 parseSambanovaModelModalities,
17 parseSambanovaToolModels,
18 sambanovaDocsPatch,
19} from '../sambanova-docs.ts'
20import type { SambanovaDocsLoad } from '../sambanova-docs.ts'
21import {
22 OPENAI_OPENAPI_URL,
23 classifyOpenAiCompat,
24 fetchOpenAiCompatibleSpec,
25 listOpenAiCompatibleModels,
26} from '../openai-compat.ts'
27import {
28 compatGenerationEndpointId,
29 sambanovaModelActivity,
30} from '../model-meta.ts'
31import { fetchText, sha256Text } from '../types.ts'
32import type {
33 DocsFailures,
34 ListModelsResult,
35 ProviderConfig,
36 ProviderSecrets,
37 SpecFetchResult,
38} from '../types.ts'
39
40const MODELS_URL = 'https://api.sambanova.ai/v1/models'
41const SERVER_URL = 'https://api.sambanova.ai/v1'
42
43async function fetchSpec(_env: ProviderSecrets): Promise<SpecFetchResult> {
44 const { spec, url, hash } = await fetchOpenAiCompatibleSpec({
45 title: 'SambaNova',
46 serverUrl: SERVER_URL,
47 include: ['/chat/completions'],
48 })
49 return {
50 specs: [spec],
51 sources: [{ url, hash }],
52 outputStrategy: 'post-200',
53 }
54}
55
56async function loadDocs(kv: KVNamespace | undefined): Promise<{
57 loaded: SambanovaDocsLoad
58 failures: DocsFailures
59}> {
60 const docs = docsRun()
61 const [modalities, flags, tools] = await Promise.all([
62 tryDocs(docs, SAMBANOVA_MODELS_DOCS_URL, (cached) =>
63 cached(kv, SAMBANOVA_MODELS_DOCS_URL, async () => {
64 const markdown = await fetchText(SAMBANOVA_MODELS_DOCS_URL)
65 const rows = parseSambanovaModelModalities(markdown)
66 assertParsed(rows, SAMBANOVA_MODELS_DOCS_URL)
67 return {
68 byId: Object.fromEntries(rows),
69 hash: await sha256Text(markdown),
70 }
71 }),
72 ),
73 tryDocs(docs, SAMBANOVA_SPEC_URL, (cached) =>
74 cached(kv, SAMBANOVA_SPEC_URL, async () => {
75 const text = await fetchText(SAMBANOVA_SPEC_URL)
76 return {
77 flags: parseSambanovaChatFlags(JSON.parse(text) as unknown),
78 hash: await sha256Text(text),
79 }
80 }),
81 ),
82 tryDocs(docs, SAMBANOVA_TOOLS_DOCS_URL, (cached) =>
83 cached(kv, SAMBANOVA_TOOLS_DOCS_URL, async () => {
84 const markdown = await fetchText(SAMBANOVA_TOOLS_DOCS_URL)
85 const ids = parseSambanovaToolModels(markdown)
86 assertParsed(
87 new Map([...ids].map((id) => [id, true])),
88 SAMBANOVA_TOOLS_DOCS_URL,
89 )
90 return { ids: [...ids], hash: await sha256Text(markdown) }
91 }),
92 ),
93 ])
94 return {
95 loaded: { modalities, flags, tools },
96 failures: docsReport(docs),
97 }
98}
99
100async function listModels(
101 env: ProviderSecrets,
102 kv?: KVNamespace,
103): Promise<ListModelsResult> {
104 const listed = await listOpenAiCompatibleModels({
105 providerId: 'sambanova',
106 url: MODELS_URL,
107 env,
108 envVar: 'SAMBANOVA_API_KEY',
109 activity: sambanovaModelActivity,
110 extend: async (row) => {
111 // `input_cache_read` is the billed cached-input rate. Omitted (the
112 // pricing page says N/A) or zero stays off the card.
113 const pricing = await perTokenListingCard(row.pricing, MODELS_URL)
114 return pricing ? { pricing } : {}
115 },
116 })
117 if (listed.skipped || listed.models.length === 0) return listed
118 const { loaded, failures } = await loadDocs(kv)
119 return {
120 models: listed.models.map((model) =>
121 overlayModelFacts(model, sambanovaDocsPatch(model.rawId, loaded)),
122 ),
123 docsFailures: failures,
124 }
125}
126
127export const provider: ProviderConfig = {
128 id: 'sambanova',
129 displayName: 'SambaNova',
130 authEnvVar: 'SAMBANOVA_API_KEY',
131 specSourceUrl: OPENAI_OPENAPI_URL,
132 modelsEndpoint: MODELS_URL,
133 defaultDerivation: 'generated',
134 fetchSpec,
135 listModels,
136 classify: classifyOpenAiCompat,
137 generationEndpointId: ({ activity }) => compatGenerationEndpointId(activity),
138}