77 parseMarkupToolCalls,
88 stripFencedCodeBlocks,
99 containsToolCallWrapperSyntaxOutsideIgnored,
10+ normalizeDSMLToolCallMarkup,
11+ hasRepairableXMLToolCallsWrapper,
12+ indexToolCDATAOpen,
1013 sanitizeLooseCDATA,
1114} = require ( './parse_payload' ) ;
1215
@@ -37,19 +40,23 @@ function parseToolCalls(text, toolNames) {
3740
3841function parseToolCallsDetailed ( text , toolNames ) {
3942 const result = emptyParseResult ( ) ;
40- const normalized = toStringSafe ( text ) ;
41- if ( ! normalized ) {
43+ const raw = toStringSafe ( text ) ;
44+ if ( ! raw ) {
4245 return result ;
4346 }
44- result . sawToolCallSyntax = looksLikeToolCallSyntax ( normalized ) ;
45- if ( shouldSkipToolCallParsingForCodeFenceExample ( normalized ) ) {
47+ if ( shouldSkipToolCallParsingForCodeFenceExample ( raw ) ) {
4648 return result ;
4749 }
50+ const normalized = normalizeDSMLToolCallMarkup ( stripFencedCodeBlocks ( raw ) . trim ( ) ) ;
51+ if ( ! normalized . ok || ! normalized . text ) {
52+ return result ;
53+ }
54+ result . sawToolCallSyntax = looksLikeToolCallSyntax ( normalized . text ) || hasRepairableXMLToolCallsWrapper ( normalized . text ) ;
4855 // XML markup parsing only.
49- let parsed = parseMarkupToolCalls ( normalized ) ;
50- if ( parsed . length === 0 && normalized . toLowerCase ( ) . includes ( '<![cdata[' ) ) {
51- const recovered = sanitizeLooseCDATA ( normalized ) ;
52- if ( recovered !== normalized ) {
56+ let parsed = parseMarkupToolCalls ( normalized . text ) ;
57+ if ( parsed . length === 0 && indexToolCDATAOpen ( normalized . text , 0 ) >= 0 ) {
58+ const recovered = sanitizeLooseCDATA ( normalized . text ) ;
59+ if ( recovered !== normalized . text ) {
5360 parsed = parseMarkupToolCalls ( recovered ) ;
5461 }
5562 }
@@ -70,19 +77,23 @@ function parseStandaloneToolCalls(text, toolNames) {
7077
7178function parseStandaloneToolCallsDetailed ( text , toolNames ) {
7279 const result = emptyParseResult ( ) ;
73- const trimmed = toStringSafe ( text ) ;
74- if ( ! trimmed ) {
80+ const raw = toStringSafe ( text ) ;
81+ if ( ! raw ) {
82+ return result ;
83+ }
84+ if ( shouldSkipToolCallParsingForCodeFenceExample ( raw ) ) {
7585 return result ;
7686 }
77- result . sawToolCallSyntax = looksLikeToolCallSyntax ( trimmed ) ;
78- if ( shouldSkipToolCallParsingForCodeFenceExample ( trimmed ) ) {
87+ const normalized = normalizeDSMLToolCallMarkup ( stripFencedCodeBlocks ( raw ) . trim ( ) ) ;
88+ if ( ! normalized . ok || ! normalized . text ) {
7989 return result ;
8090 }
91+ result . sawToolCallSyntax = looksLikeToolCallSyntax ( normalized . text ) || hasRepairableXMLToolCallsWrapper ( normalized . text ) ;
8192 // XML markup parsing only.
82- let parsed = parseMarkupToolCalls ( trimmed ) ;
83- if ( parsed . length === 0 && trimmed . toLowerCase ( ) . includes ( '<![cdata[' ) ) {
84- const recovered = sanitizeLooseCDATA ( trimmed ) ;
85- if ( recovered !== trimmed ) {
93+ let parsed = parseMarkupToolCalls ( normalized . text ) ;
94+ if ( parsed . length === 0 && indexToolCDATAOpen ( normalized . text , 0 ) >= 0 ) {
95+ const recovered = sanitizeLooseCDATA ( normalized . text ) ;
96+ if ( recovered !== normalized . text ) {
8697 parsed = parseMarkupToolCalls ( recovered ) ;
8798 }
8899 }
0 commit comments