@theokit/sdk 2.26.0 → 2.27.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.
@@ -11,7 +11,12 @@ export declare class WorkflowSuspendedSentinel extends Error {
11
11
  readonly name = "WorkflowSuspendedSentinel";
12
12
  constructor(payload?: unknown | undefined);
13
13
  }
14
- export declare function makeStepContext(runId: string, signal: AbortSignal): StepContext;
14
+ /** SE29 the per-run shared-state seam wired into `StepContext.state`/`setState`. */
15
+ export interface StateController {
16
+ getState: () => unknown;
17
+ setState: (next: unknown) => void;
18
+ }
19
+ export declare function makeStepContext(runId: string, signal: AbortSignal, state: StateController): StepContext;
15
20
  /**
16
21
  * Combine the caller-supplied signal with the flight signal so abort on
17
22
  * either side cancels the workflow run.
@@ -0,0 +1,13 @@
1
+ /**
2
+ * SE28 — a minimal pushable async iterator for `Workflow.stream()`. The executor
3
+ * pushes {@link WorkflowEvent}s as steps run; the consumer drains them via
4
+ * `for await`. Buffers events emitted before the consumer is ready; `end()`
5
+ * closes the iterator so a pending/next `next()` resolves `done: true`.
6
+ *
7
+ * @internal
8
+ */
9
+ export interface PushableEventStream extends AsyncIterableIterator<WorkflowEvent> {
10
+ push(event: WorkflowEvent): void;
11
+ end(): void;
12
+ }
13
+ export declare function createEventStream(): PushableEventStream;
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Pure helpers extracted from the workflow executor (G8 focus): run assembly,
3
+ * abort, whole-workflow schema validation (SE27), and the shared-state
4
+ * controller (SE29). No executor-internal dependencies — safe to import both ways.
5
+ *
6
+ * @internal
7
+ */
8
+ import type { StepResult, WorkflowOptions, WorkflowRun } from "../../types/workflow.js";
9
+ import type { StateController } from "./ctx.js";
10
+ export interface AssembleParams<TO> {
11
+ runId: string;
12
+ name: string;
13
+ status: WorkflowRun["status"];
14
+ stepResults: ReadonlyArray<StepResult>;
15
+ startedAt: number;
16
+ output?: TO;
17
+ error?: {
18
+ name: string;
19
+ message: string;
20
+ };
21
+ }
22
+ export declare function assembleRun<TO>(params: AssembleParams<TO>): WorkflowRun<TO>;
23
+ export declare function abortRun<T>(name: string, runId: string, startedAt: number, stepResults: StepResult[], signal: AbortSignal): WorkflowRun<T>;
24
+ /**
25
+ * SE27 — validate a value against a whole-workflow schema. Returns a compact
26
+ * issues string on failure, `undefined` when the schema is absent or matches.
27
+ */
28
+ export declare function validateWorkflowSchema(schema: ZodType | undefined, value: unknown): string | undefined;
29
+ /** SE29 — the per-run shared-state holder; `setState` validates against `stateSchema`. */
30
+ export declare function makeStateController(options: WorkflowOptions, initial: unknown): StateController;
@@ -101,6 +101,19 @@ export interface StepContext {
101
101
  };
102
102
  /** Pause the workflow; resume via `Workflow.resume({...})`. */
103
103
  readonly suspend: (payload?: unknown) => Promise<never>;
104
+ /**
105
+ * SE29 — the workflow's shared state (from `WorkflowOptions.initialState`,
106
+ * mutated by {@link setState}), visible to every subsequent step in the run.
107
+ * `undefined` when no `initialState`/`setState` has run. Persisted across
108
+ * suspend/resume.
109
+ */
110
+ readonly state: unknown;
111
+ /**
112
+ * SE29 — update the shared state for subsequent steps. Validated against
113
+ * `WorkflowOptions.stateSchema` when set (a mismatch throws
114
+ * {@link WorkflowStateError}, which fails the step/run — Rule 8).
115
+ */
116
+ readonly setState: (next: unknown) => void;
104
117
  }
