From d18e110668625c7f462cefb0d9c968de90f48d19 Mon Sep 17 00:00:00 2001 From: "Steven T. Cramer" Date: Mon, 16 Jun 2025 22:56:21 +0700 Subject: [PATCH] refactor: extract duplicated insideToolParameter logic into helper method - Created isInsideToolParameter() helper method to eliminate code duplication - Method handles both open and close tag scenarios with optional tagName parameter - Maintains existing functionality while improving code maintainability - All tests continue to pass --- .../DirectiveStreamingParser.ts | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/core/message-parsing/DirectiveStreamingParser.ts b/src/core/message-parsing/DirectiveStreamingParser.ts index bd5038c0b1..176efa10e3 100644 --- a/src/core/message-parsing/DirectiveStreamingParser.ts +++ b/src/core/message-parsing/DirectiveStreamingParser.ts @@ -30,10 +30,7 @@ export class DirectiveStreamingParser { const insideCodeBlock = this.isInsideCodeBlock(context, activeHandler) // Check if we're inside a tool parameter (but not at the parameter level itself) - const insideToolParameter = - activeHandler && - activeHandler.constructor.name === "ToolDirectiveHandler" && - (activeHandler as any).currentContext === "param" + const insideToolParameter = this.isInsideToolParameter(activeHandler) // Only process XML tags if NOT inside code block AND NOT inside tool parameter if (!insideCodeBlock && !insideToolParameter) { @@ -64,11 +61,7 @@ export class DirectiveStreamingParser { const insideCodeBlock = this.isInsideCodeBlock(context, activeHandler) // Check if we're inside a tool parameter (but not at the parameter level itself) - const insideToolParameter = - activeHandler && - activeHandler.constructor.name === "ToolDirectiveHandler" && - (activeHandler as any).currentContext === "param" && - tagName !== (activeHandler as any).currentParamName + const insideToolParameter = this.isInsideToolParameter(activeHandler, tagName) if (!insideCodeBlock && !insideToolParameter) { // Normal XML processing @@ -134,6 +127,25 @@ export class DirectiveStreamingParser { ) } + /** + * Check if we're inside a tool parameter (but not at the parameter level itself) + */ + private static isInsideToolParameter(activeHandler: any, tagName?: string): boolean { + if (!activeHandler || activeHandler.constructor.name !== "ToolDirectiveHandler") { + return false + } + + const isInParamContext = (activeHandler as any).currentContext === "param" + + // For close tags, also check if this is not the parameter tag itself + if (tagName !== undefined) { + return isInParamContext && tagName !== (activeHandler as any).currentParamName + } + + // For open tags, just check if we're in param context + return isInParamContext + } + /** * Convert SAX node attributes to string representation */