@tagma/sdk 0.1.7 → 0.1.9

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,113 +1,126 @@
1
- // ═══ PipelineRunner ═══
2
- //
3
- // Wraps runPipeline in a lifecycle object suited for multi-pipeline management
4
- // in sidecar / Tauri IPC scenarios. Each instance controls one pipeline run.
5
- //
6
- // Typical sidecar usage:
7
- //
8
- // const runners = new Map<string, PipelineRunner>();
9
- //
10
- // const runner = new PipelineRunner(config, workDir);
11
- // runner.subscribe(event => ipcEmit('pipeline_event', event));
12
- // runner.start();
13
- // runners.set(runner.instanceId, runner);
14
- //
15
- // // Later, from IPC:
16
- // runners.get(id)?.abort();
17
-
18
- import { runPipeline } from './engine';
19
- import type { EngineResult, PipelineEvent, RunPipelineOptions } from './engine';
20
- import type { PipelineConfig, TaskState } from './types';
21
- import { generateRunId } from './utils';
22
-
23
- export type { PipelineEvent, EngineResult };
24
-
25
- export type PipelineRunnerStatus = 'idle' | 'running' | 'done' | 'aborted';
26
-
27
- export class PipelineRunner {
28
- /**
29
- * Stable ID assigned before start() — safe to use as a Map key in the sidecar
30
- * before the engine-assigned runId becomes available.
31
- */
32
- readonly instanceId: string;
33
-
34
- /**
35
- * The runId generated by the engine. Available after the first 'pipeline_start'
36
- * event fires (i.e. effectively immediately after start() is called).
37
- * null until then.
38
- */
39
- private _runId: string | null = null;
40
- private _status: PipelineRunnerStatus = 'idle';
41
- private _result: Promise<EngineResult> | null = null;
42
- private _abortController = new AbortController();
43
- private _handlers = new Set<(event: PipelineEvent) => void>();
44
- private _states: ReadonlyMap<string, TaskState> | null = null;
45
-
46
- constructor(
47
- private readonly config: PipelineConfig,
48
- private readonly workDir: string,
49
- private readonly opts: Omit<RunPipelineOptions, 'signal' | 'onEvent'> = {},
50
- ) {
51
- this.instanceId = generateRunId();
52
- }
53
-
54
- get runId(): string | null { return this._runId; }
55
- get status(): PipelineRunnerStatus { return this._status; }
56
-
57
- /**
58
- * Start the pipeline. Calling start() more than once returns the same Promise.
59
- */
60
- start(): Promise<EngineResult> {
61
- if (this._result) return this._result;
62
-
63
- this._status = 'running';
64
- this._result = runPipeline(this.config, this.workDir, {
65
- ...this.opts,
66
- signal: this._abortController.signal,
67
- onEvent: (event) => {
68
- if (event.type === 'pipeline_start') {
69
- this._runId = event.runId;
70
- }
71
- if (event.type === 'pipeline_end') {
72
- this._status = this._abortController.signal.aborted ? 'aborted' : 'done';
73
- }
74
- for (const h of this._handlers) h(event);
75
- },
76
- }).then(result => {
77
- this._states = result.states;
78
- if (this._status === 'running') this._status = 'done';
79
- return result;
80
- }).catch(err => {
81
- this._status = 'aborted';
82
- throw err;
83
- });
84
-
85
- return this._result;
86
- }
87
-
88
- /**
89
- * Cancel the running pipeline. Safe to call multiple times or before start().
90
- */
91
- abort(reason?: string): void {
92
- this._status = 'aborted';
93
- this._abortController.abort(reason);
94
- }
95
-
96
- /**
97
- * Snapshot of task states. Populated after the run completes.
98
- * During a run, listen to subscribe() events for incremental updates.
99
- */
100
- getStates(): ReadonlyMap<string, TaskState> | null {
101
- return this._states;
102
- }
103
-
104
- /**
105
- * Subscribe to pipeline/task events. Returns an unsubscribe function.
106
- * Events are emitted synchronously in the engine's event loop, so keep
107
- * handlers non-blocking (e.g. queue to IPC, do not await inside).
108
- */
109
- subscribe(handler: (event: PipelineEvent) => void): () => void {
110
- this._handlers.add(handler);
111
- return () => this._handlers.delete(handler);
112
- }
113
- }
1
+ // ═══ PipelineRunner ═══
2
+ //
3
+ // Wraps runPipeline in a lifecycle object suited for multi-pipeline management
4
+ // in sidecar / Tauri IPC scenarios. Each instance controls one pipeline run.
5
+ //
6
+ // Typical sidecar usage:
7
+ //
8
+ // const runners = new Map<string, PipelineRunner>();
9
+ //
10
+ // const runner = new PipelineRunner(config, workDir);
11
+ // runner.subscribe(event => ipcEmit('pipeline_event', event));
12
+ // runner.start();
13
+ // runners.set(runner.instanceId, runner);
14
+ //
15
+ // // Later, from IPC:
16
+ // runners.get(id)?.abort();
17
+
18
+ import { runPipeline } from './engine';
19
+ import type { EngineResult, PipelineEvent, RunPipelineOptions } from './engine';
20
+ import type { PipelineConfig, TaskState } from './types';
21
+ import { generateRunId } from './utils';
22
+
23
+ export type { PipelineEvent, EngineResult };
24
+
25
+ export type PipelineRunnerStatus = 'idle' | 'running' | 'done' | 'aborted';
26
+
27
+ export class PipelineRunner {
28
+ /**
29
+ * Stable ID assigned before start() — safe to use as a Map key in the sidecar
30
+ * before the engine-assigned runId becomes available.
31
+ */
32
+ readonly instanceId: string;
33
+
34
+ /**
35
+ * The runId generated by the engine. Available after the first 'pipeline_start'
36
+ * event fires (i.e. effectively immediately after start() is called).
37
+ * null until then.
38
+ */
39
+ private _runId: string | null = null;
40
+ private _status: PipelineRunnerStatus = 'idle';
41
+ private _result: Promise<EngineResult> | null = null;
42
+ private _abortController = new AbortController();
43
+ private _handlers = new Set<(event: PipelineEvent) => void>();
44
+ private _states: ReadonlyMap<string, TaskState> | null = null;
45
+ private _statesMirror = new Map<string, TaskState>();
46
+
47
+ constructor(
48
+ private readonly config: PipelineConfig,
49
+ private readonly workDir: string,
50
+ private readonly opts: Omit<RunPipelineOptions, 'signal' | 'onEvent'> = {},
51
+ ) {
52
+ this.instanceId = generateRunId();
53
+ }
54
+
55
+ get runId(): string | null { return this._runId; }
56
+ get status(): PipelineRunnerStatus { return this._status; }
57
+
58
+ /**
59
+ * Start the pipeline. Calling start() more than once returns the same Promise.
60
+ */
61
+ start(): Promise<EngineResult> {
62
+ if (this._result) return this._result;
63
+
64
+ this._status = 'running';
65
+ this._result = runPipeline(this.config, this.workDir, {
66
+ ...this.opts,
67
+ signal: this._abortController.signal,
68
+ onEvent: (event) => {
69
+ if (event.type === 'pipeline_start') {
70
+ this._runId = event.runId;
71
+ // Initialize the live mirror with the full initial state snapshot
72
+ for (const [id, state] of event.states) {
73
+ this._statesMirror.set(id, { ...state });
74
+ }
75
+ }
76
+ if (event.type === 'task_status_change') {
77
+ // Keep the mirror up to date so getStates() works during the run
78
+ this._statesMirror.set(event.taskId, event.state);
79
+ }
80
+ if (event.type === 'pipeline_end') {
81
+ this._status = this._abortController.signal.aborted ? 'aborted' : 'done';
82
+ }
83
+ for (const h of this._handlers) h(event);
84
+ },
85
+ }).then(result => {
86
+ this._states = result.states;
87
+ if (this._status === 'running') this._status = 'done';
88
+ return result;
89
+ }).catch(err => {
90
+ this._status = 'aborted';
91
+ throw err;
92
+ });
93
+
94
+ return this._result;
95
+ }
96
+
97
+ /**
98
+ * Cancel the running pipeline. Safe to call multiple times or before start().
99
+ */
100
+ abort(reason?: string): void {
101
+ this._status = 'aborted';
102
+ this._abortController.abort(reason);
103
+ }
104
+
105
+ /**
106
+ * Live snapshot of task states. Available from the first pipeline_start event onward
107
+ * (i.e. as soon as start() is called) and remains accessible after the run completes.
108
+ * Returns null only if the pipeline has never started.
109
+ */
110
+ getStates(): ReadonlyMap<string, TaskState> | null {
111
+ if (this._states) return this._states;
112
+ // Return a snapshot copy so callers cannot mutate SDK-internal state.
113
+ if (this._statesMirror.size > 0) return new Map([...this._statesMirror]);
114
+ return null;
115
+ }
116
+
117
+ /**
118
+ * Subscribe to pipeline/task events. Returns an unsubscribe function.
119
+ * Events are emitted synchronously in the engine's event loop, so keep
120
+ * handlers non-blocking (e.g. queue to IPC, do not await inside).
121
+ */
122
+ subscribe(handler: (event: PipelineEvent) => void): () => void {
123
+ this._handlers.add(handler);
124
+ return () => this._handlers.delete(handler);
125
+ }
126
+ }