@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.
- package/README.md +23 -5
- package/package.json +1 -1
- package/src/adapters/stdin-approval.ts +117 -117
- package/src/adapters/websocket-approval.ts +175 -144
- package/src/approval.ts +4 -1
- package/src/completions/exit-code.ts +19 -19
- package/src/completions/file-exists.ts +39 -39
- package/src/completions/output-check.ts +57 -57
- package/src/config-ops.ts +239 -183
- package/src/dag.ts +222 -137
- package/src/drivers/claude-code.ts +207 -207
- package/src/engine.ts +743 -698
- package/src/hooks.ts +147 -138
- package/src/logger.ts +112 -107
- package/src/middlewares/static-context.ts +29 -29
- package/src/pipeline-runner.ts +126 -113
- package/src/runner.ts +213 -195
- package/src/schema.ts +386 -358
- package/src/sdk.ts +2 -2
- package/src/triggers/file.ts +105 -94
- package/src/triggers/manual.ts +61 -61
- package/src/utils.ts +154 -147
- package/src/validate-raw.ts +223 -203
package/src/pipeline-runner.ts
CHANGED
|
@@ -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
|
-
|
|
47
|
-
|
|
48
|
-
private readonly
|
|
49
|
-
private readonly
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
get
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
this.
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
*
|
|
99
|
-
*/
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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
|
+
}
|