agent-stream-kit 0.19.0

Agent Stream Kit
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
use std::sync::{Arc, Mutex};

use serde_json::Value;
use tokio::sync::{Mutex as AsyncMutex, broadcast, broadcast::error::RecvError, mpsc};

use crate::FnvIndexMap;
use crate::agent::{Agent, AgentMessage, AgentStatus, agent_new};
use crate::config::{AgentConfigs, AgentConfigsMap};
use crate::context::AgentContext;
use crate::definition::{AgentConfigSpecs, AgentDefinition, AgentDefinitions};
use crate::error::AgentError;
use crate::id::{new_id, update_ids};
use crate::message::{self, AgentEventMessage};
use crate::registry;
use crate::spec::{AgentSpec, AgentStreamSpec, ChannelSpec};
use crate::stream::{AgentStream, AgentStreamInfo, AgentStreams};
use crate::value::AgentValue;

const MESSAGE_LIMIT: usize = 1024;
const EVENT_CHANNEL_CAPACITY: usize = 256;

#[derive(Clone)]
pub struct ASKit {
    // agent id -> agent
    pub(crate) agents: Arc<Mutex<FnvIndexMap<String, Arc<AsyncMutex<Box<dyn Agent>>>>>>,

    // agent id -> sender
    pub(crate) agent_txs: Arc<Mutex<FnvIndexMap<String, mpsc::Sender<AgentMessage>>>>,

    // board name -> [board out agent id]
    pub(crate) board_out_agents: Arc<Mutex<FnvIndexMap<String, Vec<String>>>>,

    // board name -> value
    pub(crate) board_value: Arc<Mutex<FnvIndexMap<String, AgentValue>>>,

    // source agent id -> [target agent id / source handle / target handle]
    pub(crate) channels: Arc<Mutex<FnvIndexMap<String, Vec<(String, String, String)>>>>,

    // agent def name -> agent definition
    pub(crate) defs: Arc<Mutex<AgentDefinitions>>,

    // agent streams (stream id -> stream)
    pub(crate) streams: Arc<Mutex<AgentStreams>>,

    // agent def name -> config
    pub(crate) global_configs_map: Arc<Mutex<FnvIndexMap<String, AgentConfigs>>>,

    // message sender
    pub(crate) tx: Arc<Mutex<Option<mpsc::Sender<AgentEventMessage>>>>,

    // observers
    pub(crate) observers: broadcast::Sender<ASKitEvent>,
}

impl ASKit {
    pub fn new() -> Self {
        let (tx, _rx) = broadcast::channel(EVENT_CHANNEL_CAPACITY);
        Self {
            agents: Default::default(),
            agent_txs: Default::default(),
            board_out_agents: Default::default(),
            board_value: Default::default(),
            channels: Default::default(),
            defs: Default::default(),
            streams: Default::default(),
            global_configs_map: Default::default(),
            tx: Arc::new(Mutex::new(None)),
            observers: tx,
        }
    }

    pub(crate) fn tx(&self) -> Result<mpsc::Sender<AgentEventMessage>, AgentError> {
        self.tx
            .lock()
            .unwrap()
            .clone()
            .ok_or(AgentError::TxNotInitialized)
    }

    /// Initialize ASKit.
    pub fn init() -> Result<Self, AgentError> {
        let askit = Self::new();
        askit.register_agents();
        Ok(askit)
    }

    fn register_agents(&self) {
        registry::register_inventory_agents(self);
    }

    /// Prepare ASKit to be ready.
    pub async fn ready(&self) -> Result<(), AgentError> {
        self.spawn_message_loop().await?;
        Ok(())
    }

    /// Quit ASKit.
    pub fn quit(&self) {
        let mut tx_lock = self.tx.lock().unwrap();
        *tx_lock = None;
    }

