From 7eddabd79eb15ca1b2404181fcb745b6294d9c49 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Thu, 29 May 2025 05:16:24 -0400 Subject: [PATCH] fal alerts for logged out --- .../core/commands/enqueue/image/handle_fal.rs | 16 ++++++++++++++++ .../generation_enqueue_failure_event.rs | 11 +++++++++++ .../commands/fal_background_removal_command.rs | 7 +++++++ 3 files changed, 34 insertions(+) diff --git a/crates/desktop/artcraft/src/core/commands/enqueue/image/handle_fal.rs b/crates/desktop/artcraft/src/core/commands/enqueue/image/handle_fal.rs index 77f23c4cd4..97ab7ed971 100644 --- a/crates/desktop/artcraft/src/core/commands/enqueue/image/handle_fal.rs +++ b/crates/desktop/artcraft/src/core/commands/enqueue/image/handle_fal.rs @@ -11,6 +11,7 @@ use crate::services::sora::state::sora_credential_manager::SoraCredentialManager use crate::services::sora::state::sora_task_queue::SoraTaskQueue; use crate::services::storyteller::state::storyteller_credential_manager::StorytellerCredentialManager; use anyhow::anyhow; +use fal_client::creds::fal_api_key::FalApiKey; use fal_client::requests::image_gen::enqueue_flux_pro_ultra_text_to_image::{enqueue_flux_pro_ultra_text_to_image, FluxProUltraTextToImageArgs}; use fal_client::requests::image_gen::enqueue_recraft3_text_to_image::{enqueue_recraft3_text_to_image, Recraft3TextToImageArgs}; use log::{error, info, warn}; @@ -23,6 +24,21 @@ pub async fn handle_fal( fal_task_queue: &FalTaskQueue, ) -> Result<(), InternalImageError> { + let api_key = match fal_creds_manager.get_key()? { + Some(key) => key, + None => { + error!("No FAL API key is set. Can't perform action."); + let event = + GenerationEnqueueFailureEvent::no_fal_api_key(GenerationAction::GenerateImage); + + if let Err(err) = event.send(&app) { + error!("Failed to emit event: {:?}", err); // Fail open. + } + + return Err(InternalImageError::NeedsFalApiKey); + }, + }; + let api_key = fal_creds_manager.get_key_required() .map_err(|err| { error!("EnqueueTextToImage FAL api key required: {:?}", err); diff --git a/crates/desktop/artcraft/src/core/events/generation_events/generation_enqueue_failure_event.rs b/crates/desktop/artcraft/src/core/events/generation_events/generation_enqueue_failure_event.rs index d36750758a..0de37113f5 100644 --- a/crates/desktop/artcraft/src/core/events/generation_events/generation_enqueue_failure_event.rs +++ b/crates/desktop/artcraft/src/core/events/generation_events/generation_enqueue_failure_event.rs @@ -17,3 +17,14 @@ impl BasicSendableEvent for GenerationEnqueueFailureEvent { const FRONTEND_EVENT_NAME: &'static str = "generation-enqueue-failure-event"; const EVENT_STATUS: BasicEventStatus = BasicEventStatus::Failure; } + +impl GenerationEnqueueFailureEvent { + pub fn no_fal_api_key(action: GenerationAction) -> Self { + Self { + action, + service: GenerationServiceProvider::Fal, + model: None, + reason: Some("No FAL API key is set. Configure this in your settings.".to_string()), + } + } +} diff --git a/crates/desktop/artcraft/src/services/fal/commands/fal_background_removal_command.rs b/crates/desktop/artcraft/src/services/fal/commands/fal_background_removal_command.rs index 5ad2754f8b..9ff3c7defd 100644 --- a/crates/desktop/artcraft/src/services/fal/commands/fal_background_removal_command.rs +++ b/crates/desktop/artcraft/src/services/fal/commands/fal_background_removal_command.rs @@ -104,6 +104,13 @@ pub async fn fal_background_removal_command( if !has_credentials { warn!("No API key found"); + let event = + GenerationEnqueueFailureEvent::no_fal_api_key(GenerationAction::RemoveBackground); + + if let Err(err) = event.send(&app) { + error!("Failed to emit event: {:?}", err); // Fail open. + } + return Err(CommandErrorResponseWrapper { status: CommandErrorStatus::Unauthorized, error_message: Some("You need to set a FAL api key".to_string()),