agent-hub-runtime 0.6.0

AgentHub desktop and companion CLI runtime for Tauri applications
Documentation
use crate::core::{
    error::AppError,
    project_git::{self, GitStatus},
    project_worktrees,
    skill_store::SkillStore,
};
use std::sync::Arc;
use tauri::State;

#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GitTagActionRequest {
    action: project_git::GitTagAction,
    tag: String,
    expected_sha: Option<String>,
    branch: Option<String>,
    remote: Option<String>,
}

#[tauri::command]
pub async fn run_project_git_tag_action(
    store: State<'_, Arc<SkillStore>>,
    project_id: String,
    worktree_path: Option<String>,
    request: GitTagActionRequest,
) -> Result<(), AppError> {
    let store = store.inner().clone();
    tauri::async_runtime::spawn_blocking(move || {
        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
            .map_err(AppError::io)?;
        project_git::tag_action(
            &root,
            request.action,
            &request.tag,
            request.expected_sha.as_deref(),
            request.branch.as_deref(),
            request.remote.as_deref(),
        )
        .map_err(AppError::io)
    })
    .await?
}

#[tauri::command]
pub async fn run_project_git_action(
    store: State<'_, Arc<SkillStore>>,
    project_id: String,
    worktree_path: Option<String>,
    action: project_git::GitRepositoryAction,
    branch: Option<String>,
) -> Result<(), AppError> {
    let store = store.inner().clone();
    tauri::async_runtime::spawn_blocking(move || {
        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
            .map_err(AppError::io)?;
        project_git::repository_action(&root, action, branch.as_deref()).map_err(AppError::io)
    })
    .await?
}

#[tauri::command]
pub async fn get_project_git_remotes(
    store: State<'_, Arc<SkillStore>>,
    project_id: String,
    worktree_path: Option<String>,
) -> Result<Vec<project_git::GitRemote>, AppError> {
    let store = store.inner().clone();
    tauri::async_runtime::spawn_blocking(move || {
        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
            .map_err(AppError::io)?;
        project_git::remotes(&root).map_err(AppError::io)
    })
    .await?
}

#[tauri::command]
pub async fn get_project_git_status(
    store: State<'_, Arc<SkillStore>>,
    project_id: String,
    worktree_path: Option<String>,
) -> Result<Option<GitStatus>, AppError> {
    let store = store.inner().clone();
    tauri::async_runtime::spawn_blocking(move || {
        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
            .map_err(AppError::io)?;
        project_git::inspect(&root).map_err(AppError::io)
    })
    .await?
}

#[tauri::command]
pub async fn get_project_git_diff(
    store: State<'_, Arc<SkillStore>>,
    project_id: String,
    worktree_path: Option<String>,
    path: String,
    staged: bool,
) -> Result<String, AppError> {
    let store = store.inner().clone();
    tauri::async_runtime::spawn_blocking(move || {
        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
            .map_err(AppError::io)?;
        project_git::diff(&root, &path, staged).map_err(AppError::io)
    })
    .await?
}

#[tauri::command]
pub async fn set_project_git_staged(
    store: State<'_, Arc<SkillStore>>,
    project_id: String,
    worktree_path: Option<String>,
    paths: Vec<String>,
    staged: bool,
) -> Result<(), AppError> {
    let store = store.inner().clone();
    tauri::async_runtime::spawn_blocking(move || {
        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
            .map_err(AppError::io)?;
        project_git::set_staged(&root, &paths, staged).map_err(AppError::io)
    })
    .await?
}

#[tauri::command]
pub async fn discard_project_git_changes(
    store: State<'_, Arc<SkillStore>>,
    project_id: String,
    worktree_path: Option<String>,
    paths: Vec<String>,
) -> Result<(), AppError> {
    let store = store.inner().clone();
    tauri::async_runtime::spawn_blocking(move || {
        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
            .map_err(AppError::io)?;
        project_git::discard(&root, &paths).map_err(AppError::io)
    })
    .await?
}

#[tauri::command]
pub async fn commit_project_git(
    store: State<'_, Arc<SkillStore>>,
    project_id: String,
    worktree_path: Option<String>,
    message: String,
) -> Result<(), AppError> {
    let store = store.inner().clone();
    tauri::async_runtime::spawn_blocking(move || {
        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
            .map_err(AppError::io)?;
        project_git::commit(&root, &message).map_err(AppError::io)
    })
    .await?
}

#[tauri::command]
pub async fn get_project_git_commit(
    store: State<'_, Arc<SkillStore>>,
    project_id: String,
    worktree_path: Option<String>,
    sha: String,
    path: Option<String>,
) -> Result<project_git::GitCommitDetails, AppError> {
    let store = store.inner().clone();
    tauri::async_runtime::spawn_blocking(move || {
        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
            .map_err(AppError::io)?;
        project_git::commit_details(&root, &sha, path.as_deref()).map_err(AppError::io)
    })
    .await?
}

#[tauri::command]
pub async fn get_project_git_commit_diff(
    store: State<'_, Arc<SkillStore>>,
    project_id: String,
    worktree_path: Option<String>,
    sha: String,
    path: String,
    view: Option<String>,
) -> Result<project_git::GitCommitFileView, AppError> {
    let store = store.inner().clone();
    tauri::async_runtime::spawn_blocking(move || {
        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
            .map_err(AppError::io)?;
        project_git::commit_file_view(&root, &sha, &path, view.as_deref()).map_err(AppError::io)
    })
    .await?
}

#[tauri::command]
pub async fn get_project_git_references(
    store: State<'_, Arc<SkillStore>>,
    project_id: String,
    worktree_path: Option<String>,
) -> Result<project_git::GitReferences, AppError> {
    let store = store.inner().clone();
    tauri::async_runtime::spawn_blocking(move || {
        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
            .map_err(AppError::io)?;
        project_git::references(&root).map_err(AppError::io)
    })
    .await?
}

#[tauri::command]
pub async fn get_project_git_history(
    store: State<'_, Arc<SkillStore>>,
    project_id: String,
    worktree_path: Option<String>,
    sha: String,
    offset: usize,
) -> Result<project_git::GitHistoryPage, AppError> {
    let store = store.inner().clone();
    tauri::async_runtime::spawn_blocking(move || {
        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
            .map_err(AppError::io)?;
        project_git::commit_history(&root, &sha, offset).map_err(AppError::io)
    })
    .await?
}

#[tauri::command]
pub async fn get_project_git_graph(
    store: State<'_, Arc<SkillStore>>,
    project_id: String,
    worktree_path: Option<String>,
    include_remotes: bool,
    limit: usize,
    branch: Option<String>,
    path: Option<String>,
) -> Result<project_git::GitGraph, AppError> {
    let store = store.inner().clone();
    tauri::async_runtime::spawn_blocking(move || {
        let root = project_worktrees::directory(&store, &project_id, worktree_path.as_deref())
            .map_err(AppError::io)?;
        match path {
            Some(path) => project_git::graph_for_path(
                &root,
                include_remotes,
                limit,
                branch.as_deref(),
                Some(&path),
            ),
            None => project_git::graph_for_branch(&root, include_remotes, limit, branch.as_deref()),
        }
        .map_err(AppError::io)
    })
    .await?
}