Skip to main content

aither_agent/
workspace_request.rs

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