You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
transport: move /api/chat default from core to Vercel layer
The default HTTP endpoint "/api/chat" is the Vercel AI SDK's default
(set by DefaultChatTransport), but we were setting it in our generic
core transport. This violates the two-layer architecture principle
that the generic layer should know nothing about Vercel.
Make `api` required on core `ClientTransportOptions` and apply the
"/api/chat" default in the Vercel layer's `createClientTransport`
factory.
To avoid forcing useChat users to call `useClientTransport` (where
`api` would now be required), `useChatTransport` now returns a
`ChatTransportHandle` containing both the `ChatTransport` and the
underlying `ClientTransport`. Previously it only returned the
`ChatTransport`, so callers had no way to access the transport it
created internally — they needed a separate `useClientTransport`
call. The handle lets useChat users get the Vercel default without
extra configuration, while still having access to the transport for
`useMessageSync`, `useActiveTurns`, `useView`, etc.
[AIT-676]
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: docs/frameworks/vercel-ai-sdk.md
+3-6Lines changed: 3 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,13 +20,10 @@ The Vercel AI SDK provides model abstraction, streaming primitives, and React ho
20
20
Wrap the transport in a `ChatTransport` adapter and pass it to Vercel's `useChat`. Message state is managed by `useChat` - the transport delivers messages over Ably instead of HTTP.
`useChatTransport`wraps the core transport into the `ChatTransport` interface that `useChat` expects. `useMessageSync` pushes the transport's authoritative message list into `useChat`'s state - this is how messages from other clients appear.
37
+
`useChatTransport`creates the core transport and wraps it into the `ChatTransport` interface that `useChat` expects, returning both as `{ chatTransport, transport }`. `useMessageSync` pushes the transport's authoritative message list into `useChat`'s state - this is how messages from other clients appear.
//4. Sync transport messages into useChat's state (for observer messages)
176
+
//3. Sync transport messages into useChat's state (for observer messages)
181
177
useMessageSync(transport, setMessages);
182
178
183
-
//5. Track active turns for loading state
179
+
//4. Track active turns for loading state
184
180
const activeTurns =useActiveTurns(transport);
185
181
const isStreaming =activeTurns.size>0;
186
182
187
-
//6. Load history on mount
183
+
//5. Load history on mount
188
184
useView(transport, { limit: 30 });
189
185
190
186
return (
@@ -245,12 +241,11 @@ Open `http://localhost:3000`. Type a message - you'll see tokens stream in real
245
241
246
242
## What's happening
247
243
248
-
1.`useClientTransport` creates a transport that subscribes to the Ably channel before it attaches - no messages are lost.
249
-
2.`useChatTransport` wraps the transport into Vercel's `ChatTransport` interface, which `useChat` expects.
250
-
3. When you send a message, `useChat` calls the chat transport's `sendMessages`, which fires an HTTP POST to `/api/chat` and opens a stream on the Ably channel.
251
-
4. The server creates a turn, publishes user messages, streams the LLM response through the encoder to the channel, and publishes a turn-end event.
252
-
5. The client transport decodes incoming Ably messages through `UIMessageCodec` and routes them to the stream.
253
-
6.`useMessageSync` syncs messages from the transport (including messages from other clients) into `useChat`'s state.
244
+
1.`useChatTransport` creates a transport that subscribes to the Ably channel before it attaches (no messages are lost), and wraps it into Vercel's `ChatTransport` interface, which `useChat` expects. It returns both the `chatTransport` adapter and the underlying `transport`.
245
+
2. When you send a message, `useChat` calls the chat transport's `sendMessages`, which fires an HTTP POST to `/api/chat` and opens a stream on the Ably channel.
246
+
3. The server creates a turn, publishes user messages, streams the LLM response through the encoder to the channel, and publishes a turn-end event.
247
+
4. The client transport decodes incoming Ably messages through `UIMessageCodec` and routes them to the stream.
248
+
5.`useMessageSync` syncs messages from the transport (including messages from other clients) into `useChat`'s state.
254
249
255
250
For the conceptual details, see [Client and server transport](../concepts/transport.md) and [Turns](../concepts/turns.md).
| `transport` | `ClientTransport<UIMessageChunk, UIMessage>` | The underlying transport for use with `useMessageSync`, `useActiveTurns`, `useView`, etc. |
261
269
262
-
`ChatTransportOptions.prepareSendMessagesRequest` lets you customize the HTTP POST body and headers:
270
+
`ChatTransportOptions.prepareSendMessagesRequest` lets you customise the HTTP POST body and headers:
0 commit comments