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

openrouter-reasoning.ts

114 lines4.4 KB

1import type { ThinkingRequest } from './request-map.ts'
2​
3/** Gateway effort names come from normative provider documentation, never examples. */
4export const OPENROUTER_REASONING_URL =
5 'https://openrouter.ai/docs/guides/best-practices/reasoning-tokens.md'
6export function openRouterGatewayEfforts(markdown: string): Array<string> {
7 const prose = markdown.replace(/```[\s\S]*?```/g, '')
8 if (!/When `null`, all gateway effort values are accepted/.test(prose))
9 throw new Error('openrouter: native null-efforts contract missing')
10 if (
11 !/\*\*`mandatory`\*\*:[^\n]*When `true`[^\n]*do not send `effort: "none"`[^\n]*model rejects it/.test(
12 prose,
13 )
14 )
15 throw new Error('openrouter: native mandatory effort rejection missing')
16 const values = [...prose.matchAll(/^\* `"effort": "([a-z]+)"` - .+$/gm)].map(
17 (match) => match[1]!,
18 )
19 if (!values.length || new Set(values).size !== values.length)
20 throw new Error(
21 'openrouter: missing or ambiguous normative gateway efforts',
22 )
23 return values
25​
26/** Messages compatibility is explicitly normalized for every reasoning model. */
27export function openRouterGatewayToggle(markdown: string): ThinkingRequest {
28 const prose = markdown.replace(/```[\s\S]*?```/g, '')
29 const section = prose
30 .split(/^## Reasoning with the Anthropic Messages API\s*$/m)[1]
31 ?.split(/^#{1,2} /m)[0]
32 if (
33 !section ||
34 !/^Requests sent through[^\n]*OpenRouter normalizes both into the unified `reasoning` parameter, so they work on every reasoning model, not only Claude\./m.test(
35 section,
36 )
37 )
38 throw new Error('openrouter: native gateway toggle scope missing')
39 if (
40 !/^\| `thinking: \{ type: "adaptive" \}` \| `reasoning: \{ enabled: true \}` \|$/m.test(
41 section,
42 ) ||
43 !/^\| `thinking: \{ type: "disabled" \}` \| `reasoning: \{ enabled: false \}` \|$/m.test(
44 section,
45 ) ||
46 !/^`thinking: \{ type: "disabled" \}` disables reasoning even when an `output_config\.effort` is also present\./m.test(
47 section,
48 )
49 )
50 throw new Error(
51 'openrouter: native gateway enable/disable contract missing',
52 )
53 const body = (type: 'adaptive' | 'disabled') => {
54 const row = section
55 .split('\n')
56 .find((line) => line.startsWith(`| \`thinking: { type: "${type}" }\``))
57 const wire = row?.match(/\| `([a-z_]+): \{ ([a-z_]+): (true|false) \}` \|$/)
58 if (!wire?.[1] || !wire[2] || !wire[3])
59 throw new Error('openrouter: native toggle wire map unreadable')
60 return { [wire[1]]: { [wire[2]]: wire[3] === 'true' } }
61 }
62 return { on: body('adaptive'), off: body('disabled'), levels: null }
64​
65/**
66 * The guide states `reasoning_content` as an alias of `reasoning`, and
67 * `reasoning_details` replay, for every supported reasoning model. A model
68 * exception would have to be named. Throws when that statement is gone.
69 */
70export function openRouterReasoningContentReplay(markdown: string): true {
71 const prose = markdown.replace(/```[\s\S]*?```/g, '')
72 if (
73 !/You can also use `reasoning_content` as an alias - it functions identically to `reasoning`\./.test(
74 prose,
75 )
76 ) {
77 throw new Error('openrouter: reasoning_content alias missing')
78 }
79 const section = prose.split(/^## Preserving Reasoning\s*$/m)[1]
80 if (
81 !section ||
82 !/you can pass it back to the API/.test(section) ||
83 !/The `reasoning_details` functionality works identically across all supported reasoning models\./.test(
84 prose,
85 )
86 ) {
87 throw new Error('openrouter: reasoning_content replay statement missing')
88 }
89 return true
91​
92/** Read actual canonical effort wire fields from the native normalization table. */
93export function openRouterGatewayEffortWire(markdown: string): {
94 outer: string
95 field: string
96} {
97 const prose = markdown.replace(/```[\s\S]*?```/g, '')
98 const section = prose
99 .split(/^## Reasoning with the Anthropic Messages API\s*$/m)[1]
100 ?.split(/^#{1,2} /m)[0]
101 if (
102 !section ||
103 !/^Requests sent through[^\n]*OpenRouter normalizes both into the unified `reasoning` parameter, so they work on every reasoning model, not only Claude\./m.test(
104 section,
105 )
106 )
107 throw new Error('openrouter: native effort wire scope missing')
108 const row = section.match(
109 /^\| `output_config: \{ effort \}` \| `([a-z_]+): \{ ([a-z_]+) \}` \|$/m,
110 )
111 if (!row?.[1] || !row[2])
112 throw new Error('openrouter: native effort wire fields missing')
113 return { outer: row[1], field: row[2] }