Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/en/tools-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,13 @@ Queue a Veo video or Imagen image generation job on `JobQueue`. Returns a `JobVi
| :--- | :--- | :--- | :--- |
| `prompt` | `string` | Yes | Text prompt that `flow-mcp` sends to Google Flow. |
| `mode` | `string` | No | `"video"` (default) or `"image"`. |
| `model` | `string` | No | Model override. Defaults to `"veo-3.1-fast"` (`video`) or `"nano-banana-2"` (`image`). |
| `aspect` | `string` | No | Aspect ratio: `"16:9"` (default), `"9:16"`, or `"1:1"`. |
| `model` | `string` | No | Model override. Defaults to `"Veo 3.1 Lite"` (`"Veo 3.1 Fast"` on legacy builds) for `video` or `"Nano Banana 2"` for `image`. |
| `aspect` | `string` | No | Aspect ratio: `"16:9"` (default), `"9:16"`, or `"1:1"` (`image` mode only). |
| `count` | `integer` | No | Output batch count in `1..=4`. Default `2`. |
| `frames` | `string[]` | No | Local reference or keyframe image paths to upload before submission. |
| `asset_ids` | `string[]` | No | Existing project image asset IDs to attach as ingredients or frames. |
| `characters` | `string[]` | No | Character IDs or names to attach to the prompt controls. |
| `video_type` | `string` | No | Video generation mode: `"frames"` or `"ingredients"`. |
| `characters` | `string[]` | No | Character IDs or names to attach in `"ingredients"` or `"image"` mode. |
| `video_type` | `string` | No | Video generation mode: `"ingredients"` (default) or `"frames"`. |
| `duration` | `string` | No | Video duration setting (for example `"5s"` or `"8s"`). |
| `resolution` | `string` | No | Video resolution setting (for example `"720p"`, `"1080p"`, or `"4K"`). |
| `name` | `string` | No | Optional scene or asset label for the job. |
Expand Down
8 changes: 4 additions & 4 deletions docs/ja/tools-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,13 @@ Google Flow でキャラクターボイスピッカーを開き、リスト全
| :--- | :--- | :--- | :--- |
| `prompt` | `string` | はい | `flow-mcp` が Google Flow へ送信するテキストプロンプト。 |
| `mode` | `string` | いいえ | `"video"`(既定)または `"image"`。 |
| `model` | `string` | いいえ | モデル上書き。既定は `"veo-3.1-fast"`(`video`)または `"nano-banana-2"`(`image`)。 |
| `aspect` | `string` | いいえ | アスペクト比:`"16:9"`(既定)、`"9:16"`、または `"1:1"`。 |
| `model` | `string` | いいえ | モデル上書き。既定は `video` で `"Veo 3.1 Lite"`(旧ビルドでは `"Veo 3.1 Fast"`)、`image` で `"Nano Banana 2"` です。 |
| `aspect` | `string` | いいえ | アスペクト比:`"16:9"`(既定)、`"9:16"`、または `"1:1"`(`image` モードのみ)。 |
| `count` | `integer` | いいえ | 出力バッチ数(`1..=4`)。既定は `2`。 |
| `frames` | `string[]` | いいえ | 送信前にアップロードするローカル参照またはキーフレーム画像パス。 |
| `asset_ids` | `string[]` | いいえ | 素材またはフレームとして添付する既存プロジェクト画像アセット ID。 |
| `characters` | `string[]` | いいえ | プロンプトコントロールへ添付するキャラクター ID または名前。 |
| `video_type` | `string` | いいえ | 動画生成モード:`"frames"` または `"ingredients"`。 |
| `characters` | `string[]` | いいえ | `"ingredients"` または `"image"` モードで添付するキャラクター ID または名前。 |
| `video_type` | `string` | いいえ | 動画生成モード:`"ingredients"`(既定)または `"frames"`。 |
| `duration` | `string` | いいえ | 動画尺設定(たとえば `"5s"` または `"8s"`)。 |
| `resolution` | `string` | いいえ | 動画解像度設定(たとえば `"720p"`、`"1080p"`、または `"4K"`)。 |
| `name` | `string` | いいえ | ジョブ用の任意のシーンまたはアセットラベル。 |
Expand Down
8 changes: 4 additions & 4 deletions docs/zh-CN/tools-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,13 @@ flow-mcp [OPTIONS] <COMMAND>
| :--- | :--- | :--- | :--- |
| `prompt` | `string` | 是 | `flow-mcp` 发送给 Google Flow 的文本提示词。 |
| `mode` | `string` | 否 | `"video"`(默认)或 `"image"`。 |
| `model` | `string` | 否 | 模型覆盖。默认为 `"veo-3.1-fast"`(`video`)或 `"nano-banana-2"`(`image`)。 |
| `aspect` | `string` | 否 | 画面比例:`"16:9"`(默认)、`"9:16"` 或 `"1:1"`。 |
| `model` | `string` | 否 | 模型覆盖。`video` 默认为 `"Veo 3.1 Lite"`(旧版构建为 `"Veo 3.1 Fast"`),`image` 默认为 `"Nano Banana 2"`。 |
| `aspect` | `string` | 否 | 画面比例:`"16:9"`(默认)、`"9:16"` 或 `"1:1"`(仅限 `image` 模式)。 |
| `count` | `integer` | 否 | 输出批次数量,范围 `1..=4`。默认 `2`。 |
| `frames` | `string[]` | 否 | 提交前上传的本地参考图像或关键帧路径。 |
| `asset_ids` | `string[]` | 否 | 作为素材或关键帧附加的既有项目图像资产 ID。 |
| `characters` | `string[]` | 否 | 附加到提示词控件的角色 ID 或名称。 |
| `video_type` | `string` | 否 | 视频生成模式:`"frames"` 或 `"ingredients"`。 |
| `characters` | `string[]` | 否 | 在 `"ingredients"` 或 `"image"` 模式下附加的角色 ID 或名称。 |
| `video_type` | `string` | 否 | 视频生成模式:`"ingredients"`(默认)或 `"frames"`。 |
| `duration` | `string` | 否 | 视频时长设置(例如 `"5s"` 或 `"8s"`)。 |
| `resolution` | `string` | 否 | 视频分辨率设置(例如 `"720p"`、`"1080p"` 或 `"4K"`)。 |
| `name` | `string` | 否 | 任务的可选场景或资产标签。 |
Expand Down
212 changes: 176 additions & 36 deletions src/generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ pub(crate) struct GenerateParams {
pub(crate) prompt: String,
/// Generation mode: "video" (default) or "image".
pub(crate) mode: Option<String>,
/// Model name override (defaults to "veo-3.1-fast" for video or "nano-banana-2" for image).
/// Model family override (defaults to "Veo 3.1 Lite", or "Veo 3.1 Fast" on legacy builds, for video and "Nano Banana 2" for image).
pub(crate) model: Option<String>,
/// Output aspect ratio: "16:9" (default), "9:16", or "1:1".
/// Output aspect ratio: "16:9" (default), "9:16", or "1:1" (image mode only).
pub(crate) aspect: Option<String>,
/// Output count from 1 to 4 (default 2).
#[schemars(range(min = 1, max = 4))]
Expand All @@ -55,7 +55,7 @@ pub(crate) struct GenerateParams {
pub(crate) asset_ids: Option<Vec<String>>,
/// Existing character IDs or names to attach to video or image generation.
pub(crate) characters: Option<Vec<String>>,
/// Video generation mode (`"frames"` or `"ingredients"`).
/// Video generation mode ("ingredients" default or "frames").
pub(crate) video_type: Option<String>,
/// Video duration setting (for example `"5s"` or `"8s"`).
pub(crate) duration: Option<String>,
Expand Down Expand Up @@ -87,35 +87,170 @@ struct AssetWaitSpec<'a> {
submit_summary: &'a str,
}

/// Context for `apply_generation_controls` distinguishing standard generation from edit flows.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SubmissionKind {
/// Standard `flow_generate` request.
Generate,
/// Image edit fallback on the project workspace when no edit view opens.
EditFallback,
/// Edit session already opened via `/edit/` or an inline tile edit button.
InlineEdit,
}

/// Validated and normalized control parameters sent to the browser generation runtime.
struct NormalizedControls {
/// Canonical mode string (`"video"` or `"image"`).
mode: &'static str,
/// Canonical aspect ratio label when explicitly provided or defaulted for generation.
aspect: Option<String>,
/// Trimmed explicit model family override when specified by the caller.
model: Option<String>,
/// Explicit video sub-mode (`Some("ingredients")` or `Some("frames")`) when supplied by the caller.
explicit_video_type: Option<&'static str>,
/// Non-empty trimmed project asset IDs to attach.
asset_ids: Option<Vec<String>>,
/// Non-empty trimmed character IDs or names to attach.
characters: Option<Vec<String>>,
/// Clamped batch output count in `1..=4`.
count: u32,
}

/// Trims items in `raw` and drops empty strings, returning `None` when none remain.
fn clean_string_list(raw: Option<&[String]>) -> Option<Vec<String>> {
let items: Vec<String> = raw
.unwrap_or(&[])
.iter()
.map(|item| item.trim())
.filter(|item| !item.is_empty())
.map(str::to_string)
.collect();
(!items.is_empty()).then_some(items)
}

/// Normalizes generation parameters and rejects invalid video aspect, character, and frame combinations.
fn resolve_generation_controls(
scene: &SceneParams,
kind: SubmissionKind,
) -> Result<NormalizedControls> {
let is_edit = kind != SubmissionKind::Generate;
let raw_mode = scene
.mode
.as_deref()
.map(str::trim)
.filter(|s| !s.is_empty())
.unwrap_or("video");
let mode = if raw_mode.eq_ignore_ascii_case("image")
|| raw_mode.eq_ignore_ascii_case("character")
|| raw_mode.eq_ignore_ascii_case("scene")
{
"image"
} else {
"video"
};
let explicit_aspect = scene
.aspect
.as_deref()
.map(str::trim)
.filter(|s| !s.is_empty());
let aspect = if is_edit {
explicit_aspect.map(str::to_string)
} else {
Some(explicit_aspect.unwrap_or("16:9").to_string())
};
if let Some(a) = aspect.as_deref()
&& mode == "video"
&& (a == "1:1" || a.eq_ignore_ascii_case("square"))
{
anyhow::bail!("Video mode does not support aspect ratio '{a}'; use '16:9' or '9:16'");
}
let explicit_video_type = if mode == "video" && !is_edit {
match scene
.video_type
.as_deref()
.map(str::trim)
.filter(|s| !s.is_empty())
{
None => None,
Some(vt)
if vt.eq_ignore_ascii_case("ingredients")
|| vt.eq_ignore_ascii_case("ingredient")
|| vt.eq_ignore_ascii_case("video_ingredients")
|| vt.eq_ignore_ascii_case("video_references") =>
{
Some("ingredients")
}
Some(vt)
if vt.eq_ignore_ascii_case("frames")
|| vt.eq_ignore_ascii_case("frame")
|| vt.eq_ignore_ascii_case("video_frames") =>
{
Some("frames")
}
Some(vt) => anyhow::bail!(
"Unsupported video_type '{vt}' for video mode; use 'ingredients' or 'frames'"
),
}
} else {
None
};
let effective_video_type = if mode == "video" && !is_edit {
Some(explicit_video_type.unwrap_or("ingredients"))
} else {
None
};
let asset_ids = clean_string_list(scene.asset_ids.as_deref());
let characters = clean_string_list(scene.characters.as_deref());
if effective_video_type == Some("frames") {
if characters.is_some() {
anyhow::bail!(
"Video frames mode does not support character references; use video_type 'ingredients'"
);
}
let ref_count =
scene.frames.as_ref().map_or(0, Vec::len) + asset_ids.as_ref().map_or(0, Vec::len);
anyhow::ensure!(
ref_count <= 2,
"Frame mode accepts at most two references, got {ref_count}"
);
}
let model = scene
.model
.as_deref()
.map(str::trim)
.filter(|s| !s.is_empty())
.map(str::to_string);
Ok(NormalizedControls {
mode,
aspect,
model,
explicit_video_type,
asset_ids,
characters,
count: scene.count.unwrap_or(2).clamp(1, 4),
})
}

/// Configures generation controls, uploads reference frames, writes prompt text, and submits.
async fn apply_generation_controls(
engine: &ChromeEngine,
scene: &SceneParams,
kind: SubmissionKind,
) -> Result<serde_json::Value> {
let controls = resolve_generation_controls(scene, kind)?;
validate_local_files(scene.frames.as_deref(), "Reference image").await?;

let raw_mode = scene.mode.as_deref().unwrap_or("video");
let mode = match raw_mode {
"image" | "character" | "scene" => "image",
_ => "video",
};
let aspect = scene.aspect.as_deref().unwrap_or("16:9");
let model = scene.model.as_deref().unwrap_or(if mode == "image" {
"nano-banana-2"
} else {
"veo-3.1-fast"
});
let count = scene.count.unwrap_or(2).clamp(1, 4);

let cfg = json!({
"mode": mode,
"aspect": aspect,
"model": model,
"count": count,
"mode": controls.mode,
"aspect": controls.aspect,
"model": controls.model,
"edit": kind != SubmissionKind::Generate,
"inline_edit": kind == SubmissionKind::InlineEdit,
"count": controls.count,
"prompt": scene.prompt,
"asset_ids": scene.asset_ids,
"characters": scene.characters,
"video_type": scene.video_type,
"asset_ids": controls.asset_ids,
"characters": controls.characters,
"video_type": controls.explicit_video_type,
"duration": scene.duration,
"resolution": scene.resolution,
});
Expand All @@ -134,7 +269,7 @@ async fn apply_generation_controls(
}

let res = engine
.call_flow_async("attachIngredients", json!([cfg]))
.call_flow_async("attachIngredients", json!([cfg.clone()]))
.await?;
if res.get("ok").and_then(|v| v.as_bool()) != Some(true) {
anyhow::bail!(
Expand Down Expand Up @@ -195,13 +330,16 @@ async fn apply_generation_controls(
}

let mut submit_res = engine
.call_flow_async("submitGeneration", json!([]))
.call_flow_async("submitGeneration", json!([cfg]))
.await?;
if submit_res.get("ok").and_then(|v| v.as_bool()) != Some(true) {
let err = submit_res
.get("error")
.and_then(|v| v.as_str())
.unwrap_or("unknown");
if matches!(err, "unsupported_model" | "unsupported_aspect_ratio") {
anyhow::bail!("Failed to configure Flow generation UI: {err}");
}
let detail = submit_res
.get("detail")
.and_then(|v| v.as_str())
Expand Down Expand Up @@ -525,17 +663,14 @@ pub(crate) async fn run_generation(
scene: &SceneParams,
on_progress: ProgressCallback,
) -> Result<(Vec<String>, Vec<String>, Vec<String>)> {
let controls = resolve_generation_controls(scene, SubmissionKind::Generate)?;
ensure_project_workspace_root(engine).await?;
ensure_flow_store(engine).await?;
if scene.video_type.as_deref() == Some("frames") {
let count = scene.frames.as_ref().map_or(0, Vec::len)
+ scene.asset_ids.as_ref().map_or(0, Vec::len)
+ scene.characters.as_ref().map_or(0, Vec::len);
anyhow::ensure!(
count <= 2,
"Frame mode accepts at most two references, got {count}"
);
}
let mut normalized_scene = scene.clone();
normalized_scene.video_type = controls.explicit_video_type.map(str::to_string);
normalized_scene.asset_ids = controls.asset_ids;
normalized_scene.characters = controls.characters;
let scene = &normalized_scene;
// Upload local frames into the library first and attach them as assets, so that the
// uploaded tiles are not counted as generated outputs.
let uploaded_scene;
Expand Down Expand Up @@ -565,7 +700,7 @@ pub(crate) async fn run_generation(
let expected_count = scene.count.unwrap_or(2).clamp(1, 4) as usize;

let mut net_rx = engine.subscribe_network().await?;
let submit_res = apply_generation_controls(engine, scene).await?;
let submit_res = apply_generation_controls(engine, scene, SubmissionKind::Generate).await?;
let submit_summary = format_submit_progress("submitted generation", &submit_res);
on_progress(submit_summary.clone());

Expand Down Expand Up @@ -691,8 +826,13 @@ pub(crate) async fn run_edit(
count: Some(1),
..Default::default()
};
let edit_kind = if opened_in_ui || in_edit_route {
SubmissionKind::InlineEdit
} else {
SubmissionKind::EditFallback
};
let mut net_rx = engine.subscribe_network().await?;
let submit_res = apply_generation_controls(engine, &scene).await?;
let submit_res = apply_generation_controls(engine, &scene, edit_kind).await?;
let submit_summary = format_submit_progress("submitted edit", &submit_res);
on_progress(submit_summary.clone());

Expand Down
Loading
Loading