@@ -320,6 +320,8 @@ export type ChatTaskWirePayload<TMessage extends UIMessage = UIMessage, TMetadat
320320 continuation ?: boolean ;
321321 /** The run ID of the previous run (only set when `continuation` is true). */
322322 previousRunId ?: string ;
323+ /** Override warm timeout for this run (seconds). Set by transport.preload(). */
324+ warmTimeoutInSeconds ?: number ;
323325} ;
324326
325327/**
@@ -941,40 +943,26 @@ function chatTask<
941943
942944 // Wait for the first real message — use preload-specific timeouts if configured
943945 const effectivePreloadWarmTimeout =
944- ( metadata . get ( WARM_TIMEOUT_METADATA_KEY ) as number | undefined )
946+ payload . warmTimeoutInSeconds
945947 ?? preloadWarmTimeoutInSeconds
946948 ?? warmTimeoutInSeconds ;
947949
948- let firstMessage : ChatTaskWirePayload | undefined ;
950+ const effectivePreloadTimeout =
951+ ( metadata . get ( TURN_TIMEOUT_METADATA_KEY ) as string | undefined )
952+ ?? preloadTimeout
953+ ?? turnTimeout ;
949954
950- if ( effectivePreloadWarmTimeout > 0 ) {
951- const warm = await messagesInput . once ( {
952- timeoutMs : effectivePreloadWarmTimeout * 1000 ,
953- spanName : "preload wait (warm) " ,
954- } ) ;
955+ const preloadResult = await messagesInput . waitWithWarmup ( {
956+ warmTimeoutInSeconds : effectivePreloadWarmTimeout ,
957+ timeout : effectivePreloadTimeout ,
958+ spanName : "waiting for first message " ,
959+ } ) ;
955960
956- if ( warm . ok ) {
957- firstMessage = warm . output ;
958- }
961+ if ( ! preloadResult . ok ) {
962+ return ; // Timed out waiting for first message — end run
959963 }
960964
961- if ( ! firstMessage ) {
962- const effectivePreloadTimeout =
963- ( metadata . get ( TURN_TIMEOUT_METADATA_KEY ) as string | undefined )
964- ?? preloadTimeout
965- ?? turnTimeout ;
966-
967- const suspended = await messagesInput . wait ( {
968- timeout : effectivePreloadTimeout ,
969- spanName : "preload wait (suspended)" ,
970- } ) ;
971-
972- if ( ! suspended . ok ) {
973- return ; // Timed out waiting for first message — end run
974- }
975-
976- firstMessage = suspended . output ;
977- }
965+ let firstMessage = preloadResult . output ;
978966
979967 currentWirePayload = firstMessage ;
980968 }
@@ -1335,35 +1323,19 @@ function chatTask<
13351323 return "continue" ;
13361324 }
13371325
1338- // Phase 1: Keep the run warm for quick response to the next message.
1339- // The run stays active (using compute) during this window.
1326+ // Wait for the next message — stay warm briefly, then suspend
13401327 const effectiveWarmTimeout =
13411328 ( metadata . get ( WARM_TIMEOUT_METADATA_KEY ) as number | undefined ) ?? warmTimeoutInSeconds ;
1342-
1343- if ( effectiveWarmTimeout > 0 ) {
1344- const warm = await messagesInput . once ( {
1345- timeoutMs : effectiveWarmTimeout * 1000 ,
1346- spanName : "waiting (warm)" ,
1347- } ) ;
1348-
1349- if ( warm . ok ) {
1350- // Message arrived while warm — respond instantly
1351- currentWirePayload = warm . output ;
1352- return "continue" ;
1353- }
1354- }
1355-
1356- // Phase 2: Suspend the task (frees compute) until the next message arrives
13571329 const effectiveTurnTimeout =
13581330 ( metadata . get ( TURN_TIMEOUT_METADATA_KEY ) as string | undefined ) ?? turnTimeout ;
13591331
1360- const next = await messagesInput . wait ( {
1332+ const next = await messagesInput . waitWithWarmup ( {
1333+ warmTimeoutInSeconds : effectiveWarmTimeout ,
13611334 timeout : effectiveTurnTimeout ,
1362- spanName : "waiting (suspended) " ,
1335+ spanName : "waiting for next message " ,
13631336 } ) ;
13641337
13651338 if ( ! next . ok ) {
1366- // Timed out waiting for the next message — end the conversation
13671339 return "exit" ;
13681340 }
13691341
0 commit comments