( message: GmailMessage, params?: GmailReadParams )
| 52 | |
| 53 | // Helper function to process a Gmail message |
| 54 | export async function processMessage( |
| 55 | message: GmailMessage, |
| 56 | params?: GmailReadParams |
| 57 | ): Promise<GmailToolResponse> { |
| 58 | // Check if message and payload exist |
| 59 | if (!message || !message.payload) { |
| 60 | return { |
| 61 | success: true, |
| 62 | output: { |
| 63 | content: 'Unable to process email: Invalid message format', |
| 64 | metadata: { |
| 65 | id: message?.id || '', |
| 66 | threadId: message?.threadId || '', |
| 67 | labelIds: message?.labelIds || [], |
| 68 | }, |
| 69 | }, |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | const headers = message.payload.headers || [] |
| 74 | const subject = headers.find((h) => h.name.toLowerCase() === 'subject')?.value || '' |
| 75 | const from = headers.find((h) => h.name.toLowerCase() === 'from')?.value || '' |
| 76 | const to = headers.find((h) => h.name.toLowerCase() === 'to')?.value || '' |
| 77 | const date = headers.find((h) => h.name.toLowerCase() === 'date')?.value || '' |
| 78 | |
| 79 | // Extract the message body |
| 80 | const body = extractMessageBody(message.payload) |
| 81 | |
| 82 | // Check for attachments |
| 83 | const attachmentInfo = extractAttachmentInfo(message.payload) |
| 84 | const hasAttachments = attachmentInfo.length > 0 |
| 85 | |
| 86 | // Download attachments if requested |
| 87 | let attachments: GmailAttachment[] | undefined |
| 88 | if (params?.includeAttachments && hasAttachments && params.accessToken) { |
| 89 | try { |
| 90 | attachments = await downloadAttachments(message.id, attachmentInfo, params.accessToken) |
| 91 | } catch (error) { |
| 92 | // Continue without attachments rather than failing the entire request |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | const result: GmailToolResponse = { |
| 97 | success: true, |
| 98 | output: { |
| 99 | content: body || 'No content found in email', |
| 100 | metadata: { |
| 101 | id: message.id || '', |
| 102 | threadId: message.threadId || '', |
| 103 | labelIds: message.labelIds || [], |
| 104 | from, |
| 105 | to, |
| 106 | subject, |
| 107 | date, |
| 108 | hasAttachments, |
| 109 | attachmentCount: attachmentInfo.length, |
| 110 | }, |
| 111 | // Always include attachments array (empty if none downloaded) |
no test coverage detected