|
| 1 | +# AIT-676: Options for the `api` default |
| 2 | + |
| 3 | +## Background |
| 4 | + |
| 5 | +The default HTTP endpoint `"/api/chat"` is currently hardcoded in the generic core transport, but it's a Vercel AI SDK / Next.js convention. It should not live in the generic layer. |
| 6 | + |
| 7 | +The Vercel AI SDK's own `HttpChatTransport` defaults to `"/api/chat"` internally — it's not exported as a constant, so we'd have to hardcode it too if we want to mirror the default. |
| 8 | + |
| 9 | +Two options for how the README `useChat` example changes: |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## Option A: Make `api` required everywhere |
| 14 | + |
| 15 | +Simplest change. Remove the default from core, make `api` required on `ClientTransportOptions`. All callers must specify it explicitly. No new types or return type changes. |
| 16 | + |
| 17 | +```diff |
| 18 | + import { useChat } from '@ai-sdk/react'; |
| 19 | + import { useChannel } from 'ably/react'; |
| 20 | + import { useClientTransport, useActiveTurns, useView } from '@ably/ai-transport/react'; |
| 21 | + import { useChatTransport, useMessageSync } from '@ably/ai-transport/vercel/react'; |
| 22 | + import { UIMessageCodec } from '@ably/ai-transport/vercel'; |
| 23 | + |
| 24 | + function Chat({ chatId, clientId }: { chatId: string; clientId?: string }) { |
| 25 | + const { channel } = useChannel({ channelName: chatId }); |
| 26 | + |
| 27 | +- const transport = useClientTransport({ channel, codec: UIMessageCodec, clientId }); |
| 28 | ++ const transport = useClientTransport({ channel, codec: UIMessageCodec, clientId, api: '/api/chat' }); |
| 29 | + const chatTransport = useChatTransport(transport); |
| 30 | + |
| 31 | + const { messages, setMessages, sendMessage, stop } = useChat({ |
| 32 | + id: chatId, |
| 33 | + transport: chatTransport, |
| 34 | + }); |
| 35 | + |
| 36 | + useMessageSync(transport, setMessages); |
| 37 | + |
| 38 | + const activeTurns = useActiveTurns(transport); |
| 39 | + const view = useView(transport, { limit: 30 }); |
| 40 | +``` |
| 41 | + |
| 42 | +**Pros:** Simple implementation (one small commit), explicit, no new abstractions. |
| 43 | +**Cons:** `useChat` users must specify `api` even though Vercel's own default transport doesn't require it. |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +## Option B: Preserve the default in the Vercel layer |
| 48 | + |
| 49 | +Make `api` required in core but optional in the Vercel layer. `useChatTransport` gains the ability to create the transport internally (applying the `"/api/chat"` default) and returns both the `ChatTransport` and the underlying `ClientTransport`. |
| 50 | + |
| 51 | +```diff |
| 52 | + import { useChat } from '@ai-sdk/react'; |
| 53 | + import { useChannel } from 'ably/react'; |
| 54 | +-import { useClientTransport, useActiveTurns, useView } from '@ably/ai-transport/react'; |
| 55 | ++import { useActiveTurns, useView } from '@ably/ai-transport/react'; |
| 56 | + import { useChatTransport, useMessageSync } from '@ably/ai-transport/vercel/react'; |
| 57 | +-import { UIMessageCodec } from '@ably/ai-transport/vercel'; |
| 58 | + |
| 59 | + function Chat({ chatId, clientId }: { chatId: string; clientId?: string }) { |
| 60 | + const { channel } = useChannel({ channelName: chatId }); |
| 61 | + |
| 62 | +- const transport = useClientTransport({ channel, codec: UIMessageCodec, clientId }); |
| 63 | +- const chatTransport = useChatTransport(transport); |
| 64 | ++ const { chatTransport, transport } = useChatTransport({ channel, clientId }); |
| 65 | + |
| 66 | + const { messages, setMessages, sendMessage, stop } = useChat({ |
| 67 | + id: chatId, |
| 68 | + transport: chatTransport, |
| 69 | + }); |
| 70 | + |
| 71 | + useMessageSync(transport, setMessages); |
| 72 | + |
| 73 | + const activeTurns = useActiveTurns(transport); |
| 74 | + const view = useView(transport, { limit: 30 }); |
| 75 | +``` |
| 76 | + |
| 77 | +**Pros:** Matches Vercel's own ergonomics (no endpoint needed), fewer imports, fewer lines. |
| 78 | +**Cons:** More complex implementation — new `ChatTransportHandle` return type, `useChatTransport` takes on transport creation responsibility. |
0 commit comments