awaitly 2.0.0 → 3.0.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 (88) hide show
  1. package/dist/di-Biw_plBn.d.cts +15 -0
  2. package/dist/di-DxyH2N3i.d.ts +15 -0
  3. package/dist/durable.cjs +4670 -0
  4. package/dist/durable.cjs.map +1 -0
  5. package/dist/durable.d.cts +9 -0
  6. package/dist/durable.d.ts +9 -0
  7. package/dist/durable.js +4610 -0
  8. package/dist/durable.js.map +1 -0
  9. package/dist/{di-BbFFfO8y.d.ts → duration-BjVn3QpB.d.cts} +1 -15
  10. package/dist/{di-BDlT7InM.d.cts → duration-BjVn3QpB.d.ts} +1 -15
  11. package/dist/engine.cjs +4501 -0
  12. package/dist/engine.cjs.map +1 -0
  13. package/dist/engine.d.cts +113 -0
  14. package/dist/engine.d.ts +113 -0
  15. package/dist/engine.js +4474 -0
  16. package/dist/engine.js.map +1 -0
  17. package/dist/{errors-DtXvrCiO.d.cts → errors-sDSeaZBO.d.cts} +1 -1
  18. package/dist/{errors-DtXvrCiO.d.ts → errors-sDSeaZBO.d.ts} +1 -1
  19. package/dist/guards-CwQZro1F.d.ts +72 -0
  20. package/dist/guards-DhhOJZda.d.cts +72 -0
  21. package/dist/hitl-BnnnLzh2.d.cts +468 -0
  22. package/dist/hitl-pYfkQMv_.d.ts +468 -0
  23. package/dist/hitl.cjs +646 -0
  24. package/dist/hitl.cjs.map +1 -0
  25. package/dist/hitl.d.cts +440 -0
  26. package/dist/hitl.d.ts +440 -0
  27. package/dist/hitl.js +605 -0
  28. package/dist/hitl.js.map +1 -0
  29. package/dist/index-Bew12SZ8.d.ts +417 -0
  30. package/dist/index-Bwt-iz50.d.cts +417 -0
  31. package/dist/{types-B8NfNRGX.d.ts → index-Cv4K_3sZ.d.ts} +2 -1162
  32. package/dist/{types-BZ2f4MRR.d.cts → index-ICmfQi08.d.cts} +2 -1162
  33. package/dist/index.cjs.map +1 -1
  34. package/dist/index.d.cts +11 -1527
  35. package/dist/index.d.ts +11 -1527
  36. package/dist/persistence.cjs +552 -0
  37. package/dist/persistence.cjs.map +1 -0
  38. package/dist/persistence.d.cts +254 -0
  39. package/dist/persistence.d.ts +254 -0
  40. package/dist/persistence.js +499 -0
  41. package/dist/persistence.js.map +1 -0
  42. package/dist/reliability.cjs +1651 -0
  43. package/dist/reliability.cjs.map +1 -0
  44. package/dist/reliability.d.cts +1442 -0
  45. package/dist/reliability.d.ts +1442 -0
  46. package/dist/reliability.js +1589 -0
  47. package/dist/reliability.js.map +1 -0
  48. package/dist/result.d.cts +1 -1
  49. package/dist/result.d.ts +1 -1
  50. package/dist/run.cjs +2283 -0
  51. package/dist/run.cjs.map +1 -0
  52. package/dist/run.d.cts +90 -0
  53. package/dist/run.d.ts +90 -0
  54. package/dist/run.js +2247 -0
  55. package/dist/run.js.map +1 -0
  56. package/dist/saga.cjs +3699 -0
  57. package/dist/saga.cjs.map +1 -0
  58. package/dist/saga.d.cts +162 -0
  59. package/dist/saga.d.ts +162 -0
  60. package/dist/saga.js +3670 -0
  61. package/dist/saga.js.map +1 -0
  62. package/dist/store-contract-BI98VYmX.d.ts +66 -0
  63. package/dist/store-contract-C2HyR_o6.d.cts +66 -0
  64. package/dist/streaming.cjs +895 -0
  65. package/dist/streaming.cjs.map +1 -0
  66. package/dist/streaming.d.cts +594 -0
  67. package/dist/streaming.d.ts +594 -0
  68. package/dist/streaming.js +832 -0
  69. package/dist/streaming.js.map +1 -0
  70. package/dist/testing.d.cts +4 -2
  71. package/dist/testing.d.ts +4 -2
  72. package/dist/types-BDTxgKKc.d.ts +845 -0
  73. package/dist/types-C9Y71dua.d.cts +845 -0
  74. package/dist/types-d8q8iQlk.d.cts +323 -0
  75. package/dist/types-sObbY4mX.d.ts +323 -0
  76. package/dist/webhook.cjs +461 -0
  77. package/dist/webhook.cjs.map +1 -0
  78. package/dist/webhook.d.cts +497 -0
  79. package/dist/webhook.d.ts +497 -0
  80. package/dist/webhook.js +422 -0
  81. package/dist/webhook.js.map +1 -0
  82. package/dist/workflow.cjs +2 -2614
  83. package/dist/workflow.cjs.map +1 -1
  84. package/dist/workflow.d.cts +12 -3023
  85. package/dist/workflow.d.ts +12 -3023
  86. package/dist/workflow.js +2 -2535
  87. package/dist/workflow.js.map +1 -1
  88. package/package.json +46 -1
