UNPKG

@mastra/core

Version:
181 lines (178 loc) • 7.02 kB
'use strict'; var chunkCJAAPSS7_cjs = require('../chunk-CJAAPSS7.cjs'); var chunkOAN2CNR7_cjs = require('../chunk-OAN2CNR7.cjs'); var chunkMFHFXZHO_cjs = require('../chunk-MFHFXZHO.cjs'); var v4 = require('zod/v4'); var notificationActionSchema = v4.z.object({ action: v4.z.enum(["list", "read", "markSeen", "dismiss", "archive", "search"]), threadId: v4.z.string().optional(), id: v4.z.string().optional(), status: v4.z.enum(["pending", "delivered", "seen", "dismissed", "archived", "discarded"]).optional(), priority: v4.z.enum(["low", "medium", "high", "urgent"]).optional(), source: v4.z.string().optional(), query: v4.z.string().optional(), limit: v4.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 = chunkOAN2CNR7_cjs.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 chunkMFHFXZHO_cjs.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] }) }; } }); } Object.defineProperty(exports, "defaultNotificationDeliveryDecision", { enumerable: true, get: function () { return chunkCJAAPSS7_cjs.defaultNotificationDeliveryDecision; } }); Object.defineProperty(exports, "resolveNotificationDeliveryDecision", { enumerable: true, get: function () { return chunkCJAAPSS7_cjs.resolveNotificationDeliveryDecision; } }); Object.defineProperty(exports, "InMemoryNotificationsStorage", { enumerable: true, get: function () { return chunkOAN2CNR7_cjs.InMemoryNotificationsStorage; } }); Object.defineProperty(exports, "NotificationsStorage", { enumerable: true, get: function () { return chunkOAN2CNR7_cjs.NotificationsStorage; } }); Object.defineProperty(exports, "createNotificationSignal", { enumerable: true, get: function () { return chunkOAN2CNR7_cjs.createNotificationSignal; } }); Object.defineProperty(exports, "createNotificationSummarySignal", { enumerable: true, get: function () { return chunkOAN2CNR7_cjs.createNotificationSummarySignal; } }); Object.defineProperty(exports, "dispatchDueNotifications", { enumerable: true, get: function () { return chunkOAN2CNR7_cjs.dispatchDueNotifications; } }); Object.defineProperty(exports, "notificationSignalAttributes", { enumerable: true, get: function () { return chunkOAN2CNR7_cjs.notificationSignalAttributes; } }); Object.defineProperty(exports, "notificationSignalMetadata", { enumerable: true, get: function () { return chunkOAN2CNR7_cjs.notificationSignalMetadata; } }); Object.defineProperty(exports, "notificationSummaryContents", { enumerable: true, get: function () { return chunkOAN2CNR7_cjs.notificationSummaryContents; } }); Object.defineProperty(exports, "notificationSummarySignalMetadata", { enumerable: true, get: function () { return chunkOAN2CNR7_cjs.notificationSummarySignalMetadata; } }); Object.defineProperty(exports, "summarizeNotifications", { enumerable: true, get: function () { return chunkOAN2CNR7_cjs.summarizeNotifications; } }); exports.createNotificationInboxTool = createNotificationInboxTool; //# sourceMappingURL=index.cjs.map //# sourceMappingURL=index.cjs.map