/ crates / bot / src / context.rs
context.rs
 1  /// Execution context for bot operations
 2  use serde::{Deserialize, Serialize};
 3  use std::collections::HashMap;
 4  
 5  /// Context provided to bots during execution
 6  #[derive(Debug, Clone, Serialize, Deserialize)]
 7  pub struct ExecutionContext {
 8      /// Unique bot identifier
 9      pub bot_id: String,
10  
11      /// Role of the bot
12      pub role: String,
13  
14      /// Network endpoints
15      pub network: NetworkEndpoints,
16  
17      /// Configuration parameters
18      pub config: HashMap<String, serde_json::Value>,
19  
20      /// Execution metadata
21      pub metadata: ContextMetadata,
22  }
23  
24  #[derive(Debug, Clone, Serialize, Deserialize)]
25  pub struct NetworkEndpoints {
26      pub alphaos_rest: String,
27      pub deltaos_rest: String,
28      pub adnet_unified: String,
29  }
30  
31  #[derive(Debug, Clone, Serialize, Deserialize)]
32  pub struct ContextMetadata {
33      /// Scenario ID (if part of a scenario)
34      pub scenario_id: Option<String>,
35  
36      /// Phase within scenario
37      pub phase: Option<String>,
38  
39      /// Start timestamp
40      pub start_time_ms: i64,
41  
42      /// Tags for categorization
43      pub tags: Vec<String>,
44  }
45  
46  impl ExecutionContext {
47      pub fn new(bot_id: String, role: String, network: NetworkEndpoints) -> Self {
48          Self {
49              bot_id,
50              role,
51              network,
52              config: HashMap::new(),
53              metadata: ContextMetadata {
54                  scenario_id: None,
55                  phase: None,
56                  start_time_ms: std::time::SystemTime::now()
57                      .duration_since(std::time::UNIX_EPOCH)
58                      .unwrap_or_default()
59                      .as_millis() as i64,
60                  tags: Vec::new(),
61              },
62          }
63      }
64  
65      pub fn with_config(mut self, config: HashMap<String, serde_json::Value>) -> Self {
66          self.config = config;
67          self
68      }
69  
70      pub fn with_scenario(mut self, scenario_id: String, phase: Option<String>) -> Self {
71          self.metadata.scenario_id = Some(scenario_id);
72          self.metadata.phase = phase;
73          self
74      }
75  
76      pub fn with_tags(mut self, tags: Vec<String>) -> Self {
77          self.metadata.tags = tags;
78          self
79      }
80  
81      pub fn get_config<T: serde::de::DeserializeOwned>(&self, key: &str) -> Option<T> {
82          self.config.get(key).and_then(|v| serde_json::from_value(v.clone()).ok())
83      }
84  }