    /// Register an agent definition.
    pub fn register_agent_definiton(&self, def: AgentDefinition) {
        let def_name = def.name.clone();
        let def_global_configs = def.global_configs.clone();

        let mut defs = self.defs.lock().unwrap();
        defs.insert(def.name.clone(), def);

        // if there is a global config, set it
        if let Some(def_global_configs) = def_global_configs {
            let mut new_configs = AgentConfigs::default();
            for (key, config_entry) in def_global_configs.iter() {
                new_configs.set(key.clone(), config_entry.value.clone());
            }
            self.set_global_configs(def_name, new_configs);
        }
    }

    /// Get all agent definitions.
    pub fn get_agent_definitions(&self) -> AgentDefinitions {
        let defs = self.defs.lock().unwrap();
        defs.clone()
    }

    /// Get an agent definition by name.
    pub fn get_agent_definition(&self, def_name: &str) -> Option<AgentDefinition> {
        let defs = self.defs.lock().unwrap();
        defs.get(def_name).cloned()
    }

    /// Get the config specs of an agent definition by name.
    pub fn get_agent_config_specs(&self, def_name: &str) -> Option<AgentConfigSpecs> {
        let defs = self.defs.lock().unwrap();
        let Some(def) = defs.get(def_name) else {
            return None;
        };
        def.configs.clone()
    }

    /// Get the agent spec by id.
    pub async fn get_agent_spec(&self, agent_id: &str) -> Option<AgentSpec> {
        let agent = {
            let agents = self.agents.lock().unwrap();
            let Some(agent) = agents.get(agent_id) else {
                return None;
            };
            agent.clone()
        };
        let agent = agent.lock().await;
        Some(agent.spec().clone())
    }

    /// Update the agent spec by id.
    pub async fn update_agent_spec(&self, agent_id: &str, value: &Value) -> Result<(), AgentError> {
        let agent = {
            let agents = self.agents.lock().unwrap();
            let Some(agent) = agents.get(agent_id) else {
                return Err(AgentError::AgentNotFound(agent_id.to_string()));
            };
            agent.clone()
        };
        let mut agent = agent.lock().await;
        agent.update_spec(value)?;
        Ok(())
    }

    // streams

    /// Get info of the agent stream by id.
    pub fn get_agent_stream_info(&self, id: &str) -> Option<AgentStreamInfo> {
        let streams = self.streams.lock().unwrap();
        streams.get(id).map(|stream| stream.into())
    }

    /// Get infos of all agent streams.
    pub fn get_agent_stream_infos(&self) -> Vec<AgentStreamInfo> {
        let streams = self.streams.lock().unwrap();
        streams.values().map(|s| s.into()).collect()
    }

    /// Get the agent stream spec by id.
    pub async fn get_agent_stream_spec(&self, id: &str) -> Option<AgentStreamSpec> {
        let stream_spec = {
            let streams = self.streams.lock().unwrap();
            streams.get(id).map(|stream| stream.spec().clone())
        };
        let Some(mut stream_spec) = stream_spec else {
            return None;
        };

        // collect agent specs in the stream
        let mut agent_specs = Vec::new();
        for agent in &stream_spec.agents {
            if let Some(spec) = self.get_agent_spec(&agent.id).await {
                agent_specs.push(spec);
            }
        }
        stream_spec.agents = agent_specs;

        // No need to change channels

        Some(stream_spec)
    }

    /// Update the agent stream spec
    pub fn update_agent_stream_spec(&self, id: &str, value: &Value) -> Result<(), AgentError> {
        let mut streams = self.streams.lock().unwrap();
        let Some(stream) = streams.get_mut(id) else {
            return Err(AgentError::StreamNotFound(id.to_string()));
        };
        stream.update_spec(value)?;
        Ok(())
    }

    /// Create a new agent stream with the given name.
    /// If the name already exists, a unique name will be generated by appending a number suffix.
    /// Returns the id of the new agent stream.
    pub fn new_agent_stream(&self, name: &str) -> Result<String, AgentError> {
        if !is_valid_stream_name(name) {
            return Err(AgentError::InvalidStreamName(name.into()));
        }
        let new_name = self.unique_stream_name(name);
        let spec = AgentStreamSpec::default();
        let id = self.add_agent_stream(new_name, spec)?;
        Ok(id)
    }

