use crate::core::{
error::AppError,
herdr::{
control::*,
hc::Core,
session_sources::{self, SessionRoute},
tools::HerdrTools,
},
skill_store::SkillStore,
terminal::{TerminalEvent, TerminalManager, TerminalSession, TerminalSize, TerminalSpec},
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tauri::{ipc::Channel, Manager, State, Webview};
fn tools_for(window: &Webview) -> Arc<HerdrTools> {
window.state::<Arc<HerdrTools>>().inner().clone()
}
#[derive(Serialize)]
pub struct Inventory {
installed: bool,
core: bool,
core_installed: bool,
legacy_tui: bool,
sessions: Vec<SessionInfo>,
}
#[tauri::command]
pub async fn herdr_sidebar_keys(window: Webview) -> Result<String, AppError> {
crate::commands::terminal::owner(&window)?;
tauri::async_runtime::spawn_blocking(|| {
crate::core::herdr::control::sidebar_keys().map_err(AppError::io)
})
.await?
}
#[tauri::command]
pub async fn herdr_inventory(
window: Webview,
tools: State<'_, Arc<HerdrTools>>,
store: State<'_, Arc<SkillStore>>,
) -> Result<Inventory, AppError> {
crate::commands::terminal::owner(&window)?;
let tools = tools.inner().clone();
let store = store.inner().clone();
tauri::async_runtime::spawn_blocking(move || {
let core = Core::discover();
let legacy = tools.detect().ok().flatten();
let core_installed = core.is_some();
let legacy_tui = legacy.is_some();
let mut sessions = core.map(session_sources::core_sessions).unwrap_or_default();
for session in sessions.iter_mut().filter(|session| !session.default) {
if let Ok(Some(label)) =
store.get_setting(&session_sources::label_setting(&session.name))
{
if !label.is_empty() {
session.label = Some(label);
}
}
}
if let Some(tui) = legacy {
sessions.extend(session_sources::tui_sessions(&HerdrControl::new(
tui.path.into(),
)));
}
Ok(Inventory {
installed: core_installed || legacy_tui,
core: true,
core_installed,
legacy_tui,
sessions,
})
})
.await?
}
#[tauri::command]
pub async fn herdr_prepare_session(window: Webview) -> Result<String, AppError> {
crate::commands::terminal::owner(&window)?;
tauri::async_runtime::spawn_blocking(move || {
HerdrControl::for_core(session_sources::core("default")?)
.prepare_session()
.map_err(AppError::io)
})
.await?
}
#[tauri::command]
pub async fn herdr_restart_session(window: Webview, session: String) -> Result<(), AppError> {
crate::commands::terminal::owner(&window)?;
let tools = tools_for(&window);
tauri::async_runtime::spawn_blocking(move || {
let route = SessionRoute::open(&tools, &session)?;
route
.control
.restart_session(route.native_session())
.map_err(AppError::io)
})
.await?
}
#[tauri::command]
pub async fn herdr_delete_session(
window: Webview,
session: String,
store: State<'_, Arc<SkillStore>>,
) -> Result<(), AppError> {
crate::commands::terminal::owner(&window)?;
let tools = tools_for(&window);
let store = store.inner().clone();
tauri::async_runtime::spawn_blocking(move || {
if session.starts_with(crate::core::herdr::hc::sessions::PREFIX) {
session_sources::core(&session)?
.archive_named_session()
.map_err(AppError::io)?;
if let Err(error) = store.set_setting(&session_sources::label_setting(&session), "") {
log::warn!("Could not clear the archived HC session display name: {error}");
}
return Ok(());
}
let route = SessionRoute::open(&tools, &session)?;
route
.control
.delete_session(route.native_session())
.map_err(AppError::io)
})
.await?
}
#[tauri::command]
pub async fn herdr_rename_session(
window: Webview,
session: String,
name: String,
store: State<'_, Arc<SkillStore>>,
) -> Result<(), AppError> {
crate::commands::terminal::owner(&window)?;
let store = store.inner().clone();
tauri::async_runtime::spawn_blocking(move || {
let name = name.trim();
if !session.starts_with(crate::core::herdr::hc::sessions::PREFIX)
|| name.chars().count() > 80
|| name.chars().any(char::is_control)
{
return Err(AppError::invalid_input(
"Enter up to 80 characters for a named HC session",
));
}
session_sources::core(&session)?;
store
.set_setting(&session_sources::label_setting(&session), name)
.map_err(AppError::db)
})
.await?
}
#[tauri::command]
pub async fn herdr_snapshot(
window: Webview,
session: String,
refresh_worktrees: Option<bool>,
) -> Result<Snapshot, AppError> {
crate::commands::terminal::owner(&window)?;
let tools = tools_for(&window);
tauri::async_runtime::spawn_blocking(move || {
let route = SessionRoute::open(&tools, &session)?;
let control = &route.control;
let session = route.native_session();
if refresh_worktrees.unwrap_or(false) {
control.refresh_worktrees(session).map_err(AppError::io)?;
}
control.snapshot(session).map_err(AppError::io)
})
.await?
}
#[tauri::command]
pub async fn herdr_open(
window: Webview,
store: State<'_, Arc<SkillStore>>,
session: String,
workspace_id: String,
pane_id: Option<String>,
) -> Result<OpenedWorkspace, AppError> {
crate::commands::terminal::owner(&window)?;
let store = store.inner().clone();
let tools = tools_for(&window);
tauri::async_runtime::spawn_blocking(move || {
let route = SessionRoute::open(&tools, &session)?;
route
.control
.open(
&store,
route.native_session(),
&workspace_id,
pane_id.as_deref(),
)
.map(|opened| route.opened(opened))
.map_err(AppError::io)
})
.await?
}
#[tauri::command]
pub async fn herdr_ensure(
window: Webview,
store: State<'_, Arc<SkillStore>>,
session: String,
project_id: String,
worktree_path: Option<String>,
parent_workspace_id: Option<String>,
) -> Result<OpenedWorkspace, AppError> {
crate::commands::terminal::owner(&window)?;
let store = store.inner().clone();
let tools = tools_for(&window);
tauri::async_runtime::spawn_blocking(move || {
let route = SessionRoute::open(&tools, &session)?;
let control = &route.control;
let session = route.native_session();
if let Some(parent) = parent_workspace_id {
control.open_worktree(
&store,
session,
&project_id,
&parent,
worktree_path
.as_deref()
.ok_or_else(|| AppError::io("Worktree path is required"))?,
)
} else {
control.ensure(&store, session, &project_id, worktree_path.as_deref())
}
.map(|opened| route.opened(opened))
.map_err(AppError::io)
})
.await?
}
#[tauri::command]
pub(crate) async fn herdr_sync_legacy_workspaces(
window: Webview,
tools: State<'_, Arc<HerdrTools>>,
session: Option<String>,
) -> Result<hc_migration::SyncResult, AppError> {
crate::commands::terminal::owner(&window)?;
let tools = tools.inner().clone();
tauri::async_runtime::spawn_blocking(move || {
let core = session_sources::core(session.as_deref().unwrap_or("default"))?;
let legacy = tools
.detect()
.map_err(AppError::io)?
.ok_or_else(|| AppError::not_found("Herdr TUI is not installed"))?;
hc_migration::sync_legacy(&HerdrControl::new(legacy.path.into()), &core)
.map_err(AppError::io)
})
.await?
}
#[tauri::command]
pub(crate) async fn herdr_manage_space(
window: Webview,
store: State<'_, Arc<SkillStore>>,
input: hc_management::SpaceAction,
session: Option<String>,
) -> Result<Option<OpenedWorkspace>, AppError> {
crate::commands::terminal::owner(&window)?;
let store = store.inner().clone();
tauri::async_runtime::spawn_blocking(move || {
let core = session_sources::core(session.as_deref().unwrap_or("default"))?;
hc_management::manage(&core, &store, input).map_err(AppError::io)
})
.await?
}
#[tauri::command]
pub async fn herdr_action(
window: Webview,
target: Target,
action: Action,
text: Option<String>,
) -> Result<ActionResult, AppError> {
crate::commands::terminal::owner(&window)?;
let tools = tools_for(&window);
tauri::async_runtime::spawn_blocking(move || {
let route = SessionRoute::open(&tools, &target.session)?;
let target = route.native_target(target);
route
.control
.action(&target, action, text.as_deref())
.map(|mut result| {
result.target = result.target.map(|target| route.target(target));
result
})
.map_err(AppError::io)
})
.await?
}
#[tauri::command]
pub async fn herdr_start_agent(
window: Webview,
target: Target,
input: NewAgent,
created: Channel<Target>,
) -> Result<StartedAgent, AppError> {
crate::commands::terminal::owner(&window)?;
let tools = tools_for(&window);
tauri::async_runtime::spawn_blocking(move || {
let route = SessionRoute::open(&tools, &target.session)?;
let target = route.native_target(target);
route
.control
.start_agent_with_created(&target, input, |pane| {
created
.send(route.target(pane.clone()))
.map_err(anyhow::Error::from)?;
Ok(())
})
.map(|mut result| {
result.target = route.target(result.target);
result
})
.map_err(AppError::io)
})
.await?
}
#[tauri::command]
pub async fn herdr_terminal_create(
window: Webview,
store: State<'_, Arc<SkillStore>>,
manager: State<'_, Arc<TerminalManager>>,
request: TerminalAttachmentRequest,
events: Channel<TerminalEvent>,
) -> Result<TerminalSession, AppError> {
let owner = crate::commands::terminal::owner(&window)?;
let store = store.inner().clone();
let manager = manager.inner().clone();
let tools = tools_for(&window);
tauri::async_runtime::spawn_blocking(move || {
let TerminalAttachmentRequest {
project_id,
worktree_path,
target,
size,
} = request;
let route = SessionRoute::open(&tools, &target.session)?;
let target = route.native_target(target);
let cwd =
crate::core::project_worktrees::directory(&store, &project_id, Some(&worktree_path))
.map_err(AppError::io)?;
let command = route
.control
.attachment(&store, &project_id, &worktree_path, &target)
.map_err(AppError::io)?;
manager
.create(
&owner,
TerminalSpec {
project_id,
cwd,
command,
integration: None,
size,
},
move |event| events.send(event).is_ok(),
)
.map_err(AppError::io)
})
.await?
}
#[derive(Deserialize)]
pub struct TerminalAttachmentRequest {
project_id: String,
worktree_path: String,
target: Target,
size: TerminalSize,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DeleteWorktreeInput {
session: String,
workspace_id: Option<String>,
project_id: String,
path: String,
}
#[tauri::command]
pub async fn herdr_delete_worktree(
window: Webview,
store: State<'_, Arc<SkillStore>>,
input: DeleteWorktreeInput,
) -> Result<(), AppError> {
crate::commands::terminal::owner(&window)?;
let store = store.inner().clone();
let tools = tools_for(&window);
tauri::async_runtime::spawn_blocking(move || {
if let Some(workspace_id) = input.workspace_id {
let route = SessionRoute::open(&tools, &input.session)?;
route
.control
.remove_worktree(
&store,
route.native_session(),
&workspace_id,
&input.project_id,
&input.path,
)
.map_err(AppError::io)
} else {
crate::core::project_worktrees::remove(&store, &input.project_id, &input.path)
.map_err(AppError::io)
}
})
.await?
}