ares-server 0.7.5

A.R.E.S - Agentic Retrieval Enhanced Server: A production-grade agentic chatbot server with multi-provider LLM support, tool calling, RAG, and MCP integration
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
use ares::llm::*;
use ares::types::{ToolCall, ToolDefinition};
use futures::StreamExt;

// Import common test utilities
mod common;
use common::mocks::MockLLMClient;

// ============= Basic LLM Client Tests =============

#[tokio::test]
async fn test_mock_llm_client_generate() {
    let client = MockLLMClient::new("Hello, world!");
    let result = client.generate("test prompt").await;
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), "Hello, world!");
}

#[tokio::test]
async fn test_mock_llm_client_generate_with_system() {
    let client = MockLLMClient::new("System response");
    let result = client
        .generate_with_system("You are a helpful assistant", "Hello")
        .await;
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), "System response");
}

#[tokio::test]
async fn test_mock_llm_client_generate_with_history() {
    let client = MockLLMClient::new("History response");
    let messages = vec![
        ("user".to_string(), "Hello".to_string()),
        ("assistant".to_string(), "Hi there!".to_string()),
        ("user".to_string(), "How are you?".to_string()),
    ];
    let result = client.generate_with_history(&messages).await;
    assert!(result.is_ok());
    assert_eq!(result.unwrap().content, "History response");
}

#[tokio::test]
async fn test_mock_llm_client_generate_with_tools_no_calls() {
    let client = MockLLMClient::new("Tool response");
    let tools: Vec<ToolDefinition> = vec![ToolDefinition {
        name: "calculator".to_string(),
        description: "Performs arithmetic".to_string(),
        parameters: serde_json::json!({
            "type": "object",
            "properties": {
                "operation": {"type": "string"},
                "a": {"type": "number"},
                "b": {"type": "number"}
            },
            "required": ["operation", "a", "b"]
        }),
    }];

    let result = client.generate_with_tools("Calculate 2+2", &tools).await;
    assert!(result.is_ok());
    let response = result.unwrap();
    assert_eq!(response.content, "Tool response");
    assert_eq!(response.finish_reason, "stop");
    assert!(response.tool_calls.is_empty());
}

#[tokio::test]
async fn test_mock_llm_client_generate_with_tools_with_calls() {
    let tool_calls = vec![ToolCall {
        id: "call-1".to_string(),
        name: "calculator".to_string(),
        arguments: serde_json::json!({"operation": "add", "a": 2, "b": 2}),
    }];

    let client = MockLLMClient::with_tool_calls("I need to calculate", tool_calls);
    let tools: Vec<ToolDefinition> = vec![ToolDefinition {
        name: "calculator".to_string(),
        description: "Performs arithmetic".to_string(),
        parameters: serde_json::json!({}),
    }];

    let result = client.generate_with_tools("Calculate 2+2", &tools).await;
    assert!(result.is_ok());
    let response = result.unwrap();
    assert_eq!(response.finish_reason, "tool_calls");
    assert_eq!(response.tool_calls.len(), 1);
    assert_eq!(response.tool_calls[0].name, "calculator");
}

#[tokio::test]
async fn test_mock_llm_client_model_name() {
    let client = MockLLMClient::new("test");
    assert_eq!(client.model_name(), "mock-model");
}

#[tokio::test]
async fn test_mock_llm_client_streaming() {
    let client = MockLLMClient::new("Hello streaming world!");
    let result = client.stream("test").await;
    assert!(result.is_ok());

    let mut stream = result.unwrap();
    let mut collected = String::new();

    while let Some(chunk_result) = stream.next().await {
        match chunk_result {
            Ok(chunk) => collected.push_str(&chunk),
            Err(_) => break,
        }
    }

    assert_eq!(collected, "Hello streaming world!");
}

#[tokio::test]
async fn test_mock_llm_client_streaming_with_system() {
    let client = MockLLMClient::new("System streaming response");
    let result = client.stream_with_system("You are helpful", "test").await;
    assert!(result.is_ok());

    let mut stream = result.unwrap();
    let mut collected = String::new();

    while let Some(chunk_result) = stream.next().await {
        match chunk_result {
            Ok(chunk) => collected.push_str(&chunk),
            Err(_) => break,
        }
    }

    assert_eq!(collected, "System streaming response");
}

