feagi-agent 0.0.1

Client library for building FEAGI agents in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use crate::command_and_control::agent_registration_message::{
    AgentRegistrationMessage, DeregistrationRequest, DeregistrationResponse, RegistrationRequest,
    RegistrationResponse,
};
use crate::command_and_control::FeagiMessage;
use crate::{AgentCapabilities, AgentDescriptor, AuthToken, FeagiAgentError};
use feagi_io::traits_and_enums::client::{FeagiClientRequester, FeagiClientRequesterProperties};
use feagi_io::traits_and_enums::shared::{FeagiEndpointState, TransportProtocolEndpoint};
use feagi_io::AgentID;
use feagi_serialization::FeagiByteContainer;
use std::collections::HashMap;

pub struct CommandControlAgent {
    properties: Box<dyn FeagiClientRequesterProperties>,
    requester: Option<Box<dyn FeagiClientRequester>>,
    request_buffer: FeagiByteContainer,
    send_buffer: FeagiByteContainer,
    registration_status: AgentRegistrationStatus,
}

impl CommandControlAgent {
    pub fn new(endpoint_properties: Box<dyn FeagiClientRequesterProperties>) -> Self {
        Self {
            registration_status: AgentRegistrationStatus::NotRegistered,
            properties: endpoint_properties,
            requester: None,
            request_buffer: FeagiByteContainer::new_empty(),
            send_buffer: FeagiByteContainer::new_empty(),
        }
    }

    //region Properties
    pub fn registration_status(&self) -> &AgentRegistrationStatus {
        &self.registration_status
    }

    pub fn registered_endpoint_target(&mut self) -> TransportProtocolEndpoint {
        self.properties.get_endpoint_target()
    }
    //endregion

    //region Helpers

    pub fn request_connect(&mut self) -> Result<(), FeagiAgentError> {
        if self.registration_status != AgentRegistrationStatus::NotRegistered {
            return Err(FeagiAgentError::ConnectionFailed(
                "Agent already connected and registered!".to_string(),
            ));
        }

        if self.requester.is_none() {
            self.requester = Some(self.properties.as_boxed_client_requester());
        }

        let mut requester = self.requester.take().unwrap();

        match requester.poll() {
            FeagiEndpointState::Inactive => {
                requester.request_connect()?;
                self.requester = Some(requester);
                Ok(())
            }
            _ => {
                self.requester = Some(requester);
                Err(FeagiAgentError::ConnectionFailed(
                    "Socket is already active!".to_string(),
                ))
            }
        }
    }

    pub fn request_registration(
        &mut self,
        agent_descriptor: AgentDescriptor,
        auth_token: AuthToken,
        requested_capabilities: Vec<AgentCapabilities>,
    ) -> Result<(), FeagiAgentError> {
        let transport_protocol = if let Some(requester) = &mut self.requester {
            requester
                .get_endpoint_target()
                .as_transport_protocol_implementation()
        } else {
            return Err(FeagiAgentError::ConnectionFailed(
                "Cannot register to endpoint when not connected!".to_string(),
            ));
        };

        let request = RegistrationRequest::new(
            agent_descriptor,
            auth_token,
            requested_capabilities,
            transport_protocol,
        );

        let request_message = FeagiMessage::AgentRegistration(
            AgentRegistrationMessage::ClientRequestRegistration(request),
        );

        self.send_message(request_message, 0)?;
        Ok(())
    }

    /// Request voluntary deregistration for the given session.
    ///
    /// The optional `reason` string is forwarded for observability and does not
    /// alter deregistration behavior on the server.
    pub fn request_deregistration(
        &mut self,
        reason: Option<String>, // TODO Please dont use strings, use ENUMS!
    ) -> Result<(), FeagiAgentError> {
        let request = DeregistrationRequest::new(reason);
        let message = FeagiMessage::AgentRegistration(
            AgentRegistrationMessage::ClientRequestDeregistration(request),
        );

        self.send_message(message, 0)?;
        Ok(())
    }

    /// Send a heartbeat over the command/control channel for the provided session.
    ///
    /// This is the deterministic, tick-driven heartbeat primitive used by
    /// higher-level client loops.
    pub fn send_heartbeat(&mut self) -> Result<(), FeagiAgentError> {
        let heartbeat_message = FeagiMessage::HeartBeat;
        self.send_message(heartbeat_message, 0)
    }

