Open History to see the last commit
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
24}
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 }
63}
64
65/** Read actual canonical effort wire fields from the native normalization table. */
66export function openRouterGatewayEffortWire(markdown: string): {
67 outer: string
68 field: string
69} {
70 const prose = markdown.replace(/```[\s\S]*?```/g, '')
71 const section = prose
72 .split(/^## Reasoning with the Anthropic Messages API\s*$/m)[1]
73 ?.split(/^#{1,2} /m)[0]
74 if (
75 !section ||
76 !/^Requests sent through[^\n]*OpenRouter normalizes both into the unified `reasoning` parameter, so they work on every reasoning model, not only Claude\./m.test(
77 section,
78 )
79 )
80 throw new Error('openrouter: native effort wire scope missing')
81 const row = section.match(
82 /^\| `output_config: \{ effort \}` \| `([a-z_]+): \{ ([a-z_]+) \}` \|$/m,
83 )
84 if (!row?.[1] || !row[2])
85 throw new Error('openrouter: native effort wire fields missing')
86 return { outer: row[1], field: row[2] }
87}