@defai.digital/agent-domain 13.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/LICENSE +214 -0
  2. package/dist/enhanced-executor.d.ts +170 -0
  3. package/dist/enhanced-executor.d.ts.map +1 -0
  4. package/dist/enhanced-executor.js +1072 -0
  5. package/dist/enhanced-executor.js.map +1 -0
  6. package/dist/executor.d.ts +120 -0
  7. package/dist/executor.d.ts.map +1 -0
  8. package/dist/executor.js +929 -0
  9. package/dist/executor.js.map +1 -0
  10. package/dist/index.d.ts +25 -0
  11. package/dist/index.d.ts.map +1 -0
  12. package/dist/index.js +34 -0
  13. package/dist/index.js.map +1 -0
  14. package/dist/loader.d.ts +50 -0
  15. package/dist/loader.d.ts.map +1 -0
  16. package/dist/loader.js +160 -0
  17. package/dist/loader.js.map +1 -0
  18. package/dist/persistent-registry.d.ts +105 -0
  19. package/dist/persistent-registry.d.ts.map +1 -0
  20. package/dist/persistent-registry.js +183 -0
  21. package/dist/persistent-registry.js.map +1 -0
  22. package/dist/production-factories.d.ts +70 -0
  23. package/dist/production-factories.d.ts.map +1 -0
  24. package/dist/production-factories.js +434 -0
  25. package/dist/production-factories.js.map +1 -0
  26. package/dist/prompt-executor.d.ts +119 -0
  27. package/dist/prompt-executor.d.ts.map +1 -0
  28. package/dist/prompt-executor.js +211 -0
  29. package/dist/prompt-executor.js.map +1 -0
  30. package/dist/registry.d.ts +57 -0
  31. package/dist/registry.d.ts.map +1 -0
  32. package/dist/registry.js +123 -0
  33. package/dist/registry.js.map +1 -0
  34. package/dist/selection-service.d.ts +74 -0
  35. package/dist/selection-service.d.ts.map +1 -0
  36. package/dist/selection-service.js +322 -0
  37. package/dist/selection-service.js.map +1 -0
  38. package/dist/selector.d.ts +51 -0
  39. package/dist/selector.d.ts.map +1 -0
  40. package/dist/selector.js +249 -0
  41. package/dist/selector.js.map +1 -0
  42. package/dist/stub-checkpoint.d.ts +23 -0
  43. package/dist/stub-checkpoint.d.ts.map +1 -0
  44. package/dist/stub-checkpoint.js +137 -0
  45. package/dist/stub-checkpoint.js.map +1 -0
  46. package/dist/stub-delegation-tracker.d.ts +25 -0
  47. package/dist/stub-delegation-tracker.d.ts.map +1 -0
  48. package/dist/stub-delegation-tracker.js +118 -0
  49. package/dist/stub-delegation-tracker.js.map +1 -0
  50. package/dist/stub-parallel-executor.d.ts +19 -0
  51. package/dist/stub-parallel-executor.d.ts.map +1 -0
  52. package/dist/stub-parallel-executor.js +176 -0
  53. package/dist/stub-parallel-executor.js.map +1 -0
  54. package/dist/types.d.ts +614 -0
  55. package/dist/types.d.ts.map +1 -0
  56. package/dist/types.js +15 -0
  57. package/dist/types.js.map +1 -0
  58. package/dist/workflow-templates.d.ts +117 -0
  59. package/dist/workflow-templates.d.ts.map +1 -0
  60. package/dist/workflow-templates.js +342 -0
  61. package/dist/workflow-templates.js.map +1 -0
  62. package/package.json +51 -0
  63. package/src/enhanced-executor.ts +1395 -0
  64. package/src/executor.ts +1153 -0
  65. package/src/index.ts +172 -0
  66. package/src/loader.ts +191 -0
  67. package/src/persistent-registry.ts +235 -0
  68. package/src/production-factories.ts +613 -0
  69. package/src/prompt-executor.ts +310 -0
  70. package/src/registry.ts +167 -0
  71. package/src/selection-service.ts +411 -0
  72. package/src/selector.ts +299 -0
  73. package/src/stub-checkpoint.ts +187 -0
  74. package/src/stub-delegation-tracker.ts +161 -0
  75. package/src/stub-parallel-executor.ts +224 -0
  76. package/src/types.ts +784 -0
  77. package/src/workflow-templates.ts +393 -0
