@@ -717,14 +717,14 @@ async function* streamInternal<TContext, TOutput = undefined>(
717717 }
718718 }
719719
720- if ( ! gotDone ) {
720+ if ( ! gotDone || ! finalResponse ) {
721721 throw new StratusError ( "Stream ended without a done event" ) ;
722722 }
723723
724724 // Fire onLlmEnd hooks
725725 const llmEndInfo = {
726- content : finalResponse ? .content ,
727- toolCallCount : finalResponse ? .toolCalls . length ,
726+ content : finalResponse . content ,
727+ toolCallCount : finalResponse . toolCalls . length ,
728728 } ;
729729 if ( currentAgent . hooks . onLlmEnd ) {
730730 await currentAgent . hooks . onLlmEnd ( {
@@ -742,12 +742,12 @@ async function* streamInternal<TContext, TOutput = undefined>(
742742 }
743743
744744 checkAborted ( signal ) ;
745- lastFinishReason = finalResponse ? .finishReason ;
746- if ( finalResponse ? .responseId ) lastResponseId = finalResponse ? .responseId ;
747- ctx . addUsage ( finalResponse ? .usage ) ;
745+ lastFinishReason = finalResponse . finishReason ;
746+ if ( finalResponse . responseId ) lastResponseId = finalResponse . responseId ;
747+ ctx . addUsage ( finalResponse . usage ) ;
748748 ctx . numTurns ++ ;
749749
750- applyTurnCost ( ctx , finalResponse ? .usage , costEstimator ) ;
750+ applyTurnCost ( ctx , finalResponse . usage , costEstimator ) ;
751751
752752 // Check budget after each model call
753753 try {
@@ -765,16 +765,16 @@ async function* streamInternal<TContext, TOutput = undefined>(
765765
766766 const assistantMsg : AssistantMessage = {
767767 role : "assistant" ,
768- content : finalResponse ? .content ,
769- ...( finalResponse ? .toolCalls . length > 0 ? { tool_calls : finalResponse ? .toolCalls } : { } ) ,
768+ content : finalResponse . content ?? null ,
769+ ...( finalResponse . toolCalls . length > 0 ? { tool_calls : finalResponse . toolCalls } : { } ) ,
770770 } ;
771771 messages . push ( assistantMsg ) ;
772772
773- if ( finalResponse ? .toolCalls . length === 0 ) {
773+ if ( finalResponse . toolCalls . length === 0 ) {
774774 if ( runHooks ?. onAgentEnd ) {
775775 await runHooks . onAgentEnd ( {
776776 agent : currentAgent ,
777- output : finalResponse ? .content ?? "" ,
777+ output : finalResponse . content ?? "" ,
778778 context : ctx . context ,
779779 } ) ;
780780 }
@@ -795,7 +795,7 @@ async function* streamInternal<TContext, TOutput = undefined>(
795795 const { toolMessages, handoffAgent } = await executeToolCallsWithHandoffs (
796796 currentAgent ,
797797 ctx ,
798- finalResponse ? .toolCalls ,
798+ finalResponse . toolCalls ,
799799 trace ,
800800 signal ,
801801 toolErrorFmt ,
@@ -806,7 +806,7 @@ async function* streamInternal<TContext, TOutput = undefined>(
806806 messages . push ( ...toolMessages ) ;
807807
808808 // Check toolUseBehavior
809- if ( await shouldStopAfterToolCalls ( currentAgent , finalResponse ? .toolCalls , toolMessages ) ) {
809+ if ( await shouldStopAfterToolCalls ( currentAgent , finalResponse . toolCalls , toolMessages ) ) {
810810 const toolOutput = toolMessages . map ( ( m ) => m . content ) . join ( "\n" ) ;
811811 resolveResult (
812812 new RunResult < TOutput > ( {
0 commit comments