MCPcopy Create free account
hub / github.com/astercloud/aster / executionExample

Function executionExample

examples/execution-plan/main.go:59–124  ·  view source on GitHub ↗

executionExample demonstrates plan execution with approval

()

Source from the content-addressed store, hash-verified

57
58// executionExample demonstrates plan execution with approval
59func 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)

Callers 1

mainFunction · 0.85

Calls 10

AddStepMethod · 0.95
CanExecuteMethod · 0.95
IsApprovedMethod · 0.95
ApproveMethod · 0.95
ExecuteMethod · 0.95
NewExecutionPlanFunction · 0.92
NewExecutorFunction · 0.92
WithOnStepStartFunction · 0.92
WithOnStepCompleteFunction · 0.92
PrintfMethod · 0.80

Tested by

no test coverage detected