api_claude 0.6.1

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
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
//! Enhanced Function Calling Tests
//!
//! Tests for advanced tool choice modes : AUTO, ANY, NONE

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

// ============================================================================
// UNIT TESTS - TOOL CHOICE MODES
// ============================================================================

#[ test ]
fn test_tool_choice_auto()
{
  // Test AUTO mode - model decides when to use tools
  let choice = the_module::ToolChoice::Auto;

  assert!( choice.is_auto() );
  assert!( !choice.is_none() );
  assert!( !choice.is_specific() );
}

#[ test ]
fn test_tool_choice_none()
{
  // Test NONE mode - disable all tool calling
  let choice = the_module::ToolChoice::None;

  assert!( choice.is_none() );
  assert!( !choice.is_auto() );
  assert!( !choice.is_specific() );
}

#[ test ]
fn test_tool_choice_any()
{
  // Test ANY mode - force use of any available tool
  let choice = the_module::ToolChoice::Any;

  assert!( choice.is_any() );
  assert!( !choice.is_auto() );
  assert!( !choice.is_none() );
  assert!( !choice.is_specific() );
}

#[ test ]
fn test_tool_choice_specific()
{
  // Test specific tool choice
  let choice = the_module::ToolChoice::specific( "calculator" );

  assert!( choice.is_specific() );
  assert!( !choice.is_auto() );
  assert!( !choice.is_none() );
  assert!( !choice.is_any() );
  assert_eq!( choice.tool_name(), Some( "calculator" ) );
}

#[ test ]
fn test_tool_choice_serialization()
{
  // Test that tool choices serialize to correct API format
  let auto = the_module::ToolChoice::Auto;
  let none = the_module::ToolChoice::None;
  let any = the_module::ToolChoice::Any;
  let specific = the_module::ToolChoice::specific( "get_weather" );

  let auto_json = serde_json::to_value( &auto ).unwrap();
  let none_json = serde_json::to_value( &none ).unwrap();
  let any_json = serde_json::to_value( &any ).unwrap();
  let specific_json = serde_json::to_value( &specific ).unwrap();

  assert_eq!( auto_json[ "type" ], "auto" );
  assert_eq!( none_json[ "type" ], "none" );
  assert_eq!( any_json[ "type" ], "any" );
  assert_eq!( specific_json[ "type" ], "tool" );
  assert_eq!( specific_json[ "name" ], "get_weather" );
}

// ============================================================================
// INTEGRATION TESTS - REAL API TOOL CHOICE MODES
// ============================================================================

#[ cfg( feature = "integration" ) ]
#[ tokio::test ]
async fn integration_tool_choice_auto_mode()
{
  // Test AUTO mode - model decides whether to use tools
  let client = the_module::Client::from_workspace()
    .expect( "INTEGRATION: Must have valid API key" );

  let tool = the_module::ToolDefinition
  {
    name : "calculator".to_string(),
    description : "Perform basic arithmetic operations".to_string(),
    input_schema : serde_json::json!({
      "type": "object",
      "properties": {
        "operation": { "type": "string", "enum": ["add", "subtract", "multiply", "divide"] },
        "a": { "type": "number" },
        "b": { "type": "number" }
      },
      "required": ["operation", "a", "b"]
    }),
  };

  let request = the_module::CreateMessageRequest
  {
    model : "claude-haiku-4-5-20251001".to_string(),
    max_tokens : 100,
    messages : vec![ the_module::Message::user( "What is 25 + 17?".to_string() ) ],
    system : None,
    temperature : Some( 0.0 ),
    stream : None,
    tools : Some( vec![ tool ] ),
    tool_choice : Some( the_module::ToolChoice::Auto ),
  };

  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" ) =>
    {
      panic!( "INTEGRATION: credit balance exhausted - real API call succeeded but account has no credits. Test must fail per Loud Failure Mandate: {}", api_err.message )
    },
    Err( err ) => panic!( "INTEGRATION: AUTO mode must work : {err}" ),
  };

  // With AUTO mode, model should decide to use calculator for math
  assert!( !response.id.is_empty() );

  println!( "✅ Tool choice AUTO mode test passed!" );
}

#[ cfg( feature = "integration" ) ]
#[ tokio::test ]
async fn integration_tool_choice_any_mode()
{
  // Test ANY mode - force use of any available tool
  let client = the_module::Client::from_workspace()
    .expect( "INTEGRATION: Must have valid API key" );

  let tool = the_module::ToolDefinition
  {
    name : "get_info".to_string(),
    description : "Get information about a topic".to_string(),
    input_schema : serde_json::json!({
      "type": "object",
      "properties": {
        "topic": { "type": "string" }
      },
      "required": ["topic"]
    }),
  };

  let request = the_module::CreateMessageRequest
  {
    model : "claude-haiku-4-5-20251001".to_string(),
    max_tokens : 100,
    messages : vec![ the_module::Message::user( "Tell me about Rust".to_string() ) ],
    system : None,
    temperature : Some( 0.0 ),
    stream : None,
    tools : Some( vec![ tool ] ),
    tool_choice : Some( the_module::ToolChoice::Any ),
  };

  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" ) =>
    {
      panic!( "INTEGRATION: credit balance exhausted - real API call succeeded but account has no credits. Test must fail per Loud Failure Mandate: {}", api_err.message )
    },
    Err( err ) => panic!( "INTEGRATION: ANY mode must work : {err}" ),
  };

  // With ANY mode, model must use at least one tool
  assert!( !response.id.is_empty() );

  // Check that model used a tool (check type field in response content)
  let has_tool_use = response.content.iter().any( |c| c.r#type == "tool_use" );
  assert!( has_tool_use, "INTEGRATION: ANY mode must force tool use" );

  println!( "✅ Tool choice ANY mode test passed!" );
}

