marona 3.0.0

Universal Model Gateway and MCP/Skill SDK with mandatory project-scoped Marona authentication
Documentation
use crate::{core::Core, Result};
use reqwest::Method;
use serde_json::{json, Value};
use std::sync::Arc;

#[derive(Clone, Debug)]
pub struct Apps {
    pub(crate) core: Arc<Core>,
}

#[derive(Clone, Debug, Default)]
pub struct AppsListOptions {
    pub user_id: Option<String>,
    pub cursor: Option<String>,
    pub limit: Option<u32>,
    pub search: Option<String>,
}

#[derive(Clone, Debug)]
pub struct AppsPage {
    pub apps: Vec<Value>,
    pub next_cursor: Option<String>,
    pub has_more: bool,
    pub raw: Value,
}

impl Apps {
    pub async fn list(&self, options: AppsListOptions) -> Result<AppsPage> {
        let mut query = url::form_urlencoded::Serializer::new(String::new());
        for (key, value) in [
            ("user_id", options.user_id),
            ("cursor", options.cursor),
            ("limit", options.limit.map(|v| v.to_string())),
            ("search", options.search),
        ] {
            if let Some(value) = value.filter(|v| !v.is_empty()) {
                query.append_pair(key, &value);
            }
        }
        let raw: Value = self
            .core
            .json(
                &self.core.runtime_url,
                Method::GET,
                &format!("/runtime/apps?{}", query.finish()),
                None,
            )
            .await?;
        let apps = raw
            .as_array()
            .or_else(|| {
                ["items", "apps", "available_agent_apps"]
                    .iter()
                    .find_map(|key| raw.get(key).and_then(Value::as_array))
            })
            .cloned()
            .unwrap_or_default();
        let next_cursor = raw
            .get("next_cursor")
            .and_then(Value::as_str)
            .filter(|v| !v.is_empty())
            .map(str::to_owned);
        Ok(AppsPage {
            apps: apps.into_iter().filter(Value::is_object).collect(),
            has_more: raw
                .get("has_more")
                .and_then(Value::as_bool)
                .unwrap_or(false)
                || next_cursor.is_some(),
            next_cursor,
            raw,
        })
    }
    pub async fn connect(&self, slug: &str, options: Value) -> Result<Value> {
        let mut options = options.as_object().cloned().unwrap_or_default();
        options.entry("interface").or_insert(json!("api"));
        self.core
            .json(
                &self.core.runtime_url,
                Method::POST,
                &format!("/runtime/service-connections/{}/connect", encode(slug)),
                Some(&json!(options)),
            )
            .await
    }
    pub async fn disconnect(&self, slug: &str) -> Result<Value> {
        self.core
            .json(
                &self.core.runtime_url,
                Method::DELETE,
                &format!("/runtime/service-connections/{}", encode(slug)),
                None,
            )
            .await
    }
}

fn encode(value: &str) -> String {
    url::form_urlencoded::byte_serialize(value.as_bytes()).collect()
}