@@ -172,16 +172,37 @@ function formatTextPreview(texts: string[], maxChars = 800): string {
172172 return combined . length > maxChars ? `${ combined . slice ( 0 , maxChars ) } ...` : combined ;
173173}
174174
175+ function extractAssistantTexts ( messages : unknown [ ] ) : string [ ] {
176+ const texts : string [ ] = [ ] ;
177+ for ( const entry of messages ) {
178+ if ( ! entry || typeof entry !== "object" ) {
179+ continue ;
180+ }
181+ if ( ( entry as { role ?: unknown } ) . role !== "assistant" ) {
182+ continue ;
183+ }
184+ const text = extractVisibleMessageText ( entry ) ;
185+ if ( typeof text === "string" && text . trim ( ) . length > 0 ) {
186+ texts . push ( text ) ;
187+ }
188+ }
189+ return texts ;
190+ }
191+
175192async function waitForTrajectoryExportInstructionText ( params : {
193+ client : GatewayClient ;
176194 events : EventFrame [ ] ;
195+ eventStartIndex : number ;
177196 expectedText : string ;
178197 runId : string ;
198+ sessionKey : string ;
179199 timeoutMs : number ;
180200} ) : Promise < string > {
181201 const deadline = Date . now ( ) + params . timeoutMs ;
182202 let finalTexts : string [ ] = [ ] ;
183203 while ( Date . now ( ) < deadline ) {
184- finalTexts = params . events
204+ const newEvents = params . events . slice ( params . eventStartIndex ) ;
205+ finalTexts = newEvents
185206 . map ( ( event ) => extractChatFinalText ( event , params . runId ) )
186207 . filter ( ( text ) : text is string => typeof text === "string" && text . trim ( ) . length > 0 ) ;
187208 const matchedText = finalTexts . find ( ( text ) => text . includes ( params . expectedText ) ) ;
@@ -192,9 +213,23 @@ async function waitForTrajectoryExportInstructionText(params: {
192213 setTimeout ( resolve , 500 ) ;
193214 } ) ;
194215 }
216+ let assistantTexts : string [ ] ;
217+ try {
218+ const history = ( await params . client . request (
219+ "chat.history" ,
220+ {
221+ sessionKey : params . sessionKey ,
222+ limit : 24 ,
223+ } ,
224+ { timeoutMs : 10_000 } ,
225+ ) ) as { messages ?: unknown [ ] } ;
226+ assistantTexts = extractAssistantTexts ( history . messages ?? [ ] ) ;
227+ } catch {
228+ assistantTexts = [ ] ;
229+ }
195230 throw new Error (
196231 `timed out waiting for trajectory export instruction text for ${ params . runId } ; ` +
197- `events=${ params . events . length } ; finalTexts=${ formatTextPreview ( finalTexts ) } ` ,
232+ `events=${ params . events . length } ; finalTexts=${ formatTextPreview ( finalTexts ) } ; assistantTexts= ${ formatTextPreview ( assistantTexts ) } ` ,
198233 ) ;
199234}
200235
@@ -210,6 +245,10 @@ function extractChatFinalText(event: EventFrame, runId: string): string | undefi
210245 if ( record . runId !== runId || record . state !== "final" ) {
211246 return undefined ;
212247 }
248+ return extractChatFinalRecordText ( record ) ;
249+ }
250+
251+ function extractChatFinalRecordText ( record : Record < string , unknown > ) : string | undefined {
213252 const message = record . message ;
214253 if ( ! message || typeof message !== "object" ) {
215254 return undefined ;
@@ -395,6 +434,7 @@ describeLive("gateway live trajectory export", () => {
395434 const bundleDir = path . join ( workspaceDir , ".openclaw" , "trajectory-exports" , "bundle" ) ;
396435 const beforeExport = new Set ( await listDirectoryNames ( tempDir ) ) ;
397436 const exportRunId = `chat-export-${ randomUUID ( ) } ` ;
437+ const exportEventStartIndex = gatewayEvents . length ;
398438 logLiveStep ( "export:start" , { bundleDir, exportRunId } ) ;
399439 const exportResponse = ( await client . request (
400440 "chat.send" ,
@@ -415,9 +455,12 @@ describeLive("gateway live trajectory export", () => {
415455 typeof exportResponse ?. message === "object"
416456 ? extractVisibleMessageText ( exportResponse . message )
417457 : await waitForTrajectoryExportInstructionText ( {
458+ client,
418459 events : gatewayEvents ,
460+ eventStartIndex : exportEventStartIndex ,
419461 expectedText : "Trajectory exports can include" ,
420462 runId : exportRunId ,
463+ sessionKey,
421464 timeoutMs : 60_000 ,
422465 } ) ;
423466 expect ( finalText ) . toContain ( "Trajectory exports can include" ) ;
0 commit comments