    /// Request transport disconnect for command/control requester.
    ///
    /// This is a best-effort disconnect primitive used by higher-level shutdown
    /// orchestration to ensure sockets are torn down deterministically.
    pub fn request_disconnect(&mut self) -> Result<(), FeagiAgentError> {
        let requester = self.requester.as_mut().ok_or_else(|| {
            FeagiAgentError::ConnectionFailed("No socket is active to disconnect!".to_string())
        })?;
        requester.request_disconnect()?;
        Ok(())
    }

    //endregion

    //region Base Functions

    pub fn poll_for_messages(
        &mut self,
    ) -> Result<(&FeagiEndpointState, Option<FeagiMessage>), FeagiAgentError> {
        let maybe_message = {
            let requester = self.requester.as_mut().ok_or_else(|| {
                FeagiAgentError::ConnectionFailed("No socket is active to poll!".to_string())
            })?;

            let state_snapshot = requester.poll().clone();
            match state_snapshot {
                FeagiEndpointState::Inactive
                | FeagiEndpointState::Pending
                | FeagiEndpointState::ActiveWaiting => Ok(None),
                FeagiEndpointState::ActiveHasData => {
                    let data = requester.consume_retrieved_response()?;
                    self.request_buffer
                        .try_write_data_by_copy_and_verify(data)?;
                    let feagi_message: FeagiMessage = (&self.request_buffer).try_into()?;

                    match &feagi_message {
                        FeagiMessage::HeartBeat => Ok(Some(FeagiMessage::HeartBeat)),
                        FeagiMessage::AgentRegistration(registration_message) => {
                            match registration_message {
                                AgentRegistrationMessage::ClientRequestRegistration(_) => {
                                    Err(FeagiAgentError::ConnectionFailed(
                                        "Client cannot register agents!".to_string(),
                                    ))
                                }
                                AgentRegistrationMessage::ServerRespondsRegistration(
                                    registration_response,
                                ) => match registration_response {
                                    RegistrationResponse::FailedInvalidRequest => {
                                        Err(FeagiAgentError::ConnectionFailed(
                                            "Invalid server responses!".to_string(),
                                        ))
                                    }
                                    RegistrationResponse::FailedInvalidAuth => {
                                        Err(FeagiAgentError::ConnectionFailed(
                                            "Invalid auth token!".to_string(),
                                        ))
                                    }
                                    RegistrationResponse::AlreadyRegistered => {
                                        Err(FeagiAgentError::ConnectionFailed(
                                            "Client already registered!".to_string(),
                                        ))
                                    }
                                    RegistrationResponse::Success(session_id, endpoints) => {
                                        self.registration_status =
                                            AgentRegistrationStatus::Registered(
                                                *session_id,
                                                endpoints.clone(),
                                            );
                                        Ok(Some(feagi_message))
                                    }
                                },
                                AgentRegistrationMessage::ClientRequestDeregistration(_) => {
                                    Err(FeagiAgentError::ConnectionFailed(
                                        "Client cannot receive deregistration request from server!"
                                            .to_string(),
                                    ))
                                }
                                AgentRegistrationMessage::ServerRespondsDeregistration(
                                    deregistration_response,
                                ) => match deregistration_response {
                                    DeregistrationResponse::Success => {
                                        requester.request_disconnect()?;
                                        self.registration_status =
                                            AgentRegistrationStatus::NotRegistered;
                                        Ok(Some(feagi_message))
                                    }
                                    DeregistrationResponse::NotRegistered => {
                                        Ok(Some(feagi_message))
                                    }
                                },
                            }
                        }
                        _ => Ok(Some(feagi_message)),
                    }
                }
                FeagiEndpointState::Errored(_) => {
                    requester.confirm_error_and_close()?;
                    Err(FeagiAgentError::ConnectionFailed(
                        "Error occurred".to_string(),
                    ))
                }
            }
        }?;

        let state = self
            .requester
            .as_mut()
            .ok_or_else(|| {
                FeagiAgentError::ConnectionFailed("No socket is active to poll!".to_string())
            })?
            .poll();

        Ok((state, maybe_message))
    }

    pub fn send_message(
        &mut self,
        message: FeagiMessage,
        increment_value: u16,
    ) -> Result<(), FeagiAgentError> {
        let agent_id = match &self.registration_status {
            AgentRegistrationStatus::Registered(agent_id, _) => *agent_id,
            AgentRegistrationStatus::NotRegistered => {
                // Registration must be possible before a session id exists.
                // FEAGI servers accept a blank agent id for registration requests.
                match &message {
                    FeagiMessage::AgentRegistration(
                        AgentRegistrationMessage::ClientRequestRegistration(_),
                    ) => AgentID::new_blank(),
                    _ => {
                        return Err(FeagiAgentError::UnableToSendData(
                            "Nonregistered agent cannot send message!".to_string(),
                        ));
                    }
                }
            }
        };

        if let Some(requester) = &mut self.requester {
            message.serialize_to_byte_container(
                &mut self.send_buffer,
                agent_id,
                increment_value,
            )?;
            requester.publish_request(self.send_buffer.get_byte_ref())?;
            Ok(())
        } else {
            // This state should be impossible. something went very wrong
            panic!("Active state but no socket!!")
        }
    }

