@computekit/core 0.1.2 → 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.
@@ -0,0 +1,363 @@
1
+ /**
2
+ * ComputeKit Core Types
3
+ * Type definitions for the WASM + Worker toolkit
4
+ */
5
+ /** Configuration options for ComputeKit */
6
+ interface ComputeKitOptions {
7
+ /** Maximum number of workers in the pool (default: navigator.hardwareConcurrency || 4) */
8
+ maxWorkers?: number;
9
+ /** Timeout for compute operations in milliseconds (default: 30000) */
10
+ timeout?: number;
11
+ /** Enable debug logging (default: false) */
12
+ debug?: boolean;
13
+ /** Custom path to worker script */
14
+ workerPath?: string;
15
+ /** Whether to use SharedArrayBuffer when available (default: true) */
16
+ useSharedMemory?: boolean;
17
+ /** Remote scripts to load in workers via importScripts */
18
+ remoteDependencies?: string[];
19
+ }
20
+ /** Options for individual compute operations */
21
+ interface ComputeOptions {
22
+ /** Timeout for this specific operation (overrides global) */
23
+ timeout?: number;
24
+ /** Transfer these ArrayBuffers to the worker (improves performance) */
25
+ transfer?: ArrayBuffer[];
26
+ /** Priority level for scheduling (0-10, higher = more priority) */
27
+ priority?: number;
28
+ /** Abort signal to cancel the operation */
29
+ signal?: AbortSignal;
30
+ /** Progress callback for long-running operations */
31
+ onProgress?: (progress: ComputeProgress) => void;
32
+ }
33
+ /** Progress information for compute operations */
34
+ interface ComputeProgress {
35
+ /** Progress percentage (0-100) */
36
+ percent: number;
37
+ /** Current step/phase name */
38
+ phase?: string;
39
+ /** Estimated time remaining in milliseconds */
40
+ estimatedTimeRemaining?: number;
41
+ /** Any additional data from the compute function */
42
+ data?: unknown;
43
+ }
44
+ /** Result wrapper with metadata */
45
+ interface ComputeResult<T> {
46
+ /** The computed result */
47
+ data: T;
48
+ /** Time taken in milliseconds */
49
+ duration: number;
50
+ /** Whether the result came from cache */
51
+ cached: boolean;
52
+ /** Worker ID that processed this */
53
+ workerId: string;
54
+ /** Size of input data in bytes (available in debug mode) */
55
+ inputSize?: number;
56
+ /** Size of output data in bytes (available in debug mode) */
57
+ outputSize?: number;
58
+ }
59
+ /** Function definition for registration */
60
+ interface ComputeFunction<TInput = unknown, TOutput = unknown> {
61
+ /** The compute function implementation */
62
+ fn: (input: TInput) => TOutput | Promise<TOutput>;
63
+ /** Optional WASM module to load */
64
+ wasmModule?: WebAssembly.Module | ArrayBuffer | string;
65
+ /** Whether this function supports progress reporting */
66
+ supportsProgress?: boolean;
67
+ }
68
+ /** WASM module configuration */
69
+ interface WasmModuleConfig {
70
+ /** Path to the WASM file or base64 encoded WASM */
71
+ source: string | ArrayBuffer;
72
+ /** Imports to provide to the WASM module */
73
+ imports?: WebAssembly.Imports;
74
+ /** Memory configuration */
75
+ memory?: {
76
+ initial: number;
77
+ maximum?: number;
78
+ shared?: boolean;
79
+ };
80
+ }
81
+ /** Worker state */
82
+ type WorkerState = 'idle' | 'busy' | 'error' | 'terminated';
83
+ /** Worker info */
84
+ interface WorkerInfo {
85
+ id: string;
86
+ state: WorkerState;
87
+ currentTask?: string;
88
+ tasksCompleted: number;
89
+ errors: number;
90
+ createdAt: number;
91
+ lastActiveAt: number;
92
+ }
93
+ /** Pool statistics */
94
+ interface PoolStats {
95
+ workers: WorkerInfo[];
96
+ totalWorkers: number;
97
+ activeWorkers: number;
98
+ idleWorkers: number;
99
+ queueLength: number;
100
+ tasksCompleted: number;
101
+ tasksFailed: number;
102
+ averageTaskDuration: number;
103
+ }
104
+ /** Event data for worker:created */
105
+ interface WorkerCreatedEvent {
106
+ info: WorkerInfo;
107
+ }
108
+ /** Event data for worker:terminated */
109
+ interface WorkerTerminatedEvent {
110
+ info: WorkerInfo;
111
+ }
112
+ /** Event data for worker:error */
113
+ interface WorkerErrorEvent {
114
+ error: Error;
115
+ info: WorkerInfo;
116
+ }
117
+ /** Event data for task:start */
118
+ interface TaskStartEvent {
119
+ taskId: string;
120
+ functionName: string;
121
+ }
122
+ /** Event data for task:complete */
123
+ interface TaskCompleteEvent {
124
+ taskId: string;
125
+ duration: number;
126
+ }
127
+ /** Event data for task:error */
128
+ interface TaskErrorEvent {
129
+ taskId: string;
130
+ error: Error;
131
+ }
132
+ /** Event data for task:progress */
133
+ interface TaskProgressEvent {
134
+ taskId: string;
135
+ progress: ComputeProgress;
136
+ }
137
+ /** Events emitted by ComputeKit */
138
+ type ComputeKitEvents = {
139
+ 'worker:created': WorkerCreatedEvent;
140
+ 'worker:terminated': WorkerTerminatedEvent;
141
+ 'worker:error': WorkerErrorEvent;
142
+ 'task:start': TaskStartEvent;
143
+ 'task:complete': TaskCompleteEvent;
144
+ 'task:error': TaskErrorEvent;
145
+ 'task:progress': TaskProgressEvent;
146
+ [key: string]: unknown;
147
+ };
148
+ /** Status of a pipeline stage */
149
+ type StageStatus = 'pending' | 'running' | 'completed' | 'failed' | 'skipped';
150
+ /** Detailed information about a single pipeline stage */
151
+ interface StageInfo<TInput = unknown, TOutput = unknown> {
152
+ /** Unique identifier for the stage */
153
+ id: string;
154
+ /** Display name for the stage */
155
+ name: string;
156
+ /** Name of the registered compute function to execute */
157
+ functionName: string;
158
+ /** Current status of this stage */
159
+ status: StageStatus;
160
+ /** Input data for this stage (set when stage starts) */
161
+ input?: TInput;
162
+ /** Output data from this stage (set when stage completes) */
163
+ output?: TOutput;
164
+ /** Error if stage failed */
165
+ error?: Error;
166
+ /** Start timestamp (ms since epoch) */
167
+ startedAt?: number;
168
+ /** End timestamp (ms since epoch) */
169
+ completedAt?: number;
170
+ /** Duration in milliseconds */
171
+ duration?: number;
172
+ /** Progress within this stage (0-100) */
173
+ progress?: number;
174
+ /** Number of retry attempts */
175
+ retryCount: number;
176
+ /** Compute options specific to this stage */
177
+ options?: ComputeOptions;
178
+ }
179
+ /** Pipeline execution mode */
180
+ type PipelineMode = 'sequential' | 'parallel';
181
+ /** Configuration for a pipeline stage */
182
+ interface StageConfig<TInput = unknown, TOutput = unknown> {
183
+ /** Unique identifier for the stage */
184
+ id: string;
185
+ /** Display name for the stage */
186
+ name: string;
187
+ /** Name of the registered compute function */
188
+ functionName: string;
189
+ /** Transform input before passing to compute function */
190
+ transformInput?: (input: TInput, previousResults: unknown[]) => unknown;
191
+ /** Transform output after compute function returns */
192
+ transformOutput?: (output: unknown) => TOutput;
193
+ /** Whether to skip this stage based on previous results */
194
+ shouldSkip?: (input: TInput, previousResults: unknown[]) => boolean;
195
+ /** Maximum retry attempts on failure (default: 0) */
196
+ maxRetries?: number;
197
+ /** Delay between retries in ms (default: 1000) */
198
+ retryDelay?: number;
199
+ /** Compute options for this stage */
200
+ options?: ComputeOptions;
201
+ }
202
+ /** Overall pipeline status */
203
+ type PipelineStatus = 'idle' | 'running' | 'paused' | 'completed' | 'failed' | 'cancelled';
204
+ /** Comprehensive pipeline state for debugging */
205
+ interface PipelineState<TInput = unknown, TOutput = unknown> {
206
+ /** Overall pipeline status */
207
+ status: PipelineStatus;
208
+ /** All stage information */
209
+ stages: StageInfo[];
210
+ /** Index of currently executing stage (-1 if not running) */
211
+ currentStageIndex: number;
212
+ /** Current stage info (convenience) */
213
+ currentStage: StageInfo | null;
214
+ /** Overall progress percentage (0-100) */
215
+ progress: number;
216
+ /** Final output from the last stage */
217
+ output: TOutput | null;
218
+ /** Initial input that started the pipeline */
219
+ input: TInput | null;
220
+ /** Error that caused pipeline failure */
221
+ error: Error | null;
222
+ /** Pipeline start timestamp */
223
+ startedAt: number | null;
224
+ /** Pipeline completion timestamp */
225
+ completedAt: number | null;
226
+ /** Total duration in milliseconds */
227
+ totalDuration: number | null;
228
+ /** Results from each completed stage */
229
+ stageResults: unknown[];
230
+ /** Execution metrics for debugging */
231
+ metrics: PipelineMetrics;
232
+ }
233
+ /** Metrics for pipeline debugging and reporting */
234
+ interface PipelineMetrics {
235
+ /** Total stages in pipeline */
236
+ totalStages: number;
237
+ /** Number of completed stages */
238
+ completedStages: number;
239
+ /** Number of failed stages */
240
+ failedStages: number;
241
+ /** Number of skipped stages */
242
+ skippedStages: number;
243
+ /** Total retry attempts across all stages */
244
+ totalRetries: number;
245
+ /** Slowest stage info */
246
+ slowestStage: {
247
+ id: string;
248
+ name: string;
249
+ duration: number;
250
+ } | null;
251
+ /** Fastest stage info */
252
+ fastestStage: {
253
+ id: string;
254
+ name: string;
255
+ duration: number;
256
+ } | null;
257
+ /** Average stage duration */
258
+ averageStageDuration: number;
259
+ /** Timestamp of each stage transition for timeline view */
260
+ timeline: Array<{
261
+ stageId: string;
262
+ stageName: string;
263
+ event: 'started' | 'completed' | 'failed' | 'skipped' | 'retry';
264
+ timestamp: number;
265
+ duration?: number;
266
+ error?: string;
267
+ }>;
268
+ }
269
+ /** Pipeline configuration options */
270
+ interface PipelineOptions {
271
+ /** Execution mode (default: 'sequential') */
272
+ mode?: PipelineMode;
273
+ /** Stop pipeline on first stage failure (default: true) */
274
+ stopOnError?: boolean;
275
+ /** Global timeout for entire pipeline in ms */
276
+ timeout?: number;
277
+ /** Enable detailed timeline tracking (default: true) */
278
+ trackTimeline?: boolean;
279
+ /** Called when pipeline state changes */
280
+ onStateChange?: (state: PipelineState) => void;
281
+ /** Called when a stage starts */
282
+ onStageStart?: (stage: StageInfo) => void;
283
+ /** Called when a stage completes */
284
+ onStageComplete?: (stage: StageInfo) => void;
285
+ /** Called when a stage fails */
286
+ onStageError?: (stage: StageInfo, error: Error) => void;
287
+ /** Called when a stage is retried */
288
+ onStageRetry?: (stage: StageInfo, attempt: number) => void;
289
+ }
290
+ /** Events emitted by Pipeline */
291
+ interface PipelineEvents {
292
+ 'pipeline:start': {
293
+ input: unknown;
294
+ };
295
+ 'pipeline:complete': {
296
+ output: unknown;
297
+ duration: number;
298
+ };
299
+ 'pipeline:error': {
300
+ error: Error;
301
+ stageId: string;
302
+ };
303
+ 'pipeline:cancel': {
304
+ stageId: string;
305
+ };
306
+ 'stage:start': StageInfo;
307
+ 'stage:progress': {
308
+ stageId: string;
309
+ progress: number;
310
+ };
311
+ 'stage:complete': StageInfo;
312
+ 'stage:error': {
313
+ stage: StageInfo;
314
+ error: Error;
315
+ };
316
+ 'stage:skip': StageInfo;
317
+ 'stage:retry': {
318
+ stage: StageInfo;
319
+ attempt: number;
320
+ };
321
+ }
322
+ /** Configuration for parallel batch processing */
323
+ interface ParallelBatchConfig<TItem = unknown> {
324
+ /** Items to process in parallel */
325
+ items: TItem[];
326
+ /** Name of the registered compute function */
327
+ functionName: string;
328
+ /** Maximum concurrent executions (default: all) */
329
+ concurrency?: number;
330
+ /** Compute options for batch items */
331
+ options?: ComputeOptions;
332
+ }
333
+ /** Result of a single item in parallel batch */
334
+ interface BatchItemResult<TOutput = unknown> {
335
+ /** Index of the item in original array */
336
+ index: number;
337
+ /** Whether this item succeeded */
338
+ success: boolean;
339
+ /** Result if successful */
340
+ data?: TOutput;
341
+ /** Error if failed */
342
+ error?: Error;
343
+ /** Duration in ms */
344
+ duration: number;
345
+ }
346
+ /** Aggregate result of parallel batch processing */
347
+ interface ParallelBatchResult<TOutput = unknown> {
348
+ /** All individual results */
349
+ results: BatchItemResult<TOutput>[];
350
+ /** Successfully processed items */
351
+ successful: TOutput[];
352
+ /** Failed items with their errors */
353
+ failed: Array<{
354
+ index: number;
355
+ error: Error;
356
+ }>;
357
+ /** Total duration */
358
+ totalDuration: number;
359
+ /** Success rate (0-1) */
360
+ successRate: number;
361
+ }
362
+
363
+ export type { BatchItemResult as B, ComputeKitOptions as C, PoolStats as P, StageStatus as S, WasmModuleConfig as W, ComputeOptions as a, ComputeKitEvents as b, ComputeResult as c, ComputeProgress as d, ComputeFunction as e, WorkerInfo as f, StageInfo as g, StageConfig as h, PipelineMode as i, PipelineStatus as j, PipelineState as k, PipelineMetrics as l, PipelineOptions as m, PipelineEvents as n, ParallelBatchConfig as o, ParallelBatchResult as p };