executionExample demonstrates plan execution with approval
()
| 57 | |
| 58 | // executionExample demonstrates plan execution with approval |
| 59 | func executionExample() { |
| 60 | // Create a plan that requires approval |
| 61 | plan := executionplan.NewExecutionPlan("File operations demo") |
| 62 | plan.Options = &executionplan.ExecutionOptions{ |
| 63 | RequireApproval: true, |
| 64 | StopOnError: true, |
| 65 | } |
| 66 | |
| 67 | // Add steps |
| 68 | plan.AddStep("list_files", "List files in current directory", map[string]any{ |
| 69 | "path": ".", |
| 70 | }) |
| 71 | |
| 72 | plan.AddStep("create_file", "Create a test file", map[string]any{ |
| 73 | "path": "test.txt", |
| 74 | "content": "Hello, World!", |
| 75 | }) |
| 76 | |
| 77 | // Print initial state |
| 78 | fmt.Printf("Plan Status: %s\n", plan.Status) |
| 79 | fmt.Printf("Can Execute: %v\n", plan.CanExecute()) |
| 80 | fmt.Printf("Is Approved: %v\n", plan.IsApproved()) |
| 81 | |
| 82 | // Simulate approval |
| 83 | plan.Approve("user@example.com") |
| 84 | fmt.Printf("\nAfter Approval:\n") |
| 85 | fmt.Printf("Plan Status: %s\n", plan.Status) |
| 86 | fmt.Printf("Can Execute: %v\n", plan.CanExecute()) |
| 87 | fmt.Printf("Approved By: %s\n", plan.ApprovedBy) |
| 88 | |
| 89 | // Create mock tools for execution demo |
| 90 | mockTools := map[string]tools.Tool{ |
| 91 | "list_files": &MockTool{name: "list_files", result: []string{"file1.go", "file2.go"}}, |
| 92 | "create_file": &MockTool{name: "create_file", result: "created"}, |
| 93 | } |
| 94 | |
| 95 | // Create executor |
| 96 | executor := executionplan.NewExecutor( |
| 97 | mockTools, |
| 98 | executionplan.WithOnStepStart(func(p *executionplan.ExecutionPlan, s *executionplan.Step) { |
| 99 | fmt.Printf(" Starting step %d: %s\n", s.Index+1, s.Description) |
| 100 | }), |
| 101 | executionplan.WithOnStepComplete(func(p *executionplan.ExecutionPlan, s *executionplan.Step) { |
| 102 | fmt.Printf(" Completed step %d: %s (took %dms)\n", s.Index+1, s.Description, s.DurationMs) |
| 103 | }), |
| 104 | ) |
| 105 | |
| 106 | // Execute the plan |
| 107 | fmt.Println("\nExecuting plan...") |
| 108 | ctx := context.Background() |
| 109 | toolCtx := &tools.ToolContext{AgentID: "example-agent"} |
| 110 | |
| 111 | if err := executor.Execute(ctx, plan, toolCtx); err != nil { |
| 112 | fmt.Printf("Execution failed: %v\n", err) |
| 113 | } else { |
| 114 | fmt.Printf("\nExecution completed!\n") |
| 115 | fmt.Printf("Final Status: %s\n", plan.Status) |
| 116 | fmt.Printf("Total Duration: %dms\n", plan.TotalDurationMs) |
no test coverage detected