Vercel integration API

Open in

The Vercel integration provides a pre-built codec and transport factories for the Vercel AI SDK. It maps Vercel's UIMessageChunk events to UIMessage objects, so you can use AI Transport with useChat without writing a custom codec.

Entry points

Entry pointImport pathContents
Vercel@ably/ai-transport/vercelUIMessageCodec, createClientTransport, createServerTransport
Vercel React@ably/ai-transport/vercel/reactuseChatTransport, useMessageSync

UIMessageCodec

A pre-built Codec<UIMessageChunk, UIMessage> for the Vercel AI SDK. It handles encoding and decoding of Vercel's message types through Ably channels.

JavaScript

1

import { UIMessageCodec } from '@ably/ai-transport/vercel'

You do not need to use UIMessageCodec directly if you use the Vercel-specific transport factories or React hooks, as they bind the codec automatically.

createClientTransport

A pre-bound version of the core createClientTransport that omits the codec option. The codec is set to UIMessageCodec.

JavaScript

1

2

3

import { createClientTransport } from '@ably/ai-transport/vercel'

function createClientTransport(options: VercelClientTransportOptions): ClientTransport

VercelClientTransportOptions

Identical to ClientTransportOptions but without the codec property.

PropertyRequiredTypeDescription
channelrequiredAbly.RealtimeChannelThe Ably channel for the session.
clientIdoptionalstringThe client ID for this transport instance.
apioptionalstringURL of the API endpoint.
headersoptionalRecord<string, string>Additional HTTP headers.
bodyoptionalRecord<string, unknown>Additional request body fields.
credentialsoptionalRequestCredentialsCredentials mode for fetch.
fetchoptionaltypeof fetchCustom fetch implementation.
messagesoptionalUIMessage[]Pre-loaded messages for history.
loggeroptionalLoggerLogger instance.

createServerTransport

A pre-bound version of the core createServerTransport that omits the codec option.

JavaScript

1

2

3

import { createServerTransport } from '@ably/ai-transport/vercel'

function createServerTransport(options: VercelServerTransportOptions): ServerTransport

VercelServerTransportOptions

Identical to ServerTransportOptions but without the codec property.

PropertyRequiredTypeDescription
channelrequiredAbly.RealtimeChannelThe Ably channel for the session.
loggeroptionalLoggerLogger instance.
onErroroptional(error: ErrorInfo) => voidCallback for transport-level errors.

createChatTransport

Create a ChatTransport from a ClientTransport. The ChatTransport is the adapter that plugs into Vercel's useChat hook.

JavaScript

1

2

3

import { createChatTransport } from '@ably/ai-transport/vercel'

function createChatTransport(transport: ClientTransport, options?: ChatTransportOptions): ChatTransport

In most cases you will use the useChatTransport hook instead of calling this factory directly.

useChatTransport

React hook that creates a ChatTransport for use with Vercel's useChat. Accepts either a ChatTransportOptions object or an existing ClientTransport instance.

JavaScript

1

2

3

4

import { useChatTransport } from '@ably/ai-transport/vercel/react'

function useChatTransport(options: ChatTransportOptions): ChatTransport
function useChatTransport(transport: ClientTransport): ChatTransport

When you pass ChatTransportOptions, the hook creates a ClientTransport internally with the codec pre-bound to UIMessageCodec. When you pass an existing ClientTransport, it wraps that instance as a ChatTransport without creating a new transport. Use the second form when you need direct access to the ClientTransport for features like active turn tracking or cancellation.

See the React hooks reference for full usage examples.

ChatTransportOptions

Options for useChatTransport (from @ably/ai-transport/vercel/react).

PropertyRequiredTypeDescription
channelrequiredAbly.RealtimeChannelThe Ably channel for the session.
clientIdoptionalstringThe client ID for this transport instance.
apioptionalstringURL of the API endpoint.
headersoptionalRecord<string, string>Additional HTTP headers.
bodyoptionalRecord<string, unknown>Additional request body fields.
credentialsoptionalRequestCredentialsCredentials mode for fetch.
fetchoptionaltypeof fetchCustom fetch implementation.
messagesoptionalUIMessage[]Pre-loaded messages for history.
loggeroptionalLoggerLogger instance.
prepareSendMessagesRequestoptional(context: SendMessagesRequestContext) => { body?: Record<string, unknown>; headers?: Record<string, string> }Hook to customize the HTTP POST body and headers before sending to the API. Receives conversation context including history, new messages, and trigger type.