Adds a new 'consult' tool that makes single tool-less calls to a configured consultant model. The feature includes: - Consult backend (consult.rs) with permission checks - Call limit (default 5 per session) tracked in State - Three nudge triggers: before exit_plan, after edits, after 3 failed tools - Side-panel card showing consultant status and call count - Config keys: consultant = "<model>" to enable; consultant_max_calls for limit - All nudges are always on; disable by unsetting the consultant config The consultant model sees the question and context (passed by itself since the tool has no context visibility). Token usage counts toward subagent quotas.
131 lines
3.5 KiB
Rust
131 lines
3.5 KiB
Rust
pub mod agent;
|
|
pub mod approve;
|
|
pub mod command;
|
|
pub mod compact;
|
|
pub mod consult;
|
|
pub mod context;
|
|
pub mod danger;
|
|
pub mod ext;
|
|
pub mod herdr;
|
|
pub mod hook;
|
|
mod mask;
|
|
pub mod memory;
|
|
pub mod nudge;
|
|
pub mod permit;
|
|
pub mod route;
|
|
pub mod sandbox;
|
|
pub mod session;
|
|
pub mod stats;
|
|
pub mod subagent;
|
|
pub mod switch;
|
|
pub mod tool;
|
|
pub mod window;
|
|
|
|
use std::collections::HashMap;
|
|
use std::path::PathBuf;
|
|
use std::sync::Mutex;
|
|
use std::time::SystemTime;
|
|
|
|
pub type Ask = std::sync::Arc<
|
|
dyn Fn(String) -> std::pin::Pin<Box<dyn std::future::Future<Output = bool> + Send>>
|
|
+ Send
|
|
+ Sync,
|
|
>;
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
pub struct Question {
|
|
pub text: String,
|
|
pub options: Vec<(String, String)>,
|
|
pub multi: bool,
|
|
pub other: bool,
|
|
pub plan: Option<String>,
|
|
}
|
|
|
|
pub type Choose = std::sync::Arc<
|
|
dyn Fn(
|
|
Question,
|
|
)
|
|
-> std::pin::Pin<Box<dyn std::future::Future<Output = Option<Vec<String>>> + Send>>
|
|
+ Send
|
|
+ Sync,
|
|
>;
|
|
|
|
#[derive(Default)]
|
|
pub struct State {
|
|
pub seen: Mutex<HashMap<PathBuf, Option<SystemTime>>>,
|
|
pub ask: std::sync::OnceLock<Ask>,
|
|
pub permit: std::sync::OnceLock<permit::Permit>,
|
|
pub judge: std::sync::OnceLock<danger::Judge>,
|
|
pub allow: Mutex<permit::Allow>,
|
|
pub asking: tokio::sync::Mutex<()>,
|
|
pub edits: Mutex<Vec<permit::FileEdit>>,
|
|
pub choose: std::sync::OnceLock<Choose>,
|
|
pub abort: std::sync::OnceLock<tokio::sync::watch::Receiver<bool>>,
|
|
pub sub_usage: Mutex<hx_ai::Usage>,
|
|
pub subs: subagent::Subs,
|
|
pub consult: std::sync::OnceLock<std::sync::Arc<consult::Stats>>,
|
|
pub live: Mutex<HashMap<String, String>>,
|
|
pub claude_seen: Mutex<std::collections::HashSet<String>>,
|
|
pub lens: Mutex<LensView>,
|
|
pub nested: Mutex<Option<tokio::sync::mpsc::UnboundedSender<String>>>,
|
|
pub codemode_store: Mutex<std::collections::BTreeMap<String, serde_json::Value>>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Default, PartialEq)]
|
|
pub struct LensView {
|
|
pub diags: Vec<Diag>,
|
|
pub tests: Option<Vec<(String, bool)>>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
pub struct Diag {
|
|
pub file: String,
|
|
pub line: u32,
|
|
pub col: u32,
|
|
pub error: bool,
|
|
pub message: String,
|
|
}
|
|
|
|
impl State {
|
|
pub async fn confirm(&self, question: String) -> bool {
|
|
match self.ask.get() {
|
|
Some(a) => a(question).await,
|
|
None => false,
|
|
}
|
|
}
|
|
|
|
pub async fn choose(&self, q: Question) -> Result<Option<Vec<String>>, &'static str> {
|
|
match self.choose.get() {
|
|
Some(c) => Ok(c(q).await),
|
|
None => Err("no interactive user attached; decide yourself or state your assumption"),
|
|
}
|
|
}
|
|
|
|
pub fn log_edit(&self, p: &std::path::Path, old: Option<String>, new: &str) {
|
|
self.edits
|
|
.lock()
|
|
.unwrap_or_else(|e| e.into_inner())
|
|
.push(permit::FileEdit {
|
|
path: p.to_path_buf(),
|
|
old,
|
|
new: new.into(),
|
|
});
|
|
}
|
|
|
|
pub fn mark_seen(&self, p: &std::path::Path) {
|
|
let m = std::fs::metadata(p).and_then(|m| m.modified()).ok();
|
|
self.seen
|
|
.lock()
|
|
.unwrap_or_else(|e| e.into_inner())
|
|
.insert(p.to_path_buf(), m);
|
|
}
|
|
pub fn is_fresh(&self, p: &std::path::Path) -> bool {
|
|
let now = std::fs::metadata(p).and_then(|m| m.modified()).ok();
|
|
self.seen
|
|
.lock()
|
|
.unwrap_or_else(|e| e.into_inner())
|
|
.get(p)
|
|
.is_some_and(|m| *m == now)
|
|
}
|
|
}
|