use std::time::Duration;
use aither_core::llm::tool::ApprovalRequest;
use aither_core::llm::{CallIdentity, ToolResult};
use crate::context::ContextBlock;
use crate::event::RunId;
#[derive(Debug)]
#[non_exhaustive]
pub struct RequestCx<'a> {
pub run: &'a RunId,
pub turn: usize,
pub message_count: usize,
}
#[derive(Debug)]
#[non_exhaustive]
pub struct ToolInvocation<'a> {
pub call: &'a CallIdentity,
pub name: &'a str,
pub arguments: &'a serde_json::Value,
pub turn: usize,
pub message_count: usize,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum ToolDecision {
Allow,
Deny(String),
Rewrite(serde_json::Value),
Ask(ApprovalRequest),
Abort(String),
}
#[derive(Debug)]
#[non_exhaustive]
pub struct ToolOutcome {
pub call: CallIdentity,
pub name: String,
pub arguments: serde_json::Value,
pub result: Result<ToolResult, aither_core::Error>,
pub duration: Duration,
}
impl ToolOutcome {
#[must_use]
pub fn for_model(&self) -> ToolResult {
match &self.result {
Ok(result) => result.clone(),
Err(error) => ToolResult::error(format!("Error: {error}")),
}
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum AfterTool {
Keep,
Replace(ToolResult),
Append(String),
Abort(String),
}
#[derive(Debug)]
#[non_exhaustive]
pub struct StopCx<'a> {
pub final_text: &'a str,
pub turn: usize,
pub message_count: usize,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum StopDecision {
End,
Continue {
reason: String,
},
}
#[derive(Debug)]
#[non_exhaustive]
pub struct TurnBoundaryContext<'a> {
pub assistant_text: &'a str,
pub turn: usize,
pub message_count: usize,
pub outcomes: &'a [ToolOutcome],
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum TurnBoundaryAction {
Continue,
EndTurn,
}
pub trait Hooks: Send + Sync + 'static {
fn before_request(
&self,
cx: &RequestCx<'_>,
) -> impl std::future::Future<Output = Vec<ContextBlock>> + Send {
let _ = cx;
async { Vec::new() }
}
fn before_tool(
&self,
call: &ToolInvocation<'_>,
) -> impl std::future::Future<Output = ToolDecision> + Send {
let _ = call;
async { ToolDecision::Allow }
}
fn after_tool(
&self,
outcome: &ToolOutcome,
) -> impl std::future::Future<Output = AfterTool> + Send {
let _ = outcome;
async { AfterTool::Keep }
}
fn on_stop(&self, cx: &StopCx<'_>) -> impl std::future::Future<Output = StopDecision> + Send {
let _ = cx;
async { StopDecision::End }
}
fn on_turn_boundary(
&self,
cx: &TurnBoundaryContext<'_>,
) -> impl std::future::Future<Output = TurnBoundaryAction> + Send {
let _ = cx;
async { TurnBoundaryAction::Continue }
}
}
impl Hooks for () {}