Files
ai-workflow-skill/packages/inbox-runtime/internal/cli/inbox/reply.go
T

113 lines
3.2 KiB
Go

package inbox
import (
"fmt"
"ai-workflow-skill/packages/coord-core/protocol"
"ai-workflow-skill/packages/coord-core/store"
"github.com/spf13/cobra"
)
type replyOptions struct {
from string
to string
threadID string
kind string
summary string
body string
bodyFile string
payloadJSON string
artifacts artifactOptions
}
func newReplyCmd(root *rootOptions) *cobra.Command {
opts := &replyOptions{}
cmd := &cobra.Command{
Use: "reply",
Short: "Reply inside an existing thread",
Long: helpLong(
"Use reply to append a directed answer, control note, or follow-up message to an existing thread.",
"Leaders usually use reply or orch answer when a worker is blocked.",
"Workers may also use reply for non-terminal coordination that should stay inside the same thread history.",
"reply is for one existing thread; use send when you need to create a new thread.",
),
Example: ` inbox --db .agents/coord.db reply --from leader --to worker-a --thread thr_123 --summary "Use stdout" --body "Use stdout for MVP."
inbox --db .agents/coord.db reply --from leader --to worker-a --thread thr_123 --kind control --summary "Proceed with option A" --payload-json '{"decision":"option_a"}'`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
from := opts.from
if from == "" {
from = root.agent
}
if from == "" {
return protocol.InvalidInput("from agent is required", nil)
}
body, err := resolveBodyValue(opts.body, opts.bodyFile)
if err != nil {
return err
}
artifacts, err := resolveArtifacts(opts.artifacts)
if err != nil {
return err
}
sqlDB, err := openInboxDB(ctx, root.dbPath)
if err != nil {
return err
}
defer sqlDB.Close()
s := store.NewInboxStore(sqlDB)
thread, message, err := s.ReplyToThread(ctx, store.ReplyInput{
ThreadID: opts.threadID,
FromAgent: from,
ToAgent: opts.to,
Kind: opts.kind,
Summary: opts.summary,
Body: body,
PayloadJSON: opts.payloadJSON,
Artifacts: artifacts,
})
if err != nil {
return err
}
resp := protocol.Success{
OK: true,
Command: "reply",
Data: map[string]any{
"thread": thread,
"message": message,
},
}
if root.json {
return protocol.WriteJSON(cmd.OutOrStdout(), resp)
}
_, err = fmt.Fprintf(cmd.OutOrStdout(), "replied on thread %s\n", thread.ThreadID)
return err
},
}
cmd.Flags().StringVar(&opts.from, "from", "", "Replying agent")
cmd.Flags().StringVar(&opts.to, "to", "", "Receiving agent")
cmd.Flags().StringVar(&opts.threadID, "thread", "", "Thread ID")
cmd.Flags().StringVar(&opts.kind, "kind", "answer", "Reply kind")
cmd.Flags().StringVar(&opts.summary, "summary", "", "Short reply summary")
cmd.Flags().StringVar(&opts.body, "body", "", "Reply body")
cmd.Flags().StringVar(&opts.bodyFile, "body-file", "", "Read reply body from file")
cmd.Flags().StringVar(&opts.payloadJSON, "payload-json", "", "Structured payload JSON string")
addArtifactFlags(cmd, &opts.artifacts)
_ = cmd.MarkFlagRequired("thread")
_ = cmd.MarkFlagRequired("to")
_ = cmd.MarkFlagRequired("summary")
return cmd
}