@@ -0,0 +1,323 @@
1
+ import { A as AsyncResult } from './index-ICmfQi08.cjs';
2
+
3
+ /**
4
+ * awaitly/streaming - Types
5
+ *
6
+ * Core types for Result-aware streaming in workflows.
7
+ * All stream operations return Result types, enabling typed error handling
8
+ * throughout the streaming pipeline.
9
+ */
10
+
11
+ /** Discriminant for stream write errors */
12
+ declare const STREAM_WRITE_ERROR: "STREAM_WRITE_ERROR";
13
+ /** Discriminant for stream read errors */
14
+ declare const STREAM_READ_ERROR: "STREAM_READ_ERROR";
15
+ /** Discriminant for stream close errors */
16
+ declare const STREAM_CLOSE_ERROR: "STREAM_CLOSE_ERROR";
17
+ /** Discriminant for stream store errors */
18
+ declare const STREAM_STORE_ERROR: "STREAM_STORE_ERROR";
19
+ /** Discriminant for stream ended marker */
20
+ declare const STREAM_ENDED: "STREAM_ENDED";
21
+ /** Discriminant for stream backpressure errors */
22
+ declare const STREAM_BACKPRESSURE_ERROR: "STREAM_BACKPRESSURE_ERROR";
23
+ /**
24
+ * Error returned when a write operation fails.
25
+ */
26
+ type StreamWriteError = {
27
+ type: typeof STREAM_WRITE_ERROR;
28
+ reason: "closed" | "aborted" | "store_error";
29
+ message: string;
30
+ cause?: unknown;
31
+ };
32
+ /**
33
+ * Error returned when a read operation fails.
34
+ */
35
+ type StreamReadError = {
36
+ type: typeof STREAM_READ_ERROR;
37
+ reason: "closed" | "store_error";
38
+ message: string;
39
+ cause?: unknown;
40
+ };
41
+ /**
42
+ * Error returned when closing a stream fails.
43
+ */
44
+ type StreamCloseError = {
45
+ type: typeof STREAM_CLOSE_ERROR;
46
+ reason: "already_closed" | "store_error";
47
+ message: string;
48
+ cause?: unknown;
49
+ };
50
+ /**
51
+ * Error returned from StreamStore operations.
52
+ */
53
+ type StreamStoreError = {
54
+ type: typeof STREAM_STORE_ERROR;
55
+ reason: "read_error" | "write_error" | "metadata_error" | "close_error";
56
+ message: string;
57
+ cause?: unknown;
58
+ };
59
+ /**
60
+ * Marker indicating stream has ended (not an error, but a terminal state).
61
+ * Used as the "error" type when stream is exhausted.
62
+ */
63
+ type StreamEndedMarker = {
64
+ type: typeof STREAM_ENDED;
65
+ finalPosition: number;
66
+ };
67
+ /**
68
+ * Backpressure error when writer is paused.
69
+ */
70
+ type StreamBackpressureError = {
71
+ type: typeof STREAM_BACKPRESSURE_ERROR;
72
+ bufferedCount: number;
73
+ highWaterMark: number;
74
+ };
75
+ /**
76
+ * Union of all stream errors.
77
+ */
78
+ type StreamError = StreamWriteError | StreamReadError | StreamCloseError | StreamStoreError | StreamBackpressureError;
79
+ /**
80
+ * A single item in the stream with metadata.
81
+ */
82
+ interface StreamItem<T> {
83
+ /** The value stored in this stream item */
84
+ value: T;
85
+ /** Position in the stream (0-indexed) */
86
+ position: number;
87
+ /** Timestamp when item was written */
88
+ ts: number;
89
+ }
90
+ /**
91
+ * Metadata about a stream.
92
+ */
93
+ interface StreamMetadata {
94
+ /** Unique identifier for the stream (workflowId + namespace) */
95
+ id: string;
96
+ /** Namespace within the workflow */
97
+ namespace: string;
98
+ /** Workflow ID that owns this stream */
99
+ workflowId: string;
100
+ /** Number of items in the stream */
101
+ length: number;
102
+ /** Whether the stream has been closed */
103
+ closed: boolean;
104
+ /** Timestamp when stream was created */
105
+ createdAt: number;
106
+ /** Timestamp when stream was last written to */
107
+ lastWriteAt?: number;
108
+ /** Timestamp when stream was closed */
109
+ closedAt?: number;
110
+ }
111
+ /**
112
+ * Options for creating a writable stream.
113
+ */
114
+ interface StreamOptions {
115
+ /** Named streams (default: 'default') */
116
+ namespace?: string;
117
+ /** Backpressure threshold (default: 16) */
118
+ highWaterMark?: number;
119
+ }
120
+ /**
121
+ * Options for creating a readable stream.
122
+ */
123
+ interface StreamReadOptions {
124
+ /** Named streams (default: 'default') */
125
+ namespace?: string;
126
+ /** Resume from position (0-indexed) */
127
+ startIndex?: number;
128
+ }
129
+ /**
130
+ * Options for streamForEach operation.
131
+ */
132
+ interface StreamForEachOptions {
133
+ /** Name for the operation (used in events) */
134
+ name?: string;
135
+ /** Checkpoint after every N items (default: 1 = checkpoint each item) */
136
+ checkpointInterval?: number;
137
+ /** Maximum concurrent processors (default: 1 = sequential) */
138
+ concurrency?: number;
139
+ }
140
+ /**
141
+ * Result from streamForEach operation.
142
+ */
143
+ interface StreamForEachResult<R> {
144
+ /** Results from each processed item */
145
+ results: R[];
146
+ /** Total items processed */
147
+ processedCount: number;
148
+ /** Position of last processed item */
149
+ lastPosition: number;
150
+ }
151
+ /**
152
+ * Writable stream interface - never throws, returns Results.
153
+ *
154
+ * Use within a step to write values to a stream that can be consumed
155
+ * by readers (e.g., HTTP response streaming, AI token streaming).
156
+ *
157
+ * @template T - Type of values written to the stream
158
+ *
159
+ * @example
160
+ * ```typescript
161
+ * const writer = step.getWritable<string>({ namespace: 'ai-response' });
162
+ *
163
+ * await step(() => generateAI({
164
+ * prompt: 'Hello',
165
+ * onToken: async (token) => { await writer.write(token); }
166
+ * }), { key: 'generate' });
167
+ *
168
+ * await writer.close();
169
+ * ```
170
+ */
171
+ interface StreamWriter<T> {
172
+ /**
173
+ * Write a value to the stream.
174
+ * Returns an error if the stream is closed, aborted, or store fails.
175
+ */
176
+ write(value: T): AsyncResult<void, StreamWriteError>;
177
+ /**
178
+ * Close the stream normally.
179
+ * Signals to readers that no more data will be written.
180
+ */
181
+ close(): AsyncResult<void, StreamCloseError>;
182
+ /**
183
+ * Abort the stream with a reason.
184
+ * Use for error conditions that should terminate the stream.
185
+ */
186
+ abort(reason: unknown): void;
187
+ /** Whether the stream is still writable */
188
+ readonly writable: boolean;
189
+ /** Current write position (number of items written) */
190
+ readonly position: number;
191
+ /** Stream namespace */
192
+ readonly namespace: string;
193
+ }
194
+ /**
195
+ * Readable stream interface - returns STREAM_ENDED marker when complete.
196
+ *
197
+ * Use to consume values from a stream, with support for resuming from
198
+ * a specific position.
199
+ *
200
+ * @template T - Type of values read from the stream
201
+ *
202
+ * @example
203
+ * ```typescript
204
+ * const reader = getStreamReader<string>(runId, { namespace: 'ai-response' });
205
+ *
206
+ * let result = await reader.read();
207
+ * while (result.ok) {
208
+ * response.write(result.value);
209
+ * result = await reader.read();
210
+ * }
211
+ *
212
+ * if (result.error.type === 'STREAM_ENDED') {
213
+ * console.log('Stream complete at position', result.error.finalPosition);
214
+ * }
215
+ * ```
216
+ */
217
+ interface StreamReader<T> {
218
+ /**
219
+ * Read the next value from the stream.
220
+ * Returns StreamEndedMarker when stream is exhausted.
221
+ */
222
+ read(): AsyncResult<T, StreamReadError | StreamEndedMarker>;
223
+ /**
224
+ * Close the reader (stop consuming).
225
+ * Does not affect the underlying stream.
226
+ */
227
+ close(): void;
228
+ /** Whether there may be more data to read */
229
+ readonly readable: boolean;
230
+ /** Current read position */
231
+ readonly position: number;
232
+ /** Stream namespace */
233
+ readonly namespace: string;
234
+ }
235
+ /** Unsubscribe function returned by subscribe */
236
+ type Unsubscribe = () => void;
237
+ /**
238
+ * Storage backend for stream data.
239
+ * Follows the same patterns as persistence.ts adapters.
240
+ *
241
+ * @example In-memory store
242
+ * ```typescript
243
+ * const store = createMemoryStreamStore();
244
+ * ```
245
+ *
246
+ * @example File-based store
247
+ * ```typescript
248
+ * const store = createFileStreamStore({ directory: './streams', fs });
249
+ * ```
250
+ */
251
+ interface StreamStore {
252
+ /**
253
+ * Append an item to the stream.
254
+ */
255
+ append<T>(workflowId: string, namespace: string, item: StreamItem<T>): AsyncResult<void, StreamStoreError>;
256
+ /**
257
+ * Read items from the stream starting at an index.
258
+ * @param startIndex - Position to start reading from (0-indexed)
259
+ * @param limit - Maximum number of items to read (default: all remaining)
260
+ */
261
+ read<T>(workflowId: string, namespace: string, startIndex: number, limit?: number): AsyncResult<StreamItem<T>[], StreamStoreError>;
262
+ /**
263
+ * Get metadata about a stream.
264
+ * Returns undefined if stream doesn't exist.
265
+ */
266
+ getMetadata(workflowId: string, namespace: string): AsyncResult<StreamMetadata | undefined, StreamStoreError>;
267
+ /**
268
+ * Mark stream as closed.
269
+ */
270
+ closeStream(workflowId: string, namespace: string): AsyncResult<void, StreamStoreError>;
271
+ /**
272
+ * Subscribe to new items in a stream.
273
+ * Callback is invoked for each new item written.
274
+ * Returns unsubscribe function.
275
+ */
276
+ subscribe<T>(workflowId: string, namespace: string, callback: (item: StreamItem<T>) => void): Unsubscribe;
277
+ }
278
+ /**
279
+ * Check if an error is a StreamEndedMarker.
280
+ */
281
+ declare function isStreamEnded(error: unknown): error is StreamEndedMarker;
282
+ /**
283
+ * Check if an error is a StreamWriteError.
284
+ */
285
+ declare function isStreamWriteError(error: unknown): error is StreamWriteError;
286
+ /**
287
+ * Check if an error is a StreamReadError.
288
+ */
289
+ declare function isStreamReadError(error: unknown): error is StreamReadError;
290
+ /**
291
+ * Check if an error is a StreamStoreError.
292
+ */
293
+ declare function isStreamStoreError(error: unknown): error is StreamStoreError;
294
+ /**
295
+ * Check if an error is a StreamBackpressureError.
296
+ */
297
+ declare function isStreamBackpressureError(error: unknown): error is StreamBackpressureError;
298
+ /**
299
+ * Create a StreamWriteError.
300
+ */
301
+ declare function streamWriteError(reason: StreamWriteError["reason"], message: string, cause?: unknown): StreamWriteError;
302
+ /**
303
+ * Create a StreamReadError.
304
+ */
305
+ declare function streamReadError(reason: StreamReadError["reason"], message: string, cause?: unknown): StreamReadError;
306
+ /**
307
+ * Create a StreamCloseError.
308
+ */
309
+ declare function streamCloseError(reason: StreamCloseError["reason"], message: string, cause?: unknown): StreamCloseError;
310
+ /**
311
+ * Create a StreamStoreError.
312
+ */
313
+ declare function streamStoreError(reason: StreamStoreError["reason"], message: string, cause?: unknown): StreamStoreError;
314
+ /**
315
+ * Create a StreamEndedMarker.
316
+ */
317
+ declare function streamEnded(finalPosition: number): StreamEndedMarker;
318
+ /**
319
+ * Create a StreamBackpressureError.
320
+ */
321
+ declare function streamBackpressureError(bufferedCount: number, highWaterMark: number): StreamBackpressureError;
322
+
323
+ export { streamBackpressureError as A, streamCloseError as B, streamEnded as C, streamReadError as D, streamStoreError as E, streamWriteError as F, type StreamStore as S, type Unsubscribe as U, type StreamReader as a, STREAM_BACKPRESSURE_ERROR as b, STREAM_CLOSE_ERROR as c, STREAM_ENDED as d, STREAM_READ_ERROR as e, STREAM_STORE_ERROR as f, STREAM_WRITE_ERROR as g, type StreamBackpressureError as h, type StreamCloseError as i, type StreamEndedMarker as j, type StreamError as k, type StreamForEachOptions as l, type StreamForEachResult as m, type StreamItem as n, type StreamMetadata as o, type StreamOptions as p, type StreamReadError as q, type StreamReadOptions as r, type StreamStoreError as s, type StreamWriteError as t, type StreamWriter as u, isStreamBackpressureError as v, isStreamEnded as w, isStreamReadError as x, isStreamStoreError as y, isStreamWriteError as z };
@@ -0,0 +1,323 @@
1
+ import { A as AsyncResult } from './index-Cv4K_3sZ.js';
2
+
3
+ /**
4
+ * awaitly/streaming - Types
5
+ *
6
+ * Core types for Result-aware streaming in workflows.
7
+ * All stream operations return Result types, enabling typed error handling
8
+ * throughout the streaming pipeline.
9
+ */
10
+
11
+ /** Discriminant for stream write errors */
12
+ declare const STREAM_WRITE_ERROR: "STREAM_WRITE_ERROR";
13
+ /** Discriminant for stream read errors */
14
+ declare const STREAM_READ_ERROR: "STREAM_READ_ERROR";
15
+ /** Discriminant for stream close errors */
16
+ declare const STREAM_CLOSE_ERROR: "STREAM_CLOSE_ERROR";
17
+ /** Discriminant for stream store errors */
18
+ declare const STREAM_STORE_ERROR: "STREAM_STORE_ERROR";
19
+ /** Discriminant for stream ended marker */
20
+ declare const STREAM_ENDED: "STREAM_ENDED";
21
+ /** Discriminant for stream backpressure errors */
22
+ declare const STREAM_BACKPRESSURE_ERROR: "STREAM_BACKPRESSURE_ERROR";
23
+ /**
24
+ * Error returned when a write operation fails.
25
+ */
26
+ type StreamWriteError = {
27
+ type: typeof STREAM_WRITE_ERROR;
28
+ reason: "closed" | "aborted" | "store_error";
29
+ message: string;
30
+ cause?: unknown;
31
+ };
32
+ /**
33
+ * Error returned when a read operation fails.
34
+ */
35
+ type StreamReadError = {
36
+ type: typeof STREAM_READ_ERROR;
37
+ reason: "closed" | "store_error";
38
+ message: string;
39
+ cause?: unknown;
40
+ };
41
+ /**
42
+ * Error returned when closing a stream fails.
43
+ */
44
+ type StreamCloseError = {
45
+ type: typeof STREAM_CLOSE_ERROR;
46
+ reason: "already_closed" | "store_error";
47
+ message: string;
48
+ cause?: unknown;
49
+ };
50
+ /**
51
+ * Error returned from StreamStore operations.
52
+ */
53
+ type StreamStoreError = {
54
+ type: typeof STREAM_STORE_ERROR;
55
+ reason: "read_error" | "write_error" | "metadata_error" | "close_error";
56
+ message: string;
57
+ cause?: unknown;
58
+ };
59
+ /**
60
+ * Marker indicating stream has ended (not an error, but a terminal state).
61
+ * Used as the "error" type when stream is exhausted.
62
+ */
63
+ type StreamEndedMarker = {
64
+ type: typeof STREAM_ENDED;
65
+ finalPosition: number;
66
+ };
67
+ /**
68
+ * Backpressure error when writer is paused.
69
+ */
70
+ type StreamBackpressureError = {
71
+ type: typeof STREAM_BACKPRESSURE_ERROR;
72
+ bufferedCount: number;
73
+ highWaterMark: number;
74
+ };
75
+ /**
76
+ * Union of all stream errors.
77
+ */
78
+ type StreamError = StreamWriteError | StreamReadError | StreamCloseError | StreamStoreError | StreamBackpressureError;
79
+ /**
80
+ * A single item in the stream with metadata.
81
+ */
82
+ interface StreamItem<T> {
83
+ /** The value stored in this stream item */
84
+ value: T;
85
+ /** Position in the stream (0-indexed) */
86
+ position: number;
87
+ /** Timestamp when item was written */
88
+ ts: number;
89
+ }
90
+ /**
91
+ * Metadata about a stream.
92
+ */
93
+ interface StreamMetadata {
94
+ /** Unique identifier for the stream (workflowId + namespace) */
95
+ id: string;
96
+ /** Namespace within the workflow */
97
+ namespace: string;
98
+ /** Workflow ID that owns this stream */
99
+ workflowId: string;
100
+ /** Number of items in the stream */
101
+ length: number;
102
+ /** Whether the stream has been closed */
103
+ closed: boolean;
104
+ /** Timestamp when stream was created */
105
+ createdAt: number;
106
+ /** Timestamp when stream was last written to */
107
+ lastWriteAt?: number;
108
+ /** Timestamp when stream was closed */
109
+ closedAt?: number;
110
+ }
111
+ /**
112
+ * Options for creating a writable stream.
113
+ */
114
+ interface StreamOptions {
115
+ /** Named streams (default: 'default') */
116
+ namespace?: string;
117
+ /** Backpressure threshold (default: 16) */
118
+ highWaterMark?: number;
119
+ }
120
+ /**
121
+ * Options for creating a readable stream.
122
+ */
123
+ interface StreamReadOptions {
124
+ /** Named streams (default: 'default') */
125
+ namespace?: string;
126
+ /** Resume from position (0-indexed) */
127
+ startIndex?: number;
128
+ }
129
+ /**
130
+ * Options for streamForEach operation.
131
+ */
132
+ interface StreamForEachOptions {
133
+ /** Name for the operation (used in events) */
134
+ name?: string;
135
+ /** Checkpoint after every N items (default: 1 = checkpoint each item) */
136
+ checkpointInterval?: number;
137
+ /** Maximum concurrent processors (default: 1 = sequential) */
138
+ concurrency?: number;
139
+ }
140
+ /**
141
+ * Result from streamForEach operation.
142
+ */
143
+ interface StreamForEachResult<R> {
144
+ /** Results from each processed item */
145
+ results: R[];
146
+ /** Total items processed */
147
+ processedCount: number;
148
+ /** Position of last processed item */
149
+ lastPosition: number;
150
+ }
151
+ /**
152
+ * Writable stream interface - never throws, returns Results.
153
+ *
154
+ * Use within a step to write values to a stream that can be consumed
155
+ * by readers (e.g., HTTP response streaming, AI token streaming).
156
+ *
157
+ * @template T - Type of values written to the stream
158
+ *
159
+ * @example
160
+ * ```typescript
161
+ * const writer = step.getWritable<string>({ namespace: 'ai-response' });
162
+ *
163
+ * await step(() => generateAI({
164
+ * prompt: 'Hello',
165
+ * onToken: async (token) => { await writer.write(token); }
166
+ * }), { key: 'generate' });
167
+ *
168
+ * await writer.close();
169
+ * ```
170
+ */
171
+ interface StreamWriter<T> {
172
+ /**
173
+ * Write a value to the stream.
174
+ * Returns an error if the stream is closed, aborted, or store fails.
175
+ */
176
+ write(value: T): AsyncResult<void, StreamWriteError>;
177
+ /**
178
+ * Close the stream normally.
179
+ * Signals to readers that no more data will be written.
180
+ */
181
+ close(): AsyncResult<void, StreamCloseError>;
182
+ /**
183
+ * Abort the stream with a reason.
184
+ * Use for error conditions that should terminate the stream.
185
+ */
186
+ abort(reason: unknown): void;
187
+ /** Whether the stream is still writable */
188
+ readonly writable: boolean;
189
+ /** Current write position (number of items written) */
190
+ readonly position: number;
191
+ /** Stream namespace */
192
+ readonly namespace: string;
193
+ }
194
+ /**
195
+ * Readable stream interface - returns STREAM_ENDED marker when complete.
196
+ *
197
+ * Use to consume values from a stream, with support for resuming from
198
+ * a specific position.
199
+ *
200
+ * @template T - Type of values read from the stream
201
+ *
202
+ * @example
203
+ * ```typescript
204
+ * const reader = getStreamReader<string>(runId, { namespace: 'ai-response' });
205
+ *
206
+ * let result = await reader.read();
207
+ * while (result.ok) {
208
+ * response.write(result.value);
209
+ * result = await reader.read();
210
+ * }
211
+ *
212
+ * if (result.error.type === 'STREAM_ENDED') {
213
+ * console.log('Stream complete at position', result.error.finalPosition);
214
+ * }
215
+ * ```
216
+ */
217
+ interface StreamReader<T> {
218
+ /**
219
+ * Read the next value from the stream.
220
+ * Returns StreamEndedMarker when stream is exhausted.
221
+ */
222
+ read(): AsyncResult<T, StreamReadError | StreamEndedMarker>;
223
+ /**
224
+ * Close the reader (stop consuming).
225
+ * Does not affect the underlying stream.
226
+ */
227
+ close(): void;
228
+ /** Whether there may be more data to read */
229
+ readonly readable: boolean;
230
+ /** Current read position */
231
+ readonly position: number;
232
+ /** Stream namespace */
233
+ readonly namespace: string;
234
+ }
235
+ /** Unsubscribe function returned by subscribe */
236
+ type Unsubscribe = () => void;
237
+ /**
238
+ * Storage backend for stream data.
239
+ * Follows the same patterns as persistence.ts adapters.
240
+ *
241
+ * @example In-memory store
242
+ * ```typescript
243
+ * const store = createMemoryStreamStore();
244
+ * ```
245
+ *
246
+ * @example File-based store
247
+ * ```typescript
248
+ * const store = createFileStreamStore({ directory: './streams', fs });
249
+ * ```
250
+ */
251
+ interface StreamStore {
252
+ /**
253
+ * Append an item to the stream.
254
+ */
255
+ append<T>(workflowId: string, namespace: string, item: StreamItem<T>): AsyncResult<void, StreamStoreError>;
256
+ /**
257
+ * Read items from the stream starting at an index.
258
+ * @param startIndex - Position to start reading from (0-indexed)
259
+ * @param limit - Maximum number of items to read (default: all remaining)
260
+ */
261
+ read<T>(workflowId: string, namespace: string, startIndex: number, limit?: number): AsyncResult<StreamItem<T>[], StreamStoreError>;
262
+ /**
263
+ * Get metadata about a stream.
264
+ * Returns undefined if stream doesn't exist.
265
+ */
266
+ getMetadata(workflowId: string, namespace: string): AsyncResult<StreamMetadata | undefined, StreamStoreError>;
267
+ /**
268
+ * Mark stream as closed.
269
+ */
270
+ closeStream(workflowId: string, namespace: string): AsyncResult<void, StreamStoreError>;
271
+ /**
272
+ * Subscribe to new items in a stream.
273
+ * Callback is invoked for each new item written.
274
+ * Returns unsubscribe function.
275
+ */
276
+ subscribe<T>(workflowId: string, namespace: string, callback: (item: StreamItem<T>) => void): Unsubscribe;
277
+ }
278
+ /**
279
+ * Check if an error is a StreamEndedMarker.
280
+ */
281
+ declare function isStreamEnded(error: unknown): error is StreamEndedMarker;
282
+ /**
283
+ * Check if an error is a StreamWriteError.
284
+ */
285
+ declare function isStreamWriteError(error: unknown): error is StreamWriteError;
286
+ /**
287
+ * Check if an error is a StreamReadError.
288
+ */
289
+ declare function isStreamReadError(error: unknown): error is StreamReadError;
290
+ /**
291
+ * Check if an error is a StreamStoreError.
292
+ */
293
+ declare function isStreamStoreError(error: unknown): error is StreamStoreError;
294
+ /**
295
+ * Check if an error is a StreamBackpressureError.
296
+ */
297
+ declare function isStreamBackpressureError(error: unknown): error is StreamBackpressureError;
298
+ /**
299
+ * Create a StreamWriteError.
300
+ */
301
+ declare function streamWriteError(reason: StreamWriteError["reason"], message: string, cause?: unknown): StreamWriteError;
302
+ /**
303
+ * Create a StreamReadError.
304
+ */
305
+ declare function streamReadError(reason: StreamReadError["reason"], message: string, cause?: unknown): StreamReadError;
306
+ /**
307
+ * Create a StreamCloseError.
308
+ */
309
+ declare function streamCloseError(reason: StreamCloseError["reason"], message: string, cause?: unknown): StreamCloseError;
310
+ /**
311
+ * Create a StreamStoreError.
312
+ */
313
+ declare function streamStoreError(reason: StreamStoreError["reason"], message: string, cause?: unknown): StreamStoreError;
314
+ /**
315
+ * Create a StreamEndedMarker.
316
+ */
317
+ declare function streamEnded(finalPosition: number): StreamEndedMarker;
318
+ /**
319
+ * Create a StreamBackpressureError.
320
+ */
321
+ declare function streamBackpressureError(bufferedCount: number, highWaterMark: number): StreamBackpressureError;
322
+
323
+ export { streamBackpressureError as A, streamCloseError as B, streamEnded as C, streamReadError as D, streamStoreError as E, streamWriteError as F, type StreamStore as S, type Unsubscribe as U, type StreamReader as a, STREAM_BACKPRESSURE_ERROR as b, STREAM_CLOSE_ERROR as c, STREAM_ENDED as d, STREAM_READ_ERROR as e, STREAM_STORE_ERROR as f, STREAM_WRITE_ERROR as g, type StreamBackpressureError as h, type StreamCloseError as i, type StreamEndedMarker as j, type StreamError as k, type StreamForEachOptions as l, type StreamForEachResult as m, type StreamItem as n, type StreamMetadata as o, type StreamOptions as p, type StreamReadError as q, type StreamReadOptions as r, type StreamStoreError as s, type StreamWriteError as t, type StreamWriter as u, isStreamBackpressureError as v, isStreamEnded as w, isStreamReadError as x, isStreamStoreError as y, isStreamWriteError as z };