use crate::app::{apply_model, apply_reasoning, apply_setting, AppState};
use eclipse_core::commands::{self, Command, Front};
#[derive(Debug, Clone)]
pub struct Answer {
pub text: String,
pub refused: bool,
pub clear_session: bool,
pub yolo: Option<bool>,
}
impl Answer {
fn said(text: impl Into<String>) -> Self {
Self {
text: text.into(),
refused: false,
clear_session: false,
yolo: None,
}
}
pub(crate) fn refused(text: impl Into<String>) -> Self {
Self {
text: text.into(),
refused: true,
clear_session: false,
yolo: None,
}
}
}
pub fn bang_refusal(line: &str) -> Option<Answer> {
if !line.trim_start().starts_with('!') {
return None;
}
Some(Answer::refused(
"`!command` runs a shell in a terminal and nowhere else — nothing was run here. A `!` line \
is refused rather than sent on, because sending it would turn \"run this\" into \"ask the \
model about this\". Type it in the terminal, or send it without the `!` to ask about it.",
))
}
pub async fn serve(st: &AppState, command: &Command, arg: &str, session: Option<&str>) -> Answer {
let arg = arg.trim();
match command.name {
"/help" => Answer::said(commands::help(Front::Web)),
"/new" => Answer {
text: "a new session starts with the next message".to_string(),
refused: false,
clear_session: true,
yolo: None,
},
"/id" => Answer::said(match session.filter(|s| !s.is_empty()) {
Some(id) => id.to_string(),
None => "(none yet — the next message opens one)".to_string(),
}),
"/model" if st.agent.is_none() => Answer::refused(crate::app::unconfigured(st)),
"/model" => match arg.is_empty() {
true => Answer::said(model_readout(st)),
false => match apply_model(st, Some(arg.to_string())).await {
Ok(answer) => Answer::said(format!(
"model: {} (the next turn uses it; config.toml is not touched)",
answer["model"].as_str().unwrap_or("?")
)),
Err((_, why)) => Answer::refused(why),
},
},
"/goal" => match session.filter(|s| !s.is_empty()) {
Some(id) => {
let now = eclipse_core::session::now_ms() as i64;
match eclipse_core::goals::manage(&st.store, id, arg, now) {
Ok(said) => Answer::said(said),
Err(why) => Answer::refused(format!("{why:#}")),
}
}
None => Answer::said(
"no session yet — send a message first, then `/goal <text>` sets a standing goal \
for it",
),
},
"/loop" => match crate::loops_gateway::manage(st, session, arg) {
Ok(said) => Answer::said(said),
Err(why) => Answer::refused(why),
},
"/reasoning" => match arg.is_empty() {
true => Answer::said(reasoning_readout(st)),
false => {
let level = if arg == "default" {
None
} else {
Some(arg.to_string())
};
match apply_reasoning(st, level).await {
Ok(answer) => Answer::said(reasoning_line(&answer)),
Err((_, why)) => Answer::refused(why),
}
}
},
"/yolo" => match st.yolo.apply(arg) {
Ok(mut said) => {
let unasked = st.unasked.load(std::sync::atomic::Ordering::Relaxed);
if unasked > 0 {
said.push_str(&format!(
"\n\n{} tool call(s) have run in this dashboard without a card.",
unasked
));
}
Answer {
yolo: Some(st.yolo.on()),
..Answer::said(said)
}
}
Err(why) => Answer::refused(why),
},
"/status" => Answer::said(status_readout(st, session)),
"/settings" => Answer::said(settings_readout(st)),
"/set" => {
let Some((key, value)) = split_set(arg) else {
return Answer::refused("usage: /set <key> <value> — the keys are in /settings");
};
match apply_setting(st, key, Some(value)).await {
Ok(setting) => Answer::said(format!(
"{} = {} — written; the next run uses it",
setting["key"].as_str().unwrap_or(key),
setting["value"].as_str().unwrap_or(value)
)),
Err((_, why)) => Answer::refused(why),
}
}
"/cron" => Answer::said(cron_readout(st)),
"/compress" => match st.agent.clone() {
Some(agent) => {
agent.compress_now();
Answer::said(
"compacting: the older turns are replaced with the handoff on the next request",
)
}
None => Answer::refused(crate::app::unconfigured(st)),
},
"/archive" => {
let store = st.store.clone();
match arg {
"list" => match store.list_with_archived() {
Err(e) => Answer::refused(format!("cannot read the archive: {e}")),
Ok(listed) => {
let filed: Vec<_> = listed.iter().filter(|s| s.archived).collect();
if filed.is_empty() {
Answer::said("nothing is archived: every session is in the list")
} else {
let mut out = format!(
"{} archived session(s) — out of the list, still in the find box:",
filed.len()
);
for s in filed {
out.push_str(&format!("\n{} · {}", s.id, s.label()));
}
Answer::said(out)
}
}
},
"off" => match session.filter(|id| !id.is_empty()) {
None => Answer::refused("no session is open yet: there is nothing to put back"),
Some(id) => match store.set_archived(id, false) {
Ok(()) => Answer::said(format!(
"{id} is back in the list, and a prune may take it again"
)),
Err(e) => Answer::refused(e.to_string()),
},
},
"" => match session.filter(|id| !id.is_empty()) {
None => Answer::refused("no session is open yet: the next message opens one"),
Some(id) => match store.set_archived(id, true) {
Ok(()) => Answer::said(format!(
"{id} is out of the list and still in the find box — `/archive off` \
puts it back"
)),
Err(e) => Answer::refused(e.to_string()),
},
},
other => Answer::refused(format!("/archive takes `list` or `off`, not {other:?}")),
}
}
other => Answer::refused(format!(
"{other} has no implementation in the dashboard — that is a bug, not a refusal"
)),
}
}
fn split_set(arg: &str) -> Option<(&str, &str)> {
let (key, value) = arg.split_once(char::is_whitespace)?;
let value = value.trim();
(!key.is_empty() && !value.is_empty()).then_some((key, value))
}
fn model_readout(st: &AppState) -> String {
let current = st.current_model().unwrap_or_else(|| "(none)".into());
let configured = st.configured_model().unwrap_or_else(|| "(none)".into());
format!(
"model: {current} (configured: {configured}) — the picker in the stats row lists what the \
endpoint serves"
)
}
fn reasoning_line(answer: &serde_json::Value) -> String {
let effort = answer["effort"].as_str().unwrap_or("?");
let detail = answer["effort_detail"].as_str().unwrap_or("");
format!("effort: {effort} — {detail}")
}
fn reasoning_readout(st: &AppState) -> String {
let Some(agent) = st.agent.as_ref() else {
return crate::app::unconfigured(st);
};
let report = agent.reasoning_report();
let mut out = format!("effort: {} — {}", report.label, report.sentence());
match agent.reasoning_control() {
Some(eclipse_core::provider::Reasoning::Policy(policy)) => {
let words = policy.vocabulary();
if !words.is_empty() {
out.push_str(&format!("\nthis route takes: {}", words.join(" · ")));
}
}
Some(eclipse_core::provider::Reasoning::Off) => {
out.push_str("\nno reasoning control on this model");
}
Some(eclipse_core::provider::Reasoning::Undeclared) => {
out.push_str("\nthis backend declares no reasoning control");
}
None => {}
}
out
}
fn status_readout(st: &AppState, session: Option<&str>) -> String {
let model = st.current_model().unwrap_or_else(|| "(none)".into());
let window = st.agent.as_ref().map(|a| a.context_window()).unwrap_or(0);
let mut out = format!(
"model: {model}\ncontext window: {}",
match window {
0 => "not stated by the endpoint".to_string(),
n => format!("{n} tokens"),
}
);
if let Some(agent) = st.agent.as_ref() {
out.push_str(&format!(
"\neffort: {} — {}",
agent.reasoning_report().label,
agent.reasoning_report().sentence()
));
out.push_str(&format!("\nmax iterations: {}", agent.max_iterations()));
}
out.push_str(&format!(
"\nsession: {}",
session.filter(|s| !s.is_empty()).unwrap_or("(none yet)")
));
out
}
fn settings_readout(st: &AppState) -> String {
let cfg = match st.reload_config() {
Ok(cfg) => cfg,
Err(why) => return why,
};
let mut out = format!(
"{} key(s), profile {}\n",
cfg.settings().len(),
cfg.profile_name()
);
for s in cfg.settings() {
let shown = if s.secret {
if s.value.is_some() {
"(set)".to_string()
} else {
"(not set)".to_string()
}
} else {
s.rendered()
};
out.push_str(&format!(
"{} = {}{}\n",
s.path(),
shown,
if s.is_default() { " (default)" } else { "" }
));
}
out.push_str("\n/set <key> <value> writes one");
out
}
fn cron_readout(st: &AppState) -> String {
let Some(root) = st.cron_root.as_ref() else {
return "this dashboard was started without an install root, so it has no cron state"
.to_string();
};
let now = eclipse_core::session::now_ms() as i64;
let jobs = match eclipse_core::cron::views(root, now) {
Ok(jobs) => jobs,
Err(e) => {
return format!(
"{} could not be read: {e}",
eclipse_core::cron::jobs_path(root).display()
)
}
};
if jobs.is_empty() {
return format!(
"nothing scheduled — {} holds no jobs",
eclipse_core::cron::jobs_path(root).display()
);
}
let width = jobs.iter().map(|j| j.name.len()).max().unwrap_or(4).max(4);
let mut out = format!("{:<width$} {:<18} next", "job", "schedule");
for job in &jobs {
let next = match (job.enabled, job.next_ms) {
(false, _) => "— (disabled)".to_string(),
(true, Some(ms)) => {
let (_, _, _, h, m) = eclipse_core::cron::local_parts(ms);
format!("{h:02}:{m:02}")
}
(true, None) => "—".to_string(),
};
out.push_str(&format!(
"\n{:<width$} {:<18} {next}",
job.name, job.schedule
));
if let Some(outcome) = &job.last_outcome {
out.push_str(&format!(", last {outcome}"));
}
}
out.push_str("\nthe cron pane runs, edits and removes them");
out
}