frontend

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: MIT Imports: 31 Imported by: 0

Documentation

Overview

Package frontend abstracts messaging transport (Telegram, Matrix) behind a unified interface. The daemon initialises one Frontend per team and routes all human↔agent messaging through it.

Plane: shared

Index

Constants

View Source
const (
	GateOptionApprove = "✅ Approve"
	GateOptionReject  = "❌ Reject"
)

Gate option labels — used in askHumanGate (advance.go) and handleAskHumanCallback.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command struct {
	Name         string // Telegram command name (sanitized: only [a-z0-9_])
	Description  string
	OriginalName string // original hyphenated name for agent dispatch (e.g. "review-pr")
}

Command describes a bot command for registration and handler dispatch.

type Frontend

type Frontend interface {
	// Start begins polling/syncing for inbound messages.
	Start(ctx context.Context) error

	// Stop gracefully shuts down the frontend.
	Stop(ctx context.Context) error

	// SendText sends a text message to an agent's chat/room.
	SendText(ctx context.Context, agentName string, text string) error

	// SendVoice sends voice audio to an agent's chat/room.
	SendVoice(ctx context.Context, agentName string, data []byte) error

	// SendNotification sends a system notification (daemon ready, task done, etc).
	// Routes to the team's notification channel.
	SendNotification(ctx context.Context, text string) error

	// SetReaction sets an emoji reaction on the last inbound message for an agent.
	SetReaction(ctx context.Context, agentName string, emoji string) error

	// AskHuman sends a question and blocks until answered or timed out.
	// If options is non-empty, presents them as choices.
	// Returns the human's answer, or skipped=true on timeout/skip.
	AskHuman(ctx context.Context, agentName, question string, options []string) (answer string, skipped bool, err error)

	// ClearTracking clears the tracked inbound message for an agent.
	// Called after the agent responds to prevent stale reactions on old messages.
	ClearTracking(ctx context.Context, agentName string) error

	// RegisterCommands registers bot commands for discoverability and stores them
	// for use by the polling handlers. Must be called before Start.
	RegisterCommands(commands []Command) error

	// AskHumanHTTPHandler returns an http.HandlerFunc for POST /ask/human.
	AskHumanHTTPHandler() http.HandlerFunc
}

Frontend abstracts a messaging transport (Telegram, Matrix). The daemon initialises one Frontend per team at startup.

type InboundHandler

type InboundHandler func(teamName, agentName, text string)

InboundHandler is called when a message arrives from the human. The frontend formats the message with the correct transport prefix before calling this. e.g. "[telegram from:neil] hello" or "[matrix from:neil] hello".

type MatrixConfig

type MatrixConfig struct {
	TeamName   string
	MCfg       *config.DaemonConfig
	OnMessage  InboundHandler
	MsgSvc     *message.Service
	UserNameFn func() string // returns human display name for this team
	GetUsageFn func() string // returns formatted usage string, or "" if unavailable
	RestartFn  func() error  // triggers daemon restart (launchctl kickstart -k)
}

MatrixConfig holds construction parameters for MatrixFrontend.

type MatrixFrontend

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

MatrixFrontend implements Frontend using the Matrix protocol via mautrix-go.

func NewMatrix

func NewMatrix(cfg MatrixConfig) (*MatrixFrontend, error)

NewMatrix constructs a MatrixFrontend from the given config. Returns an error if required config fields are missing or callbacks are nil. Agents whose env-var tokens are unset are skipped with a warning (partial setup is OK).

func (*MatrixFrontend) AskHuman

func (f *MatrixFrontend) AskHuman(
	ctx context.Context, agentName, question string, options []string,
) (string, bool, error)

AskHuman sends a question to the agent's Matrix room and blocks until answered or timed out.

func (*MatrixFrontend) AskHumanHTTPHandler

func (f *MatrixFrontend) AskHumanHTTPHandler() http.HandlerFunc

AskHumanHTTPHandler returns an http.HandlerFunc for POST /ask/human.

func (*MatrixFrontend) ClearTracking

func (f *MatrixFrontend) ClearTracking(_ context.Context, agentName string) error

ClearTracking clears the tracked inbound event ID for an agent. Called after the agent responds to prevent stale reactions on old messages.

func (*MatrixFrontend) RegisterCommands

func (f *MatrixFrontend) RegisterCommands(commands []Command) error

RegisterCommands stores bot commands for /help and skill dispatch. Matrix has no native /setMyCommands equivalent, so we only store locally.

func (*MatrixFrontend) SendNotification

