Convert a string value to its JSON-schema-typed equivalent.
| 69 | if (fn.contains("parameters") && fn["parameters"].is_object()) { |
| 70 | const auto & params = fn["parameters"]; |
| 71 | if (params.contains("properties") && params["properties"].is_object()) { |
| 72 | return params["properties"]; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | return json::object(); |
| 77 | } |
| 78 | |
| 79 | // Convert a string value to its JSON-schema-typed equivalent. |
| 80 | static json convert_param_value(const std::string & val, const std::string & key, |
| 81 | const json & props) { |
| 82 | if (val == "null") return nullptr; |
| 83 | if (!props.contains(key)) return val; |
| 84 | |
| 85 | const auto & cfg = props[key]; |
| 86 | std::string ptype = "string"; |
| 87 | if (cfg.is_object() && cfg.contains("type")) { |
| 88 | const auto & t = cfg["type"]; |
| 89 | if (t.is_string()) { |
| 90 | ptype = t.get<std::string>(); |
| 91 | } else if (t.is_array()) { |
| 92 | // JSON Schema allows "type": ["string","null"]; take the first |
| 93 | // non-null string entry instead of throwing. |
| 94 | for (const auto & e : t) { |
| 95 | if (e.is_string() && e.get<std::string>() != "null") { |
| 96 | ptype = e.get<std::string>(); |
| 97 | break; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // string types |
| 104 | if (ptype == "string" || ptype == "str" || ptype == "enum") return val; |
| 105 | |
| 106 | // integer types |
| 107 | if (ptype.substr(0, 3) == "int" || ptype == "integer") { |
| 108 | try { return std::stol(val); } catch (...) { return val; } |
| 109 | } |
| 110 | |
| 111 | // number / float |
| 112 | if (ptype == "number" || ptype.substr(0, 5) == "float") { |
| 113 | try { |
| 114 | double f = std::stod(val); |
| 115 | if (f == (double)(long)f) return (long)f; |
| 116 | return f; |