use dekopon_config::CatalogSnapshot;
use dekopon_core::Redacted;
use dekopon_model::chatgpt::ChatGptAuthStatus;
use dekopon_protocol::{Agent, Capability, Provider};
use serde::Serialize;
use crate::{
catalog::{CatalogError, LocalConfigReader, ResourceReader},
cli::{Command, ConfigCommand, DescribeCommand, GetCommand},
};
#[derive(Clone, Debug)]
pub enum CommandResult {
Version(VersionInfo),
Auth(ModelAuthStatus),
CredentialExport(Redacted<String>),
Agents(Vec<Agent>),
Agent(Agent),
Capabilities(Vec<Capability>),
Capability(Capability),
Providers(Vec<Provider>),
Provider(Provider),
AgentDescription(AgentDescription),
Validation(ValidationSummary),
Config(CatalogSnapshot),
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ModelAuthStatus {
pub account: &'static str,
pub credential_file: String,
pub signed_in: bool,
pub expired: bool,
}
impl ModelAuthStatus {
pub(crate) fn chatgpt(status: ChatGptAuthStatus) -> Self {
Self {
account: "chatgpt",
credential_file: status.path.display().to_string(),
signed_in: status.signed_in,
expired: status.expired,
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct VersionInfo {
pub product: &'static str,
pub version: &'static str,
pub api_version: &'static str,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AgentDescription {
pub agent: Agent,
pub capabilities: Vec<Capability>,
pub providers: Vec<Provider>,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ValidationSummary {
pub source: String,
pub valid: bool,
pub agents: usize,
pub capabilities: usize,
pub providers: usize,
}
#[must_use]
pub const fn version_result() -> CommandResult {
CommandResult::Version(VersionInfo {
product: "dekopon",
version: env!("CARGO_PKG_VERSION"),
api_version: "dekopon.dev/v1alpha1",
})
}
pub fn execute(
command: &Command,
reader: &LocalConfigReader,
) -> Result<CommandResult, CatalogError> {
match command {
Command::Version => Ok(version_result()),
Command::Auth { .. } => {
unreachable!("auth commands are executed before catalog resolution")
}
Command::Get { resource } => execute_get(resource, reader),
Command::Describe { resource } => execute_describe(resource, reader),
Command::Validate => {
let agents = reader.list_agents()?.len();
let capabilities = reader.list_capabilities()?.len();
let providers = reader.list_providers()?.len();
Ok(CommandResult::Validation(ValidationSummary {
source: reader.source_display(),
valid: true,
agents,
capabilities,
providers,
}))
}
Command::Config {
command: ConfigCommand::View,
} => Ok(CommandResult::Config(reader.snapshot())),
}
}
fn execute_get(
command: &GetCommand,
reader: &impl ResourceReader,
) -> Result<CommandResult, CatalogError> {
match command {
GetCommand::Agent { name } => reader.get_agent(name).map(CommandResult::Agent),
GetCommand::Agents => reader.list_agents().map(CommandResult::Agents),
GetCommand::Capability { name } => {
reader.get_capability(name).map(CommandResult::Capability)
}
GetCommand::Capabilities => reader.list_capabilities().map(CommandResult::Capabilities),
GetCommand::Provider { name } => reader.get_provider(name).map(CommandResult::Provider),
GetCommand::Providers => reader.list_providers().map(CommandResult::Providers),
}
}
fn execute_describe(
command: &DescribeCommand,
reader: &impl ResourceReader,
) -> Result<CommandResult, CatalogError> {
match command {
DescribeCommand::Agent { name } => {
let agent = reader.get_agent(name)?;
let mut capabilities = agent
.spec
.capabilities
.iter()
.map(|id| reader.get_capability(id))
.collect::<Result<Vec<_>, _>>()?;
let mut providers = agent
.spec
.providers
.iter()
.map(|id| reader.get_provider(id))
.collect::<Result<Vec<_>, _>>()?;
capabilities.sort_by(|left, right| left.metadata.name.cmp(&right.metadata.name));
providers.sort_by(|left, right| left.metadata.name.cmp(&right.metadata.name));
Ok(CommandResult::AgentDescription(AgentDescription {
agent,
capabilities,
providers,
}))
}
}
}