Module: Xeno::Arguments
- Defined in:
- lib/xeno/arguments.rb
Overview
Models send JSON-typed arguments loosely — the first field run had llama sending "4200" (String) for an integer-typed parameter, which flowed into execute unchecked. Casts each argument per the tool's declared parameter type; anything uncoercible becomes a validation error the runner turns into an error tool result (the model retries with fixed arguments) — never an exception into execute.
Class Method Summary collapse
- .boolean(value) ⇒ Object
- .cast(value, type) ⇒ Object
-
.coerce(tool_class, arguments) ⇒ Object
Returns [coerced_arguments, errors].
- .scalar_to_string(value) ⇒ Object
Class Method Details
.boolean(value) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/xeno/arguments.rb', line 47 def boolean(value) case value when true, false then value when "true", "1", 1 then true when "false", "0", 0 then false else raise TypeError end end |
.cast(value, type) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/xeno/arguments.rb', line 35 def cast(value, type) case type when "integer" then value.is_a?(Integer) ? value : Integer(value, exception: true) when "number" then value.is_a?(Numeric) ? value : Float(value) when "boolean" then boolean(value) when "string" then value.is_a?(String) ? value : scalar_to_string(value) when "array" then value.is_a?(Array) ? value : raise(TypeError) when "object" then value.is_a?(Hash) ? value : raise(TypeError) else value end end |
.coerce(tool_class, arguments) ⇒ Object
Returns [coerced_arguments, errors]. Undeclared keys pass through untouched (schema-based tools declare nothing here).
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/xeno/arguments.rb', line 13 def coerce(tool_class, arguments) declared = tool_class.respond_to?(:declared_parameters) ? tool_class.declared_parameters : {} return [ arguments, [] ] if declared.empty? || !arguments.is_a?(Hash) coerced = {} errors = [] arguments.each do |key, value| parameter = declared[key.to_sym] if parameter.nil? || value.nil? coerced[key] = value next end begin coerced[key] = cast(value, parameter.type.to_s) rescue ArgumentError, TypeError errors << "#{key}: expected #{parameter.type}, got #{value.inspect}" end end [ coerced, errors ] end |
.scalar_to_string(value) ⇒ Object
56 57 58 59 60 |
# File 'lib/xeno/arguments.rb', line 56 def scalar_to_string(value) raise TypeError if value.is_a?(Array) || value.is_a?(Hash) value.to_s end |