push

package
v0.16.521 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: AGPL-3.0 Imports: 35 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 push sends the web push notifications of the runtime: a finished turn or a question of an agent, to the person's subscribed browsers (RFC 8030 delivery, RFC 8291 message encryption, RFC 8292 VAPID). The subscriptions and the key pair are files in the data folder, shared with the page's subscribe route.

Index

Constants

View Source
const SubscriptionsFile = "push-subscriptions.json"

SubscriptionsFile is the browsers' push subscriptions in the data folder.

View Source
const TTL = 6 * time.Hour

TTL is how long a push service keeps an undelivered notification.

View Source
const VAPIDFile = "vapid-keys.json"

VAPIDFile is the key pair's file in the data folder.

View Source
const VisibilityTTL = 60 * time.Second

VisibilityTTL: a device that has not said it is visible for this long counts as not looked at.

Variables

View Source
var ErrNoDevice = errors.New("no-device")

ErrNoDevice: the selector names no subscribed device.

Functions

func Clip

func Clip(text string, max int) string

Clip flattens whitespace and cuts text to max UTF-16 units with an ellipsis.

func Decrypt

func Decrypt(body []byte, uaPrivate *ecdh.PrivateKey, authSecret []byte) ([]byte, error)

Decrypt is the user agent's side, for tests and a fake push service: it opens an aes128gcm body with the subscription's private key and auth secret.

func DeviceName

func DeviceName(ua string) string

DeviceName is a device as the person knows it, from its browser's User-Agent: «iPhone · Safari», «Android · Chrome», «Mac · Chrome». Empty when the agent says nothing.

func Encrypt

func Encrypt(plaintext, uaPublic, authSecret []byte) ([]byte, error)

Encrypt encrypts a message for a subscription with a fresh sender key and salt.

func EncryptWith

func EncryptWith(plaintext, uaPublic, authSecret []byte, asPrivate *ecdh.PrivateKey, salt []byte) ([]byte, error)

