@tagma/sdk 0.6.0 → 0.6.1

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,173 +1,173 @@
1
- // ═══ PipelineRunner ═══
2
- //
3
- // Wraps runPipeline in a lifecycle object suited for multi-pipeline
4
- // management in sidecar / Tauri IPC scenarios. Each instance controls
5
- // one pipeline run.
6
- //
7
- // The runner forwards wire-shape `RunEventPayload` values to its
8
- // subscribers — identical to what the editor server broadcasts over SSE —
9
- // so sidecar hosts don't need to know anything about the engine's
10
- // internal TaskState.
11
- //
12
- // Typical sidecar usage:
13
- //
14
- // const runners = new Map<string, PipelineRunner>();
15
- //
16
- // const runner = new PipelineRunner(config, workDir);
17
- // runner.subscribe(event => ipcEmit('run_event', event));
18
- // runner.start();
19
- // runners.set(runner.instanceId, runner);
20
- //
21
- // // Later, from IPC:
22
- // runners.get(id)?.abort();
23
-
24
- import { runPipeline } from './engine';
25
- import type { EngineResult, RunPipelineOptions } from './engine';
26
- import type { PipelineConfig, RunEventPayload, RunTaskState } from './types';
27
- import { generateRunId } from './utils';
28
-
29
- export type { EngineResult };
30
-
31
- export type PipelineRunnerStatus = 'idle' | 'running' | 'done' | 'aborted';
32
-
33
- export class PipelineRunner {
34
- /**
35
- * Stable ID assigned before start() — safe to use as a Map key before
36
- * the engine-assigned runId becomes available.
37
- */
38
- readonly instanceId: string;
39
-
40
- /**
41
- * The runId generated by the engine. Set when the first `run_start`
42
- * event arrives on the forwarded event stream. null until then.
43
- */
44
- private _runId: string | null = null;
45
- private _status: PipelineRunnerStatus = 'idle';
46
- private _result: Promise<EngineResult> | null = null;
47
- private _abortController = new AbortController();
48
- private _handlers = new Set<(event: RunEventPayload) => void>();
49
- /**
50
- * Wire-shape task mirror, kept in sync with `run_start` / `task_update`
51
- * events. Exposed through `getTasks()`. Hosts see the same wire
52
- * projection the editor client sees, so there is exactly one task-state
53
- * vocabulary across IPC boundaries.
54
- */
55
- private _tasks = new Map<string, RunTaskState>();
56
-
57
- constructor(
58
- private readonly config: PipelineConfig,
59
- private readonly workDir: string,
60
- private readonly opts: Omit<RunPipelineOptions, 'signal' | 'onEvent'> = {},
61
- ) {
62
- this.instanceId = generateRunId();
63
- }
64
-
65
- get runId(): string | null {
66
- return this._runId;
67
- }
68
- get status(): PipelineRunnerStatus {
69
- return this._status;
70
- }
71
-
72
- /**
73
- * Start the pipeline. Calling start() more than once returns the same Promise.
74
- */
75
- start(): Promise<EngineResult> {
76
- if (this._result) return this._result;
77
-
78
- // Guard: if abort() was called before start(), the signal is already
79
- // aborted. Create a fresh controller so the pipeline doesn't terminate
80
- // immediately. If users truly want pre-abort semantics, they call
81
- // abort() after start().
82
- if (this._abortController.signal.aborted) {
83
- this._abortController = new AbortController();
84
- this._status = 'idle';
85
- }
86
-
87
- this._status = 'running';
88
- this._result = runPipeline(this.config, this.workDir, {
89
- ...this.opts,
90
- signal: this._abortController.signal,
91
- onEvent: (event) => {
92
- this._applyEvent(event);
93
- for (const h of this._handlers) h(event);
94
- },
95
- })
96
- .then((result) => {
97
- if (this._status === 'running') this._status = 'done';
98
- return result;
99
- })
100
- .catch((err) => {
101
- this._status = 'aborted';
102
- throw err;
103
- });
104
-
105
- return this._result;
106
- }
107
-
108
- private _applyEvent(event: RunEventPayload): void {
109
- switch (event.type) {
110
- case 'run_start':
111
- this._runId = event.runId;
112
- this._tasks.clear();
113
- for (const t of event.tasks) this._tasks.set(t.taskId, { ...t });
114
- return;
115
- case 'task_update': {
116
- const prev = this._tasks.get(event.taskId);
117
- if (!prev) return;
118
- const pick = <T>(incoming: T | undefined, previous: T): T =>
119
- incoming !== undefined ? incoming : previous;
120
- this._tasks.set(event.taskId, {
121
- ...prev,
122
- status: event.status,
123
- startedAt: pick(event.startedAt, prev.startedAt),
124
- finishedAt: pick(event.finishedAt, prev.finishedAt),
125
- durationMs: pick(event.durationMs, prev.durationMs),
126
- exitCode: pick(event.exitCode, prev.exitCode),
127
- stdout: pick(event.stdout, prev.stdout),
128
- stderr: pick(event.stderr, prev.stderr),
129
- stderrPath: pick(event.stderrPath, prev.stderrPath),
130
- sessionId: pick(event.sessionId, prev.sessionId),
131
- normalizedOutput: pick(event.normalizedOutput, prev.normalizedOutput),
132
- resolvedDriver: pick(event.resolvedDriver, prev.resolvedDriver),
133
- resolvedModel: pick(event.resolvedModel, prev.resolvedModel),
134
- resolvedPermissions: pick(event.resolvedPermissions, prev.resolvedPermissions),
135
- });
136
- return;
137
- }
138
- case 'run_end':
139
- this._status = this._abortController.signal.aborted ? 'aborted' : 'done';
140
- return;
141
- default:
142
- return;
143
- }
144
- }
145
-
146
- /**
147
- * Cancel the running pipeline. Safe to call multiple times or before start().
148
- */
149
- abort(reason?: string): void {
150
- this._status = 'aborted';
151
- this._abortController.abort(reason);
152
- }
153
-
154
- /**
155
- * Live snapshot of wire-shape task states. Populated from the first
156
- * `run_start` event onward. Returns an empty map before the run starts.
157
- */
158
- getTasks(): ReadonlyMap<string, RunTaskState> {
159
- const copy = new Map<string, RunTaskState>();
160
- for (const [id, t] of this._tasks) copy.set(id, { ...t });
161
- return copy;
162
- }
163
-
164
- /**
165
- * Subscribe to run events. Returns an unsubscribe function. Events are
166
- * emitted synchronously in the engine's event loop, so keep handlers
167
- * non-blocking (e.g. queue to IPC, do not await inside).
168
- */
169
- subscribe(handler: (event: RunEventPayload) => void): () => void {
170
- this._handlers.add(handler);
171
- return () => this._handlers.delete(handler);
172
- }
173
- }
1
+ // ═══ PipelineRunner ═══
2
+ //
3
+ // Wraps runPipeline in a lifecycle object suited for multi-pipeline
4
+ // management in sidecar / Tauri IPC scenarios. Each instance controls
5
+ // one pipeline run.
6
+ //
7
+ // The runner forwards wire-shape `RunEventPayload` values to its
8
+ // subscribers — identical to what the editor server broadcasts over SSE —
9
+ // so sidecar hosts don't need to know anything about the engine's
10
+ // internal TaskState.
11
+ //
12
+ // Typical sidecar usage:
13
+ //
14
+ // const runners = new Map<string, PipelineRunner>();
15
+ //
16
+ // const runner = new PipelineRunner(config, workDir);
17
+ // runner.subscribe(event => ipcEmit('run_event', event));
18
+ // runner.start();
19
+ // runners.set(runner.instanceId, runner);
20
+ //
21
+ // // Later, from IPC:
22
+ // runners.get(id)?.abort();
23
+
24
+ import { runPipeline } from './engine';
25
+ import type { EngineResult, RunPipelineOptions } from './engine';
26
+ import type { PipelineConfig, RunEventPayload, RunTaskState } from './types';
27
+ import { generateRunId } from './utils';
28
+
29
+ export type { EngineResult };
30
+
31
+ export type PipelineRunnerStatus = 'idle' | 'running' | 'done' | 'aborted';
32
+
33
+ export class PipelineRunner {
34
+ /**
35
+ * Stable ID assigned before start() — safe to use as a Map key before
36
+ * the engine-assigned runId becomes available.
37
+ */
38
+ readonly instanceId: string;
39
+
40
+ /**
41
+ * The runId generated by the engine. Set when the first `run_start`
42
+ * event arrives on the forwarded event stream. null until then.
43
+ */
44
+ private _runId: string | null = null;
45
+ private _status: PipelineRunnerStatus = 'idle';
46
+ private _result: Promise<EngineResult> | null = null;
47
+ private _abortController = new AbortController();
48
+ private _handlers = new Set<(event: RunEventPayload) => void>();
49
+ /**
50
+ * Wire-shape task mirror, kept in sync with `run_start` / `task_update`
51
+ * events. Exposed through `getTasks()`. Hosts see the same wire
52
+ * projection the editor client sees, so there is exactly one task-state
53
+ * vocabulary across IPC boundaries.
54
+ */
55
+ private _tasks = new Map<string, RunTaskState>();
56
+
57
+ constructor(
58
+ private readonly config: PipelineConfig,
59
+ private readonly workDir: string,
60
+ private readonly opts: Omit<RunPipelineOptions, 'signal' | 'onEvent'> = {},
61
+ ) {
62
+ this.instanceId = generateRunId();
63
+ }
64
+
65
+ get runId(): string | null {
66
+ return this._runId;
67
+ }
68
+ get status(): PipelineRunnerStatus {
69
+ return this._status;
70
+ }
71
+
72
+ /**
73
+ * Start the pipeline. Calling start() more than once returns the same Promise.
74
+ */
75
+ start(): Promise<EngineResult> {
76
+ if (this._result) return this._result;
77
+
78
+ // Guard: if abort() was called before start(), the signal is already
79
+ // aborted. Create a fresh controller so the pipeline doesn't terminate
80
+ // immediately. If users truly want pre-abort semantics, they call
81
+ // abort() after start().
82
+ if (this._abortController.signal.aborted) {
83
+ this._abortController = new AbortController();
84
+ this._status = 'idle';
85
+ }
86
+
87
+ this._status = 'running';
88
+ this._result = runPipeline(this.config, this.workDir, {
89
+ ...this.opts,
90
+ signal: this._abortController.signal,
91
+ onEvent: (event) => {
92
+ this._applyEvent(event);
93
+ for (const h of this._handlers) h(event);
94
+ },
95
+ })
96
+ .then((result) => {
97
+ if (this._status === 'running') this._status = 'done';
98
+ return result;
99
+ })
100
+ .catch((err) => {
101
+ this._status = 'aborted';
102
+ throw err;
103
+ });
104
+
105
+ return this._result;
106
+ }
107
+
108
+ private _applyEvent(event: RunEventPayload): void {
109
+ switch (event.type) {
110
+ case 'run_start':
111
+ this._runId = event.runId;
112
+ this._tasks.clear();
113
+ for (const t of event.tasks) this._tasks.set(t.taskId, { ...t });
114
+ return;
115
+ case 'task_update': {
116
+ const prev = this._tasks.get(event.taskId);
117
+ if (!prev) return;
118
+ const pick = <T>(incoming: T | undefined, previous: T): T =>
119
+ incoming !== undefined ? incoming : previous;
120
+ this._tasks.set(event.taskId, {
121
+ ...prev,
122
+ status: event.status,
123
+ startedAt: pick(event.startedAt, prev.startedAt),
124
+ finishedAt: pick(event.finishedAt, prev.finishedAt),
125
+ durationMs: pick(event.durationMs, prev.durationMs),
126
+ exitCode: pick(event.exitCode, prev.exitCode),
127
+ stdout: pick(event.stdout, prev.stdout),
128
+ stderr: pick(event.stderr, prev.stderr),
129
+ stderrPath: pick(event.stderrPath, prev.stderrPath),
130
+ sessionId: pick(event.sessionId, prev.sessionId),
131
+ normalizedOutput: pick(event.normalizedOutput, prev.normalizedOutput),
132
+ resolvedDriver: pick(event.resolvedDriver, prev.resolvedDriver),
133
+ resolvedModel: pick(event.resolvedModel, prev.resolvedModel),
134
+ resolvedPermissions: pick(event.resolvedPermissions, prev.resolvedPermissions),
135
+ });
136
+ return;
137
+ }
138
+ case 'run_end':
139
+ this._status = this._abortController.signal.aborted ? 'aborted' : 'done';
140
+ return;
141
+ default:
142
+ return;
143
+ }
144
+ }
145
+
146
+ /**
147
+ * Cancel the running pipeline. Safe to call multiple times or before start().
148
+ */
149
+ abort(reason?: string): void {
150
+ this._status = 'aborted';
151
+ this._abortController.abort(reason);
152
+ }
153
+
154
+ /**
155
+ * Live snapshot of wire-shape task states. Populated from the first
156
+ * `run_start` event onward. Returns an empty map before the run starts.
157
+ */
158
+ getTasks(): ReadonlyMap<string, RunTaskState> {
159
+ const copy = new Map<string, RunTaskState>();
160
+ for (const [id, t] of this._tasks) copy.set(id, { ...t });
161
+ return copy;
162
+ }
163
+
164
+ /**
165
+ * Subscribe to run events. Returns an unsubscribe function. Events are
166
+ * emitted synchronously in the engine's event loop, so keep handlers
167
+ * non-blocking (e.g. queue to IPC, do not await inside).
168
+ */
169
+ subscribe(handler: (event: RunEventPayload) => void): () => void {
170
+ this._handlers.add(handler);
171
+ return () => this._handlers.delete(handler);
172
+ }
173
+ }
package/src/prompt-doc.ts CHANGED
@@ -1,49 +1,49 @@
1
- import type { PromptDocument, PromptContextBlock } from './types';
2
-
3
- /**
4
- * Build a fresh `PromptDocument` from a raw task string.
5
- * Middlewares receive this from the engine and push context blocks onto
6
- * `contexts`. `task` is the user's original prompt and should not be
7
- * rewritten by middlewares (translation middlewares are the rare exception).
8
- */
9
- export function promptDocumentFromString(task: string): PromptDocument {
10
- return { contexts: [], task };
11
- }
12
-
13
- /**
14
- * Serialize a `PromptDocument` to the default string form consumed by
15
- * drivers that read `task.prompt` instead of `ctx.promptDoc`.
16
- *
17
- * Format:
18
- *
19
- * [<label1>]
20
- * <content1>
21
- *
22
- * [<label2>]
23
- * <content2>
24
- *
25
- * <task>
26
- *
27
- * Each context block is separated from the next (and from `task`) by a
28
- * single blank line. No implicit `[Task]` header is emitted — that framing
29
- * is the driver's responsibility (e.g. opencode's `agent_profile` wrapping).
30
- * Emitting one here would compose incorrectly with any driver that also
31
- * adds a `[Task]` header, producing a double header that some models
32
- * (observed with `opencode/big-pickle`) misread as a cut-off message.
33
- */
34
- export function serializePromptDocument(doc: PromptDocument): string {
35
- if (doc.contexts.length === 0) return doc.task;
36
- const blocks = doc.contexts.map((c) => `[${c.label}]\n${c.content}`);
37
- return `${blocks.join('\n\n')}\n\n${doc.task}`;
38
- }
39
-
40
- /**
41
- * Helper for middlewares: return a new document with the given block
42
- * appended to `contexts`, preserving immutability of `doc`.
43
- */
44
- export function appendContext(
45
- doc: PromptDocument,
46
- block: PromptContextBlock,
47
- ): PromptDocument {
48
- return { contexts: [...doc.contexts, block], task: doc.task };
49
- }
1
+ import type { PromptDocument, PromptContextBlock } from './types';
2
+
3
+ /**
4
+ * Build a fresh `PromptDocument` from a raw task string.
5
+ * Middlewares receive this from the engine and push context blocks onto
6
+ * `contexts`. `task` is the user's original prompt and should not be
7
+ * rewritten by middlewares (translation middlewares are the rare exception).
8
+ */
9
+ export function promptDocumentFromString(task: string): PromptDocument {
10
+ return { contexts: [], task };
11
+ }
12
+
13
+ /**
14
+ * Serialize a `PromptDocument` to the default string form consumed by
15
+ * drivers that read `task.prompt` instead of `ctx.promptDoc`.
16
+ *
17
+ * Format:
18
+ *
19
+ * [<label1>]
20
+ * <content1>
21
+ *
22
+ * [<label2>]
23
+ * <content2>
24
+ *
25
+ * <task>
26
+ *
27
+ * Each context block is separated from the next (and from `task`) by a
28
+ * single blank line. No implicit `[Task]` header is emitted — that framing
29
+ * is the driver's responsibility (e.g. opencode's `agent_profile` wrapping).
30
+ * Emitting one here would compose incorrectly with any driver that also
31
+ * adds a `[Task]` header, producing a double header that some models
32
+ * (observed with `opencode/big-pickle`) misread as a cut-off message.
33
+ */
34
+ export function serializePromptDocument(doc: PromptDocument): string {
35
+ if (doc.contexts.length === 0) return doc.task;
36
+ const blocks = doc.contexts.map((c) => `[${c.label}]\n${c.content}`);
37
+ return `${blocks.join('\n\n')}\n\n${doc.task}`;
38
+ }
39
+
40
+ /**
41
+ * Helper for middlewares: return a new document with the given block
42
+ * appended to `contexts`, preserving immutability of `doc`.
43
+ */
44
+ export function appendContext(
45
+ doc: PromptDocument,
46
+ block: PromptContextBlock,
47
+ ): PromptDocument {
48
+ return { contexts: [...doc.contexts, block], task: doc.task };
49
+ }