| 1 | import { createTool } from '@mastra/core/tools' |
| 2 | import { z } from 'zod' |
| 3 | |
| 4 | import { AgentMailClient } from 'agentmail' |
| 5 | |
| 6 | const ListItemsInput = z.object({ |
| 7 | limit: z.number().optional().describe('Max number of items to return'), |
| 8 | pageToken: z.string().optional().describe('Pagination page token'), |
| 9 | }) |
| 10 | |
| 11 | const InboxId = z.string().describe('ID of inbox') |
| 12 | |
| 13 | const GetInboxInput = z.object({ inboxId: InboxId }) |
| 14 | |
| 15 | const CreateInboxInput = z.object({ |
| 16 | username: z.string().optional().describe('Username'), |
| 17 | domain: z.string().optional().describe('Domain'), |
| 18 | displayName: z.string().optional().describe('Display name'), |
| 19 | }) |
| 20 | |
| 21 | const ListInboxItemsInput = ListItemsInput.extend({ |
| 22 | inboxId: InboxId, |
| 23 | labels: z.array(z.string()).optional().describe('Filter items with labels'), |
| 24 | before: z.coerce.date().optional().describe('Filter items before datetime'), |
| 25 | after: z.coerce.date().optional().describe('Filter items after datetime'), |
| 26 | }) |
| 27 | |
| 28 | const GetThreadInput = GetInboxInput.extend({ |
| 29 | threadId: z.string().describe('ID of thread'), |
| 30 | }) |
| 31 | |
| 32 | const MessageId = z.string().describe('ID of message') |
| 33 | |
| 34 | const BaseMessageInput = z.object({ |
| 35 | inboxId: InboxId, |
| 36 | text: z.string().optional().describe('Plain text body'), |
| 37 | html: z.string().optional().describe('HTML body'), |
| 38 | labels: z.array(z.string()).optional().describe('Labels'), |
| 39 | }) |
| 40 | |
| 41 | const SendMessageInput = BaseMessageInput.extend({ |
| 42 | to: z.union([z.string(), z.array(z.string())]).describe('To recipients'), |
| 43 | cc: z |
| 44 | .union([z.string(), z.array(z.string())]) |
| 45 | .optional() |
| 46 | .describe('CC recipients'), |
| 47 | bcc: z |
| 48 | .union([z.string(), z.array(z.string())]) |
| 49 | .optional() |
| 50 | .describe('BCC recipients'), |
| 51 | subject: z.string().optional().describe('Subject'), |
| 52 | }) |
| 53 | |
| 54 | const ReplyToMessageInput = BaseMessageInput.extend({ messageId: MessageId }) |
| 55 | |
| 56 | const UpdateMessageInput = z.object({ |
| 57 | inboxId: InboxId, |
| 58 | messageId: MessageId, |
| 59 | addLabels: z.array(z.string()).optional().describe('Labels to add'), |
| 60 | removeLabels: z.array(z.string()).optional().describe('Labels to remove'), |
| 61 | }) |
| 62 | |
| 63 | const listInboxesTool = createTool({ |
| 64 | id: 'list-inboxes', |
| 65 | description: 'List inboxes', |
| 66 | inputSchema: ListItemsInput, |
| 67 | execute: ({ context }) => { |
| 68 | const client = new AgentMailClient() |
| 69 | return client.inboxes.list(context) |
| 70 | }, |
| 71 | }) |
| 72 | |
| 73 | const createInboxTool = createTool({ |
| 74 | id: 'create-inbox', |
| 75 | description: 'Create inbox', |
| 76 | inputSchema: CreateInboxInput, |
| 77 | execute: ({ context }) => { |
| 78 | const client = new AgentMailClient() |
| 79 | return client.inboxes.create(context) |
| 80 | }, |
| 81 | }) |
| 82 | |
| 83 | const deleteInboxTool = createTool({ |
| 84 | id: 'delete-inbox', |
| 85 | description: 'Delete inbox', |
| 86 | inputSchema: GetInboxInput, |
| 87 | execute: ({ context }) => { |
| 88 | const client = new AgentMailClient() |
| 89 | return client.inboxes.delete(context.inboxId) |
| 90 | }, |
| 91 | }) |
| 92 | |
| 93 | const listThreadsTool = createTool({ |
| 94 | id: 'list-threads', |
| 95 | description: 'List threads', |
| 96 | inputSchema: ListInboxItemsInput, |
| 97 | execute: ({ context }) => { |
| 98 | const client = new AgentMailClient() |
| 99 | return client.inboxes.threads.list(context.inboxId, context) |
| 100 | }, |
| 101 | }) |
| 102 | |
| 103 | const getThreadTool = createTool({ |
| 104 | id: 'get-thread', |
| 105 | description: 'Get thread and its messages', |
| 106 | inputSchema: GetThreadInput, |
| 107 | execute: ({ context }) => { |
| 108 | const client = new AgentMailClient() |
| 109 | return client.inboxes.threads.get(context.inboxId, context.threadId) |
| 110 | }, |
| 111 | }) |
| 112 | |
| 113 | const sendMessageTool = createTool({ |
| 114 | id: 'send-message', |
| 115 | description: 'Send message', |
| 116 | inputSchema: SendMessageInput, |
| 117 | execute: ({ context }) => { |
| 118 | const client = new AgentMailClient() |
| 119 | return client.inboxes.messages.send(context.inboxId, context) |
| 120 | }, |
| 121 | }) |
| 122 | |
| 123 | const replyToMessageTool = createTool({ |
| 124 | id: 'reply-to-message', |
| 125 | description: 'Reply to message', |
| 126 | inputSchema: ReplyToMessageInput, |
| 127 | execute: ({ context }) => { |
| 128 | const client = new AgentMailClient() |
| 129 | return client.inboxes.messages.reply(context.inboxId, context.messageId, context) |
| 130 | }, |
| 131 | }) |
| 132 | |
| 133 | const updateMessageTool = createTool({ |
| 134 | id: 'update-message', |
| 135 | description: 'Update message', |
| 136 | inputSchema: UpdateMessageInput, |
| 137 | execute: ({ context }) => { |
| 138 | const client = new AgentMailClient() |
| 139 | return client.inboxes.messages.update(context.inboxId, context.messageId, context) |
| 140 | }, |
| 141 | }) |
| 142 | |
| 143 | export const tools = { |
| 144 | listInboxesTool, |
| 145 | createInboxTool, |
| 146 | deleteInboxTool, |
| 147 | listThreadsTool, |
| 148 | getThreadTool, |
| 149 | sendMessageTool, |
| 150 | replyToMessageTool, |
| 151 | updateMessageTool, |
| 152 | } |