    /// Rename an existing agent stream.
    pub fn rename_agent_stream(&self, id: &str, new_name: &str) -> Result<String, AgentError> {
        if !is_valid_stream_name(new_name) {
            return Err(AgentError::InvalidStreamName(new_name.into()));
        }

        // check if the new name is already used
        let new_name = self.unique_stream_name(new_name);

        let mut streams = self.streams.lock().unwrap();

        // remove the original stream
        let Some(mut stream) = streams.swap_remove(id) else {
            return Err(AgentError::RenameStreamFailed(id.into()));
        };

        // insert renamed stream
        stream.set_name(new_name.clone());
        streams.insert(stream.id().to_string(), stream);
        Ok(new_name)
    }

    /// Generate a unique stream name by appending a number suffix if needed.
    pub fn unique_stream_name(&self, name: &str) -> String {
        let mut new_name = name.trim().to_string();
        let mut i = 2;
        let streams = self.streams.lock().unwrap();
        while streams.values().any(|stream| stream.name() == new_name) {
            new_name = format!("{}{}", name, i);
            i += 1;
        }
        new_name
    }

    /// Add a new agent stream with the given name and spec, and returns the id of the new agent stream.
    ///
    /// The ids of the given spec, including agents and channels, are changed to new unique ids.
    pub fn add_agent_stream(
        &self,
        name: String,
        spec: AgentStreamSpec,
    ) -> Result<String, AgentError> {
        let stream = AgentStream::new(name, spec);
        let id = stream.id().to_string();

        // add agents
        for agent in &stream.spec().agents {
            if let Err(e) = self.add_agent_internal(id.clone(), agent.clone()) {
                log::error!("Failed to add_agent {}: {}", agent.id, e);
            }
        }

        // add channels
        for channel in &stream.spec().channels {
            self.add_channel_internal(channel.clone())
                .unwrap_or_else(|e| {
                    log::error!("Failed to add_channel {}: {}", channel.source, e);
                });
        }

        // add the given stream into streams
        let mut streams = self.streams.lock().unwrap();
        if streams.contains_key(&id) {
            return Err(AgentError::DuplicateId(id.into()));
        }
        streams.insert(id.to_string(), stream);

        Ok(id)
    }

    /// Remove an agent stream by id.
    pub async fn remove_agent_stream(&self, id: &str) -> Result<(), AgentError> {
        let mut stream = {
            let mut streams = self.streams.lock().unwrap();
            let Some(stream) = streams.swap_remove(id) else {
                return Err(AgentError::StreamNotFound(id.to_string()));
            };
            stream
        };

        stream.stop(self).await.unwrap_or_else(|e| {
            log::error!("Failed to stop stream {}: {}", id, e);
        });

        // Remove all agents and channels associated with the stream
        for agent in &stream.spec().agents {
            self.remove_agent_internal(&agent.id)
                .await
                .unwrap_or_else(|e| {
                    log::error!("Failed to remove_agent {}: {}", agent.id, e);
                });
        }
        for channel in &stream.spec().channels {
            self.remove_channel_internal(channel);
        }

        Ok(())
    }

    /// Start an agent stream by id.
    pub async fn start_agent_stream(&self, id: &str) -> Result<(), AgentError> {
        let mut stream = {
            let mut streams = self.streams.lock().unwrap();
            let Some(stream) = streams.swap_remove(id) else {
                return Err(AgentError::StreamNotFound(id.to_string()));
            };
            stream
        };

        stream.start(self).await?;

        let mut streams = self.streams.lock().unwrap();
        streams.insert(id.to_string(), stream);
        Ok(())
    }

    /// Stop an agent stream by id.
    pub async fn stop_agent_stream(&self, id: &str) -> Result<(), AgentError> {
        let mut stream = {
            let mut streams = self.streams.lock().unwrap();
            let Some(stream) = streams.swap_remove(id) else {
                return Err(AgentError::StreamNotFound(id.to_string()));
            };
            stream
        };

        stream.stop(self).await?;

        let mut streams = self.streams.lock().unwrap();
        streams.insert(id.to_string(), stream);
        Ok(())
    }

