Documentation
¶
Overview ¶
Package typist types into an agent's tmux pane: it reads the screen before any key, decides whether a message may go in now, pastes its pictures and its text, presses Enter and watches the text leave the input box. It owns the pane lock, one per pane for the whole process, that every writer of a pane takes — v2's outbox and Legacy alike — so two of them never type into the same box. A hold has a term and a wait for it has one too (panelock.go): Write is the way in for everything that is not a message.
The package keeps no state of messages: it answers what one try did, and the caller decides what comes next. It reaches tmux only through Pane, so the tests run on screens from testdata and a fake clock.
Index ¶
- Constants
- Variables
- func Answer(p Pane, c Clock, shapes []screen.QuestionShape, ...) error
- func ChatAbout(p Pane, c Clock) bool
- func CheckGoldens() []string
- func ClearOwn(p Pane, c Clock, provider wire.Provider, text string, pictures bool, ...) bool
- func CountImageRefs(text string) int
- func DrawnCommand(plain string) string
- func DrawnPlan(plain string) string
- func DrawnPlanFile(plain string) string
- func DrawsCommand(plain, summary, cwd string) bool
- func EscapePathForPrompt(path string) string
- func Expired() int64
- func HarnessLeftover(s Screen, provider wire.Provider) bool
- func Hold(ctx context.Context, pane string) (held context.Context, unlock func(), err error)
- func Interrupt(p Pane, c Clock) error
- func IsSlow(err error) bool
- func Lock(pane string) (unlock func())
- func LockContext(ctx context.Context, pane string) (unlock func(), err error)
- func Locks() int
- func NewImageRefs(before, now string) int
- func OptionLabel(line string) string
- func Pasting(plain string) bool
- func PathLeftover(s Screen, provider wire.Provider, paths []string) bool
- func PermissionModeNow(p Pane) (string, bool)
- func Pick(p Pane, c Clock, provider wire.Provider, want PickWant) error
- func Preempt(pane string) (held context.Context, unlock func())
- func SetEffort(p Pane, c Clock, effort wire.Effort) error
- func SetFast(p Pane, c Clock, on bool) error
- func SetModel(p Pane, c Clock, id string) error
- func SetPermissionMode(p Pane, c Clock, mode string) error
- func SetProfileModel(p Pane, c Clock, id, label string) error
- func SetUltracode(p Pane, c Clock, on bool) error
- func Shows(s Screen, want PickWant) error
- func StillInBox(s Screen, provider wire.Provider, text string) bool
- func SubmitsAsTypedLine(text string) bool
- func TryLock(pane string) (unlock func(), ok bool)
- func TypedLeftover(s Screen, provider wire.Provider, texts []string) string
- func Wipe(p Pane, c Clock, provider wire.Provider) bool
- func Write(ctx context.Context, server any, pane string, ...) error
- func WriteNow(pane string, write func(held context.Context) error) error
- type Binder
- type BusyError
- type Clock
- type Decision
- type DialogGrower
- type Golden
- type LineTyper
- type MenuKind
- type Message
- type NativeOwner
- type Outcome
- type Pane
- type PickWant
- type Plan
- type Prober
- type Responder
- type Result
- type Screen
- type SlowError
- type SystemClock
- type TallCapturer
Constants ¶
const ( // HoldLimit is longer than the longest honest hold: a message with pictures typed into a loaded agent takes tens of // seconds, a dialog driven by keys about as much. HoldLimit = 2 * time.Minute // WaitLimit is how long LockContext waits for a pane when its context names no sooner end. WaitLimit = 45 * time.Second // ProbeLimit is how long the terminal server may take to answer before anything is typed: a server slower than // this takes each key later than the typist's own waits, and the keys of one message arrive apart. ProbeLimit = 2 * time.Second )
The pane lock has two terms. A writer holds a pane for HoldLimit at most: past it the lock is let go for it, so a writer that hangs (a tmux that never answers, a wait that never ends) does not keep every other writer out for good. A writer waits for a pane for WaitLimit at most (or its context's end, whichever is first) and is told who is in the way, instead of hanging behind it.
const LongPromptText = 800
LongPromptText is the longest text Claude Code takes as typed; a longer typed burst reads as a paste too.
Variables ¶
var ( ErrHoldExpired = errors.New("typist: the hold of the pane ended at its term") ErrPreempted = errors.New("typist: the pane was taken for the person's key") )
ErrHoldExpired and ErrPreempted are why a writer's hold of a pane ended before the writer let it go: its term passed, or a writer that does not wait (the person's Stop) took the pane from it.
var ( // ErrNotAtPrompt: the agent works, asks, or shows no input line, so a command typed now would go to the wrong place. ErrNotAtPrompt = errors.New("typist: the agent is not at its prompt") // ErrInputOccupied: the box holds text that is not the command's. ErrInputOccupied = errors.New("typist: the input box holds other text") // ErrCommandDeclined: the agent asked to confirm the change and the confirmation was answered "keep". ErrCommandDeclined = errors.New("typist: the agent kept the previous value") // ErrCommandRefused: the agent answered it will not do it (a model that does not take ultracode). ErrCommandRefused = errors.New("typist: the agent refused the command") // ErrCommandNotApplied: the command was typed and the screen never said it took effect. ErrCommandNotApplied = errors.New("typist: the agent did not confirm the command") )
var ErrDialogStayed = errors.New("typist: the dialog is still on the screen")
ErrDialogStayed: the keys went in and the dialog is still on the screen.
var ErrMenuChanged = errors.New("typist: the menu on the screen is not the one decided on")
ErrMenuChanged: the menu on the screen is not the one the person decided on — another tool call asks, or the options differ — so no key was pressed.
var OnExpired func(pane string, held time.Duration)
OnExpired, when set, is told of a pane whose writer held it past HoldLimit: the lock was let go for it.
Functions ¶
func Answer ¶
func Answer(p Pane, c Clock, shapes []screen.QuestionShape, choices []screen.QuestionChoice) error
Answer types the answers of a question dialog, one group of keys per question, and waits for it to go.
func ChatAbout ¶
ChatAbout closes a question dialog by its "Chat about this" row; false when the row is not there or the dialog stayed.
func CheckGoldens ¶
func CheckGoldens() []string
CheckGoldens reads every reference screen and returns what the reader got wrong, one line per screen; nil when it reads them all.
func ClearOwn ¶
func ClearOwn(p Pane, c Clock, provider wire.Provider, text string, pictures bool, paths ...string) bool
ClearOwn empties Claude's box of text — what it holds must be a piece of text (with pictures: picture references of ours too), else nothing is pressed — row by row with ctrl+u, a reference ctrl+u leaves by Backspace, looking after every key: it stops as soon as the box holds anything that is not ours (the person started typing). Never while Claude still reads a paste of pictures: their references would land after the keys. True when the box ended empty. paths are picture paths of ours the box may hold besides (pasted, read late): they are cleared with the text.
func CountImageRefs ¶
CountImageRefs counts the `[Image #n]` references in a text, wrapped ones too.
func DrawnCommand ¶ added in v0.16.15
DrawnCommand is the shell command the permission dialog on a plain screen draws, its wrapped lines joined by newlines as drawn ("" when no command box is found). A gate made from the screen alone (the prompt of a background agent: no call of the chat's own is pending) shows it and checks the menu against it before a key goes in.
func DrawnPlan ¶ added in v0.16.86
DrawnPlan is the plan as Claude's plan dialog draws it, between its dashed rules; "" when they are off the screen.
func DrawnPlanFile ¶ added in v0.16.86
DrawnPlanFile is the name of the plan file Claude's plan dialog names under ~/.claude/plans; "" when it names none.
func DrawsCommand ¶
DrawsCommand: the permission dialog on the screen asks about exactly this shell command (a summary «$ cmd»), whole and alone in its box. False for a call whose summary is no shell command, and for a screen with no command box.
func EscapePathForPrompt ¶
EscapePathForPrompt puts a backslash before the characters an agent's prompt would read specially in a pasted path.
func HarnessLeftover ¶
HarnessLeftover: the box holds nothing but a setting command of the harness.
func Hold ¶
Hold is LockContext for a writer that types more than one key: held is the hold's context — it ends when the writer lets go, at HoldLimit (ErrHoldExpired) or when the pane is taken from it (ErrPreempted) — and the writer's keys are to be bound to it.
func Interrupt ¶
Interrupt presses Escape once; a key that did not go in is pressed again, interruptTries in all.
func IsSlow ¶
IsSlow says the error is a write put off because the terminal server answers too slowly.
func Lock ¶
func Lock(pane string) (unlock func())
Lock takes the pane's lock, waiting for the writer that holds it; unlock lets it go and is safe to call twice.
func LockContext ¶
LockContext takes the pane's lock like Lock, but waits no longer than ctx and WaitLimit let it: a *BusyError says the pane stayed with another writer, and nothing is held.
func NewImageRefs ¶
NewImageRefs counts the references on a screen that the earlier one did not have, reference by reference: older ones that scrolled away meanwhile do not hide new ones, and a number shown again after a reset counts once more.
func OptionLabel ¶
OptionLabel is a menu line's text without its number: "2. Yes, and don't ask again" → "Yes, and don't ask again".
func PathLeftover ¶
PathLeftover: the box holds nothing but picture paths the harness pasted (escaped as pasted, or bare; the box wraps them anywhere), with picture references among them or not — a paste the typist gave up on that landed later.
func PermissionModeNow ¶
PermissionModeNow is the mode Claude's footer says now, ok false when it cannot be read.
func Pick ¶
Pick presses the chosen option of a permission or a plan menu and waits for the menu to go. It looks at the screen first, under the lock the caller holds: a menu that changed since the person decided (another tool call, other options) gets no key, because the number would approve what they did not see.
func Preempt ¶
Preempt takes the pane at once, from whoever holds it: the holder's context ends with ErrPreempted — its keys stop there, the one under way is cut short — and the pane is the caller's. It is for the person's own key that must not wait behind a message being typed: Stop.
func SetEffort ¶
SetEffort types «/effort level» and waits for «Set effort level to level». Since Claude Code 2.1.310 a level leaves ultracode as it is.
func SetFast ¶
SetFast types «/fast on|off» and waits for «Fast mode ON» or «Fast mode OFF». A model or an account without the mode answers «Fast mode is not available …»: ErrCommandRefused.
func SetModel ¶
SetModel types «/model id» and waits for «Set model to»; Claude's «Switch model?» question is the person's consent already (the page asked), so its focused "Yes" is pressed.
func SetPermissionMode ¶
SetPermissionMode turns Claude's permission mode (screen.ClaudeMode*) with shift+tab, one press at a time, each read back off the footer under the box, until the footer says the mode asked for. A mode the cycle does not have (a Claude started without --allow-dangerously-skip-permissions has no bypass in it) is gone round to the mode it started in and ends in ErrCommandRefused, the agent left as it was; a press the footer does not show ends in ErrCommandNotApplied.
func SetProfileModel ¶
SetProfileModel checks the external model or its catalogue label. A reply for another gateway alias must never acknowledge this switch, even when both share an upstream ID.
func SetUltracode ¶
SetUltracode types «/effort ultracode on|off» and waits for «Ultracode on (…)» or «Ultracode off.». A model or a setup that cannot run it answers «Ultracode isn't available on …»: ErrCommandRefused.
func Shows ¶ added in v0.16.5
Shows reports whether the screen shows the menu the person decided on — the check Pick makes before it presses a key (ErrMenuChanged when it does not). The runtime asks it of an open gate as the screen changes, so a card whose menu cannot be matched says so at once and not after a press that did nothing.
func StillInBox ¶
StillInBox: the box still holds text, the message's start (Claude: the whole box, its wrapped lines and wrapped picture references too; Codex: its prompt box).
func SubmitsAsTypedLine ¶
SubmitsAsTypedLine: a line Claude Code takes as typed and submits on an Enter right behind it — short and single, not opening a mode with its first character (! shell, # memory, / command, & background) and not ending in an @ or / word, where Claude's completion list is open and would take the Enter.
func TryLock ¶
TryLock takes the pane's lock when nobody holds it; ok false otherwise, and nothing is held.
func TypedLeftover ¶ added in v0.16.24
TypedLeftover is the earlier message of ours the box holds what is left of (its paths and references aside): its start, or a piece of it pieceMin long at least. "" when the box is not that.
func Wipe ¶ added in v0.16.24
Wipe empties the box whatever it holds — only on the person's word («Очистить поле и отправить»), never by itself: Claude's row by row with ctrl+u (Backspace for a reference ctrl+u leaves), Codex's by ctrl+u. Never ctrl+c: in a working agent it interrupts the turn. True when the box ended empty.
func Write ¶
func Write(ctx context.Context, server any, pane string, write func(held context.Context) error) error
Write is how anything but a message puts keys into a pane: write runs with the pane's lock held, after the terminal server answered in time, and is given the hold's context to bind its keys to. server is what the keys go through; one that is a Responder is asked first, and a slow answer is a *SlowError with nothing typed. A pane of "" is nobody's: write runs as it is.
Types ¶
type Binder ¶
Binder is a Pane whose keys can be bound to a hold: the pane it returns types nothing once held has ended, and a key under way then is cut short.
type BusyError ¶
type BusyError struct {
Pane string
Waited time.Duration
// Cause is the context's error when it ended the wait, nil when WaitLimit did.
Cause error
}
BusyError is a pane another writer holds: the wait for it ended, nothing was typed.
type Decision ¶
type Decision string
Decision is whether a message may be typed now.
func Deliverable ¶
Deliverable decides from the screen: nothing readable or no input line, or a working agent → wait; a menu (it would take the keys as an answer) → blocked-menu; text in the box (it would go along with ours) → blocked-text; else send.
type DialogGrower ¶ added in v0.16.86
type DialogGrower interface {
GrowForDialog() bool
}
DialogGrower is a pane whose window can grow for a dialog that does not fit. GrowForDialog answers true when the window grew and the program had time to redraw.
type Golden ¶
Golden is one reference screen and what the reader must see on it ("" from Want: it does).
type LineTyper ¶ added in v0.15.46
LineTyper is a pane that can type a line's keys without its Enter: the typist then presses the Enter once the box has read the line to its end (a loaded Claude reads a burst of keys as a paste, slowly).
type MenuKind ¶
type MenuKind string
MenuKind is what the menu on a screen asks.
const ( NoMenu MenuKind = "" MenuQuestion MenuKind = "question" // Claude's AskUserQuestion dialog MenuPermission MenuKind = "permission" // a tool permission prompt MenuPlan MenuKind = "plan" // the plan approval prompt MenuOther MenuKind = "other" // any other numbered choice block (a picker, a trust dialog) )
type Message ¶
type Message struct {
Text string
Images []string
// Steer types into a working agent: its turn takes the text (Claude queues it until the next step). A menu or
// foreign text still stops it.
Steer bool
// HandOver: the running tool is one Claude can hand to the background (Bash, an MCP call), so a steered message
// goes in now by Claude's chord instead of waiting for the tool to end.
HandOver bool
// HandOverOK, when set, is asked right before the chord: the tool may have ended while the text was typed, and then
// the chord would cut the model's answer short instead of handing a call over. Nil: go ahead.
HandOverOK func() bool
// Agent: another agent wrote the message. A question the agent asks is the person's to answer: the message waits
// for the dialog to close and is never typed in as the reply.
Agent bool
// EnterOnly: a restart found the text in the box; only its Enter is pressed, and a box without it types nothing.
EnterOnly bool
// ClearOnly: the message is not to be sent but taken out of the box, where an earlier try left it (it stalled):
// Sent when the box no longer holds it, BlockedText when it could not be cleared, Wait when it was not there.
ClearOnly bool
// Leftovers are the picture paths the harness pasted for its messages (this one's and earlier ones'). Claude reads
// a big picture late: a paste the typist gave up on lands after it, as path text, and is ours to clear.
Leftovers []string
// Typed are the texts of the earlier messages the harness typed (not this one's): what is left of one in the box,
// its paths and references aside, is ours to clear.
Typed []string
// Wipe: the person said to clear the box and send («Очистить поле и отправить»): text in the box that is not ours
// is taken out too, row by row, and the message goes.
Wipe bool
}
Message is what one try types: pictures (absolute paths) before the text.
type NativeOwner ¶
type NativeOwner interface{ NativeAgentAlive() bool }
NativeOwner is checked immediately before each native write. A recognized input frame alone is never sufficient: a crashed CLI may have returned the pane to sh.
type Outcome ¶
type Outcome string
Outcome is how one try ended.
const ( // Sent: the text left the input box (or the screen could not be read to say otherwise) — not yet accepted. An // empty box is not delivery: the agent's prompt-submit hook or the message's entry in the transcript is, and the // caller waits for one of them. Sent Outcome = "sent" // Wait: no input line or a working agent; nothing was typed. Wait Outcome = "wait" // BlockedMenu, BlockedText: a menu or someone else's text; nothing was typed. BlockedMenu Outcome = "blocked-menu" BlockedText Outcome = "blocked-text" // Failed: a paste or a key did not go in, or a picture did not show up. Nothing reached the agent for sure: the // next try finds what did reach the box as its own and types only the rest. Failed Outcome = "failed" // Stuck: typed, Entered three times, still in the box. Stuck Outcome = "stuck" )
type Pane ¶
type Pane interface {
// CaptureStyled is the pane with its styles, Capture without; false when it cannot be read.
CaptureStyled() (string, bool)
Capture() (string, bool)
// Paste puts text in as one bracketed paste, without submitting it.
Paste(text string) error
// TypeLine types a short line as keys with its Enter right behind it.
TypeLine(text string) error
// Keys sends tmux key names in one go, in order.
Keys(keys ...string) error
}
Pane is one agent pane in tmux.
type PickWant ¶
type PickWant struct {
// N is the option's number (1-based) and Label its text without the number, as the screen showed them.
N int
Label string
// Plan: the menu is a plan's approval, not a tool's permission.
Plan bool
// Dialog: the menu is one no tool call named, known by its heading and options. A permission or a plan is never one:
// a label like «Yes» says nothing of what it approves.
Dialog bool
// Request is a dialog's heading, as the gate showed it.
Request string
CredentialPromptID string
// Command is the tool call the permission asks about ("" for a plan, or when the gate did not know it).
Command string
// Cwd is the session's working directory: the one a «cd <dir> &&» of the command may be left out for.
Cwd string
}
PickWant is the menu a person decided on and the option they chose, as the gate showed them.
type Plan ¶
type Plan struct {
Decision Decision
Landed int
OwnText bool
// Nothing: an Enter-only try finds the box without its text — there is nothing left to type.
Nothing bool
// ChatAbout: the question dialog is to be closed by «Chat about this» before the text goes in as the answer.
ChatAbout bool
// Dismiss: a screen the person left open in the terminal (/login, /model) is closed by Esc first.
Dismiss screen.Aside
// Clear: the box holds a setting command of the harness that did not go through, or picture paths the harness
// pasted that never became pictures, or what is left of an earlier message of ours; it is cleared first, with its
// picture references when ClearRefs.
Clear string
ClearRefs bool
// Wipe: the box holds text not of ours, and the message says to clear it all the same (Message.Wipe).
Wipe bool
}
Plan is what one look at the screen lets a message do: the decision, and what of the message the box holds already from an earlier try — Landed of its pictures, or its text (OwnText).
type Prober ¶
type Prober interface {
Responsive() error
}
Prober is a Pane that can say the same of itself; Type asks it before the first key.
type Responder ¶
Responder is a terminal server that can say whether it answers in time for a pane: nil when it does, an error when it took longer than ProbeLimit.
type Result ¶
type Result struct {
Outcome Outcome
Before Screen
// Typed: keys went in (Sent, Stuck, a Failed after the first key). Own: the box held this message's pictures or
// text from an earlier try, and only the rest was typed. ChatAbout: the question dialog was closed first.
Typed, ChatAbout, HandedOver bool
// Cleared: the screen the person left over the box (their /login, a picker) was closed by Esc first.
Cleared screen.Aside
Own bool
// AsPath: Claude did not show a picture as a reference, and the message went with its path as text.
AsPath bool
// Wiped: the box held text not of ours, and the person's word cleared it (Message.Wipe).
Wiped bool
// Slow: the terminal did not answer in ProbeLimit, and the try ended before its first key (Wait).
Slow bool
// Lost: the try's hold of the pane ended under it (TypeHeld): it stopped where it was, and whatever it typed is
// not a delivery.
Lost bool
Err error
}
Result is one try: its outcome, the screen it started from, and what it did on the way.
func Type ¶
Type tries one message once: it reads the screen, closes a question dialog by «Chat about this» when the text is the person's answer in words, decides whether it may type now, and types what the box does not hold yet. The caller holds the pane's lock.
func TypeChecked ¶
TypeChecked is Type for a CLI whose version the screen reading was checked against, and its cautious form for one it was not (cliversion.Cautious). On an unchecked version the reader may take one screen for another, so no key is pressed on the strength of a guess:
- a screen read as the person's left-open picker or as a question dialog is not closed (no Esc, no «Chat about this»): the message waits as blocked by a menu, and the person answers the card or the terminal;
- a box that reads empty after the Enter is not taken as proof that Claude queued the text: the chord that hands the running tool to the background is not pressed. Whether the agent took the message is said by its hook and its transcript, as for every message.
Everything else — what is typed, when it waits, how it is confirmed — is Type's.
func TypeHeld ¶
TypeHeld is TypeChecked (Type, or its cautious form for an unchecked CLI version) under the pane's lock: it takes the lock, binds the pane's keys to the hold and types. A hold that ended under the try (the person's Stop took the pane, or its term passed) ends the try there: nothing more is typed, and the result says Lost — the message was not delivered by this try and keeps its place.
type Screen ¶
type Screen struct {
Readable bool
Plain string
Busy bool
Menu MenuKind
// Panel: a TUI panel or picker is open over the box (Claude's /effort slider, its /model picker).
Panel bool
// Aside: one of those the person left open (Claude's /login, /model, /effort), which Esc closes safely.
Aside screen.Aside
Options []string // the menu's options as the screen shows them
// Request is what the menu asks, its heading on the screen.
Request string
// CredentialPromptID binds a local choice to the credential hint drawn in this prompt. It never leaves the runtime.
CredentialPromptID string
Input string
InputLine bool
Box string
Pictures int
// OnlyPictures: the box holds picture references and nothing else.
OnlyPictures bool
// Queued: Claude shows messages waiting in its own queue (a steered message not handed over yet).
Queued bool
// Word: the reply Claude is writing, off the screen, while it works (empty: none shown).
Word string
// contains filtered or unexported fields
}
Screen is one look at an agent's pane. Input is meaningful only with InputLine; Box is the whole input box (its wrapped lines too), Pictures the [Image #n] references in it, OnlyPictures that it holds nothing else.
func Look ¶
Look reads the pane; a pane that cannot be read is a Screen that is not Readable. A permission dialog too tall for the window has its top off the screen and out of the scrollback (Claude draws it on the alternate screen), so the call in it cannot be read whole: the pane grows when it can, and the look reads the dialog as redrawn.
func (Screen) PromptReady ¶
PromptReady: the agent shows an input line and is not working.
func (Screen) StartupPicker ¶
StartupPicker recognizes a menu or an active picker footer below the cursor. A stale footer higher in the terminal history does not consume an otherwise ready input box.
type SlowError ¶
SlowError is a terminal server that did not answer in ProbeLimit: nothing was typed, the write is to be tried later.
type SystemClock ¶
type SystemClock struct{}
SystemClock is the real clock.
func (SystemClock) Now ¶
func (SystemClock) Now() time.Time
func (SystemClock) Sleep ¶
func (SystemClock) Sleep(d time.Duration)
type TallCapturer ¶ added in v0.16.86
TallCapturer is a pane with a terminal attached, whose size it may change only for a moment: CaptureTall is the styled screen at a size a dialog fits in. short is the screen now, as the look read it; false leaves that as it is.