1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3
4#[derive(Debug, Clone, Serialize, Deserialize, Default)]
6pub struct Config {
7 pub ollama: OllamaConfig,
9 pub chat: ChatConfig,
11 #[serde(flatten)]
13 pub additional: HashMap<String, serde_json::Value>,
14}
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
18pub struct OllamaConfig {
19 pub host: String,
21 pub port: u16,
23 pub model: String,
25 #[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#[derive(Debug, Clone, Serialize, Deserialize)]
43pub struct ChatConfig {
44 pub max_history: usize,
46 pub enable_streaming: bool,
48 pub temperature: f32,
50 pub max_tokens: u32,
52 #[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
69pub trait ConfigExt {
71 fn core(&self) -> &Config;
73
74 fn core_mut(&mut self) -> &mut Config;
76
77 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 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}