Complete inbox CLI implementation

This commit is contained in:
2026-03-19 03:15:17 +08:00
parent 11bee52ff4
commit c3314cd9cf
15 changed files with 1524 additions and 43 deletions
+21
View File
@@ -0,0 +1,21 @@
package inbox
import (
"fmt"
"os"
)
func resolveBodyValue(body, bodyFile string) (string, error) {
if body != "" && bodyFile != "" {
return "", fmt.Errorf("body and body-file are mutually exclusive")
}
if bodyFile == "" {
return body, nil
}
content, err := os.ReadFile(bodyFile)
if err != nil {
return "", fmt.Errorf("read body file %q: %w", bodyFile, err)
}
return string(content), nil
}
+77
View File
@@ -0,0 +1,77 @@
package inbox
import (
"fmt"
"ai-workflow-skill/internal/db"
"ai-workflow-skill/internal/protocol"
"ai-workflow-skill/internal/store"
"github.com/spf13/cobra"
)
type cancelOptions struct {
agent string
threadID string
reason string
}
func newCancelCmd(root *rootOptions) *cobra.Command {
opts := &cancelOptions{}
cmd := &cobra.Command{
Use: "cancel",
Short: "Cancel a thread",
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.CancelThread(ctx, store.CancelInput{
ThreadID: opts.threadID,
Agent: agent,
Reason: opts.reason,
})
if err != nil {
return err
}
resp := protocol.Success{
OK: true,
Command: "cancel",
Data: map[string]any{
"thread": thread,
"message": message,
},
}
if root.json {
return protocol.WriteJSON(cmd.OutOrStdout(), resp)
}
_, err = fmt.Fprintf(cmd.OutOrStdout(), "cancelled thread %s\n", thread.ThreadID)
return err
},
}
cmd.Flags().StringVar(&opts.agent, "agent", "", "Acting agent")
cmd.Flags().StringVar(&opts.threadID, "thread", "", "Thread ID")
cmd.Flags().StringVar(&opts.reason, "reason", "", "Cancellation reason")
_ = cmd.MarkFlagRequired("thread")
return cmd
}
+8 -1
View File
@@ -15,6 +15,7 @@ type completeOptions struct {
threadID string
summary string
body string
bodyFile string
payloadJSON string
}
@@ -43,6 +44,11 @@ func newCompleteCmd(root *rootOptions, mode string) *cobra.Command {
return fmt.Errorf("agent is required")
}
body, err := resolveBodyValue(opts.body, opts.bodyFile)
if err != nil {
return err
}
sqlDB, err := db.Open(ctx, root.dbPath)
if err != nil {
return err
@@ -54,7 +60,7 @@ func newCompleteCmd(root *rootOptions, mode string) *cobra.Command {
ThreadID: opts.threadID,
Agent: agent,
Summary: opts.summary,
Body: opts.body,
Body: body,
PayloadJSON: opts.payloadJSON,
Failed: mode == "fail",
})
@@ -84,6 +90,7 @@ func newCompleteCmd(root *rootOptions, mode string) *cobra.Command {
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")
_ = cmd.MarkFlagRequired("thread")
+3
View File
@@ -15,6 +15,7 @@ type fetchOptions struct {
agent string
statuses string
limit int
unread bool
}
func newFetchCmd(root *rootOptions) *cobra.Command {
@@ -42,6 +43,7 @@ func newFetchCmd(root *rootOptions) *cobra.Command {
Agent: agent,
Statuses: parseCSV(opts.statuses),
Limit: opts.limit,
Unread: opts.unread,
})
if err != nil {
return err
@@ -71,6 +73,7 @@ func newFetchCmd(root *rootOptions) *cobra.Command {
cmd.Flags().StringVar(&opts.agent, "agent", "", "Assigned agent filter")
cmd.Flags().StringVar(&opts.statuses, "status", "pending", "Comma-separated status filter")
cmd.Flags().IntVar(&opts.limit, "limit", 20, "Maximum number of threads")
cmd.Flags().BoolVar(&opts.unread, "unread", false, "Only return threads whose latest message is unread by the agent")
return cmd
}
+289 -5
View File
@@ -3,8 +3,10 @@ package inbox
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
)
func TestInboxLifecycle(t *testing.T) {
@@ -238,9 +240,294 @@ func TestInboxFailLifecycle(t *testing.T) {
}
}
func TestInboxRenewWaitReplyAndCancel(t *testing.T) {
t.Parallel()
dbPath := filepath.Join(t.TempDir(), "coord.db")
runInboxCommand(t, "--db", dbPath, "--json", "init")
sendOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"send",
"--from", "leader",
"--to", "worker-c",
"--subject", "Investigate auth edge case",
"--summary", "Check auth redirect behavior",
"--run", "run_blog_003",
"--task", "T3",
)
var sendResp map[string]any
mustDecodeJSON(t, sendOut, &sendResp)
threadID := nestedString(t, sendResp, "data", "thread", "thread_id")
runInboxCommand(
t,
"--db", dbPath,
"--json",
"claim",
"--agent", "worker-c",
"--thread", threadID,
"--lease-seconds", "300",
)
renewOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"renew",
"--agent", "worker-c",
"--thread", threadID,
"--lease-seconds", "600",
)
var renewResp map[string]any
mustDecodeJSON(t, renewOut, &renewResp)
if got := nestedString(t, renewResp, "data", "message", "summary"); got != "lease renewed" {
t.Fatalf("expected lease renewed summary, got %q", got)
}
blockedOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"update",
"--agent", "worker-c",
"--thread", threadID,
"--status", "blocked",
"--summary", "Need policy decision",
"--body", "Should guest users be redirected to login or shown a 403 page?",
)
var blockedResp map[string]any
mustDecodeJSON(t, blockedOut, &blockedResp)
blockedMessageID := nestedString(t, blockedResp, "data", "message", "message_id")
type commandResult struct {
stdout string
stderr string
err error
}
waitCh := make(chan commandResult, 1)
go func() {
stdout, stderr, err := executeInboxCommand(
"--db", dbPath,
"--json",
"wait-reply",
"--thread", threadID,
"--after-message", blockedMessageID,
"--timeout-seconds", "2",
)
waitCh <- commandResult{stdout: stdout, stderr: stderr, err: err}
}()
time.Sleep(200 * time.Millisecond)
runInboxCommand(
t,
"--db", dbPath,
"--json",
"reply",
"--from", "leader",
"--to", "worker-c",
"--thread", threadID,
"--summary", "Redirect to login",
"--body", "Redirect guests to login for the MVP.",
)
var waitResult commandResult
select {
case waitResult = <-waitCh:
case <-time.After(3 * time.Second):
t.Fatal("wait-reply command did not return")
}
if waitResult.err != nil {
t.Fatalf("wait-reply failed: %v\nstderr:\n%s", waitResult.err, waitResult.stderr)
}
var waitResp map[string]any
mustDecodeJSON(t, waitResult.stdout, &waitResp)
if woke, ok := nestedValue(t, waitResp, "data", "woke").(bool); !ok || !woke {
t.Fatalf("expected wait-reply to wake, got %#v", nestedValue(t, waitResp, "data", "woke"))
}
if kind := nestedString(t, waitResp, "data", "message", "kind"); kind != "answer" {
t.Fatalf("expected answer wake message, got %q", kind)
}
cancelOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"cancel",
"--agent", "leader",
"--thread", threadID,
"--reason", "Task superseded by a larger refactor",
)
var cancelResp map[string]any
mustDecodeJSON(t, cancelOut, &cancelResp)
if status := nestedString(t, cancelResp, "data", "thread", "status"); status != "cancelled" {
t.Fatalf("expected cancelled thread, got %q", status)
}
}
func TestInboxWatchListUnreadAndAppend(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
dbPath := filepath.Join(tempDir, "coord.db")
bodyPath := filepath.Join(tempDir, "task.md")
if err := os.WriteFile(bodyPath, []byte("Implement the initial admin post editor."), 0o644); err != nil {
t.Fatalf("write body file: %v", err)
}
runInboxCommand(t, "--db", dbPath, "--json", "init")
type commandResult struct {
stdout string
stderr string
err error
}
watchCh := make(chan commandResult, 1)
go func() {
stdout, stderr, err := executeInboxCommand(
"--db", dbPath,
"--json",
"watch",
"--agent", "worker-d",
"--status", "pending",
"--timeout-seconds", "2",
)
watchCh <- commandResult{stdout: stdout, stderr: stderr, err: err}
}()
time.Sleep(200 * time.Millisecond)
sendOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"send",
"--from", "leader",
"--to", "worker-d",
"--subject", "Build admin editor",
"--summary", "Create the first editor screen",
"--body-file", bodyPath,
"--run", "run_blog_004",
"--task", "T4",
)
var sendResp map[string]any
mustDecodeJSON(t, sendOut, &sendResp)
threadID := nestedString(t, sendResp, "data", "thread", "thread_id")
var watchResult commandResult
select {
case watchResult = <-watchCh:
case <-time.After(3 * time.Second):
t.Fatal("watch command did not return")
}
if watchResult.err != nil {
t.Fatalf("watch failed: %v\nstderr:\n%s", watchResult.err, watchResult.stderr)
}
var watchResp map[string]any
mustDecodeJSON(t, watchResult.stdout, &watchResp)
if woke, ok := nestedValue(t, watchResp, "data", "woke").(bool); !ok || !woke {
t.Fatalf("expected watch to wake, got %#v", nestedValue(t, watchResp, "data", "woke"))
}
if watchedThreadID := nestedString(t, watchResp, "data", "thread", "thread_id"); watchedThreadID != threadID {
t.Fatalf("expected watch on thread %s, got %s", threadID, watchedThreadID)
}
fetchOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"fetch",
"--agent", "worker-d",
"--status", "pending",
"--unread",
)
var fetchResp map[string]any
mustDecodeJSON(t, fetchOut, &fetchResp)
fetchedThreads, ok := nestedValue(t, fetchResp, "data", "threads").([]any)
if !ok || len(fetchedThreads) != 1 {
t.Fatalf("expected one unread pending thread, got %#v", nestedValue(t, fetchResp, "data", "threads"))
}
listOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"list",
"--assigned-to", "worker-d",
"--status", "pending",
)
var listResp map[string]any
mustDecodeJSON(t, listOut, &listResp)
listedThreads, ok := nestedValue(t, listResp, "data", "threads").([]any)
if !ok || len(listedThreads) != 1 {
t.Fatalf("expected one listed thread, got %#v", nestedValue(t, listResp, "data", "threads"))
}
runInboxCommand(
t,
"--db", dbPath,
"--json",
"send",
"--from", "leader",
"--to", "worker-d",
"--thread", threadID,
"--summary", "Use a markdown editor",
"--body", "Prefer a textarea-based markdown editor for v1.",
)
showOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"show",
"--thread", threadID,
)
var showResp map[string]any
mustDecodeJSON(t, showOut, &showResp)
messages, ok := nestedValue(t, showResp, "data", "messages").([]any)
if !ok || len(messages) != 2 {
t.Fatalf("expected two messages after append, got %#v", nestedValue(t, showResp, "data", "messages"))
}
firstMessage, ok := messages[0].(map[string]any)
if !ok {
t.Fatalf("expected first message object, got %#v", messages[0])
}
if firstMessage["body"] != "Implement the initial admin post editor." {
t.Fatalf("expected body-file content in first message, got %#v", firstMessage["body"])
}
}
func runInboxCommand(t *testing.T, args ...string) string {
t.Helper()
stdout, stderr, err := executeInboxCommand(args...)
if err != nil {
t.Fatalf("execute inbox command %v: %v\nstderr:\n%s", args, err, stderr)
}
return stdout
}
func executeInboxCommand(args ...string) (string, string, error) {
cmd := NewRootCmd()
var stdout bytes.Buffer
var stderr bytes.Buffer
@@ -248,11 +535,8 @@ func runInboxCommand(t *testing.T, args ...string) string {
cmd.SetErr(&stderr)
cmd.SetArgs(args)
if err := cmd.Execute(); err != nil {
t.Fatalf("execute inbox command %v: %v\nstderr:\n%s", args, err, stderr.String())
}
return stdout.String()
err := cmd.Execute()
return stdout.String(), stderr.String(), err
}
func mustDecodeJSON(t *testing.T, raw string, target any) {
+81
View File
@@ -0,0 +1,81 @@
package inbox
import (
"fmt"
"ai-workflow-skill/internal/db"
"ai-workflow-skill/internal/protocol"
"ai-workflow-skill/internal/store"
"github.com/spf13/cobra"
)
type listOptions struct {
agent string
statuses string
createdBy string
assignedTo string
limit int
}
func newListCmd(root *rootOptions) *cobra.Command {
opts := &listOptions{}
cmd := &cobra.Command{
Use: "list",
Short: "List threads with filters",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
agent := opts.agent
if agent == "" {
agent = root.agent
}
sqlDB, err := db.Open(ctx, root.dbPath)
if err != nil {
return err
}
defer sqlDB.Close()
s := store.NewInboxStore(sqlDB)
threads, err := s.ListThreads(ctx, store.ListInput{
Agent: agent,
Statuses: parseCSV(opts.statuses),
CreatedBy: opts.createdBy,
AssignedTo: opts.assignedTo,
Limit: opts.limit,
})
if err != nil {
return err
}
resp := protocol.Success{
OK: true,
Command: "list",
Data: map[string]any{
"threads": threads,
},
}
if root.json {
return protocol.WriteJSON(cmd.OutOrStdout(), resp)
}
for _, thread := range threads {
if _, err := fmt.Fprintf(cmd.OutOrStdout(), "%s\t%s\t%s\t%s\n", thread.ThreadID, thread.Status, thread.AssignedTo, thread.Subject); err != nil {
return err
}
}
return nil
},
}
cmd.Flags().StringVar(&opts.agent, "agent", "", "Assigned agent filter shortcut")
cmd.Flags().StringVar(&opts.statuses, "status", "", "Comma-separated status filter")
cmd.Flags().StringVar(&opts.createdBy, "created-by", "", "Created-by filter")
cmd.Flags().StringVar(&opts.assignedTo, "assigned-to", "", "Assigned-to filter")
cmd.Flags().IntVar(&opts.limit, "limit", 20, "Maximum number of threads")
return cmd
}
+77
View File
@@ -0,0 +1,77 @@
package inbox
import (
"fmt"
"ai-workflow-skill/internal/db"
"ai-workflow-skill/internal/protocol"
"ai-workflow-skill/internal/store"
"github.com/spf13/cobra"
)
type renewOptions struct {
agent string
threadID string
leaseSeconds int
}
func newRenewCmd(root *rootOptions) *cobra.Command {
opts := &renewOptions{}
cmd := &cobra.Command{
Use: "renew",
Short: "Extend an existing lease",
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)
result, err := s.RenewLease(ctx, store.RenewInput{
ThreadID: opts.threadID,
Agent: agent,
LeaseSeconds: opts.leaseSeconds,
})
if err != nil {
return err
}
resp := protocol.Success{
OK: true,
Command: "renew",
Data: map[string]any{
"thread": result.Thread,
"message": result.Message,
},
}
if root.json {
return protocol.WriteJSON(cmd.OutOrStdout(), resp)
}
_, err = fmt.Fprintf(cmd.OutOrStdout(), "renewed lease on thread %s\n", result.Thread.ThreadID)
return err
},
}
cmd.Flags().StringVar(&opts.agent, "agent", "", "Lease owner")
cmd.Flags().StringVar(&opts.threadID, "thread", "", "Thread ID")
cmd.Flags().IntVar(&opts.leaseSeconds, "lease-seconds", 900, "Lease duration in seconds")
_ = cmd.MarkFlagRequired("thread")
return cmd
}
+8 -1
View File
@@ -17,6 +17,7 @@ type replyOptions struct {
kind string
summary string
body string
bodyFile string
payloadJSON string
}
@@ -37,6 +38,11 @@ func newReplyCmd(root *rootOptions) *cobra.Command {
return fmt.Errorf("from agent is required")
}
body, err := resolveBodyValue(opts.body, opts.bodyFile)
if err != nil {
return err
}
sqlDB, err := db.Open(ctx, root.dbPath)
if err != nil {
return err
@@ -50,7 +56,7 @@ func newReplyCmd(root *rootOptions) *cobra.Command {
ToAgent: opts.to,
Kind: opts.kind,
Summary: opts.summary,
Body: opts.body,
Body: body,
PayloadJSON: opts.payloadJSON,
})
if err != nil {
@@ -81,6 +87,7 @@ func newReplyCmd(root *rootOptions) *cobra.Command {
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")
_ = cmd.MarkFlagRequired("thread")
+5
View File
@@ -26,10 +26,15 @@ func NewRootCmd() *cobra.Command {
cmd.AddCommand(newSendCmd(opts))
cmd.AddCommand(newFetchCmd(opts))
cmd.AddCommand(newClaimCmd(opts))
cmd.AddCommand(newRenewCmd(opts))
cmd.AddCommand(newUpdateCmd(opts))
cmd.AddCommand(newReplyCmd(opts))
cmd.AddCommand(newDoneCmd(opts))
cmd.AddCommand(newFailCmd(opts))
cmd.AddCommand(newCancelCmd(opts))
cmd.AddCommand(newListCmd(opts))
cmd.AddCommand(newWatchCmd(opts))
cmd.AddCommand(newWaitReplyCmd(opts))
cmd.AddCommand(newShowCmd(opts))
return cmd
+20 -4
View File
@@ -20,6 +20,7 @@ type sendOptions struct {
kind string
summary string
body string
bodyFile string
payloadJSON string
priority string
}
@@ -33,6 +34,22 @@ func newSendCmd(root *rootOptions) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
from := opts.from
if from == "" {
from = root.agent
}
if from == "" {
return fmt.Errorf("from agent is required")
}
if opts.threadID == "" && opts.subject == "" {
return fmt.Errorf("subject is required when creating a new thread")
}
body, err := resolveBodyValue(opts.body, opts.bodyFile)
if err != nil {
return err
}
sqlDB, err := db.Open(ctx, root.dbPath)
if err != nil {
return err
@@ -45,11 +62,11 @@ func newSendCmd(root *rootOptions) *cobra.Command {
RunID: opts.runID,
TaskID: opts.taskID,
Subject: opts.subject,
FromAgent: opts.from,
FromAgent: from,
ToAgent: opts.to,
Kind: opts.kind,
Summary: opts.summary,
Body: opts.body,
Body: body,
PayloadJSON: opts.payloadJSON,
Priority: opts.priority,
})
@@ -84,12 +101,11 @@ func newSendCmd(root *rootOptions) *cobra.Command {
cmd.Flags().StringVar(&opts.kind, "kind", "task", "Initial message kind")
cmd.Flags().StringVar(&opts.summary, "summary", "", "Short message summary")
cmd.Flags().StringVar(&opts.body, "body", "", "Message body")
cmd.Flags().StringVar(&opts.bodyFile, "body-file", "", "Read message body from file")
cmd.Flags().StringVar(&opts.payloadJSON, "payload-json", "", "Structured payload JSON string")
cmd.Flags().StringVar(&opts.priority, "priority", "normal", "Thread priority")
_ = cmd.MarkFlagRequired("from")
_ = cmd.MarkFlagRequired("to")
_ = cmd.MarkFlagRequired("subject")
return cmd
}
+8 -1
View File
@@ -16,6 +16,7 @@ type updateOptions struct {
status string
summary string
body string
bodyFile string
payloadJSON string
}
@@ -36,6 +37,11 @@ func newUpdateCmd(root *rootOptions) *cobra.Command {
return fmt.Errorf("agent is required")
}
body, err := resolveBodyValue(opts.body, opts.bodyFile)
if err != nil {
return err
}
sqlDB, err := db.Open(ctx, root.dbPath)
if err != nil {
return err
@@ -48,7 +54,7 @@ func newUpdateCmd(root *rootOptions) *cobra.Command {
Agent: agent,
Status: opts.status,
Summary: opts.summary,
Body: opts.body,
Body: body,
PayloadJSON: opts.payloadJSON,
})
if err != nil {
@@ -78,6 +84,7 @@ func newUpdateCmd(root *rootOptions) *cobra.Command {
cmd.Flags().StringVar(&opts.status, "status", "", "New status: in_progress or blocked")
cmd.Flags().StringVar(&opts.summary, "summary", "", "Short update summary")
cmd.Flags().StringVar(&opts.body, "body", "", "Update body")
cmd.Flags().StringVar(&opts.bodyFile, "body-file", "", "Read update body from file")
cmd.Flags().StringVar(&opts.payloadJSON, "payload-json", "", "Structured payload JSON string")
_ = cmd.MarkFlagRequired("thread")
+85
View File
@@ -0,0 +1,85 @@
package inbox
import (
"fmt"
"time"
"ai-workflow-skill/internal/db"
"ai-workflow-skill/internal/protocol"
"ai-workflow-skill/internal/store"
"github.com/spf13/cobra"
)
type waitReplyOptions struct {
threadID string
afterMessageID string
afterEventID int64
kinds string
timeoutSeconds int
}
func newWaitReplyCmd(root *rootOptions) *cobra.Command {
opts := &waitReplyOptions{}
cmd := &cobra.Command{
Use: "wait-reply",
Short: "Block until a reply-like message appears in a thread",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
sqlDB, err := db.Open(ctx, root.dbPath)
if err != nil {
return err
}
defer sqlDB.Close()
s := store.NewInboxStore(sqlDB)
result, err := s.WaitReply(ctx, store.WaitReplyInput{
ThreadID: opts.threadID,
AfterMessageID: opts.afterMessageID,
AfterEventID: opts.afterEventID,
Kinds: parseCSV(opts.kinds),
Timeout: time.Duration(opts.timeoutSeconds) * time.Second,
})
if err != nil {
return err
}
data := map[string]any{
"woke": result.Woke,
"next_event_id": result.NextEventID,
}
if result.Message != nil {
data["message"] = result.Message
}
resp := protocol.Success{
OK: true,
Command: "wait-reply",
Data: data,
}
if root.json {
return protocol.WriteJSON(cmd.OutOrStdout(), resp)
}
if !result.Woke {
_, err = fmt.Fprintln(cmd.OutOrStdout(), "wait-reply timed out")
return err
}
_, err = fmt.Fprintf(cmd.OutOrStdout(), "reply received on thread %s at event %d\n", result.Message.ThreadID, result.NextEventID)
return err
},
}
cmd.Flags().StringVar(&opts.threadID, "thread", "", "Thread ID")
cmd.Flags().StringVar(&opts.afterMessageID, "after-message", "", "Resume after a known message ID")
cmd.Flags().Int64Var(&opts.afterEventID, "after-event", 0, "Resume after a known event ID")
cmd.Flags().StringVar(&opts.kinds, "kinds", "answer,control,result", "Comma-separated message kinds to wake on")
cmd.Flags().IntVar(&opts.timeoutSeconds, "timeout-seconds", 0, "Maximum time to wait; 0 waits forever")
_ = cmd.MarkFlagRequired("thread")
return cmd
}
+92
View File
@@ -0,0 +1,92 @@
package inbox
import (
"fmt"
"time"
"ai-workflow-skill/internal/db"
"ai-workflow-skill/internal/protocol"
"ai-workflow-skill/internal/store"
"github.com/spf13/cobra"
)
type watchOptions struct {
agent string
statuses string
timeoutSeconds int
afterEventID int64
}
func newWatchCmd(root *rootOptions) *cobra.Command {
opts := &watchOptions{}
cmd := &cobra.Command{
Use: "watch",
Short: "Block until new matching activity appears",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
agent := opts.agent
if agent == "" {
agent = root.agent
}
sqlDB, err := db.Open(ctx, root.dbPath)
if err != nil {
return err
}
defer sqlDB.Close()
s := store.NewInboxStore(sqlDB)
result, err := s.WatchThreads(ctx, store.WatchInput{
Agent: agent,
Statuses: parseCSV(opts.statuses),
AfterEventID: opts.afterEventID,
StartFromNow: !cmd.Flags().Changed("after-event"),
Timeout: time.Duration(opts.timeoutSeconds) * time.Second,
})
if err != nil {
return err
}
data := map[string]any{
"woke": result.Woke,
"next_event_id": result.NextEventID,
}
if result.Thread != nil {
data["thread"] = result.Thread
}
if result.Message != nil {
data["message"] = result.Message
}
if result.Event != nil {
data["event"] = result.Event
}
resp := protocol.Success{
OK: true,
Command: "watch",
Data: data,
}
if root.json {
return protocol.WriteJSON(cmd.OutOrStdout(), resp)
}
if !result.Woke {
_, err = fmt.Fprintln(cmd.OutOrStdout(), "watch timed out")
return err
}
_, err = fmt.Fprintf(cmd.OutOrStdout(), "watch woke on thread %s at event %d\n", result.Thread.ThreadID, result.NextEventID)
return err
},
}
cmd.Flags().StringVar(&opts.agent, "agent", "", "Assigned agent filter")
cmd.Flags().StringVar(&opts.statuses, "status", "pending,blocked,done,failed", "Comma-separated status filter")
cmd.Flags().IntVar(&opts.timeoutSeconds, "timeout-seconds", 0, "Maximum time to wait; 0 waits forever")
cmd.Flags().Int64Var(&opts.afterEventID, "after-event", 0, "Resume after a known event ID")
return cmd
}