|
| 1 | +use serde::Deserialize; |
| 2 | +use tauri::{AppHandle, State}; |
| 3 | +use tauri_plugin_dialog::DialogExt; |
| 4 | + |
| 5 | +use crate::config; |
| 6 | +use crate::db::repository::MessageBlockRepo; |
| 7 | +use crate::services::artifacts::{ |
| 8 | + ArtifactMetadata, ArtifactOrigin, ArtifactPreview, ArtifactService, ContentSafetyPolicy, |
| 9 | + ExportOutcome, |
| 10 | +}; |
| 11 | +use crate::services::content::{ContentBlock, MessageBlockService}; |
| 12 | +use crate::AppState; |
| 13 | + |
| 14 | +#[derive(Debug, Deserialize)] |
| 15 | +pub struct ArtifactRegisterRequest { |
| 16 | + pub session_id: String, |
| 17 | + pub origin_message_id: Option<String>, |
| 18 | + pub origin_kind: ArtifactOrigin, |
| 19 | + pub display_name: String, |
| 20 | + pub media_type: String, |
| 21 | + /// Transport-only ingress. It is decoded immediately and never persisted in |
| 22 | + /// SQLite or returned by metadata commands. |
| 23 | + pub bytes_base64: String, |
| 24 | +} |
| 25 | + |
| 26 | +fn service() -> Result<ArtifactService, String> { |
| 27 | + ArtifactService::new( |
| 28 | + config::artifacts_dir().map_err(|error| error.to_string())?, |
| 29 | + ContentSafetyPolicy::default(), |
| 30 | + ) |
| 31 | + .map_err(|error| error.to_string()) |
| 32 | +} |
| 33 | + |
| 34 | +#[tauri::command] |
| 35 | +pub fn artifact_register( |
| 36 | + state: State<'_, AppState>, |
| 37 | + request: ArtifactRegisterRequest, |
| 38 | +) -> Result<ArtifactMetadata, String> { |
| 39 | + if !state.feature_flags.rich_content_write { |
| 40 | + return Err("CONTENT_BLOCK_UNSUPPORTED".to_string()); |
| 41 | + } |
| 42 | + let conn = state.db.lock().map_err(|error| error.to_string())?; |
| 43 | + service()? |
| 44 | + .register_base64( |
| 45 | + &conn, |
| 46 | + request.session_id, |
| 47 | + request.origin_message_id, |
| 48 | + request.origin_kind, |
| 49 | + request.display_name, |
| 50 | + request.media_type, |
| 51 | + &request.bytes_base64, |
| 52 | + ) |
| 53 | + .map_err(|error| error.to_string()) |
| 54 | +} |
| 55 | + |
| 56 | +#[tauri::command] |
| 57 | +pub fn artifact_get_metadata( |
| 58 | + state: State<'_, AppState>, |
| 59 | + session_id: String, |
| 60 | + artifact_id: String, |
| 61 | +) -> Result<ArtifactMetadata, String> { |
| 62 | + let conn = state.db.lock().map_err(|error| error.to_string())?; |
| 63 | + service()? |
| 64 | + .metadata(&conn, &session_id, &artifact_id) |
| 65 | + .map_err(|error| error.to_string()) |
| 66 | +} |
| 67 | + |
| 68 | +#[tauri::command] |
| 69 | +pub fn artifact_get_preview( |
| 70 | + state: State<'_, AppState>, |
| 71 | + session_id: String, |
| 72 | + artifact_id: String, |
| 73 | +) -> Result<ArtifactPreview, String> { |
| 74 | + let conn = state.db.lock().map_err(|error| error.to_string())?; |
| 75 | + service()? |
| 76 | + .preview(&conn, &session_id, &artifact_id) |
| 77 | + .map_err(|error| error.to_string()) |
| 78 | +} |
| 79 | + |
| 80 | +#[tauri::command] |
| 81 | +pub fn artifact_read_preview_base64( |
| 82 | + state: State<'_, AppState>, |
| 83 | + session_id: String, |
| 84 | + artifact_id: String, |
| 85 | +) -> Result<String, String> { |
| 86 | + let conn = state.db.lock().map_err(|error| error.to_string())?; |
| 87 | + service()? |
| 88 | + .read_preview_base64(&conn, &session_id, &artifact_id) |
| 89 | + .map_err(|error| error.to_string()) |
| 90 | +} |
| 91 | + |
| 92 | +#[tauri::command] |
| 93 | +pub async fn artifact_export( |
| 94 | + app: AppHandle, |
| 95 | + state: State<'_, AppState>, |
| 96 | + session_id: String, |
| 97 | + artifact_id: String, |
| 98 | +) -> Result<ExportOutcome, String> { |
| 99 | + let metadata = { |
| 100 | + let conn = state.db.lock().map_err(|error| error.to_string())?; |
| 101 | + service()? |
| 102 | + .metadata(&conn, &session_id, &artifact_id) |
| 103 | + .map_err(|error| error.to_string())? |
| 104 | + }; |
| 105 | + let app_for_dialog = app.clone(); |
| 106 | + let target = tauri::async_runtime::spawn_blocking(move || { |
| 107 | + app_for_dialog |
| 108 | + .dialog() |
| 109 | + .file() |
| 110 | + .set_title("Save artifact") |
| 111 | + .set_file_name(metadata.display_name) |
| 112 | + .blocking_save_file() |
| 113 | + .and_then(|path| path.into_path().ok()) |
| 114 | + }) |
| 115 | + .await |
| 116 | + .map_err(|error| error.to_string())?; |
| 117 | + let Some(target) = target else { |
| 118 | + return Ok(ExportOutcome { |
| 119 | + status: "cancelled".to_string(), |
| 120 | + file_name: None, |
| 121 | + }); |
| 122 | + }; |
| 123 | + let conn = state.db.lock().map_err(|error| error.to_string())?; |
| 124 | + service()? |
| 125 | + .export_to_path(&conn, &session_id, &artifact_id, &target) |
| 126 | + .map_err(|error| error.to_string()) |
| 127 | +} |
| 128 | + |
| 129 | +#[tauri::command] |
| 130 | +pub fn artifact_delete_or_expire( |
| 131 | + state: State<'_, AppState>, |
| 132 | + session_id: String, |
| 133 | + artifact_id: String, |
| 134 | +) -> Result<(), String> { |
| 135 | + let conn = state.db.lock().map_err(|error| error.to_string())?; |
| 136 | + service()? |
| 137 | + .expire(&conn, &session_id, &artifact_id) |
| 138 | + .map_err(|error| error.to_string()) |
| 139 | +} |
| 140 | + |
| 141 | +#[tauri::command] |
| 142 | +pub fn append_content_block(state: State<'_, AppState>, block: ContentBlock) -> Result<(), String> { |
| 143 | + if !state.feature_flags.rich_content_write { |
| 144 | + return Err("CONTENT_BLOCK_UNSUPPORTED".to_string()); |
| 145 | + } |
| 146 | + let conn = state.db.lock().map_err(|error| error.to_string())?; |
| 147 | + MessageBlockService::append(&conn, &block, service()?.policy()) |
| 148 | + .map_err(|error| error.to_string()) |
| 149 | +} |
| 150 | + |
| 151 | +#[tauri::command] |
| 152 | +pub fn get_message_blocks( |
| 153 | + state: State<'_, AppState>, |
| 154 | + message_id: String, |
| 155 | +) -> Result<Vec<ContentBlock>, String> { |
| 156 | + if !state.feature_flags.rich_content_render { |
| 157 | + return Ok(Vec::new()); |
| 158 | + } |
| 159 | + let conn = state.db.lock().map_err(|error| error.to_string())?; |
| 160 | + MessageBlockRepo::find_by_message(&conn, &message_id).map_err(|error| error.to_string()) |
| 161 | +} |
0 commit comments