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
//! Cloud inference provider
//!
//! Wraps the existing `LlmClient` to implement `InferenceProvider` with
//! tool calling and structured output support across OpenAI, Anthropic,
//! and OpenRouter backends.
use crate::http_input::llm_client::{LlmClient, LlmProvider};
use crate::reasoning::conversation::Conversation;
use crate::reasoning::inference::*;
use async_trait::async_trait;
/// Map an Anthropic `stop_reason` string to a [`FinishReason`].
///
/// `has_tool_calls` is whether the parsed content produced at least one
/// `tool_use` block. A `"refusal"` (safety-classifier decline; Anthropic
/// returns it with HTTP 200) maps to [`FinishReason::Refusal`] so the loop can
/// fail over rather than reading a refused turn as an empty, successful stop.
fn map_anthropic_stop_reason(stop_reason: &str, has_tool_calls: bool) -> FinishReason {
match stop_reason {
"tool_use" => FinishReason::ToolCalls,
"max_tokens" => FinishReason::MaxTokens,
"refusal" => FinishReason::Refusal,
// end_turn / stop_sequence / pause_turn / anything else: a turn that
// still emitted tool calls is a tool turn; otherwise a plain stop.
_ if has_tool_calls => FinishReason::ToolCalls,
_ => FinishReason::Stop,
}
}
/// Cloud inference provider wrapping `LlmClient`.
pub struct CloudInferenceProvider {
client: LlmClient,
}
impl CloudInferenceProvider {
/// Create a new cloud provider wrapping an existing LlmClient.
pub fn new(client: LlmClient) -> Self {
Self { client }
}
/// Auto-detect from environment, returning None if no API key is set.
pub fn from_env() -> Option<Self> {
LlmClient::from_env().map(|c| Self { client: c })
}
/// Like `from_env`, but also accepts an optional `SecretStore` so API
/// keys can be sourced from HashiCorp Vault, OpenBao, or the file
/// backend instead of env vars. See [`LlmClient::from_env_or_secrets`]
/// for the resolution order and the `*_API_KEY_REF` env vars that
/// point at secret-store keys. When `store` is `None` or no `*_REF` is
/// configured, behaviour is identical to `from_env`.
pub async fn from_env_or_secrets(
store: Option<std::sync::Arc<dyn crate::secrets::SecretStore + Send + Sync>>,
) -> Option<Self> {
LlmClient::from_env_or_secrets(store)
.await
.map(|c| Self { client: c })
}
/// Build the request body for OpenAI-compatible APIs (OpenAI, OpenRouter).
fn build_openai_body(
&self,
conversation: &Conversation,
options: &InferenceOptions,
) -> serde_json::Value {
let model = options
.model
.as_deref()
.unwrap_or_else(|| self.client.model());
let mut body = serde_json::json!({
"model": model,
"messages": conversation.to_openai_messages(),
"max_tokens": options.max_tokens,
"temperature": options.temperature,
});
// Add tools if provided
if !options.tool_definitions.is_empty() {
let tools: Vec<serde_json::Value> = options
.tool_definitions
.iter()
.map(|td| {
serde_json::json!({
"type": "function",
"function": {
"name": td.name,
"description": td.description,
"parameters": td.parameters,
}
})
})
.collect();
body["tools"] = serde_json::Value::Array(tools);
// OpenAI Chat Completions tool_choice. Only emit when an
// explicit choice is supplied; absent the field, OpenAI
// defaults to "auto".
if let Some(choice) = &options.tool_choice {
body["tool_choice"] = match choice {
crate::reasoning::inference::ToolChoice::Auto => {
serde_json::Value::String("auto".into())
}
crate::reasoning::inference::ToolChoice::Any => {
serde_json::Value::String("required".into())
}
crate::reasoning::inference::ToolChoice::Tool { name } => {
serde_json::json!({
"type": "function",
"function": {"name": name}
})
}
};
}
}
// Add response_format if not plain text
match &options.response_format {
ResponseFormat::Text => {}
ResponseFormat::JsonObject => {
body["response_format"] = serde_json::json!({"type": "json_object"});
}
ResponseFormat::JsonSchema { schema, name } => {
body["response_format"] = serde_json::json!({
"type": "json_schema",
"json_schema": {
"name": name.as_deref().unwrap_or("response"),
"schema": schema,
}
});
}
}
body
}
/// Build the request body for the Anthropic Messages API.
fn build_anthropic_body(
&self,
conversation: &Conversation,
options: &InferenceOptions,
) -> serde_json::Value {
let model = options
.model
.as_deref()
.unwrap_or_else(|| self.client.model());
let (system, messages) = conversation.to_anthropic_messages();
let mut body = serde_json::json!({
"model": model,
"messages": messages,
"max_tokens": options.max_tokens,
});
// Anthropic uses temperature in metadata, not a direct field always present
if options.temperature > 0.0 {
body["temperature"] = serde_json::json!(options.temperature);
}
// Prompt caching is a prefix match: bytes up to a `cache_control`
// breakpoint are cached and reused verbatim, and any change
// anywhere in that prefix invalidates it. Render order for the
// Anthropic Messages API is tools -> system -> messages, so a
// single breakpoint on the LAST block of the stable (tools+system)
// prefix caches everything before it in one shot. We emit at most
// one breakpoint here (the API caps requests at 4 total): on the
// system block when a system prompt is present (system renders
// after tools, so this covers both tools and system), otherwise on
// the last tool so the tool definitions alone still cache.
let has_system = system.is_some();
if let Some(sys) = system {
body["system"] = serde_json::json!([
{
"type": "text",
"text": sys,
"cache_control": { "type": "ephemeral" }
}
]);
}
// Add tools
if !options.tool_definitions.is_empty() {
let mut tools: Vec<serde_json::Value> = options
.tool_definitions
.iter()
.map(|td| {
serde_json::json!({
"name": td.name,
"description": td.description,
"input_schema": td.parameters,
})
})
.collect();
// No system prompt to carry the breakpoint -- put it on the
// last tool instead so the tool-definitions prefix still caches.
if !has_system {
if let Some(last_tool) = tools.last_mut().and_then(|t| t.as_object_mut()) {
last_tool.insert(
"cache_control".to_string(),
serde_json::json!({"type": "ephemeral"}),
);
}
}
body["tools"] = serde_json::Value::Array(tools);
// Anthropic Messages API tool_choice. We emit it only when
// an explicit choice is set; absent the field, Anthropic
// defaults to {"type":"auto"}.
if let Some(choice) = &options.tool_choice {
body["tool_choice"] = match choice {
crate::reasoning::inference::ToolChoice::Auto => {
serde_json::json!({"type": "auto"})
}
crate::reasoning::inference::ToolChoice::Any => {
serde_json::json!({"type": "any"})
}
crate::reasoning::inference::ToolChoice::Tool { name } => {
serde_json::json!({"type": "tool", "name": name})
}
};
}
}
// Forward provider-specific extras (e.g. Anthropic's `output_config`
// for effort, `thinking`, etc.) after the body is otherwise fully
// built. This is the enabling mechanism for passing Anthropic-only
// params without a code change here; `extra` is applied last and so
// overrides any same-named key set above, by design. Deliberately
// does NOT hardcode a default `effort` or `thinking` -- this method
// is shared by every Anthropic-routed call, and a hardcoded default
// would silently change behavior for callers that don't want it.
for (k, v) in &options.extra {
body[k] = v.clone();
}
body
}
/// Parse an OpenAI-format response into InferenceResponse.
fn parse_openai_response(
&self,
resp: &serde_json::Value,
model: &str,
) -> Result<InferenceResponse, InferenceError> {
let choice = resp
.get("choices")
.and_then(|c| c.get(0))
.ok_or_else(|| InferenceError::ParseError("No choices in response".into()))?;
let message = choice
.get("message")
.ok_or_else(|| InferenceError::ParseError("No message in choice".into()))?;
let content = message
.get("content")
.and_then(|c| c.as_str())
.unwrap_or("")
.to_string();
let tool_calls = message
.get("tool_calls")
.and_then(|tc| tc.as_array())
.map(|arr| {
arr.iter()
.filter_map(|tc| {
let id = tc.get("id")?.as_str()?.to_string();
let func = tc.get("function")?;
let name = func.get("name")?.as_str()?.to_string();
let arguments = func.get("arguments")?.as_str()?.to_string();
Some(ToolCallRequest {
id,
name,
arguments,
})
})
.collect::<Vec<_>>()
})
.unwrap_or_default();
let finish_reason = match choice
.get("finish_reason")
.and_then(|f| f.as_str())
.unwrap_or("stop")
{
"tool_calls" => FinishReason::ToolCalls,
"length" => FinishReason::MaxTokens,
"content_filter" => FinishReason::ContentFilter,
_ => {
if tool_calls.is_empty() {
FinishReason::Stop
} else {
FinishReason::ToolCalls
}
}
};
let usage = resp
.get("usage")
.map(|u| Usage {
prompt_tokens: u.get("prompt_tokens").and_then(|v| v.as_u64()).unwrap_or(0) as u32,
completion_tokens: u
.get("completion_tokens")
.and_then(|v| v.as_u64())
.unwrap_or(0) as u32,
total_tokens: u.get("total_tokens").and_then(|v| v.as_u64()).unwrap_or(0) as u32,
})
.unwrap_or_default();
let actual_model = resp
.get("model")
.and_then(|m| m.as_str())
.unwrap_or(model)
.to_string();
Ok(InferenceResponse {
content,
tool_calls,
finish_reason,
usage,
model: actual_model,
})
}
/// Parse an Anthropic-format response into InferenceResponse.
fn parse_anthropic_response(
&self,
resp: &serde_json::Value,
model: &str,
) -> Result<InferenceResponse, InferenceError> {
let content_blocks = resp
.get("content")
.and_then(|c| c.as_array())
.ok_or_else(|| InferenceError::ParseError("No content in response".into()))?;
let mut text_content = String::new();
let mut tool_calls = Vec::new();
for block in content_blocks {
match block.get("type").and_then(|t| t.as_str()) {
Some("text") => {
if let Some(text) = block.get("text").and_then(|t| t.as_str()) {
if !text_content.is_empty() {
text_content.push('\n');
}
text_content.push_str(text);
}
}
Some("tool_use") => {
if let (Some(id), Some(name), Some(input)) = (
block.get("id").and_then(|v| v.as_str()),
block.get("name").and_then(|v| v.as_str()),
block.get("input"),
) {
tool_calls.push(ToolCallRequest {
id: id.to_string(),
name: name.to_string(),
arguments: serde_json::to_string(input).unwrap_or_default(),
});
}
}
_ => {}
}
}
let stop_reason = resp
.get("stop_reason")
.and_then(|s| s.as_str())
.unwrap_or("end_turn");
let finish_reason = map_anthropic_stop_reason(stop_reason, !tool_calls.is_empty());
// A turn that produced neither text nor a tool call is a no-progress
// turn (e.g. a thinking-only / redacted-thinking-only turn, or a
// block type we don't parse). It's indistinguishable downstream from a
// deliberate empty stop, so warn to make loop-termination diagnosable.
if finish_reason == FinishReason::Refusal {
tracing::warn!(
"Anthropic response was a refusal (stop_reason=refusal); returning FinishReason::Refusal"
);
} else if text_content.is_empty() && tool_calls.is_empty() {
tracing::warn!(
"Anthropic response produced no text and no tool calls (stop_reason={}); \
the turn made no progress — likely a thinking-only turn or an unparsed block type",
stop_reason
);
}
let usage = resp
.get("usage")
.map(|u| {
let input = u.get("input_tokens").and_then(|v| v.as_u64()).unwrap_or(0) as u32;
let output = u.get("output_tokens").and_then(|v| v.as_u64()).unwrap_or(0) as u32;
Usage {
prompt_tokens: input,
completion_tokens: output,
total_tokens: input + output,
}
})
.unwrap_or_default();
let actual_model = resp
.get("model")
.and_then(|m| m.as_str())
.unwrap_or(model)
.to_string();
Ok(InferenceResponse {
content: text_content,
tool_calls,
finish_reason,
usage,
model: actual_model,
})
}
}
#[async_trait]
impl InferenceProvider for CloudInferenceProvider {
async fn complete(
&self,
conversation: &Conversation,
options: &InferenceOptions,
) -> Result<InferenceResponse, InferenceError> {
// Access reqwest client and provider info through the LlmClient
// We need to make the HTTP call ourselves since LlmClient::chat_completion
// only supports simple system+user messages without tools.
let is_anthropic = matches!(self.client.provider(), LlmProvider::Anthropic);
let model = options
.model
.as_deref()
.unwrap_or_else(|| self.client.model());
// Bedrock signs its own requests with SigV4; route through the shared
// LlmClient Converse path rather than the hand-built HTTP path below.
// Use the caller-supplied temperature and max_tokens so the reasoning
// loop's configured values are honoured (rather than fixed defaults).
#[cfg(feature = "bedrock")]
if matches!(self.client.provider(), LlmProvider::Bedrock) {
let (system_opt, messages) = conversation.to_anthropic_messages();
let system = system_opt.as_deref().unwrap_or("");
let tools: Vec<serde_json::Value> = options
.tool_definitions
.iter()
.map(|td| {
serde_json::json!({
"name": td.name,
"description": td.description,
"input_schema": td.parameters,
})
})
.collect();
let resp_json = self
.client
.bedrock_converse(
system,
&messages,
&tools,
options.temperature,
options.max_tokens,
)
.await
.map_err(|e| InferenceError::Provider(format!("Bedrock Converse error: {e}")))?;
return self.parse_anthropic_response(&resp_json, model);
}
let body = if is_anthropic {
self.build_anthropic_body(conversation, options)
} else {
self.build_openai_body(conversation, options)
};
// Build and send the HTTP request using reqwest
let http_client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(120))
.build()
.map_err(|e| InferenceError::Provider(format!("HTTP client error: {}", e)))?;
// The base URL and API key were resolved once at LlmClient
// construction (env or secret store) — reuse the cached values
// instead of re-reading the env on every request.
let base = self.client.base_url();
let api_key = self.client.api_key();
let (url, request_builder) = if is_anthropic {
let url = format!("{}/messages", base);
let rb = http_client
.post(&url)
.header("x-api-key", api_key)
.header("anthropic-version", "2023-06-01")
.header("content-type", "application/json")
.json(&body);
(url, rb)
} else {
let url = format!("{}/chat/completions", base);
let mut rb = http_client
.post(&url)
.header("authorization", format!("Bearer {}", api_key))
.header("content-type", "application/json");
if matches!(self.client.provider(), LlmProvider::OpenRouter) {
for (k, v) in crate::http_input::llm_client::openrouter_attribution_headers() {
rb = rb.header(k, v);
}
}
let rb = rb.json(&body);
(url, rb)
};
tracing::debug!(
"Cloud inference: provider={} model={} url={}",
self.provider_name(),
model,
url
);
// Debug-level fingerprint of the request body. Useful for
// diagnosing why an agent terminates early (missing tool_choice,
// empty messages array, etc.). Enable with RUST_LOG=symbi_runtime=debug.
tracing::debug!(
"Cloud request fingerprint: tool_choice={} tools={} system_chars={} msg_count={}",
body.get("tool_choice")
.map(|v| v.to_string())
.unwrap_or_else(|| "<absent>".into()),
body.get("tools")
.and_then(|v| v.as_array())
.map(|a| a.len())
.unwrap_or(0),
body.get("system")
.and_then(|v| v.as_str())
.map(|s| s.len())
.unwrap_or(0),
body.get("messages")
.and_then(|v| v.as_array())
.map(|a| a.len())
.unwrap_or(0),
);
let start = std::time::Instant::now();
let response = request_builder.send().await.map_err(|e| {
if e.is_timeout() {
InferenceError::Timeout(std::time::Duration::from_secs(120))
} else {
InferenceError::Provider(format!("Request failed: {}", e))
}
})?;
let status = response.status();
if status.as_u16() == 429 {
let retry_after = response
.headers()
.get("retry-after")
.and_then(|v| v.to_str().ok())
.and_then(|v| v.parse::<u64>().ok())
.unwrap_or(1000);
return Err(InferenceError::RateLimited {
retry_after_ms: retry_after * 1000,
});
}
if !status.is_success() {
let error_text = response
.text()
.await
.unwrap_or_else(|_| "Unknown error".into());
tracing::warn!(
"Cloud API non-success: status={} body={}",
status,
error_text.chars().take(400).collect::<String>()
);
return Err(InferenceError::Provider(format!(
"API error ({}): {}",
status, error_text
)));
}
let resp_json: serde_json::Value = response
.json()
.await
.map_err(|e| InferenceError::ParseError(format!("JSON parse error: {}", e)))?;
let latency = start.elapsed();
tracing::debug!("Cloud inference completed in {:?}", latency);
// Debug-level response-shape log. Pairs with the request
// fingerprint above for diagnosing loop termination.
if is_anthropic {
let stop = resp_json
.get("stop_reason")
.and_then(|v| v.as_str())
.unwrap_or("<absent>");
let content_types: Vec<&str> = resp_json
.get("content")
.and_then(|v| v.as_array())
.map(|arr| {
arr.iter()
.filter_map(|c| c.get("type").and_then(|t| t.as_str()))
.collect()
})
.unwrap_or_default();
tracing::debug!(
"Cloud response fingerprint: stop_reason={} content_types={:?}",
stop,
content_types
);
}
if is_anthropic {
self.parse_anthropic_response(&resp_json, model)
} else {
self.parse_openai_response(&resp_json, model)
}
}
fn provider_name(&self) -> &str {
match self.client.provider() {
LlmProvider::OpenRouter => "openrouter",
LlmProvider::OpenAI => "openai",
LlmProvider::Anthropic => "anthropic",
#[cfg(feature = "bedrock")]
LlmProvider::Bedrock => "bedrock",
}
}
fn default_model(&self) -> &str {
self.client.model()
}
fn supports_native_tools(&self) -> bool {
true
}
fn supports_structured_output(&self) -> bool {
// OpenAI and Anthropic both support structured output
true
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::reasoning::conversation::{ConversationMessage, ToolCall};
use serial_test::serial;
#[test]
fn test_build_openai_body_basic() {
// We can't easily create a CloudInferenceProvider without env vars,
// so test the parsing functions directly with mock data.
let openai_response = serde_json::json!({
"choices": [{
"message": {
"role": "assistant",
"content": "Hello!",
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 5,
"total_tokens": 15,
},
"model": "gpt-4o"
});
// Simulate parsing
let choice = openai_response["choices"][0].clone();
let content = choice["message"]["content"].as_str().unwrap();
assert_eq!(content, "Hello!");
}
#[test]
fn test_parse_openai_response_with_tools() {
let resp = serde_json::json!({
"choices": [{
"message": {
"role": "assistant",
"content": null,
"tool_calls": [{
"id": "call_abc123",
"type": "function",
"function": {
"name": "web_search",
"arguments": "{\"query\": \"rust crates\"}"
}
}]
},
"finish_reason": "tool_calls"
}],
"usage": {
"prompt_tokens": 20,
"completion_tokens": 10,
"total_tokens": 30,
},
"model": "gpt-4o"
});
let tool_calls = resp["choices"][0]["message"]["tool_calls"]
.as_array()
.unwrap();
assert_eq!(tool_calls.len(), 1);
assert_eq!(tool_calls[0]["function"]["name"], "web_search");
}
#[test]
fn test_parse_anthropic_response() {
let resp = serde_json::json!({
"content": [
{"type": "text", "text": "I'll search for that."},
{
"type": "tool_use",
"id": "toolu_123",
"name": "web_search",
"input": {"query": "rust crates"}
}
],
"stop_reason": "tool_use",
"usage": {
"input_tokens": 15,
"output_tokens": 20,
},
"model": "claude-sonnet-4-5-20250514"
});
let content = resp["content"].as_array().unwrap();
assert_eq!(content.len(), 2);
assert_eq!(content[0]["type"], "text");
assert_eq!(content[1]["type"], "tool_use");
assert_eq!(content[1]["name"], "web_search");
}
#[test]
fn test_map_anthropic_stop_reason() {
use super::map_anthropic_stop_reason;
// A refusal is surfaced distinctly — not collapsed into Stop — so the
// loop can fail over instead of reading it as an empty completion.
assert_eq!(
map_anthropic_stop_reason("refusal", false),
FinishReason::Refusal
);
// Even a refused turn that somehow carried tool calls stays a Refusal.
assert_eq!(
map_anthropic_stop_reason("refusal", true),
FinishReason::Refusal
);
// Known terminal/tool reasons map as before.
assert_eq!(
map_anthropic_stop_reason("tool_use", false),
FinishReason::ToolCalls
);
assert_eq!(
map_anthropic_stop_reason("max_tokens", false),
FinishReason::MaxTokens
);
// end_turn with tool calls is a tool turn; without, a plain stop.
assert_eq!(
map_anthropic_stop_reason("end_turn", true),
FinishReason::ToolCalls
);
assert_eq!(
map_anthropic_stop_reason("end_turn", false),
FinishReason::Stop
);
// A thinking-only turn (no text, no tool calls) reports stop_reason
// end_turn and no tool calls -> Stop (the parser separately warns).
assert_eq!(
map_anthropic_stop_reason("pause_turn", false),
FinishReason::Stop
);
}
#[test]
fn test_conversation_to_openai_format() {
let mut conv = Conversation::with_system("sys");
conv.push(ConversationMessage::user("hello"));
conv.push(ConversationMessage::assistant_tool_calls(vec![ToolCall {
id: "tc1".into(),
name: "search".into(),
arguments: r#"{"q":"test"}"#.into(),
}]));
conv.push(ConversationMessage::tool_result("tc1", "search", "result"));
let msgs = conv.to_openai_messages();
assert_eq!(msgs.len(), 4);
assert_eq!(msgs[0]["role"], "system");
assert_eq!(msgs[2]["tool_calls"][0]["function"]["name"], "search");
assert_eq!(msgs[3]["tool_call_id"], "tc1");
}
/// FIX 1 + FIX 2: `build_anthropic_body` emits a single prompt-cache
/// breakpoint on the byte-stable prefix, and forwards `options.extra`
/// into the request body verbatim.
///
/// Constructing a `CloudInferenceProvider` requires a real `LlmClient`,
/// which is only buildable via `from_env()` (no bypass constructor
/// exists) -- so this follows the same env-var-dance + `#[serial]`
/// pattern as `test_cloud_provider_name_bedrock` below.
#[serial]
#[test]
fn test_build_anthropic_body_cache_control_and_extra() {
use crate::reasoning::inference::{InferenceOptions, ToolDefinition};
std::env::set_var("ANTHROPIC_API_KEY", "test-key-not-real");
for k in ["OPENROUTER_API_KEY", "OPENAI_API_KEY", "BEDROCK_MODEL_ID"] {
std::env::remove_var(k);
}
let provider = CloudInferenceProvider::from_env()
.expect("CloudInferenceProvider should resolve via ANTHROPIC_API_KEY");
// Case 1: system prompt present -> the breakpoint goes on the
// system block, emitted as a content-block array (not a bare
// string), and extra params are forwarded into the body.
let conv = Conversation::with_system("You are a helpful assistant.");
let mut options = InferenceOptions {
tool_definitions: vec![ToolDefinition {
name: "search".into(),
description: "Search the web".into(),
parameters: serde_json::json!({"type": "object", "properties": {}}),
}],
..Default::default()
};
options.extra.insert(
"output_config".into(),
serde_json::json!({"effort": "high"}),
);
let body = provider.build_anthropic_body(&conv, &options);
let system = body["system"]
.as_array()
.expect("system should be a content-block array carrying cache_control");
assert_eq!(system.len(), 1);
assert_eq!(system[0]["type"], "text");
assert_eq!(system[0]["text"], "You are a helpful assistant.");
assert_eq!(system[0]["cache_control"]["type"], "ephemeral");
// At most one breakpoint from this method: since system carried it,
// the tool must not also carry one.
let tools = body["tools"].as_array().expect("tools array");
assert!(tools[0].get("cache_control").is_none());
// FIX 2: `extra` lands in the body verbatim.
assert_eq!(body["output_config"]["effort"], "high");
// Case 2: no system prompt, tools present -> the breakpoint goes on
// the LAST tool instead, so the tool-definitions prefix still caches.
let conv_no_system = Conversation::new();
let options_no_system = InferenceOptions {
tool_definitions: vec![
ToolDefinition {
name: "first_tool".into(),
description: "d1".into(),
parameters: serde_json::json!({"type": "object", "properties": {}}),
},
ToolDefinition {
name: "last_tool".into(),
description: "d2".into(),
parameters: serde_json::json!({"type": "object", "properties": {}}),
},
],
..Default::default()
};
let body_no_system = provider.build_anthropic_body(&conv_no_system, &options_no_system);
assert!(body_no_system.get("system").is_none());
let tools_no_system = body_no_system["tools"].as_array().expect("tools array");
assert_eq!(tools_no_system.len(), 2);
assert!(tools_no_system[0].get("cache_control").is_none());
assert_eq!(tools_no_system[1]["cache_control"]["type"], "ephemeral");
std::env::remove_var("ANTHROPIC_API_KEY");
}
/// Verify that a Bedrock-configured `CloudInferenceProvider` reports
/// `provider_name() == "bedrock"` without making any network calls.
#[cfg(feature = "bedrock")]
#[serial]
#[test]
fn test_cloud_provider_name_bedrock() {
// Temporarily set the env vars used by `CloudInferenceProvider::from_env`.
std::env::set_var(
"BEDROCK_MODEL_ID",
"anthropic.claude-3-5-sonnet-20241022-v2:0",
);
std::env::set_var("AWS_REGION", "us-east-1");
for k in ["OPENROUTER_API_KEY", "OPENAI_API_KEY", "ANTHROPIC_API_KEY"] {
std::env::remove_var(k);
}
let provider = CloudInferenceProvider::from_env()
.expect("CloudInferenceProvider should resolve via BEDROCK_MODEL_ID");
assert_eq!(provider.provider_name(), "bedrock");
std::env::remove_var("BEDROCK_MODEL_ID");
std::env::remove_var("AWS_REGION");
}
}