pub mod artifacts;
pub mod cell_hints;
pub mod code_skills;
pub mod context_control;
pub mod durable_work;
pub mod family;
pub mod goal;
pub mod hints;
pub mod images;
pub mod instances;
pub mod memory;
pub mod memory_import;
pub mod memory_pointers;
pub mod memory_records;
pub mod progress;
pub mod prompt_schedule;
pub mod schedule_tick;
pub mod schedules;
pub mod sessions;
pub mod skills;
pub mod store;
pub mod tasks;
pub mod tool_calls;
pub mod util;
use kernel::{HostHandler, Signal};
use serde_json::{Map, Value};
use std::sync::{Arc, Mutex};
pub use artifacts::{FileKeyedDocuments, KeyedDocuments, MemoryKeyedDocuments};
pub use code_skills::{CodeSkillKind, CodeSkills, CodeSkillsOptions};
pub use context_control::{ContextBranches, ContextControl};
pub use durable_work::{DurableWork, DurableWorkOptions, Recovered};
pub use family::{FamilyModule, FamilyOptions};
pub use goal::{GoalDriver, GoalJobs, JobState, TaskGoalJobs};
pub use hints::HintsModule;
pub use images::{ImagesConfig, ImagesModule};
pub use instances::InstancesModule;
pub use memory::{MemoryApi, MemoryBackend, MemoryError, MemoryService, worker_gate};
pub use progress::ProgressModule;
pub use schedule_tick::{ManualTickClock, ScheduleTicker, TickClock};
pub use schedules::SchedulesModule;
pub use sessions::SessionsModule;
pub use skills::{PinnedSkill, SkillLoader, SkillSource, SkillsModule};
pub use tasks::{RootOnly, TaskHost};
pub use tool_calls::{ToolCallEnd, ToolCalls, ToolInfo, ToolRunner};
pub struct Request<'a> {
pub kind: &'a str,
pub payload: &'a Map<String, Value>,
pub lane: &'a str,
pub caller_task: Option<&'a str>,
pub host: &'a dyn TaskHost,
pub signal: &'a Signal,
}
pub trait ApiModule: Send + Sync {
fn prefixes(&self) -> &[&str];
fn start(&self, _host: &dyn TaskHost) -> Result<(), String> {
Ok(())
}
fn handle(&self, request: &Request<'_>) -> Result<Value, String>;
}
pub struct KernelApis {
inner: Arc<dyn HostHandler>,
host: Arc<dyn TaskHost>,
lane: String,
modules: Vec<Arc<dyn ApiModule>>,
started: Mutex<Option<Result<(), String>>>,
}
impl KernelApis {
pub fn new(inner: Arc<dyn HostHandler>) -> Self {
Self {
inner,
host: Arc::new(RootOnly {
now: util::system_clock(),
}),
lane: kernel::ROOT_LANE.into(),
modules: Vec::new(),
started: Mutex::new(None),
}
}
pub fn with_host(mut self, host: Arc<dyn TaskHost>, lane: impl Into<String>) -> Self {
self.host = host;
self.lane = lane.into();
self
}
pub fn with(mut self, module: Arc<dyn ApiModule>) -> Self {
self.modules.push(module);
self
}
fn start(&self) -> Result<(), String> {
let mut started = jsrt::sync::lock(&self.started);
started
.get_or_insert_with(|| {
self.modules
.iter()
.try_for_each(|module| module.start(self.host.as_ref()))
})
.clone()
}
}
impl HostHandler for KernelApis {
fn handle(&self, kind: &str, payload: Map<String, Value>, signal: &Signal) -> Result<Value, String> {
self.start()?;
let module = self
.modules
.iter()
.find(|module| module.prefixes().iter().any(|prefix| kind.starts_with(prefix)));
let Some(module) = module else {
return self.inner.handle(kind, payload, signal);
};
let caller = self.host.caller_task_id(&self.lane);
module.handle(&Request {
kind,
payload: &payload,
lane: &self.lane,
caller_task: caller.as_deref(),
host: self.host.as_ref(),
signal,
})
}
}