Providers

Open in

The React integration is context-based. A provider higher in the tree creates the underlying ClientTransport (and, for the Vercel layer, the ChatTransport adapter) and exposes them through React context. Every hook in this section reads from the nearest provider.

There are two providers. Use TransportProvider from the generic entry point when you build a UI directly against AI Transport's hooks. Use ChatTransportProvider from the Vercel entry point when you wire AI Transport into Vercel's useChat. ChatTransportProvider internally wraps its children with a TransportProvider, so both layers' hooks resolve from a single provider.

React

1

2

import { TransportProvider } from '@ably/ai-transport/react';
import { ChatTransportProvider } from '@ably/ai-transport/vercel/react';

TransportProvider

Creates a ClientTransport and makes it available to descendant components. Wraps children with Ably's ChannelProvider using the supplied channelName. Descendants read the transport with useClientTransport.

React

1

2

3

4

5

6

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

<TransportProvider channelName="conversation-42" codec={UIMessageCodec} api="/api/chat">
  <Chat />
</TransportProvider>

Parameters

The provider accepts every ClientTransportOptions field except channel (managed internally), plus a channelName.

channelNamerequiredString
The Ably channel name to subscribe to. Also the context registry key.
codecrequiredCodec<TEvent, TMessage>
The codec to use for encoding and decoding messages.
apirequiredString
Server endpoint URL for the HTTP POST that triggers a new turn.
clientIdoptionalString
The client's identity. Sent to the server in the POST body.
headersoptionalRecord<string, string> or () => Record<string, string>
Headers for the HTTP POST. Function form for dynamic values.
bodyoptionalRecord<string, unknown> or () => Record<string, unknown>
Additional body fields merged into the HTTP POST. Function form for dynamic values.
credentialsoptionalRequestCredentials
Fetch credentials mode for the HTTP POST.
fetchoptionaltypeof fetch
Custom fetch implementation.
messagesoptionalTMessage[]
Initial messages to seed the conversation tree with.
loggeroptionalLogger
Logger instance for diagnostic output.

If createClientTransport throws during construction, the error is surfaced through useClientTransport as transportError — the component tree does not crash and children are still rendered.

For multiple transports, nest providers with distinct channelName values. Descendants call useClientTransport({ channelName }) to disambiguate.

ChatTransportProvider

Creates a ChatTransport adapter for Vercel's useChat, alongside the underlying ClientTransport. Wraps children with a TransportProvider using UIMessageCodec, so the Ably channel lifecycle is managed in one place.

React

1

2

3

4

5

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

<ChatTransportProvider channelName="conversation-42">
  <Chat />
</ChatTransportProvider>

Parameters

The provider accepts every VercelClientTransportOptions field (defaulting api to /api/chat), plus a channelName.

channelNamerequiredString
The Ably channel name to subscribe to. Also the context registry key.
apioptionalString
Server endpoint URL for the HTTP POST. Defaults to /api/chat.
clientIdoptionalString
The client's identity.
headersoptionalRecord<string, string> or () => Record<string, string>
Headers for the HTTP POST.
bodyoptionalRecord<string, unknown> or () => Record<string, unknown>
Additional body fields merged into the HTTP POST.
credentialsoptionalRequestCredentials
Fetch credentials mode.
fetchoptionaltypeof fetch
Custom fetch implementation.
messagesoptionalUIMessage[]
Initial messages to seed the conversation tree with.
loggeroptionalLogger
Logger instance.
chatOptionsoptionalChatTransportOptions
ChatTransportOptions for customising the adapter (for example, prepareSendMessagesRequest).

ChatTransport itself is not closed on unmount. The underlying ClientTransport's lifecycle is managed by the wrapping TransportProvider. Multiple ChatTransportProviders can be nested using distinct channelName values; each merges its transport into the parent registry, so descendants can access multiple transports via useChatTransport({ channelName }).

Example

React

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

'use client';

import { useEffect, useState } from 'react';
import * as Ably from 'ably';
import { AblyProvider } from 'ably/react';
import { ChatTransportProvider } from '@ably/ai-transport/vercel/react';
import { Chat } from './chat';

function Providers({ children }) {
  const [client, setClient] = useState(null);

  useEffect(() => {
    const ably = new Ably.Realtime({ authUrl: '/auth' });
    setClient(ably);
    return () => ably.close();
  }, []);

  if (!client) return null;
  return <AblyProvider client={client}>{children}</AblyProvider>;
}

export default function Page() {
  const chatId = 'conversation-42';
  return (
    <Providers>
      <ChatTransportProvider channelName={chatId}>
        <Chat chatId={chatId} />
      </ChatTransportProvider>
    </Providers>
  );
}