Documentation
¶
Overview ¶
Package notify sends notifications to offline push channels: an ntfy topic and/or a generic webhook. Sending is asynchronous through a small bounded queue; nothing here ever carries terminal output (callers only pass wire.NotificationData, whose body is a fixed phrase). Every push is titled with the host it comes from, and the last failure of each channel is kept (on disk, across restarts) until a push to it succeeds. On ntfy, the notifications of one session share a sequence ID: a newer one replaces the older, and Clear dismisses it.
Index ¶
- Constants
- func ClickURL(clickBase, hostID, sessionID string) string
- func Enabled(cfg wire.NotifyConfig) bool
- func Fold(ns []wire.NotificationData, lang string) wire.NotificationData
- func LoadFailures(path string) (map[string]wire.NotifyFailure, error)
- func SequenceID(sessionID string) string
- func Text(lang string, p Phrase, args ...any) string
- func Wants(cfg wire.NotifyConfig, n wire.NotificationData) bool
- type Digester
- type Phrase
- type Sender
- func (s *Sender) Clear(cfg wire.NotifyConfig, sessionID string) bool
- func (s *Sender) Close()
- func (s *Sender) LastErrors() map[string]wire.NotifyFailure
- func (s *Sender) Push(ctx context.Context, cfg wire.NotifyConfig, n wire.NotificationData) error
- func (s *Sender) Send(cfg wire.NotifyConfig, n wire.NotificationData) bool
Constants ¶
const Timeout = 5 * time.Second
Timeout bounds one HTTP request.
Variables ¶
This section is empty.
Functions ¶
func ClickURL ¶ added in v0.4.0
ClickURL is the link a push of sessionID (may be "") opens: clickBase with h=<hostID> and, for a session, s=<sessionID> added to its query. It is "" — no link — when clickBase or hostID is empty or clickBase is not an absolute URL. Nothing else goes into the link.
func Enabled ¶
func Enabled(cfg wire.NotifyConfig) bool
Enabled reports whether cfg has any channel to send to.
func Fold ¶
func Fold(ns []wire.NotificationData, lang string) wire.NotificationData
Fold combines notifications into one. A single notification is returned unchanged; several become reason "digest" with Count set, level warn if any was a warning, titled in lang.
func LoadFailures ¶ added in v0.4.0
func LoadFailures(path string) (map[string]wire.NotifyFailure, error)
LoadFailures reads a last failures file (Sender). A missing file is no failure; the map is never nil, even with an error.
func SequenceID ¶ added in v0.4.0
SequenceID is the ntfy sequence ID of session sessionID's notifications: the id itself when ntfy accepts it (1–64 of [A-Za-z0-9_-]; stagent's 16 hex characters always are), else a hash of it that is.
func Text ¶ added in v0.4.0
Text is phrase p in lang (English for an unknown language), formatted with args.
func Wants ¶ added in v0.4.0
func Wants(cfg wire.NotifyConfig, n wire.NotificationData) bool
Wants reports whether the reasons of cfg (with defaults applied) push n: an exited notification is abnormal when its level is warn.
Types ¶
type Digester ¶
type Digester struct {
// contains filtered or unexported fields
}
Digester collects the notifications arriving within one window: the first notification opens a window, and when it closes the collected notifications are flushed together (the caller folds them, see Fold).
func NewDigester ¶
func NewDigester(flush func([]wire.NotificationData)) *Digester
NewDigester calls flush for every closed window that still holds notifications, from a timer goroutine.
func (*Digester) Add ¶
func (d *Digester) Add(n wire.NotificationData, window time.Duration)
Add queues n into the current window, opening one of the given length if none is open.
type Phrase ¶ added in v0.4.0
type Phrase int
Phrase is one of the fixed texts stagent writes into notifications. Program output never becomes a phrase.
const ( PhraseWaitingInput Phrase = iota PhraseTurnComplete // PhraseNeedsApproval // followed by ": <tool>" when known PhraseExited // PhraseExitedCode // %d: exit code PhraseSessionLost // PhraseTerminal // push body of a program's own notification PhraseBell // in-app body of a bare BEL PhraseTest // notify.test // Digest titles (%d: count) and the line closing a long digest body. PhraseDigest PhraseDigestTurnComplete PhraseDigestNeedsApproval PhraseDigestWaitingInput PhraseDigestExited PhraseDigestMore )
type Sender ¶
type Sender struct {
// contains filtered or unexported fields
}
Sender delivers notifications to the enabled channels of a config.
func NewSender ¶
NewSender returns a Sender keeping the last failure per channel in the file failures (loaded now, rewritten when it changes) and linking ntfy pushes to hostID (see ClickURL). logf (may be nil) receives delivery errors.
func (*Sender) Clear ¶ added in v0.4.0
func (s *Sender) Clear(cfg wire.NotifyConfig, sessionID string) bool
Clear queues the dismissal of session sessionID's ntfy notification (ntfy's clear: marked read and removed from the notification drawer), behind the pushes queued before it. Webhooks get nothing.
func (*Sender) Close ¶
func (s *Sender) Close()
Close stops the worker after the queued pushes (each bounded by Timeout).
func (*Sender) LastErrors ¶ added in v0.4.0
func (s *Sender) LastErrors() map[string]wire.NotifyFailure
LastErrors returns the last failed push per channel (never nil).
func (*Sender) Push ¶
func (s *Sender) Push(ctx context.Context, cfg wire.NotifyConfig, n wire.NotificationData) error
Push sends n, titled with the host (labelled), to every enabled channel of cfg synchronously and records each channel's outcome (LastErrors). notify.test reports its error, which never contains the ntfy topic or the webhook URL.
func (*Sender) Send ¶
func (s *Sender) Send(cfg wire.NotifyConfig, n wire.NotificationData) bool
Send queues n for delivery to cfg's enabled channels without blocking. It reports false when the queue is full (the push is dropped) or the sender is closed.