api_claude 0.4.0

Claude API for accessing Anthropic's large language models (LLMs).
Documentation
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
//! Structured Logging Tests
//!
//! Tests for structured logging functionality including log output validation,
//! log level filtering, tracing integration, and zero-overhead verification.

#[ allow( unused_imports ) ]
use super::*;

// ============================================================================
// UNIT TESTS - STRUCTURED LOG OUTPUT
// ============================================================================

#[ test ]
fn test_structured_logger_creation()
{
  // Test creating a structured logger
  let logger = the_module::StructuredLogger::new();

  assert!( logger.is_enabled() );
}

#[ test ]
fn test_log_request_structure()
{
  // Test logging a request with structured fields
  let mut logger = the_module::StructuredLogger::new();

  let request = the_module::CreateMessageRequest
  {
    model : "claude-3-5-haiku-20241022".to_string(),
    max_tokens : 100,
    messages : vec![ the_module::Message::user( "Test" ) ],
    system : None,
    temperature : None,
    stream : None,
    #[ cfg( feature = "tools" ) ]
    tools : None,
    #[ cfg( feature = "tools" ) ]
    tool_choice : None,
  };

  logger.log_request( &request, "request_id_123" );

  // Verify log entry was recorded
  let logs = logger.get_logs();
  assert_eq!( logs.len(), 1 );
  assert!( logs[ 0 ].contains( "request_id_123" ) );
  assert!( logs[ 0 ].contains( "claude-3-5-haiku-20241022" ) );
}

#[ test ]
fn test_log_response_structure()
{
  // Test logging a response with structured fields
  let mut logger = the_module::StructuredLogger::new();

  // Create a sample response (simplified structure)
  let response_json = r#"{
    "id": "msg_test_123",
    "type": "message",
    "role": "assistant",
    "content": [{"type": "text", "text": "Test response"}],
    "model": "claude-3-5-haiku-20241022",
    "stop_reason": "end_turn",
    "usage": {"input_tokens": 10, "output_tokens": 5}
  }"#;

  let response : the_module::CreateMessageResponse = serde_json::from_str( response_json ).unwrap();

  logger.log_response( &response, "request_id_123" );

  let logs = logger.get_logs();
  assert_eq!( logs.len(), 1 );
  assert!( logs[ 0 ].contains( "msg_test_123" ) );
  assert!( logs[ 0 ].contains( "request_id_123" ) );
}

#[ test ]
fn test_log_error_structure()
{
  // Test logging an error with structured fields
  let mut logger = the_module::StructuredLogger::new();

  let error = the_module::AnthropicError::InvalidRequest( "Test error".to_string() );

  logger.log_error( &error, "request_id_123" );

  let logs = logger.get_logs();
  assert_eq!( logs.len(), 1 );
  assert!( logs[ 0 ].contains( "request_id_123" ) );
  assert!( logs[ 0 ].contains( "Test error" ) );
  assert!( logs[ 0 ].contains( "InvalidRequest" ) );
}

// ============================================================================
// UNIT TESTS - LOG LEVEL FILTERING
// ============================================================================

#[ test ]
fn test_log_level_filtering_debug()
{
  // Test that debug level logs are filtered correctly
  let mut logger = the_module::StructuredLogger::with_level( the_module::LogLevel::Debug );

  logger.debug( "Debug message" );
  logger.info( "Info message" );
  logger.warn( "Warn message" );
  logger.error( "Error message" );

  let logs = logger.get_logs();
  assert_eq!( logs.len(), 4 ); // All levels should be logged
}

#[ test ]
fn test_log_level_filtering_info()
{
  // Test that info level filters out debug
  let mut logger = the_module::StructuredLogger::with_level( the_module::LogLevel::Info );

  logger.debug( "Debug message" );
  logger.info( "Info message" );
  logger.warn( "Warn message" );
  logger.error( "Error message" );

  let logs = logger.get_logs();
  assert_eq!( logs.len(), 3 ); // Debug should be filtered out
  assert!( !logs.iter().any( |l| l.contains( "Debug message" ) ) );
}

#[ test ]
fn test_log_level_filtering_warn()
{
  // Test that warn level filters out debug and info
  let mut logger = the_module::StructuredLogger::with_level( the_module::LogLevel::Warn );

  logger.debug( "Debug message" );
  logger.info( "Info message" );
  logger.warn( "Warn message" );
  logger.error( "Error message" );

  let logs = logger.get_logs();
  assert_eq!( logs.len(), 2 ); // Only warn and error
}

