( task: InboxTask, attachments: AgentMailAttachment[] = [] )
| 14 | * Handles forwarded emails, CC'd conversations, and attachment metadata. |
| 15 | */ |
| 16 | export function formatEmailAsMessage( |
| 17 | task: InboxTask, |
| 18 | attachments: AgentMailAttachment[] = [] |
| 19 | ): string { |
| 20 | const parts: string[] = [] |
| 21 | const isForwarded = isForwardedEmail(task.subject, task.bodyText) |
| 22 | |
| 23 | if (isForwarded) { |
| 24 | parts.push(`**Forwarded email from:** ${task.fromName || task.fromEmail}`) |
| 25 | } |
| 26 | |
| 27 | if (task.subject && task.subject !== 'Re:' && task.subject !== '(no subject)') { |
| 28 | const cleanSubject = task.subject.replace(/^(fwd?|fw|re):\s*/gi, '').trim() |
| 29 | parts.push(`**Subject:** ${cleanSubject}`) |
| 30 | } |
| 31 | |
| 32 | if (task.ccRecipients) { |
| 33 | try { |
| 34 | const cc = JSON.parse(task.ccRecipients) as string[] |
| 35 | if (cc.length > 0) { |
| 36 | parts.push(`**CC'd:** ${cc.join(', ')}`) |
| 37 | } |
| 38 | } catch {} |
| 39 | } |
| 40 | |
| 41 | const rawBody = task.bodyText || extractTextFromHtml(task.bodyHtml) || '(empty email body)' |
| 42 | const hasExistingChat = !!task.chatId |
| 43 | const body = hasExistingChat ? stripQuotedReply(rawBody) : rawBody |
| 44 | parts.push(body) |
| 45 | |
| 46 | if (attachments.length > 0) { |
| 47 | const attachmentList = attachments |
| 48 | .map((a) => `- ${a.filename} (${a.content_type}, ${formatFileSize(a.size)})`) |
| 49 | .join('\n') |
| 50 | parts.push(`**Attachments:**\n${attachmentList}`) |
| 51 | } else if (task.hasAttachments) { |
| 52 | parts.push('**Attachments:** (attached files are available for processing)') |
| 53 | } |
| 54 | |
| 55 | return parts.join('\n\n') |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Strips quoted reply content from email body. |
no test coverage detected