122 lines
3.9 KiB
Rust
122 lines
3.9 KiB
Rust
//! hx core: tools, hooks, agent loop, sessions, context.
|
|
|
|
pub mod agent;
|
|
pub mod command;
|
|
pub mod compact;
|
|
pub mod context;
|
|
pub mod ext;
|
|
pub mod hook;
|
|
mod mask;
|
|
pub mod memory;
|
|
pub mod permit;
|
|
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;
|
|
|
|
/// Asks the user a yes/no question; resolves to their answer.
|
|
pub type Ask = std::sync::Arc<
|
|
dyn Fn(String) -> std::pin::Pin<Box<dyn std::future::Future<Output = bool> + Send>>
|
|
+ Send
|
|
+ Sync,
|
|
>;
|
|
|
|
/// A multiple-choice question for the user (the `ask_user` tool).
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
pub struct Question {
|
|
pub text: String,
|
|
/// (label, description)
|
|
pub options: Vec<(String, String)>,
|
|
pub multi: bool,
|
|
/// offer a free-text "Other" row
|
|
pub other: bool,
|
|
}
|
|
|
|
/// Shows a [`Question`]; resolves to the chosen labels / typed text, None when dismissed.
|
|
pub type Choose = std::sync::Arc<
|
|
dyn Fn(
|
|
Question,
|
|
)
|
|
-> std::pin::Pin<Box<dyn std::future::Future<Output = Option<Vec<String>>> + Send>>
|
|
+ Send
|
|
+ Sync,
|
|
>;
|
|
|
|
/// Cross-tool shared state.
|
|
#[derive(Default)]
|
|
pub struct State {
|
|
/// File -> mtime when the agent last read or wrote it. Used by the read-guard.
|
|
pub seen: Mutex<HashMap<PathBuf, Option<SystemTime>>>,
|
|
/// Set by interactive frontends; None = nobody to ask (print mode, mcp-serve).
|
|
pub ask: std::sync::OnceLock<Ask>,
|
|
/// Permission prompt for bash/write/edit; unset = everything runs.
|
|
pub permit: std::sync::OnceLock<permit::Permit>,
|
|
/// Rules the user allowed for good.
|
|
pub allow: Mutex<permit::Allow>,
|
|
/// Serializes permission prompts: parallel tool calls are asked one by one.
|
|
pub asking: tokio::sync::Mutex<()>,
|
|
/// Every write/edit this session, for `/review`.
|
|
pub edits: Mutex<Vec<permit::FileEdit>>,
|
|
/// Same for multiple-choice questions.
|
|
pub choose: std::sync::OnceLock<Choose>,
|
|
/// The owning agent's abort signal (set by `Agent::new`), so subagents stop with it.
|
|
pub abort: std::sync::OnceLock<tokio::sync::watch::Receiver<bool>>,
|
|
/// Tokens spent by subagents, drained into the agent's usage after each tool round.
|
|
pub sub_usage: Mutex<hx_ai::Usage>,
|
|
/// Subagents of this session (see [`subagent`]).
|
|
pub subs: subagent::Subs,
|
|
}
|
|
|
|
impl State {
|
|
/// Ask the user; false when no interactive frontend is attached.
|
|
pub async fn confirm(&self, question: String) -> bool {
|
|
match self.ask.get() {
|
|
Some(a) => a(question).await,
|
|
None => false,
|
|
}
|
|
}
|
|
|
|
/// Err when no interactive frontend is attached; Ok(None) when the user dismissed it.
|
|
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);
|
|
}
|
|
/// True when the file was read/written by the agent and not changed on disk since.
|
|
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)
|
|
}
|
|
}
|