@nagi-js/core 0.1.1-rc.12 → 0.1.1-rc.13
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 +13 -1
- package/dist/index.js +146 -6
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -154,7 +154,7 @@ interface Register {
|
|
|
154
154
|
type Tx = Register extends {
|
|
155
155
|
tx: infer T;
|
|
156
156
|
} ? T : unknown;
|
|
157
|
-
type StepKind = "task" | "signal" | "match" | "subflow" | "streaming";
|
|
157
|
+
type StepKind = "task" | "activity" | "signal" | "match" | "subflow" | "streaming";
|
|
158
158
|
interface Step<Output = unknown> {
|
|
159
159
|
readonly kind: StepKind;
|
|
160
160
|
readonly id: StepId;
|
|
@@ -197,6 +197,7 @@ interface StepCtx<Input = unknown> {
|
|
|
197
197
|
once<T extends Json>(scope: string, fn: () => Promise<T>): Promise<T>;
|
|
198
198
|
idempotencyKey(scope: string): string;
|
|
199
199
|
}
|
|
200
|
+
type ActivityCtx<Input = unknown> = Omit<StepCtx<Input>, "tx">;
|
|
200
201
|
type StreamEvent<C = Json> = {
|
|
201
202
|
readonly kind: "chunk";
|
|
202
203
|
readonly chunk: C;
|
|
@@ -245,6 +246,14 @@ interface TaskConfig<Input, N extends NeedsMap, Output> extends StepConfigBase<I
|
|
|
245
246
|
readonly ctx: StepCtx<NoInfer<Input>>;
|
|
246
247
|
}) => Promise<Output>;
|
|
247
248
|
}
|
|
249
|
+
interface ActivityConfig<Input, N extends NeedsMap, Output> extends StepConfigBase<Input, N>, StepLifecycleHooks<Output> {
|
|
250
|
+
readonly retry?: RetryPolicy;
|
|
251
|
+
readonly run: (args: {
|
|
252
|
+
readonly input: NoInfer<Input>;
|
|
253
|
+
readonly needs: NoInfer<ResolvedNeeds<N>>;
|
|
254
|
+
readonly ctx: ActivityCtx<NoInfer<Input>>;
|
|
255
|
+
}) => Promise<Output>;
|
|
256
|
+
}
|
|
248
257
|
interface StreamingTaskConfig<Input, N extends NeedsMap, Output, Chunk> extends StepConfigBase<Input, N>, StepLifecycleHooks<Output> {
|
|
249
258
|
readonly retry?: RetryPolicy;
|
|
250
259
|
readonly run: (args: {
|
|
@@ -302,6 +311,7 @@ interface SubflowStepOutput<ChildOutput> {
|
|
|
302
311
|
}
|
|
303
312
|
interface Builder<Input = unknown> {
|
|
304
313
|
task<N extends NeedsMap, Output>(config: TaskConfig<Input, N, Output>): Step<Output>;
|
|
314
|
+
activity<N extends NeedsMap, Output>(config: ActivityConfig<Input, N, Output>): Step<Output>;
|
|
305
315
|
streamingTask<N extends NeedsMap, O, C = Json>(config: StreamingTaskConfig<Input, N, O, C>): Step<O>;
|
|
306
316
|
signal<N extends NeedsMap, S extends StandardSchemaV1>(config: SignalConfig<Input, N, S>): Step<InferSchemaOutput<S>>;
|
|
307
317
|
subflow<N extends NeedsMap, Child extends Flow>(child: Child, config: SubflowConfig<Input, N, Child>): Step<SubflowStepOutput<FlowOutput<Child>>>;
|
|
@@ -921,6 +931,8 @@ interface NagiConfig {
|
|
|
921
931
|
readonly onLog?: (entry: LogEntry) => void;
|
|
922
932
|
readonly defaultRetry?: RetryPolicy;
|
|
923
933
|
readonly codeVersion?: string;
|
|
934
|
+
readonly heartbeatIntervalMs?: Millis;
|
|
935
|
+
readonly heartbeatLeaseMs?: Millis;
|
|
924
936
|
}
|
|
925
937
|
interface StartOpts {
|
|
926
938
|
readonly runId?: RunId;
|
package/dist/index.js
CHANGED
|
@@ -81,6 +81,7 @@ function startTarget(stepKind, attempt) {
|
|
|
81
81
|
case "subflow":
|
|
82
82
|
return { tag: "awaitingChild", attempt };
|
|
83
83
|
case "task":
|
|
84
|
+
case "activity":
|
|
84
85
|
case "streaming":
|
|
85
86
|
case "match":
|
|
86
87
|
return { tag: "running", attempt };
|
|
@@ -292,7 +293,7 @@ function peekDef(step) {
|
|
|
292
293
|
return step[DEF];
|
|
293
294
|
}
|
|
294
295
|
function handlerDef(def) {
|
|
295
|
-
return def.kind === "task" || def.kind === "streaming" ? def : void 0;
|
|
296
|
+
return def.kind === "task" || def.kind === "activity" || def.kind === "streaming" ? def : void 0;
|
|
296
297
|
}
|
|
297
298
|
function needsStepIds(def) {
|
|
298
299
|
return Object.values(def.needs).map((ref) => ref.step.id);
|
|
@@ -337,6 +338,23 @@ function makeBuilder() {
|
|
|
337
338
|
};
|
|
338
339
|
return attachDef({ kind: "task", id: "" }, def);
|
|
339
340
|
}
|
|
341
|
+
function activity(config) {
|
|
342
|
+
const def = {
|
|
343
|
+
kind: "activity",
|
|
344
|
+
needs: normalizeNeeds(config.needs),
|
|
345
|
+
run: config.run,
|
|
346
|
+
...compact({
|
|
347
|
+
retry: config.retry,
|
|
348
|
+
timeoutMs: config.timeoutMs,
|
|
349
|
+
when: config.when,
|
|
350
|
+
onStart: config.onStart,
|
|
351
|
+
onComplete: config.onComplete,
|
|
352
|
+
onError: config.onError,
|
|
353
|
+
onRetry: config.onRetry
|
|
354
|
+
})
|
|
355
|
+
};
|
|
356
|
+
return attachDef({ kind: "activity", id: "" }, def);
|
|
357
|
+
}
|
|
340
358
|
function streamingTask(config) {
|
|
341
359
|
const def = {
|
|
342
360
|
kind: "streaming",
|
|
@@ -398,6 +416,7 @@ function makeBuilder() {
|
|
|
398
416
|
}
|
|
399
417
|
return {
|
|
400
418
|
task,
|
|
419
|
+
activity,
|
|
401
420
|
streamingTask,
|
|
402
421
|
signal,
|
|
403
422
|
subflow,
|
|
@@ -585,7 +604,7 @@ async function canonicalize(flow2) {
|
|
|
585
604
|
async function canonicalizeStep(id, def) {
|
|
586
605
|
const needs = [...needsStepIds(def)].sort();
|
|
587
606
|
const base = { id, kind: def.kind, needs };
|
|
588
|
-
if (def.kind === "task" || def.kind === "streaming")
|
|
607
|
+
if (def.kind === "task" || def.kind === "activity" || def.kind === "streaming")
|
|
589
608
|
return canonicalizeTask(base, def);
|
|
590
609
|
if (def.kind === "signal") return canonicalizeSignal(base, def);
|
|
591
610
|
if (def.kind === "subflow") return canonicalizeSubflow(base, def);
|
|
@@ -2033,6 +2052,39 @@ function startCancelWatcher(args) {
|
|
|
2033
2052
|
}
|
|
2034
2053
|
};
|
|
2035
2054
|
}
|
|
2055
|
+
var DEFAULT_HEARTBEAT_LEASE_MS = 12e4;
|
|
2056
|
+
var DEFAULT_HEARTBEAT_INTERVAL_MS = 4e4;
|
|
2057
|
+
function startHeartbeat(args) {
|
|
2058
|
+
const { queue, receipt, intervalMs, leaseMs, emitLog } = args;
|
|
2059
|
+
let stopped = false;
|
|
2060
|
+
let timer;
|
|
2061
|
+
const schedule = () => {
|
|
2062
|
+
timer = setTimeout(() => {
|
|
2063
|
+
void (async () => {
|
|
2064
|
+
try {
|
|
2065
|
+
await queue.extend(receipt, leaseMs);
|
|
2066
|
+
} catch (err) {
|
|
2067
|
+
emitLog({
|
|
2068
|
+
level: "warn",
|
|
2069
|
+
msg: "heartbeat: failed to extend message visibility",
|
|
2070
|
+
attrs: { receipt, error: String(err) }
|
|
2071
|
+
});
|
|
2072
|
+
}
|
|
2073
|
+
if (!stopped) schedule();
|
|
2074
|
+
})();
|
|
2075
|
+
}, intervalMs);
|
|
2076
|
+
};
|
|
2077
|
+
schedule();
|
|
2078
|
+
return {
|
|
2079
|
+
// Clear the pending timer so a settled step strands nothing on the event
|
|
2080
|
+
// loop — without this every fast step would leak a timer for up to
|
|
2081
|
+
// intervalMs, keeping the process alive and delaying clean shutdown.
|
|
2082
|
+
stop: () => {
|
|
2083
|
+
stopped = true;
|
|
2084
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
2085
|
+
}
|
|
2086
|
+
};
|
|
2087
|
+
}
|
|
2036
2088
|
function classifyFailure(args) {
|
|
2037
2089
|
const { attempt, policy, err, runIsCanceled, stepAborted } = args;
|
|
2038
2090
|
if (runIsCanceled || stepAborted) {
|
|
@@ -2064,8 +2116,8 @@ function retryAllows(policy, err) {
|
|
|
2064
2116
|
if (!policy.retryOn) return true;
|
|
2065
2117
|
return policy.retryOn(err);
|
|
2066
2118
|
}
|
|
2067
|
-
function
|
|
2068
|
-
const { runId, stepId, attempt, input, store, clock,
|
|
2119
|
+
function makeBaseCtx(args) {
|
|
2120
|
+
const { runId, stepId, attempt, input, store, clock, signal, emitLog } = args;
|
|
2069
2121
|
const log = (level) => (msg, attrs) => emitLog({ level, msg, attrs: { ...attrs, runId, stepId, attempt } });
|
|
2070
2122
|
const logger = {
|
|
2071
2123
|
debug: log("debug"),
|
|
@@ -2080,12 +2132,25 @@ function makeStepCtx(args) {
|
|
|
2080
2132
|
attempt,
|
|
2081
2133
|
signal,
|
|
2082
2134
|
now: () => clock.now(),
|
|
2083
|
-
tx,
|
|
2084
2135
|
logger,
|
|
2085
2136
|
once: makeOnce({ runId, stepId, store }),
|
|
2086
2137
|
idempotencyKey: makeIdempotencyKey(runId, stepId)
|
|
2087
2138
|
};
|
|
2088
2139
|
}
|
|
2140
|
+
function makeStepCtx(args) {
|
|
2141
|
+
const { tx, ...base } = args;
|
|
2142
|
+
return { ...makeBaseCtx(base), tx };
|
|
2143
|
+
}
|
|
2144
|
+
function makeActivityCtx(args) {
|
|
2145
|
+
return {
|
|
2146
|
+
...makeBaseCtx(args),
|
|
2147
|
+
get tx() {
|
|
2148
|
+
throw new Error(
|
|
2149
|
+
"nagi: activity steps have no ctx.tx \u2014 they run outside the durable transaction. Write idempotently via your own client, or use a task."
|
|
2150
|
+
);
|
|
2151
|
+
}
|
|
2152
|
+
};
|
|
2153
|
+
}
|
|
2089
2154
|
|
|
2090
2155
|
// src/exec/message.ts
|
|
2091
2156
|
function makeMessage(deps, hooks, progression) {
|
|
@@ -2102,11 +2167,20 @@ function makeMessage(deps, hooks, progression) {
|
|
|
2102
2167
|
const { def, state } = admission;
|
|
2103
2168
|
await recordStarted({ flow: flow2, message, def, state });
|
|
2104
2169
|
const startedAt = Date.now();
|
|
2170
|
+
const heartbeat = startHeartbeat({
|
|
2171
|
+
queue,
|
|
2172
|
+
receipt: message.receipt,
|
|
2173
|
+
intervalMs: deps.heartbeat.intervalMs,
|
|
2174
|
+
leaseMs: deps.heartbeat.leaseMs,
|
|
2175
|
+
emitLog: deps.emitLog
|
|
2176
|
+
});
|
|
2105
2177
|
let outcome;
|
|
2106
2178
|
try {
|
|
2107
2179
|
outcome = await execute({ flow: flow2, message, def, state });
|
|
2108
2180
|
} catch (err) {
|
|
2109
2181
|
outcome = await handleStepError({ flow: flow2, message, def, err });
|
|
2182
|
+
} finally {
|
|
2183
|
+
heartbeat.stop();
|
|
2110
2184
|
}
|
|
2111
2185
|
await queue.ack(message.receipt);
|
|
2112
2186
|
await interpret({ flow: flow2, message, def, startedAt, outcome });
|
|
@@ -2142,7 +2216,7 @@ function makeMessage(deps, hooks, progression) {
|
|
|
2142
2216
|
runId,
|
|
2143
2217
|
Facts.stepStarted(runId, stepId, attempt, def.kind, clock.now())
|
|
2144
2218
|
);
|
|
2145
|
-
const startCarriesInput = def.kind === "task" || def.kind === "streaming" || def.kind === "subflow";
|
|
2219
|
+
const startCarriesInput = def.kind === "task" || def.kind === "activity" || def.kind === "streaming" || def.kind === "subflow";
|
|
2146
2220
|
const input = startCarriesInput ? state.input : null;
|
|
2147
2221
|
await fireStepLifecycle(
|
|
2148
2222
|
handler?.onStart,
|
|
@@ -2174,6 +2248,16 @@ function makeMessage(deps, hooks, progression) {
|
|
|
2174
2248
|
});
|
|
2175
2249
|
return skipAdvance ? { tag: "parked" } : { tag: "completed", output };
|
|
2176
2250
|
}
|
|
2251
|
+
case "activity": {
|
|
2252
|
+
const { output, skipAdvance } = await executeActivity({
|
|
2253
|
+
def,
|
|
2254
|
+
runId,
|
|
2255
|
+
stepId,
|
|
2256
|
+
attempt,
|
|
2257
|
+
state
|
|
2258
|
+
});
|
|
2259
|
+
return skipAdvance ? { tag: "parked" } : { tag: "completed", output };
|
|
2260
|
+
}
|
|
2177
2261
|
case "signal": {
|
|
2178
2262
|
const settled = await deps.store.settleSignal({
|
|
2179
2263
|
runId,
|
|
@@ -2315,6 +2399,56 @@ function makeMessage(deps, hooks, progression) {
|
|
|
2315
2399
|
watcher.stop();
|
|
2316
2400
|
}
|
|
2317
2401
|
}
|
|
2402
|
+
async function executeActivity(args) {
|
|
2403
|
+
const { def, runId, stepId, attempt, state } = args;
|
|
2404
|
+
const { store, clock } = deps;
|
|
2405
|
+
const input = state.input;
|
|
2406
|
+
const needs = resolveNeeds(def, (id) => resolvedOf(stepStateOf(state, id)));
|
|
2407
|
+
const ac = new AbortController();
|
|
2408
|
+
const watcher = startCancelWatcher({
|
|
2409
|
+
store,
|
|
2410
|
+
runId,
|
|
2411
|
+
stepId,
|
|
2412
|
+
attempt,
|
|
2413
|
+
ac,
|
|
2414
|
+
intervalMs: deps.cancelPollIntervalMs ?? CANCEL_POLL_INTERVAL_MS
|
|
2415
|
+
});
|
|
2416
|
+
try {
|
|
2417
|
+
const ctx = makeActivityCtx({
|
|
2418
|
+
runId,
|
|
2419
|
+
stepId,
|
|
2420
|
+
attempt,
|
|
2421
|
+
input,
|
|
2422
|
+
store,
|
|
2423
|
+
clock,
|
|
2424
|
+
signal: ac.signal,
|
|
2425
|
+
emitLog: deps.emitLog
|
|
2426
|
+
});
|
|
2427
|
+
const out = await runHandler(def, { input, needs, ctx, runId, stepId });
|
|
2428
|
+
let stepAbortedHere = false;
|
|
2429
|
+
const output = await store.runStep(
|
|
2430
|
+
runId,
|
|
2431
|
+
stepId,
|
|
2432
|
+
attempt,
|
|
2433
|
+
async () => {
|
|
2434
|
+
const postState = await store.loadRunState(runId);
|
|
2435
|
+
const { fact, abortedHere } = resolveExecutionFact({
|
|
2436
|
+
postState,
|
|
2437
|
+
runId,
|
|
2438
|
+
stepId,
|
|
2439
|
+
attempt,
|
|
2440
|
+
output: out,
|
|
2441
|
+
at: clock.now()
|
|
2442
|
+
});
|
|
2443
|
+
stepAbortedHere = abortedHere;
|
|
2444
|
+
return { output: out, fact };
|
|
2445
|
+
}
|
|
2446
|
+
);
|
|
2447
|
+
return { output, skipAdvance: stepAbortedHere };
|
|
2448
|
+
} finally {
|
|
2449
|
+
watcher.stop();
|
|
2450
|
+
}
|
|
2451
|
+
}
|
|
2318
2452
|
async function executeSubflow(args) {
|
|
2319
2453
|
const { def, runId, stepId, attempt, state } = args;
|
|
2320
2454
|
const child = deps.lookupFlow(def.childFlowId);
|
|
@@ -3175,6 +3309,12 @@ async function nagiImpl(config) {
|
|
|
3175
3309
|
queue: config.queue,
|
|
3176
3310
|
clock,
|
|
3177
3311
|
emitLog,
|
|
3312
|
+
// The one fork point for heartbeat tuning: collapse the public optionals to
|
|
3313
|
+
// a single required config here so the dispatch internals never re-check it.
|
|
3314
|
+
heartbeat: {
|
|
3315
|
+
intervalMs: config.heartbeatIntervalMs ?? DEFAULT_HEARTBEAT_INTERVAL_MS,
|
|
3316
|
+
leaseMs: config.heartbeatLeaseMs ?? DEFAULT_HEARTBEAT_LEASE_MS
|
|
3317
|
+
},
|
|
3178
3318
|
...compact({
|
|
3179
3319
|
hooks: config.hooks,
|
|
3180
3320
|
defaultRetry: config.defaultRetry,
|