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

Function formatThread

apps/sim/connectors/gmail/gmail.ts:198–252  ·  view source on GitHub ↗

* Formats a thread's messages into a single document string.

(thread: GmailThread)

Source from the content-addressed store, hash-verified

196 * Formats a thread's messages into a single document string.
197 */
198function formatThread(thread: GmailThread): {
199 content: string
200 subject: string
201 metadata: Record<string, unknown>
202} {
203 const messages = thread.messages || []
204 if (messages.length === 0) {
205 return { content: '', subject: 'Untitled Thread', metadata: {} }
206 }
207
208 const firstMessage = messages[0]
209 const lastMessage = messages[messages.length - 1]
210 const subject = getHeader(firstMessage.payload, 'Subject') || 'No Subject'
211 const from = getHeader(firstMessage.payload, 'From') || 'Unknown'
212 const to = getHeader(firstMessage.payload, 'To') || ''
213 const labelIds = firstMessage.labelIds || []
214
215 const lines: string[] = []
216 lines.push(`Subject: ${subject}`)
217 lines.push(`From: ${from}`)
218 if (to) lines.push(`To: ${to}`)
219 lines.push(`Messages: ${messages.length}`)
220 lines.push('')
221
222 for (const msg of messages) {
223 const msgFrom = getHeader(msg.payload, 'From') || 'Unknown'
224 const msgDate = getHeader(msg.payload, 'Date') || ''
225 const body = msg.payload ? extractBody(msg.payload) : ''
226
227 lines.push(`--- ${msgFrom} (${msgDate}) ---`)
228 lines.push(body.trim())
229 lines.push('')
230 }
231
232 const firstDate = firstMessage.internalDate
233 ? new Date(Number(firstMessage.internalDate)).toISOString()
234 : undefined
235 const lastDate = lastMessage.internalDate
236 ? new Date(Number(lastMessage.internalDate)).toISOString()
237 : undefined
238
239 return {
240 content: lines.join('\n').trim(),
241 subject,
242 metadata: {
243 from,
244 to,
245 subject,
246 messageCount: messages.length,
247 labelIds,
248 firstMessageDate: firstDate,
249 lastMessageDate: lastDate,
250 },
251 }
252}
253
254/**
255 * Fetches a full thread with all its messages.

Callers 1

gmail.tsFile · 0.85

Calls 4

extractBodyFunction · 0.85
joinMethod · 0.80
getHeaderFunction · 0.70
pushMethod · 0.45

Tested by

no test coverage detected