Notify: an agent's notify reaches every connected app
The notify tool now records a chat_notify app notification with its own id per call (no dedupe of repeated words) and the tool's priority, so every subscribed client gets it and a reconnecting client replays it. ntfy delivery is unchanged; the tool result names where it went. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -123,30 +123,74 @@ func shownImageURL(raw string) (string, error) {
|
||||
return parsed.String(), nil
|
||||
}
|
||||
|
||||
// chatToolNotifier sends one model-requested phone notification for a chat and
|
||||
// reports whether ntfy accepted it, or why not.
|
||||
type chatToolNotifier func(ctx context.Context, sessionID, title, message string) (bool, string)
|
||||
// chatToolNotice is one model-requested notification. ID is the chat's shown
|
||||
// item id, so every call is its own notification even with repeated words.
|
||||
type chatToolNotice struct {
|
||||
ID string
|
||||
Title string
|
||||
Message string
|
||||
Priority string // low, default or high
|
||||
}
|
||||
|
||||
// publishToolNotification publishes through the operator's existing "system"
|
||||
// ntfy destination (the one chat attention uses), honoring their ntfy switch.
|
||||
// Tapping it opens the chat (pipelines://chat/<id>).
|
||||
func (h *chatHub) publishToolNotification(ctx context.Context, sessionID, title, message string) (bool, string) {
|
||||
title = truncateUTF8(title, 256)
|
||||
message = truncateUTF8(message, 2000)
|
||||
// chatToolDelivery reports where a chat tool notification went: App when it
|
||||
// was recorded on the app notification stream (every connected Pipelines app
|
||||
// receives it and a reconnecting one replays it), Phone when ntfy accepted it.
|
||||
// Detail explains a phone delivery that was skipped or refused.
|
||||
type chatToolDelivery struct {
|
||||
App bool
|
||||
Phone bool
|
||||
Detail string
|
||||
}
|
||||
|
||||
// chatToolNotifier delivers one model-requested notification for a chat.
|
||||
type chatToolNotifier func(ctx context.Context, sessionID string, notice chatToolNotice) chatToolDelivery
|
||||
|
||||
// chatNotifyPriority maps the tool's priority word to the app stream's
|
||||
// ntfy-style 1-5 priority.
|
||||
func chatNotifyPriority(priority string) int {
|
||||
switch priority {
|
||||
case "low":
|
||||
return 2
|
||||
case "high":
|
||||
return 4
|
||||
}
|
||||
return protocol.DefaultNotificationPriority
|
||||
}
|
||||
|
||||
// publishToolNotification records the notification on the app notification
|
||||
// stream (kind chat_notify), which is pushed to every subscribed client and
|
||||
// replayed to one that reconnects while it is still active, then publishes it
|
||||
// through the operator's existing "system" ntfy destination (the one chat
|
||||
// attention uses), honoring their ntfy switch. Tapping it opens the chat
|
||||
// (pipelines://chat/<id>).
|
||||
func (h *chatHub) publishToolNotification(ctx context.Context, sessionID string, notice chatToolNotice) chatToolDelivery {
|
||||
title := truncateUTF8(notice.Title, 256)
|
||||
message := truncateUTF8(notice.Message, 2000)
|
||||
click, views := chatAttentionLinks(sessionID)
|
||||
digest := sha256.Sum256([]byte(title + "\x00" + message))
|
||||
id := notice.ID
|
||||
if id == "" {
|
||||
digest := sha256.Sum256([]byte(title + "\x00" + message))
|
||||
id = hex.EncodeToString(digest[:8])
|
||||
}
|
||||
var delivery chatToolDelivery
|
||||
if h.notes != nil {
|
||||
h.notes.record(ctx, pipeline.AppNotificationRecord{
|
||||
AppNotification: protocol.AppNotification{ID: "chat:" + sessionID + ":notify:" + hex.EncodeToString(digest[:8]), Kind: protocol.NotificationChatReply, Title: title, Message: message, Click: click, Priority: protocol.DefaultNotificationPriority, Tags: []string{}, Group: "chat:" + sessionID},
|
||||
delivery.App = h.notes.record(ctx, pipeline.AppNotificationRecord{
|
||||
AppNotification: protocol.AppNotification{ID: "chat:" + sessionID + ":notify:" + id, Kind: protocol.NotificationChatNotify, Title: title, Message: message, Click: click, Priority: chatNotifyPriority(notice.Priority), Tags: []string{}, Group: "chat:" + sessionID},
|
||||
SessionID: sessionID,
|
||||
})
|
||||
}
|
||||
if !chatAttentionConfigured() || !chatNotificationSettings(ctx, h.settings).NtfyEnabled {
|
||||
return true, ""
|
||||
switch {
|
||||
case !chatAttentionConfigured():
|
||||
delivery.Detail = "phone notifications (ntfy) are not configured"
|
||||
case !chatNotificationSettings(ctx, h.settings).NtfyEnabled:
|
||||
delivery.Detail = "phone notifications (ntfy) are turned off"
|
||||
default:
|
||||
if _, err := chatAttentionPublish(ctx, "system", title, message, "", nil, click, views...); err != nil {
|
||||
log.Printf("chat tool notification session=%s: %v", sessionID, err)
|
||||
delivery.Detail = "the phone notification service refused it: " + err.Error()
|
||||
} else {
|
||||
delivery.Phone = true
|
||||
}
|
||||
}
|
||||
if _, err := chatAttentionPublish(ctx, "system", title, message, "", nil, click, views...); err != nil {
|
||||
log.Printf("chat tool notification session=%s: %v", sessionID, err)
|
||||
return false, "the notification service refused it: " + err.Error()
|
||||
}
|
||||
return true, ""
|
||||
return delivery
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user