#![cfg_attr(
feature = "drivers",
doc = r#"| [`DriverManifest`], [`DriverSandbox`], [`DriverLifecycle`], [`DriverAccess`], [`load_manifests`] (feature `drivers`) | §7.7 |
| [`DriverStore`], [`DriverRecord`], [`DriverPark`], [`DriverIsolation`] (feature `drivers`) | §7.7, §9.3, §13 Q8, Q9 |
| [`DriverProcessTree`] (feature `drivers`) | §7.7, §10 W1, K-I5 |
| [`DriverSupervisor`], [`DriverSupervisorBuilder`], [`DriverProposal`], [`DriverView`], [`StandingRulePins`] (feature `drivers`) | §7.7, K-I13, K-I14 |
| [`DriverPolicy`] (feature `drivers`) | §7.7, K-I9, K-I14 |"#
)]
#![cfg_attr(
not(feature = "drivers"),
doc = r#"| `DriverManifest`, `DriverSandbox`, `DriverLifecycle`, `DriverAccess`, `load_manifests` (feature `drivers`) | §7.7 |
| `DriverStore`, `DriverRecord`, `DriverPark`, `DriverIsolation` (feature `drivers`) | §7.7, §9.3, §13 Q8, Q9 |
| `DriverProcessTree` (feature `drivers`) | §7.7, §10 W1, K-I5 |
| `DriverSupervisor`, `DriverSupervisorBuilder`, `DriverProposal`, `DriverView`, `StandingRulePins` (feature `drivers`) | §7.7, K-I13, K-I14 |
| `DriverPolicy` (feature `drivers`) | §7.7, K-I9, K-I14 |"#
)]
use std::fmt;
use std::str::FromStr;
use adk_core::ToolEffect;
use serde::{Deserialize, Serialize};
use crate::error::{KernelError, Result};
#[cfg(feature = "drivers")]
mod launch;
#[cfg(feature = "drivers")]
mod manifest;
#[cfg(feature = "drivers")]
mod policy;
#[cfg(feature = "drivers")]
pub(crate) mod store;
#[cfg(feature = "drivers")]
mod supervisor;
#[cfg(feature = "drivers")]
mod tool;
#[cfg(feature = "drivers")]
pub use manifest::{
DriverAccess, DriverLifecycle, DriverManifest, DriverSandbox, SECRET_SCHEME, load_dir,
load_manifests,
};
#[cfg(feature = "drivers")]
pub use policy::DriverPolicy;
#[cfg(feature = "drivers")]
pub use store::{DriverIsolation, DriverPark, DriverProcessTree, DriverRecord, DriverStore};
#[cfg(feature = "drivers")]
pub(crate) use supervisor::not_mounted;
#[cfg(feature = "drivers")]
pub use supervisor::{
DriverProposal, DriverSupervisor, DriverSupervisorBuilder, DriverView, StandingRulePins,
};
pub fn driver_source(id: &str) -> String {
format!("{DRIVER_SOURCE_PREFIX}{id}")
}
pub fn driver_of(source: &str) -> Option<&str> {
source.strip_prefix(DRIVER_SOURCE_PREFIX).filter(|id| !id.is_empty())
}
pub const DRIVER_SOURCE_PREFIX: &str = "driver:";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum EffectClass {
Read,
WriteLocal,
ScheduleWithOthers,
SendExternal,
PublishPublic,
Financial,
Delete,
Other,
}
impl EffectClass {
pub const ALL: [EffectClass; 8] = [
Self::Read,
Self::WriteLocal,
Self::ScheduleWithOthers,
Self::SendExternal,
Self::PublishPublic,
Self::Financial,
Self::Delete,
Self::Other,
];
pub fn as_str(self) -> &'static str {
match self {
Self::Read => "read",
Self::WriteLocal => "write_local",
Self::ScheduleWithOthers => "schedule_with_others",
Self::SendExternal => "send_external",
Self::PublishPublic => "publish_public",
Self::Financial => "financial",
Self::Delete => "delete",
Self::Other => "other",
}
}
pub fn tool_effect(self) -> ToolEffect {
match self {
Self::Read => ToolEffect::ReadOnly,
_ => ToolEffect::NonIdempotent,
}
}
pub fn is_never_auto_allowed(self) -> bool {
matches!(self, Self::PublishPublic | Self::Financial)
}
pub fn strictness(self) -> u8 {
match self {
Self::Read => 0,
Self::PublishPublic | Self::Financial => 2,
_ => 1,
}
}
}
impl fmt::Display for EffectClass {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for EffectClass {
type Err = KernelError;
fn from_str(text: &str) -> Result<Self> {
let normalized = text.trim().to_ascii_lowercase().replace('-', "_");
Self::ALL.into_iter().find(|class| class.as_str() == normalized).ok_or_else(|| {
KernelError::Config {
reason: format!(
"unknown effect class `{text}`; use one of {}",
Self::ALL.map(Self::as_str).join(", ")
),
}
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum DriverState {
Starting,
Running,
Stopped,
Crashed,
Restarting,
Disabled,
FailedToStart,
NeedsReapproval,
}
#[cfg(feature = "drivers")]
impl From<adk_tool::mcp::ServerStatus> for DriverState {
fn from(status: adk_tool::mcp::ServerStatus) -> Self {
use adk_tool::mcp::ServerStatus;
match status {
ServerStatus::Starting => Self::Starting,
ServerStatus::Running => Self::Running,
ServerStatus::Stopped => Self::Stopped,
ServerStatus::Crashed => Self::Crashed,
ServerStatus::Restarting => Self::Restarting,
ServerStatus::Disabled => Self::Disabled,
ServerStatus::FailedToStart => Self::FailedToStart,
}
}
}
impl DriverState {
pub fn as_str(self) -> &'static str {
match self {
Self::Starting => "starting",
Self::Running => "running",
Self::Stopped => "stopped",
Self::Crashed => "crashed",
Self::Restarting => "restarting",
Self::Disabled => "disabled",
Self::FailedToStart => "failedToStart",
Self::NeedsReapproval => "needsReapproval",
}
}
}