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::{PendingAskUser, 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 pub(crate) pending_ask_user: Arc<PendingAskUser>,
29}
30
31impl InfraApi {
32 pub fn new(
34 git_layer: Arc<GitLayer>,
35 scheduler: Arc<AgentScheduler>,
36 cron_scheduler: Arc<CronScheduler>,
37 resource_monitor: Arc<ResourceMonitor>,
38 event_bus: EventBus,
39 config: OxiosConfig,
40 start_time: Instant,
41 ) -> Self {
42 Self {
43 git_layer,
44 scheduler,
45 cron_scheduler,
46 resource_monitor,
47 event_bus,
48 config,
49 start_time,
50 orchestrator_config: parking_lot::RwLock::new(
51 crate::config::OrchestratorConfig::default(),
52 ),
53 pending_tool_approvals: PendingToolApprovals::new(),
54 pending_ask_user: Arc::new(PendingAskUser::new()),
55 }
56 }
57 pub fn git(&self) -> &GitLayer {
59 &self.git_layer
60 }
61
62 pub fn git_log(&self, max: usize) -> anyhow::Result<Vec<LogEntry>> {
64 self.git_layer.log(max)
65 }
66
67 pub fn git_tag(&self, name: &str, message: &str) -> anyhow::Result<()> {
69 self.git_layer.tag(name, message)
70 }
71
72 pub fn git_restore(&self, path: &str, hash: &str) -> anyhow::Result<()> {
74 self.git_layer.restore_file(path, hash)
75 }
76
77 pub fn git_verify(&self) -> anyhow::Result<bool> {
79 self.git_layer.verify()
80 }
81
82 pub fn git_tags(&self) -> anyhow::Result<Vec<String>> {
84 self.git_layer.list_tags()
85 }
86
87 pub fn scheduler_stats(&self) -> SchedulerStats {
89 self.scheduler.stats()
90 }
91
92 pub fn queued_tasks(&self) -> Vec<ScheduledTask> {
94 self.scheduler.queued_tasks()
95 }
96
97 pub fn running_tasks(&self) -> Vec<ScheduledTask> {
99 self.scheduler.running_tasks()
100 }
101
102 pub async fn add_cron(&self, job: CronJob) -> anyhow::Result<uuid::Uuid> {
104 self.cron_scheduler.add_job(job).await
105 }
106
107 pub fn get_cron(&self, id: uuid::Uuid) -> Option<CronJob> {
109 self.cron_scheduler.get_job(id)
110 }
111
112 pub async fn update_cron(&self, id: uuid::Uuid, update: CronJobUpdate) -> anyhow::Result<()> {
114 self.cron_scheduler.update_job(id, update).await
115 }
116
117 pub async fn remove_cron(&self, id: uuid::Uuid) -> anyhow::Result<()> {
119 self.cron_scheduler.remove_job(id).await
120 }
121
122 pub fn trigger_cron(&self, id: uuid::Uuid) -> anyhow::Result<CronJob> {
124 self.cron_scheduler.trigger_job(id)
125 }
126
127 pub async fn complete_cron(&self, id: uuid::Uuid, success: bool, summary: String) {
129 self.cron_scheduler
130 .mark_job_completed(id, success, summary)
131 .await
132 }
133
134 pub fn list_crons(&self) -> Vec<CronJob> {
136 self.cron_scheduler.list_jobs()
137 }
138
139 pub fn resource_snapshot(&self) -> ResourceSnapshot {
141 self.resource_monitor.snapshot()
142 }
143
144 pub fn resource_history(&self, last_n: usize) -> Vec<ResourceSnapshot> {
146 self.resource_monitor.history(last_n)
147 }
148
149 pub fn is_overloaded(&self) -> bool {
151 self.resource_monitor.is_overloaded()
152 }
153
154 pub fn subscribe(&self) -> tokio::sync::broadcast::Receiver<KernelEvent> {
156 self.event_bus.subscribe()
157 }
158
159 pub fn publish(&self, event: KernelEvent) -> anyhow::Result<()> {
161 self.event_bus
162 .publish(event)
163 .map_err(|e| anyhow::anyhow!("broadcast error: {e}"))
164 }
165
166 pub fn config(&self) -> &OxiosConfig {
168 &self.config
169 }
170
171 pub fn scheduler(&self) -> &Arc<AgentScheduler> {
173 &self.scheduler
174 }
175
176 pub fn resource_monitor(&self) -> &Arc<ResourceMonitor> {
178 &self.resource_monitor
179 }
180
181 pub fn update_orchestrator_config(&self, config: crate::config::OrchestratorConfig) {
183 *self.orchestrator_config.write() = config;
184 }
185
186 pub fn uptime(&self) -> Duration {
188 self.start_time.elapsed()
189 }
190
191 pub fn pending_tool_approvals(&self) -> &PendingToolApprovals {
193 &self.pending_tool_approvals
194 }
195
196 pub fn pending_ask_user(&self) -> Arc<PendingAskUser> {
198 Arc::clone(&self.pending_ask_user)
199 }
200
201 pub fn event_bus_clone(&self) -> EventBus {
204 self.event_bus.clone()
205 }
206
207 pub fn list_available_tools(&self) -> &'static [ToolMeta] {
209 known_tools()
210 }
211}