use async_graphql::{Enum, Object, SimpleObject};
use chrono::{DateTime, Utc};
use llm_registry_core::{Asset, AssetStatus, AssetType};
use llm_registry_service::DependencyNode;
#[derive(Clone)]
pub struct GqlAsset(pub Asset);
#[Object]
impl GqlAsset {
async fn id(&self) -> String {
self.0.id.to_string()
}
async fn asset_type(&self) -> GqlAssetType {
GqlAssetType::from_core(&self.0.asset_type)
}
async fn name(&self) -> &str {
&self.0.metadata.name
}
async fn version(&self) -> String {
self.0.metadata.version.to_string()
}
async fn description(&self) -> Option<&str> {
self.0.metadata.description.as_deref()
}
async fn license(&self) -> Option<&str> {
self.0.metadata.license.as_deref()
}
async fn tags(&self) -> &[String] {
&self.0.metadata.tags
}
async fn annotations(&self) -> Vec<GqlAnnotation> {
self.0
.metadata
.annotations
.iter()
.map(|(k, v)| GqlAnnotation {
key: k.clone(),
value: v.clone(),
})
.collect()
}
async fn size_bytes(&self) -> Option<u64> {
self.0.metadata.size_bytes
}
async fn content_type(&self) -> Option<&str> {
self.0.metadata.content_type.as_deref()
}
async fn status(&self) -> GqlAssetStatus {
GqlAssetStatus::from_core(&self.0.status)
}
async fn storage_path(&self) -> &str {
&self.0.storage.path
}
async fn storage_uri(&self) -> Option<&str> {
self.0.storage.uri.as_deref()
}
async fn checksum_algorithm(&self) -> String {
self.0.checksum.algorithm.to_string()
}
async fn checksum_value(&self) -> &str {
&self.0.checksum.value
}
async fn dependency_count(&self) -> usize {
self.0.dependencies.len()
}
async fn created_at(&self) -> DateTime<Utc> {
self.0.created_at
}
async fn updated_at(&self) -> DateTime<Utc> {
self.0.updated_at
}
async fn deprecated_at(&self) -> Option<DateTime<Utc>> {
self.0.deprecated_at
}
}
#[derive(Clone)]
pub struct GqlDependencyNode {
node: DependencyNode,
}
impl From<DependencyNode> for GqlDependencyNode {
fn from(node: DependencyNode) -> Self {
GqlDependencyNode { node }
}
}
#[Object]
impl GqlDependencyNode {
async fn asset_id(&self) -> String {
self.node.asset_id.to_string()
}
async fn name(&self) -> &str {
&self.node.name
}
async fn version(&self) -> String {
self.node.version.to_string()
}
async fn depth(&self) -> i32 {
self.node.depth
}
async fn dependency_count(&self) -> usize {
self.node.dependencies.len()
}
}
#[derive(Enum, Copy, Clone, Eq, PartialEq)]
pub enum GqlAssetType {
Model,
Pipeline,
TestSuite,
Policy,
Dataset,
}
impl GqlAssetType {
pub fn from_core(asset_type: &AssetType) -> Self {
match asset_type {
AssetType::Model => GqlAssetType::Model,
AssetType::Pipeline => GqlAssetType::Pipeline,
AssetType::TestSuite => GqlAssetType::TestSuite,
AssetType::Policy => GqlAssetType::Policy,
AssetType::Dataset => GqlAssetType::Dataset,
AssetType::Custom(_) => GqlAssetType::Model, }
}
pub fn to_core(&self) -> AssetType {
match self {
GqlAssetType::Model => AssetType::Model,
GqlAssetType::Pipeline => AssetType::Pipeline,
GqlAssetType::TestSuite => AssetType::TestSuite,
GqlAssetType::Policy => AssetType::Policy,
GqlAssetType::Dataset => AssetType::Dataset,
}
}
}
#[derive(Enum, Copy, Clone, Eq, PartialEq)]
pub enum GqlAssetStatus {
Active,
Deprecated,
Archived,
NonCompliant,
}
impl GqlAssetStatus {
pub fn from_core(status: &AssetStatus) -> Self {
match status {
AssetStatus::Active => GqlAssetStatus::Active,
AssetStatus::Deprecated => GqlAssetStatus::Deprecated,
AssetStatus::Archived => GqlAssetStatus::Archived,
AssetStatus::NonCompliant => GqlAssetStatus::NonCompliant,
}
}
pub fn to_core(&self) -> AssetStatus {
match self {
GqlAssetStatus::Active => AssetStatus::Active,
GqlAssetStatus::Deprecated => AssetStatus::Deprecated,
GqlAssetStatus::Archived => AssetStatus::Archived,
GqlAssetStatus::NonCompliant => AssetStatus::NonCompliant,
}
}
}
#[derive(SimpleObject, Clone)]
pub struct GqlAnnotation {
pub key: String,
pub value: String,
}
#[derive(SimpleObject)]
pub struct GqlAssetConnection {
pub nodes: Vec<GqlAsset>,
pub total_count: i64,
pub has_next_page: bool,
}
#[derive(async_graphql::InputObject)]
pub struct GqlAssetFilter {
pub asset_type: Option<GqlAssetType>,
pub status: Option<GqlAssetStatus>,
pub tags: Option<Vec<String>>,
pub name: Option<String>,
}
#[derive(SimpleObject)]
pub struct GqlRegisterResult {
pub asset: GqlAsset,
pub message: String,
}
#[derive(SimpleObject)]
pub struct GqlUpdateResult {
pub asset: GqlAsset,
pub message: String,
}
#[derive(SimpleObject)]
pub struct GqlDeleteResult {
pub asset_id: String,
pub message: String,
}