@nagi-js/core 0.1.0 → 0.1.1-rc.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.
- package/dist/index.d.ts +387 -20
- package/dist/index.js +638 -118
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -104,7 +104,7 @@ interface StepConfigBase<Input, N extends NeedsMap> {
|
|
|
104
104
|
readonly input: NoInfer<Input>;
|
|
105
105
|
readonly needs: NoInfer<NeedsOutputs<N>>;
|
|
106
106
|
}) => boolean;
|
|
107
|
-
readonly
|
|
107
|
+
readonly timeoutMs?: Millis;
|
|
108
108
|
}
|
|
109
109
|
interface TaskConfig<Input, N extends NeedsMap, Output> extends StepConfigBase<Input, N> {
|
|
110
110
|
readonly retry?: RetryPolicy;
|
|
@@ -113,6 +113,20 @@ interface TaskConfig<Input, N extends NeedsMap, Output> extends StepConfigBase<I
|
|
|
113
113
|
readonly needs: NoInfer<NeedsOutputs<N>>;
|
|
114
114
|
readonly ctx: StepCtx<NoInfer<Input>>;
|
|
115
115
|
}) => Promise<Output>;
|
|
116
|
+
/**
|
|
117
|
+
* Lifecycle hooks colocated with this step. Fire *before* the cross-cutting
|
|
118
|
+
* `FlowHooks` callbacks (deterministic ordering). A throwing hook is logged
|
|
119
|
+
* via the runtime's `Logger` and swallowed — hooks cannot fail the run.
|
|
120
|
+
*
|
|
121
|
+
* `onComplete`'s event is typed against this step's `Output` — no `Json`
|
|
122
|
+
* narrowing required at the call site.
|
|
123
|
+
*/
|
|
124
|
+
readonly onStart?: (event: StepStartEvent) => void | Promise<void>;
|
|
125
|
+
readonly onComplete?: (event: StepCompleteEvent & {
|
|
126
|
+
readonly output: NoInfer<Output>;
|
|
127
|
+
}) => void | Promise<void>;
|
|
128
|
+
readonly onError?: (event: StepErrorEvent) => void | Promise<void>;
|
|
129
|
+
readonly onRetry?: (event: StepRetryEvent) => void | Promise<void>;
|
|
116
130
|
}
|
|
117
131
|
interface SignalConfig<Input, N extends NeedsMap, Schema extends StandardSchemaV1> extends StepConfigBase<Input, N> {
|
|
118
132
|
readonly schema: Schema;
|
|
@@ -129,7 +143,7 @@ interface MatchDiscriminatorConfig<Input, N extends NeedsMap, D extends string,
|
|
|
129
143
|
readonly needs: NoInfer<NeedsOutputs<N>>;
|
|
130
144
|
}) => D;
|
|
131
145
|
readonly cases: {
|
|
132
|
-
readonly [K in D]: (b: Builder<Input>) => M[K]
|
|
146
|
+
readonly [K in D]: (b: Builder<Input>) => BuildResult<Input, M[K]>;
|
|
133
147
|
};
|
|
134
148
|
}
|
|
135
149
|
interface MatchArmGuard<Input, N extends NeedsMap, M extends StepMap> {
|
|
@@ -138,12 +152,12 @@ interface MatchArmGuard<Input, N extends NeedsMap, M extends StepMap> {
|
|
|
138
152
|
readonly needs: NoInfer<NeedsOutputs<N>>;
|
|
139
153
|
}) => boolean;
|
|
140
154
|
readonly otherwise?: never;
|
|
141
|
-
readonly build: (b: Builder<Input>) => M
|
|
155
|
+
readonly build: (b: Builder<Input>) => BuildResult<Input, M>;
|
|
142
156
|
}
|
|
143
157
|
interface MatchArmOtherwise<Input, M extends StepMap> {
|
|
144
158
|
readonly otherwise: true;
|
|
145
159
|
readonly when?: never;
|
|
146
|
-
readonly build: (b: Builder<Input>) => M
|
|
160
|
+
readonly build: (b: Builder<Input>) => BuildResult<Input, M>;
|
|
147
161
|
}
|
|
148
162
|
type MatchArm<Input, N extends NeedsMap, M extends StepMap> = MatchArmGuard<Input, N, M> | MatchArmOtherwise<Input, M>;
|
|
149
163
|
/**
|
|
@@ -158,11 +172,11 @@ type MatchArmShape<Input, N extends NeedsMap> = {
|
|
|
158
172
|
readonly needs: NoInfer<NeedsOutputs<N>>;
|
|
159
173
|
}) => boolean;
|
|
160
174
|
readonly otherwise?: never;
|
|
161
|
-
readonly build: (b: Builder<Input>) => StepMap
|
|
175
|
+
readonly build: (b: Builder<Input>) => BuildResult<Input, StepMap>;
|
|
162
176
|
} | {
|
|
163
177
|
readonly otherwise: true;
|
|
164
178
|
readonly when?: never;
|
|
165
|
-
readonly build: (b: Builder<Input>) => StepMap
|
|
179
|
+
readonly build: (b: Builder<Input>) => BuildResult<Input, StepMap>;
|
|
166
180
|
};
|
|
167
181
|
/**
|
|
168
182
|
* Guard mode — Rust-style `match { x if guard => ... }`. Top-to-bottom,
|
|
@@ -182,14 +196,59 @@ type MatchDiscriminatorOutput<D extends string, M extends Record<D, StepMap>> =
|
|
|
182
196
|
[K in D]: MatchArmOutput<M[K]>;
|
|
183
197
|
}[D];
|
|
184
198
|
/**
|
|
185
|
-
*
|
|
186
|
-
*
|
|
199
|
+
* Per-entry config for `b.step(key, config)`. `Needs` is a const tuple of
|
|
200
|
+
* sibling keys that resolve against the chain's accumulator `A`.
|
|
187
201
|
*/
|
|
188
|
-
interface
|
|
202
|
+
interface StepEntryConfig<Input, A extends Record<string, unknown>, Needs extends ReadonlyArray<keyof A & string>, Output> {
|
|
203
|
+
readonly needs?: Needs;
|
|
204
|
+
readonly retry?: RetryPolicy;
|
|
205
|
+
readonly timeoutMs?: Millis;
|
|
206
|
+
readonly when?: (args: {
|
|
207
|
+
readonly input: NoInfer<Input>;
|
|
208
|
+
readonly needs: {
|
|
209
|
+
readonly [P in Needs[number]]: A[P];
|
|
210
|
+
};
|
|
211
|
+
}) => boolean;
|
|
212
|
+
readonly run: (args: {
|
|
213
|
+
readonly input: NoInfer<Input>;
|
|
214
|
+
readonly needs: {
|
|
215
|
+
readonly [P in Needs[number]]: A[P];
|
|
216
|
+
};
|
|
217
|
+
readonly ctx: StepCtx<NoInfer<Input>>;
|
|
218
|
+
}) => Promise<Output>;
|
|
219
|
+
/** See {@link TaskConfig} for hook semantics. */
|
|
220
|
+
readonly onStart?: (event: StepStartEvent) => void | Promise<void>;
|
|
221
|
+
readonly onComplete?: (event: StepCompleteEvent & {
|
|
222
|
+
readonly output: NoInfer<Output>;
|
|
223
|
+
}) => void | Promise<void>;
|
|
224
|
+
readonly onError?: (event: StepErrorEvent) => void | Promise<void>;
|
|
225
|
+
readonly onRetry?: (event: StepRetryEvent) => void | Promise<void>;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* `b` inside `flow({ build: (b) => ... })`.
|
|
229
|
+
*
|
|
230
|
+
* Two complementary authoring styles:
|
|
231
|
+
*
|
|
232
|
+
* 1. **Standalone constructors** — `b.task`, `b.signal`, `b.match` each
|
|
233
|
+
* return a `Step<Output>` value the user can assign and reference. The
|
|
234
|
+
* build callback returns a `StepMap` of those values.
|
|
235
|
+
*
|
|
236
|
+
* 2. **Chain (key-first)** — `b.step(key, config)` is keyed and chainable.
|
|
237
|
+
* Each call extends the builder's accumulator `A` with `{ [Key]: Output }`,
|
|
238
|
+
* and subsequent `.step()` calls type `needs: ["sibling"]` against `A`.
|
|
239
|
+
* `b.include(key, prebuiltStep)` brings a `Step<O>` (from `b.task` /
|
|
240
|
+
* `b.signal` / `b.match`) into the chain under a key. The chain itself
|
|
241
|
+
* is returned from the build callback; `flow()` extracts the accumulator.
|
|
242
|
+
*
|
|
243
|
+
* Both styles compose in the same build callback — the chain end is the
|
|
244
|
+
* canonical result; pre-built standalone steps enter the chain via
|
|
245
|
+
* `.include(key, step)`.
|
|
246
|
+
*/
|
|
247
|
+
interface Builder<Input = unknown, A extends Record<string, unknown> = Record<never, never>> {
|
|
189
248
|
task<N extends NeedsMap, Output>(config: TaskConfig<Input, N, Output>): Step<Output>;
|
|
190
249
|
signal<N extends NeedsMap, S extends StandardSchemaV1>(config: SignalConfig<Input, N, S>): Step<InferSchemaOutput<S>>;
|
|
191
250
|
match<N extends NeedsMap, D extends string, Cases extends {
|
|
192
|
-
readonly [K in D]: (b: Builder<Input>) => StepMap
|
|
251
|
+
readonly [K in D]: (b: Builder<Input>) => BuildResult<Input, StepMap>;
|
|
193
252
|
}>(config: {
|
|
194
253
|
readonly needs?: N;
|
|
195
254
|
readonly on: (args: {
|
|
@@ -198,33 +257,96 @@ interface Builder<Input = unknown> {
|
|
|
198
257
|
}) => D;
|
|
199
258
|
readonly cases: Cases;
|
|
200
259
|
}): Step<{
|
|
201
|
-
readonly [K in keyof Cases]: MatchArmOutput<ReturnType<Cases[K]
|
|
260
|
+
readonly [K in keyof Cases]: MatchArmOutput<AsStepMap<ReturnType<Cases[K]>>>;
|
|
202
261
|
}[keyof Cases]>;
|
|
203
262
|
match<N extends NeedsMap, Arms extends ReadonlyArray<MatchArmShape<Input, N>>>(config: {
|
|
204
263
|
readonly needs?: N;
|
|
205
264
|
readonly arms: Arms;
|
|
206
|
-
}): Step<MatchArmOutput<ReturnType<Arms[number]["build"]
|
|
265
|
+
}): Step<MatchArmOutput<AsStepMap<ReturnType<Arms[number]["build"]>>>>;
|
|
266
|
+
/**
|
|
267
|
+
* Chainable task definition. The first arg is the persisted step id; the
|
|
268
|
+
* config follows the standalone `task` shape plus a `needs: ["sibling"]`
|
|
269
|
+
* tuple of accumulator keys. Returns the same builder typed with the new
|
|
270
|
+
* entry appended to `A`.
|
|
271
|
+
*
|
|
272
|
+
* b.step('fetchRecording', { run: async () => ({ url: '…' }) })
|
|
273
|
+
* .step('transcribe', {
|
|
274
|
+
* needs: ['fetchRecording'],
|
|
275
|
+
* run: async ({ needs }) => ({ text: needs.fetchRecording.url + '…' }),
|
|
276
|
+
* })
|
|
277
|
+
*/
|
|
278
|
+
step<const Key extends string, const Needs extends ReadonlyArray<keyof A & string>, Output>(key: Exclude<Key, keyof A>, config: StepEntryConfig<Input, A, Needs, Output>): Builder<Input, A & {
|
|
279
|
+
readonly [K in Key]: Output;
|
|
280
|
+
}>;
|
|
281
|
+
/**
|
|
282
|
+
* Include a pre-built `Step<O>` (from `b.task` / `b.signal` / `b.match`)
|
|
283
|
+
* into the chain under a key. The included step's output joins `A`.
|
|
284
|
+
*/
|
|
285
|
+
include<const Key extends string, S extends Step<unknown>>(key: Exclude<Key, keyof A>, step: S): Builder<Input, A & {
|
|
286
|
+
readonly [K in Key]: S extends Step<infer O> ? O : never;
|
|
287
|
+
}>;
|
|
207
288
|
}
|
|
208
|
-
|
|
289
|
+
/**
|
|
290
|
+
* Acceptable shape for a build callback's return. Either a plain `StepMap`
|
|
291
|
+
* (record-literal style) or a `Builder<Input, A>` whose accumulator is
|
|
292
|
+
* extracted by `flow()`.
|
|
293
|
+
*/
|
|
294
|
+
type BuildResult<Input, M extends StepMap> = M | Builder<Input, BuilderAccumulator<M>>;
|
|
295
|
+
/** Convert a `StepMap` back to its accumulator shape `{ [K]: Output }`. */
|
|
296
|
+
type BuilderAccumulator<M extends StepMap> = {
|
|
297
|
+
readonly [K in keyof M]: M[K] extends Step<infer O> ? O : never;
|
|
298
|
+
};
|
|
299
|
+
/** Extract a `StepMap` from either a plain map or a `Builder` chain result. */
|
|
300
|
+
type AsStepMap<R> = R extends Builder<unknown, infer A> ? {
|
|
301
|
+
readonly [K in keyof A]: Step<A[K]>;
|
|
302
|
+
} : R extends StepMap ? R : never;
|
|
303
|
+
interface FlowConfig<Id extends string, InputSchema extends StandardSchemaV1, R, Output = unknown> {
|
|
209
304
|
/** Stable persistence handle. TanStack-key shaped (kebab or snake). */
|
|
210
305
|
readonly id: Id;
|
|
211
306
|
readonly input: InputSchema;
|
|
212
|
-
|
|
307
|
+
/**
|
|
308
|
+
* Returns either a `StepMap` (record-literal style with `b.task` / `b.signal`
|
|
309
|
+
* / `b.match`) or a chained `Builder<Input, A>` (Express-style with `b.step`
|
|
310
|
+
* / `b.include`). `flow()` extracts the assembled `StepMap` either way.
|
|
311
|
+
*/
|
|
312
|
+
readonly build: (b: Builder<InferSchemaOutput<InputSchema>>) => R;
|
|
213
313
|
/**
|
|
214
314
|
* Compute the flow's terminal output from its step outputs. Fired once at
|
|
215
315
|
* `flow.completed` and persisted on the fact + `onFlowComplete` event.
|
|
216
316
|
* Skipped steps land as `null` in the input record at runtime even though
|
|
217
317
|
* the type claims otherwise (consistent with the "skip is transitive" lock).
|
|
218
|
-
* Method-shorthand syntax keeps `M` bivariant so `Flow<Id, S, M, O>` remains
|
|
219
|
-
* assignable to `Flow<string, S, StepMap, O>` in fixture/test code.
|
|
220
318
|
*/
|
|
221
|
-
output?(steps: NeedsOutputs<
|
|
319
|
+
output?(steps: NeedsOutputs<AsStepMap<R>>): Output;
|
|
320
|
+
/**
|
|
321
|
+
* Flow-level lifecycle hooks. Like step-local hooks (see {@link TaskConfig}),
|
|
322
|
+
* these fire *before* the cross-cutting `FlowHooks` callbacks, and a thrown
|
|
323
|
+
* hook is swallowed + logged rather than failing the run.
|
|
324
|
+
*
|
|
325
|
+
* `onComplete`'s event is typed against this flow's `Output` (whatever the
|
|
326
|
+
* `output()` resolver returns).
|
|
327
|
+
*/
|
|
328
|
+
readonly onStart?: (event: FlowStartEvent) => void | Promise<void>;
|
|
329
|
+
readonly onComplete?: (event: FlowCompleteEvent & {
|
|
330
|
+
readonly output: NoInfer<Output>;
|
|
331
|
+
}) => void | Promise<void>;
|
|
332
|
+
readonly onError?: (event: FlowErrorEvent) => void | Promise<void>;
|
|
222
333
|
}
|
|
223
334
|
interface Flow<Id extends string = string, InputSchema extends StandardSchemaV1 = StandardSchemaV1, M extends StepMap = StepMap, Output = unknown> {
|
|
224
335
|
readonly id: Id;
|
|
225
336
|
readonly input: InputSchema;
|
|
226
337
|
readonly steps: M;
|
|
227
338
|
output?(steps: NeedsOutputs<M>): Output;
|
|
339
|
+
/**
|
|
340
|
+
* Forwarded from {@link FlowConfig}; surfaced for the dispatcher.
|
|
341
|
+
*
|
|
342
|
+
* Note these use the plain (non-Output-augmented) event types — the typed
|
|
343
|
+
* narrowing lives on {@link FlowConfig}'s callbacks at build time, while
|
|
344
|
+
* the runtime form is variance-friendly so a concrete `Flow<...{x: number}>`
|
|
345
|
+
* stays assignable to `Flow<...unknown>` (the dispatcher's view).
|
|
346
|
+
*/
|
|
347
|
+
readonly onStart?: (event: FlowStartEvent) => void | Promise<void>;
|
|
348
|
+
readonly onComplete?: (event: FlowCompleteEvent) => void | Promise<void>;
|
|
349
|
+
readonly onError?: (event: FlowErrorEvent) => void | Promise<void>;
|
|
228
350
|
}
|
|
229
351
|
type FlowInput<F> = F extends Flow<string, infer S, StepMap, unknown> ? InferSchemaOutput<S> : never;
|
|
230
352
|
type FlowOutput<F> = F extends Flow<string, StandardSchemaV1, StepMap, infer O> ? O : never;
|
|
@@ -386,6 +508,48 @@ interface Store {
|
|
|
386
508
|
readonly output: T;
|
|
387
509
|
readonly fact: StepCompletedFact | StepFailedFact;
|
|
388
510
|
}>): Promise<T>;
|
|
511
|
+
/**
|
|
512
|
+
* Deduped store of canonical flow DAGs, keyed by content hash. Inserts are
|
|
513
|
+
* idempotent — the same `(flowHash, dag)` may be written by many concurrent
|
|
514
|
+
* boots; only the first physical row survives. Subsequent writes are
|
|
515
|
+
* no-ops.
|
|
516
|
+
*
|
|
517
|
+
* Adapters MUST enforce this at the durability layer (e.g. Postgres
|
|
518
|
+
* `INSERT ... ON CONFLICT (flow_hash) DO NOTHING`).
|
|
519
|
+
*/
|
|
520
|
+
upsertSnapshot(args: {
|
|
521
|
+
readonly flowHash: string;
|
|
522
|
+
readonly flowId: string;
|
|
523
|
+
readonly dag: Json;
|
|
524
|
+
}): Promise<void>;
|
|
525
|
+
/**
|
|
526
|
+
* Read the currently-published hash for a flow id. Returns `null` if no
|
|
527
|
+
* `flow_ref` row exists yet — first boot of a never-published flow.
|
|
528
|
+
*/
|
|
529
|
+
getRef(flowId: string): Promise<string | null>;
|
|
530
|
+
/**
|
|
531
|
+
* Atomically set the published hash for a flow id. Last writer wins; the
|
|
532
|
+
* audit trail of ref changes lives in {@link appendGlobalFact} as
|
|
533
|
+
* `flow_ref.updated`.
|
|
534
|
+
*
|
|
535
|
+
* Adapters MUST enforce atomicity at the durability layer (e.g. Postgres
|
|
536
|
+
* `INSERT ... ON CONFLICT (flow_id) DO UPDATE`).
|
|
537
|
+
*/
|
|
538
|
+
setRef(flowId: string, flowHash: string): Promise<void>;
|
|
539
|
+
/**
|
|
540
|
+
* Load a snapshot by its content hash. Returns `null` if no snapshot with
|
|
541
|
+
* this hash has been recorded — e.g., the deployment is replaying a run
|
|
542
|
+
* whose pinned topology was never seen by this store.
|
|
543
|
+
*/
|
|
544
|
+
loadSnapshot(flowHash: string): Promise<{
|
|
545
|
+
readonly flowId: string;
|
|
546
|
+
readonly dag: Json;
|
|
547
|
+
} | null>;
|
|
548
|
+
/**
|
|
549
|
+
* Append a non-run-scoped fact. See {@link GlobalFact} for the union.
|
|
550
|
+
* Currently only `flow_ref.updated` events flow through this method.
|
|
551
|
+
*/
|
|
552
|
+
appendGlobalFact(fact: GlobalFact): Promise<void>;
|
|
389
553
|
}
|
|
390
554
|
interface QueueMessage {
|
|
391
555
|
readonly receipt: string;
|
|
@@ -439,7 +603,39 @@ interface FlowStartedFact extends FactBase {
|
|
|
439
603
|
readonly kind: "flow.started";
|
|
440
604
|
readonly flowId: string;
|
|
441
605
|
readonly input: Json;
|
|
606
|
+
/**
|
|
607
|
+
* Content hash of the canonical flow DAG at the moment this run started.
|
|
608
|
+
* Pins the run to a specific topology; `wf.replay()` reads this to detect
|
|
609
|
+
* snapshot drift. Optional in v0 — legacy runs from before the snapshot
|
|
610
|
+
* store landed have `undefined`. After a soak period it becomes required.
|
|
611
|
+
*/
|
|
612
|
+
readonly flowHash?: string;
|
|
613
|
+
/**
|
|
614
|
+
* Free-form handler-code identifier, typically a git SHA. Sourced from
|
|
615
|
+
* `nagi({ codeVersion })`. Captures handler-source drift orthogonally to
|
|
616
|
+
* `flowHash` (which only captures topology). See RFC 0001
|
|
617
|
+
* "Topology vs handler code."
|
|
618
|
+
*/
|
|
619
|
+
readonly codeVersion?: string;
|
|
442
620
|
}
|
|
621
|
+
/**
|
|
622
|
+
* A non-run-scoped event, recorded as a side effect of nagi runtime activity
|
|
623
|
+
* that has no specific `runId`. Lives in the `nagi.global_fact` table.
|
|
624
|
+
*
|
|
625
|
+
* Per RFC 0001: when nagi boots and a flow's canonical hash differs from the
|
|
626
|
+
* currently-published one, the runtime updates `flow_ref` and appends a
|
|
627
|
+
* `flow_ref.updated` global fact. The audit trail of ref changes is queryable
|
|
628
|
+
* independently of any single run.
|
|
629
|
+
*/
|
|
630
|
+
interface FlowRefUpdatedFact {
|
|
631
|
+
readonly kind: "flow_ref.updated";
|
|
632
|
+
readonly flowId: string;
|
|
633
|
+
/** Previous hash, or null when this is the first publish of `flowId`. */
|
|
634
|
+
readonly from: string | null;
|
|
635
|
+
readonly to: string;
|
|
636
|
+
readonly at: Date;
|
|
637
|
+
}
|
|
638
|
+
type GlobalFact = FlowRefUpdatedFact;
|
|
443
639
|
interface FlowCompletedFact extends FactBase {
|
|
444
640
|
readonly kind: "flow.completed";
|
|
445
641
|
readonly output: Json;
|
|
@@ -513,10 +709,32 @@ interface RunState {
|
|
|
513
709
|
readonly status: RunStatus;
|
|
514
710
|
readonly steps: Readonly<Record<StepId, StepState>>;
|
|
515
711
|
readonly facts: ReadonlyArray<Fact>;
|
|
712
|
+
/** Content hash of the canonical flow DAG this run is pinned to. */
|
|
713
|
+
readonly flowHash?: string;
|
|
714
|
+
/** Handler-code identifier (typically a git SHA) recorded at run start. */
|
|
715
|
+
readonly codeVersion?: string;
|
|
516
716
|
}
|
|
517
717
|
type ReplayMode = "inspect" | "continue";
|
|
518
718
|
interface ReplayOpts {
|
|
519
719
|
readonly mode: ReplayMode;
|
|
720
|
+
/**
|
|
721
|
+
* When true, allow replays to proceed even if the live flow's canonical
|
|
722
|
+
* hash differs from the snapshot pinned to the run. Scheduling decisions
|
|
723
|
+
* come from the pinned snapshot; handler bodies are resolved from the
|
|
724
|
+
* currently-registered flow by step id (best-effort).
|
|
725
|
+
*
|
|
726
|
+
* When false (default), drift throws `NagiSnapshotDriftError`.
|
|
727
|
+
*/
|
|
728
|
+
readonly allowDrift?: boolean;
|
|
729
|
+
/**
|
|
730
|
+
* Whether step / flow lifecycle hooks (both the step-local ones on
|
|
731
|
+
* `TaskConfig` / `FlowConfig` and the cross-cutting `FlowHooks`) fire during
|
|
732
|
+
* replay. Defaults to `true` — replay re-executes steps via the idempotent
|
|
733
|
+
* dispatcher and emits the same observable lifecycle. Set to `false` for
|
|
734
|
+
* backfills or inspection-style replays where re-emitting webhooks would
|
|
735
|
+
* dual-publish.
|
|
736
|
+
*/
|
|
737
|
+
readonly fireHooks?: boolean;
|
|
520
738
|
}
|
|
521
739
|
|
|
522
740
|
/**
|
|
@@ -535,7 +753,115 @@ interface ReplayOpts {
|
|
|
535
753
|
* 4. Promote `MatchArmDef._nested` → `MatchArmDef.stepIds` (the namespaced
|
|
536
754
|
* IDs of the arm's nested steps), then drop `_nested`.
|
|
537
755
|
*/
|
|
538
|
-
declare function flow<const Id extends string, InputSchema extends StandardSchemaV1,
|
|
756
|
+
declare function flow<const Id extends string, InputSchema extends StandardSchemaV1, R, Output = unknown>(config: FlowConfig<Id, InputSchema, R, Output>): Flow<Id, InputSchema, AsStepMap<R>, Output>;
|
|
757
|
+
|
|
758
|
+
/**
|
|
759
|
+
* Canonical retry shape. Defaults are materialized explicitly so equivalent
|
|
760
|
+
* retry configs hash identically regardless of source-side omission style.
|
|
761
|
+
* `retryOn` is dropped — it's a function, not serializable.
|
|
762
|
+
*/
|
|
763
|
+
interface CanonicalRetryPolicy {
|
|
764
|
+
readonly maxAttempts: number;
|
|
765
|
+
readonly backoff: BackoffStrategy;
|
|
766
|
+
readonly initialDelayMs: Millis;
|
|
767
|
+
readonly maxDelayMs: Millis;
|
|
768
|
+
}
|
|
769
|
+
/**
|
|
770
|
+
* Canonical schema fingerprint. v0 hashes `validate.toString()` alongside
|
|
771
|
+
* runtime-stable `vendor` + `version` — best-effort, breaks under minification.
|
|
772
|
+
* Documented as a known limit in RFC 0001.
|
|
773
|
+
*/
|
|
774
|
+
interface CanonicalSchema {
|
|
775
|
+
readonly vendor: string;
|
|
776
|
+
readonly version: number;
|
|
777
|
+
readonly validateHash: string;
|
|
778
|
+
}
|
|
779
|
+
interface CanonicalMatchArm {
|
|
780
|
+
readonly id: string;
|
|
781
|
+
readonly otherwise?: true;
|
|
782
|
+
readonly whenHash?: string;
|
|
783
|
+
/** Namespaced IDs of nested steps for this arm, sorted lexicographically. */
|
|
784
|
+
readonly stepIds: readonly StepId[];
|
|
785
|
+
}
|
|
786
|
+
interface CanonicalStep {
|
|
787
|
+
readonly id: StepId;
|
|
788
|
+
readonly kind: StepKind;
|
|
789
|
+
/** Upstream step ids the step depends on, sorted lexicographically. */
|
|
790
|
+
readonly needs: readonly StepId[];
|
|
791
|
+
readonly whenHash?: string;
|
|
792
|
+
readonly retry?: CanonicalRetryPolicy;
|
|
793
|
+
readonly timeoutMs?: Millis;
|
|
794
|
+
readonly signalSchema?: CanonicalSchema;
|
|
795
|
+
readonly matchMode?: "discriminator" | "guard";
|
|
796
|
+
readonly matchOnHash?: string;
|
|
797
|
+
readonly matchArms?: readonly CanonicalMatchArm[];
|
|
798
|
+
}
|
|
799
|
+
interface CanonicalDag {
|
|
800
|
+
readonly flowId: string;
|
|
801
|
+
readonly inputSchema: CanonicalSchema;
|
|
802
|
+
/** Steps sorted by `id` lexicographically — insertion order is ignored. */
|
|
803
|
+
readonly steps: readonly CanonicalStep[];
|
|
804
|
+
}
|
|
805
|
+
/**
|
|
806
|
+
* Build the canonical DAG for a flow.
|
|
807
|
+
*
|
|
808
|
+
* Stable across cosmetic source changes (step-key reorder, needs-key reorder)
|
|
809
|
+
* and different across topology changes (added/removed steps, edge changes,
|
|
810
|
+
* `when` flips, retry edits, timeout edits).
|
|
811
|
+
*
|
|
812
|
+
* Handler `run` bodies are intentionally NOT part of the hash — see RFC 0001
|
|
813
|
+
* "Topology vs handler code." Use `nagi({ codeVersion })` (typically a git
|
|
814
|
+
* SHA) to capture handler-code drift orthogonally.
|
|
815
|
+
*
|
|
816
|
+
* Async because predicate/schema source bodies are sha256'd via
|
|
817
|
+
* `crypto.subtle` so the stored `dag` JSON stays compact.
|
|
818
|
+
*/
|
|
819
|
+
declare function canonicalize(flow: Flow): Promise<CanonicalDag>;
|
|
820
|
+
/**
|
|
821
|
+
* Compute the sha256 hex digest of a canonical DAG. Uses Web Crypto
|
|
822
|
+
* (`crypto.subtle.digest`) which is available in Node ≥ 19, modern browsers,
|
|
823
|
+
* Cloudflare Workers, Deno, and Bun.
|
|
824
|
+
*
|
|
825
|
+
* The DAG is serialized via {@link stableStringify} so object-key order is
|
|
826
|
+
* deterministic. Two equivalent DAGs produce byte-identical input bytes.
|
|
827
|
+
*/
|
|
828
|
+
declare function sha256Canonical(dag: CanonicalDag): Promise<string>;
|
|
829
|
+
|
|
830
|
+
/** Fields on a step whose hash-relevant value can change between snapshots. */
|
|
831
|
+
type SnapshotChangedField = "kind" | "when" | "retry" | "timeoutMs" | "signalSchema" | "matchMode" | "matchOn" | "matchArms";
|
|
832
|
+
interface SnapshotChangedEdge {
|
|
833
|
+
readonly from: StepId;
|
|
834
|
+
readonly to: StepId;
|
|
835
|
+
readonly before: "needed" | "absent";
|
|
836
|
+
readonly after: "needed" | "absent";
|
|
837
|
+
}
|
|
838
|
+
interface SnapshotChangedField_ {
|
|
839
|
+
readonly stepId: StepId;
|
|
840
|
+
readonly field: SnapshotChangedField;
|
|
841
|
+
}
|
|
842
|
+
/**
|
|
843
|
+
* Structural delta between two snapshots — added/removed steps, added/removed
|
|
844
|
+
* edges, per-field topology changes on shared steps.
|
|
845
|
+
*
|
|
846
|
+
* Pure function over canonical DAGs. The output mirrors the RFC 0001 shape:
|
|
847
|
+
*
|
|
848
|
+
* {
|
|
849
|
+
* addedSteps,
|
|
850
|
+
* removedSteps,
|
|
851
|
+
* changedEdges, // append-or-remove edges between shared steps
|
|
852
|
+
* changedPredicates // when / retry / timeoutMs / schema / match changes
|
|
853
|
+
* }
|
|
854
|
+
*
|
|
855
|
+
* "Changed" means the canonical hash-relevant value differs. For predicates
|
|
856
|
+
* this is best-effort under the same source-hash limit as canonicalize.
|
|
857
|
+
*/
|
|
858
|
+
interface SnapshotDiff {
|
|
859
|
+
readonly addedSteps: readonly StepId[];
|
|
860
|
+
readonly removedSteps: readonly StepId[];
|
|
861
|
+
readonly changedEdges: readonly SnapshotChangedEdge[];
|
|
862
|
+
readonly changedPredicates: readonly SnapshotChangedField_[];
|
|
863
|
+
}
|
|
864
|
+
declare function diffSnapshots(before: CanonicalDag, after: CanonicalDag): SnapshotDiff;
|
|
539
865
|
|
|
540
866
|
interface InMemoryStoreOpts {
|
|
541
867
|
readonly leaseMs?: Millis;
|
|
@@ -545,6 +871,9 @@ declare class InMemoryStore implements Store {
|
|
|
545
871
|
private readonly outputs;
|
|
546
872
|
private readonly onces;
|
|
547
873
|
private readonly leases;
|
|
874
|
+
private readonly snapshots;
|
|
875
|
+
private readonly refs;
|
|
876
|
+
private readonly globalFacts;
|
|
548
877
|
private readonly leaseMs;
|
|
549
878
|
constructor(opts?: InMemoryStoreOpts);
|
|
550
879
|
appendFact(runId: RunId, fact: Fact): Promise<void>;
|
|
@@ -562,6 +891,20 @@ declare class InMemoryStore implements Store {
|
|
|
562
891
|
readonly output: T;
|
|
563
892
|
readonly fact: StepCompletedFact | StepFailedFact;
|
|
564
893
|
}>): Promise<T>;
|
|
894
|
+
upsertSnapshot(args: {
|
|
895
|
+
readonly flowHash: string;
|
|
896
|
+
readonly flowId: string;
|
|
897
|
+
readonly dag: Json;
|
|
898
|
+
}): Promise<void>;
|
|
899
|
+
getRef(flowId: string): Promise<string | null>;
|
|
900
|
+
setRef(flowId: string, flowHash: string): Promise<void>;
|
|
901
|
+
loadSnapshot(flowHash: string): Promise<{
|
|
902
|
+
readonly flowId: string;
|
|
903
|
+
readonly dag: Json;
|
|
904
|
+
} | null>;
|
|
905
|
+
appendGlobalFact(fact: GlobalFact): Promise<void>;
|
|
906
|
+
/** Test-only: peek at the in-memory global fact log. */
|
|
907
|
+
readGlobalFacts(): ReadonlyArray<GlobalFact>;
|
|
565
908
|
}
|
|
566
909
|
/**
|
|
567
910
|
* Project an append-only fact stream into a `RunState`. Public so adapters
|
|
@@ -624,6 +967,14 @@ interface NagiConfig {
|
|
|
624
967
|
readonly hooks?: FlowHooks;
|
|
625
968
|
readonly logger?: Logger;
|
|
626
969
|
readonly defaultRetry?: RetryPolicy;
|
|
970
|
+
/**
|
|
971
|
+
* Handler-code identifier — typically a git SHA from `process.env.GIT_SHA`
|
|
972
|
+
* or your build's bundle hash. Persisted on `workflow_run.code_version` and
|
|
973
|
+
* on every `flow.started` fact for runs started by this process. Captures
|
|
974
|
+
* handler-body drift orthogonally to the topology hash. See RFC 0001
|
|
975
|
+
* "Topology vs handler code."
|
|
976
|
+
*/
|
|
977
|
+
readonly codeVersion?: string;
|
|
627
978
|
}
|
|
628
979
|
interface StartOpts {
|
|
629
980
|
/**
|
|
@@ -660,6 +1011,22 @@ declare class NagiValidationError extends Error {
|
|
|
660
1011
|
declare class NagiRuntimeError extends Error {
|
|
661
1012
|
constructor(message: string);
|
|
662
1013
|
}
|
|
663
|
-
|
|
1014
|
+
/**
|
|
1015
|
+
* Thrown by `wf.replay(runId)` when the live flow's canonical hash differs
|
|
1016
|
+
* from the snapshot the run was pinned to. The default replay behavior is
|
|
1017
|
+
* fail-loud — pass `replayOpts.allowDrift = true` to proceed using a
|
|
1018
|
+
* synthesized flow (topology from snapshot, handlers/predicates from live).
|
|
1019
|
+
*/
|
|
1020
|
+
declare class NagiSnapshotDriftError extends Error {
|
|
1021
|
+
readonly runId: RunId;
|
|
1022
|
+
readonly expected: string;
|
|
1023
|
+
readonly actual: string;
|
|
1024
|
+
constructor(args: {
|
|
1025
|
+
readonly runId: RunId;
|
|
1026
|
+
readonly expected: string;
|
|
1027
|
+
readonly actual: string;
|
|
1028
|
+
});
|
|
1029
|
+
}
|
|
1030
|
+
declare function nagi(config: NagiConfig): Promise<Wf>;
|
|
664
1031
|
|
|
665
|
-
export { type AttemptNumber, type BackoffStrategy, type Builder, type ClaimToken, type Clock, type Fact, type FactKind, type Flow, type FlowCompleteEvent, type FlowCompletedFact, type FlowConfig, type FlowErrorEvent, type FlowEvent, type FlowFailedFact, type FlowHooks, type FlowInput, type FlowOutput, type FlowStartEvent, type FlowStartedFact, InMemoryClock, InMemoryQueue, InMemoryStore, InMemoryTrigger, type InferSchemaInput, type InferSchemaOutput, type Json, type Logger, type MatchArm, type MatchArmGuard, type MatchArmOtherwise, type MatchArmOutput, type MatchArmSelectedFact, type MatchArmShape, type MatchDiscriminatorConfig, type MatchDiscriminatorOutput, type MatchGuardConfig, type Millis, type NagiConfig, NagiRuntimeError, NagiValidationError, type NeedsMap, type NeedsOutputs, type OnceRecordedFact, type Queue, type QueueDequeueOpts, type QueueEnqueueOpts, type QueueMessage, type Register, type ReplayMode, type ReplayOpts, type RetryPolicy, type RunId, type RunState, type RunStatus, type SerializedError, type SignalConfig, type SignalReceivedEvent, type SignalReceivedFact, type SignalSentEvent, type SignalSentFact, StandardSchemaV1, type StartOpts, type Step, type StepCompleteEvent, type StepCompletedFact, type StepCtx, type StepErrorEvent, type StepEvent, type StepFailedFact, type StepId, type StepKind, type StepMap, type StepOutput, type StepRetriedFact, type StepRetryEvent, type StepSkippedFact, type StepStartEvent, type StepStartedFact, type StepState, type StepStatus, type Store, type TaskConfig, type Trigger, type Tx, type Wf, type Worker, type WorkerConfig, type WorkerRunOnceOpts, type WorkerRunResult, type WorkerRunUntilEmptyOpts, flow, nagi, projectRunState };
|
|
1032
|
+
export { type AsStepMap, type AttemptNumber, type BackoffStrategy, type BuildResult, type Builder, type BuilderAccumulator, type CanonicalDag, type CanonicalMatchArm, type CanonicalRetryPolicy, type CanonicalSchema, type CanonicalStep, type ClaimToken, type Clock, type Fact, type FactKind, type Flow, type FlowCompleteEvent, type FlowCompletedFact, type FlowConfig, type FlowErrorEvent, type FlowEvent, type FlowFailedFact, type FlowHooks, type FlowInput, type FlowOutput, type FlowRefUpdatedFact, type FlowStartEvent, type FlowStartedFact, type GlobalFact, InMemoryClock, InMemoryQueue, InMemoryStore, InMemoryTrigger, type InferSchemaInput, type InferSchemaOutput, type Json, type Logger, type MatchArm, type MatchArmGuard, type MatchArmOtherwise, type MatchArmOutput, type MatchArmSelectedFact, type MatchArmShape, type MatchDiscriminatorConfig, type MatchDiscriminatorOutput, type MatchGuardConfig, type Millis, type NagiConfig, NagiRuntimeError, NagiSnapshotDriftError, NagiValidationError, type NeedsMap, type NeedsOutputs, type OnceRecordedFact, type Queue, type QueueDequeueOpts, type QueueEnqueueOpts, type QueueMessage, type Register, type ReplayMode, type ReplayOpts, type RetryPolicy, type RunId, type RunState, type RunStatus, type SerializedError, type SignalConfig, type SignalReceivedEvent, type SignalReceivedFact, type SignalSentEvent, type SignalSentFact, type SnapshotChangedEdge, type SnapshotChangedField, type SnapshotDiff, StandardSchemaV1, type StartOpts, type Step, type StepCompleteEvent, type StepCompletedFact, type StepCtx, type StepEntryConfig, type StepErrorEvent, type StepEvent, type StepFailedFact, type StepId, type StepKind, type StepMap, type StepOutput, type StepRetriedFact, type StepRetryEvent, type StepSkippedFact, type StepStartEvent, type StepStartedFact, type StepState, type StepStatus, type Store, type TaskConfig, type Trigger, type Tx, type Wf, type Worker, type WorkerConfig, type WorkerRunOnceOpts, type WorkerRunResult, type WorkerRunUntilEmptyOpts, canonicalize, diffSnapshots, flow, nagi, projectRunState, sha256Canonical };
|