@wundr.io/langgraph-orchestrator 1.0.3
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/README.md +842 -0
- package/dist/checkpointing.d.ts +265 -0
- package/dist/checkpointing.d.ts.map +1 -0
- package/dist/checkpointing.js +577 -0
- package/dist/checkpointing.js.map +1 -0
- package/dist/edges/conditional-edge.d.ts +230 -0
- package/dist/edges/conditional-edge.d.ts.map +1 -0
- package/dist/edges/conditional-edge.js +439 -0
- package/dist/edges/conditional-edge.js.map +1 -0
- package/dist/edges/loop-edge.d.ts +290 -0
- package/dist/edges/loop-edge.d.ts.map +1 -0
- package/dist/edges/loop-edge.js +503 -0
- package/dist/edges/loop-edge.js.map +1 -0
- package/dist/index.d.ts +125 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +269 -0
- package/dist/index.js.map +1 -0
- package/dist/nodes/decision-node.d.ts +276 -0
- package/dist/nodes/decision-node.d.ts.map +1 -0
- package/dist/nodes/decision-node.js +403 -0
- package/dist/nodes/decision-node.js.map +1 -0
- package/dist/nodes/human-node.d.ts +272 -0
- package/dist/nodes/human-node.d.ts.map +1 -0
- package/dist/nodes/human-node.js +394 -0
- package/dist/nodes/human-node.js.map +1 -0
- package/dist/nodes/llm-node.d.ts +173 -0
- package/dist/nodes/llm-node.d.ts.map +1 -0
- package/dist/nodes/llm-node.js +325 -0
- package/dist/nodes/llm-node.js.map +1 -0
- package/dist/nodes/tool-node.d.ts +151 -0
- package/dist/nodes/tool-node.d.ts.map +1 -0
- package/dist/nodes/tool-node.js +373 -0
- package/dist/nodes/tool-node.js.map +1 -0
- package/dist/prebuilt-graphs/plan-execute-refine.d.ts +149 -0
- package/dist/prebuilt-graphs/plan-execute-refine.d.ts.map +1 -0
- package/dist/prebuilt-graphs/plan-execute-refine.js +600 -0
- package/dist/prebuilt-graphs/plan-execute-refine.js.map +1 -0
- package/dist/state-graph.d.ts +158 -0
- package/dist/state-graph.d.ts.map +1 -0
- package/dist/state-graph.js +756 -0
- package/dist/state-graph.js.map +1 -0
- package/dist/types.d.ts +762 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +73 -0
- package/dist/types.js.map +1 -0
- package/package.json +57 -0
- package/src/checkpointing.ts +702 -0
- package/src/edges/conditional-edge.ts +518 -0
- package/src/edges/loop-edge.ts +623 -0
- package/src/index.ts +416 -0
- package/src/nodes/decision-node.ts +538 -0
- package/src/nodes/human-node.ts +572 -0
- package/src/nodes/llm-node.ts +448 -0
- package/src/nodes/tool-node.ts +525 -0
- package/src/prebuilt-graphs/plan-execute-refine.ts +769 -0
- package/src/state-graph.ts +990 -0
- package/src/types.ts +729 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,729 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for LangGraph-style workflow orchestration
|
|
3
|
+
* @module @wundr.io/langgraph-orchestrator
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
|
|
8
|
+
// ============================================================================
|
|
9
|
+
// Core State Types
|
|
10
|
+
// ============================================================================
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Generic agent state that can be extended for specific workflows
|
|
14
|
+
*/
|
|
15
|
+
export interface AgentState {
|
|
16
|
+
/** Unique identifier for this state instance */
|
|
17
|
+
readonly id: string;
|
|
18
|
+
/** Current messages in the conversation/workflow */
|
|
19
|
+
readonly messages: Message[];
|
|
20
|
+
/** Arbitrary key-value data store */
|
|
21
|
+
readonly data: Record<string, unknown>;
|
|
22
|
+
/** Current step in the workflow */
|
|
23
|
+
readonly currentStep: string;
|
|
24
|
+
/** Previous steps for backtracking */
|
|
25
|
+
readonly history: StateHistoryEntry[];
|
|
26
|
+
/** Error information if workflow failed */
|
|
27
|
+
readonly error?: WorkflowError;
|
|
28
|
+
/** Timestamp of state creation */
|
|
29
|
+
readonly createdAt: Date;
|
|
30
|
+
/** Timestamp of last state update */
|
|
31
|
+
readonly updatedAt: Date;
|
|
32
|
+
/** Metadata for tracking and debugging */
|
|
33
|
+
readonly metadata: StateMetadata;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Message in the agent conversation
|
|
38
|
+
*/
|
|
39
|
+
export interface Message {
|
|
40
|
+
/** Unique message identifier */
|
|
41
|
+
readonly id: string;
|
|
42
|
+
/** Role of the message sender */
|
|
43
|
+
readonly role: MessageRole;
|
|
44
|
+
/** Content of the message */
|
|
45
|
+
readonly content: string;
|
|
46
|
+
/** Tool calls if any */
|
|
47
|
+
readonly toolCalls?: ToolCall[];
|
|
48
|
+
/** Tool response if this is a tool result */
|
|
49
|
+
readonly toolResult?: ToolResult;
|
|
50
|
+
/** Timestamp of message creation */
|
|
51
|
+
readonly timestamp: Date;
|
|
52
|
+
/** Additional message metadata */
|
|
53
|
+
readonly metadata?: Record<string, unknown>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Role of a message sender
|
|
58
|
+
*/
|
|
59
|
+
export type MessageRole = 'system' | 'user' | 'assistant' | 'tool';
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Tool call request from LLM
|
|
63
|
+
*/
|
|
64
|
+
export interface ToolCall {
|
|
65
|
+
/** Unique identifier for this tool call */
|
|
66
|
+
readonly id: string;
|
|
67
|
+
/** Name of the tool to call */
|
|
68
|
+
readonly name: string;
|
|
69
|
+
/** Arguments to pass to the tool */
|
|
70
|
+
readonly arguments: Record<string, unknown>;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Result from a tool execution
|
|
75
|
+
*/
|
|
76
|
+
export interface ToolResult {
|
|
77
|
+
/** ID of the tool call this is responding to */
|
|
78
|
+
readonly toolCallId: string;
|
|
79
|
+
/** Result content */
|
|
80
|
+
readonly content: string;
|
|
81
|
+
/** Whether the tool execution was successful */
|
|
82
|
+
readonly success: boolean;
|
|
83
|
+
/** Error message if failed */
|
|
84
|
+
readonly error?: string;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Entry in state history for time-travel debugging
|
|
89
|
+
*/
|
|
90
|
+
export interface StateHistoryEntry {
|
|
91
|
+
/** Step name that produced this state */
|
|
92
|
+
readonly step: string;
|
|
93
|
+
/** Timestamp when this state was created */
|
|
94
|
+
readonly timestamp: Date;
|
|
95
|
+
/** Snapshot of the state at this point */
|
|
96
|
+
readonly snapshot: Partial<AgentState>;
|
|
97
|
+
/** Changes made in this step */
|
|
98
|
+
readonly changes: StateChange[];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Individual state change record
|
|
103
|
+
*/
|
|
104
|
+
export interface StateChange {
|
|
105
|
+
/** Path to the changed property */
|
|
106
|
+
readonly path: string;
|
|
107
|
+
/** Previous value */
|
|
108
|
+
readonly previousValue: unknown;
|
|
109
|
+
/** New value */
|
|
110
|
+
readonly newValue: unknown;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Error information for failed workflows
|
|
115
|
+
*/
|
|
116
|
+
export interface WorkflowError {
|
|
117
|
+
/** Error code */
|
|
118
|
+
readonly code: string;
|
|
119
|
+
/** Human-readable error message */
|
|
120
|
+
readonly message: string;
|
|
121
|
+
/** Stack trace if available */
|
|
122
|
+
readonly stack?: string;
|
|
123
|
+
/** Node where error occurred */
|
|
124
|
+
readonly node?: string;
|
|
125
|
+
/** Whether the error is recoverable */
|
|
126
|
+
readonly recoverable: boolean;
|
|
127
|
+
/** Suggested recovery actions */
|
|
128
|
+
readonly recoveryHints?: string[];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Metadata for state tracking
|
|
133
|
+
*/
|
|
134
|
+
export interface StateMetadata {
|
|
135
|
+
/** Session ID for cross-session tracking */
|
|
136
|
+
readonly sessionId: string;
|
|
137
|
+
/** User ID if applicable */
|
|
138
|
+
readonly userId?: string;
|
|
139
|
+
/** Workflow execution ID */
|
|
140
|
+
readonly executionId: string;
|
|
141
|
+
/** Total number of steps executed */
|
|
142
|
+
readonly stepCount: number;
|
|
143
|
+
/** Total tokens consumed (if LLM involved) */
|
|
144
|
+
readonly tokensUsed?: number;
|
|
145
|
+
/** Custom tags for filtering */
|
|
146
|
+
readonly tags: string[];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// ============================================================================
|
|
150
|
+
// Graph Configuration Types
|
|
151
|
+
// ============================================================================
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Configuration for a workflow graph
|
|
155
|
+
*/
|
|
156
|
+
export interface GraphConfig {
|
|
157
|
+
/** Unique identifier for the graph */
|
|
158
|
+
readonly id: string;
|
|
159
|
+
/** Human-readable name */
|
|
160
|
+
readonly name: string;
|
|
161
|
+
/** Description of what this graph does */
|
|
162
|
+
readonly description?: string;
|
|
163
|
+
/** Entry point node name */
|
|
164
|
+
readonly entryPoint: string;
|
|
165
|
+
/** Node definitions */
|
|
166
|
+
readonly nodes: Map<string, NodeDefinition>;
|
|
167
|
+
/** Edge definitions */
|
|
168
|
+
readonly edges: Map<string, EdgeDefinition[]>;
|
|
169
|
+
/** Global graph configuration */
|
|
170
|
+
readonly config: GraphGlobalConfig;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Global configuration options for graph execution
|
|
175
|
+
*/
|
|
176
|
+
export interface GraphGlobalConfig {
|
|
177
|
+
/** Maximum number of iterations (cycle protection) */
|
|
178
|
+
readonly maxIterations: number;
|
|
179
|
+
/** Timeout in milliseconds */
|
|
180
|
+
readonly timeout: number;
|
|
181
|
+
/** Whether to enable checkpointing */
|
|
182
|
+
readonly checkpointEnabled: boolean;
|
|
183
|
+
/** Checkpoint interval (every N steps) */
|
|
184
|
+
readonly checkpointInterval: number;
|
|
185
|
+
/** Whether to enable parallel execution where possible */
|
|
186
|
+
readonly parallelExecution: boolean;
|
|
187
|
+
/** Retry configuration */
|
|
188
|
+
readonly retry: RetryConfig;
|
|
189
|
+
/** Logging level */
|
|
190
|
+
readonly logLevel: LogLevel;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Retry configuration for failed nodes
|
|
195
|
+
*/
|
|
196
|
+
export interface RetryConfig {
|
|
197
|
+
/** Maximum number of retries */
|
|
198
|
+
readonly maxRetries: number;
|
|
199
|
+
/** Initial delay in milliseconds */
|
|
200
|
+
readonly initialDelay: number;
|
|
201
|
+
/** Backoff multiplier */
|
|
202
|
+
readonly backoffMultiplier: number;
|
|
203
|
+
/** Maximum delay in milliseconds */
|
|
204
|
+
readonly maxDelay: number;
|
|
205
|
+
/** Error codes that should trigger retry */
|
|
206
|
+
readonly retryableErrors: string[];
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Logging level for graph execution
|
|
211
|
+
*/
|
|
212
|
+
export type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent';
|
|
213
|
+
|
|
214
|
+
// ============================================================================
|
|
215
|
+
// Node Types
|
|
216
|
+
// ============================================================================
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Definition of a node in the workflow graph
|
|
220
|
+
*/
|
|
221
|
+
export interface NodeDefinition<TState extends AgentState = AgentState> {
|
|
222
|
+
/** Unique identifier for this node */
|
|
223
|
+
readonly id: string;
|
|
224
|
+
/** Human-readable name */
|
|
225
|
+
readonly name: string;
|
|
226
|
+
/** Node type */
|
|
227
|
+
readonly type: NodeType;
|
|
228
|
+
/** Node-specific configuration */
|
|
229
|
+
readonly config: NodeConfig;
|
|
230
|
+
/** The function to execute for this node */
|
|
231
|
+
readonly execute: NodeExecutor<TState>;
|
|
232
|
+
/** Pre-execution hooks */
|
|
233
|
+
readonly preHooks?: NodeHook<TState>[];
|
|
234
|
+
/** Post-execution hooks */
|
|
235
|
+
readonly postHooks?: NodeHook<TState>[];
|
|
236
|
+
/** Validation schema for node output */
|
|
237
|
+
readonly outputSchema?: z.ZodSchema;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Types of nodes in the workflow
|
|
242
|
+
*/
|
|
243
|
+
export type NodeType =
|
|
244
|
+
| 'llm' // LLM-based decision/generation
|
|
245
|
+
| 'tool' // Tool execution
|
|
246
|
+
| 'decision' // Conditional branching
|
|
247
|
+
| 'human' // Human-in-the-loop
|
|
248
|
+
| 'aggregate' // Combine multiple inputs
|
|
249
|
+
| 'transform' // State transformation
|
|
250
|
+
| 'start' // Entry point
|
|
251
|
+
| 'end'; // Exit point
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Node configuration options
|
|
255
|
+
*/
|
|
256
|
+
export interface NodeConfig {
|
|
257
|
+
/** Whether this node can run in parallel with others */
|
|
258
|
+
readonly parallel?: boolean;
|
|
259
|
+
/** Timeout for this specific node */
|
|
260
|
+
readonly timeout?: number;
|
|
261
|
+
/** Retry configuration override */
|
|
262
|
+
readonly retry?: Partial<RetryConfig>;
|
|
263
|
+
/** Custom metadata */
|
|
264
|
+
readonly metadata?: Record<string, unknown>;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Function signature for node execution
|
|
269
|
+
*/
|
|
270
|
+
export type NodeExecutor<TState extends AgentState = AgentState> = (
|
|
271
|
+
state: TState,
|
|
272
|
+
context: NodeContext
|
|
273
|
+
) => Promise<NodeResult<TState>>;
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Context provided to node executors
|
|
277
|
+
*/
|
|
278
|
+
export interface NodeContext {
|
|
279
|
+
/** Node definition */
|
|
280
|
+
readonly node: NodeDefinition;
|
|
281
|
+
/** Graph configuration */
|
|
282
|
+
readonly graph: GraphConfig;
|
|
283
|
+
/** Execution ID */
|
|
284
|
+
readonly executionId: string;
|
|
285
|
+
/** Current iteration count */
|
|
286
|
+
readonly iterationCount: number;
|
|
287
|
+
/** Shared services */
|
|
288
|
+
readonly services: NodeServices;
|
|
289
|
+
/** Abort signal for cancellation */
|
|
290
|
+
readonly signal?: AbortSignal;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Services available to nodes
|
|
295
|
+
*/
|
|
296
|
+
export interface NodeServices {
|
|
297
|
+
/** Logger instance */
|
|
298
|
+
readonly logger: Logger;
|
|
299
|
+
/** Checkpointing service */
|
|
300
|
+
readonly checkpointer?: GraphCheckpointer;
|
|
301
|
+
/** Tool registry */
|
|
302
|
+
readonly toolRegistry?: ToolRegistry;
|
|
303
|
+
/** LLM provider */
|
|
304
|
+
readonly llmProvider?: LLMProvider;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Logger interface
|
|
309
|
+
*/
|
|
310
|
+
export interface Logger {
|
|
311
|
+
debug(message: string, data?: Record<string, unknown>): void;
|
|
312
|
+
info(message: string, data?: Record<string, unknown>): void;
|
|
313
|
+
warn(message: string, data?: Record<string, unknown>): void;
|
|
314
|
+
error(message: string, data?: Record<string, unknown>): void;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Tool registry interface
|
|
319
|
+
*/
|
|
320
|
+
export interface ToolRegistry {
|
|
321
|
+
get(name: string): Tool | undefined;
|
|
322
|
+
list(): Tool[];
|
|
323
|
+
register(tool: Tool): void;
|
|
324
|
+
unregister(name: string): void;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Tool definition
|
|
329
|
+
*/
|
|
330
|
+
export interface Tool {
|
|
331
|
+
/** Unique tool name */
|
|
332
|
+
readonly name: string;
|
|
333
|
+
/** Tool description for LLM */
|
|
334
|
+
readonly description: string;
|
|
335
|
+
/** Input schema */
|
|
336
|
+
readonly inputSchema: z.ZodSchema;
|
|
337
|
+
/** Output schema */
|
|
338
|
+
readonly outputSchema?: z.ZodSchema;
|
|
339
|
+
/** Tool execution function */
|
|
340
|
+
execute(input: unknown): Promise<unknown>;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* LLM provider interface
|
|
345
|
+
*/
|
|
346
|
+
export interface LLMProvider {
|
|
347
|
+
/** Generate a response */
|
|
348
|
+
generate(request: LLMRequest): Promise<LLMResponse>;
|
|
349
|
+
/** Stream a response */
|
|
350
|
+
stream?(request: LLMRequest): AsyncIterable<LLMStreamChunk>;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* LLM request
|
|
355
|
+
*/
|
|
356
|
+
export interface LLMRequest {
|
|
357
|
+
/** Messages to send */
|
|
358
|
+
readonly messages: Message[];
|
|
359
|
+
/** Model to use */
|
|
360
|
+
readonly model?: string;
|
|
361
|
+
/** Temperature */
|
|
362
|
+
readonly temperature?: number;
|
|
363
|
+
/** Maximum tokens */
|
|
364
|
+
readonly maxTokens?: number;
|
|
365
|
+
/** Available tools */
|
|
366
|
+
readonly tools?: Tool[];
|
|
367
|
+
/** Stop sequences */
|
|
368
|
+
readonly stop?: string[];
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* LLM response
|
|
373
|
+
*/
|
|
374
|
+
export interface LLMResponse {
|
|
375
|
+
/** Generated message */
|
|
376
|
+
readonly message: Message;
|
|
377
|
+
/** Token usage */
|
|
378
|
+
readonly usage: TokenUsage;
|
|
379
|
+
/** Model used */
|
|
380
|
+
readonly model: string;
|
|
381
|
+
/** Finish reason */
|
|
382
|
+
readonly finishReason: FinishReason;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Token usage information
|
|
387
|
+
*/
|
|
388
|
+
export interface TokenUsage {
|
|
389
|
+
/** Prompt tokens */
|
|
390
|
+
readonly promptTokens: number;
|
|
391
|
+
/** Completion tokens */
|
|
392
|
+
readonly completionTokens: number;
|
|
393
|
+
/** Total tokens */
|
|
394
|
+
readonly totalTokens: number;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* Reason for LLM completion
|
|
399
|
+
*/
|
|
400
|
+
export type FinishReason =
|
|
401
|
+
| 'stop'
|
|
402
|
+
| 'length'
|
|
403
|
+
| 'tool_calls'
|
|
404
|
+
| 'content_filter'
|
|
405
|
+
| 'error';
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Streaming chunk from LLM
|
|
409
|
+
*/
|
|
410
|
+
export interface LLMStreamChunk {
|
|
411
|
+
/** Delta content */
|
|
412
|
+
readonly delta: string;
|
|
413
|
+
/** Whether this is the final chunk */
|
|
414
|
+
readonly done: boolean;
|
|
415
|
+
/** Finish reason if done */
|
|
416
|
+
readonly finishReason?: FinishReason;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Result from node execution
|
|
421
|
+
*/
|
|
422
|
+
export interface NodeResult<TState extends AgentState = AgentState> {
|
|
423
|
+
/** Updated state */
|
|
424
|
+
readonly state: TState;
|
|
425
|
+
/** Next node(s) to execute */
|
|
426
|
+
readonly next?: string | string[];
|
|
427
|
+
/** Whether to terminate the workflow */
|
|
428
|
+
readonly terminate?: boolean;
|
|
429
|
+
/** Execution metadata */
|
|
430
|
+
readonly metadata?: NodeExecutionMetadata;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Metadata from node execution
|
|
435
|
+
*/
|
|
436
|
+
export interface NodeExecutionMetadata {
|
|
437
|
+
/** Duration in milliseconds */
|
|
438
|
+
readonly duration: number;
|
|
439
|
+
/** Tokens used if LLM node */
|
|
440
|
+
readonly tokensUsed?: number;
|
|
441
|
+
/** Tool calls made */
|
|
442
|
+
readonly toolCalls?: ToolCall[];
|
|
443
|
+
/** Retry count if retried */
|
|
444
|
+
readonly retryCount?: number;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Hook function signature
|
|
449
|
+
*/
|
|
450
|
+
export type NodeHook<TState extends AgentState = AgentState> = (
|
|
451
|
+
state: TState,
|
|
452
|
+
context: NodeContext
|
|
453
|
+
) => Promise<TState>;
|
|
454
|
+
|
|
455
|
+
// ============================================================================
|
|
456
|
+
// Edge Types
|
|
457
|
+
// ============================================================================
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Definition of an edge in the workflow graph
|
|
461
|
+
*/
|
|
462
|
+
export interface EdgeDefinition {
|
|
463
|
+
/** Source node */
|
|
464
|
+
readonly from: string;
|
|
465
|
+
/** Target node */
|
|
466
|
+
readonly to: string;
|
|
467
|
+
/** Edge type */
|
|
468
|
+
readonly type: EdgeType;
|
|
469
|
+
/** Condition for conditional edges */
|
|
470
|
+
readonly condition?: EdgeCondition;
|
|
471
|
+
/** Edge metadata */
|
|
472
|
+
readonly metadata?: Record<string, unknown>;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* Types of edges
|
|
477
|
+
*/
|
|
478
|
+
export type EdgeType =
|
|
479
|
+
| 'direct' // Always follow this edge
|
|
480
|
+
| 'conditional' // Follow based on condition
|
|
481
|
+
| 'loop' // Loop back to a previous node
|
|
482
|
+
| 'parallel'; // Branch to multiple nodes
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* Condition for edge traversal
|
|
486
|
+
*/
|
|
487
|
+
export interface EdgeCondition {
|
|
488
|
+
/** Condition type */
|
|
489
|
+
readonly type: ConditionType;
|
|
490
|
+
/** Field to check in state */
|
|
491
|
+
readonly field?: string;
|
|
492
|
+
/** Value to compare against */
|
|
493
|
+
readonly value?: unknown;
|
|
494
|
+
/** Custom condition function */
|
|
495
|
+
readonly evaluate?: EdgeConditionEvaluator;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Types of conditions
|
|
500
|
+
*/
|
|
501
|
+
export type ConditionType =
|
|
502
|
+
| 'equals'
|
|
503
|
+
| 'not_equals'
|
|
504
|
+
| 'contains'
|
|
505
|
+
| 'greater_than'
|
|
506
|
+
| 'less_than'
|
|
507
|
+
| 'exists'
|
|
508
|
+
| 'not_exists'
|
|
509
|
+
| 'custom';
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* Custom condition evaluator
|
|
513
|
+
*/
|
|
514
|
+
export type EdgeConditionEvaluator<TState extends AgentState = AgentState> = (
|
|
515
|
+
state: TState,
|
|
516
|
+
context: EdgeContext
|
|
517
|
+
) => Promise<boolean>;
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* Context provided to edge evaluators
|
|
521
|
+
*/
|
|
522
|
+
export interface EdgeContext {
|
|
523
|
+
/** Edge definition */
|
|
524
|
+
readonly edge: EdgeDefinition;
|
|
525
|
+
/** Source node result */
|
|
526
|
+
readonly sourceResult: NodeResult;
|
|
527
|
+
/** Graph configuration */
|
|
528
|
+
readonly graph: GraphConfig;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
// ============================================================================
|
|
532
|
+
// Checkpointing Types
|
|
533
|
+
// ============================================================================
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* Interface for checkpoint persistence
|
|
537
|
+
*/
|
|
538
|
+
export interface GraphCheckpointer {
|
|
539
|
+
/** Save a checkpoint */
|
|
540
|
+
save(checkpoint: Checkpoint): Promise<void>;
|
|
541
|
+
/** Load a checkpoint by ID */
|
|
542
|
+
load(checkpointId: string): Promise<Checkpoint | null>;
|
|
543
|
+
/** List checkpoints for an execution */
|
|
544
|
+
list(executionId: string): Promise<CheckpointSummary[]>;
|
|
545
|
+
/** Delete a checkpoint */
|
|
546
|
+
delete(checkpointId: string): Promise<void>;
|
|
547
|
+
/** Get the latest checkpoint for an execution */
|
|
548
|
+
getLatest(executionId: string): Promise<Checkpoint | null>;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* Checkpoint data structure
|
|
553
|
+
*/
|
|
554
|
+
export interface Checkpoint {
|
|
555
|
+
/** Unique checkpoint identifier */
|
|
556
|
+
readonly id: string;
|
|
557
|
+
/** Execution ID this checkpoint belongs to */
|
|
558
|
+
readonly executionId: string;
|
|
559
|
+
/** Step number */
|
|
560
|
+
readonly stepNumber: number;
|
|
561
|
+
/** Node that created this checkpoint */
|
|
562
|
+
readonly nodeName: string;
|
|
563
|
+
/** Full state snapshot */
|
|
564
|
+
readonly state: AgentState;
|
|
565
|
+
/** Timestamp of checkpoint creation */
|
|
566
|
+
readonly timestamp: Date;
|
|
567
|
+
/** Parent checkpoint ID for branching */
|
|
568
|
+
readonly parentId?: string;
|
|
569
|
+
/** Custom metadata */
|
|
570
|
+
readonly metadata?: Record<string, unknown>;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
/**
|
|
574
|
+
* Summary of a checkpoint for listing
|
|
575
|
+
*/
|
|
576
|
+
export interface CheckpointSummary {
|
|
577
|
+
/** Checkpoint ID */
|
|
578
|
+
readonly id: string;
|
|
579
|
+
/** Execution ID */
|
|
580
|
+
readonly executionId: string;
|
|
581
|
+
/** Step number */
|
|
582
|
+
readonly stepNumber: number;
|
|
583
|
+
/** Node name */
|
|
584
|
+
readonly nodeName: string;
|
|
585
|
+
/** Timestamp */
|
|
586
|
+
readonly timestamp: Date;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
// ============================================================================
|
|
590
|
+
// Execution Types
|
|
591
|
+
// ============================================================================
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* Options for graph execution
|
|
595
|
+
*/
|
|
596
|
+
export interface ExecutionOptions {
|
|
597
|
+
/** Initial state */
|
|
598
|
+
readonly initialState?: Partial<AgentState>;
|
|
599
|
+
/** Checkpoint ID to resume from */
|
|
600
|
+
readonly resumeFrom?: string;
|
|
601
|
+
/** Override graph config */
|
|
602
|
+
readonly configOverrides?: Partial<GraphGlobalConfig>;
|
|
603
|
+
/** Abort signal */
|
|
604
|
+
readonly signal?: AbortSignal;
|
|
605
|
+
/** Event handlers */
|
|
606
|
+
readonly handlers?: ExecutionHandlers;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* Event handlers for execution lifecycle
|
|
611
|
+
*/
|
|
612
|
+
export interface ExecutionHandlers {
|
|
613
|
+
/** Called when execution starts */
|
|
614
|
+
onStart?: (state: AgentState) => void;
|
|
615
|
+
/** Called when entering a node */
|
|
616
|
+
onNodeEnter?: (nodeName: string, state: AgentState) => void;
|
|
617
|
+
/** Called when exiting a node */
|
|
618
|
+
onNodeExit?: (nodeName: string, result: NodeResult) => void;
|
|
619
|
+
/** Called on checkpoint creation */
|
|
620
|
+
onCheckpoint?: (checkpoint: Checkpoint) => void;
|
|
621
|
+
/** Called on error */
|
|
622
|
+
onError?: (error: WorkflowError) => void;
|
|
623
|
+
/** Called when execution completes */
|
|
624
|
+
onComplete?: (state: AgentState) => void;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
/**
|
|
628
|
+
* Result of graph execution
|
|
629
|
+
*/
|
|
630
|
+
export interface ExecutionResult {
|
|
631
|
+
/** Final state */
|
|
632
|
+
readonly state: AgentState;
|
|
633
|
+
/** Whether execution was successful */
|
|
634
|
+
readonly success: boolean;
|
|
635
|
+
/** Error if failed */
|
|
636
|
+
readonly error?: WorkflowError;
|
|
637
|
+
/** Execution statistics */
|
|
638
|
+
readonly stats: ExecutionStats;
|
|
639
|
+
/** Path through the graph */
|
|
640
|
+
readonly path: string[];
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* Statistics from graph execution
|
|
645
|
+
*/
|
|
646
|
+
export interface ExecutionStats {
|
|
647
|
+
/** Total execution time in milliseconds */
|
|
648
|
+
readonly duration: number;
|
|
649
|
+
/** Number of nodes executed */
|
|
650
|
+
readonly nodesExecuted: number;
|
|
651
|
+
/** Number of iterations */
|
|
652
|
+
readonly iterations: number;
|
|
653
|
+
/** Total tokens used */
|
|
654
|
+
readonly tokensUsed: number;
|
|
655
|
+
/** Checkpoints created */
|
|
656
|
+
readonly checkpointsCreated: number;
|
|
657
|
+
/** Retries performed */
|
|
658
|
+
readonly retries: number;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
// ============================================================================
|
|
662
|
+
// Zod Schemas for Validation
|
|
663
|
+
// ============================================================================
|
|
664
|
+
|
|
665
|
+
/**
|
|
666
|
+
* Schema for validating message structure
|
|
667
|
+
*/
|
|
668
|
+
export const MessageSchema = z.object({
|
|
669
|
+
id: z.string(),
|
|
670
|
+
role: z.enum(['system', 'user', 'assistant', 'tool']),
|
|
671
|
+
content: z.string(),
|
|
672
|
+
toolCalls: z
|
|
673
|
+
.array(
|
|
674
|
+
z.object({
|
|
675
|
+
id: z.string(),
|
|
676
|
+
name: z.string(),
|
|
677
|
+
arguments: z.record(z.unknown()),
|
|
678
|
+
}),
|
|
679
|
+
)
|
|
680
|
+
.optional(),
|
|
681
|
+
toolResult: z
|
|
682
|
+
.object({
|
|
683
|
+
toolCallId: z.string(),
|
|
684
|
+
content: z.string(),
|
|
685
|
+
success: z.boolean(),
|
|
686
|
+
error: z.string().optional(),
|
|
687
|
+
})
|
|
688
|
+
.optional(),
|
|
689
|
+
timestamp: z.date(),
|
|
690
|
+
metadata: z.record(z.unknown()).optional(),
|
|
691
|
+
});
|
|
692
|
+
|
|
693
|
+
/**
|
|
694
|
+
* Schema for validating graph configuration
|
|
695
|
+
*/
|
|
696
|
+
export const GraphConfigSchema = z.object({
|
|
697
|
+
id: z.string(),
|
|
698
|
+
name: z.string(),
|
|
699
|
+
description: z.string().optional(),
|
|
700
|
+
entryPoint: z.string(),
|
|
701
|
+
config: z.object({
|
|
702
|
+
maxIterations: z.number().min(1).max(10000),
|
|
703
|
+
timeout: z.number().min(0),
|
|
704
|
+
checkpointEnabled: z.boolean(),
|
|
705
|
+
checkpointInterval: z.number().min(1),
|
|
706
|
+
parallelExecution: z.boolean(),
|
|
707
|
+
retry: z.object({
|
|
708
|
+
maxRetries: z.number().min(0),
|
|
709
|
+
initialDelay: z.number().min(0),
|
|
710
|
+
backoffMultiplier: z.number().min(1),
|
|
711
|
+
maxDelay: z.number().min(0),
|
|
712
|
+
retryableErrors: z.array(z.string()),
|
|
713
|
+
}),
|
|
714
|
+
logLevel: z.enum(['debug', 'info', 'warn', 'error', 'silent']),
|
|
715
|
+
}),
|
|
716
|
+
});
|
|
717
|
+
|
|
718
|
+
/**
|
|
719
|
+
* Schema for validating checkpoint data
|
|
720
|
+
*/
|
|
721
|
+
export const CheckpointSchema = z.object({
|
|
722
|
+
id: z.string(),
|
|
723
|
+
executionId: z.string(),
|
|
724
|
+
stepNumber: z.number(),
|
|
725
|
+
nodeName: z.string(),
|
|
726
|
+
timestamp: z.date(),
|
|
727
|
+
parentId: z.string().optional(),
|
|
728
|
+
metadata: z.record(z.unknown()).optional(),
|
|
729
|
+
});
|