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

api.openrouter-reasoning-wire.ts

66 lines2.2 KB

1import { createFileRoute } from '@tanstack/react-router'
2import { chat, createChatOptions } from '@tanstack/ai'
3import { createOpenRouterText } from '@tanstack/ai-openrouter'
4import { HTTPClient } from '@openrouter/sdk'
5​
6const LLMOCK_DEFAULT_BASE = process.env.LLMOCK_URL || 'http://127.0.0.1:4010'
7const DUMMY_KEY = 'sk-e2e-test-dummy-key'
8​
9/**
10 * Drives the real OpenRouter SDK request path with `chat({ reasoning: 'off' })`
11 * or no reasoning at all. The companion spec inspects aimock's request journal
12 * to verify the adapter sends `effort: 'none'` or omits the field before the
13 * SDK serializes the request body.
14 */
15export const Route = createFileRoute('/api/openrouter-reasoning-wire')({
16 server: {
17 handlers: {
18 POST: async ({ request }) => {
19 const url = new URL(request.url)
20 const testId = url.searchParams.get('testId') ?? undefined
21 const scenario =
22 url.searchParams.get('scenario') === 'empty' ? 'empty' : 'disabled'
23​
24 const httpClient = new HTTPClient()
25 if (testId) {
26 httpClient.addHook('beforeRequest', (req) => {
27 const next = new Request(req)
28 next.headers.set('X-Test-Id', testId)
29 return next
30 })
31 }
32​
33 const adapter = createOpenRouterText('openai/gpt-5.5', DUMMY_KEY, {
34 serverURL: `${LLMOCK_DEFAULT_BASE}/v1`,
35 httpClient,
36 })
37​
38 try {
39 for await (const _ of chat({
40 threadId: 'openrouter-reasoning-wire',
41 ...createChatOptions({ adapter }),
42 messages: [
43 {
44 role: 'user',
45 content:
46 scenario === 'empty'
47 ? '[reasoning-wire] empty reasoning'
48 : '[reasoning-wire] disable reasoning',
49 },
50 ],
51 ...(scenario === 'empty' ? {} : { reasoning: 'off' as const }),
52 })) {
53 // Drain the stream.
54 }
55 } catch (error) {
56 return Response.json({
57 ok: false,
58 error: error instanceof Error ? error.message : String(error),
59 })
60 }
61​
62 return Response.json({ ok: true })
63 },
64 },
65 },
66})