Class: Clacky::Utils::ArgumentsParser
- Inherits:
-
Object
- Object
- Clacky::Utils::ArgumentsParser
- Defined in:
- lib/clacky/utils/arguments_parser.rb
Class Method Summary collapse
- .build_error_message(call, tool, original_error) ⇒ Object
-
.extract_provided_params(json_str) ⇒ Object
Extract parameter names from incomplete JSON.
-
.format_tool_definition(tool) ⇒ Object
Format tool definition (concise version).
-
.parse_and_validate(call, tool_registry) ⇒ Object
Parse and validate tool call arguments with JSON repair capability.
-
.raise_helpful_error(call, tool_registry, original_error) ⇒ Object
Generate error message with tool definition.
-
.repair_json(json_str) ⇒ Object
Simple JSON repair: complete brackets and quotes, and remove XML contamination.
-
.schema_type(properties, key) ⇒ Object
Look up a parameter's declared JSON-Schema type, tolerating string/symbol keys in both
propertiesand the per-param spec hash. -
.undouble_serialize_args(args, properties = {}) ⇒ Object
Undo one layer of accidental double-serialization, guided by the tool's parameter schema.
-
.validate_required_params(call, args, tool_registry) ⇒ Object
Validate required parameters and filter unknown parameters.
Class Method Details
.build_error_message(call, tool, original_error) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/clacky/utils/arguments_parser.rb', line 212 def self.(call, tool, original_error) # Extract tool information required_params = tool.parameters&.dig(:required) || [] # Try to parse provided parameters from incomplete JSON provided_params = extract_provided_params(call[:arguments]) # Build clear error message msg = [] msg << "Failed to parse arguments for tool '#{call[:name]}'." msg << "" msg << "Error: #{original_error.}" msg << "" if provided_params.any? msg << "Provided parameters: #{provided_params.join(', ')}" else msg << "No valid parameters could be extracted." end msg << "Required parameters: #{required_params.join(', ')}" msg << "" msg << "Tool definition:" msg << format_tool_definition(tool) msg << "" msg << "Suggestions:" msg << "- If the parameter value is too large (e.g., large file content), consider breaking it into smaller operations" msg << "- Ensure all required parameters are provided" msg << "- Simplify complex parameter values" msg.join("\n") end |
.extract_provided_params(json_str) ⇒ Object
Extract parameter names from incomplete JSON
246 247 248 249 |
# File 'lib/clacky/utils/arguments_parser.rb', line 246 def self.extract_provided_params(json_str) # Simple extraction: find all "key": patterns json_str.scan(/"(\w+)"\s*:/).flatten.uniq end |
.format_tool_definition(tool) ⇒ Object
Format tool definition (concise version)
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/clacky/utils/arguments_parser.rb', line 252 def self.format_tool_definition(tool) lines = [] lines << " Name: #{tool.name}" lines << " Description: #{tool.description}" params = tool.parameters properties = params && params[:properties] if properties lines << " Parameters:" properties.each do |param, spec| required_mark = params[:required]&.include?(param.to_s) ? " (required)" : "" desc = spec.is_a?(Hash) ? spec[:description] : spec.to_s lines << " - #{param}#{required_mark}: #{desc}" end end lines.join("\n") end |
.parse_and_validate(call, tool_registry) ⇒ Object
Parse and validate tool call arguments with JSON repair capability
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/clacky/utils/arguments_parser.rb', line 9 def self.parse_and_validate(call, tool_registry) # 1. Try standard parsing begin args = JSON.parse(call[:arguments], symbolize_names: true) # Check if any key contains XML tags (< or >) indicating contamination # Even though JSON.parse succeeded, the keys might be malformed has_xml_contamination = args.keys.any? { |k| k.to_s.include?('<') || k.to_s.include?('>') } if has_xml_contamination # Force repair even though JSON.parse succeeded raise JSON::ParserError.new("Keys contain XML contamination") end return validate_required_params(call, args, tool_registry) rescue JSON::ParserError => e # Continue to repair end # 2. Try simple repair repaired = repair_json(call[:arguments]) begin args = JSON.parse(repaired, symbolize_names: true) return validate_required_params(call, args, tool_registry) rescue JSON::ParserError, MissingRequiredParamsError => e # 3. Repair failed or missing params, return helpful error raise_helpful_error(call, tool_registry, e) end end |
.raise_helpful_error(call, tool_registry, original_error) ⇒ Object
Generate error message with tool definition
206 207 208 209 210 |
# File 'lib/clacky/utils/arguments_parser.rb', line 206 def self.raise_helpful_error(call, tool_registry, original_error) tool = tool_registry.get(call[:name]) error_msg = (call, tool, original_error) raise BadArgumentsError, error_msg end |
.repair_json(json_str) ⇒ Object
Simple JSON repair: complete brackets and quotes, and remove XML contamination
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/clacky/utils/arguments_parser.rb', line 42 def self.repair_json(json_str) result = json_str.strip # Step 0: Convert literal \n (backslash+n) to real newlines result = result.gsub(/\\n/, "\n") # Step 0.5: Unescape quotes in JSON keys and values (\" -> ") # This handles cases like {"end_line\":550 or name=\"path\" result = result.gsub(/\\"/, '"') # Step 1: Remove XML-style parameter tags that Claude might mix in # Pattern 1: </parameter> closing tags - remove completely result = result.gsub(/<\/parameter>/, '') # Pattern 2: <parameter name="key"> or <parameter name="key": opening tags -> convert to JSON key # Example: \n<parameter name="end_line"> 330 -> , "end_line": 330 # Also handles: \n<parameter name="end_line": 330 -> , "end_line": 330 # result = result.gsub(/<parameter\s+name="([^"\\]+)":\s*/) { |match| ", \"#{$1}\": " } # result = result.gsub(/<parameter\s+name="([^"\\]+)">/) { |match| ", \"#{$1}\":" } result = result.gsub(/<parameter\s+name=\\?"([^"\\]+)\\?"[>:]?\s*/) { |match| ", \"#{$1}\": " } # Pattern 3: Remove any remaining XML-like tags result = result.gsub(/<[^>]+>/, '') # Step 2: Clean up newlines with commas # Example: 315\n, "end_line" -> 315, "end_line" result = result.gsub(/\n\s*,/, ',') result = result.gsub(/\n,/, ',') result = result.gsub(/,\s*\n/, ',') # Step 3: Clean up formatting issues # Remove multiple consecutive commas result = result.gsub(/,+/, ',') # Remove trailing commas before closing braces/brackets result = result.gsub(/,\s*}/, '}') result = result.gsub(/,\s*\]/, ']') # Remove leading commas after opening braces/brackets result = result.gsub(/\{\s*,/, '{') result = result.gsub(/\[\s*,/, '[') # Step 4: Complete unclosed strings result += '"' if result.count('"').odd? # Step 5: Close unclosed braces/brackets in the order they were opened. # Tracks a delimiter stack so truncated nested arrays (e.g. a cut-off # `questions` list) are closed as `}]}` rather than `}}}`. stack = [] in_string = false escaped = false result.each_char do |c| if escaped escaped = false next end if c == '\\' escaped = true if in_string next end if c == '"' in_string = !in_string next end next if in_string case c when '{' then stack.push('}') when '[' then stack.push(']') when '}', ']' then stack.pop if stack.last == c end end result += stack.reverse.join result end |
.schema_type(properties, key) ⇒ Object
Look up a parameter's declared JSON-Schema type, tolerating string/symbol
keys in both properties and the per-param spec hash.
174 175 176 177 178 |
# File 'lib/clacky/utils/arguments_parser.rb', line 174 def self.schema_type(properties, key) spec = properties[key] || properties[key.to_s] || properties[key.to_sym] return nil unless spec.is_a?(Hash) spec[:type] || spec["type"] end |
.undouble_serialize_args(args, properties = {}) ⇒ Object
Undo one layer of accidental double-serialization, guided by the tool's parameter schema.
Some LLMs (e.g. glm-5.2) emit array/object parameters as a JSON string (e.g. "task":"[\"a\",\"b\"]") instead of a native JSON value. JSON.parse only strips the outer layer, so such values arrive here as a literal String and break downstream tools that expect an Array/Hash.
To stay safe we rely on the declared schema type instead of a blind "looks like JSON" heuristic:
- a parameter declared `type: "string"` is NEVER parsed, so a value
that merely happens to be valid JSON text (file content, a code
snippet, a JSON blob to be written) is preserved verbatim;
- a parameter declared `type: "array"`/`"object"` that still arrives
as a String is unwrapped exactly once;
- additionally, when a concrete type IS declared, the parsed result
must match it (an array-typed param that receives a JSON object
string is left untouched rather than silently coerced to a Hash);
- a parameter with no declared `type` (e.g. flexible params such as
todo_manager's task/id, which may be a scalar or a list) falls back
to a conservative heuristic: unwrap only if the stripped value
starts with "["/"{" and parses cleanly.
Strings that fail JSON.parse are always left untouched.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/clacky/utils/arguments_parser.rb', line 141 def self.undouble_serialize_args(args, properties = {}) args.to_h do |key, value| expected = schema_type(properties, key) # Explicit string params: never unwrap (protects content / code / JSON text). next [key, value] if expected == "string" # Scalar-typed params (integer/number/boolean): no JSON shape to recover. next [key, value] if expected && expected != "array" && expected != "object" # For array/object/untyped params, only unwrap a String that looks like JSON. next [key, value] unless value.is_a?(String) stripped = value.strip next [key, value] unless stripped.start_with?("[") || stripped.start_with?("{") begin # symbolize_names keeps unwrapped Hash keys consistent with the # outer JSON.parse(call[:arguments], symbolize_names: true). parsed = JSON.parse(value, symbolize_names: true) # Type-match guard: if the schema declares a concrete type, the # parsed value must match it. Prevents silently turning a Hash into # an Array (or vice-versa) when an array-typed param receives a # JSON object string, etc. if expected == "array" && !parsed.is_a?(Array) next [key, value] elsif expected == "object" && !parsed.is_a?(Hash) next [key, value] end [key, parsed] rescue JSON::ParserError [key, value] end end end |
.validate_required_params(call, args, tool_registry) ⇒ Object
Validate required parameters and filter unknown parameters
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/clacky/utils/arguments_parser.rb', line 181 def self.validate_required_params(call, args, tool_registry) tool = tool_registry.get(call[:name]) required = tool.parameters&.dig(:required) || [] properties = tool.parameters&.dig(:properties) || {} # Undo accidental double-serialization BEFORE filtering, using the schema # so that parameters declared as "string" are never touched. args = undouble_serialize_args(args, properties) missing = required.reject { |param| args.key?(param.to_sym) || args.key?(param.to_s) } if missing.any? raise MissingRequiredParamsError.new(call[:name], missing, args.keys) end # Filter out unknown parameters to prevent errors when LLM sends extra arguments known_params = properties.keys.map(&:to_sym) + properties.keys.map(&:to_s) filtered_args = args.select { |key, _| known_params.include?(key) } filtered_args end |