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
40 changes: 37 additions & 3 deletions flow-agent/flow_agent/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, StreamingResponse
from pydantic import BaseModel, ConfigDict, Field
from pydantic import BaseModel, ConfigDict, Field, field_validator

from .engine import EngineError, FlowEngine

Expand Down Expand Up @@ -75,23 +75,49 @@ class ImageGenerationRequest(BaseModel):
model_config = ConfigDict(extra="allow")

prompt: str
model: str = "narwhal"
model: str = "beluga"
n: int = Field(default=1, ge=1, le=4)
size: str = "1024x1024"
response_format: str = "url"
# Flow-specific, ignored by OpenAI clients.
all_accounts: bool = False
cookies: Optional[str] = None

@field_validator("model", mode="before")
@classmethod
def sanitize_model(cls, v: Any) -> str:
# Fallback to BELUGA if empty or set to legacy/unsupported models or placeholders
val = str(v).strip().lower() if v else "beluga"
if val in ("narwhal", "string", "default", "none", "null", ""):
return "beluga"
return val


class FlowImageRequest(BaseModel):
prompt: str
aspect: str = "1:1"
count: int = Field(default=1, ge=1, le=4)
model: str = "narwhal"
model: str = "beluga"
all_accounts: bool = False
cookies: Optional[str] = None

@field_validator("model", mode="before")
@classmethod
def sanitize_model(cls, v: Any) -> str:
# Fallback to BELUGA if empty or set to legacy/unsupported models or placeholders
val = str(v).strip().lower() if v else "beluga"
if val in ("narwhal", "string", "default", "none", "null", ""):
return "beluga"
return val

@field_validator("cookies", mode="before")
@classmethod
def sanitize_cookies(cls, v: Any) -> Optional[str]:
# Filter out Swagger UI default placeholder ('string') and empty values
if not v or str(v).strip().lower() in ("string", "null", "none", "undefined", '""', "''"):
return None
return str(v).strip()


class FlowVideoRequest(BaseModel):
prompt: str
Expand All @@ -103,6 +129,14 @@ class FlowVideoRequest(BaseModel):
all_accounts: bool = False
cookies: Optional[str] = None

@field_validator("cookies", mode="before")
@classmethod
def sanitize_cookies(cls, v: Any) -> Optional[str]:
# Filter out Swagger UI default placeholder ('string') and empty values
if not v or str(v).strip().lower() in ("string", "null", "none", "undefined", '""', "''"):
return None
return str(v).strip()


class ChatMessage(BaseModel):
model_config = ConfigDict(extra="allow")
Expand Down
39 changes: 30 additions & 9 deletions flow-agent/flow_agent/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
DEFAULT_TIMEOUT = 900


def _is_valid_cookie_arg(val: Optional[str]) -> bool:
"""Filter out dummy placeholders (e.g. 'string', 'null') and empty values sent by Swagger UI or clients."""
if not val:
return False
clean = str(val).strip()
return bool(clean and clean.lower() not in ("string", "null", "none", "undefined", '""', "''"))


class EngineError(RuntimeError):
"""A Flow engine invocation that did not succeed.

Expand Down Expand Up @@ -112,15 +120,25 @@ def _find_binary(root: Path, explicit: Optional[str | Path] = None) -> Path:

import platform
system = platform.system().lower()
machine = platform.machine().lower()
bin_dir = root / "bin"

is_arm = machine in ("aarch64", "arm64", "armv8l", "armv8")

# Platform priority candidates
if "windows" in system:
candidates = ["flow-windows.exe", "flow.exe", "flow"]
elif "darwin" in system:
candidates = ["flow-macos", "flow", "flow-darwin"]
candidates = (
["flow-macos-arm64", "flow-macos", "flow", "flow-darwin"]
if is_arm
else ["flow-macos", "flow", "flow-darwin"]
)
else: # Linux / Unix
candidates = ["flow-linux", "flow", "flow-x86_64"]
if is_arm:
candidates = ["flow-linux-arm64", "flow-arm64", "flow-linux", "flow"]
else:
candidates = ["flow-linux-amd64", "flow-linux", "flow", "flow-x86_64"]

for name in candidates:
candidate_path = bin_dir / name
Expand Down Expand Up @@ -218,7 +236,7 @@ async def generate_image(
prompt: str,
aspect: str = "1:1",
count: int = 1,
model: str = "narwhal",
model: str = "beluga",
all_accounts: bool = False,
cookies: Optional[str] = None,
download: bool = True,
Expand All @@ -235,14 +253,17 @@ async def generate_image(
which happens with `download=False` and on a refusal alike, so a caller
that needs the bytes should read it and not assume it.
"""
# Fallback to BELUGA if empty or set to legacy/unsupported models (e.g. narwhal) or dummy placeholders
effective_model = "beluga" if (not model or model.lower() in ("narwhal", "string", "default", "none", "null")) else model

args: list[Any] = ["image", prompt, aspect]
if count and count > 1:
args.append(f"x{count}")
if model:
args += ["--model", model]
if effective_model:
args += ["--model", effective_model]
if all_accounts:
args.append("--all")
if cookies:
if _is_valid_cookie_arg(cookies):
args += ["--cookies", cookies]
if not download:
args.append("--no-download")
Expand Down Expand Up @@ -275,7 +296,7 @@ async def generate_video(
args += ["--start-image", start_image]
if all_accounts:
args.append("--all")
if cookies:
if _is_valid_cookie_arg(cookies):
args += ["--cookies", cookies]
if not download:
args.append("--no-download")
Expand All @@ -298,7 +319,7 @@ async def upload_video(
args: list[Any] = ["upload-video", file_path]
if force:
args.append("--force-upload")
if cookies:
if _is_valid_cookie_arg(cookies):
args += ["--cookies", cookies]

return await self._run_json(*args, timeout=timeout)
Expand Down Expand Up @@ -328,7 +349,7 @@ async def get_projects(self, cookies: Optional[str] = None) -> list[dict]:
not an object wrapping one.
"""
args: list[Any] = ["projects", "--json"]
if cookies:
if _is_valid_cookie_arg(cookies):
args += ["--cookies", cookies]
out, _ = await self._run(*args)
try:
Expand Down