@@ -47,6 +47,7 @@ export function parseAssistantMessageV2(assistantMessage: string): AssistantMess
4747 let currentToolUse : ToolUse | undefined = undefined
4848 let currentParamValueStart = 0
4949 let currentParamName : ToolParamName | undefined = undefined
50+ let parameterNestingDepth = 0
5051 let inFunctionCalls = false
5152
5253 const len = assistantMessage . length
@@ -56,19 +57,35 @@ export function parseAssistantMessageV2(assistantMessage: string): AssistantMess
5657
5758 // Inside function_calls block, handle parameters (check FIRST to avoid nested tag issues)
5859 if ( currentToolUse && currentParamName ) {
60+ // Check for nested <parameter name=" opening tags
61+ const paramOpenPattern = '<parameter name="'
62+ if (
63+ currentCharIndex >= paramOpenPattern . length - 1 &&
64+ assistantMessage . startsWith ( paramOpenPattern , currentCharIndex - paramOpenPattern . length + 1 )
65+ ) {
66+ parameterNestingDepth ++
67+ }
68+
69+ // Check for </parameter> closing tag
5970 const paramCloseTag = "</parameter>"
6071 if (
6172 currentCharIndex >= paramCloseTag . length - 1 &&
6273 assistantMessage . startsWith ( paramCloseTag , currentCharIndex - paramCloseTag . length + 1 )
6374 ) {
64- // Found the closing tag for the parameter
75+ if ( parameterNestingDepth > 0 ) {
76+ // This is a nested closing tag, decrement depth and continue
77+ parameterNestingDepth --
78+ continue
79+ }
80+ // This is the actual closing tag for our parameter
6581 const value = assistantMessage . slice (
6682 currentParamValueStart ,
6783 currentCharIndex - paramCloseTag . length + 1 ,
6884 )
6985 currentToolUse . params [ currentParamName ] =
7086 currentParamName === "content" ? value . replace ( / ^ \n / , "" ) . replace ( / \n $ / , "" ) : value . trim ( )
7187 currentParamName = undefined
88+ parameterNestingDepth = 0
7289 } else {
7390 continue // Still inside param value
7491 }
@@ -159,6 +176,7 @@ export function parseAssistantMessageV2(assistantMessage: string): AssistantMess
159176 if ( toolParamNames . includes ( paramName as ToolParamName ) ) {
160177 currentParamName = paramName as ToolParamName
161178 currentParamValueStart = currentCharIndex + 1
179+ parameterNestingDepth = 0
162180 }
163181 continue
164182 }
0 commit comments