@oni.bot/core 1.0.3 → 1.1.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.
- package/dist/checkpointers/postgres.d.ts.map +1 -1
- package/dist/checkpointers/postgres.js +2 -1
- package/dist/checkpointers/postgres.js.map +1 -1
- package/dist/cli/inspect.d.ts.map +1 -1
- package/dist/cli/inspect.js +4 -2
- package/dist/cli/inspect.js.map +1 -1
- package/dist/coordination/request-reply.d.ts +11 -2
- package/dist/coordination/request-reply.d.ts.map +1 -1
- package/dist/coordination/request-reply.js.map +1 -1
- package/dist/events/bus.d.ts.map +1 -1
- package/dist/events/bus.js +1 -0
- package/dist/events/bus.js.map +1 -1
- package/dist/graph.d.ts +11 -1
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +4 -2
- package/dist/graph.js.map +1 -1
- package/dist/harness/agent-loop.d.ts +1 -7
- package/dist/harness/agent-loop.d.ts.map +1 -1
- package/dist/harness/agent-loop.js +2 -642
- package/dist/harness/agent-loop.js.map +1 -1
- package/dist/harness/loop/hooks.d.ts +7 -0
- package/dist/harness/loop/hooks.d.ts.map +1 -0
- package/dist/harness/loop/hooks.js +46 -0
- package/dist/harness/loop/hooks.js.map +1 -0
- package/dist/harness/loop/index.d.ts +8 -0
- package/dist/harness/loop/index.d.ts.map +1 -0
- package/dist/harness/loop/index.js +257 -0
- package/dist/harness/loop/index.js.map +1 -0
- package/dist/harness/loop/inference.d.ts +19 -0
- package/dist/harness/loop/inference.d.ts.map +1 -0
- package/dist/harness/loop/inference.js +121 -0
- package/dist/harness/loop/inference.js.map +1 -0
- package/dist/harness/loop/memory.d.ts +22 -0
- package/dist/harness/loop/memory.d.ts.map +1 -0
- package/dist/harness/loop/memory.js +73 -0
- package/dist/harness/loop/memory.js.map +1 -0
- package/dist/harness/loop/safety.d.ts +8 -0
- package/dist/harness/loop/safety.d.ts.map +1 -0
- package/dist/harness/loop/safety.js +21 -0
- package/dist/harness/loop/safety.js.map +1 -0
- package/dist/harness/loop/tools.d.ts +24 -0
- package/dist/harness/loop/tools.d.ts.map +1 -0
- package/dist/harness/loop/tools.js +184 -0
- package/dist/harness/loop/tools.js.map +1 -0
- package/dist/harness/loop/types.d.ts +7 -0
- package/dist/harness/loop/types.d.ts.map +1 -0
- package/dist/harness/loop/types.js +9 -0
- package/dist/harness/loop/types.js.map +1 -0
- package/dist/harness/memory/fs-compat.d.ts +3 -0
- package/dist/harness/memory/fs-compat.d.ts.map +1 -0
- package/dist/harness/memory/fs-compat.js +26 -0
- package/dist/harness/memory/fs-compat.js.map +1 -0
- package/dist/harness/memory/index.d.ts +105 -0
- package/dist/harness/memory/index.d.ts.map +1 -0
- package/dist/harness/memory/index.js +491 -0
- package/dist/harness/memory/index.js.map +1 -0
- package/dist/harness/memory/prompter.d.ts +7 -0
- package/dist/harness/memory/prompter.d.ts.map +1 -0
- package/dist/harness/memory/prompter.js +24 -0
- package/dist/harness/memory/prompter.js.map +1 -0
- package/dist/harness/memory/ranker.d.ts +15 -0
- package/dist/harness/memory/ranker.d.ts.map +1 -0
- package/dist/harness/memory/ranker.js +72 -0
- package/dist/harness/memory/ranker.js.map +1 -0
- package/dist/harness/memory/scanner.d.ts +26 -0
- package/dist/harness/memory/scanner.d.ts.map +1 -0
- package/dist/harness/memory/scanner.js +187 -0
- package/dist/harness/memory/scanner.js.map +1 -0
- package/dist/harness/memory/types.d.ts +50 -0
- package/dist/harness/memory/types.d.ts.map +1 -0
- package/dist/harness/memory/types.js +7 -0
- package/dist/harness/memory/types.js.map +1 -0
- package/dist/harness/memory-loader.d.ts +2 -149
- package/dist/harness/memory-loader.d.ts.map +1 -1
- package/dist/harness/memory-loader.js +1 -713
- package/dist/harness/memory-loader.js.map +1 -1
- package/dist/hitl/interrupt.d.ts.map +1 -1
- package/dist/hitl/interrupt.js +2 -1
- package/dist/hitl/interrupt.js.map +1 -1
- package/dist/prebuilt/react-agent.d.ts.map +1 -1
- package/dist/prebuilt/react-agent.js +6 -2
- package/dist/prebuilt/react-agent.js.map +1 -1
- package/dist/pregel/checkpointing.d.ts +12 -0
- package/dist/pregel/checkpointing.d.ts.map +1 -0
- package/dist/pregel/checkpointing.js +60 -0
- package/dist/pregel/checkpointing.js.map +1 -0
- package/dist/pregel/execution.d.ts +7 -0
- package/dist/pregel/execution.d.ts.map +1 -0
- package/dist/pregel/execution.js +178 -0
- package/dist/pregel/execution.js.map +1 -0
- package/dist/pregel/index.d.ts +61 -0
- package/dist/pregel/index.d.ts.map +1 -0
- package/dist/pregel/index.js +154 -0
- package/dist/pregel/index.js.map +1 -0
- package/dist/pregel/interrupts.d.ts +3 -0
- package/dist/pregel/interrupts.d.ts.map +1 -0
- package/dist/pregel/interrupts.js +7 -0
- package/dist/pregel/interrupts.js.map +1 -0
- package/dist/pregel/state-helpers.d.ts +12 -0
- package/dist/pregel/state-helpers.d.ts.map +1 -0
- package/dist/pregel/state-helpers.js +71 -0
- package/dist/pregel/state-helpers.js.map +1 -0
- package/dist/pregel/streaming.d.ts +5 -0
- package/dist/pregel/streaming.d.ts.map +1 -0
- package/dist/pregel/streaming.js +462 -0
- package/dist/pregel/streaming.js.map +1 -0
- package/dist/pregel/types.d.ts +48 -0
- package/dist/pregel/types.d.ts.map +1 -0
- package/dist/pregel/types.js +5 -0
- package/dist/pregel/types.js.map +1 -0
- package/dist/pregel.d.ts +1 -66
- package/dist/pregel.d.ts.map +1 -1
- package/dist/pregel.js +2 -854
- package/dist/pregel.js.map +1 -1
- package/dist/swarm/agent-node.d.ts +11 -0
- package/dist/swarm/agent-node.d.ts.map +1 -0
- package/dist/swarm/agent-node.js +156 -0
- package/dist/swarm/agent-node.js.map +1 -0
- package/dist/swarm/compile-ext.d.ts +5 -0
- package/dist/swarm/compile-ext.d.ts.map +1 -0
- package/dist/swarm/compile-ext.js +126 -0
- package/dist/swarm/compile-ext.js.map +1 -0
- package/dist/swarm/config.d.ts +147 -0
- package/dist/swarm/config.d.ts.map +1 -0
- package/dist/swarm/config.js +17 -0
- package/dist/swarm/config.js.map +1 -0
- package/dist/swarm/factories.d.ts +37 -0
- package/dist/swarm/factories.d.ts.map +1 -0
- package/dist/swarm/factories.js +703 -0
- package/dist/swarm/factories.js.map +1 -0
- package/dist/swarm/graph.d.ts +14 -147
- package/dist/swarm/graph.d.ts.map +1 -1
- package/dist/swarm/graph.js +30 -917
- package/dist/swarm/graph.js.map +1 -1
- package/dist/swarm/pool.js.map +1 -1
- package/dist/swarm/supervisor.js.map +1 -1
- package/dist/testing/index.d.ts +2 -2
- package/dist/testing/index.d.ts.map +1 -1
- package/dist/testing/index.js +3 -2
- package/dist/testing/index.js.map +1 -1
- package/dist/tools/define.d.ts +2 -1
- package/dist/tools/define.d.ts.map +1 -1
- package/dist/tools/define.js +3 -1
- package/dist/tools/define.js.map +1 -1
- package/dist/tools/types.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/pregel.js
CHANGED
|
@@ -1,855 +1,3 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
// New: interrupt() context management, HITL resume, token streaming
|
|
4
|
-
// ============================================================
|
|
5
|
-
import { START, END, Send, Command, } from "./types.js";
|
|
6
|
-
import { RecursionLimitError, NodeNotFoundError, ONIInterrupt, NodeTimeoutError, ONIError, NodeExecutionError, CircuitBreakerOpenError } from "./errors.js";
|
|
7
|
-
import { CircuitBreaker } from "./circuit-breaker.js";
|
|
8
|
-
import { withRetry } from "./retry.js";
|
|
9
|
-
import { NamespacedCheckpointer } from "./checkpointers/namespaced.js";
|
|
10
|
-
import { _runWithContext } from "./context.js";
|
|
11
|
-
import { StreamWriterImpl, _withTokenHandler } from "./streaming.js";
|
|
12
|
-
import { NodeInterruptSignal, HITLInterruptException, HITLSessionStore, _installInterruptContext, _clearInterruptContext, } from "./hitl/index.js";
|
|
13
|
-
import { EventBus } from "./events/bus.js";
|
|
14
|
-
import { AuditLog } from "./guardrails/audit.js";
|
|
15
|
-
import { BudgetTracker } from "./guardrails/budget.js";
|
|
16
|
-
import { runFilters } from "./guardrails/filters.js";
|
|
17
|
-
import { ONITracer } from "./telemetry.js";
|
|
18
|
-
const DEFAULT_RECURSION_LIMIT = 25;
|
|
19
|
-
/** Maximum entries in the node result cache — oldest entries evicted FIFO when full. */
|
|
20
|
-
const NODE_CACHE_MAX_SIZE = 256;
|
|
21
|
-
export class ONIPregelRunner {
|
|
22
|
-
nodes;
|
|
23
|
-
edges;
|
|
24
|
-
channels;
|
|
25
|
-
interruptConfig;
|
|
26
|
-
checkpointer;
|
|
27
|
-
store;
|
|
28
|
-
defaults;
|
|
29
|
-
dlq;
|
|
30
|
-
hitlStore = new HITLSessionStore();
|
|
31
|
-
nodeCache = new Map();
|
|
32
|
-
circuitBreakers = new Map();
|
|
33
|
-
/** Count of concurrent subgraph invocations active on this runner. >0 means running as subgraph. */
|
|
34
|
-
_subgraphRefCount = 0;
|
|
35
|
-
/** Per-invocation parent updates from Command.PARENT, keyed by parent threadId. */
|
|
36
|
-
_perInvocationParentUpdates = new Map();
|
|
37
|
-
/** Per-invocation checkpointer override for subgraph isolation, keyed by threadId. */
|
|
38
|
-
_perInvocationCheckpointer = new Map();
|
|
39
|
-
eventBus;
|
|
40
|
-
auditLog;
|
|
41
|
-
budgetTracker;
|
|
42
|
-
contentFilters;
|
|
43
|
-
toolPermissions;
|
|
44
|
-
tracer;
|
|
45
|
-
/** Pre-indexed edges by source node — O(1) lookup instead of O(n) filter */
|
|
46
|
-
_edgesBySource;
|
|
47
|
-
/** Pre-computed ephemeral channel keys — avoids iterating all channels */
|
|
48
|
-
_ephemeralKeys;
|
|
49
|
-
constructor(nodes, edges, channels, interruptConfig = {}, checkpointer = null, store = null, guardrails, listeners, defaults, dlq = null, tracer) {
|
|
50
|
-
this.nodes = nodes;
|
|
51
|
-
this.edges = edges;
|
|
52
|
-
this.channels = channels;
|
|
53
|
-
this.interruptConfig = interruptConfig;
|
|
54
|
-
this.checkpointer = checkpointer;
|
|
55
|
-
this.store = store;
|
|
56
|
-
this.defaults = defaults;
|
|
57
|
-
this.dlq = dlq;
|
|
58
|
-
this.eventBus = new EventBus(listeners);
|
|
59
|
-
this.auditLog = guardrails?.audit ? new AuditLog() : null;
|
|
60
|
-
this.budgetTracker = guardrails?.budget ? new BudgetTracker(guardrails.budget) : null;
|
|
61
|
-
this.contentFilters = guardrails?.filters ?? [];
|
|
62
|
-
this.toolPermissions = guardrails?.toolPermissions;
|
|
63
|
-
this.tracer = new ONITracer(tracer ?? null);
|
|
64
|
-
// Pre-index edges by source for O(1) lookups in getNextNodes
|
|
65
|
-
this._edgesBySource = new Map();
|
|
66
|
-
for (const edge of edges) {
|
|
67
|
-
const from = edge.from;
|
|
68
|
-
let list = this._edgesBySource.get(from);
|
|
69
|
-
if (!list) {
|
|
70
|
-
list = [];
|
|
71
|
-
this._edgesBySource.set(from, list);
|
|
72
|
-
}
|
|
73
|
-
list.push(edge);
|
|
74
|
-
}
|
|
75
|
-
// Pre-compute ephemeral keys to avoid scanning all channels per superstep
|
|
76
|
-
this._ephemeralKeys = Object.keys(channels).filter((k) => channels[k].ephemeral);
|
|
77
|
-
}
|
|
78
|
-
// ----------------------------------------------------------------
|
|
79
|
-
// State helpers
|
|
80
|
-
// ----------------------------------------------------------------
|
|
81
|
-
buildInitialState() {
|
|
82
|
-
const state = {};
|
|
83
|
-
for (const key of Object.keys(this.channels)) {
|
|
84
|
-
state[key] = this.channels[key].default();
|
|
85
|
-
}
|
|
86
|
-
return state;
|
|
87
|
-
}
|
|
88
|
-
applyUpdate(current, update) {
|
|
89
|
-
const keys = Object.keys(update);
|
|
90
|
-
if (keys.length === 0)
|
|
91
|
-
return current;
|
|
92
|
-
const next = { ...current };
|
|
93
|
-
for (const key of keys) {
|
|
94
|
-
if (update[key] !== undefined) {
|
|
95
|
-
const ch = this.channels[key];
|
|
96
|
-
next[key] = ch
|
|
97
|
-
? ch.reducer(current[key], update[key])
|
|
98
|
-
: update[key];
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
return next;
|
|
102
|
-
}
|
|
103
|
-
resetEphemeral(state) {
|
|
104
|
-
if (this._ephemeralKeys.length === 0)
|
|
105
|
-
return state;
|
|
106
|
-
const next = { ...state };
|
|
107
|
-
for (const key of this._ephemeralKeys) {
|
|
108
|
-
next[key] = this.channels[key].default();
|
|
109
|
-
}
|
|
110
|
-
return next;
|
|
111
|
-
}
|
|
112
|
-
// ----------------------------------------------------------------
|
|
113
|
-
// Edge resolution
|
|
114
|
-
// ----------------------------------------------------------------
|
|
115
|
-
getNextNodes(fromNode, state, config) {
|
|
116
|
-
const outgoing = this._edgesBySource.get(fromNode) ?? [];
|
|
117
|
-
const nodes = [];
|
|
118
|
-
const sends = [];
|
|
119
|
-
for (const edge of outgoing) {
|
|
120
|
-
if (edge.type === "static") {
|
|
121
|
-
nodes.push(edge.to);
|
|
122
|
-
}
|
|
123
|
-
else {
|
|
124
|
-
const result = edge.condition(state, config);
|
|
125
|
-
const resolved = Array.isArray(result) ? result : [result];
|
|
126
|
-
for (const r of resolved) {
|
|
127
|
-
if (r instanceof Send)
|
|
128
|
-
sends.push({ node: r.node, args: r.args });
|
|
129
|
-
else
|
|
130
|
-
nodes.push(edge.pathMap?.[r] ?? r);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
return { nodes, sends };
|
|
135
|
-
}
|
|
136
|
-
// ----------------------------------------------------------------
|
|
137
|
-
// Execute a node with interrupt() context installed
|
|
138
|
-
// ----------------------------------------------------------------
|
|
139
|
-
async executeNode(nodeDef, state, config, resumeValue, hasResume, writer, step, recursionLimit) {
|
|
140
|
-
// Check cache (compute key once, reuse for both lookup and store)
|
|
141
|
-
let cacheKey;
|
|
142
|
-
if (nodeDef.cache) {
|
|
143
|
-
const policy = typeof nodeDef.cache === "object" ? nodeDef.cache : {};
|
|
144
|
-
const keyFn = policy.key ?? ((s) => JSON.stringify(s));
|
|
145
|
-
cacheKey = `${nodeDef.name}::${keyFn(state)}`;
|
|
146
|
-
const cached = this.nodeCache.get(cacheKey);
|
|
147
|
-
if (cached) {
|
|
148
|
-
const ttl = policy.ttl ?? Infinity;
|
|
149
|
-
if (Date.now() - cached.timestamp < ttl) {
|
|
150
|
-
return cached.result;
|
|
151
|
-
}
|
|
152
|
-
this.nodeCache.delete(cacheKey);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
const _tid = config?.threadId ?? "unknown";
|
|
156
|
-
const ctx = {
|
|
157
|
-
config: config ?? {},
|
|
158
|
-
store: this.store,
|
|
159
|
-
writer: writer ?? null,
|
|
160
|
-
state: state,
|
|
161
|
-
parentGraph: null,
|
|
162
|
-
parentUpdates: [],
|
|
163
|
-
step: step ?? 0,
|
|
164
|
-
recursionLimit: recursionLimit ?? DEFAULT_RECURSION_LIMIT,
|
|
165
|
-
toolPermissions: this.toolPermissions,
|
|
166
|
-
_recordUsage: (agentName, modelId, usage) => {
|
|
167
|
-
if (!this.budgetTracker)
|
|
168
|
-
return;
|
|
169
|
-
const entries = this.budgetTracker.record(agentName, modelId, usage);
|
|
170
|
-
for (const e of entries)
|
|
171
|
-
this.auditLog?.record(_tid, e);
|
|
172
|
-
},
|
|
173
|
-
_emitEvent: (event) => this.eventBus.emit(event),
|
|
174
|
-
_auditRecord: (entry) => this.auditLog?.record(_tid, entry),
|
|
175
|
-
};
|
|
176
|
-
return _runWithContext(ctx, async () => {
|
|
177
|
-
_installInterruptContext({
|
|
178
|
-
nodeName: nodeDef.name,
|
|
179
|
-
resumeValues: hasResume ? [resumeValue] : [],
|
|
180
|
-
});
|
|
181
|
-
try {
|
|
182
|
-
// Content filter — input direction
|
|
183
|
-
if (this.contentFilters.length > 0) {
|
|
184
|
-
const inputStr = JSON.stringify(state);
|
|
185
|
-
const inputCheck = runFilters(this.contentFilters, inputStr, "input");
|
|
186
|
-
if (!inputCheck.passed) {
|
|
187
|
-
const threadId = config?.threadId ?? "unknown";
|
|
188
|
-
this.eventBus.emit({ type: "filter.blocked", filter: inputCheck.blockedBy, agent: nodeDef.name, direction: "input", reason: inputCheck.reason, timestamp: Date.now() });
|
|
189
|
-
this.auditLog?.record(threadId, { timestamp: Date.now(), agent: nodeDef.name, action: "filter.blocked", data: { filter: inputCheck.blockedBy, direction: "input", reason: inputCheck.reason } });
|
|
190
|
-
throw new Error(`Content blocked by filter "${inputCheck.blockedBy}" on input to node "${nodeDef.name}": ${inputCheck.reason}`);
|
|
191
|
-
}
|
|
192
|
-
// Apply redaction if content was rewritten by a redacting filter
|
|
193
|
-
if (inputCheck.content !== inputStr) {
|
|
194
|
-
try {
|
|
195
|
-
state = JSON.parse(inputCheck.content);
|
|
196
|
-
}
|
|
197
|
-
catch { /* leave state unchanged on parse failure */ }
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
const run = () => Promise.resolve(nodeDef.fn(state, config));
|
|
201
|
-
// Core execute call: retry-aware
|
|
202
|
-
const executeCall = async () => {
|
|
203
|
-
if (nodeDef.retry)
|
|
204
|
-
return withRetry(run, nodeDef.name, nodeDef.retry);
|
|
205
|
-
return run();
|
|
206
|
-
};
|
|
207
|
-
// Wrap with timeout if configured (per-node > global default > none)
|
|
208
|
-
const timeoutMs = nodeDef.timeout ?? this.defaults?.nodeTimeout;
|
|
209
|
-
const executeWithTimeout = async () => {
|
|
210
|
-
if (timeoutMs != null && timeoutMs > 0) {
|
|
211
|
-
const ac = new AbortController();
|
|
212
|
-
const timer = setTimeout(() => ac.abort(), timeoutMs);
|
|
213
|
-
try {
|
|
214
|
-
return await Promise.race([
|
|
215
|
-
executeCall(),
|
|
216
|
-
new Promise((_, reject) => {
|
|
217
|
-
ac.signal.addEventListener("abort", () => {
|
|
218
|
-
reject(new NodeTimeoutError(nodeDef.name, timeoutMs));
|
|
219
|
-
});
|
|
220
|
-
}),
|
|
221
|
-
]);
|
|
222
|
-
}
|
|
223
|
-
finally {
|
|
224
|
-
clearTimeout(timer);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
return executeCall();
|
|
228
|
-
};
|
|
229
|
-
// Wrap with circuit breaker if configured
|
|
230
|
-
const cb = this.getCircuitBreaker(nodeDef);
|
|
231
|
-
let result;
|
|
232
|
-
try {
|
|
233
|
-
if (cb) {
|
|
234
|
-
result = await cb.execute(executeWithTimeout);
|
|
235
|
-
}
|
|
236
|
-
else {
|
|
237
|
-
result = await executeWithTimeout();
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
catch (err) {
|
|
241
|
-
// Pass through interrupt signals (thrown by interrupt() inside nodes)
|
|
242
|
-
if (err instanceof NodeInterruptSignal)
|
|
243
|
-
throw err;
|
|
244
|
-
// Circuit breaker open — invoke user fallback with real state + error
|
|
245
|
-
if (err instanceof CircuitBreakerOpenError && nodeDef.circuitBreaker?.fallback) {
|
|
246
|
-
result = nodeDef.circuitBreaker.fallback(state, err);
|
|
247
|
-
}
|
|
248
|
-
else {
|
|
249
|
-
// Pass through structured ONI errors (NodeExecutionError from retry, NodeTimeoutError, etc.)
|
|
250
|
-
if (err instanceof ONIError)
|
|
251
|
-
throw err;
|
|
252
|
-
// Wrap raw errors and non-Error throws in NodeExecutionError
|
|
253
|
-
const cause = err instanceof Error ? err : new Error(String(err));
|
|
254
|
-
throw new NodeExecutionError(nodeDef.name, cause);
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
// Content filter — output direction
|
|
258
|
-
if (this.contentFilters.length > 0 && result != null) {
|
|
259
|
-
const outputStr = JSON.stringify(result);
|
|
260
|
-
const outputCheck = runFilters(this.contentFilters, outputStr, "output");
|
|
261
|
-
if (!outputCheck.passed) {
|
|
262
|
-
const threadId = config?.threadId ?? "unknown";
|
|
263
|
-
this.eventBus.emit({ type: "filter.blocked", filter: outputCheck.blockedBy, agent: nodeDef.name, direction: "output", reason: outputCheck.reason, timestamp: Date.now() });
|
|
264
|
-
this.auditLog?.record(threadId, { timestamp: Date.now(), agent: nodeDef.name, action: "filter.blocked", data: { filter: outputCheck.blockedBy, direction: "output", reason: outputCheck.reason } });
|
|
265
|
-
throw new Error(`Content blocked by filter "${outputCheck.blockedBy}" on output of node "${nodeDef.name}": ${outputCheck.reason}`);
|
|
266
|
-
}
|
|
267
|
-
// Apply redaction if content was rewritten by a redacting filter
|
|
268
|
-
if (outputCheck.content !== outputStr) {
|
|
269
|
-
try {
|
|
270
|
-
result = JSON.parse(outputCheck.content);
|
|
271
|
-
}
|
|
272
|
-
catch { /* leave result unchanged on parse failure */ }
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
// Store in cache (reuse key computed above); evict oldest entry when full
|
|
276
|
-
if (nodeDef.cache && cacheKey) {
|
|
277
|
-
if (this.nodeCache.size >= NODE_CACHE_MAX_SIZE) {
|
|
278
|
-
const oldest = this.nodeCache.keys().next().value;
|
|
279
|
-
if (oldest !== undefined)
|
|
280
|
-
this.nodeCache.delete(oldest);
|
|
281
|
-
}
|
|
282
|
-
this.nodeCache.set(cacheKey, { result, timestamp: Date.now() });
|
|
283
|
-
}
|
|
284
|
-
return result;
|
|
285
|
-
}
|
|
286
|
-
finally {
|
|
287
|
-
_clearInterruptContext();
|
|
288
|
-
}
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
// ----------------------------------------------------------------
|
|
292
|
-
// Dynamic interrupt check
|
|
293
|
-
// ----------------------------------------------------------------
|
|
294
|
-
checkDynamicInterrupt(node, timing, state, config) {
|
|
295
|
-
const dynamics = config?.dynamicInterrupts;
|
|
296
|
-
if (!dynamics)
|
|
297
|
-
return;
|
|
298
|
-
for (const di of dynamics) {
|
|
299
|
-
if (di.node === node && di.timing === timing && di.condition(state)) {
|
|
300
|
-
throw new ONIInterrupt(node, timing, state);
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
// ----------------------------------------------------------------
|
|
305
|
-
// Core stream generator
|
|
306
|
-
// ----------------------------------------------------------------
|
|
307
|
-
async *_stream(input, config, streamMode = "updates") {
|
|
308
|
-
const threadId = config?.threadId ?? `oni-${Date.now()}`;
|
|
309
|
-
const recursionLimit = config?.recursionLimit ?? DEFAULT_RECURSION_LIMIT;
|
|
310
|
-
const agentId = config?.agentId;
|
|
311
|
-
const modes = new Set(Array.isArray(streamMode) ? streamMode : [streamMode]);
|
|
312
|
-
const isMultiMode = Array.isArray(streamMode);
|
|
313
|
-
const tag = (evt, mode) => isMultiMode ? { ...evt, mode } : evt;
|
|
314
|
-
// Pre-compute mode flags — eliminates ~20 Set.has() lookups per superstep
|
|
315
|
-
const modeDebug = modes.has("debug");
|
|
316
|
-
const modeUpdates = modes.has("updates");
|
|
317
|
-
const modeValues = modes.has("values");
|
|
318
|
-
const modeCustom = modes.has("custom");
|
|
319
|
-
const modeMessages = modes.has("messages");
|
|
320
|
-
// Telemetry: graph-level span
|
|
321
|
-
const graphSpan = this.tracer.startGraphSpan("invoke", { threadId, agentId });
|
|
322
|
-
let step = 0; // declared before try so finally can read it for setAttribute
|
|
323
|
-
try {
|
|
324
|
-
// Load resume values from config (set by resume() call)
|
|
325
|
-
const resumeMap = config?.__resumeValues ?? {};
|
|
326
|
-
// Load or init state
|
|
327
|
-
let state;
|
|
328
|
-
let pendingNodes = [];
|
|
329
|
-
let pendingSends = [];
|
|
330
|
-
const effectiveCheckpointer = (this._perInvocationCheckpointer.get(threadId) ?? this.checkpointer);
|
|
331
|
-
if (effectiveCheckpointer && config?.threadId) {
|
|
332
|
-
const cp = await effectiveCheckpointer.get(threadId);
|
|
333
|
-
if (cp) {
|
|
334
|
-
state = this.applyUpdate(cp.state, input);
|
|
335
|
-
step = cp.step;
|
|
336
|
-
pendingNodes = cp.nextNodes;
|
|
337
|
-
pendingSends = cp.pendingSends ?? [];
|
|
338
|
-
}
|
|
339
|
-
else {
|
|
340
|
-
state = this.applyUpdate(this.buildInitialState(), input);
|
|
341
|
-
const init = this.getNextNodes(START, state, config);
|
|
342
|
-
pendingNodes = init.nodes;
|
|
343
|
-
pendingSends = init.sends;
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
else {
|
|
347
|
-
state = this.applyUpdate(this.buildInitialState(), input);
|
|
348
|
-
const init = this.getNextNodes(START, state, config);
|
|
349
|
-
pendingNodes = init.nodes;
|
|
350
|
-
pendingSends = init.sends;
|
|
351
|
-
}
|
|
352
|
-
if (modeValues)
|
|
353
|
-
yield tag(this.evt("state_update", state, step, agentId), "values");
|
|
354
|
-
// ---- Main superstep loop ----
|
|
355
|
-
while (true) {
|
|
356
|
-
const nextNodes = [];
|
|
357
|
-
const nextSends = [];
|
|
358
|
-
state = this.resetEphemeral(state);
|
|
359
|
-
// Drain sends — group by target node for parallel fan-out execution
|
|
360
|
-
const sendGroups = new Map();
|
|
361
|
-
for (const send of pendingSends) {
|
|
362
|
-
if (!sendGroups.has(send.node))
|
|
363
|
-
sendGroups.set(send.node, []);
|
|
364
|
-
sendGroups.get(send.node).push(send);
|
|
365
|
-
if (modeDebug)
|
|
366
|
-
yield tag(this.evt("send", send, step, agentId, send.node), "debug");
|
|
367
|
-
}
|
|
368
|
-
// Recursion limit guard — must fire before any sends execute so that
|
|
369
|
-
// node side-effects are not applied to a step that will be discarded.
|
|
370
|
-
if (step >= recursionLimit)
|
|
371
|
-
throw new RecursionLimitError(recursionLimit);
|
|
372
|
-
// Execute fan-out sends (each Send → separate node execution with its own state)
|
|
373
|
-
if (sendGroups.size > 0) {
|
|
374
|
-
// Build promises directly — avoids spread+flatMap intermediate arrays
|
|
375
|
-
const sendPromises = [];
|
|
376
|
-
for (const [node, sends] of sendGroups) {
|
|
377
|
-
const nodeDef = this.nodes.get(node);
|
|
378
|
-
if (!nodeDef)
|
|
379
|
-
throw new NodeNotFoundError(node);
|
|
380
|
-
for (const send of sends) {
|
|
381
|
-
sendPromises.push((async () => {
|
|
382
|
-
const sendState = this.applyUpdate(state, send.args);
|
|
383
|
-
const result = await this.executeNode(nodeDef, sendState, config, undefined, undefined, undefined, step, recursionLimit);
|
|
384
|
-
return { name: node, result };
|
|
385
|
-
})());
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
const sendResults = await Promise.all(sendPromises);
|
|
389
|
-
// Reduce all send results through channels
|
|
390
|
-
for (const { name, result } of sendResults) {
|
|
391
|
-
if (result instanceof Command) {
|
|
392
|
-
if (result.update)
|
|
393
|
-
state = this.applyUpdate(state, result.update);
|
|
394
|
-
const gotos = result.goto
|
|
395
|
-
? (Array.isArray(result.goto) ? result.goto : [result.goto])
|
|
396
|
-
: this.getNextNodes(name, state, config).nodes;
|
|
397
|
-
nextNodes.push(...gotos);
|
|
398
|
-
}
|
|
399
|
-
else if (result && typeof result === "object") {
|
|
400
|
-
state = this.applyUpdate(state, result);
|
|
401
|
-
const { nodes, sends } = this.getNextNodes(name, state, config);
|
|
402
|
-
nextNodes.push(...nodes);
|
|
403
|
-
nextSends.push(...sends);
|
|
404
|
-
}
|
|
405
|
-
else {
|
|
406
|
-
const { nodes, sends } = this.getNextNodes(name, state, config);
|
|
407
|
-
nextNodes.push(...nodes);
|
|
408
|
-
nextSends.push(...sends);
|
|
409
|
-
}
|
|
410
|
-
if (modeUpdates || modeDebug) {
|
|
411
|
-
const delta = result instanceof Command ? (result.update ?? {}) : (result ?? {});
|
|
412
|
-
if (modeUpdates)
|
|
413
|
-
yield tag(this.evt("node_end", delta, step, agentId, name), "updates");
|
|
414
|
-
if (modeDebug)
|
|
415
|
-
yield tag(this.evt("node_end", delta, step, agentId, name), "debug");
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
pendingSends = [];
|
|
420
|
-
// Filter executable nodes (non-END), excluding nodes already handled by sends
|
|
421
|
-
const executableNodes = pendingNodes.filter((n) => n !== END && !sendGroups.has(n));
|
|
422
|
-
if (executableNodes.length === 0 && sendGroups.size === 0)
|
|
423
|
-
break;
|
|
424
|
-
// Emit debug node_start events before parallel execution
|
|
425
|
-
if (modeDebug) {
|
|
426
|
-
for (const nodeName of executableNodes) {
|
|
427
|
-
const name = nodeName;
|
|
428
|
-
if (!this.nodes.has(name))
|
|
429
|
-
throw new NodeNotFoundError(name);
|
|
430
|
-
// Static interrupt BEFORE (check before emitting start)
|
|
431
|
-
if (this.interruptConfig.interruptBefore?.includes(name))
|
|
432
|
-
throw new ONIInterrupt(name, "before", state);
|
|
433
|
-
this.checkDynamicInterrupt(name, "before", state, config);
|
|
434
|
-
yield tag(this.evt("node_start", {}, step, agentId, name), "debug");
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
// Execute all active nodes in parallel
|
|
438
|
-
const allCustomEvents = [];
|
|
439
|
-
const allMessageEvents = [];
|
|
440
|
-
const allSubgraphEvents = [];
|
|
441
|
-
const nodeWriters = new Map();
|
|
442
|
-
// Track the first HITL interrupt across all parallel nodes. We use
|
|
443
|
-
// allSettled (not Promise.all) so that when one node raises an interrupt,
|
|
444
|
-
// all other in-flight nodes complete before the interrupt is surfaced.
|
|
445
|
-
// This prevents orphaned background executions that would apply side
|
|
446
|
-
// effects without being checkpointed, causing double-application on resume.
|
|
447
|
-
let pendingInterrupt = null;
|
|
448
|
-
const allSettledResults = await Promise.allSettled(executableNodes.map(async (nodeName) => {
|
|
449
|
-
const name = nodeName;
|
|
450
|
-
const nodeDef = this.nodes.get(name);
|
|
451
|
-
if (!nodeDef)
|
|
452
|
-
throw new NodeNotFoundError(name);
|
|
453
|
-
// Static interrupt BEFORE (non-debug mode)
|
|
454
|
-
if (!modeDebug) {
|
|
455
|
-
if (this.interruptConfig.interruptBefore?.includes(name))
|
|
456
|
-
throw new ONIInterrupt(name, "before", state);
|
|
457
|
-
this.checkDynamicInterrupt(name, "before", state, config);
|
|
458
|
-
}
|
|
459
|
-
// Create a StreamWriter for this node
|
|
460
|
-
const messageId = `msg-${threadId}-${step}-${name}`;
|
|
461
|
-
const customEvents = [];
|
|
462
|
-
const messageEvents = [];
|
|
463
|
-
const writerImpl = new StreamWriterImpl((evt) => customEvents.push(evt), (_token) => { }, (evt) => messageEvents.push(evt), name, step, messageId, agentId);
|
|
464
|
-
nodeWriters.set(name, writerImpl);
|
|
465
|
-
// Check if this node has a pending resume value
|
|
466
|
-
const resumeValue = resumeMap[name];
|
|
467
|
-
const hasResume = name in resumeMap;
|
|
468
|
-
// Emit agent.start lifecycle event
|
|
469
|
-
const nodeStartTime = Date.now();
|
|
470
|
-
this.eventBus.emit({ type: "agent.start", agent: name, timestamp: nodeStartTime, step });
|
|
471
|
-
// Telemetry: node-level span
|
|
472
|
-
const nodeSpan = this.tracer.startNodeSpan(name, { threadId, step, agentId });
|
|
473
|
-
let result;
|
|
474
|
-
let subParentUpdates = [];
|
|
475
|
-
try {
|
|
476
|
-
// Scope emitToken to this node's async context via ALS — parallel nodes each
|
|
477
|
-
// get their own handler so tokens are never dropped or misrouted.
|
|
478
|
-
result = await _withTokenHandler((token) => writerImpl.token(token), async () => {
|
|
479
|
-
if (nodeDef.subgraph) {
|
|
480
|
-
const childRunner = nodeDef.subgraph._runner;
|
|
481
|
-
// Per-invocation key for concurrent-safe state isolation
|
|
482
|
-
const invocationKey = threadId;
|
|
483
|
-
if (childRunner) {
|
|
484
|
-
childRunner._subgraphRefCount++;
|
|
485
|
-
childRunner._perInvocationParentUpdates.set(invocationKey, []);
|
|
486
|
-
}
|
|
487
|
-
// Install a namespaced checkpointer per invocation instead of swapping a shared field
|
|
488
|
-
if (this.checkpointer && childRunner) {
|
|
489
|
-
childRunner._perInvocationCheckpointer.set(invocationKey, new NamespacedCheckpointer(this.checkpointer, name));
|
|
490
|
-
}
|
|
491
|
-
// Stream the subgraph — always clean up per-invocation state, even on throw/interrupt
|
|
492
|
-
let subFinalState;
|
|
493
|
-
try {
|
|
494
|
-
const childStreamMode = ["debug", "values"];
|
|
495
|
-
for await (const evt of nodeDef.subgraph.stream(state, {
|
|
496
|
-
...config,
|
|
497
|
-
// Pass the parent's effective threadId explicitly so the child's
|
|
498
|
-
// _perInvocationParentUpdates lookup at Command.PARENT time uses
|
|
499
|
-
// the same key that was registered in invocationKey above.
|
|
500
|
-
threadId: invocationKey,
|
|
501
|
-
parentRunId: config?.threadId,
|
|
502
|
-
streamMode: childStreamMode,
|
|
503
|
-
})) {
|
|
504
|
-
// Namespace-prefix the node name
|
|
505
|
-
allSubgraphEvents.push({
|
|
506
|
-
...evt,
|
|
507
|
-
node: evt.node ? `${name}:${evt.node}` : name,
|
|
508
|
-
});
|
|
509
|
-
// Track the last state_update as the final subgraph state
|
|
510
|
-
if (evt.event === "state_update") {
|
|
511
|
-
subFinalState = evt.data;
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
if (childRunner) {
|
|
515
|
-
subParentUpdates = childRunner._perInvocationParentUpdates.get(invocationKey) ?? [];
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
finally {
|
|
519
|
-
// Clean up per-invocation state — decrement ref count, remove Maps entries
|
|
520
|
-
if (childRunner) {
|
|
521
|
-
childRunner._subgraphRefCount--;
|
|
522
|
-
childRunner._perInvocationParentUpdates.delete(invocationKey);
|
|
523
|
-
childRunner._perInvocationCheckpointer.delete(invocationKey);
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
return subFinalState ?? {};
|
|
527
|
-
}
|
|
528
|
-
else {
|
|
529
|
-
return this.executeNode(nodeDef, state, config, resumeValue, hasResume, writerImpl, step, recursionLimit);
|
|
530
|
-
}
|
|
531
|
-
});
|
|
532
|
-
}
|
|
533
|
-
catch (err) {
|
|
534
|
-
// Catch interrupt() signals thrown from inside nodes
|
|
535
|
-
if (err instanceof NodeInterruptSignal) {
|
|
536
|
-
const iv = {
|
|
537
|
-
value: err.value,
|
|
538
|
-
node: name,
|
|
539
|
-
resumeId: err.resumeId,
|
|
540
|
-
timestamp: Date.now(),
|
|
541
|
-
};
|
|
542
|
-
const exc = new HITLInterruptException(threadId, iv, state);
|
|
543
|
-
// Claim the first-interrupt slot SYNCHRONOUSLY before any await.
|
|
544
|
-
// Both concurrent interrupt handlers check this flag before yielding,
|
|
545
|
-
// so whichever catch block runs first exclusively owns the checkpoint
|
|
546
|
-
// save. Without this guard, the last saveCheckpoint wins and its
|
|
547
|
-
// nextNodes diverge from pendingInterrupt's node, corrupting resume().
|
|
548
|
-
const isFirstInterrupt = !pendingInterrupt;
|
|
549
|
-
if (isFirstInterrupt)
|
|
550
|
-
pendingInterrupt = exc;
|
|
551
|
-
// Only save checkpoint for the first interrupt — the stored nextNodes
|
|
552
|
-
// must match pendingInterrupt's node so resume() restores correctly.
|
|
553
|
-
if (isFirstInterrupt) {
|
|
554
|
-
await this.saveCheckpoint(threadId, step, state, [name], pendingSends, agentId, config?.metadata);
|
|
555
|
-
// Record HITL session if checkpointer exists
|
|
556
|
-
if (effectiveCheckpointer) {
|
|
557
|
-
const cp = await effectiveCheckpointer.get(threadId);
|
|
558
|
-
if (cp)
|
|
559
|
-
this.hitlStore.record(threadId, iv, cp);
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
throw exc; // marks this node's settled result as rejected
|
|
563
|
-
}
|
|
564
|
-
// Record to DLQ before re-throwing — use original cause if wrapped
|
|
565
|
-
if (this.dlq && err instanceof Error) {
|
|
566
|
-
const dlqErr = (err instanceof NodeExecutionError && err.cause instanceof Error) ? err.cause : err;
|
|
567
|
-
this.dlq.record(threadId, name, state, dlqErr, nodeDef.retry?.maxAttempts ?? 1);
|
|
568
|
-
}
|
|
569
|
-
// Telemetry: record error on node span — use original cause if wrapped
|
|
570
|
-
if (err instanceof Error) {
|
|
571
|
-
const telErr = (err instanceof NodeExecutionError && err.cause instanceof Error) ? err.cause : err;
|
|
572
|
-
this.tracer.recordError(nodeSpan, telErr);
|
|
573
|
-
}
|
|
574
|
-
this.tracer.endSpan(nodeSpan);
|
|
575
|
-
// Lifecycle event: emit error for non-interrupt failures
|
|
576
|
-
if (err instanceof Error) {
|
|
577
|
-
this.eventBus.emit({ type: "error", agent: name, error: err, timestamp: Date.now() });
|
|
578
|
-
}
|
|
579
|
-
throw err;
|
|
580
|
-
}
|
|
581
|
-
// Telemetry: end node span
|
|
582
|
-
this.tracer.endSpan(nodeSpan);
|
|
583
|
-
// Emit agent.end lifecycle event
|
|
584
|
-
this.eventBus.emit({ type: "agent.end", agent: name, timestamp: Date.now(), step, duration: Date.now() - nodeStartTime });
|
|
585
|
-
// Collect events for yielding after parallel execution
|
|
586
|
-
allCustomEvents.push(...customEvents);
|
|
587
|
-
allMessageEvents.push(...messageEvents);
|
|
588
|
-
return { name, result, subParentUpdates };
|
|
589
|
-
}));
|
|
590
|
-
// Extract results now that all nodes have settled.
|
|
591
|
-
// Re-throw the first non-interrupt error (DLQ/telemetry already handled
|
|
592
|
-
// inside each node's catch block), then surface any HITL interrupt.
|
|
593
|
-
const nodeResults = [];
|
|
594
|
-
for (const settled of allSettledResults) {
|
|
595
|
-
if (settled.status === "fulfilled") {
|
|
596
|
-
nodeResults.push(settled.value);
|
|
597
|
-
}
|
|
598
|
-
else if (!(settled.reason instanceof HITLInterruptException)) {
|
|
599
|
-
throw settled.reason; // first non-interrupt error
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
if (pendingInterrupt)
|
|
603
|
-
throw pendingInterrupt;
|
|
604
|
-
// Yield buffered subgraph events — filtered by parent's active modes
|
|
605
|
-
for (const evt of allSubgraphEvents) {
|
|
606
|
-
const e = evt;
|
|
607
|
-
if (modeDebug) {
|
|
608
|
-
yield tag(e, "debug");
|
|
609
|
-
}
|
|
610
|
-
else if (modeUpdates && (e.event === "node_end")) {
|
|
611
|
-
yield tag(e, "updates");
|
|
612
|
-
}
|
|
613
|
-
else if (modeValues && e.event === "state_update") {
|
|
614
|
-
yield tag(e, "values");
|
|
615
|
-
}
|
|
616
|
-
// Custom and message events from subgraphs are forwarded if those modes are active
|
|
617
|
-
if (modeCustom && e.event === "custom") {
|
|
618
|
-
yield tag(e, "custom");
|
|
619
|
-
}
|
|
620
|
-
if (modeMessages && (e.event === "messages" || e.event === "messages/complete")) {
|
|
621
|
-
yield tag(e, "messages");
|
|
622
|
-
}
|
|
623
|
-
}
|
|
624
|
-
// Apply results
|
|
625
|
-
const stepWrites = [];
|
|
626
|
-
for (const { name, result, subParentUpdates: parentUpdates } of nodeResults) {
|
|
627
|
-
if (result instanceof Command) {
|
|
628
|
-
if (result.graph === Command.PARENT) {
|
|
629
|
-
// Push update to parent — do NOT apply locally
|
|
630
|
-
if (!this._subgraphRefCount) {
|
|
631
|
-
throw new Error("Command.PARENT used but graph is not running as a subgraph");
|
|
632
|
-
}
|
|
633
|
-
if (result.update) {
|
|
634
|
-
const myParentUpdates = this._perInvocationParentUpdates.get(threadId);
|
|
635
|
-
if (myParentUpdates)
|
|
636
|
-
myParentUpdates.push(result.update);
|
|
637
|
-
}
|
|
638
|
-
// Still resolve next nodes normally
|
|
639
|
-
const { nodes, sends } = this.getNextNodes(name, state, config);
|
|
640
|
-
nextNodes.push(...nodes);
|
|
641
|
-
nextSends.push(...sends);
|
|
642
|
-
}
|
|
643
|
-
else {
|
|
644
|
-
if (result.update) {
|
|
645
|
-
state = this.applyUpdate(state, result.update);
|
|
646
|
-
if (Object.keys(result.update).length > 0) {
|
|
647
|
-
stepWrites.push({ nodeId: name, writes: result.update });
|
|
648
|
-
}
|
|
649
|
-
}
|
|
650
|
-
const gotos = result.goto
|
|
651
|
-
? (Array.isArray(result.goto) ? result.goto : [result.goto])
|
|
652
|
-
: this.getNextNodes(name, state, config).nodes;
|
|
653
|
-
nextNodes.push(...gotos);
|
|
654
|
-
if (result.send)
|
|
655
|
-
nextSends.push(...result.send.map((s) => ({ node: s.node, args: s.args })));
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
else if (result && typeof result === "object") {
|
|
659
|
-
state = this.applyUpdate(state, result);
|
|
660
|
-
const writes = result;
|
|
661
|
-
if (Object.keys(writes).length > 0) {
|
|
662
|
-
stepWrites.push({ nodeId: name, writes });
|
|
663
|
-
}
|
|
664
|
-
const { nodes, sends } = this.getNextNodes(name, state, config);
|
|
665
|
-
nextNodes.push(...nodes);
|
|
666
|
-
nextSends.push(...sends);
|
|
667
|
-
}
|
|
668
|
-
else {
|
|
669
|
-
const { nodes, sends } = this.getNextNodes(name, state, config);
|
|
670
|
-
nextNodes.push(...nodes);
|
|
671
|
-
nextSends.push(...sends);
|
|
672
|
-
}
|
|
673
|
-
// Apply parent updates from subgraph Command.PARENT (after normal result)
|
|
674
|
-
for (const pu of parentUpdates) {
|
|
675
|
-
state = this.applyUpdate(state, pu);
|
|
676
|
-
}
|
|
677
|
-
if (modeUpdates || modeDebug) {
|
|
678
|
-
const delta = result instanceof Command ? (result.update ?? {}) : (result ?? {});
|
|
679
|
-
if (modeUpdates)
|
|
680
|
-
yield tag(this.evt("node_end", delta, step, agentId, name), "updates");
|
|
681
|
-
if (modeDebug)
|
|
682
|
-
yield tag(this.evt("node_end", delta, step, agentId, name), "debug");
|
|
683
|
-
}
|
|
684
|
-
// Static interrupt AFTER
|
|
685
|
-
if (this.interruptConfig.interruptAfter?.includes(name)) {
|
|
686
|
-
await this.saveCheckpoint(threadId, step, state, nextNodes, nextSends, agentId, config?.metadata);
|
|
687
|
-
throw new ONIInterrupt(name, "after", state);
|
|
688
|
-
}
|
|
689
|
-
this.checkDynamicInterrupt(name, "after", state, config);
|
|
690
|
-
}
|
|
691
|
-
// Yield buffered custom/message events based on stream mode
|
|
692
|
-
if (modeCustom || modeDebug) {
|
|
693
|
-
for (const evt of allCustomEvents) {
|
|
694
|
-
if (modeCustom)
|
|
695
|
-
yield tag(evt, "custom");
|
|
696
|
-
if (modeDebug)
|
|
697
|
-
yield tag(evt, "debug");
|
|
698
|
-
}
|
|
699
|
-
}
|
|
700
|
-
if (modeMessages || modeDebug) {
|
|
701
|
-
for (const evt of allMessageEvents) {
|
|
702
|
-
if (modeMessages)
|
|
703
|
-
yield tag(evt, "messages");
|
|
704
|
-
if (modeDebug)
|
|
705
|
-
yield tag(evt, "debug");
|
|
706
|
-
}
|
|
707
|
-
// Emit messages/complete for each node that produced tokens
|
|
708
|
-
for (const [, writer] of nodeWriters) {
|
|
709
|
-
const complete = writer._complete();
|
|
710
|
-
if (complete) {
|
|
711
|
-
if (modeMessages)
|
|
712
|
-
yield tag(complete, "messages");
|
|
713
|
-
if (modeDebug)
|
|
714
|
-
yield tag(complete, "debug");
|
|
715
|
-
}
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
if (modeValues)
|
|
719
|
-
yield tag(this.evt("state_update", state, step, agentId), "values");
|
|
720
|
-
// Deduplicate nextNodes — avoid Set+spread when no dupes (common case)
|
|
721
|
-
if (nextNodes.length <= 1) {
|
|
722
|
-
pendingNodes = nextNodes;
|
|
723
|
-
}
|
|
724
|
-
else {
|
|
725
|
-
const seen = new Set();
|
|
726
|
-
pendingNodes = [];
|
|
727
|
-
for (const n of nextNodes) {
|
|
728
|
-
const key = n;
|
|
729
|
-
if (!seen.has(key)) {
|
|
730
|
-
seen.add(key);
|
|
731
|
-
pendingNodes.push(n);
|
|
732
|
-
}
|
|
733
|
-
}
|
|
734
|
-
}
|
|
735
|
-
pendingSends = nextSends;
|
|
736
|
-
step++;
|
|
737
|
-
await this.saveCheckpoint(threadId, step, state, pendingNodes, pendingSends, agentId, config?.metadata, stepWrites);
|
|
738
|
-
}
|
|
739
|
-
if (modeValues)
|
|
740
|
-
yield tag(this.evt("state_update", state, step, agentId), "values");
|
|
741
|
-
}
|
|
742
|
-
finally {
|
|
743
|
-
// Telemetry: end graph span — always runs, even on error or interrupt
|
|
744
|
-
graphSpan.setAttribute("oni.steps", step);
|
|
745
|
-
this.tracer.endSpan(graphSpan);
|
|
746
|
-
}
|
|
747
|
-
}
|
|
748
|
-
// ----------------------------------------------------------------
|
|
749
|
-
// Public API
|
|
750
|
-
// ----------------------------------------------------------------
|
|
751
|
-
async invoke(input, config) {
|
|
752
|
-
let finalState;
|
|
753
|
-
for await (const evt of this._stream(input, config, "values")) {
|
|
754
|
-
if (evt.event === "state_update")
|
|
755
|
-
finalState = evt.data;
|
|
756
|
-
}
|
|
757
|
-
return finalState;
|
|
758
|
-
}
|
|
759
|
-
async *stream(input, config) {
|
|
760
|
-
yield* this._stream(input, config, config?.streamMode ?? "updates");
|
|
761
|
-
}
|
|
762
|
-
async batch(inputs, config) {
|
|
763
|
-
return Promise.all(inputs.map((inp, i) => this.invoke(inp, {
|
|
764
|
-
...config,
|
|
765
|
-
threadId: config?.threadId ? `${config.threadId}-${i}` : undefined,
|
|
766
|
-
})));
|
|
767
|
-
}
|
|
768
|
-
// ---- State ----
|
|
769
|
-
async getState(threadId) {
|
|
770
|
-
if (!this.checkpointer)
|
|
771
|
-
return null;
|
|
772
|
-
return (await this.checkpointer.get(threadId))?.state ?? null;
|
|
773
|
-
}
|
|
774
|
-
async updateState(threadId, update) {
|
|
775
|
-
if (!this.checkpointer)
|
|
776
|
-
return;
|
|
777
|
-
const cp = await this.checkpointer.get(threadId);
|
|
778
|
-
if (!cp)
|
|
779
|
-
return;
|
|
780
|
-
await this.checkpointer.put({ ...cp, state: this.applyUpdate(cp.state, update), timestamp: Date.now() });
|
|
781
|
-
}
|
|
782
|
-
// ---- Time-travel ----
|
|
783
|
-
async getStateAt(threadId, step) {
|
|
784
|
-
if (!this.checkpointer)
|
|
785
|
-
return null;
|
|
786
|
-
const history = await this.checkpointer.list(threadId);
|
|
787
|
-
return history.find((c) => c.step === step)?.state ?? null;
|
|
788
|
-
}
|
|
789
|
-
async getHistory(threadId) {
|
|
790
|
-
if (!this.checkpointer)
|
|
791
|
-
return [];
|
|
792
|
-
return this.checkpointer.list(threadId);
|
|
793
|
-
}
|
|
794
|
-
async forkFrom(threadId, step, newThreadId) {
|
|
795
|
-
if (!this.checkpointer)
|
|
796
|
-
return;
|
|
797
|
-
const cp = this.checkpointer;
|
|
798
|
-
if (typeof cp.fork === "function") {
|
|
799
|
-
await cp.fork(threadId, step, newThreadId);
|
|
800
|
-
}
|
|
801
|
-
else {
|
|
802
|
-
const history = await this.checkpointer.list(threadId);
|
|
803
|
-
for (const c of history.filter((x) => x.step <= step)) {
|
|
804
|
-
await this.checkpointer.put({ ...c, threadId: newThreadId });
|
|
805
|
-
}
|
|
806
|
-
}
|
|
807
|
-
}
|
|
808
|
-
// ---- HITL ----
|
|
809
|
-
getPendingInterrupts(threadId) {
|
|
810
|
-
return this.hitlStore.getByThread(threadId);
|
|
811
|
-
}
|
|
812
|
-
hitlSessionStore() {
|
|
813
|
-
return this.hitlStore;
|
|
814
|
-
}
|
|
815
|
-
// ---- Circuit Breaker ----
|
|
816
|
-
getCircuitBreaker(nodeDef) {
|
|
817
|
-
if (!nodeDef.circuitBreaker)
|
|
818
|
-
return null;
|
|
819
|
-
let cb = this.circuitBreakers.get(nodeDef.name);
|
|
820
|
-
if (!cb) {
|
|
821
|
-
cb = new CircuitBreaker({
|
|
822
|
-
threshold: nodeDef.circuitBreaker.threshold,
|
|
823
|
-
resetAfter: nodeDef.circuitBreaker.resetAfter,
|
|
824
|
-
}, nodeDef.name);
|
|
825
|
-
this.circuitBreakers.set(nodeDef.name, cb);
|
|
826
|
-
}
|
|
827
|
-
return cb;
|
|
828
|
-
}
|
|
829
|
-
// ---- Dead Letter Queue ----
|
|
830
|
-
getDeadLetters(threadId) {
|
|
831
|
-
return this.dlq?.getAll(threadId) ?? [];
|
|
832
|
-
}
|
|
833
|
-
// ---- Helpers ----
|
|
834
|
-
async saveCheckpoint(threadId, step, state, nextNodes, pendingSends, agentId, metadata, pendingWrites) {
|
|
835
|
-
const cp = (this._perInvocationCheckpointer.get(threadId) ?? this.checkpointer);
|
|
836
|
-
if (!cp)
|
|
837
|
-
return;
|
|
838
|
-
const cpSpan = this.tracer.startCheckpointSpan("put", { threadId });
|
|
839
|
-
try {
|
|
840
|
-
await cp.put({
|
|
841
|
-
threadId, step, state, agentId, metadata, pendingWrites,
|
|
842
|
-
nextNodes: nextNodes.map(String),
|
|
843
|
-
pendingSends: pendingSends,
|
|
844
|
-
timestamp: Date.now(),
|
|
845
|
-
});
|
|
846
|
-
}
|
|
847
|
-
finally {
|
|
848
|
-
this.tracer.endSpan(cpSpan);
|
|
849
|
-
}
|
|
850
|
-
}
|
|
851
|
-
evt(event, data, step, agentId, node) {
|
|
852
|
-
return { event, data, step, timestamp: Date.now(), agentId, node };
|
|
853
|
-
}
|
|
854
|
-
}
|
|
1
|
+
// src/pregel.ts — internals moved to src/pregel/
|
|
2
|
+
export { ONIPregelRunner } from './pregel/index.js';
|
|
855
3
|
//# sourceMappingURL=pregel.js.map
|