#[ cfg( feature = "integration" ) ]
#[ tokio::test ]
async fn integration_tool_choice_none_mode()
{
  // Test NONE mode - disable all tool calling
  let client = the_module::Client::from_workspace()
    .expect( "INTEGRATION: Must have valid API key" );

  let tool = the_module::ToolDefinition
  {
    name : "calculator".to_string(),
    description : "Perform calculations".to_string(),
    input_schema : serde_json::json!({
      "type": "object",
      "properties": {
        "expression": { "type": "string" }
      },
      "required": ["expression"]
    }),
  };

  let request = the_module::CreateMessageRequest
  {
    model : "claude-haiku-4-5-20251001".to_string(),
    max_tokens : 100,
    messages : vec![ the_module::Message::user( "Calculate 10 + 5".to_string() ) ],
    system : None,
    temperature : Some( 0.0 ),
    stream : None,
    tools : Some( vec![ tool ] ),
    tool_choice : Some( the_module::ToolChoice::None ),
  };

  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" ) =>
    {
      panic!( "INTEGRATION: credit balance exhausted - real API call succeeded but account has no credits. Test must fail per Loud Failure Mandate: {}", api_err.message )
    },
    Err( err ) => panic!( "INTEGRATION: NONE mode must work : {err}" ),
  };

  // With NONE mode, model should not use tools
  assert!( !response.id.is_empty() );

  // Check that no tools were used (check type field in response content)
  let has_tool_use = response.content.iter().any( |c| c.r#type == "tool_use" );
  assert!( !has_tool_use, "INTEGRATION: NONE mode must prevent tool use" );

  println!( "✅ Tool choice NONE mode test passed!" );
}

#[ cfg( feature = "integration" ) ]
#[ tokio::test ]
async fn integration_tool_choice_mode_transitions()
{
  // Test switching between different tool choice modes
  let client = the_module::Client::from_workspace()
    .expect( "INTEGRATION: Must have valid API key" );

  let tool = the_module::ToolDefinition::simple( "test_tool", "A test tool" );

  // First request with AUTO
  let request1 = the_module::CreateMessageRequest
  {
    model : "claude-haiku-4-5-20251001".to_string(),
    max_tokens : 50,
    messages : vec![ the_module::Message::user( "Hello".to_string() ) ],
    system : None,
    temperature : Some( 0.0 ),
    stream : None,
    tools : Some( vec![ tool.clone() ] ),
    tool_choice : Some( the_module::ToolChoice::Auto ),
  };

  let response1 = match client.create_message( request1 ).await
  {
    Ok( r ) => r,
    Err( the_module::AnthropicError::Api( ref api_err ) ) if api_err.message.contains( "credit balance is too low" ) =>
    {
      panic!( "INTEGRATION: credit balance exhausted - real API call succeeded but account has no credits. Test must fail per Loud Failure Mandate: {}", api_err.message )
    },
    Err( err ) => panic!( "INTEGRATION: First request must work : {err}" ),
  };

  assert!( !response1.id.is_empty() );

  // Second request with NONE
  let request2 = the_module::CreateMessageRequest
  {
    model : "claude-haiku-4-5-20251001".to_string(),
    max_tokens : 50,
    messages : vec![ the_module::Message::user( "Hi again".to_string() ) ],
    system : None,
    temperature : Some( 0.0 ),
    stream : None,
    tools : Some( vec![ tool ] ),
    tool_choice : Some( the_module::ToolChoice::None ),
  };

  let response2 = match client.create_message( request2 ).await
  {
    Ok( r ) => r,
    Err( the_module::AnthropicError::Api( ref api_err ) ) if api_err.message.contains( "credit balance is too low" ) =>
    {
      panic!( "INTEGRATION: credit balance exhausted on second request - real API call succeeded but account has no credits. Test must fail per Loud Failure Mandate: {}", api_err.message )
    },
    Err( err ) => panic!( "INTEGRATION: Second request must work : {err}" ),
  };

  assert!( !response2.id.is_empty() );
  assert_ne!( response1.id, response2.id, "Responses should have different IDs" );

  println!( "✅ Tool choice mode transitions test passed!" );
}

// ============================================================================
// UNIT TESTS - TOOL EXECUTOR AND REGISTRY
// (Migrated from src/enhanced_function_calling.rs #[cfg(test)] block)
// ============================================================================

