-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsaved_models.cpp
More file actions
759 lines (716 loc) · 30 KB
/
Copy pathsaved_models.cpp
File metadata and controls
759 lines (716 loc) · 30 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
// saved_models 实现:纯函数 parse + validate。
// 对应 openspec/changes/model-profiles 任务 1.2。
#include "config/saved_models.hpp"
#include "model_provider_registry.hpp"
#include "request_headers.hpp"
#include "../provider/builtin_model_catalog.hpp"
#include "../utils/sha256.hpp"
#include <algorithm>
#include <cctype>
#include <cstdint>
#include <iomanip>
#include <limits>
#include <map>
#include <set>
#include <sstream>
namespace acecode {
namespace {
bool is_valid_capability_tag(const std::string& tag) {
if (tag.empty()) return false;
for (unsigned char ch : tag) {
if (std::iscntrl(ch)) return false;
}
return true;
}
bool is_valid_reasoning_effort(const std::string& effort) {
static const std::set<std::string> kEfforts{
"minimal", "low", "medium", "high", "xhigh", "max"};
return kEfforts.count(effort) != 0;
}
bool validate_reasoning_options(const ModelReasoningOptions& r,
std::string& err) {
if (!r.supported) {
if (r.mandatory || r.default_enabled || r.enabled.has_value() ||
!r.supported_efforts.empty() || r.default_effort.has_value() ||
r.effort.has_value() || r.supports_max_tokens ||
r.max_tokens.has_value()) {
err = "reasoning options are present but reasoning is unsupported";
return false;
}
return true;
}
if (r.mandatory && !r.default_enabled) {
err = "mandatory reasoning must be enabled by default";
return false;
}
if (r.mandatory && r.enabled.has_value() && !*r.enabled) {
err = "mandatory reasoning cannot be disabled";
return false;
}
std::set<std::string> efforts;
for (const auto& effort : r.supported_efforts) {
if (!is_valid_reasoning_effort(effort) ||
!efforts.insert(effort).second) {
err = "reasoning supported_efforts contains an invalid value";
return false;
}
}
auto check_effort = [&](const std::optional<std::string>& effort,
const char* field) {
if (!effort.has_value()) return true;
if (!is_valid_reasoning_effort(*effort) ||
efforts.count(*effort) == 0) {
err = std::string("reasoning ") + field +
" is not in supported_efforts";
return false;
}
return true;
};
if (!check_effort(r.default_effort, "default_effort") ||
!check_effort(r.effort, "effort")) {
return false;
}
if (r.max_tokens.has_value() &&
(!r.supports_max_tokens || *r.max_tokens <= 0)) {
err = "reasoning max_tokens requires positive supported token budgets";
return false;
}
return true;
}
std::string trim_ascii(std::string value) {
auto first = std::find_if_not(value.begin(), value.end(), [](unsigned char ch) {
return std::isspace(ch) != 0;
});
auto last = std::find_if_not(value.rbegin(), value.rend(), [](unsigned char ch) {
return std::isspace(ch) != 0;
}).base();
if (first >= last) return {};
return std::string(first, last);
}
std::string ascii_lower(std::string value) {
std::transform(value.begin(), value.end(), value.begin(), [](unsigned char ch) {
return static_cast<char>(std::tolower(ch));
});
return value;
}
bool is_acemodel_catalog_profile(const ModelProfile& profile) {
if (ascii_lower(profile.provider) != "openai" ||
!profile.models_dev_provider_id.has_value() ||
ascii_lower(*profile.models_dev_provider_id) != "acemodel" ||
profile.capabilities_source.value_or("") != "catalog") {
return false;
}
// The explicit catalog provider identity must survive endpoint edits (for
// example a user-selected proxy). Tying this to the official URL would
// turn the generated fallback into a manual override after such an edit.
const std::string model = ascii_lower(profile.model);
return model == "aurora" || model == "starrylight" || model == "moonlight";
}
bool is_loopback_endpoint(const std::string& endpoint) {
const std::string normalized = normalize_model_endpoint_identity(endpoint);
const auto scheme_end = normalized.find("://");
if (scheme_end == std::string::npos) return false;
const auto authority_start = scheme_end + 3;
const auto authority_end = normalized.find_first_of("/?#", authority_start);
std::string authority = normalized.substr(
authority_start,
authority_end == std::string::npos
? std::string::npos
: authority_end - authority_start);
const auto at = authority.rfind('@');
if (at != std::string::npos) authority.erase(0, at + 1);
if (authority == "[::1]" || authority.rfind("[::1]:", 0) == 0) return true;
const auto colon = authority.find(':');
const std::string host = colon == std::string::npos
? authority
: authority.substr(0, colon);
return host == "localhost" || host == "127.0.0.1";
}
bool is_known_local_provider_id(const std::optional<std::string>& provider_id) {
if (!provider_id.has_value()) return false;
static const std::set<std::string> kLocalIds{
"ollama", "lmstudio", "lm-studio", "llamacpp", "llama.cpp",
"localai", "vllm"};
return kLocalIds.count(ascii_lower(*provider_id)) != 0;
}
std::vector<std::string> parse_capabilities_array(const nlohmann::json& node) {
std::vector<std::string> out;
if (!node.is_array()) return out;
for (const auto& item : node) {
if (!item.is_string()) continue;
std::string tag = item.get<std::string>();
if (tag.empty()) continue;
out.push_back(std::move(tag));
}
return out;
}
// 单条 entry 解析。失败时把错误塞进 err(已带 entry 索引/name 上下文)并返回 nullopt。
std::optional<ModelProfile> parse_one_entry(const nlohmann::json& node, std::size_t idx,
std::string& err) {
if (!node.is_object()) {
std::ostringstream oss;
oss << "saved_models[" << idx << "] is not an object";
err = oss.str();
return std::nullopt;
}
ModelProfile e;
auto get_str = [&](const char* key, std::string& out) -> bool {
if (!node.contains(key)) return false;
if (!node[key].is_string()) return false;
out = node[key].get<std::string>();
return true;
};
if (!get_str("name", e.name) || e.name.empty()) {
std::ostringstream oss;
oss << "saved_models[" << idx << "] missing required field 'name'";
err = oss.str();
return std::nullopt;
}
if (!get_str("provider", e.provider) || e.provider.empty()) {
std::ostringstream oss;
oss << "saved_models[" << idx << "] (name='" << e.name
<< "') missing required field 'provider'";
err = oss.str();
return std::nullopt;
}
if (!is_known_model_provider(e.provider)) {
std::ostringstream oss;
oss << "saved_models[" << idx << "] (name='" << e.name
<< "') has unknown provider '" << e.provider
<< "' (expected 'openai', 'anthropic', 'copilot', 'grok', or 'codex')";
err = oss.str();
return std::nullopt;
}
if (!get_str("model", e.model) || e.model.empty()) {
std::ostringstream oss;
oss << "saved_models[" << idx << "] (name='" << e.name
<< "') missing required field 'model'";
err = oss.str();
return std::nullopt;
}
// base_url / api_key:openai/anthropic 必填;copilot/codex 忽略(允许字段缺失或为空)。
get_str("base_url", e.base_url);
get_str("api_key", e.api_key);
if (node.contains("models_dev_provider_id") &&
node["models_dev_provider_id"].is_string()) {
std::string s = node["models_dev_provider_id"].get<std::string>();
if (!s.empty()) e.models_dev_provider_id = std::move(s);
}
if (node.contains("context_window") && node["context_window"].is_number_integer()) {
int context_window = node["context_window"].get<int>();
if (context_window <= 0) {
std::ostringstream oss;
oss << "saved_models[" << idx << "] (name='" << e.name
<< "') has invalid context_window";
err = oss.str();
return std::nullopt;
}
e.context_window = context_window;
}
if (node.contains("stream_timeout_ms") && node["stream_timeout_ms"].is_number_integer()) {
int stream_timeout_ms = node["stream_timeout_ms"].get<int>();
if (stream_timeout_ms <= 0) {
std::ostringstream oss;
oss << "saved_models[" << idx << "] (name='" << e.name
<< "') has invalid stream_timeout_ms";
err = oss.str();
return std::nullopt;
}
e.stream_timeout_ms = stream_timeout_ms;
}
if (node.contains("capabilities")) {
e.capabilities = parse_capabilities_array(node["capabilities"]);
}
if (node.contains("endpoint_mode")) {
if (!node["endpoint_mode"].is_string()) {
err = "saved_models[" + std::to_string(idx) +
"] field 'endpoint_mode' must be string";
return std::nullopt;
}
e.endpoint_mode = node["endpoint_mode"].get<std::string>();
}
if (node.contains("max_output_tokens")) {
if (!node["max_output_tokens"].is_number_integer()) {
err = "saved_models[" + std::to_string(idx) +
"] field 'max_output_tokens' must be integer";
return std::nullopt;
}
const long long value = node["max_output_tokens"].get<long long>();
if (value <= 0 || value > std::numeric_limits<int>::max()) {
err = "saved_models[" + std::to_string(idx) +
"] has invalid max_output_tokens";
return std::nullopt;
}
e.max_output_tokens = static_cast<int>(value);
}
if (node.contains("capabilities_source")) {
if (!node["capabilities_source"].is_string()) {
err = "saved_models[" + std::to_string(idx) +
"] field 'capabilities_source' must be string";
return std::nullopt;
}
e.capabilities_source = node["capabilities_source"].get<std::string>();
}
if (node.contains("reasoning")) {
std::string reasoning_err;
auto reasoning = parse_model_reasoning_options(node["reasoning"],
reasoning_err);
if (!reasoning.has_value()) {
err = "saved_models[" + std::to_string(idx) +
"] has invalid reasoning: " + reasoning_err;
return std::nullopt;
}
e.reasoning = std::move(*reasoning);
}
if (node.contains("request_headers")) {
std::ostringstream context;
context << "saved_models[" << idx << "] (name='" << e.name << "')";
auto parsed = parse_request_headers_json(node["request_headers"], context.str(), err);
if (!parsed.has_value()) return std::nullopt;
e.request_headers = std::move(*parsed);
}
if (node.contains("readonly") && node["readonly"].is_boolean()) {
e.readonly = node["readonly"].get<bool>();
}
if (is_acemodel_catalog_profile(e)) {
// Catalog-sourced ACEModel profiles follow the current built-in
// metadata. Manual capability choices remain untouched because they
// carry capabilities_source=manual and do not enter this branch.
if (const ModelEntry* model = find_acemodel_catalog_model(e.model)) {
e.capabilities = model_capability_tags(*model);
}
// The previous first-party catalog and Windows seeder persisted 200K
// as if it were a manual override. Normalize that generated value to
// the new 250K fallback while keeping manual 200K overrides untouched.
if (e.context_window == 200000) e.context_window = 250000;
}
return e;
}
} // namespace
std::optional<std::vector<ModelProfile>> parse_saved_models(const nlohmann::json& node,
std::string& err) {
std::vector<ModelProfile> out;
if (node.is_null()) return out;
if (!node.is_array()) {
err = "saved_models must be a JSON array";
return std::nullopt;
}
out.reserve(node.size());
for (std::size_t i = 0; i < node.size(); ++i) {
auto entry = parse_one_entry(node[i], i, err);
if (!entry.has_value()) return std::nullopt;
out.push_back(std::move(*entry));
}
return out;
}
std::optional<std::vector<SavedModelNameRepair>>
repair_duplicate_saved_model_names(std::vector<ModelProfile>& entries,
std::string& err) {
constexpr std::uint32_t kShortHashSpace = 1u << 24;
constexpr std::uint32_t kShortHashMask = kShortHashSpace - 1;
err.clear();
std::map<std::string, std::size_t> last_indices;
std::set<std::string> occupied_names;
for (std::size_t i = 0; i < entries.size(); ++i) {
last_indices[entries[i].name] = i;
occupied_names.insert(entries[i].name);
}
std::vector<ModelProfile> repaired_entries = entries;
std::vector<SavedModelNameRepair> repairs;
for (std::size_t i = 0; i < repaired_entries.size(); ++i) {
const std::string original_name = repaired_entries[i].name;
if (last_indices[original_name] == i) continue;
std::string hash_input = original_name;
hash_input.push_back('\0');
hash_input += std::to_string(i);
const std::string digest = sha256_hex(hash_input);
const std::uint32_t hash_start = static_cast<std::uint32_t>(
std::stoul(digest.substr(0, 6), nullptr, 16));
bool found = false;
for (std::uint32_t offset = 0; offset < kShortHashSpace; ++offset) {
const std::uint32_t suffix_value =
(hash_start + offset) & kShortHashMask;
std::ostringstream suffix;
suffix << std::hex << std::nouppercase << std::setfill('0')
<< std::setw(6) << suffix_value;
std::string candidate = original_name + "-" + suffix.str();
if (!occupied_names.insert(candidate).second) continue;
repaired_entries[i].name = candidate;
repairs.push_back({i, original_name, std::move(candidate)});
found = true;
break;
}
if (!found) {
err = "saved_models duplicate name '" + original_name +
"' has no available 6-hex repair suffix";
return std::nullopt;
}
}
entries = std::move(repaired_entries);
return repairs;
}
bool validate_saved_models(const std::vector<ModelProfile>& entries,
const std::string& default_name,
std::string& err) {
std::set<std::string> seen_names;
for (std::size_t i = 0; i < entries.size(); ++i) {
const auto& e = entries[i];
if (e.name.empty()) {
std::ostringstream oss;
oss << "saved_models[" << i << "] has empty name";
err = oss.str();
return false;
}
if (!e.name.empty() && e.name.front() == '(') {
std::ostringstream oss;
oss << "saved_models[" << i << "] name '" << e.name
<< "' uses reserved prefix '(' (reserved for ACECode-synthesized "
"names like \"(session:...)\")";
err = oss.str();
return false;
}
if (seen_names.count(e.name)) {
std::ostringstream oss;
oss << "saved_models has duplicate name '" << e.name << "'";
err = oss.str();
return false;
}
seen_names.insert(e.name);
if (e.provider == "openai" || e.provider == "anthropic") {
if (e.base_url.empty()) {
std::ostringstream oss;
oss << "saved_models entry '" << e.name
<< "' (provider=" << e.provider << ") requires non-empty base_url";
err = oss.str();
return false;
}
if (e.api_key.empty() && !model_profile_allows_no_api_key(e)) {
std::ostringstream oss;
oss << "saved_models entry '" << e.name
<< "' (provider=" << e.provider << ") requires non-empty api_key";
err = oss.str();
return false;
}
} else if (!e.request_headers.empty()) {
std::ostringstream oss;
oss << "saved_models entry '" << e.name
<< "' has request_headers but provider is not openai or anthropic";
err = oss.str();
return false;
}
if (e.provider == "copilot" || e.provider == "grok") {
if (!e.base_url.empty() || !e.api_key.empty()) {
err = "saved_models entry '" + e.name +
"' cannot customize managed provider endpoint or credentials";
return false;
}
if (e.endpoint_mode.has_value() || e.reasoning.has_value() ||
e.max_output_tokens.has_value() ||
(e.provider == "grok" && e.stream_timeout_ms.has_value())) {
err = "saved_models entry '" + e.name +
"' contains unsupported managed provider runtime options";
return false;
}
}
if (e.endpoint_mode.has_value()) {
if (*e.endpoint_mode != "base_url" &&
*e.endpoint_mode != "full_url") {
err = "saved_models entry '" + e.name +
"' has unknown endpoint_mode";
return false;
}
if (*e.endpoint_mode == "full_url" &&
(e.provider != "openai" ||
e.models_dev_provider_id.has_value())) {
err = "saved_models entry '" + e.name +
"' cannot use full_url for this provider";
return false;
}
}
if (e.max_output_tokens.has_value() && *e.max_output_tokens <= 0) {
err = "saved_models entry '" + e.name +
"' has invalid max_output_tokens";
return false;
}
if (e.capabilities_source.has_value() &&
*e.capabilities_source != "catalog" &&
*e.capabilities_source != "manual") {
err = "saved_models entry '" + e.name +
"' has invalid capabilities_source";
return false;
}
if (e.reasoning.has_value()) {
std::string reasoning_err;
if (!validate_reasoning_options(*e.reasoning, reasoning_err)) {
err = "saved_models entry '" + e.name +
"' has invalid reasoning: " + reasoning_err;
return false;
}
if (e.capabilities_source.has_value()) {
const bool declared = std::find(
e.capabilities.begin(), e.capabilities.end(), "reasoning") !=
e.capabilities.end();
if (declared != e.reasoning->supported) {
err = "saved_models entry '" + e.name +
"' has inconsistent reasoning capability metadata";
return false;
}
}
if (e.reasoning->max_tokens.has_value() &&
e.max_output_tokens.has_value() &&
*e.max_output_tokens <= *e.reasoning->max_tokens) {
err = "saved_models entry '" + e.name +
"' requires max_output_tokens greater than reasoning max_tokens";
return false;
}
if (e.provider == "anthropic") {
if (e.reasoning->max_tokens.has_value() &&
e.max_output_tokens.value_or(4096) <=
*e.reasoning->max_tokens) {
err = "saved_models entry '" + e.name +
"' requires Anthropic max_output_tokens greater than "
"reasoning max_tokens";
return false;
}
}
}
if (e.context_window.has_value() && *e.context_window <= 0) {
std::ostringstream oss;
oss << "saved_models entry '" << e.name
<< "' has invalid context_window";
err = oss.str();
return false;
}
if (e.stream_timeout_ms.has_value() && *e.stream_timeout_ms <= 0) {
std::ostringstream oss;
oss << "saved_models entry '" << e.name
<< "' has invalid stream_timeout_ms";
err = oss.str();
return false;
}
std::set<std::string> seen_capabilities;
for (const auto& tag : e.capabilities) {
if (!is_valid_capability_tag(tag)) {
std::ostringstream oss;
oss << "saved_models entry '" << e.name
<< "' has invalid capability tag";
err = oss.str();
return false;
}
if (!seen_capabilities.insert(tag).second) {
std::ostringstream oss;
oss << "saved_models entry '" << e.name
<< "' has duplicate capability '" << tag << "'";
err = oss.str();
return false;
}
}
if (!e.request_headers.empty()) {
std::string headers_err;
if (!validate_request_headers(e.request_headers, headers_err)) {
std::ostringstream oss;
oss << "saved_models entry '" << e.name << "' invalid "
<< headers_err;
err = oss.str();
return false;
}
}
// copilot/codex:不需要 base_url / api_key,model 已在 parse 阶段检验非空。
}
if (!default_name.empty()) {
if (!seen_names.count(default_name)) {
std::ostringstream oss;
oss << "default_model_name '" << default_name
<< "' does not match any saved_models entry";
err = oss.str();
return false;
}
}
return true;
}
std::optional<ModelReasoningOptions> parse_model_reasoning_options(
const nlohmann::json& node,
std::string& err) {
if (!node.is_object()) {
err = "reasoning must be an object";
return std::nullopt;
}
ModelReasoningOptions result;
auto required_bool = [&](const char* key, bool& out) {
auto it = node.find(key);
if (it == node.end() || !it->is_boolean()) {
if (err.empty()) err = std::string("reasoning field '") + key +
"' must be boolean";
return false;
}
out = it->get<bool>();
return true;
};
if (!required_bool("supported", result.supported) ||
!required_bool("mandatory", result.mandatory) ||
!required_bool("default_enabled", result.default_enabled) ||
!required_bool("supports_max_tokens", result.supports_max_tokens)) {
return std::nullopt;
}
if (auto it = node.find("enabled"); it != node.end() && !it->is_null()) {
if (!it->is_boolean()) {
err = "reasoning field 'enabled' must be boolean or null";
return std::nullopt;
}
result.enabled = it->get<bool>();
}
auto efforts_it = node.find("supported_efforts");
if (efforts_it == node.end() || !efforts_it->is_array()) {
err = "reasoning field 'supported_efforts' must be an array";
return std::nullopt;
}
for (const auto& item : *efforts_it) {
if (!item.is_string()) {
err = "reasoning supported_efforts must contain strings";
return std::nullopt;
}
result.supported_efforts.push_back(item.get<std::string>());
}
auto optional_string = [&](const char* key,
std::optional<std::string>& out) {
auto it = node.find(key);
if (it == node.end() || it->is_null()) return true;
if (!it->is_string()) {
err = std::string("reasoning field '") + key +
"' must be string or null";
return false;
}
out = it->get<std::string>();
return true;
};
if (!optional_string("default_effort", result.default_effort) ||
!optional_string("effort", result.effort)) {
return std::nullopt;
}
if (auto it = node.find("max_tokens"); it != node.end() && !it->is_null()) {
if (!it->is_number_integer()) {
err = "reasoning field 'max_tokens' must be integer or null";
return std::nullopt;
}
const long long value = it->get<long long>();
if (value <= 0 || value > std::numeric_limits<int>::max()) {
err = "reasoning max_tokens must be positive";
return std::nullopt;
}
result.max_tokens = static_cast<int>(value);
}
if (!validate_reasoning_options(result, err)) return std::nullopt;
return result;
}
nlohmann::json model_reasoning_options_to_json(
const ModelReasoningOptions& options) {
nlohmann::json result{
{"supported", options.supported},
{"mandatory", options.mandatory},
{"default_enabled", options.default_enabled},
{"supported_efforts", options.supported_efforts},
{"supports_max_tokens", options.supports_max_tokens},
};
if (options.enabled.has_value()) result["enabled"] = *options.enabled;
if (options.default_effort.has_value()) {
result["default_effort"] = *options.default_effort;
}
if (options.effort.has_value()) result["effort"] = *options.effort;
if (options.max_tokens.has_value()) result["max_tokens"] = *options.max_tokens;
return result;
}
bool model_profile_allows_no_api_key(const ModelProfile& profile) {
if (profile.provider == "copilot" || profile.provider == "grok") return true;
if (profile.provider != "openai") return false;
return is_known_local_provider_id(profile.models_dev_provider_id) ||
is_loopback_endpoint(profile.base_url);
}
std::string normalize_model_endpoint_identity(const std::string& value) {
std::string normalized = trim_ascii(value);
auto suffix_start = normalized.find_first_of("?#");
auto path_end = suffix_start == std::string::npos
? normalized.size()
: suffix_start;
while (path_end > 0 && normalized[path_end - 1] == '/') {
normalized.erase(path_end - 1, 1);
--path_end;
}
const auto scheme_end = normalized.find("://");
if (scheme_end == std::string::npos) return normalized;
const std::string scheme = ascii_lower(normalized.substr(0, scheme_end));
const auto authority_start = scheme_end + 3;
const auto authority_end = normalized.find_first_of("/?#", authority_start);
const auto lower_end = authority_end == std::string::npos
? normalized.size()
: authority_end;
std::transform(normalized.begin(), normalized.begin() + lower_end,
normalized.begin(), [](unsigned char ch) {
return static_cast<char>(std::tolower(ch));
});
const std::string default_port = scheme == "http"
? ":80"
: (scheme == "https" ? ":443" : std::string{});
if (!default_port.empty()) {
const auto current_authority_end =
normalized.find_first_of("/?#", authority_start);
const auto end = current_authority_end == std::string::npos
? normalized.size()
: current_authority_end;
if (end >= authority_start + default_port.size() &&
normalized.compare(end - default_port.size(), default_port.size(),
default_port) == 0) {
normalized.erase(end - default_port.size(), default_port.size());
}
}
return normalized;
}
bool is_acemodel_catalog_context_fallback(const ModelProfile& profile) {
if (!is_acemodel_catalog_profile(profile) ||
!profile.context_window.has_value()) {
return false;
}
return *profile.context_window == 200000 || *profile.context_window == 250000;
}
bool model_reasoning_options_equal(const ModelReasoningOptions& left,
const ModelReasoningOptions& right) {
return left.supported == right.supported &&
left.mandatory == right.mandatory &&
left.default_enabled == right.default_enabled &&
left.enabled == right.enabled &&
left.supported_efforts == right.supported_efforts &&
left.default_effort == right.default_effort &&
left.effort == right.effort &&
left.supports_max_tokens == right.supports_max_tokens &&
left.max_tokens == right.max_tokens;
}
bool model_profiles_equal(const ModelProfile& left,
const ModelProfile& right) {
const bool reasoning_equal =
left.reasoning.has_value() == right.reasoning.has_value() &&
(!left.reasoning.has_value() ||
model_reasoning_options_equal(*left.reasoning, *right.reasoning));
return left.name == right.name &&
left.provider == right.provider &&
left.base_url == right.base_url &&
left.api_key == right.api_key &&
left.model == right.model &&
left.models_dev_provider_id == right.models_dev_provider_id &&
left.context_window == right.context_window &&
left.stream_timeout_ms == right.stream_timeout_ms &&
left.capabilities == right.capabilities &&
left.endpoint_mode == right.endpoint_mode &&
left.max_output_tokens == right.max_output_tokens &&
left.capabilities_source == right.capabilities_source &&
reasoning_equal &&
left.request_headers == right.request_headers &&
left.readonly == right.readonly;
}
bool saved_model_lists_equal(const std::vector<ModelProfile>& left,
const std::vector<ModelProfile>& right) {
if (left.size() != right.size()) return false;
for (std::size_t index = 0; index < left.size(); ++index) {
if (!model_profiles_equal(left[index], right[index])) return false;
}
return true;
}
} // namespace acecode