@@ -129,9 +129,13 @@ function limitFirstUserSection(section: string): string {
129129 ) } ${ FIRST_USER_CONTEXT_TRUNCATION_MARKER } `;
130130}
131131
132- function formatThreadTitleContext ( messages : ReadonlyArray < ThreadTitleMessage > ) : {
133- readonly message : string ;
132+ function collectRecentThreadTitleContext (
133+ messages : ReadonlyArray < ThreadTitleMessage > ,
134+ maxChars : number ,
135+ ) : {
136+ readonly context : string ;
134137 readonly attachments : ReadonlyArray < ChatAttachment > ;
138+ readonly truncated : boolean ;
135139} {
136140 let context = "" ;
137141 let truncated = false ;
@@ -144,7 +148,7 @@ function formatThreadTitleContext(messages: ReadonlyArray<ThreadTitleMessage>):
144148 }
145149
146150 const separator = context . length > 0 ? "\n\n" : "" ;
147- const available = MAX_THREAD_TITLE_CONTEXT_CHARS - context . length - separator . length ;
151+ const available = maxChars - context . length - separator . length ;
148152 if ( section . length > available ) {
149153 if ( available > 0 ) {
150154 context = `${ section . slice ( - available ) } ${ separator } ${ context } ` ;
@@ -157,34 +161,48 @@ function formatThreadTitleContext(messages: ReadonlyArray<ThreadTitleMessage>):
157161 retainedAttachments . unshift ( ...( message . attachments ?? [ ] ) ) ;
158162 }
159163
160- let recentContext = context ;
161- const firstUserMessage = truncated
162- ? messages . find ( ( message ) => message . role === "user" && formatThreadTitleSection ( message ) )
164+ return { context, attachments : retainedAttachments , truncated } ;
165+ }
166+
167+ function formatThreadTitleContext ( messages : ReadonlyArray < ThreadTitleMessage > ) : {
168+ readonly message : string ;
169+ readonly attachments : ReadonlyArray < ChatAttachment > ;
170+ } {
171+ const recent = collectRecentThreadTitleContext ( messages , MAX_THREAD_TITLE_CONTEXT_CHARS ) ;
172+ if ( ! recent . truncated ) {
173+ return {
174+ message : recent . context ,
175+ attachments : recent . attachments . slice ( - MAX_REGENERATION_ATTACHMENTS ) ,
176+ } ;
177+ }
178+
179+ const firstUserMessage = messages . find (
180+ ( message ) => message . role === "user" && formatThreadTitleSection ( message ) ,
181+ ) ;
182+ const firstUserSection = firstUserMessage
183+ ? formatThreadTitleSection ( firstUserMessage )
163184 : undefined ;
164- if ( firstUserMessage ) {
165- const firstUserSection = formatThreadTitleSection ( firstUserMessage ) ;
166- if ( firstUserSection ) {
167- const pinnedSection = limitFirstUserSection ( firstUserSection ) ;
168- const recentContextBudget =
169- MAX_THREAD_TITLE_CONTEXT_CHARS -
170- pinnedSection . length -
171- "\n\n" . length -
172- THREAD_TITLE_CONTEXT_TRUNCATION_MARKER . length ;
173- recentContext = context . slice ( - recentContextBudget ) ;
174- context = `${ pinnedSection } \n\n${ THREAD_TITLE_CONTEXT_TRUNCATION_MARKER } ${ recentContext } ` ;
175- }
176- } else if ( truncated ) {
177- context = `${ THREAD_TITLE_CONTEXT_TRUNCATION_MARKER } ${ context } ` ;
185+ if ( ! firstUserMessage || ! firstUserSection ) {
186+ return {
187+ message : `${ THREAD_TITLE_CONTEXT_TRUNCATION_MARKER } ${ recent . context } ` ,
188+ attachments : recent . attachments . slice ( - MAX_REGENERATION_ATTACHMENTS ) ,
189+ } ;
178190 }
179191
180- const pinnedAttachment = firstUserMessage ?. attachments ?. [ 0 ] ;
181- const recentAttachments = retainedAttachments . filter (
182- ( attachment ) =>
183- attachment . id !== pinnedAttachment ?. id && recentContext . includes ( attachment . name ) ,
192+ const pinnedSection = limitFirstUserSection ( firstUserSection ) ;
193+ const recentContextBudget =
194+ MAX_THREAD_TITLE_CONTEXT_CHARS -
195+ pinnedSection . length -
196+ "\n\n" . length -
197+ THREAD_TITLE_CONTEXT_TRUNCATION_MARKER . length ;
198+ const retainedRecent = collectRecentThreadTitleContext ( messages , recentContextBudget ) ;
199+ const pinnedAttachment = firstUserMessage . attachments ?. [ 0 ] ;
200+ const recentAttachments = retainedRecent . attachments . filter (
201+ ( attachment ) => attachment . id !== pinnedAttachment ?. id ,
184202 ) ;
185203
186204 return {
187- message : context ,
205+ message : ` ${ pinnedSection } \n\n ${ THREAD_TITLE_CONTEXT_TRUNCATION_MARKER } ${ retainedRecent . context } ` ,
188206 attachments : [
189207 ...( pinnedAttachment ? [ pinnedAttachment ] : [ ] ) ,
190208 ...recentAttachments . slice (
0 commit comments