-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathanthropic_provider.cpp
More file actions
1243 lines (1141 loc) · 46.4 KB
/
Copy pathanthropic_provider.cpp
File metadata and controls
1243 lines (1141 loc) · 46.4 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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "anthropic_provider.hpp"
#include "config/request_headers.hpp"
#include "network/proxy_resolver.hpp"
#include "session/attachment_prompt_context.hpp"
#include "session/attachment_store.hpp"
#include "session/session_history_recovery.hpp"
#include "utils/logger.hpp"
#include "utils/sha1.hpp"
#include <cpr/cpr.h>
#include <cpr/ssl_options.h>
#include <algorithm>
#include <chrono>
#include <cctype>
#include <map>
#include <limits>
#include <optional>
#include <sstream>
#include <utility>
#include <vector>
namespace acecode {
AnthropicProvider::AnthropicProvider(const std::string& base_url,
const std::string& api_key,
const std::string& model,
int stream_timeout_ms,
std::map<std::string, std::string> request_headers,
ProviderRequestOptions request_options)
: base_url_(normalize_base_url(base_url)),
api_key_(api_key),
model_(model),
request_headers_(std::move(request_headers)),
request_options_(std::move(request_options)),
stream_timeout_ms_(stream_timeout_ms > 0
? stream_timeout_ms
: OpenAiConfig::kDefaultStreamTimeoutMs) {}
namespace {
constexpr int kStreamConnectTimeoutCapMs = 15000;
std::int64_t steady_now_ms() {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count();
}
std::string ascii_lower(std::string value) {
std::transform(value.begin(), value.end(), value.begin(), [](unsigned char c) {
return static_cast<char>(std::tolower(c));
});
return value;
}
std::string json_string_or_empty(const nlohmann::json& value, const char* key) {
if (!value.is_object() || !value.contains(key) || !value[key].is_string()) {
return {};
}
return value[key].get<std::string>();
}
std::string synthesize_tool_call_id(int index,
const std::string& name,
const std::string& arguments) {
std::string fingerprint = std::to_string(index);
fingerprint.push_back('\n');
fingerprint.append(name);
fingerprint.push_back('\n');
fingerprint.append(arguments);
return "toolu_ace_" + sha1_hex(fingerprint).substr(0, 24);
}
nlohmann::json parse_tool_input_or_empty(const std::string& arguments) {
auto parsed = nlohmann::json::parse(arguments, nullptr, false);
if (!parsed.is_discarded() && (parsed.is_object() || parsed.is_array())) {
return parsed;
}
return nlohmann::json::object();
}
void append_text_block(nlohmann::json& blocks, const std::string& text) {
if (text.empty()) return;
blocks.push_back(nlohmann::json{{"type", "text"}, {"text", text}});
}
std::string textual_content_parts(const ChatMessage& msg) {
if (msg.content_parts.is_null() || !msg.content_parts.is_array() ||
msg.content_parts.empty()) {
return msg.content;
}
std::ostringstream oss;
bool first = true;
auto append = [&](const std::string& text) {
if (text.empty()) return;
if (!first) oss << "\n\n";
first = false;
oss << text;
};
for (const auto& part : msg.content_parts) {
if (!part.is_object()) continue;
const std::string type = part.value("type", std::string{});
if (type == "text") {
append(part.value("text", std::string{}));
} else if (type == "browser_context") {
const auto ctx = part.contains("context") ? part["context"] : nlohmann::json::object();
append("[Browser context]\n" + ctx.dump(2));
} else if (type == "file") {
auto record = part.contains("attachment")
? attachment_from_json(part["attachment"])
: std::optional<AttachmentRecord>{};
append(record.has_value()
? file_attachment_reference_text(*record)
: std::string{"[Attached file unavailable: invalid metadata]"});
} else if (type == "image") {
append("[Image attachment omitted: Anthropic image blocks are not supported by this provider yet]");
} else if (!part.is_null()) {
append(part.dump());
}
}
std::string text = oss.str();
if (text.empty()) return msg.content;
return text;
}
nlohmann::json anthropic_content_blocks_for_text(const std::string& text) {
nlohmann::json blocks = nlohmann::json::array();
append_text_block(blocks, text);
return blocks;
}
std::optional<nlohmann::json> signed_anthropic_assistant_blocks(
const ChatMessage& msg) {
if (!msg.content_parts.is_array() || msg.content_parts.empty()) {
return std::nullopt;
}
bool contains_signed_thinking = false;
for (const auto& part : msg.content_parts) {
if (!part.is_object()) continue;
const std::string type = part.value("type", std::string{});
if (type == "thinking" && part.contains("signature") &&
part["signature"].is_string() &&
!part["signature"].get_ref<const std::string&>().empty()) {
contains_signed_thinking = true;
break;
}
if (type == "redacted_thinking" && part.contains("data") &&
part["data"].is_string()) {
contains_signed_thinking = true;
break;
}
}
if (!contains_signed_thinking) return std::nullopt;
// These blocks originated from the Anthropic response. Preserve their
// order and provider-owned fields exactly, especially thinking.signature
// and redacted_thinking.data.
return std::optional<nlohmann::json>(std::in_place, msg.content_parts);
}
nlohmann::json anthropic_tool_use_blocks(const nlohmann::json& tool_calls,
int& repaired_count,
int& dropped_count) {
nlohmann::json items;
if (tool_calls.is_array()) {
items = tool_calls;
} else if (tool_calls.is_object()) {
items = nlohmann::json::array({tool_calls});
} else {
return nlohmann::json::array();
}
nlohmann::json out = nlohmann::json::array();
int index = 0;
for (const auto& raw_tc : items) {
if (!raw_tc.is_object()) {
++dropped_count;
continue;
}
std::string id = json_string_or_empty(raw_tc, "id");
if (!raw_tc.contains("function") || !raw_tc["function"].is_object()) {
++dropped_count;
continue;
}
const auto& fn = raw_tc["function"];
std::string name = json_string_or_empty(fn, "name");
std::string arguments = json_string_or_empty(fn, "arguments");
if (name.empty()) {
++dropped_count;
continue;
}
if (id.empty()) {
id = synthesize_tool_call_id(index, name, arguments);
++repaired_count;
}
out.push_back(nlohmann::json{
{"type", "tool_use"},
{"id", id},
{"name", name},
{"input", parse_tool_input_or_empty(arguments)},
});
++index;
}
return out;
}
} // namespace
void AnthropicProvider::merge_usage(TokenUsage& usage, const nlohmann::json& node) {
if (!node.is_object()) return;
const auto read_int = [&node](const char* key) -> std::optional<int> {
const auto it = node.find(key);
if (it == node.end() || !it->is_number_integer()) return std::nullopt;
return it->get<int>();
};
// Anthropic reports `input_tokens` EXCLUDING cache reads and cache writes,
// while OpenAI-compatible providers report `prompt_tokens` INCLUDING the
// cached prefix. Normalize to the OpenAI contract here: prompt_tokens is
// the total input and cache_read_tokens is a subset of it, so that
// context meters and cache hit-rate math stay provider-agnostic.
//
// The uncached remainder is recovered from the accumulated totals rather
// than remembered separately, which keeps this idempotent across the
// multiple usage nodes a streaming response delivers.
int uncached_input =
usage.prompt_tokens - usage.cache_read_tokens - usage.cache_write_tokens;
if (uncached_input < 0) uncached_input = 0;
if (const auto input = read_int("input_tokens")) {
uncached_input = *input;
usage.has_data = true;
}
if (const auto output = read_int("output_tokens")) {
usage.completion_tokens = *output;
usage.has_data = true;
}
if (const auto cache_read = read_int("cache_read_input_tokens")) {
usage.cache_read_tokens = *cache_read;
usage.has_data = true;
}
if (const auto cache_write = read_int("cache_creation_input_tokens")) {
usage.cache_write_tokens = *cache_write;
usage.has_data = true;
}
if (usage.has_data) {
usage.prompt_tokens =
uncached_input + usage.cache_read_tokens + usage.cache_write_tokens;
usage.total_tokens = usage.prompt_tokens + usage.completion_tokens;
}
}
namespace {
std::string header_value_ci(const cpr::Header& headers, const std::string& key) {
const std::string wanted = ascii_lower(key);
for (const auto& [header_key, value] : headers) {
if (ascii_lower(header_key) == wanted) return value;
}
return {};
}
std::string extract_request_id(const cpr::Header& headers) {
for (const std::string& key : {
"request-id",
"x-request-id",
"cf-ray",
}) {
std::string value = header_value_ci(headers, key);
if (!value.empty()) return value;
}
return {};
}
bool parse_json_body(const std::string& body,
bool& body_is_json,
std::string& pretty_json) {
body_is_json = false;
pretty_json.clear();
if (body.empty()) return false;
try {
const auto parsed = nlohmann::json::parse(body);
pretty_json = parsed.dump(2);
body_is_json = true;
return true;
} catch (const nlohmann::json::parse_error&) {
return false;
}
}
std::string provider_error_prefix(ProviderErrorKind kind, int status_code) {
switch (kind) {
case ProviderErrorKind::UserCancelled: return "Request cancelled";
case ProviderErrorKind::Timeout: return "Request timed out";
case ProviderErrorKind::Network: return "Connection failed";
case ProviderErrorKind::Http: return "HTTP " + std::to_string(status_code);
case ProviderErrorKind::MalformedJson: return "Malformed JSON in streaming response";
case ProviderErrorKind::MalformedSse: return "Malformed or incomplete streaming response";
case ProviderErrorKind::Unknown: return "Provider request failed";
case ProviderErrorKind::None: return {};
}
return "Provider request failed";
}
ProviderErrorInfo make_provider_error(ProviderErrorKind kind,
int status_code,
const std::string& provider,
const std::string& model,
const std::string& request_id,
const std::string& raw_body,
const std::string& transport_message,
bool retryable) {
ProviderErrorInfo info;
info.kind = kind;
info.status_code = status_code;
info.provider = provider;
info.model = model;
info.request_id = request_id;
info.raw_body = raw_body;
info.retryable = retryable;
parse_json_body(raw_body, info.body_is_json, info.pretty_json);
if (kind == ProviderErrorKind::UserCancelled) {
info.display_message = "Request cancelled";
return info;
}
std::ostringstream display;
display << provider_error_prefix(kind, status_code);
if (!provider.empty()) display << " from " << provider;
if (!model.empty()) display << " model " << model;
if (!request_id.empty()) display << " request_id=" << request_id;
if (!transport_message.empty()) display << ": " << transport_message;
const std::string body_for_display = info.body_is_json ? info.pretty_json : raw_body;
if (!body_for_display.empty()) {
display << "\n" << body_for_display;
}
info.display_message = display.str();
return info;
}
ChatResponse make_chat_error_response(ProviderErrorInfo info) {
ChatResponse response;
response.content = "[Error] " + info.display_message;
response.finish_reason = "error";
response.provider_error = std::move(info);
return response;
}
ProviderErrorKind classify_cpr_error(const cpr::Error& error) {
if (error.code == cpr::ErrorCode::OPERATION_TIMEDOUT) {
return ProviderErrorKind::Timeout;
}
const std::string message = ascii_lower(error.message);
if (message.find("timed out") != std::string::npos ||
message.find("timeout") != std::string::npos) {
return ProviderErrorKind::Timeout;
}
return ProviderErrorKind::Network;
}
bool is_retryable_http_status(int status_code, const std::string& body) {
return provider_http_error_is_retryable(status_code, body);
}
void emit_provider_error(const StreamCallback& callback, const ProviderErrorInfo& info) {
StreamEvent evt;
evt.type = StreamEventType::Error;
evt.error = info.display_message;
evt.provider_error = info;
callback(evt);
}
void emit_retry_event(const StreamCallback& callback, ProviderErrorInfo info) {
StreamEvent evt;
evt.type = StreamEventType::Retry;
evt.provider_error = std::move(info);
evt.error = evt.provider_error.display_message;
callback(evt);
}
void emit_retry_resume_event(const StreamCallback& callback,
const ProviderErrorInfo& info) {
StreamEvent evt;
evt.type = StreamEventType::RetryResume;
evt.provider_error = info;
callback(evt);
}
int retry_after_delay_ms(const cpr::Header& headers,
std::uint64_t retry_number) {
std::optional<std::int64_t> server_delay;
const std::string retry_after = header_value_ci(headers, "retry-after");
if (!retry_after.empty()) {
server_delay = parse_retry_after_ms(retry_after);
}
return static_cast<int>(
provider_retry_delay_ms(retry_number, server_delay));
}
bool find_event_delimiter(const std::string& buffer,
size_t& pos,
size_t& delimiter_len) {
const size_t lf = buffer.find("\n\n");
const size_t crlf = buffer.find("\r\n\r\n");
if (lf == std::string::npos && crlf == std::string::npos) return false;
if (crlf != std::string::npos &&
(lf == std::string::npos || crlf < lf)) {
pos = crlf;
delimiter_len = 4;
} else {
pos = lf;
delimiter_len = 2;
}
return true;
}
struct SseEventBlock {
std::string event;
std::string data;
};
SseEventBlock parse_sse_event_block(const std::string& event_block) {
SseEventBlock out;
std::istringstream iss(event_block);
std::string line;
while (std::getline(iss, line)) {
if (!line.empty() && line.back() == '\r') {
line.pop_back();
}
static constexpr std::string_view kEventPrefix = "event:";
static constexpr std::string_view kDataPrefix = "data:";
if (line.compare(0, kEventPrefix.size(), kEventPrefix) == 0) {
size_t value_start = kEventPrefix.size();
if (value_start < line.size() && line[value_start] == ' ') ++value_start;
out.event = line.substr(value_start);
} else if (line.compare(0, kDataPrefix.size(), kDataPrefix) == 0) {
size_t value_start = kDataPrefix.size();
if (value_start < line.size() && line[value_start] == ' ') ++value_start;
if (!out.data.empty()) out.data += "\n";
out.data += line.substr(value_start);
}
}
return out;
}
std::string anthropic_error_message_from_json(const nlohmann::json& j) {
if (!j.is_object()) return {};
auto it = j.find("error");
if (it == j.end()) return {};
const auto& error = *it;
if (error.is_string()) return error.get<std::string>();
if (error.is_object()) {
const std::string type = error.value("type", std::string{});
const std::string message = error.value("message", std::string{});
if (!type.empty() && !message.empty()) return type + ": " + message;
if (!message.empty()) return message;
if (!type.empty()) return type;
}
return error.dump();
}
} // namespace
nlohmann::json AnthropicProvider::build_request_body(
const std::vector<ChatMessage>& messages,
const std::vector<ToolDef>& tools,
bool stream
) const {
nlohmann::json body;
body["model"] = model_;
body["max_tokens"] = request_options_.max_output_tokens.value_or(
kDefaultMaxTokens);
if (stream) body["stream"] = true;
if (request_options_.reasoning_protocol == ReasoningWireProtocol::Anthropic &&
request_options_.reasoning.has_value() &&
request_options_.reasoning->supported) {
const auto& reasoning = *request_options_.reasoning;
const bool enabled = reasoning.mandatory ||
reasoning.enabled.value_or(reasoning.default_enabled);
if (!enabled) {
body["thinking"] = {{"type", "disabled"}};
} else if (reasoning.max_tokens.has_value()) {
body["thinking"] = {
{"type", "enabled"},
{"budget_tokens", *reasoning.max_tokens},
};
} else if (!reasoning.supports_max_tokens) {
body["thinking"] = {{"type", "adaptive"}};
const auto effort = reasoning.effort.has_value()
? reasoning.effort
: reasoning.default_effort;
if (effort.has_value()) {
body["output_config"] = {{"effort", *effort}};
}
} else if (reasoning.effort.has_value()) {
// A budget-capable model with no chosen budget must not be guessed
// into adaptive mode. An explicit effort remains a valid
// independent output_config override; catalog defaults are left to
// the provider when this model family does not support adaptive.
body["output_config"] = {{"effort", *reasoning.effort}};
}
}
std::string system_text;
nlohmann::json anthropic_messages = nlohmann::json::array();
int repaired_tool_calls = 0;
int dropped_tool_calls = 0;
int dropped_invalid_role = 0;
const auto recovered_history = recover_provider_history(messages);
for (const auto& msg : recovered_history.messages) {
if (msg.role == "system") {
const std::string text = textual_content_parts(msg);
if (!text.empty()) {
if (!system_text.empty()) system_text += "\n\n";
system_text += text;
}
continue;
}
if (msg.role == "tool") {
if (msg.tool_call_id.empty()) {
nlohmann::json blocks = anthropic_content_blocks_for_text(
"[Tool result omitted: missing tool_use_id]\n" + msg.content);
anthropic_messages.push_back(nlohmann::json{
{"role", "user"},
{"content", std::move(blocks)},
});
continue;
}
nlohmann::json blocks = nlohmann::json::array();
blocks.push_back(nlohmann::json{
{"type", "tool_result"},
{"tool_use_id", msg.tool_call_id},
{"content", textual_content_parts(msg)},
});
anthropic_messages.push_back(nlohmann::json{
{"role", "user"},
{"content", std::move(blocks)},
});
continue;
}
if (msg.role != "user" && msg.role != "assistant") {
++dropped_invalid_role;
continue;
}
nlohmann::json blocks = nlohmann::json::array();
const auto preserved_blocks = msg.role == "assistant"
? signed_anthropic_assistant_blocks(msg)
: std::nullopt;
if (preserved_blocks.has_value()) {
blocks = *preserved_blocks;
} else {
const std::string text = textual_content_parts(msg);
append_text_block(blocks, text);
if (msg.role == "assistant" && !msg.reasoning_content.empty()) {
append_text_block(
blocks, "[Previous reasoning]\n" + msg.reasoning_content);
}
if (msg.role == "assistant" && !msg.tool_calls.is_null() &&
!msg.tool_calls.empty()) {
nlohmann::json tool_blocks = anthropic_tool_use_blocks(
msg.tool_calls, repaired_tool_calls, dropped_tool_calls);
for (auto& block : tool_blocks) {
blocks.push_back(std::move(block));
}
}
}
if (blocks.empty()) {
append_text_block(blocks, "[Empty message]");
}
anthropic_messages.push_back(nlohmann::json{
{"role", msg.role},
{"content", std::move(blocks)},
});
}
if (!system_text.empty()) body["system"] = system_text;
body["messages"] = std::move(anthropic_messages);
if (!tools.empty()) {
nlohmann::json tools_json = nlohmann::json::array();
for (const auto& tool : tools) {
nlohmann::json t;
t["name"] = tool.name;
t["description"] = tool.description;
t["input_schema"] = tool.parameters.is_object()
? tool.parameters
: nlohmann::json::object();
tools_json.push_back(std::move(t));
}
body["tools"] = std::move(tools_json);
}
if (repaired_tool_calls > 0) {
LOG_WARN("anthropic build_request_body: synthesized " +
std::to_string(repaired_tool_calls) + " tool_use id(s)");
}
if (dropped_tool_calls > 0 || dropped_invalid_role > 0) {
LOG_WARN("anthropic build_request_body: dropped invalid entries roles=" +
std::to_string(dropped_invalid_role) + " tool_calls=" +
std::to_string(dropped_tool_calls));
}
return body;
}
ChatResponse AnthropicProvider::parse_response(const nlohmann::json& j) {
ChatResponse resp;
resp.finish_reason = j.value("stop_reason", std::string{"stop"});
if (j.contains("usage")) merge_usage(resp.usage, j["usage"]);
if (!j.contains("content") || !j["content"].is_array()) {
resp.content = "[Error] No content blocks in Anthropic response.";
resp.finish_reason = "error";
return resp;
}
int tool_index = 0;
resp.content_parts = nlohmann::json::array();
for (const auto& block : j["content"]) {
if (!block.is_object()) continue;
resp.content_parts.push_back(block);
const std::string type = block.value("type", std::string{});
if (type == "text" && block.contains("text") && block["text"].is_string()) {
resp.content += block["text"].get<std::string>();
} else if (type == "thinking" && block.contains("thinking") &&
block["thinking"].is_string()) {
resp.reasoning_content += block["thinking"].get<std::string>();
} else if (type == "tool_use") {
ToolCall call;
call.id = block.value("id", std::string{});
call.function_name = block.value("name", std::string{});
if (block.contains("input")) {
call.function_arguments = block["input"].dump();
} else {
call.function_arguments = "{}";
}
if (call.id.empty()) {
call.id = synthesize_tool_call_id(
tool_index, call.function_name, call.function_arguments);
}
resp.tool_calls.push_back(std::move(call));
++tool_index;
}
}
return resp;
}
ChatResponse AnthropicProvider::chat(
const std::vector<ChatMessage>& messages,
const std::vector<ToolDef>& tools
) {
if (api_key_.empty()) {
auto info = make_provider_error(
ProviderErrorKind::Unknown,
0,
name(),
model_,
std::string{},
std::string{},
"missing Anthropic API key",
false);
return make_chat_error_response(std::move(info));
}
nlohmann::json body = build_request_body(messages, tools, false);
const std::string url = base_url_ + "/messages";
cpr::Header headers = {
{"Content-Type", "application/json"},
{"anthropic-version", "2023-06-01"},
{"x-api-key", api_key_},
};
std::string header_error;
auto resolved_headers = resolve_request_headers(request_headers_, header_error);
if (!resolved_headers.has_value()) {
return make_chat_error_response(make_provider_error(
ProviderErrorKind::Unknown,
0,
name(),
model_,
std::string{},
std::string{},
header_error,
false));
}
for (const auto& [k, v] : *resolved_headers) {
headers[k] = v;
}
auto proxy_opts = network::proxy_options_for(url);
cpr::Response r = cpr::Post(
cpr::Url{url},
headers,
cpr::Body{body.dump()},
network::build_ssl_options(proxy_opts),
proxy_opts.proxies,
proxy_opts.auth,
cpr::Timeout{stream_timeout_ms_}
);
if (r.status_code == 0) {
const ProviderErrorKind kind = classify_cpr_error(r.error);
return make_chat_error_response(make_provider_error(
kind,
0,
name(),
model_,
extract_request_id(r.header),
r.text,
r.error.message,
kind == ProviderErrorKind::Timeout ||
kind == ProviderErrorKind::Network));
}
if (r.status_code < 200 || r.status_code >= 300) {
auto info = make_provider_error(
ProviderErrorKind::Http,
static_cast<int>(r.status_code),
name(),
model_,
extract_request_id(r.header),
r.text,
std::string{},
is_retryable_http_status(static_cast<int>(r.status_code), r.text));
const std::string retry_after =
header_value_ci(r.header, "retry-after");
if (!retry_after.empty()) {
if (auto parsed = parse_retry_after_ms(retry_after)) {
info.server_retry_after_ms = *parsed;
}
}
return make_chat_error_response(std::move(info));
}
try {
nlohmann::json response_json = nlohmann::json::parse(r.text);
return parse_response(response_json);
} catch (const nlohmann::json::parse_error& e) {
return make_chat_error_response(make_provider_error(
ProviderErrorKind::MalformedJson,
200,
name(),
model_,
extract_request_id(r.header),
r.text,
e.what(),
false));
}
}
ChatResponse AnthropicProvider::parse_sse_stream(
const std::string& url,
const nlohmann::json& body,
const std::map<std::string, std::string>& extra_headers,
const StreamCallback& callback,
std::atomic<bool>* abort_flag
) {
LOG_INFO("anthropic parse_sse_stream url=" + url);
cpr::Header headers = {
{"Content-Type", "application/json"},
{"anthropic-version", "2023-06-01"},
};
for (const auto& [k, v] : extra_headers) {
headers[k] = v;
}
std::atomic<std::int64_t> last_stream_activity_ms{steady_now_ms()};
std::atomic<bool> stream_idle_timed_out{false};
const int stream_idle_timeout_ms = (std::max)(1, stream_timeout_ms_);
auto progress_cb = cpr::ProgressCallback{
[abort_flag, &last_stream_activity_ms, &stream_idle_timed_out,
stream_idle_timeout_ms](cpr::cpr_off_t,
cpr::cpr_off_t,
cpr::cpr_off_t,
cpr::cpr_off_t,
intptr_t) -> bool {
if (abort_flag && abort_flag->load()) {
return false;
}
const std::int64_t idle_ms =
steady_now_ms() - last_stream_activity_ms.load();
if (idle_ms >= stream_idle_timeout_ms) {
stream_idle_timed_out.store(true);
return false;
}
return true;
}
};
struct ContentBlockAccumulator {
std::string type;
std::string id;
std::string name;
std::string input_json;
nlohmann::json raw = nlohmann::json::object();
};
ChatResponse last_accumulated;
last_accumulated.finish_reason = "stop";
for (std::uint64_t attempt = 1; ;
attempt = attempt == (std::numeric_limits<std::uint64_t>::max)()
? attempt
: attempt + 1) {
last_stream_activity_ms.store(steady_now_ms());
stream_idle_timed_out.store(false);
ChatResponse accumulated;
accumulated.finish_reason = "stop";
std::string reported_finish_reason;
std::string sse_buffer;
std::string raw_body_capture;
std::map<int, ContentBlockAccumulator> blocks;
bool saw_done = false;
bool saw_sse_data = false;
bool saw_parse_error = false;
bool saw_payload_error = false;
std::string payload_error_body;
std::string payload_error_message;
auto emit_usage = [&]() {
if (!accumulated.usage.has_data) return;
StreamEvent usage_evt;
usage_evt.type = StreamEventType::Usage;
usage_evt.usage = accumulated.usage;
callback(usage_evt);
};
auto flush_tool_block = [&](int index) {
auto it = blocks.find(index);
if (it == blocks.end() || it->second.type != "tool_use") return;
const auto& block = it->second;
ToolCall call;
call.id = block.id.empty()
? synthesize_tool_call_id(index, block.name, block.input_json)
: block.id;
call.function_name = block.name;
call.function_arguments = block.input_json.empty()
? std::string("{}")
: block.input_json;
accumulated.tool_calls.push_back(call);
StreamEvent evt;
evt.type = StreamEventType::ToolCall;
evt.tool_call = call;
evt.tool_index = index;
callback(evt);
};
auto emit_done = [&]() {
accumulated.content_parts = nlohmann::json::array();
for (const auto& [index, state] : blocks) {
(void)index;
if (!state.raw.is_object() || state.raw.empty()) continue;
nlohmann::json raw = state.raw;
if (state.type == "tool_use") {
raw["input"] = parse_tool_input_or_empty(state.input_json);
}
accumulated.content_parts.push_back(std::move(raw));
}
emit_usage();
StreamEvent done_evt;
done_evt.type = StreamEventType::Done;
done_evt.finish_reason = reported_finish_reason;
done_evt.content_parts = accumulated.content_parts;
callback(done_evt);
saw_done = true;
};
auto write_cb = cpr::WriteCallback{[&](const std::string_view data, intptr_t) -> bool {
if (abort_flag && abort_flag->load()) {
return false;
}
if (!data.empty()) {
last_stream_activity_ms.store(steady_now_ms());
}
raw_body_capture.append(data.data(), data.size());
sse_buffer += std::string(data);
size_t pos = 0;
size_t delimiter_len = 0;
while (find_event_delimiter(sse_buffer, pos, delimiter_len)) {
const std::string event_block = sse_buffer.substr(0, pos);
sse_buffer.erase(0, pos + delimiter_len);
SseEventBlock evt_block = parse_sse_event_block(event_block);
if (evt_block.data.empty()) continue;
saw_sse_data = true;
nlohmann::json j;
try {
j = nlohmann::json::parse(evt_block.data);
} catch (const nlohmann::json::parse_error& e) {
saw_parse_error = true;
LOG_WARN("Anthropic SSE JSON parse error: " + std::string(e.what()) +
" data=" + log_truncate(evt_block.data, 200));
continue;
}
const std::string event_type = evt_block.event.empty()
? j.value("type", std::string{})
: evt_block.event;
if (event_type == "ping") {
continue;
}
if (event_type == "error" || j.value("type", std::string{}) == "error") {
saw_payload_error = true;
payload_error_body = evt_block.data;
payload_error_message = anthropic_error_message_from_json(j);
LOG_ERROR("Anthropic SSE payload error: " +
log_truncate(payload_error_message.empty()
? evt_block.data
: payload_error_message, 500));
return false;
}
if (event_type == "message_start") {
if (j.contains("message") && j["message"].is_object()) {
const auto& message = j["message"];
if (message.contains("usage")) {
merge_usage(accumulated.usage, message["usage"]);
}
}
continue;
}
if (event_type == "content_block_start") {
const int index = j.value("index", 0);
auto& block = blocks[index];
if (j.contains("content_block") && j["content_block"].is_object()) {
const auto& cb = j["content_block"];
block.type = cb.value("type", std::string{});
block.id = cb.value("id", std::string{});
block.name = cb.value("name", std::string{});
block.raw = cb;
if (cb.contains("input") && !cb["input"].is_null() &&
!cb["input"].empty()) {
block.input_json = cb["input"].dump();
}
}
continue;
}
if (event_type == "content_block_delta") {
const int index = j.value("index", 0);
if (!j.contains("delta") || !j["delta"].is_object()) continue;
const auto& delta = j["delta"];
const std::string delta_type = delta.value("type", std::string{});
if (delta_type == "text_delta" && delta.contains("text") &&
delta["text"].is_string()) {
const std::string token = delta["text"].get<std::string>();
accumulated.content += token;
auto& block = blocks[index];
block.type = "text";
block.raw["type"] = "text";
if (!block.raw.contains("text") ||
!block.raw["text"].is_string()) {
block.raw["text"] = "";
}
block.raw["text"].get_ref<std::string&>() += token;
if (!token.empty()) {
StreamEvent event;
event.type = StreamEventType::Delta;
event.content = token;
callback(event);
}
} else if (delta_type == "thinking_delta" &&
delta.contains("thinking") &&
delta["thinking"].is_string()) {
const std::string token = delta["thinking"].get<std::string>();
accumulated.reasoning_content += token;
auto& block = blocks[index];
block.type = "thinking";
block.raw["type"] = "thinking";
if (!block.raw.contains("thinking") ||
!block.raw["thinking"].is_string()) {
block.raw["thinking"] = "";
}
block.raw["thinking"].get_ref<std::string&>() += token;
if (!token.empty()) {
StreamEvent event;
event.type = StreamEventType::ReasoningDelta;
event.content = token;
callback(event);
}