@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/index.ts
ADDED
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LangGraph Orchestrator - LangGraph-style cyclic, state-driven workflows
|
|
3
|
+
* @module @wundr.io/langgraph-orchestrator
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* import {
|
|
8
|
+
* StateGraph,
|
|
9
|
+
* createLLMNode,
|
|
10
|
+
* createToolNode,
|
|
11
|
+
* createDecisionNode,
|
|
12
|
+
* MemoryCheckpointer
|
|
13
|
+
* } from '@wundr.io/langgraph-orchestrator';
|
|
14
|
+
*
|
|
15
|
+
* // Create a workflow graph
|
|
16
|
+
* const graph = new StateGraph('my-workflow')
|
|
17
|
+
* .addNode('agent', createLLMNode({
|
|
18
|
+
* id: 'agent',
|
|
19
|
+
* name: 'Agent',
|
|
20
|
+
* config: { model: 'claude-3-sonnet-20240229' }
|
|
21
|
+
* }))
|
|
22
|
+
* .addNode('tools', createToolNode({
|
|
23
|
+
* id: 'tools',
|
|
24
|
+
* name: 'Tools'
|
|
25
|
+
* }))
|
|
26
|
+
* .addEdge('agent', 'tools')
|
|
27
|
+
* .addConditionalEdge('tools', 'agent', {
|
|
28
|
+
* type: 'exists',
|
|
29
|
+
* field: 'data.pendingToolCalls'
|
|
30
|
+
* })
|
|
31
|
+
* .setEntryPoint('agent')
|
|
32
|
+
* .setCheckpointer(new MemoryCheckpointer());
|
|
33
|
+
*
|
|
34
|
+
* // Execute the workflow
|
|
35
|
+
* const result = await graph.execute({
|
|
36
|
+
* initialState: {
|
|
37
|
+
* data: { task: 'Research AI developments' }
|
|
38
|
+
* }
|
|
39
|
+
* });
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
// ============================================================================
|
|
44
|
+
// Core Types
|
|
45
|
+
// ============================================================================
|
|
46
|
+
// ============================================================================
|
|
47
|
+
// Utility Functions
|
|
48
|
+
// ============================================================================
|
|
49
|
+
|
|
50
|
+
// Import for internal use in utility functions
|
|
51
|
+
import { createSwitchNode as createSwitchNodeFn } from './nodes/decision-node';
|
|
52
|
+
import {
|
|
53
|
+
createLLMNode as createLLMNodeFn,
|
|
54
|
+
createConversationalLLMNode as createConversationalLLMNodeFn,
|
|
55
|
+
} from './nodes/llm-node';
|
|
56
|
+
import {
|
|
57
|
+
createToolNode as createToolNodeFn,
|
|
58
|
+
createToolRegistry as createToolRegistryFn,
|
|
59
|
+
} from './nodes/tool-node';
|
|
60
|
+
import { StateGraph as StateGraphClass } from './state-graph';
|
|
61
|
+
|
|
62
|
+
import type { LLMProvider, Tool } from './types';
|
|
63
|
+
|
|
64
|
+
export type {
|
|
65
|
+
// State types
|
|
66
|
+
AgentState,
|
|
67
|
+
Message,
|
|
68
|
+
MessageRole,
|
|
69
|
+
ToolCall,
|
|
70
|
+
ToolResult,
|
|
71
|
+
StateHistoryEntry,
|
|
72
|
+
StateChange,
|
|
73
|
+
WorkflowError,
|
|
74
|
+
StateMetadata,
|
|
75
|
+
|
|
76
|
+
// Graph configuration
|
|
77
|
+
GraphConfig,
|
|
78
|
+
GraphGlobalConfig,
|
|
79
|
+
RetryConfig,
|
|
80
|
+
LogLevel,
|
|
81
|
+
|
|
82
|
+
// Node types
|
|
83
|
+
NodeDefinition,
|
|
84
|
+
NodeType,
|
|
85
|
+
NodeConfig,
|
|
86
|
+
NodeExecutor,
|
|
87
|
+
NodeContext,
|
|
88
|
+
NodeServices,
|
|
89
|
+
NodeResult,
|
|
90
|
+
NodeExecutionMetadata,
|
|
91
|
+
NodeHook,
|
|
92
|
+
Logger,
|
|
93
|
+
|
|
94
|
+
// Tool types
|
|
95
|
+
Tool,
|
|
96
|
+
ToolRegistry,
|
|
97
|
+
|
|
98
|
+
// LLM types
|
|
99
|
+
LLMProvider,
|
|
100
|
+
LLMRequest,
|
|
101
|
+
LLMResponse,
|
|
102
|
+
TokenUsage,
|
|
103
|
+
FinishReason,
|
|
104
|
+
LLMStreamChunk,
|
|
105
|
+
|
|
106
|
+
// Edge types
|
|
107
|
+
EdgeDefinition,
|
|
108
|
+
EdgeType,
|
|
109
|
+
EdgeCondition,
|
|
110
|
+
ConditionType,
|
|
111
|
+
EdgeConditionEvaluator,
|
|
112
|
+
EdgeContext,
|
|
113
|
+
|
|
114
|
+
// Checkpoint types
|
|
115
|
+
GraphCheckpointer,
|
|
116
|
+
Checkpoint,
|
|
117
|
+
CheckpointSummary,
|
|
118
|
+
|
|
119
|
+
// Execution types
|
|
120
|
+
ExecutionOptions,
|
|
121
|
+
ExecutionHandlers,
|
|
122
|
+
ExecutionResult,
|
|
123
|
+
ExecutionStats,
|
|
124
|
+
} from './types';
|
|
125
|
+
|
|
126
|
+
// Zod schemas
|
|
127
|
+
export { MessageSchema, GraphConfigSchema, CheckpointSchema } from './types';
|
|
128
|
+
|
|
129
|
+
// ============================================================================
|
|
130
|
+
// Core Classes
|
|
131
|
+
// ============================================================================
|
|
132
|
+
export { StateGraph, StateGraphEvents } from './state-graph';
|
|
133
|
+
|
|
134
|
+
// ============================================================================
|
|
135
|
+
// Nodes
|
|
136
|
+
// ============================================================================
|
|
137
|
+
|
|
138
|
+
// LLM Node
|
|
139
|
+
export {
|
|
140
|
+
createLLMNode,
|
|
141
|
+
createLLMRouter,
|
|
142
|
+
createStructuredLLMNode,
|
|
143
|
+
createConversationalLLMNode,
|
|
144
|
+
LLMNodeConfig,
|
|
145
|
+
LLMNodeConfigSchema,
|
|
146
|
+
} from './nodes/llm-node';
|
|
147
|
+
|
|
148
|
+
// Tool Node
|
|
149
|
+
export {
|
|
150
|
+
createToolNode,
|
|
151
|
+
createToolRegistry,
|
|
152
|
+
createTool,
|
|
153
|
+
createBatchToolNode,
|
|
154
|
+
ToolNodeConfig,
|
|
155
|
+
ToolNodeConfigSchema,
|
|
156
|
+
} from './nodes/tool-node';
|
|
157
|
+
|
|
158
|
+
// Decision Node
|
|
159
|
+
export {
|
|
160
|
+
createDecisionNode,
|
|
161
|
+
createSwitchNode,
|
|
162
|
+
createThresholdNode,
|
|
163
|
+
createIfElseNode,
|
|
164
|
+
createMultiConditionNode,
|
|
165
|
+
DecisionNodeConfig,
|
|
166
|
+
DecisionBranch,
|
|
167
|
+
DecisionNodeConfigSchema,
|
|
168
|
+
} from './nodes/decision-node';
|
|
169
|
+
|
|
170
|
+
// Human Node
|
|
171
|
+
export {
|
|
172
|
+
createHumanNode,
|
|
173
|
+
createConsoleInputHandler,
|
|
174
|
+
createCallbackInputHandler,
|
|
175
|
+
createConfirmationNode,
|
|
176
|
+
createFeedbackNode,
|
|
177
|
+
HumanNodeConfig,
|
|
178
|
+
HumanInputHandler,
|
|
179
|
+
HumanInputContext,
|
|
180
|
+
HumanChoice,
|
|
181
|
+
HumanResponse,
|
|
182
|
+
HumanNodeConfigSchema,
|
|
183
|
+
} from './nodes/human-node';
|
|
184
|
+
|
|
185
|
+
// ============================================================================
|
|
186
|
+
// Edges
|
|
187
|
+
// ============================================================================
|
|
188
|
+
|
|
189
|
+
// Conditional Edge
|
|
190
|
+
export {
|
|
191
|
+
ConditionalEdgeBuilder,
|
|
192
|
+
conditionalEdge,
|
|
193
|
+
createRouter,
|
|
194
|
+
conditions,
|
|
195
|
+
EdgeConditionSchema,
|
|
196
|
+
validateCondition,
|
|
197
|
+
} from './edges/conditional-edge';
|
|
198
|
+
|
|
199
|
+
// Loop Edge
|
|
200
|
+
export {
|
|
201
|
+
LoopEdgeBuilder,
|
|
202
|
+
loopEdge,
|
|
203
|
+
createForLoop,
|
|
204
|
+
createWhileLoop,
|
|
205
|
+
createDoWhileLoop,
|
|
206
|
+
createRetryLoop,
|
|
207
|
+
createPaginationLoop,
|
|
208
|
+
LoopConfig,
|
|
209
|
+
LoopConfigSchema,
|
|
210
|
+
validateLoopConfig,
|
|
211
|
+
} from './edges/loop-edge';
|
|
212
|
+
|
|
213
|
+
// ============================================================================
|
|
214
|
+
// Checkpointing
|
|
215
|
+
// ============================================================================
|
|
216
|
+
export {
|
|
217
|
+
MemoryCheckpointer,
|
|
218
|
+
FileCheckpointer,
|
|
219
|
+
TimeTravelDebugger,
|
|
220
|
+
createCheckpoint,
|
|
221
|
+
applyRetentionPolicy,
|
|
222
|
+
validateCheckpoint,
|
|
223
|
+
FileSystem,
|
|
224
|
+
StateDiff,
|
|
225
|
+
StateHistoryItem,
|
|
226
|
+
RetentionPolicy,
|
|
227
|
+
CheckpointSchema as CheckpointValidationSchema,
|
|
228
|
+
} from './checkpointing';
|
|
229
|
+
|
|
230
|
+
// ============================================================================
|
|
231
|
+
// Prebuilt Graphs
|
|
232
|
+
// ============================================================================
|
|
233
|
+
export {
|
|
234
|
+
createPlanExecuteRefineGraph,
|
|
235
|
+
createSimpleTaskGraph,
|
|
236
|
+
PlanExecuteState,
|
|
237
|
+
PlanStep,
|
|
238
|
+
StepResult,
|
|
239
|
+
PlanExecuteRefineConfig,
|
|
240
|
+
PlanSchema,
|
|
241
|
+
} from './prebuilt-graphs/plan-execute-refine';
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Create a simple agent workflow
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
* ```typescript
|
|
248
|
+
* const graph = createAgentWorkflow({
|
|
249
|
+
* name: 'research-agent',
|
|
250
|
+
* llmProvider: myProvider,
|
|
251
|
+
* tools: [searchTool, writeTool],
|
|
252
|
+
* systemPrompt: 'You are a helpful research assistant.'
|
|
253
|
+
* });
|
|
254
|
+
* ```
|
|
255
|
+
*/
|
|
256
|
+
export function createAgentWorkflow(options: {
|
|
257
|
+
name: string;
|
|
258
|
+
llmProvider: LLMProvider;
|
|
259
|
+
tools?: Tool[];
|
|
260
|
+
systemPrompt?: string;
|
|
261
|
+
maxIterations?: number;
|
|
262
|
+
}): StateGraphClass {
|
|
263
|
+
const graph = new StateGraphClass(options.name, {
|
|
264
|
+
maxIterations: options.maxIterations ?? 50,
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
const registry = createToolRegistryFn();
|
|
268
|
+
options.tools?.forEach(tool => registry.register(tool));
|
|
269
|
+
|
|
270
|
+
graph.setServices({
|
|
271
|
+
llmProvider: options.llmProvider,
|
|
272
|
+
toolRegistry: registry,
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
// Add agent node
|
|
276
|
+
graph.addNode(
|
|
277
|
+
'agent',
|
|
278
|
+
createLLMNodeFn({
|
|
279
|
+
id: 'agent',
|
|
280
|
+
name: 'Agent',
|
|
281
|
+
config: {
|
|
282
|
+
systemPrompt: options.systemPrompt,
|
|
283
|
+
tools: options.tools,
|
|
284
|
+
},
|
|
285
|
+
}),
|
|
286
|
+
);
|
|
287
|
+
|
|
288
|
+
// Add tool node if tools provided
|
|
289
|
+
if (options.tools?.length) {
|
|
290
|
+
graph.addNode(
|
|
291
|
+
'tools',
|
|
292
|
+
createToolNodeFn({
|
|
293
|
+
id: 'tools',
|
|
294
|
+
name: 'Tool Executor',
|
|
295
|
+
config: {
|
|
296
|
+
tools: options.tools,
|
|
297
|
+
parallel: true,
|
|
298
|
+
},
|
|
299
|
+
}),
|
|
300
|
+
);
|
|
301
|
+
|
|
302
|
+
// Add edges
|
|
303
|
+
graph.addConditionalEdge('agent', 'tools', {
|
|
304
|
+
type: 'exists',
|
|
305
|
+
field: 'data.pendingToolCalls',
|
|
306
|
+
});
|
|
307
|
+
graph.addEdge('tools', 'agent');
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
graph.setEntryPoint('agent');
|
|
311
|
+
|
|
312
|
+
return graph;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Create a chat workflow with conversation history
|
|
317
|
+
*
|
|
318
|
+
* @example
|
|
319
|
+
* ```typescript
|
|
320
|
+
* const chat = createChatWorkflow({
|
|
321
|
+
* name: 'chat-assistant',
|
|
322
|
+
* llmProvider: myProvider,
|
|
323
|
+
* systemPrompt: 'You are a friendly assistant.',
|
|
324
|
+
* maxHistory: 20
|
|
325
|
+
* });
|
|
326
|
+
* ```
|
|
327
|
+
*/
|
|
328
|
+
export function createChatWorkflow(options: {
|
|
329
|
+
name: string;
|
|
330
|
+
llmProvider: LLMProvider;
|
|
331
|
+
systemPrompt?: string;
|
|
332
|
+
maxHistory?: number;
|
|
333
|
+
}): StateGraphClass {
|
|
334
|
+
const graph = new StateGraphClass(options.name);
|
|
335
|
+
|
|
336
|
+
graph.setServices({
|
|
337
|
+
llmProvider: options.llmProvider,
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
graph.addNode(
|
|
341
|
+
'chat',
|
|
342
|
+
createConversationalLLMNodeFn({
|
|
343
|
+
id: 'chat',
|
|
344
|
+
name: 'Chat',
|
|
345
|
+
config: {
|
|
346
|
+
systemPrompt: options.systemPrompt,
|
|
347
|
+
},
|
|
348
|
+
maxHistoryLength: options.maxHistory ?? 50,
|
|
349
|
+
}),
|
|
350
|
+
);
|
|
351
|
+
|
|
352
|
+
graph.setEntryPoint('chat');
|
|
353
|
+
|
|
354
|
+
return graph;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Create a decision tree workflow
|
|
359
|
+
*
|
|
360
|
+
* @example
|
|
361
|
+
* ```typescript
|
|
362
|
+
* const tree = createDecisionTree({
|
|
363
|
+
* name: 'support-router',
|
|
364
|
+
* decisions: [
|
|
365
|
+
* {
|
|
366
|
+
* field: 'data.category',
|
|
367
|
+
* branches: {
|
|
368
|
+
* 'billing': 'billing-handler',
|
|
369
|
+
* 'technical': 'tech-handler',
|
|
370
|
+
* 'general': 'general-handler'
|
|
371
|
+
* },
|
|
372
|
+
* default: 'general-handler'
|
|
373
|
+
* }
|
|
374
|
+
* ]
|
|
375
|
+
* });
|
|
376
|
+
* ```
|
|
377
|
+
*/
|
|
378
|
+
export function createDecisionTree(options: {
|
|
379
|
+
name: string;
|
|
380
|
+
decisions: Array<{
|
|
381
|
+
id?: string;
|
|
382
|
+
field: string;
|
|
383
|
+
branches: Record<string, string>;
|
|
384
|
+
default?: string;
|
|
385
|
+
}>;
|
|
386
|
+
}): StateGraphClass {
|
|
387
|
+
const graph = new StateGraphClass(options.name);
|
|
388
|
+
|
|
389
|
+
options.decisions.forEach((decision, index) => {
|
|
390
|
+
const nodeId = decision.id ?? `decision-${index}`;
|
|
391
|
+
graph.addNode(
|
|
392
|
+
nodeId,
|
|
393
|
+
createSwitchNodeFn({
|
|
394
|
+
id: nodeId,
|
|
395
|
+
name: nodeId,
|
|
396
|
+
field: decision.field,
|
|
397
|
+
cases: decision.branches,
|
|
398
|
+
default: decision.default,
|
|
399
|
+
}),
|
|
400
|
+
);
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
if (options.decisions.length > 0) {
|
|
404
|
+
const firstDecision = options.decisions[0];
|
|
405
|
+
if (firstDecision) {
|
|
406
|
+
graph.setEntryPoint(firstDecision.id ?? 'decision-0');
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
return graph;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// ============================================================================
|
|
414
|
+
// Version
|
|
415
|
+
// ============================================================================
|
|
416
|
+
export const VERSION = '1.0.3';
|