    //endregion
}

#[derive(Debug, PartialEq, Clone)]
pub enum AgentRegistrationStatus {
    NotRegistered,
    Registered(
        AgentID,
        HashMap<AgentCapabilities, TransportProtocolEndpoint>,
    ),
}

#[cfg(test)]
mod tests {
    use super::*;
    use feagi_io::protocol_implementations::zmq::ZmqUrl;
    use feagi_io::traits_and_enums::shared::{FeagiEndpointState, TransportProtocolEndpoint};
    use std::sync::{Arc, Mutex};

    #[derive(Clone)]
    struct DummyRequesterProperties {
        endpoint: TransportProtocolEndpoint,
        last_request: Arc<Mutex<Vec<u8>>>,
    }

    struct DummyRequester {
        endpoint: TransportProtocolEndpoint,
        state: FeagiEndpointState,
        last_request: Arc<Mutex<Vec<u8>>>,
    }

    impl feagi_io::traits_and_enums::client::FeagiClient for DummyRequester {
        fn poll(&mut self) -> &FeagiEndpointState {
            &self.state
        }

        fn request_connect(&mut self) -> Result<(), feagi_io::FeagiNetworkError> {
            self.state = FeagiEndpointState::ActiveWaiting;
            Ok(())
        }

        fn request_disconnect(&mut self) -> Result<(), feagi_io::FeagiNetworkError> {
            self.state = FeagiEndpointState::Inactive;
            Ok(())
        }

        fn confirm_error_and_close(&mut self) -> Result<(), feagi_io::FeagiNetworkError> {
            self.state = FeagiEndpointState::Inactive;
            Ok(())
        }

        fn get_endpoint_target(&self) -> TransportProtocolEndpoint {
            self.endpoint.clone()
        }
    }

    impl feagi_io::traits_and_enums::client::FeagiClientRequester for DummyRequester {
        fn publish_request(&mut self, request: &[u8]) -> Result<(), feagi_io::FeagiNetworkError> {
            *self.last_request.lock().expect("lock") = request.to_vec();
            Ok(())
        }

        fn consume_retrieved_response(&mut self) -> Result<&[u8], feagi_io::FeagiNetworkError> {
            Err(feagi_io::FeagiNetworkError::ReceiveFailed(
                "dummy requester has no responses".to_string(),
            ))
        }

        fn as_boxed_requester_properties(
            &self,
        ) -> Box<dyn feagi_io::traits_and_enums::client::FeagiClientRequesterProperties> {
            Box::new(DummyRequesterProperties {
                endpoint: self.endpoint.clone(),
                last_request: self.last_request.clone(),
            })
        }
    }

    impl feagi_io::traits_and_enums::client::FeagiClientRequesterProperties
        for DummyRequesterProperties
    {
        fn as_boxed_client_requester(
            &self,
        ) -> Box<dyn feagi_io::traits_and_enums::client::FeagiClientRequester> {
            Box::new(DummyRequester {
                endpoint: self.endpoint.clone(),
                state: FeagiEndpointState::Inactive,
                last_request: self.last_request.clone(),
            })
        }

        fn get_endpoint_target(&self) -> TransportProtocolEndpoint {
            self.endpoint.clone()
        }
    }

    #[test]
    fn registration_request_can_be_sent_before_registration() {
        let endpoint = TransportProtocolEndpoint::Zmq(
            ZmqUrl::new("tcp://example:1").expect("valid dummy endpoint"),
        );
        let last_request: Arc<Mutex<Vec<u8>>> = Arc::new(Mutex::new(Vec::new()));
        let props = Box::new(DummyRequesterProperties {
            endpoint,
            last_request: last_request.clone(),
        });

        let mut agent = CommandControlAgent::new(props);
        agent
            .request_connect()
            .expect("connect request should succeed");

        agent
            .request_registration(
                AgentDescriptor::new("m", "n", 1).expect("descriptor"),
                AuthToken::new([0u8; 32]),
                vec![AgentCapabilities::SendSensorData],
            )
            .expect("registration request should be sendable with blank id");

        assert!(
            !last_request.lock().expect("lock").is_empty(),
            "expected a serialized registration request to be published"
        );
    }
}