#[tokio::test]
async fn test_mock_llm_client_streaming_with_history() {
    let client = MockLLMClient::new("History streaming response");
    let history = vec![
        ("user".to_string(), "Hello".to_string()),
        ("assistant".to_string(), "Hi!".to_string()),
    ];
    let result = client.stream_with_history(&history).await;
    assert!(result.is_ok());

    let mut stream = result.unwrap();
    let mut collected = String::new();

    while let Some(chunk_result) = stream.next().await {
        match chunk_result {
            Ok(chunk) => collected.push_str(&chunk),
            Err(_) => break,
        }
    }

    assert_eq!(collected, "History streaming response");
}

// ============= LLM Response Tests =============

#[test]
fn test_llm_response_struct() {
    let response = LLMResponse {
        content: "Test content".to_string(),
        tool_calls: vec![],
        finish_reason: "stop".to_string(),
        usage: None,
    };

    assert_eq!(response.content, "Test content");
    assert!(response.tool_calls.is_empty());
    assert_eq!(response.finish_reason, "stop");
}

#[test]
fn test_llm_response_with_tool_calls() {
    let tool_calls = vec![
        ToolCall {
            id: "1".to_string(),
            name: "func1".to_string(),
            arguments: serde_json::json!({"arg": "value"}),
        },
        ToolCall {
            id: "2".to_string(),
            name: "func2".to_string(),
            arguments: serde_json::json!({"num": 42}),
        },
    ];

    let response = LLMResponse {
        content: "".to_string(),
        tool_calls: tool_calls.clone(),
        finish_reason: "tool_calls".to_string(),
        usage: None,
    };

    assert_eq!(response.tool_calls.len(), 2);
    assert_eq!(response.tool_calls[0].name, "func1");
    assert_eq!(response.tool_calls[1].arguments["num"], 42);
}

// ============= Provider Tests =============

#[test]
fn test_provider_from_env_no_config() {
    // Clear environment variables for this test
    unsafe {
        std::env::remove_var("LLAMACPP_MODEL_PATH");
        std::env::remove_var("OPENAI_API_KEY");
        std::env::remove_var("OLLAMA_URL");
        std::env::remove_var("OLLAMA_MODEL");
    }

    // With ollama feature enabled by default, this should succeed
    let result = Provider::from_env();
    // Result depends on which features are enabled at compile time
    // If ollama is enabled, it should return Ollama provider
    // Otherwise it should error
    #[cfg(feature = "ollama")]
    assert!(result.is_ok());

    #[cfg(not(any(feature = "ollama", feature = "openai", feature = "llamacpp")))]
    assert!(result.is_err());
}

// ============= Tool Definition Tests =============

#[test]
fn test_tool_definition_serialization() {
    let tool = ToolDefinition {
        name: "search".to_string(),
        description: "Search the web".to_string(),
        parameters: serde_json::json!({
            "type": "object",
            "properties": {
                "query": {"type": "string", "description": "Search query"}
            },
            "required": ["query"]
        }),
    };

    assert_eq!(tool.name, "search");
    assert_eq!(tool.description, "Search the web");
    assert!(tool.parameters["properties"]["query"].is_object());
}

#[test]
fn test_tool_call_structure() {
    let call = ToolCall {
        id: "unique-id-123".to_string(),
        name: "get_weather".to_string(),
        arguments: serde_json::json!({
            "city": "London",
            "units": "celsius"
        }),
    };

    assert_eq!(call.id, "unique-id-123");
    assert_eq!(call.name, "get_weather");
    assert_eq!(call.arguments["city"], "London");
    assert_eq!(call.arguments["units"], "celsius");
}

// ============= LLMClientFactory Tests =============

#[cfg(feature = "ollama")]
#[test]
fn test_llm_client_factory_creation() {
    // Create a factory with an Ollama provider (which won't be called)
    let factory = LLMClientFactory::new(Provider::Ollama {
        base_url: "http://localhost:11434".to_string(),
        model: "ministral-3:3b".to_string(),
        params: Default::default(),
    });

    // Factory should be created successfully
    assert!(std::mem::size_of_val(&factory) > 0);
}

