Open History to see the last commit
1import { createFileRoute } from '@tanstack/react-router'
2import {
3 convertMessagesToModelMessages,
4 EventType,
5 modelMessagesToUIMessages,
6 StreamProcessor,
7 uiMessagesToWire,
8} from '@tanstack/ai'
9import { z } from 'zod'
10import type { UIMessage } from '@tanstack/ai'
11
12const sourceSchema = z.discriminatedUnion('type', [
13 z.object({
14 type: z.literal('data'),
15 value: z.string(),
16 mimeType: z.string(),
17 }),
18 z.object({
19 type: z.literal('url'),
20 value: z.string(),
21 mimeType: z.string().optional(),
22 }),
23])
24
25const contentPartSchema = z.discriminatedUnion('type', [
26 z.object({
27 type: z.literal('text'),
28 content: z.string(),
29 metadata: z.record(z.string(), z.unknown()).optional(),
30 }),
31 ...(['image', 'audio', 'video', 'document'] as const).map((type) =>
32 z.object({
33 type: z.literal(type),
34 source: sourceSchema,
35 metadata: z.record(z.string(), z.unknown()).optional(),
36 }),
37 ),
38])
39
40const messagePartSchema = z.discriminatedUnion('type', [
41 ...contentPartSchema.options,
42 z.object({
43 type: z.literal('tool-call'),
44 id: z.string(),
45 name: z.string(),
46 arguments: z.string(),
47 input: z.unknown().optional(),
48 state: z.enum([
49 'awaiting-input',
50 'input-streaming',
51 'input-complete',
52 'approval-requested',
53 'approval-responded',
54 'complete',
55 'error',
56 ]),
57 approval: z
58 .object({
59 id: z.string(),
60 needsApproval: z.boolean(),
61 approved: z.boolean().optional(),
62 })
63 .optional(),
64 output: z.unknown().optional(),
65 metadata: z.record(z.string(), z.unknown()).optional(),
66 }),
67 z.object({
68 type: z.literal('tool-result'),
69 id: z.string().optional(),
70 name: z.string().optional(),
71 toolCallId: z.string(),
72 content: z.union([z.string(), z.array(contentPartSchema)]),
73 state: z.enum(['streaming', 'complete', 'error']),
74 error: z.string().optional(),
75 metadata: z.record(z.string(), z.unknown()).optional(),
76 createdAt: z.coerce.date().optional(),
77 }),
78 z.object({
79 type: z.literal('thinking'),
80 content: z.string(),
81 stepId: z.string().optional(),
82 signature: z.string().optional(),
83 }),
84 z.object({
85 type: z.literal('structured-output'),
86 status: z.enum(['streaming', 'complete', 'error']),
87 partial: z.unknown().optional(),
88 data: z.unknown().optional(),
89 raw: z.string(),
90 reasoning: z.string().optional(),
91 errorMessage: z.string().optional(),
92 }),
93 z.object({
94 type: z.literal('ui-resource'),
95 resource: z.object({
96 uri: z.string(),
97 mimeType: z.string(),
98 text: z.string().optional(),
99 blob: z.string().optional(),
100 }),
101 serverId: z.string().optional(),
102 toolCallId: z.string(),
103 toolName: z.string(),
104 meta: z.record(z.string(), z.unknown()).optional(),
105 }),
106])
107
108const requestBodySchema: z.ZodType<{ messages: Array<UIMessage> }> = z.object({
109 messages: z.array(
110 z.object({
111 id: z.string(),
112 role: z.enum(['system', 'user', 'assistant']),
113 parts: z.array(messagePartSchema),
114 createdAt: z.coerce.date().optional(),
115 }),
116 ),
117})
118
119/**
120 * Provider-free harness for the UIMessage -> ModelMessage identity contract.
121 * The route keeps this regression at the public server boundary without
122 * starting a provider request or requiring an API key.
123 */
124export const Route = createFileRoute('/api/message-ids')({
125 server: {
126 handlers: {
127 POST: async ({ request }) => {
128 let body: unknown
129 try {
130 body = await request.json()
131 } catch {
132 return new Response('Invalid JSON request body', { status: 400 })
133 }
134
135 const parsed = requestBodySchema.safeParse(body)
136 if (!parsed.success) {
137 return new Response('Invalid message data', { status: 400 })
138 }
139
140 const modelMessages = convertMessagesToModelMessages(
141 parsed.data.messages,
142 )
143 const wireMessages = uiMessagesToWire(parsed.data.messages)
144 const processor = new StreamProcessor()
145 processor.processChunk({
146 type: EventType.MESSAGES_SNAPSHOT,
147 messages: wireMessages,
148 })
149 const snapshots = processor.getMessages()
150 const mergedSnapshots = modelMessagesToUIMessages(modelMessages)
151 return Response.json({
152 modelMessages,
153 wireMessages,
154 snapshots,
155 mergedSnapshots,
156 })
157 },
158 },
159 },
160})