Route toast notifications to agent assistant bubbles

This commit is contained in:
jxxghp
2026-06-24 12:55:01 +08:00
parent 7f0f12ac41
commit 2b426a47c6
3 changed files with 257 additions and 40 deletions
+64 -19
View File
@@ -1,11 +1,20 @@
import type { SystemNotification } from '@/api/types'
const AGENT_ASSISTANT_BUBBLE_EVENT = 'agentAssistantBubble'
let agentAssistantBubbleListenerCount = 0
let agentAssistantBubbleEntryActive = false
export interface AgentAssistantNotificationBubblePayload {
export type AgentAssistantBubbleKind = 'assistant' | 'custom' | 'notification' | 'toast'
export type AgentAssistantBubbleVariant = 'default' | 'info' | 'success' | 'warning' | 'error'
export interface AgentAssistantBubblePayload {
id: string
kind?: AgentAssistantBubbleKind
variant?: AgentAssistantBubbleVariant
title?: string
text?: string
duration?: number
keepOpen?: boolean
type?: string
mtype?: string
source?: string
@@ -13,7 +22,16 @@ export interface AgentAssistantNotificationBubblePayload {
reg_time?: string
}
interface AgentAssistantBubbleEvent extends CustomEvent<AgentAssistantNotificationBubblePayload> {}
export interface AgentAssistantNotificationBubblePayload extends AgentAssistantBubblePayload {
kind?: 'notification'
}
export interface AgentAssistantToastBubblePayload extends AgentAssistantBubblePayload {
kind: 'toast'
variant: AgentAssistantBubbleVariant
}
interface AgentAssistantBubbleEvent extends CustomEvent<AgentAssistantBubblePayload> {}
function createNotificationBubbleId(notification: SystemNotification) {
if (notification.id) return `notification-${notification.id}`
@@ -21,29 +39,44 @@ function createNotificationBubbleId(notification: SystemNotification) {
return `notification-${Date.now()}-${Math.random().toString(16).slice(2)}`
}
// 通知中心和智能助手入口没有父子关系,通过全局事件传递实时通知气泡数据。
export function emitAgentAssistantNotificationBubble(notification: SystemNotification) {
function emitAgentAssistantBubble(payload: AgentAssistantBubblePayload) {
if (typeof window === 'undefined') return
window.dispatchEvent(
new CustomEvent<AgentAssistantNotificationBubblePayload>(AGENT_ASSISTANT_BUBBLE_EVENT, {
detail: {
id: createNotificationBubbleId(notification),
title: notification.title,
text: notification.text,
type: notification.type,
mtype: notification.mtype,
source: notification.source,
date: notification.date,
reg_time: notification.reg_time,
},
new CustomEvent<AgentAssistantBubblePayload>(AGENT_ASSISTANT_BUBBLE_EVENT, {
detail: payload,
}),
)
}
export function onAgentAssistantNotificationBubble(
callback: (payload: AgentAssistantNotificationBubblePayload) => void,
) {
// 通知中心、toast 和智能助手入口没有父子关系,通过全局事件传递实时气泡数据。
export function emitAgentAssistantNotificationBubble(notification: SystemNotification) {
emitAgentAssistantBubble({
id: createNotificationBubbleId(notification),
kind: 'notification',
title: notification.title,
text: notification.text,
type: notification.type,
mtype: notification.mtype,
source: notification.source,
date: notification.date,
reg_time: notification.reg_time,
})
}
export function emitAgentAssistantToastBubble(payload: AgentAssistantToastBubblePayload) {
emitAgentAssistantBubble(payload)
}
export function setAgentAssistantBubbleEntryActive(active: boolean) {
agentAssistantBubbleEntryActive = active
}
export function canUseAgentAssistantBubble() {
return agentAssistantBubbleEntryActive && agentAssistantBubbleListenerCount > 0
}
export function onAgentAssistantBubble(callback: (payload: AgentAssistantBubblePayload) => void) {
if (typeof window === 'undefined') return () => {}
const handler = (event: Event) => {
@@ -51,6 +84,18 @@ export function onAgentAssistantNotificationBubble(
}
window.addEventListener(AGENT_ASSISTANT_BUBBLE_EVENT, handler)
agentAssistantBubbleListenerCount += 1
return () => window.removeEventListener(AGENT_ASSISTANT_BUBBLE_EVENT, handler)
return () => {
window.removeEventListener(AGENT_ASSISTANT_BUBBLE_EVENT, handler)
agentAssistantBubbleListenerCount = Math.max(0, agentAssistantBubbleListenerCount - 1)
}
}
export function onAgentAssistantNotificationBubble(
callback: (payload: AgentAssistantNotificationBubblePayload) => void,
) {
return onAgentAssistantBubble(payload => {
if ((payload.kind || 'notification') === 'notification') callback(payload as AgentAssistantNotificationBubblePayload)
})
}