#[cfg(not(feature = "ollama"))]
#[test]
fn test_llm_client_factory_creation() {
    // When ollama feature is not enabled, we just verify types exist
    // This is a placeholder test that always passes
    assert!(true);
}

// ============= Multi-Message Conversation Tests =============

#[tokio::test]
async fn test_multi_turn_conversation() {
    let client = MockLLMClient::new("Final response after history");

    // Simulate a multi-turn conversation
    let history = vec![
        (
            "system".to_string(),
            "You are a helpful assistant.".to_string(),
        ),
        ("user".to_string(), "What is 2+2?".to_string()),
        ("assistant".to_string(), "2+2 equals 4.".to_string()),
        ("user".to_string(), "What about 3+3?".to_string()),
    ];

    let result = client.generate_with_history(&history).await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn test_empty_history() {
    let client = MockLLMClient::new("Response to empty history");
    let history: Vec<(String, String)> = vec![];

    let result = client.generate_with_history(&history).await;
    assert!(result.is_ok());
}

// ============= Edge Case Tests =============

#[tokio::test]
async fn test_empty_prompt() {
    let client = MockLLMClient::new("Response to empty");
    let result = client.generate("").await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn test_very_long_prompt() {
    let client = MockLLMClient::new("Response to long prompt");
    let long_prompt = "test ".repeat(1000);
    let result = client.generate(&long_prompt).await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn test_unicode_prompt() {
    let client = MockLLMClient::new("Response with unicode: 你好世界 🌍");
    let result = client.generate("Hello in Chinese: 你好").await;
    assert!(result.is_ok());
    assert!(result.unwrap().contains("你好世界"));
}

#[tokio::test]
async fn test_special_characters_in_prompt() {
    let client = MockLLMClient::new("Response with special chars");
    let prompt = r#"Test with "quotes", 'apostrophes', \backslash, and {braces}"#;
    let result = client.generate(prompt).await;
    assert!(result.is_ok());
}

// ============= Tool Call Argument Tests =============

#[test]
fn test_tool_call_complex_arguments() {
    let call = ToolCall {
        id: "complex-call".to_string(),
        name: "complex_tool".to_string(),
        arguments: serde_json::json!({
            "string_arg": "hello",
            "number_arg": 42,
            "float_arg": 2.75,
            "bool_arg": true,
            "null_arg": null,
            "array_arg": [1, 2, 3],
            "object_arg": {"nested": "value"}
        }),
    };

    assert_eq!(call.arguments["string_arg"], "hello");
    assert_eq!(call.arguments["number_arg"], 42);
    assert!(call.arguments["bool_arg"].as_bool().unwrap());
    assert!(call.arguments["null_arg"].is_null());
    assert_eq!(call.arguments["array_arg"].as_array().unwrap().len(), 3);
    assert_eq!(call.arguments["object_arg"]["nested"], "value");
}

// ============= Multiple Tool Calls Tests =============

#[tokio::test]
async fn test_multiple_tool_calls_in_single_response() {
    let tool_calls = vec![
        ToolCall {
            id: "call-1".to_string(),
            name: "get_weather".to_string(),
            arguments: serde_json::json!({"city": "London"}),
        },
        ToolCall {
            id: "call-2".to_string(),
            name: "get_time".to_string(),
            arguments: serde_json::json!({"timezone": "UTC"}),
        },
        ToolCall {
            id: "call-3".to_string(),
            name: "search".to_string(),
            arguments: serde_json::json!({"query": "news"}),
        },
    ];

    let client = MockLLMClient::with_tool_calls("Processing multiple tools", tool_calls);
    let tools: Vec<ToolDefinition> = vec![];

    let result = client
        .generate_with_tools("What's the weather, time, and news?", &tools)
        .await;
    assert!(result.is_ok());

    let response = result.unwrap();
    assert_eq!(response.tool_calls.len(), 3);
    assert_eq!(response.tool_calls[0].name, "get_weather");
    assert_eq!(response.tool_calls[1].name, "get_time");
    assert_eq!(response.tool_calls[2].name, "search");
}