Open History to see the last commit
1import { tryDocs, unavailable } from './model-facts.ts'
2import type { DocsRun } from './model-facts.ts'
3import { fetchText, sha256Text } from './types.ts'
4import type { ModelInfo, FactSource } from './types.ts'
5
6export const HF_CHAT_GUIDE =
7 'https://huggingface.co/docs/inference-providers/tasks/chat-completion'
8function reasoningStatement(text: string): boolean {
9 // A negation elsewhere in a description can qualify a later positive
10 // phrase or describe a different model. Ambiguous prose remains unknown.
11 if (
12 /\b(?:not|no|without|never|cannot|doesn't|don't|lack|lacks|lacking|unsupported)\b/i.test(
13 text,
14 )
15 )
16 return false
17 const clauses = text.split(/[.!?;]/)
18 return clauses.some(
19 (clause) =>
20 !/\b(?:not|no|without|never|cannot|doesn't)\b/i.test(clause) &&
21 /(?:reasoning (?:capabilities|based|vision language model|trace)|reasoning, and conversational capabilities|controllable thinking effort|shorter reasoning traces)/i.test(
22 clause,
23 ),
24 )
25}
26export function parseHfHostedReasoning(html: string): Array<string> {
27 const section = html.match(
28 /id="recommended-models"[\s\S]*?<\/h3>([\s\S]*?)(?:<h2|$)/i,
29 )?.[1]
30 if (!section)
31 throw new Error('huggingface: native recommendation section missing')
32 const ids: Array<string> = []
33 for (const row of section.matchAll(/<li>([\s\S]*?)<\/li>/g)) {
34 const text = row[1]!.replace(/<[^>]*>/g, '')
35 if (!reasoningStatement(text)) continue
36 const link = row[1]!.match(/href="https:\/\/huggingface\.co\/([^"?#]+)"/)
37 if (!link || !link[1]!.includes('/'))
38 throw new Error(
39 'huggingface: reasoning recommendation has no exact native model link',
40 )
41 ids.push(link[1]!)
42 }
43 if (!ids.length || new Set(ids).size !== ids.length)
44 throw new Error('huggingface: native reasoning recommendations unreadable')
45 return ids
46}
47export function nativeReasoningCapability(
48 model: ModelInfo,
49 source: FactSource,
50): ModelInfo {
51 const caps = model.capabilities
52 if (
53 model.unsupportedCapabilities?.includes('reasoning') ||
54 (caps &&
55 !Array.isArray(caps) &&
56 typeof caps === 'object' &&
57 'reasoning' in caps &&
58 caps.reasoning === false)
59 )
60 throw new Error(
61 'native host reasoning evidence contradicts explicit negative for ' +
62 model.rawId,
63 )
64 if (caps != null && !Array.isArray(caps) && typeof caps !== 'object')
65 throw new Error('native host reasoning: unreadable capabilities')
66 if (
67 Array.isArray(caps) &&
68 !(caps as Array<unknown>).every((value) => typeof value === 'string')
69 )
70 throw new Error('native host reasoning: unreadable capability list')
71 const capabilities = Array.isArray(caps)
72 ? [...new Set([...(caps as Array<string>), 'reasoning'])]
73 : caps && typeof caps === 'object'
74 ? { ...caps, reasoning: true }
75 : ['reasoning']
76 return {
77 ...model,
78 capabilities,
79 factSources: {
80 ...model.factSources,
81 capabilities: { ...model.factSources?.capabilities, reasoning: source },
82 },
83 }
84}
85export function parseFireworksNativeReasoning(
86 body: unknown,
87 rawId: string,
88): boolean {
89 if (!body || typeof body !== 'object' || Array.isArray(body))
90 throw new Error('fireworks: unreadable native model metadata')
91 const data = body as Record<string, unknown>
92 if (data.name !== rawId || typeof data.description !== 'string')
93 throw new Error(
94 'fireworks: native model metadata identity/description mismatch for ' +
95 rawId,
96 )
97 return reasoningStatement(data.description)
98}
99/**
100 * One model's native metadata is a docs source like any other. A failed
101 * load (Fireworks answers 403 for some listed ids) marks the row's
102 * capabilities `unavailable`: the stored map is kept whole (null on a new
103 * row) and the failure is counted in `run` instead of failing the poll.
104 */
105export async function fireworksHostedReasoning(
106 model: ModelInfo,
107 apiKey: string,
108 run: DocsRun,
109 kv?: KVNamespace,
110): Promise<ModelInfo> {
111 if (
112 model.activity !== 'chat' ||
113 model.reasoning ||
114 !/^accounts\/[^/]+\/models\/[^/]+$/.test(model.rawId)
115 )
116 return model
117 const url = 'https://api.fireworks.ai/v1/' + model.rawId
118 const doc = await tryDocs(run, url, (cached) =>
119 cached(kv, url, async () => {
120 const text = await fetchText(url, {
121 headers: { Authorization: 'Bearer ' + apiKey },
122 signal: AbortSignal.timeout(30_000),
123 })
124 return {
125 supported: parseFireworksNativeReasoning(JSON.parse(text), model.rawId),
126 hash: await sha256Text(text),
127 }
128 }),
129 )
130 if (!doc)
131 return {
132 ...model,
133 absent: { ...model.absent, ...unavailable('capabilities').absent },
134 }
135 return doc.supported
136 ? nativeReasoningCapability(model, {
137 derivation: 'listing',
138 sourceUrl: url,
139 sourceHash: doc.hash,
140 path: 'description',
141 })
142 : model
143}