AI media harness for planning, generating, and editing locally
TypeScript
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub(crate) struct ImageGenerationSnapshot {
pub scene: ImageScene,
pub resolution: String,
pub aspect_ratio: String,
pub default_look: Option<String>,
pub brief_prompt: String,
pub prompt: String,
pub generator_template_id: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub used_seed: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub template: Option<ImageTemplateSnapshot>,
pub references: Vec<super::references::ReusableReference>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub(crate) struct ImageTemplateSnapshot {
pub kind: String,
pub source_id: String,
pub source_name: String,
pub generator_name: String,
pub prompt: String,
pub steps: u32,
pub seed: i64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub bounds: Option<ImageBox>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub height: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub views: Option<CharacterSheetViews>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub(crate) struct CharacterSheetViews {
pub close_up: bool,
pub front: bool,
pub side: bool,
pub back: bool,
}
impl ImageTemplateSnapshot {
pub(crate) fn validate(&self) -> Result<(), String> {
let valid_options = match self.kind.as_str() {
"extend" => self.views.is_none() && self.height.is_none() && self.bounds.as_ref().is_some_and(|b|
[b.x, b.y, b.width, b.height].iter().all(|v| v.is_finite()) && b.width > 0.0 && b.height > 0.0),
"character-sheet" => self.bounds.is_none() && self.height.is_some_and(|height| (512..=3424).contains(&height) && height % 32 == 0)
&& self.views.as_ref().is_none_or(|views| views.close_up || views.front || views.side || views.back),
_ => false,
};
if !valid_options || self.source_id.is_empty() || self.source_name.is_empty() || self.generator_name.is_empty()
|| !(2..=1000).contains(&self.steps) || !(-1..=9_007_199_254_740_991).contains(&self.seed) {
return Err("Invalid image template snapshot settings.".into());
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ImageScene {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub negative_prompts: Option<std::collections::BTreeMap<String, String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cfg_scales: Option<std::collections::BTreeMap<String, f64>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub generation_effects: Option<slopus_core::generation_effects::GenerationEffects>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub root_type: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub source_image: Option<ImageSource>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub output_size: Option<ImageOutputSize>,
pub nodes: Vec<ImageNode>,
pub background: String,
pub style: ImageStyle,
pub steps: u32,
pub seed: i64,
pub reference_ids: Vec<String>,
pub output_asset_id: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ImageSource {
pub relative_path: String,
pub name: String,
pub width: u32,
pub height: u32,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ImageOutputSize {
pub width: u32,
pub height: u32,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ImageNode {
pub id: String,
pub parent_id: Option<String>,
pub kind: String,
pub name: String,
pub description: String,
pub text: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reference_ids: Option<Vec<String>>,
pub r#box: Option<ImageBox>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub lasso: Option<Vec<slopus_core::image_mask::MaskPoint>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub lasso_contours: Option<Vec<Vec<slopus_core::image_mask::MaskPoint>>>,
pub colors: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ImageBox {
pub x: f64,
pub y: f64,
pub width: f64,
pub height: f64,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ImageStyle {
pub mode: String,
pub aesthetics: String,
pub lighting: String,
pub medium: String,
pub detail: String,
}
impl ImageScene {
/// Every reference the scene's prompts cite as `@[ref:<id>]` — node
/// descriptions and the background — which is what generation is given.
pub(crate) fn cited_reference_ids(&self) -> Result<Vec<&str>, String> {
let mut ids = Vec::new();
for text in self.nodes.iter().map(|node| node.description.as_str()).chain([self.background.as_str()]) {
for id in super::reference_tokens::action_reference_ids(text)? {
if !ids.contains(&id) {
ids.push(id);
}
}
}
Ok(ids)
}
pub fn validate(&self) -> Result<(), String> {
if let Some(scales) = &self.cfg_scales {
for (model, scale) in scales {
if model.is_empty() { return Err("CFG settings require a model ID.".into()); }
slopus_core::models::validate_cfg_scale(*scale)?;
}
}
if let Some(effects) = &self.generation_effects { effects.validate()?; }
if self.output_size.as_ref().is_some_and(|size| [size.width, size.height].iter().any(|value|
!(32..=8192).contains(value) || value % 32 != 0)) {
return Err("Image output dimensions must be multiples of 32 between 32 and 8192 pixels.".into());
}
if self.root_type.as_deref().is_some_and(|kind| !matches!(kind, "prompt" | "image")) {
return Err("Invalid image root type.".into());
}
if let Some(source) = &self.source_image {
crate::project::paths::normalize_project_path(&source.relative_path)?;
if source.name.is_empty() || !(1..=16_384).contains(&source.width) || !(1..=16_384).contains(&source.height) {
return Err("Source image dimensions must be between 1 and 16384 pixels.".into());
}
}
if self.nodes.is_empty()
|| self.nodes.len() > 500
|| !(2..=1000).contains(&self.steps)
|| !(-1..=9_007_199_254_740_991).contains(&self.seed)
|| !matches!(self.style.mode.as_str(), "none" | "photo" | "art")
|| self.reference_ids.len() > 100
|| self.reference_ids.iter().any(String::is_empty)
|| self.output_asset_id.as_ref().is_some_and(String::is_empty)
{
return Err("Invalid image generation settings.".into());
}
let nodes: HashMap<_, _> = self
.nodes
.iter()
.map(|node| (node.id.as_str(), node))
.collect();
let roots: Vec<_> = self
.nodes
.iter()
.filter(|node| node.kind == "root")
.collect();
if nodes.len() != self.nodes.len() || roots.len() != 1 || roots[0].parent_id.is_some() {
return Err("Image nodes must form one connected tree with unique IDs.".into());
}
for node in &self.nodes {
if node.id.is_empty()
|| node.name.is_empty()
|| node.name.chars().count() > 120
|| !matches!(
node.kind.as_str(),
"root" | "group" | "object" | "text" | "background"
)
|| node.colors.len() > 16
|| node.reference_ids.as_ref().is_some_and(|ids| ids.len() > 100 || ids.iter().any(String::is_empty))
|| node.colors.iter().any(|color| {
color.len() != 7
|| !color.starts_with('#')
|| !color[1..].bytes().all(|c| c.is_ascii_hexdigit())
})
{
return Err("Invalid image node.".into());
}
if node.lasso_contours.is_some() && node.lasso.is_none() { return Err("Lasso contours require an outline.".into()); }
if let Some(points) = &node.lasso {
if node.kind != "object" || node.r#box.is_none() { return Err("Lasso regions require an edit node and placement box.".into()); }
let mut contours = vec![points.clone()];
if let Some(additional) = &node.lasso_contours {
if additional.is_empty() { return Err("Additional lasso contours must not be empty.".into()); }
contours.extend(additional.iter().cloned());
}
slopus_core::image_mask::validate_contours(&contours, 1000, 1000)?;
}
if let Some(b) = &node.r#box {
if ![b.x, b.y, b.width, b.height].iter().all(|v| v.is_finite())
|| b.x < 0.0
|| b.y < 0.0
|| b.x > 999.0
|| b.y > 999.0
|| b.width <= 0.0
|| b.height <= 0.0
|| b.x + b.width > 1000.0
|| b.y + b.height > 1000.0
{
return Err("Image placement must stay inside the image.".into());
}
}
let mut seen = HashSet::from([node.id.as_str()]);
let mut parent = node.parent_id.as_deref();
while let Some(id) = parent {
if !seen.insert(id) {
return Err("Image hierarchy contains a cycle.".into());
}
parent = nodes
.get(id)
.ok_or("Image parent does not exist.")?
.parent_id
.as_deref();
}
if !seen.contains(roots[0].id.as_str()) {
return Err("Image node is disconnected from the root.".into());
}
}
Ok(())
}
}