notify

package
v1.0.41 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 13 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 notify is Aviary's notification service (#1215): event sources emit events to it, it checks the user's notification settings, de-duplicates and rate-limits them, keeps a short history for the web UI's bell, and pushes each one to connected clients, or to the OS when none is connected.

Index

Constants

View Source
const (
	SeverityInfo    = "info"
	SeveritySuccess = "success"
	SeverityWarning = "warning"
	SeverityError   = "error"
)

The severities of a notification.

View Source
const (
	// HistoryLimit is how many notifications the history keeps.
	HistoryLimit = 100
	// DedupeWindow is how long a repeat of the same event merges into it.
	// Failures with a Source use the repeats settings' window instead.
	DedupeWindow = time.Minute
	// RateWindow is the window the rate limit counts deliveries in.
	RateWindow = 30 * time.Second
	// RateLimit caps one event type's deliveries per RateWindow; the rest
	// are grouped into one.
	RateLimit = 3
)
View Source
const (
	TypeChatReply       = "chat.reply"
	TypeCodeLoopFailed  = "codeloop.failed"
	TypeCodeLoopBlocked = "codeloop.blocked"
	TypeCodeLoopDone    = "codeloop.done"
	// TypeCodeLoopScheduledDone is a scheduled project task's run finishing,
	// kept apart from TypeCodeLoopDone so frequent schedules do not bury
	// issue jobs and merges (#1720). A failed scheduled run is still
	// TypeCodeLoopFailed.
	TypeCodeLoopScheduledDone = "codeloop.scheduled_done"
	TypeTaskRun               = "task.run"
	TypeWorkflowFailed        = "workflow.failed"
	TypeScriptFailing         = "task.script_failing"
	TypeCustom                = "custom"
	// TypeTest is the settings page's Test notification. It has no settings
	// of its own and reaches every enabled delivery method.
	TypeTest = "test"
)

The event types a notification can have. Each has its own settings.

View Source
const (
	SectionChats     = "chats"
	SectionCodeLoop  = "codeloop"
	SectionTasks     = "tasks"
	SectionWorkflows = "workflows"
	SectionSystem    = "system"
)

The sections of Aviary events come from.

View Source
const (
	DeliveryBig    = "big"
	DeliverySmall  = "small"
	DeliverySystem = "system"
)

The delivery styles an event type can have.

View Source
const (
	MaxRepeatWindowMinutes = 7 * 24 * 60
	MinStreakCount         = 2
	MaxStreakCount         = 100
)

The bounds of the repeats settings.

View Source
const MaxBodyBytes = 2000

MaxBodyBytes caps the body of a notification an event source builds from run output.

View Source
const SoundNone = "none"

SoundNone silences one event type.

Variables

View Source
var EventTypes = []EventType{
	{TypeChatReply, SectionChats, "Agent replied in a chat you're not viewing", store.NotificationEvent{Enabled: true, Delivery: DeliverySmall}},
	{TypeCodeLoopFailed, SectionCodeLoop, "Code Loop job failed", store.NotificationEvent{Enabled: true, Delivery: DeliveryBig}},
	{TypeCodeLoopBlocked, SectionCodeLoop, "Code Loop job blocked or waiting on you", store.NotificationEvent{Enabled: true, Delivery: DeliveryBig}},
	{TypeCodeLoopDone, SectionCodeLoop, "Code Loop issue job done or pull request merged", store.NotificationEvent{Enabled: false, Delivery: DeliverySmall}},
	{TypeCodeLoopScheduledDone, SectionCodeLoop, "Scheduled project task run finished", store.NotificationEvent{Enabled: false, Delivery: DeliverySmall}},
	{TypeTaskRun, SectionTasks, "Task or workflow with the Notify target finished", store.NotificationEvent{Enabled: true, Delivery: DeliverySmall}},
	{TypeWorkflowFailed, SectionWorkflows, "Workflow failed", store.NotificationEvent{Enabled: true, Delivery: DeliverySmall}},
	{TypeScriptFailing, SectionTasks, "Precompiled script of a task or workflow started failing", store.NotificationEvent{Enabled: true, Delivery: DeliverySmall}},
	{TypeCustom, SectionSystem, "Sent by an agent or the notify API", store.NotificationEvent{Enabled: true, Delivery: DeliverySmall}},
}

EventTypes lists every configurable event type, in settings order.

View Source
var Sounds = []string{"chime", "bell", "ping", "pop", "marimba", "alert"}

Sounds are the sound types the web UI bundles.

Functions

func Clip

func Clip(text string, limit int) string

Clip shortens text to limit bytes on a rune boundary.

func DefaultRepeats added in v0.7.75

func DefaultRepeats() store.NotificationRepeats

DefaultRepeats are the default handling of repeating failures: repeats within six hours merge silently into one entry, and five failures in a row notify once.

func DefaultSettings

func DefaultSettings() store.NotificationPreferences

DefaultSettings are a new user's notification settings: on, with in-app and system delivery, and sound off.

func Normalize

Normalize fills what prefs leaves out with the defaults and drops values the web UI does not offer. Nil prefs are the defaults.

