Open History to see the last commit
1import { createFileRoute } from '@tanstack/react-router'
2import { chat } from '@tanstack/ai'
3import { keyedAdapter, keyedAdapters } from '@tanstack/ai/byok'
4import {
5 byokMissing,
6 getByokKey,
7 keyedAdapterFromRequest,
8} from '@tanstack/ai/byok/server'
9import { createAnthropicChat } from '@tanstack/ai-anthropic'
10import { createOpenaiChat } from '@tanstack/ai-openai'
11
12const LLMOCK_DEFAULT_BASE = process.env.LLMOCK_URL || 'http://127.0.0.1:4010'
13
14const messages = [
15 {
16 role: 'user' as const,
17 content: '[oneshot] what is your most popular guitar',
18 },
19]
20
21/**
22 * `?mode=many`: `keyedAdapters` with OpenAI and Anthropic. The route builds the
23 * adapter of the provider whose key the user sent, and reports which provider
24 * got which credential.
25 */
26async function many(request: Request, testId: string | undefined) {
27 const sent: { provider?: string; credential?: string | null } = {}
28 const record =
29 (provider: string, header: string): typeof fetch =>
30 (input, init) => {
31 sent.provider = provider
32 sent.credential = new Headers(init?.headers).get(header)
33 return fetch(input, init)
34 }
35 const defaultHeaders = testId ? { 'X-Test-Id': testId } : undefined
36 const models = keyedAdapters({
37 openai: (key) =>
38 createOpenaiChat('gpt-5.2', key, {
39 baseURL: `${LLMOCK_DEFAULT_BASE}/v1`,
40 defaultHeaders,
41 fetch: record('openai', 'authorization'),
42 }),
43 anthropic: (key) =>
44 createAnthropicChat('claude-haiku-4-5', key, {
45 baseURL: LLMOCK_DEFAULT_BASE,
46 defaultHeaders,
47 fetch: record('anthropic', 'x-api-key'),
48 }),
49 })
50 const adapter = keyedAdapterFromRequest(request, models)
51 if (!adapter) return byokMissing('openai')
52 const { text } = await chat({
53 threadId: 'keyed-adapter',
54 adapter,
55 messages,
56 stream: false,
57 })
58 return Response.json({ text, ...sent })
59}
60
61/**
62 * Builds a `keyedAdapter` with the user's key from the BYOK request header,
63 * then chats against aimock. aimock redacts `authorization` in its journal,
64 * so the adapter's `fetch` records the header the provider gets.
65 */
66export const Route = createFileRoute('/api/keyed-adapter')({
67 server: {
68 handlers: {
69 POST: async ({ request }) => {
70 let authorization: string | null = null
71 const testId = request.headers.get('x-test-id') ?? undefined
72 if (new URL(request.url).searchParams.get('mode') === 'many') {
73 return many(request, testId)
74 }
75 const adapter = keyedAdapter('openai', (key) =>
76 createOpenaiChat('gpt-5.2', key, {
77 baseURL: `${LLMOCK_DEFAULT_BASE}/v1`,
78 defaultHeaders: testId ? { 'X-Test-Id': testId } : undefined,
79 fetch: (input, init) => {
80 authorization = new Headers(init?.headers).get('authorization')
81 return fetch(input, init)
82 },
83 }),
84 )
85
86 const key = getByokKey(request, adapter.provider)
87 if (!key) return byokMissing('openai')
88
89 const { text } = await chat({
90 threadId: 'keyed-adapter',
91 adapter: adapter.create(key),
92 messages,
93 stream: false,
94 })
95 return Response.json({ text, authorization })
96 },
97 },
98 },
99})