hub

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 26 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 hub keeps one Claude Code session in a stream-json process that several channels (the phone, atc, WhatsApp, other sessions) talk into, and routes its replies back.

ws /ws?channel=<name>

in:  {"type":"message","text":...}  {"type":"note","text":...}  {"type":"interrupt"}
out: text{delta} tool{name} block{text} read joined held done{interrupted,subtype} error{error} notice{text}

ws /events gets every event of every channel, each with its channel.

Index

Constants

View Source
const (
	Answered = "answered"
	NoAnswer = "no-answer"
	Busy     = "busy"
	// Knocked is a call that found the user on the line with an agent: they were told on that call.
	Knocked = "knocked"
)
View Source
const (
	Phone = "phone"
	Agent = "agent"
	// Guest is someone other than the user writing in (a WhatsApp guest); like an agent's report,
	// the reply reaches no one.
	Guest = "guest"
	// GuestCall is a guest calling the assistant's line: spoken like the phone, but not the user.
	GuestCall = "guest-call"
)

Variables

View Source
var ErrBusy = errors.New("a turn is running")
View Source
var ErrNoPhone = errors.New("no phone line configured (ATC_CALL_LINE)")
View Source
var ErrRestarting = errors.New("claude is restarting, try again")

Functions

func NewID

func NewID() string

NewID is a new session id.

Types

type CallConfig

type CallConfig struct {
	URL     string
	Number  string
	Timeout time.Duration
	Poll    time.Duration
	AppAck  time.Duration
	HTTP    *http.Client
}

CallConfig is the call line the hub's session talks on and the one number it dials.

type CallReq

type CallReq struct {
	Reason   string `json:"reason"`
	From     string `json:"from,omitempty"`
	Notify   bool   `json:"notify,omitempty"`
	Timeout  int    `json:"timeout,omitempty"`
	WhatsApp bool   `json:"whatsapp,omitempty"`
	Via      string `json:"via,omitempty"`
}

type Client

type Client struct {
	URL  string
	HTTP *http.Client
}

Client is how atc talks to the hub.

func NewClient

func NewClient(url string) *Client

func (*Client) Call

func (c *Client) Call(ctx context.Context, r CallReq) (string, error)

func (*Client) Interrupt

func (c *Client) Interrupt(ctx context.Context, channel string) (bool, error)

func (*Client) Lines

func (c *Client) Lines(ctx context.Context) (out []Line, err error)

Lines is what agents' conversations the hub holds on calls right now.

func (*Client) Notice

func (c *Client) Notice(ctx context.Context) (bool, error)

func (*Client) Reset

func (c *Client) Reset(ctx context.Context) error

func (*Client) Send

func (c *Client) Send(ctx context.Context, channel, text string) (string, error)

Send is "sent", or "held" until the turn the phone is part of ends.

func (*Client) Status

func (c *Client) Status(ctx context.Context) (st Status, err error)

func (*Client) Watch

func (c *Client) Watch(ctx context.Context, fn func(Event)) error

Watch calls fn with every event of every channel until the connection drops.

type Config

type Config struct {
	Bin   string
	Args  []string
	Dir   string
	State string
	// Mode is the text that tells the session how to talk on a channel, "" for none.
	Mode func(channel string) string
	// News is what changed in atc since it was last asked, or with full how things stand; "" for nothing.
	News func(full bool) string

	Reset startover.Policy

	Call CallConfig
	Now  func() time.Time

	// Guests is the guests' own conversation, which takes the guest channels; nil keeps them here.
	Guests *Hub
	// GuestDesk is set on that conversation: the user never talks into it, so it is not told where they are.
	GuestDesk bool

	// OpenLine asks atc for an agent's conversation to take onto a call, LineMode is how those talk; see lines.go.
	OpenLine OpenLine
	LineMode func(channel string) string
}

type Event

type Event struct {
	Type        string `json:"type"`
	Channel     string `json:"channel,omitempty"`
	Delta       string `json:"delta,omitempty"`
	Name        string `json:"name,omitempty"`
	Text        string `json:"text,omitempty"`
	Error       string `json:"error,omitempty"`
	Interrupted bool   `json:"interrupted,omitempty"`
	Subtype     string `json:"subtype,omitempty"`
}

