use anyhow::{anyhow, Result};
use std::path::Path;
#[cfg(feature = "comfy-renderer")]
use crate::backend::ComfyContext;
#[cfg(feature = "comfy-renderer")]
use crate::services::comfy::{ValidatedWorkflowTemplate, WorkflowLoader};
#[cfg(feature = "python-renderer")]
use crate::backend::PythonContext;
use crate::{
backend::{self, BuildContext, RuntimeBackend},
config::model::AppConfig,
RuntimeInput, RuntimeResult, SdxlBackend,
};
pub fn run_backend(
config: &AppConfig,
profile_name: &str,
input: &RuntimeInput,
output_dir: &Path,
) -> Result<RuntimeResult> {
let profile = config
.profile(profile_name)
.ok_or_else(|| anyhow!("Profile '{}' not found", profile_name))?;
let backend_def = config
.backend(&profile.backend_name)
.ok_or_else(|| anyhow!("Backend '{}' not found", profile.backend_name))?;
if !backend_def.enabled {
anyhow::bail!("Backend '{}' is disabled", profile.backend_name);
}
let builder = BuildContext::builder();
let builder = match &backend_def.options {
#[cfg(feature = "comfy-renderer")]
crate::config::model::BackendOptions::ComfyUi(cfg) => {
let raw = WorkflowLoader::load(&cfg.workflow_template_path)
.map_err(|e| anyhow!("Failed to load workflow: {}", e))?;
let workflow = ValidatedWorkflowTemplate::new(raw)
.map_err(|e| anyhow!("Invalid workflow: {}", e))?;
let runtime_handle = tokio::runtime::Handle::current();
let comfy_ctx = ComfyContext::new(workflow, runtime_handle);
builder.with_comfy(comfy_ctx)
}
#[cfg(feature = "python-renderer")]
crate::config::model::BackendOptions::Python(cfg) => {
let interpreter = cfg.python_path.clone();
let script_root = cfg.script_path.parent().map(|p| p.to_path_buf());
let environment = None;
let python_ctx = PythonContext::new(interpreter, script_root, environment);
builder.with_python(python_ctx)
}
_ => builder,
};
let ctx = builder.build();
let renderer = backend::build_renderer(backend_def, &ctx)?;
let backend = SdxlBackend::new(renderer);
backend.execute(input, output_dir)
}