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

api.reasoning-wire.ts

113 lines4.0 KB

1import { createFileRoute } from '@tanstack/react-router'
2import { chat } from '@tanstack/ai'
3import { createGeminiChat } from '@tanstack/ai-gemini'
4import { createAnthropicChat } from '@tanstack/ai-anthropic'
5import { createOpenaiChat } from '@tanstack/ai-openai'
6import { grokVertexText } from '@tanstack/ai-grok/vertex'
7import { vertexE2eAuthClient } from '@/lib/vertex-e2e'
8import type {
9 AnyTextAdapter,
10 FetchWrapper,
11 ReasoningRequest,
12} from '@tanstack/ai'
13​
14const LLMOCK_DEFAULT_BASE = process.env.LLMOCK_URL || 'http://127.0.0.1:4010'
15const DUMMY_KEY = 'sk-e2e-test-dummy-key'
16​
17const reasoning: ReasoningRequest = { level: 'medium', summary: true }
18// Gemini 2.5 Flash takes only `off` and `high`, so ask for `high` with an
19// explicit budget. The mount checks the budget on the wire.
20const geminiReasoning: ReasoningRequest = {
21 level: 'high',
22 summary: true,
23 budgetTokens: 8192,
25// grok-4.20-reasoning always thinks and has no levels, so even `off` sends
26// no reasoning field.
27const grokReasoning: ReasoningRequest = { level: 'off', summary: true }
28const messages = [
29 {
30 role: 'user' as const,
31 content: '[reasoning] recommend a guitar for a beginner',
32 },
34​
35/**
36 * Wire check for `chat({ reasoning })`. OpenAI and Anthropic record the raw
37 * request body with `wrapFetch`, because aimock's journal normalizes bodies.
38 * Gemini posts to the `/reasoning-wire-gemini` mount, which answers only when
39 * `generationConfig.thinkingConfig` is right.
40 */
41export const Route = createFileRoute('/api/reasoning-wire')({
42 server: {
43 handlers: {
44 POST: async ({ request }) => {
45 const url = new URL(request.url)
46 const testId = url.searchParams.get('testId') ?? undefined
47 const provider = url.searchParams.get('provider')
48​
49 const bodies: Array<Record<string, unknown>> = []
50 const capture: FetchWrapper = (next) => async (input, init) => {
51 const body = new Request(input, init).clone()
52 bodies.push(JSON.parse(await body.text()) as Record<string, unknown>)
53 return next(input, init)
54 }
55​
56 const headers = testId ? { 'X-Test-Id': testId } : undefined
57 const adapter: AnyTextAdapter =
58 provider === 'gemini'
59 ? createGeminiChat('gemini-2.5-flash', DUMMY_KEY, {
60 baseURL: `${LLMOCK_DEFAULT_BASE}/reasoning-wire-gemini`,
61 })
62 : provider === 'anthropic' || provider === 'anthropic-adaptive'
63 ? createAnthropicChat(
64 provider === 'anthropic'
65 ? 'claude-sonnet-4-5'
66 : 'claude-sonnet-4-6',
67 DUMMY_KEY,
68 {
69 baseURL: LLMOCK_DEFAULT_BASE,
70 defaultHeaders: headers,
71 },
72 )
73 : provider === 'grok'
74 ? grokVertexText('grok-4.20-reasoning', {
75 project: 'e2e-project',
76 location: 'global',
77 baseURL: `${LLMOCK_DEFAULT_BASE}/v1`,
78 authClient: vertexE2eAuthClient(),
79 defaultHeaders: headers,
80 })
81 : createOpenaiChat('o3', DUMMY_KEY, {
82 baseURL: `${LLMOCK_DEFAULT_BASE}/v1`,
83 defaultHeaders: headers,
84 })
85​
86 let text = ''
87 try {
88 for await (const chunk of chat({
89 adapter,
90 messages,
91 reasoning:
92 provider === 'gemini'
93 ? geminiReasoning
94 : provider === 'grok'
95 ? grokReasoning
96 : reasoning,
97 wrapFetch: capture,
98 })) {
99 if (chunk.type === 'TEXT_MESSAGE_CONTENT') text += chunk.delta
100 }
101 } catch (error) {
102 return Response.json({
103 ok: false,
104 error: error instanceof Error ? error.message : String(error),
105 bodies,
106 })
107 }
108​
109 return Response.json({ ok: true, text, bodies })
110 },
111 },
112 },
113})