@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.
Files changed (167) hide show
  1. package/README.md +114 -116
  2. package/dist/ably-ai-transport.js +1743 -961
  3. package/dist/ably-ai-transport.js.map +1 -1
  4. package/dist/ably-ai-transport.umd.cjs +1 -1
  5. package/dist/ably-ai-transport.umd.cjs.map +1 -1
  6. package/dist/constants.d.ts +117 -39
  7. package/dist/core/agent.d.ts +29 -0
  8. package/dist/core/codec/decoder.d.ts +20 -23
  9. package/dist/core/codec/encoder.d.ts +11 -8
  10. package/dist/core/codec/index.d.ts +1 -2
  11. package/dist/core/codec/lifecycle-tracker.d.ts +10 -9
  12. package/dist/core/codec/types.d.ts +410 -101
  13. package/dist/core/transport/agent-session.d.ts +10 -0
  14. package/dist/core/transport/branch-chain.d.ts +43 -0
  15. package/dist/core/transport/client-session.d.ts +13 -0
  16. package/dist/core/transport/decode-fold.d.ts +47 -0
  17. package/dist/core/transport/headers.d.ts +97 -17
  18. package/dist/core/transport/index.d.ts +5 -3
  19. package/dist/core/transport/internal/bounded-map.d.ts +20 -0
  20. package/dist/core/transport/invocation.d.ts +74 -0
  21. package/dist/core/transport/load-conversation.d.ts +128 -0
  22. package/dist/core/transport/load-history.d.ts +39 -0
  23. package/dist/core/transport/pipe-stream.d.ts +9 -8
  24. package/dist/core/transport/run-manager.d.ts +78 -0
  25. package/dist/core/transport/tree.d.ts +435 -0
  26. package/dist/core/transport/types/agent.d.ts +353 -0
  27. package/dist/core/transport/types/client.d.ts +168 -0
  28. package/dist/core/transport/types/shared.d.ts +24 -0
  29. package/dist/core/transport/types/tree.d.ts +315 -0
  30. package/dist/core/transport/types/view.d.ts +222 -0
  31. package/dist/core/transport/types.d.ts +13 -402
  32. package/dist/core/transport/view.d.ts +354 -0
  33. package/dist/errors.d.ts +37 -9
  34. package/dist/index.d.ts +6 -6
  35. package/dist/logger.d.ts +12 -0
  36. package/dist/react/ably-ai-transport-react.js +1164 -645
  37. package/dist/react/ably-ai-transport-react.js.map +1 -1
  38. package/dist/react/ably-ai-transport-react.umd.cjs +1 -1
  39. package/dist/react/ably-ai-transport-react.umd.cjs.map +1 -1
  40. package/dist/react/contexts/client-session-context.d.ts +36 -0
  41. package/dist/react/contexts/client-session-provider.d.ts +53 -0
  42. package/dist/react/create-session-hooks.d.ts +116 -0
  43. package/dist/react/index.d.ts +16 -10
  44. package/dist/react/internal/use-resolved-session.d.ts +36 -0
  45. package/dist/react/use-ably-messages.d.ts +20 -11
  46. package/dist/react/use-client-session.d.ts +81 -0
  47. package/dist/react/use-create-view.d.ts +23 -0
  48. package/dist/react/use-tree.d.ts +35 -0
  49. package/dist/react/use-view.d.ts +110 -0
  50. package/dist/utils.d.ts +32 -23
  51. package/dist/vercel/ably-ai-transport-vercel.js +2748 -1625
  52. package/dist/vercel/ably-ai-transport-vercel.js.map +1 -1
  53. package/dist/vercel/ably-ai-transport-vercel.umd.cjs +1 -1
  54. package/dist/vercel/ably-ai-transport-vercel.umd.cjs.map +1 -1
  55. package/dist/vercel/codec/decoder.d.ts +5 -18
  56. package/dist/vercel/codec/encoder.d.ts +6 -36
  57. package/dist/vercel/codec/events.d.ts +51 -0
  58. package/dist/vercel/codec/index.d.ts +24 -12
  59. package/dist/vercel/codec/reducer.d.ts +144 -0
  60. package/dist/vercel/codec/tool-transitions.d.ts +50 -0
  61. package/dist/vercel/index.d.ts +4 -2
  62. package/dist/vercel/react/ably-ai-transport-vercel-react.js +10298 -1410
  63. package/dist/vercel/react/ably-ai-transport-vercel-react.js.map +1 -1
  64. package/dist/vercel/react/ably-ai-transport-vercel-react.umd.cjs +70 -1
  65. package/dist/vercel/react/ably-ai-transport-vercel-react.umd.cjs.map +1 -1
  66. package/dist/vercel/react/contexts/chat-transport-context.d.ts +33 -0
  67. package/dist/vercel/react/contexts/chat-transport-provider.d.ts +96 -0
  68. package/dist/vercel/react/index.d.ts +4 -0
  69. package/dist/vercel/react/use-chat-transport.d.ts +66 -21
  70. package/dist/vercel/react/use-message-sync.d.ts +31 -12
  71. package/dist/vercel/run-end-reason.d.ts +29 -0
  72. package/dist/vercel/transport/chat-transport.d.ts +71 -30
  73. package/dist/vercel/transport/index.d.ts +25 -18
  74. package/dist/vercel/transport/run-output-stream.d.ts +56 -0
  75. package/dist/version.d.ts +2 -0
  76. package/package.json +47 -34
  77. package/src/constants.ts +126 -47
  78. package/src/core/agent.ts +68 -0
  79. package/src/core/codec/decoder.ts +71 -98
  80. package/src/core/codec/encoder.ts +115 -58
  81. package/src/core/codec/index.ts +13 -6
  82. package/src/core/codec/lifecycle-tracker.ts +10 -9
  83. package/src/core/codec/types.ts +438 -106
  84. package/src/core/transport/agent-session.ts +1344 -0
  85. package/src/core/transport/branch-chain.ts +58 -0
  86. package/src/core/transport/client-session.ts +775 -0
  87. package/src/core/transport/decode-fold.ts +91 -0
  88. package/src/core/transport/headers.ts +182 -19
  89. package/src/core/transport/index.ts +29 -22
  90. package/src/core/transport/internal/bounded-map.ts +27 -0
  91. package/src/core/transport/invocation.ts +98 -0
  92. package/src/core/transport/load-conversation.ts +355 -0
  93. package/src/core/transport/load-history.ts +269 -0
  94. package/src/core/transport/pipe-stream.ts +58 -40
  95. package/src/core/transport/run-manager.ts +249 -0
  96. package/src/core/transport/tree.ts +1167 -0
  97. package/src/core/transport/types/agent.ts +407 -0
  98. package/src/core/transport/types/client.ts +211 -0
  99. package/src/core/transport/types/shared.ts +27 -0
  100. package/src/core/transport/types/tree.ts +344 -0
  101. package/src/core/transport/types/view.ts +259 -0
  102. package/src/core/transport/types.ts +13 -527
  103. package/src/core/transport/view.ts +1271 -0
  104. package/src/errors.ts +42 -9
  105. package/src/event-emitter.ts +3 -2
  106. package/src/index.ts +55 -39
  107. package/src/logger.ts +14 -1
  108. package/src/react/contexts/client-session-context.ts +41 -0
  109. package/src/react/contexts/client-session-provider.tsx +186 -0
  110. package/src/react/create-session-hooks.ts +141 -0
  111. package/src/react/index.ts +27 -10
  112. package/src/react/internal/use-resolved-session.ts +63 -0
  113. package/src/react/use-ably-messages.ts +47 -19
  114. package/src/react/use-client-session.ts +201 -0
  115. package/src/react/use-create-view.ts +72 -0
  116. package/src/react/use-tree.ts +84 -0
  117. package/src/react/use-view.ts +275 -0
  118. package/src/react/vite.config.ts +4 -1
  119. package/src/utils.ts +63 -45
  120. package/src/vercel/codec/decoder.ts +336 -255
  121. package/src/vercel/codec/encoder.ts +348 -196
  122. package/src/vercel/codec/events.ts +87 -0
  123. package/src/vercel/codec/index.ts +59 -14
  124. package/src/vercel/codec/reducer.ts +977 -0
  125. package/src/vercel/codec/tool-transitions.ts +122 -0
  126. package/src/vercel/index.ts +7 -3
  127. package/src/vercel/react/contexts/chat-transport-context.ts +41 -0
  128. package/src/vercel/react/contexts/chat-transport-provider.tsx +150 -0
  129. package/src/vercel/react/index.ts +13 -1
  130. package/src/vercel/react/use-chat-transport.ts +162 -42
  131. package/src/vercel/react/use-message-sync.ts +121 -22
  132. package/src/vercel/react/vite.config.ts +4 -2
  133. package/src/vercel/run-end-reason.ts +78 -0
  134. package/src/vercel/transport/chat-transport.ts +553 -113
  135. package/src/vercel/transport/index.ts +40 -28
  136. package/src/vercel/transport/run-output-stream.ts +170 -0
  137. package/src/version.ts +2 -0
  138. package/dist/core/transport/client-transport.d.ts +0 -10
  139. package/dist/core/transport/conversation-tree.d.ts +0 -9
  140. package/dist/core/transport/decode-history.d.ts +0 -41
  141. package/dist/core/transport/server-transport.d.ts +0 -7
  142. package/dist/core/transport/stream-router.d.ts +0 -19
  143. package/dist/core/transport/turn-manager.d.ts +0 -34
  144. package/dist/react/use-active-turns.d.ts +0 -8
  145. package/dist/react/use-client-transport.d.ts +0 -7
  146. package/dist/react/use-conversation-tree.d.ts +0 -20
  147. package/dist/react/use-edit.d.ts +0 -7
  148. package/dist/react/use-history.d.ts +0 -19
  149. package/dist/react/use-messages.d.ts +0 -7
  150. package/dist/react/use-regenerate.d.ts +0 -7
  151. package/dist/react/use-send.d.ts +0 -7
  152. package/dist/vercel/codec/accumulator.d.ts +0 -21
  153. package/src/core/transport/client-transport.ts +0 -959
  154. package/src/core/transport/conversation-tree.ts +0 -434
  155. package/src/core/transport/decode-history.ts +0 -337
  156. package/src/core/transport/server-transport.ts +0 -458
  157. package/src/core/transport/stream-router.ts +0 -118
  158. package/src/core/transport/turn-manager.ts +0 -147
  159. package/src/react/use-active-turns.ts +0 -61
  160. package/src/react/use-client-transport.ts +0 -37
  161. package/src/react/use-conversation-tree.ts +0 -71
  162. package/src/react/use-edit.ts +0 -24
  163. package/src/react/use-history.ts +0 -111
  164. package/src/react/use-messages.ts +0 -32
  165. package/src/react/use-regenerate.ts +0 -24
  166. package/src/react/use-send.ts +0 -25
  167. package/src/vercel/codec/accumulator.ts +0 -603
