Repository navigation
Expand file tree
/
Copy pathutils.ts
More file actions
559 lines (492 loc) · 17.9 KB
/
Copy pathutils.ts
File metadata and controls
559 lines (492 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
import { generateRandomString } from '@sim/utils/random'
import { convert } from 'html-to-text'
import type {
GmailAttachment,
GmailMessage,
GmailReadParams,
GmailToolResponse,
} from '@/tools/gmail/types'
export const GMAIL_API_BASE = 'https://gmail.googleapis.com/gmail/v1/users/me'
/**
* Fetch original message headers for threading
* @param messageId Gmail message ID to fetch headers from
* @param accessToken Gmail access token
* @returns Object containing threading headers (messageId, references, subject)
*/
export async function fetchThreadingHeaders(
messageId: string,
accessToken: string
): Promise<{
messageId?: string
references?: string
subject?: string
}> {
try {
const messageResponse = await fetch(
`${GMAIL_API_BASE}/messages/${messageId}?format=metadata&metadataHeaders=Message-ID&metadataHeaders=References&metadataHeaders=Subject`,
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
)
if (messageResponse.ok) {
const messageData = await messageResponse.json()
const headers = messageData.payload?.headers || []
return {
messageId: headers.find((h: any) => h.name.toLowerCase() === 'message-id')?.value,
references: headers.find((h: any) => h.name.toLowerCase() === 'references')?.value,
subject: headers.find((h: any) => h.name.toLowerCase() === 'subject')?.value,
}
}
} catch (error) {
// Continue without threading headers rather than failing
}
return {}
}
// Helper function to process a Gmail message
export async function processMessage(
message: GmailMessage,
params?: GmailReadParams
): Promise<GmailToolResponse> {
// Check if message and payload exist
if (!message || !message.payload) {
return {
success: true,
output: {
content: 'Unable to process email: Invalid message format',
metadata: {
id: message?.id || '',
threadId: message?.threadId || '',
labelIds: message?.labelIds || [],
},
},
}
}
const headers = message.payload.headers || []
const subject = headers.find((h) => h.name.toLowerCase() === 'subject')?.value || ''
const from = headers.find((h) => h.name.toLowerCase() === 'from')?.value || ''
const to = headers.find((h) => h.name.toLowerCase() === 'to')?.value || ''
const date = headers.find((h) => h.name.toLowerCase() === 'date')?.value || ''
// Extract the message body
const body = extractMessageBody(message.payload)
// Check for attachments
const attachmentInfo = extractAttachmentInfo(message.payload)
const hasAttachments = attachmentInfo.length > 0
// Download attachments if requested
let attachments: GmailAttachment[] | undefined
if (params?.includeAttachments && hasAttachments && params.accessToken) {
try {
attachments = await downloadAttachments(message.id, attachmentInfo, params.accessToken)
} catch (error) {
// Continue without attachments rather than failing the entire request
}
}
const result: GmailToolResponse = {
success: true,
output: {
content: body || 'No content found in email',
metadata: {
id: message.id || '',
threadId: message.threadId || '',
labelIds: message.labelIds || [],
from,
to,
subject,
date,
hasAttachments,
attachmentCount: attachmentInfo.length,
},
// Always include attachments array (empty if none downloaded)
attachments: attachments || [],
},
}
return result
}
// Helper function to process a message for summary (without full content)
export function processMessageForSummary(message: GmailMessage): any {
if (!message || !message.payload) {
return {
id: message?.id || '',
threadId: message?.threadId || '',
subject: 'Unknown Subject',
from: 'Unknown Sender',
to: '',
date: '',
snippet: message?.snippet || '',
}
}
const headers = message.payload.headers || []
const subject = headers.find((h) => h.name.toLowerCase() === 'subject')?.value || 'No Subject'
const from = headers.find((h) => h.name.toLowerCase() === 'from')?.value || 'Unknown Sender'
const to = headers.find((h) => h.name.toLowerCase() === 'to')?.value || ''
const date = headers.find((h) => h.name.toLowerCase() === 'date')?.value || ''
return {
id: message.id,
threadId: message.threadId,
subject,
from,
to,
date,
snippet: message.snippet || '',
}
}
// Helper function to recursively extract message body from MIME parts
export function extractMessageBody(payload: any): string {
// If the payload has a body with data, decode it
if (payload.body?.data) {
return Buffer.from(payload.body.data, 'base64').toString()
}
// If there are no parts, return empty string
if (!payload.parts || !Array.isArray(payload.parts) || payload.parts.length === 0) {
return ''
}
// First try to find a text/plain part
const textPart = payload.parts.find((part: any) => part.mimeType === 'text/plain')
if (textPart?.body?.data) {
return Buffer.from(textPart.body.data, 'base64').toString()
}
// If no text/plain, try to find text/html
const htmlPart = payload.parts.find((part: any) => part.mimeType === 'text/html')
if (htmlPart?.body?.data) {
return Buffer.from(htmlPart.body.data, 'base64').toString()
}
// If we have multipart/alternative or other complex types, recursively check parts
for (const part of payload.parts) {
if (part.parts) {
const nestedBody = extractMessageBody(part)
if (nestedBody) {
return nestedBody
}
}
}
// If we couldn't find any text content, return empty string
return ''
}
// Helper function to extract attachment information from message payload
export function extractAttachmentInfo(
payload: any
): Array<{ attachmentId: string; filename: string; mimeType: string; size: number }> {
const attachments: Array<{
attachmentId: string
filename: string
mimeType: string
size: number
}> = []
function processPayloadPart(part: any) {
// Check if this part has an attachment
if (part.body?.attachmentId && part.filename) {
attachments.push({
attachmentId: part.body.attachmentId,
filename: part.filename,
mimeType: part.mimeType || 'application/octet-stream',
size: part.body.size || 0,
})
}
// Recursively process nested parts
if (part.parts && Array.isArray(part.parts)) {
part.parts.forEach(processPayloadPart)
}
}
// Process the main payload
processPayloadPart(payload)
return attachments
}
// Helper function to download attachments from Gmail API
export async function downloadAttachments(
messageId: string,
attachmentInfo: Array<{ attachmentId: string; filename: string; mimeType: string; size: number }>,
accessToken: string
): Promise<GmailAttachment[]> {
const downloadedAttachments: GmailAttachment[] = []
for (const attachment of attachmentInfo) {
try {
// Download attachment from Gmail API
const attachmentResponse = await fetch(
`${GMAIL_API_BASE}/messages/${messageId}/attachments/${attachment.attachmentId}`,
{
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
}
)
if (!attachmentResponse.ok) {
await attachmentResponse.body?.cancel().catch(() => {})
continue
}
const attachmentData = (await attachmentResponse.json()) as { data: string; size: number }
// Decode base64url data to buffer
// Gmail API returns data in base64url format (URL-safe base64)
const base64Data = attachmentData.data.replace(/-/g, '+').replace(/_/g, '/')
const buffer = Buffer.from(base64Data, 'base64')
downloadedAttachments.push({
name: attachment.filename,
data: buffer.toString('base64'),
mimeType: attachment.mimeType,
size: attachment.size,
})
} catch (error) {
// Continue with other attachments
}
}
return downloadedAttachments
}
// Helper function to create a summary of multiple messages
export function createMessagesSummary(messages: any[]): string {
if (messages.length === 0) {
return 'No messages found.'
}
let summary = `Found ${messages.length} messages:\n\n`
messages.forEach((msg, index) => {
summary += `${index + 1}. Subject: ${msg.subject}\n`
summary += ` From: ${msg.from}\n`
summary += ` To: ${msg.to}\n`
summary += ` Date: ${msg.date}\n`
summary += ` ID: ${msg.id}\n`
summary += ` Thread ID: ${msg.threadId}\n`
summary += ` Preview: ${msg.snippet}\n\n`
})
summary += `To read full content of a specific message, use the gmail_read tool with messageId: ${messages.map((m) => m.id).join(', ')}`
return summary
}
/**
* Generate a unique MIME boundary string
*/
function generateBoundary(): string {
return `----=_Part_${Date.now()}_${generateRandomString(13)}`
}
/**
* Encode a header value using RFC 2047 Base64 encoding if it contains non-ASCII characters.
* This matches Google's own Gmail API sample: `=?utf-8?B?${Buffer.from(subject).toString('base64')}?=`
* @see https://github.com/googleapis/google-api-nodejs-client/blob/main/samples/gmail/send.js
*/
export function encodeRfc2047(value: string): string {
// eslint-disable-next-line no-control-regex
if (/^[\x00-\x7F]*$/.test(value)) {
return value
}
return `=?UTF-8?B?${Buffer.from(value, 'utf-8').toString('base64')}?=`
}
/**
* Strips CR/LF so a value can't introduce extra lines when placed into a MIME header.
*/
export function sanitizeHeaderValue(value: string): string {
return value.replace(/[\r\n]+/g, ' ')
}
/**
* Encode string or buffer to base64url format (URL-safe base64)
* Gmail API requires base64url encoding for the raw message field
*/
export function base64UrlEncode(data: string | Buffer): string {
const base64 = Buffer.from(data).toString('base64')
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
}
/**
* Escape HTML special characters so user-supplied text renders safely inside an HTML body.
*/
export function escapeHtml(value: string): string {
return value
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
}
/**
* Convert a plain-text body to an HTML body that flows naturally in Gmail.
* Newlines become `<br>` rather than per-paragraph `<p>` tags or a CSS
* `white-space` rule — `<p>` margins are what Gmail's "Remove formatting"
* button strips, and `white-space` has inconsistent support across email
* clients (including Gmail for non-Google accounts). Plain `<br>` line
* breaks are the standard, client-agnostic way to preserve plain-text
* formatting in an HTML alternative part.
* This avoids the narrow hard-wrapped rendering Gmail uses for `text/plain`.
*/
export function plainTextToHtml(body: string): string {
const normalized = body.replace(/\r\n/g, '\n').replace(/\r/g, '\n')
const escaped = escapeHtml(normalized).replace(/\n/g, '<br>')
return `<!DOCTYPE html><html><body>${escaped}</body></html>`
}
/**
* Best-effort conversion of an HTML body to a plain-text fallback. Used so we
* always include a plain-text part alongside HTML for clients that don't render
* HTML. Delegates to the `html-to-text` library for robust tag stripping and
* entity decoding (also used elsewhere in the repo for the same purpose).
*/
export function htmlToPlainText(html: string): string {
return convert(html, {
wordwrap: false,
selectors: [
{ selector: 'a', options: { hideLinkHrefIfSameAsText: true, noAnchorUrl: true } },
{ selector: 'img', format: 'skip' },
{ selector: 'script', format: 'skip' },
{ selector: 'style', format: 'skip' },
],
})
}
/**
* Produce the plain-text and HTML representations of a body so we can emit a
* `multipart/alternative` section. Gmail renders the HTML version full-width
* (matching how a manually-composed email looks); the plain-text part is the
* fallback for clients that don't render HTML.
*/
export function buildBodyAlternatives(
body: string,
contentType: 'text' | 'html' | undefined
): { plain: string; html: string } {
if (contentType === 'html') {
return { plain: htmlToPlainText(body) || body, html: body }
}
return { plain: body, html: plainTextToHtml(body) }
}
/**
* Encode a text body as base64 with RFC 2045 line wrapping (max 76 chars).
* Using base64 lets us safely transport arbitrary UTF-8 (emoji, accented
* characters, etc.) — `7bit` is only valid for strict 7-bit ASCII.
*/
function encodeBodyBase64(content: string): string[] {
const base64 = Buffer.from(content, 'utf-8').toString('base64')
return base64.match(/.{1,76}/g) || ['']
}
/**
* Render the inner part of a `multipart/alternative` section (text/plain
* followed by text/html, per RFC 2046 — clients pick the last format they
* understand).
*/
function renderAlternativeParts(plain: string, html: string, boundary: string): string[] {
return [
`--${boundary}`,
'Content-Type: text/plain; charset="UTF-8"',
'Content-Transfer-Encoding: base64',
'',
...encodeBodyBase64(plain),
'',
`--${boundary}`,
'Content-Type: text/html; charset="UTF-8"',
'Content-Transfer-Encoding: base64',
'',
...encodeBodyBase64(html),
'',
`--${boundary}--`,
]
}
/**
* Build a `multipart/alternative` email (without attachments). Always emits
* both text/plain and text/html parts so Gmail renders messages full-width
* like a hand-composed email.
* @returns Base64url encoded raw message
*/
export function buildSimpleEmailMessage(params: {
to: string
cc?: string | null
bcc?: string | null
subject?: string | null
body: string
contentType?: 'text' | 'html'
inReplyTo?: string
references?: string
}): string {
const { to, cc, bcc, subject, body, contentType, inReplyTo, references } = params
const boundary = generateBoundary()
const { plain, html } = buildBodyAlternatives(body, contentType)
const emailHeaders = ['MIME-Version: 1.0', `To: ${sanitizeHeaderValue(to)}`]
if (cc) {
emailHeaders.push(`Cc: ${sanitizeHeaderValue(cc)}`)
}
if (bcc) {
emailHeaders.push(`Bcc: ${sanitizeHeaderValue(bcc)}`)
}
emailHeaders.push(`Subject: ${encodeRfc2047(sanitizeHeaderValue(subject || ''))}`)
if (inReplyTo) {
const sanitizedInReplyTo = sanitizeHeaderValue(inReplyTo)
emailHeaders.push(`In-Reply-To: ${sanitizedInReplyTo}`)
const referencesChain = references
? `${sanitizeHeaderValue(references)} ${sanitizedInReplyTo}`
: sanitizedInReplyTo
emailHeaders.push(`References: ${referencesChain}`)
}
emailHeaders.push(`Content-Type: multipart/alternative; boundary="${boundary}"`)
emailHeaders.push('')
emailHeaders.push(...renderAlternativeParts(plain, html, boundary))
const email = emailHeaders.join('\n')
return Buffer.from(email).toString('base64url')
}
/**
* Build a MIME multipart message with optional attachments
* @param params Message parameters including recipients, subject, body, and attachments
* @returns Complete MIME message string ready to be base64url encoded
*/
export interface BuildMimeMessageParams {
to: string
cc?: string
bcc?: string
subject?: string
body: string
contentType?: 'text' | 'html'
inReplyTo?: string
references?: string
attachments?: Array<{
filename: string
mimeType: string
content: Buffer
}>
}
export function buildMimeMessage(params: BuildMimeMessageParams): string {
const { to, cc, bcc, subject, body, contentType, inReplyTo, references, attachments } = params
const messageParts: string[] = []
const { plain, html } = buildBodyAlternatives(body, contentType)
messageParts.push(`To: ${sanitizeHeaderValue(to)}`)
if (cc) {
messageParts.push(`Cc: ${sanitizeHeaderValue(cc)}`)
}
if (bcc) {
messageParts.push(`Bcc: ${sanitizeHeaderValue(bcc)}`)
}
messageParts.push(`Subject: ${encodeRfc2047(sanitizeHeaderValue(subject || ''))}`)
const sanitizedInReplyTo = inReplyTo ? sanitizeHeaderValue(inReplyTo) : undefined
const sanitizedReferences = references ? sanitizeHeaderValue(references) : undefined
if (sanitizedInReplyTo) {
messageParts.push(`In-Reply-To: ${sanitizedInReplyTo}`)
}
if (sanitizedReferences) {
const referencesChain = sanitizedInReplyTo
? `${sanitizedReferences} ${sanitizedInReplyTo}`
: sanitizedReferences
messageParts.push(`References: ${referencesChain}`)
} else if (sanitizedInReplyTo) {
messageParts.push(`References: ${sanitizedInReplyTo}`)
}
messageParts.push('MIME-Version: 1.0')
if (attachments && attachments.length > 0) {
const mixedBoundary = generateBoundary()
const altBoundary = generateBoundary()
messageParts.push(`Content-Type: multipart/mixed; boundary="${mixedBoundary}"`)
messageParts.push('')
messageParts.push(`--${mixedBoundary}`)
messageParts.push(`Content-Type: multipart/alternative; boundary="${altBoundary}"`)
messageParts.push('')
messageParts.push(...renderAlternativeParts(plain, html, altBoundary))
messageParts.push('')
for (const attachment of attachments) {
messageParts.push(`--${mixedBoundary}`)
messageParts.push(`Content-Type: ${sanitizeHeaderValue(attachment.mimeType)}`)
messageParts.push(
`Content-Disposition: attachment; filename="${sanitizeHeaderValue(attachment.filename)}"`
)
messageParts.push('Content-Transfer-Encoding: base64')
messageParts.push('')
const base64Content = attachment.content.toString('base64')
const lines = base64Content.match(/.{1,76}/g) || []
messageParts.push(...lines)
messageParts.push('')
}
messageParts.push(`--${mixedBoundary}--`)
} else {
const altBoundary = generateBoundary()
messageParts.push(`Content-Type: multipart/alternative; boundary="${altBoundary}"`)
messageParts.push('')
messageParts.push(...renderAlternativeParts(plain, html, altBoundary))
}
return messageParts.join('\n')
}