MCPcopy Index your code
hub / github.com/simstudioai/sim / formatEmailAsMessage

Function formatEmailAsMessage

apps/sim/lib/mothership/inbox/format.ts:16–56  ·  view source on GitHub ↗
(
  task: InboxTask,
  attachments: AgentMailAttachment[] = []
)

Source from the content-addressed store, hash-verified

14 * Handles forwarded emails, CC'd conversations, and attachment metadata.
15 */
16export 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.

Callers 1

executeInboxTaskFunction · 0.90

Calls 8

formatFileSizeFunction · 0.90
isForwardedEmailFunction · 0.85
extractTextFromHtmlFunction · 0.85
stripQuotedReplyFunction · 0.85
parseMethod · 0.80
joinMethod · 0.80
replaceMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected