matrixcode-core 0.4.43

MatrixCode Agent Core - Pure logic, no UI
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
//! Registry Service Implementation
//!
//! Provides service registration, discovery, and management functionality.

use std::collections::HashMap;
use std::sync::Arc;

use tokio::sync::RwLock;

use crate::matrixrpc::service::{ExtensionService, RegistrationInfo, ServiceId, ServiceStatus};

/// Error type for registry operations
#[derive(Debug, thiserror::Error)]
pub enum RegistryError {
    /// Service already exists
    #[error("Service '{0}' already exists in registry")]
    AlreadyExists(String),

    /// Service not found
    #[error("Service '{0}' not found in registry")]
    NotFound(String),

    /// Service is not running
    #[error("Service '{0}' is not running (status: {1:?})")]
    NotRunning(String, ServiceStatus),

    /// Invalid service state
    #[error("Invalid service state: {0}")]
    InvalidState(String),

    /// Internal error
    #[error("Internal error: {0}")]
    Internal(String),
}

/// Service query filter
#[derive(Debug, Clone, Default)]
pub struct ServiceFilter {
    /// Filter by status
    pub status: Option<ServiceStatus>,

    /// Filter by capability
    pub capability: Option<String>,

    /// Filter by transport type
    pub transport_type: Option<String>,
}

impl ServiceFilter {
    /// Create a new filter
    pub fn new() -> Self {
        Self::default()
    }

    /// Filter by status
    pub fn status(mut self, status: ServiceStatus) -> Self {
        self.status = Some(status);
        self
    }

    /// Filter by capability
    pub fn capability(mut self, cap: impl Into<String>) -> Self {
        self.capability = Some(cap.into());
        self
    }

    /// Filter by transport type
    pub fn transport_type(mut self, transport: impl Into<String>) -> Self {
        self.transport_type = Some(transport.into());
        self
    }

    /// Check if a service matches this filter
    pub fn matches(&self, service: &ExtensionService) -> bool {
        if let Some(status) = &self.status {
            if service.status != *status {
                return false;
            }
        }

        if let Some(cap) = &self.capability {
            if !service.has_capability(cap) {
                return false;
            }
        }

        true
    }
}

/// Registry statistics
#[derive(Debug, Clone, Default)]
pub struct RegistryStats {
    /// Total number of services
    pub total: usize,

    /// Number of running services
    pub running: usize,

    /// Number of stopped services
    pub stopped: usize,

    /// Number of error services
    pub errors: usize,

    /// Number of reconnecting services
    pub reconnecting: usize,
}

/// Service Registry
///
/// Manages registration, discovery, and lifecycle of extension services.
#[derive(Debug)]
pub struct RegistryService {
    /// Registered services
    services: Arc<RwLock<HashMap<ServiceId, RegistrationInfo>>>,

    /// Service name to ID mapping
    name_index: Arc<RwLock<HashMap<String, ServiceId>>>,

    /// Capability to service IDs mapping
    capability_index: Arc<RwLock<HashMap<String, Vec<ServiceId>>>>,

    /// Heartbeat timeout in seconds
    heartbeat_timeout_secs: u64,
}

impl Default for RegistryService {
    fn default() -> Self {
        Self::new()
    }
}

impl RegistryService {
    /// Create a new registry service
    pub fn new() -> Self {
        Self {
            services: Arc::new(RwLock::new(HashMap::new())),
            name_index: Arc::new(RwLock::new(HashMap::new())),
            capability_index: Arc::new(RwLock::new(HashMap::new())),
            heartbeat_timeout_secs: 60,
        }
    }

    /// Set heartbeat timeout
    pub fn with_heartbeat_timeout(mut self, secs: u64) -> Self {
        self.heartbeat_timeout_secs = secs;
        self
    }

    /// Register a new service
    pub async fn register(&self, service: ExtensionService) -> Result<ServiceId, RegistryError> {
        let name = service.name.clone();
        let id = service.id.clone();
        let capabilities: Vec<_> = service.capabilities.iter().map(|c| c.name.clone()).collect();

        // Check if service name already exists
        {
            let name_index = self.name_index.read().await;
            if name_index.contains_key(&name) {
                return Err(RegistryError::AlreadyExists(name));
            }
        }

        // Create registration info
        let registration = RegistrationInfo::new(service);

        // Add to services
        {
            let mut services = self.services.write().await;
            services.insert(id.clone(), registration);
        }

        // Update name index
        {
            let mut name_index = self.name_index.write().await;
            name_index.insert(name, id.clone());
        }

        // Update capability index
        {
            let mut cap_index = self.capability_index.write().await;
            for cap in capabilities {
                cap_index
                    .entry(cap)
                    .or_insert_with(Vec::new)
                    .push(id.clone());
            }
        }

        Ok(id)
    }

