oxios_kernel/kernel_handle/
infra_api.rs1use crate::ToolMeta;
4use crate::config::OxiosConfig;
5use crate::cron::{CronJob, CronJobUpdate, CronScheduler};
6use crate::event_bus::{EventBus, KernelEvent};
7use crate::git_layer::{GitLayer, LogEntry};
8use crate::resource_monitor::{ResourceMonitor, ResourceSnapshot};
9use crate::scheduler::{AgentScheduler, ScheduledTask, SchedulerStats};
10use crate::tools::{PendingToolApprovals, known_tools};
11use std::sync::Arc;
12use std::time::{Duration, Instant};
13
14pub struct InfraApi {
16 pub(crate) git_layer: Arc<GitLayer>,
17 pub(crate) scheduler: Arc<AgentScheduler>,
18 pub(crate) cron_scheduler: Arc<CronScheduler>,
19 pub(crate) resource_monitor: Arc<ResourceMonitor>,
20 pub(crate) event_bus: EventBus,
21 pub(crate) config: OxiosConfig,
22 pub(crate) start_time: Instant,
23 pub(crate) orchestrator_config: parking_lot::RwLock<crate::config::OrchestratorConfig>,
25 pub(crate) pending_tool_approvals: PendingToolApprovals,
27}
28
29impl InfraApi {
30 pub fn new(
32 git_layer: Arc<GitLayer>,
33 scheduler: Arc<AgentScheduler>,
34 cron_scheduler: Arc<CronScheduler>,
35 resource_monitor: Arc<ResourceMonitor>,
36 event_bus: EventBus,
37 config: OxiosConfig,
38 start_time: Instant,
39 ) -> Self {
40 Self {
41 git_layer,
42 scheduler,
43 cron_scheduler,
44 resource_monitor,
45 event_bus,
46 config,
47 start_time,
48 orchestrator_config: parking_lot::RwLock::new(
49 crate::config::OrchestratorConfig::default(),
50 ),
51 pending_tool_approvals: PendingToolApprovals::new(),
52 }
53 }
54 pub fn git(&self) -> &GitLayer {
56 &self.git_layer
57 }
58
59 pub fn git_log(&self, max: usize) -> anyhow::Result<Vec<LogEntry>> {
61 self.git_layer.log(max)
62 }
63
64 pub fn git_tag(&self, name: &str, message: &str) -> anyhow::Result<()> {
66 self.git_layer.tag(name, message)
67 }
68
69 pub fn git_restore(&self, path: &str, hash: &str) -> anyhow::Result<()> {
71 self.git_layer.restore_file(path, hash)
72 }
73
74 pub fn git_verify(&self) -> anyhow::Result<bool> {
76 self.git_layer.verify()
77 }
78
79 pub fn git_tags(&self) -> anyhow::Result<Vec<String>> {
81 self.git_layer.list_tags()
82 }
83
84 pub fn scheduler_stats(&self) -> SchedulerStats {
86 self.scheduler.stats()
87 }
88
89 pub fn queued_tasks(&self) -> Vec<ScheduledTask> {
91 self.scheduler.queued_tasks()
92 }
93
94 pub fn running_tasks(&self) -> Vec<ScheduledTask> {
96 self.scheduler.running_tasks()
97 }
98
99 pub async fn add_cron(&self, job: CronJob) -> anyhow::Result<uuid::Uuid> {
101 self.cron_scheduler.add_job(job).await
102 }
103
104 pub fn get_cron(&self, id: uuid::Uuid) -> Option<CronJob> {
106 self.cron_scheduler.get_job(id)
107 }
108
109 pub async fn update_cron(&self, id: uuid::Uuid, update: CronJobUpdate) -> anyhow::Result<()> {
111 self.cron_scheduler.update_job(id, update).await
112 }
113
114 pub async fn remove_cron(&self, id: uuid::Uuid) -> anyhow::Result<()> {
116 self.cron_scheduler.remove_job(id).await
117 }
118
119 pub fn trigger_cron(&self, id: uuid::Uuid) -> anyhow::Result<CronJob> {
121 self.cron_scheduler.trigger_job(id)
122 }
123
124 pub async fn complete_cron(&self, id: uuid::Uuid, success: bool, summary: String) {
126 self.cron_scheduler
127 .mark_job_completed(id, success, summary)
128 .await
129 }
130
131 pub fn list_crons(&self) -> Vec<CronJob> {
133 self.cron_scheduler.list_jobs()
134 }
135
136 pub fn resource_snapshot(&self) -> ResourceSnapshot {
138 self.resource_monitor.snapshot()
139 }
140
141 pub fn resource_history(&self, last_n: usize) -> Vec<ResourceSnapshot> {
143 self.resource_monitor.history(last_n)
144 }
145
146 pub fn is_overloaded(&self) -> bool {
148 self.resource_monitor.is_overloaded()
149 }
150
151 pub fn subscribe(&self) -> tokio::sync::broadcast::Receiver<KernelEvent> {
153 self.event_bus.subscribe()
154 }
155
156 pub fn publish(&self, event: KernelEvent) -> anyhow::Result<()> {
158 self.event_bus
159 .publish(event)
160 .map_err(|e| anyhow::anyhow!("broadcast error: {e}"))
161 }
162
163 pub fn config(&self) -> &OxiosConfig {
165 &self.config
166 }
167
168 pub fn scheduler(&self) -> &Arc<AgentScheduler> {
170 &self.scheduler
171 }
172
173 pub fn resource_monitor(&self) -> &Arc<ResourceMonitor> {
175 &self.resource_monitor
176 }
177
178 pub fn update_orchestrator_config(&self, config: crate::config::OrchestratorConfig) {
180 *self.orchestrator_config.write() = config;
181 }
182
183 pub fn uptime(&self) -> Duration {
185 self.start_time.elapsed()
186 }
187
188 pub fn pending_tool_approvals(&self) -> &PendingToolApprovals {
190 &self.pending_tool_approvals
191 }
192
193 pub fn list_available_tools(&self) -> &'static [ToolMeta] {
195 known_tools()
196 }
197}