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

api.chat.ts

34 lines995 Bytes

1import { createFileRoute } from '@tanstack/react-router'
2import {
3 chat,
4 chatParamsFromRequest,
5 toServerSentEventsResponse,
6} from '@tanstack/ai'
7import { createOpenRouterText } from '@tanstack/ai-openrouter'
8import { openrouterByok } from '@tanstack/ai-openrouter/byok'
9import { byokMissing, getByokKey } from '@tanstack/ai/byok/server'
10import { tableSchema } from '@/lib/table-schema'
11​
12export async function POST({ request }: { request: Request }) {
13 const params = await chatParamsFromRequest(request)
14 const apiKey = getByokKey(request, openrouterByok)
15 if (!apiKey) return byokMissing(openrouterByok)
16​
17 const stream = chat({
18 adapter: createOpenRouterText('openai/gpt-5.5', apiKey),
19 messages: params.messages,
20 threadId: params.threadId,
21 runId: params.runId,
22 outputSchema: tableSchema,
23 stream: true,
24 })
25 return toServerSentEventsResponse(stream)
27​
28export const Route = createFileRoute('/api/chat')({
29 server: {
30 handlers: {
31 POST,
32 },
33 },
34})