    /// Unregister a service by ID
    pub async fn unregister(&self, id: &ServiceId) -> Result<RegistrationInfo, RegistryError> {
        // Remove from services
        let registration = {
            let mut services = self.services.write().await;
            services
                .remove(id)
                .ok_or_else(|| RegistryError::NotFound(id.to_string()))?
        };

        // Remove from name index
        {
            let mut name_index = self.name_index.write().await;
            name_index.remove(&registration.service.name);
        }

        // Remove from capability index
        {
            let mut cap_index = self.capability_index.write().await;
            for cap in &registration.service.capabilities {
                if let Some(ids) = cap_index.get_mut(&cap.name) {
                    ids.retain(|sid| sid != id);
                }
            }
        }

        Ok(registration)
    }

    /// Get a service by ID
    pub async fn get(&self, id: &ServiceId) -> Option<ExtensionService> {
        let services = self.services.read().await;
        services.get(id).map(|r| r.service.clone())
    }

    /// Get a service by name
    pub async fn get_by_name(&self, name: &str) -> Option<ExtensionService> {
        let name_index = self.name_index.read().await;
        let id = name_index.get(name)?;

        let services = self.services.read().await;
        services.get(id).map(|r| r.service.clone())
    }

    /// Get service registration info by ID
    pub async fn get_registration(&self, id: &ServiceId) -> Option<RegistrationInfo> {
        let services = self.services.read().await;
        services.get(id).cloned()
    }

    /// Update service status
    pub async fn update_status(
        &self,
        id: &ServiceId,
        status: ServiceStatus,
    ) -> Result<(), RegistryError> {
        let mut services = self.services.write().await;
        let registration = services
            .get_mut(id)
            .ok_or_else(|| RegistryError::NotFound(id.to_string()))?;

        registration.service.set_status(status);
        registration.touch();
        Ok(())
    }

    /// Record a heartbeat for a service
    pub async fn heartbeat(&self, id: &ServiceId) -> Result<(), RegistryError> {
        let mut services = self.services.write().await;
        let registration = services
            .get_mut(id)
            .ok_or_else(|| RegistryError::NotFound(id.to_string()))?;

        registration.service.heartbeat();
        registration.touch();
        Ok(())
    }

    /// List all services
    pub async fn list_all(&self) -> Vec<ExtensionService> {
        let services = self.services.read().await;
        services.values().map(|r| r.service.clone()).collect()
    }

    /// List services matching a filter
    pub async fn list(&self, filter: &ServiceFilter) -> Vec<ExtensionService> {
        let services = self.services.read().await;
        services
            .values()
            .filter(|r| filter.matches(&r.service))
            .map(|r| r.service.clone())
            .collect()
    }

    /// Find services by capability
    pub async fn find_by_capability(&self, capability: &str) -> Vec<ExtensionService> {
        let cap_index = self.capability_index.read().await;
        let ids = cap_index.get(capability).cloned().unwrap_or_default();
        drop(cap_index);

        let services = self.services.read().await;
        ids.iter()
            .filter_map(|id| services.get(id).map(|r| r.service.clone()))
            .collect()
    }

    /// Get registry statistics
    pub async fn stats(&self) -> RegistryStats {
        let services = self.services.read().await;

        let mut stats = RegistryStats {
            total: services.len(),
            ..Default::default()
        };

        for registration in services.values() {
            match registration.service.status {
                ServiceStatus::Running => stats.running += 1,
                ServiceStatus::Stopped => stats.stopped += 1,
                ServiceStatus::Error => stats.errors += 1,
                ServiceStatus::Reconnecting => stats.reconnecting += 1,
                _ => {}
            }
        }

        stats
    }

    /// Check health of all services and update status
    pub async fn health_check(&self) -> Vec<ServiceId> {
        let timeout = self.heartbeat_timeout_secs;
        let mut unhealthy = Vec::new();

        let mut services = self.services.write().await;
        for (id, registration) in services.iter_mut() {
            if !registration.service.is_healthy(timeout) {
                if registration.service.status == ServiceStatus::Running {
                    registration.service.set_status(ServiceStatus::Reconnecting);
                    registration.touch();
                    unhealthy.push(id.clone());
                }
            }
        }

        unhealthy
    }

    /// Clear all registrations
    pub async fn clear(&self) {
        let mut services = self.services.write().await;
        let mut name_index = self.name_index.write().await;
        let mut cap_index = self.capability_index.write().await;

        services.clear();
        name_index.clear();
        cap_index.clear();
    }

    /// Get the number of registered services
    pub async fn count(&self) -> usize {
        let services = self.services.read().await;
        services.len()
    }
}

/// Builder for creating registry with custom configuration
#[derive(Debug)]
pub struct RegistryBuilder {
    heartbeat_timeout_secs: u64,
}

