@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
package/dist/constants.d.ts
CHANGED
|
@@ -1,54 +1,128 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Shared constants used by both codec and transport layers.
|
|
3
3
|
*
|
|
4
|
-
* Header constants define the
|
|
5
|
-
* name constants define the
|
|
4
|
+
* Header constants define the transport wire header names. Message and event
|
|
5
|
+
* name constants define the session lifecycle signals on the channel.
|
|
6
6
|
*
|
|
7
7
|
* These live at the top level (not in codec/ or transport/) because both
|
|
8
8
|
* layers need them — the codec core reads/writes stream and status headers,
|
|
9
|
-
* while the transport layer reads/writes
|
|
9
|
+
* while the transport layer reads/writes run, cancel, and role headers.
|
|
10
10
|
*/
|
|
11
11
|
/** Header: whether this Ably message uses streaming (message appends) or is discrete. Always "true" or "false". */
|
|
12
|
-
export declare const HEADER_STREAM = "
|
|
13
|
-
/** Header: lifecycle status of a streamed message. Only set when
|
|
14
|
-
export declare const HEADER_STATUS = "
|
|
12
|
+
export declare const HEADER_STREAM = "stream";
|
|
13
|
+
/** Header: lifecycle status of a streamed message. Only set when stream is "true". One of "streaming", "complete", or "cancelled". */
|
|
14
|
+
export declare const HEADER_STATUS = "status";
|
|
15
15
|
/** Header: stream identity. Set by the encoder on every streamed message; read by the decoder to correlate streams. */
|
|
16
|
-
export declare const HEADER_STREAM_ID = "
|
|
16
|
+
export declare const HEADER_STREAM_ID = "stream-id";
|
|
17
17
|
/** Header: marks a message as a discrete message part (from writeMessages). Set by publishDiscreteBatch; not set on lifecycle events from publishDiscrete. */
|
|
18
|
-
export declare const HEADER_DISCRETE = "
|
|
19
|
-
/** Header:
|
|
20
|
-
export declare const
|
|
18
|
+
export declare const HEADER_DISCRETE = "discrete";
|
|
19
|
+
/** Header: run correlation ID. Set on every agent-published message and on continuation client inputs, but omitted from the originating fresh client input (the agent mints the run-id at run-start). */
|
|
20
|
+
export declare const HEADER_RUN_ID = "run-id";
|
|
21
|
+
/** Header: invocation correlation ID; identifies a specific invocation under a run. Agent-minted and stamped by the agent on every event it publishes for the invocation — run lifecycle (run-start/resume/suspend/end) and assistant outputs. Never set by the client on its input. */
|
|
22
|
+
export declare const HEADER_INVOCATION_ID = "invocation-id";
|
|
23
|
+
/**
|
|
24
|
+
* Header: per-event identifier stamped by the client on every
|
|
25
|
+
* client-published event in a send — user-message events AND amend
|
|
26
|
+
* events (tool-approval responses, client tool outputs). Distinct from
|
|
27
|
+
* `codec-message-id` so it survives edits/retries that reuse the same
|
|
28
|
+
* codec-message-id, and so amend events that target an existing message can
|
|
29
|
+
* carry their own per-send identity. The invocation body lists every
|
|
30
|
+
* inputEventId the agent must observe on the channel before starting LLM
|
|
31
|
+
* work — see `Run.start()`'s input-event lookup.
|
|
32
|
+
*/
|
|
33
|
+
export declare const HEADER_EVENT_ID = "event-id";
|
|
21
34
|
/** Header: message identity. Assigned per message (user or assistant). Used for optimistic reconciliation on the client. */
|
|
22
|
-
export declare const
|
|
23
|
-
/** Header: clientId of the user who initiated the
|
|
24
|
-
export declare const
|
|
35
|
+
export declare const HEADER_CODEC_MESSAGE_ID = "codec-message-id";
|
|
36
|
+
/** Header: clientId of the user who initiated the run. Stamped by the client on its user input and re-stamped by the agent on the run's lifecycle and stream messages. */
|
|
37
|
+
export declare const HEADER_RUN_CLIENT_ID = "run-client-id";
|
|
38
|
+
/**
|
|
39
|
+
* Header: clientId of the input event (the `ai-input`) that drove the
|
|
40
|
+
* current invocation. The agent reads the publisher's Ably-level `clientId`
|
|
41
|
+
* from the triggering input event on the channel and re-stamps it as
|
|
42
|
+
* `input-client-id` on every event it publishes for that invocation
|
|
43
|
+
* (run lifecycle and assistant outputs). May differ from
|
|
44
|
+
* `run-client-id` on continuation invocations driven by an input
|
|
45
|
+
* from a non-owner (e.g. a tool-result publish from a different client).
|
|
46
|
+
* Not stamped on `ai-input` events themselves — the wire publisher's
|
|
47
|
+
* Ably `clientId` already conveys that.
|
|
48
|
+
*/
|
|
49
|
+
export declare const HEADER_INPUT_CLIENT_ID = "input-client-id";
|
|
25
50
|
/** Header: message role (e.g. "user", "assistant"). */
|
|
26
|
-
export declare const HEADER_ROLE = "
|
|
27
|
-
/** Header: the
|
|
28
|
-
export declare const
|
|
29
|
-
/** Header:
|
|
30
|
-
export declare const
|
|
31
|
-
/**
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
export declare const
|
|
51
|
+
export declare const HEADER_ROLE = "role";
|
|
52
|
+
/** Header: the codec-message-id of the immediately preceding message in this branch. */
|
|
53
|
+
export declare const HEADER_PARENT = "parent";
|
|
54
|
+
/** Header: the codec-message-id of the message this one replaces (creates a fork). */
|
|
55
|
+
export declare const HEADER_FORK_OF = "fork-of";
|
|
56
|
+
/**
|
|
57
|
+
* Header: the codec-message-id of the assistant message this run regenerates.
|
|
58
|
+
*
|
|
59
|
+
* Stamped on the regenerate wire (and echoed on `run-start`) when the
|
|
60
|
+
* client requested a regeneration. A regenerate run parents at the SAME input
|
|
61
|
+
* node as the reply it regenerates, so it joins that input's reply runs as a
|
|
62
|
+
* same-parent sibling (no fork-of). The View consults this header to resolve
|
|
63
|
+
* the message-level sibling group and to drop the regenerated message from
|
|
64
|
+
* earlier Runs in the visible chain (Spec: AIT-CT13d).
|
|
65
|
+
*/
|
|
66
|
+
export declare const HEADER_MSG_REGENERATE = "msg-regenerate";
|
|
67
|
+
/** Header: reason a run ended (on ai-run-end messages). */
|
|
68
|
+
export declare const HEADER_RUN_REASON = "run-reason";
|
|
69
|
+
/**
|
|
70
|
+
* Header: the `codec-message-id` of the input event that triggered the run.
|
|
71
|
+
* The triggering input is the one whose `event-id` matches the invocation's
|
|
72
|
+
* `inputEventId` (the last input of the originating send). The agent
|
|
73
|
+
* re-stamps it on every event it publishes for the invocation (run
|
|
74
|
+
* lifecycle + assistant outputs), mirroring `input-client-id`. This is the
|
|
75
|
+
* codec-message-id the client owns at send time, so it lets the client
|
|
76
|
+
* correlate any of those events back to the originating input without
|
|
77
|
+
* depending on a client-minted run-id or invocation-id.
|
|
78
|
+
*/
|
|
79
|
+
export declare const HEADER_INPUT_CODEC_MESSAGE_ID = "input-codec-message-id";
|
|
80
|
+
/** Header: numeric error code accompanying an `ai-run-end` with reason `error`. */
|
|
81
|
+
export declare const HEADER_ERROR_CODE = "error-code";
|
|
82
|
+
/** Header: human-readable error message accompanying an `ai-run-end` with reason `error`. */
|
|
83
|
+
export declare const HEADER_ERROR_MESSAGE = "error-message";
|
|
84
|
+
/**
|
|
85
|
+
* Message name: client->agent cancel intent. Targets a run by `run-id` (a
|
|
86
|
+
* continuation, whose run-id the client already knows) and/or by
|
|
87
|
+
* `input-codec-message-id` (a fresh send, whose run-id the agent mints at
|
|
88
|
+
* run-start — so the client can only key the cancel by the triggering input's
|
|
89
|
+
* codec-message-id it owns at send time). The agent resolves whichever is
|
|
90
|
+
* present to the registered run; a cancel that arrives before the run is known
|
|
91
|
+
* (the input-event lookup hasn't resolved the input id to a run yet) is
|
|
92
|
+
* buffered by `input-codec-message-id` and honoured when the run resolves it.
|
|
93
|
+
* Also carries an `event-id` so channel rewind redelivers it to a per-request /
|
|
94
|
+
* serverless agent that attaches after the cancel was published.
|
|
95
|
+
*/
|
|
96
|
+
export declare const EVENT_CANCEL = "ai-cancel";
|
|
97
|
+
/** Message name: server publishes this to signal a run has started. */
|
|
98
|
+
export declare const EVENT_RUN_START = "ai-run-start";
|
|
99
|
+
/**
|
|
100
|
+
* Message name: server publishes this to signal a run has suspended — paused
|
|
101
|
+
* awaiting participant input (e.g. a client tool result or approval) without
|
|
102
|
+
* ending. The run stays live and may be resumed under the same `runId`.
|
|
103
|
+
* Distinct from `ai-run-end`, which is terminal.
|
|
104
|
+
*/
|
|
105
|
+
export declare const EVENT_RUN_SUSPEND = "ai-run-suspend";
|
|
106
|
+
/**
|
|
107
|
+
* Message name: server publishes this when a subsequent invocation re-enters an
|
|
108
|
+
* already-started run (e.g. a tool-result follow-up under the same `runId`).
|
|
109
|
+
* A pure re-entry signal: unlike `ai-run-start` it carries no `parent` / `fork-of`
|
|
110
|
+
* (the original `ai-run-start` already established the run's structure).
|
|
111
|
+
*/
|
|
112
|
+
export declare const EVENT_RUN_RESUME = "ai-run-resume";
|
|
113
|
+
/** Message name: server publishes this to signal a run has ended. */
|
|
114
|
+
export declare const EVENT_RUN_END = "ai-run-end";
|
|
115
|
+
/**
|
|
116
|
+
* Message name: every agent-published codec event (text, reasoning, tool calls,
|
|
117
|
+
* tool outputs, lifecycle helpers, file / source parts, data-* chunks) rides
|
|
118
|
+
* this single wire name. The codec event's own `type` is carried in the
|
|
119
|
+
* codec-level `type` header so the decoder can dispatch.
|
|
120
|
+
*/
|
|
121
|
+
export declare const EVENT_AI_OUTPUT = "ai-output";
|
|
122
|
+
/**
|
|
123
|
+
* Message name: every client-published codec event (user-message parts,
|
|
124
|
+
* tool-approval responses, regenerate signals) rides this single wire
|
|
125
|
+
* name. The codec event's own kind is carried in the codec-level `type`
|
|
126
|
+
* header so the decoder can dispatch.
|
|
127
|
+
*/
|
|
128
|
+
export declare const EVENT_AI_INPUT = "ai-input";
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wraps the two paths chat-js uses (see ChatClient._addAgent): the
|
|
3
|
+
* `options.agents` mutation (read by ably-js when opening the initial
|
|
4
|
+
* WebSocket) and the `params.agent` channel option (sent on ATTACH so
|
|
5
|
+
* an already-open connection still carries the identifier).
|
|
6
|
+
*
|
|
7
|
+
* `options.agents` is a private API on the Realtime client — no public
|
|
8
|
+
* typed accessor exists in the `ably` package — so this module casts to a
|
|
9
|
+
* `RealtimeWithOptions` shape to write it.
|
|
10
|
+
*/
|
|
11
|
+
import type * as Ably from 'ably';
|
|
12
|
+
/**
|
|
13
|
+
* Register this SDK (and optionally a codec) on the supplied Realtime client
|
|
14
|
+
* and return the channel options the caller should pass to
|
|
15
|
+
* `client.channels.get(...)` so the agent is also carried on channel ATTACH.
|
|
16
|
+
* Sets `options.agents['ai-transport-js'] = VERSION`. When the codec carries
|
|
17
|
+
* an internal `adapterTag` field (via {@link AdapterTagHolder}), also sets
|
|
18
|
+
* `options.agents[adapterTag] = VERSION`.
|
|
19
|
+
* Idempotent — repeated calls with the same client and codec produce the same keys/values.
|
|
20
|
+
* Spec: AIT-CT1a, AIT-CT1a2, AIT-CT1a3, AIT-ST1a, AIT-ST1a2, AIT-ST1a3.
|
|
21
|
+
* @param client - The Ably Realtime client to register on.
|
|
22
|
+
* @param codec - The codec instance; cast to {@link AdapterTagHolder} to detect an optional identifier.
|
|
23
|
+
* @returns Channel options containing `params.agent` for `channels.get`.
|
|
24
|
+
*/
|
|
25
|
+
export declare const registerAgent: (client: Ably.Realtime, codec?: unknown) => {
|
|
26
|
+
params: {
|
|
27
|
+
agent: string;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Logger } from '../../logger.js';
|
|
2
|
-
import {
|
|
2
|
+
import { MessagePayload, StreamTrackerState } from './types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Decoder core — action dispatch and serial tracking machinery.
|
|
5
5
|
*
|
|
@@ -8,15 +8,12 @@ import { DecoderOutput, MessagePayload, StreamTrackerState } from './types.js';
|
|
|
8
8
|
* event decoding.
|
|
9
9
|
*
|
|
10
10
|
* Domain decoders call `createDecoderCore(hooks, options)` and provide hooks
|
|
11
|
-
* for stream classification, event building, and discrete decoding.
|
|
11
|
+
* for stream classification, event building, and discrete decoding. Hooks
|
|
12
|
+
* return a flat `TEvent[]` — no event-vs-message union. Per-message routing
|
|
13
|
+
* concerns (`codec-message-id`) are handled by the SDK via `ReducerMeta`, not
|
|
14
|
+
* here.
|
|
12
15
|
*/
|
|
13
16
|
import type * as Ably from 'ably';
|
|
14
|
-
/**
|
|
15
|
-
* Wrap a domain event as a single-element decoder output array.
|
|
16
|
-
* @param event - The domain event to wrap.
|
|
17
|
-
* @returns A single-element array containing the event as a decoder output.
|
|
18
|
-
*/
|
|
19
|
-
export declare const eventOutput: <TEvent, TMessage>(event: TEvent) => DecoderOutput<TEvent, TMessage>[];
|
|
20
17
|
/** Options for creating a decoder core. */
|
|
21
18
|
export interface DecoderCoreOptions {
|
|
22
19
|
/** Called when a tracked stream is replaced (non-prefix update). Receives the tracker with updated state. */
|
|
@@ -27,31 +24,31 @@ export interface DecoderCoreOptions {
|
|
|
27
24
|
logger?: Logger;
|
|
28
25
|
}
|
|
29
26
|
/** Hooks that a domain codec provides to the decoder core for stream classification and event building. */
|
|
30
|
-
export interface DecoderCoreHooks<TEvent
|
|
27
|
+
export interface DecoderCoreHooks<TEvent> {
|
|
31
28
|
/**
|
|
32
29
|
* Build domain events emitted when a new stream starts. May return multiple
|
|
33
30
|
* events (e.g. a start event and a start-step event).
|
|
34
31
|
*/
|
|
35
|
-
buildStartEvents(tracker: StreamTrackerState):
|
|
32
|
+
buildStartEvents(tracker: StreamTrackerState): TEvent[];
|
|
36
33
|
/** Build domain events for a text delta received on a stream. */
|
|
37
|
-
buildDeltaEvents(tracker: StreamTrackerState, delta: string):
|
|
34
|
+
buildDeltaEvents(tracker: StreamTrackerState, delta: string): TEvent[];
|
|
38
35
|
/**
|
|
39
|
-
* Build domain events emitted when a stream
|
|
40
|
-
* Not called for
|
|
41
|
-
* tracker.
|
|
36
|
+
* Build domain events emitted when a stream completes (status:complete).
|
|
37
|
+
* Not called for cancelled streams. The closing codec headers may differ
|
|
38
|
+
* from tracker.codecHeaders if the closing append carried updated headers.
|
|
42
39
|
*/
|
|
43
|
-
buildEndEvents(tracker: StreamTrackerState,
|
|
40
|
+
buildEndEvents(tracker: StreamTrackerState, closingCodecHeaders: Record<string, string>): TEvent[];
|
|
44
41
|
/**
|
|
45
|
-
* Decode a discrete message (message.create
|
|
46
|
-
* or a non-streamable first-contact update). Handles user messages,
|
|
47
|
-
*
|
|
42
|
+
* Decode a discrete message (a `message.create` whose stream header is not
|
|
43
|
+
* "true", or a non-streamable first-contact update). Handles user messages,
|
|
44
|
+
* tool lifecycle, data-*, etc.
|
|
48
45
|
*/
|
|
49
|
-
decodeDiscrete(input: MessagePayload):
|
|
46
|
+
decodeDiscrete(input: MessagePayload): TEvent[];
|
|
50
47
|
}
|
|
51
48
|
/** The decoder core returned by {@link createDecoderCore}. */
|
|
52
|
-
export interface DecoderCore<TEvent
|
|
53
|
-
/** Decode a single Ably message into zero or more domain
|
|
54
|
-
decode(message: Ably.InboundMessage):
|
|
49
|
+
export interface DecoderCore<TEvent> {
|
|
50
|
+
/** Decode a single Ably message into zero or more domain TEvents. */
|
|
51
|
+
decode(message: Ably.InboundMessage): TEvent[];
|
|
55
52
|
}
|
|
56
53
|
/**
|
|
57
54
|
* Create a decoder core with the given domain hooks.
|
|
@@ -59,4 +56,4 @@ export interface DecoderCore<TEvent, TMessage> {
|
|
|
59
56
|
* @param options - Decoder configuration (callbacks, logger).
|
|
60
57
|
* @returns A new {@link DecoderCore} instance.
|
|
61
58
|
*/
|
|
62
|
-
export declare const createDecoderCore: <TEvent
|
|
59
|
+
export declare const createDecoderCore: <TEvent>(hooks: DecoderCoreHooks<TEvent>, options?: DecoderCoreOptions) => DecoderCore<TEvent>;
|
|
@@ -3,7 +3,7 @@ import { ChannelWriter, EncoderOptions, MessagePayload, StreamPayload, WriteOpti
|
|
|
3
3
|
/**
|
|
4
4
|
* Encoder core — message append lifecycle machinery.
|
|
5
5
|
*
|
|
6
|
-
* Provides Ably primitives (publish, append, close,
|
|
6
|
+
* Provides Ably primitives (publish, append, close, cancel, flush) that
|
|
7
7
|
* domain-specific encoders wire their event types to.
|
|
8
8
|
*
|
|
9
9
|
* Domain encoders call `createEncoderCore(writer, options)` and use the
|
|
@@ -22,28 +22,31 @@ export interface EncoderCore {
|
|
|
22
22
|
publishDiscrete(payload: MessagePayload, opts?: WriteOptions): Promise<Ably.PublishResult>;
|
|
23
23
|
/** Publish multiple discrete messages atomically in a single channel publish. */
|
|
24
24
|
publishDiscreteBatch(payloads: MessagePayload[], opts?: WriteOptions): Promise<Ably.PublishResult>;
|
|
25
|
-
/** Start a streamed message with
|
|
25
|
+
/** Start a streamed message with status:streaming. */
|
|
26
26
|
startStream(streamId: string, payload: StreamPayload, opts?: WriteOptions): Promise<void>;
|
|
27
27
|
/**
|
|
28
28
|
* Append data to an in-flight streamed message. Fire-and-forget: errors are
|
|
29
|
-
* collected internally and surfaced by {@link closeStream}
|
|
29
|
+
* collected internally and surfaced by {@link closeStream}, {@link cancelStream},
|
|
30
|
+
* {@link cancelAllStreams} or {@link close}.
|
|
31
|
+
* @throws {Ably.ErrorInfo} InvalidArgument if there is no active stream for `streamId` or the core is closed.
|
|
30
32
|
*/
|
|
31
33
|
appendStream(streamId: string, data: string): void;
|
|
32
34
|
/**
|
|
33
|
-
* Close a streamed message with
|
|
35
|
+
* Close a streamed message with status:complete. Flushes all pending
|
|
34
36
|
* appends for recovery before returning. Repeats persistent and payload headers.
|
|
37
|
+
* @throws {Ably.ErrorInfo} InvalidArgument if there is no active stream for `streamId`, or the encoder has been closed; EncoderRecoveryFailed if a failed append cannot be recovered during the flush.
|
|
35
38
|
*/
|
|
36
39
|
closeStream(streamId: string, payload: StreamPayload): Promise<void>;
|
|
37
40
|
/**
|
|
38
|
-
*
|
|
41
|
+
* Cancel a single in-progress stream (status:cancelled) and flush all
|
|
39
42
|
* pending appends for recovery before returning.
|
|
40
43
|
*/
|
|
41
|
-
|
|
44
|
+
cancelStream(streamId: string, opts?: WriteOptions): Promise<void>;
|
|
42
45
|
/**
|
|
43
|
-
*
|
|
46
|
+
* Cancel all in-progress streams (status:cancelled) and flush all
|
|
44
47
|
* pending appends for recovery before returning.
|
|
45
48
|
*/
|
|
46
|
-
|
|
49
|
+
cancelAllStreams(opts?: WriteOptions): Promise<void>;
|
|
47
50
|
/** Flush + clear trackers. Idempotent. */
|
|
48
51
|
close(): Promise<void>;
|
|
49
52
|
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export type { ChannelWriter, Codec, DecoderOutput, DiscreteEncoder, EncoderOptions, Extras, MessageAccumulator, MessagePayload, StreamDecoder, StreamEncoder, StreamPayload, StreamTrackerState, WriteOptions, } from './types.js';
|
|
1
|
+
export type { ChannelWriter, Codec, CodecInputEvent, CodecMessage, CodecOutputEvent, DecodedMessage, Decoder, Encoder, EncoderOptions, Extras, MessagePayload, Reducer, ReducerMeta, Regenerate, StreamPayload, StreamTrackerState, ToolApprovalResponse, ToolResult, ToolResultError, UserMessage, WriteOptions, } from './types.js';
|
|
3
2
|
export type { EncoderCore, EncoderCoreOptions } from './encoder.js';
|
|
4
3
|
export { createEncoderCore } from './encoder.js';
|
|
5
4
|
export type { DecoderCore, DecoderCoreHooks, DecoderCoreOptions } from './decoder.js';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Generic lifecycle tracker for codec decoders.
|
|
3
3
|
*
|
|
4
|
-
* Manages per-scope (typically per-
|
|
4
|
+
* Manages per-scope (typically per-run) tracking of lifecycle phases that
|
|
5
5
|
* must be emitted before content events. When a phase has not been emitted
|
|
6
6
|
* (e.g. mid-stream join), the tracker synthesizes the missing events using
|
|
7
7
|
* codec-provided build functions.
|
|
@@ -30,15 +30,16 @@ export interface PhaseConfig<TEvent> {
|
|
|
30
30
|
* Per-scope lifecycle tracker that ensures required phases are emitted
|
|
31
31
|
* before content events, synthesizing missing ones for mid-stream joins.
|
|
32
32
|
*
|
|
33
|
-
* Scoped by an arbitrary string key (typically a
|
|
33
|
+
* Scoped by an arbitrary string key (typically a run ID). Each scope
|
|
34
34
|
* tracks independently which phases have been emitted.
|
|
35
35
|
*/
|
|
36
36
|
export interface LifecycleTracker<TEvent> {
|
|
37
37
|
/**
|
|
38
38
|
* Ensure all configured phases have been emitted for the given scope.
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
39
|
+
* Synthesizes and returns the events for any phases not yet marked as
|
|
40
|
+
* emitted, marking each as emitted so it is not synthesized again.
|
|
41
|
+
* Returns an empty array if all phases are already emitted.
|
|
42
|
+
* @param scopeId - The scope to check (e.g. run ID).
|
|
42
43
|
* @param context - Key-value pairs passed through to phase build functions.
|
|
43
44
|
* @returns Synthetic events for missing phases, in configuration order.
|
|
44
45
|
*/
|
|
@@ -46,7 +47,7 @@ export interface LifecycleTracker<TEvent> {
|
|
|
46
47
|
/**
|
|
47
48
|
* Mark a phase as emitted from the wire (not synthetic). Call this
|
|
48
49
|
* when the real event arrives so the tracker does not re-synthesize it.
|
|
49
|
-
* @param scopeId - The scope (e.g.
|
|
50
|
+
* @param scopeId - The scope (e.g. run ID).
|
|
50
51
|
* @param phaseKey - The phase key to mark.
|
|
51
52
|
*/
|
|
52
53
|
markEmitted(scopeId: string, phaseKey: string): void;
|
|
@@ -54,13 +55,13 @@ export interface LifecycleTracker<TEvent> {
|
|
|
54
55
|
* Reset a phase so it will be re-synthesized on the next
|
|
55
56
|
* {@link ensurePhases} call. Used for repeating phases (e.g. "start-step"
|
|
56
57
|
* resets after "finish-step").
|
|
57
|
-
* @param scopeId - The scope (e.g.
|
|
58
|
+
* @param scopeId - The scope (e.g. run ID).
|
|
58
59
|
* @param phaseKey - The phase key to reset.
|
|
59
60
|
*/
|
|
60
61
|
resetPhase(scopeId: string, phaseKey: string): void;
|
|
61
62
|
/**
|
|
62
|
-
* Remove all tracking state for a scope. Call on
|
|
63
|
-
* (finish,
|
|
63
|
+
* Remove all tracking state for a scope. Call on run completion
|
|
64
|
+
* (finish, cancel) to free memory.
|
|
64
65
|
* @param scopeId - The scope to clear.
|
|
65
66
|
*/
|
|
66
67
|
clearScope(scopeId: string): void;
|