aither_agent/
workspace_request.rs1use 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#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
23pub struct RequestWorkspaceArgs {
24 pub path: String,
26
27 #[serde(default = "default_reason")]
29 pub reason: String,
30}
31
32fn default_reason() -> String {
33 DEFAULT_REASON.trim().to_string()
34}
35
36#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
38pub struct WorkspaceAccess {
39 pub path: String,
41 pub reason: String,
43 pub approved: bool,
45}
46
47#[derive(Debug, Clone, Serialize, Deserialize)]
49pub struct WorkspaceRequestPayload {
50 pub session_id: String,
52 pub request: RequestWorkspaceArgs,
54}
55
56pub type WorkspaceRequest = ToolRequest<WorkspaceRequestPayload, bool>;
58
59pub type WorkspaceRequestBroker = ToolRequestBroker<WorkspaceRequestPayload, bool>;
61
62pub type WorkspaceRequestQueue = ToolRequestQueue<WorkspaceRequestPayload, bool>;
64
65#[must_use]
67pub fn channel() -> (WorkspaceRequestBroker, WorkspaceRequestQueue) {
68 tool_request_channel()
69}
70
71#[derive(Debug, Clone)]
73pub struct RequestWorkspaceTool {
74 broker: WorkspaceRequestBroker,
75 session_id: String,
76}
77
78impl RequestWorkspaceTool {
79 #[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}