Add inbox update reply done fail commands
This commit is contained in:
+283
-5
@@ -80,6 +80,34 @@ type ClaimResult struct {
|
||||
Message Message `json:"message"`
|
||||
}
|
||||
|
||||
type UpdateInput struct {
|
||||
ThreadID string
|
||||
Agent string
|
||||
Status string
|
||||
Summary string
|
||||
Body string
|
||||
PayloadJSON string
|
||||
}
|
||||
|
||||
type ReplyInput struct {
|
||||
ThreadID string
|
||||
FromAgent string
|
||||
ToAgent string
|
||||
Kind string
|
||||
Summary string
|
||||
Body string
|
||||
PayloadJSON string
|
||||
}
|
||||
|
||||
type CompleteInput struct {
|
||||
ThreadID string
|
||||
Agent string
|
||||
Summary string
|
||||
Body string
|
||||
PayloadJSON string
|
||||
Failed bool
|
||||
}
|
||||
|
||||
func NewInboxStore(db *sql.DB) *InboxStore {
|
||||
return &InboxStore{db: db}
|
||||
}
|
||||
@@ -381,6 +409,215 @@ func (s *InboxStore) ClaimThread(ctx context.Context, input ClaimInput) (ClaimRe
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *InboxStore) UpdateThreadStatus(ctx context.Context, input UpdateInput) (Thread, Message, error) {
|
||||
now := nowUTC()
|
||||
messageID := newID("msg")
|
||||
|
||||
if input.Status != "in_progress" && input.Status != "blocked" {
|
||||
return Thread{}, Message{}, fmt.Errorf("unsupported update status %q", input.Status)
|
||||
}
|
||||
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return Thread{}, Message{}, fmt.Errorf("begin update transaction: %w", err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
thread, err := selectThreadForUpdate(ctx, tx, input.ThreadID)
|
||||
if err != nil {
|
||||
return Thread{}, Message{}, err
|
||||
}
|
||||
|
||||
if thread.Status == "done" || thread.Status == "failed" || thread.Status == "cancelled" {
|
||||
return Thread{}, Message{}, fmt.Errorf("thread %s is already terminal", input.ThreadID)
|
||||
}
|
||||
|
||||
kind := "progress"
|
||||
if input.Status == "blocked" {
|
||||
kind = "question"
|
||||
}
|
||||
|
||||
message := Message{
|
||||
MessageID: messageID,
|
||||
ThreadID: thread.ThreadID,
|
||||
FromAgent: input.Agent,
|
||||
ToAgent: thread.CreatedBy,
|
||||
Kind: kind,
|
||||
Summary: input.Summary,
|
||||
Body: input.Body,
|
||||
PayloadJSON: json.RawMessage(normalizeJSON(input.PayloadJSON)),
|
||||
CreatedAt: now,
|
||||
}
|
||||
|
||||
if err := insertMessage(ctx, tx, message); err != nil {
|
||||
return Thread{}, Message{}, err
|
||||
}
|
||||
|
||||
if err := updateThreadState(ctx, tx, thread.ThreadID, input.Status, thread.AssignedTo, message.MessageID, now); err != nil {
|
||||
return Thread{}, Message{}, err
|
||||
}
|
||||
|
||||
if err := insertEvent(ctx, tx, eventInput{
|
||||
RunID: thread.RunID,
|
||||
TaskID: thread.TaskID,
|
||||
ThreadID: thread.ThreadID,
|
||||
Source: "inbox",
|
||||
EventType: "thread_" + input.Status,
|
||||
MessageID: message.MessageID,
|
||||
Summary: message.Summary,
|
||||
PayloadJSON: string(message.PayloadJSON),
|
||||
CreatedAt: now,
|
||||
}); err != nil {
|
||||
return Thread{}, Message{}, err
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return Thread{}, Message{}, fmt.Errorf("commit update transaction: %w", err)
|
||||
}
|
||||
|
||||
thread.Status = input.Status
|
||||
thread.LatestMessageID = message.MessageID
|
||||
thread.UpdatedAt = now
|
||||
return thread, message, nil
|
||||
}
|
||||
|
||||
func (s *InboxStore) ReplyToThread(ctx context.Context, input ReplyInput) (Thread, Message, error) {
|
||||
now := nowUTC()
|
||||
messageID := newID("msg")
|
||||
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return Thread{}, Message{}, fmt.Errorf("begin reply transaction: %w", err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
thread, err := selectThreadForUpdate(ctx, tx, input.ThreadID)
|
||||
if err != nil {
|
||||
return Thread{}, Message{}, err
|
||||
}
|
||||
|
||||
message := Message{
|
||||
MessageID: messageID,
|
||||
ThreadID: thread.ThreadID,
|
||||
FromAgent: input.FromAgent,
|
||||
ToAgent: input.ToAgent,
|
||||
Kind: defaultString(input.Kind, "answer"),
|
||||
Summary: input.Summary,
|
||||
Body: input.Body,
|
||||
PayloadJSON: json.RawMessage(normalizeJSON(input.PayloadJSON)),
|
||||
CreatedAt: now,
|
||||
}
|
||||
|
||||
if err := insertMessage(ctx, tx, message); err != nil {
|
||||
return Thread{}, Message{}, err
|
||||
}
|
||||
|
||||
if err := updateThreadState(ctx, tx, thread.ThreadID, thread.Status, thread.AssignedTo, message.MessageID, now); err != nil {
|
||||
return Thread{}, Message{}, err
|
||||
}
|
||||
|
||||
if err := insertEvent(ctx, tx, eventInput{
|
||||
RunID: thread.RunID,
|
||||
TaskID: thread.TaskID,
|
||||
ThreadID: thread.ThreadID,
|
||||
Source: "inbox",
|
||||
EventType: "thread_replied",
|
||||
MessageID: message.MessageID,
|
||||
Summary: message.Summary,
|
||||
PayloadJSON: string(message.PayloadJSON),
|
||||
CreatedAt: now,
|
||||
}); err != nil {
|
||||
return Thread{}, Message{}, err
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return Thread{}, Message{}, fmt.Errorf("commit reply transaction: %w", err)
|
||||
}
|
||||
|
||||
thread.LatestMessageID = message.MessageID
|
||||
thread.UpdatedAt = now
|
||||
return thread, message, nil
|
||||
}
|
||||
|
||||
func (s *InboxStore) CompleteThread(ctx context.Context, input CompleteInput) (Thread, Message, error) {
|
||||
now := nowUTC()
|
||||
messageID := newID("msg")
|
||||
|
||||
nextStatus := "done"
|
||||
eventType := "thread_done"
|
||||
summary := input.Summary
|
||||
if input.Failed {
|
||||
nextStatus = "failed"
|
||||
eventType = "thread_failed"
|
||||
}
|
||||
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return Thread{}, Message{}, fmt.Errorf("begin complete transaction: %w", err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
thread, err := selectThreadForUpdate(ctx, tx, input.ThreadID)
|
||||
if err != nil {
|
||||
return Thread{}, Message{}, err
|
||||
}
|
||||
|
||||
message := Message{
|
||||
MessageID: messageID,
|
||||
ThreadID: thread.ThreadID,
|
||||
FromAgent: input.Agent,
|
||||
ToAgent: thread.CreatedBy,
|
||||
Kind: "result",
|
||||
Summary: summary,
|
||||
Body: input.Body,
|
||||
PayloadJSON: json.RawMessage(normalizeJSON(input.PayloadJSON)),
|
||||
CreatedAt: now,
|
||||
}
|
||||
|
||||
if err := insertMessage(ctx, tx, message); err != nil {
|
||||
return Thread{}, Message{}, err
|
||||
}
|
||||
|
||||
if err := updateThreadState(ctx, tx, thread.ThreadID, nextStatus, thread.AssignedTo, message.MessageID, now); err != nil {
|
||||
return Thread{}, Message{}, err
|
||||
}
|
||||
|
||||
if _, err := tx.ExecContext(
|
||||
ctx,
|
||||
`UPDATE leases
|
||||
SET released_at = ?
|
||||
WHERE thread_id = ?
|
||||
AND released_at IS NULL`,
|
||||
formatTime(now),
|
||||
thread.ThreadID,
|
||||
); err != nil {
|
||||
return Thread{}, Message{}, fmt.Errorf("release lease: %w", err)
|
||||
}
|
||||
|
||||
if err := insertEvent(ctx, tx, eventInput{
|
||||
RunID: thread.RunID,
|
||||
TaskID: thread.TaskID,
|
||||
ThreadID: thread.ThreadID,
|
||||
Source: "inbox",
|
||||
EventType: eventType,
|
||||
MessageID: message.MessageID,
|
||||
Summary: message.Summary,
|
||||
PayloadJSON: string(message.PayloadJSON),
|
||||
CreatedAt: now,
|
||||
}); err != nil {
|
||||
return Thread{}, Message{}, err
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return Thread{}, Message{}, fmt.Errorf("commit complete transaction: %w", err)
|
||||
}
|
||||
|
||||
thread.Status = nextStatus
|
||||
thread.LatestMessageID = message.MessageID
|
||||
thread.UpdatedAt = now
|
||||
return thread, message, nil
|
||||
}
|
||||
|
||||
func (s *InboxStore) GetThread(ctx context.Context, threadID string) (ThreadDetail, error) {
|
||||
thread, err := selectThread(ctx, s.db, threadID)
|
||||
if err != nil {
|
||||
@@ -427,9 +664,9 @@ type threadScanner interface {
|
||||
|
||||
func scanThread(scanner threadScanner) (Thread, error) {
|
||||
var (
|
||||
thread Thread
|
||||
createdAt, updatedAt string
|
||||
latestMessageID sql.NullString
|
||||
thread Thread
|
||||
createdAt, updatedAt string
|
||||
latestMessageID sql.NullString
|
||||
)
|
||||
|
||||
if err := scanner.Scan(
|
||||
@@ -459,8 +696,8 @@ func scanThread(scanner threadScanner) (Thread, error) {
|
||||
|
||||
func scanMessage(scanner threadScanner) (Message, error) {
|
||||
var (
|
||||
message Message
|
||||
payload, createdAt string
|
||||
message Message
|
||||
payload, createdAt string
|
||||
)
|
||||
|
||||
if err := scanner.Scan(
|
||||
@@ -543,6 +780,47 @@ func insertEvent(ctx context.Context, tx *sql.Tx, input eventInput) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func insertMessage(ctx context.Context, tx *sql.Tx, message Message) error {
|
||||
_, err := tx.ExecContext(
|
||||
ctx,
|
||||
`INSERT INTO messages (
|
||||
message_id, thread_id, from_agent, to_agent, kind, summary, body,
|
||||
payload_json, created_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
message.MessageID,
|
||||
message.ThreadID,
|
||||
message.FromAgent,
|
||||
message.ToAgent,
|
||||
message.Kind,
|
||||
message.Summary,
|
||||
message.Body,
|
||||
string(message.PayloadJSON),
|
||||
formatTime(message.CreatedAt),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("insert message: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func updateThreadState(ctx context.Context, tx *sql.Tx, threadID, status, assignedTo, latestMessageID string, updatedAt time.Time) error {
|
||||
_, err := tx.ExecContext(
|
||||
ctx,
|
||||
`UPDATE threads
|
||||
SET status = ?, assigned_to = ?, latest_message_id = ?, updated_at = ?
|
||||
WHERE thread_id = ?`,
|
||||
status,
|
||||
assignedTo,
|
||||
latestMessageID,
|
||||
formatTime(updatedAt),
|
||||
threadID,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("update thread state: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func defaultID(value, prefix string) string {
|
||||
if value != "" {
|
||||
return value
|
||||
|
||||
Reference in New Issue
Block a user