@@ -0,0 +1,393 @@
1
+ /**
2
+ * Agent Workflow Templates
3
+ *
4
+ * Predefined workflow templates for common agent patterns.
5
+ * These templates can be used when registering agents to provide
6
+ * standard workflow steps without custom configuration.
7
+ *
8
+ * Usage:
9
+ * - Use 'prompt-response' for simple Q&A agents
10
+ * - Use 'research' for agents that need to gather information
11
+ * - Use 'code-review' for agents that analyze code
12
+ * - Use 'multi-step' for agents with sequential processing
13
+ */
14
+
15
+ import type { AgentWorkflowStep } from '@defai.digital/contracts';
16
+
17
+ /**
18
+ * Available workflow template names
19
+ */
20
+ export type WorkflowTemplateName =
21
+ | 'prompt-response'
22
+ | 'research'
23
+ | 'code-review'
24
+ | 'multi-step'
25
+ | 'delegate-chain'
26
+ | 'agent-selection';
27
+
28
+ /**
29
+ * Workflow template definition
30
+ */
31
+ export interface WorkflowTemplate {
32
+ /** Template identifier */
33
+ name: WorkflowTemplateName;
34
+
35
+ /** Human-readable description */
36
+ description: string;
37
+
38
+ /** Workflow steps */
39
+ steps: AgentWorkflowStep[];
40
+
41
+ /** Required configuration keys */
42
+ requiredConfig?: string[];
43
+
44
+ /** Optional configuration with defaults */
45
+ defaultConfig?: Record<string, unknown>;
46
+ }
47
+
48
+ /**
49
+ * Prompt-Response Template
50
+ *
51
+ * Simple single-step workflow for Q&A style interactions.
52
+ * The agent uses its system prompt to respond to user input.
53
+ */
54
+ export const PROMPT_RESPONSE_TEMPLATE: WorkflowTemplate = {
55
+ name: 'prompt-response',
56
+ description: 'Simple single-step prompt-response workflow. Uses system prompt to generate response.',
57
+ steps: [
58
+ {
59
+ stepId: 'respond',
60
+ name: 'Generate Response',
61
+ description: 'Process input using system prompt and generate response',
62
+ type: 'prompt',
63
+ config: {
64
+ prompt: '{{input}}',
65
+ useSystemPrompt: true,
66
+ },
67
+ },
68
+ ],
69
+ };
70
+
71
+ /**
72
+ * Research Template
73
+ *
74
+ * Multi-step workflow for research tasks:
75
+ * 1. Analyze the research question
76
+ * 2. Gather information (via tools if available)
77
+ * 3. Synthesize findings into a response
78
+ */
79
+ export const RESEARCH_TEMPLATE: WorkflowTemplate = {
80
+ name: 'research',
81
+ description: 'Research workflow with analysis, gathering, and synthesis steps.',
82
+ steps: [
83
+ {
84
+ stepId: 'analyze',
85
+ name: 'Analyze Question',
86
+ description: 'Break down the research question and identify key aspects',
87
+ type: 'prompt',
88
+ config: {
89
+ prompt: 'Analyze this research question and identify the key aspects to investigate:\n\n{{input}}\n\nList the main points to research.',
90
+ useSystemPrompt: true,
91
+ },
92
+ },
93
+ {
94
+ stepId: 'gather',
95
+ name: 'Gather Information',
96
+ description: 'Collect relevant information based on analysis',
97
+ type: 'prompt',
98
+ dependencies: ['analyze'],
99
+ config: {
100
+ prompt: 'Based on the analysis:\n\n{{steps.analyze.output}}\n\nProvide detailed information on each aspect. Use your knowledge to answer comprehensively.',
101
+ useSystemPrompt: true,
102
+ },
103
+ },
104
+ {
105
+ stepId: 'synthesize',
106
+ name: 'Synthesize Response',
107
+ description: 'Combine gathered information into a coherent response',
108
+ type: 'prompt',
109
+ dependencies: ['gather'],
110
+ config: {
111
+ prompt: 'Synthesize the following information into a clear, comprehensive response to the original question:\n\nOriginal Question: {{input}}\n\nGathered Information:\n{{steps.gather.output}}\n\nProvide a well-structured response.',
112
+ useSystemPrompt: true,
113
+ },
114
+ },
115
+ ],
116
+ };
117
+
118
+ /**
119
+ * Code Review Template
120
+ *
121
+ * Workflow for reviewing code:
122
+ * 1. Understand the code structure
123
+ * 2. Analyze for issues and patterns
124
+ * 3. Generate review report
125
+ */
126
+ export const CODE_REVIEW_TEMPLATE: WorkflowTemplate = {
127
+ name: 'code-review',
128
+ description: 'Code review workflow with structure analysis, issue detection, and report generation.',
129
+ steps: [
130
+ {
131
+ stepId: 'understand',
132
+ name: 'Understand Code',
133
+ description: 'Analyze the structure and purpose of the code',
134
+ type: 'prompt',
135
+ config: {
136
+ prompt: 'Analyze this code and explain its structure, purpose, and main components:\n\n{{input}}',
137
+ useSystemPrompt: true,
138
+ },
139
+ },
140
+ {
141
+ stepId: 'analyze',
142
+ name: 'Analyze Issues',
143
+ description: 'Identify potential issues, bugs, and improvement opportunities',
144
+ type: 'prompt',
145
+ dependencies: ['understand'],
146
+ config: {
147
+ prompt: 'Based on the code understanding:\n\n{{steps.understand.output}}\n\nIdentify:\n1. Potential bugs or errors\n2. Performance issues\n3. Security concerns\n4. Code style and best practices\n5. Opportunities for improvement',
148
+ useSystemPrompt: true,
149
+ },
150
+ },
151
+ {
152
+ stepId: 'report',
153
+ name: 'Generate Report',
154
+ description: 'Create a structured code review report',
155
+ type: 'prompt',
156
+ dependencies: ['analyze'],
157
+ config: {
158
+ prompt: 'Create a structured code review report based on the analysis:\n\n{{steps.analyze.output}}\n\nFormat the report with:\n- Summary\n- Critical Issues (if any)\n- Warnings\n- Suggestions\n- Overall Assessment',
159
+ useSystemPrompt: true,
160
+ },
161
+ },
162
+ ],
163
+ };
164
+
165
+ /**
166
+ * Multi-Step Template
167
+ *
168
+ * Generic multi-step workflow for sequential processing:
169
+ * 1. Plan the approach
170
+ * 2. Execute the plan
171
+ * 3. Review and refine
172
+ */
173
+ export const MULTI_STEP_TEMPLATE: WorkflowTemplate = {
174
+ name: 'multi-step',
175
+ description: 'Generic multi-step workflow with planning, execution, and review phases.',
176
+ steps: [
177
+ {
178
+ stepId: 'plan',
179
+ name: 'Plan Approach',
180
+ description: 'Create a plan for addressing the task',
181
+ type: 'prompt',
182
+ config: {
183
+ prompt: 'Create a step-by-step plan to address this task:\n\n{{input}}\n\nList the concrete steps needed.',
184
+ useSystemPrompt: true,
185
+ },
186
+ },
187
+ {
188
+ stepId: 'execute',
189
+ name: 'Execute Plan',
190
+ description: 'Execute each step of the plan',
191
+ type: 'prompt',
192
+ dependencies: ['plan'],
193
+ config: {
194
+ prompt: 'Execute the following plan step by step:\n\n{{steps.plan.output}}\n\nProvide the output for each step.',
195
+ useSystemPrompt: true,
196
+ },
197
+ },
198
+ {
199
+ stepId: 'review',
200
+ name: 'Review Results',
201
+ description: 'Review and refine the execution results',
202
+ type: 'prompt',
203
+ dependencies: ['execute'],
204
+ config: {
205
+ prompt: 'Review the execution results:\n\n{{steps.execute.output}}\n\nProvide:\n1. Summary of what was accomplished\n2. Any refinements or corrections needed\n3. Final response to the original task: {{input}}',
206
+ useSystemPrompt: true,
207
+ },
208
+ },
209
+ ],
210
+ };
211
+
212
+ /**
213
+ * Delegate Chain Template
214
+ *
215
+ * Workflow that demonstrates delegation to other agents.
216
+ * Useful for orchestration patterns.
217
+ */
218
+ export const DELEGATE_CHAIN_TEMPLATE: WorkflowTemplate = {
219
+ name: 'delegate-chain',
220
+ description: 'Workflow that delegates to specialized agents for different aspects.',
221
+ steps: [
222
+ {
223
+ stepId: 'analyze-task',
224
+ name: 'Analyze Task',
225
+ description: 'Analyze the task and determine delegation strategy',
226
+ type: 'prompt',
227
+ config: {
228
+ prompt: 'Analyze this task and determine which aspects should be handled:\n\n{{input}}\n\nIdentify the key components and what expertise is needed.',
229
+ useSystemPrompt: true,
230
+ },
231
+ },
232
+ {
233
+ stepId: 'coordinate',
234
+ name: 'Coordinate Work',
235
+ description: 'Coordinate the work based on analysis',
236
+ type: 'prompt',
237
+ dependencies: ['analyze-task'],
238
+ config: {
239
+ prompt: 'Based on the task analysis:\n\n{{steps.analyze-task.output}}\n\nDetermine how to best address each component. Provide your response.',
240
+ useSystemPrompt: true,
241
+ },
242
+ },
243
+ {
244
+ stepId: 'finalize',
245
+ name: 'Finalize Response',
246
+ description: 'Combine all results into final response',
247
+ type: 'prompt',
248
+ dependencies: ['coordinate'],
249
+ config: {
250
+ prompt: 'Combine all the work into a final comprehensive response:\n\nOriginal Task: {{input}}\n\nCoordinated Work:\n{{steps.coordinate.output}}\n\nProvide the final response.',
251
+ useSystemPrompt: true,
252
+ },
253
+ },
254
+ ],
255
+ requiredConfig: [],
256
+ };
257
+
258
+ /**
259
+ * Agent Selection Template
260
+ *
261
+ * Workflow for intelligent agent selection:
262
+ * 1. Analyze the task to understand requirements
263
+ * 2. Match task against agent capabilities
264
+ * 3. Select best agent with confidence scoring
265
+ *
266
+ * Invariants enforced:
267
+ * - INV-AGT-SEL-001: Deterministic selection
268
+ * - INV-AGT-SEL-002: Confidence range [0,1]
269
+ * - INV-AGT-SEL-003: Results sorted by confidence
270
+ * - INV-AGT-SEL-004: Fallback to standard agent
271
+ * - INV-AGT-SEL-005: exampleTasks boost scoring
272
+ * - INV-AGT-SEL-006: notForTasks reduce scoring
273
+ */
274
+ export const AGENT_SELECTION_TEMPLATE: WorkflowTemplate = {
275
+ name: 'agent-selection',
276
+ description: 'Workflow for selecting the best agent based on task analysis and capability matching.',
277
+ steps: [
278
+ {
279
+ stepId: 'analyze-task',
280
+ name: 'Analyze Task Requirements',
281
+ description: 'Extract key requirements and intent from the task description',
282
+ type: 'prompt',
283
+ config: {
284
+ prompt: 'Analyze this task and extract:\n1. Primary intent (what needs to be done)\n2. Required capabilities\n3. Domain keywords\n4. Task category\n\nTask: {{input}}\n\nProvide a structured analysis.',
285
+ useSystemPrompt: true,
286
+ },
287
+ },
288
+ {
289
+ stepId: 'match-capabilities',
290
+ name: 'Match Against Capabilities',
291
+ description: 'Score available agents based on task requirements',
292
+ type: 'tool',
293
+ dependencies: ['analyze-task'],
294
+ config: {
295
+ tool: 'agent_recommend',
296
+ passTaskAnalysis: true,
297
+ },
298
+ },
299
+ {
300
+ stepId: 'validate-selection',
301
+ name: 'Validate Selection',
302
+ description: 'Verify the selected agent is appropriate and provide reasoning',
303
+ type: 'prompt',
304
+ dependencies: ['match-capabilities'],
305
+ config: {
306
+ prompt: 'Based on the task analysis:\n{{steps.analyze-task.output}}\n\nAnd the agent recommendation:\n{{steps.match-capabilities.output}}\n\nValidate this is the best choice and explain:\n1. Why this agent is suitable\n2. What capabilities match the task\n3. Any limitations to be aware of\n4. Confidence level in the selection',
307
+ useSystemPrompt: true,
308
+ },
309
+ },
310
+ ],
311
+ requiredConfig: [],
312
+ defaultConfig: {
313
+ minConfidence: 0.3,
314
+ maxAlternatives: 3,
315
+ },
316
+ };
317
+
318
+ /**
319
+ * All available workflow templates
320
+ */
321
+ export const WORKFLOW_TEMPLATES: Record<WorkflowTemplateName, WorkflowTemplate> = {
322
+ 'prompt-response': PROMPT_RESPONSE_TEMPLATE,
323
+ 'research': RESEARCH_TEMPLATE,
324
+ 'code-review': CODE_REVIEW_TEMPLATE,
325
+ 'multi-step': MULTI_STEP_TEMPLATE,
326
+ 'delegate-chain': DELEGATE_CHAIN_TEMPLATE,
327
+ 'agent-selection': AGENT_SELECTION_TEMPLATE,
328
+ };
329
+
330
+ /**
331
+ * Gets a workflow template by name
332
+ */
333
+ export function getWorkflowTemplate(name: string): WorkflowTemplate | undefined {
334
+ return WORKFLOW_TEMPLATES[name as WorkflowTemplateName];
335
+ }
336
+
337
+ /**
338
+ * Gets all available template names
339
+ */
340
+ export function getAvailableTemplates(): WorkflowTemplateName[] {
341
+ return Object.keys(WORKFLOW_TEMPLATES) as WorkflowTemplateName[];
342
+ }
343
+
344
+ /**
345
+ * Creates workflow steps from a template
346
+ * Optionally applies custom configuration to the template
347
+ */
348
+ export function createWorkflowFromTemplate(
349
+ templateName: string,
350
+ customConfig?: Record<string, unknown>
351
+ ): AgentWorkflowStep[] | undefined {
352
+ const template = getWorkflowTemplate(templateName);
353
+ if (template === undefined) {
354
+ return undefined;
355
+ }
356
+
357
+ // If no custom config, return template steps directly
358
+ if (customConfig === undefined || Object.keys(customConfig).length === 0) {
359
+ return template.steps.map(step => ({ ...step }));
360
+ }
361
+
362
+ // Apply custom configuration to steps
363
+ return template.steps.map(step => {
364
+ const newConfig = { ...step.config };
365
+
366
+ // Merge custom config into step config
367
+ for (const [key, value] of Object.entries(customConfig)) {
368
+ if (key in newConfig) {
369
+ newConfig[key] = value;
370
+ }
371
+ }
372
+
373
+ return {
374
+ ...step,
375
+ config: newConfig,
376
+ };
377
+ });
378
+ }
379
+
380
+ /**
381
+ * Validates that a template name is valid
382
+ */
383
+ export function isValidTemplateName(name: string): name is WorkflowTemplateName {
384
+ return name in WORKFLOW_TEMPLATES;
385
+ }
386
+
387
+ /**
388
+ * Gets template description for display
389
+ */
390
+ export function getTemplateDescription(name: string): string | undefined {
391
+ const template = getWorkflowTemplate(name);
392
+ return template?.description;
393
+ }