    // Agents

    /// Create a new agent spec from the given agent definition name.
    pub fn new_agent_spec(&self, def_name: &str) -> Result<AgentSpec, AgentError> {
        let def = self
            .get_agent_definition(def_name)
            .ok_or_else(|| AgentError::AgentDefinitionNotFound(def_name.to_string()))?;
        Ok(def.to_spec())
    }

    /// Add an agent to the specified stream, and returns the id of the newly added agent.
    pub fn add_agent(&self, stream_id: String, mut spec: AgentSpec) -> Result<String, AgentError> {
        let mut streams = self.streams.lock().unwrap();
        let Some(stream) = streams.get_mut(&stream_id) else {
            return Err(AgentError::StreamNotFound(stream_id.to_string()));
        };
        let id = new_id();
        spec.id = id.clone();
        self.add_agent_internal(stream_id, spec.clone())?;
        stream.add_agent(spec.clone());
        Ok(id)
    }

    fn add_agent_internal(&self, stream_id: String, spec: AgentSpec) -> Result<(), AgentError> {
        let mut agents = self.agents.lock().unwrap();
        if agents.contains_key(&spec.id) {
            return Err(AgentError::AgentAlreadyExists(spec.id.to_string()));
        }
        let spec_id = spec.id.clone();
        let mut agent = agent_new(self.clone(), spec_id.clone(), spec)?;
        agent.set_stream_id(stream_id);
        agents.insert(spec_id, Arc::new(AsyncMutex::new(agent)));
        Ok(())
    }

    /// Get the agent by id.
    pub fn get_agent(&self, agent_id: &str) -> Option<Arc<AsyncMutex<Box<dyn Agent>>>> {
        let agents = self.agents.lock().unwrap();
        agents.get(agent_id).cloned()
    }

    /// Add a channel to the specified stream.
    pub fn add_channel(&self, stream_id: &str, channel: ChannelSpec) -> Result<(), AgentError> {
        // check if the source and target agents exist
        {
            let agents = self.agents.lock().unwrap();
            if !agents.contains_key(&channel.source) {
                return Err(AgentError::AgentNotFound(channel.source.to_string()));
            }
            if !agents.contains_key(&channel.target) {
                return Err(AgentError::AgentNotFound(channel.target.to_string()));
            }
        }

        // check if handles are valid
        if channel.source_handle.is_empty() {
            return Err(AgentError::EmptySourceHandle);
        }
        if channel.target_handle.is_empty() {
            return Err(AgentError::EmptyTargetHandle);
        }

        let mut streams = self.streams.lock().unwrap();
        let Some(stream) = streams.get_mut(stream_id) else {
            return Err(AgentError::StreamNotFound(stream_id.to_string()));
        };
        stream.add_channel(channel.clone());
        self.add_channel_internal(channel)?;
        Ok(())
    }

    fn add_channel_internal(&self, channel: ChannelSpec) -> Result<(), AgentError> {
        let mut channels = self.channels.lock().unwrap();
        if let Some(targets) = channels.get_mut(&channel.source) {
            if targets
                .iter()
                .any(|(target, source_handle, target_handle)| {
                    *target == channel.target
                        && *source_handle == channel.source_handle
                        && *target_handle == channel.target_handle
                })
            {
                return Err(AgentError::ChannelAlreadyExists);
            }
            targets.push((channel.target, channel.source_handle, channel.target_handle));
        } else {
            channels.insert(
                channel.source,
                vec![(channel.target, channel.source_handle, channel.target_handle)],
            );
        }
        Ok(())
    }

    /// Add agents and channels to the specified stream.
    ///
    /// The ids of the given agents and channels are changed to new unique ids.
    /// The agents are not started automatically, even if the stream is running.
    pub fn add_agents_and_channels(
        &self,
        stream_id: &str,
        agents: &Vec<AgentSpec>,
        channels: &Vec<ChannelSpec>,
    ) -> Result<(Vec<AgentSpec>, Vec<ChannelSpec>), AgentError> {
        let (agents, channels) = update_ids(agents, channels);

        let mut streams = self.streams.lock().unwrap();
        let Some(stream) = streams.get_mut(stream_id) else {
            return Err(AgentError::StreamNotFound(stream_id.to_string()));
        };

        for agent in &agents {
            self.add_agent_internal(stream_id.to_string(), agent.clone())?;
            stream.add_agent(agent.clone());
        }

        for channel in &channels {
            self.add_channel_internal(channel.clone())?;
            stream.add_channel(channel.clone());
        }

        Ok((agents, channels))
    }

