@@ -4,6 +4,8 @@ import { fetchWithRetry, VALIDATE_RETRY_OPTIONS } from '@/lib/knowledge/document
44import { DEFAULT_MAX_THREADS , gmailConnectorMeta } from '@/connectors/gmail/meta'
55import type { ConnectorConfig , ExternalDocument , ExternalDocumentList } from '@/connectors/types'
66import {
7+ BoundedLines ,
8+ CONNECTOR_TEXT_DOCUMENT_MAX_BYTES ,
79 htmlToPlainText ,
810 joinTagArray ,
911 parseDefaultedUnlimitedSafeInteger ,
@@ -323,21 +325,17 @@ function formatThread(thread: GmailThread): {
323325 }
324326 const labelIds = [ ...labelIdSet ]
325327
326- const lines : string [ ] = [ ]
327- lines . push ( `Subject: ${ subject } ` )
328- lines . push ( `From: ${ from } ` )
328+ const lines = new BoundedLines ( )
329+ lines . push ( `Subject: ${ subject } ` , `From: ${ from } ` )
329330 if ( to ) lines . push ( `To: ${ to } ` )
330- lines . push ( `Messages: ${ messages . length } ` )
331- lines . push ( '' )
331+ lines . push ( `Messages: ${ messages . length } ` , '' )
332332
333333 for ( const msg of messages ) {
334334 const msgFrom = getHeader ( msg . payload , 'From' ) || 'Unknown'
335335 const msgDate = getHeader ( msg . payload , 'Date' ) || ''
336336 const body = msg . payload ? extractBody ( msg . payload ) : ''
337337
338- lines . push ( `--- ${ msgFrom } (${ msgDate } ) ---` )
339- lines . push ( body . trim ( ) )
340- lines . push ( '' )
338+ if ( ! lines . push ( `--- ${ msgFrom } (${ msgDate } ) ---` , body . trim ( ) , '' ) ) break
341339 }
342340
343341 const firstDate = firstMessage . internalDate
@@ -348,7 +346,7 @@ function formatThread(thread: GmailThread): {
348346 : undefined
349347
350348 return {
351- content : lines . join ( '\n' ) . trim ( ) ,
349+ content : lines . join ( ) . trim ( ) ,
352350 subject,
353351 metadata : {
354352 from,
@@ -398,13 +396,6 @@ async function resolveLabelNames(
398396 . filter ( ( name ) => ! name . startsWith ( 'CATEGORY_' ) && name !== 'UNREAD' )
399397}
400398
401- /**
402- * What a hydrated thread retains: message bodies as text, never attachments,
403- * so a generous ceiling for a long thread is still small. Lets a batch of
404- * threads hydrate together instead of one at a time.
405- */
406- const ESTIMATED_THREAD_BYTES = 256 * 1024
407-
408399/**
409400 * Creates a lightweight document stub from a thread list entry.
410401 * Uses metadata-based contentHash for change detection without downloading content.
@@ -419,7 +410,7 @@ function threadToStub(thread: {
419410 title : thread . snippet || 'Untitled Thread' ,
420411 content : '' ,
421412 contentDeferred : true ,
422- estimatedBytes : ESTIMATED_THREAD_BYTES ,
413+ estimatedBytes : CONNECTOR_TEXT_DOCUMENT_MAX_BYTES ,
423414 mimeType : 'text/plain' ,
424415 sourceUrl : threadUrl ( thread . id ) ,
425416 contentHash : `gmail:${ thread . id } :${ thread . historyId ?? '' } ` ,
0 commit comments