package inbox import ( "fmt" "ai-workflow-skill/internal/db" "ai-workflow-skill/internal/protocol" "ai-workflow-skill/internal/store" "github.com/spf13/cobra" ) type completeOptions struct { agent string threadID string summary string body string payloadJSON string } func newDoneCmd(root *rootOptions) *cobra.Command { return newCompleteCmd(root, "done") } func newFailCmd(root *rootOptions) *cobra.Command { return newCompleteCmd(root, "fail") } func newCompleteCmd(root *rootOptions, mode string) *cobra.Command { opts := &completeOptions{} cmd := &cobra.Command{ Use: mode, Short: map[string]string{"done": "Mark a thread complete", "fail": "Mark a thread failed"}[mode], RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() agent := opts.agent if agent == "" { agent = root.agent } if agent == "" { return fmt.Errorf("agent is required") } sqlDB, err := db.Open(ctx, root.dbPath) if err != nil { return err } defer sqlDB.Close() s := store.NewInboxStore(sqlDB) thread, message, err := s.CompleteThread(ctx, store.CompleteInput{ ThreadID: opts.threadID, Agent: agent, Summary: opts.summary, Body: opts.body, PayloadJSON: opts.payloadJSON, Failed: mode == "fail", }) if err != nil { return err } resp := protocol.Success{ OK: true, Command: mode, Data: map[string]any{ "thread": thread, "message": message, }, } if root.json { return protocol.WriteJSON(cmd.OutOrStdout(), resp) } _, err = fmt.Fprintf(cmd.OutOrStdout(), "%s thread %s\n", mode, thread.ThreadID) return err }, } cmd.Flags().StringVar(&opts.agent, "agent", "", "Acting agent") cmd.Flags().StringVar(&opts.threadID, "thread", "", "Thread ID") cmd.Flags().StringVar(&opts.summary, "summary", "", "Short completion summary") cmd.Flags().StringVar(&opts.body, "body", "", "Completion body") cmd.Flags().StringVar(&opts.payloadJSON, "payload-json", "", "Structured payload JSON string") _ = cmd.MarkFlagRequired("thread") _ = cmd.MarkFlagRequired("summary") return cmd }