    /// Remove an agent from the specified stream.
    ///
    /// If the agent is running, it will be stopped first.
    pub async fn remove_agent(&self, stream_id: &str, agent_id: &str) -> Result<(), AgentError> {
        {
            let mut streams = self.streams.lock().unwrap();
            let Some(stream) = streams.get_mut(stream_id) else {
                return Err(AgentError::StreamNotFound(stream_id.to_string()));
            };
            stream.remove_agent(agent_id);
        }
        if let Err(e) = self.remove_agent_internal(agent_id).await {
            return Err(e);
        }
        Ok(())
    }

    async fn remove_agent_internal(&self, agent_id: &str) -> Result<(), AgentError> {
        self.stop_agent(agent_id).await?;

        // remove from channels
        {
            let mut channels = self.channels.lock().unwrap();
            let mut sources_to_remove = Vec::new();
            for (source, targets) in channels.iter_mut() {
                targets.retain(|(target, _, _)| target != agent_id);
                if targets.is_empty() {
                    sources_to_remove.push(source.clone());
                }
            }
            for source in sources_to_remove {
                channels.swap_remove(&source);
            }
            channels.swap_remove(agent_id);
        }

        // remove from agents
        {
            let mut agents = self.agents.lock().unwrap();
            agents.swap_remove(agent_id);
        }

        Ok(())
    }

    /// Remove a channel from the specified stream.
    pub fn remove_channel(&self, stream_id: &str, channel: &ChannelSpec) -> Result<(), AgentError> {
        let mut stream = {
            let mut streams = self.streams.lock().unwrap();
            let Some(stream) = streams.swap_remove(stream_id) else {
                return Err(AgentError::StreamNotFound(stream_id.to_string()));
            };
            stream
        };

        let Some(channel) = stream.remove_channel(channel) else {
            let mut streams = self.streams.lock().unwrap();
            streams.insert(stream_id.to_string(), stream);
            return Err(AgentError::ChannelNotFound(format!(
                "{}:{}->{}:{}",
                channel.source, channel.source_handle, channel.target, channel.target_handle
            )));
        };
        let mut streams = self.streams.lock().unwrap();
        streams.insert(stream_id.to_string(), stream);

        self.remove_channel_internal(&channel);
        Ok(())
    }

    fn remove_channel_internal(&self, channel: &ChannelSpec) {
        let mut channels = self.channels.lock().unwrap();
        if let Some(targets) = channels.get_mut(&channel.source) {
            targets.retain(|(target, source_handle, target_handle)| {
                *target != channel.target
                    || *source_handle != channel.source_handle
                    || *target_handle != channel.target_handle
            });
            if targets.is_empty() {
                channels.swap_remove(&channel.source);
            }
        }
    }

