-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodels_dev_catalog.cpp
More file actions
349 lines (310 loc) · 11.6 KB
/
Copy pathmodels_dev_catalog.cpp
File metadata and controls
349 lines (310 loc) · 11.6 KB
1
2
3
4
5
6
7
8
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
39
40
41
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "models_dev_catalog.hpp"
#include "../provider/models_dev_registry.hpp"
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <mutex>
#include <sstream>
namespace acecode {
namespace {
std::optional<int> as_int(const nlohmann::json& v) {
if (v.is_number_integer()) return v.get<int>();
if (v.is_number_unsigned()) return static_cast<int>(v.get<unsigned int>());
if (v.is_number_float()) return static_cast<int>(v.get<double>());
return std::nullopt;
}
std::optional<double> as_double(const nlohmann::json& v) {
if (v.is_number()) return v.get<double>();
return std::nullopt;
}
std::vector<std::string> as_string_array(const nlohmann::json& v) {
std::vector<std::string> out;
if (!v.is_array()) return out;
out.reserve(v.size());
for (const auto& item : v) {
if (item.is_string()) out.push_back(item.get<std::string>());
}
return out;
}
std::optional<std::string> first_string(const nlohmann::json& obj,
std::initializer_list<const char*> keys) {
if (!obj.is_object()) return std::nullopt;
for (const char* k : keys) {
auto it = obj.find(k);
if (it != obj.end() && it->is_string()) {
std::string v = it->get<std::string>();
if (!v.empty()) return v;
}
}
return std::nullopt;
}
bool first_bool(const nlohmann::json& obj, const char* key, bool fallback) {
auto it = obj.find(key);
if (it != obj.end() && it->is_boolean()) return it->get<bool>();
return fallback;
}
ModelEntry parse_model(const std::string& id, const nlohmann::json& mj) {
ModelEntry m;
m.id = id;
if (auto name = first_string(mj, {"name"})) m.name = *name;
if (m.name.empty()) m.name = id;
auto limit_it = mj.find("limit");
if (limit_it != mj.end() && limit_it->is_object()) {
if (auto ctx = limit_it->find("context"); ctx != limit_it->end())
m.context = as_int(*ctx);
if (auto out = limit_it->find("output"); out != limit_it->end())
m.max_output = as_int(*out);
}
auto cost_it = mj.find("cost");
if (cost_it != mj.end() && cost_it->is_object()) {
if (auto ci = cost_it->find("input"); ci != cost_it->end())
m.cost_input = as_double(*ci);
if (auto co = cost_it->find("output"); co != cost_it->end())
m.cost_output = as_double(*co);
}
m.reasoning = first_bool(mj, "reasoning", false);
auto parse_reasoning_option = [&](const nlohmann::json& option) {
if (!option.is_object()) return;
const std::string type = option.value("type", std::string{});
if (type == "toggle") {
m.reasoning_can_disable = true;
} else if (type == "effort") {
if (const auto values = option.find("values"); values != option.end()) {
static const std::vector<std::string> kCanonicalEfforts{
"minimal", "low", "medium", "high", "xhigh", "max"};
for (const auto& value : as_string_array(*values)) {
if (value == "none") {
m.reasoning_can_disable = true;
continue;
}
if (std::find(kCanonicalEfforts.begin(),
kCanonicalEfforts.end(), value) ==
kCanonicalEfforts.end()) {
continue;
}
if (std::find(m.reasoning_efforts.begin(),
m.reasoning_efforts.end(), value) ==
m.reasoning_efforts.end()) {
m.reasoning_efforts.push_back(value);
}
}
}
} else if (type == "budget_tokens") {
m.reasoning_supports_max_tokens = true;
}
};
if (auto options_it = mj.find("reasoning_options"); options_it != mj.end()) {
if (options_it->is_object()) {
parse_reasoning_option(*options_it);
} else if (options_it->is_array()) {
for (const auto& option : *options_it) parse_reasoning_option(option);
}
}
m.tool_call = first_bool(mj, "tool_call", false);
m.attachment = first_bool(mj, "attachment", false);
m.deprecated = first_bool(mj, "deprecated", false);
auto mod_it = mj.find("modalities");
if (mod_it != mj.end() && mod_it->is_object()) {
if (auto ii = mod_it->find("input"); ii != mod_it->end())
m.input_modalities = as_string_array(*ii);
if (auto oi = mod_it->find("output"); oi != mod_it->end())
m.output_modalities = as_string_array(*oi);
}
if (auto k = first_string(mj, {"knowledge"})) m.knowledge_cutoff = *k;
return m;
}
void parse_models_field(const nlohmann::json& models, std::vector<ModelEntry>& out) {
if (models.is_object()) {
for (auto it = models.begin(); it != models.end(); ++it) {
if (!it->is_object()) continue;
std::string id = it.key();
if (auto idj = it->find("id"); idj != it->end() && idj->is_string()) {
id = idj->get<std::string>();
}
out.push_back(parse_model(id, *it));
}
} else if (models.is_array()) {
for (const auto& item : models) {
if (!item.is_object()) continue;
std::string id;
if (auto idj = item.find("id"); idj != item.end() && idj->is_string()) {
id = idj->get<std::string>();
}
if (id.empty()) continue;
out.push_back(parse_model(id, item));
}
}
}
ProviderEntry parse_provider(const std::string& key, const nlohmann::json& pj) {
ProviderEntry p;
p.id = key;
if (auto idj = pj.find("id"); idj != pj.end() && idj->is_string()) {
p.id = idj->get<std::string>();
}
if (auto name = first_string(pj, {"name"})) p.name = *name;
if (p.name.empty()) p.name = p.id;
if (auto env_it = pj.find("env"); env_it != pj.end()) {
if (env_it->is_array()) {
p.env = as_string_array(*env_it);
} else if (env_it->is_string()) {
p.env.push_back(env_it->get<std::string>());
}
}
if (auto doc = first_string(pj, {"doc"})) p.doc = *doc;
// models.dev's `api` field is a plain string (the OpenAI-compatible base URL).
// Older / hand-curated payloads sometimes used a nested form
// (`api.openai.base` or `api.base`); accept either shape so a future schema
// tweak doesn't break the catalog.
auto api_it = pj.find("api");
if (api_it != pj.end()) {
if (api_it->is_string()) {
std::string s = api_it->get<std::string>();
if (!s.empty()) p.base_url = s;
} else if (api_it->is_object()) {
auto openai_it = api_it->find("openai");
if (openai_it != api_it->end() && openai_it->is_object()) {
if (auto base = first_string(*openai_it, {"base", "base_url"})) {
p.base_url = *base;
}
}
if (!p.base_url.has_value()) {
if (auto base = first_string(*api_it, {"base", "base_url"})) {
p.base_url = *base;
}
}
}
}
if (!p.base_url.has_value()) {
if (auto base = first_string(pj, {"base_url"})) p.base_url = *base;
}
p.openai_compatible = p.base_url.has_value();
if (auto models = pj.find("models"); models != pj.end()) {
parse_models_field(*models, p.models);
std::sort(p.models.begin(), p.models.end(),
[](const ModelEntry& a, const ModelEntry& b) { return a.id < b.id; });
}
return p;
}
struct CatalogCache {
std::shared_ptr<const nlohmann::json> source;
std::vector<ProviderEntry> providers;
unsigned long long version = 0;
};
std::mutex& cache_mutex() {
static std::mutex m;
return m;
}
CatalogCache& cache_storage() {
static CatalogCache c;
return c;
}
const std::vector<ProviderEntry>& refresh_cache_if_stale() {
std::lock_guard<std::mutex> lk(cache_mutex());
auto current = current_registry();
auto& cache = cache_storage();
if (cache.source.get() == current.get() && cache.source) {
return cache.providers;
}
cache.source = current;
cache.providers = current ? build_catalog(*current) : std::vector<ProviderEntry>{};
std::sort(cache.providers.begin(), cache.providers.end(),
[](const ProviderEntry& a, const ProviderEntry& b) {
return a.name < b.name;
});
++cache.version;
return cache.providers;
}
} // namespace
std::vector<ProviderEntry> build_catalog(const nlohmann::json& registry) {
std::vector<ProviderEntry> out;
if (!registry.is_object()) return out;
out.reserve(registry.size());
for (auto it = registry.begin(); it != registry.end(); ++it) {
if (!it->is_object()) continue;
out.push_back(parse_provider(it.key(), *it));
}
return out;
}
const std::vector<ProviderEntry>& all_providers() {
return refresh_cache_if_stale();
}
std::vector<const ProviderEntry*> openai_compat_providers() {
const auto& providers = all_providers();
std::vector<const ProviderEntry*> out;
out.reserve(providers.size());
for (const auto& p : providers) {
if (p.openai_compatible) out.push_back(&p);
}
return out;
}
const ProviderEntry* find_provider(const std::string& id) {
for (const auto& p : all_providers()) {
if (p.id == id) return &p;
}
return nullptr;
}
const ModelEntry* find_model(const ProviderEntry& provider, const std::string& model_id) {
for (const auto& m : provider.models) {
if (m.id == model_id) return &m;
}
return nullptr;
}
std::string format_context(const std::optional<int>& tokens) {
if (!tokens.has_value() || *tokens <= 0) return "";
int n = *tokens;
std::ostringstream oss;
if (n >= 1000000 && n % 1000000 == 0) {
oss << (n / 1000000) << "M";
} else if (n >= 1000000) {
oss << std::fixed << std::setprecision(1) << (n / 1000000.0) << "M";
} else if (n >= 1000) {
if (n % 1000 == 0) oss << (n / 1000) << "k";
else oss << std::fixed << std::setprecision(1) << (n / 1000.0) << "k";
} else {
oss << n;
}
return oss.str();
}
namespace {
std::string trim_zero(std::string s) {
if (s.find('.') == std::string::npos) return s;
while (!s.empty() && s.back() == '0') s.pop_back();
if (!s.empty() && s.back() == '.') s.pop_back();
return s;
}
}
std::string format_cost(const std::optional<double>& input,
const std::optional<double>& output) {
if (!input.has_value() && !output.has_value()) return "";
auto fmt = [](double v) {
std::ostringstream oss;
oss << std::fixed << std::setprecision(2) << v;
return trim_zero(oss.str());
};
std::ostringstream oss;
oss << "in=$" << (input ? fmt(*input) : std::string("?"))
<< "/out=$" << (output ? fmt(*output) : std::string("?"));
return oss.str();
}
std::string format_capabilities(const ModelEntry& model) {
std::vector<std::string> tags;
if (model.tool_call) tags.push_back("tools");
if (model.attachment) tags.push_back("vision");
if (model.reasoning) tags.push_back("reasoning");
if (model.deprecated) tags.push_back("deprecated");
if (tags.empty()) return "";
std::ostringstream oss;
oss << "[";
for (size_t i = 0; i < tags.size(); ++i) {
if (i) oss << ", ";
oss << tags[i];
}
oss << "]";
return oss.str();
}
unsigned long long catalog_version() {
refresh_cache_if_stale();
std::lock_guard<std::mutex> lk(cache_mutex());
return cache_storage().version;
}
} // namespace acecode