bridge

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package bridge provides agent-to-agent communication via JSONL files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bridge

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

Bridge handles message passing between agents with SHA256 deduplication.

func New

func New(storagePath string, runID int) *Bridge

New creates a new bridge with the given storage path.

func (*Bridge) Close

func (b *Bridge) Close() error

Close closes the bridge storage.

func (*Bridge) Drain

func (b *Bridge) Drain(ctx context.Context, target string, seenIDs map[string]bool) ([]*Message, error)

Drain retrieves all unprocessed messages for a target agent. Messages are marked as seen to prevent re-delivery.

func (*Bridge) DrainNew

func (b *Bridge) DrainNew(ctx context.Context, target string, sinceID string) ([]*Message, error)

DrainNew retrieves messages for a target agent since a given message ID.

func (*Bridge) GetByID

func (b *Bridge) GetByID(id string) (*Message, error)

GetByID retrieves a message by its ID.

func (*Bridge) Open

func (b *Bridge) Open() error

Open initializes the bridge, loading existing messages for deduplication.

func (*Bridge) Send

func (b *Bridge) Send(ctx context.Context, msg *Message) (bool, error)

Send sends a message through the bridge. Returns false if the message is a duplicate.

func (*Bridge) SetLogger

func (b *Bridge) SetLogger(log *slog.Logger)

SetLogger sets the logger for the bridge.

func (*Bridge) Status

func (b *Bridge) Status() *Status

Status returns the current bridge status.

type Message

type Message struct {
	// ID is the unique SHA256 hash of the message content.
	ID string `json:"id"`

	// Type categorizes the message.
	Type MessageType `json:"type"`

	// From identifies the sender agent.
	From string `json:"from"`

	// To identifies the target agent (empty for broadcast).
	To string `json:"to,omitempty"`

	// Content is the message payload.
	Content string `json:"content"`

	// Signal is set for signal-type messages.
	Signal Signal `json:"signal,omitempty"`

	// Metadata contains optional key-value data.
	Metadata map[string]any `json:"metadata,omitempty"`

	// Timestamp is when the message was created.
	Timestamp time.Time `json:"timestamp"`

	// RunID identifies the run this message belongs to.
	RunID int `json:"run_id"`

	// Iteration is the loop iteration number.
	Iteration int `json:"iteration"`
}

Message represents a message passed between agents via the bridge.

func NewMessage

func NewMessage(msgType MessageType, from, to, content string) *Message

NewMessage creates a new message with auto-generated ID.

func NewResultMessage

func NewResultMessage(from, to, result string) *Message

NewResultMessage creates a result message.

func NewReviewMessage

func NewReviewMessage(from, to, review string) *Message

NewReviewMessage creates a review request/response message.

func NewSignalMessage

func NewSignalMessage(from string, signal Signal, content string) *Message

NewSignalMessage creates a signal message.

func NewTaskMessage

func NewTaskMessage(from, to, task string) *Message

NewTaskMessage creates a task assignment message.

func (*Message) IsDoneSignal

func (m *Message) IsDoneSignal() bool

IsDoneSignal returns true if this is a DONE signal.

func (*Message) IsFailSignal

func (m *Message) IsFailSignal() bool

IsFailSignal returns true if this is a FAIL signal.

func (*Message) IsForAgent

func (m *Message) IsForAgent(agent string) bool

IsForAgent returns true if this message is targeted at the specified agent.

func (*Message) IsPassSignal

func (m *Message) IsPassSignal() bool

IsPassSignal returns true if this is a PASS signal.

func (*Message) WithMetadata

func (m *Message) WithMetadata(key string, value any) *Message

WithMetadata adds metadata to the message and returns it for chaining.

func (*Message) WithRunInfo

func (m *Message) WithRunInfo(runID, iteration int) *Message

WithRunInfo sets run ID and iteration and returns it for chaining.

type MessageType

type MessageType string

MessageType represents the type of bridge message.

const (
	// TypeTask indicates a task assignment message.
	TypeTask MessageType = "task"
	// TypeResult indicates a task result message.
	TypeResult MessageType = "result"
	// TypeReview indicates a review request/response.
	TypeReview MessageType = "review"
	// TypeSignal indicates a control signal (e.g., DONE, PASS, FAIL).
	TypeSignal MessageType = "signal"
	// TypeChat indicates a general chat/context message.
	TypeChat MessageType = "chat"
)

type Server

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

Server is an MCP server that exposes bridge tools to agents.

func NewServer

func NewServer(bridge *Bridge) *Server

NewServer creates a new MCP server for bridge tools.

func (*Server) Addr

func (s *Server) Addr() string

Addr returns the server's listen address (useful when using ":0").

func (*Server) Close

func (s *Server) Close() error

Close shuts down the server.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe(ctx context.Context, addr string) error

ListenAndServe starts the server on the given address. Use "stdio" for stdin/stdout transport, or a TCP address like ":0" for socket.

func (*Server) ServeIO

func (s *Server) ServeIO(ctx context.Context, r io.Reader, w io.Writer) error

ServeIO runs the server over custom reader/writer.

func (*Server) ServeStdio

func (s *Server) ServeStdio(ctx context.Context) error

ServeStdio runs the server over stdin/stdout.

type Signal

type Signal string

Signal represents control signals.

const (
	SignalDone Signal = "DONE"
	SignalPass Signal = "PASS"
	SignalFail Signal = "FAIL"
)

type Status

type Status struct {
	TotalMessages int
	ByAgent       map[string]int
	ByType        map[MessageType]int
	HasDoneSignal bool
	PassCount     int
	FailCount     int
}

Status contains bridge status information.

func (*Status) String

func (s *Status) String() string

String returns a human-readable status string.

type Storage

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

Storage handles persistent storage of bridge messages in JSONL format.

func NewStorage

func NewStorage(path string) *Storage

NewStorage creates a new storage instance for the given file path.

func (*Storage) Append

func (s *Storage) Append(msg *Message) error

Append writes a message to the storage file.

func (*Storage) Close

func (s *Storage) Close() error

Close closes the storage file.

func (*Storage) Exists

func (s *Storage) Exists() bool

Exists returns true if the storage file exists.

func (*Storage) Open

func (s *Storage) Open() error

Open opens the storage file for appending.

func (*Storage) Path

func (s *Storage) Path() string

Path returns the storage file path.

func (*Storage) ReadAll

func (s *Storage) ReadAll() ([]*Message, error)

ReadAll reads all messages from the storage file.

func (*Storage) ReadFiltered

func (s *Storage) ReadFiltered(filter func(*Message) bool) ([]*Message, error)

ReadFiltered reads messages matching the given filter function.

Jump to

Keyboard shortcuts

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