    /// Start an agent by id.
    pub async fn start_agent(&self, agent_id: &str) -> Result<(), AgentError> {
        let agent = {
            let agents = self.agents.lock().unwrap();
            let Some(a) = agents.get(agent_id) else {
                return Err(AgentError::AgentNotFound(agent_id.to_string()));
            };
            a.clone()
        };
        let def_name = {
            let agent = agent.lock().await;
            agent.def_name().to_string()
        };
        let uses_native_thread = {
            let defs = self.defs.lock().unwrap();
            let Some(def) = defs.get(&def_name) else {
                return Err(AgentError::AgentDefinitionNotFound(agent_id.to_string()));
            };
            def.native_thread
        };
        let agent_status = {
            // This will not block since the agent is not started yet.
            let agent = agent.lock().await;
            agent.status().clone()
        };
        if agent_status == AgentStatus::Init {
            log::info!("Starting agent {}", agent_id);

            let (tx, mut rx) = mpsc::channel(MESSAGE_LIMIT);

            {
                let mut agent_txs = self.agent_txs.lock().unwrap();
                agent_txs.insert(agent_id.to_string(), tx.clone());
            };

            let agent_clone = agent.clone();
            let agent_id_clone = agent_id.to_string();

            let agent_loop = async move {
                {
                    let mut agent_guard = agent_clone.lock().await;
                    if let Err(e) = agent_guard.start().await {
                        log::error!("Failed to start agent {}: {}", agent_id_clone, e);
                        return;
                    }
                }

                while let Some(message) = rx.recv().await {
                    match message {
                        AgentMessage::Input { ctx, pin, value } => {
                            agent_clone
                                .lock()
                                .await
                                .process(ctx, pin, value)
                                .await
                                .unwrap_or_else(|e| {
                                    log::error!("Process Error {}: {}", agent_id_clone, e);
                                });
                        }
                        AgentMessage::Config { key, value } => {
                            agent_clone
                                .lock()
                                .await
                                .set_config(key, value)
                                .unwrap_or_else(|e| {
                                    log::error!("Config Error {}: {}", agent_id_clone, e);
                                });
                        }
                        AgentMessage::Configs { configs } => {
                            agent_clone
                                .lock()
                                .await
                                .set_configs(configs)
                                .unwrap_or_else(|e| {
                                    log::error!("Configs Error {}: {}", agent_id_clone, e);
                                });
                        }
                        AgentMessage::Stop => {
                            rx.close();
                            break;
                        }
                    }
                }
            };

            if uses_native_thread {
                std::thread::spawn(move || {
                    let rt = tokio::runtime::Builder::new_current_thread()
                        .enable_all()
                        .build()
                        .unwrap();
                    rt.block_on(agent_loop);
                });
            } else {
                tokio::spawn(agent_loop);
            }
        }
        Ok(())
    }

    /// Stop an agent by id.
    pub async fn stop_agent(&self, agent_id: &str) -> Result<(), AgentError> {
        {
            // remove the sender first to prevent new messages being sent
            let mut agent_txs = self.agent_txs.lock().unwrap();
            if let Some(tx) = agent_txs.swap_remove(agent_id) {
                if let Err(e) = tx.try_send(AgentMessage::Stop) {
                    log::warn!("Failed to send stop message to agent {}: {}", agent_id, e);
                }
            }
        }

        let agent = {
            let agents = self.agents.lock().unwrap();
            let Some(a) = agents.get(agent_id) else {
                return Err(AgentError::AgentNotFound(agent_id.to_string()));
            };
            a.clone()
        };
        let mut agent_guard = agent.lock().await;
        if *agent_guard.status() == AgentStatus::Start {
            log::info!("Stopping agent {}", agent_id);
            agent_guard.stop().await?;
        }

        Ok(())
    }

    /// Set configs for an agent by id.
    pub async fn set_agent_configs(
        &self,
        agent_id: String,
        configs: AgentConfigs,
    ) -> Result<(), AgentError> {
        let tx = {
            let agent_txs = self.agent_txs.lock().unwrap();
            agent_txs.get(&agent_id).cloned()
        };

        let Some(tx) = tx else {
            // The agent is not running. We can set the configs directly.
            let agent = {
                let agents = self.agents.lock().unwrap();
                let Some(a) = agents.get(&agent_id) else {
                    return Err(AgentError::AgentNotFound(agent_id.to_string()));
                };
                a.clone()
            };
            agent.lock().await.set_configs(configs.clone())?;
            return Ok(());
        };
        let message = AgentMessage::Configs { configs };
        tx.send(message).await.map_err(|_| {
            AgentError::SendMessageFailed("Failed to send config message".to_string())
        })?;
        Ok(())
    }

    /// Get global configs for the agent definition by name.
    pub fn get_global_configs(&self, def_name: &str) -> Option<AgentConfigs> {
        let global_configs_map = self.global_configs_map.lock().unwrap();
        global_configs_map.get(def_name).cloned()
    }

