use crate::config_edit::RealFileIo;
use crate::config_edit::brazen::{
BUILT_IN_ROWS_HINT, BrazenEditor, BrazenPaths, BzRunner, ProviderRow, RealBzRunner,
};
use crate::keymap::CenterTab;
use crate::theme;
use crate::xdg::Env;
use super::RELOAD_HINT;
use super::status::{describe_applied, reload_status, status_line};
const NO_WALL_HINT: &str = "focus a workspace to edit its providers — brazen's config, sign-ins and \
model cache live inside a workspace, not on the machine";
pub struct BrazenPane {
io: RealFileIo,
bz: RealBzRunner,
pub(crate) editor: Option<BrazenEditor>,
pub(crate) status: String,
effective: String,
pub(crate) providers: Vec<ProviderRow>,
}
impl BrazenPane {
pub fn new(wall: &Env) -> Self {
let io = RealFileIo;
Self {
io,
bz: RealBzRunner::resolve(wall),
editor: BrazenPaths::of(wall).and_then(|paths| BrazenEditor::load(paths, &io).ok()),
status: String::new(),
effective: String::new(),
providers: Vec::new(),
}
}
pub(crate) fn open(&mut self) {
if let Some(editor) = self.editor.as_mut() {
let _ = editor.refresh(&self.io);
}
self.providers = self.bz.providers();
}
}
pub(crate) fn render(ui: &mut egui::Ui, pane: &mut BrazenPane) -> Option<CenterTab> {
ui.heading(egui::RichText::new("brazen config.toml").color(theme::integration_hue("bz")));
if pane.editor.is_none() {
ui.weak(NO_WALL_HINT);
return None;
}
let route = provider_reference(ui, pane);
ui.weak(BUILT_IN_ROWS_HINT);
raw_fold(ui, pane);
route
}
fn raw_fold(ui: &mut egui::Ui, pane: &mut BrazenPane) {
egui::CollapsingHeader::new("raw config.toml")
.show(ui, |ui| {
let Some(editor) = pane.editor.as_mut() else {
return;
};
super::form_ui::raw_editor(ui, editor.draft_mut());
ui.horizontal(|ui| {
if ui
.button("Apply")
.on_hover_text(
"Hand this TOML to bz to validate, and write it to config.toml \
only if bz accepts it. A file changed underneath you is \
refused, not overwritten. Typed, it is `/config brazen \
<text…>`.",
)
.clicked()
{
pane.status = describe_applied(editor.apply(&pane.bz, &pane.io));
}
if ui.button("Reload").on_hover_text(RELOAD_HINT).clicked() {
pane.status = reload_status(editor.reload(&pane.io));
}
if ui
.button("Effective (bz --dump-config)")
.on_hover_text(
"Ask bz what configuration it actually ends up with once every \
layer and built-in is folded in, and print it below. Reachable \
by Tab, pressed with Space.",
)
.clicked()
{
pane.effective = editor.effective(&pane.bz).stdout;
}
});
status_line(ui, &pane.status);
if !pane.effective.is_empty() {
ui.monospace(&pane.effective);
}
})
.header_response
.on_hover_text(
"Open brazen's config.toml raw — the providers and credentials every \
model call is routed through. Editing there changes nothing until Apply, \
and bz validates it before it lands. Reachable by Tab, pressed with \
Space.",
);
}
fn provider_reference(ui: &mut egui::Ui, pane: &BrazenPane) -> Option<CenterTab> {
if pane.providers.is_empty() {
ui.weak("brazen listed no provider rows — reopen the pane to ask again");
return None;
}
ui.weak(format!(
"{} provider rows are effective in this workspace — the Login tab names them \
and states each one's credential",
pane.providers.len()
));
ui.button(CenterTab::Login.label())
.on_hover_text(CenterTab::Login.focus_hover())
.clicked()
.then_some(CenterTab::Login)
}