UNPKG

@mastra/core

Version:
132 lines (130 loc) 5.29 kB
export { defaultNotificationDeliveryDecision, resolveNotificationDeliveryDecision } from '../chunk-OE4IEL7C.js'; import { createNotificationSignal } from '../chunk-PIFDJ2RZ.js'; export { InMemoryNotificationsStorage, NotificationsStorage, createNotificationSignal, createNotificationSummarySignal, dispatchDueNotifications, notificationSignalAttributes, notificationSignalMetadata, notificationSummaryContents, notificationSummarySignalMetadata, summarizeNotifications } from '../chunk-PIFDJ2RZ.js'; import { createTool } from '../chunk-O5QBSZXE.js'; import { z } from 'zod/v4'; var notificationActionSchema = z.object({ action: z.enum(["list", "read", "markSeen", "dismiss", "archive", "search"]), threadId: z.string().optional(), id: z.string().optional(), status: z.enum(["pending", "delivered", "seen", "dismissed", "archived", "discarded"]).optional(), priority: z.enum(["low", "medium", "high", "urgent"]).optional(), source: z.string().optional(), query: z.string().optional(), limit: z.number().int().positive().optional() }); var isReadable = (notification) => notification.status === "pending" || notification.status === "delivered"; async function deliverNotifications({ notifications, storage, context }) { let delivered = 0; let markedSeen = 0; let unavailable = 0; let alreadyRead = 0; for (const notification of notifications) { if (!isReadable(notification)) { alreadyRead += 1; continue; } const agentId = notification.agentId ?? context?.agent?.agentId; const resourceId = notification.resourceId ?? context?.agent?.resourceId; const mastra = context?.mastra; const agent = agentId && typeof mastra?.getAgentById === "function" ? await mastra.getAgentById(agentId) : void 0; if (agent && resourceId && !notification.deliveredSignalId) { const signal = createNotificationSignal({ ...notification, status: "delivered" }); const result = agent.sendSignal(signal, { resourceId, threadId: notification.threadId }); await result.persisted; await storage.updateNotification({ threadId: notification.threadId, id: notification.id, status: "seen", deliveredSignalId: result.signal.id }); delivered += 1; continue; } if (notification.deliveredSignalId) { await storage.updateNotification({ threadId: notification.threadId, id: notification.id, status: "seen" }); markedSeen += 1; } else { unavailable += 1; } } const message = delivered > 0 ? `${delivered} notification${delivered === 1 ? "" : "s"} will now be delivered.` : "No unread notifications needed delivery."; return { message, delivered, markedSeen, unavailable, alreadyRead }; } function createNotificationInboxTool({ storage }) { return createTool({ id: "notification-inbox", description: "Inspect and manage the current thread notification inbox. Use this to list pending notifications, read full details after a summary, mark notifications seen, dismiss, archive, or search old notifications.", inputSchema: notificationActionSchema, execute: async (input, context) => { const threadId = input.threadId ?? context?.agent?.threadId; if (!threadId) { throw new Error("notification-inbox requires a threadId"); } if (input.action === "list") { const listInput = { threadId, status: input.status, priority: input.priority, source: input.source, limit: input.limit }; return { notifications: await storage.listNotifications(listInput) }; } if (input.action === "search") { if (!input.query) throw new Error("notification-inbox search requires query"); return { notifications: await storage.listNotifications({ threadId, search: input.query, status: input.status, priority: input.priority, source: input.source, limit: input.limit }) }; } if (input.action === "read") { const notifications = input.id ? [await storage.getNotification({ threadId, id: input.id })] : await storage.listNotifications({ threadId, status: input.status ?? ["pending", "delivered"], priority: input.priority, source: input.source, limit: input.limit }); if (input.id && !notifications[0]) throw new Error(`Notification ${input.id} was not found for thread ${threadId}`); return deliverNotifications({ notifications: notifications.filter( (notification) => Boolean(notification) ), storage, context }); } if (!input.id) throw new Error(`notification-inbox ${input.action} requires id`); const statusByAction = { markSeen: "seen", dismiss: "dismissed", archive: "archived" }; return { notification: await storage.updateNotification({ threadId, id: input.id, status: statusByAction[input.action] }) }; } }); } export { createNotificationInboxTool }; //# sourceMappingURL=index.js.map //# sourceMappingURL=index.js.map