@@ -1,50 +1,128 @@
1
1
  /**
2
2
  * Shared constants used by both codec and transport layers.
3
3
  *
4
- * Header constants define the `x-ably-*` wire protocol. Message and event
5
- * name constants define the transport lifecycle signals on the channel.
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 turn, cancel, and role headers.
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 = "x-ably-stream";
13
- /** Header: lifecycle status of a streamed message. Only set when x-ably-stream is "true". */
14
- export declare const HEADER_STATUS = "x-ably-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 = "x-ably-stream-id";
17
- /** Header: turn correlation ID. Set on every message in a turn. */
18
- export declare const HEADER_TURN_ID = "x-ably-turn-id";
16
+ export declare const HEADER_STREAM_ID = "stream-id";
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 = "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";
19
34
  /** Header: message identity. Assigned per message (user or assistant). Used for optimistic reconciliation on the client. */
20
- export declare const HEADER_MSG_ID = "x-ably-msg-id";
21
- /** Header: clientId of the user who initiated the turn. Set by the server on stream messages. */
22
- export declare const HEADER_TURN_CLIENT_ID = "x-ably-turn-client-id";
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";
23
50
  /** Header: message role (e.g. "user", "assistant"). */
24
- export declare const HEADER_ROLE = "x-ably-role";
25
- /** Header: cancel a specific turn by ID. */
26
- export declare const HEADER_CANCEL_TURN_ID = "x-ably-cancel-turn-id";
27
- /** Header: cancel all turns belonging to the sender's clientId. */
28
- export declare const HEADER_CANCEL_OWN = "x-ably-cancel-own";
29
- /** Header: cancel all turns on the channel. */
30
- export declare const HEADER_CANCEL_ALL = "x-ably-cancel-all";
31
- /** Header: cancel all turns belonging to a specific clientId. */
32
- export declare const HEADER_CANCEL_CLIENT_ID = "x-ably-cancel-client-id";
33
- /** Header: the msg-id of the immediately preceding message in this branch. */
34
- export declare const HEADER_PARENT = "x-ably-parent";
35
- /** Header: the msg-id of the message this one replaces (creates a fork). */
36
- export declare const HEADER_FORK_OF = "x-ably-fork-of";
37
- /** Header: reason a turn ended (on x-ably-turn-end messages). */
38
- export declare const HEADER_TURN_REASON = "x-ably-turn-reason";
39
- /** Message name: client->server cancel signal. */
40
- export declare const EVENT_CANCEL = "x-ably-cancel";
41
- /** Message name: server publishes this to signal a turn has started. */
42
- export declare const EVENT_TURN_START = "x-ably-turn-start";
43
- /** Message name: server publishes this to signal a turn has ended. */
44
- export declare const EVENT_TURN_END = "x-ably-turn-end";
45
- /** Message name: transport-level abort signal (stream cancelled). */
46
- export declare const EVENT_ABORT = "x-ably-abort";
47
- /** Message name: transport-level error signal. */
48
- export declare const EVENT_ERROR = "x-ably-error";
49
- /** Prefix for domain-specific headers. Distinguishes codec-layer headers from transport `x-ably-*` headers. */
50
- export declare const DOMAIN_HEADER_PREFIX = "x-domain-";
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 { DecoderOutput, MessagePayload, StreamTrackerState } from './types.js';
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, TMessage> {
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): DecoderOutput<TEvent, TMessage>[];
32
+ buildStartEvents(tracker: StreamTrackerState): TEvent[];
36
33
  /** Build domain events for a text delta received on a stream. */
