narrate

package
v0.167.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 10 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 narrate turns tool events into the human-readable phrases the Now screen shows ("editing main.go", "running go test — 2nd attempt") and derives a session's health badge and plan progress. Pure functions, unit-tested; the UI and any future TUI render what this package says. See .ai/04-ui.md.

Index

Constants

View Source
const (
	HealthWorking = "working"
	HealthIdle    = "idle"
	HealthWaiting = "waiting-on-you"
	HealthLooping = "looping"
	HealthError   = "error"
	HealthEnded   = "ended"
)

Health badges (spec: working / idle / waiting-on-you / looping? / error; done is in need.go).

View Source
const (
	// NudgeSubmit is Enter typed, or sent, at a session not showing a
	// permission menu: a message was submitted, or nothing was.
	NudgeSubmit = "submit"
	// NudgeApproved is a permission menu answered with an option that lets
	// the call run (a key in the terminal, or a button anywhere).
	NudgeApproved = "approved"
	// NudgeRejected is a permission menu answered No, or Esc / Ctrl+C at
	// it: Claude Code drops the call and returns to its prompt.
	NudgeRejected = "rejected"
)

Kinds of Nudge.

View Source
const (
	NeedPermission = "permission"
	NeedQuestion   = "question"
	NeedPlan       = "plan"
	NeedError      = "error"
	NeedAsks       = "asks"
	NeedDone       = "done"
)

Why a session needs its owner (Need.Reason), first match wins:

  • NeedPermission: a permission dialog is open. Certain.
  • NeedQuestion: AskUserQuestion's dialog is open. Certain.
  • NeedPlan: ExitPlanMode's dialog, a plan to approve. Certain.
  • NeedError: the turn ended on a StopFailure (a limit, an overload). Certain.
  • NeedAsks: a normal Stop whose last message asks something. A guess.
  • NeedDone: a normal Stop whose last message asks nothing. A guess; the session's Health is HealthDone, not waiting-on-you.
View Source
const HealthDone = "done"

HealthDone is a session whose turn ended with nothing asked of anyone: the last message Claude wrote reads as a report, not a question. It replaces waiting-on-you for those turns, so "Needs you", its counts, the badge and the OS notification are left to the turns that do want an answer (.ai/04-ui.md § Needs you and Done). Always a guess.

View Source
const NudgeWindow = 5 * time.Second

