UNPKG

@mastra/core

Version:
164 lines (163 loc) 5.88 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const require_tool = require("../tool-d85xHVkl.cjs"); const require_storage = require("../storage-C4FD5U8Z.cjs"); let zod_v4 = require("zod/v4"); //#region src/notifications/tool.ts const notificationActionSchema = zod_v4.z.object({ action: zod_v4.z.enum([ "list", "read", "markSeen", "dismiss", "archive", "search" ]), threadId: zod_v4.z.string().optional(), id: zod_v4.z.string().optional(), status: zod_v4.z.enum([ "pending", "delivered", "seen", "dismissed", "archived", "discarded", "failed" ]).optional(), priority: zod_v4.z.enum([ "low", "medium", "high", "urgent" ]).optional(), source: zod_v4.z.string().optional(), query: zod_v4.z.string().optional(), limit: zod_v4.z.number().int().positive().optional() }); const 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 = require_storage.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; } return { message: delivered > 0 ? `${delivered} notification${delivered === 1 ? "" : "s"} will now be delivered.` : "No unread notifications needed delivery.", delivered, markedSeen, unavailable, alreadyRead }; } function createNotificationInboxTool({ storage }) { return require_tool.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`); return { notification: await storage.updateNotification({ threadId, id: input.id, status: { markSeen: "seen", dismiss: "dismissed", archive: "archived" }[input.action] }) }; } }); } //#endregion exports.InMemoryNotificationsStorage = require_storage.InMemoryNotificationsStorage; exports.MAX_NOTIFICATION_DELIVERY_ATTEMPTS = require_storage.MAX_NOTIFICATION_DELIVERY_ATTEMPTS; exports.NotificationsStorage = require_storage.NotificationsStorage; exports.createNotificationInboxTool = createNotificationInboxTool; exports.createNotificationSignal = require_storage.createNotificationSignal; exports.createNotificationSummarySignal = require_storage.createNotificationSummarySignal; exports.defaultNotificationDeliveryDecision = require_storage.defaultNotificationDeliveryDecision; exports.dispatchDueNotifications = require_storage.dispatchDueNotifications; exports.notificationSignalAttributes = require_storage.notificationSignalAttributes; exports.notificationSignalMetadata = require_storage.notificationSignalMetadata; exports.notificationSummaryContents = require_storage.notificationSummaryContents; exports.notificationSummarySignalMetadata = require_storage.notificationSummarySignalMetadata; exports.resolveDeliveryFailureUpdate = require_storage.resolveDeliveryFailureUpdate; exports.resolveNotificationDeliveryDecision = require_storage.resolveNotificationDeliveryDecision; exports.summarizeNotifications = require_storage.summarizeNotifications; //# sourceMappingURL=index.cjs.map