mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-05 01:38:44 +00:00
fix bugs, credentials work
This commit is contained in:
1 parent
5deb9ef11a
commit
055cc80aef
6 files changed
+21
-70
No files matched your search
@@ -99,7 +99,9 @@ pub async fn generate_image(
|
||||
|
||||
let mut sora_media_tokens = Vec::with_capacity(files_to_upload.len());
|
||||
|
||||
for file_path in files_to_upload {
|
||||
for (i, file_path) in files_to_upload.iter().enumerate() {
|
||||
info!("Uploading image {} of {}...", (i+1), files_to_upload.len());
|
||||
|
||||
// TODO(bt,2025-04-24): Handle JWT reset error.
|
||||
let sora_upload_response = sora_media_upload_from_file(file_path, CredentialMigrationRef::New(&creds))
|
||||
.await?;
|
||||
@@ -107,6 +109,8 @@ pub async fn generate_image(
|
||||
sora_media_tokens.push(sora_upload_response.id);
|
||||
}
|
||||
|
||||
info!("Calling image generation...");
|
||||
|
||||
// TODO(bt,2025-04-21): Download media tokens.
|
||||
// Note: This is incredibly inefficient. We should keep a local cache.
|
||||
// Also, if they've already been uploaded to OpenAI, we shouldn't continue to re-upload.
|
||||
|
||||
@@ -16,7 +16,7 @@ use tauri_plugin_log::Target;
|
||||
use tauri_plugin_log::TargetKind;
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub async fn run() {
|
||||
pub fn run() {
|
||||
// NB: Tauri wants to install the logger itself, so we can't rely on the logger crate
|
||||
// until the tauri runtime begins.
|
||||
println!("Loading model config...");
|
||||
|
||||
@@ -1,50 +1,26 @@
|
||||
use anyhow::anyhow;
|
||||
use errors::AnyhowResult;
|
||||
use openai_sora_client::credentials::SoraCredentials;
|
||||
use openai_sora_client::creds::sora_credential_set::SoraCredentialSet;
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
// TODO: This is just so we can phase in the new credentials
|
||||
pub struct Inner {
|
||||
credentials: Option<SoraCredentials>,
|
||||
credential_set: Option<SoraCredentialSet>,
|
||||
}
|
||||
|
||||
/// Hold credentials for the application.
|
||||
#[derive(Clone)]
|
||||
pub struct SoraCredentialHolder {
|
||||
credentials: Arc<RwLock<Inner>>,
|
||||
credentials: Arc<RwLock<Option<SoraCredentialSet>>>,
|
||||
}
|
||||
|
||||
impl SoraCredentialHolder {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
credentials: Arc::new(RwLock::new(Inner {
|
||||
credentials: None,
|
||||
credential_set: None,
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_legacy_credentials(&self, credentials: &SoraCredentials) -> AnyhowResult<()> {
|
||||
let credential_set = SoraCredentialSet::from_legacy_credentials(credentials)?;
|
||||
match self.credentials.write() {
|
||||
Err(err) => Err(anyhow!("Failed to acquire write lock: {:?}", err)),
|
||||
Ok(mut creds) => {
|
||||
creds.credentials = Some(credentials.clone());
|
||||
creds.credential_set = Some(credential_set);
|
||||
Ok(())
|
||||
}
|
||||
credentials: Arc::new(RwLock::new(None)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_credentials(&self, credentials: &SoraCredentialSet) -> AnyhowResult<()> {
|
||||
let credentials_legacy = credentials.to_legacy_credentials()?;
|
||||
match self.credentials.write() {
|
||||
Err(err) => Err(anyhow!("Failed to acquire write lock: {:?}", err)),
|
||||
Ok(mut creds) => {
|
||||
creds.credentials = Some(credentials_legacy);
|
||||
creds.credential_set = Some(credentials.clone());
|
||||
*creds = Some(credentials.clone());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -54,37 +30,17 @@ impl SoraCredentialHolder {
|
||||
match self.credentials.write() {
|
||||
Err(err) => Err(anyhow!("Failed to acquire write lock: {:?}", err)),
|
||||
Ok(mut creds) => {
|
||||
creds.credentials = None;
|
||||
creds.credential_set = None;
|
||||
*creds = None;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_legacy_credentials(&self) -> AnyhowResult<Option<SoraCredentials>> {
|
||||
match self.credentials.read() {
|
||||
Err(err) => Err(anyhow!("Failed to acquire read lock: {:?}", err)),
|
||||
Ok(creds) => {
|
||||
Ok(creds.credentials.clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_legacy_credentials_required(&self) -> AnyhowResult<SoraCredentials> {
|
||||
match self.credentials.read() {
|
||||
Err(err) => Err(anyhow!("Failed to acquire read lock: {:?}", err)),
|
||||
Ok(creds) => match &creds.credentials {
|
||||
None => Err(anyhow!("Credentials not set")),
|
||||
Some(creds) => Ok(creds.clone()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_credentials(&self) -> AnyhowResult<Option<SoraCredentialSet>> {
|
||||
match self.credentials.read() {
|
||||
Err(err) => Err(anyhow!("Failed to acquire read lock: {:?}", err)),
|
||||
Ok(creds) => {
|
||||
Ok(creds.credential_set.clone())
|
||||
Ok(creds.clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -92,7 +48,7 @@ impl SoraCredentialHolder {
|
||||
pub fn get_credentials_required(&self) -> AnyhowResult<SoraCredentialSet> {
|
||||
match self.credentials.read() {
|
||||
Err(err) => Err(anyhow!("Failed to acquire read lock: {:?}", err)),
|
||||
Ok(creds) => match &creds.credential_set {
|
||||
Ok(creds) => match &*creds {
|
||||
None => Err(anyhow!("Credentials not set")),
|
||||
Some(creds) => Ok(creds.clone()),
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ use crate::state::sora::sora_credential_holder::SoraCredentialHolder;
|
||||
use anyhow::anyhow;
|
||||
use errors::AnyhowResult;
|
||||
use log::{error, info, warn};
|
||||
use openai_sora_client::credentials::SoraCredentials;
|
||||
use openai_sora_client::creds::sora_credential_set::SoraCredentialSet;
|
||||
use openai_sora_client::creds::sora_sentinel::SoraSentinel;
|
||||
use openai_sora_client::requests::sentinel_refresh::generate::token::generate_token;
|
||||
@@ -35,10 +34,6 @@ impl SoraCredentialManager {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_legacy_credentials(&self, creds: &SoraCredentials) -> AnyhowResult<()> {
|
||||
self.holder.set_legacy_credentials(creds)
|
||||
}
|
||||
|
||||
pub fn set_credentials(&self, creds: &SoraCredentialSet) -> AnyhowResult<()> {
|
||||
self.holder.set_credentials(creds)
|
||||
}
|
||||
@@ -48,14 +43,6 @@ impl SoraCredentialManager {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_legacy_credentials(&self) -> AnyhowResult<Option<SoraCredentials>> {
|
||||
self.holder.get_legacy_credentials()
|
||||
}
|
||||
|
||||
pub fn get_legacy_credentials_required(&self) -> AnyhowResult<SoraCredentials> {
|
||||
self.holder.get_legacy_credentials_required()
|
||||
}
|
||||
|
||||
pub fn get_credentials(&self) -> AnyhowResult<Option<SoraCredentialSet>> {
|
||||
self.holder.get_credentials()
|
||||
}
|
||||
|
||||
@@ -113,8 +113,10 @@ fn extract_cookies_to_file(
|
||||
|
||||
let mut should_write_cookies = maybe_old_cookies.is_none();
|
||||
|
||||
if let Some(old_cookies) = maybe_old_cookies {
|
||||
should_write_cookies = old_cookies != new_cookies;
|
||||
if new_cookies.len() > 50 {
|
||||
if let Some(old_cookies) = maybe_old_cookies {
|
||||
should_write_cookies = old_cookies != new_cookies;
|
||||
}
|
||||
}
|
||||
|
||||
if !should_write_cookies {
|
||||
@@ -125,9 +127,9 @@ fn extract_cookies_to_file(
|
||||
|
||||
sora_credential_manager.clear_credentials()?;
|
||||
|
||||
fs::remove_file(app_data_root.get_sora_cookie_file_path())?;
|
||||
fs::remove_file(app_data_root.get_sora_bearer_token_file_path())?;
|
||||
fs::remove_file(app_data_root.get_sora_sentinel_file_path())?;
|
||||
let _r = fs::remove_file(app_data_root.get_sora_cookie_file_path());
|
||||
let _r = fs::remove_file(app_data_root.get_sora_bearer_token_file_path());
|
||||
let _r = fs::remove_file(app_data_root.get_sora_sentinel_file_path());
|
||||
|
||||
let cookie_file = app_data_root.get_sora_cookie_file_path();
|
||||
|
||||
|
||||
+2
@@ -1,3 +1,4 @@
|
||||
use log::info;
|
||||
use crate::credentials::{SoraCredentials, USER_AGENT};
|
||||
use crate::creds::credential_migration::CredentialMigrationRef;
|
||||
use crate::sora_error::SoraError;
|
||||
@@ -105,6 +106,7 @@ pub (crate) async fn upload_media_http_request(
|
||||
|
||||
// Check response status
|
||||
if !response.status().is_success() {
|
||||
info!("Error uploading image: {:?}", response.status());
|
||||
let error = classify_error(response).await;
|
||||
return Err(error);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user