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

123 lines
3.6 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 completeOptions struct {
agent string
threadID string
summary string
body string
bodyFile string
payloadJSON string
artifacts artifactOptions
}
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],
Long: map[string]string{
"done": helpLong(
"Use done to close a claimed thread successfully and record the final result summary.",
"done is a terminal operation; use it only when the work is complete enough to hand back to the leader.",
"Include a short summary and optionally a detailed body or artifacts for the final result.",
),
"fail": helpLong(
"Use fail to close a claimed thread unsuccessfully and record the failure summary.",
"fail is a terminal operation; use it when the attempt cannot complete successfully.",
"Include a short summary and enough detail for the leader to decide on retry, reassignment, or cancellation.",
),
}[mode],
Example: map[string]string{
"done": ` inbox --db .agents/coord.db done --agent worker-a --thread thr_123 --summary "Implemented retry policy" --body "Retries now cover read timeouts."`,
"fail": ` inbox --db .agents/coord.db fail --agent worker-a --thread thr_123 --summary "Blocked by migration issue" --body "Previous schema state is inconsistent."`,
}[mode],
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
agent := opts.agent
if agent == "" {
agent = root.agent
}
if agent == "" {
return protocol.InvalidInput("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.CompleteThread(ctx, store.CompleteInput{
ThreadID: opts.threadID,
Agent: agent,
Summary: opts.summary,
Body: body,
PayloadJSON: opts.payloadJSON,
Failed: mode == "fail",
Artifacts: artifacts,
})
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.bodyFile, "body-file", "", "Read completion body from file")
cmd.Flags().StringVar(&opts.payloadJSON, "payload-json", "", "Structured payload JSON string")
addArtifactFlags(cmd, &opts.artifacts)
_ = cmd.MarkFlagRequired("thread")
_ = cmd.MarkFlagRequired("summary")
return cmd
}