NudgeWindow is how long a NudgeSubmit reads working with no event behind it. A prompt Claude Code accepted fires UserPromptSubmit within milliseconds; Enter that submitted nothing (an empty prompt, a slash command's menu) is not work, and the status goes back.

Variables

This section is empty.

Functions

func AsksSomething added in v0.146.0

func AsksSomething(msg string) (ok bool, detail, line string)

AsksSomething reads the last message of a turn the way the Stop rule does (.ai/04-ui.md § Needs you and Done): it asks something when one of its last lines ends with "?", when it ends on a list of options, or when its last lines say "should I", "do you want", "which", "let me know" and the like. ok is false when it asks nothing. detail is "choice" or "question"; line is the line the answer rests on (the question, else the last line).

func BackgroundPhrase added in v0.94.0

func BackgroundPhrase(n int) string

BackgroundPhrase is what a session whose turn ended says while n of its subagents still work.

func LastMessage added in v0.146.0

func LastMessage(payload json.RawMessage) string

LastMessage is the Stop hook's last_assistant_message, "" when the payload has none (an older Claude Code).

func MainTurnEnded added in v0.94.0

func MainTurnEnded(events []event.Event, mainLast *event.Event) bool

MainTurnEnded reports whether the main thread's newest event is a top-level Stop: the parent finished its turn, whatever its subagents still do. mainLast, when given, is that event; otherwise it is found in events (oldest first).

func Ordinal

func Ordinal(n int) string

Ordinal renders 2 → "2nd", 3 → "3rd", 11 → "11th".

func Phrase

func Phrase(tool string, payload json.RawMessage) string

Phrase renders one tool call as a present-participle phrase.

func PromptNeed added in v0.146.0

func PromptNeed(tool string) string

PromptNeed is the reason a permission dialog gives: a question, a plan, or an approval.

func StopFailure added in v0.146.0

func StopFailure(payload json.RawMessage) string

StopFailure is why a turn failed, from a StopFailure (throttle) payload: "rate limit", "overloaded". "" when it does not say.

Types

type Activity

type Activity struct {
	Phrase string    `json:"phrase"`
	Tool   string    `json:"tool,omitempty"`
	At     time.Time `json:"at"`
	Health string    `json:"health"`
	Plan   *Plan     `json:"plan,omitempty"`
	// Repeats counts consecutive identical (tool, sample) calls ending at the latest event.
	Repeats int `json:"repeats,omitempty"`
	// Background is how many subagents are still working after the main
	// thread ended its turn. Claude Code resumes the parent by itself when
	// they finish, so nothing is wanted from the user and Health is working.
	// Zero otherwise.
	Background int `json:"background,omitempty"`
	// Provisional marks a status read from input Caprock typed into the
	// session rather than from an event the session wrote: Enter at a
	// waiting prompt reads working for NudgeWindow, and goes back to what
	// the events say when nothing follows it.
	Provisional bool `json:"provisional,omitempty"`
	// Need is why the session waits: set beside Health waiting-on-you
	// (Needs you) and done (Done), nil otherwise (need.go).
	Need *Need `json:"need,omitempty"`
	// FromInput is set when a Nudge decided the status. A caller that would
	// otherwise judge the session by how long its events have been quiet
	// (the session row's idle status) leaves it alone.
	FromInput bool `json:"-"`
}

Activity is the current-activity summary for a session.

func Summarize

func Summarize(events []event.Event, opt Options) Activity

Summarize derives the activity for a session from its most recent events (oldest first). It looks at the last tool.pre for the phrase, the last event kind for the health badge, and the last TodoWrite for plan progress.

type Move added in v0.146.0

type Move struct {
	// StopAt is the Ts of the Stop the owner looked at. A later Stop is a
	// new turn, and the move no longer applies.
	StopAt time.Time
	// Needs is true for "needs me?", false for "not needed".
	Needs bool
}

Move is the owner's correction of a guess, for one turn: the Stop it was made at, and which way. Kept in memory by the API (§ Needs you and Done).

type Need added in v0.146.0

type Need struct {
	Reason string `json:"reason"`
	// Detail qualifies the reason: for NeedError why the turn stopped ("rate
	// limit"); for NeedAsks "choice" when the message offers options or an
	// either/or, else "question".
	Detail string `json:"detail,omitempty"`
	// Text is the line of the last message the reading rests on: the
	// question for NeedAsks, the last line for NeedDone. Clipped.
	Text string `json:"text,omitempty"`
	// Guess says Caprock read it from the message's words: no dialog is
	// open, and Claude Code is only waiting for the next message.
	Guess bool `json:"guess,omitempty"`
	// Moved says the owner moved this turn by hand ("needs me?" or "not
	// needed"); it holds until the session's next turn.
	Moved bool `json:"moved,omitempty"`
}

Need is why a session waits, set beside Health waiting-on-you or done.

func StopNeed added in v0.146.0

func StopNeed(events []event.Event, stop event.Event) Need

StopNeed reads a main-thread Stop: NeedAsks when its last message asks something, NeedDone otherwise (an empty or missing message included), both guesses. A Stop that follows a StopFailure in the same turn is NeedError: Claude Code may fire both, and the failure is the news.

type Nudge added in v0.117.2

type Nudge struct {
	At   time.Time
	Kind string
}

Nudge is input to a session Caprock holds, seen as it was typed: what the user did that the session has not yet written an event for. Only a session Caprock started has one — every key and button reaches it through the daemon — and only the newest is kept.

type Options

type Options struct {
	Now          time.Time
	IdleAfter    time.Duration // no events for this long ⇒ idle (default 5m)
	Looping      bool          // an unexpired loop alert exists for the session
	SessionEnded bool
	// LiveSubagents is how many subagents are working in the session now
	// (store.LiveSubagents). With the main thread's turn ended, they make the
	// session "working in background", not waiting on anyone.
	LiveSubagents int
	// MainLast is the main thread's newest event, for a caller that had to
	// look it up because events holds none (a parent whose subagents logged
	// more than the window). Nil means "find it in events".
	MainLast *event.Event
	// Move is the owner's "needs me?" or "not needed" over a guessed turn;
	// it applies only to the Stop it names (need.go).
	Move *Move
	// Nudge is the newest input Caprock typed into the session (owned
	// sessions only). It decides the status only when it came after the
	// newest event, so the next event always wins.
	Nudge *Nudge
}

Options tune Summarize.

type Plan

type Plan struct {
	Done  int    `json:"done"`
	Total int    `json:"total"`
	Next  string `json:"next,omitempty"` // first in_progress or pending item
}

Plan is TodoWrite-derived progress.

Jump to

Keyboard shortcuts

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