-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathworkflow.go
663 lines (569 loc) · 18.7 KB
/
workflow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
package swarmgo
import (
"context"
"crypto/sha256"
"errors"
"fmt"
"regexp"
"strings"
"time"
"github.com/prathyushnallamothu/swarmgo/llm"
)
// WorkflowType defines the type of agent interaction pattern
type WorkflowType int
const (
CollaborativeWorkflow WorkflowType = iota
SupervisorWorkflow
HierarchicalWorkflow
)
// TeamType represents a type of agent team
type TeamType string
const (
ResearchTeam TeamType = "research"
DocumentTeam TeamType = "document"
SupervisorTeam TeamType = "supervisor"
AnalysisTeam TeamType = "analysis"
DeveloperTeam TeamType = "developer"
)
// CycleHandling represents how to handle detected cycles
type CycleHandling int
const (
StopOnCycle CycleHandling = iota
ContinueOnCycle
)
// Workflow represents a collection of agents and their connections.
type Workflow struct {
swarm *Swarm
agents map[string]*Agent
connections map[string][]string
workflowType WorkflowType
sharedState map[string]interface{}
agentStates map[string]map[string]interface{}
teams map[TeamType][]*Agent // Agents grouped by team
teamLeaders map[TeamType]string // Team leader for each team
currentAgent string // Track current active agent
routingLog []string // Log of agent transitions
cycleHandling CycleHandling
cycleCallback func(from, to string) (bool, error) // Callback for cycle detection
stepResults []StepResult // Track results of each step
currentStep int // Current step number
}
// NewWorkflow initializes a new Workflow instance.
func NewWorkflow(apikey string, provider llm.LLMProvider, workflowType WorkflowType) *Workflow {
swarm := NewSwarm(apikey, provider)
return &Workflow{
swarm: swarm,
agents: make(map[string]*Agent),
connections: make(map[string][]string),
workflowType: workflowType,
sharedState: make(map[string]interface{}),
agentStates: make(map[string]map[string]interface{}),
teams: make(map[TeamType][]*Agent),
teamLeaders: make(map[TeamType]string),
routingLog: make([]string, 0),
cycleHandling: StopOnCycle,
}
}
// SetCycleCallback sets a callback function to be called when a cycle is detected
func (wf *Workflow) SetCycleCallback(callback func(from, to string) (bool, error)) {
wf.cycleCallback = callback
}
// SetCycleHandling sets how cycles should be handled
func (wf *Workflow) SetCycleHandling(handling CycleHandling) {
wf.cycleHandling = handling
}
// logTransition logs agent transitions for debugging
func (wf *Workflow) logTransition(from, to string, reason string) {
log := fmt.Sprintf("Transition: %s -> %s (%s)", from, to, reason)
wf.routingLog = append(wf.routingLog, log)
fmt.Printf("\033[93m%s\033[0m\n", log)
}
// GetCurrentAgent returns the currently active agent
func (wf *Workflow) GetCurrentAgent() string {
return wf.currentAgent
}
// GetRoutingLog returns the routing history
func (wf *Workflow) GetRoutingLog() []string {
return wf.routingLog
}
// GetAgents returns all agents in the workflow
func (wf *Workflow) GetAgents() map[string]*Agent {
return wf.agents
}
// GetConnections returns all connections in the workflow
func (wf *Workflow) GetConnections() map[string][]string {
return wf.connections
}
// GetTeams returns all teams in the workflow
func (wf *Workflow) GetTeams() map[TeamType][]*Agent {
return wf.teams
}
// GetTeamLeaders returns all team leaders in the workflow
func (wf *Workflow) GetTeamLeaders() map[TeamType]string {
return wf.teamLeaders
}
// AddAgent adds an agent to the workflow.
func (wf *Workflow) AddAgent(agent *Agent) {
wf.agents[agent.Name] = agent
wf.agentStates[agent.Name] = make(map[string]interface{})
}
// AddAgentToTeam adds an agent to a specific team
func (wf *Workflow) AddAgentToTeam(agent *Agent, team TeamType) {
wf.agents[agent.Name] = agent
wf.agentStates[agent.Name] = make(map[string]interface{})
wf.teams[team] = append(wf.teams[team], agent)
}
// SetTeamLeader designates an agent as the leader of a team
func (wf *Workflow) SetTeamLeader(agentName string, team TeamType) error {
if _, exists := wf.agents[agentName]; !exists {
return errors.New("agent does not exist")
}
wf.teamLeaders[team] = agentName
return nil
}
// Execute runs the workflow and returns detailed results including step outcomes
func (wf *Workflow) Execute(startAgent string, userRequest string) (*WorkflowResult, error) {
result := &WorkflowResult{
Steps: make([]StepResult, 0),
StartTime: time.Now(),
}
if _, exists := wf.agents[startAgent]; !exists {
return result, errors.New("startAgent does not exist")
}
messageHistory := []llm.Message{{Role: llm.RoleUser, Content: userRequest}}
visited := make(map[string]bool)
cycleCount := make(map[string]int)
wf.currentAgent = startAgent
wf.currentStep = 0
wf.logTransition("start", startAgent, "workflow initialization")
for {
// Start new step
stepResult := StepResult{
AgentName: wf.currentAgent,
Input: messageHistory,
StartTime: time.Now(),
StepNumber: wf.currentStep + 1,
}
// Execute current agent
fmt.Printf("\033[96mExecuting agent: %s (Step %d)\033[0m\n", wf.currentAgent, stepResult.StepNumber)
response, err := wf.executeAgent(wf.currentAgent, messageHistory)
stepResult.EndTime = time.Now()
if err != nil {
stepResult.Error = err
result.Steps = append(result.Steps, stepResult)
result.Error = err
result.EndTime = time.Now()
return result, err
}
stepResult.Output = response
messageHistory = append(messageHistory, response...)
// Determine next agent
nextAgent, shouldContinue := wf.routeToNextAgent(wf.currentAgent, messageHistory)
stepResult.NextAgent = nextAgent
// Store step result
wf.stepResults = append(wf.stepResults, stepResult)
result.Steps = append(result.Steps, stepResult)
wf.currentStep++
if !shouldContinue {
wf.logTransition(wf.currentAgent, "end", "workflow complete")
break
}
// Check for cycles
if visited[nextAgent] {
cycleCount[nextAgent]++
reason := fmt.Sprintf("cycle detected (%d times)", cycleCount[nextAgent])
wf.logTransition(wf.currentAgent, nextAgent, reason)
switch wf.cycleHandling {
case StopOnCycle:
break
case ContinueOnCycle:
if wf.cycleCallback != nil {
shouldContinue, err := wf.cycleCallback(wf.currentAgent, nextAgent)
if err != nil {
result.Error = fmt.Errorf("cycle callback error: %v", err)
result.EndTime = time.Now()
return result, result.Error
}
if !shouldContinue {
result.EndTime = time.Now()
result.FinalOutput = messageHistory
return result, nil
}
}
// Continue with the cycle
wf.currentAgent = nextAgent
continue
}
break
}
// Log transition and update current agent
wf.logTransition(wf.currentAgent, nextAgent, "normal routing")
wf.currentAgent = nextAgent
visited[nextAgent] = true
}
result.EndTime = time.Now()
result.FinalOutput = messageHistory
return result, nil
}
// GetStepResult returns the result of a specific step
func (wf *Workflow) GetStepResult(stepNumber int) (*StepResult, error) {
if stepNumber < 1 || stepNumber > len(wf.stepResults) {
return nil, fmt.Errorf("invalid step number: %d", stepNumber)
}
return &wf.stepResults[stepNumber-1], nil
}
// GetAllStepResults returns all step results
func (wf *Workflow) GetAllStepResults() []StepResult {
return wf.stepResults
}
// GetLastStepResult returns the result of the last executed step
func (wf *Workflow) GetLastStepResult() (*StepResult, error) {
if len(wf.stepResults) == 0 {
return nil, errors.New("no steps executed yet")
}
return &wf.stepResults[len(wf.stepResults)-1], nil
}
// executeAgent executes a single agent and manages its state
func (wf *Workflow) executeAgent(agentName string, messageHistory []llm.Message) ([]llm.Message, error) {
agent := wf.agents[agentName]
fmt.Printf("\033[95mAgent %s processing message...\033[0m\n", agentName)
// Prepare agent state
var state map[string]interface{}
if wf.workflowType == CollaborativeWorkflow {
state = wf.sharedState
} else {
state = wf.agentStates[agentName]
}
// Execute agent
response, err := wf.swarm.Run(
context.Background(),
agent,
messageHistory,
state,
"",
false,
false,
0,
true,
)
if err != nil {
fmt.Printf("\033[91mError executing agent %s: %v\033[0m\n", agentName, err)
return nil, err
}
fmt.Printf("\033[92mAgent %s completed processing\033[0m\n", agentName)
// Update state
if wf.workflowType == CollaborativeWorkflow {
wf.sharedState = state
} else {
wf.agentStates[agentName] = state
}
return response.Messages, nil
}
// routeToNextAgent determines the next agent based on workflow type and message content
func (wf *Workflow) routeToNextAgent(currentAgent string, messageHistory []llm.Message) (string, bool) {
lastMessage := messageHistory[len(messageHistory)-1]
//state := wf.agentStates[currentAgent]
// Check for explicit routing instructions
if containsRoutingInstruction(lastMessage.Content) {
nextAgent := extractRoutingAgent(lastMessage.Content)
if _, exists := wf.agents[nextAgent]; exists {
return nextAgent, true
}
}
// Handle different workflow types
switch wf.workflowType {
case SupervisorWorkflow:
return wf.handleSupervisorRouting(currentAgent, messageHistory)
case HierarchicalWorkflow:
return wf.handleHierarchicalRouting(currentAgent, messageHistory)
case CollaborativeWorkflow:
return wf.handleCollaborativeRouting(currentAgent, messageHistory)
}
return "", false
}
func (wf *Workflow) handleSupervisorRouting(currentAgent string, messageHistory []llm.Message) (string, bool) {
lastMessage := messageHistory[len(messageHistory)-1]
content := strings.ToLower(lastMessage.Content)
// Get supervisor name from team leaders
supervisorName := wf.teamLeaders[SupervisorTeam]
if currentAgent == supervisorName {
// Task classification patterns
taskTeams := map[string]TeamType{
`(?i)(research|search|find|look up|scrape|collect)`: ResearchTeam,
`(?i)(write|draft|compose|create|document|chart|generate)`: DocumentTeam,
`(?i)(analyze|evaluate|assess|interpret|review|investigate)`: AnalysisTeam,
`(?i)(code|develop|implement|program|debug|test|build)`: DeveloperTeam,
}
// Determine appropriate team based on task
for pattern, team := range taskTeams {
re := regexp.MustCompile(pattern)
if re.MatchString(content) {
if leader, exists := wf.teamLeaders[team]; exists {
return leader, true
}
// If no leader, try to find any team member
if agents, exists := wf.teams[team]; exists && len(agents) > 0 {
return agents[0].Name, true
}
}
}
} else {
// Non-supervisor agents report back to supervisor
return supervisorName, true
}
return "", false
}
func (wf *Workflow) handleHierarchicalRouting(currentAgent string, messageHistory []llm.Message) (string, bool) {
lastMessage := messageHistory[len(messageHistory)-1]
// If task is complete, route back to team leader or supervisor
if isTaskComplete(lastMessage.Content) {
// Find which team the current agent belongs to
for team, agents := range wf.teams {
for _, agent := range agents {
if agent.Name == currentAgent {
// Route to team leader if exists
if leader, exists := wf.teamLeaders[team]; exists {
return leader, true
}
// Otherwise route to supervisor
return "supervisor", true
}
}
}
}
// Check for specific tool/function calls
if strings.Contains(lastMessage.Content, "function") || strings.Contains(lastMessage.Content, "tool") {
// Route to appropriate specialized agent based on function type
functionPatterns := map[string][]string{
`(?i)(search|api)`: {"searcher", "web_scraper"},
`(?i)(write|text)`: {"writer", "note_taker"},
`(?i)(chart|graph|plot)`: {"chart_generator"},
`(?i)(analyze|evaluate|assess)`: {"analyzer", "evaluator"},
`(?i)(interpret|review|investigate)`: {"reviewer", "investigator"},
`(?i)(code|program|implement)`: {"developer", "programmer"},
`(?i)(test|debug|fix)`: {"tester", "debugger"},
`(?i)(build|deploy|release)`: {"builder", "deployer"},
`(?i)(optimize|refactor|improve)`: {"optimizer", "refactorer"},
}
for pattern, agents := range functionPatterns {
re := regexp.MustCompile(pattern)
if re.MatchString(lastMessage.Content) {
for _, agentName := range agents {
if _, exists := wf.agents[agentName]; exists {
return agentName, true
}
}
}
}
}
return "", false
}
func (wf *Workflow) handleCollaborativeRouting(currentAgent string, messageHistory []llm.Message) (string, bool) {
// In collaborative mode, agents share context and can work together
lastMessage := messageHistory[len(messageHistory)-1]
// Check if any connected agent hasn't processed this message
for _, nextAgent := range wf.connections[currentAgent] {
if !hasProcessedMessage(wf.agentStates[nextAgent], messageHistory) {
return nextAgent, true
}
}
// If all connected agents have processed, check if we need more processing
if !isFinalAnswer(lastMessage.Content) {
// Find an agent that might have relevant capabilities
for name, _ := range wf.agents {
if name != currentAgent && !hasProcessedMessage(wf.agentStates[name], messageHistory) {
return name, true
}
}
}
return "", false
}
// Helper functions for message analysis
func containsRoutingInstruction(content string) bool {
// Check for routing keywords like "route to", "send to", "forward to"
routingKeywords := []string{
"route to",
"send to",
"forward to",
"delegate to",
"assign to",
"@",
}
content = strings.ToLower(content)
for _, keyword := range routingKeywords {
if strings.Contains(content, keyword) {
return true
}
}
return false
}
func extractRoutingAgent(content string) string {
// Extract agent name after routing keywords
routingPatterns := []string{
`(?i)route to (\w+)`,
`(?i)send to (\w+)`,
`(?i)forward to (\w+)`,
`(?i)delegate to (\w+)`,
`(?i)assign to (\w+)`,
`@(\w+)`,
}
content = strings.TrimSpace(content)
for _, pattern := range routingPatterns {
re := regexp.MustCompile(pattern)
if matches := re.FindStringSubmatch(content); len(matches) > 1 {
return matches[1]
}
}
return ""
}
func isTaskComplete(content string) bool {
// Check for task completion indicators
completionKeywords := []string{
"task complete",
"completed",
"finished",
"done",
"task accomplished",
"objective achieved",
"✓",
"✔",
}
content = strings.ToLower(content)
for _, keyword := range completionKeywords {
if strings.Contains(content, keyword) {
return true
}
}
return false
}
func isFinalAnswer(content string) bool {
// Check for final answer indicators
finalKeywords := []string{
"final answer",
"final response",
"final solution",
"final result",
"end workflow",
"complete workflow",
"FINAL:",
}
content = strings.ToLower(content)
for _, keyword := range finalKeywords {
if strings.Contains(content, keyword) {
return true
}
}
return false
}
func hasProcessedMessage(state map[string]interface{}, history []llm.Message) bool {
if state == nil {
return false
}
// Get processed message IDs from state
processedIDs, ok := state["processed_message_ids"].([]string)
if !ok {
processedIDs = []string{}
}
// Generate ID for the last message
lastMsg := history[len(history)-1]
msgID := generateMessageID(lastMsg)
// Check if message has been processed
for _, id := range processedIDs {
if id == msgID {
return true
}
}
// Mark message as processed
state["processed_message_ids"] = append(processedIDs, msgID)
return false
}
// generateMessageID creates a unique ID for a message based on its content and role
func generateMessageID(msg llm.Message) string {
data := fmt.Sprintf("%s:%s", msg.Role, msg.Content)
hash := sha256.Sum256([]byte(data))
return fmt.Sprintf("%x", hash[:8]) // Use first 8 bytes for shorter ID
}
func (wf *Workflow) findParentAgent(agentName string) string {
// Check all agents' connections to find the parent
for agent, connections := range wf.connections {
for _, conn := range connections {
if conn == agentName {
return agent // This agent is the parent
}
}
}
return ""
}
func (wf *Workflow) routeSupervisorToWorker(messageHistory []llm.Message) (string, bool) {
lastMessage := messageHistory[len(messageHistory)-1]
content := strings.ToLower(lastMessage.Content)
// Define task-agent mappings
taskPatterns := map[string][]string{
`(?i)(research|search|find|look up)`: {"researcher", "analyst"},
`(?i)(write|draft|compose|create)`: {"writer", "creator"},
`(?i)(review|check|validate|verify)`: {"reviewer", "validator"},
`(?i)(analyze|evaluate|assess|interpret)`: {"analyzer", "evaluator"},
`(?i)(investigate|examine|study|explore)`: {"investigator", "researcher"},
`(?i)(calculate|compute|analyze)`: {"calculator", "analyst"},
`(?i)(summarize|summarise|recap)`: {"summarizer", "writer"},
`(?i)(code|develop|program|implement)`: {"developer", "programmer"},
`(?i)(test|debug|fix|resolve)`: {"tester", "debugger"},
`(?i)(build|deploy|release|package)`: {"builder", "deployer"},
`(?i)(optimize|refactor|improve)`: {"optimizer", "refactorer"},
}
// Check each pattern and return appropriate agent
for pattern, agents := range taskPatterns {
re := regexp.MustCompile(pattern)
if re.MatchString(content) {
// Find first available agent
for _, agent := range agents {
if _, exists := wf.agents[agent]; exists {
return agent, true
}
}
}
}
// If no specific task pattern matched, try to extract agent name
if agent := extractRoutingAgent(content); agent != "" {
if _, exists := wf.agents[agent]; exists {
return agent, true
}
}
// Default to first available worker if no specific routing found
for name := range wf.agents {
if name != "supervisor" {
return name, true
}
}
return "", false
}
// ConnectAgents creates a connection between two agents.
func (wf *Workflow) ConnectAgents(fromAgent, toAgent string) error {
if _, exists := wf.agents[fromAgent]; !exists {
return errors.New("fromAgent does not exist")
}
if _, exists := wf.agents[toAgent]; !exists {
return errors.New("toAgent does not exist")
}
wf.connections[fromAgent] = append(wf.connections[fromAgent], toAgent)
return nil
}
// StepResult represents the outcome of a single workflow step
type StepResult struct {
AgentName string
Input []llm.Message
Output []llm.Message
Error error
StartTime time.Time
EndTime time.Time
NextAgent string
StepNumber int
}
// WorkflowResult represents the complete workflow execution result
type WorkflowResult struct {
Steps []StepResult
FinalOutput []llm.Message
Error error
StartTime time.Time
EndTime time.Time
}