EncryptWith is RFC 8291 message encryption with a given sender key and salt (the test vector's); Encrypt draws them. uaPublic is the subscription's p256dh key (65 bytes, uncompressed), authSecret its auth secret (16 bytes). The result is the aes128gcm body: salt, record size, key id (the sender's public key) and one record.

func FullAnswer

func FullAnswer(e status.Entry) (string, bool)

FullAnswer is the whole text of the answer a finished turn's notification is about, as the transcript's tail has it; ok is false for a turn that failed or left no answer.

func StripMarkdown

func StripMarkdown(text string) string

StripMarkdown is an answer as plain text for a notification: code blocks dropped, links and emphasis unwrapped, headings, quotes and list marks removed, everything on one line.

func Topic

func Topic(tag string) string

Topic is the Topic header of a tag: a hex digest, as Apple rejects "-" and "_" although RFC 8030 allows them.

Types

type Action

type Action struct {
	Action string `json:"action"`
	Title  string `json:"title"`
}

Action is a button of a notification.

type As

type As struct {
	TabID, WorkspaceID, Title string
	AgentSessionID            *string
	// From is the title of the subchat the notification is about.
	From string
}

As is the chat a delegated chat's notification speaks for: the root of its delegation, where the person sits The tap leads there; the words say which subchat it is.

type Config

type Config struct {
	// Dir is the data folder holding the subscriptions and the key pair.
	Dir string
	// Subject is the VAPID subject: a reachable https: or mailto: URL (Apple refuses others).
	Subject string
	// TagPrefix starts a notification's tag (prefix + tab id): a newer push of a conversation replaces the older.
	TagPrefix string
	Client    *http.Client
	Now       func() time.Time
	Log       func(level, msg string, kv ...any)
	// Summary writes the words of a finished turn's notification with a model the person picked for it; nil, or one
	// that is not picked, leaves the notification as BuildMessage wrote it.
	Summary Summarizer
}

Config is what the sender needs; the deployment names the product parts.

type Device

type Device struct {
	ID         string `json:"id"`
	Name       string `json:"name"`
	Mobile     bool   `json:"mobile"`
	CreatedAt  string `json:"createdAt,omitempty"`
	LastSeen   string `json:"lastSeen,omitempty"`
	LastActive string `json:"lastActive,omitempty"`
	// Visible: the person looks at the app on it now.
	Visible bool `json:"visible"`
	// Last: the device the person used most recently.
	Last bool `json:"last,omitempty"`
}

Device is a subscribed device as the agent and the page see it: never its endpoint or keys.

type DeviceInfo

type DeviceInfo struct {
	// UserAgent names the device (DeviceName).
	UserAgent string
	// Via is the ?machine= the page reached the runtime through.
	Via string
}

DeviceInfo is what the subscribe request says of its device besides its id.

type Message

type Message struct {
	Kind              string  `json:"kind"`
	Title             string  `json:"title"`
	Body              string  `json:"body"`
	ConversationTitle string  `json:"conversationTitle"`
	Detail            string  `json:"detail"`
	Provider          string  `json:"provider"`
	TabID             string  `json:"tabId"`
	WorkspaceID       string  `json:"workspaceId"`
	AgentSessionID    *string `json:"agentSessionId"`
	Tag               string  `json:"tag"`
	At                int64   `json:"at"`
	// URL is where a tap leads (a pushed design board); absent for the others.
	URL string `json:"url,omitempty"`
	// Actions are the notification's buttons (an action waiting for the person's yes); a browser that shows none
	// (iOS) still opens the chat on a tap.
	Actions []Action `json:"actions,omitempty"`
	// Proposal and Token let the buttons answer it: the token is single-use and only for this proposal.
	Proposal string `json:"proposal,omitempty"`
	Token    string `json:"token,omitempty"`
	// Machine is the ?machine= the buttons' request goes through (Subscription.Via); Send fills it.
	Machine string `json:"machine,omitempty"`
	// Results are the words the service worker shows after a button: {approve: «Отправлено», …}.
	Results map[string]string `json:"results,omitempty"`
}

Message is what the page's service worker and its in-app toast render; the keys are in the order the page's writer put them.

func AsRoot

func AsRoot(msg Message, as As) Message

AsRoot is msg told from the root chat as says.

func BuildMessage

func BuildMessage(tabID string, e status.Entry, kind, tagPrefix string, now time.Time) Message

BuildMessage writes the notification of a finished turn ("done") or a question ("question"): the conversation's title, what the person has to do, and the words of the answer or the question from the transcript's tail.

type Sender

type Sender struct {
	Store *Store
	// contains filtered or unexported fields
}

Sender sends pushes and implements status.Pusher.

func New

func New(cfg Config) *Sender

New sends from cfg.Dir.

func (*Sender) PublicKey

func (s *Sender) PublicKey() (string, error)

PublicKey is the VAPID public key the page subscribes with (created with the pair when missing).

func (*Sender) Push

func (s *Sender) Push(tabID string, e status.Entry, kind string)

Push sends a turn's notification ("done" or "question") to the devices it is for, dropping subscriptions the push service says are gone (404, 410). It implements status.Pusher.

func (*Sender) PushAs

func (s *Sender) PushAs(tabID string, e status.Entry, kind string, as As)

PushAs sends a delegated chat's notification as the root's: the tap opens the root, its title is the root's, and the words start with the subchat's name. The tag stays the subchat's, so it does not take the place of the root's own.

func (*Sender) Send

func (s *Sender) Send(sub Subscription, msg Message) (int, error)

Send delivers one message and returns the push service's HTTP status (0 when it could not be reached), so the caller can drop a subscription that is gone.

func (*Sender) SendTo

func (s *Sender) SendTo(subs []Subscription, msg Message) int

SendTo delivers msg to exactly these subscriptions, dropping the ones the push service says are gone; it answers how many took it.

func (*Sender) TagPrefix

func (s *Sender) TagPrefix() string

TagPrefix starts the tags of this deployment's notifications.

func (*Sender) Wait

func (s *Sender) Wait()

Wait returns when every notification that waited for a summary went.

type Store

type Store struct {
	Dir string
	Now func() time.Time
	// contains filtered or unexported fields
}

Store reads and edits the subscriptions file and remembers which devices are on screen. Every writer of this process goes through one Store and its lock (the sender dropping a gone subscription, the page's subscribe route); a writer in another process (the previous runtime's routes, while their slice is off) replaces the file whole, as these do.

func (*Store) Add

func (s *Store) Add(endpoint string, keys any, deviceID string, mobile *bool) error

Add stores a browser's subscription ({endpoint, keys} as the browser gave them) with the device that made it.

func (*Store) AddDevice

func (s *Store) AddDevice(endpoint string, keys any, deviceID string, mobile *bool, info DeviceInfo) error

AddDevice stores a subscription as Add, with the device's name and the times it was seen. A device subscribing again replaces its earlier subscription and keeps when it was first stored and last active; an unchanged record is not written again within seenEvery (every page load registers).

func (*Store) ApprovalTargets

func (s *Store) ApprovalTargets(conv string) []Subscription

ApprovalTargets are the devices a proposal waiting for the person's yes goes to: none when the chat with its card is on a screen now; else the phone used most recently; no phone, every device.

func (*Store) DeviceEndpoints

func (s *Store) DeviceEndpoints() []any

DeviceEndpoints is {endpoint} of every stored subscription, in the file's order.

func (*Store) Devices

func (s *Store) Devices() []Device

Devices are the subscribed devices, the most recently used first.

func (*Store) Looking

func (s *Store) Looking(conv string) bool

Looking says whether some device shows the chat conv on screen now.

func (*Store) MarkHidden

func (s *Store) MarkHidden(deviceID string)

MarkHidden: the device hid the app.

func (*Store) MarkLooking

func (s *Store) MarkLooking(deviceID, conv string)

MarkLooking: the device shows the app now, with the chat conv open (empty: none, or the page did not say). The device's lastActive is kept in the file.

func (*Store) MarkVisible

func (s *Store) MarkVisible(deviceID string)

MarkVisible: the device shows the app now.

func (*Store) Pick

func (s *Store) Pick(selector string) ([]Subscription, error)

Pick are the subscriptions a selector names: "all", "last" (the device used most recently), "mobile" (the phone used most recently), or a device id.

func (*Store) Remove

func (s *Store) Remove(endpoint string) error

Remove drops the subscriptions of an endpoint (one the push service said is gone, or the page unsubscribed); the others are written back as they were.

func (*Store) Subscriptions

func (s *Store) Subscriptions() []Subscription

Subscriptions are every stored subscription; none when the file is missing or unreadable.

func (*Store) Visible

func (s *Store) Visible() map[string]bool

Visible are the devices seen on screen within VisibilityTTL.

type Subscription

type Subscription struct {
	Endpoint string `json:"endpoint"`
	Keys     struct {
		P256dh string `json:"p256dh"`
		Auth   string `json:"auth"`
	} `json:"keys"`
	DeviceID string `json:"deviceId,omitempty"`
	Mobile   *bool  `json:"mobile,omitempty"`
	// Name is the device as the person knows it, from its browser («iPhone · Safari»).
	Name string `json:"name,omitempty"`
	// Via is the machine the page reached this runtime through (its ?machine=): a notification's buttons answer the
	// same way. Empty when the page talks to the runtime directly.
	Via string `json:"via,omitempty"`
	// CreatedAt, LastSeen (the page last registered) and LastActive (the person last looked at the app on it), RFC 3339.
	CreatedAt  string `json:"createdAt,omitempty"`
	LastSeen   string `json:"lastSeen,omitempty"`
	LastActive string `json:"lastActive,omitempty"`
}

Subscription is one browser's push subscription, with the device that made it.

func SelectTargets

func SelectTargets(subs []Subscription, visible map[string]bool) []Subscription

SelectTargets: while someone has the app open on a device, only that device is notified (its page turns the push into a toast and the phone in the pocket stays quiet); nobody looking, every device is.

type Summarizer

type Summarizer interface {
	// Picked says whether the person picked a model for the summaries; false is «as now».
	Picked() bool
	// Summarize is one short line of what the agent did, from the text of its answer.
	Summarize(ctx context.Context, answer string) (string, error)
}

Summarizer is the background feature «notification summaries» (internal/bgmodel).

type VAPIDKeys

type VAPIDKeys struct {
	PublicKey  string `json:"publicKey"`
	PrivateKey string `json:"privateKey"`
}

VAPIDKeys are the application server's key pair as the file holds it: base64url without padding, the public key uncompressed (65 bytes), the private key the raw scalar (32 bytes). Every runtime of a person must use the same pair: a browser subscribes once, against one key.

func LoadOrCreateVAPID

func LoadOrCreateVAPID(dir string) (VAPIDKeys, error)

LoadOrCreateVAPID reads the key pair from dir, or creates and stores one when the file is missing or unreadable (0600, through a temporary file).

Jump to

Keyboard shortcuts

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