client_integration.rs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. use std::collections::HashMap;
  2. use std::sync::Arc;
  3. use std::time::Duration;
  4. use api::{
  5. AnthropicClient, ApiError, ContentBlockDelta, ContentBlockDeltaEvent, ContentBlockStartEvent,
  6. InputContentBlock, InputMessage, MessageDeltaEvent, MessageRequest, OutputContentBlock,
  7. StreamEvent, ToolChoice, ToolDefinition,
  8. };
  9. use serde_json::json;
  10. use telemetry::{ClientIdentity, MemoryTelemetrySink, SessionTracer, TelemetryEvent};
  11. use tokio::io::{AsyncReadExt, AsyncWriteExt};
  12. use tokio::net::TcpListener;
  13. use tokio::sync::Mutex;
  14. #[tokio::test]
  15. async fn send_message_posts_json_and_parses_response() {
  16. let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
  17. let body = concat!(
  18. "{",
  19. "\"id\":\"msg_test\",",
  20. "\"type\":\"message\",",
  21. "\"role\":\"assistant\",",
  22. "\"content\":[{\"type\":\"text\",\"text\":\"Hello from Claude\"}],",
  23. "\"model\":\"claude-3-7-sonnet-latest\",",
  24. "\"stop_reason\":\"end_turn\",",
  25. "\"stop_sequence\":null,",
  26. "\"usage\":{\"input_tokens\":12,\"output_tokens\":4},",
  27. "\"request_id\":\"req_body_123\"",
  28. "}"
  29. );
  30. let server = spawn_server(
  31. state.clone(),
  32. vec![http_response("200 OK", "application/json", body)],
  33. )
  34. .await;
  35. let client = AnthropicClient::new("test-key")
  36. .with_auth_token(Some("proxy-token".to_string()))
  37. .with_base_url(server.base_url());
  38. let response = client
  39. .send_message(&sample_request(false))
  40. .await
  41. .expect("request should succeed");
  42. assert_eq!(response.id, "msg_test");
  43. assert_eq!(response.total_tokens(), 16);
  44. assert_eq!(response.request_id.as_deref(), Some("req_body_123"));
  45. assert_eq!(
  46. response.content,
  47. vec![OutputContentBlock::Text {
  48. text: "Hello from Claude".to_string(),
  49. }]
  50. );
  51. let captured = state.lock().await;
  52. let request = captured.first().expect("server should capture request");
  53. assert_eq!(request.method, "POST");
  54. assert_eq!(request.path, "/v1/messages");
  55. assert_eq!(
  56. request.headers.get("x-api-key").map(String::as_str),
  57. Some("test-key")
  58. );
  59. assert_eq!(
  60. request.headers.get("authorization").map(String::as_str),
  61. Some("Bearer proxy-token")
  62. );
  63. assert_eq!(
  64. request.headers.get("anthropic-version").map(String::as_str),
  65. Some("2023-06-01")
  66. );
  67. assert_eq!(
  68. request.headers.get("user-agent").map(String::as_str),
  69. Some("claude-code/0.1.0")
  70. );
  71. assert_eq!(
  72. request.headers.get("anthropic-beta").map(String::as_str),
  73. Some("claude-code-20250219,prompt-caching-scope-2026-01-05")
  74. );
  75. let body: serde_json::Value =
  76. serde_json::from_str(&request.body).expect("request body should be json");
  77. assert_eq!(
  78. body.get("model").and_then(serde_json::Value::as_str),
  79. Some("claude-3-7-sonnet-latest")
  80. );
  81. assert!(body.get("stream").is_none());
  82. assert_eq!(body["tools"][0]["name"], json!("get_weather"));
  83. assert_eq!(body["tool_choice"]["type"], json!("auto"));
  84. assert_eq!(
  85. body["betas"],
  86. json!(["claude-code-20250219", "prompt-caching-scope-2026-01-05"])
  87. );
  88. }
  89. #[tokio::test]
  90. async fn send_message_applies_request_profile_and_records_telemetry() {
  91. let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
  92. let server = spawn_server(
  93. state.clone(),
  94. vec![http_response_with_headers(
  95. "200 OK",
  96. "application/json",
  97. concat!(
  98. "{",
  99. "\"id\":\"msg_profile\",",
  100. "\"type\":\"message\",",
  101. "\"role\":\"assistant\",",
  102. "\"content\":[{\"type\":\"text\",\"text\":\"ok\"}],",
  103. "\"model\":\"claude-3-7-sonnet-latest\",",
  104. "\"stop_reason\":\"end_turn\",",
  105. "\"stop_sequence\":null,",
  106. "\"usage\":{\"input_tokens\":1,\"output_tokens\":1}",
  107. "}"
  108. ),
  109. &[("request-id", "req_profile_123")],
  110. )],
  111. )
  112. .await;
  113. let sink = Arc::new(MemoryTelemetrySink::default());
  114. let client = AnthropicClient::new("test-key")
  115. .with_base_url(server.base_url())
  116. .with_client_identity(ClientIdentity::new("claude-code", "9.9.9").with_runtime("rust-cli"))
  117. .with_beta("tools-2026-04-01")
  118. .with_extra_body_param("metadata", json!({"source": "clawd-code"}))
  119. .with_session_tracer(SessionTracer::new("session-telemetry", sink.clone()));
  120. let response = client
  121. .send_message(&sample_request(false))
  122. .await
  123. .expect("request should succeed");
  124. assert_eq!(response.request_id.as_deref(), Some("req_profile_123"));
  125. let captured = state.lock().await;
  126. let request = captured.first().expect("server should capture request");
  127. assert_eq!(
  128. request.headers.get("anthropic-beta").map(String::as_str),
  129. Some("claude-code-20250219,prompt-caching-scope-2026-01-05,tools-2026-04-01")
  130. );
  131. assert_eq!(
  132. request.headers.get("user-agent").map(String::as_str),
  133. Some("claude-code/9.9.9")
  134. );
  135. let body: serde_json::Value =
  136. serde_json::from_str(&request.body).expect("request body should be json");
  137. assert_eq!(body["metadata"]["source"], json!("clawd-code"));
  138. assert_eq!(
  139. body["betas"],
  140. json!([
  141. "claude-code-20250219",
  142. "prompt-caching-scope-2026-01-05",
  143. "tools-2026-04-01"
  144. ])
  145. );
  146. let events = sink.events();
  147. assert_eq!(events.len(), 4);
  148. assert!(matches!(
  149. &events[0],
  150. TelemetryEvent::HttpRequestStarted {
  151. session_id,
  152. attempt: 1,
  153. method,
  154. path,
  155. ..
  156. } if session_id == "session-telemetry" && method == "POST" && path == "/v1/messages"
  157. ));
  158. assert!(matches!(
  159. &events[1],
  160. TelemetryEvent::SessionTrace(trace) if trace.name == "http_request_started"
  161. ));
  162. assert!(matches!(
  163. &events[2],
  164. TelemetryEvent::HttpRequestSucceeded {
  165. request_id,
  166. status: 200,
  167. ..
  168. } if request_id.as_deref() == Some("req_profile_123")
  169. ));
  170. assert!(matches!(
  171. &events[3],
  172. TelemetryEvent::SessionTrace(trace) if trace.name == "http_request_succeeded"
  173. ));
  174. }
  175. #[tokio::test]
  176. async fn stream_message_parses_sse_events_with_tool_use() {
  177. let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
  178. let sse = concat!(
  179. "event: message_start\n",
  180. "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,\"output_tokens\":0}}}\n\n",
  181. "event: content_block_start\n",
  182. "data: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"toolu_123\",\"name\":\"get_weather\",\"input\":{}}}\n\n",
  183. "event: content_block_delta\n",
  184. "data: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"city\\\":\\\"Paris\\\"}\"}}\n\n",
  185. "event: content_block_stop\n",
  186. "data: {\"type\":\"content_block_stop\",\"index\":0}\n\n",
  187. "event: message_delta\n",
  188. "data: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\",\"stop_sequence\":null},\"usage\":{\"input_tokens\":8,\"output_tokens\":1}}\n\n",
  189. "event: message_stop\n",
  190. "data: {\"type\":\"message_stop\"}\n\n",
  191. "data: [DONE]\n\n"
  192. );
  193. let server = spawn_server(
  194. state.clone(),
  195. vec![http_response_with_headers(
  196. "200 OK",
  197. "text/event-stream",
  198. sse,
  199. &[("request-id", "req_stream_456")],
  200. )],
  201. )
  202. .await;
  203. let client = AnthropicClient::new("test-key")
  204. .with_auth_token(Some("proxy-token".to_string()))
  205. .with_base_url(server.base_url());
  206. let mut stream = client
  207. .stream_message(&sample_request(false))
  208. .await
  209. .expect("stream should start");
  210. assert_eq!(stream.request_id(), Some("req_stream_456"));
  211. let mut events = Vec::new();
  212. while let Some(event) = stream
  213. .next_event()
  214. .await
  215. .expect("stream event should parse")
  216. {
  217. events.push(event);
  218. }
  219. assert_eq!(events.len(), 6);
  220. assert!(matches!(events[0], StreamEvent::MessageStart(_)));
  221. assert!(matches!(
  222. events[1],
  223. StreamEvent::ContentBlockStart(ContentBlockStartEvent {
  224. content_block: OutputContentBlock::ToolUse { .. },
  225. ..
  226. })
  227. ));
  228. assert!(matches!(
  229. events[2],
  230. StreamEvent::ContentBlockDelta(ContentBlockDeltaEvent {
  231. delta: ContentBlockDelta::InputJsonDelta { .. },
  232. ..
  233. })
  234. ));
  235. assert!(matches!(events[3], StreamEvent::ContentBlockStop(_)));
  236. assert!(matches!(
  237. events[4],
  238. StreamEvent::MessageDelta(MessageDeltaEvent { .. })
  239. ));
  240. assert!(matches!(events[5], StreamEvent::MessageStop(_)));
  241. match &events[1] {
  242. StreamEvent::ContentBlockStart(ContentBlockStartEvent {
  243. content_block: OutputContentBlock::ToolUse { name, input, .. },
  244. ..
  245. }) => {
  246. assert_eq!(name, "get_weather");
  247. assert_eq!(input, &json!({}));
  248. }
  249. other => panic!("expected tool_use block, got {other:?}"),
  250. }
  251. let captured = state.lock().await;
  252. let request = captured.first().expect("server should capture request");
  253. assert!(request.body.contains("\"stream\":true"));
  254. }
  255. #[tokio::test]
  256. async fn retries_retryable_failures_before_succeeding() {
  257. let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
  258. let server = spawn_server(
  259. state.clone(),
  260. vec![
  261. http_response(
  262. "429 Too Many Requests",
  263. "application/json",
  264. "{\"type\":\"error\",\"error\":{\"type\":\"rate_limit_error\",\"message\":\"slow down\"}}",
  265. ),
  266. http_response(
  267. "200 OK",
  268. "application/json",
  269. "{\"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}}",
  270. ),
  271. ],
  272. )
  273. .await;
  274. let client = AnthropicClient::new("test-key")
  275. .with_base_url(server.base_url())
  276. .with_retry_policy(2, Duration::from_millis(1), Duration::from_millis(2));
  277. let response = client
  278. .send_message(&sample_request(false))
  279. .await
  280. .expect("retry should eventually succeed");
  281. assert_eq!(response.total_tokens(), 5);
  282. assert_eq!(state.lock().await.len(), 2);
  283. }
  284. #[tokio::test]
  285. async fn surfaces_retry_exhaustion_for_persistent_retryable_errors() {
  286. let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
  287. let server = spawn_server(
  288. state.clone(),
  289. vec![
  290. http_response(
  291. "503 Service Unavailable",
  292. "application/json",
  293. "{\"type\":\"error\",\"error\":{\"type\":\"overloaded_error\",\"message\":\"busy\"}}",
  294. ),
  295. http_response(
  296. "503 Service Unavailable",
  297. "application/json",
  298. "{\"type\":\"error\",\"error\":{\"type\":\"overloaded_error\",\"message\":\"still busy\"}}",
  299. ),
  300. ],
  301. )
  302. .await;
  303. let client = AnthropicClient::new("test-key")
  304. .with_base_url(server.base_url())
  305. .with_retry_policy(1, Duration::from_millis(1), Duration::from_millis(2));
  306. let error = client
  307. .send_message(&sample_request(false))
  308. .await
  309. .expect_err("persistent 503 should fail");
  310. match error {
  311. ApiError::RetriesExhausted {
  312. attempts,
  313. last_error,
  314. } => {
  315. assert_eq!(attempts, 2);
  316. assert!(matches!(
  317. *last_error,
  318. ApiError::Api {
  319. status: reqwest::StatusCode::SERVICE_UNAVAILABLE,
  320. retryable: true,
  321. ..
  322. }
  323. ));
  324. }
  325. other => panic!("expected retries exhausted, got {other:?}"),
  326. }
  327. }
  328. #[tokio::test]
  329. #[ignore = "requires ANTHROPIC_API_KEY and network access"]
  330. async fn live_stream_smoke_test() {
  331. let client = AnthropicClient::from_env().expect("ANTHROPIC_API_KEY must be set");
  332. let mut stream = client
  333. .stream_message(&MessageRequest {
  334. model: std::env::var("ANTHROPIC_MODEL")
  335. .unwrap_or_else(|_| "claude-3-7-sonnet-latest".to_string()),
  336. max_tokens: 32,
  337. messages: vec![InputMessage::user_text(
  338. "Reply with exactly: hello from rust",
  339. )],
  340. system: None,
  341. tools: None,
  342. tool_choice: None,
  343. stream: false,
  344. })
  345. .await
  346. .expect("live stream should start");
  347. while let Some(_event) = stream
  348. .next_event()
  349. .await
  350. .expect("live stream should yield events")
  351. {}
  352. }
  353. #[derive(Debug, Clone, PartialEq, Eq)]
  354. struct CapturedRequest {
  355. method: String,
  356. path: String,
  357. headers: HashMap<String, String>,
  358. body: String,
  359. }
  360. struct TestServer {
  361. base_url: String,
  362. join_handle: tokio::task::JoinHandle<()>,
  363. }
  364. impl TestServer {
  365. fn base_url(&self) -> String {
  366. self.base_url.clone()
  367. }
  368. }
  369. impl Drop for TestServer {
  370. fn drop(&mut self) {
  371. self.join_handle.abort();
  372. }
  373. }
  374. async fn spawn_server(
  375. state: Arc<Mutex<Vec<CapturedRequest>>>,
  376. responses: Vec<String>,
  377. ) -> TestServer {
  378. let listener = TcpListener::bind("127.0.0.1:0")
  379. .await
  380. .expect("listener should bind");
  381. let address = listener
  382. .local_addr()
  383. .expect("listener should have local addr");
  384. let join_handle = tokio::spawn(async move {
  385. for response in responses {
  386. let (mut socket, _) = listener.accept().await.expect("server should accept");
  387. let mut buffer = Vec::new();
  388. let mut header_end = None;
  389. loop {
  390. let mut chunk = [0_u8; 1024];
  391. let read = socket
  392. .read(&mut chunk)
  393. .await
  394. .expect("request read should succeed");
  395. if read == 0 {
  396. break;
  397. }
  398. buffer.extend_from_slice(&chunk[..read]);
  399. if let Some(position) = find_header_end(&buffer) {
  400. header_end = Some(position);
  401. break;
  402. }
  403. }
  404. let header_end = header_end.expect("request should include headers");
  405. let (header_bytes, remaining) = buffer.split_at(header_end);
  406. let header_text =
  407. String::from_utf8(header_bytes.to_vec()).expect("headers should be utf8");
  408. let mut lines = header_text.split("\r\n");
  409. let request_line = lines.next().expect("request line should exist");
  410. let mut parts = request_line.split_whitespace();
  411. let method = parts.next().expect("method should exist").to_string();
  412. let path = parts.next().expect("path should exist").to_string();
  413. let mut headers = HashMap::new();
  414. let mut content_length = 0_usize;
  415. for line in lines {
  416. if line.is_empty() {
  417. continue;
  418. }
  419. let (name, value) = line.split_once(':').expect("header should have colon");
  420. let value = value.trim().to_string();
  421. if name.eq_ignore_ascii_case("content-length") {
  422. content_length = value.parse().expect("content length should parse");
  423. }
  424. headers.insert(name.to_ascii_lowercase(), value);
  425. }
  426. let mut body = remaining[4..].to_vec();
  427. while body.len() < content_length {
  428. let mut chunk = vec![0_u8; content_length - body.len()];
  429. let read = socket
  430. .read(&mut chunk)
  431. .await
  432. .expect("body read should succeed");
  433. if read == 0 {
  434. break;
  435. }
  436. body.extend_from_slice(&chunk[..read]);
  437. }
  438. state.lock().await.push(CapturedRequest {
  439. method,
  440. path,
  441. headers,
  442. body: String::from_utf8(body).expect("body should be utf8"),
  443. });
  444. socket
  445. .write_all(response.as_bytes())
  446. .await
  447. .expect("response write should succeed");
  448. }
  449. });
  450. TestServer {
  451. base_url: format!("http://{address}"),
  452. join_handle,
  453. }
  454. }
  455. fn find_header_end(bytes: &[u8]) -> Option<usize> {
  456. bytes.windows(4).position(|window| window == b"\r\n\r\n")
  457. }
  458. fn http_response(status: &str, content_type: &str, body: &str) -> String {
  459. http_response_with_headers(status, content_type, body, &[])
  460. }
  461. fn http_response_with_headers(
  462. status: &str,
  463. content_type: &str,
  464. body: &str,
  465. headers: &[(&str, &str)],
  466. ) -> String {
  467. let mut extra_headers = String::new();
  468. for (name, value) in headers {
  469. use std::fmt::Write as _;
  470. write!(&mut extra_headers, "{name}: {value}\r\n").expect("header write should succeed");
  471. }
  472. format!(
  473. "HTTP/1.1 {status}\r\ncontent-type: {content_type}\r\n{extra_headers}content-length: {}\r\nconnection: close\r\n\r\n{body}",
  474. body.len()
  475. )
  476. }
  477. fn sample_request(stream: bool) -> MessageRequest {
  478. MessageRequest {
  479. model: "claude-3-7-sonnet-latest".to_string(),
  480. max_tokens: 64,
  481. messages: vec![InputMessage {
  482. role: "user".to_string(),
  483. content: vec![
  484. InputContentBlock::Text {
  485. text: "Say hello".to_string(),
  486. },
  487. InputContentBlock::ToolResult {
  488. tool_use_id: "toolu_prev".to_string(),
  489. content: vec![api::ToolResultContentBlock::Json {
  490. value: json!({"forecast": "sunny"}),
  491. }],
  492. is_error: false,
  493. },
  494. ],
  495. }],
  496. system: Some("Use tools when needed".to_string()),
  497. tools: Some(vec![ToolDefinition {
  498. name: "get_weather".to_string(),
  499. description: Some("Fetches the weather".to_string()),
  500. input_schema: json!({
  501. "type": "object",
  502. "properties": {"city": {"type": "string"}},
  503. "required": ["city"]
  504. }),
  505. }]),
  506. tool_choice: Some(ToolChoice::Auto),
  507. stream,
  508. }
  509. }