capri-computer: add access module
This commit is contained in:
1 file changed
+143
@@ -0,0 +1,143 @@
|
||||
//! Full computer access, the Application-access pause and indicator appearance.
|
||||
//! A model can request, never grant: only the owner's confirmation enables it.
|
||||
use std::time::Duration;
|
||||
|
||||
use capri_agent::tools::{Approval, Approvals};
|
||||
use capri_core::{Section, SettingsStore};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{json, Value};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
|
||||
#[serde(default)]
|
||||
pub struct FullComputerAccess {
|
||||
pub enabled: bool,
|
||||
/// Linux: exactly `["/"]` when enabled.
|
||||
pub drives: Vec<String>,
|
||||
}
|
||||
|
||||
impl Section for FullComputerAccess {
|
||||
const NAME: &'static str = "fullComputerAccess";
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
||||
#[serde(default)]
|
||||
pub struct ApplicationAccess {
|
||||
pub enabled: bool,
|
||||
}
|
||||
|
||||
impl Default for ApplicationAccess {
|
||||
fn default() -> Self {
|
||||
Self { enabled: true }
|
||||
}
|
||||
}
|
||||
|
||||
impl Section for ApplicationAccess {
|
||||
const NAME: &'static str = "applicationAccess";
|
||||
}
|
||||
|
||||
/// Indicator appearance (`computer-control.json`).
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct ComputerAppearance {
|
||||
/// Decorative edge effects (the border and caption are always shown).
|
||||
pub enabled: bool,
|
||||
pub intensity: u8,
|
||||
/// `capri` or `blackwall`.
|
||||
pub theme: String,
|
||||
pub cursor: bool,
|
||||
/// Pointer animation 0..=1000 ms.
|
||||
pub pointer_duration: u16,
|
||||
}
|
||||
|
||||
impl Default for ComputerAppearance {
|
||||
fn default() -> Self {
|
||||
Self { enabled: true, intensity: 50, theme: "capri".into(), cursor: true, pointer_duration: 260 }
|
||||
}
|
||||
}
|
||||
|
||||
impl Section for ComputerAppearance {
|
||||
const NAME: &'static str = "computerControl";
|
||||
}
|
||||
|
||||
/// Effective permission: full access granted and application access not paused.
|
||||
pub fn allowed(store: &SettingsStore) -> bool {
|
||||
let full: FullComputerAccess = store.section();
|
||||
full.enabled && full.drives == ["/"] && store.section::<ApplicationAccess>().enabled
|
||||
}
|
||||
|
||||
/// File-access scopes provided by full access (consumed by the file tools).
|
||||
pub fn scopes(store: &SettingsStore) -> Vec<Value> {
|
||||
let full: FullComputerAccess = store.section();
|
||||
if !full.enabled {
|
||||
return vec![];
|
||||
}
|
||||
full.drives.iter().map(|root| json!({"id": format!("fa11acc0-0000-4000-8000-{}", &hex::encode(Sha256::digest(root))[..12]), "root": root, "write": true, "kind": "computer", "confirmationRequired": false})).collect()
|
||||
}
|
||||
|
||||
pub const CONFIRM_TITLE: &str = "Give access to the whole computer?";
|
||||
pub const CONFIRM_DETAIL: &str = "Capri will be able to use supported file, application, keyboard and mouse actions on your local drives without repeated confirmation. Requested actions can change your files and applications. You can revoke this access here at any time. Linux file permissions and separate remote-provider settings still apply.";
|
||||
/// Approval kind: the UI must show a danger card whose confirm button ("Give full computer
|
||||
/// access") cannot be accepted before 3 s.
|
||||
pub const CONFIRM_KIND: &str = "settings.fullComputerAccess";
|
||||
|
||||
/// `ComputerRequests.configure`: enabling asks the owner (180 s); disabling is immediate.
|
||||
pub async fn configure(store: &SettingsStore, approvals: &dyn Approvals, enabled: bool, ru: bool) -> Value {
|
||||
let current: FullComputerAccess = store.section();
|
||||
let (applied, cancelled) = if !enabled {
|
||||
let ok = store.set_section(&FullComputerAccess { enabled: false, ..current }).is_ok();
|
||||
(ok, false)
|
||||
} else if current.enabled {
|
||||
(true, false)
|
||||
} else {
|
||||
let accepted = approvals.confirm(Approval { kind: CONFIRM_KIND.into(), title: CONFIRM_TITLE.into(), detail: CONFIRM_DETAIL.into(), timeout: Duration::from_secs(180) }).await;
|
||||
let ok = accepted && store.set_section(&FullComputerAccess { enabled: true, drives: vec!["/".into()] }).is_ok();
|
||||
(ok, !accepted)
|
||||
};
|
||||
let now_enabled = store.section::<FullComputerAccess>().enabled;
|
||||
let confirmation = match (cancelled, now_enabled, ru) {
|
||||
(true, _, false) => "Cancelled.",
|
||||
(true, _, true) => "Отменено.",
|
||||
(false, true, false) => "Full computer access is enabled.",
|
||||
(false, true, true) => "Полный доступ к компьютеру включён.",
|
||||
(false, false, false) => "Full computer access is off.",
|
||||
(false, false, true) => "Полный доступ к компьютеру выключен.",
|
||||
};
|
||||
let instruction = if applied { "Report this actual access state. If a computer task was requested, pass the complete original goal to desktop_control run_task. Access alone does not complete the task." } else { "The owner did not grant access. Stop this task; do not repeat the request or switch to another tool." };
|
||||
json!({"ok": applied, "enabled": now_enabled, "cancelled": cancelled, "status": if cancelled { "cancelled" } else { "completed" }, "steps": 0, "confirmation": confirmation, "summary": confirmation, "instruction": instruction})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use async_trait::async_trait;
|
||||
|
||||
struct Yes(bool);
|
||||
#[async_trait]
|
||||
impl Approvals for Yes {
|
||||
async fn confirm(&self, a: Approval) -> bool {
|
||||
assert_eq!(a.kind, CONFIRM_KIND);
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn grant_flow() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let paths = capri_core::Paths::under(dir.path().into());
|
||||
paths.ensure().unwrap();
|
||||
let store = SettingsStore::new(paths, Default::default());
|
||||
assert!(!allowed(&store));
|
||||
let r = configure(&store, &Yes(false), true, false).await;
|
||||
assert_eq!((r["ok"].clone(), r["status"].clone()), (json!(false), json!("cancelled")));
|
||||
assert!(!allowed(&store), "a declined grant never enables");
|
||||
let r = configure(&store, &Yes(true), true, false).await;
|
||||
assert_eq!(r["confirmation"], "Full computer access is enabled.");
|
||||
assert!(allowed(&store));
|
||||
assert_eq!(scopes(&store)[0]["root"], "/");
|
||||
store.set_section(&ApplicationAccess { enabled: false }).unwrap();
|
||||
assert!(!allowed(&store), "the pause masks the grant");
|
||||
let r = configure(&store, &Yes(false), false, true).await;
|
||||
assert_eq!(r["confirmation"], "Полный доступ к компьютеру выключен.");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user