| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 1 | package commands |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "os/signal" |
| 8 | "syscall" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/spf13/cobra" |
| 12 | ) |
| 13 | |
| 14 | var startAgentCmd = &cobra.Command{ |
| 15 | Use: "start-agent [agent-name]", |
| 16 | Short: "Start an agent to process tasks", |
| 17 | Long: `Start a specific agent to continuously process assigned tasks. |
| 18 | |
| 19 | The agent will: |
| 20 | 1. Check for assigned tasks every 30 seconds |
| 21 | 2. Process tasks using the configured LLM |
| 22 | 3. Create GitHub PRs for solutions |
| 23 | 4. Mark tasks as completed |
| 24 | |
| 25 | Examples: |
| 26 | staff start-agent backend-engineer |
| 27 | staff start-agent frontend-engineer`, |
| 28 | Args: cobra.ExactArgs(1), |
| 29 | RunE: runStartAgent, |
| 30 | } |
| 31 | |
| 32 | var ( |
| 33 | agentInterval time.Duration = 30 * time.Second |
| 34 | ) |
| 35 | |
| 36 | func init() { |
| 37 | startAgentCmd.Flags().DurationVar(&agentInterval, "interval", 30*time.Second, "Task check interval") |
| 38 | } |
| 39 | |
| 40 | func runStartAgent(cmd *cobra.Command, args []string) error { |
| 41 | agentName := args[0] |
| 42 | |
| 43 | // Check if agent exists in configuration |
| 44 | var agentExists bool |
| 45 | for _, agent := range cfg.Agents { |
| 46 | if agent.Name == agentName { |
| 47 | agentExists = true |
| 48 | break |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | if !agentExists { |
| 53 | return fmt.Errorf("agent '%s' not found in configuration", agentName) |
| 54 | } |
| 55 | |
| 56 | fmt.Printf("Starting agent: %s\n", agentName) |
| 57 | fmt.Printf("Task check interval: %v\n", agentInterval) |
| 58 | fmt.Printf("Press Ctrl+C to stop the agent\n\n") |
| 59 | |
| 60 | // Set up signal handling for graceful shutdown |
| 61 | ctx, cancel := context.WithCancel(context.Background()) |
| 62 | defer cancel() |
| 63 | |
| 64 | sigChan := make(chan os.Signal, 1) |
| 65 | signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) |
| 66 | |
| 67 | go func() { |
| 68 | <-sigChan |
| 69 | fmt.Printf("\nReceived shutdown signal, stopping agent...\n") |
| 70 | cancel() |
| 71 | }() |
| 72 | |
| 73 | // Start the agent |
| 74 | err := agentManager.StartAgent(agentName, agentInterval) |
| 75 | if err != nil { |
| 76 | return fmt.Errorf("failed to start agent: %w", err) |
| 77 | } |
| 78 | |
| 79 | // Wait for context cancellation (Ctrl+C) |
| 80 | <-ctx.Done() |
| 81 | |
| 82 | fmt.Printf("Agent %s stopped\n", agentName) |
| 83 | return nil |
| iomodo | 50598c6 | 2025-07-27 22:06:32 +0400 | [diff] [blame] | 84 | } |