    /// Set global configs for the agent definition by name.
    pub fn set_global_configs(&self, def_name: String, configs: AgentConfigs) {
        let mut global_configs_map = self.global_configs_map.lock().unwrap();

        let Some(existing_configs) = global_configs_map.get_mut(&def_name) else {
            global_configs_map.insert(def_name, configs);
            return;
        };

        for (key, value) in configs {
            existing_configs.set(key, value);
        }
    }

    /// Get the global configs map.
    pub fn get_global_configs_map(&self) -> AgentConfigsMap {
        let global_configs_map = self.global_configs_map.lock().unwrap();
        global_configs_map.clone()
    }

    /// Set the global configs map.
    pub fn set_global_configs_map(&self, new_configs_map: AgentConfigsMap) {
        for (agent_name, new_configs) in new_configs_map {
            self.set_global_configs(agent_name, new_configs);
        }
    }

    /// Send input to an agent.
    pub(crate) async fn agent_input(
        &self,
        agent_id: String,
        ctx: AgentContext,
        pin: String,
        value: AgentValue,
    ) -> Result<(), AgentError> {
        let message = if pin.starts_with("config:") {
            let config_key = pin[7..].to_string();
            AgentMessage::Config {
                key: config_key,
                value,
            }
        } else {
            AgentMessage::Input {
                ctx,
                pin: pin.clone(),
                value,
            }
        };

        let tx = {
            let agent_txs = self.agent_txs.lock().unwrap();
            agent_txs.get(&agent_id).cloned()
        };

        let Some(tx) = tx else {
            // The agent is not running. If it's a config message, we can set it directly.
            let agent: Arc<AsyncMutex<Box<dyn Agent>>> = {
                let agents = self.agents.lock().unwrap();
                let Some(a) = agents.get(&agent_id) else {
                    return Err(AgentError::AgentNotFound(agent_id.to_string()));
                };
                a.clone()
            };
            if let AgentMessage::Config { key, value } = message {
                agent.lock().await.set_config(key, value)?;
            }
            return Ok(());
        };
        tx.send(message).await.map_err(|_| {
            AgentError::SendMessageFailed("Failed to send input message".to_string())
        })?;

        self.emit_agent_input(agent_id.to_string(), pin);

        Ok(())
    }

    /// Send output from an agent. (Async version)
    pub async fn send_agent_out(
        &self,
        agent_id: String,
        ctx: AgentContext,
        pin: String,
        value: AgentValue,
    ) -> Result<(), AgentError> {
        message::send_agent_out(self, agent_id, ctx, pin, value).await
    }

    /// Send output from an agent.
    pub fn try_send_agent_out(
        &self,
        agent_id: String,
        ctx: AgentContext,
        pin: String,
        value: AgentValue,
    ) -> Result<(), AgentError> {
        message::try_send_agent_out(self, agent_id, ctx, pin, value)
    }

    /// Write a value to the board.
    pub async fn write_board_value(
        &self,
        name: String,
        value: AgentValue,
    ) -> Result<(), AgentError> {
        self.send_board_out(name, AgentContext::new(), value).await
    }

    /// Write a value to the variable board.
    pub async fn write_var_value(
        &self,
        stream_id: &str,
        name: &str,
        value: AgentValue,
    ) -> Result<(), AgentError> {
        let var_name = format!("%{}/{}", stream_id, name);
        self.send_board_out(var_name, AgentContext::new(), value)
            .await
    }

    pub(crate) async fn send_board_out(
        &self,
        name: String,
        ctx: AgentContext,
        value: AgentValue,
    ) -> Result<(), AgentError> {
        message::send_board_out(self, name, ctx, value).await
    }

