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
- Variables
- func Clip(text string, max int) string
- func Decrypt(body []byte, uaPrivate *ecdh.PrivateKey, authSecret []byte) ([]byte, error)
- func DeviceName(ua string) string
- func Encrypt(plaintext, uaPublic, authSecret []byte) ([]byte, error)
- func EncryptWith(plaintext, uaPublic, authSecret []byte, asPrivate *ecdh.PrivateKey, ...) ([]byte, error)
- func FullAnswer(e status.Entry) (string, bool)
- func StripMarkdown(text string) string
- func Topic(tag string) string
- type Action
- type As
- type Config
- type Device
- type DeviceInfo
- type Message
- type Sender
- func (s *Sender) PublicKey() (string, error)
- func (s *Sender) Push(tabID string, e status.Entry, kind string)
- func (s *Sender) PushAs(tabID string, e status.Entry, kind string, as As)
- func (s *Sender) Send(sub Subscription, msg Message) (int, error)
- func (s *Sender) SendTo(subs []Subscription, msg Message) int
- func (s *Sender) TagPrefix() string
- func (s *Sender) Wait()
- type Store
- func (s *Store) Add(endpoint string, keys any, deviceID string, mobile *bool) error
- func (s *Store) AddDevice(endpoint string, keys any, deviceID string, mobile *bool, info DeviceInfo) error
- func (s *Store) ApprovalTargets(conv string) []Subscription
- func (s *Store) DeviceEndpoints() []any
- func (s *Store) Devices() []Device
- func (s *Store) Looking(conv string) bool
- func (s *Store) MarkHidden(deviceID string)
- func (s *Store) MarkLooking(deviceID, conv string)
- func (s *Store) MarkVisible(deviceID string)
- func (s *Store) Pick(selector string) ([]Subscription, error)
- func (s *Store) Remove(endpoint string) error
- func (s *Store) Subscriptions() []Subscription
- func (s *Store) Visible() map[string]bool
- type Subscription
- type Summarizer
- type VAPIDKeys
Constants ¶
const SubscriptionsFile = "push-subscriptions.json"
SubscriptionsFile is the browsers' push subscriptions in the data folder.
const TTL = 6 * time.Hour
TTL is how long a push service keeps an undelivered notification.
const VAPIDFile = "vapid-keys.json"
VAPIDFile is the key pair's file in the data folder.
const VisibilityTTL = 60 * time.Second
VisibilityTTL: a device that has not said it is visible for this long counts as not looked at.
Variables ¶
var ErrNoDevice = errors.New("no-device")
ErrNoDevice: the selector names no subscribed device.
Functions ¶
func Decrypt ¶
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 ¶
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 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 ¶
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 ¶
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.
Types ¶
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 BuildMessage ¶
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 (*Sender) PublicKey ¶
PublicKey is the VAPID public key the page subscribes with (created with the pair when missing).
func (*Sender) Push ¶
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 ¶
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.
type Store ¶
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 ¶
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 ¶
DeviceEndpoints is {endpoint} of every stored subscription, in the file's order.
func (*Store) MarkHidden ¶
MarkHidden: the device hid the app.
func (*Store) MarkLooking ¶
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 ¶
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 ¶
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.
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 ¶
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 ¶
LoadOrCreateVAPID reads the key pair from dir, or creates and stores one when the file is missing or unreadable (0600, through a temporary file).