reasonkit-core 0.1.8

The Reasoning Engine — Auditable Reasoning for Production AI | Rust-Native | Turn Prompts into Protocols
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
//! Multi-Backend LLM Orchestration
//!
//! This module provides unified LLM orchestration using the Graniet `llm` crate,
//! supporting multiple backends (OpenAI, Claude, xAI, etc.) with tool use and memory sharing.
//!
//! # Features
//! - Unified API across all LLM providers
//! - Tool/function calling support
//! - Memory sharing between conversations
//! - Streaming responses
//!
//! Enable with: `cargo build --features llm-orchestration-multi`

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// Re-export the llm crate for direct access
pub use llm;

/// Supported LLM backends
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum LlmBackend {
    /// OpenAI (GPT-4, GPT-4o, etc.)
    OpenAI,
    /// Anthropic (Claude 3/4)
    Anthropic,
    /// xAI (Grok)
    XAI,
    /// Google (Gemini)
    Google,
    /// Local Ollama
    Ollama,
    /// OpenRouter (multi-provider)
    OpenRouter,
}

impl LlmBackend {
    /// Get the environment variable name for the API key
    pub fn api_key_env_var(&self) -> &'static str {
        match self {
            Self::OpenAI => "OPENAI_API_KEY",
            Self::Anthropic => "ANTHROPIC_API_KEY",
            Self::XAI => "XAI_API_KEY",
            Self::Google => "GOOGLE_API_KEY",
            Self::Ollama => "OLLAMA_HOST",
            Self::OpenRouter => "OPENROUTER_API_KEY",
        }
    }

    /// Get the default model for this backend
    pub fn default_model(&self) -> &'static str {
        match self {
            Self::OpenAI => "gpt-4o",
            Self::Anthropic => "claude-sonnet-4-20250514",
            Self::XAI => "grok-2",
            Self::Google => "gemini-2.0-flash",
            Self::Ollama => "llama3.2",
            Self::OpenRouter => "anthropic/claude-sonnet-4",
        }
    }
}

/// Configuration for multi-backend LLM client
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultiLlmConfig {
    /// Primary backend to use
    pub primary_backend: LlmBackend,
    /// Fallback backends in order of preference
    pub fallback_backends: Vec<LlmBackend>,
    /// Model overrides per backend
    pub model_overrides: HashMap<String, String>,
    /// Maximum tokens for response
    pub max_tokens: Option<u32>,
    /// Temperature (0.0 - 2.0)
    pub temperature: Option<f32>,
    /// Enable streaming responses
    pub streaming: bool,
    /// Timeout in seconds
    pub timeout_secs: u64,
}

impl Default for MultiLlmConfig {
    fn default() -> Self {
        Self {
            primary_backend: LlmBackend::Anthropic,
            fallback_backends: vec![LlmBackend::OpenAI, LlmBackend::OpenRouter],
            model_overrides: HashMap::new(),
            max_tokens: Some(4096),
            temperature: Some(0.7),
            streaming: true,
            timeout_secs: 120,
        }
    }
}

/// A message in the conversation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
    /// Role: "user", "assistant", or "system"
    pub role: String,
    /// Message content
    pub content: String,
    /// Optional name for the message sender
    pub name: Option<String>,
}

impl Message {
    /// Create a new user message
    pub fn user(content: impl Into<String>) -> Self {
        Self {
            role: "user".to_string(),
            content: content.into(),
            name: None,
        }
    }

    /// Create a new assistant message
    pub fn assistant(content: impl Into<String>) -> Self {
        Self {
            role: "assistant".to_string(),
            content: content.into(),
            name: None,
        }
    }

    /// Create a new system message
    pub fn system(content: impl Into<String>) -> Self {
        Self {
            role: "system".to_string(),
            content: content.into(),
            name: None,
        }
    }
}

/// Tool definition for function calling
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolDefinition {
    /// Tool name
    pub name: String,
    /// Tool description for the LLM
    pub description: String,
    /// JSON Schema for parameters
    pub parameters: serde_json::Value,
}

/// Tool call request from the LLM
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCall {
    /// Tool call ID
    pub id: String,
    /// Tool name
    pub name: String,
    /// Arguments as JSON
    pub arguments: serde_json::Value,
}

/// Response from LLM completion
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompletionResponse {
    /// Generated content
    pub content: String,
    /// Tool calls requested (if any)
    pub tool_calls: Vec<ToolCall>,
    /// Finish reason
    pub finish_reason: String,
    /// Token usage
    pub usage: TokenUsage,
    /// Backend that processed the request
    pub backend: LlmBackend,
    /// Model used
    pub model: String,
}

/// Token usage statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TokenUsage {
    /// Input tokens
    pub prompt_tokens: u32,
    /// Output tokens
    pub completion_tokens: u32,
    /// Total tokens
    pub total_tokens: u32,
}

/// Conversation memory for context sharing
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ConversationMemory {
    /// Conversation ID
    pub id: String,
    /// Messages in the conversation
    pub messages: Vec<Message>,
    /// Metadata
    pub metadata: HashMap<String, serde_json::Value>,
}

impl ConversationMemory {
    /// Create a new conversation memory
    pub fn new(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            messages: Vec::new(),
            metadata: HashMap::new(),
        }
    }

    /// Add a message to the conversation
    pub fn add_message(&mut self, message: Message) {
        self.messages.push(message);
    }

    /// Get the last N messages
    pub fn last_messages(&self, n: usize) -> &[Message] {
        let start = self.messages.len().saturating_sub(n);
        &self.messages[start..]
    }

    /// Clear all messages
    pub fn clear(&mut self) {
        self.messages.clear();
    }

    /// Get total message count
    pub fn len(&self) -> usize {
        self.messages.len()
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.messages.is_empty()
    }
}

