@ably/ai-transport 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +93 -111
- package/dist/ably-ai-transport.js +2401 -1387
- package/dist/ably-ai-transport.js.map +1 -1
- package/dist/ably-ai-transport.umd.cjs +1 -1
- package/dist/ably-ai-transport.umd.cjs.map +1 -1
- package/dist/constants.d.ts +116 -42
- package/dist/core/agent.d.ts +44 -0
- package/dist/core/channel-options.d.ts +57 -0
- package/dist/core/codec/codec-event.d.ts +9 -0
- package/dist/core/codec/decoder.d.ts +24 -24
- package/dist/core/codec/define-codec.d.ts +100 -0
- package/dist/core/codec/encoder.d.ts +10 -12
- package/dist/core/codec/field-bag.d.ts +85 -0
- package/dist/core/codec/fields.d.ts +141 -0
- package/dist/core/codec/index.d.ts +8 -2
- package/dist/core/codec/input-descriptor-decoder.d.ts +19 -0
- package/dist/core/codec/input-descriptor-encoder.d.ts +22 -0
- package/dist/core/codec/input-descriptors.d.ts +281 -0
- package/dist/core/codec/lifecycle-tracker.d.ts +10 -9
- package/dist/core/codec/output-descriptor-decoder.d.ts +29 -0
- package/dist/core/codec/output-descriptor-encoder.d.ts +31 -0
- package/dist/core/codec/output-descriptors.d.ts +237 -0
- package/dist/core/codec/types.d.ts +470 -119
- package/dist/core/codec/well-known-inputs.d.ts +52 -0
- package/dist/core/transport/agent-session.d.ts +10 -0
- package/dist/core/transport/agent-view.d.ts +296 -0
- package/dist/core/transport/client-session.d.ts +13 -0
- package/dist/core/transport/decode-fold.d.ts +55 -0
- package/dist/core/transport/headers.d.ts +121 -14
- package/dist/core/transport/index.d.ts +5 -6
- package/dist/core/transport/internal/bounded-map.d.ts +20 -0
- package/dist/core/transport/invocation.d.ts +74 -0
- package/dist/core/transport/load-history-pages.d.ts +71 -0
- package/dist/core/transport/load-history.d.ts +44 -0
- package/dist/core/transport/pipe-stream.d.ts +9 -9
- package/dist/core/transport/run-manager.d.ts +76 -0
- package/dist/core/transport/session-support.d.ts +55 -0
- package/dist/core/transport/tree.d.ts +523 -109
- package/dist/core/transport/types/agent.d.ts +375 -0
- package/dist/core/transport/types/client.d.ts +201 -0
- package/dist/core/transport/types/shared.d.ts +24 -0
- package/dist/core/transport/types/tree.d.ts +357 -0
- package/dist/core/transport/types/view.d.ts +249 -0
- package/dist/core/transport/types.d.ts +13 -553
- package/dist/core/transport/view.d.ts +390 -84
- package/dist/core/transport/wire-log.d.ts +102 -0
- package/dist/errors.d.ts +27 -10
- package/dist/index.d.ts +8 -9
- package/dist/logger.d.ts +12 -0
- package/dist/react/ably-ai-transport-react.js +1365 -1010
- package/dist/react/ably-ai-transport-react.js.map +1 -1
- package/dist/react/ably-ai-transport-react.umd.cjs +1 -1
- package/dist/react/ably-ai-transport-react.umd.cjs.map +1 -1
- package/dist/react/contexts/client-session-context.d.ts +37 -0
- package/dist/react/contexts/client-session-provider.d.ts +56 -0
- package/dist/react/create-session-hooks.d.ts +116 -0
- package/dist/react/index.d.ts +13 -12
- package/dist/react/internal/skipped-session.d.ts +8 -0
- package/dist/react/internal/use-resolved-session.d.ts +36 -0
- package/dist/react/use-ably-messages.d.ts +17 -14
- package/dist/react/use-client-session.d.ts +81 -0
- package/dist/react/use-create-view.d.ts +14 -13
- package/dist/react/use-tree.d.ts +30 -15
- package/dist/react/use-view.d.ts +81 -50
- package/dist/utils.d.ts +48 -71
- package/dist/vercel/ably-ai-transport-vercel.js +3257 -2499
- package/dist/vercel/ably-ai-transport-vercel.js.map +1 -1
- package/dist/vercel/ably-ai-transport-vercel.umd.cjs +1 -1
- package/dist/vercel/ably-ai-transport-vercel.umd.cjs.map +1 -1
- package/dist/vercel/codec/decode-lifecycle.d.ts +9 -0
- package/dist/vercel/codec/events.d.ts +50 -0
- package/dist/vercel/codec/fields.d.ts +44 -0
- package/dist/vercel/codec/fold-content.d.ts +16 -0
- package/dist/vercel/codec/fold-data.d.ts +16 -0
- package/dist/vercel/codec/fold-input.d.ts +67 -0
- package/dist/vercel/codec/fold-lifecycle.d.ts +16 -0
- package/dist/vercel/codec/fold-text.d.ts +16 -0
- package/dist/vercel/codec/fold-tool-input.d.ts +17 -0
- package/dist/vercel/codec/fold-tool-output.d.ts +16 -0
- package/dist/vercel/codec/index.d.ts +7 -20
- package/dist/vercel/codec/inputs.d.ts +11 -0
- package/dist/vercel/codec/outputs.d.ts +11 -0
- package/dist/vercel/codec/reducer-state.d.ts +121 -0
- package/dist/vercel/codec/reducer.d.ts +62 -0
- package/dist/vercel/codec/tool-transitions.d.ts +2 -8
- package/dist/vercel/codec/wire-data.d.ts +34 -0
- package/dist/vercel/index.d.ts +5 -5
- package/dist/vercel/react/ably-ai-transport-vercel-react.js +2859 -9705
- package/dist/vercel/react/ably-ai-transport-vercel-react.js.map +1 -1
- package/dist/vercel/react/ably-ai-transport-vercel-react.umd.cjs +1 -45
- package/dist/vercel/react/ably-ai-transport-vercel-react.umd.cjs.map +1 -1
- package/dist/vercel/react/contexts/chat-transport-context.d.ts +9 -7
- package/dist/vercel/react/contexts/chat-transport-provider.d.ts +53 -41
- package/dist/vercel/react/index.d.ts +1 -2
- package/dist/vercel/react/use-chat-transport.d.ts +30 -26
- package/dist/vercel/react/use-message-sync.d.ts +17 -30
- package/dist/vercel/run-end-reason.d.ts +84 -0
- package/dist/vercel/tool-part.d.ts +21 -0
- package/dist/vercel/transport/chat-transport.d.ts +41 -24
- package/dist/vercel/transport/index.d.ts +24 -20
- package/dist/vercel/transport/run-output-stream.d.ts +54 -0
- package/dist/version.d.ts +2 -0
- package/package.json +31 -24
- package/src/constants.ts +124 -51
- package/src/core/agent.ts +92 -0
- package/src/core/channel-options.ts +89 -0
- package/src/core/codec/codec-event.ts +27 -0
- package/src/core/codec/decoder.ts +202 -105
- package/src/core/codec/define-codec.ts +432 -0
- package/src/core/codec/encoder.ts +114 -107
- package/src/core/codec/field-bag.ts +142 -0
- package/src/core/codec/fields.ts +193 -0
- package/src/core/codec/index.ts +56 -6
- package/src/core/codec/input-descriptor-decoder.ts +97 -0
- package/src/core/codec/input-descriptor-encoder.ts +150 -0
- package/src/core/codec/input-descriptors.ts +373 -0
- package/src/core/codec/lifecycle-tracker.ts +10 -9
- package/src/core/codec/output-descriptor-decoder.ts +139 -0
- package/src/core/codec/output-descriptor-encoder.ts +101 -0
- package/src/core/codec/output-descriptors.ts +307 -0
- package/src/core/codec/types.ts +505 -126
- package/src/core/codec/well-known-inputs.ts +96 -0
- package/src/core/transport/agent-session.ts +1085 -0
- package/src/core/transport/agent-view.ts +738 -0
- package/src/core/transport/client-session.ts +780 -0
- package/src/core/transport/decode-fold.ts +101 -0
- package/src/core/transport/headers.ts +234 -22
- package/src/core/transport/index.ts +27 -27
- package/src/core/transport/internal/bounded-map.ts +27 -0
- package/src/core/transport/invocation.ts +98 -0
- package/src/core/transport/load-history-pages.ts +220 -0
- package/src/core/transport/load-history.ts +271 -0
- package/src/core/transport/pipe-stream.ts +63 -39
- package/src/core/transport/run-manager.ts +243 -0
- package/src/core/transport/session-support.ts +96 -0
- package/src/core/transport/tree.ts +1293 -308
- package/src/core/transport/types/agent.ts +434 -0
- package/src/core/transport/types/client.ts +247 -0
- package/src/core/transport/types/shared.ts +27 -0
- package/src/core/transport/types/tree.ts +393 -0
- package/src/core/transport/types/view.ts +288 -0
- package/src/core/transport/types.ts +13 -706
- package/src/core/transport/view.ts +1229 -450
- package/src/core/transport/wire-log.ts +189 -0
- package/src/errors.ts +29 -9
- package/src/event-emitter.ts +3 -2
- package/src/index.ts +86 -42
- package/src/logger.ts +14 -1
- package/src/react/contexts/client-session-context.ts +41 -0
- package/src/react/contexts/client-session-provider.tsx +222 -0
- package/src/react/create-session-hooks.ts +141 -0
- package/src/react/index.ts +24 -13
- package/src/react/internal/skipped-session.ts +62 -0
- package/src/react/internal/use-resolved-session.ts +63 -0
- package/src/react/use-ably-messages.ts +32 -22
- package/src/react/use-client-session.ts +178 -0
- package/src/react/use-create-view.ts +33 -29
- package/src/react/use-tree.ts +61 -30
- package/src/react/use-view.ts +138 -96
- package/src/utils.ts +83 -131
- package/src/vercel/codec/decode-lifecycle.ts +70 -0
- package/src/vercel/codec/events.ts +85 -0
- package/src/vercel/codec/fields.ts +58 -0
- package/src/vercel/codec/fold-content.ts +54 -0
- package/src/vercel/codec/fold-data.ts +46 -0
- package/src/vercel/codec/fold-input.ts +255 -0
- package/src/vercel/codec/fold-lifecycle.ts +85 -0
- package/src/vercel/codec/fold-text.ts +55 -0
- package/src/vercel/codec/fold-tool-input.ts +86 -0
- package/src/vercel/codec/fold-tool-output.ts +79 -0
- package/src/vercel/codec/index.ts +28 -21
- package/src/vercel/codec/inputs.ts +116 -0
- package/src/vercel/codec/outputs.ts +207 -0
- package/src/vercel/codec/reducer-state.ts +169 -0
- package/src/vercel/codec/reducer.ts +191 -0
- package/src/vercel/codec/tool-transitions.ts +3 -14
- package/src/vercel/codec/wire-data.ts +64 -0
- package/src/vercel/index.ts +7 -19
- package/src/vercel/react/contexts/chat-transport-context.ts +8 -7
- package/src/vercel/react/contexts/chat-transport-provider.tsx +87 -59
- package/src/vercel/react/index.ts +3 -5
- package/src/vercel/react/use-chat-transport.ts +44 -66
- package/src/vercel/react/use-message-sync.ts +75 -39
- package/src/vercel/run-end-reason.ts +157 -0
- package/src/vercel/tool-part.ts +25 -0
- package/src/vercel/transport/chat-transport.ts +380 -98
- package/src/vercel/transport/index.ts +38 -37
- package/src/vercel/transport/run-output-stream.ts +169 -0
- package/src/version.ts +2 -0
- package/dist/core/transport/client-transport.d.ts +0 -10
- package/dist/core/transport/decode-history.d.ts +0 -43
- package/dist/core/transport/server-transport.d.ts +0 -7
- package/dist/core/transport/stream-router.d.ts +0 -29
- package/dist/core/transport/turn-manager.d.ts +0 -37
- package/dist/react/contexts/transport-context.d.ts +0 -31
- package/dist/react/contexts/transport-provider.d.ts +0 -49
- package/dist/react/create-transport-hooks.d.ts +0 -124
- package/dist/react/use-active-turns.d.ts +0 -12
- package/dist/react/use-client-transport.d.ts +0 -80
- package/dist/vercel/codec/accumulator.d.ts +0 -21
- package/dist/vercel/codec/decoder.d.ts +0 -22
- package/dist/vercel/codec/encoder.d.ts +0 -41
- package/dist/vercel/react/use-staged-add-tool-approval-response.d.ts +0 -30
- package/dist/vercel/tool-approvals.d.ts +0 -124
- package/dist/vercel/tool-events.d.ts +0 -26
- package/src/core/transport/client-transport.ts +0 -977
- package/src/core/transport/decode-history.ts +0 -485
- package/src/core/transport/server-transport.ts +0 -612
- package/src/core/transport/stream-router.ts +0 -136
- package/src/core/transport/turn-manager.ts +0 -165
- package/src/react/contexts/transport-context.ts +0 -37
- package/src/react/contexts/transport-provider.tsx +0 -164
- package/src/react/create-transport-hooks.ts +0 -144
- package/src/react/use-active-turns.ts +0 -72
- package/src/react/use-client-transport.ts +0 -197
- package/src/vercel/codec/accumulator.ts +0 -588
- package/src/vercel/codec/decoder.ts +0 -618
- package/src/vercel/codec/encoder.ts +0 -410
- package/src/vercel/react/use-staged-add-tool-approval-response.ts +0 -87
- package/src/vercel/tool-approvals.ts +0 -380
- package/src/vercel/tool-events.ts +0 -53
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ClientSession } from '../../../core/transport/types.js';
|
|
2
|
+
import { VercelInput, VercelOutput, VercelProjection } from '../../codec/index.js';
|
|
2
3
|
import { ChatTransport } from '../../transport/chat-transport.js';
|
|
3
4
|
import type * as Ably from 'ably';
|
|
4
5
|
import type * as AI from 'ai';
|
|
5
6
|
/**
|
|
6
7
|
* A single entry in the chat transport registry, holding both the
|
|
7
|
-
* underlying {@link
|
|
8
|
+
* underlying {@link ClientSession} and the {@link ChatTransport} wrapping it.
|
|
8
9
|
*/
|
|
9
10
|
export interface ChatTransportSlot {
|
|
10
|
-
/** The underlying client
|
|
11
|
-
readonly
|
|
12
|
-
/** Construction error from the underlying {@link
|
|
13
|
-
readonly
|
|
11
|
+
/** The underlying client session used to create the chat transport. */
|
|
12
|
+
readonly session: ClientSession<VercelInput, VercelOutput, VercelProjection, AI.UIMessage>;
|
|
13
|
+
/** Construction error from the underlying {@link ClientSession}, or `undefined` on success. */
|
|
14
|
+
readonly sessionError?: Ably.ErrorInfo | undefined;
|
|
14
15
|
/** The chat transport adapter for use with Vercel's useChat hook. */
|
|
15
16
|
readonly chatTransport: ChatTransport;
|
|
16
17
|
}
|
|
@@ -18,7 +19,7 @@ export interface ChatTransportSlot {
|
|
|
18
19
|
* The shape of the single {@link ChatTransportContext} value.
|
|
19
20
|
* Combines the nearest slot with the full registry in one context object.
|
|
20
21
|
*/
|
|
21
|
-
|
|
22
|
+
interface ChatTransportContextValue {
|
|
22
23
|
/** The slot from the nearest {@link ChatTransportProvider} in the tree. */
|
|
23
24
|
readonly nearest: ChatTransportSlot | undefined;
|
|
24
25
|
/** All registered slots, keyed by channelName. */
|
|
@@ -30,3 +31,4 @@ export interface ChatTransportContextValue {
|
|
|
30
31
|
* read by {@link useChatTransport}.
|
|
31
32
|
*/
|
|
32
33
|
export declare const ChatTransportContext: import('react').Context<ChatTransportContextValue>;
|
|
34
|
+
export {};
|
|
@@ -1,70 +1,79 @@
|
|
|
1
1
|
import { PropsWithChildren, ReactNode } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { ClientSessionProviderProps } from '../../../react/index.js';
|
|
3
|
+
import { VercelInput, VercelOutput, VercelProjection } from '../../codec/index.js';
|
|
3
4
|
import { ChatTransportOptions } from '../../transport/index.js';
|
|
4
5
|
/**
|
|
5
|
-
* ChatTransportProvider: creates a ChatTransport from a
|
|
6
|
+
* ChatTransportProvider: creates a ChatTransport from a ClientSession and makes it
|
|
6
7
|
* available to descendants via ChatTransportContext.
|
|
7
8
|
*
|
|
8
|
-
* Wraps children with
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
9
|
+
* Wraps children with ClientSessionProvider (using UIMessageCodec). The
|
|
10
|
+
* surrounding `<AblyProvider>` supplies the Realtime client; the session
|
|
11
|
+
* resolves the channel from `channelName` itself. An inner component reads
|
|
12
|
+
* the ClientSession via useClientSession() and creates the ChatTransport
|
|
13
|
+
* via useMemo, keyed on the session and transport options.
|
|
12
14
|
*
|
|
13
|
-
* The ChatTransport is NOT closed on unmount — the underlying
|
|
14
|
-
* lifecycle is managed by the wrapping
|
|
15
|
-
* React Strict Mode, and ChatTransport.close() delegates to
|
|
16
|
-
* which
|
|
15
|
+
* The ChatTransport is NOT closed on unmount — the underlying ClientSession
|
|
16
|
+
* lifecycle is managed by the wrapping ClientSessionProvider. Auto-closing would break
|
|
17
|
+
* React Strict Mode, and ChatTransport.close() delegates to ClientSession.close()
|
|
18
|
+
* which ClientSessionProvider already calls.
|
|
17
19
|
*
|
|
18
20
|
* Multiple ChatTransportProviders can be nested using distinct channelNames.
|
|
19
|
-
* Each provider merges its
|
|
20
|
-
* can access all registered
|
|
21
|
+
* Each provider merges its session into the parent registry, so descendants
|
|
22
|
+
* can access all registered sessions via useChatTransport({ channelName }).
|
|
21
23
|
*/
|
|
22
24
|
import type * as AI from 'ai';
|
|
23
|
-
export declare const
|
|
24
|
-
|
|
25
|
+
export declare const ClientSessionProvider: import('react').ComponentType<ClientSessionProviderProps<VercelInput, VercelOutput, VercelProjection, AI.UIMessage<unknown, AI.UIDataTypes, AI.UITools>>>, useAblyMessages: (props?: {
|
|
26
|
+
session?: import('../../../react/index.js').ClientSession<VercelInput, VercelOutput, VercelProjection, AI.UIMessage<unknown, AI.UIDataTypes, AI.UITools>> | undefined;
|
|
25
27
|
skip?: boolean;
|
|
26
|
-
} | undefined) => import('ably').InboundMessage[],
|
|
27
|
-
transport?: import('../../../index.js').ClientTransport<AI.UIMessageChunk, AI.UIMessage<unknown, AI.UIDataTypes, AI.UITools>> | null | undefined;
|
|
28
|
-
} | undefined) => Map<string, Set<string>>, useClientTransport: (props?: {
|
|
28
|
+
} | undefined) => import('ably').InboundMessage[], useClientSession: (props?: {
|
|
29
29
|
channelName?: string;
|
|
30
30
|
skip?: boolean;
|
|
31
31
|
onError?: (error: import('ably').ErrorInfo) => void;
|
|
32
|
-
}) => import('../../../react/use-client-
|
|
33
|
-
|
|
32
|
+
}) => import('../../../react/use-client-session.js').ClientSessionHandle<VercelInput, VercelOutput, VercelProjection, AI.UIMessage<unknown, AI.UIDataTypes, AI.UITools>>, useCreateView: (props?: {
|
|
33
|
+
session?: import('../../../react/index.js').ClientSession<VercelInput, VercelOutput, VercelProjection, AI.UIMessage<unknown, AI.UIDataTypes, AI.UITools>> | null | undefined;
|
|
34
34
|
limit?: number;
|
|
35
35
|
skip?: boolean;
|
|
36
|
-
} | undefined) => import('../../../react/use-view.js').ViewHandle<
|
|
37
|
-
|
|
38
|
-
} | undefined) => import('../../../react/use-tree.js').TreeHandle<
|
|
39
|
-
|
|
40
|
-
view?: import('../../../index.js').View<
|
|
36
|
+
} | undefined) => import('../../../react/use-view.js').ViewHandle<VercelInput, AI.UIMessage<unknown, AI.UIDataTypes, AI.UITools>>, useTree: (props?: {
|
|
37
|
+
session?: import('../../../react/index.js').ClientSession<VercelInput, VercelOutput, VercelProjection, AI.UIMessage<unknown, AI.UIDataTypes, AI.UITools>> | undefined;
|
|
38
|
+
} | undefined) => import('../../../react/use-tree.js').TreeHandle<VercelProjection>, useView: (props?: {
|
|
39
|
+
session?: import('../../../react/index.js').ClientSession<VercelInput, VercelOutput, VercelProjection, AI.UIMessage<unknown, AI.UIDataTypes, AI.UITools>> | null | undefined;
|
|
40
|
+
view?: import('../../../index.js').View<VercelInput, AI.UIMessage<unknown, AI.UIDataTypes, AI.UITools>> | null | undefined;
|
|
41
41
|
limit?: number;
|
|
42
42
|
skip?: boolean;
|
|
43
|
-
} | undefined) => import('../../../react/use-view.js').ViewHandle<
|
|
44
|
-
type
|
|
43
|
+
} | undefined) => import('../../../react/use-view.js').ViewHandle<VercelInput, AI.UIMessage<unknown, AI.UIDataTypes, AI.UITools>>;
|
|
44
|
+
type CoreClientSessionProviderProps = Omit<ClientSessionProviderProps<VercelInput, VercelOutput, VercelProjection, AI.UIMessage>, 'codec'>;
|
|
45
45
|
/**
|
|
46
46
|
* Props for {@link ChatTransportProvider}.
|
|
47
47
|
*
|
|
48
|
-
* All {@link
|
|
49
|
-
* plus `
|
|
48
|
+
* All {@link ClientSessionProviderProps} for Vercel types except `codec` (baked as UIMessageCodec),
|
|
49
|
+
* plus the transport-owned invocation POST options (`api` / `credentials` / `fetch`) and
|
|
50
|
+
* `chatOptions` for customizing chat request construction.
|
|
50
51
|
*/
|
|
51
|
-
export interface ChatTransportProviderProps extends
|
|
52
|
+
export interface ChatTransportProviderProps extends CoreClientSessionProviderProps {
|
|
53
|
+
/** Endpoint the chat transport POSTs the invocation to, to wake the agent. Default `/api/chat`. */
|
|
54
|
+
api?: string;
|
|
55
|
+
/** Fetch credentials mode for the invocation POST. */
|
|
56
|
+
credentials?: RequestCredentials;
|
|
57
|
+
/** Custom fetch implementation for the invocation POST. Defaults to `globalThis.fetch`. */
|
|
58
|
+
fetch?: typeof globalThis.fetch;
|
|
52
59
|
/**
|
|
53
|
-
* Optional
|
|
60
|
+
* Optional transport options for customizing chat request construction (e.g. the `prepareSendMessagesRequest` hook).
|
|
54
61
|
* Must be stable across renders — wrap in `useMemo` or define outside the component.
|
|
55
62
|
* A new object reference triggers ChatTransport recreation.
|
|
63
|
+
* If this object also sets `api`/`credentials`/`fetch`, the dedicated top-level props of the same name take precedence.
|
|
56
64
|
*/
|
|
57
65
|
chatOptions?: ChatTransportOptions;
|
|
58
66
|
}
|
|
59
67
|
/**
|
|
60
|
-
* Provide a {@link ChatTransport} and its underlying {@link
|
|
68
|
+
* Provide a {@link ChatTransport} and its underlying {@link ClientSession} to descendant components.
|
|
61
69
|
*
|
|
62
|
-
* Wraps children with
|
|
63
|
-
*
|
|
70
|
+
* Wraps children with `ClientSessionProvider` using `channelName` (the Realtime
|
|
71
|
+
* client is read from the surrounding `<AblyProvider>`), creates a
|
|
72
|
+
* {@link ClientSession} with UIMessageCodec, wraps it in a {@link ChatTransport},
|
|
64
73
|
* and registers the full slot in `ChatTransportContext` under `channelName`. Descendants call
|
|
65
|
-
* {@link useChatTransport} with the same `channelName` to access both
|
|
74
|
+
* {@link useChatTransport} with the same `channelName` to access both.
|
|
66
75
|
*
|
|
67
|
-
* `
|
|
76
|
+
* `useClientSession` is also available inside this provider's subtree.
|
|
68
77
|
*
|
|
69
78
|
* ```tsx
|
|
70
79
|
* <ChatTransportProvider channelName="ai:demo">
|
|
@@ -72,13 +81,16 @@ export interface ChatTransportProviderProps extends CoreTransportProviderProps {
|
|
|
72
81
|
* </ChatTransportProvider>
|
|
73
82
|
*
|
|
74
83
|
* // Inside Chat:
|
|
75
|
-
* const { chatTransport,
|
|
76
|
-
* const {
|
|
84
|
+
* const { chatTransport, session } = useChatTransport();
|
|
85
|
+
* const { session } = useClientSession(); // also available
|
|
77
86
|
* ```
|
|
78
|
-
* @param props - Provider configuration including `channelName`, optional `chatOptions`, and all other
|
|
87
|
+
* @param props - Provider configuration including `channelName`, the invocation POST options (`api` / `credentials` / `fetch`), optional `chatOptions`, and all other session options.
|
|
88
|
+
* @param props.api - Endpoint the chat transport POSTs the invocation to. Default `/api/chat`.
|
|
89
|
+
* @param props.credentials - Fetch credentials mode for the invocation POST.
|
|
90
|
+
* @param props.fetch - Custom fetch implementation for the invocation POST.
|
|
79
91
|
* @param props.chatOptions - Optional hooks for customizing chat request construction. Must be stable (memoized) — a new reference recreates the ChatTransport.
|
|
80
|
-
* @param props.children - Descendant components that consume the transport via hooks.
|
|
81
|
-
* @returns A React element wrapping children with
|
|
92
|
+
* @param props.children - Descendant components that consume the chat transport via hooks.
|
|
93
|
+
* @returns A React element wrapping children with ClientSessionContext and ChatTransportContext.
|
|
82
94
|
*/
|
|
83
|
-
export declare const ChatTransportProvider: ({ chatOptions, children, ...
|
|
95
|
+
export declare const ChatTransportProvider: ({ api, credentials, fetch, chatOptions, children, ...sessionProps }: ChatTransportProviderProps & PropsWithChildren) => ReactNode;
|
|
84
96
|
export {};
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
export type { ChatTransport } from '../transport/chat-transport.js';
|
|
2
2
|
export type { ChatTransportProviderProps } from './contexts/chat-transport-provider.js';
|
|
3
|
-
export { ChatTransportProvider,
|
|
3
|
+
export { ChatTransportProvider, ClientSessionProvider, useAblyMessages, useClientSession, useCreateView, useTree, useView, } from './contexts/chat-transport-provider.js';
|
|
4
4
|
export type { ChatTransportHandle, UseChatTransportOptions } from './use-chat-transport.js';
|
|
5
5
|
export { useChatTransport } from './use-chat-transport.js';
|
|
6
6
|
export type { UseMessageSyncOptions } from './use-message-sync.js';
|
|
7
7
|
export { useMessageSync } from './use-message-sync.js';
|
|
8
|
-
export { useStagedAddToolApprovalResponse } from './use-staged-add-tool-approval-response.js';
|
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ClientSession } from '../../core/transport/types.js';
|
|
2
|
+
import { VercelInput, VercelOutput, VercelProjection } from '../codec/index.js';
|
|
2
3
|
import { ChatTransport } from '../transport/index.js';
|
|
3
4
|
/**
|
|
4
|
-
* useChatTransport: reads a ChatTransport and its underlying
|
|
5
|
+
* useChatTransport: reads a ChatTransport and its underlying ClientSession from
|
|
5
6
|
* the nearest ChatTransportProvider.
|
|
6
7
|
*
|
|
7
|
-
* The transport is created by ChatTransportProvider, which
|
|
8
|
-
* with
|
|
9
|
-
*
|
|
8
|
+
* The chat transport is created by ChatTransportProvider, which wraps the subtree
|
|
9
|
+
* with ClientSessionProvider. The Ably Realtime client is read from the
|
|
10
|
+
* surrounding `<AblyProvider>`. This hook is a thin context reader — it does
|
|
11
|
+
* not create or manage any session/transport state.
|
|
10
12
|
*
|
|
11
13
|
* Pass `channelName` to look up a specific provider by name. Omit to use the nearest
|
|
12
14
|
* provider in the tree. Pass `skip: true` to defer (e.g. when auth is not yet resolved)
|
|
13
|
-
* — returns
|
|
15
|
+
* — returns stubs whose properties throw with a descriptive error.
|
|
14
16
|
*/
|
|
15
17
|
import * as Ably from 'ably';
|
|
16
18
|
import type * as AI from 'ai';
|
|
@@ -18,53 +20,55 @@ import type * as AI from 'ai';
|
|
|
18
20
|
export interface UseChatTransportOptions {
|
|
19
21
|
/** Channel name to look up; omit to use the nearest {@link ChatTransportProvider}. */
|
|
20
22
|
channelName?: string;
|
|
21
|
-
/** When `true`, return
|
|
23
|
+
/** When `true`, return stubs that throw on any access. */
|
|
22
24
|
skip?: boolean;
|
|
23
25
|
}
|
|
24
26
|
/**
|
|
25
27
|
* The value returned by {@link useChatTransport}.
|
|
26
|
-
* Provides both the underlying {@link
|
|
28
|
+
* Provides both the underlying {@link ClientSession} and the {@link ChatTransport}
|
|
27
29
|
* adapter for Vercel's useChat hook.
|
|
28
30
|
*/
|
|
29
31
|
export interface ChatTransportHandle {
|
|
30
32
|
/**
|
|
31
|
-
* The underlying client
|
|
32
|
-
* A throwing stub when `skip` is `true`, when no matching {@link
|
|
33
|
-
* was found in the tree, or when
|
|
33
|
+
* The underlying client session, also available via {@link useClientSession}.
|
|
34
|
+
* A throwing stub when `skip` is `true`, when no matching {@link ClientSessionProvider}
|
|
35
|
+
* was found in the tree, or when session construction failed. Check `sessionError` before use.
|
|
34
36
|
*/
|
|
35
|
-
|
|
37
|
+
session: ClientSession<VercelInput, VercelOutput, VercelProjection, AI.UIMessage>;
|
|
36
38
|
/**
|
|
37
39
|
* The chat transport adapter for use with Vercel's `useChat` hook.
|
|
38
40
|
*
|
|
39
|
-
* A throwing stub when `skip` is `true
|
|
40
|
-
* {@link ChatTransportProvider} was found in the tree
|
|
41
|
-
* {@link
|
|
42
|
-
* and `
|
|
41
|
+
* A throwing stub when `skip` is `true` or when no matching
|
|
42
|
+
* {@link ChatTransportProvider} was found in the tree. When a provider is found
|
|
43
|
+
* but the underlying {@link ClientSession} failed to construct, this is the real
|
|
44
|
+
* transport and `sessionError` is set instead. Check `chatTransportError` and
|
|
45
|
+
* `sessionError` before use.
|
|
43
46
|
*/
|
|
44
47
|
chatTransport: ChatTransport;
|
|
45
48
|
/**
|
|
46
|
-
* Set when no matching {@link
|
|
49
|
+
* Set when no matching {@link ClientSessionProvider} was found, when session
|
|
47
50
|
* construction failed, and `skip` is `false`.
|
|
48
|
-
* `undefined` when the
|
|
51
|
+
* `undefined` when the session resolved successfully or when `skip` is `true`.
|
|
49
52
|
*/
|
|
50
|
-
|
|
53
|
+
sessionError?: Ably.ErrorInfo | undefined;
|
|
51
54
|
/**
|
|
52
|
-
* Set when no matching {@link ChatTransportProvider} was found
|
|
53
|
-
*
|
|
54
|
-
* `undefined` when the transport resolved successfully
|
|
55
|
+
* Set only when no matching {@link ChatTransportProvider} was found and `skip` is
|
|
56
|
+
* `false`.
|
|
57
|
+
* `undefined` when the chat transport resolved successfully (even if session
|
|
58
|
+
* construction failed — see `sessionError`) or when `skip` is `true`.
|
|
55
59
|
*/
|
|
56
60
|
chatTransportError?: Ably.ErrorInfo | undefined;
|
|
57
61
|
}
|
|
58
62
|
/**
|
|
59
|
-
* Access a {@link ChatTransport} and {@link
|
|
63
|
+
* Access a {@link ChatTransport} and {@link ClientSession} from the nearest {@link ChatTransportProvider}.
|
|
60
64
|
*
|
|
61
65
|
* When `channelName` is omitted, the innermost `ChatTransportProvider` in the tree is used.
|
|
62
|
-
* When `skip` is `true`, returns
|
|
66
|
+
* When `skip` is `true`, returns stubs whose every property and method throws
|
|
63
67
|
* an {@link Ably.ErrorInfo} — safe to hold in state before conditions are ready.
|
|
64
68
|
* When no provider is found, returns stubs with `chatTransportError` set instead of throwing.
|
|
65
|
-
* @param props - Options for selecting the transport.
|
|
69
|
+
* @param props - Options for selecting the chat transport.
|
|
66
70
|
* @param props.channelName - The channel name passed to the enclosing `ChatTransportProvider`. Omit to use the nearest.
|
|
67
71
|
* @param props.skip - When `true`, return stubs that throw on any access instead of reading from context.
|
|
68
|
-
* @returns The `ChatTransportHandle` containing both the chat transport adapter and the underlying client
|
|
72
|
+
* @returns The `ChatTransportHandle` containing both the chat transport adapter and the underlying client session.
|
|
69
73
|
*/
|
|
70
74
|
export declare const useChatTransport: ({ channelName, skip }?: UseChatTransportOptions) => ChatTransportHandle;
|
|
@@ -1,51 +1,38 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* useMessageSync:
|
|
2
|
+
* useMessageSync: wire view updates into useChat's setMessages.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* During active own-run streams, setMessages is gated to avoid an
|
|
5
|
+
* ID-mismatch in useChat's write(). When the stream ends, the gate
|
|
6
|
+
* opens and the view is synced into useChat's overlay.
|
|
6
7
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* the gate
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* All dependencies are resolved from the nearest ChatTransportProvider via
|
|
14
|
-
* useChatTransport(). Pass channelName to select a specific provider; omit to use
|
|
15
|
-
* the nearest. Pass skip: true to pause all subscriptions.
|
|
16
|
-
*
|
|
17
|
-
* Returns the unsubscribe function in the useEffect cleanup so handlers
|
|
18
|
-
* are removed on unmount or when dependencies change.
|
|
8
|
+
* The sync is a per-message merge, not a replace: when the overlay has
|
|
9
|
+
* resolved a client-side tool locally (via addToolResult) but the
|
|
10
|
+
* tree's echo hasn't landed yet, the overlay's resolution wins.
|
|
11
|
+
* Without that, the gate-open sync would race the AI SDK's post-stream
|
|
12
|
+
* sendAutomaticallyWhen check and could clobber the resolution before
|
|
13
|
+
* the continuation publishes.
|
|
19
14
|
*/
|
|
20
15
|
import type * as AI from 'ai';
|
|
21
16
|
/** Options for {@link useMessageSync}. */
|
|
22
17
|
export interface UseMessageSyncOptions {
|
|
23
18
|
/**
|
|
24
|
-
* The `setMessages` updater function from `useChat()`.
|
|
25
|
-
*
|
|
26
|
-
* transport's current authoritative message list.
|
|
19
|
+
* The `setMessages` updater function from `useChat()`. Called with an
|
|
20
|
+
* updater that returns the next overlay.
|
|
27
21
|
*/
|
|
28
22
|
setMessages: (updater: (prev: AI.UIMessage[]) => AI.UIMessage[]) => void;
|
|
29
23
|
/**
|
|
30
24
|
* Channel name of the {@link ChatTransportProvider} to observe.
|
|
31
|
-
* Omit to use the nearest provider
|
|
25
|
+
* Omit to use the nearest provider.
|
|
32
26
|
*/
|
|
33
27
|
channelName?: string;
|
|
34
|
-
/**
|
|
35
|
-
* When `true`, skip all subscriptions and do nothing.
|
|
36
|
-
* Use when the hook's dependencies are not yet resolved (e.g. auth pending).
|
|
37
|
-
*/
|
|
28
|
+
/** When `true`, skip all subscriptions. */
|
|
38
29
|
skip?: boolean;
|
|
39
30
|
}
|
|
40
31
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* Resolves both the transport view and the streaming gate from the nearest
|
|
44
|
-
* `ChatTransportProvider`. Pass `channelName` to target a specific provider.
|
|
45
|
-
* Pass `skip: true` to pause all subscriptions.
|
|
32
|
+
* Subscribe to view updates and sync them into `useChat()`'s overlay.
|
|
46
33
|
* @param options - Hook options.
|
|
47
|
-
* @param options.setMessages - The `setMessages` function from `useChat()`.
|
|
48
|
-
* @param options.channelName - Channel name of the provider to observe; defaults to nearest.
|
|
34
|
+
* @param options.setMessages - The `setMessages` function from `useChat()`.
|
|
35
|
+
* @param options.channelName - Channel name of the provider to observe; defaults to the nearest.
|
|
49
36
|
* @param options.skip - When `true`, skip all subscriptions.
|
|
50
37
|
*/
|
|
51
38
|
export declare const useMessageSync: ({ setMessages, channelName, skip }: UseMessageSyncOptions) => void;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { RunEndReason, StreamResult } from '../core/transport/types.js';
|
|
2
|
+
import * as Ably from 'ably';
|
|
3
|
+
import type * as AI from 'ai';
|
|
4
|
+
/**
|
|
5
|
+
* The outcome of a Vercel `streamText` response piped through `Run.pipe`.
|
|
6
|
+
* Discriminated on `reason`: `'suspend'` means the run should pause; the
|
|
7
|
+
* non-`'suspend'` arms describe how it terminated, and an `'error'` outcome
|
|
8
|
+
* always carries `error`.
|
|
9
|
+
*
|
|
10
|
+
* This is a *description of what the Vercel run resulted in*, not a command to
|
|
11
|
+
* the SDK. The common case maps cleanly onto one transport action — `'suspend'`
|
|
12
|
+
* → `Run.suspend()`, everything else → `Run.end()` — and to make that case a
|
|
13
|
+
* one-liner the non-`'suspend'` arms are deliberately assignable to
|
|
14
|
+
* {@link RunEndParams}, so after a `suspend` guard the whole object passes
|
|
15
|
+
* straight to `Run.end(outcome)`. That assignability is a convenience for this
|
|
16
|
+
* adapter, not a constraint on what an outcome can mean: responding to an
|
|
17
|
+
* outcome may also involve work outside this SDK (persisting a result,
|
|
18
|
+
* notifying a human, triggering a downstream workflow), and the developer is
|
|
19
|
+
* free to do that around the terminal call.
|
|
20
|
+
*
|
|
21
|
+
* The type is Vercel-specific by design. Outcomes are the layer where agent
|
|
22
|
+
* SDKs diverge most — both in what they report (the `'suspend'` arm exists only
|
|
23
|
+
* because Vercel surfaces unexecuted tool calls as a non-terminal finish) and
|
|
24
|
+
* in what a developer must do in response. A different SDK's outcome type would
|
|
25
|
+
* have different arms; hence each adapter names its own rather than sharing a
|
|
26
|
+
* single core `RunOutcome`. The vocabulary it bottoms out in
|
|
27
|
+
* ({@link RunEndParams}, `Run.suspend`/`Run.end`) is the shared, codec-agnostic
|
|
28
|
+
* part that does live in core.
|
|
29
|
+
*/
|
|
30
|
+
export type VercelRunOutcome = {
|
|
31
|
+
/**
|
|
32
|
+
* The LLM requested tools the SDK did not auto-execute, so the run
|
|
33
|
+
* pauses rather than ending — call `Run.suspend()`.
|
|
34
|
+
*/
|
|
35
|
+
reason: 'suspend';
|
|
36
|
+
/** Never present for a suspend outcome. */
|
|
37
|
+
error?: never;
|
|
38
|
+
} | {
|
|
39
|
+
/** A non-error terminal reason; pass the outcome to `Run.end()`. */
|
|
40
|
+
reason: Exclude<RunEndReason, 'error'>;
|
|
41
|
+
/** Never present for a non-error outcome. */
|
|
42
|
+
error?: never;
|
|
43
|
+
} | {
|
|
44
|
+
/** The run ended in error; pass the outcome to `Run.end()`. */
|
|
45
|
+
reason: Extract<RunEndReason, 'error'>;
|
|
46
|
+
/**
|
|
47
|
+
* The terminal error: the underlying stream / `finishReason` failure
|
|
48
|
+
* wrapped as an `Ably.ErrorInfo` (code `StreamError`).
|
|
49
|
+
*/
|
|
50
|
+
error: Ably.ErrorInfo;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Derive the {@link VercelRunOutcome} for a Vercel `streamText` response that
|
|
54
|
+
* was piped through `Run.pipe`. Preserves transport-level outcomes
|
|
55
|
+
* (`'cancelled'`, `'error'`) from the pipe result; when the pipe completed
|
|
56
|
+
* naturally, awaits Vercel's `finishReason` and returns `'suspend'` for
|
|
57
|
+
* `'tool-calls'` (the LLM requested tools the SDK did not auto-execute, so the
|
|
58
|
+
* run should suspend rather than end), or `'complete'` otherwise.
|
|
59
|
+
*
|
|
60
|
+
* Surfaces the failure for both error shapes so the caller can forward it to
|
|
61
|
+
* `Run.end(reason, error)`: a stream that threw (`pipeResult.error`) and a
|
|
62
|
+
* `finishReason` that rejected with a non-abort error (e.g.
|
|
63
|
+
* `NoOutputGeneratedError`, network blow-ups). The error is wrapped as an
|
|
64
|
+
* `Ably.ErrorInfo` (code `StreamError`). A stream that already produced a
|
|
65
|
+
* codec-level error chunk is unaffected — stamping run-end is the
|
|
66
|
+
* codec-agnostic baseline that any consumer can read.
|
|
67
|
+
*
|
|
68
|
+
* Tolerates `finishReason` rejection. Vercel AI SDK v6 rejects
|
|
69
|
+
* `streamText().finishReason` with the abort signal's reason when the stream
|
|
70
|
+
* is aborted before any step completes, and rejects with
|
|
71
|
+
* `NoOutputGeneratedError` when the model produced nothing at all. Without
|
|
72
|
+
* this guard the rejection would bubble out of the route handler's `after()`
|
|
73
|
+
* block, skip the developer's `Run.end(...)` call, and leave the run with no
|
|
74
|
+
* `ai-run-end` event on the channel — so observers' UIs stay stuck on
|
|
75
|
+
* `streaming` indefinitely.
|
|
76
|
+
*
|
|
77
|
+
* Saves callers from interpreting Vercel domain semantics inline at the end
|
|
78
|
+
* of every route handler.
|
|
79
|
+
* @param pipeResult - The result returned by `Run.pipe`.
|
|
80
|
+
* @param finishReason - The `finishReason` promise from a `streamText` result.
|
|
81
|
+
* @returns The {@link VercelRunOutcome}: the terminal `reason` (or `'suspend'`)
|
|
82
|
+
* and, when `reason` is `'error'`, the wrapped `error` to pass to `Run.end`.
|
|
83
|
+
*/
|
|
84
|
+
export declare const vercelRunOutcome: (pipeResult: StreamResult, finishReason: PromiseLike<AI.FinishReason>) => Promise<VercelRunOutcome>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared tool-part type guard for the Vercel layer.
|
|
3
|
+
*
|
|
4
|
+
* The codec normalises every tool part to the `dynamic-tool` shape, but the AI
|
|
5
|
+
* SDK emits `tool-${name}` parts for statically-declared tools. Both shapes
|
|
6
|
+
* carry `toolCallId` and `state`. The guard accepts either representation so
|
|
7
|
+
* the transport's unresolved-tool detection and the React overlay merge can
|
|
8
|
+
* match tool parts uniformly — and so the cross-representation rule lives in
|
|
9
|
+
* one place rather than being re-spelled per call site.
|
|
10
|
+
*/
|
|
11
|
+
import type * as AI from 'ai';
|
|
12
|
+
/** A UIMessage tool part in either the `dynamic-tool` or `tool-${name}` representation. */
|
|
13
|
+
export type ToolPart = AI.DynamicToolUIPart | AI.ToolUIPart;
|
|
14
|
+
/**
|
|
15
|
+
* Whether a UIMessage part is a tool part of either representation. The
|
|
16
|
+
* `toolCallId`/`state` shape check is defensive against a future AI SDK release
|
|
17
|
+
* introducing a non-tool variant under the `tool-` prefix (none exists today).
|
|
18
|
+
* @param part - The UIMessage part to inspect.
|
|
19
|
+
* @returns True when the part is a tool part.
|
|
20
|
+
*/
|
|
21
|
+
export declare const isToolPart: (part: AI.UIMessage["parts"][number]) => part is ToolPart;
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ClientSession } from '../../core/transport/types.js';
|
|
2
|
+
import { VercelInput, VercelOutput, VercelProjection } from '../codec/index.js';
|
|
2
3
|
/**
|
|
3
|
-
* Vercel chat transport: wraps a core
|
|
4
|
+
* Vercel chat transport: wraps a core ClientSession to satisfy the
|
|
4
5
|
* ChatTransport interface that useChat expects.
|
|
5
6
|
*
|
|
6
|
-
* This is a thin adapter — the real logic lives in the core
|
|
7
|
+
* This is a thin adapter — the real logic lives in the core client session.
|
|
7
8
|
* The chat transport maps Vercel's sendMessages/reconnectToStream contract
|
|
8
|
-
* to the core
|
|
9
|
+
* to the core session's send/cancel methods.
|
|
9
10
|
*
|
|
10
11
|
* useChat manages message state before calling sendMessages:
|
|
11
12
|
* - submit-message (new): appends the new user message, passes the full array
|
|
@@ -13,13 +14,20 @@ import { ClientTransport, CloseOptions } from '../../core/transport/types.js';
|
|
|
13
14
|
* passes the truncated array with messageId set
|
|
14
15
|
* - regenerate-message: truncates after the target, passes the truncated array
|
|
15
16
|
*
|
|
16
|
-
* The adapter uses `trigger` to determine the
|
|
17
|
-
*
|
|
17
|
+
* The adapter uses `(trigger, last-message role)` to determine the
|
|
18
|
+
* history/messages split:
|
|
19
|
+
* - submit-message + last message is a user message: that last message is new
|
|
20
|
+
* (publish to channel), rest is history. A new submit and an edit both take
|
|
21
|
+
* this path — an edit just carries a messageId.
|
|
22
|
+
* - submit-message + last message is an assistant already in the tree
|
|
23
|
+
* (continuation): no new messages, entire array is history
|
|
18
24
|
* - regenerate-message: no new messages, entire array is history
|
|
19
25
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* place the response on the
|
|
26
|
+
* For an edit (submit-message with messageId) and for forking off an
|
|
27
|
+
* unresolved tool call, the adapter computes fork metadata (forkOf/parent)
|
|
28
|
+
* from the conversation tree so the server can place the response on the
|
|
29
|
+
* correct branch. Regeneration fork metadata is NOT computed here —
|
|
30
|
+
* `View.regenerate` derives forkOf/parent from the tree itself.
|
|
23
31
|
*/
|
|
24
32
|
import type * as AI from 'ai';
|
|
25
33
|
/**
|
|
@@ -40,23 +48,32 @@ export interface SendMessagesRequestContext {
|
|
|
40
48
|
messageId?: string;
|
|
41
49
|
/** Previous messages in the conversation (context for the LLM). */
|
|
42
50
|
history: AI.UIMessage[];
|
|
43
|
-
/** The new message(s) being sent (to publish to the channel). Empty for regeneration. */
|
|
51
|
+
/** The new message(s) being sent (to publish to the channel). Empty for regeneration and for continuations (an auto-submit where the last message is an already-tracked assistant). */
|
|
44
52
|
messages: AI.UIMessage[];
|
|
45
|
-
/** The
|
|
53
|
+
/** The codec-message-id of the message being forked — the edited user message, or the preceding assistant when forking off an unresolved tool call. Undefined for regeneration (View.regenerate derives it) and fresh sends. */
|
|
46
54
|
forkOf?: string;
|
|
47
|
-
/** The
|
|
55
|
+
/** The codec-message-id of the predecessor in the conversation thread. */
|
|
48
56
|
parent?: string;
|
|
49
57
|
}
|
|
50
58
|
/** Options for customizing the ChatTransport behavior. */
|
|
51
59
|
export interface ChatTransportOptions {
|
|
52
60
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
61
|
+
* Endpoint the transport POSTs the invocation pointer to, to wake the
|
|
62
|
+
* agent. Mirrors useChat's request-driven contract. Default `/api/chat`.
|
|
63
|
+
*/
|
|
64
|
+
api?: string;
|
|
65
|
+
/** Fetch credentials mode for the invocation POST (e.g. `'include'`). */
|
|
66
|
+
credentials?: RequestCredentials;
|
|
67
|
+
/** Custom fetch implementation for the invocation POST. Defaults to `globalThis.fetch`. */
|
|
68
|
+
fetch?: typeof globalThis.fetch;
|
|
69
|
+
/**
|
|
70
|
+
* Customize the invocation POST before sending. Called by sendMessages()
|
|
71
|
+
* with the conversation context; the returned `body` is merged into the
|
|
72
|
+
* POST body (the run's invocation identifiers always take precedence) and
|
|
73
|
+
* `headers` are added to the request. Use it for auth headers or extra
|
|
74
|
+
* agent metadata.
|
|
58
75
|
* @param context - The conversation context for the current request.
|
|
59
|
-
* @returns The body and headers to
|
|
76
|
+
* @returns The body and headers to merge into the invocation POST.
|
|
60
77
|
*/
|
|
61
78
|
prepareSendMessagesRequest?: (context: SendMessagesRequestContext) => {
|
|
62
79
|
body?: Record<string, unknown>;
|
|
@@ -109,8 +126,8 @@ export interface ChatTransport {
|
|
|
109
126
|
chatId: string;
|
|
110
127
|
} & ChatRequestOptions) => Promise<ReadableStream<AI.UIMessageChunk> | null>;
|
|
111
128
|
/** Close the underlying transport, releasing all resources. */
|
|
112
|
-
close(
|
|
113
|
-
/** Whether an own-
|
|
129
|
+
close(): Promise<void>;
|
|
130
|
+
/** Whether an own-run stream is currently being consumed by useChat. */
|
|
114
131
|
readonly streaming: boolean;
|
|
115
132
|
/**
|
|
116
133
|
* Subscribe to streaming state changes. The callback fires when the
|
|
@@ -122,19 +139,19 @@ export interface ChatTransport {
|
|
|
122
139
|
onStreamingChange(callback: (streaming: boolean) => void): () => void;
|
|
123
140
|
}
|
|
124
141
|
/**
|
|
125
|
-
* Create a Vercel ChatTransport from a core
|
|
142
|
+
* Create a Vercel ChatTransport from a core ClientSession.
|
|
126
143
|
*
|
|
127
144
|
* Exposes a `streaming` flag and `onStreamingChange` callback so that
|
|
128
|
-
* `useMessageSync` can gate `setMessages` calls during active own-
|
|
145
|
+
* `useMessageSync` can gate `setMessages` calls during active own-run
|
|
129
146
|
* streams, preventing the push/replace ID mismatch in useChat's `write()`.
|
|
130
147
|
*
|
|
131
148
|
* Note: concurrent `sendMessage` calls from the same user are a useChat
|
|
132
149
|
* limitation that cannot be fixed from the transport layer. The
|
|
133
150
|
* developer must respect useChat's `status` and only call `sendMessage`
|
|
134
151
|
* when status is `'ready'`.
|
|
135
|
-
* @param
|
|
152
|
+
* @param session - The core client session to wrap.
|
|
136
153
|
* @param chatOptions - Optional hooks for customizing request construction.
|
|
137
154
|
* @returns A {@link ChatTransport} compatible with Vercel's useChat hook.
|
|
138
155
|
*/
|
|
139
|
-
export declare const createChatTransport: (
|
|
156
|
+
export declare const createChatTransport: (session: ClientSession<VercelInput, VercelOutput, VercelProjection, AI.UIMessage>, chatOptions?: ChatTransportOptions) => ChatTransport;
|
|
140
157
|
export {};
|