105
118
  export interface StepResult {
106
119
  readonly stepId: string;
@@ -128,15 +141,57 @@ export interface WorkflowRun<TOutput = unknown> {
128
141
  readonly stepResults: ReadonlyArray<StepResult>;
129
142
  }
130
143
  export interface WorkflowSnapshot {
131
- readonly _schemaVersion: 1;
144
+ /** v1 = pre-SE29 (no `state`); v2 = SE29 (carries `state`). Resume reads both. */
145
+ readonly _schemaVersion: 1 | 2;
132
146
  readonly runId: string;
133
147
  readonly workflowName: string;
134
148
  readonly currentStepId: string;
135
149
  readonly suspendedPayload?: unknown;
136
150
  readonly stepResults: ReadonlyArray<StepResult>;
137
151
  readonly accumulatedInput: unknown;
152
+ /** SE29 — shared state captured at suspend (v2). Absent on a v1 snapshot. */
153
+ readonly state?: unknown;
138
154
  readonly suspendedAt: number;
139
155
  }
156
+ /**
157
+ * SE28 — a step-level workflow event emitted by `Workflow.stream()` as top-level
158
+ * steps run. Coarse-grained (one event per top-level step; nested
159
+ * parallel/branch/foreach emit as their single wrapping step), distinct from the
160
+ * token-delta agent stream. Discriminate on `type`.
161
+ *
162
+ * @public
163
+ */
164
+ export type WorkflowEvent = {
165
+ readonly type: "step_started";
166
+ readonly stepId: string;
167
+ } | {
168
+ readonly type: "step_completed";
169
+ readonly stepId: string;
170
+ readonly output: unknown;
171
+ } | {
172
+ readonly type: "step_failed";
173
+ readonly stepId: string;
174
+ readonly error: {
175
+ readonly name: string;
176
+ readonly message: string;
177
+ };
178
+ } | {
179
+ readonly type: "workflow_suspended";
180
+ readonly stepId: string;
181
+ } | {
182
+ readonly type: "workflow_completed";
183
+ };
184
+ /**
185
+ * SE28 — the async iterator returned by `Workflow.stream()`. Yields
186
+ * {@link WorkflowEvent}s in execution order; `result` resolves to the same
187
+ * terminal {@link WorkflowRun} the `run()` path returns (the authoritative
188
+ * outcome — the stream ends when the run terminates).
189
+ *
190
+ * @public
191
+ */
192
+ export type WorkflowStream<TOutput = unknown> = AsyncIterableIterator<WorkflowEvent> & {
193
+ readonly result: Promise<WorkflowRun<TOutput>>;
194
+ };
140
195
  export interface WorkflowPersistenceOptions {
141
196
  readonly backend: "memory" | "json";
142
197
  /** Required for `backend: "json"`. */
@@ -145,6 +200,34 @@ export interface WorkflowPersistenceOptions {
145
200
  export interface WorkflowOptions {
146
201
  readonly name: string;
147
202
  readonly persistence?: WorkflowPersistenceOptions;
203
+ /**
204
+ * SE27 — Zod schema for the WHOLE workflow's input. When set, `run(input)`
205
+ * validates `input` BEFORE step 1; a mismatch yields `status: "failed"` with a
206
+ * typed {@link WorkflowInputError} in `error` (fail-fast, no step runs, no
207
+ * silent coerce). Absent ⇒ no whole-workflow input validation (unchanged).
208
+ */
209
+ readonly inputSchema?: ZodType;
210
+ /**
211
+ * SE27 — Zod schema for the workflow's final output. When set, the terminal
212
+ * `completed` output is validated before `WorkflowRun.output` is populated; a
213
+ * mismatch yields `status: "failed"` with a typed {@link WorkflowOutputError}.
214
+ * Only validated on the `completed` path (suspended/failed runs skip it).
215
+ */
216
+ readonly outputSchema?: ZodType;
217
+ /**
218
+ * SE29 — Zod schema for the workflow's shared state (see `StepContext.state` /
219
+ * `setState`). When set, `initialState` and every `setState(next)` are
220
+ * validated against it (a mismatch throws {@link WorkflowStateError}). When
221
+ * `initialState` is absent, `state` starts as `undefined` and validation fires
222
+ * on the first `setState` call.
223
+ */
224
+ readonly stateSchema?: ZodType;
225
+ /**
226
+ * SE29 — the initial shared state, seeded onto `StepContext.state` before
227
+ * step 1. Validated against `stateSchema` when both are set. Persisted across
228
+ * suspend/resume.
229
+ */
230
+ readonly initialState?: unknown;
148
231
  /** Internal — minted at `.commit()`. Not user-facing. */
149
232
  readonly workflowId?: string;
150
233
  }
@@ -179,6 +262,56 @@ export declare class WorkflowDuplicateStepIdError extends Error {
179
262
  readonly name = "WorkflowDuplicateStepIdError";
180
263
  constructor(stepId: string);
181
264
  }
265
+ /**
266
+ * SE27 — the whole-workflow `inputSchema` rejected `run(input)` (before step 1).
267
+ * `detail` is a pre-formatted issues summary (a string, NOT Zod's `ZodIssue[]`).
268
+ */
269
+ export declare class WorkflowInputError extends Error {
270
+ readonly workflowName: string;
271
+ readonly detail: string;
272
+ readonly name = "WorkflowInputError";
273
+ constructor(workflowName: string, detail: string);
274
+ }
275
+ /**
276
+ * SE27 — the whole-workflow `outputSchema` rejected the final output (on `completed`).
277
+ * `detail` is a pre-formatted issues summary (a string, NOT Zod's `ZodIssue[]`).
278
+ */
279
+ export declare class WorkflowOutputError extends Error {
280
+ readonly workflowName: string;
281
+ readonly detail: string;
282
+ readonly name = "WorkflowOutputError";
283
+ constructor(workflowName: string, detail: string);
284
+ }
285
+ /**
286
+ * SE29 — `WorkflowOptions.stateSchema` rejected an `initialState` or a
287
+ * `setState(next)` call. `detail` is a pre-formatted issues summary.
288
+ */
289
+ export declare class WorkflowStateError extends Error {
290
+ readonly workflowName: string;
291
+ readonly detail: string;
292
+ readonly name = "WorkflowStateError";
293
+ constructor(workflowName: string, detail: string);
294
+ }
295
+ /**
296
+ * SE30 — a nested workflow (via `workflowStep`) did not `complete`. A nested
297
+ * `suspended` is NOT resumable in v1 (resume continues AFTER the step, so the
298
+ * child would be skipped) — restructure with a top-level suspend. A nested
299
+ * `failed`/`cancelled` fails the parent step with the child's error attached.
300
+ */
301
+ export declare class WorkflowNestedError extends Error {
302
+ readonly stepId: string;
303
+ readonly childName: string;
304
+ readonly childStatus: Exclude<WorkflowRun["status"], "completed">;
305
+ readonly childError?: {
306
+ name: string;
307
+ message: string;
308
+ } | undefined;
309
+ readonly name = "WorkflowNestedError";
310
+ constructor(stepId: string, childName: string, childStatus: Exclude<WorkflowRun["status"], "completed">, childError?: {
311
+ name: string;
312
+ message: string;
313
+ } | undefined);
314
+ }
182
315
  export declare class WorkflowAlreadyRunningError extends Error {
183
316
  readonly workflowName: string;
184
317
  readonly runId: string;