#[ test ]
fn test_log_level_filtering_error()
{
  // Test that error level only logs errors
  let mut logger = the_module::StructuredLogger::with_level( the_module::LogLevel::Error );

  logger.debug( "Debug message" );
  logger.info( "Info message" );
  logger.warn( "Warn message" );
  logger.error( "Error message" );

  let logs = logger.get_logs();
  assert_eq!( logs.len(), 1 ); // Only error
  assert!( logs[ 0 ].contains( "Error message" ) );
}

// ============================================================================
// UNIT TESTS - LOG FORMATTING
// ============================================================================

#[ test ]
fn test_log_json_formatting()
{
  // Test that logs can be formatted as JSON
  let mut logger = the_module::StructuredLogger::new();

  logger.info( "Test message" );

  let json_logs = logger.to_json().expect( "JSON export must work" );

  assert!( json_logs.contains( "Test message" ) );
  assert!( json_logs.contains( "level" ) );
  assert!( json_logs.contains( "timestamp" ) );
}

#[ test ]
fn test_log_with_context_fields()
{
  // Test logging with additional context fields
  let mut logger = the_module::StructuredLogger::new();

  let mut context = std::collections::HashMap::new();
  context.insert( "user_id".to_string(), "user_123".to_string() );
  context.insert( "request_type".to_string(), "chat".to_string() );

  logger.info_with_context( "Message with context", context );

  let logs = logger.get_logs();
  assert!( logs[ 0 ].contains( "user_id" ) );
  assert!( logs[ 0 ].contains( "user_123" ) );
  assert!( logs[ 0 ].contains( "request_type" ) );
  assert!( logs[ 0 ].contains( "chat" ) );
}

// ============================================================================
// UNIT TESTS - ZERO OVERHEAD WHEN DISABLED
// ============================================================================

#[ test ]
fn test_disabled_logger_zero_overhead()
{
  // Test that disabled logger has minimal overhead
  let start = std::time::Instant::now();

  for _ in 0..10000
  {
    let _result = simple_operation();
  }

  let duration_without_logging = start.elapsed();

  // Now with disabled logger
  let logger = the_module::StructuredLogger::disabled();
  let start = std::time::Instant::now();

  for _ in 0..10000
  {
    let _result = simple_operation();
    logger.log_if_enabled( "operation", "completed" );
  }

  let duration_with_disabled_logging = start.elapsed();

  // Overhead should be minimal (less than 2x under concurrent test load)
  let overhead_ratio = duration_with_disabled_logging.as_micros() as f64 / duration_without_logging.as_micros() as f64;

  assert!( overhead_ratio < 2.0, "Disabled logging has too much overhead : {overhead_ratio}x" );

  // Verify no logs were collected (functional correctness)
  assert_eq!( logger.get_logs().len(), 0, "Disabled logger must not collect logs" );

  println!( "✅ Zero overhead test passed!" );
  println!( "   Without logging : {:?}", duration_without_logging );
  println!( "   With disabled logging : {:?}", duration_with_disabled_logging );
  println!( "   Overhead ratio : {:.2}x", overhead_ratio );
}

// Helper function
fn simple_operation() -> u64
{
  ( 1..100 ).sum()
}

// ============================================================================
// INTEGRATION TESTS - REAL API LOGGING
// ============================================================================

