@ably/ai-transport 0.0.1 → 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 +114 -116
- package/dist/ably-ai-transport.js +1743 -961
- 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 +117 -39
- 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 +410 -101
- 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 +97 -17
- package/dist/core/transport/index.d.ts +5 -3
- 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 -8
- package/dist/core/transport/run-manager.d.ts +78 -0
- package/dist/core/transport/tree.d.ts +435 -0
- 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 -402
- package/dist/core/transport/view.d.ts +354 -0
- package/dist/errors.d.ts +37 -9
- package/dist/index.d.ts +6 -6
- package/dist/logger.d.ts +12 -0
- package/dist/react/ably-ai-transport-react.js +1164 -645
- 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 +16 -10
- package/dist/react/internal/use-resolved-session.d.ts +36 -0
- package/dist/react/use-ably-messages.d.ts +20 -11
- package/dist/react/use-client-session.d.ts +81 -0
- package/dist/react/use-create-view.d.ts +23 -0
- package/dist/react/use-tree.d.ts +35 -0
- package/dist/react/use-view.d.ts +110 -0
- package/dist/utils.d.ts +32 -23
- package/dist/vercel/ably-ai-transport-vercel.js +2748 -1625
- 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 +50 -0
- package/dist/vercel/index.d.ts +4 -2
- package/dist/vercel/react/ably-ai-transport-vercel-react.js +10298 -1410
- package/dist/vercel/react/ably-ai-transport-vercel-react.js.map +1 -1
- package/dist/vercel/react/ably-ai-transport-vercel-react.umd.cjs +70 -1
- package/dist/vercel/react/ably-ai-transport-vercel-react.umd.cjs.map +1 -1
- package/dist/vercel/react/contexts/chat-transport-context.d.ts +33 -0
- package/dist/vercel/react/contexts/chat-transport-provider.d.ts +96 -0
- package/dist/vercel/react/index.d.ts +4 -0
- package/dist/vercel/react/use-chat-transport.d.ts +66 -21
- package/dist/vercel/react/use-message-sync.d.ts +31 -12
- package/dist/vercel/run-end-reason.d.ts +29 -0
- package/dist/vercel/transport/chat-transport.d.ts +71 -30
- package/dist/vercel/transport/index.d.ts +25 -18
- package/dist/vercel/transport/run-output-stream.d.ts +56 -0
- package/dist/version.d.ts +2 -0
- package/package.json +47 -34
- package/src/constants.ts +126 -47
- package/src/core/agent.ts +68 -0
- package/src/core/codec/decoder.ts +71 -98
- package/src/core/codec/encoder.ts +115 -58
- package/src/core/codec/index.ts +13 -6
- package/src/core/codec/lifecycle-tracker.ts +10 -9
- package/src/core/codec/types.ts +438 -106
- 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 +182 -19
- package/src/core/transport/index.ts +29 -22
- 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 +58 -40
- package/src/core/transport/run-manager.ts +249 -0
- package/src/core/transport/tree.ts +1167 -0
- 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 -527
- package/src/core/transport/view.ts +1271 -0
- package/src/errors.ts +42 -9
- package/src/event-emitter.ts +3 -2
- package/src/index.ts +55 -39
- 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 +27 -10
- package/src/react/internal/use-resolved-session.ts +63 -0
- package/src/react/use-ably-messages.ts +47 -19
- package/src/react/use-client-session.ts +201 -0
- package/src/react/use-create-view.ts +72 -0
- package/src/react/use-tree.ts +84 -0
- package/src/react/use-view.ts +275 -0
- package/src/react/vite.config.ts +4 -1
- package/src/utils.ts +63 -45
- package/src/vercel/codec/decoder.ts +336 -255
- package/src/vercel/codec/encoder.ts +348 -196
- package/src/vercel/codec/events.ts +87 -0
- package/src/vercel/codec/index.ts +59 -14
- package/src/vercel/codec/reducer.ts +977 -0
- package/src/vercel/codec/tool-transitions.ts +122 -0
- package/src/vercel/index.ts +7 -3
- package/src/vercel/react/contexts/chat-transport-context.ts +41 -0
- package/src/vercel/react/contexts/chat-transport-provider.tsx +150 -0
- package/src/vercel/react/index.ts +13 -1
- package/src/vercel/react/use-chat-transport.ts +162 -42
- package/src/vercel/react/use-message-sync.ts +121 -22
- package/src/vercel/react/vite.config.ts +4 -2
- package/src/vercel/run-end-reason.ts +78 -0
- package/src/vercel/transport/chat-transport.ts +553 -113
- package/src/vercel/transport/index.ts +40 -28
- 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/conversation-tree.d.ts +0 -9
- package/dist/core/transport/decode-history.d.ts +0 -41
- package/dist/core/transport/server-transport.d.ts +0 -7
- package/dist/core/transport/stream-router.d.ts +0 -19
- package/dist/core/transport/turn-manager.d.ts +0 -34
- package/dist/react/use-active-turns.d.ts +0 -8
- package/dist/react/use-client-transport.d.ts +0 -7
- package/dist/react/use-conversation-tree.d.ts +0 -20
- package/dist/react/use-edit.d.ts +0 -7
- package/dist/react/use-history.d.ts +0 -19
- package/dist/react/use-messages.d.ts +0 -7
- package/dist/react/use-regenerate.d.ts +0 -7
- package/dist/react/use-send.d.ts +0 -7
- package/dist/vercel/codec/accumulator.d.ts +0 -21
- package/src/core/transport/client-transport.ts +0 -959
- package/src/core/transport/conversation-tree.ts +0 -434
- package/src/core/transport/decode-history.ts +0 -337
- package/src/core/transport/server-transport.ts +0 -458
- package/src/core/transport/stream-router.ts +0 -118
- package/src/core/transport/turn-manager.ts +0 -147
- package/src/react/use-active-turns.ts +0 -61
- package/src/react/use-client-transport.ts +0 -37
- package/src/react/use-conversation-tree.ts +0 -71
- package/src/react/use-edit.ts +0 -24
- package/src/react/use-history.ts +0 -111
- package/src/react/use-messages.ts +0 -32
- package/src/react/use-regenerate.ts +0 -24
- package/src/react/use-send.ts +0 -25
- package/src/vercel/codec/accumulator.ts +0 -603
|
@@ -1,407 +1,18 @@
|
|
|
1
|
-
import { Logger } from '../../logger.js';
|
|
2
|
-
import { Codec } from '../codec/types.js';
|
|
3
1
|
/**
|
|
4
2
|
* Core transport types, parameterized by codec event and message types.
|
|
5
3
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
|
|
9
|
-
import type * as Ably from 'ably';
|
|
10
|
-
/** Why a turn ended. */
|
|
11
|
-
export type TurnEndReason = 'complete' | 'cancelled' | 'error';
|
|
12
|
-
/** Filter for cancel operations. At most one field should be set. */
|
|
13
|
-
export interface CancelFilter {
|
|
14
|
-
/** Cancel a specific turn by ID. */
|
|
15
|
-
turnId?: string;
|
|
16
|
-
/** Cancel all turns belonging to the sender's clientId. */
|
|
17
|
-
own?: boolean;
|
|
18
|
-
/** Cancel all turns belonging to a specific clientId. */
|
|
19
|
-
clientId?: string;
|
|
20
|
-
/** Cancel all turns on the channel. */
|
|
21
|
-
all?: boolean;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Passed to the server's `onCancel` hook for authorization decisions.
|
|
25
|
-
* The hook inspects the incoming cancel message and decides whether to
|
|
26
|
-
* allow each matched turn to be aborted.
|
|
27
|
-
*/
|
|
28
|
-
export interface CancelRequest {
|
|
29
|
-
/** The raw Ably message that carried the cancel signal. */
|
|
30
|
-
message: Ably.InboundMessage;
|
|
31
|
-
/** The parsed cancel scope from the message headers. */
|
|
32
|
-
filter: CancelFilter;
|
|
33
|
-
/** Which active turnIds would be cancelled if allowed. */
|
|
34
|
-
matchedTurnIds: string[];
|
|
35
|
-
/** Map of turnId to the ownerClientId for the matched turns. */
|
|
36
|
-
turnOwners: Map<string, string>;
|
|
37
|
-
}
|
|
38
|
-
/** A domain message paired with its Ably transport headers. Used on the read path to snapshot conversation state (e.g. for HTTP POST bodies). */
|
|
39
|
-
export interface MessageWithHeaders<TMessage> {
|
|
40
|
-
/** The domain message. */
|
|
41
|
-
message: TMessage;
|
|
42
|
-
/** Ably headers associated with this message (transport metadata, domain headers). */
|
|
43
|
-
headers?: Record<string, string>;
|
|
44
|
-
}
|
|
45
|
-
/** Options for creating a server transport. */
|
|
46
|
-
export interface ServerTransportOptions<TEvent, TMessage> {
|
|
47
|
-
/** The Ably channel to publish to. Must match the client's channel. */
|
|
48
|
-
channel: Ably.RealtimeChannel;
|
|
49
|
-
/** The codec to use for encoding events and messages. */
|
|
50
|
-
codec: Codec<TEvent, TMessage>;
|
|
51
|
-
/** Logger instance for diagnostic output. */
|
|
52
|
-
logger?: Logger;
|
|
53
|
-
/**
|
|
54
|
-
* Called with non-fatal transport-level errors not scoped to any turn.
|
|
55
|
-
* Examples: cancel listener subscription failure, channel attach errors.
|
|
56
|
-
*/
|
|
57
|
-
onError?: (error: Ably.ErrorInfo) => void;
|
|
58
|
-
}
|
|
59
|
-
/** Options for addMessages — per-operation overrides for message identity and branching. */
|
|
60
|
-
export interface AddMessageOptions {
|
|
61
|
-
/** The user's clientId for attribution. */
|
|
62
|
-
clientId?: string;
|
|
63
|
-
/** The msg-id of the immediately preceding message in this branch. */
|
|
64
|
-
parent?: string | null;
|
|
65
|
-
/** The msg-id of the message this one replaces (creates a fork). */
|
|
66
|
-
forkOf?: string;
|
|
67
|
-
}
|
|
68
|
-
/** Result of publishing user messages via addMessages. */
|
|
69
|
-
export interface AddMessagesResult {
|
|
70
|
-
/** The `x-ably-msg-id` of each published message, in order. */
|
|
71
|
-
msgIds: string[];
|
|
72
|
-
}
|
|
73
|
-
/** Options for streamResponse — per-operation overrides for the assistant message. */
|
|
74
|
-
export interface StreamResponseOptions {
|
|
75
|
-
/** The msg-id of the immediately preceding message in this branch. */
|
|
76
|
-
parent?: string | null;
|
|
77
|
-
/** The msg-id of the message this response replaces (for regeneration). */
|
|
78
|
-
forkOf?: string;
|
|
79
|
-
}
|
|
80
|
-
/** The result of streaming a response through the encoder. */
|
|
81
|
-
export interface StreamResult {
|
|
82
|
-
/** Why the stream ended. */
|
|
83
|
-
reason: TurnEndReason;
|
|
84
|
-
}
|
|
85
|
-
/** Options passed to newTurn for configuring the turn lifecycle. */
|
|
86
|
-
export interface NewTurnOptions<TEvent> {
|
|
87
|
-
/** The turn identifier (generated by the client transport or the server). */
|
|
88
|
-
turnId: string;
|
|
89
|
-
/** The user's clientId for attribution. */
|
|
90
|
-
clientId?: string;
|
|
91
|
-
/**
|
|
92
|
-
* The msg-id of the immediately preceding message in this branch.
|
|
93
|
-
* Used as the default parent for user messages (via addMessages) and
|
|
94
|
-
* assistant messages (via streamResponse) when not overridden per-operation.
|
|
95
|
-
*/
|
|
96
|
-
parent?: string | null;
|
|
97
|
-
/**
|
|
98
|
-
* The msg-id of the message this turn replaces (creates a fork).
|
|
99
|
-
* Stamped on user messages (for edits) or assistant messages
|
|
100
|
-
* (for regeneration).
|
|
101
|
-
*/
|
|
102
|
-
forkOf?: string;
|
|
103
|
-
/**
|
|
104
|
-
* Called before each Ably message is published in this turn.
|
|
105
|
-
* Mutate the Ably message in place to add custom extras.headers.
|
|
106
|
-
*/
|
|
107
|
-
onMessage?: (message: Ably.Message) => void;
|
|
108
|
-
/**
|
|
109
|
-
* Called when the turn's stream is aborted (by cancel or server).
|
|
110
|
-
* Receives a write function to publish final events before the abort finalises.
|
|
111
|
-
*/
|
|
112
|
-
onAbort?: (write: (event: TEvent) => Promise<void>) => void | Promise<void>;
|
|
113
|
-
/**
|
|
114
|
-
* Called when a cancel message arrives matching this turn.
|
|
115
|
-
* Return true to allow cancellation (fires abortSignal, stream aborts).
|
|
116
|
-
* Return false to reject (cancel ignored, stream continues).
|
|
117
|
-
* If not provided, all cancels are accepted.
|
|
118
|
-
*/
|
|
119
|
-
onCancel?: (request: CancelRequest) => Promise<boolean>;
|
|
120
|
-
/**
|
|
121
|
-
* Called with non-fatal errors scoped to this turn. Examples: turn-start
|
|
122
|
-
* publish failure, encoder recovery failure, stream encoding errors.
|
|
123
|
-
*/
|
|
124
|
-
onError?: (error: Ably.ErrorInfo) => void;
|
|
125
|
-
}
|
|
126
|
-
/** A server-side turn with explicit lifecycle methods. */
|
|
127
|
-
export interface Turn<TEvent, TMessage> {
|
|
128
|
-
/** The turn's unique identifier. */
|
|
129
|
-
readonly turnId: string;
|
|
130
|
-
/** Abort signal scoped to this turn. Fires when a cancel event arrives for this turnId. */
|
|
131
|
-
readonly abortSignal: AbortSignal;
|
|
132
|
-
/** Publish turn-start event to the channel. Must be called before addMessages or streamResponse. */
|
|
133
|
-
start(): Promise<void>;
|
|
134
|
-
/**
|
|
135
|
-
* Publish user messages to the channel, scoped to this turn.
|
|
136
|
-
* Each message is published with its own headers (including `x-ably-msg-id`
|
|
137
|
-
* for optimistic reconciliation with the client's inserts). Per-message
|
|
138
|
-
* headers from `MessageWithHeaders` override transport-generated defaults.
|
|
139
|
-
* @returns The msg-ids of all published messages, in order.
|
|
140
|
-
*/
|
|
141
|
-
addMessages(messages: MessageWithHeaders<TMessage>[], options?: AddMessageOptions): Promise<AddMessagesResult>;
|
|
142
|
-
/**
|
|
143
|
-
* Pipe a ReadableStream through the encoder to the channel.
|
|
144
|
-
* Returns when the stream completes, is cancelled, or errors.
|
|
145
|
-
* Does NOT call end() — the caller must call end() after streamResponse returns.
|
|
146
|
-
*/
|
|
147
|
-
streamResponse(stream: ReadableStream<TEvent>, options?: StreamResponseOptions): Promise<StreamResult>;
|
|
148
|
-
/** Publish turn-end event to the channel and clean up. */
|
|
149
|
-
end(reason: TurnEndReason): Promise<void>;
|
|
150
|
-
}
|
|
151
|
-
/** Server-side transport that manages turn lifecycles over an Ably channel. */
|
|
152
|
-
export interface ServerTransport<TEvent, TMessage> {
|
|
153
|
-
/**
|
|
154
|
-
* Create a new turn. Synchronous — no channel activity until start() is called.
|
|
155
|
-
* The turn is registered for cancel routing immediately so that early cancels
|
|
156
|
-
* fire the abort signal.
|
|
157
|
-
*/
|
|
158
|
-
newTurn(options: NewTurnOptions<TEvent>): Turn<TEvent, TMessage>;
|
|
159
|
-
/** Unsubscribe from cancel messages, abort all active turns, and clean up. */
|
|
160
|
-
close(): void;
|
|
161
|
-
}
|
|
162
|
-
/** Options for creating a client transport. */
|
|
163
|
-
export interface ClientTransportOptions<TEvent, TMessage> {
|
|
164
|
-
/** The Ably channel to receive responses on and publish cancel signals to. */
|
|
165
|
-
channel: Ably.RealtimeChannel;
|
|
166
|
-
/** The codec to use for encoding/decoding. */
|
|
167
|
-
codec: Codec<TEvent, TMessage>;
|
|
168
|
-
/** The client's identity. Sent to the server in the POST body. */
|
|
169
|
-
clientId?: string;
|
|
170
|
-
/** Server endpoint URL for the HTTP POST. Defaults to `"/api/chat"`. */
|
|
171
|
-
api?: string;
|
|
172
|
-
/** Headers for the HTTP POST. Function form for dynamic values (e.g. auth tokens). */
|
|
173
|
-
headers?: Record<string, string> | (() => Record<string, string>);
|
|
174
|
-
/** Additional body fields merged into the HTTP POST. Function form for dynamic values. */
|
|
175
|
-
body?: Record<string, unknown> | (() => Record<string, unknown>);
|
|
176
|
-
/** Fetch credentials mode for the HTTP POST. */
|
|
177
|
-
credentials?: RequestCredentials;
|
|
178
|
-
/** Custom fetch implementation. Defaults to `globalThis.fetch`. */
|
|
179
|
-
fetch?: typeof globalThis.fetch;
|
|
180
|
-
/** Initial messages to seed the conversation tree with. Forms a linear chain. */
|
|
181
|
-
messages?: TMessage[];
|
|
182
|
-
/** Logger instance for diagnostic output. */
|
|
183
|
-
logger?: Logger;
|
|
184
|
-
}
|
|
185
|
-
/** Per-send options for customizing the HTTP POST and branching metadata. */
|
|
186
|
-
export interface SendOptions {
|
|
187
|
-
/** Additional fields merged into the HTTP POST body. */
|
|
188
|
-
body?: Record<string, unknown>;
|
|
189
|
-
/** Additional headers for the HTTP POST. */
|
|
190
|
-
headers?: Record<string, string>;
|
|
191
|
-
/**
|
|
192
|
-
* The msg-id of the message this send replaces (fork).
|
|
193
|
-
* Set for regeneration (forkOf an assistant message) or
|
|
194
|
-
* edit (forkOf a user message).
|
|
195
|
-
*/
|
|
196
|
-
forkOf?: string;
|
|
197
|
-
/**
|
|
198
|
-
* The msg-id of the message that precedes this one in the
|
|
199
|
-
* conversation thread. Null means the message is a root.
|
|
200
|
-
* If omitted, auto-computed from the last message in the tree.
|
|
201
|
-
*/
|
|
202
|
-
parent?: string | null;
|
|
203
|
-
}
|
|
204
|
-
/** A structured event describing a turn starting or ending. */
|
|
205
|
-
export type TurnLifecycleEvent = {
|
|
206
|
-
type: 'x-ably-turn-start';
|
|
207
|
-
turnId: string;
|
|
208
|
-
clientId: string;
|
|
209
|
-
} | {
|
|
210
|
-
type: 'x-ably-turn-end';
|
|
211
|
-
turnId: string;
|
|
212
|
-
clientId: string;
|
|
213
|
-
reason: TurnEndReason;
|
|
214
|
-
};
|
|
215
|
-
/** A handle to an active client-side turn, returned by `send()`, `regenerate()`, and `edit()`. */
|
|
216
|
-
export interface ActiveTurn<TEvent> {
|
|
217
|
-
/** The decoded event stream for this turn. */
|
|
218
|
-
stream: ReadableStream<TEvent>;
|
|
219
|
-
/** The turn's unique identifier. */
|
|
220
|
-
turnId: string;
|
|
221
|
-
/** Cancel this specific turn. Publishes a cancel message and closes the local stream. */
|
|
222
|
-
cancel(): Promise<void>;
|
|
223
|
-
}
|
|
224
|
-
/** Options for closing a client transport. */
|
|
225
|
-
export interface CloseOptions {
|
|
226
|
-
/** Cancel in-progress turns before closing. Publishes a cancel message to the channel. */
|
|
227
|
-
cancel?: CancelFilter;
|
|
228
|
-
}
|
|
229
|
-
/** A page of decoded messages from channel history. */
|
|
230
|
-
export interface PaginatedMessages<TMessage> {
|
|
231
|
-
/** Decoded messages in chronological order (oldest first). */
|
|
232
|
-
items: TMessage[];
|
|
233
|
-
/** Headers for each item, parallel to `items`. Used by the transport to populate the tree. */
|
|
234
|
-
itemHeaders?: Record<string, string>[];
|
|
235
|
-
/** Ably serial for each item, parallel to `items`. Used by the transport for tree ordering. */
|
|
236
|
-
itemSerials?: string[];
|
|
237
|
-
/** Raw Ably messages that produced this page, in chronological order. */
|
|
238
|
-
rawMessages?: Ably.InboundMessage[];
|
|
239
|
-
/** Whether there are older pages available. */
|
|
240
|
-
hasNext(): boolean;
|
|
241
|
-
/** Fetch the next (older) page. Returns undefined if no more pages. */
|
|
242
|
-
next(): Promise<PaginatedMessages<TMessage> | undefined>;
|
|
243
|
-
}
|
|
244
|
-
/** Options for loading channel history. */
|
|
245
|
-
export interface LoadHistoryOptions {
|
|
246
|
-
/** Max messages per page. Default: 100. */
|
|
247
|
-
limit?: number;
|
|
248
|
-
}
|
|
249
|
-
/** A node in the conversation tree, representing a single domain message. */
|
|
250
|
-
export interface ConversationNode<TMessage> {
|
|
251
|
-
/** The domain message. */
|
|
252
|
-
message: TMessage;
|
|
253
|
-
/** The x-ably-msg-id of this node — primary key in the tree. */
|
|
254
|
-
msgId: string;
|
|
255
|
-
/** Parent node's msg-id (x-ably-parent), or undefined for root messages. */
|
|
256
|
-
parentId: string | undefined;
|
|
257
|
-
/** The msg-id this node forks from (x-ably-fork-of), or undefined if first version. */
|
|
258
|
-
forkOf: string | undefined;
|
|
259
|
-
/** Full Ably headers for this message. */
|
|
260
|
-
headers: Record<string, string>;
|
|
261
|
-
/**
|
|
262
|
-
* Ably serial for this message. Lexicographically comparable for total order.
|
|
263
|
-
* Used to sort siblings deterministically regardless of delivery/history order.
|
|
264
|
-
* Absent for optimistic messages (set when the server relay arrives).
|
|
265
|
-
*/
|
|
266
|
-
serial: string | undefined;
|
|
267
|
-
}
|
|
268
|
-
/**
|
|
269
|
-
* Materializes a branching conversation tree from a flat oplog.
|
|
4
|
+
* The definitions live in `./types/`, split along the Tree / View / Session
|
|
5
|
+
* boundary. This barrel re-exports them so consumers keep importing from a
|
|
6
|
+
* single module:
|
|
270
7
|
*
|
|
271
|
-
*
|
|
272
|
-
*
|
|
273
|
-
*
|
|
8
|
+
* - `shared.ts` — cross-cutting (RunEndReason, CancelRequest).
|
|
9
|
+
* - `agent.ts` — agent session, run runtime, Run / AgentSession.
|
|
10
|
+
* - `tree.ts` — conversation-tree nodes, lifecycle events, Tree.
|
|
11
|
+
* - `view.ts` — history pagination, branch selection, View.
|
|
12
|
+
* - `client.ts` — client session options, ActiveRun, ClientSession.
|
|
274
13
|
*/
|
|
275
|
-
export
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
flatten(): TMessage[];
|
|
281
|
-
/**
|
|
282
|
-
* Get all messages that are siblings (alternatives) at a given
|
|
283
|
-
* fork point. Returns an array ordered chronologically by serial.
|
|
284
|
-
* The message identified by msgId is always included.
|
|
285
|
-
*/
|
|
286
|
-
getSiblings(msgId: string): TMessage[];
|
|
287
|
-
/** Whether a message has sibling alternatives (i.e., show navigation arrows). */
|
|
288
|
-
hasSiblings(msgId: string): boolean;
|
|
289
|
-
/** Get the index of the currently selected sibling at a fork point. */
|
|
290
|
-
getSelectedIndex(msgId: string): number;
|
|
291
|
-
/**
|
|
292
|
-
* Select a sibling at a fork point by index. Updates the active branch.
|
|
293
|
-
* Calling flatten() after this returns the new linear thread.
|
|
294
|
-
* Index is clamped to `[0, siblings.length - 1]`.
|
|
295
|
-
*/
|
|
296
|
-
select(msgId: string, index: number): void;
|
|
297
|
-
/** Get a node by msgId, or undefined if not found. */
|
|
298
|
-
getNode(msgId: string): ConversationNode<TMessage> | undefined;
|
|
299
|
-
/**
|
|
300
|
-
* Get a node by codec message key (e.g. UIMessage.id), or undefined if
|
|
301
|
-
* not found. Uses a secondary index since the tree is keyed by x-ably-msg-id.
|
|
302
|
-
*/
|
|
303
|
-
getNodeByKey(key: string): ConversationNode<TMessage> | undefined;
|
|
304
|
-
/** Get the stored headers for a node by msgId, or undefined if not found. */
|
|
305
|
-
getHeaders(msgId: string): Record<string, string> | undefined;
|
|
306
|
-
/**
|
|
307
|
-
* Insert or update a message in the tree. Reads parent/forkOf from the
|
|
308
|
-
* provided headers. If the message already exists (by msgId), updates
|
|
309
|
-
* it in place. The optional serial is the Ably message serial used for
|
|
310
|
-
* deterministic sibling ordering.
|
|
311
|
-
*/
|
|
312
|
-
upsert(msgId: string, message: TMessage, headers: Record<string, string>, serial?: string): void;
|
|
313
|
-
/** Remove a message from the tree. */
|
|
314
|
-
delete(msgId: string): void;
|
|
315
|
-
}
|
|
316
|
-
/** Entry in the StreamRouter's turn map. Not part of the public API. */
|
|
317
|
-
export interface TurnEntry<TEvent> {
|
|
318
|
-
/** The ReadableStream controller for this turn. */
|
|
319
|
-
controller: ReadableStreamDefaultController<TEvent>;
|
|
320
|
-
/** The turn's unique identifier. */
|
|
321
|
-
turnId: string;
|
|
322
|
-
}
|
|
323
|
-
/** Client-side transport that manages conversation state over an Ably channel. */
|
|
324
|
-
export interface ClientTransport<TEvent, TMessage> {
|
|
325
|
-
/**
|
|
326
|
-
* Send one or more messages and start a new turn. Returns a handle to the
|
|
327
|
-
* active turn with the decoded event stream and a cancel function.
|
|
328
|
-
*
|
|
329
|
-
* The HTTP POST is fire-and-forget — the returned stream is available
|
|
330
|
-
* immediately. If the POST fails, the error is surfaced via `on("error")`.
|
|
331
|
-
*/
|
|
332
|
-
send(messages: TMessage | TMessage[], options?: SendOptions): Promise<ActiveTurn<TEvent>>;
|
|
333
|
-
/**
|
|
334
|
-
* Regenerate an assistant message. Creates a new turn that forks the
|
|
335
|
-
* target message with no new user messages. Automatically computes
|
|
336
|
-
* `forkOf`, `parent`, and truncated `history` from the tree.
|
|
337
|
-
*
|
|
338
|
-
* Pass `options.body.history` to override the default truncated history.
|
|
339
|
-
*/
|
|
340
|
-
regenerate(messageId: string, options?: SendOptions): Promise<ActiveTurn<TEvent>>;
|
|
341
|
-
/**
|
|
342
|
-
* Edit a user message. Creates a new turn that forks the target message
|
|
343
|
-
* with replacement content. Automatically computes `forkOf`, `parent`,
|
|
344
|
-
* and `history` from the tree.
|
|
345
|
-
*/
|
|
346
|
-
edit(messageId: string, newMessages: TMessage | TMessage[], options?: SendOptions): Promise<ActiveTurn<TEvent>>;
|
|
347
|
-
/**
|
|
348
|
-
* Access the conversation tree for branch navigation.
|
|
349
|
-
* The tree is updated in real-time by the transport's channel subscription.
|
|
350
|
-
*/
|
|
351
|
-
getTree(): ConversationTree<TMessage>;
|
|
352
|
-
/** Cancel turns matching the filter. Defaults to `{ own: true }` (all own turns). */
|
|
353
|
-
cancel(filter?: CancelFilter): Promise<void>;
|
|
354
|
-
/**
|
|
355
|
-
* Returns a promise that resolves when all active turns matching the filter
|
|
356
|
-
* have completed. Resolves immediately if no matching turns are active.
|
|
357
|
-
* Defaults to `{ own: true }`.
|
|
358
|
-
*/
|
|
359
|
-
waitForTurn(filter?: CancelFilter): Promise<void>;
|
|
360
|
-
/**
|
|
361
|
-
* Subscribe to message store changes or raw Ably message additions.
|
|
362
|
-
* The handler is called with no arguments — call `getMessages()` or
|
|
363
|
-
* `getAblyMessages()` for the current state. Returns an unsubscribe function.
|
|
364
|
-
*/
|
|
365
|
-
on(event: 'message' | 'ably-message', handler: () => void): () => void;
|
|
366
|
-
/** Subscribe to turn lifecycle events (start, end). Returns an unsubscribe function. */
|
|
367
|
-
on(event: 'turn', handler: (event: TurnLifecycleEvent) => void): () => void;
|
|
368
|
-
/**
|
|
369
|
-
* Subscribe to non-fatal transport errors. These indicate something went
|
|
370
|
-
* wrong but the transport is still operational. Returns an unsubscribe function.
|
|
371
|
-
*/
|
|
372
|
-
on(event: 'error', handler: (error: Ably.ErrorInfo) => void): () => void;
|
|
373
|
-
/**
|
|
374
|
-
* Get the accumulated raw Ably messages, in chronological order.
|
|
375
|
-
* Includes both live messages and history-loaded messages.
|
|
376
|
-
*/
|
|
377
|
-
getAblyMessages(): Ably.InboundMessage[];
|
|
378
|
-
/** Get all currently active turns, keyed by clientId. */
|
|
379
|
-
getActiveTurnIds(): Map<string, Set<string>>;
|
|
380
|
-
/** Get Ably headers associated with a message via the conversation tree. */
|
|
381
|
-
getMessageHeaders(message: TMessage): Record<string, string> | undefined;
|
|
382
|
-
/** Get the current message list (follows selected branches). Updated by message lifecycle events. */
|
|
383
|
-
getMessages(): TMessage[];
|
|
384
|
-
/**
|
|
385
|
-
* Snapshot the current message list as message + headers pairs.
|
|
386
|
-
* Convenience for building the `history` body field in HTTP POSTs.
|
|
387
|
-
*/
|
|
388
|
-
getMessagesWithHeaders(): MessageWithHeaders<TMessage>[];
|
|
389
|
-
/**
|
|
390
|
-
* Load a page of conversation history from the channel, decoded through
|
|
391
|
-
* the transport's codec. Uses `untilAttach` for gapless continuity with
|
|
392
|
-
* the live subscription.
|
|
393
|
-
*
|
|
394
|
-
* History messages are inserted into the conversation tree and trigger
|
|
395
|
-
* a notification. Returns a PaginatedMessages handle — call `next()`
|
|
396
|
-
* for older pages.
|
|
397
|
-
*/
|
|
398
|
-
history(options?: LoadHistoryOptions): Promise<PaginatedMessages<TMessage>>;
|
|
399
|
-
/**
|
|
400
|
-
* Tear down the transport: unsubscribe from the channel, close active
|
|
401
|
-
* streams, clear all handlers, and prevent further operations.
|
|
402
|
-
*
|
|
403
|
-
* Pass `cancel` to publish a cancel message before closing. Without it,
|
|
404
|
-
* only local state is torn down (the server keeps streaming).
|
|
405
|
-
*/
|
|
406
|
-
close(options?: CloseOptions): Promise<void>;
|
|
407
|
-
}
|
|
14
|
+
export type * from './types/agent.js';
|
|
15
|
+
export type * from './types/client.js';
|
|
16
|
+
export type * from './types/shared.js';
|
|
17
|
+
export type * from './types/tree.js';
|
|
18
|
+
export type * from './types/view.js';
|