use crate::{GraphrtError, Runtime};
#[derive(Clone, Debug, PartialEq, Eq, uniffi::Record)]
pub struct SkillGrant {
pub tool: String,
pub allow: bool,
}
#[derive(Clone, Debug, PartialEq, Eq, uniffi::Record)]
pub struct SkillInstall {
pub skill_md: String,
#[uniffi(default)]
pub grants: Vec<SkillGrant>,
#[uniffi(default)]
pub replace: bool,
#[uniffi(default)]
pub accept_findings: bool,
}
#[derive(Clone, Debug, PartialEq, Eq, uniffi::Record)]
pub struct SkillInfo {
pub name: String,
pub description: String,
pub title: Option<String>,
pub enabled: bool,
pub requested_tools: Vec<String>,
pub grants: Vec<SkillGrant>,
pub findings: Vec<String>,
pub version: Option<String>,
}
#[cfg(feature = "skills")]
mod imp {
use std::sync::Arc;
use graphrt_skill::{Grant, GrantMode, InstallOptions, OriginKind, SkillEntry, SkillOrigin, SkillStore};
use super::{SkillGrant, SkillInfo, SkillInstall};
use crate::GraphrtError;
pub(crate) type Store = Arc<SkillStore>;
fn failed(e: graphrt_skill::Error) -> GraphrtError {
GraphrtError::input(e.to_string())
}
pub(crate) fn install(dir: &str, services: &mut graphrt_core::Services) -> Store {
let store = Arc::new(SkillStore::open(dir));
services.insert(graphrt_skill::SkillSet::new(Arc::clone(&store)).into_service());
store
}
fn info(entry: SkillEntry) -> SkillInfo {
SkillInfo {
name: entry.name,
description: entry.description,
title: entry.title,
enabled: entry.enabled,
requested_tools: entry.requested_tools,
grants: entry
.grants
.into_iter()
.map(|g| SkillGrant { tool: g.tool, allow: g.mode == GrantMode::Allow })
.collect(),
findings: entry
.findings
.iter()
.map(|f| format!("{:?} {}: {}", f.severity, f.code, f.message).to_lowercase())
.collect(),
version: entry.version,
}
}
pub(crate) fn install_skill(store: &SkillStore, request: SkillInstall) -> Result<SkillInfo, GraphrtError> {
let package = graphrt_skill::package::package_from_skill_file(&request.skill_md).map_err(failed)?;
let grants = request
.grants
.into_iter()
.map(|g| if g.allow { Grant::allow(g.tool) } else { Grant::ask(g.tool) })
.collect();
let options = InstallOptions {
grants,
accept_findings: request.accept_findings,
replace: request.replace,
origin: SkillOrigin {
kind: OriginKind::Authored,
detail: Some("app".to_owned()),
..SkillOrigin::default()
},
};
store.install_package(&package, options).map(info).map_err(failed)
}
pub(crate) fn list(store: &SkillStore) -> Result<Vec<SkillInfo>, GraphrtError> {
Ok(store.list().map_err(failed)?.into_iter().map(info).collect())
}
pub(crate) fn remove(store: &SkillStore, name: &str) -> Result<(), GraphrtError> {
store.remove(name).map_err(failed)
}
pub(crate) fn set_enabled(store: &SkillStore, name: &str, enabled: bool) -> Result<SkillInfo, GraphrtError> {
store.set_enabled(name, enabled).map(info).map_err(failed)
}
}
#[cfg(feature = "skills")]
pub(crate) use imp::{Store, install};
#[cfg(not(feature = "skills"))]
pub(crate) type Store = ();
impl Runtime {
#[cfg(feature = "skills")]
fn skill_store(&self) -> Result<&Store, GraphrtError> {
self.skills.as_ref().ok_or_else(|| GraphrtError::not_configured("skills", "skills_dir"))
}
}
#[uniffi::export]
impl Runtime {
pub fn install_skill(&self, request: SkillInstall) -> Result<SkillInfo, GraphrtError> {
#[cfg(feature = "skills")]
{
imp::install_skill(self.skill_store()?, request)
}
#[cfg(not(feature = "skills"))]
{
let _ = request;
Err(GraphrtError::unsupported("skills", "skills"))
}
}
pub fn list_skills(&self) -> Result<Vec<SkillInfo>, GraphrtError> {
#[cfg(feature = "skills")]
{
imp::list(self.skill_store()?)
}
#[cfg(not(feature = "skills"))]
{
Err(GraphrtError::unsupported("skills", "skills"))
}
}
pub fn set_skill_enabled(&self, name: String, enabled: bool) -> Result<SkillInfo, GraphrtError> {
#[cfg(feature = "skills")]
{
imp::set_enabled(self.skill_store()?, &name, enabled)
}
#[cfg(not(feature = "skills"))]
{
let _ = (name, enabled);
Err(GraphrtError::unsupported("skills", "skills"))
}
}
pub fn remove_skill(&self, name: String) -> Result<(), GraphrtError> {
#[cfg(feature = "skills")]
{
imp::remove(self.skill_store()?, &name)
}
#[cfg(not(feature = "skills"))]
{
let _ = name;
Err(GraphrtError::unsupported("skills", "skills"))
}
}
}