type Hub

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

func New

func New(cfg Config) *Hub

func (*Hub) Call

func (h *Hub) Call(ctx context.Context, r CallReq) (string, error)

Call has the user rung and waits for the outcome. Answered, the call's first message reaches the session with the reason in front of it, so it speaks first and knows why. Not answered, a Notify call puts that in front of the session on the channel it was busy with.

func (*Hub) Handler

func (h *Hub) Handler() http.Handler

Handler serves the hub. With a guests hub (cfg.Guests), what guests say goes to that one, on the same paths, and its own API is under /guests/.

func (*Hub) HasConversation added in v0.1.1

func (h *Hub) HasConversation() bool

HasConversation says whether this hub has ever talked to anyone. One that hasn't starts with its first message.

func (*Hub) Interrupt

func (h *Hub) Interrupt(ch string, force bool) bool

Interrupt stops the running turn. A channel that is not forcing it (the phone's barge-in) only cuts what is being said to it, and lets a running tool finish first so its result is kept. Claude Code replays a message only about two seconds after taking it, when its answer starts; a cut in that time (the user's turn reopened right after it was sent) stops the turn before it began.

func (*Hub) Left

func (h *Hub) Left(channel string)

Left is a channel's connection going away. A phone connection lives exactly as long as a call, so the next call's first message must carry the phone mode again even though the last message was also the phone's (the previous call's end note).

func (*Hub) Notice

func (h *Hub) Notice() (bool, error)

Notice is atc saying a session now needs the user. While a phone turn is running a tool, the change joins that turn right away, so the assistant can weigh it into what the user is about to hear; otherwise the next message carries it, since a call only speaks answers to the user's own words.

func (*Hub) Reset

func (h *Hub) Reset(why string) error

Reset starts the conversation over: the session writes a handover note in one last turn, and a new session starts with it as its first message.

func (*Hub) Say

func (h *Hub) Say(channel, text string) (string, error)

Say hands text from a channel to the session: "sent", or "held" until the running turn ends.

func (*Hub) Start

func (h *Hub) Start() error

func (*Hub) Status

func (h *Hub) Status() Status

func (*Hub) Stop

func (h *Hub) Stop()

Stop ends the claude process for good, for tests and shutdown.

func (*Hub) Subscribe

func (h *Hub) Subscribe(channel string) (<-chan Event, func())

Subscribe gets the events of one channel, or of all of them when channel is "".

func (*Hub) Watch

func (h *Hub) Watch(ctx context.Context, every time.Duration)

Watch applies the reset rule (cfg.Reset) every tick.

type Line

type Line struct {
	Name    string `json:"name"`
	Session string `json:"session"`
}

type LineSpec

type LineSpec struct {
	Session string   `json:"session"`
	Dir     string   `json:"dir"`
	Args    []string `json:"args"`
}

LineSpec is what atc answers to opening a line, beside what the talker uses (title, person, voices).

type OpenLine

type OpenLine func(name string) (int, []byte, error)

OpenLine is the agent's name going in, and atc's status and body coming out.

type Status

type Status struct {
	Session  string         `json:"session"`
	PID      int            `json:"pid"`
	Alive    bool           `json:"alive"`
	Context  int            `json:"context"`
	IdleS    int            `json:"idle_s"`
	Owners   []string       `json:"owners"`
	Route    string         `json:"route,omitempty"`
	Pending  []string       `json:"pending"`
	Held     []string       `json:"held"`
	Home     string         `json:"home,omitempty"`
	Call     string         `json:"call,omitempty"`
	OnLine   string         `json:"onLine,omitempty"`
	Channels map[string]int `json:"channels"`

	Reset     startover.Policy `json:"reset"`
	Dir       string           `json:"dir"`
	LastReset time.Time        `json:"lastReset,omitzero"`
}

Jump to

Keyboard shortcuts

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