    async fn spawn_message_loop(&self) -> Result<(), AgentError> {
        // TODO: settings for the channel size
        let (tx, mut rx) = mpsc::channel(4096);
        {
            let mut tx_lock = self.tx.lock().unwrap();
            *tx_lock = Some(tx);
        }

        // spawn the main loop
        let askit = self.clone();
        tokio::spawn(async move {
            while let Some(message) = rx.recv().await {
                use AgentEventMessage::*;

                match message {
                    AgentOut {
                        agent,
                        ctx,
                        pin,
                        value,
                    } => {
                        message::agent_out(&askit, agent, ctx, pin, value).await;
                    }
                    BoardOut { name, ctx, value } => {
                        message::board_out(&askit, name, ctx, value).await;
                    }
                }
            }
        });

        tokio::task::yield_now().await;

        Ok(())
    }

    /// Subscribe to all ASKit events.
    pub fn subscribe(&self) -> broadcast::Receiver<ASKitEvent> {
        self.observers.subscribe()
    }

    /// Subscribe to a specific type of `ASKitEvent`.
    ///
    /// It takes a closure that filters and maps the events, and returns an `mpsc::UnboundedReceiver`
    /// that will receive only the successfully mapped events.
    pub fn subscribe_to_event<F, T>(&self, mut filter_map: F) -> mpsc::UnboundedReceiver<T>
    where
        F: FnMut(ASKitEvent) -> Option<T> + Send + 'static,
        T: Send + 'static,
    {
        let (tx, rx) = mpsc::unbounded_channel();
        let mut event_rx = self.subscribe();

        tokio::spawn(async move {
            loop {
                match event_rx.recv().await {
                    Ok(event) => {
                        if let Some(mapped_event) = filter_map(event) {
                            if tx.send(mapped_event).is_err() {
                                // Receiver dropped, task can exit
                                break;
                            }
                        }
                    }
                    Err(RecvError::Lagged(n)) => {
                        log::warn!("Event subscriber lagged by {} events", n);
                    }
                    Err(RecvError::Closed) => {
                        // Sender dropped, task can exit
                        break;
                    }
                }
            }
        });
        rx
    }

    pub(crate) fn emit_agent_config_updated(
        &self,
        agent_id: String,
        key: String,
        value: AgentValue,
    ) {
        self.notify_observers(ASKitEvent::AgentConfigUpdated(agent_id, key, value));
    }

    pub(crate) fn emit_agent_error(&self, agent_id: String, message: String) {
        self.notify_observers(ASKitEvent::AgentError(agent_id, message));
    }

    pub(crate) fn emit_agent_input(&self, agent_id: String, pin: String) {
        self.notify_observers(ASKitEvent::AgentIn(agent_id, pin));
    }

    pub(crate) fn emit_agent_spec_updated(&self, agent_id: String) {
        self.notify_observers(ASKitEvent::AgentSpecUpdated(agent_id));
    }

    pub(crate) fn emit_board(&self, name: String, value: AgentValue) {
        // // ignore variables
        // if name.starts_with('%') {
        //     return;
        // }
        self.notify_observers(ASKitEvent::Board(name, value));
    }

    fn notify_observers(&self, event: ASKitEvent) {
        let _ = self.observers.send(event);
    }
}

fn is_valid_stream_name(new_name: &str) -> bool {
    // Check if the name is empty
    if new_name.trim().is_empty() {
        return false;
    }

    // Checks for path-like names:
    if new_name.contains('/') {
        // Disallow leading, trailing, or consecutive slashes
        if new_name.starts_with('/') || new_name.ends_with('/') || new_name.contains("//") {
            return false;
        }
        // Disallow segments that are "." or ".."
        if new_name
            .split('/')
            .any(|segment| segment == "." || segment == "..")
        {
            return false;
        }
    }

    // Check if the name contains invalid characters
    let invalid_chars = ['\\', ':', '*', '?', '"', '<', '>', '|'];
    for c in invalid_chars {
        if new_name.contains(c) {
            return false;
        }
    }

    true
}

#[derive(Clone, Debug)]
pub enum ASKitEvent {
    AgentConfigUpdated(String, String, AgentValue), // (agent_id, key, value)
    AgentError(String, String),                     // (agent_id, message)
    AgentIn(String, String),                        // (agent_id, pin)
    AgentSpecUpdated(String),                       // (agent_id)
    Board(String, AgentValue),                      // (board name, value)
}