Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

Cli preferences fix (#705)

Aleksandr Golokozae56e22edd8d
diff --git a/Cargo.lock b/Cargo.lock
index 86060ecaa..05eeee70b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -999,6 +999,7 @@ dependencies = [
"chrono",
"clap",
"comfy-table",
+ "confy",
"crossterm",
"dirs",
"dotenvy",
@@ -1018,6 +1019,7 @@ dependencies = [
"thiserror 2.0.18",
"tokio",
"tokio-stream",
+ "toml 1.1.2+spec-1.1.0",
"tracing",
"unicode-segmentation",
"unicode-width 0.2.2",
diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml
index 5ee2f98cd..a621c2526 100644
--- a/crates/cli/Cargo.toml
+++ b/crates/cli/Cargo.toml
@@ -24,6 +24,7 @@ backend-uzu.workspace = true
chrono.workspace = true
clap.workspace = true
comfy-table.workspace = true
+confy.workspace = true
crossterm.workspace = true
dirs.workspace = true
dotenvy.workspace = true
@@ -43,6 +44,7 @@ sysinfo.workspace = true
thiserror.workspace = true
tokio = { workspace = true, features = ["full"] }
tokio-stream.workspace = true
+toml.workspace = true
tracing.workspace = true
unicode-segmentation.workspace = true
unicode-width.workspace = true
diff --git a/crates/cli/src/interactive/app.rs b/crates/cli/src/interactive/app.rs
index ba79aeda4..8127acce5 100644
--- a/crates/cli/src/interactive/app.rs
+++ b/crates/cli/src/interactive/app.rs
@@ -6,10 +6,7 @@ use uzu::{
settings::SettingsError,
};
-use crate::interactive::{
- components::{AppSettings, Application, Preferences, Theme},
- model::{ModelResolutionError, resolve_model_id},
-};
+use crate::interactive::{components::Application, model::ModelResolutionError};
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
#[non_exhaustive]
@@ -54,33 +51,12 @@ impl CliApplication {
}
let settings = self.engine.settings().await.ok();
- let theme = match &settings {
- Some(settings) => Theme::load(settings)?.unwrap_or_default(),
- None => Theme::default(),
- };
- let preferences = match &settings {
- Some(settings) => Preferences::load(settings)?,
- None => Preferences::default(),
- };
- let app_settings = match &settings {
- Some(settings) => AppSettings::load(settings)?,
- None => AppSettings::default(),
- };
-
- let requested_model = model.or_else(|| app_settings.selected_model_id.clone());
- let selected_model = match requested_model {
- Some(model) => resolve_model_id(&self.engine, model).await?,
- None => None,
- };
element! {
Application(
engine: Some(self.engine.clone()),
- settings: settings,
- theme: Some(theme),
- preferences: Some(preferences),
- app_settings: Some(app_settings),
- model: selected_model,
+ settings,
+ model,
)
}
.render_loop()
diff --git a/crates/cli/src/interactive/components/app_settings.rs b/crates/cli/src/interactive/components/app_settings.rs
deleted file mode 100644
index f1f1ce239..000000000
--- a/crates/cli/src/interactive/components/app_settings.rs
+++ /dev/null
@@ -1,28 +0,0 @@
-use serde::{Deserialize, Serialize};
-use uzu::settings::{SettingKind, Settings, SettingsError};
-
-const SETTINGS_APP: &str = "app";
-
-#[derive(Clone, Serialize, Deserialize, Default)]
-pub struct AppSettings {
- pub selected_model_id: Option<String>,
-}
-
-impl AppSettings {
- pub fn load(settings: &Settings) -> Result<Self, SettingsError> {
- let Some(raw) = settings.load(SettingKind::Config, SETTINGS_APP.to_string())? else {
- return Ok(Self::default());
- };
- Ok(serde_json::from_str(&raw).unwrap_or_default())
- }
-
- pub fn save(
- &self,
- settings: &Settings,
- ) -> Result<(), SettingsError> {
- let raw = serde_json::to_string(self).map_err(|error| SettingsError::BackendError {
- message: error.to_string(),
- })?;
- settings.save(SettingKind::Config, SETTINGS_APP.to_string(), Some(raw))
- }
-}
diff --git a/crates/cli/src/interactive/components/application.rs b/crates/cli/src/interactive/components/application.rs
index 7bd79c7b6..cf39b3b40 100644
--- a/crates/cli/src/interactive/components/application.rs
+++ b/crates/cli/src/interactive/components/application.rs
@@ -1,3 +1,5 @@
+use std::path::PathBuf;
+
use iocraft::prelude::*;
use shoji::types::model::Model;
use uzu::{
@@ -9,12 +11,11 @@ use uzu::{
use crate::{
common::model_capabilities::ModelCapabilities,
interactive::{
- components::{
- CommandInput, HistoryCell, HistoryCellType, Logo, Preferences, SelectedModel, Theme,
- app_settings::AppSettings,
- },
+ APP_IDENTIFIER,
+ components::{CommandInput, HistoryCell, HistoryCellType, Logo, Preferences, SelectedModel, Theme},
flows::{AuthFlow, ExitFlow, Flow, FlowEvent, FlowRegistry, ModelRegistriesFlow, SettingsFlow, ThemeFlow},
helpers::SYMBOL_COMMAND,
+ model::resolve_model_id,
sessions::{self, SessionState},
},
};
@@ -25,9 +26,6 @@ const HISTORY_LIMIT: usize = 20;
pub struct ApplicationProps {
pub engine: Option<Engine>,
pub settings: Option<Settings>,
- pub theme: Option<Theme>,
- pub preferences: Option<Preferences>,
- pub app_settings: Option<AppSettings>,
pub model: Option<String>,
}
@@ -39,11 +37,9 @@ pub struct ModelState {
}
pub struct ApplicationState {
+ preferences: Preferences,
pub engine: Engine,
pub settings: Option<Settings>,
- pub theme: Theme,
- pub preferences: Preferences,
- pub app_settings: AppSettings,
pub flow: Option<Box<dyn Flow>>,
pub history: Vec<HistoryCellType>,
pub registry: FlowRegistry,
@@ -51,9 +47,42 @@ pub struct ApplicationState {
}
impl ApplicationState {
+ pub fn load_preferences() -> Result<Preferences, Box<dyn std::error::Error>> {
+ Preferences::load_or_migrate(&Self::settings_file_path("config")?, &Self::settings_file_path("settings")?)
+ }
+
pub fn session_state(&self) -> Option<&dyn SessionState> {
self.model_state.as_ref().and_then(|model_state| model_state.session_state.as_deref())
}
+
+ pub fn preferences(&self) -> &Preferences {
+ &self.preferences
+ }
+
+ pub fn theme(&self) -> &Theme {
+ &self.preferences.theme
+ }
+
+ pub fn set_preferences(
+ &mut self,
+ prefs: &Preferences,
+ ) -> Result<(), Box<dyn std::error::Error>> {
+ self.preferences = prefs.clone();
+ self.preferences.store(&Self::settings_file_path("config")?)
+ }
+
+ pub fn set_theme(
+ &mut self,
+ theme: Theme,
+ ) -> Result<(), Box<dyn std::error::Error>> {
+ let mut preferences = self.preferences.clone();
+ preferences.theme = theme;
+ self.set_preferences(&preferences)
+ }
+
+ fn settings_file_path(name: &str) -> Result<PathBuf, Box<dyn std::error::Error>> {
+ confy::get_configuration_file_path(APP_IDENTIFIER, name).map_err(Into::into)
+ }
}
#[component]
@@ -66,9 +95,10 @@ pub fn Application(
let state = hooks.use_state(|| ApplicationState {
engine,
settings: props.settings.clone(),
- theme: props.theme.clone().unwrap_or_default(),
- preferences: props.preferences.unwrap_or_default(),
- app_settings: props.app_settings.clone().unwrap_or_default(),
+ preferences: ApplicationState::load_preferences().unwrap_or_else(|error| {
+ tracing::warn!("Unable to load or migrate CLI preferences: {error}");
+ Preferences::default()
+ }),
flow: None,
history: Vec::new(),
registry: FlowRegistry::default()
@@ -80,12 +110,26 @@ pub fn Application(
model_state: None,
});
let (width, _) = hooks.use_terminal_size();
+ let requested_model = props.model.clone().or_else(|| state.read().preferences().selected_model_id.clone());
hooks.use_future({
let engine = state.read().engine.clone();
- let initial_model = props.model.clone();
let mut state = state;
async move {
+ let initial_model = match requested_model {
+ Some(model) => match resolve_model_id(&engine, model).await {
+ Ok(model) => model,
+ Err(error) => {
+ state.write().history.push(HistoryCellType::CommandResult {
+ result: format!("Failed to resolve model: {error}"),
+ });
+ state.write().flow = Some(Box::new(ModelRegistriesFlow));
+ return;
+ },
+ },
+ None => None,
+ };
+
let Some(identifier) = initial_model else {
state.write().flow = Some(Box::new(ModelRegistriesFlow));
return;
@@ -284,7 +328,7 @@ pub fn Application(
width: 100pct,
height: 1u16,
border_style: BorderStyle::Single,
- border_color: state.read().theme.accent_color,
+ border_color: state.read().theme().accent_color,
border_edges: Some(Edges::Top),
)
#(flow_component)
@@ -332,8 +376,8 @@ pub fn Application(
width: width,
) {
View(
- padding_left: state.read().theme.padding(),
- padding_right: state.read().theme.padding(),
+ padding_left: state.read().theme().padding(),
+ padding_right: state.read().theme().padding(),
) {
Logo
}
@@ -347,7 +391,7 @@ pub fn Application(
flex_direction: FlexDirection::Column,
column_gap: 0,
) {
- View(height: state.read().theme.padding())
+ View(height: state.read().theme().padding())
#(selected_model_component)
#(input_component)
}
diff --git a/crates/cli/src/interactive/components/command_input.rs b/crates/cli/src/interactive/components/command_input.rs
index 84cb0904e..4edb685e7 100644
--- a/crates/cli/src/interactive/components/command_input.rs
+++ b/crates/cli/src/interactive/components/command_input.rs
@@ -38,10 +38,10 @@ pub fn CommandInput(
TextInputFocus::Minimal
};
- let padding = state.read().theme.padding();
- let accent_color = state.read().theme.accent_color;
- let subtitle_color = state.read().theme.subtitle_color;
- let overlay_color = state.read().theme.overlay_color();
+ let padding = state.read().theme().padding();
+ let accent_color = state.read().theme().accent_color;
+ let subtitle_color = state.read().theme().subtitle_color;
+ let overlay_color = state.read().theme().overlay_color();
let maximal_width = width
.saturating_sub(UnicodeWidthStr::width(SYMBOL_INPUT) as u16)
.saturating_sub(padding)
@@ -118,7 +118,7 @@ fn hint_component(
return element! {
Text(
content: hints.join("\n"),
- color: state.read().theme.subtitle_color,
+ color: state.read().theme().subtitle_color,
)
}
.into();
@@ -133,9 +133,9 @@ fn hint_component(
})
.collect();
let maximal_height = (items.len() as u16).min(5);
- let accent_color = state.read().theme.accent_color;
- let subtitle_color = state.read().theme.subtitle_color;
- let columns_padding = state.read().theme.padding_wide();
+ let accent_color = state.read().theme().accent_color;
+ let subtitle_color = state.read().theme().subtitle_color;
+ let columns_padding = state.read().theme().padding_wide();
element! {
Selector(
diff --git a/crates/cli/src/interactive/components/history_cell.rs b/crates/cli/src/interactive/components/history_cell.rs
index d0251983e..211e49f9a 100644
--- a/crates/cli/src/interactive/components/history_cell.rs
+++ b/crates/cli/src/interactive/components/history_cell.rs
@@ -53,7 +53,7 @@ pub fn HistoryCell(
hooks: Hooks,
) -> impl Into<AnyElement<'static>> {
let state = hooks.use_context::<State<ApplicationState>>();
- let theme = state.read().theme.clone();
+ let theme = state.read().theme().clone();
let view: AnyElement<'static> = match props.r#type.clone() {
Some(HistoryCellType::Command {
diff --git a/crates/cli/src/interactive/components/loading.rs b/crates/cli/src/interactive/components/loading.rs
index 344c4becb..83e07ce2a 100644
--- a/crates/cli/src/interactive/components/loading.rs
+++ b/crates/cli/src/interactive/components/loading.rs
@@ -34,7 +34,7 @@ pub fn Loading<'a>(
.into()
} else {
element! {
- Text(content: label, color: state.read().theme.subtitle_color)
+ Text(content: label, color: state.read().theme().subtitle_color)
}
.into()
};
diff --git a/crates/cli/src/interactive/components/logo.rs b/crates/cli/src/interactive/components/logo.rs
index 86289f965..d40ba36e0 100644
--- a/crates/cli/src/interactive/components/logo.rs
+++ b/crates/cli/src/interactive/components/logo.rs
@@ -43,15 +43,15 @@ pub fn Logo(mut hooks: Hooks) -> impl Into<AnyElement<'static>> {
flex_direction: FlexDirection::Row,
align_items: AlignItems::FlexStart,
width: 100pct,
- column_gap: state.read().theme.padding_wide(),
+ column_gap: state.read().theme().padding_wide(),
) {
Text(
content: LOGO,
- color: state.read().theme.accent_color
+ color: state.read().theme().accent_color
)
Text(
content: about,
- color: state.read().theme.subtitle_color
+ color: state.read().theme().subtitle_color
)
}
}
diff --git a/crates/cli/src/interactive/components/mod.rs b/crates/cli/src/interactive/components/mod.rs
index 2a30b1f32..1bf68cb53 100644
--- a/crates/cli/src/interactive/components/mod.rs
+++ b/crates/cli/src/interactive/components/mod.rs
@@ -1,4 +1,3 @@
-mod app_settings;
mod application;
mod command_input;
mod gradient;
@@ -14,14 +13,13 @@ mod text_input;
mod theme;
mod thinking_support_ext;
-pub use app_settings::AppSettings;
pub use application::{Application, ApplicationState, ModelState};
pub use command_input::CommandInput;
pub use gradient::Gradient;
pub use history_cell::{HistoryCell, HistoryCellType, TranscriptItem};
pub use loading::Loading;
pub use logo::Logo;
-pub use preferences::{Preferences, SamplingMode};
+pub use preferences::Preferences;
pub use progress_bar::ProgressBar;
pub use rendered_text::RenderedText;
pub use selected_model::SelectedModel;
diff --git a/crates/cli/src/interactive/components/preferences.rs b/crates/cli/src/interactive/components/preferences.rs
index 8459957e8..4306c860f 100644
--- a/crates/cli/src/interactive/components/preferences.rs
+++ b/crates/cli/src/interactive/components/preferences.rs
@@ -1,158 +1,75 @@
-use serde::{Deserialize, Serialize};
-use shoji::types::basic::{SamplingMethod, SamplingPolicy};
-use uzu::settings::{SettingKind, Settings, SettingsError};
-
-use crate::common::model_capabilities::ThinkingPreference;
-
-const SETTINGS_PREFERENCES: &str = "cli_preferences";
-
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
-#[serde(rename_all = "snake_case")]
-pub enum SamplingMode {
- #[default]
- ModelDefault,
- Greedy,
- Stochastic,
-}
-
-impl SamplingMode {
- pub const ALL: [SamplingMode; 3] = [Self::ModelDefault, Self::Greedy, Self::Stochastic];
-
- pub fn label(&self) -> &'static str {
- match self {
- Self::ModelDefault => "model default",
- Self::Greedy => "greedy",
- Self::Stochastic => "stochastic",
- }
- }
+use std::{collections::HashMap, error::Error, fs, io, path::Path};
- pub fn next(self) -> Self {
- cycle(&Self::ALL, self, 1)
- }
-
- pub fn previous(self) -> Self {
- cycle(&Self::ALL, self, -1)
- }
-}
-
-#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Default)]
-#[serde(default)]
-pub struct SamplingPreferences {
- pub mode: SamplingMode,
- pub temperature_enabled: bool,
- pub temperature: f64,
- pub top_k_enabled: bool,
- pub top_k: i64,
- pub top_p_enabled: bool,
- pub top_p: f64,
- pub min_p_enabled: bool,
- pub min_p: f64,
- pub repetition_penalty_enabled: bool,
- pub repetition_penalty: f64,
- pub suffix_repetition_length: i64,
-}
-
-impl SamplingPreferences {
- pub fn suffix_repetition_length_enabled(&self) -> bool {
- self.repetition_penalty_enabled
- }
-
- pub fn suffix_repetition_length_disable(&mut self) {
- self.repetition_penalty_enabled = false;
- }
-
- pub fn policy(&self) -> SamplingPolicy {
- match self.mode {
- SamplingMode::ModelDefault => SamplingPolicy::Default {},
- SamplingMode::Greedy => SamplingPolicy::Custom {
- method: SamplingMethod::Greedy {},
- },
- SamplingMode::Stochastic => SamplingPolicy::Custom {
- method: SamplingMethod::Stochastic {
- temperature: self.temperature_enabled.then_some(self.temperature),
- top_k: self.top_k_enabled.then_some(self.top_k),
- top_p: self.top_p_enabled.then_some(self.top_p),
- min_p: self.min_p_enabled.then_some(self.min_p),
- repetition_penalty: self.repetition_penalty_enabled.then_some(self.repetition_penalty),
- suffix_repetition_length: self
- .suffix_repetition_length_enabled()
- .then_some(self.suffix_repetition_length),
- },
- },
- }
- }
+use serde::{Deserialize, Serialize};
- pub fn summary(&self) -> String {
- match self.mode {
- SamplingMode::ModelDefault => "default".to_string(),
- SamplingMode::Greedy => "greedy".to_string(),
- SamplingMode::Stochastic => {
- let mut parts = Vec::new();
- if self.temperature_enabled {
- parts.push(format!("temp {:.2}", self.temperature));
- }
- if self.top_k_enabled {
- parts.push(format!("top-k {}", self.top_k));
- }
- if self.top_p_enabled {
- parts.push(format!("top-p {:.2}", self.top_p));
- }
- if self.min_p_enabled {
- parts.push(format!("min-p {:.2}", self.min_p));
- }
- if self.repetition_penalty_enabled {
- parts.push(format!("repetition penalty {:.2}", self.repetition_penalty));
- }
- if self.suffix_repetition_length_enabled() {
- parts.push(format!("suffix repetition length {:.2}", self.suffix_repetition_length));
- }
- if parts.is_empty() {
- "stochastic".to_string()
- } else {
- parts.join(", ")
- }
- },
- }
- }
-}
+use crate::{
+ common::model_capabilities::ThinkingPreference,
+ interactive::{components::Theme, sampling::SamplingPreferences},
+};
-#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Default)]
+#[derive(Clone, Default, Deserialize, Serialize)]
#[serde(default)]
pub struct Preferences {
+ pub theme: Theme,
pub thinking: ThinkingPreference,
pub sampling: SamplingPreferences,
+ pub selected_model_id: Option<String>,
}
impl Preferences {
- pub fn load(settings: &Settings) -> Result<Self, SettingsError> {
- let Some(raw) = settings.load(SettingKind::Config, SETTINGS_PREFERENCES.to_string())? else {
- return Ok(Self::default());
+ pub(super) fn load_or_migrate(
+ config_path: &Path,
+ legacy_path: &Path,
+ ) -> Result<Self, Box<dyn Error>> {
+ if !legacy_path.exists() {
+ return if config_path.exists() {
+ Self::load(config_path)
+ } else {
+ Ok(Self::default())
+ };
+ }
+
+ let mut preferences = if config_path.exists() {
+ Self::load(config_path)?
+ } else {
+ Self::default()
};
- Ok(serde_json::from_str(&raw).unwrap_or_default())
+ let mut legacy: HashMap<String, HashMap<String, String>> = toml::from_str(&fs::read_to_string(legacy_path)?)?;
+ let settings = legacy.remove("settings").unwrap_or_default();
+
+ if let Some(raw) = settings.get("cli_preferences")
+ && let Ok(legacy_preferences) = serde_json::from_str::<Self>(raw)
+ {
+ preferences.thinking = legacy_preferences.thinking;
+ preferences.sampling = legacy_preferences.sampling;
+ }
+ if let Some(raw) = settings.get("app") {
+ let app_settings: Self = serde_json::from_str(raw)?;
+ preferences.selected_model_id = app_settings.selected_model_id;
+ }
+ if let Some(name) = settings.get("theme") {
+ preferences.theme = Theme::from_name(name)
+ .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, format!("unknown legacy theme: {name}")))?;
+ }
+
+ preferences.store(config_path)?;
+ Ok(preferences)
}
- pub fn save(
+ pub(super) fn store(
&self,
- settings: &Settings,
- ) -> Result<(), SettingsError> {
- let raw = serde_json::to_string(self).map_err(|error| SettingsError::BackendError {
- message: error.to_string(),
- })?;
- settings.save(SettingKind::Config, SETTINGS_PREFERENCES.to_string(), Some(raw))
+ path: &Path,
+ ) -> Result<(), Box<dyn Error>> {
+ let parent = path
+ .parent()
+ .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "settings file has no parent directory"))?;
+ fs::create_dir_all(parent)?;
+ fs::write(path, toml::to_string(self)?)?;
+ Ok(())
}
- pub fn sampling_policy(&self) -> SamplingPolicy {
- self.sampling.policy()
+ fn load(path: &Path) -> Result<Self, Box<dyn Error>> {
+ let contents = fs::read_to_string(path)?;
+ toml::from_str(&contents).map_err(Into::into)
}
}
-
-pub(super) fn cycle<T: Copy + PartialEq>(
- values: &[T],
- current: T,
- delta: isize,
-) -> T {
- let length = values.len() as isize;
- let index = values.iter().position(|value| *value == current).map(|index| index as isize).unwrap_or(0);
- let next = (index + delta).rem_euclid(length) as usize;
- values[next]
-}
diff --git a/crates/cli/src/interactive/components/progress_bar.rs b/crates/cli/src/interactive/components/progress_bar.rs
index 03bd1c3dc..efa7b033b 100644
--- a/crates/cli/src/interactive/components/progress_bar.rs
+++ b/crates/cli/src/interactive/components/progress_bar.rs
@@ -16,7 +16,7 @@ pub fn ProgressBar(
hooks: Hooks,
) -> impl Into<AnyElement<'static>> {
let state = hooks.use_context::<State<ApplicationState>>();
- let theme = state.read().theme.clone();
+ let theme = state.read().theme().clone();
let from_color = theme.accent_color.darker(0.5);
let to_color = theme.accent_color;
let track_color = theme.overlay_color();
diff --git a/crates/cli/src/interactive/components/selected_model.rs b/crates/cli/src/interactive/components/selected_model.rs
index 423d3e7bd..173a77fbe 100644
--- a/crates/cli/src/interactive/components/selected_model.rs
+++ b/crates/cli/src/interactive/components/selected_model.rs
@@ -167,8 +167,8 @@ pub fn SelectedModel(
}
});
- let theme = state.read().theme.clone();
- let preferences = state.read().preferences;
+ let theme = state.read().theme().clone();
+ let preferences = state.read().preferences().clone();
let model_data = state.read().model_state.as_ref().map(|model_state| {
let session_status = model_state.session_state.as_deref().and_then(|session_state| session_state.status_text());
(model_state.model.clone(), model_state.download_state.clone(), session_status, model_state.capabilities)
diff --git a/crates/cli/src/interactive/components/theme.rs b/crates/cli/src/interactive/components/theme.rs
index cfe33d872..60bd95435 100644
--- a/crates/cli/src/interactive/components/theme.rs
+++ b/crates/cli/src/interactive/components/theme.rs
@@ -1,10 +1,8 @@
use iocraft::prelude::*;
-use uzu::settings::{SettingKind, Settings, SettingsError};
+use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::interactive::helpers::ColorRgb;
-const SETTINGS_THEME: &str = "theme";
-
#[derive(Debug, Clone)]
pub struct Theme {
pub name: String,
@@ -18,6 +16,10 @@ impl Theme {
vec![Self::blue(), Self::green(), Self::yellow(), Self::red(), Self::purple()]
}
+ pub(super) fn from_name(name: &str) -> Option<Self> {
+ Self::all().into_iter().find(|theme| theme.name == name)
+ }
+
pub fn blue() -> Self {
Self {
name: "blue".to_string(),
@@ -62,15 +64,7 @@ impl Theme {
symbol_heart: "💜".to_string(),
}
}
-}
-
-impl Default for Theme {
- fn default() -> Self {
- Self::blue()
- }
-}
-impl Theme {
pub fn padding(&self) -> u16 {
1
}
@@ -84,18 +78,30 @@ impl Theme {
}
}
-impl Theme {
- pub fn load(settings: &Settings) -> Result<Option<Self>, SettingsError> {
- let Some(name) = settings.load(SettingKind::Config, SETTINGS_THEME.to_string())? else {
- return Ok(None);
- };
- Ok(Self::all().into_iter().find(|theme| theme.name == name))
+impl Default for Theme {
+ fn default() -> Self {
+ Self::blue()
}
+}
- pub fn save(
+impl Serialize for Theme {
+ fn serialize<S>(
&self,
- settings: &Settings,
- ) -> Result<(), SettingsError> {
- settings.save(SettingKind::Config, SETTINGS_THEME.to_string(), Some(self.name.clone()))
+ serializer: S,
+ ) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ serializer.serialize_str(&self.name)
+ }
+}
+
+impl<'de> Deserialize<'de> for Theme {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ let name = String::deserialize(deserializer)?;
+ Self::from_name(&name).ok_or_else(|| serde::de::Error::custom(format!("unknown theme: {name}")))
}
}
diff --git a/crates/cli/src/interactive/components/thinking_support_ext.rs b/crates/cli/src/interactive/components/thinking_support_ext.rs
index 712c4a486..e16516c39 100644
--- a/crates/cli/src/interactive/components/thinking_support_ext.rs
+++ b/crates/cli/src/interactive/components/thinking_support_ext.rs
@@ -1,7 +1,9 @@
use shoji::types::basic::ReasoningEffort;
-use super::preferences::cycle;
-use crate::common::model_capabilities::{ThinkingPreference, ThinkingSupport};
+use crate::{
+ common::model_capabilities::{ThinkingPreference, ThinkingSupport},
+ interactive::util::cycle,
+};
const LEVELS: [ReasoningEffort; 5] = [
ReasoningEffort::Default,
diff --git a/crates/cli/src/interactive/flows/auth.rs b/crates/cli/src/interactive/flows/auth.rs
index f88df6f72..9ff3b4afe 100644
--- a/crates/cli/src/interactive/flows/auth.rs
+++ b/crates/cli/src/interactive/flows/auth.rs
@@ -51,9 +51,9 @@ fn AuthProviderFlowView(
items: items,
style: SelectorStyle::WithIcon,
maximal_height: (providers.len() as u16).min(5),
- accent_color: state.read().theme.accent_color,
- subtitle_color: state.read().theme.subtitle_color,
- columns_padding: state.read().theme.padding_wide(),
+ accent_color: state.read().theme().accent_color,
+ subtitle_color: state.read().theme().subtitle_color,
+ columns_padding: state.read().theme().padding_wide(),
on_submit: move |index: usize| {
if let Some(provider) = providers.get(index) {
on_event(FlowEvent::transition(provider.title.clone(), Box::new(AuthKeyFlow::new(provider.clone()))));
diff --git a/crates/cli/src/interactive/flows/auth_key.rs b/crates/cli/src/interactive/flows/auth_key.rs
index f096214ab..26c4948f8 100644
--- a/crates/cli/src/interactive/flows/auth_key.rs
+++ b/crates/cli/src/interactive/flows/auth_key.rs
@@ -96,8 +96,8 @@ fn AuthKeyFlowView(
let state = *hooks.use_context::<State<ApplicationState>>();
let (width, _) = hooks.use_terminal_size();
- let padding = state.read().theme.padding();
- let subtitle_color = state.read().theme.subtitle_color;
+ let padding = state.read().theme().padding();
+ let subtitle_color = state.read().theme().subtitle_color;
let label = format!("{} {}", SYMBOL_INPUT_RESULT, provider.value_title);
let maximal_width = width
.saturating_sub(UnicodeWidthStr::width(label.as_str()) as u16)
diff --git a/crates/cli/src/interactive/flows/exit.rs b/crates/cli/src/interactive/flows/exit.rs
index 11b5b717a..7ea9cd739 100644
--- a/crates/cli/src/interactive/flows/exit.rs
+++ b/crates/cli/src/interactive/flows/exit.rs
@@ -23,5 +23,10 @@ fn ExitFlowView(hooks: Hooks) -> impl Into<AnyElement<'static>> {
let mut system = hooks.use_context_mut::<SystemContext>();
system.exit();
- element! { Text(content: format!("See you soon {}!", state.read().theme.symbol_heart), color: state.read().theme.accent_color) }
+ element! {
+ Text(
+ content: format!("See you soon {}!", state.read().theme().symbol_heart),
+ color: state.read().theme().accent_color
+ )
+ }
}
diff --git a/crates/cli/src/interactive/flows/model_families.rs b/crates/cli/src/interactive/flows/model_families.rs
index b1e346e6d..affec6cc3 100644
--- a/crates/cli/src/interactive/flows/model_families.rs
+++ b/crates/cli/src/interactive/flows/model_families.rs
@@ -65,9 +65,9 @@ fn ModelFamilies(
}
});
- let accent_color = state.read().theme.accent_color;
- let subtitle_color = state.read().theme.subtitle_color;
- let columns_padding = state.read().theme.padding_wide();
+ let accent_color = state.read().theme().accent_color;
+ let subtitle_color = state.read().theme().subtitle_color;
+ let columns_padding = state.read().theme().padding_wide();
let list = families.read().clone().unwrap_or_default();
let loaded = families.read().is_some();
diff --git a/crates/cli/src/interactive/flows/model_registries.rs b/crates/cli/src/interactive/flows/model_registries.rs
index 1daa5eb40..9a551e9ac 100644
--- a/crates/cli/src/interactive/flows/model_registries.rs
+++ b/crates/cli/src/interactive/flows/model_registries.rs
@@ -75,9 +75,9 @@ fn ModelRegistries(
}
});
- let accent_color = state.read().theme.accent_color;
- let subtitle_color = state.read().theme.subtitle_color;
- let columns_padding = state.read().theme.padding_wide();
+ let accent_color = state.read().theme().accent_color;
+ let subtitle_color = state.read().theme().subtitle_color;
+ let columns_padding = state.read().theme().padding_wide();
let list = registries_state.read().clone().unwrap_or_default();
let loaded = registries_state.read().is_some();
diff --git a/crates/cli/src/interactive/flows/models.rs b/crates/cli/src/interactive/flows/models.rs
index bde7f3c78..70f65dc97 100644
--- a/crates/cli/src/interactive/flows/models.rs
+++ b/crates/cli/src/interactive/flows/models.rs
@@ -76,9 +76,9 @@ fn Models(
}
});
- let accent_color = state.read().theme.accent_color;
- let subtitle_color = state.read().theme.subtitle_color;
- let columns_padding = state.read().theme.padding_wide();
+ let accent_color = state.read().theme().accent_color;
+ let subtitle_color = state.read().theme().subtitle_color;
+ let columns_padding = state.read().theme().padding_wide();
let list = models_state.read().clone().unwrap_or_default();
let loaded = models_state.read().is_some();
@@ -111,21 +111,19 @@ fn Models(
on_submit: move |index: usize| {
let mut state = state;
if let Some(model) = list.get(index) {
- state.write().model_state = Some(ModelState {
- model: model.clone(),
- download_state: DownloadState::not_downloaded(0),
- session_state: None,
- capabilities: ModelCapabilities::default(),
- });
- state.write().app_settings.selected_model_id = Some(model.identifier.clone());
- let settings_result = {
- let state = state.read();
- match state.settings.as_ref() {
- Some(settings) => state.app_settings.save(settings),
- None => Ok(()),
- }
+ let preferences_result = {
+ let mut app_state = state.write();
+ app_state.model_state = Some(ModelState {
+ model: model.clone(),
+ download_state: DownloadState::not_downloaded(0),
+ session_state: None,
+ capabilities: ModelCapabilities::default(),
+ });
+ let mut preferences = app_state.preferences().clone();
+ preferences.selected_model_id = Some(model.identifier.clone());
+ app_state.set_preferences(&preferences)
};
- let result = match settings_result {
+ let result = match preferences_result {
Ok(()) => format!("Model: {}", model.name()),
Err(error) => {
format!("Model: {}, unable to save preference: {}", model.name(), error)
diff --git a/crates/cli/src/interactive/flows/settings.rs b/crates/cli/src/interactive/flows/settings.rs
index 006319609..a3da60606 100644
--- a/crates/cli/src/interactive/flows/settings.rs
+++ b/crates/cli/src/interactive/flows/settings.rs
@@ -3,8 +3,9 @@ use iocraft::prelude::*;
use crate::{
common::model_capabilities::{ModelSamplingDefaults, ThinkingSupport},
interactive::{
- components::{ApplicationState, Preferences, SamplingMode, Theme, ThinkingSupportExt},
+ components::{ApplicationState, Preferences, Theme, ThinkingSupportExt},
flows::{Flow, FlowEvent},
+ sampling::SamplingMode,
},
};
@@ -211,10 +212,10 @@ fn SettingsFlowView(
let support = capabilities.thinking;
let defaults = capabilities.sampling_defaults;
- let mut draft = hooks.use_state(|| state.read().preferences);
+ let mut draft = hooks.use_state(|| state.read().preferences().clone());
let mut selected_index = hooks.use_state(|| 0usize);
- let preferences = draft.get();
+ let preferences = draft.read().clone();
let fields = visible_fields(&preferences, support);
if selected_index.get() >= fields.len() {
selected_index.set(fields.len().saturating_sub(1));
@@ -233,7 +234,7 @@ fn SettingsFlowView(
return;
}
- let preferences = draft.get();
+ let preferences = draft.read().clone();
let fields = visible_fields(&preferences, support);
if fields.is_empty() {
return;
@@ -259,43 +260,39 @@ fn SettingsFlowView(
selected_index.set(next);
},
KeyCode::Left => {
- let mut preferences = draft.get();
+ let mut preferences = draft.read().clone();
adjust(&mut preferences, field, -1, support);
draft.set(preferences);
},
KeyCode::Right => {
- let mut preferences = draft.get();
+ let mut preferences = draft.read().clone();
adjust(&mut preferences, field, 1, support);
draft.set(preferences);
},
KeyCode::Char(' ') => {
- let mut preferences = draft.get();
+ let mut preferences = draft.read().clone();
toggle(&mut preferences, field, support, defaults);
draft.set(preferences);
},
KeyCode::Enter => {
let mut state = state;
- let preferences = draft.get();
+ let preferences = draft.read().clone();
let summary = format!(
"thinking {} · sampling {}",
thinking_summary(support, &preferences),
preferences.sampling.summary()
);
- let result = match state.read().settings.clone() {
- Some(settings) => match preferences.save(&settings) {
- Ok(()) => format!("Settings saved ({})", summary),
- Err(error) => format!("Settings applied ({}), unable to save: {}", summary, error),
- },
- None => format!("Settings applied ({})", summary),
+ let result = match state.write().set_preferences(&preferences) {
+ Ok(()) => format!("Settings saved ({})", summary),
+ Err(error) => format!("Settings applied ({}), unable to save: {}", summary, error),
};
- state.write().preferences = preferences;
on_event(FlowEvent::finish(result));
},
_ => {},
}
});
- let theme = state.read().theme.clone();
+ let theme = state.read().theme().clone();
let selected = fields.get(selected_index.get().min(fields.len().saturating_sub(1))).copied();
let padding = theme.padding();
diff --git a/crates/cli/src/interactive/flows/theme.rs b/crates/cli/src/interactive/flows/theme.rs
index 164b22ece..77aae534e 100644
--- a/crates/cli/src/interactive/flows/theme.rs
+++ b/crates/cli/src/interactive/flows/theme.rs
@@ -44,18 +44,13 @@ fn ThemeFlowView(
items: items,
style: SelectorStyle::Plain,
maximal_height: (themes.len() as u16).min(5),
- accent_color: state.read().theme.accent_color,
- subtitle_color: state.read().theme.subtitle_color,
- columns_padding: state.read().theme.padding_wide(),
+ accent_color: state.read().theme().accent_color,
+ subtitle_color: state.read().theme().subtitle_color,
+ columns_padding: state.read().theme().padding_wide(),
on_submit: move |index: usize| {
let mut state = state;
if let Some(theme) = themes.get(index) {
- let settings_result = match state.read().settings.as_ref() {
- Some(settings) => theme.save(settings),
- None => Ok(()),
- };
- state.write().theme = theme.clone();
- let result = match settings_result {
+ let result = match state.write().set_theme(theme.clone()) {
Ok(()) => format!("Theme set to {}", theme.name),
Err(error) => format!("Theme set to {}, unable to save preference: {}", theme.name, error),
};
diff --git a/crates/cli/src/interactive/mod.rs b/crates/cli/src/interactive/mod.rs
index dffd62450..a8f1e0695 100644
--- a/crates/cli/src/interactive/mod.rs
+++ b/crates/cli/src/interactive/mod.rs
@@ -16,7 +16,9 @@ mod flows;
mod helpers;
mod list;
mod model;
+mod sampling;
mod sessions;
+mod util;
const APP_IDENTIFIER: &str = "com.trymirai.cli";
diff --git a/crates/cli/src/interactive/sampling.rs b/crates/cli/src/interactive/sampling.rs
new file mode 100644
index 000000000..243fd676f
--- /dev/null
+++ b/crates/cli/src/interactive/sampling.rs
@@ -0,0 +1,114 @@
+use rocket::serde::{Deserialize, Serialize};
+use shoji::types::basic::{SamplingMethod, SamplingPolicy};
+
+use crate::interactive::util::cycle;
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
+#[serde(rename_all = "snake_case")]
+pub enum SamplingMode {
+ #[default]
+ ModelDefault,
+ Greedy,
+ Stochastic,
+}
+
+impl SamplingMode {
+ pub const ALL: [SamplingMode; 3] = [Self::ModelDefault, Self::Greedy, Self::Stochastic];
+
+ pub fn label(&self) -> &'static str {
+ match self {
+ Self::ModelDefault => "model default",
+ Self::Greedy => "greedy",
+ Self::Stochastic => "stochastic",
+ }
+ }
+
+ pub fn next(self) -> Self {
+ cycle(&Self::ALL, self, 1)
+ }
+
+ pub fn previous(self) -> Self {
+ cycle(&Self::ALL, self, -1)
+ }
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Default)]
+#[serde(default)]
+pub struct SamplingPreferences {
+ pub mode: SamplingMode,
+ pub temperature_enabled: bool,
+ pub temperature: f64,
+ pub top_k_enabled: bool,
+ pub top_k: i64,
+ pub top_p_enabled: bool,
+ pub top_p: f64,
+ pub min_p_enabled: bool,
+ pub min_p: f64,
+ pub repetition_penalty_enabled: bool,
+ pub repetition_penalty: f64,
+ pub suffix_repetition_length: i64,
+}
+
+impl SamplingPreferences {
+ pub fn suffix_repetition_length_enabled(&self) -> bool {
+ self.repetition_penalty_enabled
+ }
+
+ pub fn suffix_repetition_length_disable(&mut self) {
+ self.repetition_penalty_enabled = false;
+ }
+
+ pub fn policy(&self) -> SamplingPolicy {
+ match self.mode {
+ SamplingMode::ModelDefault => SamplingPolicy::Default {},
+ SamplingMode::Greedy => SamplingPolicy::Custom {
+ method: SamplingMethod::Greedy {},
+ },
+ SamplingMode::Stochastic => SamplingPolicy::Custom {
+ method: SamplingMethod::Stochastic {
+ temperature: self.temperature_enabled.then_some(self.temperature),
+ top_k: self.top_k_enabled.then_some(self.top_k),
+ top_p: self.top_p_enabled.then_some(self.top_p),
+ min_p: self.min_p_enabled.then_some(self.min_p),
+ repetition_penalty: self.repetition_penalty_enabled.then_some(self.repetition_penalty),
+ suffix_repetition_length: self
+ .suffix_repetition_length_enabled()
+ .then_some(self.suffix_repetition_length),
+ },
+ },
+ }
+ }
+
+ pub fn summary(&self) -> String {
+ match self.mode {
+ SamplingMode::ModelDefault => "default".to_string(),
+ SamplingMode::Greedy => "greedy".to_string(),
+ SamplingMode::Stochastic => {
+ let mut parts = Vec::new();
+ if self.temperature_enabled {
+ parts.push(format!("temp {:.2}", self.temperature));
+ }
+ if self.top_k_enabled {
+ parts.push(format!("top-k {}", self.top_k));
+ }
+ if self.top_p_enabled {
+ parts.push(format!("top-p {:.2}", self.top_p));
+ }
+ if self.min_p_enabled {
+ parts.push(format!("min-p {:.2}", self.min_p));
+ }
+ if self.repetition_penalty_enabled {
+ parts.push(format!("repetition penalty {:.2}", self.repetition_penalty));
+ }
+ if self.suffix_repetition_length_enabled() {
+ parts.push(format!("suffix repetition length {:.2}", self.suffix_repetition_length));
+ }
+ if parts.is_empty() {
+ "stochastic".to_string()
+ } else {
+ parts.join(", ")
+ }
+ },
+ }
+ }
+}
diff --git a/crates/cli/src/interactive/sessions/chat.rs b/crates/cli/src/interactive/sessions/chat.rs
index ab20f2ee7..60331bb17 100644
--- a/crates/cli/src/interactive/sessions/chat.rs
+++ b/crates/cli/src/interactive/sessions/chat.rs
@@ -155,19 +155,19 @@ pub async fn run_session(
text: String,
) {
let mut state = state;
- let preferences = state.read().preferences;
let thinking_support =
state.read().model_state.as_ref().map(|model_state| model_state.capabilities.thinking).unwrap_or_default();
let user_message = ChatMessage::user().with_text(text);
let mut messages = vec![user_message];
- if let Some(reasoning_effort) = thinking_support.with_preference(&preferences.thinking).reasoning_effort()
+ if let Some(reasoning_effort) =
+ thinking_support.with_preference(&state.read().preferences().thinking).reasoning_effort()
&& session.messages().await.is_empty()
{
let system_message = ChatMessage::system().with_reasoning_effort(reasoning_effort);
messages.insert(0, system_message);
};
- let reply_config = ChatReplyConfig::default().with_sampling_policy(preferences.sampling_policy());
+ let reply_config = ChatReplyConfig::default().with_sampling_policy(state.read().preferences().sampling.policy());
{
let mut state = state.write();
if let Some(chat_state) = chat_state_mut(&mut state) {
diff --git a/crates/cli/src/interactive/util.rs b/crates/cli/src/interactive/util.rs
new file mode 100644
index 000000000..4d0ce1d27
--- /dev/null
+++ b/crates/cli/src/interactive/util.rs
@@ -0,0 +1,10 @@
+pub fn cycle<T: Copy + PartialEq>(
+ values: &[T],
+ current: T,
+ delta: isize,
+) -> T {
+ let length = values.len() as isize;
+ let index = values.iter().position(|value| *value == current).map(|index| index as isize).unwrap_or(0);
+ let next = (index + delta).rem_euclid(length) as usize;
+ values[next]
+}