Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion crates/assistant/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ all the main assistant sees. Don't ask questions; where something is unclear, ma
/// This is not a whole-conversation/run allowance; [`Config::pause_after`] separately pauses a user run.
pub const MAX_RETRIES: u32 = 3;

/// What an [`Entry::Tool`] holds as its result when the user declined the call: it is marked as
/// failed, and this tells it from one that was tried.
pub const DECLINED: &str = "Declined";

/// What the caller does next.
#[derive(Debug, PartialEq)]
pub enum Step {
Expand Down Expand Up @@ -1095,7 +1099,7 @@ impl Agent {
} else {
("The user declined this action. Don't retry it; ask what they'd like instead.".to_string(), true, Vec::new())
};
let shown = if allow { content.clone() } else { "Declined".to_string() };
let shown = if allow { content.clone() } else { DECLINED.to_string() };
let key = (call.name.clone(), call.args.to_string());
self.record(call, content, shown, error, images);
// The user declining it isn't the call failing.
Expand Down
2 changes: 1 addition & 1 deletion crates/assistant/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ pub mod skills;
mod tests;
pub mod tools;

pub use agent::{Agent, Asking, Entry, Step};
pub use agent::{Agent, Asking, DECLINED, Entry, Step};
pub use provider::{Api, Config, HttpRequest, HttpResponse, Note, PRESETS, Preset};
110 changes: 110 additions & 0 deletions crates/ui-egui/src/assistant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ pub struct AssistantState {
pub(crate) local_agents: bool,
/// The tokens a message took are left out of the conversation (shown unless the user says).
pub(crate) hide_tokens: bool,
/// Each call has a line of its own instead of sharing one ([`lines`]).
pub(crate) every_step: bool,
/// Agent threads on this computer: their processes, windows and requests (desktop).
#[cfg(not(target_arch = "wasm32"))]
pub(crate) threads: crate::agentthreads::Threads,
Expand Down Expand Up @@ -291,6 +293,20 @@ pub fn showing_tokens(app: &RustGisApp) -> bool {
!app.assistant.hide_tokens
}

/// The preference that gives each call the assistant makes a line of its own ("on").
const EVERY_STEP_PREF: &str = "assistant_show_every_step";

/// Gives each call a line of its own, or groups them ([`lines`]), and remembers it.
pub fn show_every_step(app: &mut RustGisApp, every: bool) {
app.assistant.every_step = every;
save_pref(app, EVERY_STEP_PREF, if every { "on" } else { "off" });
}

/// Whether each call the assistant makes has a line of its own.
pub fn showing_every_step(app: &RustGisApp) -> bool {
app.assistant.every_step
}

/// Saves a preference through the platform hook.
pub(crate) fn save_pref(app: &RustGisApp, key: &str, value: &str) {
if let Some(s) = &app.services.save_pref {
Expand Down Expand Up @@ -380,6 +396,7 @@ pub(crate) fn ensure_loaded(app: &mut RustGisApp, ctx: &egui::Context) {
let mut agent = Agent::new(cfg, &app.session);
app.assistant.local_agents = pref(app, "assistant_local_agents").as_deref() == Some("on");
app.assistant.hide_tokens = pref(app, TOKENS_PREF).as_deref() == Some("off");
app.assistant.every_step = pref(app, EVERY_STEP_PREF).as_deref() == Some("on");
// A pause set in the environment that isn't one is said, as the command line says it.
if let Ok(asked) = std::env::var(provider::PAUSE_ENV)
&& !asked.trim().is_empty()
Expand Down Expand Up @@ -1237,6 +1254,99 @@ pub fn plain_call(app: &RustGisApp, name: &str, summary: &str) -> String {
}
}

/// A line of the conversation as it is drawn ([`lines`]), each entry with its place in
/// [`AssistantState::messages`].
pub enum Line<'a> {
/// A message, a note, an error, or a call on a line of its own.
Entry(usize, &'a Entry),
/// Calls that share a line.
Steps(Steps<'a>),
}

/// Two or more calls that share a line, which opens to show each.
pub struct Steps<'a> {
/// The calls ([`Entry::Tool`]), oldest first.
pub calls: Vec<(usize, &'a Entry)>,
/// More may still join them: the run they are part of goes on.
pub running: bool,
}

/// The conversation as it is drawn. Unless every step is asked for ([`showing_every_step`]), the
/// calls between one message, note or error and the next share a line, which stands where the
/// latest of them was made: what the model said between them comes before it, and what it said
/// after them (its reply) comes after. Messages, notes (a pause, a stop, a retry) and errors are
/// never part of it.
pub fn lines(app: &RustGisApp) -> Vec<Line<'_>> {
let entries = app.assistant.messages();
if app.assistant.every_step {
return entries.iter().enumerate().map(|(i, e)| Line::Entry(i, e)).collect();
}
let mut lines = Vec::new();
let mut calls = Vec::new();
// What the model said since the last call: before the calls' line if another call follows.
let mut said = Vec::new();
for (i, e) in entries.iter().enumerate() {
match e {
Entry::Tool { .. } => {
lines.append(&mut said);
calls.push((i, e));
}
Entry::Assistant(_) if !calls.is_empty() => said.push(Line::Entry(i, e)),
Entry::Assistant(_) => lines.push(Line::Entry(i, e)),
Entry::User(_) | Entry::Note(_) | Entry::Error(_) => {
share_line(&mut lines, &mut calls, false);
lines.append(&mut said);
lines.push(Line::Entry(i, e));
}
}
}
share_line(&mut lines, &mut calls, app.assistant.busy());
lines.append(&mut said);
lines
}

/// Puts the calls gathered on one line of [`lines`]; one call alone keeps its own.
fn share_line<'a>(lines: &mut Vec<Line<'a>>, calls: &mut Vec<(usize, &'a Entry)>, running: bool) {
if calls.len() > 1 {
lines.push(Line::Steps(Steps { calls: std::mem::take(calls), running }));
} else {
lines.extend(calls.drain(..).map(|(i, e)| Line::Entry(i, e)));
}
}

/// What the line of calls that share one says: how many, how many of them failed or were
/// declined, and while the run goes on, the latest in words ([`plain_call`]). In display form.
pub fn steps_line(app: &RustGisApp, steps: &Steps<'_>) -> String {
use crate::i18n::fmt_raw;
let (mut failed, mut declined) = (0, 0);
for (_, e) in &steps.calls {
if let Entry::Tool { result, error: true, .. } = e {
if result == rustgis_assistant::DECLINED {
declined += 1;
} else {
failed += 1;
}
}
}
let count = steps.calls.len().to_string();
let count = [("count", count.as_str())];
// "Actions", not "steps": a step, as the pause settings count them, may make several.
let mut parts =
vec![if steps.running { fmt_raw("{count} actions so far", &count) } else { fmt_raw("Worked through {count} actions", &count) }];
if failed > 0 {
parts.push(fmt_raw("{count} failed", &[("count", &failed.to_string())]));
}
if declined > 0 {
parts.push(fmt_raw("{count} declined", &[("count", &declined.to_string())]));
}
if steps.running
&& let Some((_, Entry::Tool { name, summary, error, .. })) = steps.calls.last()
{
parts.push(format!("{} {}", if *error { "✗" } else { "✓" }, plain_call(app, name, summary)));
}
crate::i18n::display(&parts.join(" · "))
}

/// An action waiting for the user's approval.
#[derive(Clone)]
pub struct Approval {
Expand Down
Loading
Loading