@agentforge/patterns 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/README.md +489 -0
- package/dist/index.cjs +2564 -0
- package/dist/index.d.cts +2600 -0
- package/dist/index.d.ts +2600 -0
- package/dist/index.js +2464 -0
- package/package.json +69 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,2600 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import * as _langchain_langgraph from '@langchain/langgraph';
|
|
3
|
+
import { CompiledStateGraph } from '@langchain/langgraph';
|
|
4
|
+
import { BaseChatModel } from '@langchain/core/language_models/chat_models';
|
|
5
|
+
import { ToolRegistry, Tool } from '@agentforge/core';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Zod schemas for ReAct pattern state
|
|
9
|
+
*
|
|
10
|
+
* These schemas define the structure of messages, thoughts, actions, and observations
|
|
11
|
+
* used in the ReAct (Reasoning and Action) pattern.
|
|
12
|
+
*
|
|
13
|
+
* @module langgraph/patterns/react/schemas
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Base message schema
|
|
18
|
+
*/
|
|
19
|
+
declare const MessageSchema: z.ZodObject<{
|
|
20
|
+
role: z.ZodEnum<["system", "user", "assistant", "tool"]>;
|
|
21
|
+
content: z.ZodString;
|
|
22
|
+
name: z.ZodOptional<z.ZodString>;
|
|
23
|
+
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
24
|
+
}, "strip", z.ZodTypeAny, {
|
|
25
|
+
role: "system" | "user" | "assistant" | "tool";
|
|
26
|
+
content: string;
|
|
27
|
+
name?: string | undefined;
|
|
28
|
+
metadata?: Record<string, any> | undefined;
|
|
29
|
+
}, {
|
|
30
|
+
role: "system" | "user" | "assistant" | "tool";
|
|
31
|
+
content: string;
|
|
32
|
+
name?: string | undefined;
|
|
33
|
+
metadata?: Record<string, any> | undefined;
|
|
34
|
+
}>;
|
|
35
|
+
type Message = z.infer<typeof MessageSchema>;
|
|
36
|
+
/**
|
|
37
|
+
* Thought schema - represents a reasoning step
|
|
38
|
+
*/
|
|
39
|
+
declare const ThoughtSchema: z.ZodObject<{
|
|
40
|
+
content: z.ZodString;
|
|
41
|
+
timestamp: z.ZodOptional<z.ZodNumber>;
|
|
42
|
+
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
43
|
+
}, "strip", z.ZodTypeAny, {
|
|
44
|
+
content: string;
|
|
45
|
+
metadata?: Record<string, any> | undefined;
|
|
46
|
+
timestamp?: number | undefined;
|
|
47
|
+
}, {
|
|
48
|
+
content: string;
|
|
49
|
+
metadata?: Record<string, any> | undefined;
|
|
50
|
+
timestamp?: number | undefined;
|
|
51
|
+
}>;
|
|
52
|
+
type Thought = z.infer<typeof ThoughtSchema>;
|
|
53
|
+
/**
|
|
54
|
+
* Tool call schema - represents an action to take
|
|
55
|
+
*/
|
|
56
|
+
declare const ToolCallSchema: z.ZodObject<{
|
|
57
|
+
id: z.ZodString;
|
|
58
|
+
name: z.ZodString;
|
|
59
|
+
arguments: z.ZodRecord<z.ZodString, z.ZodAny>;
|
|
60
|
+
timestamp: z.ZodOptional<z.ZodNumber>;
|
|
61
|
+
}, "strip", z.ZodTypeAny, {
|
|
62
|
+
name: string;
|
|
63
|
+
id: string;
|
|
64
|
+
arguments: Record<string, any>;
|
|
65
|
+
timestamp?: number | undefined;
|
|
66
|
+
}, {
|
|
67
|
+
name: string;
|
|
68
|
+
id: string;
|
|
69
|
+
arguments: Record<string, any>;
|
|
70
|
+
timestamp?: number | undefined;
|
|
71
|
+
}>;
|
|
72
|
+
type ToolCall = z.infer<typeof ToolCallSchema>;
|
|
73
|
+
/**
|
|
74
|
+
* Tool result schema - represents an observation from a tool
|
|
75
|
+
*/
|
|
76
|
+
declare const ToolResultSchema: z.ZodObject<{
|
|
77
|
+
toolCallId: z.ZodString;
|
|
78
|
+
result: z.ZodAny;
|
|
79
|
+
error: z.ZodOptional<z.ZodString>;
|
|
80
|
+
timestamp: z.ZodOptional<z.ZodNumber>;
|
|
81
|
+
}, "strip", z.ZodTypeAny, {
|
|
82
|
+
toolCallId: string;
|
|
83
|
+
timestamp?: number | undefined;
|
|
84
|
+
result?: any;
|
|
85
|
+
error?: string | undefined;
|
|
86
|
+
}, {
|
|
87
|
+
toolCallId: string;
|
|
88
|
+
timestamp?: number | undefined;
|
|
89
|
+
result?: any;
|
|
90
|
+
error?: string | undefined;
|
|
91
|
+
}>;
|
|
92
|
+
type ToolResult = z.infer<typeof ToolResultSchema>;
|
|
93
|
+
/**
|
|
94
|
+
* Scratchpad entry schema - intermediate reasoning
|
|
95
|
+
*/
|
|
96
|
+
declare const ScratchpadEntrySchema: z.ZodObject<{
|
|
97
|
+
step: z.ZodNumber;
|
|
98
|
+
thought: z.ZodOptional<z.ZodString>;
|
|
99
|
+
action: z.ZodOptional<z.ZodString>;
|
|
100
|
+
observation: z.ZodOptional<z.ZodString>;
|
|
101
|
+
timestamp: z.ZodOptional<z.ZodNumber>;
|
|
102
|
+
}, "strip", z.ZodTypeAny, {
|
|
103
|
+
step: number;
|
|
104
|
+
timestamp?: number | undefined;
|
|
105
|
+
thought?: string | undefined;
|
|
106
|
+
action?: string | undefined;
|
|
107
|
+
observation?: string | undefined;
|
|
108
|
+
}, {
|
|
109
|
+
step: number;
|
|
110
|
+
timestamp?: number | undefined;
|
|
111
|
+
thought?: string | undefined;
|
|
112
|
+
action?: string | undefined;
|
|
113
|
+
observation?: string | undefined;
|
|
114
|
+
}>;
|
|
115
|
+
type ScratchpadEntry = z.infer<typeof ScratchpadEntrySchema>;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Create the ReAct state annotation
|
|
119
|
+
*
|
|
120
|
+
* This uses LangGraph's Annotation.Root() under the hood, with optional Zod validation.
|
|
121
|
+
*/
|
|
122
|
+
declare const ReActState: _langchain_langgraph.AnnotationRoot<_langchain_langgraph.StateDefinition>;
|
|
123
|
+
/**
|
|
124
|
+
* TypeScript type for ReAct state
|
|
125
|
+
*/
|
|
126
|
+
type ReActStateType = typeof ReActState.State;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* ReAct pattern types and configuration
|
|
130
|
+
*
|
|
131
|
+
* @module langgraph/patterns/react/types
|
|
132
|
+
*/
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Configuration for creating a ReAct agent
|
|
136
|
+
*/
|
|
137
|
+
interface ReActAgentConfig {
|
|
138
|
+
/**
|
|
139
|
+
* Language model to use for reasoning
|
|
140
|
+
*/
|
|
141
|
+
llm: BaseChatModel;
|
|
142
|
+
/**
|
|
143
|
+
* Tools available to the agent
|
|
144
|
+
* Can be a ToolRegistry or an array of Tools
|
|
145
|
+
*/
|
|
146
|
+
tools: ToolRegistry | Tool[];
|
|
147
|
+
/**
|
|
148
|
+
* System prompt for the agent
|
|
149
|
+
* @default "You are a helpful assistant that uses tools to solve problems."
|
|
150
|
+
*/
|
|
151
|
+
systemPrompt?: string;
|
|
152
|
+
/**
|
|
153
|
+
* Maximum number of thought-action-observation iterations
|
|
154
|
+
* @default 10
|
|
155
|
+
*/
|
|
156
|
+
maxIterations?: number;
|
|
157
|
+
/**
|
|
158
|
+
* Whether to return intermediate steps in the response
|
|
159
|
+
* @default false
|
|
160
|
+
*/
|
|
161
|
+
returnIntermediateSteps?: boolean;
|
|
162
|
+
/**
|
|
163
|
+
* Custom stop condition function
|
|
164
|
+
* Return true to stop the ReAct loop
|
|
165
|
+
*/
|
|
166
|
+
stopCondition?: (state: ReActStateType) => boolean;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Options for the ReAct agent builder
|
|
170
|
+
*/
|
|
171
|
+
interface ReActBuilderOptions {
|
|
172
|
+
/**
|
|
173
|
+
* Whether to enable verbose logging
|
|
174
|
+
* @default false
|
|
175
|
+
*/
|
|
176
|
+
verbose?: boolean;
|
|
177
|
+
/**
|
|
178
|
+
* Custom node names (for debugging/observability)
|
|
179
|
+
*/
|
|
180
|
+
nodeNames?: {
|
|
181
|
+
reasoning?: string;
|
|
182
|
+
action?: string;
|
|
183
|
+
observation?: string;
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Type alias for ReAct agent options
|
|
188
|
+
*/
|
|
189
|
+
type ReActAgentOptions = ReActBuilderOptions;
|
|
190
|
+
/**
|
|
191
|
+
* Stop condition function type
|
|
192
|
+
*/
|
|
193
|
+
type StopConditionFn = (state: ReActStateType) => boolean;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Prompt templates for ReAct pattern
|
|
197
|
+
*
|
|
198
|
+
* @module langgraph/patterns/react/prompts
|
|
199
|
+
*/
|
|
200
|
+
/**
|
|
201
|
+
* Default system prompt for ReAct agents
|
|
202
|
+
*/
|
|
203
|
+
declare const DEFAULT_REACT_SYSTEM_PROMPT = "You are a helpful assistant that uses tools to solve problems.\n\nYou follow the ReAct (Reasoning and Action) pattern:\n1. THINK: Reason about what to do next\n2. ACT: Use a tool or provide a final answer\n3. OBSERVE: Examine the tool's result\n4. REPEAT: Continue until you can answer the user's question\n\nWhen you need to use a tool:\n- Think carefully about which tool to use\n- Provide the correct arguments\n- Wait for the observation before proceeding\n\nWhen you have enough information:\n- Provide a clear, complete answer\n- Cite the tools you used if relevant";
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* ReAct agent builder
|
|
207
|
+
*
|
|
208
|
+
* Creates a ReAct (Reasoning and Action) agent using LangGraph.
|
|
209
|
+
*
|
|
210
|
+
* @module langgraph/patterns/react/agent
|
|
211
|
+
*/
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Create a ReAct agent
|
|
215
|
+
*
|
|
216
|
+
* This function creates a compiled LangGraph StateGraph that implements the
|
|
217
|
+
* ReAct (Reasoning and Action) pattern.
|
|
218
|
+
*
|
|
219
|
+
* @param config - Configuration for the ReAct agent
|
|
220
|
+
* @param options - Optional builder options
|
|
221
|
+
* @returns A compiled LangGraph StateGraph
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* ```typescript
|
|
225
|
+
* import { createReActAgent } from '@agentforge/patterns';
|
|
226
|
+
* import { ChatOpenAI } from '@langchain/openai';
|
|
227
|
+
*
|
|
228
|
+
* const agent = createReActAgent({
|
|
229
|
+
* llm: new ChatOpenAI({ model: 'gpt-4' }),
|
|
230
|
+
* tools: toolRegistry,
|
|
231
|
+
* systemPrompt: 'You are a helpful assistant.',
|
|
232
|
+
* maxIterations: 10
|
|
233
|
+
* });
|
|
234
|
+
*
|
|
235
|
+
* const result = await agent.invoke({
|
|
236
|
+
* messages: [{ role: 'user', content: 'What is the weather?' }]
|
|
237
|
+
* });
|
|
238
|
+
* ```
|
|
239
|
+
*/
|
|
240
|
+
declare function createReActAgent(config: ReActAgentConfig, options?: ReActBuilderOptions): CompiledStateGraph<any, any>;
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* ReAct Agent Builder
|
|
244
|
+
*
|
|
245
|
+
* Fluent API for building ReAct agents with automatic validation.
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```ts
|
|
249
|
+
* const agent = new ReActAgentBuilder()
|
|
250
|
+
* .withLLM(new ChatOpenAI({ model: 'gpt-4' }))
|
|
251
|
+
* .withTools(toolRegistry)
|
|
252
|
+
* .withSystemPrompt('You are a helpful assistant.')
|
|
253
|
+
* .withMaxIterations(10)
|
|
254
|
+
* .withReturnIntermediateSteps(true)
|
|
255
|
+
* .build();
|
|
256
|
+
* ```
|
|
257
|
+
*/
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Builder for creating ReAct agents with a fluent API
|
|
261
|
+
*
|
|
262
|
+
* This provides a more ergonomic way to create ReAct agents compared to
|
|
263
|
+
* manually constructing the configuration object.
|
|
264
|
+
*/
|
|
265
|
+
declare class ReActAgentBuilder {
|
|
266
|
+
private config;
|
|
267
|
+
private options;
|
|
268
|
+
/**
|
|
269
|
+
* Set the language model (required)
|
|
270
|
+
*
|
|
271
|
+
* @param llm - LangChain chat model to use for reasoning
|
|
272
|
+
*/
|
|
273
|
+
withLLM(llm: BaseChatModel): this;
|
|
274
|
+
/**
|
|
275
|
+
* Set the tools (required)
|
|
276
|
+
*
|
|
277
|
+
* @param tools - Tool registry or array of tools
|
|
278
|
+
*/
|
|
279
|
+
withTools(tools: ToolRegistry | Tool[]): this;
|
|
280
|
+
/**
|
|
281
|
+
* Set the system prompt (optional)
|
|
282
|
+
*
|
|
283
|
+
* @param systemPrompt - System prompt for the agent
|
|
284
|
+
*/
|
|
285
|
+
withSystemPrompt(systemPrompt: string): this;
|
|
286
|
+
/**
|
|
287
|
+
* Set the maximum iterations (optional, default: 10)
|
|
288
|
+
*
|
|
289
|
+
* @param maxIterations - Maximum number of thought-action loops
|
|
290
|
+
*/
|
|
291
|
+
withMaxIterations(maxIterations: number): this;
|
|
292
|
+
/**
|
|
293
|
+
* Set whether to return intermediate steps (optional, default: false)
|
|
294
|
+
*
|
|
295
|
+
* @param returnIntermediateSteps - Whether to include reasoning steps in output
|
|
296
|
+
*/
|
|
297
|
+
withReturnIntermediateSteps(returnIntermediateSteps: boolean): this;
|
|
298
|
+
/**
|
|
299
|
+
* Set a custom stop condition (optional)
|
|
300
|
+
*
|
|
301
|
+
* @param stopCondition - Function that determines when to stop the agent
|
|
302
|
+
*/
|
|
303
|
+
withStopCondition(stopCondition: StopConditionFn): this;
|
|
304
|
+
/**
|
|
305
|
+
* Enable verbose logging (optional, default: false)
|
|
306
|
+
*
|
|
307
|
+
* @param verbose - Whether to enable verbose logging
|
|
308
|
+
*/
|
|
309
|
+
withVerbose(verbose: boolean): this;
|
|
310
|
+
/**
|
|
311
|
+
* Set custom node names (optional)
|
|
312
|
+
*
|
|
313
|
+
* @param nodeNames - Custom names for nodes (for debugging/observability)
|
|
314
|
+
*/
|
|
315
|
+
withNodeNames(nodeNames: {
|
|
316
|
+
reasoning?: string;
|
|
317
|
+
action?: string;
|
|
318
|
+
observation?: string;
|
|
319
|
+
}): this;
|
|
320
|
+
/**
|
|
321
|
+
* Build the ReAct agent
|
|
322
|
+
*
|
|
323
|
+
* @returns A compiled LangGraph StateGraph
|
|
324
|
+
* @throws Error if required configuration is missing
|
|
325
|
+
*/
|
|
326
|
+
build(): CompiledStateGraph<any, any>;
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Create a new ReAct agent builder
|
|
330
|
+
*
|
|
331
|
+
* @returns A new ReActAgentBuilder instance
|
|
332
|
+
*
|
|
333
|
+
* @example
|
|
334
|
+
* ```typescript
|
|
335
|
+
* const agent = createReActAgentBuilder()
|
|
336
|
+
* .withLLM(llm)
|
|
337
|
+
* .withTools(tools)
|
|
338
|
+
* .build();
|
|
339
|
+
* ```
|
|
340
|
+
*/
|
|
341
|
+
declare function createReActAgentBuilder(): ReActAgentBuilder;
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Zod Schemas for Plan-and-Execute Pattern
|
|
345
|
+
*
|
|
346
|
+
* This module defines the validation schemas for the Plan-and-Execute agent pattern.
|
|
347
|
+
* The pattern separates planning from execution for better performance on complex tasks.
|
|
348
|
+
*
|
|
349
|
+
* @module patterns/plan-execute/schemas
|
|
350
|
+
*/
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Schema for a single step in the plan
|
|
354
|
+
*/
|
|
355
|
+
declare const PlanStepSchema: z.ZodObject<{
|
|
356
|
+
/**
|
|
357
|
+
* Unique identifier for the step
|
|
358
|
+
*/
|
|
359
|
+
id: z.ZodString;
|
|
360
|
+
/**
|
|
361
|
+
* Description of what this step should accomplish
|
|
362
|
+
*/
|
|
363
|
+
description: z.ZodString;
|
|
364
|
+
/**
|
|
365
|
+
* Optional dependencies on other steps (by ID)
|
|
366
|
+
*/
|
|
367
|
+
dependencies: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
368
|
+
/**
|
|
369
|
+
* Optional tool to use for this step
|
|
370
|
+
*/
|
|
371
|
+
tool: z.ZodOptional<z.ZodString>;
|
|
372
|
+
/**
|
|
373
|
+
* Optional arguments for the tool
|
|
374
|
+
*/
|
|
375
|
+
args: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
376
|
+
}, "strip", z.ZodTypeAny, {
|
|
377
|
+
id: string;
|
|
378
|
+
description: string;
|
|
379
|
+
tool?: string | undefined;
|
|
380
|
+
args?: Record<string, any> | undefined;
|
|
381
|
+
dependencies?: string[] | undefined;
|
|
382
|
+
}, {
|
|
383
|
+
id: string;
|
|
384
|
+
description: string;
|
|
385
|
+
tool?: string | undefined;
|
|
386
|
+
args?: Record<string, any> | undefined;
|
|
387
|
+
dependencies?: string[] | undefined;
|
|
388
|
+
}>;
|
|
389
|
+
type PlanStep = z.infer<typeof PlanStepSchema>;
|
|
390
|
+
/**
|
|
391
|
+
* Schema for a completed step with its result
|
|
392
|
+
*/
|
|
393
|
+
declare const CompletedStepSchema: z.ZodObject<{
|
|
394
|
+
/**
|
|
395
|
+
* The step that was executed
|
|
396
|
+
*/
|
|
397
|
+
step: z.ZodObject<{
|
|
398
|
+
/**
|
|
399
|
+
* Unique identifier for the step
|
|
400
|
+
*/
|
|
401
|
+
id: z.ZodString;
|
|
402
|
+
/**
|
|
403
|
+
* Description of what this step should accomplish
|
|
404
|
+
*/
|
|
405
|
+
description: z.ZodString;
|
|
406
|
+
/**
|
|
407
|
+
* Optional dependencies on other steps (by ID)
|
|
408
|
+
*/
|
|
409
|
+
dependencies: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
410
|
+
/**
|
|
411
|
+
* Optional tool to use for this step
|
|
412
|
+
*/
|
|
413
|
+
tool: z.ZodOptional<z.ZodString>;
|
|
414
|
+
/**
|
|
415
|
+
* Optional arguments for the tool
|
|
416
|
+
*/
|
|
417
|
+
args: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
418
|
+
}, "strip", z.ZodTypeAny, {
|
|
419
|
+
id: string;
|
|
420
|
+
description: string;
|
|
421
|
+
tool?: string | undefined;
|
|
422
|
+
args?: Record<string, any> | undefined;
|
|
423
|
+
dependencies?: string[] | undefined;
|
|
424
|
+
}, {
|
|
425
|
+
id: string;
|
|
426
|
+
description: string;
|
|
427
|
+
tool?: string | undefined;
|
|
428
|
+
args?: Record<string, any> | undefined;
|
|
429
|
+
dependencies?: string[] | undefined;
|
|
430
|
+
}>;
|
|
431
|
+
/**
|
|
432
|
+
* The result of executing the step
|
|
433
|
+
*/
|
|
434
|
+
result: z.ZodAny;
|
|
435
|
+
/**
|
|
436
|
+
* Whether the step succeeded
|
|
437
|
+
*/
|
|
438
|
+
success: z.ZodBoolean;
|
|
439
|
+
/**
|
|
440
|
+
* Optional error message if the step failed
|
|
441
|
+
*/
|
|
442
|
+
error: z.ZodOptional<z.ZodString>;
|
|
443
|
+
/**
|
|
444
|
+
* Timestamp when the step was completed
|
|
445
|
+
*/
|
|
446
|
+
timestamp: z.ZodString;
|
|
447
|
+
}, "strip", z.ZodTypeAny, {
|
|
448
|
+
timestamp: string;
|
|
449
|
+
step: {
|
|
450
|
+
id: string;
|
|
451
|
+
description: string;
|
|
452
|
+
tool?: string | undefined;
|
|
453
|
+
args?: Record<string, any> | undefined;
|
|
454
|
+
dependencies?: string[] | undefined;
|
|
455
|
+
};
|
|
456
|
+
success: boolean;
|
|
457
|
+
result?: any;
|
|
458
|
+
error?: string | undefined;
|
|
459
|
+
}, {
|
|
460
|
+
timestamp: string;
|
|
461
|
+
step: {
|
|
462
|
+
id: string;
|
|
463
|
+
description: string;
|
|
464
|
+
tool?: string | undefined;
|
|
465
|
+
args?: Record<string, any> | undefined;
|
|
466
|
+
dependencies?: string[] | undefined;
|
|
467
|
+
};
|
|
468
|
+
success: boolean;
|
|
469
|
+
result?: any;
|
|
470
|
+
error?: string | undefined;
|
|
471
|
+
}>;
|
|
472
|
+
type CompletedStep = z.infer<typeof CompletedStepSchema>;
|
|
473
|
+
/**
|
|
474
|
+
* Schema for the complete plan
|
|
475
|
+
*/
|
|
476
|
+
declare const PlanSchema: z.ZodObject<{
|
|
477
|
+
/**
|
|
478
|
+
* List of steps in the plan
|
|
479
|
+
*/
|
|
480
|
+
steps: z.ZodArray<z.ZodObject<{
|
|
481
|
+
/**
|
|
482
|
+
* Unique identifier for the step
|
|
483
|
+
*/
|
|
484
|
+
id: z.ZodString;
|
|
485
|
+
/**
|
|
486
|
+
* Description of what this step should accomplish
|
|
487
|
+
*/
|
|
488
|
+
description: z.ZodString;
|
|
489
|
+
/**
|
|
490
|
+
* Optional dependencies on other steps (by ID)
|
|
491
|
+
*/
|
|
492
|
+
dependencies: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
493
|
+
/**
|
|
494
|
+
* Optional tool to use for this step
|
|
495
|
+
*/
|
|
496
|
+
tool: z.ZodOptional<z.ZodString>;
|
|
497
|
+
/**
|
|
498
|
+
* Optional arguments for the tool
|
|
499
|
+
*/
|
|
500
|
+
args: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
501
|
+
}, "strip", z.ZodTypeAny, {
|
|
502
|
+
id: string;
|
|
503
|
+
description: string;
|
|
504
|
+
tool?: string | undefined;
|
|
505
|
+
args?: Record<string, any> | undefined;
|
|
506
|
+
dependencies?: string[] | undefined;
|
|
507
|
+
}, {
|
|
508
|
+
id: string;
|
|
509
|
+
description: string;
|
|
510
|
+
tool?: string | undefined;
|
|
511
|
+
args?: Record<string, any> | undefined;
|
|
512
|
+
dependencies?: string[] | undefined;
|
|
513
|
+
}>, "many">;
|
|
514
|
+
/**
|
|
515
|
+
* Overall goal of the plan
|
|
516
|
+
*/
|
|
517
|
+
goal: z.ZodString;
|
|
518
|
+
/**
|
|
519
|
+
* Timestamp when the plan was created
|
|
520
|
+
*/
|
|
521
|
+
createdAt: z.ZodString;
|
|
522
|
+
/**
|
|
523
|
+
* Optional confidence score (0-1)
|
|
524
|
+
*/
|
|
525
|
+
confidence: z.ZodOptional<z.ZodNumber>;
|
|
526
|
+
}, "strip", z.ZodTypeAny, {
|
|
527
|
+
steps: {
|
|
528
|
+
id: string;
|
|
529
|
+
description: string;
|
|
530
|
+
tool?: string | undefined;
|
|
531
|
+
args?: Record<string, any> | undefined;
|
|
532
|
+
dependencies?: string[] | undefined;
|
|
533
|
+
}[];
|
|
534
|
+
goal: string;
|
|
535
|
+
createdAt: string;
|
|
536
|
+
confidence?: number | undefined;
|
|
537
|
+
}, {
|
|
538
|
+
steps: {
|
|
539
|
+
id: string;
|
|
540
|
+
description: string;
|
|
541
|
+
tool?: string | undefined;
|
|
542
|
+
args?: Record<string, any> | undefined;
|
|
543
|
+
dependencies?: string[] | undefined;
|
|
544
|
+
}[];
|
|
545
|
+
goal: string;
|
|
546
|
+
createdAt: string;
|
|
547
|
+
confidence?: number | undefined;
|
|
548
|
+
}>;
|
|
549
|
+
type Plan = z.infer<typeof PlanSchema>;
|
|
550
|
+
/**
|
|
551
|
+
* Schema for re-planning decision
|
|
552
|
+
*/
|
|
553
|
+
declare const ReplanDecisionSchema: z.ZodObject<{
|
|
554
|
+
/**
|
|
555
|
+
* Whether to replan
|
|
556
|
+
*/
|
|
557
|
+
shouldReplan: z.ZodBoolean;
|
|
558
|
+
/**
|
|
559
|
+
* Reason for the decision
|
|
560
|
+
*/
|
|
561
|
+
reason: z.ZodString;
|
|
562
|
+
/**
|
|
563
|
+
* Optional new goal if replanning
|
|
564
|
+
*/
|
|
565
|
+
newGoal: z.ZodOptional<z.ZodString>;
|
|
566
|
+
}, "strip", z.ZodTypeAny, {
|
|
567
|
+
shouldReplan: boolean;
|
|
568
|
+
reason: string;
|
|
569
|
+
newGoal?: string | undefined;
|
|
570
|
+
}, {
|
|
571
|
+
shouldReplan: boolean;
|
|
572
|
+
reason: string;
|
|
573
|
+
newGoal?: string | undefined;
|
|
574
|
+
}>;
|
|
575
|
+
type ReplanDecision = z.infer<typeof ReplanDecisionSchema>;
|
|
576
|
+
/**
|
|
577
|
+
* Schema for execution status
|
|
578
|
+
*/
|
|
579
|
+
declare const ExecutionStatusSchema: z.ZodEnum<["planning", "executing", "replanning", "completed", "failed"]>;
|
|
580
|
+
type ExecutionStatus = z.infer<typeof ExecutionStatusSchema>;
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* Plan-and-Execute state configuration
|
|
584
|
+
*
|
|
585
|
+
* This configuration is used with createStateAnnotation() to create a
|
|
586
|
+
* LangGraph Annotation.Root() with optional Zod validation.
|
|
587
|
+
*/
|
|
588
|
+
declare const PlanExecuteStateConfig: {
|
|
589
|
+
/**
|
|
590
|
+
* Original user input/query
|
|
591
|
+
*/
|
|
592
|
+
input: {
|
|
593
|
+
schema: z.ZodString;
|
|
594
|
+
default: () => string;
|
|
595
|
+
description: string;
|
|
596
|
+
};
|
|
597
|
+
/**
|
|
598
|
+
* The current plan
|
|
599
|
+
*/
|
|
600
|
+
plan: {
|
|
601
|
+
schema: z.ZodOptional<z.ZodObject<{
|
|
602
|
+
steps: z.ZodArray<z.ZodObject<{
|
|
603
|
+
id: z.ZodString;
|
|
604
|
+
description: z.ZodString;
|
|
605
|
+
dependencies: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
606
|
+
tool: z.ZodOptional<z.ZodString>;
|
|
607
|
+
args: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
608
|
+
}, "strip", z.ZodTypeAny, {
|
|
609
|
+
id: string;
|
|
610
|
+
description: string;
|
|
611
|
+
tool?: string | undefined;
|
|
612
|
+
args?: Record<string, any> | undefined;
|
|
613
|
+
dependencies?: string[] | undefined;
|
|
614
|
+
}, {
|
|
615
|
+
id: string;
|
|
616
|
+
description: string;
|
|
617
|
+
tool?: string | undefined;
|
|
618
|
+
args?: Record<string, any> | undefined;
|
|
619
|
+
dependencies?: string[] | undefined;
|
|
620
|
+
}>, "many">;
|
|
621
|
+
goal: z.ZodString;
|
|
622
|
+
createdAt: z.ZodString;
|
|
623
|
+
confidence: z.ZodOptional<z.ZodNumber>;
|
|
624
|
+
}, "strip", z.ZodTypeAny, {
|
|
625
|
+
steps: {
|
|
626
|
+
id: string;
|
|
627
|
+
description: string;
|
|
628
|
+
tool?: string | undefined;
|
|
629
|
+
args?: Record<string, any> | undefined;
|
|
630
|
+
dependencies?: string[] | undefined;
|
|
631
|
+
}[];
|
|
632
|
+
goal: string;
|
|
633
|
+
createdAt: string;
|
|
634
|
+
confidence?: number | undefined;
|
|
635
|
+
}, {
|
|
636
|
+
steps: {
|
|
637
|
+
id: string;
|
|
638
|
+
description: string;
|
|
639
|
+
tool?: string | undefined;
|
|
640
|
+
args?: Record<string, any> | undefined;
|
|
641
|
+
dependencies?: string[] | undefined;
|
|
642
|
+
}[];
|
|
643
|
+
goal: string;
|
|
644
|
+
createdAt: string;
|
|
645
|
+
confidence?: number | undefined;
|
|
646
|
+
}>>;
|
|
647
|
+
description: string;
|
|
648
|
+
};
|
|
649
|
+
/**
|
|
650
|
+
* Completed steps with their results
|
|
651
|
+
* Accumulates all completed steps
|
|
652
|
+
*/
|
|
653
|
+
pastSteps: {
|
|
654
|
+
schema: z.ZodArray<z.ZodObject<{
|
|
655
|
+
step: z.ZodObject<{
|
|
656
|
+
id: z.ZodString;
|
|
657
|
+
description: z.ZodString;
|
|
658
|
+
dependencies: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
659
|
+
tool: z.ZodOptional<z.ZodString>;
|
|
660
|
+
args: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
661
|
+
}, "strip", z.ZodTypeAny, {
|
|
662
|
+
id: string;
|
|
663
|
+
description: string;
|
|
664
|
+
tool?: string | undefined;
|
|
665
|
+
args?: Record<string, any> | undefined;
|
|
666
|
+
dependencies?: string[] | undefined;
|
|
667
|
+
}, {
|
|
668
|
+
id: string;
|
|
669
|
+
description: string;
|
|
670
|
+
tool?: string | undefined;
|
|
671
|
+
args?: Record<string, any> | undefined;
|
|
672
|
+
dependencies?: string[] | undefined;
|
|
673
|
+
}>;
|
|
674
|
+
result: z.ZodAny;
|
|
675
|
+
success: z.ZodBoolean;
|
|
676
|
+
error: z.ZodOptional<z.ZodString>;
|
|
677
|
+
timestamp: z.ZodString;
|
|
678
|
+
}, "strip", z.ZodTypeAny, {
|
|
679
|
+
timestamp: string;
|
|
680
|
+
step: {
|
|
681
|
+
id: string;
|
|
682
|
+
description: string;
|
|
683
|
+
tool?: string | undefined;
|
|
684
|
+
args?: Record<string, any> | undefined;
|
|
685
|
+
dependencies?: string[] | undefined;
|
|
686
|
+
};
|
|
687
|
+
success: boolean;
|
|
688
|
+
result?: any;
|
|
689
|
+
error?: string | undefined;
|
|
690
|
+
}, {
|
|
691
|
+
timestamp: string;
|
|
692
|
+
step: {
|
|
693
|
+
id: string;
|
|
694
|
+
description: string;
|
|
695
|
+
tool?: string | undefined;
|
|
696
|
+
args?: Record<string, any> | undefined;
|
|
697
|
+
dependencies?: string[] | undefined;
|
|
698
|
+
};
|
|
699
|
+
success: boolean;
|
|
700
|
+
result?: any;
|
|
701
|
+
error?: string | undefined;
|
|
702
|
+
}>, "many">;
|
|
703
|
+
reducer: (left: CompletedStep[], right: CompletedStep[]) => {
|
|
704
|
+
timestamp: string;
|
|
705
|
+
step: {
|
|
706
|
+
id: string;
|
|
707
|
+
description: string;
|
|
708
|
+
tool?: string | undefined;
|
|
709
|
+
args?: Record<string, any> | undefined;
|
|
710
|
+
dependencies?: string[] | undefined;
|
|
711
|
+
};
|
|
712
|
+
success: boolean;
|
|
713
|
+
result?: any;
|
|
714
|
+
error?: string | undefined;
|
|
715
|
+
}[];
|
|
716
|
+
default: () => never[];
|
|
717
|
+
description: string;
|
|
718
|
+
};
|
|
719
|
+
/**
|
|
720
|
+
* Index of the current step being executed
|
|
721
|
+
*/
|
|
722
|
+
currentStepIndex: {
|
|
723
|
+
schema: z.ZodOptional<z.ZodNumber>;
|
|
724
|
+
description: string;
|
|
725
|
+
};
|
|
726
|
+
/**
|
|
727
|
+
* Current execution status
|
|
728
|
+
*/
|
|
729
|
+
status: {
|
|
730
|
+
schema: z.ZodEnum<["planning", "executing", "replanning", "completed", "failed"]>;
|
|
731
|
+
default: () => ExecutionStatus;
|
|
732
|
+
description: string;
|
|
733
|
+
};
|
|
734
|
+
/**
|
|
735
|
+
* Final response
|
|
736
|
+
*/
|
|
737
|
+
response: {
|
|
738
|
+
schema: z.ZodOptional<z.ZodString>;
|
|
739
|
+
description: string;
|
|
740
|
+
};
|
|
741
|
+
/**
|
|
742
|
+
* Error message if execution failed
|
|
743
|
+
*/
|
|
744
|
+
error: {
|
|
745
|
+
schema: z.ZodOptional<z.ZodString>;
|
|
746
|
+
description: string;
|
|
747
|
+
};
|
|
748
|
+
/**
|
|
749
|
+
* Iteration counter for replanning
|
|
750
|
+
*/
|
|
751
|
+
iteration: {
|
|
752
|
+
schema: z.ZodNumber;
|
|
753
|
+
reducer: (left: number, right: number) => number;
|
|
754
|
+
default: () => number;
|
|
755
|
+
description: string;
|
|
756
|
+
};
|
|
757
|
+
/**
|
|
758
|
+
* Maximum iterations allowed
|
|
759
|
+
*/
|
|
760
|
+
maxIterations: {
|
|
761
|
+
schema: z.ZodNumber;
|
|
762
|
+
default: () => number;
|
|
763
|
+
description: string;
|
|
764
|
+
};
|
|
765
|
+
};
|
|
766
|
+
/**
|
|
767
|
+
* Create the Plan-Execute state annotation
|
|
768
|
+
*
|
|
769
|
+
* This uses LangGraph's Annotation.Root() under the hood, with optional Zod validation.
|
|
770
|
+
*/
|
|
771
|
+
declare const PlanExecuteState: _langchain_langgraph.AnnotationRoot<_langchain_langgraph.StateDefinition>;
|
|
772
|
+
/**
|
|
773
|
+
* TypeScript type for Plan-Execute state
|
|
774
|
+
*
|
|
775
|
+
* Manually defined to work around TypeScript's inability to infer the .State property
|
|
776
|
+
*/
|
|
777
|
+
type PlanExecuteStateType = {
|
|
778
|
+
input: string;
|
|
779
|
+
plan?: Plan;
|
|
780
|
+
pastSteps: CompletedStep[];
|
|
781
|
+
currentStepIndex?: number;
|
|
782
|
+
status: ExecutionStatus;
|
|
783
|
+
response?: string;
|
|
784
|
+
error?: string;
|
|
785
|
+
iteration: number;
|
|
786
|
+
maxIterations: number;
|
|
787
|
+
};
|
|
788
|
+
|
|
789
|
+
/**
|
|
790
|
+
* TypeScript Types for Plan-and-Execute Pattern
|
|
791
|
+
*
|
|
792
|
+
* This module defines the TypeScript types for the Plan-and-Execute agent pattern.
|
|
793
|
+
*
|
|
794
|
+
* @module patterns/plan-execute/types
|
|
795
|
+
*/
|
|
796
|
+
|
|
797
|
+
/**
|
|
798
|
+
* Configuration for the planner node
|
|
799
|
+
*/
|
|
800
|
+
interface PlannerConfig {
|
|
801
|
+
/**
|
|
802
|
+
* LLM to use for planning
|
|
803
|
+
*/
|
|
804
|
+
llm: BaseChatModel;
|
|
805
|
+
/**
|
|
806
|
+
* System prompt for the planner
|
|
807
|
+
*/
|
|
808
|
+
systemPrompt?: string;
|
|
809
|
+
/**
|
|
810
|
+
* Maximum number of steps in a plan
|
|
811
|
+
*/
|
|
812
|
+
maxSteps?: number;
|
|
813
|
+
/**
|
|
814
|
+
* Whether to include tool descriptions in planning
|
|
815
|
+
*/
|
|
816
|
+
includeToolDescriptions?: boolean;
|
|
817
|
+
}
|
|
818
|
+
/**
|
|
819
|
+
* Configuration for the executor node
|
|
820
|
+
*/
|
|
821
|
+
interface ExecutorConfig {
|
|
822
|
+
/**
|
|
823
|
+
* Available tools for execution
|
|
824
|
+
*/
|
|
825
|
+
tools: Tool[];
|
|
826
|
+
/**
|
|
827
|
+
* Optional LLM for sub-tasks
|
|
828
|
+
*/
|
|
829
|
+
llm?: BaseChatModel;
|
|
830
|
+
/**
|
|
831
|
+
* Enable parallel execution of independent steps
|
|
832
|
+
*/
|
|
833
|
+
parallel?: boolean;
|
|
834
|
+
/**
|
|
835
|
+
* Timeout for each step execution (ms)
|
|
836
|
+
*/
|
|
837
|
+
stepTimeout?: number;
|
|
838
|
+
}
|
|
839
|
+
/**
|
|
840
|
+
* Configuration for the replanner node
|
|
841
|
+
*/
|
|
842
|
+
interface ReplannerConfig {
|
|
843
|
+
/**
|
|
844
|
+
* LLM to use for replanning decisions
|
|
845
|
+
*/
|
|
846
|
+
llm: BaseChatModel;
|
|
847
|
+
/**
|
|
848
|
+
* Confidence threshold for replanning (0-1)
|
|
849
|
+
* If confidence is below this, trigger replanning
|
|
850
|
+
*/
|
|
851
|
+
replanThreshold?: number;
|
|
852
|
+
/**
|
|
853
|
+
* System prompt for replanning
|
|
854
|
+
*/
|
|
855
|
+
systemPrompt?: string;
|
|
856
|
+
}
|
|
857
|
+
/**
|
|
858
|
+
* Configuration for creating a Plan-Execute agent
|
|
859
|
+
*/
|
|
860
|
+
interface PlanExecuteAgentConfig {
|
|
861
|
+
/**
|
|
862
|
+
* Planner configuration
|
|
863
|
+
*/
|
|
864
|
+
planner: PlannerConfig;
|
|
865
|
+
/**
|
|
866
|
+
* Executor configuration
|
|
867
|
+
*/
|
|
868
|
+
executor: ExecutorConfig;
|
|
869
|
+
/**
|
|
870
|
+
* Optional replanner configuration
|
|
871
|
+
* If not provided, no replanning will occur
|
|
872
|
+
*/
|
|
873
|
+
replanner?: ReplannerConfig;
|
|
874
|
+
/**
|
|
875
|
+
* Maximum number of planning iterations
|
|
876
|
+
*/
|
|
877
|
+
maxIterations?: number;
|
|
878
|
+
/**
|
|
879
|
+
* Whether to return intermediate steps
|
|
880
|
+
*/
|
|
881
|
+
returnIntermediateSteps?: boolean;
|
|
882
|
+
/**
|
|
883
|
+
* Verbose logging
|
|
884
|
+
*/
|
|
885
|
+
verbose?: boolean;
|
|
886
|
+
}
|
|
887
|
+
/**
|
|
888
|
+
* Node function type for Plan-Execute pattern
|
|
889
|
+
*/
|
|
890
|
+
type PlanExecuteNode = (state: PlanExecuteStateType) => Partial<PlanExecuteStateType> | Promise<Partial<PlanExecuteStateType>>;
|
|
891
|
+
/**
|
|
892
|
+
* Routing decision for conditional edges
|
|
893
|
+
*/
|
|
894
|
+
type PlanExecuteRoute = 'execute' | 'replan' | 'finish' | 'error';
|
|
895
|
+
/**
|
|
896
|
+
* Router function type
|
|
897
|
+
*/
|
|
898
|
+
type PlanExecuteRouter = (state: PlanExecuteStateType) => PlanExecuteRoute;
|
|
899
|
+
|
|
900
|
+
/**
|
|
901
|
+
* Plan-and-Execute Agent Factory
|
|
902
|
+
*
|
|
903
|
+
* This module provides the main factory function for creating Plan-and-Execute agents.
|
|
904
|
+
*
|
|
905
|
+
* @module patterns/plan-execute/agent
|
|
906
|
+
*/
|
|
907
|
+
|
|
908
|
+
/**
|
|
909
|
+
* Create a Plan-and-Execute agent
|
|
910
|
+
*
|
|
911
|
+
* This agent separates planning from execution for better performance on complex tasks.
|
|
912
|
+
* It creates a plan, executes steps, and optionally replans based on results.
|
|
913
|
+
*
|
|
914
|
+
* @param config - Configuration for the agent
|
|
915
|
+
* @returns A compiled LangGraph StateGraph
|
|
916
|
+
*
|
|
917
|
+
* @example
|
|
918
|
+
* ```typescript
|
|
919
|
+
* import { createPlanExecuteAgent } from '@agentforge/patterns';
|
|
920
|
+
* import { ChatOpenAI } from '@langchain/openai';
|
|
921
|
+
*
|
|
922
|
+
* const agent = createPlanExecuteAgent({
|
|
923
|
+
* planner: {
|
|
924
|
+
* llm: new ChatOpenAI({ model: 'gpt-4' }),
|
|
925
|
+
* maxSteps: 5
|
|
926
|
+
* },
|
|
927
|
+
* executor: {
|
|
928
|
+
* tools: [searchTool, calculatorTool],
|
|
929
|
+
* parallel: false
|
|
930
|
+
* },
|
|
931
|
+
* replanner: {
|
|
932
|
+
* llm: new ChatOpenAI({ model: 'gpt-4' }),
|
|
933
|
+
* replanThreshold: 0.7
|
|
934
|
+
* }
|
|
935
|
+
* });
|
|
936
|
+
*
|
|
937
|
+
* const result = await agent.invoke({
|
|
938
|
+
* input: 'Research the latest AI developments and summarize them'
|
|
939
|
+
* });
|
|
940
|
+
* ```
|
|
941
|
+
*/
|
|
942
|
+
declare function createPlanExecuteAgent(config: PlanExecuteAgentConfig): any;
|
|
943
|
+
|
|
944
|
+
/**
|
|
945
|
+
* Node Implementations for Plan-and-Execute Pattern
|
|
946
|
+
*
|
|
947
|
+
* This module implements the core nodes for the Plan-and-Execute agent pattern.
|
|
948
|
+
*
|
|
949
|
+
* @module patterns/plan-execute/nodes
|
|
950
|
+
*/
|
|
951
|
+
|
|
952
|
+
/**
|
|
953
|
+
* Create a planner node that generates a multi-step plan
|
|
954
|
+
*/
|
|
955
|
+
declare function createPlannerNode(config: PlannerConfig): (state: PlanExecuteStateType) => Promise<Partial<PlanExecuteStateType>>;
|
|
956
|
+
/**
|
|
957
|
+
* Create an executor node that executes plan steps
|
|
958
|
+
*/
|
|
959
|
+
declare function createExecutorNode(config: ExecutorConfig): (state: PlanExecuteStateType) => Promise<Partial<PlanExecuteStateType>>;
|
|
960
|
+
/**
|
|
961
|
+
* Create a replanner node that decides whether to replan
|
|
962
|
+
*/
|
|
963
|
+
declare function createReplannerNode(config: ReplannerConfig): (state: PlanExecuteStateType) => Promise<Partial<PlanExecuteStateType>>;
|
|
964
|
+
/**
|
|
965
|
+
* Create a finisher node that sets the final status and response
|
|
966
|
+
*/
|
|
967
|
+
declare function createFinisherNode$1(): (state: PlanExecuteStateType) => Promise<Partial<PlanExecuteStateType>>;
|
|
968
|
+
|
|
969
|
+
/**
|
|
970
|
+
* Prompt Templates for Plan-and-Execute Pattern
|
|
971
|
+
*
|
|
972
|
+
* This module contains the prompt templates used by the Plan-and-Execute agent pattern.
|
|
973
|
+
*
|
|
974
|
+
* @module patterns/plan-execute/prompts
|
|
975
|
+
*/
|
|
976
|
+
/**
|
|
977
|
+
* Default system prompt for the planner
|
|
978
|
+
*/
|
|
979
|
+
declare const DEFAULT_PLANNER_SYSTEM_PROMPT = "You are an expert planning assistant. Your job is to create a detailed, step-by-step plan to accomplish the user's goal.\n\nGuidelines for creating plans:\n1. Break down complex tasks into clear, actionable steps\n2. Each step should have a specific, measurable outcome\n3. Identify dependencies between steps\n4. Consider which tools are needed for each step\n5. Keep the plan focused and efficient\n6. Aim for 3-7 steps for most tasks\n\nOutput your plan as a JSON object with the following structure:\n{\n \"goal\": \"The overall goal\",\n \"steps\": [\n {\n \"id\": \"step-1\",\n \"description\": \"What this step accomplishes\",\n \"tool\": \"tool_name (optional)\",\n \"args\": {\"key\": \"value (optional)\"},\n \"dependencies\": [\"step-id (optional)\"]\n }\n ],\n \"confidence\": 0.9\n}";
|
|
980
|
+
/**
|
|
981
|
+
* Default system prompt for the replanner
|
|
982
|
+
*/
|
|
983
|
+
declare const DEFAULT_REPLANNER_SYSTEM_PROMPT = "You are an expert replanning assistant. Your job is to decide whether the current plan needs to be adjusted based on the results so far.\n\nConsider:\n1. Have the completed steps achieved their intended outcomes?\n2. Are there any unexpected results that require plan changes?\n3. Is the original goal still achievable with the current plan?\n4. Would a different approach be more effective?\n\nOutput your decision as a JSON object:\n{\n \"shouldReplan\": true/false,\n \"reason\": \"Explanation for the decision\",\n \"newGoal\": \"Updated goal if replanning (optional)\"\n}";
|
|
984
|
+
/**
|
|
985
|
+
* Template for formatting the planning prompt with context
|
|
986
|
+
*/
|
|
987
|
+
declare const PLANNING_PROMPT_TEMPLATE = "User Goal: {input}\n\n{toolDescriptions}\n\nCreate a step-by-step plan to accomplish this goal.";
|
|
988
|
+
/**
|
|
989
|
+
* Template for formatting the replanning prompt with context
|
|
990
|
+
*/
|
|
991
|
+
declare const REPLANNING_PROMPT_TEMPLATE = "Original Goal: {goal}\n\nCompleted Steps:\n{completedSteps}\n\nCurrent Plan:\n{remainingSteps}\n\nBased on the results so far, should we continue with the current plan or replan?";
|
|
992
|
+
|
|
993
|
+
/**
|
|
994
|
+
* Zod Schemas for Reflection Pattern
|
|
995
|
+
*
|
|
996
|
+
* This module defines the data structures used in the Reflection pattern.
|
|
997
|
+
* The pattern iteratively improves outputs through generation, reflection, and revision.
|
|
998
|
+
*
|
|
999
|
+
* @module patterns/reflection/schemas
|
|
1000
|
+
*/
|
|
1001
|
+
|
|
1002
|
+
/**
|
|
1003
|
+
* Schema for a reflection/critique
|
|
1004
|
+
*/
|
|
1005
|
+
declare const ReflectionSchema: z.ZodObject<{
|
|
1006
|
+
/**
|
|
1007
|
+
* The critique or feedback on the current response
|
|
1008
|
+
*/
|
|
1009
|
+
critique: z.ZodString;
|
|
1010
|
+
/**
|
|
1011
|
+
* Specific issues identified
|
|
1012
|
+
*/
|
|
1013
|
+
issues: z.ZodArray<z.ZodString, "many">;
|
|
1014
|
+
/**
|
|
1015
|
+
* Suggestions for improvement
|
|
1016
|
+
*/
|
|
1017
|
+
suggestions: z.ZodArray<z.ZodString, "many">;
|
|
1018
|
+
/**
|
|
1019
|
+
* Quality score (0-10)
|
|
1020
|
+
*/
|
|
1021
|
+
score: z.ZodOptional<z.ZodNumber>;
|
|
1022
|
+
/**
|
|
1023
|
+
* Whether the response meets quality standards
|
|
1024
|
+
*/
|
|
1025
|
+
meetsStandards: z.ZodBoolean;
|
|
1026
|
+
/**
|
|
1027
|
+
* Timestamp of the reflection
|
|
1028
|
+
*/
|
|
1029
|
+
timestamp: z.ZodOptional<z.ZodDate>;
|
|
1030
|
+
}, "strip", z.ZodTypeAny, {
|
|
1031
|
+
issues: string[];
|
|
1032
|
+
critique: string;
|
|
1033
|
+
suggestions: string[];
|
|
1034
|
+
meetsStandards: boolean;
|
|
1035
|
+
timestamp?: Date | undefined;
|
|
1036
|
+
score?: number | undefined;
|
|
1037
|
+
}, {
|
|
1038
|
+
issues: string[];
|
|
1039
|
+
critique: string;
|
|
1040
|
+
suggestions: string[];
|
|
1041
|
+
meetsStandards: boolean;
|
|
1042
|
+
timestamp?: Date | undefined;
|
|
1043
|
+
score?: number | undefined;
|
|
1044
|
+
}>;
|
|
1045
|
+
type Reflection = z.infer<typeof ReflectionSchema>;
|
|
1046
|
+
/**
|
|
1047
|
+
* Schema for a revision entry
|
|
1048
|
+
*/
|
|
1049
|
+
declare const RevisionSchema: z.ZodObject<{
|
|
1050
|
+
/**
|
|
1051
|
+
* The revised content
|
|
1052
|
+
*/
|
|
1053
|
+
content: z.ZodString;
|
|
1054
|
+
/**
|
|
1055
|
+
* Which iteration this revision is from
|
|
1056
|
+
*/
|
|
1057
|
+
iteration: z.ZodNumber;
|
|
1058
|
+
/**
|
|
1059
|
+
* The reflection that prompted this revision
|
|
1060
|
+
*/
|
|
1061
|
+
basedOn: z.ZodOptional<z.ZodObject<{
|
|
1062
|
+
/**
|
|
1063
|
+
* The critique or feedback on the current response
|
|
1064
|
+
*/
|
|
1065
|
+
critique: z.ZodString;
|
|
1066
|
+
/**
|
|
1067
|
+
* Specific issues identified
|
|
1068
|
+
*/
|
|
1069
|
+
issues: z.ZodArray<z.ZodString, "many">;
|
|
1070
|
+
/**
|
|
1071
|
+
* Suggestions for improvement
|
|
1072
|
+
*/
|
|
1073
|
+
suggestions: z.ZodArray<z.ZodString, "many">;
|
|
1074
|
+
/**
|
|
1075
|
+
* Quality score (0-10)
|
|
1076
|
+
*/
|
|
1077
|
+
score: z.ZodOptional<z.ZodNumber>;
|
|
1078
|
+
/**
|
|
1079
|
+
* Whether the response meets quality standards
|
|
1080
|
+
*/
|
|
1081
|
+
meetsStandards: z.ZodBoolean;
|
|
1082
|
+
/**
|
|
1083
|
+
* Timestamp of the reflection
|
|
1084
|
+
*/
|
|
1085
|
+
timestamp: z.ZodOptional<z.ZodDate>;
|
|
1086
|
+
}, "strip", z.ZodTypeAny, {
|
|
1087
|
+
issues: string[];
|
|
1088
|
+
critique: string;
|
|
1089
|
+
suggestions: string[];
|
|
1090
|
+
meetsStandards: boolean;
|
|
1091
|
+
timestamp?: Date | undefined;
|
|
1092
|
+
score?: number | undefined;
|
|
1093
|
+
}, {
|
|
1094
|
+
issues: string[];
|
|
1095
|
+
critique: string;
|
|
1096
|
+
suggestions: string[];
|
|
1097
|
+
meetsStandards: boolean;
|
|
1098
|
+
timestamp?: Date | undefined;
|
|
1099
|
+
score?: number | undefined;
|
|
1100
|
+
}>>;
|
|
1101
|
+
/**
|
|
1102
|
+
* Timestamp of the revision
|
|
1103
|
+
*/
|
|
1104
|
+
timestamp: z.ZodOptional<z.ZodDate>;
|
|
1105
|
+
}, "strip", z.ZodTypeAny, {
|
|
1106
|
+
content: string;
|
|
1107
|
+
iteration: number;
|
|
1108
|
+
timestamp?: Date | undefined;
|
|
1109
|
+
basedOn?: {
|
|
1110
|
+
issues: string[];
|
|
1111
|
+
critique: string;
|
|
1112
|
+
suggestions: string[];
|
|
1113
|
+
meetsStandards: boolean;
|
|
1114
|
+
timestamp?: Date | undefined;
|
|
1115
|
+
score?: number | undefined;
|
|
1116
|
+
} | undefined;
|
|
1117
|
+
}, {
|
|
1118
|
+
content: string;
|
|
1119
|
+
iteration: number;
|
|
1120
|
+
timestamp?: Date | undefined;
|
|
1121
|
+
basedOn?: {
|
|
1122
|
+
issues: string[];
|
|
1123
|
+
critique: string;
|
|
1124
|
+
suggestions: string[];
|
|
1125
|
+
meetsStandards: boolean;
|
|
1126
|
+
timestamp?: Date | undefined;
|
|
1127
|
+
score?: number | undefined;
|
|
1128
|
+
} | undefined;
|
|
1129
|
+
}>;
|
|
1130
|
+
type Revision = z.infer<typeof RevisionSchema>;
|
|
1131
|
+
/**
|
|
1132
|
+
* Schema for reflection status
|
|
1133
|
+
*/
|
|
1134
|
+
declare const ReflectionStatusSchema: z.ZodEnum<["generating", "reflecting", "revising", "completed", "failed"]>;
|
|
1135
|
+
type ReflectionStatus = z.infer<typeof ReflectionStatusSchema>;
|
|
1136
|
+
/**
|
|
1137
|
+
* Schema for quality criteria
|
|
1138
|
+
*/
|
|
1139
|
+
declare const QualityCriteriaSchema: z.ZodObject<{
|
|
1140
|
+
/**
|
|
1141
|
+
* Minimum quality score required (0-10)
|
|
1142
|
+
*/
|
|
1143
|
+
minScore: z.ZodDefault<z.ZodNumber>;
|
|
1144
|
+
/**
|
|
1145
|
+
* Specific criteria to evaluate
|
|
1146
|
+
*/
|
|
1147
|
+
criteria: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
1148
|
+
/**
|
|
1149
|
+
* Whether all criteria must be met
|
|
1150
|
+
*/
|
|
1151
|
+
requireAll: z.ZodDefault<z.ZodBoolean>;
|
|
1152
|
+
}, "strip", z.ZodTypeAny, {
|
|
1153
|
+
minScore: number;
|
|
1154
|
+
requireAll: boolean;
|
|
1155
|
+
criteria?: string[] | undefined;
|
|
1156
|
+
}, {
|
|
1157
|
+
minScore?: number | undefined;
|
|
1158
|
+
criteria?: string[] | undefined;
|
|
1159
|
+
requireAll?: boolean | undefined;
|
|
1160
|
+
}>;
|
|
1161
|
+
type QualityCriteria = z.infer<typeof QualityCriteriaSchema>;
|
|
1162
|
+
/**
|
|
1163
|
+
* Schema for reflection configuration
|
|
1164
|
+
*/
|
|
1165
|
+
declare const ReflectionConfigSchema: z.ZodObject<{
|
|
1166
|
+
/**
|
|
1167
|
+
* Maximum number of reflection iterations
|
|
1168
|
+
*/
|
|
1169
|
+
maxIterations: z.ZodDefault<z.ZodNumber>;
|
|
1170
|
+
/**
|
|
1171
|
+
* Quality criteria for completion
|
|
1172
|
+
*/
|
|
1173
|
+
qualityCriteria: z.ZodOptional<z.ZodObject<{
|
|
1174
|
+
/**
|
|
1175
|
+
* Minimum quality score required (0-10)
|
|
1176
|
+
*/
|
|
1177
|
+
minScore: z.ZodDefault<z.ZodNumber>;
|
|
1178
|
+
/**
|
|
1179
|
+
* Specific criteria to evaluate
|
|
1180
|
+
*/
|
|
1181
|
+
criteria: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
1182
|
+
/**
|
|
1183
|
+
* Whether all criteria must be met
|
|
1184
|
+
*/
|
|
1185
|
+
requireAll: z.ZodDefault<z.ZodBoolean>;
|
|
1186
|
+
}, "strip", z.ZodTypeAny, {
|
|
1187
|
+
minScore: number;
|
|
1188
|
+
requireAll: boolean;
|
|
1189
|
+
criteria?: string[] | undefined;
|
|
1190
|
+
}, {
|
|
1191
|
+
minScore?: number | undefined;
|
|
1192
|
+
criteria?: string[] | undefined;
|
|
1193
|
+
requireAll?: boolean | undefined;
|
|
1194
|
+
}>>;
|
|
1195
|
+
/**
|
|
1196
|
+
* Whether to include previous reflections in context
|
|
1197
|
+
*/
|
|
1198
|
+
includeHistory: z.ZodDefault<z.ZodBoolean>;
|
|
1199
|
+
}, "strip", z.ZodTypeAny, {
|
|
1200
|
+
maxIterations: number;
|
|
1201
|
+
includeHistory: boolean;
|
|
1202
|
+
qualityCriteria?: {
|
|
1203
|
+
minScore: number;
|
|
1204
|
+
requireAll: boolean;
|
|
1205
|
+
criteria?: string[] | undefined;
|
|
1206
|
+
} | undefined;
|
|
1207
|
+
}, {
|
|
1208
|
+
maxIterations?: number | undefined;
|
|
1209
|
+
qualityCriteria?: {
|
|
1210
|
+
minScore?: number | undefined;
|
|
1211
|
+
criteria?: string[] | undefined;
|
|
1212
|
+
requireAll?: boolean | undefined;
|
|
1213
|
+
} | undefined;
|
|
1214
|
+
includeHistory?: boolean | undefined;
|
|
1215
|
+
}>;
|
|
1216
|
+
type ReflectionConfig = z.infer<typeof ReflectionConfigSchema>;
|
|
1217
|
+
|
|
1218
|
+
/**
|
|
1219
|
+
* Reflection state configuration
|
|
1220
|
+
*
|
|
1221
|
+
* This configuration is used with createStateAnnotation() to create a
|
|
1222
|
+
* LangGraph Annotation.Root() with optional Zod validation.
|
|
1223
|
+
*/
|
|
1224
|
+
declare const ReflectionStateConfig: {
|
|
1225
|
+
/**
|
|
1226
|
+
* Original user input/task
|
|
1227
|
+
*/
|
|
1228
|
+
input: {
|
|
1229
|
+
schema: z.ZodString;
|
|
1230
|
+
default: () => string;
|
|
1231
|
+
description: string;
|
|
1232
|
+
};
|
|
1233
|
+
/**
|
|
1234
|
+
* Current response/output
|
|
1235
|
+
*/
|
|
1236
|
+
currentResponse: {
|
|
1237
|
+
schema: z.ZodOptional<z.ZodString>;
|
|
1238
|
+
description: string;
|
|
1239
|
+
};
|
|
1240
|
+
/**
|
|
1241
|
+
* History of all reflections/critiques
|
|
1242
|
+
* Accumulates all reflections
|
|
1243
|
+
*/
|
|
1244
|
+
reflections: {
|
|
1245
|
+
schema: z.ZodArray<z.ZodObject<{
|
|
1246
|
+
critique: z.ZodString;
|
|
1247
|
+
issues: z.ZodArray<z.ZodString, "many">;
|
|
1248
|
+
suggestions: z.ZodArray<z.ZodString, "many">;
|
|
1249
|
+
score: z.ZodOptional<z.ZodNumber>;
|
|
1250
|
+
meetsStandards: z.ZodBoolean;
|
|
1251
|
+
timestamp: z.ZodOptional<z.ZodDate>;
|
|
1252
|
+
}, "strip", z.ZodTypeAny, {
|
|
1253
|
+
issues: string[];
|
|
1254
|
+
critique: string;
|
|
1255
|
+
suggestions: string[];
|
|
1256
|
+
meetsStandards: boolean;
|
|
1257
|
+
timestamp?: Date | undefined;
|
|
1258
|
+
score?: number | undefined;
|
|
1259
|
+
}, {
|
|
1260
|
+
issues: string[];
|
|
1261
|
+
critique: string;
|
|
1262
|
+
suggestions: string[];
|
|
1263
|
+
meetsStandards: boolean;
|
|
1264
|
+
timestamp?: Date | undefined;
|
|
1265
|
+
score?: number | undefined;
|
|
1266
|
+
}>, "many">;
|
|
1267
|
+
reducer: (left: Reflection[], right: Reflection[]) => {
|
|
1268
|
+
issues: string[];
|
|
1269
|
+
critique: string;
|
|
1270
|
+
suggestions: string[];
|
|
1271
|
+
meetsStandards: boolean;
|
|
1272
|
+
timestamp?: Date | undefined;
|
|
1273
|
+
score?: number | undefined;
|
|
1274
|
+
}[];
|
|
1275
|
+
default: () => never[];
|
|
1276
|
+
description: string;
|
|
1277
|
+
};
|
|
1278
|
+
/**
|
|
1279
|
+
* History of all revisions
|
|
1280
|
+
* Accumulates all revisions
|
|
1281
|
+
*/
|
|
1282
|
+
revisions: {
|
|
1283
|
+
schema: z.ZodArray<z.ZodObject<{
|
|
1284
|
+
content: z.ZodString;
|
|
1285
|
+
iteration: z.ZodNumber;
|
|
1286
|
+
basedOn: z.ZodOptional<z.ZodObject<{
|
|
1287
|
+
critique: z.ZodString;
|
|
1288
|
+
issues: z.ZodArray<z.ZodString, "many">;
|
|
1289
|
+
suggestions: z.ZodArray<z.ZodString, "many">;
|
|
1290
|
+
score: z.ZodOptional<z.ZodNumber>;
|
|
1291
|
+
meetsStandards: z.ZodBoolean;
|
|
1292
|
+
timestamp: z.ZodOptional<z.ZodDate>;
|
|
1293
|
+
}, "strip", z.ZodTypeAny, {
|
|
1294
|
+
issues: string[];
|
|
1295
|
+
critique: string;
|
|
1296
|
+
suggestions: string[];
|
|
1297
|
+
meetsStandards: boolean;
|
|
1298
|
+
timestamp?: Date | undefined;
|
|
1299
|
+
score?: number | undefined;
|
|
1300
|
+
}, {
|
|
1301
|
+
issues: string[];
|
|
1302
|
+
critique: string;
|
|
1303
|
+
suggestions: string[];
|
|
1304
|
+
meetsStandards: boolean;
|
|
1305
|
+
timestamp?: Date | undefined;
|
|
1306
|
+
score?: number | undefined;
|
|
1307
|
+
}>>;
|
|
1308
|
+
timestamp: z.ZodOptional<z.ZodDate>;
|
|
1309
|
+
}, "strip", z.ZodTypeAny, {
|
|
1310
|
+
content: string;
|
|
1311
|
+
iteration: number;
|
|
1312
|
+
timestamp?: Date | undefined;
|
|
1313
|
+
basedOn?: {
|
|
1314
|
+
issues: string[];
|
|
1315
|
+
critique: string;
|
|
1316
|
+
suggestions: string[];
|
|
1317
|
+
meetsStandards: boolean;
|
|
1318
|
+
timestamp?: Date | undefined;
|
|
1319
|
+
score?: number | undefined;
|
|
1320
|
+
} | undefined;
|
|
1321
|
+
}, {
|
|
1322
|
+
content: string;
|
|
1323
|
+
iteration: number;
|
|
1324
|
+
timestamp?: Date | undefined;
|
|
1325
|
+
basedOn?: {
|
|
1326
|
+
issues: string[];
|
|
1327
|
+
critique: string;
|
|
1328
|
+
suggestions: string[];
|
|
1329
|
+
meetsStandards: boolean;
|
|
1330
|
+
timestamp?: Date | undefined;
|
|
1331
|
+
score?: number | undefined;
|
|
1332
|
+
} | undefined;
|
|
1333
|
+
}>, "many">;
|
|
1334
|
+
reducer: (left: Revision[], right: Revision[]) => {
|
|
1335
|
+
content: string;
|
|
1336
|
+
iteration: number;
|
|
1337
|
+
timestamp?: Date | undefined;
|
|
1338
|
+
basedOn?: {
|
|
1339
|
+
issues: string[];
|
|
1340
|
+
critique: string;
|
|
1341
|
+
suggestions: string[];
|
|
1342
|
+
meetsStandards: boolean;
|
|
1343
|
+
timestamp?: Date | undefined;
|
|
1344
|
+
score?: number | undefined;
|
|
1345
|
+
} | undefined;
|
|
1346
|
+
}[];
|
|
1347
|
+
default: () => never[];
|
|
1348
|
+
description: string;
|
|
1349
|
+
};
|
|
1350
|
+
/**
|
|
1351
|
+
* Current iteration number
|
|
1352
|
+
*/
|
|
1353
|
+
iteration: {
|
|
1354
|
+
schema: z.ZodNumber;
|
|
1355
|
+
reducer: (left: number, right: number) => number;
|
|
1356
|
+
default: () => number;
|
|
1357
|
+
description: string;
|
|
1358
|
+
};
|
|
1359
|
+
/**
|
|
1360
|
+
* Current status
|
|
1361
|
+
*/
|
|
1362
|
+
status: {
|
|
1363
|
+
schema: z.ZodEnum<["generating", "reflecting", "revising", "completed", "failed"]>;
|
|
1364
|
+
default: () => ReflectionStatus;
|
|
1365
|
+
description: string;
|
|
1366
|
+
};
|
|
1367
|
+
/**
|
|
1368
|
+
* Quality criteria for completion
|
|
1369
|
+
*/
|
|
1370
|
+
qualityCriteria: {
|
|
1371
|
+
schema: z.ZodOptional<z.ZodObject<{
|
|
1372
|
+
minScore: z.ZodDefault<z.ZodNumber>;
|
|
1373
|
+
criteria: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
1374
|
+
requireAll: z.ZodDefault<z.ZodBoolean>;
|
|
1375
|
+
}, "strip", z.ZodTypeAny, {
|
|
1376
|
+
minScore: number;
|
|
1377
|
+
requireAll: boolean;
|
|
1378
|
+
criteria?: string[] | undefined;
|
|
1379
|
+
}, {
|
|
1380
|
+
minScore?: number | undefined;
|
|
1381
|
+
criteria?: string[] | undefined;
|
|
1382
|
+
requireAll?: boolean | undefined;
|
|
1383
|
+
}>>;
|
|
1384
|
+
description: string;
|
|
1385
|
+
};
|
|
1386
|
+
/**
|
|
1387
|
+
* Maximum iterations allowed
|
|
1388
|
+
*/
|
|
1389
|
+
maxIterations: {
|
|
1390
|
+
schema: z.ZodNumber;
|
|
1391
|
+
default: () => number;
|
|
1392
|
+
description: string;
|
|
1393
|
+
};
|
|
1394
|
+
/**
|
|
1395
|
+
* Final response (when completed)
|
|
1396
|
+
*/
|
|
1397
|
+
response: {
|
|
1398
|
+
schema: z.ZodOptional<z.ZodString>;
|
|
1399
|
+
description: string;
|
|
1400
|
+
};
|
|
1401
|
+
/**
|
|
1402
|
+
* Error message if failed
|
|
1403
|
+
*/
|
|
1404
|
+
error: {
|
|
1405
|
+
schema: z.ZodOptional<z.ZodString>;
|
|
1406
|
+
description: string;
|
|
1407
|
+
};
|
|
1408
|
+
};
|
|
1409
|
+
/**
|
|
1410
|
+
* Create the Reflection state annotation
|
|
1411
|
+
*
|
|
1412
|
+
* This uses LangGraph's Annotation.Root() under the hood, with optional Zod validation.
|
|
1413
|
+
*/
|
|
1414
|
+
declare const ReflectionState: _langchain_langgraph.AnnotationRoot<_langchain_langgraph.StateDefinition>;
|
|
1415
|
+
/**
|
|
1416
|
+
* TypeScript type for Reflection state
|
|
1417
|
+
*
|
|
1418
|
+
* Manually defined to work around TypeScript's inability to infer the .State property
|
|
1419
|
+
*/
|
|
1420
|
+
type ReflectionStateType = {
|
|
1421
|
+
input: string;
|
|
1422
|
+
currentResponse?: string;
|
|
1423
|
+
reflections: Reflection[];
|
|
1424
|
+
revisions: Revision[];
|
|
1425
|
+
iteration: number;
|
|
1426
|
+
status: ReflectionStatus;
|
|
1427
|
+
qualityCriteria?: QualityCriteria;
|
|
1428
|
+
maxIterations: number;
|
|
1429
|
+
response?: string;
|
|
1430
|
+
error?: string;
|
|
1431
|
+
};
|
|
1432
|
+
|
|
1433
|
+
/**
|
|
1434
|
+
* Type Definitions for Reflection Pattern
|
|
1435
|
+
*
|
|
1436
|
+
* This module defines TypeScript types for the Reflection pattern.
|
|
1437
|
+
*
|
|
1438
|
+
* @module patterns/reflection/types
|
|
1439
|
+
*/
|
|
1440
|
+
|
|
1441
|
+
/**
|
|
1442
|
+
* Configuration for the generator node
|
|
1443
|
+
*/
|
|
1444
|
+
interface GeneratorConfig {
|
|
1445
|
+
/**
|
|
1446
|
+
* Language model for generation
|
|
1447
|
+
*/
|
|
1448
|
+
llm: BaseChatModel;
|
|
1449
|
+
/**
|
|
1450
|
+
* System prompt for the generator
|
|
1451
|
+
*/
|
|
1452
|
+
systemPrompt?: string;
|
|
1453
|
+
/**
|
|
1454
|
+
* Whether to include verbose logging
|
|
1455
|
+
*/
|
|
1456
|
+
verbose?: boolean;
|
|
1457
|
+
}
|
|
1458
|
+
/**
|
|
1459
|
+
* Configuration for the reflector node
|
|
1460
|
+
*/
|
|
1461
|
+
interface ReflectorConfig {
|
|
1462
|
+
/**
|
|
1463
|
+
* Language model for reflection
|
|
1464
|
+
*/
|
|
1465
|
+
llm: BaseChatModel;
|
|
1466
|
+
/**
|
|
1467
|
+
* System prompt for the reflector
|
|
1468
|
+
*/
|
|
1469
|
+
systemPrompt?: string;
|
|
1470
|
+
/**
|
|
1471
|
+
* Quality criteria to evaluate against
|
|
1472
|
+
*/
|
|
1473
|
+
qualityCriteria?: QualityCriteria;
|
|
1474
|
+
/**
|
|
1475
|
+
* Whether to include verbose logging
|
|
1476
|
+
*/
|
|
1477
|
+
verbose?: boolean;
|
|
1478
|
+
}
|
|
1479
|
+
/**
|
|
1480
|
+
* Configuration for the reviser node
|
|
1481
|
+
*/
|
|
1482
|
+
interface ReviserConfig {
|
|
1483
|
+
/**
|
|
1484
|
+
* Language model for revision
|
|
1485
|
+
*/
|
|
1486
|
+
llm: BaseChatModel;
|
|
1487
|
+
/**
|
|
1488
|
+
* System prompt for the reviser
|
|
1489
|
+
*/
|
|
1490
|
+
systemPrompt?: string;
|
|
1491
|
+
/**
|
|
1492
|
+
* Whether to include verbose logging
|
|
1493
|
+
*/
|
|
1494
|
+
verbose?: boolean;
|
|
1495
|
+
}
|
|
1496
|
+
/**
|
|
1497
|
+
* Configuration for the reflection agent
|
|
1498
|
+
*/
|
|
1499
|
+
interface ReflectionAgentConfig {
|
|
1500
|
+
/**
|
|
1501
|
+
* Generator configuration
|
|
1502
|
+
*/
|
|
1503
|
+
generator: GeneratorConfig;
|
|
1504
|
+
/**
|
|
1505
|
+
* Reflector configuration
|
|
1506
|
+
*/
|
|
1507
|
+
reflector: ReflectorConfig;
|
|
1508
|
+
/**
|
|
1509
|
+
* Reviser configuration
|
|
1510
|
+
*/
|
|
1511
|
+
reviser: ReviserConfig;
|
|
1512
|
+
/**
|
|
1513
|
+
* Maximum number of reflection iterations
|
|
1514
|
+
*/
|
|
1515
|
+
maxIterations?: number;
|
|
1516
|
+
/**
|
|
1517
|
+
* Quality criteria for completion
|
|
1518
|
+
*/
|
|
1519
|
+
qualityCriteria?: QualityCriteria;
|
|
1520
|
+
/**
|
|
1521
|
+
* Whether to include verbose logging
|
|
1522
|
+
*/
|
|
1523
|
+
verbose?: boolean;
|
|
1524
|
+
}
|
|
1525
|
+
/**
|
|
1526
|
+
* Node function type for reflection pattern
|
|
1527
|
+
*/
|
|
1528
|
+
type ReflectionNode = (state: ReflectionStateType) => Promise<Partial<ReflectionStateType>>;
|
|
1529
|
+
/**
|
|
1530
|
+
* Routing decision for reflection pattern
|
|
1531
|
+
*/
|
|
1532
|
+
type ReflectionRoute = 'generate' | 'reflect' | 'revise' | 'finish' | 'error';
|
|
1533
|
+
/**
|
|
1534
|
+
* Router function type for reflection pattern
|
|
1535
|
+
*/
|
|
1536
|
+
type ReflectionRouter = (state: ReflectionStateType) => ReflectionRoute;
|
|
1537
|
+
|
|
1538
|
+
/**
|
|
1539
|
+
* Prompts for Reflection Pattern
|
|
1540
|
+
*
|
|
1541
|
+
* This module contains prompt templates for the Reflection pattern.
|
|
1542
|
+
*
|
|
1543
|
+
* @module patterns/reflection/prompts
|
|
1544
|
+
*/
|
|
1545
|
+
/**
|
|
1546
|
+
* Default system prompt for the generator
|
|
1547
|
+
*/
|
|
1548
|
+
declare const DEFAULT_GENERATOR_SYSTEM_PROMPT = "You are an expert content generator. Your task is to create high-quality responses to user requests.\n\nFocus on:\n- Clarity and coherence\n- Accuracy and correctness\n- Completeness and thoroughness\n- Appropriate tone and style\n\nGenerate the best possible response to the user's request.";
|
|
1549
|
+
/**
|
|
1550
|
+
* Default system prompt for the reflector
|
|
1551
|
+
*/
|
|
1552
|
+
declare const DEFAULT_REFLECTOR_SYSTEM_PROMPT = "You are an expert critic and reviewer. Your task is to provide constructive feedback on responses.\n\nEvaluate the response based on:\n- Clarity: Is it easy to understand?\n- Accuracy: Is the information correct?\n- Completeness: Does it fully address the request?\n- Quality: Is it well-written and professional?\n\nProvide specific, actionable feedback for improvement.";
|
|
1553
|
+
/**
|
|
1554
|
+
* Default system prompt for the reviser
|
|
1555
|
+
*/
|
|
1556
|
+
declare const DEFAULT_REVISER_SYSTEM_PROMPT = "You are an expert editor and reviser. Your task is to improve responses based on feedback.\n\nFocus on:\n- Addressing all identified issues\n- Implementing suggested improvements\n- Maintaining the core message\n- Enhancing overall quality\n\nCreate an improved version that addresses the critique.";
|
|
1557
|
+
/**
|
|
1558
|
+
* Template for generation prompt
|
|
1559
|
+
*/
|
|
1560
|
+
declare const GENERATION_PROMPT_TEMPLATE = "Please generate a response to the following request:\n\n{input}\n\n{context}\n\nProvide a high-quality, complete response.";
|
|
1561
|
+
/**
|
|
1562
|
+
* Template for reflection prompt
|
|
1563
|
+
*/
|
|
1564
|
+
declare const REFLECTION_PROMPT_TEMPLATE = "Please review and critique the following response:\n\nOriginal Request:\n{input}\n\nCurrent Response:\n{currentResponse}\n\n{criteria}\n\n{history}\n\nProvide a detailed critique including:\n1. What works well\n2. Specific issues or problems\n3. Suggestions for improvement\n4. A quality score (0-10)\n5. Whether it meets quality standards\n\nFormat your response as JSON with the following structure:\n{\n \"critique\": \"overall assessment\",\n \"issues\": [\"issue 1\", \"issue 2\"],\n \"suggestions\": [\"suggestion 1\", \"suggestion 2\"],\n \"score\": 8,\n \"meetsStandards\": true\n}";
|
|
1565
|
+
/**
|
|
1566
|
+
* Template for revision prompt
|
|
1567
|
+
*/
|
|
1568
|
+
declare const REVISION_PROMPT_TEMPLATE = "Please revise the following response based on the critique:\n\nOriginal Request:\n{input}\n\nCurrent Response:\n{currentResponse}\n\nCritique:\n{critique}\n\nIssues to Address:\n{issues}\n\nSuggestions:\n{suggestions}\n\n{history}\n\nCreate an improved version that addresses all the feedback while maintaining the core message.";
|
|
1569
|
+
/**
|
|
1570
|
+
* Template for quality criteria
|
|
1571
|
+
*/
|
|
1572
|
+
declare const QUALITY_CRITERIA_TEMPLATE = "Evaluate against these specific criteria:\n{criteria}\n\nMinimum required score: {minScore}/10\n{requireAll}";
|
|
1573
|
+
/**
|
|
1574
|
+
* Template for reflection history
|
|
1575
|
+
*/
|
|
1576
|
+
declare const REFLECTION_HISTORY_TEMPLATE = "Previous Reflections:\n{reflections}\n\nPrevious Revisions:\n{revisions}";
|
|
1577
|
+
/**
|
|
1578
|
+
* Template for formatting a single reflection
|
|
1579
|
+
*/
|
|
1580
|
+
declare const REFLECTION_ENTRY_TEMPLATE = "Iteration {iteration}:\nCritique: {critique}\nScore: {score}/10\nIssues: {issues}\nSuggestions: {suggestions}";
|
|
1581
|
+
/**
|
|
1582
|
+
* Template for formatting a single revision
|
|
1583
|
+
*/
|
|
1584
|
+
declare const REVISION_ENTRY_TEMPLATE = "Iteration {iteration}:\n{content}";
|
|
1585
|
+
|
|
1586
|
+
/**
|
|
1587
|
+
* Node Implementations for Reflection Pattern
|
|
1588
|
+
*
|
|
1589
|
+
* This module implements the core nodes for the Reflection agent pattern.
|
|
1590
|
+
*
|
|
1591
|
+
* @module patterns/reflection/nodes
|
|
1592
|
+
*/
|
|
1593
|
+
|
|
1594
|
+
/**
|
|
1595
|
+
* Create a generator node that creates initial responses
|
|
1596
|
+
*/
|
|
1597
|
+
declare function createGeneratorNode(config: GeneratorConfig): (state: ReflectionStateType) => Promise<Partial<ReflectionStateType>>;
|
|
1598
|
+
/**
|
|
1599
|
+
* Create a reflector node that critiques responses
|
|
1600
|
+
*/
|
|
1601
|
+
declare function createReflectorNode(config: ReflectorConfig): (state: ReflectionStateType) => Promise<Partial<ReflectionStateType>>;
|
|
1602
|
+
/**
|
|
1603
|
+
* Create a reviser node that improves responses based on critiques
|
|
1604
|
+
*/
|
|
1605
|
+
declare function createReviserNode(config: ReviserConfig): (state: ReflectionStateType) => Promise<Partial<ReflectionStateType>>;
|
|
1606
|
+
/**
|
|
1607
|
+
* Create a finisher node that marks the reflection as complete
|
|
1608
|
+
*/
|
|
1609
|
+
declare function createFinisherNode(): (state: ReflectionStateType) => Promise<Partial<ReflectionStateType>>;
|
|
1610
|
+
|
|
1611
|
+
/**
|
|
1612
|
+
* Reflection Agent Factory
|
|
1613
|
+
*
|
|
1614
|
+
* Creates a Reflection agent using LangGraph.
|
|
1615
|
+
*
|
|
1616
|
+
* @module patterns/reflection/agent
|
|
1617
|
+
*/
|
|
1618
|
+
|
|
1619
|
+
/**
|
|
1620
|
+
* Create a Reflection agent
|
|
1621
|
+
*
|
|
1622
|
+
* This function creates a compiled LangGraph StateGraph that implements the
|
|
1623
|
+
* Reflection pattern for iterative improvement through generation, reflection, and revision.
|
|
1624
|
+
*
|
|
1625
|
+
* @param config - Configuration for the Reflection agent
|
|
1626
|
+
* @returns A compiled LangGraph StateGraph
|
|
1627
|
+
*
|
|
1628
|
+
* @example
|
|
1629
|
+
* ```typescript
|
|
1630
|
+
* import { createReflectionAgent } from '@agentforge/patterns';
|
|
1631
|
+
* import { ChatOpenAI } from '@langchain/openai';
|
|
1632
|
+
*
|
|
1633
|
+
* const llm = new ChatOpenAI({ model: 'gpt-4' });
|
|
1634
|
+
*
|
|
1635
|
+
* const agent = createReflectionAgent({
|
|
1636
|
+
* generator: { llm },
|
|
1637
|
+
* reflector: { llm },
|
|
1638
|
+
* reviser: { llm },
|
|
1639
|
+
* maxIterations: 3,
|
|
1640
|
+
* qualityCriteria: {
|
|
1641
|
+
* minScore: 8,
|
|
1642
|
+
* criteria: ['clarity', 'accuracy', 'completeness']
|
|
1643
|
+
* }
|
|
1644
|
+
* });
|
|
1645
|
+
*
|
|
1646
|
+
* const result = await agent.invoke({
|
|
1647
|
+
* input: 'Write an essay about AI safety'
|
|
1648
|
+
* });
|
|
1649
|
+
* ```
|
|
1650
|
+
*/
|
|
1651
|
+
declare function createReflectionAgent(config: ReflectionAgentConfig): any;
|
|
1652
|
+
|
|
1653
|
+
/**
|
|
1654
|
+
* Zod Schemas for Multi-Agent Coordination Pattern
|
|
1655
|
+
*
|
|
1656
|
+
* This module defines the validation schemas for the Multi-Agent pattern.
|
|
1657
|
+
* The pattern enables multiple specialized agents to collaborate on complex tasks
|
|
1658
|
+
* through a supervisor that routes work and coordinates execution.
|
|
1659
|
+
*
|
|
1660
|
+
* @module patterns/multi-agent/schemas
|
|
1661
|
+
*/
|
|
1662
|
+
|
|
1663
|
+
/**
|
|
1664
|
+
* Schema for agent roles in the system
|
|
1665
|
+
*/
|
|
1666
|
+
declare const AgentRoleSchema: z.ZodEnum<["supervisor", "worker"]>;
|
|
1667
|
+
type AgentRole = z.infer<typeof AgentRoleSchema>;
|
|
1668
|
+
/**
|
|
1669
|
+
* Schema for message types in multi-agent communication
|
|
1670
|
+
*/
|
|
1671
|
+
declare const MessageTypeSchema: z.ZodEnum<["user_input", "task_assignment", "task_result", "handoff", "error", "completion"]>;
|
|
1672
|
+
type MessageType = z.infer<typeof MessageTypeSchema>;
|
|
1673
|
+
/**
|
|
1674
|
+
* Schema for a message in the multi-agent system
|
|
1675
|
+
*/
|
|
1676
|
+
declare const AgentMessageSchema: z.ZodObject<{
|
|
1677
|
+
/**
|
|
1678
|
+
* Unique identifier for the message
|
|
1679
|
+
*/
|
|
1680
|
+
id: z.ZodString;
|
|
1681
|
+
/**
|
|
1682
|
+
* Type of message
|
|
1683
|
+
*/
|
|
1684
|
+
type: z.ZodEnum<["user_input", "task_assignment", "task_result", "handoff", "error", "completion"]>;
|
|
1685
|
+
/**
|
|
1686
|
+
* Agent that sent the message
|
|
1687
|
+
*/
|
|
1688
|
+
from: z.ZodString;
|
|
1689
|
+
/**
|
|
1690
|
+
* Agent(s) that should receive the message
|
|
1691
|
+
*/
|
|
1692
|
+
to: z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>;
|
|
1693
|
+
/**
|
|
1694
|
+
* Message content
|
|
1695
|
+
*/
|
|
1696
|
+
content: z.ZodString;
|
|
1697
|
+
/**
|
|
1698
|
+
* Optional metadata
|
|
1699
|
+
*/
|
|
1700
|
+
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
1701
|
+
/**
|
|
1702
|
+
* Timestamp when message was created
|
|
1703
|
+
*/
|
|
1704
|
+
timestamp: z.ZodNumber;
|
|
1705
|
+
}, "strip", z.ZodTypeAny, {
|
|
1706
|
+
content: string;
|
|
1707
|
+
type: "error" | "user_input" | "task_assignment" | "task_result" | "handoff" | "completion";
|
|
1708
|
+
timestamp: number;
|
|
1709
|
+
id: string;
|
|
1710
|
+
from: string;
|
|
1711
|
+
to: string | string[];
|
|
1712
|
+
metadata?: Record<string, any> | undefined;
|
|
1713
|
+
}, {
|
|
1714
|
+
content: string;
|
|
1715
|
+
type: "error" | "user_input" | "task_assignment" | "task_result" | "handoff" | "completion";
|
|
1716
|
+
timestamp: number;
|
|
1717
|
+
id: string;
|
|
1718
|
+
from: string;
|
|
1719
|
+
to: string | string[];
|
|
1720
|
+
metadata?: Record<string, any> | undefined;
|
|
1721
|
+
}>;
|
|
1722
|
+
type AgentMessage = z.infer<typeof AgentMessageSchema>;
|
|
1723
|
+
/**
|
|
1724
|
+
* Schema for routing strategies
|
|
1725
|
+
*/
|
|
1726
|
+
declare const RoutingStrategySchema: z.ZodEnum<["llm-based", "rule-based", "round-robin", "skill-based", "load-balanced"]>;
|
|
1727
|
+
type RoutingStrategy = z.infer<typeof RoutingStrategySchema>;
|
|
1728
|
+
/**
|
|
1729
|
+
* Schema for routing decision
|
|
1730
|
+
*/
|
|
1731
|
+
declare const RoutingDecisionSchema: z.ZodObject<{
|
|
1732
|
+
/**
|
|
1733
|
+
* Target agent to route to
|
|
1734
|
+
*/
|
|
1735
|
+
targetAgent: z.ZodString;
|
|
1736
|
+
/**
|
|
1737
|
+
* Reasoning for the routing decision
|
|
1738
|
+
*/
|
|
1739
|
+
reasoning: z.ZodOptional<z.ZodString>;
|
|
1740
|
+
/**
|
|
1741
|
+
* Confidence in the routing decision (0-1)
|
|
1742
|
+
*/
|
|
1743
|
+
confidence: z.ZodOptional<z.ZodNumber>;
|
|
1744
|
+
/**
|
|
1745
|
+
* Strategy used for routing
|
|
1746
|
+
*/
|
|
1747
|
+
strategy: z.ZodEnum<["llm-based", "rule-based", "round-robin", "skill-based", "load-balanced"]>;
|
|
1748
|
+
/**
|
|
1749
|
+
* Timestamp of the routing decision
|
|
1750
|
+
*/
|
|
1751
|
+
timestamp: z.ZodOptional<z.ZodNumber>;
|
|
1752
|
+
}, "strip", z.ZodTypeAny, {
|
|
1753
|
+
targetAgent: string;
|
|
1754
|
+
strategy: "llm-based" | "rule-based" | "round-robin" | "skill-based" | "load-balanced";
|
|
1755
|
+
timestamp?: number | undefined;
|
|
1756
|
+
reasoning?: string | undefined;
|
|
1757
|
+
confidence?: number | undefined;
|
|
1758
|
+
}, {
|
|
1759
|
+
targetAgent: string;
|
|
1760
|
+
strategy: "llm-based" | "rule-based" | "round-robin" | "skill-based" | "load-balanced";
|
|
1761
|
+
timestamp?: number | undefined;
|
|
1762
|
+
reasoning?: string | undefined;
|
|
1763
|
+
confidence?: number | undefined;
|
|
1764
|
+
}>;
|
|
1765
|
+
type RoutingDecision = z.infer<typeof RoutingDecisionSchema>;
|
|
1766
|
+
/**
|
|
1767
|
+
* Schema for worker agent capabilities
|
|
1768
|
+
*/
|
|
1769
|
+
declare const WorkerCapabilitiesSchema: z.ZodObject<{
|
|
1770
|
+
/**
|
|
1771
|
+
* Skills/capabilities the agent has
|
|
1772
|
+
*/
|
|
1773
|
+
skills: z.ZodArray<z.ZodString, "many">;
|
|
1774
|
+
/**
|
|
1775
|
+
* Tools available to the agent
|
|
1776
|
+
*/
|
|
1777
|
+
tools: z.ZodArray<z.ZodString, "many">;
|
|
1778
|
+
/**
|
|
1779
|
+
* Whether the agent is currently available
|
|
1780
|
+
*/
|
|
1781
|
+
available: z.ZodDefault<z.ZodBoolean>;
|
|
1782
|
+
/**
|
|
1783
|
+
* Current workload (number of active tasks)
|
|
1784
|
+
*/
|
|
1785
|
+
currentWorkload: z.ZodDefault<z.ZodNumber>;
|
|
1786
|
+
}, "strip", z.ZodTypeAny, {
|
|
1787
|
+
tools: string[];
|
|
1788
|
+
skills: string[];
|
|
1789
|
+
available: boolean;
|
|
1790
|
+
currentWorkload: number;
|
|
1791
|
+
}, {
|
|
1792
|
+
tools: string[];
|
|
1793
|
+
skills: string[];
|
|
1794
|
+
available?: boolean | undefined;
|
|
1795
|
+
currentWorkload?: number | undefined;
|
|
1796
|
+
}>;
|
|
1797
|
+
type WorkerCapabilities = z.infer<typeof WorkerCapabilitiesSchema>;
|
|
1798
|
+
/**
|
|
1799
|
+
* Schema for task assignment
|
|
1800
|
+
*/
|
|
1801
|
+
declare const TaskAssignmentSchema: z.ZodObject<{
|
|
1802
|
+
/**
|
|
1803
|
+
* Unique assignment identifier
|
|
1804
|
+
*/
|
|
1805
|
+
id: z.ZodString;
|
|
1806
|
+
/**
|
|
1807
|
+
* Worker ID assigned to the task
|
|
1808
|
+
*/
|
|
1809
|
+
workerId: z.ZodString;
|
|
1810
|
+
/**
|
|
1811
|
+
* Task description
|
|
1812
|
+
*/
|
|
1813
|
+
task: z.ZodString;
|
|
1814
|
+
/**
|
|
1815
|
+
* Task priority (1-10, higher is more urgent)
|
|
1816
|
+
*/
|
|
1817
|
+
priority: z.ZodDefault<z.ZodNumber>;
|
|
1818
|
+
/**
|
|
1819
|
+
* Timestamp when task was assigned
|
|
1820
|
+
*/
|
|
1821
|
+
assignedAt: z.ZodNumber;
|
|
1822
|
+
/**
|
|
1823
|
+
* Optional deadline for task completion
|
|
1824
|
+
*/
|
|
1825
|
+
deadline: z.ZodOptional<z.ZodNumber>;
|
|
1826
|
+
}, "strip", z.ZodTypeAny, {
|
|
1827
|
+
id: string;
|
|
1828
|
+
workerId: string;
|
|
1829
|
+
task: string;
|
|
1830
|
+
priority: number;
|
|
1831
|
+
assignedAt: number;
|
|
1832
|
+
deadline?: number | undefined;
|
|
1833
|
+
}, {
|
|
1834
|
+
id: string;
|
|
1835
|
+
workerId: string;
|
|
1836
|
+
task: string;
|
|
1837
|
+
assignedAt: number;
|
|
1838
|
+
priority?: number | undefined;
|
|
1839
|
+
deadline?: number | undefined;
|
|
1840
|
+
}>;
|
|
1841
|
+
type TaskAssignment = z.infer<typeof TaskAssignmentSchema>;
|
|
1842
|
+
/**
|
|
1843
|
+
* Schema for task result
|
|
1844
|
+
*/
|
|
1845
|
+
declare const TaskResultSchema: z.ZodObject<{
|
|
1846
|
+
/**
|
|
1847
|
+
* Assignment identifier
|
|
1848
|
+
*/
|
|
1849
|
+
assignmentId: z.ZodString;
|
|
1850
|
+
/**
|
|
1851
|
+
* Worker that completed the task
|
|
1852
|
+
*/
|
|
1853
|
+
workerId: z.ZodString;
|
|
1854
|
+
/**
|
|
1855
|
+
* Whether the task succeeded
|
|
1856
|
+
*/
|
|
1857
|
+
success: z.ZodBoolean;
|
|
1858
|
+
/**
|
|
1859
|
+
* Task result/output
|
|
1860
|
+
*/
|
|
1861
|
+
result: z.ZodString;
|
|
1862
|
+
/**
|
|
1863
|
+
* Optional error message if task failed
|
|
1864
|
+
*/
|
|
1865
|
+
error: z.ZodOptional<z.ZodString>;
|
|
1866
|
+
/**
|
|
1867
|
+
* Timestamp when task was completed
|
|
1868
|
+
*/
|
|
1869
|
+
completedAt: z.ZodNumber;
|
|
1870
|
+
/**
|
|
1871
|
+
* Optional metadata about execution
|
|
1872
|
+
*/
|
|
1873
|
+
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
1874
|
+
}, "strip", z.ZodTypeAny, {
|
|
1875
|
+
result: string;
|
|
1876
|
+
success: boolean;
|
|
1877
|
+
workerId: string;
|
|
1878
|
+
assignmentId: string;
|
|
1879
|
+
completedAt: number;
|
|
1880
|
+
metadata?: Record<string, any> | undefined;
|
|
1881
|
+
error?: string | undefined;
|
|
1882
|
+
}, {
|
|
1883
|
+
result: string;
|
|
1884
|
+
success: boolean;
|
|
1885
|
+
workerId: string;
|
|
1886
|
+
assignmentId: string;
|
|
1887
|
+
completedAt: number;
|
|
1888
|
+
metadata?: Record<string, any> | undefined;
|
|
1889
|
+
error?: string | undefined;
|
|
1890
|
+
}>;
|
|
1891
|
+
type TaskResult = z.infer<typeof TaskResultSchema>;
|
|
1892
|
+
/**
|
|
1893
|
+
* Schema for multi-agent execution status
|
|
1894
|
+
*/
|
|
1895
|
+
declare const MultiAgentStatusSchema: z.ZodEnum<["initializing", "routing", "executing", "coordinating", "aggregating", "completed", "failed"]>;
|
|
1896
|
+
type MultiAgentStatus = z.infer<typeof MultiAgentStatusSchema>;
|
|
1897
|
+
/**
|
|
1898
|
+
* Schema for handoff request
|
|
1899
|
+
*/
|
|
1900
|
+
declare const HandoffRequestSchema: z.ZodObject<{
|
|
1901
|
+
/**
|
|
1902
|
+
* Agent requesting the handoff
|
|
1903
|
+
*/
|
|
1904
|
+
from: z.ZodString;
|
|
1905
|
+
/**
|
|
1906
|
+
* Target agent for handoff
|
|
1907
|
+
*/
|
|
1908
|
+
to: z.ZodString;
|
|
1909
|
+
/**
|
|
1910
|
+
* Reason for handoff
|
|
1911
|
+
*/
|
|
1912
|
+
reason: z.ZodString;
|
|
1913
|
+
/**
|
|
1914
|
+
* Context to pass to next agent
|
|
1915
|
+
*/
|
|
1916
|
+
context: z.ZodAny;
|
|
1917
|
+
/**
|
|
1918
|
+
* Timestamp of handoff request
|
|
1919
|
+
*/
|
|
1920
|
+
timestamp: z.ZodString;
|
|
1921
|
+
}, "strip", z.ZodTypeAny, {
|
|
1922
|
+
timestamp: string;
|
|
1923
|
+
reason: string;
|
|
1924
|
+
from: string;
|
|
1925
|
+
to: string;
|
|
1926
|
+
context?: any;
|
|
1927
|
+
}, {
|
|
1928
|
+
timestamp: string;
|
|
1929
|
+
reason: string;
|
|
1930
|
+
from: string;
|
|
1931
|
+
to: string;
|
|
1932
|
+
context?: any;
|
|
1933
|
+
}>;
|
|
1934
|
+
type HandoffRequest = z.infer<typeof HandoffRequestSchema>;
|
|
1935
|
+
|
|
1936
|
+
/**
|
|
1937
|
+
* Multi-Agent state configuration
|
|
1938
|
+
*
|
|
1939
|
+
* This configuration is used with createStateAnnotation() to create a
|
|
1940
|
+
* LangGraph Annotation.Root() with optional Zod validation.
|
|
1941
|
+
*/
|
|
1942
|
+
declare const MultiAgentStateConfig: {
|
|
1943
|
+
/**
|
|
1944
|
+
* Original user input/query
|
|
1945
|
+
*/
|
|
1946
|
+
input: {
|
|
1947
|
+
schema: z.ZodString;
|
|
1948
|
+
default: () => string;
|
|
1949
|
+
description: string;
|
|
1950
|
+
};
|
|
1951
|
+
/**
|
|
1952
|
+
* All messages in the multi-agent conversation
|
|
1953
|
+
* Accumulates all messages between agents
|
|
1954
|
+
*/
|
|
1955
|
+
messages: {
|
|
1956
|
+
schema: z.ZodArray<z.ZodObject<{
|
|
1957
|
+
id: z.ZodString;
|
|
1958
|
+
type: z.ZodEnum<["user_input", "task_assignment", "task_result", "handoff", "error", "completion"]>;
|
|
1959
|
+
from: z.ZodString;
|
|
1960
|
+
to: z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>;
|
|
1961
|
+
content: z.ZodString;
|
|
1962
|
+
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
1963
|
+
timestamp: z.ZodNumber;
|
|
1964
|
+
}, "strip", z.ZodTypeAny, {
|
|
1965
|
+
content: string;
|
|
1966
|
+
type: "error" | "user_input" | "task_assignment" | "task_result" | "handoff" | "completion";
|
|
1967
|
+
timestamp: number;
|
|
1968
|
+
id: string;
|
|
1969
|
+
from: string;
|
|
1970
|
+
to: string | string[];
|
|
1971
|
+
metadata?: Record<string, any> | undefined;
|
|
1972
|
+
}, {
|
|
1973
|
+
content: string;
|
|
1974
|
+
type: "error" | "user_input" | "task_assignment" | "task_result" | "handoff" | "completion";
|
|
1975
|
+
timestamp: number;
|
|
1976
|
+
id: string;
|
|
1977
|
+
from: string;
|
|
1978
|
+
to: string | string[];
|
|
1979
|
+
metadata?: Record<string, any> | undefined;
|
|
1980
|
+
}>, "many">;
|
|
1981
|
+
reducer: (left: AgentMessage[], right: AgentMessage[]) => {
|
|
1982
|
+
content: string;
|
|
1983
|
+
type: "error" | "user_input" | "task_assignment" | "task_result" | "handoff" | "completion";
|
|
1984
|
+
timestamp: number;
|
|
1985
|
+
id: string;
|
|
1986
|
+
from: string;
|
|
1987
|
+
to: string | string[];
|
|
1988
|
+
metadata?: Record<string, any> | undefined;
|
|
1989
|
+
}[];
|
|
1990
|
+
default: () => never[];
|
|
1991
|
+
description: string;
|
|
1992
|
+
};
|
|
1993
|
+
/**
|
|
1994
|
+
* Available worker agents and their capabilities
|
|
1995
|
+
*/
|
|
1996
|
+
workers: {
|
|
1997
|
+
schema: z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
1998
|
+
skills: z.ZodArray<z.ZodString, "many">;
|
|
1999
|
+
tools: z.ZodArray<z.ZodString, "many">;
|
|
2000
|
+
available: z.ZodDefault<z.ZodBoolean>;
|
|
2001
|
+
currentWorkload: z.ZodDefault<z.ZodNumber>;
|
|
2002
|
+
}, "strip", z.ZodTypeAny, {
|
|
2003
|
+
tools: string[];
|
|
2004
|
+
skills: string[];
|
|
2005
|
+
available: boolean;
|
|
2006
|
+
currentWorkload: number;
|
|
2007
|
+
}, {
|
|
2008
|
+
tools: string[];
|
|
2009
|
+
skills: string[];
|
|
2010
|
+
available?: boolean | undefined;
|
|
2011
|
+
currentWorkload?: number | undefined;
|
|
2012
|
+
}>>;
|
|
2013
|
+
reducer: (left: Record<string, WorkerCapabilities>, right: Record<string, WorkerCapabilities>) => {
|
|
2014
|
+
[x: string]: {
|
|
2015
|
+
tools: string[];
|
|
2016
|
+
skills: string[];
|
|
2017
|
+
available: boolean;
|
|
2018
|
+
currentWorkload: number;
|
|
2019
|
+
};
|
|
2020
|
+
};
|
|
2021
|
+
default: () => {};
|
|
2022
|
+
description: string;
|
|
2023
|
+
};
|
|
2024
|
+
/**
|
|
2025
|
+
* Current active agent
|
|
2026
|
+
*/
|
|
2027
|
+
currentAgent: {
|
|
2028
|
+
schema: z.ZodOptional<z.ZodString>;
|
|
2029
|
+
description: string;
|
|
2030
|
+
};
|
|
2031
|
+
/**
|
|
2032
|
+
* Routing decisions made by the supervisor
|
|
2033
|
+
* Accumulates all routing decisions
|
|
2034
|
+
*/
|
|
2035
|
+
routingHistory: {
|
|
2036
|
+
schema: z.ZodArray<z.ZodObject<{
|
|
2037
|
+
targetAgent: z.ZodString;
|
|
2038
|
+
reasoning: z.ZodOptional<z.ZodString>;
|
|
2039
|
+
confidence: z.ZodOptional<z.ZodNumber>;
|
|
2040
|
+
strategy: z.ZodEnum<["llm-based", "rule-based", "round-robin", "skill-based", "load-balanced"]>;
|
|
2041
|
+
timestamp: z.ZodOptional<z.ZodNumber>;
|
|
2042
|
+
}, "strip", z.ZodTypeAny, {
|
|
2043
|
+
targetAgent: string;
|
|
2044
|
+
strategy: "llm-based" | "rule-based" | "round-robin" | "skill-based" | "load-balanced";
|
|
2045
|
+
timestamp?: number | undefined;
|
|
2046
|
+
reasoning?: string | undefined;
|
|
2047
|
+
confidence?: number | undefined;
|
|
2048
|
+
}, {
|
|
2049
|
+
targetAgent: string;
|
|
2050
|
+
strategy: "llm-based" | "rule-based" | "round-robin" | "skill-based" | "load-balanced";
|
|
2051
|
+
timestamp?: number | undefined;
|
|
2052
|
+
reasoning?: string | undefined;
|
|
2053
|
+
confidence?: number | undefined;
|
|
2054
|
+
}>, "many">;
|
|
2055
|
+
reducer: (left: RoutingDecision[], right: RoutingDecision[]) => {
|
|
2056
|
+
targetAgent: string;
|
|
2057
|
+
strategy: "llm-based" | "rule-based" | "round-robin" | "skill-based" | "load-balanced";
|
|
2058
|
+
timestamp?: number | undefined;
|
|
2059
|
+
reasoning?: string | undefined;
|
|
2060
|
+
confidence?: number | undefined;
|
|
2061
|
+
}[];
|
|
2062
|
+
default: () => never[];
|
|
2063
|
+
description: string;
|
|
2064
|
+
};
|
|
2065
|
+
/**
|
|
2066
|
+
* Active task assignments
|
|
2067
|
+
*/
|
|
2068
|
+
activeAssignments: {
|
|
2069
|
+
schema: z.ZodArray<z.ZodObject<{
|
|
2070
|
+
id: z.ZodString;
|
|
2071
|
+
workerId: z.ZodString;
|
|
2072
|
+
task: z.ZodString;
|
|
2073
|
+
priority: z.ZodDefault<z.ZodNumber>;
|
|
2074
|
+
assignedAt: z.ZodNumber;
|
|
2075
|
+
deadline: z.ZodOptional<z.ZodNumber>;
|
|
2076
|
+
}, "strip", z.ZodTypeAny, {
|
|
2077
|
+
id: string;
|
|
2078
|
+
workerId: string;
|
|
2079
|
+
task: string;
|
|
2080
|
+
priority: number;
|
|
2081
|
+
assignedAt: number;
|
|
2082
|
+
deadline?: number | undefined;
|
|
2083
|
+
}, {
|
|
2084
|
+
id: string;
|
|
2085
|
+
workerId: string;
|
|
2086
|
+
task: string;
|
|
2087
|
+
assignedAt: number;
|
|
2088
|
+
priority?: number | undefined;
|
|
2089
|
+
deadline?: number | undefined;
|
|
2090
|
+
}>, "many">;
|
|
2091
|
+
reducer: (left: TaskAssignment[], right: TaskAssignment[]) => {
|
|
2092
|
+
id: string;
|
|
2093
|
+
workerId: string;
|
|
2094
|
+
task: string;
|
|
2095
|
+
priority: number;
|
|
2096
|
+
assignedAt: number;
|
|
2097
|
+
deadline?: number | undefined;
|
|
2098
|
+
}[];
|
|
2099
|
+
default: () => never[];
|
|
2100
|
+
description: string;
|
|
2101
|
+
};
|
|
2102
|
+
/**
|
|
2103
|
+
* Completed task results
|
|
2104
|
+
* Accumulates all completed tasks
|
|
2105
|
+
*/
|
|
2106
|
+
completedTasks: {
|
|
2107
|
+
schema: z.ZodArray<z.ZodObject<{
|
|
2108
|
+
assignmentId: z.ZodString;
|
|
2109
|
+
workerId: z.ZodString;
|
|
2110
|
+
success: z.ZodBoolean;
|
|
2111
|
+
result: z.ZodString;
|
|
2112
|
+
error: z.ZodOptional<z.ZodString>;
|
|
2113
|
+
completedAt: z.ZodNumber;
|
|
2114
|
+
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
2115
|
+
}, "strip", z.ZodTypeAny, {
|
|
2116
|
+
result: string;
|
|
2117
|
+
success: boolean;
|
|
2118
|
+
workerId: string;
|
|
2119
|
+
assignmentId: string;
|
|
2120
|
+
completedAt: number;
|
|
2121
|
+
metadata?: Record<string, any> | undefined;
|
|
2122
|
+
error?: string | undefined;
|
|
2123
|
+
}, {
|
|
2124
|
+
result: string;
|
|
2125
|
+
success: boolean;
|
|
2126
|
+
workerId: string;
|
|
2127
|
+
assignmentId: string;
|
|
2128
|
+
completedAt: number;
|
|
2129
|
+
metadata?: Record<string, any> | undefined;
|
|
2130
|
+
error?: string | undefined;
|
|
2131
|
+
}>, "many">;
|
|
2132
|
+
reducer: (left: TaskResult[], right: TaskResult[]) => {
|
|
2133
|
+
result: string;
|
|
2134
|
+
success: boolean;
|
|
2135
|
+
workerId: string;
|
|
2136
|
+
assignmentId: string;
|
|
2137
|
+
completedAt: number;
|
|
2138
|
+
metadata?: Record<string, any> | undefined;
|
|
2139
|
+
error?: string | undefined;
|
|
2140
|
+
}[];
|
|
2141
|
+
default: () => never[];
|
|
2142
|
+
description: string;
|
|
2143
|
+
};
|
|
2144
|
+
/**
|
|
2145
|
+
* Handoff requests between agents
|
|
2146
|
+
* Accumulates all handoff requests
|
|
2147
|
+
*/
|
|
2148
|
+
handoffs: {
|
|
2149
|
+
schema: z.ZodArray<z.ZodObject<{
|
|
2150
|
+
from: z.ZodString;
|
|
2151
|
+
to: z.ZodString;
|
|
2152
|
+
reason: z.ZodString;
|
|
2153
|
+
context: z.ZodAny;
|
|
2154
|
+
timestamp: z.ZodString;
|
|
2155
|
+
}, "strip", z.ZodTypeAny, {
|
|
2156
|
+
timestamp: string;
|
|
2157
|
+
reason: string;
|
|
2158
|
+
from: string;
|
|
2159
|
+
to: string;
|
|
2160
|
+
context?: any;
|
|
2161
|
+
}, {
|
|
2162
|
+
timestamp: string;
|
|
2163
|
+
reason: string;
|
|
2164
|
+
from: string;
|
|
2165
|
+
to: string;
|
|
2166
|
+
context?: any;
|
|
2167
|
+
}>, "many">;
|
|
2168
|
+
reducer: (left: HandoffRequest[], right: HandoffRequest[]) => {
|
|
2169
|
+
timestamp: string;
|
|
2170
|
+
reason: string;
|
|
2171
|
+
from: string;
|
|
2172
|
+
to: string;
|
|
2173
|
+
context?: any;
|
|
2174
|
+
}[];
|
|
2175
|
+
default: () => never[];
|
|
2176
|
+
description: string;
|
|
2177
|
+
};
|
|
2178
|
+
/**
|
|
2179
|
+
* Current execution status
|
|
2180
|
+
*/
|
|
2181
|
+
status: {
|
|
2182
|
+
schema: z.ZodEnum<["initializing", "routing", "executing", "coordinating", "aggregating", "completed", "failed"]>;
|
|
2183
|
+
default: () => MultiAgentStatus;
|
|
2184
|
+
description: string;
|
|
2185
|
+
};
|
|
2186
|
+
/**
|
|
2187
|
+
* Iteration counter
|
|
2188
|
+
*/
|
|
2189
|
+
iteration: {
|
|
2190
|
+
schema: z.ZodNumber;
|
|
2191
|
+
reducer: (left: number, right: number) => number;
|
|
2192
|
+
default: () => number;
|
|
2193
|
+
description: string;
|
|
2194
|
+
};
|
|
2195
|
+
/**
|
|
2196
|
+
* Maximum iterations allowed
|
|
2197
|
+
*/
|
|
2198
|
+
maxIterations: {
|
|
2199
|
+
schema: z.ZodNumber;
|
|
2200
|
+
default: () => number;
|
|
2201
|
+
description: string;
|
|
2202
|
+
};
|
|
2203
|
+
/**
|
|
2204
|
+
* Final aggregated response
|
|
2205
|
+
*/
|
|
2206
|
+
response: {
|
|
2207
|
+
schema: z.ZodOptional<z.ZodString>;
|
|
2208
|
+
description: string;
|
|
2209
|
+
};
|
|
2210
|
+
/**
|
|
2211
|
+
* Error message if execution failed
|
|
2212
|
+
*/
|
|
2213
|
+
error: {
|
|
2214
|
+
schema: z.ZodOptional<z.ZodString>;
|
|
2215
|
+
description: string;
|
|
2216
|
+
};
|
|
2217
|
+
};
|
|
2218
|
+
/**
|
|
2219
|
+
* Create the Multi-Agent state annotation
|
|
2220
|
+
*
|
|
2221
|
+
* This uses LangGraph's Annotation.Root() under the hood, with optional Zod validation.
|
|
2222
|
+
*/
|
|
2223
|
+
declare const MultiAgentState: _langchain_langgraph.AnnotationRoot<_langchain_langgraph.StateDefinition>;
|
|
2224
|
+
/**
|
|
2225
|
+
* TypeScript type for Multi-Agent state
|
|
2226
|
+
*
|
|
2227
|
+
* Manually defined to work around TypeScript's inability to infer the .State property
|
|
2228
|
+
*/
|
|
2229
|
+
type MultiAgentStateType = {
|
|
2230
|
+
input: string;
|
|
2231
|
+
messages: AgentMessage[];
|
|
2232
|
+
workers: Record<string, WorkerCapabilities>;
|
|
2233
|
+
currentAgent?: string;
|
|
2234
|
+
routingHistory: RoutingDecision[];
|
|
2235
|
+
activeAssignments: TaskAssignment[];
|
|
2236
|
+
completedTasks: TaskResult[];
|
|
2237
|
+
handoffs: HandoffRequest[];
|
|
2238
|
+
status: MultiAgentStatus;
|
|
2239
|
+
iteration: number;
|
|
2240
|
+
maxIterations: number;
|
|
2241
|
+
response?: string;
|
|
2242
|
+
error?: string;
|
|
2243
|
+
};
|
|
2244
|
+
|
|
2245
|
+
/**
|
|
2246
|
+
* Type Definitions for Multi-Agent Coordination Pattern
|
|
2247
|
+
*
|
|
2248
|
+
* This module defines TypeScript types for the Multi-Agent pattern.
|
|
2249
|
+
*
|
|
2250
|
+
* @module patterns/multi-agent/types
|
|
2251
|
+
*/
|
|
2252
|
+
|
|
2253
|
+
/**
|
|
2254
|
+
* Configuration for the supervisor node
|
|
2255
|
+
*/
|
|
2256
|
+
interface SupervisorConfig {
|
|
2257
|
+
/**
|
|
2258
|
+
* Language model for routing decisions (used for LLM-based routing)
|
|
2259
|
+
*/
|
|
2260
|
+
llm?: BaseChatModel;
|
|
2261
|
+
/**
|
|
2262
|
+
* Routing strategy to use
|
|
2263
|
+
*/
|
|
2264
|
+
strategy: RoutingStrategy;
|
|
2265
|
+
/**
|
|
2266
|
+
* System prompt for the supervisor (LLM-based routing only)
|
|
2267
|
+
*/
|
|
2268
|
+
systemPrompt?: string;
|
|
2269
|
+
/**
|
|
2270
|
+
* Custom routing function (for rule-based routing)
|
|
2271
|
+
*/
|
|
2272
|
+
routingFn?: (state: MultiAgentStateType) => Promise<RoutingDecision>;
|
|
2273
|
+
/**
|
|
2274
|
+
* Whether to include verbose logging
|
|
2275
|
+
*/
|
|
2276
|
+
verbose?: boolean;
|
|
2277
|
+
/**
|
|
2278
|
+
* Maximum number of routing iterations
|
|
2279
|
+
*/
|
|
2280
|
+
maxIterations?: number;
|
|
2281
|
+
}
|
|
2282
|
+
/**
|
|
2283
|
+
* Configuration for a worker agent node
|
|
2284
|
+
*/
|
|
2285
|
+
interface WorkerConfig {
|
|
2286
|
+
/**
|
|
2287
|
+
* Unique identifier for this worker
|
|
2288
|
+
*/
|
|
2289
|
+
id: string;
|
|
2290
|
+
/**
|
|
2291
|
+
* Worker capabilities
|
|
2292
|
+
*/
|
|
2293
|
+
capabilities: WorkerCapabilities;
|
|
2294
|
+
/**
|
|
2295
|
+
* Language model for the worker
|
|
2296
|
+
*/
|
|
2297
|
+
llm?: BaseChatModel;
|
|
2298
|
+
/**
|
|
2299
|
+
* Available tools for this worker
|
|
2300
|
+
*/
|
|
2301
|
+
tools?: Tool[];
|
|
2302
|
+
/**
|
|
2303
|
+
* System prompt for the worker
|
|
2304
|
+
*/
|
|
2305
|
+
systemPrompt?: string;
|
|
2306
|
+
/**
|
|
2307
|
+
* Whether to include verbose logging
|
|
2308
|
+
*/
|
|
2309
|
+
verbose?: boolean;
|
|
2310
|
+
/**
|
|
2311
|
+
* Custom execution function
|
|
2312
|
+
*/
|
|
2313
|
+
executeFn?: (state: MultiAgentStateType) => Promise<Partial<MultiAgentStateType>>;
|
|
2314
|
+
}
|
|
2315
|
+
/**
|
|
2316
|
+
* Configuration for the aggregator node
|
|
2317
|
+
*/
|
|
2318
|
+
interface AggregatorConfig {
|
|
2319
|
+
/**
|
|
2320
|
+
* Language model for aggregation (optional)
|
|
2321
|
+
*/
|
|
2322
|
+
llm?: BaseChatModel;
|
|
2323
|
+
/**
|
|
2324
|
+
* System prompt for aggregation
|
|
2325
|
+
*/
|
|
2326
|
+
systemPrompt?: string;
|
|
2327
|
+
/**
|
|
2328
|
+
* Custom aggregation function
|
|
2329
|
+
*/
|
|
2330
|
+
aggregateFn?: (state: MultiAgentStateType) => Promise<string>;
|
|
2331
|
+
/**
|
|
2332
|
+
* Whether to include verbose logging
|
|
2333
|
+
*/
|
|
2334
|
+
verbose?: boolean;
|
|
2335
|
+
}
|
|
2336
|
+
/**
|
|
2337
|
+
* Configuration for the multi-agent system
|
|
2338
|
+
*/
|
|
2339
|
+
interface MultiAgentSystemConfig {
|
|
2340
|
+
/**
|
|
2341
|
+
* Supervisor configuration
|
|
2342
|
+
*/
|
|
2343
|
+
supervisor: SupervisorConfig;
|
|
2344
|
+
/**
|
|
2345
|
+
* Worker configurations
|
|
2346
|
+
*/
|
|
2347
|
+
workers: WorkerConfig[];
|
|
2348
|
+
/**
|
|
2349
|
+
* Aggregator configuration (optional)
|
|
2350
|
+
*/
|
|
2351
|
+
aggregator?: AggregatorConfig;
|
|
2352
|
+
/**
|
|
2353
|
+
* Maximum iterations for the entire system
|
|
2354
|
+
*/
|
|
2355
|
+
maxIterations?: number;
|
|
2356
|
+
/**
|
|
2357
|
+
* Whether to include verbose logging
|
|
2358
|
+
*/
|
|
2359
|
+
verbose?: boolean;
|
|
2360
|
+
}
|
|
2361
|
+
/**
|
|
2362
|
+
* Node type for multi-agent graph
|
|
2363
|
+
*/
|
|
2364
|
+
type MultiAgentNode = 'supervisor' | 'aggregator' | string;
|
|
2365
|
+
/**
|
|
2366
|
+
* Route type for multi-agent graph
|
|
2367
|
+
*/
|
|
2368
|
+
type MultiAgentRoute = 'continue' | 'aggregate' | 'end' | string;
|
|
2369
|
+
/**
|
|
2370
|
+
* Router function type
|
|
2371
|
+
*/
|
|
2372
|
+
type MultiAgentRouter = (state: MultiAgentStateType) => MultiAgentRoute;
|
|
2373
|
+
/**
|
|
2374
|
+
* Routing strategy implementation
|
|
2375
|
+
*/
|
|
2376
|
+
interface RoutingStrategyImpl {
|
|
2377
|
+
/**
|
|
2378
|
+
* Name of the strategy
|
|
2379
|
+
*/
|
|
2380
|
+
name: RoutingStrategy;
|
|
2381
|
+
/**
|
|
2382
|
+
* Execute the routing strategy
|
|
2383
|
+
*/
|
|
2384
|
+
route: (state: MultiAgentStateType, config: SupervisorConfig) => Promise<RoutingDecision>;
|
|
2385
|
+
}
|
|
2386
|
+
|
|
2387
|
+
/**
|
|
2388
|
+
* Routing Strategy Implementations for Multi-Agent Pattern
|
|
2389
|
+
*
|
|
2390
|
+
* This module implements various routing strategies for distributing tasks
|
|
2391
|
+
* among worker agents.
|
|
2392
|
+
*
|
|
2393
|
+
* @module patterns/multi-agent/routing
|
|
2394
|
+
*/
|
|
2395
|
+
|
|
2396
|
+
/**
|
|
2397
|
+
* Default system prompt for LLM-based routing
|
|
2398
|
+
*/
|
|
2399
|
+
declare const DEFAULT_SUPERVISOR_SYSTEM_PROMPT = "You are a supervisor agent responsible for routing tasks to specialized worker agents.\n\nYour job is to:\n1. Analyze the current task and context\n2. Review available worker capabilities\n3. Select the most appropriate worker for the task\n4. Provide clear reasoning for your decision\n\nRespond with a JSON object containing:\n{\n \"targetAgent\": \"worker_id\",\n \"reasoning\": \"explanation of why this worker is best suited\",\n \"confidence\": 0.0-1.0,\n \"strategy\": \"llm-based\"\n}";
|
|
2400
|
+
/**
|
|
2401
|
+
* LLM-based routing strategy
|
|
2402
|
+
* Uses an LLM to intelligently route tasks based on worker capabilities
|
|
2403
|
+
*/
|
|
2404
|
+
declare const llmBasedRouting: RoutingStrategyImpl;
|
|
2405
|
+
/**
|
|
2406
|
+
* Round-robin routing strategy
|
|
2407
|
+
* Distributes tasks evenly across all available workers
|
|
2408
|
+
*/
|
|
2409
|
+
declare const roundRobinRouting: RoutingStrategyImpl;
|
|
2410
|
+
/**
|
|
2411
|
+
* Skill-based routing strategy
|
|
2412
|
+
* Routes tasks to workers based on matching skills
|
|
2413
|
+
*/
|
|
2414
|
+
declare const skillBasedRouting: RoutingStrategyImpl;
|
|
2415
|
+
/**
|
|
2416
|
+
* Load-balanced routing strategy
|
|
2417
|
+
* Routes tasks to workers with the lowest current workload
|
|
2418
|
+
*/
|
|
2419
|
+
declare const loadBalancedRouting: RoutingStrategyImpl;
|
|
2420
|
+
/**
|
|
2421
|
+
* Rule-based routing strategy
|
|
2422
|
+
* Uses custom routing function provided in config
|
|
2423
|
+
*/
|
|
2424
|
+
declare const ruleBasedRouting: RoutingStrategyImpl;
|
|
2425
|
+
/**
|
|
2426
|
+
* Get routing strategy implementation by name
|
|
2427
|
+
*/
|
|
2428
|
+
declare function getRoutingStrategy(name: string): RoutingStrategyImpl;
|
|
2429
|
+
|
|
2430
|
+
/**
|
|
2431
|
+
* Node Implementations for Multi-Agent Coordination Pattern
|
|
2432
|
+
*
|
|
2433
|
+
* This module implements the core nodes for the Multi-Agent pattern.
|
|
2434
|
+
*
|
|
2435
|
+
* @module patterns/multi-agent/nodes
|
|
2436
|
+
*/
|
|
2437
|
+
|
|
2438
|
+
/**
|
|
2439
|
+
* Default system prompt for aggregator
|
|
2440
|
+
*/
|
|
2441
|
+
declare const DEFAULT_AGGREGATOR_SYSTEM_PROMPT = "You are an aggregator agent responsible for combining results from multiple worker agents.\n\nYour job is to:\n1. Review all completed task results\n2. Synthesize the information into a coherent response\n3. Ensure all aspects of the original query are addressed\n4. Provide a clear, comprehensive final answer\n\nBe concise but thorough in your aggregation.";
|
|
2442
|
+
/**
|
|
2443
|
+
* Create a supervisor node that routes tasks to workers
|
|
2444
|
+
*/
|
|
2445
|
+
declare function createSupervisorNode(config: SupervisorConfig): (state: MultiAgentStateType) => Promise<Partial<MultiAgentStateType>>;
|
|
2446
|
+
/**
|
|
2447
|
+
* Create a worker agent node
|
|
2448
|
+
*/
|
|
2449
|
+
declare function createWorkerNode(config: WorkerConfig): (state: MultiAgentStateType) => Promise<Partial<MultiAgentStateType>>;
|
|
2450
|
+
/**
|
|
2451
|
+
* Create an aggregator node that combines worker results
|
|
2452
|
+
*/
|
|
2453
|
+
declare function createAggregatorNode(config?: AggregatorConfig): (state: MultiAgentStateType) => Promise<Partial<MultiAgentStateType>>;
|
|
2454
|
+
|
|
2455
|
+
/**
|
|
2456
|
+
* Create a multi-agent coordination system
|
|
2457
|
+
*
|
|
2458
|
+
* This factory function creates a complete multi-agent system with:
|
|
2459
|
+
* - A supervisor agent that routes tasks to workers
|
|
2460
|
+
* - Multiple specialized worker agents
|
|
2461
|
+
* - An aggregator that combines worker results
|
|
2462
|
+
*
|
|
2463
|
+
* @param config - Configuration for the multi-agent system
|
|
2464
|
+
* @returns Compiled LangGraph workflow
|
|
2465
|
+
*
|
|
2466
|
+
* @example
|
|
2467
|
+
* ```typescript
|
|
2468
|
+
* const system = createMultiAgentSystem({
|
|
2469
|
+
* supervisor: {
|
|
2470
|
+
* strategy: 'skill-based',
|
|
2471
|
+
* llm: chatModel,
|
|
2472
|
+
* },
|
|
2473
|
+
* workers: [
|
|
2474
|
+
* {
|
|
2475
|
+
* id: 'researcher',
|
|
2476
|
+
* capabilities: {
|
|
2477
|
+
* skills: ['research', 'analysis'],
|
|
2478
|
+
* tools: ['search', 'scrape'],
|
|
2479
|
+
* available: true,
|
|
2480
|
+
* currentWorkload: 0,
|
|
2481
|
+
* },
|
|
2482
|
+
* llm: chatModel,
|
|
2483
|
+
* },
|
|
2484
|
+
* {
|
|
2485
|
+
* id: 'writer',
|
|
2486
|
+
* capabilities: {
|
|
2487
|
+
* skills: ['writing', 'editing'],
|
|
2488
|
+
* tools: ['format', 'spell_check'],
|
|
2489
|
+
* available: true,
|
|
2490
|
+
* currentWorkload: 0,
|
|
2491
|
+
* },
|
|
2492
|
+
* llm: chatModel,
|
|
2493
|
+
* },
|
|
2494
|
+
* ],
|
|
2495
|
+
* aggregator: {
|
|
2496
|
+
* llm: chatModel,
|
|
2497
|
+
* },
|
|
2498
|
+
* });
|
|
2499
|
+
*
|
|
2500
|
+
* const result = await system.invoke({
|
|
2501
|
+
* input: 'Research AI trends and write a summary',
|
|
2502
|
+
* });
|
|
2503
|
+
* ```
|
|
2504
|
+
*/
|
|
2505
|
+
declare function createMultiAgentSystem(config: MultiAgentSystemConfig): CompiledStateGraph<unknown, Partial<unknown>, "__start__", _langchain_langgraph.StateDefinition, _langchain_langgraph.StateDefinition, _langchain_langgraph.StateDefinition>;
|
|
2506
|
+
/**
|
|
2507
|
+
* Multi-agent system builder for dynamic worker registration
|
|
2508
|
+
*
|
|
2509
|
+
* This builder allows you to register workers before compiling the graph.
|
|
2510
|
+
* Once compiled, the graph is immutable and workers cannot be added.
|
|
2511
|
+
*/
|
|
2512
|
+
declare class MultiAgentSystemBuilder {
|
|
2513
|
+
private config;
|
|
2514
|
+
private additionalWorkers;
|
|
2515
|
+
private compiled;
|
|
2516
|
+
constructor(config: Omit<MultiAgentSystemConfig, 'workers'> & {
|
|
2517
|
+
workers?: WorkerConfig[];
|
|
2518
|
+
});
|
|
2519
|
+
/**
|
|
2520
|
+
* Register workers with the system builder
|
|
2521
|
+
*
|
|
2522
|
+
* @param workers - Array of worker configurations
|
|
2523
|
+
* @returns this builder for chaining
|
|
2524
|
+
*
|
|
2525
|
+
* @example
|
|
2526
|
+
* ```typescript
|
|
2527
|
+
* const builder = new MultiAgentSystemBuilder({
|
|
2528
|
+
* supervisor: { llm, strategy: 'skill-based' },
|
|
2529
|
+
* aggregator: { llm },
|
|
2530
|
+
* });
|
|
2531
|
+
*
|
|
2532
|
+
* builder.registerWorkers([
|
|
2533
|
+
* {
|
|
2534
|
+
* name: 'math_worker',
|
|
2535
|
+
* capabilities: ['math', 'calculations'],
|
|
2536
|
+
* tools: [calculatorTool],
|
|
2537
|
+
* },
|
|
2538
|
+
* ]);
|
|
2539
|
+
*
|
|
2540
|
+
* const system = builder.build();
|
|
2541
|
+
* ```
|
|
2542
|
+
*/
|
|
2543
|
+
registerWorkers(workers: Array<{
|
|
2544
|
+
name: string;
|
|
2545
|
+
description?: string;
|
|
2546
|
+
capabilities: string[];
|
|
2547
|
+
tools?: any[];
|
|
2548
|
+
systemPrompt?: string;
|
|
2549
|
+
llm?: any;
|
|
2550
|
+
}>): this;
|
|
2551
|
+
/**
|
|
2552
|
+
* Build and compile the multi-agent system
|
|
2553
|
+
*
|
|
2554
|
+
* @returns Compiled LangGraph workflow
|
|
2555
|
+
*/
|
|
2556
|
+
build(): CompiledStateGraph<unknown, Partial<unknown>, "__start__", _langchain_langgraph.StateDefinition, _langchain_langgraph.StateDefinition, _langchain_langgraph.StateDefinition>;
|
|
2557
|
+
}
|
|
2558
|
+
/**
|
|
2559
|
+
* Extended multi-agent system with worker registration support
|
|
2560
|
+
*/
|
|
2561
|
+
interface MultiAgentSystemWithRegistry extends CompiledStateGraph<MultiAgentStateType, Partial<MultiAgentStateType>, '__start__' | 'supervisor' | 'aggregator' | string> {
|
|
2562
|
+
_workerRegistry?: Record<string, WorkerCapabilities>;
|
|
2563
|
+
_originalInvoke?: typeof CompiledStateGraph.prototype.invoke;
|
|
2564
|
+
}
|
|
2565
|
+
/**
|
|
2566
|
+
* Register workers with a compiled multi-agent system
|
|
2567
|
+
*
|
|
2568
|
+
* **Important**: This function only registers worker *capabilities* in the state.
|
|
2569
|
+
* It does NOT add worker nodes to the graph (which is impossible after compilation).
|
|
2570
|
+
*
|
|
2571
|
+
* This means:
|
|
2572
|
+
* - Workers must already exist as nodes in the compiled graph
|
|
2573
|
+
* - This function only updates their capabilities in the state
|
|
2574
|
+
* - For true dynamic worker registration, use `MultiAgentSystemBuilder` instead
|
|
2575
|
+
*
|
|
2576
|
+
* **Recommended**: Use `MultiAgentSystemBuilder` for a cleaner approach:
|
|
2577
|
+
* ```typescript
|
|
2578
|
+
* const builder = new MultiAgentSystemBuilder({
|
|
2579
|
+
* supervisor: { llm, strategy: 'skill-based' },
|
|
2580
|
+
* aggregator: { llm },
|
|
2581
|
+
* });
|
|
2582
|
+
*
|
|
2583
|
+
* builder.registerWorkers([...]);
|
|
2584
|
+
* const system = builder.build();
|
|
2585
|
+
* ```
|
|
2586
|
+
*
|
|
2587
|
+
* @param system - The compiled multi-agent system
|
|
2588
|
+
* @param workers - Array of worker configurations
|
|
2589
|
+
*
|
|
2590
|
+
* @deprecated Use `MultiAgentSystemBuilder` instead for proper worker registration
|
|
2591
|
+
*/
|
|
2592
|
+
declare function registerWorkers(system: MultiAgentSystemWithRegistry, workers: Array<{
|
|
2593
|
+
name: string;
|
|
2594
|
+
description?: string;
|
|
2595
|
+
capabilities: string[];
|
|
2596
|
+
tools?: any[];
|
|
2597
|
+
systemPrompt?: string;
|
|
2598
|
+
}>): void;
|
|
2599
|
+
|
|
2600
|
+
export { type AgentMessage, AgentMessageSchema, type AgentRole, AgentRoleSchema, type AggregatorConfig, type CompletedStep, CompletedStepSchema, DEFAULT_AGGREGATOR_SYSTEM_PROMPT, DEFAULT_GENERATOR_SYSTEM_PROMPT, DEFAULT_PLANNER_SYSTEM_PROMPT, DEFAULT_REACT_SYSTEM_PROMPT, DEFAULT_REFLECTOR_SYSTEM_PROMPT, DEFAULT_REPLANNER_SYSTEM_PROMPT, DEFAULT_REVISER_SYSTEM_PROMPT, DEFAULT_SUPERVISOR_SYSTEM_PROMPT, type ExecutionStatus, ExecutionStatusSchema, type ExecutorConfig, GENERATION_PROMPT_TEMPLATE, type GeneratorConfig, type HandoffRequest, HandoffRequestSchema, type Message, MessageSchema, type MessageType, MessageTypeSchema, type MultiAgentNode, type MultiAgentRoute, type MultiAgentRouter, MultiAgentState, MultiAgentStateConfig, type MultiAgentStateType, type MultiAgentStatus, MultiAgentStatusSchema, MultiAgentSystemBuilder, type MultiAgentSystemConfig, PLANNING_PROMPT_TEMPLATE, type Plan, type PlanExecuteAgentConfig, type PlanExecuteNode, type PlanExecuteRoute, type PlanExecuteRouter, PlanExecuteState, PlanExecuteStateConfig, type PlanExecuteStateType, PlanSchema, type PlanStep, PlanStepSchema, type PlannerConfig, QUALITY_CRITERIA_TEMPLATE, type QualityCriteria, QualityCriteriaSchema, REFLECTION_ENTRY_TEMPLATE, REFLECTION_HISTORY_TEMPLATE, REFLECTION_PROMPT_TEMPLATE, REPLANNING_PROMPT_TEMPLATE, REVISION_ENTRY_TEMPLATE, REVISION_PROMPT_TEMPLATE, ReActAgentBuilder, type ReActAgentConfig, type ReActAgentOptions, type ReActBuilderOptions, ReActState, type ReActStateType, type Reflection, type ReflectionAgentConfig, type ReflectionConfig, ReflectionConfigSchema, type ReflectionNode, type ReflectionRoute, type ReflectionRouter, ReflectionSchema, ReflectionState, ReflectionStateConfig, type ReflectionStateType, type ReflectionStatus, ReflectionStatusSchema, type ReflectorConfig, type ReplanDecision, ReplanDecisionSchema, type ReplannerConfig, type ReviserConfig, type Revision, RevisionSchema, type RoutingDecision, RoutingDecisionSchema, type RoutingStrategy, type RoutingStrategyImpl, RoutingStrategySchema, type ScratchpadEntry, ScratchpadEntrySchema, type StopConditionFn, type SupervisorConfig, type TaskAssignment, TaskAssignmentSchema, type TaskResult, TaskResultSchema, type Thought, ThoughtSchema, type ToolCall, ToolCallSchema, type ToolResult, ToolResultSchema, type WorkerCapabilities, WorkerCapabilitiesSchema, type WorkerConfig, createAggregatorNode, createExecutorNode, createFinisherNode$1 as createFinisherNode, createGeneratorNode, createMultiAgentSystem, createPlanExecuteAgent, createPlannerNode, createReActAgent, createReActAgentBuilder, createReflectionAgent, createFinisherNode as createReflectionFinisherNode, createReflectorNode, createReplannerNode, createReviserNode, createSupervisorNode, createWorkerNode, getRoutingStrategy, llmBasedRouting, loadBalancedRouting, registerWorkers, roundRobinRouting, ruleBasedRouting, skillBasedRouting };
|