-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
65 lines (57 loc) · 2.84 KB
/
index.ts
File metadata and controls
65 lines (57 loc) · 2.84 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* Vercel AI SDK transport wrappers that pre-bind the UIMessageCodec.
*
* These are convenience factories so consumers don't need to pass the codec
* explicitly when using the Vercel AI SDK integration.
*
* ```ts
* import { createClientTransport } from '@ably/ai-transport/vercel';
*
* const transport = createClientTransport({ channel });
* ```
*/
// Chat transport adapter
export type { ChatTransport, ChatTransportOptions, SendMessagesRequestContext } from './chat-transport.js';
export { createChatTransport } from './chat-transport.js';
import type * as AI from 'ai';
import { createClientTransport as createCoreClientTransport } from '../../core/transport/client-transport.js';
import { createServerTransport as createCoreServerTransport } from '../../core/transport/server-transport.js';
import type {
ClientTransport,
ClientTransportOptions,
ServerTransport,
ServerTransportOptions,
} from '../../core/transport/types.js';
import { UIMessageCodec } from '../codec/index.js';
/** Core client transport options with Vercel AI SDK types pre-applied. */
type CoreClientOpts = ClientTransportOptions<AI.UIMessageChunk, AI.UIMessage>;
/** Options for creating a Vercel client transport. Same as core options but without the codec field, and with `api` optional (defaults to `"/api/chat"`). */
export type VercelClientTransportOptions = Omit<CoreClientOpts, 'codec' | 'api'> & Partial<Pick<CoreClientOpts, 'api'>>;
/** Options for creating a Vercel server transport. Same as core options but without the codec field. */
export type VercelServerTransportOptions = Omit<ServerTransportOptions<AI.UIMessageChunk, AI.UIMessage>, 'codec'>;
/**
* Create a client-side transport pre-configured with the Vercel AI SDK codec.
*
* Equivalent to calling the core `createClientTransport` with `codec: UIMessageCodec`.
* @param options - Configuration for the client transport (codec is provided automatically).
* @returns A new {@link ClientTransport} for Vercel AI SDK UIMessage/UIMessageChunk types.
*/
export const createClientTransport = (
options: VercelClientTransportOptions,
): ClientTransport<AI.UIMessageChunk, AI.UIMessage> =>
createCoreClientTransport({
...options,
codec: UIMessageCodec,
// Mirrors the Vercel AI SDK's DefaultChatTransport default.
api: options.api ?? '/api/chat',
});
/**
* Create a server-side transport pre-configured with the Vercel AI SDK codec.
*
* Equivalent to calling the core `createServerTransport` with `codec: UIMessageCodec`.
* @param options - Configuration for the server transport (codec is provided automatically).
* @returns A new {@link ServerTransport} for Vercel AI SDK UIMessage/UIMessageChunk types.
*/
export const createServerTransport = (
options: VercelServerTransportOptions,
): ServerTransport<AI.UIMessageChunk, AI.UIMessage> => createCoreServerTransport({ ...options, codec: UIMessageCodec });