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::tools::{PendingAskUser, PendingToolApprovals, known_tools};
10use std::sync::Arc;
11use std::time::{Duration, Instant};
12
13pub struct InfraApi {
15 pub(crate) git_layer: Arc<GitLayer>,
16 pub(crate) cron_scheduler: Arc<CronScheduler>,
17 pub(crate) resource_monitor: Arc<ResourceMonitor>,
18 pub(crate) event_bus: EventBus,
19 pub(crate) config: OxiosConfig,
20 pub(crate) start_time: Instant,
21 pub(crate) orchestrator_config: parking_lot::RwLock<crate::config::OrchestratorConfig>,
23 pub(crate) pending_tool_approvals: PendingToolApprovals,
25 pub(crate) pending_ask_user: Arc<PendingAskUser>,
27}
28
29impl InfraApi {
30 pub fn new(
32 git_layer: Arc<GitLayer>,
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 cron_scheduler,
42 resource_monitor,
43 event_bus,
44 config,
45 start_time,
46 orchestrator_config: parking_lot::RwLock::new(
47 crate::config::OrchestratorConfig::default(),
48 ),
49 pending_tool_approvals: PendingToolApprovals::new(),
50 pending_ask_user: Arc::new(PendingAskUser::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 git_delete_tag(&self, name: &str) -> anyhow::Result<()> {
85 self.git_layer.delete_tag(name)
86 }
87
88 pub async fn add_cron(&self, job: CronJob) -> anyhow::Result<uuid::Uuid> {
90 self.cron_scheduler.add_job(job).await
91 }
92
93 pub fn get_cron(&self, id: uuid::Uuid) -> Option<CronJob> {
95 self.cron_scheduler.get_job(id)
96 }
97
98 pub async fn update_cron(&self, id: uuid::Uuid, update: CronJobUpdate) -> anyhow::Result<()> {
100 self.cron_scheduler.update_job(id, update).await
101 }
102
103 pub async fn remove_cron(&self, id: uuid::Uuid) -> anyhow::Result<()> {
105 self.cron_scheduler.remove_job(id).await
106 }
107
108 pub fn trigger_cron(&self, id: uuid::Uuid) -> anyhow::Result<CronJob> {
110 self.cron_scheduler.trigger_job(id)
111 }
112
113 pub async fn complete_cron(&self, id: uuid::Uuid, success: bool, summary: String) {
115 self.cron_scheduler
116 .mark_job_completed(id, success, summary)
117 .await
118 }
119
120 pub fn list_crons(&self) -> Vec<CronJob> {
122 self.cron_scheduler.list_jobs()
123 }
124
125 pub fn resource_snapshot(&self) -> ResourceSnapshot {
127 self.resource_monitor.snapshot()
128 }
129
130 pub fn resource_history(&self, last_n: usize) -> Vec<ResourceSnapshot> {
132 self.resource_monitor.history(last_n)
133 }
134
135 pub fn is_overloaded(&self) -> bool {
137 self.resource_monitor.is_overloaded()
138 }
139
140 pub fn subscribe(&self) -> tokio::sync::broadcast::Receiver<KernelEvent> {
142 self.event_bus.subscribe()
143 }
144
145 pub fn publish(&self, event: KernelEvent) -> anyhow::Result<()> {
147 self.event_bus
148 .publish(event)
149 .map_err(|e| anyhow::anyhow!("broadcast error: {e}"))
150 }
151
152 pub fn config(&self) -> &OxiosConfig {
154 &self.config
155 }
156
157 pub fn resource_monitor(&self) -> &Arc<ResourceMonitor> {
159 &self.resource_monitor
160 }
161
162 pub fn update_orchestrator_config(&self, config: crate::config::OrchestratorConfig) {
164 *self.orchestrator_config.write() = config;
165 }
166
167 pub fn uptime(&self) -> Duration {
169 self.start_time.elapsed()
170 }
171
172 pub fn pending_tool_approvals(&self) -> &PendingToolApprovals {
174 &self.pending_tool_approvals
175 }
176
177 pub fn pending_ask_user(&self) -> Arc<PendingAskUser> {
179 Arc::clone(&self.pending_ask_user)
180 }
181
182 pub fn event_bus_clone(&self) -> EventBus {
185 self.event_bus.clone()
186 }
187
188 pub fn list_available_tools(&self) -> &'static [ToolMeta] {
190 known_tools()
191 }
192}