|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/pmarsceill/mapcli/internal/client" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +var taskInputNeededCmd = &cobra.Command{ |
| 15 | + Use: "input-needed <task-id> <question>", |
| 16 | + Short: "Request user input for a task via GitHub issue", |
| 17 | + Long: `Signal that an agent needs user input by posting a comment to the originating GitHub issue. |
| 18 | +
|
| 19 | +This command: |
| 20 | +1. Posts a comment to the GitHub issue with the question |
| 21 | +2. Sets the task status to WAITING_INPUT |
| 22 | +3. The daemon will poll for responses and deliver them to the agent |
| 23 | +
|
| 24 | +The task must have originated from a GitHub issue (via 'map task sync gh-project').`, |
| 25 | + Args: cobra.MinimumNArgs(2), |
| 26 | + RunE: runTaskInputNeeded, |
| 27 | +} |
| 28 | + |
| 29 | +var taskMyTaskCmd = &cobra.Command{ |
| 30 | + Use: "my-task", |
| 31 | + Short: "Show the current task for this agent", |
| 32 | + Long: `Look up the current task by matching the working directory to an agent worktree. |
| 33 | +
|
| 34 | +This is useful for agents to introspect their assigned task.`, |
| 35 | + RunE: runTaskMyTask, |
| 36 | +} |
| 37 | + |
| 38 | +func init() { |
| 39 | + taskCmd.AddCommand(taskInputNeededCmd) |
| 40 | + taskCmd.AddCommand(taskMyTaskCmd) |
| 41 | +} |
| 42 | + |
| 43 | +func runTaskInputNeeded(cmd *cobra.Command, args []string) error { |
| 44 | + taskID := args[0] |
| 45 | + question := strings.Join(args[1:], " ") |
| 46 | + |
| 47 | + c, err := client.New(getSocketPath()) |
| 48 | + if err != nil { |
| 49 | + return fmt.Errorf("connect to daemon: %w", err) |
| 50 | + } |
| 51 | + defer func() { _ = c.Close() }() |
| 52 | + |
| 53 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 54 | + defer cancel() |
| 55 | + |
| 56 | + resp, err := c.RequestInput(ctx, taskID, question) |
| 57 | + if err != nil { |
| 58 | + return fmt.Errorf("request input: %w", err) |
| 59 | + } |
| 60 | + |
| 61 | + if !resp.Success { |
| 62 | + return fmt.Errorf("%s", resp.Message) |
| 63 | + } |
| 64 | + |
| 65 | + fmt.Println(resp.Message) |
| 66 | + return nil |
| 67 | +} |
| 68 | + |
| 69 | +func runTaskMyTask(cmd *cobra.Command, args []string) error { |
| 70 | + cwd, err := os.Getwd() |
| 71 | + if err != nil { |
| 72 | + return fmt.Errorf("get working directory: %w", err) |
| 73 | + } |
| 74 | + |
| 75 | + c, err := client.New(getSocketPath()) |
| 76 | + if err != nil { |
| 77 | + return fmt.Errorf("connect to daemon: %w", err) |
| 78 | + } |
| 79 | + defer func() { _ = c.Close() }() |
| 80 | + |
| 81 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 82 | + defer cancel() |
| 83 | + |
| 84 | + task, err := c.GetCurrentTask(ctx, cwd) |
| 85 | + if err != nil { |
| 86 | + return fmt.Errorf("get current task: %w", err) |
| 87 | + } |
| 88 | + |
| 89 | + if task == nil { |
| 90 | + fmt.Println("no task found for this working directory") |
| 91 | + return nil |
| 92 | + } |
| 93 | + |
| 94 | + fmt.Printf("Task ID: %s\n", task.TaskId) |
| 95 | + fmt.Printf("Status: %s\n", taskStatusString(task.Status)) |
| 96 | + fmt.Printf("Description: %s\n", task.Description) |
| 97 | + fmt.Printf("Assigned To: %s\n", valueOrDash(task.AssignedTo)) |
| 98 | + |
| 99 | + if task.GithubSource != nil { |
| 100 | + fmt.Printf("GitHub: %s/%s#%d\n", |
| 101 | + task.GithubSource.Owner, |
| 102 | + task.GithubSource.Repo, |
| 103 | + task.GithubSource.IssueNumber) |
| 104 | + } |
| 105 | + |
| 106 | + if task.WaitingInputQuestion != "" { |
| 107 | + fmt.Printf("\nWaiting for input:\n%s\n", task.WaitingInputQuestion) |
| 108 | + } |
| 109 | + |
| 110 | + return nil |
| 111 | +} |
0 commit comments