@m4trix/core 0.5.0 → 0.6.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.
@@ -1,304 +0,0 @@
1
- /**
2
- * StreamChunk is the base interface for all streams.
3
- * It can hold voice and text chunks.
4
- *
5
- * @template T The type of data contained in the chunk
6
- */
7
- interface StreamChunk<T> {
8
- sequence: number;
9
- data: T;
10
- done: boolean;
11
- }
12
- /**
13
- * MessageStream represents an asynchronous iterable stream of chunks.
14
- * Used as the core stream representation throughout the Pump library.
15
- *
16
- * @template T The type of data contained in the chunks of the stream
17
- */
18
- type MessageStream<T> = AsyncIterable<StreamChunk<T>>;
19
- /**
20
- * Represents any source that can be converted to a stream.
21
- * This includes AsyncIterable and ReadableStream sources.
22
- *
23
- * @template T The type of data contained in the source
24
- */
25
- type Source<T> = AsyncIterable<T> | ReadableStream<T> | NodeJS.ReadableStream;
26
- /**
27
- * A transformer for stream data that also provides a response.
28
- * Used primarily to transform and consume stream data while producing a response object.
29
- *
30
- * @template T The type of data being transformed
31
- * @template R The type of the response (defaults to Response)
32
- */
33
- type StreamTransformer<T, R = Response> = {
34
- transform(data: T): T;
35
- close(): void;
36
- response: R;
37
- };
38
- /**
39
- * Pump is an asynchronous stream processing pipeline with fluent operators.
40
- * It provides a comprehensive set of operations for transforming, filtering, batching,
41
- * combining, and consuming stream data.
42
- *
43
- * The Pump class follows a builder pattern where each operation returns a new Pump instance,
44
- * allowing for chaining of operations to build complex stream processing pipelines.
45
- *
46
- * @template T The type of data contained in the stream
47
- */
48
- declare class Pump<T> {
49
- private readonly src;
50
- constructor(src: MessageStream<T>);
51
- /**
52
- * Wrap an existing AsyncIterable or Readable stream into a Pump
53
- *
54
- * @template U The type of data in the source stream
55
- * @param source The source stream to convert to a Pump (AsyncIterable, ReadableStream, or NodeJS.ReadableStream)
56
- * @returns A new Pump instance that wraps the source
57
- */
58
- static from<U>(source: Source<U>): Pump<U>;
59
- /**
60
- * Sync or async map over the data portion of each chunk
61
- *
62
- * @template U The output type after transformation
63
- * @param fn The mapping function that transforms each chunk
64
- * @returns A new Pump instance with the transformed data
65
- */
66
- map<U>(fn: (data: T) => U | Promise<U>): Pump<U>;
67
- /**
68
- * Stateful map allows processing stream chunks with a persistent context object.
69
- *
70
- * The context is initialized when the first chunk arrives and can be updated with each chunk.
71
- * This is useful for maintaining state across the stream processing.
72
- *
73
- * If you plan to use sockets you should rather opt for asyncStatefulMap.
74
- *
75
- * The pipe closes only after all processing is complete, including any final operations in onClose.
76
- *
77
- * TODO: Un-tested
78
- *
79
- * @param handlers Object containing callback functions for stream processing
80
- * @param handlers.onFirstChunk Function called when the first chunk arrives, initializes the context
81
- * @param handlers.onChunk Function called for each subsequent chunk, updates the context
82
- * @param handlers.onClose Optional function called when the stream closes, allows final processing
83
- * @returns A new Pump instance with transformed data
84
- */
85
- statefulMap<Context, U = T>(handlers: {
86
- onFirstChunk(chunk: T, yieldData: (data: U) => void): Context | Promise<Context>;
87
- onChunk(chunk: T, context: Context, yieldData: (data: U) => void): Context | Promise<Context>;
88
- onClose?(lastChunk: T | undefined, context: Context, yieldData: (data: U) => void): void | Promise<void>;
89
- }): Pump<U>;
90
- /**
91
- * Async map means that each incoming chunk is causing an async operation that when it completes
92
- * should yield a new chunk.
93
- * The pipe closes only after you unlock the pipe by using the unlockCloseEvent callback.
94
- *
95
- * Stateful refers to the fact that you can create your own small context object that is passed in the subsequent callbacks.
96
- * This allows you to keep track of things like a socket connection.
97
- *
98
- * Why is this nice? Well if you use things like a socket the pipe might have received the close event,
99
- * before you got any or all of your socket responses. Sockets don't fit into the standard promise pattern,
100
- * which makes it harder to wait for them.
101
- *
102
- * TODO: Un-tested
103
- *
104
- * @param handlers Object containing callback functions for stream processing
105
- * @param handlers.onFirstChunk Function called when the first chunk arrives, initializes the context
106
- * @param handlers.onChunk Function called for each subsequent chunk, updates the context
107
- * @param handlers.onClose Optional function called when the stream closes, allows final processing
108
- * @returns A new Pump instance with transformed data
109
- */
110
- asyncStatefulMap<Context, U = T>(handlers: {
111
- onFirstChunk(chunk: T, yieldData: (data: U) => void, unlockCloseEvent: () => void): Context | Promise<Context>;
112
- onChunk(chunk: T, context: Context, yieldData: (data: U) => void, unlockCloseEvent: () => void): Context | Promise<Context>;
113
- onClose?(lastChunk: T | undefined, context: Context, yieldData: (data: U) => void, unlockCloseEvent: () => void): void | Promise<void>;
114
- }): Pump<U>;
115
- /**
116
- * Filter items based on a predicate
117
- *
118
- * @param predicate A function that determines whether to keep each chunk
119
- * @returns A new Pump instance containing only chunks that passed the predicate
120
- */
121
- filter(predicate: (data: T) => boolean | Promise<boolean>): Pump<T>;
122
- /**
123
- * Bundles (accumulates) chunks together based on a condition rather than a fixed size.
124
- *
125
- * This is useful when you need to group chunks dynamically based on their content or other criteria.
126
- *
127
- * Example: Bundling text chunks with a maximum character limit
128
- *
129
- * Input chunks: ["Hello", " this", " is", " a few", " chunks", " of text"]
130
- * With max size of 10 characters:
131
- * - First bundle: ["Hello", " this"] (10 chars)
132
- * - Second bundle: [" is", " a few"] (8 chars)
133
- * - Third bundle: [" chunks", " of text"] (13 chars)
134
- *
135
- * @param closeBundleCondition - Function that determines when to close the current bundle
136
- * Returns true when the current bundle should be emitted
137
- * Parameters:
138
- * - chunk: The current chunk being processed
139
- * - accumulatedChunks: Array of chunks in the current bundle
140
- *
141
- * @returns A pump that emits arrays of bundled items
142
- */
143
- bundle(closeBundleCondition: (chunk: T, accumulatedChunks: Array<T>) => boolean | Promise<boolean>): Pump<Array<T>>;
144
- /**
145
- * Tap into each chunk without altering it
146
- *
147
- * @param fn A function that receives each chunk but doesn't affect the stream
148
- * @returns The same pump instance with unmodified data
149
- */
150
- onChunk(fn: (chunk: T) => void | Promise<void>): Pump<T>;
151
- /**
152
- * Collect all chunks in the stream and run a callback when the stream is done.
153
- * The callback receives an array of all chunks that passed through.
154
- *
155
- * This is useful for analytics, logging, or processing the complete stream history
156
- * after all chunks have been received.
157
- *
158
- * @param fn - Callback function that receives the array of all chunks when the stream is complete
159
- * @returns The same pump, for chaining
160
- */
161
- onClose(fn: (history: T[]) => void | Promise<void>): Pump<T>;
162
- /**
163
- * Batch `n` chunks into arrays before emitting
164
- *
165
- * @param n The number of chunks to batch together
166
- * @returns A new Pump instance that emits arrays of batched chunks
167
- */
168
- batch(n: number): Pump<Array<T>>;
169
- /**
170
- * If you want to prevent chunk starvation, you can buffer the chunks.
171
- * Chunks will not be bundled into arrays or object but kept as is,
172
- * but the pipeline will not progress at that segment until the buffer is filled up.
173
- * Once a buffer is filled up it will drain and never buffer again.
174
- *
175
- * @param n The number of chunks to buffer before processing continues
176
- * @returns A new Pump instance with buffering behavior
177
- */
178
- buffer(n: number): Pump<T>;
179
- /**
180
- * Rechunk the stream: transform one chunk into zero, one, or many output chunks.
181
- * The handler function receives the current buffer of chunks, a push function to emit new chunks,
182
- * and a flag indicating if this is the last chunk in the stream.
183
- *
184
- * @param handler Function that transforms chunks and pushes new ones
185
- * @returns A new Pump instance with rechunked data
186
- */
187
- rechunk(handler: (params: {
188
- buffer: T[];
189
- push: (chunk: T) => void;
190
- lastChunk: boolean;
191
- setBuffer: (buffer: T[]) => void;
192
- }) => void | Promise<void>): Pump<T>;
193
- /**
194
- * Emit sliding windows of the last `size` items with step `step`.
195
- * Each window is an array [current, previous1, ..., previous(size-1)].
196
- * Optionally, map each window through a function.
197
- *
198
- * | Step | Window | Resulting Window |
199
- * |------|--------|------------------|
200
- * | 1 | ▪︎▪︎[▪︎▫︎▫︎] | ▪︎▫︎▫︎ |
201
- * | 2 | ▪︎[▪︎▪︎▫︎] | ▪︎▪︎▫︎ |
202
- * | 3 | [▪︎▪︎▪︎] | ▪︎▪︎▪︎ |
203
- * | 4 | [▫︎▪︎▪︎] | ▫︎▪︎▪︎ |
204
- * | 5 | [▫︎▫︎▪︎] | ▫︎▫︎▪ |
205
- *
206
- * @param size The size of each window
207
- * @param step The number of items to move between windows
208
- * @returns A Pump that emits arrays representing sliding windows
209
- */
210
- slidingWindow(size: number, step: number): Pump<Array<T | undefined>>;
211
- /**
212
- * Emit sliding windows of the last `size` items with step `step`,
213
- * and map each window using the provided function.
214
- *
215
- * @template N The size type parameter (extends number)
216
- * @template U The output type after window transformation
217
- * @param size The size of each window
218
- * @param step The number of items to move between windows
219
- * @param fn A function to transform each window
220
- * @returns A Pump that emits transformed sliding windows
221
- */
222
- slidingWindow<N extends number, U>(size: N, step: number, fn: (window: Array<T | undefined>) => U | Promise<U>): Pump<U>;
223
- /**
224
- * Sequentially flatten inner stream sources emitted by the pipeline.
225
- * Works with any Source type (AsyncIterable or ReadableStream).
226
- * This method is only available when the current Pump contains Source elements.
227
- *
228
- * @template U The type of data in the inner streams
229
- * @template F The type of inner stream source (extends Source<U>)
230
- * @returns A Pump instance with flattened stream data
231
- */
232
- sequenceStreams<U, F extends Source<U>>(this: Pump<F>): Pump<U>;
233
- /**
234
- * Fork the stream: two independent Pump<T> consumers
235
- * Both resulting Pumps will receive the same data, allowing for divergent processing paths.
236
- *
237
- * @returns An array containing two independent Pump instances with the same source data
238
- */
239
- fork(): [Pump<T>, Pump<T>];
240
- /**
241
- * Drain the pipeline, consuming all chunks.
242
- * Returns a Promise that resolves when all chunks have been consumed.
243
- *
244
- * @returns A Promise that resolves when all chunks have been consumed
245
- */
246
- drain(): Promise<void>;
247
- /**
248
- * Drain the pipeline to a StreamTransformer.
249
- * Applies transform() to each data chunk, then closes the transformer,
250
- * and returns its response (which can be of any type defined by the transformer).
251
- *
252
- * Example with httpStreamResponse:
253
- * ```
254
- * const { transform, response, close } = httpStreamResponse(options);
255
- * return Pump.from(messageStream).drainTo({ transform, close, response });
256
- * ```
257
- *
258
- * @template U The type of data expected by the transformer (extends T)
259
- * @template R The response type produced by the transformer
260
- * @param transformer The StreamTransformer to drain to
261
- * @returns The response from the transformer
262
- */
263
- drainTo<U extends T, R>(transformer: StreamTransformer<U, R>): R;
264
- }
265
-
266
- interface HttpStreamOptions<T> {
267
- /** HTTP ResponseInit (status, headers, etc.) */
268
- init?: ResponseInit;
269
- /** Encode each chunk of type T into bytes or string */
270
- encoder?: (data: T) => Uint8Array | string;
271
- }
272
- /**
273
- * Create a streaming HTTP response transformer.
274
- * Returns an object with:
275
- * - transform: function to write each chunk into the response
276
- * - response: the Fetch API Response ready to return
277
- * - close: function to close the stream when done
278
- *
279
- * Usage in a Next.js route:
280
- * ```
281
- * // With the new drainTo API:
282
- * const transformer = httpStreamResponse(options);
283
- * return Pump.from(messageStream).drainTo(transformer);
284
- *
285
- * // Or with manual control:
286
- * const { transform, response, close } = httpStreamResponse(options);
287
- * await Pump.from(messageStream).map(transform).drain();
288
- * close();
289
- * return response;
290
- * ```
291
- */
292
- declare function httpStreamResponse<T>(options?: HttpStreamOptions<T>): StreamTransformer<T, Response>;
293
-
294
- /**
295
- * A helper to be used with Pump.rechunk that ensures full word chunks.
296
- * Aggregates incoming chunks and emits only when a full word boundary is reached.
297
- */
298
- declare function ensureFullWords({ buffer, push, lastChunk, }: {
299
- buffer: string[];
300
- push: (chunk: string) => void;
301
- lastChunk: boolean;
302
- }): Promise<void>;
303
-
304
- export { type HttpStreamOptions, type MessageStream, Pump, type Source, type StreamChunk, type StreamTransformer, ensureFullWords, httpStreamResponse };
@@ -1,30 +0,0 @@
1
- type Selector = string;
2
- type CursorTarget = HTMLElement | Selector | [number, number];
3
- /**
4
- * The AiCursor is a class that is used to create a cursor element and acts an interface to control the cursor.
5
- * It is used to move the cursor to a target, show or hide the cursor, and to schedule moves.
6
- *
7
- * @example
8
- * ```ts
9
- * const cursor = AiCursor.spawn();
10
- * cursor.moveTo('#target-element');
11
- * ```
12
- *
13
- * @author @Pascal-Lohscheidt
14
- */
15
- declare class AiCursor {
16
- private setPosition?;
17
- private addPositionToQueue?;
18
- private playQueue?;
19
- private setShowCursor?;
20
- constructor();
21
- static spawn(): AiCursor;
22
- jumpTo(target: CursorTarget): void;
23
- moveTo(target: CursorTarget): void;
24
- scheduleMoves(targets: CursorTarget[]): void;
25
- show(): void;
26
- hide(): void;
27
- private mount;
28
- }
29
-
30
- export { AiCursor };