@@ -95,27 +95,12 @@ function transformToolCallArgumentText(
9595 if ( ! isRecord ( value ) ) {
9696 return value ;
9797 }
98- const next : Record < string , unknown > = { } ;
99- for ( const [ key , entry ] of Object . entries ( value ) ) {
100- next [ key ] = transformToolCallArgumentText ( entry , replacements ) ;
101- }
102- return next ;
103- }
104-
105- function transformToolCallText (
106- toolCall : Record < string , unknown > ,
107- replacements ?: PluginTextReplacement [ ] ,
108- ) : Record < string , unknown > {
109- if ( ! replacements || replacements . length === 0 ) {
110- return toolCall ;
111- }
112- const next = { ...toolCall } ;
113- // Only transform arguments, not the tool name — renaming a tool can
114- // break execution by routing to an unknown or unintended tool.
115- if ( Object . hasOwn ( next , "arguments" ) ) {
116- next . arguments = transformToolCallArgumentText ( next . arguments , replacements ) ;
117- }
118- return next ;
98+ return Object . fromEntries (
99+ Object . entries ( value ) . map ( ( [ key , entry ] ) => [
100+ key ,
101+ transformToolCallArgumentText ( entry , replacements ) ,
102+ ] ) ,
103+ ) ;
119104}
120105
121106/** Apply input text replacements to a stream context. */
@@ -153,8 +138,16 @@ function transformAssistantEventText(
153138 if ( next . type === "text_end" && typeof next . content === "string" ) {
154139 next . content = applyPluginTextReplacements ( next . content , replacements ) ;
155140 }
156- if ( next . type === "toolcall_end" && isRecord ( next . toolCall ) ) {
157- next . toolCall = transformToolCallText ( next . toolCall , replacements ) ;
141+ if (
142+ next . type === "toolcall_end" &&
143+ isRecord ( next . toolCall ) &&
144+ Object . hasOwn ( next . toolCall , "arguments" )
145+ ) {
146+ // Tool names are routing identifiers; only argument values are text.
147+ next . toolCall = {
148+ ...next . toolCall ,
149+ arguments : transformToolCallArgumentText ( next . toolCall . arguments , replacements ) ,
150+ } ;
158151 }
159152 if ( Object . hasOwn ( next , "partial" ) ) {
160153 next . partial = transformMessageText ( next . partial , replacements ) ;
0 commit comments