@@ -80,8 +80,9 @@ export function parseAssistantMessageV2(assistantMessage: string): AssistantMess
8080 currentParamValueStart , // Start after the opening tag.
8181 currentCharIndex - closeTag . length + 1 , // End before the closing tag.
8282 )
83- // Don't trim content parameters to preserve newlines
84- currentToolUse . params [ currentParamName ] = currentParamName === "content" ? value : value . trim ( )
83+ // Don't trim content parameters to preserve newlines, but strip first and last newline only
84+ currentToolUse . params [ currentParamName ] =
85+ currentParamName === "content" ? value . replace ( / ^ \n / , "" ) . replace ( / \n $ / , "" ) : value . trim ( )
8586 currentParamName = undefined // Go back to parsing tool content.
8687 // We don't continue loop here, need to check for tool close or other params at index i.
8788 } else {
@@ -145,8 +146,11 @@ export function parseAssistantMessageV2(assistantMessage: string): AssistantMess
145146 const contentEnd = toolContentSlice . lastIndexOf ( contentEndTag )
146147
147148 if ( contentStart !== - 1 && contentEnd !== - 1 && contentEnd > contentStart ) {
148- // Don't trim content to preserve newlines
149- const contentValue = toolContentSlice . slice ( contentStart + contentStartTag . length , contentEnd )
149+ // Don't trim content to preserve newlines, but strip first and last newline only
150+ const contentValue = toolContentSlice
151+ . slice ( contentStart + contentStartTag . length , contentEnd )
152+ . replace ( / ^ \n / , "" )
153+ . replace ( / \n $ / , "" )
150154 currentToolUse . params [ contentParamName ] = contentValue
151155 }
152156 }
@@ -249,8 +253,9 @@ export function parseAssistantMessageV2(assistantMessage: string): AssistantMess
249253 // Finalize any open parameter within an open tool use.
250254 if ( currentToolUse && currentParamName ) {
251255 const value = assistantMessage . slice ( currentParamValueStart ) // From param start to end of string.
252- // Don't trim content parameters to preserve newlines
253- currentToolUse . params [ currentParamName ] = currentParamName === "content" ? value : value . trim ( )
256+ // Don't trim content parameters to preserve newlines, but strip first and last newline only
257+ currentToolUse . params [ currentParamName ] =
258+ currentParamName === "content" ? value . replace ( / ^ \n / , "" ) . replace ( / \n $ / , "" ) : value . trim ( )
254259 // Tool use remains partial.
255260 }
256261
0 commit comments