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

native-host-reasoning.ts

128 lines4.3 KB

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