Skip to content
Open
180 changes: 180 additions & 0 deletions src/edit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// Copyright 2026 ByteLand Technology Limited
// SPDX-License-Identifier: MIT

//! Google Flow asset editing workflow.
//!
//! Opens the asset editor through the browser runtime, submits edit prompts, and tracks updated
//! outputs.

use anyhow::{Context, Result};
use schemars::JsonSchema;
use serde::Deserialize;
use serde_json::json;
use std::collections::HashSet;
use tokio::time::Duration;

use crate::assets::{list_assets_with_project, match_asset};
use crate::db::FlowDb;
use crate::engine::ChromeEngine;
use crate::generation::{
AssetWaitSpec, WatchedAsset, apply_generation_controls, collect_created_assets,
format_submit_progress, wait_for_new_assets,
};
use crate::project::{ensure_flow_store, ensure_project_workspace_root};
use crate::queue::{ProgressCallback, SceneParams};

/// Parameters for `flow_edit`.
#[derive(Debug, Clone, Deserialize, JsonSchema)]
pub(crate) struct EditParams {
/// Case-insensitive asset ID, name, or prompt substring that selects the source asset.
#[serde(rename = "match")]
pub(crate) r#match: String,
/// Edit prompt for the matched asset.
pub(crate) prompt: String,
}

/// Exits any editor subroute and returns an error when `openEditAsset` reports a blocking alert.
async fn check_open_edit_errors(
engine: &ChromeEngine,
open_res: &serde_json::Value,
label: &str,
) -> Result<()> {
if let Some(alert) = open_res
.get("alert")
.and_then(|v| v.as_str())
.filter(|s| !s.is_empty())
{
let _ = ensure_project_workspace_root(engine).await;
anyhow::bail!("Flow cannot edit '{}': {}", label, alert);
}
Ok(())
}

/// Opens an edit flow for an asset that matches `match_str`, submits `prompt`, and downloads/indexes outputs.
pub(crate) async fn run_edit(
engine: &ChromeEngine,
db: &FlowDb,
match_str: &str,
prompt: &str,
on_progress: ProgressCallback,
) -> Result<(Vec<String>, Vec<String>, Vec<String>)> {
ensure_project_workspace_root(engine).await?;
let snap = list_assets_with_project(engine).await?;
let target = match_asset(&snap.assets, match_str, true)
.cloned()
.with_context(|| format!("No asset matches '{}' for edit", match_str))?;
match target.kind.as_str() {
"scene" if target.duration.is_none() => anyhow::bail!(
"Cannot edit scene '{}' without a duration; pass a video asset ID to flow_edit.",
target.id
),
"character" => anyhow::bail!(
"Cannot edit character '{}' with flow_edit; use flow_character_edit instead.",
target.id
),
"audio" => anyhow::bail!("Cannot edit audio asset '{}' with flow_edit.", target.id),
_ => {}
}
let before: HashSet<String> = snap.assets.into_iter().map(|a| a.id).collect();

let label = if target.name.is_empty() {
target.id.clone()
} else {
target.name.clone()
};
on_progress(format!("preparing edit for {}", label));

let edit_mode =
if target.kind == "video" || (target.kind == "scene" && target.duration.is_some()) {
"video"
} else {
"image"
};

let open_args = json!([target.id, target.url, target.primary_media_id]);
let mut open_res = engine
.call_flow_async("openEditAsset", open_args.clone())
.await
.unwrap_or_default();
check_open_edit_errors(engine, &open_res, &label).await?;
let is_editor_open = |res: &serde_json::Value| {
res.get("opened").and_then(|v| v.as_bool()) == Some(true)
&& res.get("has_textbox").and_then(|v| v.as_bool()) == Some(true)
};
let mut opened_in_ui = is_editor_open(&open_res);

if !opened_in_ui && let Some(edit_url) = open_res.get("edit_url").and_then(|v| v.as_str()) {
engine.navigate(edit_url).await?;
tokio::time::sleep(Duration::from_millis(1200)).await;
let _ = ensure_flow_store(engine).await;
open_res = engine
.call_flow_async("openEditAsset", open_args)
.await
.unwrap_or_default();
check_open_edit_errors(engine, &open_res, &label).await?;
opened_in_ui = is_editor_open(&open_res);
} else if opened_in_ui {
let _ = ensure_flow_store(engine).await;
}

if !opened_in_ui {
let _ = ensure_project_workspace_root(engine).await;
anyhow::bail!(
"Could not open an edit prompt box for {} asset '{}'",
edit_mode,
label
);
}

let cur_url = engine.current_url().await.unwrap_or_default();
let in_edit_route = cur_url.contains("/edit/") || cur_url.contains("/scene/");
on_progress(format!("submitting edit for {}", label));
let scene = SceneParams {
prompt: prompt.to_string(),
mode: if in_edit_route {
None
} else {
Some(edit_mode.to_string())
},
frames: None,
count: Some(1),
..Default::default()
};
let mut net_rx = engine.subscribe_network().await?;
let submit_res = apply_generation_controls(engine, &scene).await?;
let submit_summary = format_submit_progress("submitted edit", &submit_res);
on_progress(submit_summary.clone());

let target_was_failed = matches!(target.media_status.as_deref(), Some("failed" | "canceled"));
let (created, project_id, project_title) = wait_for_new_assets(
engine,
AssetWaitSpec {
before_ids: &before,
watch_updated: Some(WatchedAsset {
id: target.id.as_str(),
primary_media_id: target.primary_media_id.as_deref(),
url: target.url.as_deref(),
prev_media_error: target.media_error.as_deref(),
prev_created_at: target.created_at.as_deref(),
was_failed: target_was_failed,
}),
expected_count: 1,
timeout_secs: 420,
submit_summary: &submit_summary,
},
&mut net_rx,
&on_progress,
)
.await?;
ensure_project_workspace_root(engine).await?;
Ok(collect_created_assets(
engine,
db,
created,
project_id,
project_title,
true,
&on_progress,
)
.await)
}
Loading
Loading