func (f *MatrixFrontend) SendNotification(ctx context.Context, text string) error

SendNotification sends a system notification to the configured notification room. If no notification client is configured, logs a warning and returns nil (not an error).

func (*MatrixFrontend) SendText

func (f *MatrixFrontend) SendText(ctx context.Context, agentName, text string) error

SendText sends a text message to an agent's Matrix room. Long messages are split at natural boundaries to stay within the 65535-byte limit.

func (*MatrixFrontend) SendVoice

func (f *MatrixFrontend) SendVoice(_ context.Context, _ string, _ []byte) error

SendVoice is a no-op stub — Phase 4 will implement voice message uploads.

func (*MatrixFrontend) SetReaction

func (f *MatrixFrontend) SetReaction(ctx context.Context, agentName string, emoji string) error

SetReaction sends an emoji reaction on the last tracked inbound message for an agent. Matrix reactions are additive (each call adds a new reaction, unlike Telegram which replaces).

func (*MatrixFrontend) Start

func (f *MatrixFrontend) Start(ctx context.Context) error

Start begins polling/syncing for inbound messages for each configured agent and starts the notification client sync with command handlers.

func (*MatrixFrontend) Stop

func (f *MatrixFrontend) Stop(_ context.Context) error

Stop gracefully shuts down all sync loops.

type TelegramConfig

type TelegramConfig struct {
	TeamName   string
	MCfg       *config.DaemonConfig
	OnMessage  InboundHandler
	MsgSvc     *message.Service
	UserNameFn func() string // returns human display name for this team
	GetUsageFn func() string // returns formatted usage string, or "" if unavailable
	RestartFn  func() error  // triggers daemon restart (launchctl kickstart -k)
}

TelegramConfig holds construction parameters for TelegramFrontend.

type TelegramFrontend

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

TelegramFrontend implements Frontend using the Telegram Bot API.

func NewTelegram

func NewTelegram(cfg TelegramConfig) *TelegramFrontend

NewTelegram creates a TelegramFrontend. RegisterCommands must be called before Start.

func (*TelegramFrontend) AskHuman

func (f *TelegramFrontend) AskHuman(
	_ context.Context, agentName, question string, options []string,
) (string, bool, error)

AskHuman implements the direct server-side ask-human call.

func (*TelegramFrontend) AskHumanHTTPHandler

func (f *TelegramFrontend) AskHumanHTTPHandler() http.HandlerFunc

AskHumanHTTPHandler returns an http.HandlerFunc for POST /ask/human. daemon.go wires this into httpHandlers.askHuman.

func (*TelegramFrontend) ClearTracking

func (f *TelegramFrontend) ClearTracking(_ context.Context, agentName string) error

ClearTracking clears the tracked inbound message for an agent. Called after the agent responds to prevent stale reactions on old messages.

func (*TelegramFrontend) RegisterCommands

func (f *TelegramFrontend) RegisterCommands(commands []Command) error

RegisterCommands stores the command list and calls Telegram setMyCommands for all agent bot tokens and the notification bot token in this team. Returns an aggregated error if any bot fails — callers may treat this as non-fatal.

func (*TelegramFrontend) SendNotification

func (f *TelegramFrontend) SendNotification(_ context.Context, text string) error

SendNotification sends a system notification to this team's notification channel.

func (*TelegramFrontend) SendText

func (f *TelegramFrontend) SendText(_ context.Context, agentName string, text string) error

SendText sends a text message to an agent's Telegram chat.

func (*TelegramFrontend) SendVoice

func (f *TelegramFrontend) SendVoice(_ context.Context, _ string, _ []byte) error

SendVoice is not yet implemented for Telegram outbound (daemon → human).

func (*TelegramFrontend) SetReaction

func (f *TelegramFrontend) SetReaction(_ context.Context, agentName string, emoji string) error

SetReaction sets an emoji reaction on the last tracked inbound message for an agent.

func (*TelegramFrontend) Start

func (f *TelegramFrontend) Start(ctx context.Context) error

Start begins Telegram polling for agents in this team. RegisterCommands must be called before Start.

func (*TelegramFrontend) StartNotificationPoller

func (f *TelegramFrontend) StartNotificationPoller(ctx context.Context) error

StartNotificationPoller starts a poller for this team's notification bot token. The notification bot handles /status, /usage, /restart, /help. Skips if the notification token is shared with an agent bot (already polled).

func (*TelegramFrontend) Stop

Stop shuts down the frontend. Calling Stop after ctx cancellation is a no-op.

Jump to

Keyboard shortcuts

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