Skip to main content

aither_agent/
workspace_request.rs

1//! Workspace request tool.
2
3use std::borrow::Cow;
4
5use aither_core::llm::tool::{Tool, ToolResult};
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8
9use crate::tool_request::{
10    ToolRequest, ToolRequestBroker, ToolRequestQueue, channel as tool_request_channel,
11};
12
13const DEFAULT_REASON: &str = include_str!("texts/request_workspace_default_reason.txt");
14
15/// Request access to a directory outside the sandbox.
16///
17/// This command asks for permission to read/write files in the specified
18/// directory. The user will be prompted to approve or deny the request.
19/// Use this when you need to modify files in the user's project directory
20/// or other locations outside your sandbox.
21#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
22pub struct RequestWorkspaceArgs {
23    /// The directory path to request access to (absolute or relative to cwd).
24    pub path: String,
25
26    /// Why access is needed (shown to user during approval prompt).
27    #[serde(default = "default_reason")]
28    pub reason: String,
29}
30
31fn default_reason() -> String {
32    DEFAULT_REASON.trim().to_string()
33}
34
35/// Result of a workspace access request.
36#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
37pub struct WorkspaceAccess {
38    /// The requested directory path.
39    pub path: String,
40    /// Reason for the request.
41    pub reason: String,
42    /// Whether access was approved.
43    pub approved: bool,
44}
45
46/// Internal request payload sent from a session-bound tool to the UI/runtime.
47#[derive(Debug, Clone, Serialize, Deserialize)]
48pub struct WorkspaceRequestPayload {
49    /// Session issuing the request.
50    pub session_id: String,
51    /// User-visible request body.
52    pub request: RequestWorkspaceArgs,
53}
54
55/// A workspace access request sent from the tool to the UI.
56pub type WorkspaceRequest = ToolRequest<WorkspaceRequestPayload, bool>;
57
58/// Broker for workspace requests (held by the tool).
59pub type WorkspaceRequestBroker = ToolRequestBroker<WorkspaceRequestPayload, bool>;
60
61/// Queue for workspace requests (held by the UI).
62pub type WorkspaceRequestQueue = ToolRequestQueue<WorkspaceRequestPayload, bool>;
63
64/// Create a new workspace request channel pair.
65#[must_use]
66pub fn channel() -> (WorkspaceRequestBroker, WorkspaceRequestQueue) {
67    tool_request_channel()
68}
69
70/// Tool for requesting workspace access.
71#[derive(Debug, Clone)]
72pub struct RequestWorkspaceTool {
73    broker: WorkspaceRequestBroker,
74    session_id: String,
75}
76
77impl RequestWorkspaceTool {
78    /// Create a new workspace request tool.
79    #[must_use]
80    pub fn new(session_id: impl Into<String>, broker: WorkspaceRequestBroker) -> Self {
81        Self {
82            broker,
83            session_id: session_id.into(),
84        }
85    }
86}
87
88impl Tool for RequestWorkspaceTool {
89    fn name(&self) -> Cow<'static, str> {
90        "request_workspace".into()
91    }
92
93    type Arguments = RequestWorkspaceArgs;
94    type Res = ToolResult;
95
96    async fn call(&self, args: Self::Arguments) -> aither_core::Result<Self::Res> {
97        let approved = self
98            .broker
99            .request(WorkspaceRequestPayload {
100                session_id: self.session_id.clone(),
101                request: args.clone(),
102            })
103            .await?;
104        let access = WorkspaceAccess {
105            path: args.path,
106            reason: args.reason,
107            approved,
108        };
109        ToolResult::json(&access)
110    }
111}