messenger

package
v0.152.2 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: MIT Imports: 19 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 messenger owns the stream-json process bridge used to deliver a message to an agent session. It is intentionally independent of HTTP and worker entry points so both server-local and worker dispatch paths share the same process lifecycle and environment rules.

Index

Constants

View Source
const (
	StatusStopped = "stopped" // no resident process
	StatusIdle    = "idle"    // process alive, no request in flight
	StatusBusy    = "busy"    // a request is queued or in flight
)

Status values reported by Manager.Status / Snapshot.Status.

View Source
const (
	// MaxDeliveries is how many recent deliveries a runner remembers.
	MaxDeliveries = 20
	// MaxStderrTail caps the stderr ring buffer (bytes).
	MaxStderrTail = 4096
)
View Source
const ListAgentsPrompt = "调用 ListAgents 并原样输出结果。不要调用其他工具,不要添加解释。"

ListAgentsPrompt is what the resident process is asked to run. The listing is read from the ListAgents tool_result, so the wording only has to make the model call the tool once.

View Source
const OpReply = "reply"

OpReply marks a delivery-history entry that is a session's reply received by the messenger (not a request sent through it).

Variables

This section is empty.

Functions

This section is empty.

Types

type Agent

type Agent struct {
	Name         string `json:"name"`
	ShortID      string `json:"short_id,omitempty"`
	Kind         string `json:"kind,omitempty"`   // e.g. interactive
	Status       string `json:"status,omitempty"` // idle | busy | ...
	Started      string `json:"started,omitempty"`
	Cwd          string `json:"cwd,omitempty"`
	LastActivity string `json:"last_activity,omitempty"`
}

Agent is one session ListAgents reports. Claude's listing carries a name, a short id, a kind, a status and a relative start time; it has NO directory or absolute timestamp, so Cwd/LastActivity stay empty unless a caller fills them from another source (the gofer session registry).

type AgentList

type AgentList struct {
	Self      string  `json:"self,omitempty"` // the messenger's own session name
	Agents    []Agent `json:"agents"`
	RawOutput string  `json:"raw_output"`
}

AgentList is the parsed ListAgents result. RawOutput is always the text the parser worked from, so nothing is lost when the format changes.

func ParseAgents

func ParseAgents(raw string) AgentList

ParseAgents turns the ListAgents listing into rows. It is deliberately tolerant: code fences are stripped, unknown "·" segments are ignored, and lines that do not look like a session row are skipped. RawOutput keeps the original text.

type Delivery

type Delivery struct {
	At         int64  `json:"at"`                // unix seconds the request started
	Op         string `json:"op,omitempty"`      // "send" | "list_agents"
	Target     string `json:"target,omitempty"`  // target session name (send)
	Message    string `json:"message,omitempty"` // excerpt, <= 200 runes
	OK         bool   `json:"ok"`
	Error      string `json:"error,omitempty"`
	DurationMS int64  `json:"duration_ms,omitempty"`
}

Delivery is the summary of one request sent through the resident process. Message holds a short excerpt (never the full text) so the history is useful without becoming a second message log.

type Manager

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

Manager keeps one stream-json Claude process per local runner. A process is never shared with a remote worker; remote paths use the worker's own Manager.

func New

func New(command string, idle time.Duration) *Manager

New creates a resident messenger manager. Non-positive idle values use the shipped ten minute default, matching the pre-package implementation.

func (*Manager) Close added in v0.135.0

func (m *Manager) Close()

Close stops every resident process and waits until its stdout reader ended. The manager stays usable: the next request starts a new process.

func (*Manager) Command

func (m *Manager) Command() string

Command is the configured messenger binary ("" on a worker, where each dispatch carries its own command).

func (*Manager) ListAgents

func (m *Manager) ListAgents(ctx context.Context, runner, cwd string, command []string) (AgentList, error)

ListAgents asks the resident process for the sessions Claude Code can message. command only supplies the binary (command[0]); the prompt is fixed.

func (*Manager) ResidentMessengerStatus

func (m *Manager) ResidentMessengerStatus(runner string) string

ResidentMessengerStatus preserves the session-relay assembly seam while the implementation lives in this package.

func (*Manager) Send

func (m *Manager) Send(ctx context.Context, runner, cwd, target string, command []string) (string, error)

Send writes one stream-json request and waits for its result. target is the addressed session's name; it only labels the delivery history.

func (*Manager) SetClaudeConfigDir added in v0.135.0

func (m *Manager) SetClaudeConfigDir(dir string)

SetClaudeConfigDir overrides where the resident process's Claude transcripts live ("" = ~/.claude: the child runs with every CLAUDE* variable scrubbed, so CLAUDE_CONFIG_DIR never applies to it).

func (*Manager) SetIdle

func (m *Manager) SetIdle(idle time.Duration)

SetIdle updates the idle lifetime used by newly created resident processes. Existing processes keep their current timer until the next request refreshes it.

func (*Manager) SetReplyHandler added in v0.135.0

func (m *Manager) SetReplyHandler(h ReplyHandler)

SetReplyHandler installs the callback for peer replies (nil = only record them in the delivery history).

func (*Manager) Snapshot

func (m *Manager) Snapshot(runner string) Snapshot

Snapshot returns the runner's current view. It never blocks on an in-flight request: every field is read from state guarded by short-held locks.

func (*Manager) Status

func (m *Manager) Status(runner string) string

Status reports the messenger state for a runner: stopped (no process), idle (process alive, nothing in flight) or busy (a request is queued or running).

type PeerReply added in v0.135.0

type PeerReply struct {
	From   string // peer address, e.g. uds:/tmp/cc-socks/<pid>.sock
	Name   string // peer session name (Claude Code's from-name)
	Body   string
	Retold string
	MsgID  string
	At     int64
}

PeerReply is a message another Claude session sent TO a resident messenger — in practice a target session answering a web message with SendMessage (its natural reply path: the message came from the messenger's socket). Body is the verbatim text from the messenger's own transcript; it is empty when the transcript could not be read, and Retold then holds the messenger model's own account of the turn (never posted as the target's words).

type ReplyHandler added in v0.135.0

type ReplyHandler func(runner string, r PeerReply)

ReplyHandler receives every PeerReply with a verbatim Body, on its own goroutine (the stdout reader never waits for it).

type Snapshot

type Snapshot struct {
	Status       string     `json:"status"`
	StartedAt    int64      `json:"started_at,omitempty"`
	LastUsedAt   int64      `json:"last_used_at,omitempty"`
	IdleDeadline int64      `json:"idle_deadline,omitempty"`
	Deliveries   []Delivery `json:"deliveries,omitempty"`
	StderrTail   string     `json:"stderr_tail,omitempty"`
}

Snapshot is a point-in-time, JSON-friendly view of one runner's messenger. Times are unix seconds (0 = never). Deliveries are newest first.

Jump to

Keyboard shortcuts

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