client_integration.rs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. use std::collections::HashMap;
  2. use std::sync::Arc;
  3. use std::sync::{Mutex as StdMutex, OnceLock};
  4. use std::time::Duration;
  5. use api::{
  6. AnthropicClient, ApiClient, ApiError, AuthSource, ContentBlockDelta, ContentBlockDeltaEvent,
  7. ContentBlockStartEvent, InputContentBlock, InputMessage, MessageDeltaEvent, MessageRequest,
  8. OutputContentBlock, PromptCache, PromptCacheConfig, ProviderClient, StreamEvent, ToolChoice,
  9. ToolDefinition,
  10. };
  11. use serde_json::json;
  12. use telemetry::{ClientIdentity, MemoryTelemetrySink, SessionTracer, TelemetryEvent};
  13. use tokio::io::{AsyncReadExt, AsyncWriteExt};
  14. use tokio::net::TcpListener;
  15. use tokio::sync::Mutex;
  16. fn env_lock() -> std::sync::MutexGuard<'static, ()> {
  17. static LOCK: OnceLock<StdMutex<()>> = OnceLock::new();
  18. LOCK.get_or_init(|| StdMutex::new(()))
  19. .lock()
  20. .unwrap_or_else(std::sync::PoisonError::into_inner)
  21. }
  22. #[tokio::test]
  23. async fn send_message_posts_json_and_parses_response() {
  24. let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
  25. let body = concat!(
  26. "{",
  27. "\"id\":\"msg_test\",",
  28. "\"type\":\"message\",",
  29. "\"role\":\"assistant\",",
  30. "\"content\":[{\"type\":\"text\",\"text\":\"Hello from Claude\"}],",
  31. "\"model\":\"claude-3-7-sonnet-latest\",",
  32. "\"stop_reason\":\"end_turn\",",
  33. "\"stop_sequence\":null,",
  34. "\"usage\":{\"input_tokens\":12,\"output_tokens\":4},",
  35. "\"request_id\":\"req_body_123\"",
  36. "}"
  37. );
  38. let server = spawn_server(
  39. state.clone(),
  40. vec![http_response("200 OK", "application/json", body)],
  41. )
  42. .await;
  43. let client = ApiClient::new("test-key")
  44. .with_auth_token(Some("proxy-token".to_string()))
  45. .with_base_url(server.base_url());
  46. let response = client
  47. .send_message(&sample_request(false))
  48. .await
  49. .expect("request should succeed");
  50. assert_eq!(response.id, "msg_test");
  51. assert_eq!(response.total_tokens(), 16);
  52. assert_eq!(response.request_id.as_deref(), Some("req_body_123"));
  53. assert_eq!(response.usage.cache_creation_input_tokens, 0);
  54. assert_eq!(response.usage.cache_read_input_tokens, 0);
  55. assert_eq!(
  56. response.content,
  57. vec![OutputContentBlock::Text {
  58. text: "Hello from Claude".to_string(),
  59. }]
  60. );
  61. let captured = state.lock().await;
  62. let request = captured.first().expect("server should capture request");
  63. assert_eq!(request.method, "POST");
  64. assert_eq!(request.path, "/v1/messages");
  65. assert_eq!(
  66. request.headers.get("x-api-key").map(String::as_str),
  67. Some("test-key")
  68. );
  69. assert_eq!(
  70. request.headers.get("authorization").map(String::as_str),
  71. Some("Bearer proxy-token")
  72. );
  73. assert_eq!(
  74. request.headers.get("anthropic-version").map(String::as_str),
  75. Some("2023-06-01")
  76. );
  77. assert_eq!(
  78. request.headers.get("user-agent").map(String::as_str),
  79. Some("claude-code/0.1.0")
  80. );
  81. assert_eq!(
  82. request.headers.get("anthropic-beta").map(String::as_str),
  83. Some("claude-code-20250219,prompt-caching-scope-2026-01-05")
  84. );
  85. let body: serde_json::Value =
  86. serde_json::from_str(&request.body).expect("request body should be json");
  87. assert_eq!(
  88. body.get("model").and_then(serde_json::Value::as_str),
  89. Some("claude-3-7-sonnet-latest")
  90. );
  91. assert!(body.get("stream").is_none());
  92. assert_eq!(body["tools"][0]["name"], json!("get_weather"));
  93. assert_eq!(body["tool_choice"]["type"], json!("auto"));
  94. assert_eq!(
  95. body["betas"],
  96. json!(["claude-code-20250219", "prompt-caching-scope-2026-01-05"])
  97. );
  98. }
  99. #[tokio::test]
  100. async fn send_message_applies_request_profile_and_records_telemetry() {
  101. let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
  102. let server = spawn_server(
  103. state.clone(),
  104. vec![http_response_with_headers(
  105. "200 OK",
  106. "application/json",
  107. concat!(
  108. "{",
  109. "\"id\":\"msg_profile\",",
  110. "\"type\":\"message\",",
  111. "\"role\":\"assistant\",",
  112. "\"content\":[{\"type\":\"text\",\"text\":\"ok\"}],",
  113. "\"model\":\"claude-3-7-sonnet-latest\",",
  114. "\"stop_reason\":\"end_turn\",",
  115. "\"stop_sequence\":null,",
  116. "\"usage\":{\"input_tokens\":1,\"cache_creation_input_tokens\":2,\"cache_read_input_tokens\":3,\"output_tokens\":1}",
  117. "}"
  118. ),
  119. &[("request-id", "req_profile_123")],
  120. )],
  121. )
  122. .await;
  123. let sink = Arc::new(MemoryTelemetrySink::default());
  124. let client = AnthropicClient::new("test-key")
  125. .with_base_url(server.base_url())
  126. .with_client_identity(ClientIdentity::new("claude-code", "9.9.9").with_runtime("rust-cli"))
  127. .with_beta("tools-2026-04-01")
  128. .with_extra_body_param("metadata", json!({"source": "clawd-code"}))
  129. .with_session_tracer(SessionTracer::new("session-telemetry", sink.clone()));
  130. let response = client
  131. .send_message(&sample_request(false))
  132. .await
  133. .expect("request should succeed");
  134. assert_eq!(response.request_id.as_deref(), Some("req_profile_123"));
  135. let captured = state.lock().await;
  136. let request = captured.first().expect("server should capture request");
  137. assert_eq!(
  138. request.headers.get("anthropic-beta").map(String::as_str),
  139. Some("claude-code-20250219,prompt-caching-scope-2026-01-05,tools-2026-04-01")
  140. );
  141. assert_eq!(
  142. request.headers.get("user-agent").map(String::as_str),
  143. Some("claude-code/9.9.9")
  144. );
  145. let body: serde_json::Value =
  146. serde_json::from_str(&request.body).expect("request body should be json");
  147. assert_eq!(body["metadata"]["source"], json!("clawd-code"));
  148. assert_eq!(
  149. body["betas"],
  150. json!([
  151. "claude-code-20250219",
  152. "prompt-caching-scope-2026-01-05",
  153. "tools-2026-04-01"
  154. ])
  155. );
  156. let events = sink.events();
  157. assert_eq!(events.len(), 6);
  158. assert!(matches!(
  159. &events[0],
  160. TelemetryEvent::HttpRequestStarted {
  161. session_id,
  162. attempt: 1,
  163. method,
  164. path,
  165. ..
  166. } if session_id == "session-telemetry" && method == "POST" && path == "/v1/messages"
  167. ));
  168. assert!(matches!(
  169. &events[1],
  170. TelemetryEvent::SessionTrace(trace) if trace.name == "http_request_started"
  171. ));
  172. assert!(matches!(
  173. &events[2],
  174. TelemetryEvent::HttpRequestSucceeded {
  175. request_id,
  176. status: 200,
  177. ..
  178. } if request_id.as_deref() == Some("req_profile_123")
  179. ));
  180. assert!(matches!(
  181. &events[3],
  182. TelemetryEvent::SessionTrace(trace) if trace.name == "http_request_succeeded"
  183. ));
  184. assert!(matches!(
  185. &events[4],
  186. TelemetryEvent::Analytics(event)
  187. if event.namespace == "api"
  188. && event.action == "message_usage"
  189. && event.properties.get("request_id") == Some(&json!("req_profile_123"))
  190. && event.properties.get("total_tokens") == Some(&json!(7))
  191. && event.properties.get("estimated_cost_usd") == Some(&json!("$0.0001"))
  192. ));
  193. assert!(matches!(
  194. &events[5],
  195. TelemetryEvent::SessionTrace(trace) if trace.name == "analytics"
  196. ));
  197. }
  198. #[tokio::test]
  199. async fn send_message_parses_prompt_cache_token_usage_from_response() {
  200. let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
  201. let body = concat!(
  202. "{",
  203. "\"id\":\"msg_cache_tokens\",",
  204. "\"type\":\"message\",",
  205. "\"role\":\"assistant\",",
  206. "\"content\":[{\"type\":\"text\",\"text\":\"Cache tokens\"}],",
  207. "\"model\":\"claude-3-7-sonnet-latest\",",
  208. "\"stop_reason\":\"end_turn\",",
  209. "\"stop_sequence\":null,",
  210. "\"usage\":{\"input_tokens\":12,\"cache_creation_input_tokens\":321,\"cache_read_input_tokens\":654,\"output_tokens\":4}",
  211. "}"
  212. );
  213. let server = spawn_server(
  214. state,
  215. vec![http_response("200 OK", "application/json", body)],
  216. )
  217. .await;
  218. let client = AnthropicClient::new("test-key").with_base_url(server.base_url());
  219. let response = client
  220. .send_message(&sample_request(false))
  221. .await
  222. .expect("request should succeed");
  223. assert_eq!(response.usage.input_tokens, 12);
  224. assert_eq!(response.usage.cache_creation_input_tokens, 321);
  225. assert_eq!(response.usage.cache_read_input_tokens, 654);
  226. assert_eq!(response.usage.output_tokens, 4);
  227. }
  228. #[tokio::test]
  229. #[allow(clippy::await_holding_lock)]
  230. async fn stream_message_parses_sse_events_with_tool_use() {
  231. let _guard = env_lock();
  232. let temp_root = std::env::temp_dir().join(format!(
  233. "api-stream-cache-{}-{}",
  234. std::process::id(),
  235. std::time::SystemTime::now()
  236. .duration_since(std::time::UNIX_EPOCH)
  237. .expect("time")
  238. .as_nanos()
  239. ));
  240. std::env::set_var("CLAUDE_CONFIG_HOME", &temp_root);
  241. let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
  242. let sse = concat!(
  243. "event: message_start\n",
  244. "data: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_stream\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"model\":\"claude-3-7-sonnet-latest\",\"stop_reason\":null,\"stop_sequence\":null,\"usage\":{\"input_tokens\":8,\"cache_creation_input_tokens\":13,\"cache_read_input_tokens\":21,\"output_tokens\":0}}}\n\n",
  245. "event: content_block_start\n",
  246. "data: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"toolu_123\",\"name\":\"get_weather\",\"input\":{}}}\n\n",
  247. "event: content_block_delta\n",
  248. "data: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"city\\\":\\\"Paris\\\"}\"}}\n\n",
  249. "event: content_block_stop\n",
  250. "data: {\"type\":\"content_block_stop\",\"index\":0}\n\n",
  251. "event: message_delta\n",
  252. "data: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\",\"stop_sequence\":null},\"usage\":{\"input_tokens\":8,\"cache_creation_input_tokens\":34,\"cache_read_input_tokens\":55,\"output_tokens\":1}}\n\n",
  253. "event: message_stop\n",
  254. "data: {\"type\":\"message_stop\"}\n\n",
  255. "data: [DONE]\n\n"
  256. );
  257. let server = spawn_server(
  258. state.clone(),
  259. vec![http_response_with_headers(
  260. "200 OK",
  261. "text/event-stream",
  262. sse,
  263. &[("request-id", "req_stream_456")],
  264. )],
  265. )
  266. .await;
  267. let client = ApiClient::new("test-key")
  268. .with_auth_token(Some("proxy-token".to_string()))
  269. .with_base_url(server.base_url())
  270. .with_prompt_cache(PromptCache::new("stream-session"));
  271. let mut stream = client
  272. .stream_message(&sample_request(false))
  273. .await
  274. .expect("stream should start");
  275. assert_eq!(stream.request_id(), Some("req_stream_456"));
  276. let mut events = Vec::new();
  277. while let Some(event) = stream
  278. .next_event()
  279. .await
  280. .expect("stream event should parse")
  281. {
  282. events.push(event);
  283. }
  284. assert_eq!(events.len(), 6);
  285. assert!(matches!(events[0], StreamEvent::MessageStart(_)));
  286. assert!(matches!(
  287. events[1],
  288. StreamEvent::ContentBlockStart(ContentBlockStartEvent {
  289. content_block: OutputContentBlock::ToolUse { .. },
  290. ..
  291. })
  292. ));
  293. assert!(matches!(
  294. events[2],
  295. StreamEvent::ContentBlockDelta(ContentBlockDeltaEvent {
  296. delta: ContentBlockDelta::InputJsonDelta { .. },
  297. ..
  298. })
  299. ));
  300. assert!(matches!(events[3], StreamEvent::ContentBlockStop(_)));
  301. assert!(matches!(
  302. events[4],
  303. StreamEvent::MessageDelta(MessageDeltaEvent { .. })
  304. ));
  305. assert!(matches!(events[5], StreamEvent::MessageStop(_)));
  306. match &events[1] {
  307. StreamEvent::ContentBlockStart(ContentBlockStartEvent {
  308. content_block: OutputContentBlock::ToolUse { name, input, .. },
  309. ..
  310. }) => {
  311. assert_eq!(name, "get_weather");
  312. assert_eq!(input, &json!({}));
  313. }
  314. other => panic!("expected tool_use block, got {other:?}"),
  315. }
  316. let captured = state.lock().await;
  317. let request = captured.first().expect("server should capture request");
  318. assert!(request.body.contains("\"stream\":true"));
  319. let cache_stats = client
  320. .prompt_cache_stats()
  321. .expect("prompt cache stats should exist");
  322. assert_eq!(cache_stats.tracked_requests, 1);
  323. assert_eq!(cache_stats.last_cache_creation_input_tokens, Some(34));
  324. assert_eq!(cache_stats.last_cache_read_input_tokens, Some(55));
  325. assert_eq!(
  326. cache_stats.last_cache_source.as_deref(),
  327. Some("api-response")
  328. );
  329. std::fs::remove_dir_all(temp_root).expect("cleanup temp root");
  330. std::env::remove_var("CLAUDE_CONFIG_HOME");
  331. }
  332. #[tokio::test]
  333. async fn retries_retryable_failures_before_succeeding() {
  334. let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
  335. let server = spawn_server(
  336. state.clone(),
  337. vec![
  338. http_response(
  339. "429 Too Many Requests",
  340. "application/json",
  341. "{\"type\":\"error\",\"error\":{\"type\":\"rate_limit_error\",\"message\":\"slow down\"}}",
  342. ),
  343. http_response(
  344. "200 OK",
  345. "application/json",
  346. "{\"id\":\"msg_retry\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"type\":\"text\",\"text\":\"Recovered\"}],\"model\":\"claude-3-7-sonnet-latest\",\"stop_reason\":\"end_turn\",\"stop_sequence\":null,\"usage\":{\"input_tokens\":3,\"output_tokens\":2}}",
  347. ),
  348. ],
  349. )
  350. .await;
  351. let client = ApiClient::new("test-key")
  352. .with_base_url(server.base_url())
  353. .with_retry_policy(2, Duration::from_millis(1), Duration::from_millis(2));
  354. let response = client
  355. .send_message(&sample_request(false))
  356. .await
  357. .expect("retry should eventually succeed");
  358. assert_eq!(response.total_tokens(), 5);
  359. assert_eq!(state.lock().await.len(), 2);
  360. }
  361. #[tokio::test]
  362. async fn provider_client_dispatches_anthropic_requests() {
  363. let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
  364. let server = spawn_server(
  365. state.clone(),
  366. vec![http_response(
  367. "200 OK",
  368. "application/json",
  369. "{\"id\":\"msg_provider\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"type\":\"text\",\"text\":\"Dispatched\"}],\"model\":\"claude-3-7-sonnet-latest\",\"stop_reason\":\"end_turn\",\"stop_sequence\":null,\"usage\":{\"input_tokens\":3,\"output_tokens\":2}}",
  370. )],
  371. )
  372. .await;
  373. let client = ProviderClient::from_model_with_anthropic_auth(
  374. "claude-sonnet-4-6",
  375. Some(AuthSource::ApiKey("test-key".to_string())),
  376. )
  377. .expect("anthropic provider client should be constructed");
  378. let client = match client {
  379. ProviderClient::Anthropic(client) => {
  380. ProviderClient::Anthropic(client.with_base_url(server.base_url()))
  381. }
  382. other => panic!("expected anthropic provider, got {other:?}"),
  383. };
  384. let response = client
  385. .send_message(&sample_request(false))
  386. .await
  387. .expect("provider-dispatched request should succeed");
  388. assert_eq!(response.total_tokens(), 5);
  389. let captured = state.lock().await;
  390. let request = captured.first().expect("server should capture request");
  391. assert_eq!(request.path, "/v1/messages");
  392. assert_eq!(
  393. request.headers.get("x-api-key").map(String::as_str),
  394. Some("test-key")
  395. );
  396. }
  397. #[tokio::test]
  398. async fn surfaces_retry_exhaustion_for_persistent_retryable_errors() {
  399. let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
  400. let server = spawn_server(
  401. state.clone(),
  402. vec![
  403. http_response(
  404. "503 Service Unavailable",
  405. "application/json",
  406. "{\"type\":\"error\",\"error\":{\"type\":\"overloaded_error\",\"message\":\"busy\"}}",
  407. ),
  408. http_response(
  409. "503 Service Unavailable",
  410. "application/json",
  411. "{\"type\":\"error\",\"error\":{\"type\":\"overloaded_error\",\"message\":\"still busy\"}}",
  412. ),
  413. ],
  414. )
  415. .await;
  416. let client = ApiClient::new("test-key")
  417. .with_base_url(server.base_url())
  418. .with_retry_policy(1, Duration::from_millis(1), Duration::from_millis(2));
  419. let error = client
  420. .send_message(&sample_request(false))
  421. .await
  422. .expect_err("persistent 503 should fail");
  423. match error {
  424. ApiError::RetriesExhausted {
  425. attempts,
  426. last_error,
  427. } => {
  428. assert_eq!(attempts, 2);
  429. assert!(matches!(
  430. *last_error,
  431. ApiError::Api {
  432. status: reqwest::StatusCode::SERVICE_UNAVAILABLE,
  433. retryable: true,
  434. ..
  435. }
  436. ));
  437. }
  438. other => panic!("expected retries exhausted, got {other:?}"),
  439. }
  440. }
  441. #[tokio::test]
  442. #[allow(clippy::await_holding_lock)]
  443. async fn send_message_reuses_recent_completion_cache_entries() {
  444. let _guard = env_lock();
  445. let temp_root = std::env::temp_dir().join(format!(
  446. "api-prompt-cache-{}-{}",
  447. std::process::id(),
  448. std::time::SystemTime::now()
  449. .duration_since(std::time::UNIX_EPOCH)
  450. .expect("time")
  451. .as_nanos()
  452. ));
  453. std::env::set_var("CLAUDE_CONFIG_HOME", &temp_root);
  454. let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
  455. let server = spawn_server(
  456. state.clone(),
  457. vec![http_response(
  458. "200 OK",
  459. "application/json",
  460. "{\"id\":\"msg_cached\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"type\":\"text\",\"text\":\"Cached once\"}],\"model\":\"claude-3-7-sonnet-latest\",\"stop_reason\":\"end_turn\",\"stop_sequence\":null,\"usage\":{\"input_tokens\":3,\"cache_creation_input_tokens\":5,\"cache_read_input_tokens\":4000,\"output_tokens\":2}}",
  461. )],
  462. )
  463. .await;
  464. let client = AnthropicClient::new("test-key")
  465. .with_base_url(server.base_url())
  466. .with_prompt_cache(PromptCache::new("integration-session"));
  467. let first = client
  468. .send_message(&sample_request(false))
  469. .await
  470. .expect("first request should succeed");
  471. let second = client
  472. .send_message(&sample_request(false))
  473. .await
  474. .expect("second request should reuse cache");
  475. assert_eq!(first.content, second.content);
  476. assert_eq!(state.lock().await.len(), 1);
  477. let cache_stats = client
  478. .prompt_cache_stats()
  479. .expect("prompt cache stats should exist");
  480. assert_eq!(cache_stats.completion_cache_hits, 1);
  481. assert_eq!(cache_stats.completion_cache_misses, 1);
  482. assert_eq!(cache_stats.completion_cache_writes, 1);
  483. std::fs::remove_dir_all(temp_root).expect("cleanup temp root");
  484. std::env::remove_var("CLAUDE_CONFIG_HOME");
  485. }
  486. #[tokio::test]
  487. #[allow(clippy::await_holding_lock)]
  488. async fn send_message_tracks_unexpected_prompt_cache_breaks() {
  489. let _guard = env_lock();
  490. let temp_root = std::env::temp_dir().join(format!(
  491. "api-prompt-break-{}-{}",
  492. std::process::id(),
  493. std::time::SystemTime::now()
  494. .duration_since(std::time::UNIX_EPOCH)
  495. .expect("time")
  496. .as_nanos()
  497. ));
  498. std::env::set_var("CLAUDE_CONFIG_HOME", &temp_root);
  499. let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
  500. let server = spawn_server(
  501. state,
  502. vec![
  503. http_response(
  504. "200 OK",
  505. "application/json",
  506. "{\"id\":\"msg_one\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"type\":\"text\",\"text\":\"One\"}],\"model\":\"claude-3-7-sonnet-latest\",\"stop_reason\":\"end_turn\",\"stop_sequence\":null,\"usage\":{\"input_tokens\":3,\"cache_creation_input_tokens\":5,\"cache_read_input_tokens\":6000,\"output_tokens\":2}}",
  507. ),
  508. http_response(
  509. "200 OK",
  510. "application/json",
  511. "{\"id\":\"msg_two\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"type\":\"text\",\"text\":\"Two\"}],\"model\":\"claude-3-7-sonnet-latest\",\"stop_reason\":\"end_turn\",\"stop_sequence\":null,\"usage\":{\"input_tokens\":3,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":1000,\"output_tokens\":2}}",
  512. ),
  513. ],
  514. )
  515. .await;
  516. let request = sample_request(false);
  517. let client = AnthropicClient::new("test-key")
  518. .with_base_url(server.base_url())
  519. .with_prompt_cache(PromptCache::with_config(PromptCacheConfig {
  520. session_id: "break-session".to_string(),
  521. completion_ttl: Duration::from_secs(0),
  522. ..PromptCacheConfig::default()
  523. }));
  524. client
  525. .send_message(&request)
  526. .await
  527. .expect("first response should succeed");
  528. client
  529. .send_message(&request)
  530. .await
  531. .expect("second response should succeed");
  532. let cache_stats = client
  533. .prompt_cache_stats()
  534. .expect("prompt cache stats should exist");
  535. assert_eq!(cache_stats.unexpected_cache_breaks, 1);
  536. assert_eq!(
  537. cache_stats.last_break_reason.as_deref(),
  538. Some("cache read tokens dropped while prompt fingerprint remained stable")
  539. );
  540. std::fs::remove_dir_all(temp_root).expect("cleanup temp root");
  541. std::env::remove_var("CLAUDE_CONFIG_HOME");
  542. }
  543. #[tokio::test]
  544. #[ignore = "requires ANTHROPIC_API_KEY and network access"]
  545. async fn live_stream_smoke_test() {
  546. let client = ApiClient::from_env().expect("ANTHROPIC_API_KEY must be set");
  547. let mut stream = client
  548. .stream_message(&MessageRequest {
  549. model: std::env::var("ANTHROPIC_MODEL")
  550. .unwrap_or_else(|_| "claude-3-7-sonnet-latest".to_string()),
  551. max_tokens: 32,
  552. messages: vec![InputMessage::user_text(
  553. "Reply with exactly: hello from rust",
  554. )],
  555. system: None,
  556. tools: None,
  557. tool_choice: None,
  558. stream: false,
  559. })
  560. .await
  561. .expect("live stream should start");
  562. while let Some(_event) = stream
  563. .next_event()
  564. .await
  565. .expect("live stream should yield events")
  566. {}
  567. }
  568. #[derive(Debug, Clone, PartialEq, Eq)]
  569. struct CapturedRequest {
  570. method: String,
  571. path: String,
  572. headers: HashMap<String, String>,
  573. body: String,
  574. }
  575. struct TestServer {
  576. base_url: String,
  577. join_handle: tokio::task::JoinHandle<()>,
  578. }
  579. impl TestServer {
  580. fn base_url(&self) -> String {
  581. self.base_url.clone()
  582. }
  583. }
  584. impl Drop for TestServer {
  585. fn drop(&mut self) {
  586. self.join_handle.abort();
  587. }
  588. }
  589. async fn spawn_server(
  590. state: Arc<Mutex<Vec<CapturedRequest>>>,
  591. responses: Vec<String>,
  592. ) -> TestServer {
  593. let listener = TcpListener::bind("127.0.0.1:0")
  594. .await
  595. .expect("listener should bind");
  596. let address = listener
  597. .local_addr()
  598. .expect("listener should have local addr");
  599. let join_handle = tokio::spawn(async move {
  600. for response in responses {
  601. let (mut socket, _) = listener.accept().await.expect("server should accept");
  602. let mut buffer = Vec::new();
  603. let mut header_end = None;
  604. loop {
  605. let mut chunk = [0_u8; 1024];
  606. let read = socket
  607. .read(&mut chunk)
  608. .await
  609. .expect("request read should succeed");
  610. if read == 0 {
  611. break;
  612. }
  613. buffer.extend_from_slice(&chunk[..read]);
  614. if let Some(position) = find_header_end(&buffer) {
  615. header_end = Some(position);
  616. break;
  617. }
  618. }
  619. let header_end = header_end.expect("request should include headers");
  620. let (header_bytes, remaining) = buffer.split_at(header_end);
  621. let header_text =
  622. String::from_utf8(header_bytes.to_vec()).expect("headers should be utf8");
  623. let mut lines = header_text.split("\r\n");
  624. let request_line = lines.next().expect("request line should exist");
  625. let mut parts = request_line.split_whitespace();
  626. let method = parts.next().expect("method should exist").to_string();
  627. let path = parts.next().expect("path should exist").to_string();
  628. let mut headers = HashMap::new();
  629. let mut content_length = 0_usize;
  630. for line in lines {
  631. if line.is_empty() {
  632. continue;
  633. }
  634. let (name, value) = line.split_once(':').expect("header should have colon");
  635. let value = value.trim().to_string();
  636. if name.eq_ignore_ascii_case("content-length") {
  637. content_length = value.parse().expect("content length should parse");
  638. }
  639. headers.insert(name.to_ascii_lowercase(), value);
  640. }
  641. let mut body = remaining[4..].to_vec();
  642. while body.len() < content_length {
  643. let mut chunk = vec![0_u8; content_length - body.len()];
  644. let read = socket
  645. .read(&mut chunk)
  646. .await
  647. .expect("body read should succeed");
  648. if read == 0 {
  649. break;
  650. }
  651. body.extend_from_slice(&chunk[..read]);
  652. }
  653. state.lock().await.push(CapturedRequest {
  654. method,
  655. path,
  656. headers,
  657. body: String::from_utf8(body).expect("body should be utf8"),
  658. });
  659. socket
  660. .write_all(response.as_bytes())
  661. .await
  662. .expect("response write should succeed");
  663. }
  664. });
  665. TestServer {
  666. base_url: format!("http://{address}"),
  667. join_handle,
  668. }
  669. }
  670. fn find_header_end(bytes: &[u8]) -> Option<usize> {
  671. bytes.windows(4).position(|window| window == b"\r\n\r\n")
  672. }
  673. fn http_response(status: &str, content_type: &str, body: &str) -> String {
  674. http_response_with_headers(status, content_type, body, &[])
  675. }
  676. fn http_response_with_headers(
  677. status: &str,
  678. content_type: &str,
  679. body: &str,
  680. headers: &[(&str, &str)],
  681. ) -> String {
  682. let mut extra_headers = String::new();
  683. for (name, value) in headers {
  684. use std::fmt::Write as _;
  685. write!(&mut extra_headers, "{name}: {value}\r\n").expect("header write should succeed");
  686. }
  687. format!(
  688. "HTTP/1.1 {status}\r\ncontent-type: {content_type}\r\n{extra_headers}content-length: {}\r\nconnection: close\r\n\r\n{body}",
  689. body.len()
  690. )
  691. }
  692. fn sample_request(stream: bool) -> MessageRequest {
  693. MessageRequest {
  694. model: "claude-3-7-sonnet-latest".to_string(),
  695. max_tokens: 64,
  696. messages: vec![InputMessage {
  697. role: "user".to_string(),
  698. content: vec![
  699. InputContentBlock::Text {
  700. text: "Say hello".to_string(),
  701. },
  702. InputContentBlock::ToolResult {
  703. tool_use_id: "toolu_prev".to_string(),
  704. content: vec![api::ToolResultContentBlock::Json {
  705. value: json!({"forecast": "sunny"}),
  706. }],
  707. is_error: false,
  708. },
  709. ],
  710. }],
  711. system: Some("Use tools when needed".to_string()),
  712. tools: Some(vec![ToolDefinition {
  713. name: "get_weather".to_string(),
  714. description: Some("Fetches the weather".to_string()),
  715. input_schema: json!({
  716. "type": "object",
  717. "properties": {"city": {"type": "string"}},
  718. "required": ["city"]
  719. }),
  720. }]),
  721. tool_choice: Some(ToolChoice::Auto),
  722. stream,
  723. }
  724. }