@nagi-js/core 0.1.1-rc.10 → 0.1.1-rc.12
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 +284 -127
- package/dist/index.js +2120 -1494
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,113 @@
|
|
|
1
|
+
type SkipReason = "when-false" | "transitive" | "manual";
|
|
2
|
+
type StepCancelCause = {
|
|
3
|
+
readonly kind: "run-canceled";
|
|
4
|
+
} | {
|
|
5
|
+
readonly kind: "aborted";
|
|
6
|
+
readonly error?: SerializedError;
|
|
7
|
+
};
|
|
8
|
+
type RunCancelCause = {
|
|
9
|
+
readonly kind: "concurrency";
|
|
10
|
+
readonly canceledByRunId: RunId;
|
|
11
|
+
readonly concurrencyKey: string;
|
|
12
|
+
} | {
|
|
13
|
+
readonly kind: "explicit";
|
|
14
|
+
readonly reason: string;
|
|
15
|
+
readonly note?: string;
|
|
16
|
+
} | {
|
|
17
|
+
readonly kind: "operator";
|
|
18
|
+
readonly actor: string;
|
|
19
|
+
readonly reason: string;
|
|
20
|
+
readonly note?: string;
|
|
21
|
+
};
|
|
22
|
+
type StepState = {
|
|
23
|
+
readonly tag: "pending";
|
|
24
|
+
} | {
|
|
25
|
+
readonly tag: "running";
|
|
26
|
+
readonly attempt: AttemptNumber;
|
|
27
|
+
} | {
|
|
28
|
+
readonly tag: "awaitingSignal";
|
|
29
|
+
readonly attempt: AttemptNumber;
|
|
30
|
+
} | {
|
|
31
|
+
readonly tag: "awaitingChild";
|
|
32
|
+
readonly attempt: AttemptNumber;
|
|
33
|
+
} | {
|
|
34
|
+
readonly tag: "backoff";
|
|
35
|
+
readonly failedAttempt: AttemptNumber;
|
|
36
|
+
readonly retryAt: Date;
|
|
37
|
+
readonly error: SerializedError;
|
|
38
|
+
} | {
|
|
39
|
+
readonly tag: "aborting";
|
|
40
|
+
readonly attempt: AttemptNumber;
|
|
41
|
+
} | {
|
|
42
|
+
readonly tag: "completed";
|
|
43
|
+
readonly attempt: AttemptNumber;
|
|
44
|
+
readonly output: Json;
|
|
45
|
+
} | {
|
|
46
|
+
readonly tag: "failed";
|
|
47
|
+
readonly attempt: AttemptNumber;
|
|
48
|
+
readonly error: SerializedError;
|
|
49
|
+
} | {
|
|
50
|
+
readonly tag: "skipped";
|
|
51
|
+
readonly reason: SkipReason;
|
|
52
|
+
} | {
|
|
53
|
+
readonly tag: "canceled";
|
|
54
|
+
readonly cause: StepCancelCause;
|
|
55
|
+
};
|
|
56
|
+
type Resolved<T = Json> = {
|
|
57
|
+
readonly tag: "value";
|
|
58
|
+
readonly value: T;
|
|
59
|
+
} | {
|
|
60
|
+
readonly tag: "skipped";
|
|
61
|
+
};
|
|
62
|
+
type RunPhase = {
|
|
63
|
+
readonly tag: "pending";
|
|
64
|
+
} | {
|
|
65
|
+
readonly tag: "running";
|
|
66
|
+
} | {
|
|
67
|
+
readonly tag: "completed";
|
|
68
|
+
readonly output: Json;
|
|
69
|
+
} | {
|
|
70
|
+
readonly tag: "failed";
|
|
71
|
+
readonly error: SerializedError;
|
|
72
|
+
} | {
|
|
73
|
+
readonly tag: "canceled";
|
|
74
|
+
readonly cause: RunCancelCause;
|
|
75
|
+
};
|
|
76
|
+
interface Anomaly {
|
|
77
|
+
readonly at: Date;
|
|
78
|
+
readonly stepId?: StepId;
|
|
79
|
+
readonly from: string;
|
|
80
|
+
readonly fact: Fact["kind"];
|
|
81
|
+
}
|
|
82
|
+
interface RunState {
|
|
83
|
+
readonly runId: RunId;
|
|
84
|
+
readonly flowId: string;
|
|
85
|
+
readonly input: Json;
|
|
86
|
+
readonly phase: RunPhase;
|
|
87
|
+
readonly steps: Readonly<Record<StepId, StepState>>;
|
|
88
|
+
readonly selectedArms: Readonly<Record<StepId, string>>;
|
|
89
|
+
readonly bufferedSignals: Readonly<Record<StepId, {
|
|
90
|
+
readonly payload: Json;
|
|
91
|
+
readonly signalName?: string;
|
|
92
|
+
}>>;
|
|
93
|
+
readonly anomalies: readonly Anomaly[];
|
|
94
|
+
readonly facts: readonly Fact[];
|
|
95
|
+
readonly parent?: ParentLink;
|
|
96
|
+
readonly flowHash?: string;
|
|
97
|
+
readonly codeVersion?: string;
|
|
98
|
+
}
|
|
99
|
+
declare function stepStateOf(runState: RunState, stepId: StepId): StepState;
|
|
100
|
+
declare function runStatusOf(state: RunState): RunStatus;
|
|
101
|
+
declare function stepStatusOf(s: StepState): StepStatus;
|
|
102
|
+
declare function outputOf(s: StepState): Json | null;
|
|
103
|
+
declare function errorOf(s: StepState): SerializedError | undefined;
|
|
104
|
+
declare function attemptOf(s: StepState): AttemptNumber;
|
|
105
|
+
declare function resolvedOf(s: StepState): Resolved;
|
|
106
|
+
declare function unwrap<T>(r: Resolved<T>): T;
|
|
107
|
+
declare function isTerminalRun(state: RunState): boolean;
|
|
108
|
+
declare function isStepTerminal(s: StepState): boolean;
|
|
109
|
+
declare function foldRun(runId: RunId, facts: readonly Fact[]): RunState;
|
|
110
|
+
|
|
1
111
|
type Json = string | number | boolean | null | Json[] | {
|
|
2
112
|
[key: string]: Json;
|
|
3
113
|
};
|
|
@@ -44,7 +154,7 @@ interface Register {
|
|
|
44
154
|
type Tx = Register extends {
|
|
45
155
|
tx: infer T;
|
|
46
156
|
} ? T : unknown;
|
|
47
|
-
type StepKind = "task" | "signal" | "match" | "subflow";
|
|
157
|
+
type StepKind = "task" | "signal" | "match" | "subflow" | "streaming";
|
|
48
158
|
interface Step<Output = unknown> {
|
|
49
159
|
readonly kind: StepKind;
|
|
50
160
|
readonly id: StepId;
|
|
@@ -52,10 +162,23 @@ interface Step<Output = unknown> {
|
|
|
52
162
|
}
|
|
53
163
|
type StepOutput<S> = S extends Step<infer O> ? O : never;
|
|
54
164
|
type StepMap = Readonly<Record<string, Step<unknown>>>;
|
|
55
|
-
|
|
165
|
+
interface Optional<S extends Step = Step> {
|
|
166
|
+
readonly __optional: S;
|
|
167
|
+
}
|
|
168
|
+
type NeedRef = Step | Optional<Step>;
|
|
169
|
+
type NeedsMap = Readonly<Record<string, NeedRef>>;
|
|
56
170
|
type NeedsOutputs<N extends NeedsMap> = {
|
|
57
171
|
readonly [K in keyof N]: StepOutput<N[K]>;
|
|
58
172
|
};
|
|
173
|
+
type ResolvedNeeds<N extends NeedsMap> = {
|
|
174
|
+
readonly [K in keyof N]: N[K] extends Optional<infer S> ? Resolved<StepOutput<S>> : N[K] extends Step ? StepOutput<N[K]> : never;
|
|
175
|
+
};
|
|
176
|
+
type LogLevel = "debug" | "info" | "warn" | "error";
|
|
177
|
+
interface LogEntry {
|
|
178
|
+
readonly level: LogLevel;
|
|
179
|
+
readonly msg: string;
|
|
180
|
+
readonly attrs?: Record<string, unknown>;
|
|
181
|
+
}
|
|
59
182
|
interface Logger {
|
|
60
183
|
debug(message: string, attrs?: Record<string, unknown>): void;
|
|
61
184
|
info(message: string, attrs?: Record<string, unknown>): void;
|
|
@@ -74,6 +197,22 @@ interface StepCtx<Input = unknown> {
|
|
|
74
197
|
once<T extends Json>(scope: string, fn: () => Promise<T>): Promise<T>;
|
|
75
198
|
idempotencyKey(scope: string): string;
|
|
76
199
|
}
|
|
200
|
+
type StreamEvent<C = Json> = {
|
|
201
|
+
readonly kind: "chunk";
|
|
202
|
+
readonly chunk: C;
|
|
203
|
+
} | {
|
|
204
|
+
readonly kind: "dropped";
|
|
205
|
+
readonly count: number;
|
|
206
|
+
} | {
|
|
207
|
+
readonly kind: "retry";
|
|
208
|
+
readonly attempt: AttemptNumber;
|
|
209
|
+
} | {
|
|
210
|
+
readonly kind: "error";
|
|
211
|
+
readonly error: SerializedError;
|
|
212
|
+
};
|
|
213
|
+
interface StreamingStepCtx<Input = unknown, Chunk = Json> extends StepCtx<Input> {
|
|
214
|
+
readonly emit: (chunk: Chunk) => Promise<void>;
|
|
215
|
+
}
|
|
77
216
|
type BackoffStrategy = "exponential" | "linear" | "fixed";
|
|
78
217
|
interface RetryPolicy {
|
|
79
218
|
readonly maxAttempts: number;
|
|
@@ -86,17 +225,11 @@ interface StepConfigBase<Input, N extends NeedsMap> {
|
|
|
86
225
|
readonly needs?: N;
|
|
87
226
|
readonly when?: (args: {
|
|
88
227
|
readonly input: NoInfer<Input>;
|
|
89
|
-
readonly needs: NoInfer<
|
|
228
|
+
readonly needs: NoInfer<ResolvedNeeds<N>>;
|
|
90
229
|
}) => boolean;
|
|
91
230
|
readonly timeoutMs?: Millis;
|
|
92
231
|
}
|
|
93
|
-
interface
|
|
94
|
-
readonly retry?: RetryPolicy;
|
|
95
|
-
readonly run: (args: {
|
|
96
|
-
readonly input: NoInfer<Input>;
|
|
97
|
-
readonly needs: NoInfer<NeedsOutputs<N>>;
|
|
98
|
-
readonly ctx: StepCtx<NoInfer<Input>>;
|
|
99
|
-
}) => Promise<Output>;
|
|
232
|
+
interface StepLifecycleHooks<Output> {
|
|
100
233
|
readonly onStart?: (event: StepStartEvent) => void | Promise<void>;
|
|
101
234
|
readonly onComplete?: (event: StepCompleteEvent & {
|
|
102
235
|
readonly output: NoInfer<Output>;
|
|
@@ -104,6 +237,22 @@ interface TaskConfig<Input, N extends NeedsMap, Output> extends StepConfigBase<I
|
|
|
104
237
|
readonly onError?: (event: StepErrorEvent) => void | Promise<void>;
|
|
105
238
|
readonly onRetry?: (event: StepRetryEvent) => void | Promise<void>;
|
|
106
239
|
}
|
|
240
|
+
interface TaskConfig<Input, N extends NeedsMap, Output> extends StepConfigBase<Input, N>, StepLifecycleHooks<Output> {
|
|
241
|
+
readonly retry?: RetryPolicy;
|
|
242
|
+
readonly run: (args: {
|
|
243
|
+
readonly input: NoInfer<Input>;
|
|
244
|
+
readonly needs: NoInfer<ResolvedNeeds<N>>;
|
|
245
|
+
readonly ctx: StepCtx<NoInfer<Input>>;
|
|
246
|
+
}) => Promise<Output>;
|
|
247
|
+
}
|
|
248
|
+
interface StreamingTaskConfig<Input, N extends NeedsMap, Output, Chunk> extends StepConfigBase<Input, N>, StepLifecycleHooks<Output> {
|
|
249
|
+
readonly retry?: RetryPolicy;
|
|
250
|
+
readonly run: (args: {
|
|
251
|
+
readonly input: NoInfer<Input>;
|
|
252
|
+
readonly needs: NoInfer<ResolvedNeeds<N>>;
|
|
253
|
+
readonly ctx: StreamingStepCtx<NoInfer<Input>, Chunk>;
|
|
254
|
+
}) => Promise<Output>;
|
|
255
|
+
}
|
|
107
256
|
interface SignalConfig<Input, N extends NeedsMap, Schema extends StandardSchemaV1> extends StepConfigBase<Input, N> {
|
|
108
257
|
readonly schema: Schema;
|
|
109
258
|
readonly names?: readonly [string, ...string[]];
|
|
@@ -111,7 +260,7 @@ interface SignalConfig<Input, N extends NeedsMap, Schema extends StandardSchemaV
|
|
|
111
260
|
interface MatchArmGuard<Input, N extends NeedsMap, M extends StepMap> {
|
|
112
261
|
readonly when: (args: {
|
|
113
262
|
readonly input: NoInfer<Input>;
|
|
114
|
-
readonly needs: NoInfer<
|
|
263
|
+
readonly needs: NoInfer<ResolvedNeeds<N>>;
|
|
115
264
|
}) => boolean;
|
|
116
265
|
readonly otherwise?: never;
|
|
117
266
|
readonly build: (b: Builder<Input>) => M;
|
|
@@ -125,7 +274,7 @@ type MatchArm<Input, N extends NeedsMap, M extends StepMap> = MatchArmGuard<Inpu
|
|
|
125
274
|
type MatchArmShape<Input, N extends NeedsMap> = {
|
|
126
275
|
readonly when: (args: {
|
|
127
276
|
readonly input: NoInfer<Input>;
|
|
128
|
-
readonly needs: NoInfer<
|
|
277
|
+
readonly needs: NoInfer<ResolvedNeeds<N>>;
|
|
129
278
|
}) => boolean;
|
|
130
279
|
readonly otherwise?: never;
|
|
131
280
|
readonly build: (b: Builder<Input>) => StepMap;
|
|
@@ -144,7 +293,7 @@ type MatchArmOutput<M extends StepMap> = {
|
|
|
144
293
|
interface SubflowConfig<Input, N extends NeedsMap, Child extends Flow> extends StepConfigBase<Input, N> {
|
|
145
294
|
readonly input: (args: {
|
|
146
295
|
readonly input: NoInfer<Input>;
|
|
147
|
-
readonly needs: NoInfer<
|
|
296
|
+
readonly needs: NoInfer<ResolvedNeeds<N>>;
|
|
148
297
|
}) => FlowInput<Child>;
|
|
149
298
|
}
|
|
150
299
|
interface SubflowStepOutput<ChildOutput> {
|
|
@@ -153,6 +302,7 @@ interface SubflowStepOutput<ChildOutput> {
|
|
|
153
302
|
}
|
|
154
303
|
interface Builder<Input = unknown> {
|
|
155
304
|
task<N extends NeedsMap, Output>(config: TaskConfig<Input, N, Output>): Step<Output>;
|
|
305
|
+
streamingTask<N extends NeedsMap, O, C = Json>(config: StreamingTaskConfig<Input, N, O, C>): Step<O>;
|
|
156
306
|
signal<N extends NeedsMap, S extends StandardSchemaV1>(config: SignalConfig<Input, N, S>): Step<InferSchemaOutput<S>>;
|
|
157
307
|
subflow<N extends NeedsMap, Child extends Flow>(child: Child, config: SubflowConfig<Input, N, Child>): Step<SubflowStepOutput<FlowOutput<Child>>>;
|
|
158
308
|
match<N extends NeedsMap, Arms extends ReadonlyArray<MatchArmShape<Input, N>>>(config: {
|
|
@@ -197,19 +347,10 @@ interface Flow<Id extends string = string, InputSchema extends StandardSchemaV1
|
|
|
197
347
|
type FlowInput<F> = F extends Flow<string, infer S, StepMap, unknown> ? InferSchemaOutput<S> : never;
|
|
198
348
|
type FlowOutput<F> = F extends Flow<string, StandardSchemaV1, StepMap, infer O> ? O : never;
|
|
199
349
|
type FlowIdOf<T extends ReadonlyArray<Flow>> = T[number] extends Flow<infer Id> ? Id : never;
|
|
200
|
-
/**
|
|
201
|
-
* Durable parent linkage persisted on {@link FlowStartedFact}. Present when a
|
|
202
|
-
* run was started as a subflow child; absent for top-level runs.
|
|
203
|
-
*/
|
|
204
350
|
interface ParentLink {
|
|
205
351
|
readonly runId: RunId;
|
|
206
352
|
readonly stepId: StepId;
|
|
207
353
|
}
|
|
208
|
-
/**
|
|
209
|
-
* In-process parent reference: a {@link ParentLink} plus the parent step's
|
|
210
|
-
* attempt, used to thread otel spans and registry lookups. Not persisted — the
|
|
211
|
-
* attempt is re-derived from parent run state when a subflow wakes its parent.
|
|
212
|
-
*/
|
|
213
354
|
interface ParentRef extends ParentLink {
|
|
214
355
|
readonly attempt: AttemptNumber;
|
|
215
356
|
}
|
|
@@ -220,11 +361,6 @@ interface FlowEvent {
|
|
|
220
361
|
}
|
|
221
362
|
interface FlowStartEvent extends FlowEvent {
|
|
222
363
|
readonly input: Json;
|
|
223
|
-
/**
|
|
224
|
-
* Set when this run was started as a subflow child. Undefined for
|
|
225
|
-
* top-level runs (start / startById). Carried in-process only — for
|
|
226
|
-
* durable linkage see {@link FlowStartedFact.parent}.
|
|
227
|
-
*/
|
|
228
364
|
readonly parent?: ParentRef;
|
|
229
365
|
}
|
|
230
366
|
interface FlowCompleteEvent extends FlowEvent {
|
|
@@ -243,7 +379,7 @@ interface StepStartEvent extends StepEvent {
|
|
|
243
379
|
}
|
|
244
380
|
interface StepCompleteEvent extends StepEvent {
|
|
245
381
|
readonly output: Json;
|
|
246
|
-
readonly durationMs
|
|
382
|
+
readonly durationMs?: Millis;
|
|
247
383
|
}
|
|
248
384
|
interface StepErrorEvent extends StepEvent {
|
|
249
385
|
readonly error: SerializedError;
|
|
@@ -297,9 +433,28 @@ interface SerializedError {
|
|
|
297
433
|
readonly stack?: string;
|
|
298
434
|
readonly cause?: Json;
|
|
299
435
|
}
|
|
436
|
+
type SettleSignalResult = {
|
|
437
|
+
readonly tag: "delivered";
|
|
438
|
+
readonly attempt: AttemptNumber;
|
|
439
|
+
readonly payload: Json;
|
|
440
|
+
readonly signalName?: string;
|
|
441
|
+
} | {
|
|
442
|
+
readonly tag: "buffered";
|
|
443
|
+
} | {
|
|
444
|
+
readonly tag: "noop";
|
|
445
|
+
};
|
|
300
446
|
interface Store {
|
|
301
447
|
appendFact(runId: RunId, fact: Fact): Promise<void>;
|
|
302
448
|
loadRunState(runId: RunId): Promise<RunState>;
|
|
449
|
+
settleSignal(args: {
|
|
450
|
+
readonly runId: RunId;
|
|
451
|
+
readonly stepId: StepId;
|
|
452
|
+
readonly at: Date;
|
|
453
|
+
readonly incoming?: {
|
|
454
|
+
readonly payload: Json;
|
|
455
|
+
readonly signalName?: string;
|
|
456
|
+
};
|
|
457
|
+
}): Promise<SettleSignalResult>;
|
|
303
458
|
tryStartRun(runId: RunId, fact: FlowStartedFact, concurrency?: {
|
|
304
459
|
readonly key: string;
|
|
305
460
|
readonly mode: ConcurrencyMode;
|
|
@@ -311,9 +466,7 @@ interface Store {
|
|
|
311
466
|
}>;
|
|
312
467
|
}>;
|
|
313
468
|
claimStep(runId: RunId, stepId: StepId, attempt: AttemptNumber): Promise<ClaimToken | null>;
|
|
314
|
-
|
|
315
|
-
failStep(runId: RunId, stepId: StepId, error: SerializedError, fact: Fact): Promise<void>;
|
|
316
|
-
getStepOutput(runId: RunId, stepId: StepId): Promise<Json | null>;
|
|
469
|
+
settleStep(runId: RunId, stepId: StepId, fact: StepCompletedFact | StepFailedFact): Promise<void>;
|
|
317
470
|
recordOnce(runId: RunId, stepId: StepId, scope: string, value: Json): Promise<void>;
|
|
318
471
|
getOnce(runId: RunId, stepId: StepId, scope: string): Promise<Json | null>;
|
|
319
472
|
runStep<T extends Json>(runId: RunId, stepId: StepId, attempt: AttemptNumber, body: (tx: Tx) => Promise<{
|
|
@@ -336,9 +489,15 @@ interface Store {
|
|
|
336
489
|
listChildren(parentRunId: RunId): Promise<ReadonlyArray<RunId>>;
|
|
337
490
|
pruneFacts(opts: Required<PruneOpts>): Promise<PruneResult>;
|
|
338
491
|
}
|
|
492
|
+
interface StreamTransport {
|
|
493
|
+
subscribeStream(runId: RunId, stepId: StepId, opts?: {
|
|
494
|
+
readonly replayBuffered?: boolean;
|
|
495
|
+
}): AsyncIterable<StreamEvent<Json>>;
|
|
496
|
+
publishChunk(runId: RunId, stepId: StepId, chunk: Json): void;
|
|
497
|
+
}
|
|
339
498
|
interface QueryRunsWhere<FlowId extends string = string> {
|
|
340
499
|
readonly flowId?: FlowId;
|
|
341
|
-
readonly status?:
|
|
500
|
+
readonly status?: ReadonlyArray<RunStatus>;
|
|
342
501
|
readonly input?: Record<string, Json>;
|
|
343
502
|
}
|
|
344
503
|
type QueryRunsOpts<FlowId extends string = string> = {
|
|
@@ -398,12 +557,6 @@ interface Queue {
|
|
|
398
557
|
delayMs?: Millis;
|
|
399
558
|
}): Promise<void>;
|
|
400
559
|
extend(receipt: string, leaseMs: Millis): Promise<void>;
|
|
401
|
-
/**
|
|
402
|
-
* Optional one-shot, idempotent provisioning of the queue's backing schema.
|
|
403
|
-
* When present, `nagi()` awaits it once at construction (fail-fast), so a
|
|
404
|
-
* misconfigured queue surfaces at boot instead of on first enqueue. Adapters
|
|
405
|
-
* needing no provisioning (e.g. in-memory) omit it.
|
|
406
|
-
*/
|
|
407
560
|
ensureSchema?(): Promise<void>;
|
|
408
561
|
}
|
|
409
562
|
interface Clock {
|
|
@@ -414,7 +567,7 @@ interface Clock {
|
|
|
414
567
|
interface Trigger {
|
|
415
568
|
subscribe(handler: (runId: RunId) => void): () => void;
|
|
416
569
|
}
|
|
417
|
-
type FactKind = "flow.started" | "flow.completed" | "flow.failed" | "flow.canceled" | "step.started" | "step.completed" | "step.failed" | "step.canceled" | "step.retried" | "step.skipped" | "step.reset" | "step.abort-requested" | "signal.sent" | "signal.received" | "once.recorded" | "match.arm-selected";
|
|
570
|
+
type FactKind = "flow.started" | "flow.completed" | "flow.failed" | "flow.canceled" | "step.started" | "step.completed" | "step.failed" | "step.canceled" | "step.retried" | "step.skipped" | "step.reset" | "step.abort-requested" | "signal.sent" | "signal.received" | "signal.buffered" | "once.recorded" | "match.arm-selected";
|
|
418
571
|
interface FactBase {
|
|
419
572
|
readonly runId: RunId;
|
|
420
573
|
readonly at: Date;
|
|
@@ -425,7 +578,6 @@ interface FlowStartedFact extends FactBase {
|
|
|
425
578
|
readonly input: Json;
|
|
426
579
|
readonly flowHash?: string;
|
|
427
580
|
readonly codeVersion?: string;
|
|
428
|
-
/** Present when this run was started as a subflow child; absent for roots. */
|
|
429
581
|
readonly parent?: ParentLink;
|
|
430
582
|
}
|
|
431
583
|
interface FlowRefUpdatedFact {
|
|
@@ -464,10 +616,13 @@ interface FlowCanceledByOperatorFact extends FactBase {
|
|
|
464
616
|
readonly note?: string;
|
|
465
617
|
}
|
|
466
618
|
type FlowCanceledFact = FlowCanceledByConcurrencyFact | FlowCanceledExplicitlyFact | FlowCanceledByOperatorFact;
|
|
619
|
+
type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never;
|
|
620
|
+
type CancelArgs = DistributiveOmit<FlowCanceledExplicitlyFact | FlowCanceledByOperatorFact, "kind" | "runId" | "at">;
|
|
467
621
|
interface StepStartedFact extends FactBase {
|
|
468
622
|
readonly kind: "step.started";
|
|
469
623
|
readonly stepId: StepId;
|
|
470
624
|
readonly attempt: AttemptNumber;
|
|
625
|
+
readonly stepKind: StepKind;
|
|
471
626
|
}
|
|
472
627
|
interface StepCompletedFact extends FactBase {
|
|
473
628
|
readonly kind: "step.completed";
|
|
@@ -492,6 +647,7 @@ interface StepRetriedFact extends FactBase {
|
|
|
492
647
|
readonly stepId: StepId;
|
|
493
648
|
readonly attempt: AttemptNumber;
|
|
494
649
|
readonly nextAttemptAt: Date;
|
|
650
|
+
readonly error: SerializedError;
|
|
495
651
|
}
|
|
496
652
|
interface StepSkippedFact extends FactBase {
|
|
497
653
|
readonly kind: "step.skipped";
|
|
@@ -499,7 +655,6 @@ interface StepSkippedFact extends FactBase {
|
|
|
499
655
|
readonly reason: "when-false" | "transitive" | "manual";
|
|
500
656
|
readonly actor?: string;
|
|
501
657
|
readonly note?: string;
|
|
502
|
-
readonly cascade?: "skip" | "continue";
|
|
503
658
|
}
|
|
504
659
|
interface SignalSentFact extends FactBase {
|
|
505
660
|
readonly kind: "signal.sent";
|
|
@@ -512,6 +667,12 @@ interface SignalReceivedFact extends FactBase {
|
|
|
512
667
|
readonly payload: Json;
|
|
513
668
|
readonly signalName?: string;
|
|
514
669
|
}
|
|
670
|
+
interface SignalBufferedFact extends FactBase {
|
|
671
|
+
readonly kind: "signal.buffered";
|
|
672
|
+
readonly stepId: StepId;
|
|
673
|
+
readonly payload: Json;
|
|
674
|
+
readonly signalName?: string;
|
|
675
|
+
}
|
|
515
676
|
interface OnceRecordedFact extends FactBase {
|
|
516
677
|
readonly kind: "once.recorded";
|
|
517
678
|
readonly stepId: StepId;
|
|
@@ -537,25 +698,10 @@ interface StepAbortRequestedFact extends FactBase {
|
|
|
537
698
|
readonly actor: string;
|
|
538
699
|
readonly note?: string;
|
|
539
700
|
}
|
|
540
|
-
type Fact = FlowStartedFact | FlowCompletedFact | FlowFailedFact | FlowCanceledFact | StepStartedFact | StepCompletedFact | StepFailedFact | StepCanceledFact | StepRetriedFact | StepSkippedFact | StepResetFact | StepAbortRequestedFact | SignalSentFact | SignalReceivedFact | OnceRecordedFact | MatchArmSelectedFact;
|
|
701
|
+
type Fact = FlowStartedFact | FlowCompletedFact | FlowFailedFact | FlowCanceledFact | StepStartedFact | StepCompletedFact | StepFailedFact | StepCanceledFact | StepRetriedFact | StepSkippedFact | StepResetFact | StepAbortRequestedFact | SignalSentFact | SignalReceivedFact | SignalBufferedFact | OnceRecordedFact | MatchArmSelectedFact;
|
|
541
702
|
type RunStatus = "pending" | "running" | "completed" | "failed" | "canceled";
|
|
542
703
|
type StepStatus = "pending" | "running" | "completed" | "failed" | "canceled" | "skipped";
|
|
543
|
-
|
|
544
|
-
readonly stepId: StepId;
|
|
545
|
-
readonly status: StepStatus;
|
|
546
|
-
readonly attempts: AttemptNumber;
|
|
547
|
-
readonly output?: Json;
|
|
548
|
-
readonly error?: SerializedError;
|
|
549
|
-
}
|
|
550
|
-
interface RunState {
|
|
551
|
-
readonly runId: RunId;
|
|
552
|
-
readonly flowId: string;
|
|
553
|
-
readonly status: RunStatus;
|
|
554
|
-
readonly steps: Readonly<Record<StepId, StepState>>;
|
|
555
|
-
readonly facts: ReadonlyArray<Fact>;
|
|
556
|
-
readonly flowHash?: string;
|
|
557
|
-
readonly codeVersion?: string;
|
|
558
|
-
}
|
|
704
|
+
|
|
559
705
|
type ReplayMode = "inspect" | "continue";
|
|
560
706
|
interface ReplayOpts {
|
|
561
707
|
readonly mode: ReplayMode;
|
|
@@ -567,16 +713,14 @@ interface OperatorAuditOpts {
|
|
|
567
713
|
readonly actor: string;
|
|
568
714
|
readonly note?: string;
|
|
569
715
|
}
|
|
570
|
-
interface OperatorSkipOpts extends OperatorAuditOpts {
|
|
571
|
-
readonly cascade?: "skip" | "continue";
|
|
572
|
-
}
|
|
573
716
|
interface Operator {
|
|
574
|
-
skip(runId: RunId, stepId: StepId, opts:
|
|
717
|
+
skip(runId: RunId, stepId: StepId, opts: OperatorAuditOpts): Promise<void>;
|
|
575
718
|
retry(runId: RunId, stepId: StepId, opts: OperatorAuditOpts): Promise<void>;
|
|
576
719
|
abort(runId: RunId, opts: OperatorAuditOpts): Promise<void>;
|
|
577
720
|
}
|
|
578
721
|
|
|
579
722
|
declare function flow<const Id extends string, InputSchema extends StandardSchemaV1, R extends StepMap, Output = unknown>(config: FlowConfig<Id, InputSchema, R, Output>): Flow<Id, InputSchema, R, Output>;
|
|
723
|
+
declare function optional<S extends Step>(step: S): Optional<S>;
|
|
580
724
|
|
|
581
725
|
interface CanonicalRetryPolicy {
|
|
582
726
|
readonly maxAttempts: number;
|
|
@@ -624,7 +768,7 @@ interface SnapshotChangedEdge {
|
|
|
624
768
|
readonly before: "needed" | "absent";
|
|
625
769
|
readonly after: "needed" | "absent";
|
|
626
770
|
}
|
|
627
|
-
interface
|
|
771
|
+
interface SnapshotChangedPredicate {
|
|
628
772
|
readonly stepId: StepId;
|
|
629
773
|
readonly field: SnapshotChangedField;
|
|
630
774
|
}
|
|
@@ -632,16 +776,43 @@ interface SnapshotDiff {
|
|
|
632
776
|
readonly addedSteps: readonly StepId[];
|
|
633
777
|
readonly removedSteps: readonly StepId[];
|
|
634
778
|
readonly changedEdges: readonly SnapshotChangedEdge[];
|
|
635
|
-
readonly changedPredicates: readonly
|
|
779
|
+
readonly changedPredicates: readonly SnapshotChangedPredicate[];
|
|
636
780
|
}
|
|
637
781
|
declare function diffSnapshots(before: CanonicalDag, after: CanonicalDag): SnapshotDiff;
|
|
638
782
|
|
|
783
|
+
declare class NagiValidationError extends Error {
|
|
784
|
+
readonly issues: ReadonlyArray<StandardSchemaV1.Issue>;
|
|
785
|
+
constructor(issues: ReadonlyArray<StandardSchemaV1.Issue>);
|
|
786
|
+
}
|
|
787
|
+
declare class NagiRuntimeError extends Error {
|
|
788
|
+
constructor(message: string);
|
|
789
|
+
}
|
|
790
|
+
declare class NagiCanceledError extends Error {
|
|
791
|
+
readonly runId: RunId;
|
|
792
|
+
readonly canceledByRunId: RunId;
|
|
793
|
+
readonly concurrencyKey: string;
|
|
794
|
+
constructor({ runId, canceledByRunId, concurrencyKey, }: {
|
|
795
|
+
readonly runId: RunId;
|
|
796
|
+
readonly canceledByRunId: RunId;
|
|
797
|
+
readonly concurrencyKey: string;
|
|
798
|
+
});
|
|
799
|
+
}
|
|
800
|
+
declare class NagiSnapshotDriftError extends Error {
|
|
801
|
+
readonly runId: RunId;
|
|
802
|
+
readonly expected: string;
|
|
803
|
+
readonly actual: string;
|
|
804
|
+
constructor({ runId, expected, actual, }: {
|
|
805
|
+
readonly runId: RunId;
|
|
806
|
+
readonly expected: string;
|
|
807
|
+
readonly actual: string;
|
|
808
|
+
});
|
|
809
|
+
}
|
|
810
|
+
|
|
639
811
|
interface InMemoryStoreOpts {
|
|
640
812
|
readonly leaseMs?: Millis;
|
|
641
813
|
}
|
|
642
|
-
declare class InMemoryStore implements Store {
|
|
814
|
+
declare class InMemoryStore implements Store, StreamTransport {
|
|
643
815
|
private readonly facts;
|
|
644
|
-
private readonly outputs;
|
|
645
816
|
private readonly onces;
|
|
646
817
|
private readonly leases;
|
|
647
818
|
private readonly snapshots;
|
|
@@ -651,6 +822,7 @@ declare class InMemoryStore implements Store {
|
|
|
651
822
|
private readonly keyByActiveRun;
|
|
652
823
|
private readonly childrenByParent;
|
|
653
824
|
private readonly summaries;
|
|
825
|
+
private readonly streamHub;
|
|
654
826
|
private readonly leaseMs;
|
|
655
827
|
constructor(opts?: InMemoryStoreOpts);
|
|
656
828
|
appendFact(runId: RunId, fact: Fact): Promise<void>;
|
|
@@ -667,12 +839,19 @@ declare class InMemoryStore implements Store {
|
|
|
667
839
|
listChildren(parentRunId: RunId): Promise<ReadonlyArray<RunId>>;
|
|
668
840
|
loadRunState(runId: RunId): Promise<RunState>;
|
|
669
841
|
claimStep(runId: RunId, stepId: StepId, attempt: AttemptNumber): Promise<ClaimToken | null>;
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
842
|
+
settleStep(runId: RunId, _stepId: StepId, fact: StepCompletedFact | StepFailedFact): Promise<void>;
|
|
843
|
+
settleSignal(args: {
|
|
844
|
+
readonly runId: RunId;
|
|
845
|
+
readonly stepId: StepId;
|
|
846
|
+
readonly at: Date;
|
|
847
|
+
readonly incoming?: {
|
|
848
|
+
readonly payload: Json;
|
|
849
|
+
readonly signalName?: string;
|
|
850
|
+
};
|
|
851
|
+
}): Promise<SettleSignalResult>;
|
|
673
852
|
recordOnce(runId: RunId, stepId: StepId, scope: string, value: Json): Promise<void>;
|
|
674
853
|
getOnce(runId: RunId, stepId: StepId, scope: string): Promise<Json | null>;
|
|
675
|
-
runStep<T extends Json>(runId: RunId,
|
|
854
|
+
runStep<T extends Json>(runId: RunId, _stepId: StepId, _attempt: AttemptNumber, body: (tx: Tx) => Promise<{
|
|
676
855
|
readonly output: T;
|
|
677
856
|
readonly fact: StepCompletedFact | StepFailedFact | StepCanceledFact;
|
|
678
857
|
}>): Promise<T>;
|
|
@@ -691,8 +870,12 @@ declare class InMemoryStore implements Store {
|
|
|
691
870
|
readGlobalFacts(): ReadonlyArray<GlobalFact>;
|
|
692
871
|
queryRuns(opts: QueryRunsOpts): Promise<QueryRunsResult>;
|
|
693
872
|
pruneFacts(opts: Required<PruneOpts>): Promise<PruneResult>;
|
|
873
|
+
subscribeStream(runId: RunId, stepId: StepId, opts?: {
|
|
874
|
+
readonly replayBuffered?: boolean;
|
|
875
|
+
}): AsyncIterable<StreamEvent<Json>>;
|
|
876
|
+
publishChunk(runId: RunId, stepId: StepId, chunk: Json): void;
|
|
694
877
|
}
|
|
695
|
-
|
|
878
|
+
|
|
696
879
|
interface InMemoryQueueOpts {
|
|
697
880
|
readonly leaseMs?: Millis;
|
|
698
881
|
}
|
|
@@ -730,11 +913,12 @@ declare class InMemoryTrigger implements Trigger {
|
|
|
730
913
|
interface NagiConfig {
|
|
731
914
|
readonly flows: ReadonlyArray<Flow>;
|
|
732
915
|
readonly store: Store;
|
|
916
|
+
readonly streamTransport?: StreamTransport;
|
|
733
917
|
readonly queue: Queue;
|
|
734
918
|
readonly clock?: Clock;
|
|
735
919
|
readonly trigger?: Trigger;
|
|
736
920
|
readonly hooks?: FlowHooks;
|
|
737
|
-
readonly
|
|
921
|
+
readonly onLog?: (entry: LogEntry) => void;
|
|
738
922
|
readonly defaultRetry?: RetryPolicy;
|
|
739
923
|
readonly codeVersion?: string;
|
|
740
924
|
}
|
|
@@ -746,72 +930,25 @@ interface CancelOpts {
|
|
|
746
930
|
}
|
|
747
931
|
interface Wf<TFlows extends ReadonlyArray<Flow> = ReadonlyArray<Flow>> {
|
|
748
932
|
start<F extends TFlows[number]>(flow: F, input: FlowInput<F>, opts?: StartOpts): Promise<RunId>;
|
|
749
|
-
/**
|
|
750
|
-
* Start a flow by id with a runtime-typed input. Intended for callers
|
|
751
|
-
* holding a serialized payload (transactional-outbox reconcilers, queue
|
|
752
|
-
* consumers replaying DLQs, admin CLIs replaying a runId): the input
|
|
753
|
-
* is validated against the registered flow's schema before the run is
|
|
754
|
-
* created, mirroring `start`'s runtime contract without requiring a
|
|
755
|
-
* compile-time-typed input.
|
|
756
|
-
*
|
|
757
|
-
* Throws `NagiRuntimeError` when `flowId` is not registered with
|
|
758
|
-
* `nagi()`, and `NagiValidationError` when the input fails the flow's
|
|
759
|
-
* schema or when `opts.runId` is invalid.
|
|
760
|
-
*/
|
|
761
933
|
startById(flowId: string, input: unknown, opts?: StartOpts): Promise<RunId>;
|
|
762
934
|
signal(runId: RunId, stepName: string, payload: unknown): Promise<void>;
|
|
763
935
|
cancel(runId: RunId, opts?: CancelOpts): Promise<void>;
|
|
764
936
|
worker(config?: WorkerConfig): Worker;
|
|
765
937
|
replay(runId: RunId, opts?: ReplayOpts): Promise<void>;
|
|
766
938
|
queryRuns(opts?: QueryRunsOpts<FlowIdOf<TFlows>>): Promise<QueryRunsResult<FlowIdOf<TFlows>>>;
|
|
939
|
+
subscribe<C = Json>(runId: RunId, stepId: StepId, opts?: {
|
|
940
|
+
readonly replayBuffered?: boolean;
|
|
941
|
+
}): AsyncIterable<StreamEvent<C>>;
|
|
767
942
|
operator(): Operator;
|
|
768
943
|
pruneFacts(opts: PruneOpts): Promise<PruneResult>;
|
|
769
944
|
}
|
|
770
|
-
declare class NagiValidationError extends Error {
|
|
771
|
-
readonly issues: ReadonlyArray<StandardSchemaV1.Issue>;
|
|
772
|
-
constructor(issues: ReadonlyArray<StandardSchemaV1.Issue>);
|
|
773
|
-
}
|
|
774
|
-
declare class NagiRuntimeError extends Error {
|
|
775
|
-
constructor(message: string);
|
|
776
|
-
}
|
|
777
|
-
declare class NagiCanceledError extends Error {
|
|
778
|
-
readonly runId: RunId;
|
|
779
|
-
readonly canceledByRunId: RunId;
|
|
780
|
-
readonly concurrencyKey: string;
|
|
781
|
-
constructor(args: {
|
|
782
|
-
readonly runId: RunId;
|
|
783
|
-
readonly canceledByRunId: RunId;
|
|
784
|
-
readonly concurrencyKey: string;
|
|
785
|
-
});
|
|
786
|
-
}
|
|
787
|
-
declare class NagiSnapshotDriftError extends Error {
|
|
788
|
-
readonly runId: RunId;
|
|
789
|
-
readonly expected: string;
|
|
790
|
-
readonly actual: string;
|
|
791
|
-
constructor(args: {
|
|
792
|
-
readonly runId: RunId;
|
|
793
|
-
readonly expected: string;
|
|
794
|
-
readonly actual: string;
|
|
795
|
-
});
|
|
796
|
-
}
|
|
797
945
|
declare function nagiImpl<const TFlows extends ReadonlyArray<Flow>>(config: NagiConfig & {
|
|
798
946
|
flows: TFlows;
|
|
799
947
|
}): Promise<Wf<TFlows>>;
|
|
800
|
-
/**
|
|
801
|
-
* Config for {@link nagi.run}: a `NagiConfig` plus optional worker tuning and an
|
|
802
|
-
* external shutdown signal. The signal is merged with an internal controller, so
|
|
803
|
-
* aborting either source drains the worker.
|
|
804
|
-
*/
|
|
805
948
|
interface NagiRunConfig extends NagiConfig {
|
|
806
949
|
readonly worker?: Omit<WorkerConfig, "signal">;
|
|
807
950
|
readonly signal?: AbortSignal;
|
|
808
951
|
}
|
|
809
|
-
/**
|
|
810
|
-
* Handle returned by {@link nagi.run}. `stop()` aborts the internal controller,
|
|
811
|
-
* awaits the worker loop, and is idempotent (safe to call twice or concurrently).
|
|
812
|
-
* It resolves cleanly even if the loop crashed — a true crash is logged once via
|
|
813
|
-
* the configured `logger`.
|
|
814
|
-
*/
|
|
815
952
|
interface RuntimeHandle<TFlows extends ReadonlyArray<Flow> = ReadonlyArray<Flow>> {
|
|
816
953
|
readonly wf: Wf<TFlows>;
|
|
817
954
|
stop(): Promise<void>;
|
|
@@ -819,12 +956,32 @@ interface RuntimeHandle<TFlows extends ReadonlyArray<Flow> = ReadonlyArray<Flow>
|
|
|
819
956
|
declare function nagiRun<const TFlows extends ReadonlyArray<Flow>>(config: NagiRunConfig & {
|
|
820
957
|
flows: TFlows;
|
|
821
958
|
}): Promise<RuntimeHandle<TFlows>>;
|
|
822
|
-
/**
|
|
823
|
-
* The nagi runtime factory. Call `nagi(config)` for fine-grained control, or
|
|
824
|
-
* `nagi.run(config)` for a turnkey worker lifecycle returning `{ wf, stop }`.
|
|
825
|
-
*/
|
|
826
959
|
declare const nagi: typeof nagiImpl & {
|
|
827
960
|
run: typeof nagiRun;
|
|
828
961
|
};
|
|
829
962
|
|
|
830
|
-
|
|
963
|
+
type SignalDecision = {
|
|
964
|
+
readonly kind: "noop";
|
|
965
|
+
readonly result: SettleSignalResult;
|
|
966
|
+
} | {
|
|
967
|
+
readonly kind: "buffer";
|
|
968
|
+
readonly fact: SignalBufferedFact | null;
|
|
969
|
+
readonly result: SettleSignalResult;
|
|
970
|
+
} | {
|
|
971
|
+
readonly kind: "deliver";
|
|
972
|
+
readonly attempt: AttemptNumber;
|
|
973
|
+
readonly received: SignalReceivedFact;
|
|
974
|
+
readonly completed: StepCompletedFact;
|
|
975
|
+
readonly result: SettleSignalResult;
|
|
976
|
+
};
|
|
977
|
+
declare function decideSignal(args: {
|
|
978
|
+
readonly runState: RunState;
|
|
979
|
+
readonly stepId: StepId;
|
|
980
|
+
readonly at: Date;
|
|
981
|
+
readonly incoming?: {
|
|
982
|
+
readonly payload: Json;
|
|
983
|
+
readonly signalName?: string;
|
|
984
|
+
} | undefined;
|
|
985
|
+
}): SignalDecision;
|
|
986
|
+
|
|
987
|
+
export { type Anomaly, type AttemptNumber, type BackoffStrategy, type Builder, type CancelArgs, type CanonicalDag, type CanonicalMatchArm, type CanonicalRetryPolicy, type CanonicalSchema, type CanonicalStep, type ClaimToken, type Clock, type ConcurrencyMode, type Fact, type FactKind, type Flow, type FlowCanceledByConcurrencyFact, type FlowCanceledByOperatorFact, type FlowCanceledExplicitlyFact, type FlowCanceledFact, type FlowCompleteEvent, type FlowCompletedFact, type FlowConcurrency, type FlowConfig, type FlowErrorEvent, type FlowEvent, type FlowFailedFact, type FlowHooks, type FlowIdOf, type FlowInput, type FlowOutput, type FlowRefUpdatedFact, type FlowStartEvent, type FlowStartedFact, type GlobalFact, InMemoryClock, InMemoryQueue, InMemoryStore, InMemoryTrigger, type InferSchemaInput, type InferSchemaOutput, type Json, type LogEntry, type LogLevel, type Logger, type MatchArm, type MatchArmGuard, type MatchArmOtherwise, type MatchArmOutput, type MatchArmSelectedFact, type MatchArmShape, type MatchGuardConfig, type Millis, NagiCanceledError, type NagiConfig, type NagiRunConfig, NagiRuntimeError, NagiSnapshotDriftError, NagiValidationError, type NeedRef, type NeedsMap, type NeedsOutputs, type OnceRecordedFact, type Operator, type OperatorAuditOpts, type Optional, type ParentLink, type ParentRef, type PrunableStatus, type PruneOpts, type PruneResult, type QueryRunsOpts, type QueryRunsResult, type QueryRunsWhere, type Queue, type QueueDequeueOpts, type QueueEnqueueOpts, type QueueMessage, type Register, type ReplayMode, type ReplayOpts, type Resolved, type ResolvedConcurrency, type ResolvedNeeds, type RetryPolicy, type RunCancelCause, type RunId, type RunPhase, type RunState, type RunStatus, type RunSummary, type RuntimeHandle, type SerializedError, type SettleSignalResult, type SignalBufferedFact, type SignalConfig, type SignalDecision, type SignalReceivedEvent, type SignalReceivedFact, type SignalSentEvent, type SignalSentFact, type SkipReason, type SnapshotChangedEdge, type SnapshotChangedField, type SnapshotDiff, StandardSchemaV1, type StartOpts, type Step, type StepAbortRequestedFact, type StepCancelCause, type StepCanceledFact, type StepCompleteEvent, type StepCompletedFact, type StepCtx, type StepErrorEvent, type StepEvent, type StepFailedFact, type StepId, type StepKind, type StepMap, type StepOutput, type StepResetFact, type StepRetriedFact, type StepRetryEvent, type StepSkippedFact, type StepStartEvent, type StepStartedFact, type StepState, type StepStatus, type Store, type StreamEvent, type StreamTransport, type StreamingStepCtx, type StreamingTaskConfig, type StringKeyOf, type SubflowConfig, type SubflowStepOutput, type TaskConfig, type Trigger, type Tx, type Wf, type Worker, type WorkerConfig, type WorkerRunOnceOpts, type WorkerRunResult, type WorkerRunUntilEmptyOpts, attemptOf, canonicalize, decideSignal, diffSnapshots, errorOf, fingerprintFlows, flow, isStepTerminal, isTerminalRun, nagi, optional, outputOf, foldRun as projectRunState, resolvedOf, runStatusOf, sha256Canonical, stepStateOf, stepStatusOf, unwrap };
|