|
| 1 | +//! Tauri commands for workspace-root FeatureSet bindings. |
| 2 | +//! |
| 3 | +//! Bindings are the middle tier of the FeatureSet resolver (pin > binding > |
| 4 | +//! space-active). Paths passed in from the UI are normalized via |
| 5 | +//! [`mcpmux_core::normalize_workspace_root`] before storage so lookups from |
| 6 | +//! the gateway's session registry hit them consistently. |
| 7 | +
|
| 8 | +use mcpmux_core::{normalize_workspace_root, WorkspaceBinding}; |
| 9 | +use serde::{Deserialize, Serialize}; |
| 10 | +use tauri::State; |
| 11 | +use tracing::{error, info}; |
| 12 | +use uuid::Uuid; |
| 13 | + |
| 14 | +use crate::state::AppState; |
| 15 | + |
| 16 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 17 | +pub struct WorkspaceBindingDto { |
| 18 | + pub id: String, |
| 19 | + pub space_id: String, |
| 20 | + pub workspace_root: String, |
| 21 | + pub feature_set_id: String, |
| 22 | + pub created_at: String, |
| 23 | + pub updated_at: String, |
| 24 | +} |
| 25 | + |
| 26 | +impl From<WorkspaceBinding> for WorkspaceBindingDto { |
| 27 | + fn from(b: WorkspaceBinding) -> Self { |
| 28 | + Self { |
| 29 | + id: b.id.to_string(), |
| 30 | + space_id: b.space_id.to_string(), |
| 31 | + workspace_root: b.workspace_root, |
| 32 | + feature_set_id: b.feature_set_id.to_string(), |
| 33 | + created_at: b.created_at.to_rfc3339(), |
| 34 | + updated_at: b.updated_at.to_rfc3339(), |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/// List every binding across all Spaces. |
| 40 | +#[tauri::command] |
| 41 | +pub async fn list_workspace_bindings( |
| 42 | + state: State<'_, AppState>, |
| 43 | +) -> Result<Vec<WorkspaceBindingDto>, String> { |
| 44 | + state |
| 45 | + .workspace_binding_repository |
| 46 | + .list() |
| 47 | + .await |
| 48 | + .map(|v| v.into_iter().map(Into::into).collect()) |
| 49 | + .map_err(|e| { |
| 50 | + error!("[workspace_binding::list] {e}"); |
| 51 | + e.to_string() |
| 52 | + }) |
| 53 | +} |
| 54 | + |
| 55 | +/// List bindings for a specific Space. |
| 56 | +#[tauri::command] |
| 57 | +pub async fn list_workspace_bindings_for_space( |
| 58 | + space_id: String, |
| 59 | + state: State<'_, AppState>, |
| 60 | +) -> Result<Vec<WorkspaceBindingDto>, String> { |
| 61 | + let space_uuid = Uuid::parse_str(&space_id).map_err(|e| e.to_string())?; |
| 62 | + state |
| 63 | + .workspace_binding_repository |
| 64 | + .list_for_space(&space_uuid) |
| 65 | + .await |
| 66 | + .map(|v| v.into_iter().map(Into::into).collect()) |
| 67 | + .map_err(|e| e.to_string()) |
| 68 | +} |
| 69 | + |
| 70 | +/// Create a new binding. `workspace_root` is normalized before storage. |
| 71 | +#[tauri::command] |
| 72 | +pub async fn create_workspace_binding( |
| 73 | + space_id: String, |
| 74 | + workspace_root: String, |
| 75 | + feature_set_id: String, |
| 76 | + state: State<'_, AppState>, |
| 77 | +) -> Result<WorkspaceBindingDto, String> { |
| 78 | + let space_uuid = Uuid::parse_str(&space_id).map_err(|e| e.to_string())?; |
| 79 | + let fs_uuid = Uuid::parse_str(&feature_set_id).map_err(|e| e.to_string())?; |
| 80 | + let normalized = normalize_workspace_root(&workspace_root); |
| 81 | + if normalized.is_empty() { |
| 82 | + return Err("workspace_root cannot be empty".into()); |
| 83 | + } |
| 84 | + let binding = WorkspaceBinding::new(space_uuid, normalized, fs_uuid); |
| 85 | + state |
| 86 | + .workspace_binding_repository |
| 87 | + .create(&binding) |
| 88 | + .await |
| 89 | + .map_err(|e| e.to_string())?; |
| 90 | + info!( |
| 91 | + space_id = %binding.space_id, |
| 92 | + workspace_root = %binding.workspace_root, |
| 93 | + feature_set_id = %binding.feature_set_id, |
| 94 | + "[workspace_binding] created", |
| 95 | + ); |
| 96 | + Ok(binding.into()) |
| 97 | +} |
| 98 | + |
| 99 | +/// Update an existing binding (e.g., point it at a different FS). |
| 100 | +#[tauri::command] |
| 101 | +pub async fn update_workspace_binding( |
| 102 | + id: String, |
| 103 | + workspace_root: String, |
| 104 | + feature_set_id: String, |
| 105 | + state: State<'_, AppState>, |
| 106 | +) -> Result<WorkspaceBindingDto, String> { |
| 107 | + let id_uuid = Uuid::parse_str(&id).map_err(|e| e.to_string())?; |
| 108 | + let fs_uuid = Uuid::parse_str(&feature_set_id).map_err(|e| e.to_string())?; |
| 109 | + let normalized = normalize_workspace_root(&workspace_root); |
| 110 | + if normalized.is_empty() { |
| 111 | + return Err("workspace_root cannot be empty".into()); |
| 112 | + } |
| 113 | + let existing = state |
| 114 | + .workspace_binding_repository |
| 115 | + .get(&id_uuid) |
| 116 | + .await |
| 117 | + .map_err(|e| e.to_string())? |
| 118 | + .ok_or_else(|| format!("binding not found: {}", id))?; |
| 119 | + let updated = WorkspaceBinding { |
| 120 | + id: existing.id, |
| 121 | + space_id: existing.space_id, |
| 122 | + workspace_root: normalized, |
| 123 | + feature_set_id: fs_uuid, |
| 124 | + created_at: existing.created_at, |
| 125 | + updated_at: chrono::Utc::now(), |
| 126 | + }; |
| 127 | + state |
| 128 | + .workspace_binding_repository |
| 129 | + .update(&updated) |
| 130 | + .await |
| 131 | + .map_err(|e| e.to_string())?; |
| 132 | + Ok(updated.into()) |
| 133 | +} |
| 134 | + |
| 135 | +/// Delete a binding by id. |
| 136 | +#[tauri::command] |
| 137 | +pub async fn delete_workspace_binding( |
| 138 | + id: String, |
| 139 | + state: State<'_, AppState>, |
| 140 | +) -> Result<(), String> { |
| 141 | + let id_uuid = Uuid::parse_str(&id).map_err(|e| e.to_string())?; |
| 142 | + state |
| 143 | + .workspace_binding_repository |
| 144 | + .delete(&id_uuid) |
| 145 | + .await |
| 146 | + .map_err(|e| e.to_string()) |
| 147 | +} |
0 commit comments