Remove AI image generation functionality from CMS and expand Docker ignore rules.

This commit is contained in:
2026-03-09 10:06:26 -03:00
parent bf3f06d831
commit 7406de9a1b
11 changed files with 146 additions and 778 deletions
+48 -397
View File
@@ -1309,381 +1309,6 @@ pub async fn generate_quiz(
Ok(Json(quiz_blocks))
}
#[derive(Deserialize)]
pub struct VideoAIRequest {
pub prompt: Option<String>,
pub width: Option<u32>,
pub height: Option<u32>,
}
pub async fn generate_image(
Org(org_ctx): Org,
claims: common::auth::Claims,
State(pool): State<PgPool>,
Path(id): Path<Uuid>,
Json(payload): Json<VideoAIRequest>,
) -> Result<Json<Lesson>, StatusCode> {
if let Some(prompt) = &payload.prompt {
tracing::info!("Received prompt for video generation: {}", prompt);
}
// 1. Fetch lesson
let _lesson =
sqlx::query_as::<_, Lesson>("SELECT * FROM lessons WHERE id = $1 AND organization_id = $2")
.bind(id)
.bind(org_ctx.id)
.fetch_one(&pool)
.await
.map_err(|_| StatusCode::NOT_FOUND)?;
// 2. Set status to queued
let updated_lesson = sqlx::query_as::<_, Lesson>(
"UPDATE lessons SET video_generation_status = 'queued', video_generation_error = NULL WHERE id = $1 RETURNING *",
)
.bind(id)
.fetch_one(&pool)
.await
.map_err(|e| {
tracing::error!("Database update failed (video queued): {}", e);
StatusCode::INTERNAL_SERVER_ERROR
})?;
log_action(
&pool,
org_ctx.id,
claims.sub,
"VIDEO_GENERATION_QUEUED",
"Lesson",
id,
json!({ "status": "queued" }),
)
.await;
// 3. Spawn background task
let pool_clone = pool.clone();
let prompt_to_task = payload.prompt.clone();
let user_id = claims.sub;
let width = payload.width;
let height = payload.height;
tokio::spawn(async move {
if let Err(e) = run_image_generation_task(pool_clone, id, prompt_to_task, Some(user_id), false, width, height).await {
tracing::error!("Image generation task failed for lesson {}: {}", id, e);
}
});
Ok(Json(updated_lesson))
}
pub async fn generate_course_image(
Org(org_ctx): Org,
claims: common::auth::Claims,
State(pool): State<PgPool>,
Path(id): Path<Uuid>,
Json(payload): Json<VideoAIRequest>,
) -> Result<Json<Course>, StatusCode> {
// 1. Fetch course
let _course =
sqlx::query_as::<_, Course>("SELECT * FROM courses WHERE id = $1 AND organization_id = $2")
.bind(id)
.bind(org_ctx.id)
.fetch_one(&pool)
.await
.map_err(|_| StatusCode::NOT_FOUND)?;
// 2. Set status to queued
let updated_course = sqlx::query_as::<_, Course>(
"UPDATE courses SET generation_status = 'queued', generation_error = NULL WHERE id = $1 RETURNING *",
)
.bind(id)
.fetch_one(&pool)
.await
.map_err(|e| {
tracing::error!("Database update failed (course image queued): {}", e);
StatusCode::INTERNAL_SERVER_ERROR
})?;
log_action(
&pool,
org_ctx.id,
claims.sub,
"COURSE_IMAGE_GENERATION_QUEUED",
"Course",
id,
json!({ "status": "queued" }),
)
.await;
// 3. Spawn background task
let pool_clone = pool.clone();
let prompt_to_task = payload.prompt.clone();
let user_id = claims.sub;
let width = payload.width;
let height = payload.height;
tokio::spawn(async move {
if let Err(e) = run_image_generation_task(pool_clone, id, prompt_to_task, Some(user_id), true, width, height).await {
tracing::error!("Image generation task failed for course {}: {}", id, e);
}
});
Ok(Json(updated_course))
}
pub async fn run_image_generation_task(
pool: PgPool,
id: Uuid,
custom_prompt: Option<String>,
user_id: Option<Uuid>,
is_course: bool,
width: Option<u32>,
height: Option<u32>
) -> Result<(), String> {
let (status_col, progress_col, error_col, table_name) = if is_course {
("generation_status", "generation_progress", "generation_error", "courses")
} else {
("video_generation_status", "generation_progress", "video_generation_error", "lessons")
};
// 1. Set status to processing ONLY if it's still queued (not cancelled/idle)
let query = format!(
"UPDATE {} SET {} = 'processing' WHERE id = $1 AND {} = 'queued'",
table_name, status_col, status_col
);
let rows_affected = sqlx::query(&query)
.bind(id)
.execute(&pool)
.await
.map_err(|e| format!("Update to processing failed: {}", e))?
.rows_affected();
if rows_affected == 0 {
tracing::info!("Task {} was cancelled or is already processing. Aborting background thread.", id);
return Ok(());
}
// 2. Call Local Video Bridge (Python) with Retry Logic
let client = reqwest::Client::new();
let bridge_base_url = std::env::var("LOCAL_VIDEO_BRIDGE_URL")
.unwrap_or_else(|_| "http://t-800:8080".to_string());
let bridge_url = format!("{}/generate", bridge_base_url);
// Fallback logic for prompt: Custom Prompt > Title
let final_prompt = match custom_prompt {
Some(p) if !p.is_empty() => p,
_ => {
let title: String = sqlx::query_scalar(&format!("SELECT title FROM {} WHERE id = $1", table_name))
.bind(id)
.fetch_one(&pool)
.await
.map_err(|e| format!("Failed to fetch fallback prompt: {}", e))?;
title
}
};
let database_url = std::env::var("BRIDGE_DATABASE_URL")
.unwrap_or_else(|_| std::env::var("DATABASE_URL").unwrap_or_default());
let mut payload = serde_json::json!({
"prompt": final_prompt,
"lesson_id": id.to_string(),
"database_url": database_url,
"table_name": table_name,
"progress_column": progress_col,
});
if let Some(w) = width {
payload["width"] = serde_json::json!(w);
}
if let Some(h) = height {
payload["height"] = serde_json::json!(h);
}
let mut retry_count = 0;
let max_retries = 3; // Initial quick retries
let long_retry_delay = tokio::time::Duration::from_secs(600); // 10 minutes
loop {
let response_result = client.post(&bridge_url)
.json(&payload)
.send()
.await;
match response_result {
Ok(response) => {
if response.status().is_success() {
let result: serde_json::Value = response.json().await
.map_err(|e| format!("Failed to parse video bridge response: {}", e))?;
let bridge_content_url = result["url"].as_str()
.ok_or_else(|| "Video bridge response missing URL".to_string())?;
// Break the loop and proceed to download
return process_image_download(&client, bridge_content_url, &pool, id, is_course, user_id, &final_prompt).await;
} else {
let err_text = response.text().await.unwrap_or_default();
let _ = sqlx::query(&format!("UPDATE {} SET {} = $1, {} = 'error' WHERE id = $2", table_name, error_col, status_col))
.bind(&err_text)
.bind(id)
.execute(&pool)
.await;
return Err(format!("Video bridge error: {}", err_text));
}
}
Err(e) => {
tracing::warn!("Failed to reach AI bridge at {}: {}. Retry {}/{}", bridge_url, e, retry_count + 1, max_retries);
// Update DB to show we are waiting/retrying
let wait_msg = format!("El servidor de IA (t-800) no responde. Reintentando automáticamente en 10 min... (Error: {})", e);
let _ = sqlx::query(&format!("UPDATE {} SET {} = $1, {} = 'queued' WHERE id = $2", table_name, error_col, status_col))
.bind(&wait_msg)
.bind(id)
.execute(&pool)
.await;
// Check if task was cancelled while we were about to sleep
let current_status: String = sqlx::query_scalar(&format!("SELECT {} FROM {} WHERE id = $1", status_col, table_name))
.bind(id)
.fetch_one(&pool)
.await
.unwrap_or_else(|_| "error".to_string());
if current_status == "idle" || current_status == "error" {
tracing::info!("Task {} was cancelled or errored during retry. Aborting.", id);
return Ok(());
}
retry_count += 1;
// Wait 10 minutes before next attempt
tokio::time::sleep(long_retry_delay).await;
// After sleep, check status AGAIN to see if user cancelled during the 10min sleep
let current_status: String = sqlx::query_scalar(&format!("SELECT {} FROM {} WHERE id = $1", status_col, table_name))
.bind(id)
.fetch_one(&pool)
.await
.unwrap_or_else(|_| "error".to_string());
if current_status != "queued" {
tracing::info!("Task {} status changed to {} during sleep. Aborting retry.", id, current_status);
return Ok(());
}
// Set back to processing to "lock" it again for this attempt
let rows_affected = sqlx::query(&format!("UPDATE {} SET {} = 'processing' WHERE id = $1 AND {} = 'queued'", table_name, status_col, status_col))
.bind(id)
.execute(&pool)
.await
.map_err(|e| e.to_string())?
.rows_affected();
if rows_affected == 0 {
return Ok(());
}
}
}
}
}
async fn process_image_download(
client: &reqwest::Client,
bridge_content_url: &str,
pool: &PgPool,
id: Uuid,
is_course: bool,
user_id: Option<Uuid>,
final_prompt: &str
) -> Result<(), String> {
// --- Download image and store as Asset ---
// 1. Download from bridge
let image_bytes = client.get(bridge_content_url)
.send()
.await
.map_err(|e| format!("Failed to download generated image: {}", e))?
.bytes()
.await
.map_err(|e| format!("Failed to read image bytes: {}", e))?;
// 2. Fetch context
let (org_id, course_id, instructor_id): (Uuid, Uuid, Uuid) = if is_course {
let c = sqlx::query_as::<sqlx::Postgres, (Uuid, Uuid, Uuid)>(
"SELECT organization_id, id as course_id, instructor_id FROM courses WHERE id = $1"
)
.bind(id)
.fetch_one(pool)
.await
.map_err(|e| format!("Failed to fetch course context: {}", e))?;
c
} else {
sqlx::query_as::<sqlx::Postgres, (Uuid, Uuid, Uuid)>(
"SELECT l.organization_id, m.course_id, c.instructor_id
FROM lessons l
JOIN modules m ON l.module_id = m.id
JOIN courses c ON m.course_id = c.id
WHERE l.id = $1"
)
.bind(id)
.fetch_one(pool)
.await
.map_err(|e| format!("Failed to fetch lesson context: {}", e))?
};
let final_user_id = user_id.unwrap_or(instructor_id);
// 3. Save file locally
let asset_id = Uuid::new_v4();
let storage_filename = format!("{}.png", asset_id);
let storage_path = format!("uploads/{}", storage_filename);
let _ = tokio::fs::create_dir_all("uploads").await;
tokio::fs::write(&storage_path, &image_bytes).await.map_err(|e| e.to_string())?;
let size_bytes = image_bytes.len() as i64;
let local_url = format!("/assets/{}", storage_filename);
// 4. Register in assets table
sqlx::query(
r#"
INSERT INTO assets (id, organization_id, uploaded_by, course_id, filename, storage_path, mimetype, size_bytes)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
"#,
)
.bind(asset_id)
.bind(org_id)
.bind(final_user_id)
.bind(course_id)
.bind(format!("AI: {}", final_prompt))
.bind(&storage_path)
.bind("image/png")
.bind(size_bytes)
.execute(pool)
.await
.map_err(|e| format!("Failed to register asset: {}", e))?;
// 5. Complete task updating entity with local URL - ONLY if not cancelled (idle)
if is_course {
sqlx::query(&format!(
"UPDATE courses SET generation_status = 'completed', course_image_url = $1 WHERE id = $2 AND generation_status = 'processing'"
))
.bind(local_url)
.bind(id)
.execute(pool)
.await
.map_err(|e| format!("Failed to complete course task: {}", e))?;
} else {
sqlx::query(&format!(
"UPDATE lessons SET video_generation_status = 'completed', content_url = $1, content_type = 'image' WHERE id = $2 AND video_generation_status = 'processing'"
))
.bind(local_url)
.bind(id)
.execute(pool)
.await
.map_err(|e| format!("Failed to complete lesson task: {}", e))?;
}
Ok(())
}
pub async fn get_lesson(
Org(org_ctx): Org,
State(pool): State<PgPool>,
@@ -3921,29 +3546,55 @@ RULES:
request = request.header("Authorization", auth_header);
}
let response = request.send().await.map_err(|e| {
tracing::error!("LLM request failed: {}", e);
StatusCode::INTERNAL_SERVER_ERROR
})?;
let response_result = request.send().await;
if !response.status().is_success() {
let err_body = response.text().await.unwrap_or_default();
tracing::error!("LLM API error: {}", err_body);
return Err(StatusCode::INTERNAL_SERVER_ERROR);
}
let llm_data: serde_json::Value = response.json().await.map_err(|e| {
tracing::error!("Failed to parse LLM JSON response: {}", e);
StatusCode::INTERNAL_SERVER_ERROR
})?;
tracing::info!("LLM Response received successfully");
let mut content_str = llm_data["choices"][0]["message"]["content"]
.as_str()
.unwrap_or("{}")
.trim()
.to_string();
let mut content_str = match response_result {
Ok(response) if response.status().is_success() => {
let llm_data: serde_json::Value = response.json().await.map_err(|e| {
tracing::error!("Failed to parse LLM JSON response: {}", e);
StatusCode::INTERNAL_SERVER_ERROR
})?;
tracing::info!("LLM Response received successfully");
llm_data["choices"][0]["message"]["content"]
.as_str()
.unwrap_or("{}")
.trim()
.to_string()
}
Ok(response) => {
let err_body = response.text().await.unwrap_or_default();
tracing::error!("LLM API error (generating course): {}", err_body);
tracing::warn!("Falling back to default course template.");
r#"{
"title": "Nueva Plantilla de Curso",
"description": "El servidor de IA no respondió. Esta es una plantilla básica que puedes editar directamente.",
"modules": [
{
"title": "Módulo 1",
"lessons": [
{ "title": "Lección 1", "content_type": "text" }
]
}
]
}"#.to_string()
}
Err(e) => {
tracing::error!("LLM request failed (generating course): {}", e);
tracing::warn!("Falling back to default course template due to unreachable AI.");
r#"{
"title": "Nueva Plantilla de Curso",
"description": "El servidor de IA no está disponible en este momento. Esta es una plantilla básica.",
"modules": [
{
"title": "Módulo 1",
"lessons": [
{ "title": "Lección 1", "content_type": "text" }
]
}
]
}"#.to_string()
}
};
tracing::info!("Extracted content string (length: {})", content_str.len());