mailbox

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package mailbox provides a persistent, async inbox for each agent. Agents communicate by sending typed messages to each other's mailbox; the recipient processes them independently without needing to be online at the same time as the sender.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mailbox

type Mailbox interface {
	// Send delivers a message to the recipient's inbox.
	// For KindBroadcast, ToAgent must be "*" and TeamID must be set;
	// the implementation expands it to all agents in that team.
	Send(ctx context.Context, msg Message) error

	// Receive returns all unread messages for agentID, oldest first.
	Receive(ctx context.Context, agentID string) ([]Message, error)

	// MarkRead marks a single message as read.
	MarkRead(ctx context.Context, msgID string) error

	// MarkAllRead marks all unread messages for agentID as read.
	MarkAllRead(ctx context.Context, agentID string) error

	// History returns up to limit messages for agentID (read and unread),
	// newest first.
	History(ctx context.Context, agentID string, limit int) ([]Message, error)

	// Thread returns all messages in a conversation thread rooted at rootID.
	Thread(ctx context.Context, rootID string) ([]Message, error)

	// Delete removes a message permanently.
	Delete(ctx context.Context, msgID string) error

	// PendingCounts returns the number of unread messages per agent for the
	// given agent IDs. Agents with zero unread messages are absent from the
	// returned map (treat missing keys as 0). Used by Dispatcher to route
	// tasks to the least-loaded available agent.
	PendingCounts(ctx context.Context, agentIDs []string) (map[string]int, error)
}

Mailbox is the interface for sending and receiving agent messages.

type Message

type Message struct {
	// ID is a UUID generated at send time.
	ID string `json:"id"`

	// Kind classifies the message intent.
	Kind MessageKind `json:"kind"`

	// FromAgent is the sender's agent UUID (or "system" for engine events).
	FromAgent string `json:"from_agent"`

	// ToAgent is the recipient's agent UUID.
	// For broadcast messages this is the expanded individual recipient.
	ToAgent string `json:"to_agent"`

	// Subject is a short one-line summary of the message.
	Subject string `json:"subject"`

	// Body is the full message content (plain text or markdown).
	Body string `json:"body"`

	// ReplyTo is the ID of the parent message when Kind == KindReply.
	ReplyTo string `json:"reply_to,omitempty"`

	// TeamID scopes the message to a team (optional).
	TeamID string `json:"team_id,omitempty"`

	CreatedAt time.Time  `json:"created_at"`
	ReadAt    *time.Time `json:"read_at,omitempty"`
}

Message is a single item in an agent's inbox.

func NewMessage

func NewMessage(kind MessageKind, from, to, subject, body string) Message

NewMessage creates a Message with a fresh UUID and the current timestamp.

type MessageKind

type MessageKind string

MessageKind classifies the intent of a message.

const (
	// KindTask delegates a unit of work to another agent.
	KindTask MessageKind = "task"
	// KindReply responds to a previous message (linked via ReplyTo).
	KindReply MessageKind = "reply"
	// KindBroadcast is a fan-out message sent to all agents in a team.
	// The Mailbox implementation expands it to individual per-recipient records.
	KindBroadcast MessageKind = "broadcast"
	// KindEvent carries a system notification (agent started, finished, errored).
	KindEvent MessageKind = "event"
)

type SQLiteMailbox

type SQLiteMailbox struct {
	// contains filtered or unexported fields
}

SQLiteMailbox is a Mailbox backed by SQLite via the shared db.DB handle.

func New

func New(database *db.DB, agentLister func(ctx context.Context, teamID string) ([]string, error)) *SQLiteMailbox

New creates a SQLiteMailbox backed by the given DB. agentLister is called when sending a KindBroadcast message to expand the recipients. Pass nil to disable broadcast support.

func (*SQLiteMailbox) Delete

func (m *SQLiteMailbox) Delete(ctx context.Context, msgID string) error

Delete removes a message permanently.

func (*SQLiteMailbox) History

func (m *SQLiteMailbox) History(ctx context.Context, agentID string, limit int) ([]Message, error)

History returns up to limit messages for agentID, newest first.

func (*SQLiteMailbox) MarkAllRead

func (m *SQLiteMailbox) MarkAllRead(ctx context.Context, agentID string) error

MarkAllRead marks all unread messages for agentID as read.

func (*SQLiteMailbox) MarkRead

func (m *SQLiteMailbox) MarkRead(ctx context.Context, msgID string) error

MarkRead marks a single message as read.

func (*SQLiteMailbox) PendingCounts

func (m *SQLiteMailbox) PendingCounts(ctx context.Context, agentIDs []string) (map[string]int, error)

PendingCounts returns the unread message count per agent for the given IDs. Agents with zero unread messages are absent from the map.

func (*SQLiteMailbox) Receive

func (m *SQLiteMailbox) Receive(ctx context.Context, agentID string) ([]Message, error)

Receive returns all unread messages for agentID, oldest first.

func (*SQLiteMailbox) Send

func (m *SQLiteMailbox) Send(ctx context.Context, msg Message) error

Send delivers a message to the recipient's inbox. For KindBroadcast, msg.ToAgent must be "*" and msg.TeamID must be set.

func (*SQLiteMailbox) Thread

func (m *SQLiteMailbox) Thread(ctx context.Context, rootID string) ([]Message, error)

Thread returns all messages in a conversation thread rooted at rootID, oldest first.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL