Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

refactor(gui): split workspace command adapters

Rach Pradhan58526a1aa152
diff --git a/gui/src-tauri/src/commands.rs b/gui/src-tauri/src/commands.rs
index 905bdb04..9f13803b 100644
--- a/gui/src-tauri/src/commands.rs
+++ b/gui/src-tauri/src/commands.rs
@@ -1,22 +1,18 @@
use crate::dto::{
- AgentsPayloadDto, ChatBindingDto, CheckoutGitBranchInput, CloneRepositoryInput,
- CommandDescriptorDto, CommandRunResultDto, CommitGitChangesInput, CompleteProviderAuthInput,
- CreateGitBranchInput, CreateSavedWorkspaceInput, FollowupResponseDto, HandoffChatInput,
- McpImportInput, McpServerActionInput, McpSettingsPayloadDto, PromptSettingsDto,
- ProviderAuthSessionDto, ProviderSummaryDto, QuickStartProjectInput, QuickStartVisibility,
- RemoveProviderInput, RuntimeStatusDto, SaveConversationLayoutInput, SendPromptInput,
- SessionSnapshotDto, StartProviderAuthInput, TerminalCloseInput, TerminalOpenInput,
- TerminalResizeInput, TerminalSessionDto, TerminalWriteInput, UpdatePromptSettingsInput,
- UpdateSavedWorkspaceLayoutInput, WorkflowDraftInput, WorkflowDraftPayloadDto,
+ AgentsPayloadDto, ChatBindingDto, CommandDescriptorDto, CommandRunResultDto,
+ CompleteProviderAuthInput, FollowupResponseDto, HandoffChatInput, McpImportInput,
+ McpServerActionInput, McpSettingsPayloadDto, PromptSettingsDto, ProviderAuthSessionDto,
+ ProviderSummaryDto, RemoveProviderInput, RuntimeStatusDto, SendPromptInput, SessionSnapshotDto,
+ StartProviderAuthInput, UpdatePromptSettingsInput, WorkflowDraftInput, WorkflowDraftPayloadDto,
WorkspaceQueryInput, WorkspaceSearchPayloadDto, WorkspaceSyncPayloadDto,
};
use crate::runtime::{DesktopState, format_error_chain};
-use anyhow::Context;
-use bstr::ByteSlice;
-use std::path::{Path, PathBuf};
-use std::process::Command;
use tauri_plugin_dialog::{DialogExt, FilePath};
+#[path = "commands_workspace.rs"]
+mod workspace;
+pub(crate) use workspace::*;
+
fn map_command_error(error: anyhow::Error) -> String {
format_error_chain(&error)
}
@@ -523,308 +519,6 @@ pub(crate) async fn rename_workspace(
.map_err(map_command_error)
}
-#[tauri::command]
-pub(crate) async fn clone_repository(input: CloneRepositoryInput) -> Result<String, String> {
- clone_repository_to_directory(input).map_err(map_command_error)
-}
-
-#[tauri::command]
-pub(crate) async fn quick_start_project(input: QuickStartProjectInput) -> Result<String, String> {
- create_github_project(input).map_err(map_command_error)
-}
-
-#[tauri::command]
-pub(crate) async fn checkout_git_branch(
- input: CheckoutGitBranchInput,
- state: tauri::State<'_, DesktopState>,
-) -> Result<RuntimeStatusDto, String> {
- state
- .manager
- .checkout_git_branch(input.workspace_path, input.branch_name)
- .await
- .map_err(map_command_error)
-}
-
-#[tauri::command]
-pub(crate) async fn create_git_branch(
- input: CreateGitBranchInput,
- state: tauri::State<'_, DesktopState>,
-) -> Result<RuntimeStatusDto, String> {
- state
- .manager
- .create_git_branch(input.workspace_path, input.branch_name)
- .await
- .map_err(map_command_error)
-}
-
-#[tauri::command]
-pub(crate) async fn commit_git_changes(
- input: CommitGitChangesInput,
- state: tauri::State<'_, DesktopState>,
-) -> Result<RuntimeStatusDto, String> {
- state
- .manager
- .commit_git_changes(input.workspace_path, input.message)
- .await
- .map_err(map_command_error)
-}
-
-#[tauri::command]
-pub(crate) async fn push_git_branch(
- workspace_path: String,
- state: tauri::State<'_, DesktopState>,
-) -> Result<RuntimeStatusDto, String> {
- state
- .manager
- .push_git_branch(workspace_path)
- .await
- .map_err(map_command_error)
-}
-
-#[tauri::command]
-pub(crate) async fn open_in_target(
- workspace_path: String,
- target_id: String,
- state: tauri::State<'_, DesktopState>,
-) -> Result<(), String> {
- state
- .manager
- .open_in_target(workspace_path, target_id)
- .await
- .map_err(map_command_error)
-}
-
-#[tauri::command]
-pub(crate) async fn open_path_in_target(
- workspace_path: String,
- target_id: String,
- path: String,
- state: tauri::State<'_, DesktopState>,
-) -> Result<(), String> {
- state
- .manager
- .open_path_in_target(workspace_path, target_id, path)
- .await
- .map_err(map_command_error)
-}
-
-#[tauri::command]
-pub(crate) async fn open_external_url(url: String) -> Result<(), String> {
- crate::desktop_open::open_external_url(&url).map_err(map_command_error)
-}
-
-#[tauri::command]
-pub(crate) async fn open_path_default(path: String) -> Result<(), String> {
- crate::desktop_open::open_path_default(std::path::Path::new(&path)).map_err(map_command_error)
-}
-
-#[tauri::command]
-pub(crate) async fn open_path_for_edit(path: String) -> Result<(), String> {
- crate::desktop_open::open_path_for_edit(std::path::Path::new(&path)).map_err(map_command_error)
-}
-
-/// Reads a workspace file's current contents so the GUI can synthesize a diff
-/// for operations the harness only reports as a byte-count summary (file
-/// creates/overwrites). Confined to the workspace and capped in size; large or
-/// out-of-tree paths return an error so the caller falls back to a placeholder.
-#[tauri::command]
-pub(crate) async fn read_workspace_file(
- workspace_path: String,
- path: String,
-) -> Result<String, String> {
- // The filesystem work (canonicalize, metadata, read) is synchronous and can
- // block — run it off the async runtime, like `image_thumbnail`.
- tokio::task::spawn_blocking(move || read_workspace_file_blocking(&workspace_path, &path))
- .await
- .map_err(|error| error.to_string())?
-}
-
-fn read_workspace_file_blocking(workspace_path: &str, path: &str) -> Result<String, String> {
- use std::io::Read;
- const MAX_BYTES: u64 = 512 * 1024;
-
- let workspace = Path::new(workspace_path);
- // Reject an empty/blank workspace root. The containment check relies on
- // canonicalizing a real workspace directory, and `Path::starts_with("")`
- // matches every path — so an empty root would bypass the sandbox entirely.
- if workspace_path.trim().is_empty() {
- return Err("path is outside the workspace".into());
- }
- // Fail closed: if the workspace root can't be canonicalized (it doesn't
- // exist or isn't a directory) refuse rather than falling back to the raw,
- // untrusted string. Falling back would let a non-existent/empty workspace
- // defeat the `starts_with` containment check.
- let canonical_workspace = std::fs::canonicalize(workspace)
- .map_err(|_| "path is outside the workspace".to_string())?;
-
- let candidate = Path::new(path);
- let resolved = if candidate.is_absolute() {
- candidate.to_path_buf()
- } else {
- canonical_workspace.join(candidate)
- };
-
- // Canonicalize the target so `..` segments and symlinks resolve before the
- // containment check. A missing target is an error, never a leak.
- let canonical = std::fs::canonicalize(&resolved).map_err(|error| error.to_string())?;
- if !canonical.starts_with(&canonical_workspace) {
- return Err("path is outside the workspace".into());
- }
-
- // Only read regular files: a FIFO/socket/device reports length 0 (passing
- // the size cap) but would block the read indefinitely, hanging the command
- // and the "Loading diff…" card forever.
- let metadata = std::fs::metadata(&canonical).map_err(|error| error.to_string())?;
- if !metadata.is_file() {
- return Err("path is not a regular file".into());
- }
- if metadata.len() > MAX_BYTES {
- return Err("file too large".into());
- }
-
- // Bounded read closes the TOCTOU between the size check above and the read:
- // if the file grows (or the stat lied) we still cap memory at MAX_BYTES.
- let mut file = std::fs::File::open(&canonical).map_err(|error| error.to_string())?;
- let mut contents = String::new();
- file.by_ref()
- .take(MAX_BYTES)
- .read_to_string(&mut contents)
- .map_err(|error| error.to_string())?;
- Ok(contents)
-}
-
-#[tauri::command]
-pub(crate) async fn save_conversation_layout(
- input: SaveConversationLayoutInput,
- state: tauri::State<'_, DesktopState>,
-) -> Result<(), String> {
- state
- .manager
- .save_conversation_layout(input)
- .await
- .map_err(map_command_error)
-}
-
-#[tauri::command]
-pub(crate) async fn get_conversation_layout(
- conversation_id: String,
- state: tauri::State<'_, DesktopState>,
-) -> Result<Option<String>, String> {
- state
- .manager
- .get_conversation_layout(conversation_id)
- .await
- .map_err(map_command_error)
-}
-
-#[tauri::command]
-pub(crate) async fn create_saved_workspace(
- input: CreateSavedWorkspaceInput,
- state: tauri::State<'_, DesktopState>,
-) -> Result<crate::dto::SavedWorkspaceDetailDto, String> {
- state
- .manager
- .create_saved_workspace(input)
- .await
- .map_err(map_command_error)
-}
-
-#[tauri::command]
-pub(crate) async fn update_saved_workspace_layout(
- input: UpdateSavedWorkspaceLayoutInput,
- state: tauri::State<'_, DesktopState>,
-) -> Result<crate::dto::SavedWorkspaceDetailDto, String> {
- state
- .manager
- .update_saved_workspace_layout(input)
- .await
- .map_err(map_command_error)
-}
-
-#[tauri::command]
-pub(crate) async fn get_saved_workspace(
- workspace_id: String,
- state: tauri::State<'_, DesktopState>,
-) -> Result<Option<crate::dto::SavedWorkspaceDetailDto>, String> {
- state
- .manager
- .get_saved_workspace(workspace_id)
- .await
- .map_err(map_command_error)
-}
-
-#[tauri::command]
-pub(crate) async fn rename_saved_workspace(
- workspace_id: String,
- name: String,
- state: tauri::State<'_, DesktopState>,
-) -> Result<SessionSnapshotDto, String> {
- state
- .manager
- .rename_saved_workspace(workspace_id, name)
- .await
- .map_err(map_command_error)
-}
-
-#[tauri::command]
-pub(crate) async fn delete_saved_workspace(
- workspace_id: String,
- state: tauri::State<'_, DesktopState>,
-) -> Result<SessionSnapshotDto, String> {
- state
- .manager
- .delete_saved_workspace(workspace_id)
- .await
- .map_err(map_command_error)
-}
-
-#[tauri::command]
-pub(crate) async fn terminal_open(
- input: TerminalOpenInput,
- state: tauri::State<'_, DesktopState>,
-) -> Result<TerminalSessionDto, String> {
- state
- .terminal_manager
- .open(input)
- .await
- .map_err(map_command_error)
-}
-
-#[tauri::command]
-pub(crate) async fn terminal_write(
- input: TerminalWriteInput,
- state: tauri::State<'_, DesktopState>,
-) -> Result<(), String> {
- state
- .terminal_manager
- .write(input)
- .await
- .map_err(map_command_error)
-}
-
-#[tauri::command]
-pub(crate) async fn terminal_resize(
- input: TerminalResizeInput,
- state: tauri::State<'_, DesktopState>,
-) -> Result<(), String> {
- state
- .terminal_manager
- .resize(input)
- .await
- .map_err(map_command_error)
-}
-
-#[tauri::command]
-pub(crate) async fn terminal_close(
- input: TerminalCloseInput,
- state: tauri::State<'_, DesktopState>,
-) -> Result<(), String> {
- state
- .terminal_manager
- .close(input)
- .await
- .map_err(map_command_error)
-}
fn file_path_to_string(path: FilePath) -> Option<String> {
match path {
FilePath::Path(path) => Some(path.to_string_lossy().into_owned()),
@@ -835,124 +529,3 @@ fn file_path_to_string(path: FilePath) -> Option<String> {
.or_else(|| Some(url.to_string())),
}
}
-
-fn clone_repository_to_directory(input: CloneRepositoryInput) -> anyhow::Result<String> {
- let repository_url = input.repository_url.trim();
- if repository_url.is_empty() {
- anyhow::bail!("Repository URL cannot be empty.");
- }
-
- let parent_directory = canonicalize_existing_directory(&input.parent_directory)?;
- let directory_name = validate_directory_name(&input.directory_name)?;
- let target_directory = parent_directory.join(&directory_name);
- ensure_directory_is_available(&target_directory)?;
-
- let mut command = Command::new("git");
- command
- .arg("clone")
- .arg(repository_url)
- .arg(&target_directory)
- .current_dir(&parent_directory);
- run_command(&mut command, "git clone")?;
-
- canonicalize_output_directory(&target_directory)
-}
-
-fn create_github_project(input: QuickStartProjectInput) -> anyhow::Result<String> {
- let parent_directory = canonicalize_existing_directory(&input.parent_directory)?;
- let project_name = validate_directory_name(&input.project_name)?;
- let target_directory = parent_directory.join(&project_name);
- ensure_directory_is_available(&target_directory)?;
-
- let visibility_flag = match input.visibility {
- QuickStartVisibility::Public => "--public",
- QuickStartVisibility::Private => "--private",
- };
-
- let mut command = Command::new("gh");
- command
- .arg("repo")
- .arg("create")
- .arg(&project_name)
- .arg(visibility_flag)
- .arg("--clone")
- .arg("--add-readme")
- .current_dir(&parent_directory);
- run_command(&mut command, "gh repo create")?;
-
- canonicalize_output_directory(&target_directory)
-}
-
-fn canonicalize_existing_directory(path: &str) -> anyhow::Result<PathBuf> {
- let candidate = PathBuf::from(path.trim());
- if path.trim().is_empty() {
- anyhow::bail!("Directory cannot be empty.");
- }
-
- if !candidate.exists() {
- anyhow::bail!("Directory does not exist: {}", candidate.display());
- }
-
- if !candidate.is_dir() {
- anyhow::bail!("Expected a directory: {}", candidate.display());
- }
-
- candidate
- .canonicalize()
- .with_context(|| format!("Failed to access {}", candidate.display()))
-}
-
-fn canonicalize_output_directory(path: &Path) -> anyhow::Result<String> {
- Ok(path
- .canonicalize()
- .with_context(|| format!("Failed to access {}", path.display()))?
- .to_string_lossy()
- .into_owned())
-}
-
-fn validate_directory_name(value: &str) -> anyhow::Result<String> {
- let trimmed = value.trim();
- if trimmed.is_empty() {
- anyhow::bail!("Name cannot be empty.");
- }
-
- if trimmed == "." || trimmed == ".." {
- anyhow::bail!("Choose a different name.");
- }
-
- if trimmed.contains('/') || trimmed.contains('\\') {
- anyhow::bail!("Name cannot contain path separators.");
- }
-
- Ok(trimmed.to_string())
-}
-
-fn ensure_directory_is_available(path: &Path) -> anyhow::Result<()> {
- if path.exists() {
- anyhow::bail!("{} already exists.", path.display());
- }
-
- Ok(())
-}
-
-fn run_command(command: &mut Command, description: &str) -> anyhow::Result<()> {
- let output = command
- .output()
- .with_context(|| format!("Failed to launch {description}. Make sure it is installed."))?;
-
- if output.status.success() {
- return Ok(());
- }
-
- let stderr = output.stderr.as_bstr().trim().to_str_lossy().into_owned();
- let stdout = output.stdout.as_bstr().trim().to_str_lossy().into_owned();
- let details = if !stderr.is_empty() {
- stderr
- } else if !stdout.is_empty() {
- stdout
- } else {
- format!("{description} exited with status {}", output.status)
- };
-
- anyhow::bail!("{details}")
-}
diff --git a/gui/src-tauri/src/commands_workspace.rs b/gui/src-tauri/src/commands_workspace.rs
new file mode 100644
index 00000000..12cce4c5
--- /dev/null
+++ b/gui/src-tauri/src/commands_workspace.rs
@@ -0,0 +1,426 @@
+use super::map_command_error;
+use crate::dto::{
+ CheckoutGitBranchInput, CloneRepositoryInput, CommitGitChangesInput, CreateGitBranchInput,
+ CreateSavedWorkspaceInput, QuickStartProjectInput, QuickStartVisibility, RuntimeStatusDto,
+ SaveConversationLayoutInput, SessionSnapshotDto, TerminalCloseInput, TerminalOpenInput,
+ TerminalResizeInput, TerminalSessionDto, TerminalWriteInput, UpdateSavedWorkspaceLayoutInput,
+};
+use crate::runtime::DesktopState;
+use anyhow::Context;
+use bstr::ByteSlice;
+use std::path::{Path, PathBuf};
+use std::process::Command;
+
+#[tauri::command]
+pub(crate) async fn clone_repository(input: CloneRepositoryInput) -> Result<String, String> {
+ clone_repository_to_directory(input).map_err(map_command_error)
+}
+
+#[tauri::command]
+pub(crate) async fn quick_start_project(input: QuickStartProjectInput) -> Result<String, String> {
+ create_github_project(input).map_err(map_command_error)
+}
+
+#[tauri::command]
+pub(crate) async fn checkout_git_branch(
+ input: CheckoutGitBranchInput,
+ state: tauri::State<'_, DesktopState>,
+) -> Result<RuntimeStatusDto, String> {
+ state
+ .manager
+ .checkout_git_branch(input.workspace_path, input.branch_name)
+ .await
+ .map_err(map_command_error)
+}
+
+#[tauri::command]
+pub(crate) async fn create_git_branch(
+ input: CreateGitBranchInput,
+ state: tauri::State<'_, DesktopState>,
+) -> Result<RuntimeStatusDto, String> {
+ state
+ .manager
+ .create_git_branch(input.workspace_path, input.branch_name)
+ .await
+ .map_err(map_command_error)
+}
+
+#[tauri::command]
+pub(crate) async fn commit_git_changes(
+ input: CommitGitChangesInput,
+ state: tauri::State<'_, DesktopState>,
+) -> Result<RuntimeStatusDto, String> {
+ state
+ .manager
+ .commit_git_changes(input.workspace_path, input.message)
+ .await
+ .map_err(map_command_error)
+}
+
+#[tauri::command]
+pub(crate) async fn push_git_branch(
+ workspace_path: String,
+ state: tauri::State<'_, DesktopState>,
+) -> Result<RuntimeStatusDto, String> {
+ state
+ .manager
+ .push_git_branch(workspace_path)
+ .await
+ .map_err(map_command_error)
+}
+
+#[tauri::command]
+pub(crate) async fn open_in_target(
+ workspace_path: String,
+ target_id: String,
+ state: tauri::State<'_, DesktopState>,
+) -> Result<(), String> {
+ state
+ .manager
+ .open_in_target(workspace_path, target_id)
+ .await
+ .map_err(map_command_error)
+}
+
+#[tauri::command]
+pub(crate) async fn open_path_in_target(
+ workspace_path: String,
+ target_id: String,
+ path: String,
+ state: tauri::State<'_, DesktopState>,
+) -> Result<(), String> {
+ state
+ .manager
+ .open_path_in_target(workspace_path, target_id, path)
+ .await
+ .map_err(map_command_error)
+}
+
+#[tauri::command]
+pub(crate) async fn open_external_url(url: String) -> Result<(), String> {
+ crate::desktop_open::open_external_url(&url).map_err(map_command_error)
+}
+
+#[tauri::command]
+pub(crate) async fn open_path_default(path: String) -> Result<(), String> {
+ crate::desktop_open::open_path_default(Path::new(&path)).map_err(map_command_error)
+}
+
+#[tauri::command]
+pub(crate) async fn open_path_for_edit(path: String) -> Result<(), String> {
+ crate::desktop_open::open_path_for_edit(Path::new(&path)).map_err(map_command_error)
+}
+
+/// Reads a workspace file's current contents so the GUI can synthesize a diff
+/// for operations the harness only reports as a byte-count summary (file
+/// creates/overwrites). Confined to the workspace and capped in size; large or
+/// out-of-tree paths return an error so the caller falls back to a placeholder.
+#[tauri::command]
+pub(crate) async fn read_workspace_file(
+ workspace_path: String,
+ path: String,
+) -> Result<String, String> {
+ // Keep blocking canonicalization, metadata, and reads off the async runtime.
+ tokio::task::spawn_blocking(move || read_workspace_file_blocking(&workspace_path, &path))
+ .await
+ .map_err(|error| error.to_string())?
+}
+
+fn read_workspace_file_blocking(workspace_path: &str, path: &str) -> Result<String, String> {
+ use std::io::Read;
+ const MAX_BYTES: u64 = 512 * 1024;
+
+ let workspace = Path::new(workspace_path);
+ // An empty root would make Path::starts_with match every candidate.
+ if workspace_path.trim().is_empty() {
+ return Err("path is outside the workspace".into());
+ }
+ // Fail closed when the trusted root cannot be canonicalized.
+ let canonical_workspace = std::fs::canonicalize(workspace)
+ .map_err(|_| "path is outside the workspace".to_string())?;
+
+ let candidate = Path::new(path);
+ let resolved = if candidate.is_absolute() {
+ candidate.to_path_buf()
+ } else {
+ canonical_workspace.join(candidate)
+ };
+
+ // Resolve traversal and symlinks before enforcing workspace containment.
+ let canonical = std::fs::canonicalize(&resolved).map_err(|error| error.to_string())?;
+ if !canonical.starts_with(&canonical_workspace) {
+ return Err("path is outside the workspace".into());
+ }
+
+ // FIFOs, sockets, and devices may report zero bytes but block indefinitely.
+ let metadata = std::fs::metadata(&canonical).map_err(|error| error.to_string())?;
+ if !metadata.is_file() {
+ return Err("path is not a regular file".into());
+ }
+ if metadata.len() > MAX_BYTES {
+ return Err("file too large".into());
+ }
+
+ // Keep the read bounded if the file grows after the metadata check.
+ let mut file = std::fs::File::open(&canonical).map_err(|error| error.to_string())?;
+ let mut contents = String::new();
+ file.by_ref()
+ .take(MAX_BYTES)
+ .read_to_string(&mut contents)
+ .map_err(|error| error.to_string())?;
+ Ok(contents)
+}
+
+#[tauri::command]
+pub(crate) async fn save_conversation_layout(
+ input: SaveConversationLayoutInput,
+ state: tauri::State<'_, DesktopState>,
+) -> Result<(), String> {
+ state
+ .manager
+ .save_conversation_layout(input)
+ .await
+ .map_err(map_command_error)
+}
+
+#[tauri::command]
+pub(crate) async fn get_conversation_layout(
+ conversation_id: String,
+ state: tauri::State<'_, DesktopState>,
+) -> Result<Option<String>, String> {
+ state
+ .manager
+ .get_conversation_layout(conversation_id)
+ .await
+ .map_err(map_command_error)
+}
+
+#[tauri::command]
+pub(crate) async fn create_saved_workspace(
+ input: CreateSavedWorkspaceInput,
+ state: tauri::State<'_, DesktopState>,
+) -> Result<crate::dto::SavedWorkspaceDetailDto, String> {
+ state
+ .manager
+ .create_saved_workspace(input)
+ .await
+ .map_err(map_command_error)
+}
+
+#[tauri::command]
+pub(crate) async fn update_saved_workspace_layout(
+ input: UpdateSavedWorkspaceLayoutInput,
+ state: tauri::State<'_, DesktopState>,
+) -> Result<crate::dto::SavedWorkspaceDetailDto, String> {
+ state
+ .manager
+ .update_saved_workspace_layout(input)
+ .await
+ .map_err(map_command_error)
+}
+
+#[tauri::command]
+pub(crate) async fn get_saved_workspace(
+ workspace_id: String,
+ state: tauri::State<'_, DesktopState>,
+) -> Result<Option<crate::dto::SavedWorkspaceDetailDto>, String> {
+ state
+ .manager
+ .get_saved_workspace(workspace_id)
+ .await
+ .map_err(map_command_error)
+}
+
+#[tauri::command]
+pub(crate) async fn rename_saved_workspace(
+ workspace_id: String,
+ name: String,
+ state: tauri::State<'_, DesktopState>,
+) -> Result<SessionSnapshotDto, String> {
+ state
+ .manager
+ .rename_saved_workspace(workspace_id, name)
+ .await
+ .map_err(map_command_error)
+}
+
+#[tauri::command]
+pub(crate) async fn delete_saved_workspace(
+ workspace_id: String,
+ state: tauri::State<'_, DesktopState>,
+) -> Result<SessionSnapshotDto, String> {
+ state
+ .manager
+ .delete_saved_workspace(workspace_id)
+ .await
+ .map_err(map_command_error)
+}
+
+#[tauri::command]
+pub(crate) async fn terminal_open(
+ input: TerminalOpenInput,
+ state: tauri::State<'_, DesktopState>,
+) -> Result<TerminalSessionDto, String> {
+ state
+ .terminal_manager
+ .open(input)
+ .await
+ .map_err(map_command_error)
+}
+
+#[tauri::command]
+pub(crate) async fn terminal_write(
+ input: TerminalWriteInput,
+ state: tauri::State<'_, DesktopState>,
+) -> Result<(), String> {
+ state
+ .terminal_manager
+ .write(input)
+ .await
+ .map_err(map_command_error)
+}
+
+#[tauri::command]
+pub(crate) async fn terminal_resize(
+ input: TerminalResizeInput,
+ state: tauri::State<'_, DesktopState>,
+) -> Result<(), String> {
+ state
+ .terminal_manager
+ .resize(input)
+ .await
+ .map_err(map_command_error)
+}
+
+#[tauri::command]
+pub(crate) async fn terminal_close(
+ input: TerminalCloseInput,
+ state: tauri::State<'_, DesktopState>,
+) -> Result<(), String> {
+ state
+ .terminal_manager
+ .close(input)
+ .await
+ .map_err(map_command_error)
+}
+
+fn clone_repository_to_directory(input: CloneRepositoryInput) -> anyhow::Result<String> {
+ let repository_url = input.repository_url.trim();
+ if repository_url.is_empty() {
+ anyhow::bail!("Repository URL cannot be empty.");
+ }
+
+ let parent_directory = canonicalize_existing_directory(&input.parent_directory)?;
+ let directory_name = validate_directory_name(&input.directory_name)?;
+ let target_directory = parent_directory.join(&directory_name);
+ ensure_directory_is_available(&target_directory)?;
+
+ let mut command = Command::new("git");
+ command
+ .arg("clone")
+ .arg(repository_url)
+ .arg(&target_directory)
+ .current_dir(&parent_directory);
+ run_command(&mut command, "git clone")?;
+
+ canonicalize_output_directory(&target_directory)
+}
+
+fn create_github_project(input: QuickStartProjectInput) -> anyhow::Result<String> {
+ let parent_directory = canonicalize_existing_directory(&input.parent_directory)?;
+ let project_name = validate_directory_name(&input.project_name)?;
+ let target_directory = parent_directory.join(&project_name);
+ ensure_directory_is_available(&target_directory)?;
+
+ let visibility_flag = match input.visibility {
+ QuickStartVisibility::Public => "--public",
+ QuickStartVisibility::Private => "--private",
+ };
+
+ let mut command = Command::new("gh");
+ command
+ .arg("repo")
+ .arg("create")
+ .arg(&project_name)
+ .arg(visibility_flag)
+ .arg("--clone")
+ .arg("--add-readme")
+ .current_dir(&parent_directory);
+ run_command(&mut command, "gh repo create")?;
+
+ canonicalize_output_directory(&target_directory)
+}
+
+fn canonicalize_existing_directory(path: &str) -> anyhow::Result<PathBuf> {
+ let candidate = PathBuf::from(path.trim());
+ if path.trim().is_empty() {
+ anyhow::bail!("Directory cannot be empty.");
+ }
+
+ if !candidate.exists() {
+ anyhow::bail!("Directory does not exist: {}", candidate.display());
+ }
+
+ if !candidate.is_dir() {
+ anyhow::bail!("Expected a directory: {}", candidate.display());
+ }
+
+ candidate
+ .canonicalize()
+ .with_context(|| format!("Failed to access {}", candidate.display()))
+}
+
+fn canonicalize_output_directory(path: &Path) -> anyhow::Result<String> {
+ Ok(path
+ .canonicalize()
+ .with_context(|| format!("Failed to access {}", path.display()))?
+ .to_string_lossy()
+ .into_owned())
+}
+
+fn validate_directory_name(value: &str) -> anyhow::Result<String> {
+ let trimmed = value.trim();
+ if trimmed.is_empty() {
+ anyhow::bail!("Name cannot be empty.");
+ }
+
+ if trimmed == "." || trimmed == ".." {
+ anyhow::bail!("Choose a different name.");
+ }
+
+ if trimmed.contains('/') || trimmed.contains('\\') {
+ anyhow::bail!("Name cannot contain path separators.");
+ }
+
+ Ok(trimmed.to_string())
+}
+
+fn ensure_directory_is_available(path: &Path) -> anyhow::Result<()> {
+ if path.exists() {
+ anyhow::bail!("{} already exists.", path.display());
+ }
+
+ Ok(())
+}
+
+fn run_command(command: &mut Command, description: &str) -> anyhow::Result<()> {
+ let output = command
+ .output()
+ .with_context(|| format!("Failed to launch {description}. Make sure it is installed."))?;
+
+ if output.status.success() {
+ return Ok(());
+ }
+
+ let stderr = output.stderr.as_bstr().trim().to_str_lossy().into_owned();
+ let stdout = output.stdout.as_bstr().trim().to_str_lossy().into_owned();
+ let details = if !stderr.is_empty() {
+ stderr
+ } else if !stdout.is_empty() {
+ stdout
+ } else {
+ format!("{description} exited with status {}", output.status)
+ };
+
+ anyhow::bail!("{details}")
+}