matrixcode-core 0.4.40

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
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
//! Node Executor Implementation
//!
//! Executes workflow node calls via JSON-RPC transport with timeout and retry support.
//! Handles callback endpoints and execution result processing.

#![allow(dead_code)]

use std::sync::Arc;
use std::time::Duration;

use tokio::sync::RwLock;

use crate::matrixrpc::{
    ErrorCode, JsonRpcError, JsonRpcId, JsonRpcResponse,
    ServiceId, ServiceStatus, RegistryService,
};
use crate::matrixrpc::transport::{StdioTransport, TransportConfig as TransportSettings};
use crate::matrixrpc::router::{NodeRouter, NodeRouteResult, NodeRouterError, NodeContext};

/// Error type for node execution operations
#[derive(Debug, thiserror::Error)]
pub enum NodeExecutorError {
    /// Transport error during execution
    #[error("Transport error: {0}")]
    TransportError(String),

    /// Execution timeout
    #[error("Node '{node_id}' execution timed out after {timeout_ms}ms")]
    Timeout { node_id: String, timeout_ms: u64 },

    /// Retry exhausted
    #[error("Node '{node_id}' execution failed after {attempts} attempts")]
    RetryExhausted { node_id: String, attempts: u32, last_error: String },

    /// Service not connected
    #[error("Service '{0}' is not connected")]
    ServiceNotConnected(ServiceId),

    /// Invalid response
    #[error("Invalid response from service: {0}")]
    InvalidResponse(String),

    /// Node execution failed
    #[error("Node '{node_id}' execution failed: {message}")]
    ExecutionFailed { node_id: String, message: String, data: Option<serde_json::Value> },

    /// Callback failed
    #[error("Callback '{callback_type}' failed for node '{node_id}': {message}")]
    CallbackFailed { node_id: String, callback_type: String, message: String },

