push

package
v0.15.40 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: Apache-2.0 Imports: 32 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

This section is empty.

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 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 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 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)
}

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

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"`
}

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 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) 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) TagPrefix

func (s *Sender) TagPrefix() string

TagPrefix starts the tags of this deployment's notifications.

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. A device subscribing again replaces its earlier subscription; an unchanged record is not written again (every page load registers).

func (*Store) DeviceEndpoints

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

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

func (*Store) MarkHidden

func (s *Store) MarkHidden(deviceID string)

MarkHidden: the device hid the app.

func (*Store) MarkVisible

func (s *Store) MarkVisible(deviceID string)

MarkVisible: the device shows the app now.

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"`
}

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 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