vc-runtime 0.1.0

Runtime backend for Visual Compiler
Documentation
use crate::{
    renderer::{ImageRenderer, RenderParams},
    BackendKind, RuntimeBackend, RuntimeInput, RuntimeResult, RuntimeStatus,
    RUNTIME_SCHEMA_VERSION,
};
use anyhow::Result;
use std::path::Path;

pub struct SdxlBackend {
    renderer: Box<dyn ImageRenderer>,
    width: u32,
    height: u32,
    steps: u32,
    cfg: f64,
    sampler: String,
    model: String,
    negative_prompt: String,
}

impl SdxlBackend {
    /// Create a new SDXL backend with the given renderer.
    pub fn new(renderer: Box<dyn ImageRenderer>) -> Self {
        Self {
            renderer,
            width: 1024,
            height: 1024,
            steps: 30,
            cfg: 7.0,
            sampler: "DPM++ 2M Karras".to_string(),
            model: "sdxl-base-1.0".to_string(),
            negative_prompt: String::new(),
        }
    }

    pub fn with_negative_prompt(mut self, negative: impl Into<String>) -> Self {
        self.negative_prompt = negative.into();
        self
    }

    pub fn with_width(mut self, width: u32) -> Self {
        self.width = width;
        self
    }

    pub fn with_height(mut self, height: u32) -> Self {
        self.height = height;
        self
    }

    pub fn with_steps(mut self, steps: u32) -> Self {
        self.steps = steps;
        self
    }

    pub fn with_cfg(mut self, cfg: f64) -> Self {
        self.cfg = cfg;
        self
    }

    pub fn with_sampler(mut self, sampler: impl Into<String>) -> Self {
        self.sampler = sampler.into();
        self
    }

    pub fn with_model(mut self, model: impl Into<String>) -> Self {
        self.model = model.into();
        self
    }
}

impl RuntimeBackend for SdxlBackend {
    fn execute(&self, input: &RuntimeInput, output_dir: &Path) -> Result<RuntimeResult> {
        let compiler_output = &input.vc_output;
        let metadata = &compiler_output.metadata;
        let prompt = &compiler_output.prompt;

        let prompt_text = prompt.character.descriptors.join(", ");

        let seed_bytes = metadata.prompt_hash.as_bytes();
        let seed = u64::from_be_bytes(seed_bytes[0..8].try_into().unwrap_or([0u8; 8]));

        let params = RenderParams {
            width: self.width,
            height: self.height,
            steps: self.steps,
            cfg: self.cfg,
            backend_options: serde_json::Value::Object(serde_json::Map::new()),
        };

        let render_output =
            self.renderer.render(&prompt_text, &self.negative_prompt, seed, output_dir, &params)?;

        Ok(RuntimeResult {
            runtime_schema_version: RUNTIME_SCHEMA_VERSION,
            status: RuntimeStatus::Success,
            backend: BackendKind::Sdxl,
            compiler_schema_version: compiler_output.schema_version,
            seed,
            image_path: Some(render_output.image_path),
            image_hash: Some(render_output.image_hash.to_hex()),
            input_prompt_hash: metadata.prompt_hash.to_hex(),
            model: Some(self.model.clone()),
            width: self.width,
            height: self.height,
            steps: self.steps,
            cfg: self.cfg,
            sampler: self.sampler.clone(),
            started_at: None,
            finished_at: None,
            duration_ms: Some(render_output.duration_ms),
        })
    }
}