/// Builder for tool definitions
pub struct ToolBuilder {
    name: String,
    description: String,
    properties: serde_json::Map<String, serde_json::Value>,
    required: Vec<String>,
}

impl ToolBuilder {
    /// Create a new tool builder
    pub fn new(name: impl Into<String>, description: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            description: description.into(),
            properties: serde_json::Map::new(),
            required: Vec::new(),
        }
    }

    /// Add a string parameter
    pub fn string_param(
        mut self,
        name: impl Into<String>,
        description: impl Into<String>,
        required: bool,
    ) -> Self {
        let name = name.into();
        self.properties.insert(
            name.clone(),
            serde_json::json!({
                "type": "string",
                "description": description.into()
            }),
        );
        if required {
            self.required.push(name);
        }
        self
    }

    /// Add a number parameter
    pub fn number_param(
        mut self,
        name: impl Into<String>,
        description: impl Into<String>,
        required: bool,
    ) -> Self {
        let name = name.into();
        self.properties.insert(
            name.clone(),
            serde_json::json!({
                "type": "number",
                "description": description.into()
            }),
        );
        if required {
            self.required.push(name);
        }
        self
    }

    /// Add a boolean parameter
    pub fn bool_param(
        mut self,
        name: impl Into<String>,
        description: impl Into<String>,
        required: bool,
    ) -> Self {
        let name = name.into();
        self.properties.insert(
            name.clone(),
            serde_json::json!({
                "type": "boolean",
                "description": description.into()
            }),
        );
        if required {
            self.required.push(name);
        }
        self
    }

    /// Add an array parameter
    pub fn array_param(
        mut self,
        name: impl Into<String>,
        description: impl Into<String>,
        item_type: &str,
        required: bool,
    ) -> Self {
        let name = name.into();
        self.properties.insert(
            name.clone(),
            serde_json::json!({
                "type": "array",
                "items": { "type": item_type },
                "description": description.into()
            }),
        );
        if required {
            self.required.push(name);
        }
        self
    }

    /// Add an enum parameter
    pub fn enum_param(
        mut self,
        name: impl Into<String>,
        description: impl Into<String>,
        values: &[&str],
        required: bool,
    ) -> Self {
        let name = name.into();
        self.properties.insert(
            name.clone(),
            serde_json::json!({
                "type": "string",
                "enum": values,
                "description": description.into()
            }),
        );
        if required {
            self.required.push(name);
        }
        self
    }

    /// Build the tool definition
    pub fn build(self) -> ToolDefinition {
        ToolDefinition {
            name: self.name,
            description: self.description,
            parameters: serde_json::json!({
                "type": "object",
                "properties": self.properties,
                "required": self.required
            }),
        }
    }
}

/// Check if a backend is available (has API key configured)
pub fn is_backend_available(backend: LlmBackend) -> bool {
    std::env::var(backend.api_key_env_var()).is_ok()
}

/// Get all available backends
pub fn available_backends() -> Vec<LlmBackend> {
    [
        LlmBackend::OpenAI,
        LlmBackend::Anthropic,
        LlmBackend::XAI,
        LlmBackend::Google,
        LlmBackend::Ollama,
        LlmBackend::OpenRouter,
    ]
    .into_iter()
    .filter(|b| is_backend_available(*b))
    .collect()
}

/// Estimate token count for text (rough approximation)
pub fn estimate_tokens(text: &str) -> u32 {
    // Rough estimate: ~4 chars per token for English
    (text.len() / 4) as u32
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_config_default() {
        let config = MultiLlmConfig::default();
        assert_eq!(config.primary_backend, LlmBackend::Anthropic);
        assert!(config.streaming);
    }

    #[test]
    fn test_message_construction() {
        let msg = Message::user("Hello");
        assert_eq!(msg.role, "user");
        assert_eq!(msg.content, "Hello");

        let msg = Message::system("You are helpful");
        assert_eq!(msg.role, "system");
    }

    #[test]
    fn test_tool_builder() {
        let tool = ToolBuilder::new("search", "Search for information")
            .string_param("query", "Search query", true)
            .number_param("limit", "Max results", false)
            .enum_param("type", "Search type", &["web", "images", "news"], false)
            .build();

        assert_eq!(tool.name, "search");
        assert!(tool.parameters["required"]
            .as_array()
            .unwrap()
            .contains(&serde_json::json!("query")));
    }

    #[test]
    fn test_conversation_memory() {
        let mut memory = ConversationMemory::new("test-conv");
        memory.add_message(Message::user("Hello"));
        memory.add_message(Message::assistant("Hi there!"));

        assert_eq!(memory.len(), 2);
        assert_eq!(memory.last_messages(1).len(), 1);
        assert_eq!(memory.last_messages(1)[0].role, "assistant");
    }

    #[test]
    fn test_backend_env_vars() {
        assert_eq!(LlmBackend::OpenAI.api_key_env_var(), "OPENAI_API_KEY");
        assert_eq!(LlmBackend::Anthropic.api_key_env_var(), "ANTHROPIC_API_KEY");
    }

    #[test]
    fn test_token_estimate() {
        let text = "Hello, world! This is a test.";
        let tokens = estimate_tokens(text);
        assert!(tokens > 0);
        assert!(tokens < 100);
    }
}