Open History to see the last commit
1import { blankRequestMap } from '../request-map.ts'
2import type { ChatRequestMap } from '../request-map.ts'
3import { reasoningViolation } from '../types.ts'
4import type { FactSource, ModelInfo } from '../types.ts'
5import type { ChatCompletionsProbe } from './chat-completions.ts'
6import { probe as groqProbe } from './groq.ts'
7import {
8 OPENCODE_GO_PROBE_SOURCE,
9 records as opencodeGo,
10} from './opencode-go.ts'
11import { records as groqRecords } from './records/groq.ts'
12import { probeIsStale } from './types.ts'
13import type { ProbedFactRecord, ProbeRecord } from './types.ts'
14
15const SOURCE: Record<string, string> = {
16 'opencode-go': OPENCODE_GO_PROBE_SOURCE,
17}
18
19/** Every committed probe. Add a provider by importing its module here. */
20export const probeRecords: Array<ProbeRecord> = [...opencodeGo]
21
22/**
23 * Providers on the generic `chat/completions` probe (issue #717). Opt in
24 * with `probes/<provider>.ts` exporting a `ChatCompletionsProbe`, an empty
25 * `records/<provider>.ts`, and one entry in each list below. Set
26 * `mandatoryRule` only for a provider whose `reasoning_effort` level list
27 * is per model and is the model's only reasoning control.
28 */
29export const chatProbes: Array<ChatCompletionsProbe> = [groqProbe]
30
31/** Every committed fact a live response stated. */
32export const probedFactRecords: Array<ProbedFactRecord> = [...groqRecords]
33
34/** The generated records file of a chat probe provider. */
35export function probedFactFile(provider: string): string {
36 return `src/server/providers/probes/records/${provider}.ts`
37}
38
39export function probesFor(provider: string): Array<ProbeRecord> {
40 return probeRecords.filter((record) => record.provider === provider)
41}
42
43function namesModel(route: string, model: string): boolean {
44 return route.split('/').some((segment) => segment.split(':')[0] === model)
45}
46
47/**
48 * The route a catalog row may bind. `:generateContent` wins when that
49 * model has several records. Another model's record never matches.
50 */
51export function probedRoute(provider: string, model: string): string | null {
52 const matches = probesFor(provider).filter(
53 (record) => record.model === model && namesModel(record.route, model),
54 )
55 const primary =
56 matches.find((record) => record.route.endsWith(':generateContent')) ??
57 (matches.length === 1 ? matches[0] : undefined)
58 return primary?.route ?? null
59}
60
61export function probeFactSource(
62 provider: string,
63 model: string,
64 route: string,
65): FactSource | null {
66 const record = probesFor(provider).find(
67 (item) => item.model === model && item.route === route,
68 )
69 if (!record) return null
70 const source = SOURCE[provider]
71 if (!source) return null
72 return {
73 derivation: 'probe-verified',
74 verifiedAt: record.verifiedAt,
75 sourceUrl: `${source}#${record.id}`,
76 }
77}
78
79/** Records whose `verifiedAt` is older than `PROBE_STALE_DAYS`. */
80export function staleProbeCount(provider: string, now = new Date()): number {
81 return [
82 ...probesFor(provider),
83 ...probedFactRecords.filter((record) => record.provider === provider),
84 ].filter((record) => probeIsStale(record.verifiedAt, now)).length
85}
86
87function probedSource(record: ProbedFactRecord): FactSource {
88 return {
89 derivation: 'probe-verified',
90 verifiedAt: record.verifiedAt,
91 sourceUrl: `${probedFactFile(record.provider)}#${record.id}`,
92 }
93}
94
95/**
96 * Fills a row's empty facts from probe records naming this exact model
97 * and the route the row binds. A stated value is never replaced: a probe
98 * fills a gap. `requestMap` is the map the row would store.
99 */
100export function withProbedFacts(
101 info: ModelInfo,
102 provider: string,
103 route: string | null,
104 requestMap: ChatRequestMap | null,
105 records: Array<ProbedFactRecord> = probedFactRecords,
106): ModelInfo {
107 const mine = records.filter(
108 (record) =>
109 record.provider === provider &&
110 record.model === info.rawId &&
111 record.route === route,
112 )
113 if (mine.length === 0) return info
114 const next: ModelInfo = { ...info, factSources: { ...info.factSources } }
115 const sources = next.factSources!
116 for (const record of mine) {
117 const { fact, value } = record
118 if (fact === 'maxOutput' || fact === 'contextWindow') {
119 if (typeof value !== 'number' || next[fact] != null) continue
120 if (next.absent?.[fact]) continue
121 next[fact] = value
122 sources[fact] = probedSource(record)
123 } else if (fact === 'replayReasoningContent') {
124 const map = requestMap ?? blankRequestMap({})
125 if (typeof value !== 'boolean' || map.replayReasoningContent !== null) {
126 continue
127 }
128 next.requestMap = { ...map, replayReasoningContent: value }
129 sources.requestMapFields = {
130 ...sources.requestMapFields,
131 replayReasoningContent: probedSource(record),
132 }
133 } else {
134 // A refused `reasoning_effort: none` speaks only to an effort row.
135 const reasoning = next.reasoning
136 if (value !== true || reasoning?.mode !== 'effort') continue
137 if (reasoning.mandatory !== null) continue
138 const filled = { ...reasoning, mandatory: true }
139 if (reasoningViolation(filled)) continue
140 next.reasoning = filled
141 sources.reasoningMandatory = probedSource(record)
142 }
143 }
144 return next
145}