@@ -42,6 +42,7 @@ import { codebaseSearchTool } from "../tools/codebaseSearchTool"
4242import { experiments , EXPERIMENT_IDS } from "../../shared/experiments"
4343import { applyDiffToolLegacy } from "../tools/applyDiffTool"
4444import { yieldPromise } from "../kilocode"
45+ import Anthropic from "@anthropic-ai/sdk" // kilocode_change
4546
4647/**
4748 * Processes and presents assistant message content to the user interface.
@@ -60,7 +61,7 @@ import { yieldPromise } from "../kilocode"
6061 * as it becomes available.
6162 */
6263
63- export async function presentAssistantMessage ( cline : Task , recursionDepth : number = 0 /*kilocode_change*/ ) {
64+ export async function presentAssistantMessage ( cline : Task ) {
6465 if ( cline . abort ) {
6566 throw new Error ( `[Task#presentAssistantMessage] task ${ cline . taskId } .${ cline . instanceId } aborted` )
6667 }
@@ -247,16 +248,26 @@ export async function presentAssistantMessage(cline: Task, recursionDepth: numbe
247248 }
248249 }
249250
251+ const pushToolResult_withToolUseId_kilocode = (
252+ ...items : ( Anthropic . TextBlockParam | Anthropic . ImageBlockParam ) [ ]
253+ ) => {
254+ if ( block . toolUseId ) {
255+ cline . userMessageContent . push ( { type : "tool_result" , tool_use_id : block . toolUseId , content : items } )
256+ } else {
257+ cline . userMessageContent . push ( ...items )
258+ }
259+ }
260+
250261 if ( cline . didRejectTool ) {
251262 // Ignore any tool content after user has rejected tool once.
252263 if ( ! block . partial ) {
253- cline . userMessageContent . push ( {
264+ pushToolResult_withToolUseId_kilocode ( {
254265 type : "text" ,
255266 text : `Skipping tool ${ toolDescription ( ) } due to user rejecting a previous tool.` ,
256267 } )
257268 } else {
258269 // Partial tool after user rejected a previous tool.
259- cline . userMessageContent . push ( {
270+ pushToolResult_withToolUseId_kilocode ( {
260271 type : "text" ,
261272 text : `Tool ${ toolDescription ( ) } was interrupted and not executed due to user rejecting a previous tool.` ,
262273 } )
@@ -267,7 +278,7 @@ export async function presentAssistantMessage(cline: Task, recursionDepth: numbe
267278
268279 if ( cline . didAlreadyUseTool ) {
269280 // Ignore any content after a tool has already been used.
270- cline . userMessageContent . push ( {
281+ pushToolResult_withToolUseId_kilocode ( {
271282 type : "text" ,
272283 text : `Tool [${ block . name } ] was not executed because a tool has already been used in this message. Only one tool may be used per message. You must assess the first tool's result before proceeding to use the next tool.` ,
273284 } )
@@ -276,13 +287,17 @@ export async function presentAssistantMessage(cline: Task, recursionDepth: numbe
276287 }
277288
278289 const pushToolResult = ( content : ToolResponse ) => {
279- cline . userMessageContent . push ( { type : "text" , text : `${ toolDescription ( ) } Result:` } )
290+ // kilocode_change start
291+ const items = new Array < Anthropic . TextBlockParam | Anthropic . ImageBlockParam > ( )
292+ items . push ( { type : "text" , text : `${ toolDescription ( ) } Result:` } )
280293
281294 if ( typeof content === "string" ) {
282- cline . userMessageContent . push ( { type : "text" , text : content || "(tool did not return anything)" } )
295+ items . push ( { type : "text" , text : content || "(tool did not return anything)" } )
283296 } else {
284- cline . userMessageContent . push ( ...content )
297+ items . push ( ...content )
285298 }
299+ pushToolResult_withToolUseId_kilocode ( ...items )
300+ // kilocode_change end
286301
287302 // Once a tool result has been collected, ignore all other tool
288303 // uses since we should only ever present one tool result per
@@ -414,7 +429,7 @@ export async function presentAssistantMessage(cline: Task, recursionDepth: numbe
414429
415430 if ( response === "messageResponse" ) {
416431 // Add user feedback to userContent.
417- cline . userMessageContent . push (
432+ pushToolResult_withToolUseId_kilocode (
418433 {
419434 type : "text" as const ,
420435 text : `Tool repetition limit reached. User feedback: ${ text } ` ,
@@ -637,7 +652,7 @@ export async function presentAssistantMessage(cline: Task, recursionDepth: numbe
637652 // this function ourselves.
638653 // kilocode_change start: prevent excessive recursion
639654 await yieldPromise ( )
640- await presentAssistantMessage ( cline , recursionDepth + 1 )
655+ await presentAssistantMessage ( cline )
641656 // kilocode_change end
642657 return
643658 }
@@ -647,7 +662,7 @@ export async function presentAssistantMessage(cline: Task, recursionDepth: numbe
647662 if ( cline . presentAssistantMessageHasPendingUpdates ) {
648663 // kilocode_change start: prevent excessive recursion
649664 await yieldPromise ( )
650- await presentAssistantMessage ( cline , recursionDepth + 1 )
665+ await presentAssistantMessage ( cline )
651666 // kilocode_change end
652667 }
653668}
0 commit comments