use crate::config_edit::RealFileIo;
use crate::config_edit::branch::config_file;
use crate::config_edit::branch::edit::EditOrigin;
use crate::config_edit::brazen::{
BrazenPaths, BzRunner, RealBzRunner, credential_presence, row_views,
};
use crate::config_edit::lernie_global::LernieGlobal;
use crate::model_pick::{BRANCH, PROVIDERS, Pick};
use crate::world::marks;
use std::path::Path;
use super::dispatch::Deps;
use super::reply::Reply;
mod file;
pub use file::ConfigFile;
pub(crate) mod read;
pub(crate) mod write;
use file::located;
use read::{branch_text, text_at};
use write::{cadence_path, commit, editor_at, saved};
pub(super) fn apply(deps: &Deps, ts: &str, file: &ConfigFile, text: &str) -> Result<Reply, String> {
let ws = &located(deps, file)?;
match file {
ConfigFile::Brazen { .. } => write::brazen(deps, ws, text),
ConfigFile::LernieModels => {
write::write_file(deps, LernieGlobal::resolve(&deps.world).models(), text)
}
ConfigFile::LernieWorkflow { name } => {
let path = LernieGlobal::resolve(&deps.world)
.new_workflow(name)
.map_err(|e| e.to_string())?;
write::write_file(deps, path, text)
}
ConfigFile::Cadence => write::write_file(deps, cadence_path(&deps.world), text),
ConfigFile::Branch {
lineage,
origin,
path,
..
} => commit(deps, ts, ws, lineage, origin, path, text),
}
}
pub(super) fn read(deps: &Deps, file: &ConfigFile) -> Result<Reply, String> {
let ws = &located(deps, file)?;
let text = match file {
ConfigFile::Brazen { .. } => text_at(&brazen_paths(deps, ws).config)?,
ConfigFile::LernieModels => text_at(&LernieGlobal::resolve(&deps.world).models())?,
ConfigFile::LernieWorkflow { name } => text_at(
&LernieGlobal::resolve(&deps.world)
.new_workflow(name)
.map_err(|e| e.to_string())?,
)?,
ConfigFile::Cadence => text_at(&cadence_path(&deps.world))?,
ConfigFile::Branch { lineage, path, .. } => branch_text(ws, lineage, path)?,
};
Ok(Reply::Config { text })
}
pub(super) fn lineages(workspace: &Path) -> Result<Reply, String> {
read::browse(workspace).map(Reply::Lineages)
}
pub(super) fn models(deps: &Deps, workspace: &Path, provider: &str) -> Result<Reply, String> {
read::models(&wall_env(deps, workspace), provider).map(Reply::Models)
}
pub(super) fn read_marks(deps: &Deps, workspace: &Path) -> Reply {
Reply::Marks {
branch: marks::read(&deps.world, workspace).branch(),
}
}
pub(super) fn providers(deps: &Deps, workspace: &Path) -> Reply {
let wall = wall_env(deps, workspace);
let rows = RealBzRunner::resolve(&wall).providers();
let dir = BrazenPaths::of(&wall)
.map(|p| p.credentials_dir)
.unwrap_or_default();
let creds = credential_presence(&dir, &rows, &RealFileIo);
Reply::Providers(row_views(&rows, &creds))
}
pub(super) fn brazen_paths(deps: &Deps, workspace: &Path) -> BrazenPaths {
BrazenPaths::in_wall(&crate::world::wall::root_of(&deps.world, workspace))
}
pub(super) fn wall_env(deps: &Deps, workspace: &Path) -> crate::xdg::Env {
crate::world::wall::env(&deps.world, workspace)
}
pub(super) fn set_marks(
deps: &Deps,
ts: &str,
workspace: &Path,
branch: &str,
) -> Result<Reply, String> {
let space = marks::read(&deps.world, workspace);
let landed = marks::apply(&space, &deps.state_root, ts, branch).map_err(|e| e.to_string())?;
Ok(Reply::Marks { branch: landed })
}
pub(super) fn pick_model(
deps: &Deps,
ts: &str,
workspace: &Path,
pick: &Pick,
) -> Result<Reply, String> {
let assigned = config_file(workspace, &format!("config/{BRANCH}"), PROVIDERS)
.map(|b| String::from_utf8_lossy(&b).into_owned())
.map_err(|e| e.to_string())?;
let mut editor = editor_at(&LernieGlobal::resolve(&deps.world).models())?;
let rows = crate::config_edit::brazen::row_names(
&crate::config_edit::brazen::RealBzRunner::resolve(&wall_env(deps, workspace)).providers(),
);
let planned = crate::model_pick::plan(
editor.draft(),
&assigned,
&rows,
pick,
served_window(deps, workspace, pick),
)
.map_err(|e| e.to_string())?;
if let Some(text) = planned.models_yaml {
editor.set_draft(text);
saved(editor.apply(&rows, &RealFileIo))?;
}
commit(
deps,
ts,
workspace,
BRANCH,
&EditOrigin::Advance,
PROVIDERS,
&planned.providers_yaml,
)
}
fn served_window(deps: &Deps, workspace: &Path, pick: &Pick) -> Option<u32> {
let cached = crate::config_edit::brazen::model_cache_at(
&brazen_paths(deps, workspace).models_cache_dir,
&pick.provider,
&RealFileIo,
)
.ok()
.flatten()?;
crate::model_pick::query::served_window(&cached, &pick.model)
}
#[cfg(test)]
mod tests;