124 lines
4.3 KiB
Rust
124 lines
4.3 KiB
Rust
use std::path::PathBuf;
|
|
|
|
use anyhow::{Context, Result};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::{fsutil, Paths};
|
|
|
|
/// User settings (config/settings.json). Defaults follow CapriCore 1.0.4 `config/core.json`.
|
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
|
#[serde(default, rename_all = "camelCase")]
|
|
pub struct Settings {
|
|
/// Conversation model as named in Ollama.
|
|
pub model: String,
|
|
/// Model to use when `model` (a Capri alias) does not exist in the store.
|
|
pub upstream_model: String,
|
|
/// Port of the Ollama server that Capri starts and owns.
|
|
pub ollama_port: u16,
|
|
/// Use an already running Ollama server instead of starting one, e.g. `http://127.0.0.1:11434`.
|
|
pub ollama_url: Option<String>,
|
|
/// Ollama model store. Defaults to `<data>/models`.
|
|
pub ollama_models: Option<PathBuf>,
|
|
/// Ollama executable. Defaults to `ollama` on PATH.
|
|
pub ollama_path: Option<PathBuf>,
|
|
pub context_tokens: u32,
|
|
/// Seconds Ollama keeps the model loaded after a reply.
|
|
pub keep_alive: i64,
|
|
pub cpu_threads: Option<u32>,
|
|
/// `auto` or `cpu`.
|
|
pub acceleration: String,
|
|
pub temperature: f32,
|
|
pub repeat_last_n: i32,
|
|
pub language: LanguageSettings,
|
|
/// Folder holding `runtime-sprite-set.json` and the Capri sprite frames.
|
|
pub avatar_dir: Option<PathBuf>,
|
|
/// Feature settings, one JSON object per feature crate (see [`Section`]).
|
|
pub sections: std::collections::BTreeMap<String, serde_json::Value>,
|
|
}
|
|
|
|
/// Typed settings owned by a feature crate, stored under `sections[NAME]` in settings.json.
|
|
/// Unknown or invalid content falls back to `Default` field by field (use `#[serde(default)]`).
|
|
pub trait Section: Serialize + serde::de::DeserializeOwned + Default + Clone + PartialEq + Send + Sync + 'static {
|
|
const NAME: &'static str;
|
|
}
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
|
#[serde(default, rename_all = "camelCase")]
|
|
pub struct LanguageSettings {
|
|
pub interface: String,
|
|
pub conversation: String,
|
|
pub custom_conversation: String,
|
|
}
|
|
|
|
impl Default for LanguageSettings {
|
|
fn default() -> Self {
|
|
Self { interface: "en".into(), conversation: "en".into(), custom_conversation: String::new() }
|
|
}
|
|
}
|
|
|
|
impl Default for Settings {
|
|
fn default() -> Self {
|
|
Self {
|
|
model: "capri-qwen3.5:4b".into(),
|
|
upstream_model: "qwen3.5:4b".into(),
|
|
ollama_port: 11435,
|
|
ollama_url: None,
|
|
ollama_models: None,
|
|
ollama_path: None,
|
|
context_tokens: 32768,
|
|
keep_alive: 300,
|
|
cpu_threads: None,
|
|
acceleration: "auto".into(),
|
|
temperature: 0.85,
|
|
repeat_last_n: 512,
|
|
language: LanguageSettings::default(),
|
|
avatar_dir: None,
|
|
sections: Default::default(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Settings {
|
|
pub fn load(paths: &Paths) -> Result<Self> {
|
|
let file = paths.settings_file();
|
|
match fsutil::read_regular(&file, 256 * 1024)? {
|
|
None => Ok(Self::default()),
|
|
Some(bytes) => serde_json::from_slice(&bytes).with_context(|| format!("Invalid {}", file.display())),
|
|
}
|
|
}
|
|
|
|
pub fn save(&self, paths: &Paths) -> Result<()> {
|
|
fsutil::write_json_atomic(&paths.settings_file(), self)
|
|
}
|
|
|
|
pub fn section<T: Section>(&self) -> T {
|
|
self.sections.get(T::NAME).and_then(|v| serde_json::from_value(v.clone()).ok()).unwrap_or_default()
|
|
}
|
|
|
|
pub fn set_section<T: Section>(&mut self, value: &T) {
|
|
self.sections.insert(T::NAME.into(), serde_json::to_value(value).expect("serializable section"));
|
|
}
|
|
|
|
pub fn ollama_base_url(&self) -> String {
|
|
self.ollama_url.clone().unwrap_or_else(|| format!("http://127.0.0.1:{}", self.ollama_port))
|
|
}
|
|
|
|
pub fn models_dir(&self, paths: &Paths) -> PathBuf {
|
|
self.ollama_models.clone().unwrap_or_else(|| paths.data.join("models"))
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn partial_file_keeps_defaults() {
|
|
let s: Settings = serde_json::from_str(r#"{"model":"x","language":{"conversation":"fr"}}"#).unwrap();
|
|
assert_eq!(s.model, "x");
|
|
assert_eq!(s.ollama_port, 11435);
|
|
assert_eq!(s.language.conversation, "fr");
|
|
assert_eq!(s.language.interface, "en");
|
|
}
|
|
}
|