1use crate::agent::Agent;
9use crate::harness::Harness;
10use crate::llm_models::LlmProviderType;
11use crate::session_file::{FileInfo, FileStat, GrepMatch, InitialFile, SessionFile};
12use crate::tool_types::{ToolCall, ToolDefinition, ToolResult};
13use crate::typed_id::{AgentId, HarnessId, ImageId, ModelId, SessionId};
14use async_trait::async_trait;
15use chrono::{DateTime, Utc};
16use std::any::{Any, TypeId};
17use std::collections::HashMap;
18use std::sync::Arc;
19use uuid::Uuid;
20
21fn build_tool_map(tool_defs: &[ToolDefinition]) -> HashMap<&str, &ToolDefinition> {
23 tool_defs.iter().map(|def| (def.name(), def)).collect()
24}
25
26use crate::error::Result;
27
28#[async_trait]
39pub trait AgentStore: Send + Sync {
40 async fn get_agent(&self, agent_id: AgentId) -> Result<Option<Agent>>;
42}
43
44#[async_trait]
45impl<T: AgentStore + ?Sized> AgentStore for std::sync::Arc<T> {
46 async fn get_agent(&self, agent_id: AgentId) -> Result<Option<Agent>> {
47 (**self).get_agent(agent_id).await
48 }
49}
50
51#[async_trait]
66pub trait HarnessStore: Send + Sync {
67 async fn get_harness_chain(&self, harness_id: HarnessId) -> Result<Vec<Harness>>;
72}
73
74#[async_trait]
75impl<T: HarnessStore + ?Sized> HarnessStore for std::sync::Arc<T> {
76 async fn get_harness_chain(&self, harness_id: HarnessId) -> Result<Vec<Harness>> {
77 (**self).get_harness_chain(harness_id).await
78 }
79}
80
81use crate::leased_resource::{LeasedResource, UpsertLeasedResource};
86use crate::session::Session;
87
88#[async_trait]
94pub trait SessionStore: Send + Sync {
95 async fn get_session(&self, session_id: SessionId) -> Result<Option<Session>>;
97}
98
99#[async_trait]
100impl<T: SessionStore + ?Sized> SessionStore for std::sync::Arc<T> {
101 async fn get_session(&self, session_id: SessionId) -> Result<Option<Session>> {
102 (**self).get_session(session_id).await
103 }
104}
105
106#[async_trait]
108pub trait SessionMutator: Send + Sync {
109 async fn update_session_title(&self, session_id: SessionId, title: String) -> Result<Session>;
111}
112
113#[async_trait]
114impl<T: SessionMutator + ?Sized> SessionMutator for std::sync::Arc<T> {
115 async fn update_session_title(&self, session_id: SessionId, title: String) -> Result<Session> {
116 (**self).update_session_title(session_id, title).await
117 }
118}
119
120#[derive(Debug, Clone)]
126pub struct ModelWithProvider {
127 pub model: String,
129 pub provider_type: LlmProviderType,
131 pub api_key: Option<String>,
133 pub base_url: Option<String>,
135}
136
137#[async_trait]
147pub trait LlmProviderStore: Send + Sync {
148 async fn get_model_with_provider(&self, model_id: ModelId)
153 -> Result<Option<ModelWithProvider>>;
154
155 async fn get_default_model(&self) -> Result<Option<ModelWithProvider>>;
159}
160
161#[async_trait]
162impl<T: LlmProviderStore + ?Sized> LlmProviderStore for std::sync::Arc<T> {
163 async fn get_model_with_provider(
164 &self,
165 model_id: ModelId,
166 ) -> Result<Option<ModelWithProvider>> {
167 (**self).get_model_with_provider(model_id).await
168 }
169
170 async fn get_default_model(&self) -> Result<Option<ModelWithProvider>> {
171 (**self).get_default_model().await
172 }
173}
174
175#[derive(Debug, Clone)]
181pub struct StoredImageInfo {
182 pub id: ImageId,
183 pub filename: String,
184 pub content_type: String,
185 pub size_bytes: i64,
186 pub metadata: serde_json::Value,
187 pub created_at: DateTime<Utc>,
188}
189
190#[derive(Debug, Clone)]
192pub struct StoredImage {
193 pub info: StoredImageInfo,
194 pub data: Vec<u8>,
195}
196
197#[derive(Debug, Clone)]
199pub struct CreateStoredImage {
200 pub filename: String,
201 pub content_type: String,
202 pub data: Vec<u8>,
203 pub metadata: serde_json::Value,
204}
205
206#[async_trait]
207pub trait ImageArtifactStore: Send + Sync {
208 async fn create_image(&self, input: CreateStoredImage) -> Result<StoredImageInfo>;
210
211 async fn get_image(&self, image_id: ImageId) -> Result<Option<StoredImage>>;
213
214 async fn get_image_info(&self, image_id: ImageId) -> Result<Option<StoredImageInfo>>;
216}
217
218#[derive(Debug, Clone)]
224pub struct ProviderCredentials {
225 pub api_key: String,
226 pub base_url: Option<String>,
227}
228
229#[async_trait]
230pub trait ProviderCredentialStore: Send + Sync {
231 async fn get_default_provider_credentials(
236 &self,
237 provider_type: &str,
238 ) -> Result<Option<ProviderCredentials>>;
239}
240
241#[async_trait]
252pub trait ToolExecutor: Send + Sync {
253 async fn execute(&self, tool_call: &ToolCall, tool_def: &ToolDefinition) -> Result<ToolResult>;
258
259 async fn execute_with_context(
264 &self,
265 tool_call: &ToolCall,
266 tool_def: &ToolDefinition,
267 _context: &ToolContext,
268 ) -> Result<ToolResult> {
269 self.execute(tool_call, tool_def).await
271 }
272
273 async fn execute_batch(
275 &self,
276 tool_calls: &[ToolCall],
277 tool_defs: &[ToolDefinition],
278 ) -> Result<Vec<ToolResult>> {
279 let mut results = Vec::with_capacity(tool_calls.len());
280
281 let tool_map = build_tool_map(tool_defs);
282
283 for tool_call in tool_calls {
284 let tool_def = tool_map.get(tool_call.name.as_str()).ok_or_else(|| {
285 crate::error::AgentLoopError::tool(format!(
286 "Tool definition not found: {}",
287 tool_call.name
288 ))
289 })?;
290
291 results.push(self.execute(tool_call, tool_def).await?);
292 }
293
294 Ok(results)
295 }
296
297 async fn execute_parallel(
299 &self,
300 tool_calls: &[ToolCall],
301 tool_defs: &[ToolDefinition],
302 ) -> Result<Vec<ToolResult>>
303 where
304 Self: Sized,
305 {
306 use futures::future::join_all;
307
308 let tool_map = build_tool_map(tool_defs);
309
310 let futures: Vec<_> = tool_calls
311 .iter()
312 .map(|tool_call| async {
313 let tool_def = tool_map.get(tool_call.name.as_str()).ok_or_else(|| {
314 crate::error::AgentLoopError::tool(format!(
315 "Tool definition not found: {}",
316 tool_call.name
317 ))
318 })?;
319 self.execute(tool_call, tool_def).await
320 })
321 .collect();
322
323 let results = join_all(futures).await;
324 results.into_iter().collect()
325 }
326}
327
328#[async_trait]
340pub trait SessionFileSystem: Send + Sync {
341 async fn read_file(&self, session_id: SessionId, path: &str) -> Result<Option<SessionFile>>;
343
344 async fn write_file(
346 &self,
347 session_id: SessionId,
348 path: &str,
349 content: &str,
350 encoding: &str,
351 ) -> Result<SessionFile>;
352
353 async fn write_file_if_content_matches(
358 &self,
359 session_id: SessionId,
360 path: &str,
361 expected_content: &str,
362 expected_encoding: &str,
363 content: &str,
364 encoding: &str,
365 ) -> Result<Option<SessionFile>> {
366 let Some(existing) = self.read_file(session_id, path).await? else {
367 return Ok(None);
368 };
369
370 if existing.is_directory {
371 return Ok(None);
372 }
373
374 let current_content = existing.content.unwrap_or_default();
375 if current_content != expected_content || existing.encoding != expected_encoding {
376 return Ok(None);
377 }
378
379 self.write_file(session_id, path, content, encoding)
380 .await
381 .map(Some)
382 }
383
384 async fn delete_file(&self, session_id: SessionId, path: &str, recursive: bool)
386 -> Result<bool>;
387
388 async fn list_directory(&self, session_id: SessionId, path: &str) -> Result<Vec<FileInfo>>;
390
391 async fn stat_file(&self, session_id: SessionId, path: &str) -> Result<Option<FileStat>>;
393
394 async fn grep_files(
396 &self,
397 session_id: SessionId,
398 pattern: &str,
399 path_pattern: Option<&str>,
400 ) -> Result<Vec<GrepMatch>>;
401
402 async fn create_directory(&self, session_id: SessionId, path: &str) -> Result<FileInfo>;
404
405 async fn seed_initial_file(&self, session_id: SessionId, file: &InitialFile) -> Result<()> {
407 if file.is_readonly {
408 return Err(crate::error::AgentLoopError::store(
409 "read-only initial files require a SessionFileSystem-specific seed implementation",
410 ));
411 }
412 self.write_file(session_id, &file.path, &file.content, &file.encoding)
413 .await?;
414 Ok(())
415 }
416}
417
418#[async_trait]
419impl<T: SessionFileSystem + ?Sized> SessionFileSystem for std::sync::Arc<T> {
420 async fn read_file(&self, session_id: SessionId, path: &str) -> Result<Option<SessionFile>> {
421 (**self).read_file(session_id, path).await
422 }
423
424 async fn write_file(
425 &self,
426 session_id: SessionId,
427 path: &str,
428 content: &str,
429 encoding: &str,
430 ) -> Result<SessionFile> {
431 (**self)
432 .write_file(session_id, path, content, encoding)
433 .await
434 }
435
436 async fn write_file_if_content_matches(
437 &self,
438 session_id: SessionId,
439 path: &str,
440 expected_content: &str,
441 expected_encoding: &str,
442 content: &str,
443 encoding: &str,
444 ) -> Result<Option<SessionFile>> {
445 (**self)
446 .write_file_if_content_matches(
447 session_id,
448 path,
449 expected_content,
450 expected_encoding,
451 content,
452 encoding,
453 )
454 .await
455 }
456
457 async fn delete_file(
458 &self,
459 session_id: SessionId,
460 path: &str,
461 recursive: bool,
462 ) -> Result<bool> {
463 (**self).delete_file(session_id, path, recursive).await
464 }
465
466 async fn list_directory(&self, session_id: SessionId, path: &str) -> Result<Vec<FileInfo>> {
467 (**self).list_directory(session_id, path).await
468 }
469
470 async fn stat_file(&self, session_id: SessionId, path: &str) -> Result<Option<FileStat>> {
471 (**self).stat_file(session_id, path).await
472 }
473
474 async fn grep_files(
475 &self,
476 session_id: SessionId,
477 pattern: &str,
478 path_pattern: Option<&str>,
479 ) -> Result<Vec<GrepMatch>> {
480 (**self).grep_files(session_id, pattern, path_pattern).await
481 }
482
483 async fn create_directory(&self, session_id: SessionId, path: &str) -> Result<FileInfo> {
484 (**self).create_directory(session_id, path).await
485 }
486
487 async fn seed_initial_file(&self, session_id: SessionId, file: &InitialFile) -> Result<()> {
488 (**self).seed_initial_file(session_id, file).await
489 }
490}
491
492pub use SessionFileSystem as SessionFileStore;
494
495#[derive(Clone, Default)]
501pub struct SessionFileSystemFactoryContext {
502 values: Arc<HashMap<TypeId, Arc<dyn Any + Send + Sync>>>,
503}
504
505impl SessionFileSystemFactoryContext {
506 pub fn new() -> Self {
507 Self::default()
508 }
509
510 pub fn with<T: Any + Send + Sync>(mut self, value: Arc<T>) -> Self {
511 let values = Arc::make_mut(&mut self.values);
512 values.insert(TypeId::of::<T>(), value);
513 self
514 }
515
516 pub fn get<T: Any + Send + Sync>(&self) -> Option<Arc<T>> {
517 self.values
518 .get(&TypeId::of::<T>())
519 .and_then(|value| value.clone().downcast::<T>().ok())
520 }
521}
522
523#[async_trait]
525pub trait SessionFileSystemFactory: Send + Sync {
526 fn name(&self) -> &'static str {
528 "SessionFileSystemFactory"
529 }
530
531 fn is_disabled(&self) -> bool {
534 false
535 }
536
537 async fn create_session_file_system(
539 &self,
540 context: SessionFileSystemFactoryContext,
541 ) -> Result<Arc<dyn SessionFileSystem>>;
542}
543
544#[derive(Debug, Clone, Default)]
546pub struct DisabledSessionFileSystemFactory;
547
548#[async_trait]
549impl SessionFileSystemFactory for DisabledSessionFileSystemFactory {
550 fn name(&self) -> &'static str {
551 "DisabledSessionFileSystemFactory"
552 }
553
554 fn is_disabled(&self) -> bool {
555 true
556 }
557
558 async fn create_session_file_system(
559 &self,
560 _context: SessionFileSystemFactoryContext,
561 ) -> Result<Arc<dyn SessionFileSystem>> {
562 Err(crate::error::AgentLoopError::config(
563 "session filesystem is disabled",
564 ))
565 }
566}
567
568#[derive(Debug, Clone)]
574pub struct KeyInfo {
575 pub key: String,
576 pub created_at: chrono::DateTime<chrono::Utc>,
577 pub updated_at: chrono::DateTime<chrono::Utc>,
578}
579
580#[derive(Debug, Clone)]
582pub struct SecretInfo {
583 pub name: String,
584 pub created_at: chrono::DateTime<chrono::Utc>,
585 pub updated_at: chrono::DateTime<chrono::Utc>,
586}
587
588#[async_trait]
598pub trait SessionStorageStore: Send + Sync {
599 async fn set_value(&self, session_id: SessionId, key: &str, value: &str) -> Result<()>;
603
604 async fn get_value(&self, session_id: SessionId, key: &str) -> Result<Option<String>>;
606
607 async fn delete_value(&self, session_id: SessionId, key: &str) -> Result<bool>;
609
610 async fn list_keys(&self, session_id: SessionId) -> Result<Vec<KeyInfo>>;
612
613 async fn set_secret(&self, session_id: SessionId, name: &str, value: &str) -> Result<()>;
617
618 async fn get_secret(&self, session_id: SessionId, name: &str) -> Result<Option<String>>;
620
621 async fn delete_secret(&self, session_id: SessionId, name: &str) -> Result<bool>;
623
624 async fn list_secrets(&self, session_id: SessionId) -> Result<Vec<SecretInfo>>;
626}
627
628use crate::session_schedule::SessionSchedule;
633use crate::typed_id::ScheduleId;
634
635#[async_trait]
639pub trait SessionScheduleStore: Send + Sync {
640 async fn create_schedule(
642 &self,
643 session_id: SessionId,
644 description: String,
645 cron_expression: Option<String>,
646 scheduled_at: Option<chrono::DateTime<chrono::Utc>>,
647 timezone: String,
648 ) -> Result<SessionSchedule>;
649
650 async fn cancel_schedule(
652 &self,
653 session_id: SessionId,
654 schedule_id: ScheduleId,
655 ) -> Result<SessionSchedule>;
656
657 async fn list_schedules(&self, session_id: SessionId) -> Result<Vec<SessionSchedule>>;
659
660 async fn count_active_schedules(&self, session_id: SessionId) -> Result<u32>;
662}
663
664#[async_trait]
674pub trait SessionResourceRegistry: Send + Sync {
675 async fn register(
677 &self,
678 entry: crate::session_resource::RegisterSessionResource,
679 ) -> Result<crate::session_resource::SessionResourceEntry>;
680
681 async fn update_status(
683 &self,
684 session_id: SessionId,
685 resource_id: &str,
686 status: crate::session_resource::SessionResourceStatus,
687 ) -> Result<Option<crate::session_resource::SessionResourceEntry>>;
688
689 async fn get(
691 &self,
692 session_id: SessionId,
693 resource_id: &str,
694 ) -> Result<Option<crate::session_resource::SessionResourceEntry>>;
695
696 async fn list(
698 &self,
699 session_id: SessionId,
700 filter: Option<&crate::session_resource::SessionResourceFilter>,
701 ) -> Result<Vec<crate::session_resource::SessionResourceEntry>>;
702
703 async fn deregister(&self, session_id: SessionId, resource_id: &str) -> Result<bool>;
705}
706
707#[async_trait]
717pub trait LeasedResourceStore: Send + Sync {
718 async fn upsert_resource(&self, input: UpsertLeasedResource) -> Result<LeasedResource>;
724
725 async fn release_resource(
731 &self,
732 session_id: SessionId,
733 provider: &str,
734 resource_type: &str,
735 external_id: &str,
736 ) -> Result<Option<LeasedResource>>;
737
738 async fn list_resources(&self, session_id: SessionId) -> Result<Vec<LeasedResource>>;
743}
744
745pub type SessionSqlDbStoreRef = Arc<dyn crate::session_sqldb::SessionSqlDbStore>;
751
752#[async_trait]
757pub trait UserConnectionResolver: Send + Sync {
758 async fn get_connection_token(
761 &self,
762 session_id: SessionId,
763 provider: &str,
764 ) -> Result<Option<String>>;
765
766 async fn get_connection_user(
771 &self,
772 _session_id: SessionId,
773 _provider: &str,
774 ) -> Result<Option<Uuid>> {
775 Ok(None)
776 }
777
778 async fn get_connection_token_for_user(
783 &self,
784 _user_id: Uuid,
785 _provider: &str,
786 ) -> Result<Option<String>> {
787 Ok(None)
788 }
789
790 async fn get_connection_metadata(
793 &self,
794 _session_id: SessionId,
795 _provider: &str,
796 ) -> Result<Option<serde_json::Value>> {
797 Ok(None)
798 }
799}
800
801#[async_trait]
811pub trait BudgetChecker: Send + Sync {
812 async fn check_budgets(&self, session_id: &str) -> Result<crate::budget::BudgetToolResponse>;
814}
815
816#[async_trait]
825pub trait PaymentAuthority: Send + Sync {
826 async fn execute_machine_payment(
827 &self,
828 session_id: SessionId,
829 request: crate::payment::MachinePaymentRequest,
830 ) -> Result<crate::payment::MachinePaymentResponse>;
831}
832
833#[derive(Clone)]
842pub struct ToolContext {
843 pub session_id: SessionId,
845
846 pub file_store: Option<Arc<dyn SessionFileSystem>>,
848
849 pub storage_store: Option<Arc<dyn SessionStorageStore>>,
851
852 pub image_store: Option<Arc<dyn ImageArtifactStore>>,
854
855 pub provider_credential_store: Option<Arc<dyn ProviderCredentialStore>>,
857
858 pub utility_llm_service: Option<Arc<dyn crate::UtilityLlmService>>,
860
861 pub sqldb_store: Option<SessionSqlDbStoreRef>,
863
864 pub message_retriever: Option<Arc<dyn crate::message_retriever::MessageRetriever>>,
866
867 pub session_store: Option<Arc<dyn SessionStore>>,
869
870 pub session_mutator: Option<Arc<dyn SessionMutator>>,
872
873 pub agent_store: Option<Arc<dyn AgentStore>>,
875
876 pub connection_resolver: Option<Arc<dyn UserConnectionResolver>>,
878
879 pub schedule_store: Option<Arc<dyn SessionScheduleStore>>,
881
882 pub platform_store: Option<Arc<dyn crate::platform_store::PlatformStore>>,
884 pub leased_resource_store: Option<Arc<dyn LeasedResourceStore>>,
886
887 pub session_resource_registry: Option<Arc<dyn SessionResourceRegistry>>,
889
890 pub event_emitter: Option<Arc<dyn EventEmitter>>,
893
894 pub event_context: Option<crate::events::EventContext>,
897
898 pub tool_call_id: Option<String>,
901 pub capability_registry: Option<crate::capabilities::CapabilityRegistry>,
903
904 pub tool_registry: Option<Arc<crate::tools::ToolRegistry>>,
907
908 pub memory_store: Option<Arc<dyn crate::memory_store::MemoryStoreBackend>>,
910
911 pub org_id: Option<crate::typed_id::OrgId>,
913
914 pub network_access: Option<crate::network_access::NetworkAccessList>,
917
918 pub locale: Option<String>,
922
923 pub budget_checker: Option<Arc<dyn BudgetChecker>>,
925
926 pub payment_authority: Option<Arc<dyn PaymentAuthority>>,
928}
929
930impl ToolContext {
931 pub fn new(session_id: SessionId) -> Self {
933 Self {
934 session_id,
935 file_store: None,
936 storage_store: None,
937 image_store: None,
938 provider_credential_store: None,
939 utility_llm_service: None,
940 sqldb_store: None,
941 message_retriever: None,
942 session_store: None,
943 session_mutator: None,
944 agent_store: None,
945 connection_resolver: None,
946 schedule_store: None,
947 platform_store: None,
948 leased_resource_store: None,
949 session_resource_registry: None,
950 event_emitter: None,
951 event_context: None,
952 tool_call_id: None,
953 capability_registry: None,
954 tool_registry: None,
955 memory_store: None,
956 org_id: None,
957 network_access: None,
958 locale: None,
959 budget_checker: None,
960 payment_authority: None,
961 }
962 }
963
964 pub fn with_file_store(session_id: SessionId, file_store: Arc<dyn SessionFileSystem>) -> Self {
966 Self {
967 session_id,
968 file_store: Some(file_store),
969 storage_store: None,
970 image_store: None,
971 provider_credential_store: None,
972 utility_llm_service: None,
973 sqldb_store: None,
974 message_retriever: None,
975 session_store: None,
976 session_mutator: None,
977 agent_store: None,
978 connection_resolver: None,
979 schedule_store: None,
980 platform_store: None,
981 leased_resource_store: None,
982 session_resource_registry: None,
983 event_emitter: None,
984 event_context: None,
985 tool_call_id: None,
986 capability_registry: None,
987 tool_registry: None,
988 memory_store: None,
989 org_id: None,
990 network_access: None,
991 locale: None,
992 budget_checker: None,
993 payment_authority: None,
994 }
995 }
996
997 pub fn with_storage_store(
999 session_id: SessionId,
1000 storage_store: Arc<dyn SessionStorageStore>,
1001 ) -> Self {
1002 Self {
1003 session_id,
1004 file_store: None,
1005 storage_store: Some(storage_store),
1006 image_store: None,
1007 provider_credential_store: None,
1008 utility_llm_service: None,
1009 sqldb_store: None,
1010 message_retriever: None,
1011 session_store: None,
1012 session_mutator: None,
1013 agent_store: None,
1014 connection_resolver: None,
1015 schedule_store: None,
1016 platform_store: None,
1017 leased_resource_store: None,
1018 session_resource_registry: None,
1019 event_emitter: None,
1020 event_context: None,
1021 tool_call_id: None,
1022 capability_registry: None,
1023 tool_registry: None,
1024 memory_store: None,
1025 org_id: None,
1026 network_access: None,
1027 locale: None,
1028 budget_checker: None,
1029 payment_authority: None,
1030 }
1031 }
1032
1033 pub fn with_stores(
1035 session_id: SessionId,
1036 file_store: Arc<dyn SessionFileSystem>,
1037 storage_store: Arc<dyn SessionStorageStore>,
1038 ) -> Self {
1039 Self {
1040 session_id,
1041 file_store: Some(file_store),
1042 storage_store: Some(storage_store),
1043 sqldb_store: None,
1044 image_store: None,
1045 provider_credential_store: None,
1046 utility_llm_service: None,
1047 message_retriever: None,
1048 session_store: None,
1049 session_mutator: None,
1050 agent_store: None,
1051 connection_resolver: None,
1052 schedule_store: None,
1053 platform_store: None,
1054 leased_resource_store: None,
1055 session_resource_registry: None,
1056 event_emitter: None,
1057 event_context: None,
1058 tool_call_id: None,
1059 capability_registry: None,
1060 tool_registry: None,
1061 memory_store: None,
1062 org_id: None,
1063 network_access: None,
1064 locale: None,
1065 budget_checker: None,
1066 payment_authority: None,
1067 }
1068 }
1069
1070 pub fn with_sqldb_store(mut self, sqldb_store: SessionSqlDbStoreRef) -> Self {
1072 self.sqldb_store = Some(sqldb_store);
1073 self
1074 }
1075
1076 pub fn with_message_retriever(
1078 mut self,
1079 retriever: Arc<dyn crate::message_retriever::MessageRetriever>,
1080 ) -> Self {
1081 self.message_retriever = Some(retriever);
1082 self
1083 }
1084
1085 pub fn with_session_store(mut self, store: Arc<dyn SessionStore>) -> Self {
1087 self.session_store = Some(store);
1088 self
1089 }
1090
1091 pub fn with_session_mutator(mut self, mutator: Arc<dyn SessionMutator>) -> Self {
1093 self.session_mutator = Some(mutator);
1094 self
1095 }
1096
1097 pub fn with_agent_store(mut self, store: Arc<dyn AgentStore>) -> Self {
1099 self.agent_store = Some(store);
1100 self
1101 }
1102
1103 pub fn with_connection_resolver(mut self, resolver: Arc<dyn UserConnectionResolver>) -> Self {
1105 self.connection_resolver = Some(resolver);
1106 self
1107 }
1108
1109 pub fn with_image_store(
1111 session_id: SessionId,
1112 image_store: Arc<dyn ImageArtifactStore>,
1113 ) -> Self {
1114 Self {
1115 session_id,
1116 file_store: None,
1117 storage_store: None,
1118 image_store: Some(image_store),
1119 provider_credential_store: None,
1120 utility_llm_service: None,
1121 sqldb_store: None,
1122 message_retriever: None,
1123 session_store: None,
1124 session_mutator: None,
1125 agent_store: None,
1126 connection_resolver: None,
1127 schedule_store: None,
1128 platform_store: None,
1129 leased_resource_store: None,
1130 session_resource_registry: None,
1131 event_emitter: None,
1132 event_context: None,
1133 tool_call_id: None,
1134 capability_registry: None,
1135 tool_registry: None,
1136 memory_store: None,
1137 org_id: None,
1138 network_access: None,
1139 locale: None,
1140 budget_checker: None,
1141 payment_authority: None,
1142 }
1143 }
1144
1145 pub fn with_provider_credential_store(
1147 mut self,
1148 store: Arc<dyn ProviderCredentialStore>,
1149 ) -> Self {
1150 self.provider_credential_store = Some(store);
1151 self
1152 }
1153
1154 pub fn with_utility_llm_service(mut self, service: Arc<dyn crate::UtilityLlmService>) -> Self {
1156 self.utility_llm_service = Some(service);
1157 self
1158 }
1159
1160 pub fn with_schedule_store(mut self, store: Arc<dyn SessionScheduleStore>) -> Self {
1162 self.schedule_store = Some(store);
1163 self
1164 }
1165
1166 pub fn with_platform_store(
1168 mut self,
1169 store: Arc<dyn crate::platform_store::PlatformStore>,
1170 ) -> Self {
1171 self.platform_store = Some(store);
1172 self
1173 }
1174
1175 pub fn with_leased_resource_store(mut self, store: Arc<dyn LeasedResourceStore>) -> Self {
1177 self.leased_resource_store = Some(store);
1178 self
1179 }
1180
1181 pub fn with_session_resource_registry(
1183 mut self,
1184 registry: Arc<dyn SessionResourceRegistry>,
1185 ) -> Self {
1186 self.session_resource_registry = Some(registry);
1187 self
1188 }
1189
1190 pub fn with_memory_store(
1192 mut self,
1193 store: Arc<dyn crate::memory_store::MemoryStoreBackend>,
1194 ) -> Self {
1195 self.memory_store = Some(store);
1196 self
1197 }
1198
1199 pub fn with_org_id(mut self, org_id: crate::typed_id::OrgId) -> Self {
1201 self.org_id = Some(org_id);
1202 self
1203 }
1204
1205 pub fn with_tool_registry(mut self, registry: Arc<crate::tools::ToolRegistry>) -> Self {
1207 self.tool_registry = Some(registry);
1208 self
1209 }
1210
1211 pub fn with_network_access(
1213 mut self,
1214 network_access: Option<crate::network_access::NetworkAccessList>,
1215 ) -> Self {
1216 self.network_access = network_access;
1217 self
1218 }
1219
1220 pub fn with_payment_authority(mut self, authority: Arc<dyn PaymentAuthority>) -> Self {
1222 self.payment_authority = Some(authority);
1223 self
1224 }
1225
1226 pub async fn emit_progress(&self, tool_name: &str, message: &str) {
1231 let (Some(emitter), Some(ctx), Some(call_id)) =
1232 (&self.event_emitter, &self.event_context, &self.tool_call_id)
1233 else {
1234 return;
1235 };
1236 if let Err(e) = emitter
1237 .emit(EventRequest::new(
1238 self.session_id,
1239 ctx.clone(),
1240 crate::events::ToolProgressData {
1241 tool_call_id: call_id.clone(),
1242 tool_name: tool_name.to_string(),
1243 message: message.to_string(),
1244 display_name: None,
1245 },
1246 ))
1247 .await
1248 {
1249 tracing::debug!(
1250 tool_call_id = call_id,
1251 tool_name,
1252 error = %e,
1253 "Failed to emit tool.progress event"
1254 );
1255 }
1256 }
1257
1258 pub async fn emit_tool_output(&self, tool_name: &str, delta: &str, stream: &str) {
1263 let (Some(emitter), Some(ctx), Some(call_id)) =
1264 (&self.event_emitter, &self.event_context, &self.tool_call_id)
1265 else {
1266 return;
1267 };
1268 if let Err(e) = emitter
1269 .emit(EventRequest::new(
1270 self.session_id,
1271 ctx.clone(),
1272 crate::events::ToolOutputDeltaData {
1273 tool_call_id: call_id.clone(),
1274 tool_name: tool_name.to_string(),
1275 delta: delta.to_string(),
1276 stream: stream.to_string(),
1277 },
1278 ))
1279 .await
1280 {
1281 tracing::debug!(
1282 tool_call_id = call_id,
1283 tool_name,
1284 error = %e,
1285 "Failed to emit tool.output.delta event"
1286 );
1287 }
1288 }
1289}
1290
1291impl std::fmt::Debug for ToolContext {
1292 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1293 f.debug_struct("ToolContext")
1294 .field("session_id", &self.session_id)
1295 .field("file_store", &self.file_store.is_some())
1296 .field("storage_store", &self.storage_store.is_some())
1297 .field("image_store", &self.image_store.is_some())
1298 .field(
1299 "provider_credential_store",
1300 &self.provider_credential_store.is_some(),
1301 )
1302 .field("utility_llm_service", &self.utility_llm_service.is_some())
1303 .field("sqldb_store", &self.sqldb_store.is_some())
1304 .field("message_retriever", &self.message_retriever.is_some())
1305 .field("session_store", &self.session_store.is_some())
1306 .field("session_mutator", &self.session_mutator.is_some())
1307 .field("agent_store", &self.agent_store.is_some())
1308 .field("connection_resolver", &self.connection_resolver.is_some())
1309 .field("schedule_store", &self.schedule_store.is_some())
1310 .field("platform_store", &self.platform_store.is_some())
1311 .field(
1312 "leased_resource_store",
1313 &self.leased_resource_store.is_some(),
1314 )
1315 .field("event_emitter", &self.event_emitter.is_some())
1316 .field("tool_registry", &self.tool_registry.is_some())
1317 .field("memory_store", &self.memory_store.is_some())
1318 .field("payment_authority", &self.payment_authority.is_some())
1319 .field("org_id", &self.org_id)
1320 .finish()
1321 }
1322}
1323
1324use crate::events::{Event, EventRequest};
1329
1330#[async_trait]
1341pub trait EventEmitter: Send + Sync {
1342 async fn emit(&self, request: EventRequest) -> Result<Event>;
1347}
1348
1349#[async_trait]
1351impl<E: EventEmitter + ?Sized> EventEmitter for Arc<E> {
1352 async fn emit(&self, request: EventRequest) -> Result<Event> {
1353 (**self).emit(request).await
1354 }
1355}
1356
1357#[derive(Debug, Clone, Default)]
1361pub struct NoopEventEmitter;
1362
1363#[async_trait]
1364impl EventEmitter for NoopEventEmitter {
1365 async fn emit(&self, request: EventRequest) -> Result<Event> {
1366 Ok(request.into_event(crate::typed_id::EventId::new(), 0))
1368 }
1369}
1370
1371#[derive(Debug, Clone)]
1384pub struct ResolvedImage {
1385 pub base64: String,
1387 pub media_type: String,
1389}
1390
1391impl ResolvedImage {
1392 pub fn new(base64: impl Into<String>, media_type: impl Into<String>) -> Self {
1394 Self {
1395 base64: base64.into(),
1396 media_type: media_type.into(),
1397 }
1398 }
1399
1400 pub fn to_data_url(&self) -> String {
1404 format!("data:{};base64,{}", self.media_type, self.base64)
1405 }
1406}
1407
1408#[async_trait]
1441pub trait ImageResolver: Send + Sync {
1442 async fn resolve_image(&self, image_id: Uuid) -> Result<Option<ResolvedImage>>;
1446}
1447
1448#[cfg(test)]
1453mod tests {
1454 use super::*;
1455
1456 #[test]
1457 fn test_resolved_image_new() {
1458 let image = ResolvedImage::new("SGVsbG8=", "image/png");
1459 assert_eq!(image.base64, "SGVsbG8=");
1460 assert_eq!(image.media_type, "image/png");
1461 }
1462
1463 #[test]
1464 fn test_resolved_image_to_data_url() {
1465 let image = ResolvedImage::new("SGVsbG8=", "image/png");
1466 let data_url = image.to_data_url();
1467 assert_eq!(data_url, "data:image/png;base64,SGVsbG8=");
1468 }
1469
1470 #[test]
1471 fn test_resolved_image_jpeg() {
1472 let image = ResolvedImage::new("base64data", "image/jpeg");
1473 let data_url = image.to_data_url();
1474 assert!(data_url.starts_with("data:image/jpeg;base64,"));
1475 }
1476}