use std::collections::BTreeMap;
use adk_core::{ApprovalScope, ToolConfirmationRequest};
use adk_tool::mcp::ParkedMcpCall;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use super::manifest::DriverManifest;
use crate::error::Result;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "camelCase", rename_all_fields = "camelCase")]
#[non_exhaustive]
pub enum DriverIsolation {
OsSandbox {
enforcer: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
program_paths: Vec<std::path::PathBuf>,
},
Unsandboxed {
reason: String,
},
}
impl DriverIsolation {
pub fn is_sandboxed(&self) -> bool {
matches!(self, Self::OsSandbox { .. })
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum DriverProcessTree {
JobObject,
ProcessGroup,
}
impl DriverProcessTree {
pub(crate) fn platform() -> Option<Self> {
if cfg!(windows) {
Some(Self::JobObject)
} else if cfg!(unix) {
Some(Self::ProcessGroup)
} else {
None
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct DriverRecord {
pub manifest: DriverManifest,
pub command_digest: String,
pub approved_by: String,
pub approved_at: DateTime<Utc>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub isolation: Option<DriverIsolation>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub process_tree: Option<DriverProcessTree>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub stale: BTreeMap<String, String>,
pub updated_at: DateTime<Utc>,
}
impl DriverRecord {
pub fn new(manifest: DriverManifest, by: impl Into<String>, at: DateTime<Utc>) -> Self {
Self {
command_digest: manifest.command_digest(),
manifest,
approved_by: by.into(),
approved_at: at,
isolation: None,
process_tree: None,
stale: BTreeMap::new(),
updated_at: at,
}
}
pub fn id(&self) -> &str {
&self.manifest.id
}
pub fn needs_reapproval(&self) -> bool {
!self.stale.is_empty()
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct DriverPark {
pub driver: String,
pub scope: ApprovalScope,
pub call_id: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub run_id: Option<String>,
pub call: ParkedMcpCall,
pub request: ToolConfirmationRequest,
pub fingerprint: String,
pub created_at: DateTime<Utc>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub expires_at: Option<DateTime<Utc>>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub carries_approval: bool,
}
impl DriverPark {
pub fn new(
scope: ApprovalScope,
call_id: impl Into<String>,
call: ParkedMcpCall,
request: ToolConfirmationRequest,
) -> Self {
Self {
driver: call.server_id.clone(),
scope,
call_id: call_id.into(),
run_id: None,
fingerprint: request.fingerprint(),
call,
request,
created_at: Utc::now(),
expires_at: None,
carries_approval: false,
}
}
#[must_use]
pub fn with_run_id(mut self, run_id: impl Into<String>) -> Self {
self.run_id = Some(run_id.into());
self
}
#[must_use]
pub fn with_expires_at(mut self, at: DateTime<Utc>) -> Self {
self.expires_at = Some(at);
self
}
}
#[async_trait]
pub trait DriverStore: Send + Sync {
async fn put(&self, record: DriverRecord) -> Result<()>;
async fn get(&self, id: &str) -> Result<Option<DriverRecord>>;
async fn list(&self) -> Result<Vec<DriverRecord>>;
async fn delete(&self, id: &str) -> Result<bool>;
async fn put_park(&self, park: DriverPark) -> Result<()>;
async fn get_park(&self, scope: &ApprovalScope, call_id: &str) -> Result<Option<DriverPark>>;
async fn list_parks(&self, driver: Option<&str>) -> Result<Vec<DriverPark>>;
async fn delete_park(&self, scope: &ApprovalScope, call_id: &str) -> Result<bool>;
}
pub(crate) fn park_key(scope: &ApprovalScope, call_id: &str) -> (String, String, String, String) {
(scope.app_name.clone(), scope.user_id.clone(), scope.session_id.clone(), call_id.to_string())
}