agentflow-core 0.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/index.cjs +406 -0
- package/dist/index.d.cts +370 -0
- package/dist/index.d.ts +370 -0
- package/dist/index.js +368 -0
- package/package.json +31 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core type definitions for AgentFlow execution graphs.
|
|
3
|
+
*
|
|
4
|
+
* All public interfaces use `readonly` at every level.
|
|
5
|
+
* String literal unions are used instead of enums for clean ESM erasure.
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
/** The kind of step an execution node represents. */
|
|
9
|
+
type NodeType = 'agent' | 'tool' | 'subagent' | 'wait' | 'decision' | 'custom';
|
|
10
|
+
/** Lifecycle status of an execution node. */
|
|
11
|
+
type NodeStatus = 'running' | 'completed' | 'failed' | 'hung' | 'timeout';
|
|
12
|
+
/** Relationship type between two nodes in the execution graph. */
|
|
13
|
+
type EdgeType = 'spawned' | 'waited_on' | 'called' | 'retried' | 'branched';
|
|
14
|
+
/** Aggregate status of the entire execution graph. */
|
|
15
|
+
type GraphStatus = 'running' | 'completed' | 'failed';
|
|
16
|
+
/**
|
|
17
|
+
* Event types emitted during execution.
|
|
18
|
+
* These map to framework-level lifecycle events for adapter compatibility.
|
|
19
|
+
*/
|
|
20
|
+
type TraceEventType = 'agent_start' | 'agent_end' | 'tool_start' | 'tool_end' | 'tool_error' | 'subagent_spawn' | 'decision' | 'timeout' | 'custom';
|
|
21
|
+
/** A single step in the execution graph. */
|
|
22
|
+
interface ExecutionNode {
|
|
23
|
+
readonly id: string;
|
|
24
|
+
readonly type: NodeType;
|
|
25
|
+
readonly name: string;
|
|
26
|
+
/** Epoch milliseconds (Date.now()). */
|
|
27
|
+
readonly startTime: number;
|
|
28
|
+
/** Epoch milliseconds. `null` while the node is still running. */
|
|
29
|
+
readonly endTime: number | null;
|
|
30
|
+
readonly status: NodeStatus;
|
|
31
|
+
/** `null` for the root node. */
|
|
32
|
+
readonly parentId: string | null;
|
|
33
|
+
/** IDs of child nodes spawned by this node. */
|
|
34
|
+
readonly children: readonly string[];
|
|
35
|
+
/** Arbitrary key-value data attached to this node. */
|
|
36
|
+
readonly metadata: Readonly<Record<string, unknown>>;
|
|
37
|
+
/** Mutable-at-build-time state snapshot for this node. */
|
|
38
|
+
readonly state: Readonly<Record<string, unknown>>;
|
|
39
|
+
}
|
|
40
|
+
/** A directed relationship between two execution nodes. */
|
|
41
|
+
interface ExecutionEdge {
|
|
42
|
+
readonly from: string;
|
|
43
|
+
readonly to: string;
|
|
44
|
+
readonly type: EdgeType;
|
|
45
|
+
}
|
|
46
|
+
/** A raw event recorded during execution, before graph construction. */
|
|
47
|
+
interface TraceEvent {
|
|
48
|
+
readonly timestamp: number;
|
|
49
|
+
readonly eventType: TraceEventType;
|
|
50
|
+
readonly nodeId: string;
|
|
51
|
+
readonly data: Readonly<Record<string, unknown>>;
|
|
52
|
+
}
|
|
53
|
+
/** The complete execution graph for one agent run. */
|
|
54
|
+
interface ExecutionGraph {
|
|
55
|
+
readonly id: string;
|
|
56
|
+
readonly rootNodeId: string;
|
|
57
|
+
/** All nodes indexed by ID. */
|
|
58
|
+
readonly nodes: ReadonlyMap<string, ExecutionNode>;
|
|
59
|
+
readonly edges: readonly ExecutionEdge[];
|
|
60
|
+
readonly startTime: number;
|
|
61
|
+
/** `null` if the graph is still running. */
|
|
62
|
+
readonly endTime: number | null;
|
|
63
|
+
readonly status: GraphStatus;
|
|
64
|
+
readonly trigger: string;
|
|
65
|
+
readonly agentId: string;
|
|
66
|
+
/** Full ordered event log for auditability. */
|
|
67
|
+
readonly events: readonly TraceEvent[];
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Configuration for AgentFlow.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* const builder = createGraphBuilder({
|
|
75
|
+
* agentId: 'portfolio-recon',
|
|
76
|
+
* trigger: 'user-request',
|
|
77
|
+
* });
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
interface AgentFlowConfig {
|
|
81
|
+
/** Identifier for the agent whose execution is being traced. */
|
|
82
|
+
readonly agentId?: string;
|
|
83
|
+
/** What initiated this execution (e.g. "user-request", "cron-job"). */
|
|
84
|
+
readonly trigger?: string;
|
|
85
|
+
/** Display name for the execution graph. */
|
|
86
|
+
readonly name?: string;
|
|
87
|
+
/** Output writer for completed graphs. */
|
|
88
|
+
readonly writer?: Writer;
|
|
89
|
+
/** Framework adapters to install. */
|
|
90
|
+
readonly adapters?: readonly Adapter[];
|
|
91
|
+
/** Override the default counter-based ID generator. */
|
|
92
|
+
readonly idGenerator?: () => string;
|
|
93
|
+
/** Timeout configuration in milliseconds. */
|
|
94
|
+
readonly timeout?: {
|
|
95
|
+
readonly default?: number;
|
|
96
|
+
readonly tool?: number;
|
|
97
|
+
readonly agent?: number;
|
|
98
|
+
};
|
|
99
|
+
/** Custom logger (defaults to console.warn). */
|
|
100
|
+
readonly logger?: (message: string) => void;
|
|
101
|
+
/** Error callback for internal failures. */
|
|
102
|
+
readonly onError?: (error: unknown) => void;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Output adapter interface. Writers receive the completed execution graph
|
|
106
|
+
* and persist it to their target (console, file, etc.).
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```ts
|
|
110
|
+
* const myWriter: Writer = {
|
|
111
|
+
* write: async (graph) => { console.log(graph.id); },
|
|
112
|
+
* };
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
interface Writer {
|
|
116
|
+
/** Write the execution graph to the output target. */
|
|
117
|
+
write(graph: ExecutionGraph): Promise<void>;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Framework adapter interface. Adapters hook into agent runtime lifecycle
|
|
121
|
+
* events and translate them into graph builder calls.
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```ts
|
|
125
|
+
* const myAdapter: Adapter = {
|
|
126
|
+
* name: 'my-framework',
|
|
127
|
+
* attach: (builder) => { /* hook into runtime *\/ },
|
|
128
|
+
* detach: () => { /* unhook *\/ },
|
|
129
|
+
* };
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
interface Adapter {
|
|
133
|
+
/** Human-readable adapter name. */
|
|
134
|
+
readonly name: string;
|
|
135
|
+
/** Hook into the framework's lifecycle, calling builder methods on events. */
|
|
136
|
+
attach(builder: GraphBuilder): void;
|
|
137
|
+
/** Unhook from the framework (cleanup). */
|
|
138
|
+
detach(): void;
|
|
139
|
+
}
|
|
140
|
+
/** Options for starting a new execution node. */
|
|
141
|
+
interface StartNodeOptions {
|
|
142
|
+
readonly type: NodeType;
|
|
143
|
+
readonly name: string;
|
|
144
|
+
/** Explicit parent node ID. If omitted, uses the `withParent` stack context. */
|
|
145
|
+
readonly parentId?: string;
|
|
146
|
+
readonly metadata?: Record<string, unknown>;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Mutable graph builder returned by `createGraphBuilder`.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```ts
|
|
153
|
+
* const builder = createGraphBuilder({ agentId: 'main' });
|
|
154
|
+
* const rootId = builder.startNode({ type: 'agent', name: 'main' });
|
|
155
|
+
* const toolId = builder.startNode({ type: 'tool', name: 'search', parentId: rootId });
|
|
156
|
+
* builder.endNode(toolId);
|
|
157
|
+
* builder.endNode(rootId);
|
|
158
|
+
* const graph = builder.build();
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
161
|
+
interface GraphBuilder {
|
|
162
|
+
/** The graph's ID, available before `build()`. */
|
|
163
|
+
readonly graphId: string;
|
|
164
|
+
/** Start a new execution node. Returns the generated node ID. */
|
|
165
|
+
startNode(opts: StartNodeOptions): string;
|
|
166
|
+
/** End a node. Status defaults to `'completed'`. */
|
|
167
|
+
endNode(nodeId: string, status?: NodeStatus): void;
|
|
168
|
+
/** Mark a node as failed with an error. */
|
|
169
|
+
failNode(nodeId: string, error: Error | string): void;
|
|
170
|
+
/** Add an explicit edge between two nodes. */
|
|
171
|
+
addEdge(from: string, to: string, type: EdgeType): void;
|
|
172
|
+
/**
|
|
173
|
+
* Record a trace event. Timestamp is added automatically.
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```ts
|
|
177
|
+
* builder.pushEvent({ eventType: 'custom', nodeId: rootId, data: { key: 'value' } });
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
pushEvent(event: Omit<TraceEvent, 'timestamp'>): void;
|
|
181
|
+
/** Shallow-merge state into a node's state object. */
|
|
182
|
+
updateState(nodeId: string, state: Record<string, unknown>): void;
|
|
183
|
+
/**
|
|
184
|
+
* Execute `fn` with an implicit parent context.
|
|
185
|
+
* Any `startNode` calls inside `fn` that omit `parentId`
|
|
186
|
+
* will automatically use `parentId` as their parent.
|
|
187
|
+
*/
|
|
188
|
+
withParent<T>(parentId: string, fn: () => T): T;
|
|
189
|
+
/**
|
|
190
|
+
* Return a frozen snapshot of the current graph state without finalising.
|
|
191
|
+
* The builder remains usable after calling this.
|
|
192
|
+
*/
|
|
193
|
+
getSnapshot(): ExecutionGraph;
|
|
194
|
+
/** Freeze and return the completed execution graph. Throws if no root node exists. */
|
|
195
|
+
build(): ExecutionGraph;
|
|
196
|
+
}
|
|
197
|
+
/** Aggregate statistics for an execution graph. */
|
|
198
|
+
interface GraphStats {
|
|
199
|
+
readonly totalNodes: number;
|
|
200
|
+
readonly byStatus: Readonly<Record<NodeStatus, number>>;
|
|
201
|
+
readonly byType: Readonly<Record<NodeType, number>>;
|
|
202
|
+
readonly depth: number;
|
|
203
|
+
readonly duration: number;
|
|
204
|
+
readonly failureCount: number;
|
|
205
|
+
readonly hungCount: number;
|
|
206
|
+
}
|
|
207
|
+
/** @internal Mutable version of ExecutionNode used during graph construction. */
|
|
208
|
+
interface MutableExecutionNode {
|
|
209
|
+
id: string;
|
|
210
|
+
type: NodeType;
|
|
211
|
+
name: string;
|
|
212
|
+
startTime: number;
|
|
213
|
+
endTime: number | null;
|
|
214
|
+
status: NodeStatus;
|
|
215
|
+
parentId: string | null;
|
|
216
|
+
children: string[];
|
|
217
|
+
metadata: Record<string, unknown>;
|
|
218
|
+
state: Record<string, unknown>;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Closure-based factory for constructing execution graphs.
|
|
223
|
+
*
|
|
224
|
+
* Zero dependencies. Counter-based IDs by default, injectable for testing.
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* ```ts
|
|
228
|
+
* const builder = createGraphBuilder({ agentId: 'main', trigger: 'user-request' });
|
|
229
|
+
* const rootId = builder.startNode({ type: 'agent', name: 'main' });
|
|
230
|
+
* const toolId = builder.startNode({ type: 'tool', name: 'search', parentId: rootId });
|
|
231
|
+
* builder.endNode(toolId);
|
|
232
|
+
* builder.endNode(rootId);
|
|
233
|
+
* const graph = builder.build();
|
|
234
|
+
* ```
|
|
235
|
+
* @module
|
|
236
|
+
*/
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Create a new execution graph builder.
|
|
240
|
+
*
|
|
241
|
+
* @param config - Optional configuration (agentId, trigger, custom ID generator, etc.).
|
|
242
|
+
* @returns A `GraphBuilder` with methods to construct the graph incrementally.
|
|
243
|
+
*
|
|
244
|
+
* @example
|
|
245
|
+
* ```ts
|
|
246
|
+
* const builder = createGraphBuilder({ agentId: 'portfolio-recon', trigger: 'cron' });
|
|
247
|
+
* const rootId = builder.startNode({ type: 'agent', name: 'recon' });
|
|
248
|
+
* builder.endNode(rootId);
|
|
249
|
+
* const graph = builder.build();
|
|
250
|
+
* ```
|
|
251
|
+
*/
|
|
252
|
+
declare function createGraphBuilder(config?: AgentFlowConfig): GraphBuilder;
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Pure query functions for interrogating a built `ExecutionGraph`.
|
|
256
|
+
* Every function takes a frozen graph and returns derived data without mutation.
|
|
257
|
+
* @module
|
|
258
|
+
*/
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Find a node by its ID.
|
|
262
|
+
*
|
|
263
|
+
* @param graph - The execution graph to search.
|
|
264
|
+
* @param nodeId - The node ID to look up.
|
|
265
|
+
* @returns The node, or `undefined` if not found.
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* ```ts
|
|
269
|
+
* const node = getNode(graph, 'node_002');
|
|
270
|
+
* if (node) console.log(node.name);
|
|
271
|
+
* ```
|
|
272
|
+
*/
|
|
273
|
+
declare function getNode(graph: ExecutionGraph, nodeId: string): ExecutionNode | undefined;
|
|
274
|
+
/**
|
|
275
|
+
* Get the direct children of a node.
|
|
276
|
+
*
|
|
277
|
+
* @param graph - The execution graph to search.
|
|
278
|
+
* @param nodeId - The parent node ID.
|
|
279
|
+
* @returns Array of child nodes (may be empty).
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* ```ts
|
|
283
|
+
* const children = getChildren(graph, rootId);
|
|
284
|
+
* ```
|
|
285
|
+
*/
|
|
286
|
+
declare function getChildren(graph: ExecutionGraph, nodeId: string): ExecutionNode[];
|
|
287
|
+
/**
|
|
288
|
+
* Get the parent of a node.
|
|
289
|
+
*
|
|
290
|
+
* @param graph - The execution graph to search.
|
|
291
|
+
* @param nodeId - The child node ID.
|
|
292
|
+
* @returns The parent node, or `undefined` if root or not found.
|
|
293
|
+
*
|
|
294
|
+
* @example
|
|
295
|
+
* ```ts
|
|
296
|
+
* const parent = getParent(graph, toolId);
|
|
297
|
+
* ```
|
|
298
|
+
*/
|
|
299
|
+
declare function getParent(graph: ExecutionGraph, nodeId: string): ExecutionNode | undefined;
|
|
300
|
+
/**
|
|
301
|
+
* Find all nodes with a failure-category status: `failed`, `hung`, or `timeout`.
|
|
302
|
+
*
|
|
303
|
+
* @param graph - The execution graph to search.
|
|
304
|
+
* @returns Array of nodes with failure statuses (may be empty).
|
|
305
|
+
*/
|
|
306
|
+
declare function getFailures(graph: ExecutionGraph): ExecutionNode[];
|
|
307
|
+
/**
|
|
308
|
+
* Find all nodes that are still running (status `'running'`, no endTime).
|
|
309
|
+
*
|
|
310
|
+
* @param graph - The execution graph to search.
|
|
311
|
+
* @returns Array of running/hung nodes.
|
|
312
|
+
*/
|
|
313
|
+
declare function getHungNodes(graph: ExecutionGraph): ExecutionNode[];
|
|
314
|
+
/**
|
|
315
|
+
* Find the critical path: the longest-duration path from the root to any leaf node.
|
|
316
|
+
* Uses node duration (endTime - startTime) as the weight.
|
|
317
|
+
* Running nodes use `Date.now()` as a provisional endTime.
|
|
318
|
+
*
|
|
319
|
+
* @param graph - The execution graph to analyse.
|
|
320
|
+
* @returns Nodes ordered from root to the deepest leaf on the longest path.
|
|
321
|
+
*/
|
|
322
|
+
declare function getCriticalPath(graph: ExecutionGraph): ExecutionNode[];
|
|
323
|
+
/**
|
|
324
|
+
* Find what a node is waiting on.
|
|
325
|
+
* Returns nodes connected via `waited_on` edges where the given nodeId is the `from` side.
|
|
326
|
+
*
|
|
327
|
+
* @param graph - The execution graph to search.
|
|
328
|
+
* @param nodeId - The node that is doing the waiting.
|
|
329
|
+
* @returns Array of nodes that are being waited on.
|
|
330
|
+
*/
|
|
331
|
+
declare function findWaitingOn(graph: ExecutionGraph, nodeId: string): ExecutionNode[];
|
|
332
|
+
/**
|
|
333
|
+
* Get all descendants of a node (children, grandchildren, etc.) in breadth-first order.
|
|
334
|
+
* The given node itself is NOT included.
|
|
335
|
+
*
|
|
336
|
+
* @param graph - The execution graph to search.
|
|
337
|
+
* @param nodeId - The ancestor node ID.
|
|
338
|
+
* @returns All descendant nodes in BFS order.
|
|
339
|
+
*/
|
|
340
|
+
declare function getSubtree(graph: ExecutionGraph, nodeId: string): ExecutionNode[];
|
|
341
|
+
/**
|
|
342
|
+
* Total wall-clock duration of the graph in milliseconds.
|
|
343
|
+
* If the graph is still running, uses `Date.now()` as the provisional end.
|
|
344
|
+
*
|
|
345
|
+
* @param graph - The execution graph.
|
|
346
|
+
* @returns Duration in milliseconds.
|
|
347
|
+
*/
|
|
348
|
+
declare function getDuration(graph: ExecutionGraph): number;
|
|
349
|
+
/**
|
|
350
|
+
* Maximum nesting depth of the graph. The root node is depth 0.
|
|
351
|
+
*
|
|
352
|
+
* @param graph - The execution graph.
|
|
353
|
+
* @returns The maximum depth (0 for a single-node graph, -1 for empty).
|
|
354
|
+
*/
|
|
355
|
+
declare function getDepth(graph: ExecutionGraph): number;
|
|
356
|
+
/**
|
|
357
|
+
* Compute aggregate statistics for the execution graph.
|
|
358
|
+
*
|
|
359
|
+
* @param graph - The execution graph to analyse.
|
|
360
|
+
* @returns Statistics including node counts by type and status, depth, duration, and failure counts.
|
|
361
|
+
*
|
|
362
|
+
* @example
|
|
363
|
+
* ```ts
|
|
364
|
+
* const stats = getStats(graph);
|
|
365
|
+
* console.log(`${stats.totalNodes} nodes, ${stats.failureCount} failures`);
|
|
366
|
+
* ```
|
|
367
|
+
*/
|
|
368
|
+
declare function getStats(graph: ExecutionGraph): GraphStats;
|
|
369
|
+
|
|
370
|
+
export { type Adapter, type AgentFlowConfig, type EdgeType, type ExecutionEdge, type ExecutionGraph, type ExecutionNode, type GraphBuilder, type GraphStats, type GraphStatus, type MutableExecutionNode, type NodeStatus, type NodeType, type StartNodeOptions, type TraceEvent, type TraceEventType, type Writer, createGraphBuilder, findWaitingOn, getChildren, getCriticalPath, getDepth, getDuration, getFailures, getHungNodes, getNode, getParent, getStats, getSubtree };
|