|
| 1 | +use agent_client_protocol::{Client, ErrorCode, ReadTextFileRequest}; |
| 2 | +use tracing::instrument; |
| 3 | + |
| 4 | +#[derive(Debug)] |
| 5 | +pub enum FsReadTextFileError { |
| 6 | + InvalidRequest(serde_json::Error), |
| 7 | + ClientError(agent_client_protocol::Error), |
| 8 | + SerializationError(serde_json::Error), |
| 9 | +} |
| 10 | + |
| 11 | +impl std::fmt::Display for FsReadTextFileError { |
| 12 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 13 | + match self { |
| 14 | + Self::InvalidRequest(e) => write!(f, "invalid request: {}", e), |
| 15 | + Self::ClientError(e) => write!(f, "client error: {}", e), |
| 16 | + Self::SerializationError(e) => write!(f, "serialization error: {}", e), |
| 17 | + } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +impl std::error::Error for FsReadTextFileError { |
| 22 | + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 23 | + match self { |
| 24 | + Self::InvalidRequest(e) => Some(e), |
| 25 | + Self::ClientError(e) => Some(e), |
| 26 | + Self::SerializationError(e) => Some(e), |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +pub fn error_code_and_message(e: &FsReadTextFileError) -> (ErrorCode, String) { |
| 32 | + match e { |
| 33 | + FsReadTextFileError::InvalidRequest(inner) => ( |
| 34 | + ErrorCode::InvalidParams, |
| 35 | + format!("Invalid read_text_file request: {}", inner), |
| 36 | + ), |
| 37 | + FsReadTextFileError::ClientError(inner) => (inner.code.clone(), inner.message.clone()), |
| 38 | + FsReadTextFileError::SerializationError(inner) => ( |
| 39 | + ErrorCode::InternalError, |
| 40 | + format!("Failed to serialize read_text_file response: {}", inner), |
| 41 | + ), |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/// Forwards read_text_file to the client. NATS enforces payload limits when publishing. |
| 46 | +#[instrument(name = "acp.client.fs.read_text_file", skip(payload, client))] |
| 47 | +pub async fn handle<C: Client>( |
| 48 | + payload: &[u8], |
| 49 | + client: &C, |
| 50 | +) -> Result<Vec<u8>, FsReadTextFileError> { |
| 51 | + let request: ReadTextFileRequest = |
| 52 | + serde_json::from_slice(payload).map_err(FsReadTextFileError::InvalidRequest)?; |
| 53 | + let response = client |
| 54 | + .read_text_file(request) |
| 55 | + .await |
| 56 | + .map_err(FsReadTextFileError::ClientError)?; |
| 57 | + serde_json::to_vec(&response).map_err(FsReadTextFileError::SerializationError) |
| 58 | +} |
| 59 | + |
| 60 | +#[cfg(test)] |
| 61 | +mod tests { |
| 62 | + use super::*; |
| 63 | + use agent_client_protocol::{ReadTextFileRequest, ReadTextFileResponse}; |
| 64 | + use agent_client_protocol::RequestPermissionRequest; |
| 65 | + use agent_client_protocol::RequestPermissionResponse; |
| 66 | + use agent_client_protocol::SessionNotification; |
| 67 | + use async_trait::async_trait; |
| 68 | + |
| 69 | + struct MockClient { |
| 70 | + content: String, |
| 71 | + } |
| 72 | + |
| 73 | + impl MockClient { |
| 74 | + fn new(content: &str) -> Self { |
| 75 | + Self { |
| 76 | + content: content.to_string(), |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + #[async_trait(?Send)] |
| 82 | + impl Client for MockClient { |
| 83 | + async fn session_notification( |
| 84 | + &self, |
| 85 | + _: SessionNotification, |
| 86 | + ) -> agent_client_protocol::Result<()> { |
| 87 | + Ok(()) |
| 88 | + } |
| 89 | + |
| 90 | + async fn request_permission( |
| 91 | + &self, |
| 92 | + _: RequestPermissionRequest, |
| 93 | + ) -> agent_client_protocol::Result<RequestPermissionResponse> { |
| 94 | + Err(agent_client_protocol::Error::new( |
| 95 | + -32603, |
| 96 | + "not implemented in test mock", |
| 97 | + )) |
| 98 | + } |
| 99 | + |
| 100 | + async fn read_text_file( |
| 101 | + &self, |
| 102 | + _: ReadTextFileRequest, |
| 103 | + ) -> agent_client_protocol::Result<ReadTextFileResponse> { |
| 104 | + Ok(ReadTextFileResponse::new(self.content.clone())) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + #[tokio::test] |
| 109 | + async fn fs_read_text_file_forwards_request_and_returns_response() { |
| 110 | + let client = MockClient::new("hello world"); |
| 111 | + let request = ReadTextFileRequest::new( |
| 112 | + agent_client_protocol::SessionId::from("sess-1"), |
| 113 | + "/tmp/foo.txt".to_string(), |
| 114 | + ); |
| 115 | + let payload = serde_json::to_vec(&request).unwrap(); |
| 116 | + |
| 117 | + let result = handle(&payload, &client).await; |
| 118 | + assert!(result.is_ok()); |
| 119 | + let response = serde_json::from_slice::<ReadTextFileResponse>(&result.unwrap()).unwrap(); |
| 120 | + assert_eq!(response.content, "hello world"); |
| 121 | + } |
| 122 | + |
| 123 | + #[tokio::test] |
| 124 | + async fn fs_read_text_file_returns_error_when_payload_is_invalid_json() { |
| 125 | + let client = MockClient::new("hello"); |
| 126 | + let result = handle(b"not json", &client).await; |
| 127 | + assert!(result.is_err()); |
| 128 | + } |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn error_code_and_message_invalid_request_returns_invalid_params() { |
| 132 | + let err = serde_json::from_slice::<ReadTextFileRequest>(b"not json") |
| 133 | + .unwrap_err(); |
| 134 | + let fs_err = FsReadTextFileError::InvalidRequest(err); |
| 135 | + let (code, message) = error_code_and_message(&fs_err); |
| 136 | + assert_eq!(code, ErrorCode::InvalidParams); |
| 137 | + assert!(message.contains("Invalid read_text_file request")); |
| 138 | + } |
| 139 | + |
| 140 | + #[test] |
| 141 | + fn error_code_and_message_client_error_preserves_client_code() { |
| 142 | + let client_err = agent_client_protocol::Error::new( |
| 143 | + ErrorCode::InvalidParams.into(), |
| 144 | + "file not found", |
| 145 | + ); |
| 146 | + let fs_err = FsReadTextFileError::ClientError(client_err); |
| 147 | + let (code, message) = error_code_and_message(&fs_err); |
| 148 | + assert_eq!(code, ErrorCode::InvalidParams); |
| 149 | + assert_eq!(message, "file not found"); |
| 150 | + } |
| 151 | + |
| 152 | + #[test] |
| 153 | + fn error_code_and_message_serialization_error_returns_internal_error() { |
| 154 | + struct Unserializable; |
| 155 | + impl serde::Serialize for Unserializable { |
| 156 | + fn serialize<S: serde::Serializer>( |
| 157 | + &self, |
| 158 | + _: S, |
| 159 | + ) -> Result<S::Ok, S::Error> { |
| 160 | + Err(serde::ser::Error::custom("test serialization failure")) |
| 161 | + } |
| 162 | + } |
| 163 | + let err = serde_json::to_vec(&Unserializable).unwrap_err(); |
| 164 | + let fs_err = FsReadTextFileError::SerializationError(err); |
| 165 | + let (code, message) = error_code_and_message(&fs_err); |
| 166 | + assert_eq!(code, ErrorCode::InternalError); |
| 167 | + assert!(message.contains("Failed to serialize read_text_file response")); |
| 168 | + } |
| 169 | +} |
0 commit comments