@ably/ai-transport 0.1.0 → 0.2.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 +91 -100
- package/dist/ably-ai-transport.js +1553 -1238
- 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 +29 -0
- package/dist/core/codec/decoder.d.ts +20 -23
- package/dist/core/codec/encoder.d.ts +11 -8
- package/dist/core/codec/index.d.ts +1 -2
- package/dist/core/codec/lifecycle-tracker.d.ts +10 -9
- package/dist/core/codec/types.d.ts +407 -115
- package/dist/core/transport/agent-session.d.ts +10 -0
- package/dist/core/transport/branch-chain.d.ts +43 -0
- package/dist/core/transport/client-session.d.ts +13 -0
- package/dist/core/transport/decode-fold.d.ts +47 -0
- package/dist/core/transport/headers.d.ts +96 -18
- 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-conversation.d.ts +128 -0
- package/dist/core/transport/load-history.d.ts +39 -0
- package/dist/core/transport/pipe-stream.d.ts +9 -9
- package/dist/core/transport/run-manager.d.ts +78 -0
- package/dist/core/transport/tree.d.ts +373 -109
- package/dist/core/transport/types/agent.d.ts +353 -0
- package/dist/core/transport/types/client.d.ts +168 -0
- package/dist/core/transport/types/shared.d.ts +24 -0
- package/dist/core/transport/types/tree.d.ts +315 -0
- package/dist/core/transport/types/view.d.ts +222 -0
- package/dist/core/transport/types.d.ts +13 -553
- package/dist/core/transport/view.d.ts +272 -84
- package/dist/errors.d.ts +21 -10
- package/dist/index.d.ts +6 -8
- package/dist/logger.d.ts +12 -0
- package/dist/react/ably-ai-transport-react.js +976 -990
- 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 +36 -0
- package/dist/react/contexts/client-session-provider.d.ts +53 -0
- package/dist/react/create-session-hooks.d.ts +116 -0
- package/dist/react/index.d.ts +12 -12
- 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 +82 -51
- package/dist/utils.d.ts +32 -23
- package/dist/vercel/ably-ai-transport-vercel.js +2573 -2086
- 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/decoder.d.ts +5 -18
- package/dist/vercel/codec/encoder.d.ts +6 -36
- package/dist/vercel/codec/events.d.ts +51 -0
- package/dist/vercel/codec/index.d.ts +24 -12
- package/dist/vercel/codec/reducer.d.ts +144 -0
- package/dist/vercel/codec/tool-transitions.d.ts +2 -2
- package/dist/vercel/index.d.ts +4 -5
- package/dist/vercel/react/ably-ai-transport-vercel-react.js +3907 -3266
- package/dist/vercel/react/ably-ai-transport-vercel-react.js.map +1 -1
- package/dist/vercel/react/ably-ai-transport-vercel-react.umd.cjs +33 -8
- package/dist/vercel/react/ably-ai-transport-vercel-react.umd.cjs.map +1 -1
- package/dist/vercel/react/contexts/chat-transport-context.d.ts +7 -6
- 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 +29 -0
- package/dist/vercel/transport/chat-transport.d.ts +43 -24
- package/dist/vercel/transport/index.d.ts +25 -21
- package/dist/vercel/transport/run-output-stream.d.ts +56 -0
- package/dist/version.d.ts +2 -0
- package/package.json +30 -23
- package/src/constants.ts +124 -51
- package/src/core/agent.ts +68 -0
- package/src/core/codec/decoder.ts +71 -98
- package/src/core/codec/encoder.ts +113 -65
- package/src/core/codec/index.ts +13 -6
- package/src/core/codec/lifecycle-tracker.ts +10 -9
- package/src/core/codec/types.ts +436 -120
- package/src/core/transport/agent-session.ts +1344 -0
- package/src/core/transport/branch-chain.ts +58 -0
- package/src/core/transport/client-session.ts +775 -0
- package/src/core/transport/decode-fold.ts +91 -0
- package/src/core/transport/headers.ts +181 -22
- package/src/core/transport/index.ts +25 -26
- package/src/core/transport/internal/bounded-map.ts +27 -0
- package/src/core/transport/invocation.ts +98 -0
- package/src/core/transport/load-conversation.ts +355 -0
- package/src/core/transport/load-history.ts +269 -0
- package/src/core/transport/pipe-stream.ts +54 -39
- package/src/core/transport/run-manager.ts +249 -0
- package/src/core/transport/tree.ts +926 -308
- package/src/core/transport/types/agent.ts +407 -0
- package/src/core/transport/types/client.ts +211 -0
- package/src/core/transport/types/shared.ts +27 -0
- package/src/core/transport/types/tree.ts +344 -0
- package/src/core/transport/types/view.ts +259 -0
- package/src/core/transport/types.ts +13 -706
- package/src/core/transport/view.ts +864 -433
- package/src/errors.ts +22 -9
- package/src/event-emitter.ts +3 -2
- package/src/index.ts +52 -41
- package/src/logger.ts +14 -1
- package/src/react/contexts/client-session-context.ts +41 -0
- package/src/react/contexts/client-session-provider.tsx +186 -0
- package/src/react/create-session-hooks.ts +141 -0
- package/src/react/index.ts +23 -13
- 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 +201 -0
- package/src/react/use-create-view.ts +33 -29
- package/src/react/use-tree.ts +61 -30
- package/src/react/use-view.ts +139 -97
- package/src/utils.ts +63 -45
- package/src/vercel/codec/decoder.ts +336 -258
- package/src/vercel/codec/encoder.ts +343 -205
- package/src/vercel/codec/events.ts +87 -0
- package/src/vercel/codec/index.ts +60 -13
- package/src/vercel/codec/reducer.ts +977 -0
- package/src/vercel/codec/tool-transitions.ts +2 -2
- package/src/vercel/index.ts +6 -19
- package/src/vercel/react/contexts/chat-transport-context.ts +7 -6
- 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 +47 -49
- package/src/vercel/react/use-message-sync.ts +80 -39
- package/src/vercel/run-end-reason.ts +78 -0
- package/src/vercel/transport/chat-transport.ts +392 -98
- package/src/vercel/transport/index.ts +39 -38
- package/src/vercel/transport/run-output-stream.ts +170 -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/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/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,22 +1,9 @@
|
|
|
1
1
|
import { DecoderCoreOptions } from '../../core/codec/decoder.js';
|
|
2
|
-
import {
|
|
2
|
+
import { Decoder } from '../../core/codec/types.js';
|
|
3
|
+
import { VercelInput, VercelOutput } from './events.js';
|
|
3
4
|
/**
|
|
4
|
-
* Vercel AI SDK
|
|
5
|
-
*
|
|
6
|
-
* Maps Ably inbound messages to DecoderOutput<UIMessageChunk, UIMessage>[].
|
|
7
|
-
*
|
|
8
|
-
* Delegates action dispatch and serial tracking to the decoder core.
|
|
9
|
-
* This file contains only the Vercel-specific event building, discrete
|
|
10
|
-
* event decoding, and synthetic event emission.
|
|
11
|
-
*
|
|
12
|
-
* Domain-specific headers use the `x-domain-` prefix. Transport-level
|
|
13
|
-
* headers use the `x-ably-` prefix.
|
|
14
|
-
*/
|
|
15
|
-
import type * as AI from 'ai';
|
|
16
|
-
/**
|
|
17
|
-
* Create a Vercel AI SDK decoder that maps Ably messages to UIMessageChunk
|
|
18
|
-
* events and UIMessage objects via the decoder core.
|
|
5
|
+
* Create a Vercel AI SDK decoder that maps Ably messages to {@link DecodedMessage}.
|
|
19
6
|
* @param options - Decoder configuration (callbacks, logger).
|
|
20
|
-
* @returns A {@link
|
|
7
|
+
* @returns A {@link Decoder} typed in both directions for the Vercel codec.
|
|
21
8
|
*/
|
|
22
|
-
export declare const createDecoder: (options?: DecoderCoreOptions) =>
|
|
9
|
+
export declare const createDecoder: (options?: DecoderCoreOptions) => Decoder<VercelInput, VercelOutput>;
|
|
@@ -1,41 +1,11 @@
|
|
|
1
1
|
import { EncoderCoreOptions } from '../../core/codec/encoder.js';
|
|
2
|
-
import { ChannelWriter,
|
|
2
|
+
import { ChannelWriter, Encoder } from '../../core/codec/types.js';
|
|
3
|
+
import { VercelInput, VercelOutput } from './events.js';
|
|
3
4
|
/**
|
|
4
|
-
* Vercel AI SDK
|
|
5
|
-
*
|
|
6
|
-
* Maps UIMessageChunk events and complete UIMessage objects to Ably channel
|
|
7
|
-
* operations (publish, appendMessage, updateMessage).
|
|
8
|
-
*
|
|
9
|
-
* Delegates the message append lifecycle (publish, append, close, abort,
|
|
10
|
-
* flush/recover) to the encoder core. This file contains only the
|
|
11
|
-
* Vercel-specific event-to-operation mapping.
|
|
12
|
-
*
|
|
13
|
-
* Domain-specific headers use the `x-domain-` prefix to distinguish them
|
|
14
|
-
* from transport-level `x-ably-` headers.
|
|
15
|
-
*
|
|
16
|
-
* ## Core operations and domain headers
|
|
17
|
-
*
|
|
18
|
-
* Each UIMessageChunk maps to exactly one encoder core operation. Domain
|
|
19
|
-
* headers are passed to every operation that accepts them — the core handles
|
|
20
|
-
* merging, persistence, and deduplication:
|
|
21
|
-
*
|
|
22
|
-
* - **`startStream`**: Opens a message stream. Domain headers become
|
|
23
|
-
* "persistent headers" — the core repeats them on every subsequent append.
|
|
24
|
-
* - **`appendStream`**: Appends a text delta. Data only, no headers parameter.
|
|
25
|
-
* The core automatically carries persistent headers from start.
|
|
26
|
-
* - **`closeStream`**: Closes the stream. Pass all domain headers from the
|
|
27
|
-
* chunk — the core merges them on top of persistent headers, so changed
|
|
28
|
-
* values (e.g. updated providerMetadata) are picked up and unchanged
|
|
29
|
-
* values are harmlessly deduplicated.
|
|
30
|
-
* - **`publishDiscrete`**: Publishes a standalone message. All domain headers
|
|
31
|
-
* for the chunk are passed directly.
|
|
32
|
-
*/
|
|
33
|
-
import type * as AI from 'ai';
|
|
34
|
-
/**
|
|
35
|
-
* Create a Vercel AI SDK encoder that maps UIMessageChunk events to Ably
|
|
36
|
-
* channel operations via the encoder core.
|
|
5
|
+
* Create a Vercel AI SDK encoder that maps VercelInput / VercelOutput to
|
|
6
|
+
* Ably channel operations via the encoder core.
|
|
37
7
|
* @param writer - The channel writer to publish messages through.
|
|
38
8
|
* @param options - Encoder configuration (clientId, extras, hooks, logger).
|
|
39
|
-
* @returns
|
|
9
|
+
* @returns An {@link Encoder} typed in both directions for the Vercel codec.
|
|
40
10
|
*/
|
|
41
|
-
export declare const createEncoder: (writer: ChannelWriter, options?: EncoderCoreOptions) =>
|
|
11
|
+
export declare const createEncoder: (writer: ChannelWriter, options?: EncoderCoreOptions) => Encoder<VercelInput, VercelOutput>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Regenerate, ToolApprovalResponse, ToolResult, ToolResultError, UserMessage } from '../../core/codec/types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Vercel codec input/output unions.
|
|
4
|
+
*
|
|
5
|
+
* The codec splits cleanly along the protocol's `ai-input` / `ai-output`
|
|
6
|
+
* wire seam:
|
|
7
|
+
*
|
|
8
|
+
* - **`VercelOutput`** = `AI.UIMessageChunk` — the AI SDK's streamed-output
|
|
9
|
+
* domain model, published by the agent on `ai-output`.
|
|
10
|
+
* - **`VercelInput`** = a discriminated union of the SDK's well-known
|
|
11
|
+
* input shapes — published by the client on `ai-input`. The Vercel
|
|
12
|
+
* codec has no codec-local input variants today: every variant comes
|
|
13
|
+
* from `@ably/ai-transport`'s well-known set.
|
|
14
|
+
*/
|
|
15
|
+
import type * as AI from 'ai';
|
|
16
|
+
/** Vercel domain payload for a {@link ToolResult}. */
|
|
17
|
+
export interface VercelToolResultPayload {
|
|
18
|
+
/** The tool call this result corresponds to. */
|
|
19
|
+
toolCallId: string;
|
|
20
|
+
/** The tool's output value. Tool-defined shape. */
|
|
21
|
+
output: unknown;
|
|
22
|
+
}
|
|
23
|
+
/** Vercel domain payload for a {@link ToolResultError}. */
|
|
24
|
+
export interface VercelToolResultErrorPayload {
|
|
25
|
+
/** The tool call this error corresponds to. */
|
|
26
|
+
toolCallId: string;
|
|
27
|
+
/** Human-readable description of the failure. */
|
|
28
|
+
message: string;
|
|
29
|
+
}
|
|
30
|
+
/** Vercel domain payload for a {@link ToolApprovalResponse}. */
|
|
31
|
+
export interface VercelToolApprovalResponsePayload {
|
|
32
|
+
/** The tool call this approval responds to. */
|
|
33
|
+
toolCallId: string;
|
|
34
|
+
/** Whether the user approved the tool execution. */
|
|
35
|
+
approved: boolean;
|
|
36
|
+
/** Optional human-readable reason (typically used on denial). */
|
|
37
|
+
reason?: string;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The Vercel codec's `TInput` — every record-shape a client publishes on
|
|
41
|
+
* the `ai-input` wire. Composed from the SDK's well-known input shapes,
|
|
42
|
+
* with the tool variants parameterized by the Vercel domain payloads above.
|
|
43
|
+
*/
|
|
44
|
+
export type VercelInput = UserMessage<AI.UIMessage> | Regenerate | ToolResult<VercelToolResultPayload> | ToolResultError<VercelToolResultErrorPayload> | ToolApprovalResponse<VercelToolApprovalResponsePayload>;
|
|
45
|
+
/**
|
|
46
|
+
* The Vercel codec's `TOutput` — every record-shape the agent publishes
|
|
47
|
+
* on the `ai-output` wire. The Vercel codec passes the AI SDK's
|
|
48
|
+
* `UIMessageChunk` through unchanged.
|
|
49
|
+
*/
|
|
50
|
+
export type VercelOutput = AI.UIMessageChunk;
|
|
51
|
+
export type { VercelProjection } from './reducer.js';
|
|
@@ -1,22 +1,34 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { VercelInput, VercelOutput, VercelToolApprovalResponsePayload, VercelToolResultErrorPayload, VercelToolResultPayload } from './events.js';
|
|
2
|
+
import { VercelProjection } from './reducer.js';
|
|
2
3
|
/**
|
|
3
|
-
* Vercel AI SDK codec —
|
|
4
|
-
*
|
|
4
|
+
* Vercel AI SDK codec — implements
|
|
5
|
+
* `Codec<VercelInput, VercelOutput, VercelProjection, UIMessage>`.
|
|
6
|
+
*
|
|
7
|
+
* The codec is the reducer (extends `Reducer<VercelInput | VercelOutput,
|
|
8
|
+
* VercelProjection>`) plus encoder/decoder factories and `getMessages`
|
|
9
|
+
* for Tree population.
|
|
5
10
|
*
|
|
6
11
|
* ```ts
|
|
7
12
|
* import { UIMessageCodec } from '@ably/ai-transport/vercel';
|
|
8
13
|
*
|
|
9
14
|
* const encoder = UIMessageCodec.createEncoder(writer, options);
|
|
10
15
|
* const decoder = UIMessageCodec.createDecoder();
|
|
11
|
-
* const
|
|
16
|
+
* const projection = UIMessageCodec.init();
|
|
12
17
|
* ```
|
|
13
18
|
*/
|
|
14
19
|
import type * as AI from 'ai';
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
export declare const UIMessageCodec: {
|
|
21
|
+
adapterTag: "vercel-ai-sdk-ui-message";
|
|
22
|
+
init: () => VercelProjection;
|
|
23
|
+
fold: (state: VercelProjection, event: VercelInput | VercelOutput, meta: import('../../core/codec/types.js').ReducerMeta) => VercelProjection;
|
|
24
|
+
createEncoder: (writer: import('../../core/codec/types.js').ChannelWriter, options?: import('../../index.js').EncoderCoreOptions) => import('../../core/codec/types.js').Encoder<VercelInput, VercelOutput>;
|
|
25
|
+
createDecoder: (options?: import('../../index.js').DecoderCoreOptions) => import('../../core/codec/types.js').Decoder<VercelInput, VercelOutput>;
|
|
26
|
+
getMessages: (projection: VercelProjection) => import('../../core/codec/types.js').CodecMessage<AI.UIMessage>[];
|
|
27
|
+
createUserMessage: (message: AI.UIMessage) => VercelInput;
|
|
28
|
+
createRegenerate: (target: string, parent: string) => VercelInput;
|
|
29
|
+
createToolResult: (codecMessageId: string, payload: VercelToolResultPayload) => VercelInput;
|
|
30
|
+
createToolResultError: (codecMessageId: string, payload: VercelToolResultErrorPayload) => VercelInput;
|
|
31
|
+
createToolApprovalResponse: (codecMessageId: string, payload: VercelToolApprovalResponsePayload) => VercelInput;
|
|
32
|
+
};
|
|
33
|
+
export type { VercelInput, VercelOutput } from './events.js';
|
|
34
|
+
export { type VercelProjection } from './reducer.js';
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { CodecMessage, ReducerMeta } from '../../core/codec/types.js';
|
|
2
|
+
import { VercelInput, VercelOutput } from './events.js';
|
|
3
|
+
/**
|
|
4
|
+
* Vercel AI SDK reducer.
|
|
5
|
+
*
|
|
6
|
+
* Pure `(init, fold)` over the `VercelInput | VercelOutput` union. Folds
|
|
7
|
+
* input variants (user-message, tool-result, tool-result-error,
|
|
8
|
+
* tool-approval-response) and `UIMessageChunk` outputs into a
|
|
9
|
+
* VercelProjection holding `UIMessage[]` plus internal stream-tracker
|
|
10
|
+
* state.
|
|
11
|
+
*
|
|
12
|
+
* The reducer is stateless: every fold is `(state, event, meta) → state'`,
|
|
13
|
+
* with no instance state. Mutation in place is allowed — the projection
|
|
14
|
+
* is single-owner.
|
|
15
|
+
*
|
|
16
|
+
* Idempotency is **per conflict key**, not stream-wide: when two events
|
|
17
|
+
* compete for the same logical state (e.g. two `tool-output-available`
|
|
18
|
+
* for the same `toolCallId`), the higher-serial one wins and the other
|
|
19
|
+
* is dropped. Unrelated events arrive freely in any order. See
|
|
20
|
+
* `_conflictKeyOf` for the per-variant key derivation.
|
|
21
|
+
*
|
|
22
|
+
* Client-published tool resolutions (`ToolResult`, `ToolResultError`,
|
|
23
|
+
* `ToolApprovalResponse`) carry `codecMessageId` targeting the assistant
|
|
24
|
+
* they amend; the reducer applies the resolution onto that assistant's
|
|
25
|
+
* `dynamic-tool` part directly. If the assistant has not yet arrived in
|
|
26
|
+
* the projection (out-of-order delivery), the resolution is buffered in
|
|
27
|
+
* `pendingToolResolutions` and re-evaluated on each subsequent fold.
|
|
28
|
+
*/
|
|
29
|
+
import type * as AI from 'ai';
|
|
30
|
+
/**
|
|
31
|
+
* Tracks an in-progress tool part within a UIMessage. Text and reasoning
|
|
32
|
+
* parts don't need this — we write to them directly via partIndex. Tool
|
|
33
|
+
* parts need an extra `inputText` buffer because deltas arrive as raw
|
|
34
|
+
* JSON fragments that must be accumulated before parsing.
|
|
35
|
+
*/
|
|
36
|
+
interface ToolPartTracker {
|
|
37
|
+
/** Index in the message's parts array. */
|
|
38
|
+
partIndex: number;
|
|
39
|
+
/** Accumulated streaming input text (for JSON parsing on completion). */
|
|
40
|
+
inputText: string;
|
|
41
|
+
}
|
|
42
|
+
/** Per-codecMessageId tracking state for in-progress streams within a UIMessage. */
|
|
43
|
+
interface MessageTrackers {
|
|
44
|
+
/** Text stream id → partIndex. */
|
|
45
|
+
text: Map<string, number>;
|
|
46
|
+
/** Reasoning stream id → partIndex. */
|
|
47
|
+
reasoning: Map<string, number>;
|
|
48
|
+
/** Tool call id → tracker. */
|
|
49
|
+
tools: Map<string, ToolPartTracker>;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The per-Run state produced by the Vercel codec's reducer.
|
|
53
|
+
*
|
|
54
|
+
* The SDK reads only `messages` (via `Codec.getMessages`). The remaining
|
|
55
|
+
* fields are internal to the reducer; they happen to live on the
|
|
56
|
+
* projection because the projection is the only thing the reducer can
|
|
57
|
+
* carry from fold to fold (it has no instance state).
|
|
58
|
+
*/
|
|
59
|
+
export interface VercelProjection {
|
|
60
|
+
/**
|
|
61
|
+
* UIMessages produced or modified in this Run, in publication order,
|
|
62
|
+
* each paired with its codec-message-id. The reducer correlates strictly
|
|
63
|
+
* on `codecMessageId`; `message.id` is preserved verbatim from the source
|
|
64
|
+
* (the AI SDK stream's `start.messageId` for assistants, the caller's id
|
|
65
|
+
* for user messages) and is never used as an identity key.
|
|
66
|
+
*/
|
|
67
|
+
messages: CodecMessage<AI.UIMessage>[];
|
|
68
|
+
/**
|
|
69
|
+
* Per-conflict-key high-water-marks. Maps a codec-derived conflict key
|
|
70
|
+
* (see `_conflictKeyOf`) to the highest `meta.serial` already folded for
|
|
71
|
+
* that key. Events whose serial is `<=` the stored value are dropped as
|
|
72
|
+
* duplicates of an already-incorporated operation. Events that have no
|
|
73
|
+
* conflict key (additive content, lifecycle markers) are folded
|
|
74
|
+
* unconditionally.
|
|
75
|
+
*/
|
|
76
|
+
conflictSerials: Map<string, string>;
|
|
77
|
+
/** Per-codecMessageId tracker state for streamed parts. Internal — do not access. */
|
|
78
|
+
trackers: Map<string, MessageTrackers>;
|
|
79
|
+
/**
|
|
80
|
+
* Tool-resolution events that arrived before any assistant in this
|
|
81
|
+
* projection had a matching `toolCallId`. Re-evaluated on every
|
|
82
|
+
* subsequent fold so that an out-of-order tool output is folded as
|
|
83
|
+
* soon as the corresponding assistant lands.
|
|
84
|
+
*/
|
|
85
|
+
pendingToolResolutions: PendingToolResolution[];
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* A buffered tool resolution waiting for its assistant message to arrive.
|
|
89
|
+
* The reducer scans pending entries after every successful fold so an
|
|
90
|
+
* out-of-order tool output is promoted as soon as the matching assistant
|
|
91
|
+
* is added to the projection.
|
|
92
|
+
*/
|
|
93
|
+
interface PendingToolResolution {
|
|
94
|
+
/** The codec-message-id of the assistant the resolution targets. */
|
|
95
|
+
targetCodecMessageId: string;
|
|
96
|
+
/** Tool call this resolution targets. */
|
|
97
|
+
toolCallId: string;
|
|
98
|
+
/** Serial of the wire message — used by the conflict-key check on promotion. */
|
|
99
|
+
serial: string;
|
|
100
|
+
/** Variant of the tool-resolution used to transition the assistant's tool part. */
|
|
101
|
+
resolution: {
|
|
102
|
+
kind: 'tool-result';
|
|
103
|
+
output: unknown;
|
|
104
|
+
} | {
|
|
105
|
+
kind: 'tool-result-error';
|
|
106
|
+
message: string;
|
|
107
|
+
} | {
|
|
108
|
+
kind: 'tool-approval-response';
|
|
109
|
+
approved: boolean;
|
|
110
|
+
reason?: string;
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Build an empty initial projection.
|
|
115
|
+
* @returns A fresh VercelProjection with no messages and no tracker state.
|
|
116
|
+
*/
|
|
117
|
+
export declare const init: () => VercelProjection;
|
|
118
|
+
/**
|
|
119
|
+
* Fold one input or output event into the projection. Mutates and returns
|
|
120
|
+
* `state`.
|
|
121
|
+
*
|
|
122
|
+
* Idempotency is per conflict key (see `_conflictKeyOf`): if the event has
|
|
123
|
+
* a conflict key and the projection has already folded an event for that
|
|
124
|
+
* key at a higher-or-equal serial, this call is a no-op. Events without a
|
|
125
|
+
* conflict key (additive content, lifecycle markers) are folded
|
|
126
|
+
* unconditionally. Orphan events (e.g. tool-output for an unknown
|
|
127
|
+
* toolCallId) are dropped silently inside the per-variant fold helpers.
|
|
128
|
+
* @param state - Projection to fold into (may be mutated in place).
|
|
129
|
+
* @param event - Input or output event to fold.
|
|
130
|
+
* @param meta - Transport-derived metadata (serial, optional messageId).
|
|
131
|
+
* @returns The same projection reference, possibly mutated.
|
|
132
|
+
*/
|
|
133
|
+
export declare const fold: (state: VercelProjection, event: VercelInput | VercelOutput, meta: ReducerMeta) => VercelProjection;
|
|
134
|
+
/**
|
|
135
|
+
* Extract the UIMessage list from a projection, each paired with its
|
|
136
|
+
* codec-message-id. Client-published tool resolutions amend existing
|
|
137
|
+
* assistants in place via `kind: 'tool-result'` etc. — they never
|
|
138
|
+
* materialise as their own UIMessage in the projection, so no filtering is
|
|
139
|
+
* needed here.
|
|
140
|
+
* @param projection - Projection produced by `init` + repeated `fold` calls.
|
|
141
|
+
* @returns The visible messages with their codec-message-ids, in publication order.
|
|
142
|
+
*/
|
|
143
|
+
export declare const getMessages: (projection: VercelProjection) => CodecMessage<AI.UIMessage>[];
|
|
144
|
+
export {};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Shared tool part transition logic for the Vercel AI SDK codec.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Keeps the tool output state transition logic in one place, reusable by the
|
|
5
|
+
* Vercel codec reducer and any other callers.
|
|
6
6
|
*/
|
|
7
7
|
import type * as AI from 'ai';
|
|
8
8
|
/** The set of UIMessageChunk types that represent tool output transitions. */
|
package/dist/vercel/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
+
export type { VercelInput, VercelOutput, VercelProjection } from './codec/index.js';
|
|
1
2
|
export { UIMessageCodec } from './codec/index.js';
|
|
2
|
-
export type { ChatTransport, ChatTransportOptions, SendMessagesRequestContext,
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
5
|
-
export type { PrepareApprovalTurnOptions, PrepareApprovalTurnResult, StreamResponseWithApprovalRedirectOptions, ToolApprovalDecision, } from './tool-approvals.js';
|
|
6
|
-
export { applyToolApprovalsToHistory, extractApprovalDecisionsFromHistory, prepareApprovalTurn, streamResponseWithApprovalRedirect, } from './tool-approvals.js';
|
|
3
|
+
export type { ChatTransport, ChatTransportOptions, SendMessagesRequestContext, VercelAgentSessionOptions, VercelClientSessionOptions, } from './transport/index.js';
|
|
4
|
+
export { createAgentSession, createChatTransport, createClientSession } from './transport/index.js';
|
|
5
|
+
export { vercelRunOutcome } from './run-end-reason.js';
|