agent_stream_kit/
stream.rs

1use serde::{Deserialize, Serialize};
2
3use crate::FnvIndexMap;
4use crate::askit::ASKit;
5use crate::error::AgentError;
6use crate::id::new_id;
7use crate::spec::AgentStreamSpec;
8
9pub type AgentStreams = FnvIndexMap<String, AgentStream>;
10
11pub struct AgentStream {
12    id: String,
13
14    name: String,
15
16    running: bool,
17
18    spec: AgentStreamSpec,
19}
20
21impl AgentStream {
22    pub fn new(name: String, spec: AgentStreamSpec) -> Self {
23        Self {
24            id: new_id(),
25            name,
26            running: false,
27            spec,
28        }
29    }
30
31    pub fn id(&self) -> &str {
32        &self.id
33    }
34
35    pub fn spec(&self) -> &AgentStreamSpec {
36        &self.spec
37    }
38
39    pub fn spec_mut(&mut self) -> &mut AgentStreamSpec {
40        &mut self.spec
41    }
42
43    pub fn name(&self) -> &str {
44        &self.name
45    }
46
47    pub fn set_name(&mut self, name: String) {
48        self.name = name;
49    }
50
51    pub fn running(&self) -> bool {
52        self.running
53    }
54
55    pub async fn start(&mut self, askit: &ASKit) -> Result<(), AgentError> {
56        if self.running {
57            // Already running
58            return Ok(());
59        }
60        self.running = true;
61
62        for agent in self.spec.agents.iter() {
63            if agent.disabled {
64                continue;
65            }
66            askit.start_agent(&agent.id).await.unwrap_or_else(|e| {
67                log::error!("Failed to start agent {}: {}", agent.id, e);
68            });
69        }
70
71        Ok(())
72    }
73
74    pub async fn stop(&mut self, askit: &ASKit) -> Result<(), AgentError> {
75        for agent in self.spec.agents.iter() {
76            askit.stop_agent(&agent.id).await.unwrap_or_else(|e| {
77                log::error!("Failed to stop agent {}: {}", agent.id, e);
78            });
79        }
80        self.running = false;
81        Ok(())
82    }
83}
84
85#[derive(Clone, Debug, Serialize, Deserialize)]
86pub struct AgentStreamInfo {
87    pub id: String,
88    pub name: String,
89    pub running: bool,
90    pub run_on_start: bool,
91}
92
93impl From<&AgentStream> for AgentStreamInfo {
94    fn from(stream: &AgentStream) -> Self {
95        Self {
96            id: stream.id.clone(),
97            name: stream.name.clone(),
98            running: stream.running,
99            run_on_start: stream.spec.run_on_start,
100        }
101    }
102}