@mastra/core
Version:
1 lines • 10.5 kB
Source Map (JSON)
{"version":3,"sources":["../../src/notifications/tool.ts"],"names":["z","createNotificationSignal","createTool"],"mappings":";;;;;;;AAOA,IAAM,wBAAA,GAA2BA,KAAE,MAAA,CAAO;AAAA,EACxC,MAAA,EAAQA,IAAA,CAAE,IAAA,CAAK,CAAC,MAAA,EAAQ,QAAQ,UAAA,EAAY,SAAA,EAAW,SAAA,EAAW,QAAQ,CAAC,CAAA;AAAA,EAC3E,QAAA,EAAUA,IAAA,CAAE,MAAA,EAAO,CAAE,QAAA,EAAS;AAAA,EAC9B,EAAA,EAAIA,IAAA,CAAE,MAAA,EAAO,CAAE,QAAA,EAAS;AAAA,EACxB,MAAA,EAAQA,IAAA,CAAE,IAAA,CAAK,CAAC,SAAA,EAAW,WAAA,EAAa,MAAA,EAAQ,WAAA,EAAa,UAAA,EAAY,WAAW,CAAC,CAAA,CAAE,QAAA,EAAS;AAAA,EAChG,QAAA,EAAUA,IAAA,CAAE,IAAA,CAAK,CAAC,KAAA,EAAO,UAAU,MAAA,EAAQ,QAAQ,CAAC,CAAA,CAAE,QAAA,EAAS;AAAA,EAC/D,MAAA,EAAQA,IAAA,CAAE,MAAA,EAAO,CAAE,QAAA,EAAS;AAAA,EAC5B,KAAA,EAAOA,IAAA,CAAE,MAAA,EAAO,CAAE,QAAA,EAAS;AAAA,EAC3B,KAAA,EAAOA,KAAE,MAAA,EAAO,CAAE,KAAI,CAAE,QAAA,GAAW,QAAA;AACrC,CAAC,CAAA;AAWD,IAAM,aAAa,CAAC,YAAA,KAClB,aAAa,MAAA,KAAW,SAAA,IAAa,aAAa,MAAA,KAAW,WAAA;AAE/D,eAAe,oBAAA,CAAqB;AAAA,EAClC,aAAA;AAAA,EACA,OAAA;AAAA,EACA;AACF,CAAA,EAIG;AACD,EAAA,IAAI,SAAA,GAAY,CAAA;AAChB,EAAA,IAAI,UAAA,GAAa,CAAA;AACjB,EAAA,IAAI,WAAA,GAAc,CAAA;AAClB,E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MAAM,IAAI,KAAA,CAAM,CAAA,mBAAA,EAAsB,KAAA,CAAM,MAAM,CAAA,YAAA,CAAc,CAAA;AAC/E,MAAA,MAAM,cAAA,GAAiB;AAAA,QACrB,QAAA,EAAU,MAAA;AAAA,QACV,OAAA,EAAS,WAAA;AAAA,QACT,OAAA,EAAS;AAAA,OACX;AAEA,MAAA,OAAO;AAAA,QACL,YAAA,EAAc,MAAM,OAAA,CAAQ,kBAAA,CAAmB;AAAA,UAC7C,QAAA;AAAA,UACA,IAAI,KAAA,CAAM,EAAA;AAAA,UACV,MAAA,EAAQ,cAAA,CAAe,KAAA,CAAM,MAAM;AAAA,SACpC;AAAA,OACH;AAAA,IACF;AAAA,GACD,CAAA;AACH","file":"index.cjs","sourcesContent":["import { z } from 'zod/v4';\nimport { createTool } from '../tools';\nimport type { ToolExecutionContext } from '../tools';\nimport { createNotificationSignal } from './signals';\nimport type { NotificationsStorage } from './storage';\nimport type { ListNotificationsInput, NotificationRecord, NotificationStatus } from './types';\n\nconst notificationActionSchema = z.object({\n action: z.enum(['list', 'read', 'markSeen', 'dismiss', 'archive', 'search']),\n threadId: z.string().optional(),\n id: z.string().optional(),\n status: z.enum(['pending', 'delivered', 'seen', 'dismissed', 'archived', 'discarded']).optional(),\n priority: z.enum(['low', 'medium', 'high', 'urgent']).optional(),\n source: z.string().optional(),\n query: z.string().optional(),\n limit: z.number().int().positive().optional(),\n});\n\ntype NotificationInboxAction = z.infer<typeof notificationActionSchema>;\n\ntype NotificationToolAgent = {\n sendSignal: (\n signal: ReturnType<typeof createNotificationSignal>,\n target: { resourceId: string; threadId: string },\n ) => { signal: ReturnType<typeof createNotificationSignal>; persisted?: Promise<void> };\n};\n\nconst isReadable = (notification: NotificationRecord) =>\n notification.status === 'pending' || notification.status === 'delivered';\n\nasync function deliverNotifications({\n notifications,\n storage,\n context,\n}: {\n notifications: NotificationRecord[];\n storage: NotificationsStorage;\n context: ToolExecutionContext;\n}) {\n let delivered = 0;\n let markedSeen = 0;\n let unavailable = 0;\n let alreadyRead = 0;\n\n for (const notification of notifications) {\n if (!isReadable(notification)) {\n alreadyRead += 1;\n continue;\n }\n\n const agentId = notification.agentId ?? context?.agent?.agentId;\n const resourceId = notification.resourceId ?? context?.agent?.resourceId;\n const mastra = context?.mastra;\n const agent =\n agentId && typeof mastra?.getAgentById === 'function' ? await mastra.getAgentById(agentId) : undefined;\n\n if (agent && resourceId && !notification.deliveredSignalId) {\n const signal = createNotificationSignal({ ...notification, status: 'delivered' });\n const result = (agent as NotificationToolAgent).sendSignal(signal, {\n resourceId,\n threadId: notification.threadId,\n });\n await result.persisted;\n await storage.updateNotification({\n threadId: notification.threadId,\n id: notification.id,\n status: 'seen',\n deliveredSignalId: result.signal.id,\n });\n delivered += 1;\n continue;\n }\n\n if (notification.deliveredSignalId) {\n await storage.updateNotification({ threadId: notification.threadId, id: notification.id, status: 'seen' });\n markedSeen += 1;\n } else {\n unavailable += 1;\n }\n }\n\n const message =\n delivered > 0\n ? `${delivered} notification${delivered === 1 ? '' : 's'} will now be delivered.`\n : 'No unread notifications needed delivery.';\n\n return { message, delivered, markedSeen, unavailable, alreadyRead };\n}\n\nexport function createNotificationInboxTool({ storage }: { storage: NotificationsStorage }) {\n return createTool({\n id: 'notification-inbox',\n description:\n '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.',\n inputSchema: notificationActionSchema,\n execute: async (input: NotificationInboxAction, context) => {\n const threadId = input.threadId ?? context?.agent?.threadId;\n if (!threadId) {\n throw new Error('notification-inbox requires a threadId');\n }\n\n if (input.action === 'list') {\n const listInput: ListNotificationsInput = {\n threadId,\n status: input.status,\n priority: input.priority,\n source: input.source,\n limit: input.limit,\n };\n return { notifications: await storage.listNotifications(listInput) };\n }\n\n if (input.action === 'search') {\n if (!input.query) throw new Error('notification-inbox search requires query');\n return {\n notifications: await storage.listNotifications({\n threadId,\n search: input.query,\n status: input.status,\n priority: input.priority,\n source: input.source,\n limit: input.limit,\n }),\n };\n }\n\n if (input.action === 'read') {\n const notifications = input.id\n ? [await storage.getNotification({ threadId, id: input.id })]\n : await storage.listNotifications({\n threadId,\n status: input.status ?? ['pending', 'delivered'],\n priority: input.priority,\n source: input.source,\n limit: input.limit,\n });\n if (input.id && !notifications[0])\n throw new Error(`Notification ${input.id} was not found for thread ${threadId}`);\n return deliverNotifications({\n notifications: notifications.filter((notification): notification is NotificationRecord =>\n Boolean(notification),\n ),\n storage,\n context,\n });\n }\n\n if (!input.id) throw new Error(`notification-inbox ${input.action} requires id`);\n const statusByAction = {\n markSeen: 'seen',\n dismiss: 'dismissed',\n archive: 'archived',\n } satisfies Record<'markSeen' | 'dismiss' | 'archive', NotificationStatus>;\n\n return {\n notification: await storage.updateNotification({\n threadId,\n id: input.id,\n status: statusByAction[input.action],\n }),\n };\n },\n });\n}\n"]}