From 17d74ebafe1ae1a11c7e5ff613bac94bd81f17a5 Mon Sep 17 00:00:00 2001 From: Cameron Cordes Date: Fri, 30 Oct 2026 15:27:28 -0400 Subject: [PATCH] feat(enhance): config, templates, ComfyUI client, jobs, endpoints (#192) Co-Authored-By: Claude Opus 5.5 --- enhance.example.toml | 65 ++ .../down.sql | 1 + .../2026-10-30-100000_add_enhance_jobs/up.sql | 31 + src/database/enhance_dao.rs | 336 ++++++++ src/database/mod.rs | 2 + src/database/schema.rs | 23 + src/enhance/comfy.rs | 423 ++++++++++ src/enhance/config.rs | 263 +++++++ src/enhance/handlers.rs | 515 ++++++++++++ src/enhance/integration_tests.rs | 0 src/enhance/mod.rs | 25 + src/enhance/path_map.rs | 174 +++++ src/enhance/service.rs | 733 ++++++++++++++++++ src/enhance/template.rs | 490 ++++++++++++ src/lib.rs | 1 + src/main.rs | 23 + tests/fixtures/enhance/edit_turbo.json | 283 +++++++ tests/fixtures/enhance/restore_dev.json | 262 +++++++ tests/fixtures/enhance/restore_turbo.json | 262 +++++++ 19 files changed, 3912 insertions(+) create mode 100644 enhance.example.toml create mode 100644 migrations/2026-10-30-100000_add_enhance_jobs/down.sql create mode 100644 migrations/2026-10-30-100000_add_enhance_jobs/up.sql create mode 100644 src/database/enhance_dao.rs create mode 100644 src/enhance/comfy.rs create mode 100644 src/enhance/config.rs create mode 100644 src/enhance/handlers.rs create mode 100644 src/enhance/integration_tests.rs create mode 100644 src/enhance/mod.rs create mode 100644 src/enhance/path_map.rs create mode 100644 src/enhance/service.rs create mode 100644 src/enhance/template.rs create mode 100755 tests/fixtures/enhance/edit_turbo.json create mode 100755 tests/fixtures/enhance/restore_dev.json create mode 100755 tests/fixtures/enhance/restore_turbo.json diff --git a/enhance.example.toml b/enhance.example.toml new file mode 100644 index 0000000..9cc80a9 --- /dev/null +++ b/enhance.example.toml @@ -0,0 +1,65 @@ +# ImageApi Enhance configuration (#192). Copy to `enhance.toml` next to +# `.env` (or point ENHANCE_CONFIG_PATH at it). Every key is optional: delete +# a line to get its default (trailing `default:` comment). Without `url`, +# Enhance is off and GET /enhance/templates reports `available: false`. +# +# ComfyUI has no auth and stays LAN-only; the apps only ever talk to +# ImageApi, which relays previews. Nothing in this file is a secret. +# +# Templates are API-format workflows (ComfyUI -> Workflow -> Export (API)). +# ImageApi finds the nodes it fills in by `_meta.title`, so re-exporting +# after editing the graph keeps working: +# imageapi:source required Load Image From Path; gets the photo's path +# imageapi:save required Save Image Version; forced to require_approval +# imageapi:prompt optional text node; its `text`, `prompt` or `value` +# input (first one present) gets the user's text +# imageapi:reference optional second loader for a context photo. With no +# reference in the request the node and every +# link to it are removed from the prompt. +# A template missing a required title (or with a prompt node that has none of +# the three inputs) is rejected at startup with a log line; the rest still load. +# Never edit the template files from here: ImageApi only reads them. + +url = "http://192.168.10.218:8188" # default: unset (Enhance disabled) +timeout_secs = 15 # default: 15; per ComfyUI request +probe_ttl_secs = 10 # default: 10; how long `available` is cached +poll_secs = 2 # default: 2; job poll interval +idle_free_secs = 300 # default: 300; ComfyUI idle (empty queue, newest + # history entry this old) -> POST /free once. 0 = off +unreachable_grace_secs = 120 # default: 120; active job -> `unavailable` after this +default_template = "restore" # default: the first template +warn_above_megapixels = 1.5 # default: 1.5; clients warn above this original size + +# Library path prefix <-> ComfyUI path prefix; longest prefix wins and +# separators are converted, in both directions. With no entries, paths pass +# through unchanged. A mapped drive (Z:\) only exists in the logged-in +# session; use a UNC prefix (\\nas\) if ComfyUI ever runs as a service. +[[path_map]] +from = "/media/nas/" +to = 'Z:\' + +[[template]] +name = "restore" # stable id clients send +label = "Restore" +description = "Sharpen and clean up the photo (about 3 s)." +file = "/media/nas/image_fibo2_restore_turbo_imageapi_API.json" # relative = beside this file +# append: the template's prompt is a full base instruction; the user's text +# is appended after prompt_join. Empty text leaves the prompt as is. +prompt_mode = "append" # default: "append" | "replace" +prompt_join = " " # default: " " (single space); e.g. "\n\nAdditional instructions: " +output_megapixels = 1.0 # default: 1.0; ~1 MP output, for the client's size warning + +[[template]] +name = "edit" +label = "Edit" +description = "Change the photo as you describe." +file = "/media/nas/image_fibo2_edit_turbo_imageapi_API.json" +# replace: the user's text IS the prompt, so it is required (empty = 400). +prompt_mode = "replace" + +# The Dev variant (50 steps, ~50 s) is no better on the archive test set, so +# it is not the default; configure it if you want it: +# [[template]] +# name = "restore-dev" +# label = "Restore (Dev, slow)" +# file = "/media/nas/image_fibo2_restore_imageapi_API.json" diff --git a/migrations/2026-10-30-100000_add_enhance_jobs/down.sql b/migrations/2026-10-30-100000_add_enhance_jobs/down.sql new file mode 100644 index 0000000..d432e23 --- /dev/null +++ b/migrations/2026-10-30-100000_add_enhance_jobs/down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS enhance_jobs; diff --git a/migrations/2026-10-30-100000_add_enhance_jobs/up.sql b/migrations/2026-10-30-100000_add_enhance_jobs/up.sql new file mode 100644 index 0000000..5485e2d --- /dev/null +++ b/migrations/2026-10-30-100000_add_enhance_jobs/up.sql @@ -0,0 +1,31 @@ +-- Enhance on demand (#192): one row per ComfyUI run submitted for a photo. +-- +-- Keyed to the photo by `original_hash` (the blake3 content_hash of the file +-- that was submitted), like every other derived table, so a job survives a +-- rename or move; `library_id` + `rel_path` say which file it was at submit +-- time (and are what the API echoes back as `path`). +CREATE TABLE enhance_jobs ( + id TEXT PRIMARY KEY NOT NULL, -- uuid, ImageApi's own + original_hash TEXT NOT NULL, + library_id INTEGER NOT NULL, + rel_path TEXT NOT NULL, + template TEXT NOT NULL, + prompt TEXT, + reference_hash TEXT, + reference_path TEXT, -- library-relative, same library + comfy_prompt_id TEXT NOT NULL, + -- queued | running | ready | approved | failed | expired | unavailable + status TEXT NOT NULL, + error TEXT, + queue_position INTEGER, + -- json: [{id, view: {filename, subfolder, type}}], ComfyUI's staged results + pending TEXT NOT NULL DEFAULT '[]', + -- json: [{n, comfy_path, library_id, rel_path}], approved results + saved TEXT NOT NULL DEFAULT '[]', + -- 1 = the user threw the result away; hidden from the per-photo list + discarded INTEGER NOT NULL DEFAULT 0, + created_at BIGINT NOT NULL, + updated_at BIGINT NOT NULL +); +CREATE INDEX idx_enhance_jobs_hash ON enhance_jobs(original_hash, created_at); +CREATE INDEX idx_enhance_jobs_status ON enhance_jobs(status); diff --git a/src/database/enhance_dao.rs b/src/database/enhance_dao.rs new file mode 100644 index 0000000..aa90569 --- /dev/null +++ b/src/database/enhance_dao.rs @@ -0,0 +1,336 @@ +//! Enhance jobs (#192): one row per ComfyUI run submitted for a photo. +//! +//! The row is the only state: the poller advances it from ComfyUI's +//! `/queue` and `/history`, the endpoints read it, approve appends to +//! `saved`. `pending` / `saved` hold small JSON documents owned by +//! `crate::enhance`; this layer treats them as opaque text. + +use std::sync::{Arc, Mutex}; + +use diesel::prelude::*; +use diesel::sql_query; +use diesel::sql_types::{BigInt, Integer, Nullable, Text}; +use diesel::sqlite::SqliteConnection; + +use crate::database::{DbError, DbErrorKind, connect}; +use crate::otel::trace_db_call; + +pub const STATUS_QUEUED: &str = "queued"; +pub const STATUS_RUNNING: &str = "running"; +pub const STATUS_READY: &str = "ready"; +pub const STATUS_APPROVED: &str = "approved"; +pub const STATUS_FAILED: &str = "failed"; +pub const STATUS_EXPIRED: &str = "expired"; +pub const STATUS_UNAVAILABLE: &str = "unavailable"; + +#[derive(Debug, Clone, PartialEq, Eq, QueryableByName)] +pub struct EnhanceJobRow { + #[diesel(sql_type = Text)] + pub id: String, + #[diesel(sql_type = Text)] + pub original_hash: String, + #[diesel(sql_type = Integer)] + pub library_id: i32, + #[diesel(sql_type = Text)] + pub rel_path: String, + #[diesel(sql_type = Text)] + pub template: String, + #[diesel(sql_type = Nullable)] + pub prompt: Option, + #[diesel(sql_type = Nullable)] + pub reference_hash: Option, + #[diesel(sql_type = Nullable)] + pub reference_path: Option, + #[diesel(sql_type = Text)] + pub comfy_prompt_id: String, + #[diesel(sql_type = Text)] + pub status: String, + #[diesel(sql_type = Nullable)] + pub error: Option, + #[diesel(sql_type = Nullable)] + pub queue_position: Option, + #[diesel(sql_type = Text)] + pub pending: String, + #[diesel(sql_type = Text)] + pub saved: String, + #[diesel(sql_type = Integer)] + pub discarded: i32, + #[diesel(sql_type = BigInt)] + pub created_at: i64, + #[diesel(sql_type = BigInt)] + pub updated_at: i64, +} + +const COLUMNS: &str = "id, original_hash, library_id, rel_path, template, prompt, reference_hash, \ + reference_path, comfy_prompt_id, status, error, queue_position, pending, saved, \ + discarded, created_at, updated_at"; + +/// What the poller / approve changes on an existing job. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct JobUpdate { + pub status: String, + pub error: Option, + pub queue_position: Option, + /// `Some` replaces the `pending` document. + pub pending: Option, + /// `Some` replaces the `saved` document. + pub saved: Option, +} + +impl JobUpdate { + pub fn status(status: &str) -> Self { + JobUpdate { + status: status.to_string(), + error: None, + queue_position: None, + pending: None, + saved: None, + } + } +} + +pub trait EnhanceJobDao: Sync + Send { + fn insert(&mut self, job: &EnhanceJobRow) -> Result<(), DbError>; + fn get(&mut self, id: &str) -> Result, DbError>; + /// Newest first. + fn list_for_hash(&mut self, original_hash: &str, limit: i64) + -> Result, DbError>; + /// Jobs the poller still has to advance (`queued` / `running`). + fn list_active(&mut self) -> Result, DbError>; + fn update(&mut self, id: &str, update: &JobUpdate) -> Result<(), DbError>; + /// Hide a job from `list_for_hash`. + fn discard(&mut self, id: &str) -> Result<(), DbError>; + /// Delete jobs last touched before `cutoff` (unix seconds) that are not + /// active. Returns the number removed. + fn prune(&mut self, cutoff: i64) -> Result; +} + +pub struct SqliteEnhanceJobDao { + connection: Arc>, +} + +impl Default for SqliteEnhanceJobDao { + fn default() -> Self { + Self::new() + } +} + +impl SqliteEnhanceJobDao { + pub fn new() -> Self { + SqliteEnhanceJobDao { + connection: Arc::new(Mutex::new(connect())), + } + } + + /// Test-only: share a connection with other DAOs. + #[cfg(test)] + pub fn from_shared(connection: Arc>) -> Self { + SqliteEnhanceJobDao { connection } + } +} + +impl EnhanceJobDao for SqliteEnhanceJobDao { + fn insert(&mut self, job: &EnhanceJobRow) -> Result<(), DbError> { + trace_db_call("insert", "enhance_job_insert", |_span| { + let mut conn = self.connection.lock().expect("Unable to get EnhanceJobDao"); + sql_query(format!( + "INSERT INTO enhance_jobs ({COLUMNS}) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)" + )) + .bind::(&job.id) + .bind::(&job.original_hash) + .bind::(job.library_id) + .bind::(&job.rel_path) + .bind::(&job.template) + .bind::, _>(&job.prompt) + .bind::, _>(&job.reference_hash) + .bind::, _>(&job.reference_path) + .bind::(&job.comfy_prompt_id) + .bind::(&job.status) + .bind::, _>(&job.error) + .bind::, _>(job.queue_position) + .bind::(&job.pending) + .bind::(&job.saved) + .bind::(job.discarded) + .bind::(job.created_at) + .bind::(job.updated_at) + .execute(&mut *conn) + .map(|_| ()) + .map_err(|e| anyhow::anyhow!("Insert error: {}", e)) + }) + .map_err(|e| DbError::log(DbErrorKind::InsertError, e)) + } + + fn get(&mut self, id: &str) -> Result, DbError> { + trace_db_call("query", "enhance_job_get", |_span| { + let mut conn = self.connection.lock().expect("Unable to get EnhanceJobDao"); + sql_query(format!("SELECT {COLUMNS} FROM enhance_jobs WHERE id = ?")) + .bind::(id) + .load::(&mut *conn) + .map(|rows| rows.into_iter().next()) + .map_err(|e| anyhow::anyhow!("Query error: {}", e)) + }) + .map_err(|e| DbError::log(DbErrorKind::QueryError, e)) + } + + fn list_for_hash( + &mut self, + original_hash: &str, + limit: i64, + ) -> Result, DbError> { + trace_db_call("query", "enhance_job_list_for_hash", |_span| { + let mut conn = self.connection.lock().expect("Unable to get EnhanceJobDao"); + sql_query(format!( + "SELECT {COLUMNS} FROM enhance_jobs WHERE original_hash = ? AND discarded = 0 \ + ORDER BY created_at DESC, rowid DESC LIMIT ?" + )) + .bind::(original_hash) + .bind::(limit) + .load::(&mut *conn) + .map_err(|e| anyhow::anyhow!("Query error: {}", e)) + }) + .map_err(|e| DbError::log(DbErrorKind::QueryError, e)) + } + + fn list_active(&mut self) -> Result, DbError> { + trace_db_call("query", "enhance_job_list_active", |_span| { + let mut conn = self.connection.lock().expect("Unable to get EnhanceJobDao"); + sql_query(format!( + "SELECT {COLUMNS} FROM enhance_jobs WHERE status IN ('queued', 'running') \ + ORDER BY created_at, rowid" + )) + .load::(&mut *conn) + .map_err(|e| anyhow::anyhow!("Query error: {}", e)) + }) + .map_err(|e| DbError::log(DbErrorKind::QueryError, e)) + } + + fn update(&mut self, id: &str, u: &JobUpdate) -> Result<(), DbError> { + trace_db_call("update", "enhance_job_update", |_span| { + let mut conn = self.connection.lock().expect("Unable to get EnhanceJobDao"); + sql_query( + "UPDATE enhance_jobs SET status = ?, error = ?, queue_position = ?, \ + pending = COALESCE(?, pending), saved = COALESCE(?, saved), updated_at = ? \ + WHERE id = ?", + ) + .bind::(&u.status) + .bind::, _>(&u.error) + .bind::, _>(u.queue_position) + .bind::, _>(&u.pending) + .bind::, _>(&u.saved) + .bind::(chrono::Utc::now().timestamp()) + .bind::(id) + .execute(&mut *conn) + .map(|_| ()) + .map_err(|e| anyhow::anyhow!("Update error: {}", e)) + }) + .map_err(|e| DbError::log(DbErrorKind::UpdateError, e)) + } + + fn discard(&mut self, id: &str) -> Result<(), DbError> { + trace_db_call("update", "enhance_job_discard", |_span| { + let mut conn = self.connection.lock().expect("Unable to get EnhanceJobDao"); + sql_query("UPDATE enhance_jobs SET discarded = 1, updated_at = ? WHERE id = ?") + .bind::(chrono::Utc::now().timestamp()) + .bind::(id) + .execute(&mut *conn) + .map(|_| ()) + .map_err(|e| anyhow::anyhow!("Update error: {}", e)) + }) + .map_err(|e| DbError::log(DbErrorKind::UpdateError, e)) + } + + fn prune(&mut self, cutoff: i64) -> Result { + trace_db_call("delete", "enhance_job_prune", |_span| { + let mut conn = self.connection.lock().expect("Unable to get EnhanceJobDao"); + sql_query( + "DELETE FROM enhance_jobs WHERE updated_at < ? \ + AND status NOT IN ('queued', 'running')", + ) + .bind::(cutoff) + .execute(&mut *conn) + .map_err(|e| anyhow::anyhow!("Delete error: {}", e)) + }) + .map_err(|e| DbError::log(DbErrorKind::UpdateError, e)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::database::test::in_memory_db_connection; + + fn dao() -> SqliteEnhanceJobDao { + SqliteEnhanceJobDao::from_shared(Arc::new(Mutex::new(in_memory_db_connection()))) + } + + fn job(id: &str, hash: &str, status: &str, created: i64) -> EnhanceJobRow { + EnhanceJobRow { + id: id.into(), + original_hash: hash.into(), + library_id: 1, + rel_path: "a.jpg".into(), + template: "restore".into(), + prompt: Some("warmer".into()), + reference_hash: None, + reference_path: None, + comfy_prompt_id: format!("p-{id}"), + status: status.into(), + error: None, + queue_position: None, + pending: "[]".into(), + saved: "[]".into(), + discarded: 0, + created_at: created, + updated_at: created, + } + } + + #[test] + fn insert_get_list_update_prune() { + let mut d = dao(); + d.insert(&job("a", "h1", "queued", 100)).unwrap(); + d.insert(&job("b", "h1", "ready", 200)).unwrap(); + d.insert(&job("c", "h2", "running", 300)).unwrap(); + + assert_eq!(d.get("a").unwrap().unwrap().comfy_prompt_id, "p-a"); + assert!(d.get("zzz").unwrap().is_none()); + + let ids: Vec = d + .list_for_hash("h1", 10) + .unwrap() + .into_iter() + .map(|j| j.id) + .collect(); + assert_eq!(ids, ["b", "a"], "newest first"); + assert_eq!(d.list_for_hash("h1", 1).unwrap().len(), 1); + + let active: Vec = d.list_active().unwrap().into_iter().map(|j| j.id).collect(); + assert_eq!(active, ["a", "c"]); + + d.update( + "a", + &JobUpdate { + status: "ready".into(), + error: None, + queue_position: None, + pending: Some("[{\"id\":\"x\"}]".into()), + saved: None, + }, + ) + .unwrap(); + let a = d.get("a").unwrap().unwrap(); + assert_eq!(a.status, "ready"); + assert_eq!(a.pending, "[{\"id\":\"x\"}]"); + assert_eq!(a.saved, "[]", "unset columns are left alone"); + assert!(a.updated_at >= 100); + + d.discard("b").unwrap(); + assert_eq!(d.list_for_hash("h1", 10).unwrap().len(), 1, "discarded hidden"); + + // Prune drops only old, finished jobs (updated_at was just bumped + // for `a`; `c` is active). + assert_eq!(d.prune(i64::MAX).unwrap(), 2); + assert!(d.get("c").unwrap().is_some()); + assert!(d.get("b").unwrap().is_none()); + } +} diff --git a/src/database/mod.rs b/src/database/mod.rs index bdbfc3f..a2d1b7a 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -43,6 +43,7 @@ pub struct UtcBackfillCandidate { pub mod calendar_dao; pub mod daily_summary_dao; +pub mod enhance_dao; pub mod insight_generation_job_dao; pub mod insights_dao; pub mod knowledge_dao; @@ -59,6 +60,7 @@ pub mod version_dao; pub use calendar_dao::{CalendarEventDao, SqliteCalendarEventDao}; pub use daily_summary_dao::{DailySummaryDao, InsertDailySummary, SqliteDailySummaryDao}; +pub use enhance_dao::{EnhanceJobDao, SqliteEnhanceJobDao}; pub use insight_generation_job_dao::{InsightGenerationJobDao, SqliteInsightGenerationJobDao}; pub use insights_dao::{InsightDao, SqliteInsightDao}; pub use knowledge_dao::{ diff --git a/src/database/schema.rs b/src/database/schema.rs index ed2cb02..e59accf 100644 --- a/src/database/schema.rs +++ b/src/database/schema.rs @@ -41,6 +41,28 @@ diesel::table! { } } +diesel::table! { + enhance_jobs (id) { + id -> Text, + original_hash -> Text, + library_id -> Integer, + rel_path -> Text, + template -> Text, + prompt -> Nullable, + reference_hash -> Nullable, + reference_path -> Nullable, + comfy_prompt_id -> Text, + status -> Text, + error -> Nullable, + queue_position -> Nullable, + pending -> Text, + saved -> Text, + discarded -> Integer, + created_at -> BigInt, + updated_at -> BigInt, + } +} + diesel::table! { entities (id) { id -> Integer, @@ -406,6 +428,7 @@ diesel::allow_tables_to_appear_in_same_query!( calendar_events, clip_failures, daily_conversation_summaries, + enhance_jobs, entities, entity_facts, entity_photo_links, diff --git a/src/enhance/comfy.rs b/src/enhance/comfy.rs new file mode 100644 index 0000000..4102410 --- /dev/null +++ b/src/enhance/comfy.rs @@ -0,0 +1,423 @@ +//! Thin client for the parts of ComfyUI's HTTP API Enhance uses (#192). +//! +//! | Step | Call | +//! |-----------|----------------------------------------| +//! | reachable | `GET /system_stats` | +//! | submit | `POST /prompt {prompt, client_id}` | +//! | status | `GET /queue`, `GET /history/{id}` | +//! | preview | `GET /view?filename=&subfolder=&type=` | +//! | approve | `POST /imageapi/approve {id}` | +//! | free VRAM | `POST /free` | + +use std::time::Duration; + +use bytes::Bytes; +use serde::Deserialize; +use serde_json::{Value, json}; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum ComfyError { + /// Connect/timeout failure: ComfyUI isn't running (a normal state). + Unreachable(String), + /// ComfyUI answered with a status we don't handle. + Http { status: u16, body: String }, + /// ComfyUI answered with something we can't read. + BadResponse(String), +} + +impl std::fmt::Display for ComfyError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + ComfyError::Unreachable(e) => write!(f, "ComfyUI is unreachable: {e}"), + ComfyError::Http { status, body } => write!(f, "ComfyUI returned {status}: {body}"), + ComfyError::BadResponse(e) => write!(f, "unexpected ComfyUI response: {e}"), + } + } +} + +impl std::error::Error for ComfyError {} + +impl From for ComfyError { + fn from(e: reqwest::Error) -> Self { + if e.is_connect() || e.is_timeout() || e.is_request() { + ComfyError::Unreachable(e.without_url().to_string()) + } else { + ComfyError::BadResponse(e.without_url().to_string()) + } + } +} + +/// `POST /prompt` outcomes other than success. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum SubmitError { + Comfy(ComfyError), + /// ComfyUI refused the prompt (HTTP 400): validation failed. + Rejected { + message: String, + /// `node_errors` was non-empty (a node is missing an input, a model + /// or a link target). + node_errors: bool, + }, +} + +impl std::fmt::Display for SubmitError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + SubmitError::Comfy(e) => e.fmt(f), + SubmitError::Rejected { message, .. } => write!(f, "ComfyUI rejected the workflow: {message}"), + } + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum ApproveError { + Comfy(ComfyError), + /// The staged result is gone (ComfyUI restarted: its temp folder is cleared). + Gone, + /// ComfyUI refused the approval (bad id, write failure). + Refused(String), +} + +/// Where `GET /view` finds a staged result. +#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, Deserialize)] +pub struct ViewRef { + pub filename: String, + #[serde(default)] + pub subfolder: String, + #[serde(default = "temp", rename = "type")] + pub kind: String, +} + +fn temp() -> String { + "temp".into() +} + +/// A result the save node staged: ComfyUI's pending id, where to preview it, +/// and the library path when it was already saved (never, with approval on). +#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, Deserialize)] +pub struct Staged { + pub id: String, + pub view: ViewRef, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct HistoryEntry { + pub completed: bool, + /// `success` / `error`. + pub status_str: String, + /// ComfyUI's exception message when the run failed. + pub error: Option, + pub staged: Vec, + /// Last event time, unix millis. + pub timestamp_ms: Option, +} + +#[derive(Debug, Clone, PartialEq, Eq, Default)] +pub struct QueueState { + pub running: Vec, + pub pending: Vec, +} + +impl QueueState { + pub fn is_empty(&self) -> bool { + self.running.is_empty() && self.pending.is_empty() + } + + /// Jobs ahead of `prompt_id`; `None` when it isn't queued. + pub fn position_of(&self, prompt_id: &str) -> Option<(bool, usize)> { + if self.running.iter().any(|p| p == prompt_id) { + return Some((true, 0)); + } + self.pending + .iter() + .position(|p| p == prompt_id) + .map(|i| (false, self.running.len() + i)) + } +} + +#[derive(Clone)] +pub struct ComfyClient { + http: reqwest::Client, + base: String, + /// Client id sent with submissions. + client_id: String, +} + +impl ComfyClient { + pub fn new(base_url: &str, timeout: Duration) -> Self { + let http = reqwest::Client::builder() + .timeout(timeout) + .connect_timeout(Duration::from_secs(3).min(timeout)) + .build() + .expect("reqwest client"); + ComfyClient { + http, + base: base_url.trim_end_matches('/').to_string(), + client_id: "imageapi".to_string(), + } + } + + fn url(&self, path: &str) -> String { + format!("{}{}", self.base, path) + } + + /// `GET /system_stats` with its own (short) timeout. + pub async fn probe(&self, timeout: Duration) -> bool { + self.http + .get(self.url("/system_stats")) + .timeout(timeout) + .send() + .await + .is_ok_and(|r| r.status().is_success()) + } + + pub async fn submit(&self, prompt: &Value) -> Result { + let body = json!({ "prompt": prompt, "client_id": self.client_id }); + let resp = self + .http + .post(self.url("/prompt")) + .json(&body) + .send() + .await + .map_err(|e| SubmitError::Comfy(e.into()))?; + let status = resp.status(); + let text = resp + .text() + .await + .map_err(|e| SubmitError::Comfy(e.into()))?; + let json: Value = serde_json::from_str(&text).unwrap_or(Value::Null); + if status.is_success() { + // A 200 can still carry node_errors for outputs that were pruned; + // the prompt itself queued. + return json["prompt_id"] + .as_str() + .map(str::to_string) + .ok_or_else(|| { + SubmitError::Comfy(ComfyError::BadResponse(format!("no prompt_id in {text}"))) + }); + } + if status.as_u16() == 400 { + return Err(SubmitError::Rejected { + message: rejection_message(&json, &text), + node_errors: json["node_errors"] + .as_object() + .is_some_and(|m| !m.is_empty()), + }); + } + Err(SubmitError::Comfy(ComfyError::Http { + status: status.as_u16(), + body: truncate(&text), + })) + } + + pub async fn queue(&self) -> Result { + let json: Value = self.get_json("/queue").await?; + let ids = |key: &str| -> Vec { + json[key] + .as_array() + .map(|a| { + a.iter() + .filter_map(|item| item.get(1).and_then(Value::as_str)) + .map(str::to_string) + .collect() + }) + .unwrap_or_default() + }; + Ok(QueueState { + running: ids("queue_running"), + pending: ids("queue_pending"), + }) + } + + /// `GET /history/{id}`; `None` when ComfyUI doesn't know the id. + pub async fn history(&self, prompt_id: &str) -> Result, ComfyError> { + let json: Value = self + .get_json(&format!("/history/{}", urlencoding::encode(prompt_id))) + .await?; + Ok(json.get(prompt_id).map(parse_history_entry)) + } + + /// The newest finished run, for the idle check. + pub async fn newest_history(&self) -> Result, ComfyError> { + let json: Value = self.get_json("/history?max_items=1").await?; + Ok(json + .as_object() + .and_then(|m| m.values().next()) + .map(parse_history_entry)) + } + + /// `GET /view`: the staged PNG. `Ok(None)` for 404. + pub async fn view(&self, r: &ViewRef) -> Result, ComfyError> { + let resp = self + .http + .get(self.url("/view")) + .query(&[ + ("filename", r.filename.as_str()), + ("subfolder", r.subfolder.as_str()), + ("type", r.kind.as_str()), + ]) + .send() + .await?; + if resp.status().as_u16() == 404 { + return Ok(None); + } + if !resp.status().is_success() { + return Err(ComfyError::Http { + status: resp.status().as_u16(), + body: truncate(&resp.text().await.unwrap_or_default()), + }); + } + let content_type = resp + .headers() + .get(reqwest::header::CONTENT_TYPE) + .and_then(|v| v.to_str().ok()) + .unwrap_or("image/png") + .to_string(); + Ok(Some((content_type, resp.bytes().await?))) + } + + /// `POST /imageapi/approve`: ComfyUI writes the file; returns its path + /// as ComfyUI sees it. + pub async fn approve(&self, pending_id: &str) -> Result { + let resp = self + .http + .post(self.url("/imageapi/approve")) + .json(&json!({ "id": pending_id })) + .send() + .await + .map_err(|e| ApproveError::Comfy(e.into()))?; + let status = resp.status(); + let text = resp + .text() + .await + .map_err(|e| ApproveError::Comfy(e.into()))?; + let json: Value = serde_json::from_str(&text).unwrap_or(Value::Null); + if status.is_success() { + return json["path"].as_str().map(str::to_string).ok_or_else(|| { + ApproveError::Comfy(ComfyError::BadResponse(format!("no path in {text}"))) + }); + } + let message = json["error"].as_str().unwrap_or(&text).to_string(); + match status.as_u16() { + 404 => Err(ApproveError::Gone), + 400 | 500 => Err(ApproveError::Refused(truncate(&message))), + s => Err(ApproveError::Comfy(ComfyError::Http { + status: s, + body: truncate(&text), + })), + } + } + + /// `POST /free`: unload models and release cached memory. + pub async fn free(&self) -> Result<(), ComfyError> { + let resp = self + .http + .post(self.url("/free")) + .json(&json!({ "unload_models": true, "free_memory": true })) + .send() + .await?; + if resp.status().is_success() { + Ok(()) + } else { + Err(ComfyError::Http { + status: resp.status().as_u16(), + body: truncate(&resp.text().await.unwrap_or_default()), + }) + } + } + + async fn get_json(&self, path: &str) -> Result { + let resp = self.http.get(self.url(path)).send().await?; + if !resp.status().is_success() { + return Err(ComfyError::Http { + status: resp.status().as_u16(), + body: truncate(&resp.text().await.unwrap_or_default()), + }); + } + Ok(resp.json().await?) + } +} + +fn truncate(s: &str) -> String { + s.chars().take(300).collect() +} + +fn rejection_message(json: &Value, raw: &str) -> String { + let err = &json["error"]; + let head = err["message"].as_str().or_else(|| err.as_str()); + let detail = err["details"].as_str().filter(|d| !d.is_empty()); + // Per-node errors say which input is wrong. + let nodes: Vec = json["node_errors"] + .as_object() + .map(|m| { + m.values() + .filter_map(|n| { + let class = n["class_type"].as_str().unwrap_or("node"); + let first = n["errors"].as_array()?.first()?; + Some(format!( + "{class}: {}", + first["message"].as_str().unwrap_or("invalid") + )) + }) + .collect() + }) + .unwrap_or_default(); + let mut parts: Vec = Vec::new(); + parts.extend(head.map(str::to_string)); + parts.extend(detail.map(str::to_string)); + parts.extend(nodes); + if parts.is_empty() { + truncate(raw) + } else { + truncate(&parts.join("; ")) + } +} + +pub(crate) fn parse_history_entry(entry: &Value) -> HistoryEntry { + let status = &entry["status"]; + let status_str = status["status_str"].as_str().unwrap_or_default().to_string(); + let completed = status["completed"].as_bool().unwrap_or(false); + let messages = status["messages"].as_array().cloned().unwrap_or_default(); + let error = messages.iter().find_map(|m| { + let (kind, data) = (m.get(0)?.as_str()?, m.get(1)?); + (kind == "execution_error").then(|| { + data["exception_message"] + .as_str() + .unwrap_or("execution error") + .trim() + .to_string() + }) + }); + let timestamp_ms = messages + .iter() + .filter_map(|m| m.get(1)?.get("timestamp")?.as_i64()) + .max(); + + let mut staged = Vec::new(); + if let Some(outputs) = entry["outputs"].as_object() { + for out in outputs.values() { + let Some(results) = out["imageapi_versions"].as_array() else { + continue; + }; + let images = out["images"].as_array().cloned().unwrap_or_default(); + for (i, r) in results.iter().enumerate() { + let (Some(id), Some(image)) = (r["id"].as_str(), images.get(i)) else { + continue; + }; + if let Ok(view) = serde_json::from_value::(image.clone()) { + staged.push(Staged { + id: id.to_string(), + view, + }); + } + } + } + } + HistoryEntry { + completed, + status_str, + error, + staged, + timestamp_ms, + } +} diff --git a/src/enhance/config.rs b/src/enhance/config.rs new file mode 100644 index 0000000..dd1f34f --- /dev/null +++ b/src/enhance/config.rs @@ -0,0 +1,263 @@ +//! Enhance configuration (#192): one `enhance.toml`, next to `llm.toml`. +//! +//! Path from `ENHANCE_CONFIG_PATH`, else `./enhance.toml`. A missing file at +//! the default path means Enhance is simply off (no `url`); a missing +//! explicit path is an error. Nothing here is a secret: ComfyUI has no auth +//! and stays LAN-only. `enhance.example.toml` is the annotated schema (a +//! test parses it). + +use std::path::{Path, PathBuf}; + +use anyhow::{Context as _, Result, bail}; +use serde::Deserialize; + +use super::path_map::PathMap; + +const DEFAULT_PATH: &str = "enhance.toml"; + +/// How the app's text combines with the template's own prompt. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, serde::Serialize, Default)] +#[serde(rename_all = "lowercase")] +pub enum PromptMode { + /// The template's prompt is a full base instruction; the app's text is + /// appended after `prompt_join`. Empty text leaves the prompt as is. + #[default] + Append, + /// The app's text *is* the prompt (and is required). + Replace, +} + +#[derive(Debug, Clone, PartialEq, Deserialize)] +#[serde(default, deny_unknown_fields)] +pub struct EnhanceConfig { + /// ComfyUI base URL, e.g. `http://192.168.10.218:8188`. Unset = Enhance + /// is disabled (`/enhance/templates` reports `available: false`). + pub url: Option, + /// Per-request timeout for ComfyUI calls. + pub timeout_secs: u64, + /// How long the `available` probe result is cached. + pub probe_ttl_secs: u64, + /// Job poll interval. + pub poll_secs: u64, + /// Idle time (empty queue, newest history entry this old) before + /// ImageApi calls `POST /free` once. 0 disables it. + pub idle_free_secs: u64, + /// An active job whose ComfyUI stays unreachable this long becomes + /// `unavailable`. + pub unreachable_grace_secs: u64, + /// Template preselected by clients; default = the first one. + pub default_template: Option, + /// Clients warn above this many megapixels in the original. + pub warn_above_megapixels: f64, + /// Library path prefix <-> ComfyUI path prefix. Longest prefix wins. + pub path_map: Vec, + #[serde(rename = "template")] + pub templates: Vec, +} + +impl Default for EnhanceConfig { + fn default() -> Self { + Self { + url: None, + timeout_secs: 15, + probe_ttl_secs: 10, + poll_secs: 2, + idle_free_secs: 300, + unreachable_grace_secs: 120, + default_template: None, + warn_above_megapixels: 1.5, + path_map: Vec::new(), + templates: Vec::new(), + } + } +} + +#[derive(Debug, Clone, PartialEq, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct PathMapping { + /// Prefix as ImageApi sees it, e.g. `/media/nas/`. + pub from: String, + /// Prefix as ComfyUI sees it, e.g. `Z:\` or `\\nas\`. + pub to: String, +} + +#[derive(Debug, Clone, PartialEq, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct TemplateConfig { + /// Stable id clients send (`POST /enhance { template }`). + pub name: String, + pub label: String, + #[serde(default)] + pub description: String, + /// API-format workflow JSON (ComfyUI -> Workflow -> Export (API)). + /// Relative paths resolve against the config file's directory. + pub file: PathBuf, + #[serde(default)] + pub prompt_mode: PromptMode, + /// Text between the template's prompt and the app's text (append mode). + #[serde(default = "default_join")] + pub prompt_join: String, + /// Approximate output size, for the client's large-original warning. + #[serde(default = "default_output_mp")] + pub output_megapixels: f64, +} + +fn default_join() -> String { + " ".into() +} + +fn default_output_mp() -> f64 { + 1.0 +} + +impl EnhanceConfig { + pub fn validate(&self) -> Result<()> { + if let Some(url) = &self.url + && !(url.starts_with("http://") || url.starts_with("https://")) + { + bail!("url must start with http:// or https://, got {url:?}"); + } + for (key, v) in [ + ("timeout_secs", self.timeout_secs), + ("probe_ttl_secs", self.probe_ttl_secs), + ("poll_secs", self.poll_secs), + ("unreachable_grace_secs", self.unreachable_grace_secs), + ] { + if v == 0 { + bail!("{key} must be greater than 0"); + } + } + if !(self.warn_above_megapixels > 0.0) { + bail!("warn_above_megapixels must be greater than 0"); + } + let mut seen = std::collections::HashSet::new(); + for t in &self.templates { + let ok = !t.name.is_empty() + && t.name + .chars() + .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-'); + if !ok { + bail!( + "template name {:?} must be non-empty and use only letters, digits, '_' and '-'", + t.name + ); + } + if !seen.insert(t.name.as_str()) { + bail!("duplicate template name {:?}", t.name); + } + if t.label.trim().is_empty() { + bail!("template {:?} needs a label", t.name); + } + if !(t.output_megapixels > 0.0) { + bail!("template {:?}: output_megapixels must be > 0", t.name); + } + } + if let Some(d) = &self.default_template + && !self.templates.iter().any(|t| &t.name == d) + { + bail!("default_template {d:?} is not a configured template"); + } + // Parses every mapping (rejects empty prefixes). + PathMap::new(&self.path_map)?; + Ok(()) + } + + /// The preselected template's name. + pub fn default_name(&self) -> Option<&str> { + self.default_template + .as_deref() + .or_else(|| self.templates.first().map(|t| t.name.as_str())) + } +} + +/// Where the config was read from, and what it said. +#[derive(Debug)] +pub struct Loaded { + pub config: EnhanceConfig, + /// Directory relative template `file`s resolve against. + pub base_dir: PathBuf, + /// The file, when one was read. + pub path: Option, +} + +/// Read `enhance.toml` (see module docs). A bad file is an error so a typo +/// fails startup instead of silently disabling the feature. +pub fn load() -> Result { + let (path, explicit) = match std::env::var("ENHANCE_CONFIG_PATH") { + Ok(p) if !p.trim().is_empty() => (PathBuf::from(p.trim()), true), + _ => (PathBuf::from(DEFAULT_PATH), false), + }; + match std::fs::read_to_string(&path) { + Ok(text) => { + let config = parse(&text).with_context(|| format!("in {}", path.display()))?; + let base_dir = path + .parent() + .filter(|p| !p.as_os_str().is_empty()) + .map(Path::to_path_buf) + .unwrap_or_else(|| PathBuf::from(".")); + Ok(Loaded { + config, + base_dir, + path: Some(path), + }) + } + Err(e) if e.kind() == std::io::ErrorKind::NotFound && !explicit => Ok(Loaded { + config: EnhanceConfig::default(), + base_dir: PathBuf::from("."), + path: None, + }), + Err(e) => Err(e).with_context(|| format!("reading enhance config {}", path.display())), + } +} + +pub fn parse(text: &str) -> Result { + let config: EnhanceConfig = toml::from_str(text)?; + config.validate()?; + Ok(config) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn example_file_parses() { + let cfg = parse(include_str!("../../enhance.example.toml")).unwrap(); + assert!(cfg.url.is_some()); + assert_eq!(cfg.templates[0].name, "restore"); + assert_eq!(cfg.default_name(), Some("restore")); + assert!( + cfg.templates + .iter() + .any(|t| t.prompt_mode == PromptMode::Replace) + ); + } + + #[test] + fn empty_file_is_disabled_defaults() { + let cfg = parse("").unwrap(); + assert_eq!(cfg, EnhanceConfig::default()); + assert!(cfg.url.is_none()); + } + + #[test] + fn rejects_unknown_keys_and_bad_values() { + assert!(parse("nope = 1").is_err()); + assert!(parse("url = \"ftp://x\"").is_err()); + assert!(parse("poll_secs = 0").is_err()); + assert!(parse("default_template = \"x\"").is_err()); + let dup = "[[template]]\nname='a'\nlabel='A'\nfile='a.json'\n[[template]]\nname='a'\nlabel='B'\nfile='b.json'"; + assert!(parse(dup).is_err()); + assert!(parse("[[template]]\nname='a b'\nlabel='A'\nfile='a.json'").is_err()); + assert!(parse("[[path_map]]\nfrom=''\nto='Z:\\\\'").is_err()); + } + + #[test] + fn template_defaults() { + let cfg = parse("[[template]]\nname='a'\nlabel='A'\nfile='a.json'").unwrap(); + let t = &cfg.templates[0]; + assert_eq!(t.prompt_mode, PromptMode::Append); + assert_eq!(t.prompt_join, " "); + assert_eq!(t.output_megapixels, 1.0); + } +} diff --git a/src/enhance/handlers.rs b/src/enhance/handlers.rs new file mode 100644 index 0000000..3f91989 --- /dev/null +++ b/src/enhance/handlers.rs @@ -0,0 +1,515 @@ +//! HTTP surface of Enhance (#192). ImageApi relays everything, previews +//! included: the apps never talk to ComfyUI. +//! +//! - `GET /enhance/templates` +//! - `POST /enhance` +//! - `GET /enhance/jobs?path=` +//! - `GET /enhance/jobs/{id}` +//! - `GET /enhance/jobs/{id}/preview/{n}` +//! - `POST /enhance/jobs/{id}/approve` +//! - `POST /enhance/jobs/{id}/discard` + +use std::path::{Component, Path, PathBuf}; +use std::sync::{Arc, Mutex}; +use std::time::Duration; + +use actix_web::{HttpResponse, Responder, get, post, web}; +use log::warn; +use serde::{Deserialize, Serialize}; +use serde_json::json; + +use super::config::PromptMode; +use super::service::{EnhanceError, EnhanceService, SourceFile}; +use crate::data::Claims; +use crate::database::ExifDao; +use crate::database::enhance_dao::{EnhanceJobRow, STATUS_APPROVED, STATUS_READY}; +use crate::libraries::{self, Library}; +use crate::state::AppState; +use crate::utils::normalize_path; +use crate::versions::handlers::build_response; + +type ExifData = web::Data>>; + +fn error_response(e: &EnhanceError) -> HttpResponse { + let (mut builder, code) = match e { + EnhanceError::Disabled => (HttpResponse::ServiceUnavailable(), "disabled"), + EnhanceError::Unavailable => (HttpResponse::ServiceUnavailable(), "unavailable"), + EnhanceError::UnknownTemplate(_) => (HttpResponse::NotFound(), "unknown_template"), + EnhanceError::BadRequest(_) => (HttpResponse::BadRequest(), "bad_request"), + EnhanceError::Rejected(_) => (HttpResponse::UnprocessableEntity(), "rejected"), + EnhanceError::Expired => (HttpResponse::Gone(), "expired"), + EnhanceError::NotFound => (HttpResponse::NotFound(), "not_found"), + EnhanceError::Unplaceable(_) => (HttpResponse::BadGateway(), "unplaceable"), + EnhanceError::Upstream(_) => (HttpResponse::BadGateway(), "upstream"), + EnhanceError::Internal(_) => (HttpResponse::InternalServerError(), "internal"), + }; + builder.json(json!({ "error": e.message(), "code": code })) +} + +fn bad(msg: &str) -> HttpResponse { + error_response(&EnhanceError::BadRequest(msg.to_string())) +} + +// ---- views -------------------------------------------------------------- + +#[derive(Debug, Serialize)] +struct PendingView { + /// Index for `preview/{n}` and `approve { n }`. + n: usize, + /// Path of the preview on this API. + preview: String, + approved: bool, +} + +#[derive(Debug, Serialize)] +struct SavedView { + n: usize, + path: Option, + library_id: Option, +} + +#[derive(Debug, Serialize)] +pub struct JobView { + id: String, + status: String, + #[serde(skip_serializing_if = "Option::is_none")] + queue_position: Option, + #[serde(skip_serializing_if = "Option::is_none")] + error: Option, + path: String, + library_id: i32, + template: String, + #[serde(skip_serializing_if = "Option::is_none")] + prompt: Option, + #[serde(skip_serializing_if = "Option::is_none")] + reference_path: Option, + created_at: i64, + updated_at: i64, + pending: Vec, + saved: Vec, +} + +pub fn job_view(job: &EnhanceJobRow) -> JobView { + let saved = EnhanceService::saved_of(job); + // An expired job's staged files are gone; don't offer them. + let pending = if job.status == STATUS_READY || job.status == STATUS_APPROVED { + EnhanceService::pending_of(job) + .iter() + .enumerate() + .map(|(n, _)| PendingView { + n, + preview: format!("/enhance/jobs/{}/preview/{n}", job.id), + approved: saved.iter().any(|s| s.n == n), + }) + .collect() + } else { + Vec::new() + }; + let queued = matches!(job.status.as_str(), "queued" | "running"); + JobView { + id: job.id.clone(), + status: job.status.clone(), + queue_position: job.queue_position.filter(|_| queued), + error: job.error.clone(), + path: job.rel_path.clone(), + library_id: job.library_id, + template: job.template.clone(), + prompt: job.prompt.clone(), + reference_path: job.reference_path.clone(), + created_at: job.created_at, + updated_at: job.updated_at, + pending, + saved: saved + .into_iter() + .map(|s| SavedView { + n: s.n, + path: s.rel_path, + library_id: s.library_id, + }) + .collect(), + } +} + +// ---- file resolution ---------------------------------------------------- + +fn safe_rel(rel: &str) -> bool { + !rel.is_empty() + && Path::new(rel) + .components() + .all(|c| matches!(c, Component::Normal(_))) +} + +/// An indexed, hashed file in a library. +fn resolve_file( + app_state: &AppState, + exif_dao: &Mutex>, + path: &str, + library: Option<&str>, +) -> Result { + let library_id = match libraries::resolve_library_param_state(app_state, library) { + Ok(l) => l.map(|l| l.id), + Err(msg) => return Err(bad(&msg)), + }; + let rel = normalize_path(path); + if !safe_rel(&rel) { + return Err(bad("invalid path")); + } + let mut rows = exif_dao + .lock() + .expect("Unable to lock ExifDao") + .get_exif_batch(library_id, std::slice::from_ref(&rel)) + .map_err(|_| error_response(&EnhanceError::Internal("lookup failed".into())))?; + rows.sort_by_key(|r| r.id); + let Some(row) = rows.iter().find(|r| r.content_hash.is_some()) else { + return Err(error_response(&EnhanceError::NotFound)); + }; + let Some(lib) = app_state.library_by_id(row.library_id) else { + return Err(error_response(&EnhanceError::NotFound)); + }; + let abs = Path::new(&lib.root_path).join(&rel); + if !abs.is_file() { + return Err(error_response(&EnhanceError::NotFound)); + } + Ok(SourceFile { + library_id: row.library_id, + rel_path: rel, + abs, + hash: row.content_hash.clone().unwrap_or_default(), + }) +} + +/// Library and relative path of an absolute file ComfyUI wrote. The job's own +/// library wins when roots overlap. +fn place_in_libraries( + libs: &[Library], + prefer: i32, + abs: &Path, +) -> Option<(i32, String)> { + let mut ordered: Vec<&Library> = libs.iter().collect(); + ordered.sort_by_key(|l| l.id != prefer); + for lib in ordered { + if let Ok(rest) = abs.strip_prefix(&lib.root_path) { + let rel = rest.to_str()?.replace('\\', "/"); + if safe_rel(&rel) { + return Some((lib.id, rel)); + } + } + } + None +} + +// ---- templates ---------------------------------------------------------- + +#[derive(Serialize)] +struct TemplateView { + name: String, + label: String, + description: String, + accepts_prompt: bool, + prompt_mode: PromptMode, + /// Replace-mode templates need non-empty text (empty is a 400). + prompt_required: bool, + accepts_reference: bool, + default: bool, + /// Approximate output size; clients warn when the original is larger. + output_megapixels: f64, + warn_above_megapixels: f64, +} + +#[get("/enhance/templates")] +pub async fn get_templates(_: Claims, svc: web::Data) -> impl Responder { + let available = svc.available().await; + let reason = if !svc.is_configured() { + Some(super::service::MSG_DISABLED) + } else if !available { + Some(super::service::MSG_UNAVAILABLE) + } else { + None + }; + let default = svc.default_template().map(str::to_string); + let templates: Vec = svc + .templates() + .iter() + .map(|t| TemplateView { + name: t.cfg.name.clone(), + label: t.cfg.label.clone(), + description: t.cfg.description.clone(), + accepts_prompt: t.accepts_prompt(), + prompt_mode: t.cfg.prompt_mode, + prompt_required: t.prompt_required(), + accepts_reference: t.accepts_reference(), + default: default.as_deref() == Some(t.cfg.name.as_str()), + output_megapixels: t.cfg.output_megapixels, + warn_above_megapixels: svc.config.warn_above_megapixels, + }) + .collect(); + let mut body = json!({ + "available": available, + "default": default, + "warn_above_megapixels": svc.config.warn_above_megapixels, + "templates": templates, + }); + if let Some(r) = reason { + body["reason"] = json!(r); + } + HttpResponse::Ok().json(body) +} + +// ---- jobs --------------------------------------------------------------- + +#[derive(Debug, Deserialize)] +pub struct LibraryQuery { + pub library: Option, +} + +#[derive(Debug, Deserialize)] +pub struct SubmitBody { + pub path: String, + pub template: String, + #[serde(default)] + pub prompt: Option, + #[serde(default)] + pub reference_path: Option, + #[serde(default)] + pub library: Option, +} + +#[post("/enhance")] +#[tracing::instrument(name = "enhance_submit", skip_all)] +pub async fn submit( + _: Claims, + query: web::Query, + body: web::Json, + svc: web::Data, + app_state: web::Data, + exif_dao: ExifData, +) -> impl Responder { + if !svc.is_configured() { + return error_response(&EnhanceError::Disabled); + } + let library = body.library.as_deref().or(query.library.as_deref()); + let photo = match resolve_file(&app_state, &exif_dao, &body.path, library) { + Ok(f) => f, + Err(r) => return r, + }; + let reference = match body.reference_path.as_deref().filter(|p| !p.trim().is_empty()) { + Some(p) => { + // The reference lives in the photo's own library. + let lib = app_state.library_by_id(photo.library_id).map(|l| l.name.clone()); + match resolve_file(&app_state, &exif_dao, p, lib.as_deref()) { + Ok(f) => Some(f), + Err(r) => return r, + } + } + None => None, + }; + match svc + .submit(&photo, &body.template, body.prompt.as_deref(), reference.as_ref()) + .await + { + Ok(job) => HttpResponse::Ok().json(job_view(&job)), + Err(e) => error_response(&e), + } +} + +#[get("/enhance/jobs/{id}")] +pub async fn get_job( + _: Claims, + id: web::Path, + svc: web::Data, +) -> impl Responder { + match svc.get_job(&id) { + Ok(job) => HttpResponse::Ok().json(job_view(&job)), + Err(e) => error_response(&e), + } +} + +#[derive(Debug, Deserialize)] +pub struct JobsQuery { + pub path: String, + pub library: Option, +} + +/// A photo's recent jobs, newest first, so the viewer can pick up an enhance +/// in progress after reopening. Discarded jobs are not listed. +#[get("/enhance/jobs")] +pub async fn list_jobs( + _: Claims, + q: web::Query, + svc: web::Data, + app_state: web::Data, + exif_dao: ExifData, +) -> impl Responder { + let file = match resolve_file(&app_state, &exif_dao, &q.path, q.library.as_deref()) { + Ok(f) => f, + Err(r) => return r, + }; + match svc.jobs_for_hash(&file.hash) { + Ok(jobs) => { + let views: Vec = jobs.iter().map(job_view).collect(); + HttpResponse::Ok().json(json!({ "jobs": views })) + } + Err(e) => error_response(&e), + } +} + +#[get("/enhance/jobs/{id}/preview/{n}")] +pub async fn preview( + _: Claims, + path: web::Path<(String, usize)>, + svc: web::Data, +) -> impl Responder { + let (id, n) = path.into_inner(); + let job = match svc.get_job(&id) { + Ok(j) => j, + Err(e) => return error_response(&e), + }; + match svc.preview(&job, n).await { + Ok((content_type, bytes)) => HttpResponse::Ok() + .content_type(content_type) + .insert_header(("Cache-Control", "private, max-age=3600")) + .body(bytes), + Err(e) => error_response(&e), + } +} + +#[post("/enhance/jobs/{id}/discard")] +pub async fn discard( + _: Claims, + id: web::Path, + svc: web::Data, +) -> impl Responder { + match svc.discard(&id) { + Ok(job) => HttpResponse::Ok().json(job_view(&job)), + Err(e) => error_response(&e), + } +} + +#[derive(Debug, Deserialize)] +pub struct ApproveBody { + #[serde(default)] + pub n: usize, +} + +/// Ask ComfyUI to write result `n` into the library, ingest that file now, +/// and answer with the version (an `/image/versions` `versions[]` entry). +#[post("/enhance/jobs/{id}/approve")] +#[tracing::instrument(name = "enhance_approve", skip_all)] +pub async fn approve( + _: Claims, + id: web::Path, + body: Option>, + svc: web::Data, + app_state: web::Data, + exif_dao: ExifData, +) -> impl Responder { + let n = body.map(|b| b.n).unwrap_or(0); + let job = match svc.get_job(&id) { + Ok(j) => j, + Err(e) => return error_response(&e), + }; + let libs = app_state.libraries.clone(); + let prefer = job.library_id; + let place = move |abs: &Path| place_in_libraries(&libs, prefer, abs); + let (job, saved) = match svc.approve(&id, n, &place).await { + Ok(v) => v, + Err(e) => return error_response(&e), + }; + let (Some(library_id), Some(rel)) = (saved.library_id, saved.rel_path.clone()) else { + return error_response(&EnhanceError::Unplaceable( + "the saved file isn't in a library".into(), + )); + }; + let Some(library) = app_state.library_by_id(library_id).cloned() else { + return error_response(&EnhanceError::Unplaceable("unknown library".into())); + }; + let abs = PathBuf::from(&library.root_path).join(&rel); + + // The file was written by another machine over a share; give a lagging + // mount a moment to show it. + for _ in 0..25 { + if abs.is_file() { + break; + } + tokio::time::sleep(Duration::from_millis(200)).await; + } + if !abs.is_file() { + return error_response(&EnhanceError::Unplaceable(format!( + "ComfyUI saved {rel} but it isn't visible to ImageApi yet; it will appear after the next scan" + ))); + } + + let version_dao = app_state.version_dao.clone(); + let exif_arc: Arc>> = exif_dao.clone().into_inner(); + let (lib2, rel2, abs2) = (library.clone(), rel.clone(), abs.clone()); + let ingested = web::block(move || { + ingest_saved(&lib2, &rel2, &abs2, &exif_arc, &version_dao) + }) + .await; + let hash = match ingested { + Ok(Some(h)) => h, + _ => { + warn!("enhance: ingest of {rel} failed"); + return HttpResponse::Accepted().json(json!({ + "linked": false, + "path": rel, + "library_id": library_id, + "job": job_view(&job), + })); + } + }; + + // Which version row did that become? + let entry = { + let mut dao = app_state.version_dao.lock().expect("Unable to lock VersionDao"); + let original = match dao.get_link(&hash) { + Ok(Some(link)) => link.original_hash, + _ => job.original_hash.clone(), + }; + build_response(dao.as_mut(), &original, job.library_id) + .ok() + .and_then(|r| r.versions.into_iter().find(|v| v.hash == hash)) + }; + match entry { + Some(v) => HttpResponse::Ok().json(v), + None => HttpResponse::Accepted().json(json!({ + "linked": false, + "path": rel, + "library_id": library_id, + "job": job_view(&job), + })), + } +} + +/// Register the saved file (if the watcher hasn't yet) and link it as a +/// version. Idempotent. Returns the file's content hash. +pub fn ingest_saved( + library: &Library, + rel: &str, + abs: &Path, + exif_dao: &Arc>>, + version_dao: &Arc>>, +) -> Option { + let rel_s = rel.to_string(); + let existing = exif_dao + .lock() + .expect("Unable to lock ExifDao") + .get_exif_batch(Some(library.id), std::slice::from_ref(&rel_s)) + .ok()?; + let file = vec![(abs.to_path_buf(), rel_s.clone())]; + if existing.is_empty() { + crate::watcher::register_new_files(library, file.clone(), exif_dao); + } + let linked = crate::versions::link_new_files(library, &file, exif_dao, version_dao) + + crate::versions::sweep_name_links(library, exif_dao, version_dao); + if linked > 0 { + crate::duplicates::invalidate_caches(); + } + exif_dao + .lock() + .expect("Unable to lock ExifDao") + .get_exif_batch(Some(library.id), std::slice::from_ref(&rel_s)) + .ok()? + .into_iter() + .find_map(|r| r.content_hash) +} diff --git a/src/enhance/integration_tests.rs b/src/enhance/integration_tests.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/enhance/mod.rs b/src/enhance/mod.rs new file mode 100644 index 0000000..2e31846 --- /dev/null +++ b/src/enhance/mod.rs @@ -0,0 +1,25 @@ +//! Enhance on demand (#192): run a ComfyUI restore/edit workflow on a photo +//! from the apps, approve the result in-app, and have it land in the library +//! as a version (#191). +//! +//! - [`config`]: `enhance.toml` (templates, path map, timings). +//! - [`template`]: workflow loading/validation and per-photo filling. +//! - [`comfy`]: the ComfyUI HTTP calls used. +//! - [`service`]: submit, poll, approve, idle `/free`. +//! - [`handlers`]: the HTTP endpoints. +//! +//! ImageApi never writes under a library root: ComfyUI writes the approved +//! file, ImageApi only ingests it. + +pub mod comfy; +pub mod config; +pub mod handlers; +pub mod path_map; +pub mod service; +pub mod template; + +#[cfg(test)] +mod integration_tests; + +pub use handlers::{approve, discard, get_job, get_templates, list_jobs, preview, submit}; +pub use service::EnhanceService; diff --git a/src/enhance/path_map.rs b/src/enhance/path_map.rs new file mode 100644 index 0000000..1071959 --- /dev/null +++ b/src/enhance/path_map.rs @@ -0,0 +1,174 @@ +//! Library path <-> ComfyUI path mapping (#192). +//! +//! ImageApi sees `/media/nas/Media/Pictures/a.jpg`; ComfyUI (on Windows) +//! sees `Z:\Media\Pictures\a.jpg` or `\\nas\Media\Pictures\a.jpg`. Each +//! mapping is a prefix pair; the longest matching prefix wins and the +//! separators are converted. Used in both directions: library -> ComfyUI on +//! submit, and ComfyUI -> library to recognise the path `approve` returns. +//! With no mappings configured, paths pass through unchanged. + +use std::path::{Path, PathBuf}; + +use anyhow::{Result, bail}; + +use super::config::PathMapping; + +#[derive(Debug, Clone)] +struct Entry { + /// Library side, `/`-separated, always ending in `/`. + from: String, + /// ComfyUI side without a trailing separator. + to: String, + /// Separator ComfyUI's side uses. + to_sep: char, +} + +#[derive(Debug, Clone, Default)] +pub struct PathMap { + entries: Vec, +} + +impl PathMap { + pub fn new(mappings: &[PathMapping]) -> Result { + let mut entries = Vec::new(); + for m in mappings { + let from = m.from.replace('\\', "/"); + let to = m.to.trim(); + if from.trim_matches('/').is_empty() || to.is_empty() { + bail!("path_map entries need non-empty from and to ({m:?})"); + } + let to_sep = if to.contains('\\') { '\\' } else { '/' }; + entries.push(Entry { + from: format!("{}/", from.trim_end_matches('/')), + to: to.trim_end_matches(['/', '\\']).to_string(), + to_sep, + }); + } + Ok(PathMap { entries }) + } + + pub fn is_identity(&self) -> bool { + self.entries.is_empty() + } + + /// The path ComfyUI should open for a library file. `None` when mappings + /// exist but none covers the file. + pub fn to_comfy(&self, abs: &Path) -> Option { + let text = abs.to_str()?.replace('\\', "/"); + if self.entries.is_empty() { + return Some(abs.to_string_lossy().into_owned()); + } + let best = self + .entries + .iter() + .filter(|e| text.starts_with(e.from.as_str())) + .max_by_key(|e| e.from.len())?; + let rest = &text[best.from.len()..]; + let sep = best.to_sep.to_string(); + Some(format!("{}{}{}", best.to, sep, rest.replace('/', &sep))) + } + + /// The library-side absolute path of a path ComfyUI reported. Matching + /// is case-insensitive (Windows). `None` when no mapping covers it or it + /// tries to climb out with `..`. + pub fn from_comfy(&self, comfy: &str) -> Option { + let text = comfy.trim().replace('\\', "/"); + if self.entries.is_empty() { + return Some(PathBuf::from(text)); + } + let best = self + .entries + .iter() + .filter_map(|e| { + let prefix = format!("{}/", e.to.replace('\\', "/")); + let head = text.get(..prefix.len())?; + head.eq_ignore_ascii_case(&prefix).then_some((e, prefix.len())) + }) + .max_by_key(|(_, len)| *len)?; + let rest = &text[best.1..]; + if rest.split('/').any(|c| c == "..") { + return None; + } + Some(PathBuf::from(format!("{}{}", best.0.from, rest))) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn map(pairs: &[(&str, &str)]) -> PathMap { + let m: Vec = pairs + .iter() + .map(|(f, t)| PathMapping { + from: f.to_string(), + to: t.to_string(), + }) + .collect(); + PathMap::new(&m).unwrap() + } + + #[test] + fn drive_letter_round_trip() { + let m = map(&[("/media/nas/", "Z:\\")]); + let c = m + .to_comfy(Path::new("/media/nas/Media/Pictures/a b.jpg")) + .unwrap(); + assert_eq!(c, "Z:\\Media\\Pictures\\a b.jpg"); + assert_eq!( + m.from_comfy(&c).unwrap(), + PathBuf::from("/media/nas/Media/Pictures/a b.jpg") + ); + // Drive letters are case-insensitive. + assert_eq!( + m.from_comfy("z:\\x\\y.png").unwrap(), + PathBuf::from("/media/nas/x/y.png") + ); + } + + #[test] + fn unc_target_and_missing_trailing_slash() { + let m = map(&[("/media/nas", "\\\\nas\\")]); + assert_eq!( + m.to_comfy(Path::new("/media/nas/Media/a.jpg")).unwrap(), + "\\\\nas\\Media\\a.jpg" + ); + assert_eq!( + m.from_comfy("\\\\nas\\Media\\a.jpg").unwrap(), + PathBuf::from("/media/nas/Media/a.jpg") + ); + } + + #[test] + fn longest_prefix_wins_both_ways() { + let m = map(&[("/media/nas/", "Z:\\"), ("/media/nas/Media/", "Y:\\")]); + assert_eq!( + m.to_comfy(Path::new("/media/nas/Media/a.jpg")).unwrap(), + "Y:\\a.jpg" + ); + assert_eq!( + m.to_comfy(Path::new("/media/nas/Other/a.jpg")).unwrap(), + "Z:\\Other\\a.jpg" + ); + assert_eq!( + m.from_comfy("Y:\\a.jpg").unwrap(), + PathBuf::from("/media/nas/Media/a.jpg") + ); + } + + #[test] + fn uncovered_and_escaping_paths_are_none() { + let m = map(&[("/media/nas/", "Z:\\")]); + assert!(m.to_comfy(Path::new("/elsewhere/a.jpg")).is_none()); + assert!(m.to_comfy(Path::new("/media/nasty/a.jpg")).is_none()); + assert!(m.from_comfy("C:\\a.png").is_none()); + assert!(m.from_comfy("Z:\\..\\etc\\passwd").is_none()); + } + + #[test] + fn no_mappings_is_identity() { + let m = PathMap::default(); + assert_eq!(m.to_comfy(Path::new("/a/b.jpg")).unwrap(), "/a/b.jpg"); + assert_eq!(m.from_comfy("/a/b.png").unwrap(), PathBuf::from("/a/b.png")); + } +} diff --git a/src/enhance/service.rs b/src/enhance/service.rs new file mode 100644 index 0000000..5b4bd5f --- /dev/null +++ b/src/enhance/service.rs @@ -0,0 +1,733 @@ +//! Enhance orchestration (#192): submit, poll, approve, idle `/free`. +//! +//! The job row (`enhance_jobs`) is the only state that matters. ComfyUI is +//! allowed to be down (a normal state) and to forget everything when it +//! restarts (unapproved results are cleared), so every transition tolerates +//! both. + +use std::collections::HashMap; +use std::path::{Path, PathBuf}; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +use log::{info, warn}; +use serde::{Deserialize, Serialize}; + +use super::comfy::{ + ApproveError, ComfyClient, ComfyError, HistoryEntry, Staged, SubmitError, +}; +use super::config::{EnhanceConfig, Loaded, PromptMode}; +use super::path_map::PathMap; +use super::template::Template; +use crate::database::enhance_dao::{ + EnhanceJobDao, EnhanceJobRow, JobUpdate, STATUS_APPROVED, STATUS_EXPIRED, STATUS_FAILED, + STATUS_QUEUED, STATUS_READY, STATUS_RUNNING, STATUS_UNAVAILABLE, +}; + +/// Short timeout for the `available` probe, so the templates call stays snappy +/// when ComfyUI's host is off. +const PROBE_TIMEOUT: Duration = Duration::from_secs(2); +/// How often the idle check looks at ComfyUI. +const IDLE_CHECK_EVERY: Duration = Duration::from_secs(30); +/// Finished jobs are kept this long. +const JOB_RETENTION_SECS: i64 = 14 * 24 * 3600; +/// Jobs shown per photo. +pub const JOBS_PER_PHOTO: i64 = 20; + +pub const MSG_UNAVAILABLE: &str = "ComfyUI isn't running"; +pub const MSG_EXPIRED: &str = + "This result expired (ComfyUI was restarted). Run it again."; +pub const MSG_DISABLED: &str = "Enhance isn't configured"; + +/// A library file a request names. +#[derive(Debug, Clone)] +pub struct SourceFile { + pub library_id: i32, + pub rel_path: String, + pub abs: PathBuf, + pub hash: String, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum EnhanceError { + /// No `url` configured. + Disabled, + /// ComfyUI isn't reachable. + Unavailable, + UnknownTemplate(String), + BadRequest(String), + /// ComfyUI refused the workflow. + Rejected(String), + /// The result is gone (ComfyUI restarted). + Expired, + NotFound, + /// Approved, but ImageApi can't place the file in a library. + Unplaceable(String), + Upstream(String), + Internal(String), +} + +impl EnhanceError { + pub fn message(&self) -> String { + match self { + EnhanceError::Disabled => MSG_DISABLED.into(), + EnhanceError::Unavailable => MSG_UNAVAILABLE.into(), + EnhanceError::UnknownTemplate(t) => format!("unknown template {t:?}"), + EnhanceError::BadRequest(m) + | EnhanceError::Rejected(m) + | EnhanceError::Unplaceable(m) + | EnhanceError::Upstream(m) + | EnhanceError::Internal(m) => m.clone(), + EnhanceError::Expired => MSG_EXPIRED.into(), + EnhanceError::NotFound => "not found".into(), + } + } +} + +fn comfy_err(e: ComfyError) -> EnhanceError { + match e { + ComfyError::Unreachable(_) => EnhanceError::Unavailable, + other => EnhanceError::Upstream(other.to_string()), + } +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct SavedEntry { + pub n: usize, + pub comfy_path: String, + pub library_id: Option, + pub rel_path: Option, +} + +/// What the idle check decided. +#[derive(Debug, Default)] +pub struct IdleState { + initialised: bool, + marker: Option, + seen_at_ms: i64, + freed: bool, +} + +impl IdleState { + /// One observation of a reachable ComfyUI. `marker` identifies the + /// newest history entry (its last event time): when it changes, or the + /// queue isn't empty, or one of our jobs is active, ComfyUI is in use. + /// Returns true once per quiet spell when it has been idle for + /// `idle_ms` and `/free` should be called. + /// + /// The marker's *value* is ComfyUI's clock; only changes in it matter, + /// and "how long ago" is measured on ImageApi's clock, so clock skew + /// between the hosts can't cause an early unload. After ImageApi + /// (re)starts the first observation counts as activity. + pub fn observe( + &mut self, + now_ms: i64, + queue_empty: bool, + active_jobs: usize, + marker: Option, + idle_ms: i64, + ) -> bool { + if !self.initialised || marker != self.marker || !queue_empty || active_jobs > 0 { + self.initialised = true; + self.marker = marker; + self.seen_at_ms = now_ms; + self.freed = false; + return false; + } + if !self.freed && now_ms - self.seen_at_ms >= idle_ms { + self.freed = true; + return true; + } + false + } + + /// ComfyUI went away: the next observation starts a fresh baseline. + pub fn reset(&mut self) { + *self = IdleState::default(); + } + + /// `/free` failed; try again on the next check. + pub fn free_failed(&mut self) { + self.freed = false; + } +} + +pub struct EnhanceService { + pub config: EnhanceConfig, + templates: Vec