struct RegistryTestTool
{
  name : String,
  description : String,
}

impl the_module::ToolExecutor for RegistryTestTool
{
  fn name( &self ) -> &str
  {
    &self.name
  }

  fn description( &self ) -> &str
  {
    &self.description
  }

  fn parameter_schema( &self ) -> serde_json::Value
  {
    serde_json::json!(
    {
      "type" : "object",
      "properties" :
      {
        "input" :
        {
          "type" : "string",
          "description" : "Test input"
        }
      },
      "required" : [ "input" ]
    })
  }

  fn execute( &self, params : serde_json::Value ) -> the_module::ToolResult
  {
    let input = params[ "input" ].as_str()
      .ok_or( "Missing input parameter" )?;
    Ok( format!( "Executed {} with: {}", self.name, input ) )
  }
}

#[ test ]
fn test_tool_registry_new()
{
  let registry = the_module::ToolRegistry::new();
  assert!( registry.is_empty() );
  assert_eq!( registry.len(), 0 );
}

#[ test ]
fn test_tool_registry_register()
{
  let mut registry = the_module::ToolRegistry::new();

  let tool = Box::new( RegistryTestTool
  {
    name : "test_tool".to_string(),
    description : "Test tool".to_string(),
  } );

  registry.register( tool );

  assert!( !registry.is_empty() );
  assert_eq!( registry.len(), 1 );
  assert!( registry.has_tool( "test_tool" ) );
}

#[ test ]
fn test_tool_registry_execute()
{
  let mut registry = the_module::ToolRegistry::new();

  let tool = Box::new( RegistryTestTool
  {
    name : "test_tool".to_string(),
    description : "Test tool".to_string(),
  } );

  registry.register( tool );

  let params = serde_json::json!( { "input" : "test value" } );
  let result = registry.execute( "test_tool", params );

  assert!( result.is_ok() );
  let output = result.unwrap();
  assert!( output.contains( "test_tool" ) );
  assert!( output.contains( "test value" ) );
}

#[ test ]
fn test_tool_registry_execute_not_found()
{
  let registry = the_module::ToolRegistry::new();
  let params = serde_json::json!( { "input" : "test" } );
  let result = registry.execute( "nonexistent", params );

  assert!( result.is_err() );
  let err = result.unwrap_err();
  assert!( err.contains( "not found" ) );
}

#[ test ]
fn test_tool_registry_definitions()
{
  let mut registry = the_module::ToolRegistry::new();

  let tool = Box::new( RegistryTestTool
  {
    name : "test_tool".to_string(),
    description : "Test tool description".to_string(),
  } );

  registry.register( tool );

  let definitions = registry.definitions();
  assert_eq!( definitions.len(), 1 );
  assert_eq!( definitions[ 0 ].name, "test_tool" );
  assert_eq!( definitions[ 0 ].description, "Test tool description" );
}

#[ test ]
fn test_tool_registry_tool_names()
{
  let mut registry = the_module::ToolRegistry::new();

  registry.register( Box::new( RegistryTestTool
  {
    name : "tool1".to_string(),
    description : "Tool 1".to_string(),
  } ) );

  registry.register( Box::new( RegistryTestTool
  {
    name : "tool2".to_string(),
    description : "Tool 2".to_string(),
  } ) );

  let names = registry.tool_names();
  assert_eq!( names.len(), 2 );
  assert!( names.contains( &"tool1".to_string() ) );
  assert!( names.contains( &"tool2".to_string() ) );
}

#[ test ]
fn test_create_tool_definition()
{
  let schema = serde_json::json!( { "type" : "object" } );
  let tool_def = the_module::create_tool_definition( "test", "Test tool", schema.clone() );

  assert_eq!( tool_def.name, "test" );
  assert_eq!( tool_def.description, "Test tool" );
  assert_eq!( tool_def.input_schema, schema );
}

#[ test ]
fn test_create_parameter_schema()
{
  let properties = serde_json::json!(
  {
    "location" :
    {
      "type" : "string",
      "description" : "City name"
    }
  });

  let schema = the_module::create_parameter_schema( &properties, &[ "location".to_string() ] );

  assert_eq!( schema[ "type" ], "object" );
  assert!( schema[ "properties" ][ "location" ].is_object() );
  assert_eq!( schema[ "required" ][ 0 ], "location" );
}

#[ test ]
fn test_tool_executor_default_schema()
{
  use the_module::ToolExecutor;
  struct MinimalTool;

  impl the_module::ToolExecutor for MinimalTool
  {
    fn name( &self ) -> &'static str { "minimal" }
    fn description( &self ) -> &'static str { "Minimal tool" }
    fn execute( &self, _params : serde_json::Value ) -> the_module::ToolResult
    {
      Ok( "done".to_string() )
    }
  }

  let tool = MinimalTool;
  let schema = tool.parameter_schema();

  assert_eq!( schema[ "type" ], "object" );
  assert!( schema[ "properties" ].is_object() );
}