func ReadSettings

func ReadSettings() (store.NotificationPreferences, error)

ReadSettings loads the user's notification settings.

func SystemNotifier

func SystemNotifier(baseURL func() string) func(Notification) error

SystemNotifier returns how a server shows a notification through the OS when no browser is connected, or nil where there is none. On macOS it uses terminal-notifier when installed, so clicking opens the linked page at baseURL, and otherwise osascript, whose Notification Center entry opens Script Editor. Banner or alert style is set per app in System Settings → Notifications.

func WriteSettings

WriteSettings saves the user's notification settings, normalized; nil restores the defaults.

Types

type Event

type Event struct {
	Type     string `json:"type"`
	Severity string `json:"severity,omitempty"`
	Title    string `json:"title"`
	Body     string `json:"body,omitempty"`
	// Link is the web UI path the notification opens, such as a chat, job
	// card, run or task.
	Link string `json:"link,omitempty"`
	// Section is where the event came from; the type's own section when
	// empty.
	Section string `json:"section,omitempty"`
	// Source names what keeps failing, such as `Task agent/name`, for a
	// failure that can repeat. Repeats of the same failure from the same
	// source are merged as the repeats settings say, and a run of failures
	// in a row notifies once. Only Aviary's own event sources set it.
	Source string `json:"-"`
	// PauseTask is the "<agent>/<task>" a streak notification offers to
	// pause.
	PauseTask string `json:"-"`
	// Force delivers the event even when its type is turned off, such as a
	// scheduled task set to always notify (#1720). Notifications turned off
	// altogether still win. Only Aviary's own event sources set it.
	Force bool `json:"-"`
}

Event is what a source emits.

func Sanitize

func Sanitize(e Event) Event

Sanitize limits an event sent through the notify endpoint or tool to the custom type, or the test one, with a known severity, clipped text, and a link inside the web UI.

type EventType

type EventType struct {
	Type    string `json:"type"`
	Section string `json:"section"`
	Label   string `json:"label"`
	// Default is the type's setting for a new user.
	Default store.NotificationEvent `json:"default"`
}

EventType describes one configurable event type.

type Notification

type Notification struct {
	ID       string    `json:"id"`
	Type     string    `json:"type"`
	Severity string    `json:"severity"`
	Title    string    `json:"title"`
	Body     string    `json:"body,omitempty"`
	Link     string    `json:"link,omitempty"`
	Section  string    `json:"section"`
	Time     time.Time `json:"time"`
	Read     bool      `json:"read"`
	// Count is how many events the notification stands for: more than one
	// for repeats of the same event or a grouped burst.
	Count int `json:"count"`
	// Delivery is the event type's style, and Sound the sound to play, ""
	// for none.
	Delivery string `json:"delivery"`
	Sound    string `json:"sound,omitempty"`
	// Quiet marks a notification that arrived during quiet hours: it is in
	// the history, but nothing is shown or played.
	Quiet bool `json:"quiet,omitempty"`
	// PauseTask is the "<agent>/<task>" the notification's Pause action
	// pauses, set on a notification about a task failing again and again.
	PauseTask string `json:"pauseTask,omitempty"`
}

Notification is an event as delivered and kept in the history.

type Outcome

type Outcome string

Outcome says what Emit did with an event.

const (
	// Delivered events were added to the history and pushed.
	Delivered Outcome = "delivered"
	// Merged events repeated a recent one, whose count went up. Nothing
	// was shown or played.
	Merged Outcome = "merged"
	// Grouped events were held back by the rate limit and are delivered
	// together when the window ends.
	Grouped Outcome = "grouped"
	// Suppressed events were turned off by the master switch or their
	// type's setting.
	Suppressed Outcome = "suppressed"
)

The outcomes of Emit.

type Service

type Service struct {

	// Settings reads the user's notification settings.
	Settings func() (store.NotificationPreferences, error)
	// Publish pushes a new notification to connected clients, and Changed
	// tells them the history changed without a new one to show.
	Publish func(Notification)
	Changed func()
	// Connected reports whether any web UI client is connected, and System
	// shows a notification through the OS, for when none is.
	Connected func() bool
	System    func(Notification) error
	// Now and AfterFunc are the clock; tests replace them.
	Now       func() time.Time
	AfterFunc func(time.Duration, func())
	// contains filtered or unexported fields
}

Service is the notification service.

func New

func New() *Service

New returns a service reading the stored settings.

func (*Service) Clear

func (s *Service) Clear()

Clear empties the history.

func (*Service) Emit

func (s *Service) Emit(e Event) (Notification, Outcome)

Emit delivers e as the user's settings say.

func (*Service) History

func (s *Service) History() ([]Notification, int)

History returns the notifications, newest first, and how many are unread.

func (*Service) MarkRead

func (s *Service) MarkRead(ids []string)

MarkRead marks the notifications ids names read, or every one when ids is empty.

func (*Service) Recover added in v0.7.75

func (s *Service) Recover(source string)

Recover ends source's run of failures, of every type, after a success, so its next failure notifies again.

Jump to

Keyboard shortcuts

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