55
66import { createOpenRouter } from "@openrouter/ai-sdk-provider" ;
77import { tool } from "ai" ;
8- import { ToolLoopAgent , stepCountIs } from "ai" ;
8+ import { type ModelMessage , ToolLoopAgent , stepCountIs } from "ai" ;
99import type { ToolSet } from "ai" ;
1010import { z } from "zod" ;
1111import { getDaemonManager } from "../../state/daemon-state" ;
1212import type { SubagentProgressEmitter } from "../../types" ;
1313import { getOpenRouterReportedCost } from "../../utils/openrouter-reported-cost" ;
14+ import { extractFinalAssistantText } from "../message-utils" ;
1415import { buildOpenRouterChatSettings , getSubagentModel } from "../model-config" ;
1516import { buildToolSet } from "./tool-registry" ;
1617
6061- The final summary needs to be self contained and needs to provide enough information to the main agent so it is clear what you have done and what the results are.
6162
6263Today's date: ${ new Date ( ) . toISOString ( ) . split ( "T" ) [ 0 ] }
63- ` ;
64+ `;
6465}
6566
6667// Global emitter that will be set by the daemon-ai module
@@ -109,7 +110,6 @@ Provide a concise summary for display and a very specific task description (espe
109110 stopWhen : stepCountIs ( MAX_SUBAGENT_STEPS ) ,
110111 } ) ;
111112
112- let responseText = "" ;
113113 let costTotal = 0 ;
114114 let hasCost = false ;
115115
@@ -119,9 +119,7 @@ Provide a concise summary for display and a very specific task description (espe
119119 } ) ;
120120
121121 for await ( const part of stream . fullStream ) {
122- if ( part . type === "text-delta" ) {
123- responseText += part . text ;
124- } else if ( part . type === "finish-step" ) {
122+ if ( part . type === "finish-step" ) {
125123 const reportedCost = getOpenRouterReportedCost ( part . providerMetadata ) ;
126124 if ( reportedCost !== undefined ) {
127125 costTotal += reportedCost ;
@@ -145,6 +143,9 @@ Provide a concise summary for display and a very specific task description (espe
145143 }
146144 }
147145
146+ const responseMessages = await stream . response . then ( ( response ) => response . messages ) ;
147+ const finalResponse = extractFinalAssistantText ( responseMessages ) ;
148+
148149 const streamUsage = await stream . usage ;
149150 if ( streamUsage ) {
150151 progressEmitter ?. onSubagentUsage ( {
@@ -163,7 +164,7 @@ Provide a concise summary for display and a very specific task description (espe
163164 return {
164165 success : true ,
165166 summary,
166- response : responseText || "Task completed but no text response generated." ,
167+ response : finalResponse || "Task completed but no text response generated." ,
167168 } ;
168169 } catch ( error ) {
169170 const errorMessage = error instanceof Error ? error . message : String ( error ) ;
0 commit comments