37
- buildDeltaEvents(tracker: StreamTrackerState, delta: string): DecoderOutput<TEvent, TMessage>[];
34
+ buildDeltaEvents(tracker: StreamTrackerState, delta: string): TEvent[];
38
35
  /**
39
- * Build domain events emitted when a stream finishes (x-ably-status:finished).
40
- * Not called for aborted streams. The closing headers may differ from
41
- * tracker.headers if the closing append carried updated headers.
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, closingHeaders: Record<string, string>): DecoderOutput<TEvent, TMessage>[];
40
+ buildEndEvents(tracker: StreamTrackerState, closingCodecHeaders: Record<string, string>): TEvent[];
44
41
  /**
45
- * Decode a discrete message (message.create where x-ably-stream is "false",
46
- * or a non-streamable first-contact update). Handles user messages, lifecycle
47
- * events, tool lifecycle, data-*, etc.
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): DecoderOutput<TEvent, TMessage>[];
46
+ decodeDiscrete(input: MessagePayload): TEvent[];
50
47
  }
51
48
  /** The decoder core returned by {@link createDecoderCore}. */
52
- export interface DecoderCore<TEvent, TMessage> {
53
- /** Decode a single Ably message into zero or more domain outputs. */
54
- decode(message: Ably.InboundMessage): DecoderOutput<TEvent, TMessage>[];
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, TMessage>(hooks: DecoderCoreHooks<TEvent, TMessage>, options?: DecoderCoreOptions) => DecoderCore<TEvent, TMessage>;
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, abort, flush) that
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 x-ably-status:streaming. */
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} or {@link close}.
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 x-ably-status:finished. Flushes all pending
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
- * Abort a single in-progress stream (x-ably-status:aborted) and flush all
41
+ * Cancel a single in-progress stream (status:cancelled) and flush all
39
42
  * pending appends for recovery before returning.
40
43
  */
41
- abortStream(streamId: string, opts?: WriteOptions): Promise<void>;
44
+ cancelStream(streamId: string, opts?: WriteOptions): Promise<void>;
42
45
  /**
43
- * Abort all in-progress streams (x-ably-status:aborted) and flush all
46
+ * Cancel all in-progress streams (status:cancelled) and flush all
44
47
  * pending appends for recovery before returning.
45
48
  */
46
- abortAllStreams(opts?: WriteOptions): Promise<void>;
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 { eventOutput } from './decoder.js';
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-turn) tracking of lifecycle phases that
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 turn ID). Each scope
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
- * Returns synthetic events for any phases not yet marked as emitted,
40
- * then marks them. Returns an empty array if all phases are current.
41
- * @param scopeId - The scope to check (e.g. turn ID).
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. turn ID).
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. turn ID).
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 turn completion
63
- * (finish, abort) to free memory.
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;