use crate::AppModel;
use crate::actions::DraftKey;
use crate::cli_outbound::Cli;
use crate::git_tree::Agent;
use crate::names::SplitMix64;
use crate::nav::convs::{display_name_of, root_of};
use crate::start;
use std::path::PathBuf;
use super::ShellState;
use super::inbox_queue;
use super::verb_row::{VerbCtx, verb_buttons};
pub fn composer(
ui: &mut egui::Ui,
model: &mut AppModel,
state: &mut ShellState,
lernie: &Cli,
bl: &Cli,
cap: f32,
) {
let Some(ws) = model.focused_workspace().map(PathBuf::from) else {
return;
};
let agents: Vec<Agent> = model
.focused_tree()
.map(|t| t.agents.clone())
.unwrap_or_default();
state.actions.selected_branch = model.focused_agent().map(|a| a.agent_id.clone());
let target = state.actions.selected_branch.clone();
let mint_seed = state.start.mint_seed;
let conv_name = target.as_deref().map(|agent| {
let root = root_of(&agents, agent).unwrap_or_else(|| agent.to_owned());
display_name_of(&agents, &root)
});
if conv_name.is_none() {
ui.weak(
start::preview(
&model.start_bare_inputs(),
&mut SplitMix64::from_seed(mint_seed),
)
.preview,
);
}
let key = DraftKey::composer(Some(ws.clone()), target.clone());
let pending = model
.focused_agent()
.map(|a| a.pending.clone())
.unwrap_or_default();
let prompts = model
.focused_agent()
.map(|agent| {
let in_flight = agent.state == crate::git_tree::AgentState::InFlight;
let tx = super::inspector::build_transcript(
model,
&mut state.inspector,
&ws,
&agent.agent_id,
in_flight,
);
crate::composer::prompts(&pending, &tx)
})
.unwrap_or_default();
let queue = inbox_queue::QueueCtx {
key: key.clone(),
agent_id: target.clone(),
pending,
prompts,
hint: match conv_name.as_deref() {
Some(name) => format!("message {name}"),
None => "start a conversation".to_owned(),
},
cap,
};
let before = state.actions.drafts.text(&key);
let edit = inbox_queue::region(ui, &mut state.composer, &mut state.actions.drafts, &queue);
if state.actions.drafts.text(&key) != before {
state.slash = None;
}
super::focus::take(state, ui, &edit);
let entered =
edit.has_focus() && ui.input(|i| i.modifiers.is_none() && i.key_pressed(egui::Key::Enter));
let ctx = VerbCtx {
ws,
agents,
key: key.clone(),
text: state.actions.drafts.text(&key),
conv_name,
entered,
};
verb_buttons(ui, model, state, lernie, bl, &ctx);
super::slash::note_ui(ui, state);
let join = model.focused_join().cloned();
super::ball_bar::actions(ui, model, lernie, bl, join.as_ref());
if let Some(failure) = model.last_failure(crate::opslog::Origin::Conversation) {
super::banner::failure_banner(ui, model, &failure);
}
}