capri-agent: add tools module
This commit is contained in:
1 file changed
+96
@@ -0,0 +1,96 @@
|
||||
//! Contract between the conversation engine and the tools offered to the model.
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use serde_json::{json, Value};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
/// A function the model can call. Implementations live in feature crates.
|
||||
#[async_trait]
|
||||
pub trait Tool: Send + Sync {
|
||||
/// Name exposed to the model, e.g. `filesystem_control`.
|
||||
fn name(&self) -> &str;
|
||||
/// Description exposed to the model (Capri wording, Linux adapted).
|
||||
fn description(&self) -> String;
|
||||
/// JSON Schema of the arguments object. Keep optional parameters few: some providers
|
||||
/// reject schemas with more than 24 optional parameters.
|
||||
fn parameters(&self) -> Value;
|
||||
/// Whether the tool is currently offered (feature enabled, permission granted...).
|
||||
fn available(&self) -> bool {
|
||||
true
|
||||
}
|
||||
/// Per-call deadline; the engine cancels `ctx.cancel` and reports a timeout after it.
|
||||
fn timeout(&self) -> Duration {
|
||||
Duration::from_secs(15)
|
||||
}
|
||||
/// Runs one call. The result is a JSON object with `ok: bool`, plus `error` when not ok,
|
||||
/// `needsApproval: true` when waiting for the owner, `cancelled: true` when refused.
|
||||
/// Text read from files, windows or the web must be returned as data, never as instructions.
|
||||
async fn call(&self, args: Value, ctx: ToolContext) -> Value;
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ToolContext {
|
||||
pub turn_id: String,
|
||||
pub call_id: String,
|
||||
pub approvals: Arc<dyn Approvals>,
|
||||
pub cancel: CancellationToken,
|
||||
}
|
||||
|
||||
/// An on-screen confirmation the owner must accept (permission grant, file outside the
|
||||
/// installation, text entry...). Capri shows an exact, temporary approval card.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Approval {
|
||||
/// Stable kind, e.g. `file.trash`, `settings.fullComputerAccess`, `desktop.type_text`.
|
||||
pub kind: String,
|
||||
/// Short question shown as the card title.
|
||||
pub title: String,
|
||||
/// Exact details of what will happen.
|
||||
pub detail: String,
|
||||
/// The card expires and counts as refused after this.
|
||||
pub timeout: Duration,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait Approvals: Send + Sync {
|
||||
/// Returns true only when the owner explicitly accepted before the timeout.
|
||||
async fn confirm(&self, request: Approval) -> bool;
|
||||
}
|
||||
|
||||
/// Refuses everything (terminal chat, tests).
|
||||
pub struct DenyAll;
|
||||
|
||||
#[async_trait]
|
||||
impl Approvals for DenyAll {
|
||||
async fn confirm(&self, _request: Approval) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Live values merged into the per-turn `DESKTOP STATE FOR THIS TURN` envelope,
|
||||
/// e.g. `{"microphone": true}`. Never put secrets or file contents here.
|
||||
pub type StateProvider = Arc<dyn Fn() -> Value + Send + Sync>;
|
||||
|
||||
pub fn ok(data: Value) -> Value {
|
||||
let mut value = json!({ "ok": true });
|
||||
if let (Some(target), Value::Object(extra)) = (value.as_object_mut(), data) {
|
||||
target.extend(extra);
|
||||
}
|
||||
value
|
||||
}
|
||||
|
||||
pub fn error(message: impl Into<String>) -> Value {
|
||||
json!({ "ok": false, "error": message.into() })
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn helpers() {
|
||||
assert_eq!(ok(json!({"n": 1})), json!({"ok": true, "n": 1}));
|
||||
assert_eq!(error("no"), json!({"ok": false, "error": "no"}));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user