d1846441cc
Record every notification published to ntfy (run outbox, stage notify hooks, chat question/approval/reply) in a persisted app_notifications outbox and stream it to subscribe_notifications clients with an after cursor; clear_notifications and ack_attention clear across devices, as do runs leaving waiting, deleted runs and answered chat questions. Add the notify.ntfy_enabled setting (default on) to stop only the ntfy publish.
103 lines
3.6 KiB
Go
103 lines
3.6 KiB
Go
package protocol
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
)
|
|
|
|
// Notification settings are the operator's global choices about which events
|
|
// publish through the server's ntfy connection. A client reads them with
|
|
// {"type":"notification_settings"} and changes them with
|
|
// {"type":"set_notification_settings","notification_settings":{"<key>":bool}};
|
|
// a set may name any subset of keys, and unknown keys or non-boolean values
|
|
// are refused. Both replies have type "notification_settings" and carry every
|
|
// key with its stored value.
|
|
const (
|
|
GetNotificationSettings = "notification_settings"
|
|
SetNotificationSettings = "set_notification_settings"
|
|
NotificationSettingsReply = "notification_settings"
|
|
)
|
|
|
|
// Notification setting keys, as carried on the wire.
|
|
const (
|
|
NotifyRunFinished = "run_finished"
|
|
NotifyChatNeedsYou = "chat_needs_you"
|
|
NotifyChatReplyFinished = "chat_reply_finished"
|
|
NotifyNtfyEnabled = "ntfy_enabled"
|
|
)
|
|
|
|
// NotificationSettings is the typed form of the global notification choices.
|
|
type NotificationSettings struct {
|
|
// RunFinished makes every run end (success or failure) notify, not only
|
|
// runs started with notify mode "on". Off by default: an "auto" run then
|
|
// notifies only when it waits for a human decision, and "off" never does.
|
|
RunFinished bool
|
|
// ChatNeedsYou notifies when a chat asks a question or requests approval.
|
|
ChatNeedsYou bool
|
|
// ChatReplyFinished notifies when an assistant turn in a chat ends.
|
|
ChatReplyFinished bool
|
|
// NtfyEnabled publishes notifications to ntfy. On by default; when off the
|
|
// server still records and streams every notification to app clients
|
|
// (subscribe_notifications) but stops publishing to ntfy.
|
|
NtfyEnabled bool
|
|
}
|
|
|
|
// NotificationSettingKeys lists every setting key in a stable order.
|
|
var NotificationSettingKeys = []string{NotifyRunFinished, NotifyChatNeedsYou, NotifyChatReplyFinished, NotifyNtfyEnabled}
|
|
|
|
// DefaultNotificationSettings are the values before the operator changes any.
|
|
func DefaultNotificationSettings() NotificationSettings {
|
|
return NotificationSettings{RunFinished: false, ChatNeedsYou: true, ChatReplyFinished: false, NtfyEnabled: true}
|
|
}
|
|
|
|
// Map returns the wire form with every key present.
|
|
func (s NotificationSettings) Map() map[string]any {
|
|
return map[string]any{
|
|
NotifyRunFinished: s.RunFinished,
|
|
NotifyChatNeedsYou: s.ChatNeedsYou,
|
|
NotifyChatReplyFinished: s.ChatReplyFinished,
|
|
NotifyNtfyEnabled: s.NtfyEnabled,
|
|
}
|
|
}
|
|
|
|
// Field returns a pointer to the setting named key, or nil for an unknown key.
|
|
func (s *NotificationSettings) Field(key string) *bool {
|
|
switch key {
|
|
case NotifyRunFinished:
|
|
return &s.RunFinished
|
|
case NotifyChatNeedsYou:
|
|
return &s.ChatNeedsYou
|
|
case NotifyChatReplyFinished:
|
|
return &s.ChatReplyFinished
|
|
case NotifyNtfyEnabled:
|
|
return &s.NtfyEnabled
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ValidateNotificationSettingsUpdate checks a partial update: at least one key,
|
|
// every key known and every value a boolean.
|
|
func ValidateNotificationSettingsUpdate(update map[string]any) (map[string]bool, error) {
|
|
if len(update) == 0 {
|
|
return nil, fmt.Errorf("notification_settings must name at least one setting")
|
|
}
|
|
keys := make([]string, 0, len(update))
|
|
for key := range update {
|
|
keys = append(keys, key)
|
|
}
|
|
sort.Strings(keys)
|
|
result := make(map[string]bool, len(update))
|
|
var probe NotificationSettings
|
|
for _, key := range keys {
|
|
if probe.Field(key) == nil {
|
|
return nil, fmt.Errorf("unknown notification setting %q", key)
|
|
}
|
|
value, ok := update[key].(bool)
|
|
if !ok {
|
|
return nil, fmt.Errorf("notification setting %q must be true or false", key)
|
|
}
|
|
result[key] = value
|
|
}
|
|
return result, nil
|
|
}
|