    /// Routing error
    #[error("Routing error: {0}")]
    RoutingError(#[from] NodeRouterError),

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

/// Result of a node execution
#[derive(Debug, Clone)]
pub struct NodeExecutionResult {
    /// The node ID that was executed
    pub node_id: String,
    /// The execution result data
    pub result: serde_json::Value,
    /// Execution status
    pub status: NodeExecutionStatus,
    /// Execution metadata
    pub metadata: serde_json::Value,
    /// Execution duration in milliseconds
    pub duration_ms: u64,
    /// Callbacks made during execution (if any)
    pub callbacks: Vec<CallbackRecord>,
}

/// Status of node execution
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NodeExecutionStatus {
    /// Node executed successfully
    Success,
    /// Node execution failed
    Failed,
    /// Node was skipped (condition not met)
    Skipped,
    /// Node is pending (async execution)
    Pending,
    /// Node requires retry
    Retry,
}

impl NodeExecutionStatus {
    /// Check if the status indicates success
    pub fn is_success(&self) -> bool {
        matches!(self, NodeExecutionStatus::Success)
    }

    /// Check if the status indicates failure
    pub fn is_failure(&self) -> bool {
        matches!(self, NodeExecutionStatus::Failed)
    }

    /// Check if the node was skipped
    pub fn is_skipped(&self) -> bool {
        matches!(self, NodeExecutionStatus::Skipped)
    }
}

/// Record of a callback made during node execution
#[derive(Debug, Clone)]
pub struct CallbackRecord {
    /// Callback type (ai, tool, context)
    pub callback_type: String,
    /// Callback request ID
    pub request_id: String,
    /// Callback parameters
    pub params: serde_json::Value,
    /// Callback result
    pub result: serde_json::Value,
    /// Callback duration in milliseconds
    pub duration_ms: u64,
}

/// Node execution configuration
#[derive(Debug, Clone)]
pub struct NodeExecutionConfig {
    /// Timeout for node execution (milliseconds)
    pub timeout_ms: u64,
    /// Maximum retry attempts
    pub max_retries: u32,
    /// Retry interval base (milliseconds)
    pub retry_interval_ms: u64,
    /// Enable callback handling
    pub enable_callbacks: bool,
    /// Callback timeout (milliseconds)
    pub callback_timeout_ms: u64,
    /// Transport settings
    pub transport: TransportSettings,
}

impl Default for NodeExecutionConfig {
    fn default() -> Self {
        Self {
            timeout_ms: 60_000,
            max_retries: 3,
            retry_interval_ms: 2000,
            enable_callbacks: true,
            callback_timeout_ms: 30_000,
            transport: TransportSettings::default(),
        }
    }
}

impl NodeExecutionConfig {
    /// Create a new configuration
    pub fn new(timeout_ms: u64) -> Self {
        Self {
            timeout_ms,
            ..Default::default()
        }
    }

    /// Set maximum retries
    pub fn max_retries(mut self, retries: u32) -> Self {
        self.max_retries = retries;
        self
    }

    /// Disable callbacks
    pub fn no_callbacks(mut self) -> Self {
        self.enable_callbacks = false;
        self
    }

    /// Set callback timeout
    pub fn callback_timeout(mut self, timeout_ms: u64) -> Self {
        self.callback_timeout_ms = timeout_ms;
        self
    }
}

/// Transport connection wrapper for node execution
#[derive(Debug)]
struct NodeTransportConnection {
    service_id: ServiceId,
    connected: RwLock<bool>,
}

impl NodeTransportConnection {
    fn new(service_id: ServiceId) -> Self {
        Self {
            service_id,
            connected: RwLock::new(false),
        }
    }

    async fn is_connected(&self) -> bool {
        *self.connected.read().await
    }
}

use std::collections::HashMap;

/// Node Executor
///
/// Executes workflow node calls through the transport layer with support for
/// timeout, retry, and callback handling.
#[derive(Debug)]
pub struct NodeExecutor {
    /// Node router for finding services
    router: Arc<NodeRouter>,
    /// Registry service for status checks
    registry: Arc<RegistryService>,
    /// Execution configuration
    config: NodeExecutionConfig,
    /// Active transport connections
    connections: RwLock<HashMap<ServiceId, Arc<NodeTransportConnection>>>,
    /// Pending callbacks waiting for response
    pending_callbacks: RwLock<HashMap<String, CallbackContext>>,
}

/// Context for a pending callback
#[derive(Debug, Clone)]
struct CallbackContext {
    /// The node execution that made the callback
    node_id: String,
    /// Original request ID
    original_request_id: JsonRpcId,
    /// Callback request ID
    callback_request_id: String,
    /// Timestamp when callback was initiated
    initiated_at: std::time::Instant,
}

impl NodeExecutor {
    /// Create a new node executor
    pub fn new(router: Arc<NodeRouter>, registry: Arc<RegistryService>) -> Self {
        Self {
            router,
            registry,
            config: NodeExecutionConfig::default(),
            connections: RwLock::new(HashMap::new()),
            pending_callbacks: RwLock::new(HashMap::new()),
        }
    }

    /// Create with custom configuration
    pub fn with_config(
        router: Arc<NodeRouter>,
        registry: Arc<RegistryService>,
        config: NodeExecutionConfig,
    ) -> Self {
        Self {
            router,
            registry,
            config,
            connections: RwLock::new(HashMap::new()),
            pending_callbacks: RwLock::new(HashMap::new()),
        }
    }

    /// Execute a node
    ///
    /// Routes the execution to the appropriate service and handles callbacks.
    pub async fn execute(
        &self,
        node_id: &str,
        context: NodeContext,
    ) -> Result<NodeExecutionResult, NodeExecutorError> {
        let request_id = JsonRpcId::generate();

        // Route the execution
        let route_result = self.router
            .route(node_id, context.clone(), request_id.clone(), vec![])
            .await?;

        // Execute with retry
        self.execute_with_retry(route_result).await
    }

    /// Execute a node with required capabilities
    pub async fn execute_with_capabilities(
        &self,
        node_id: &str,
        context: NodeContext,
        required_capabilities: Vec<String>,
    ) -> Result<NodeExecutionResult, NodeExecutorError> {
        let request_id = JsonRpcId::generate();

        // Convert capabilities
        let caps: Vec<crate::matrixrpc::router::NodeCapability> = required_capabilities
            .iter()
            .filter_map(|c| crate::matrixrpc::router::NodeCapability::from_str(c))
            .collect();

        // Route the execution
        let route_result = self.router
            .route(node_id, context.clone(), request_id.clone(), caps)
            .await?;

        // Execute with retry
        self.execute_with_retry(route_result).await
    }

    /// Execute with retry strategy
    async fn execute_with_retry(
        &self,
        route_result: NodeRouteResult,
    ) -> Result<NodeExecutionResult, NodeExecutorError> {
        let mut attempts = 0;
        let mut last_error: Option<String> = None;
        let start_time = std::time::Instant::now();

        while attempts < self.config.max_retries {
            attempts += 1;

            // Apply retry delay (skip for first attempt)
            if attempts > 1 {
                let delay = self.config.retry_interval_ms * 2u64.pow(attempts - 2);
                tokio::time::sleep(Duration::from_millis(delay)).await;
            }

            // Check service status
            let service = self.registry.get(&route_result.service_id).await;
            match service {
                Some(s) if s.status == ServiceStatus::Running => {}
                Some(s) => {
                    last_error = Some(format!("Service status: {:?}", s.status));
                    continue;
                }
                None => {
                    return Err(NodeExecutorError::ServiceNotConnected(route_result.service_id.clone()));
                }
            }

            // Execute single attempt
            let result = self.execute_single(&route_result).await;

            match result {
                Ok(execution_result) => {
                    // Update duration
                    let duration_ms = start_time.elapsed().as_millis() as u64;
                    return Ok(NodeExecutionResult {
                        node_id: execution_result.node_id,
                        result: execution_result.result,
                        status: execution_result.status,
                        metadata: execution_result.metadata,
                        duration_ms,
                        callbacks: execution_result.callbacks,
                    });
                }
                Err(NodeExecutorError::Timeout { .. }) => {
                    last_error = Some("Timeout".to_string());
                    // Retry on timeout
                }
                Err(NodeExecutorError::TransportError(_)) => {
                    last_error = Some(result.unwrap_err().to_string());
                    // Retry on transport error
                }
                Err(e) => {
                    // Don't retry on other errors
                    return Err(e);
                }
            }
        }

        // All retries exhausted
        Err(NodeExecutorError::RetryExhausted {
            node_id: route_result.node_id.clone(),
            attempts,
            last_error: last_error.unwrap_or_else(|| "Unknown error".to_string()),
        })
    }

    /// Execute a single attempt
    async fn execute_single(
        &self,
        route_result: &NodeRouteResult,
    ) -> Result<NodeExecutionResult, NodeExecutorError> {
        // Create the request
        let _request = self.router.create_node_request(route_result.clone());

        // Get connection
        let _connection = self.get_connection(&route_result.service_id).await?;

        // In real implementation, send request via transport
        // For now, return a placeholder since actual connection requires service config
        Err(NodeExecutorError::ServiceNotConnected(route_result.service_id.clone()))
    }

    /// Get or create a transport connection for a service
    async fn get_connection(
        &self,
        service_id: &ServiceId,
    ) -> Result<Arc<NodeTransportConnection>, NodeExecutorError> {
        let connections = self.connections.read().await;

        if let Some(conn) = connections.get(service_id) {
            if conn.is_connected().await {
                return Ok(conn.clone());
            }
        }

        drop(connections);

        // Need to create new connection - return error for now
        Err(NodeExecutorError::ServiceNotConnected(service_id.clone()))
    }

    /// Process the response from node execution
    fn process_response(
        &self,
        response: JsonRpcResponse,
        node_id: &str,
    ) -> Result<NodeExecutionResult, NodeExecutorError> {
        if response.is_success() {
            let result = response.result.clone().unwrap_or(serde_json::json!({}));

            // Parse execution status from result
            let status_str = result.get("status")
                .and_then(|s| s.as_str())
                .unwrap_or("success");

            let status = match status_str {
                "success" => NodeExecutionStatus::Success,
                "failed" => NodeExecutionStatus::Failed,
                "skipped" => NodeExecutionStatus::Skipped,
                "pending" => NodeExecutionStatus::Pending,
                "retry" => NodeExecutionStatus::Retry,
                _ => NodeExecutionStatus::Success,
            };

            // Parse callbacks if present
            let callbacks: Vec<CallbackRecord> = result.get("callbacks")
                .and_then(|c| c.as_array())
                .map(|arr| {
                    arr.iter()
                        .filter_map(|c| {
                            let callback_type = c.get("type").and_then(|t| t.as_str()).unwrap_or("");
                            let request_id = c.get("request_id").and_then(|r| r.as_str()).unwrap_or("");
                            let params = c.get("params").cloned().unwrap_or(serde_json::json!({}));
                            let result = c.get("result").cloned().unwrap_or(serde_json::json!({}));
                            let duration_ms = c.get("duration_ms").and_then(|d| d.as_u64()).unwrap_or(0);

                            Some(CallbackRecord {
                                callback_type: callback_type.to_string(),
                                request_id: request_id.to_string(),
                                params,
                                result,
                                duration_ms,
                            })
                        })
                        .collect()
                })
                .unwrap_or_default();

            // Extract metadata
            let metadata = result.get("metadata").cloned().unwrap_or(serde_json::json!({}));

            // Extract actual result data
            let result_data = result.get("data").cloned().unwrap_or(result);

            Ok(NodeExecutionResult {
                node_id: node_id.to_string(),
                result: result_data,
                status,
                metadata,
                duration_ms: 0, // Set by caller
                callbacks,
            })
        } else if response.is_error() {
            let error = response.error.clone().unwrap_or_else(|| {
                JsonRpcError::internal_error("Unknown error")
            });

            if error.code == ErrorCode::TIMEOUT_ERROR || error.code == ErrorCode::TRANSPORT_ERROR {
                Err(NodeExecutorError::Timeout {
                    node_id: node_id.to_string(),
                    timeout_ms: self.config.timeout_ms,
                })
            } else if error.code == ErrorCode::CALLBACK_ERROR {
                Err(NodeExecutorError::CallbackFailed {
                    node_id: node_id.to_string(),
                    callback_type: "unknown".to_string(),
                    message: error.message,
                })
            } else {
                Err(NodeExecutorError::ExecutionFailed {
                    node_id: node_id.to_string(),
                    message: error.message,
                    data: error.data,
                })
            }
        } else {
            Err(NodeExecutorError::InvalidResponse(
                "Response has neither result nor error".to_string()
            ))
        }
    }

    /// Register a transport connection for a service
    pub async fn register_connection(
        &self,
        service_id: ServiceId,
        _transport: StdioTransport,
    ) {
        let connection = Arc::new(NodeTransportConnection::new(service_id.clone()));
        *connection.connected.write().await = true;

        let mut connections = self.connections.write().await;
        connections.insert(service_id, connection);
    }

    /// Remove a transport connection
    pub async fn remove_connection(&self, service_id: &ServiceId) {
        let mut connections = self.connections.write().await;
        if let Some(conn) = connections.get(service_id) {
            *conn.connected.write().await = false;
        }
        connections.remove(service_id);
    }

    /// Check if a service is connected
    pub async fn is_connected(&self, service_id: &ServiceId) -> bool {
        let connections = self.connections.read().await;
        match connections.get(service_id) {
            Some(c) => c.is_connected().await,
            None => false,
        }
    }

    /// Handle a callback request from a node
    ///
    /// This is called when a node requests AI or tool execution.
    pub async fn handle_callback(
        &self,
        callback_type: &str,
        request_id: &str,
        params: serde_json::Value,
    ) -> Result<serde_json::Value, NodeExecutorError> {
        if !self.config.enable_callbacks {
            return Err(NodeExecutorError::CallbackFailed {
                node_id: "unknown".to_string(),
                callback_type: callback_type.to_string(),
                message: "Callbacks are disabled".to_string(),
            });
        }

        match callback_type {
            "ai" => self.handle_ai_callback(request_id, params).await,
            "tool" => self.handle_tool_callback(request_id, params).await,
            "context" => self.handle_context_callback(request_id, params).await,
            _ => Err(NodeExecutorError::CallbackFailed {
                node_id: "unknown".to_string(),
                callback_type: callback_type.to_string(),
                message: format!("Unknown callback type: {}", callback_type),
            }),
        }
    }

    /// Handle AI callback
    async fn handle_ai_callback(
        &self,
        _request_id: &str,
        params: serde_json::Value,
    ) -> Result<serde_json::Value, NodeExecutorError> {
        // In real implementation, this would call the AI provider
        // For now, return a placeholder
        let prompt = params.get("prompt").and_then(|p| p.as_str()).unwrap_or("");
        Ok(serde_json::json!({
            "response": format!("AI processing: {}", prompt),
            "model": "placeholder"
        }))
    }

    /// Handle tool callback
    async fn handle_tool_callback(
        &self,
        _request_id: &str,
        params: serde_json::Value,
    ) -> Result<serde_json::Value, NodeExecutorError> {
        // In real implementation, this would execute the tool
        let tool_name = params.get("tool_name").and_then(|t| t.as_str()).unwrap_or("");
        Ok(serde_json::json!({
            "result": format!("Tool {} executed", tool_name)
        }))
    }

    /// Handle context callback
    async fn handle_context_callback(
        &self,
        _request_id: &str,
        params: serde_json::Value,
    ) -> Result<serde_json::Value, NodeExecutorError> {
        // In real implementation, this would access workflow context
        let key = params.get("key").and_then(|k| k.as_str()).unwrap_or("");
        let operation = params.get("operation").and_then(|o| o.as_str()).unwrap_or("get");

        Ok(serde_json::json!({
            "key": key,
            "operation": operation,
            "value": null
        }))
    }
}

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

    #[test]
    fn test_node_execution_config_defaults() {
        let config = NodeExecutionConfig::default();
        assert_eq!(config.timeout_ms, 60_000);
        assert_eq!(config.max_retries, 3);
        assert!(config.enable_callbacks);
    }

    #[test]
    fn test_node_execution_status() {
        assert!(NodeExecutionStatus::Success.is_success());
        assert!(NodeExecutionStatus::Failed.is_failure());
        assert!(NodeExecutionStatus::Skipped.is_skipped());
        assert!(!NodeExecutionStatus::Pending.is_failure());
    }

    #[test]
    fn test_node_execution_config_builder() {
        let config = NodeExecutionConfig::new(5000)
            .max_retries(5)
            .no_callbacks()
            .callback_timeout(10000);

        assert_eq!(config.timeout_ms, 5000);
        assert_eq!(config.max_retries, 5);
        assert!(!config.enable_callbacks);
        assert_eq!(config.callback_timeout_ms, 10000);
    }

    #[test]
    fn test_callback_record() {
        let record = CallbackRecord {
            callback_type: "ai".to_string(),
            request_id: "cb-001".to_string(),
            params: serde_json::json!({"prompt": "test"}),
            result: serde_json::json!({"response": "ok"}),
            duration_ms: 100,
        };

        assert_eq!(record.callback_type, "ai");
        assert_eq!(record.duration_ms, 100);
    }
}