kowalski_core/
config.rs

1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3
4/// Core configuration for the Kowalski system
5#[derive(Debug, Clone, Serialize, Deserialize, Default)]
6pub struct Config {
7    /// Ollama configuration
8    pub ollama: OllamaConfig,
9    /// Chat configuration
10    pub chat: ChatConfig,
11    /// Additional configurations from other agents
12    #[serde(flatten)]
13    pub additional: HashMap<String, serde_json::Value>,
14}
15
16/// Configuration for Ollama integration
17#[derive(Debug, Clone, Serialize, Deserialize)]
18pub struct OllamaConfig {
19    /// The host where Ollama is running
20    pub host: String,
21    /// The port where Ollama is running
22    pub port: u16,
23    /// The model to use
24    pub model: String,
25    /// Additional Ollama-specific settings
26    #[serde(flatten)]
27    pub additional: HashMap<String, serde_json::Value>,
28}
29
30impl Default for OllamaConfig {
31    fn default() -> Self {
32        Self {
33            host: "localhost".to_string(),
34            port: 11434,
35            model: "llama3.2".to_string(),
36            additional: HashMap::new(),
37        }
38    }
39}
40
41/// Configuration for chat functionality
42#[derive(Debug, Clone, Serialize, Deserialize)]
43pub struct ChatConfig {
44    /// Maximum number of messages to keep in history
45    pub max_history: usize,
46    /// Whether to enable streaming responses
47    pub enable_streaming: bool,
48    /// Temperature for response generation (0.0 to 1.0)
49    pub temperature: f32,
50    /// Maximum number of tokens in generated responses
51    pub max_tokens: u32,
52    /// Additional chat-specific settings
53    #[serde(flatten)]
54    pub additional: HashMap<String, serde_json::Value>,
55}
56
57impl Default for ChatConfig {
58    fn default() -> Self {
59        Self {
60            max_history: 100,
61            enable_streaming: true,
62            temperature: 0.7,
63            max_tokens: 2048,
64            additional: HashMap::new(),
65        }
66    }
67}
68
69/// Trait for extending configuration with additional settings
70pub trait ConfigExt {
71    /// Get a reference to the core configuration
72    fn core(&self) -> &Config;
73
74    /// Get a mutable reference to the core configuration
75    fn core_mut(&mut self) -> &mut Config;
76
77    /// Get additional configuration value by key
78    fn get_additional<T: serde::de::DeserializeOwned>(&self, key: &str) -> Option<T> {
79        self.core()
80            .additional
81            .get(key)
82            .and_then(|v| serde_json::from_value(v.clone()).ok())
83    }
84
85    /// Set additional configuration value
86    fn set_additional<T: serde::Serialize>(&mut self, key: &str, value: T) {
87        if let Ok(json) = serde_json::to_value(value) {
88            self.core_mut().additional.insert(key.to_string(), json);
89        }
90    }
91}