monoloop_loop/transaction/
channel_registry.rs1use monoloop_connector::{ConnectorFactory, ConnectorInstance};
4use monoloop_contracts::{
5 ChannelCapabilities, ChannelDefaults, ChannelDescriptor, ChannelId, ChannelKind, ChannelLimits,
6 OutboundDialectEncoder, ToolExecutionMode,
7};
8use monoloop_interpreter::InterpreterFactory;
9use std::collections::HashMap;
10use std::sync::Arc;
11
12pub struct ChannelBinding {
14 pub id: ChannelId,
16 pub kind: ChannelKind,
18 pub tool_mode: ToolExecutionMode,
20 pub connector_factory: Arc<dyn ConnectorFactory>,
22 pub encoder: Arc<dyn OutboundDialectEncoder>,
24 pub interpreter: Arc<dyn InterpreterFactory>,
26 pub endpoint_ref: String,
28 pub credential_ref: Option<String>,
30 pub defaults: ChannelDefaults,
32 pub capabilities: ChannelCapabilities,
34 pub limits: ChannelLimits,
36}
37
38impl ChannelBinding {
39 pub fn descriptor(&self) -> ChannelDescriptor {
41 ChannelDescriptor {
42 kind: self.kind,
43 tool_mode: self.tool_mode,
44 capabilities: self.capabilities.clone(),
45 limits: self.limits.clone(),
46 }
47 }
48}
49
50pub struct ChannelRegistry {
52 channels: HashMap<ChannelId, ChannelBinding>,
53}
54
55impl ChannelRegistry {
56 pub fn build(bindings: Vec<ChannelBinding>) -> Result<Self, super::StartupError> {
58 if bindings.is_empty() {
59 return Err(super::StartupError::ChannelRegistry(
60 "at least one Channel is required",
61 ));
62 }
63 let mut channels = HashMap::with_capacity(bindings.len());
64 for b in bindings {
65 b.descriptor().validate()?;
66 if channels.contains_key(&b.id) {
67 return Err(super::StartupError::ChannelRegistry("duplicate ChannelId"));
68 }
69 channels.insert(b.id.clone(), b);
72 }
73 Ok(Self { channels })
74 }
75
76 pub fn iter(&self) -> impl Iterator<Item = (&ChannelId, &ChannelBinding)> {
78 self.channels.iter()
79 }
80
81 pub fn get(&self, id: &ChannelId) -> Option<&ChannelBinding> {
83 self.channels.get(id)
84 }
85
86 pub fn len(&self) -> usize {
88 self.channels.len()
89 }
90
91 pub fn is_empty(&self) -> bool {
93 self.channels.is_empty()
94 }
95}
96
97pub struct LiveChannel {
99 pub binding: ChannelBinding,
101 pub instance: ConnectorInstance,
103}