oxios_kernel/kernel_handle/
infra_api.rs1use crate::config::OxiosConfig;
4use crate::cron::{CronJob, CronJobUpdate, CronScheduler};
5use crate::event_bus::{EventBus, KernelEvent};
6use crate::git_layer::{GitLayer, LogEntry};
7use crate::resource_monitor::{ResourceMonitor, ResourceSnapshot};
8use crate::scheduler::{AgentScheduler, ScheduledTask, SchedulerStats};
9use crate::tools::PendingToolApprovals;
10use std::sync::Arc;
11use std::time::{Duration, Instant};
12
13pub struct InfraApi {
15 pub(crate) git_layer: Arc<GitLayer>,
16 pub(crate) scheduler: Arc<AgentScheduler>,
17 pub(crate) cron_scheduler: Arc<CronScheduler>,
18 pub(crate) resource_monitor: Arc<ResourceMonitor>,
19 pub(crate) event_bus: EventBus,
20 pub(crate) config: OxiosConfig,
21 pub(crate) start_time: Instant,
22 pub(crate) orchestrator_config: parking_lot::RwLock<crate::config::OrchestratorConfig>,
24 pub(crate) pending_tool_approvals: PendingToolApprovals,
26}
27
28impl InfraApi {
29 pub fn new(
31 git_layer: Arc<GitLayer>,
32 scheduler: Arc<AgentScheduler>,
33 cron_scheduler: Arc<CronScheduler>,
34 resource_monitor: Arc<ResourceMonitor>,
35 event_bus: EventBus,
36 config: OxiosConfig,
37 start_time: Instant,
38 ) -> Self {
39 Self {
40 git_layer,
41 scheduler,
42 cron_scheduler,
43 resource_monitor,
44 event_bus,
45 config,
46 start_time,
47 orchestrator_config: parking_lot::RwLock::new(
48 crate::config::OrchestratorConfig::default(),
49 ),
50 pending_tool_approvals: PendingToolApprovals::new(),
51 }
52 }
53 pub fn git(&self) -> &GitLayer {
55 &self.git_layer
56 }
57
58 pub fn git_log(&self, max: usize) -> anyhow::Result<Vec<LogEntry>> {
60 self.git_layer.log(max)
61 }
62
63 pub fn git_tag(&self, name: &str, message: &str) -> anyhow::Result<()> {
65 self.git_layer.tag(name, message)
66 }
67
68 pub fn git_restore(&self, path: &str, hash: &str) -> anyhow::Result<()> {
70 self.git_layer.restore_file(path, hash)
71 }
72
73 pub fn git_verify(&self) -> anyhow::Result<bool> {
75 self.git_layer.verify()
76 }
77
78 pub fn git_tags(&self) -> anyhow::Result<Vec<String>> {
80 self.git_layer.list_tags()
81 }
82
83 pub fn scheduler_stats(&self) -> SchedulerStats {
85 self.scheduler.stats()
86 }
87
88 pub fn queued_tasks(&self) -> Vec<ScheduledTask> {
90 self.scheduler.queued_tasks()
91 }
92
93 pub fn running_tasks(&self) -> Vec<ScheduledTask> {
95 self.scheduler.running_tasks()
96 }
97
98 pub async fn add_cron(&self, job: CronJob) -> anyhow::Result<uuid::Uuid> {
100 self.cron_scheduler.add_job(job).await
101 }
102
103 pub fn get_cron(&self, id: uuid::Uuid) -> Option<CronJob> {
105 self.cron_scheduler.get_job(id)
106 }
107
108 pub async fn update_cron(&self, id: uuid::Uuid, update: CronJobUpdate) -> anyhow::Result<()> {
110 self.cron_scheduler.update_job(id, update).await
111 }
112
113 pub async fn remove_cron(&self, id: uuid::Uuid) -> anyhow::Result<()> {
115 self.cron_scheduler.remove_job(id).await
116 }
117
118 pub fn trigger_cron(&self, id: uuid::Uuid) -> anyhow::Result<CronJob> {
120 self.cron_scheduler.trigger_job(id)
121 }
122
123 pub async fn complete_cron(&self, id: uuid::Uuid, success: bool, summary: String) {
125 self.cron_scheduler
126 .mark_job_completed(id, success, summary)
127 .await
128 }
129
130 pub fn list_crons(&self) -> Vec<CronJob> {
132 self.cron_scheduler.list_jobs()
133 }
134
135 pub fn resource_snapshot(&self) -> ResourceSnapshot {
137 self.resource_monitor.snapshot()
138 }
139
140 pub fn resource_history(&self, last_n: usize) -> Vec<ResourceSnapshot> {
142 self.resource_monitor.history(last_n)
143 }
144
145 pub fn is_overloaded(&self) -> bool {
147 self.resource_monitor.is_overloaded()
148 }
149
150 pub fn subscribe(&self) -> tokio::sync::broadcast::Receiver<KernelEvent> {
152 self.event_bus.subscribe()
153 }
154
155 pub fn publish(&self, event: KernelEvent) -> anyhow::Result<()> {
157 self.event_bus
158 .publish(event)
159 .map_err(|e| anyhow::anyhow!("broadcast error: {e}"))
160 }
161
162 pub fn config(&self) -> &OxiosConfig {
164 &self.config
165 }
166
167 pub fn scheduler(&self) -> &Arc<AgentScheduler> {
169 &self.scheduler
170 }
171
172 pub fn resource_monitor(&self) -> &Arc<ResourceMonitor> {
174 &self.resource_monitor
175 }
176
177 pub fn update_orchestrator_config(&self, config: crate::config::OrchestratorConfig) {
179 *self.orchestrator_config.write() = config;
180 }
181
182 pub fn uptime(&self) -> Duration {
184 self.start_time.elapsed()
185 }
186
187 pub fn pending_tool_approvals(&self) -> &PendingToolApprovals {
189 &self.pending_tool_approvals
190 }
191}