aither_agent/
workspace_request.rs1use 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#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
22pub struct RequestWorkspaceArgs {
23 pub path: String,
25
26 #[serde(default = "default_reason")]
28 pub reason: String,
29}
30
31fn default_reason() -> String {
32 DEFAULT_REASON.trim().to_string()
33}
34
35#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
37pub struct WorkspaceAccess {
38 pub path: String,
40 pub reason: String,
42 pub approved: bool,
44}
45
46#[derive(Debug, Clone, Serialize, Deserialize)]
48pub struct WorkspaceRequestPayload {
49 pub session_id: String,
51 pub request: RequestWorkspaceArgs,
53}
54
55pub type WorkspaceRequest = ToolRequest<WorkspaceRequestPayload, bool>;
57
58pub type WorkspaceRequestBroker = ToolRequestBroker<WorkspaceRequestPayload, bool>;
60
61pub type WorkspaceRequestQueue = ToolRequestQueue<WorkspaceRequestPayload, bool>;
63
64#[must_use]
66pub fn channel() -> (WorkspaceRequestBroker, WorkspaceRequestQueue) {
67 tool_request_channel()
68}
69
70#[derive(Debug, Clone)]
72pub struct RequestWorkspaceTool {
73 broker: WorkspaceRequestBroker,
74 session_id: String,
75}
76
77impl RequestWorkspaceTool {
78 #[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}