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},
cli::{ConfigCommand, DescribeCommand, GetCommand},
};
#[derive(Clone, Copy, Debug)]
pub enum CatalogCommand<'a> {
Get(&'a GetCommand),
Describe(&'a DescribeCommand),
Validate,
Config(&'a ConfigCommand),
}
#[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: CatalogCommand<'_>,
reader: &LocalConfigReader,
) -> Result<CommandResult, CatalogError> {
match command {
CatalogCommand::Get(resource) => execute_get(resource, reader),
CatalogCommand::Describe(resource) => execute_describe(resource, reader),
CatalogCommand::Validate => Ok(CommandResult::Validation(ValidationSummary {
source: reader.source_display(),
valid: true,
agents: reader.list_agents().len(),
capabilities: reader.list_capabilities().len(),
providers: reader.list_providers().len(),
})),
CatalogCommand::Config(ConfigCommand::View) => Ok(CommandResult::Config(reader.snapshot())),
}
}
fn execute_get(
command: &GetCommand,
reader: &LocalConfigReader,
) -> Result<CommandResult, CatalogError> {
match command {
GetCommand::Agent { name } => reader.get_agent(name).map(CommandResult::Agent),
GetCommand::Agents => Ok(CommandResult::Agents(reader.list_agents())),
GetCommand::Capability { name } => {
reader.get_capability(name).map(CommandResult::Capability)
}
GetCommand::Capabilities => Ok(CommandResult::Capabilities(reader.list_capabilities())),
GetCommand::Provider { name } => reader.get_provider(name).map(CommandResult::Provider),
GetCommand::Providers => Ok(CommandResult::Providers(reader.list_providers())),
}
}
fn execute_describe(
command: &DescribeCommand,
reader: &LocalConfigReader,
) -> 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,
}))
}
}
}