use crate::{normalize_executable_name, AdapterBinding, AgentId, AgentReadinessSpec};
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum RuntimePlatform {
Windows,
MacOs,
Linux,
Wsl,
}
impl RuntimePlatform {
pub fn current() -> Self {
if cfg!(target_os = "windows") {
Self::Windows
} else if cfg!(target_os = "macos") {
Self::MacOs
} else {
Self::Linux
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum HostArch {
X86_64,
Aarch64,
}
impl HostArch {
pub fn current() -> Option<Self> {
if cfg!(target_arch = "x86_64") {
Some(Self::X86_64)
} else if cfg!(target_arch = "aarch64") {
Some(Self::Aarch64)
} else {
None
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct DetectionSpec {
pub command: String,
#[serde(default)]
pub aliases: Vec<String>,
#[serde(default)]
pub required_commands: Vec<String>,
#[serde(default)]
pub unsupported_platforms: Vec<RuntimePlatform>,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct PlatformLaunch {
pub platform: RuntimePlatform,
#[serde(default)]
pub arch: Option<HostArch>,
pub program: String,
pub fixed_args: Vec<String>,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct LaunchSpec {
pub program: String,
#[serde(default)]
pub fixed_args: Vec<String>,
#[serde(default)]
pub by_platform: Vec<PlatformLaunch>,
}
impl LaunchSpec {
pub fn resolve(
&self,
platform: RuntimePlatform,
arch: Option<HostArch>,
remote: bool,
) -> (&str, &[String]) {
if remote && platform == RuntimePlatform::Linux {
return (self.program.as_str(), self.fixed_args.as_slice());
}
if let Some(arch) = arch {
if let Some(row) = self
.by_platform
.iter()
.find(|row| row.platform == platform && row.arch == Some(arch))
{
return (row.program.as_str(), row.fixed_args.as_slice());
}
}
if let Some(row) = self
.by_platform
.iter()
.find(|row| row.platform == platform && row.arch.is_none())
{
return (row.program.as_str(), row.fixed_args.as_slice());
}
(self.program.as_str(), self.fixed_args.as_slice())
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum ProcessMatcher {
Exact { name: String },
Prefix { prefix: String },
}
impl ProcessMatcher {
pub fn matches(&self, process_or_path: &str, platform: RuntimePlatform) -> bool {
let process = normalize_executable_name(process_or_path, platform);
match self {
Self::Exact { name } => {
let expected = normalize_executable_name(name, platform);
process == expected
}
Self::Prefix { prefix } => {
let expected = if platform == RuntimePlatform::Windows {
prefix.to_ascii_lowercase()
} else {
prefix.clone()
};
process.starts_with(&expected)
}
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum InitialPromptMode {
None,
Positional { option_terminator: bool },
Flag { flag: String },
InteractiveFlag { flag: String },
AgentNativeQuery,
AfterReady,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum NativeDraftMode {
Flag { flag: String },
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ModifiedEnterEncoding {
CsiU,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum WindowsPasteNewline {
AltEnter,
CsiU,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct PtySubmitContract {
#[serde(default)]
pub submit_joins_paste_frame: bool,
#[serde(default)]
pub paste_needs_typed_request: bool,
#[serde(default)]
pub submit_line_settle_ms_per_line: u64,
#[serde(default)]
pub submit_retry_delay_ms: Option<u64>,
#[serde(default)]
pub submit_retry_budget_ms: Option<u64>,
#[serde(default)]
pub draft_paste_ready_timeout_ms: Option<u64>,
#[serde(default)]
pub windows_shift_enter: Option<ModifiedEnterEncoding>,
#[serde(default)]
pub ctrl_enter: Option<ModifiedEnterEncoding>,
#[serde(default)]
pub windows_paste_newline: Option<WindowsPasteNewline>,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct PromptSpec {
pub initial: InitialPromptMode,
#[serde(default)]
pub native_draft: Option<NativeDraftMode>,
#[serde(default)]
pub pty_submit: PtySubmitContract,
#[serde(default)]
pub prefill_env: Option<String>,
#[serde(default)]
pub vendor_draft_signal: Option<String>,
#[serde(default)]
pub vendor_submit_signal: Option<String>,
}
impl Default for PromptSpec {
fn default() -> Self {
Self {
initial: InitialPromptMode::None,
native_draft: None,
pty_submit: PtySubmitContract::default(),
prefill_env: None,
vendor_draft_signal: None,
vendor_submit_signal: None,
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum AgentCommandMode {
SlashLine,
}
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct AgentCapabilities {
#[serde(default)]
pub agent_commands: Option<AgentCommandMode>,
#[serde(default)]
pub transports: AgentTransportCapabilities,
#[serde(default)]
pub adapters: AgentAdapterCapabilities,
}
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct AgentAdapterCapabilities {
#[serde(default)]
pub pty_sidecar: Option<AdapterBinding>,
#[serde(default)]
pub hook: Option<AdapterBinding>,
#[serde(default)]
pub managed_hook: Option<AdapterBinding>,
#[serde(default)]
pub one_shot: Option<AdapterBinding>,
#[serde(default)]
pub history: Option<AdapterBinding>,
#[serde(default)]
pub resume: Option<AdapterBinding>,
#[serde(default)]
pub session_options: Option<AdapterBinding>,
#[serde(default)]
pub capability_probe: Option<AdapterBinding>,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum PipePromptDelivery {
None,
StdinClose,
Positional,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum PipeProtocol {
#[default]
SemanticNdjson,
StructuredJsonl,
OneShotText,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct PipeTransportSpec {
pub adapter: AdapterBinding,
#[serde(default)]
pub protocol: PipeProtocol,
#[serde(default)]
pub launch_override: Option<LaunchSpec>,
#[serde(default = "default_pipe_prompt_delivery")]
pub prompt_delivery: PipePromptDelivery,
}
fn default_pipe_prompt_delivery() -> PipePromptDelivery {
PipePromptDelivery::None
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct AcpTransportSpec {
pub adapter: AdapterBinding,
#[serde(default)]
pub launch_override: Option<LaunchSpec>,
#[serde(default)]
pub env: Vec<(String, String)>,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct AgentTransportCapabilities {
#[serde(default = "default_true")]
pub pty: bool,
#[serde(default)]
pub pty_adapter: Option<AdapterBinding>,
#[serde(default)]
pub pipe: Option<PipeTransportSpec>,
#[serde(default)]
pub acp: Option<AcpTransportSpec>,
}
impl Default for AgentTransportCapabilities {
fn default() -> Self {
Self {
pty: true,
pty_adapter: None,
pipe: None,
acp: None,
}
}
}
fn default_true() -> bool {
true
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum SpecVerification {
Gate4AgentVerified,
Reference,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct AgentSpec {
pub id: AgentId,
pub revision: String,
pub display_name: String,
pub detection: DetectionSpec,
pub launch: LaunchSpec,
pub expected_processes: Vec<ProcessMatcher>,
pub prompt: PromptSpec,
pub readiness: AgentReadinessSpec,
#[serde(default)]
pub capabilities: AgentCapabilities,
pub verification: SpecVerification,
}
impl AgentSpec {
pub fn supports_platform(&self, platform: RuntimePlatform) -> bool {
!self.detection.unsupported_platforms.contains(&platform)
}
}