everruns_runtime/
backends.rs1use crate::in_memory::{InMemorySessionStorageStore, InMemorySessionStore};
8use async_trait::async_trait;
9use everruns_core::agent::Agent;
10use everruns_core::error::Result;
11use everruns_core::events::Event;
12use everruns_core::harness::Harness;
13use everruns_core::in_memory::{
14 InMemoryAgentStore, InMemoryEventEmitter, InMemoryHarnessStore, InMemoryMessageRetriever,
15 InMemoryProviderStore,
16};
17use everruns_core::message::Message;
18use everruns_core::message_retriever::{InputMessage, MessageRetriever};
19use everruns_core::platform_store::PlatformStore;
20use everruns_core::session::Session;
21use everruns_core::session_task::SessionTaskRegistry;
22use everruns_core::traits::{
23 AgentStore, EventEmitter, HarnessStore, ProviderStore, ResolvedModel, SessionMutator,
24 SessionScheduleStore, SessionStorageStore, SessionStore, UserConnectionResolver,
25};
26use everruns_core::typed_id::SessionId;
27use std::sync::Arc;
28
29pub type ScheduleStoreFactory = Arc<dyn Fn(i64) -> Arc<dyn SessionScheduleStore> + Send + Sync>;
33
34pub type PlatformStoreFactory = Arc<dyn Fn(i64, SessionId) -> Arc<dyn PlatformStore> + Send + Sync>;
38
39#[async_trait]
41pub trait RuntimeAgentStore: AgentStore + Send + Sync {
42 async fn add_agent(&self, agent: Agent) -> Result<()>;
44}
45
46#[async_trait]
48pub trait RuntimeHarnessStore: HarnessStore + Send + Sync {
49 async fn add_harness(&self, harness: Harness) -> Result<()>;
51}
52
53#[async_trait]
55pub trait RuntimeSessionStore: SessionStore + SessionMutator + Send + Sync {
56 async fn add_session(&self, session: Session) -> Result<()>;
58}
59
60#[async_trait]
62pub trait RuntimeMessageStore: MessageRetriever + Send + Sync {
63 async fn add_input_message(
65 &self,
66 session_id: SessionId,
67 input: InputMessage,
68 ) -> Result<Message>;
69
70 async fn store_message(&self, session_id: SessionId, message: Message) -> Result<()>;
72}
73
74#[async_trait]
76pub trait RuntimeProviderStore: ProviderStore + Send + Sync {
77 async fn set_default_model(&self, model: ResolvedModel) -> Result<()>;
79}
80
81#[async_trait]
87pub trait EventBus: EventEmitter {
88 async fn collected_events(&self) -> Vec<Event> {
91 Vec::new()
92 }
93}
94
95#[async_trait]
96impl<T: EventBus + ?Sized> EventBus for Arc<T> {
97 async fn collected_events(&self) -> Vec<Event> {
98 (**self).collected_events().await
99 }
100}
101
102#[derive(Clone)]
108pub struct RuntimeBackends {
109 pub harness_store: Arc<dyn RuntimeHarnessStore>,
111 pub agent_store: Arc<dyn RuntimeAgentStore>,
113 pub session_store: Arc<dyn RuntimeSessionStore>,
115 pub message_store: Arc<dyn RuntimeMessageStore>,
117 pub provider_store: Arc<dyn RuntimeProviderStore>,
119 pub event_bus: Arc<dyn EventBus>,
121 pub storage_store: Arc<dyn SessionStorageStore>,
123 pub connection_resolver: Option<Arc<dyn UserConnectionResolver>>,
131 pub session_task_registry: Option<Arc<dyn SessionTaskRegistry>>,
136 pub schedule_store_factory: Option<ScheduleStoreFactory>,
139 pub platform_store_factory: Option<PlatformStoreFactory>,
142}
143
144impl RuntimeBackends {
145 pub fn in_memory() -> Self {
150 let event_bus = Arc::new(InMemoryEventEmitter::new());
151 Self {
152 harness_store: Arc::new(InMemoryHarnessStore::new()),
153 agent_store: Arc::new(InMemoryAgentStore::new()),
154 session_store: Arc::new(InMemorySessionStore::new()),
155 message_store: Arc::new(InMemoryMessageRetriever::new()),
156 provider_store: Arc::new(InMemoryProviderStore::new()),
157 event_bus,
158 storage_store: Arc::new(InMemorySessionStorageStore::new()),
159 connection_resolver: None,
160 session_task_registry: None,
161 schedule_store_factory: None,
162 platform_store_factory: None,
163 }
164 }
165
166 pub fn with_harness_store(mut self, store: Arc<dyn RuntimeHarnessStore>) -> Self {
167 self.harness_store = store;
168 self
169 }
170
171 pub fn with_agent_store(mut self, store: Arc<dyn RuntimeAgentStore>) -> Self {
172 self.agent_store = store;
173 self
174 }
175
176 pub fn with_session_store(mut self, store: Arc<dyn RuntimeSessionStore>) -> Self {
177 self.session_store = store;
178 self
179 }
180
181 pub fn with_message_store(mut self, store: Arc<dyn RuntimeMessageStore>) -> Self {
182 self.message_store = store;
183 self
184 }
185
186 pub fn with_provider_store(mut self, store: Arc<dyn RuntimeProviderStore>) -> Self {
187 self.provider_store = store;
188 self
189 }
190
191 pub fn with_event_bus(mut self, bus: Arc<dyn EventBus>) -> Self {
192 self.event_bus = bus;
193 self
194 }
195
196 pub fn with_storage_store(mut self, store: Arc<dyn SessionStorageStore>) -> Self {
197 self.storage_store = store;
198 self
199 }
200
201 pub fn with_connection_resolver(mut self, resolver: Arc<dyn UserConnectionResolver>) -> Self {
206 self.connection_resolver = Some(resolver);
207 self
208 }
209
210 pub fn with_session_task_registry(mut self, registry: Arc<dyn SessionTaskRegistry>) -> Self {
214 self.session_task_registry = Some(registry);
215 self
216 }
217
218 pub fn with_schedule_store_factory(mut self, factory: ScheduleStoreFactory) -> Self {
221 self.schedule_store_factory = Some(factory);
222 self
223 }
224
225 pub fn with_platform_store_factory(mut self, factory: PlatformStoreFactory) -> Self {
228 self.platform_store_factory = Some(factory);
229 self
230 }
231}
232
233#[async_trait]
234impl RuntimeAgentStore for InMemoryAgentStore {
235 async fn add_agent(&self, agent: Agent) -> Result<()> {
236 InMemoryAgentStore::add_agent(self, agent).await;
237 Ok(())
238 }
239}
240
241#[async_trait]
242impl RuntimeHarnessStore for InMemoryHarnessStore {
243 async fn add_harness(&self, harness: Harness) -> Result<()> {
244 InMemoryHarnessStore::add_harness(self, harness).await;
245 Ok(())
246 }
247}
248
249#[async_trait]
250impl RuntimeSessionStore for InMemorySessionStore {
251 async fn add_session(&self, session: Session) -> Result<()> {
252 InMemorySessionStore::add_session(self, session).await;
253 Ok(())
254 }
255}
256
257#[async_trait]
258impl RuntimeMessageStore for InMemoryMessageRetriever {
259 async fn add_input_message(
260 &self,
261 session_id: SessionId,
262 input: InputMessage,
263 ) -> Result<Message> {
264 self.add(session_id, input).await
265 }
266
267 async fn store_message(&self, session_id: SessionId, message: Message) -> Result<()> {
268 self.store(session_id, message).await
269 }
270}
271
272#[async_trait]
273impl RuntimeProviderStore for InMemoryProviderStore {
274 async fn set_default_model(&self, model: ResolvedModel) -> Result<()> {
275 InMemoryProviderStore::set_default_model(self, model).await;
276 Ok(())
277 }
278}
279
280#[async_trait]
281impl EventBus for InMemoryEventEmitter {
282 async fn collected_events(&self) -> Vec<Event> {
283 InMemoryEventEmitter::events(self).await
284 }
285}