impl Default for RegistryBuilder {
    fn default() -> Self {
        Self {
            heartbeat_timeout_secs: 60,
        }
    }
}

impl RegistryBuilder {
    /// Create a new registry builder
    pub fn new() -> Self {
        Self::default()
    }

    /// Set heartbeat timeout
    pub fn heartbeat_timeout(mut self, secs: u64) -> Self {
        self.heartbeat_timeout_secs = secs;
        self
    }

    /// Build the registry service
    pub fn build(self) -> RegistryService {
        RegistryService::new().with_heartbeat_timeout(self.heartbeat_timeout_secs)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::matrixrpc::Capability;

    #[tokio::test]
    async fn test_register_service() {
        let registry = RegistryService::new();
        let service = ExtensionService::new("test-service", "1.0.0");

        let id = registry.register(service).await.unwrap();
        assert!(registry.get(&id).await.is_some());
        assert_eq!(registry.count().await, 1);
    }

    #[tokio::test]
    async fn test_register_duplicate_name() {
        let registry = RegistryService::new();
        let service1 = ExtensionService::new("test", "1.0.0");
        let service2 = ExtensionService::new("test", "2.0.0");

        registry.register(service1).await.unwrap();
        let result = registry.register(service2).await;
        assert!(matches!(result, Err(RegistryError::AlreadyExists(_))));
    }

    #[tokio::test]
    async fn test_unregister_service() {
        let registry = RegistryService::new();
        let service = ExtensionService::new("test", "1.0.0");

        let id = registry.register(service).await.unwrap();
        registry.unregister(&id).await.unwrap();
        assert!(registry.get(&id).await.is_none());
    }

    #[tokio::test]
    async fn test_get_by_name() {
        let registry = RegistryService::new();
        let service = ExtensionService::new("test", "1.0.0");

        registry.register(service).await.unwrap();

        let found = registry.get_by_name("test").await;
        assert!(found.is_some());
        assert_eq!(found.unwrap().version, "1.0.0");
    }

    #[tokio::test]
    async fn test_update_status() {
        let registry = RegistryService::new();
        let service = ExtensionService::new("test", "1.0.0");

        let id = registry.register(service).await.unwrap();
        registry
            .update_status(&id, ServiceStatus::Running)
            .await
            .unwrap();

        let service = registry.get(&id).await.unwrap();
        assert_eq!(service.status, ServiceStatus::Running);
    }

    #[tokio::test]
    async fn test_find_by_capability() {
        let registry = RegistryService::new();

        let service1 = ExtensionService::new("service1", "1.0.0")
            .capability(Capability::new("tools"));

        let service2 = ExtensionService::new("service2", "1.0.0")
            .capability(Capability::new("resources"));

        let service3 = ExtensionService::new("service3", "1.0.0")
            .capability(Capability::new("tools"));

        registry.register(service1).await.unwrap();
        registry.register(service2).await.unwrap();
        registry.register(service3).await.unwrap();

        let tools_services = registry.find_by_capability("tools").await;
        assert_eq!(tools_services.len(), 2);

        let resources_services = registry.find_by_capability("resources").await;
        assert_eq!(resources_services.len(), 1);

        let prompts_services = registry.find_by_capability("prompts").await;
        assert!(prompts_services.is_empty());
    }

    #[tokio::test]
    async fn test_registry_stats() {
        let registry = RegistryService::new();

        let mut service1 = ExtensionService::new("s1", "1.0.0");
        service1.set_status(ServiceStatus::Running);

        let mut service2 = ExtensionService::new("s2", "1.0.0");
        service2.set_status(ServiceStatus::Stopped);

        let mut service3 = ExtensionService::new("s3", "1.0.0");
        service3.set_status(ServiceStatus::Error);

        registry.register(service1).await.unwrap();
        registry.register(service2).await.unwrap();
        registry.register(service3).await.unwrap();

        let stats = registry.stats().await;
        assert_eq!(stats.total, 3);
        assert_eq!(stats.running, 1);
        assert_eq!(stats.stopped, 1);
        assert_eq!(stats.errors, 1);
    }

    #[tokio::test]
    async fn test_service_filter() {
        let registry = RegistryService::new();

        let mut service1 = ExtensionService::new("s1", "1.0.0")
            .capability(Capability::new("tools"));
        service1.set_status(ServiceStatus::Running);

        let mut service2 = ExtensionService::new("s2", "1.0.0")
            .capability(Capability::new("tools"));
        service2.set_status(ServiceStatus::Stopped);

        registry.register(service1).await.unwrap();
        registry.register(service2).await.unwrap();

        let filter = ServiceFilter::new().status(ServiceStatus::Running);
        let services = registry.list(&filter).await;
        assert_eq!(services.len(), 1);
        assert_eq!(services[0].name, "s1");
    }
}