#[ tokio::test ]
#[ cfg( feature = "integration" ) ]
#[ ignore = "Requires workspace secrets file" ]
async fn integration_log_api_request_response()
{
  // Test logging real API request and response
  let client = the_module::Client::from_workspace()
    .expect( "INTEGRATION: Must have valid API key" );

  let mut logger = the_module::StructuredLogger::new();

  let request = the_module::CreateMessageRequest
  {
    model : "claude-3-5-haiku-20241022".to_string(),
    max_tokens : 20,
    messages : vec![ the_module::Message::user( "Hello!".to_string() ) ],
    system : None,
    temperature : Some( 0.0 ),
    stream : None,
    #[ cfg( feature = "tools" ) ]
    tools : None,
    #[ cfg( feature = "tools" ) ]
    tool_choice : None,
  };

  let request_id = "integration_test_001";
  logger.log_request( &request, request_id );

  let response = match client.create_message( request ).await
  {
    Ok( response ) => response,
    Err( the_module::AnthropicError::Api( ref api_err ) ) if api_err.message.contains( "credit balance is too low" ) =>
    {
      println!( "INTEGRATION TEST SKIPPED: Credit balance exhausted" );
      return;
    },
    Err( err ) => {
      logger.log_error( &err, request_id );
      panic!( "INTEGRATION: API call must work : {err}" );
    },
  };

  logger.log_response( &response, request_id );

  // Verify logs were captured
  let logs = logger.get_logs();
  assert!( logs.len() >= 2 ); // At least request and response
  assert!( logs.iter().any( |l| l.contains( request_id ) && l.contains( "request" ) ) );
  assert!( logs.iter().any( |l| l.contains( request_id ) && l.contains( "response" ) ) );

  println!( "✅ API request/response logging integration test passed!" );
  println!( "   Logged {} entries", logs.len() );
}

#[ tokio::test ]
#[ cfg( feature = "integration" ) ]
#[ ignore = "Requires workspace secrets file" ]
async fn integration_log_api_error()
{
  // Test logging API error
  let client = the_module::Client::from_workspace()
    .expect( "INTEGRATION: Must have valid API key" );

  let mut logger = the_module::StructuredLogger::new();

  // Create an invalid request (empty model)
  let invalid_request = the_module::CreateMessageRequest
  {
    model : String::new(), // Invalid
    max_tokens : 20,
    messages : vec![ the_module::Message::user( "Test".to_string() ) ],
    system : None,
    temperature : None,
    stream : None,
    #[ cfg( feature = "tools" ) ]
    tools : None,
    #[ cfg( feature = "tools" ) ]
    tool_choice : None,
  };

  let request_id = "integration_error_test";
  logger.log_request( &invalid_request, request_id );

  let result = client.create_message( invalid_request ).await;

  match result
  {
    Ok( _response ) => {
      // Unexpected success - still log it
      println!( "Request unexpectedly succeeded" );
    },
    Err( err ) => {
      // Expected error - log it
      logger.log_error( &err, request_id );
    },
  }

  // Verify error was logged
  let logs = logger.get_logs();
  assert!( logs.iter().any( |l| l.contains( request_id ) ) );

  println!( "✅ API error logging integration test passed!" );
}

#[ tokio::test ]
#[ cfg( feature = "integration" ) ]
#[ ignore = "Requires workspace secrets file" ]
async fn integration_structured_logging_with_context()
{
  // Test structured logging with request context
  let client = the_module::Client::from_workspace()
    .expect( "INTEGRATION: Must have valid API key" );

  let mut logger = the_module::StructuredLogger::new();

  let request = the_module::CreateMessageRequest
  {
    model : "claude-3-5-haiku-20241022".to_string(),
    max_tokens : 15,
    messages : vec![ the_module::Message::user( "Test".to_string() ) ],
    system : None,
    temperature : Some( 0.0 ),
    stream : None,
    #[ cfg( feature = "tools" ) ]
    tools : None,
    #[ cfg( feature = "tools" ) ]
    tool_choice : None,
  };

  let request_id = "context_test_001";

  // Log with context
  let mut context = std::collections::HashMap::new();
  context.insert( "session_id".to_string(), "session_abc".to_string() );
  context.insert( "user_type".to_string(), "premium".to_string() );

  logger.info_with_context( "Starting API request", context.clone() );
  logger.log_request( &request, request_id );

  let _response = match client.create_message( request ).await
  {
    Ok( response ) => {
      logger.log_response( &response, request_id );
      response
    },
    Err( the_module::AnthropicError::Api( ref api_err ) ) if api_err.message.contains( "credit balance is too low" ) =>
    {
      println!( "INTEGRATION TEST SKIPPED: Credit balance exhausted" );
      return;
    },
    Err( err ) => {
      logger.log_error( &err, request_id );
      panic!( "Request failed : {err}" );
    },
  };

  logger.info_with_context( "API request completed", context );

  // Verify context was logged
  let logs = logger.get_logs();
  assert!( logs.iter().any( |l| l.contains( "session_abc" ) ) );
  assert!( logs.iter().any( |l| l.contains( "premium" ) ) );

  println!( "✅ Structured logging with context integration test passed!" );
}