The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.
TypeScript ai-videocomfyuicreative-toolsgenerative-aiideogramimage-generationimage-to-3dimage-to-videoltx-videominimax-h3nextjsopen-sourceopen-weightsqwen-imagetext-to-imagevideo-generationwan
Fix Higgsfield fake outputs (S1) and extra-key prototype pollution (S2); structured provider outputs
Claude7b0f437fd6fd
diff --git a/src/lib/catalog/payload.ts b/src/lib/catalog/payload.tsindex a73e1f60..a074a41b 100644--- a/src/lib/catalog/payload.ts+++ b/src/lib/catalog/payload.ts@@ -1,18 +1,70 @@-import type { EnumField, FieldMap, GenInput, MediaKind } from "./types";+import type { DurationField, EnumField, FieldMap, GenInput, MediaKind, Operation } from "./types";function enumValue(field: string | EnumField, value: string): [string, unknown] {if (typeof field === "string") return [field, value];return [field.key, field.values && value in field.values ? field.values[value] : value];}-/** Write `a.b.c` as nested objects (MiniMax's voice_setting.voice_id etc.). */+/** Path segments that would reach Object.prototype (or a constructor) instead of the payload. */+const UNSAFE_KEYS = new Set(["__proto__", "constructor", "prototype"]);++/**+ * Write `a.b.c` as nested objects (MiniMax's voice_setting.voice_id etc.). Keys come from users+ * (params.extra), so prototype-reaching segments are dropped and only own plain objects are descended into.+ */function setPath(out: Record<string, unknown>, key: string, value: unknown) {const parts = key.split(".");+ if (parts.some((p) => !p || UNSAFE_KEYS.has(p))) return;let o = out;- for (const k of parts.slice(0, -1)) o = (o[k] ??= {}) as Record<string, unknown>;+ for (const k of parts.slice(0, -1)) {+ const next = Object.hasOwn(o, k) ? o[k] : undefined;+ if (next === null || typeof next !== "object" || Array.isArray(next)) o[k] = {};+ o = o[k] as Record<string, unknown>;+ }o[parts[parts.length - 1]] = value;}+/** A number of seconds in the API's encoding (seconds or ms, number or "5s"). */+function timeValue(d: DurationField, seconds: number): number | string {+ const v = seconds * (d.multiplier ?? 1);+ return d.type === "number" ? v : `${v}${d.suffix ?? ""}`;+}++/** "#1a2b3c" / "1A2B3C" → {r, g, b}; null for anything else. */+export function hexToRgb(hex: string): { r: number; g: number; b: number } | null {+ const m = /^#?([0-9a-f]{6})$/i.exec(hex.trim());+ if (!m) return null;+ const n = parseInt(m[1], 16);+ return { r: (n >> 16) & 255, g: (n >> 8) & 255, b: n & 255 };+}++type PaletteFormat = NonNullable<FieldMap["palette"]>["format"];++function colorValue(format: PaletteFormat, rgb: { r: number; g: number; b: number }, hex: string): unknown {+ if (format === "rgb-object") return rgb;+ if (format === "rgb-array") return { rgb: [rgb.r, rgb.g, rgb.b] };+ if (format === "ideogram") return { rgb };+ return `#${hex.replace(/^#/, "").toUpperCase()}`;+}++/**+ * A hex palette in the API's own shape. Ideogram weighs members: earlier colours (the dominant ones+ * in an extracted palette) count more, from 1 down to 0.3.+ */+export function paletteValue(format: PaletteFormat, palette: string[]): unknown {+ const colors = palette.flatMap((hex) => {+ const rgb = hexToRgb(hex);+ return rgb ? [{ hex, rgb }] : [];+ });+ if (format === "ideogram") {+ const n = colors.length;+ return {+ members: colors.map((c, i) => ({ rgb: c.rgb, color_weight: n > 1 ? Math.round((1 - (0.7 * i) / (n - 1)) * 100) / 100 : 1 })),+ };+ }+ return colors.map((c) => colorValue(format, c.rgb, c.hex));+}+/** Build a provider payload from canonical input using a route's {@link FieldMap}. Pure + unit-tested. */export function buildPayload(map: FieldMap, input: GenInput): Record<string, unknown> {const out: Record<string, unknown> = {};@@ -22,6 +74,7 @@ export function buildPayload(map: FieldMap, input: GenInput): Record<string, unkconst images = input.images ?? [];if (map.images && images.length) out[map.images] = images;if (map.image && images.length) out[map.image] = images[0];+ if (map.image2 && images.length > 1) out[map.image2] = images[1];if (map.endImage && input.endImage) out[map.endImage] = input.endImage;if (map.mask && input.mask) out[map.mask] = input.mask;if (map.video && input.video) out[map.video] = input.video;@@ -37,11 +90,7 @@ export function buildPayload(map: FieldMap, input: GenInput): Record<string, unkconst [k, v] = enumValue(map.resolution, input.resolution);out[k] = v;}- if (map.duration && input.duration !== undefined) {- const d = map.duration;- const v = input.duration * (d.multiplier ?? 1);- out[d.key] = d.type === "number" ? v : `${v}${d.suffix ?? ""}`;- }+ if (map.duration && input.duration !== undefined) out[map.duration.key] = timeValue(map.duration, input.duration);if (map.numOutputs && input.numOutputs !== undefined) out[map.numOutputs] = input.numOutputs;if (map.seed && input.seed !== undefined && input.seed >= 0) {const r = map.seedRange;@@ -75,19 +124,36 @@ export function buildPayload(map: FieldMap, input: GenInput): Record<string, unkout[d.key] = input.dialogue.map((l) => ({ [d.text]: l.text, [d.voice]: l.voice ?? input.voice }));}if (map.targetLanguage && input.targetLanguage) out[map.targetLanguage] = input.targetLanguage;+ if (map.sourceLanguage && input.sourceLanguage) out[map.sourceLanguage] = input.sourceLanguage;if (map.draftId && input.draftId) out[map.draftId] = input.draftId;+ // Trims, frame rates, palettes, saved styles and layer counts (research part 3).+ if (map.startTime && input.startTime !== undefined) out[map.startTime.key] = timeValue(map.startTime, input.startTime);+ if (map.endTime && input.endTime !== undefined) out[map.endTime.key] = timeValue(map.endTime, input.endTime);+ if (map.fps && input.fps !== undefined) out[map.fps] = input.fps;+ if (map.styleId && input.styleId) out[map.styleId] = input.styleId;+ if (map.numLayers && input.numLayers !== undefined) out[map.numLayers] = input.numLayers;+ if (map.palette && input.palette?.length) out[map.palette.key] = paletteValue(map.palette.format, input.palette);+for (const [k, v] of Object.entries({ ...map.fixed, ...input.extra })) setPath(out, k, v);+ // A background colour given as hex goes out in the palette's shape (Recraft wants {r,g,b}).+ const bg = map.palette?.background;+ if (bg && typeof out[bg] === "string") {+ const rgb = hexToRgb(out[bg] as string);+ if (rgb) out[bg] = colorValue(map.palette!.format === "ideogram" ? "rgb-object" : map.palette!.format, rgb, out[bg] as string);+ }return out;}const VIDEO_EXT = /\.(mp4|webm|mov|m4v|gif)(\?|$)/i;const AUDIO_EXT = /\.(mp3|wav|ogg|flac|m4a|aac)(\?|$)/i;+const MODEL3D_EXT = /\.(glb|gltf)(\?|$)/i;export function guessKind(url: string, contentType?: string, hint?: string): MediaKind {if (contentType?.startsWith("video/")) return "video";if (contentType?.startsWith("audio/")) return "audio";if (contentType?.startsWith("image/")) return "image";+ if (contentType?.startsWith("model/") || MODEL3D_EXT.test(url)) return "model3d";if (hint && /video/i.test(hint)) return "video";if (hint && /audio|speech|music|voice/i.test(hint)) return "audio";if (VIDEO_EXT.test(url)) return "video";@@ -95,6 +161,13 @@ export function guessKind(url: string, contentType?: string, hint?: string): Medreturn "image";}+/**+ * Keys of a response that are request lifecycle links or ids, never results: Higgsfield's RequestStatus+ * carries status_url and cancel_url next to the media, fal's queue receipts carry response_url.+ */+const LIFECYCLE_KEYS = new Set(["status_url", "cancel_url", "response_url", "logs_url", "webhook_url", "request_id"]);+const SKIP_KEYS = new Set(["seed", "timings", "has_nsfw_concepts", "logs", ...LIFECYCLE_KEYS]);+/*** Extract media URLs from an arbitrary provider response. Handles the common shapes:* {images:[{url}]}, {image:{url}}, {video:{url}}, {audio:{url}}, {audio_file:{url}},@@ -108,7 +181,7 @@ export function extractMedia(response: unknown): { url: string; kind: MediaKind;if (typeof node === "string") {if (/^(https?:\/\/|data:(image|video|audio)\/)/.test(node) && !seen.has(node)) {// Skip obvious non-media links (docs, logs).- if (/^https?:\/\//.test(node) && !/\.(png|jpe?g|webp|gif|mp4|webm|mov|mp3|wav|ogg|flac|m4a)(\?|$)/i.test(node) && !/(image|video|audio|output|url|file|media)/i.test(keyHint)) return;+ if (/^https?:\/\//.test(node) && !/\.(png|jpe?g|webp|gif|svg|mp4|webm|mov|mp3|wav|ogg|flac|m4a|glb|gltf)(\?|$)/i.test(node) && !/(image|video|audio|output|url|file|media)/i.test(keyHint)) return;seen.add(node);found.push({ url: node, kind: guessKind(node, undefined, keyHint) });}@@ -127,7 +200,7 @@ export function extractMedia(response: unknown): { url: string; kind: MediaKind;return;}for (const [k, v] of Object.entries(obj)) {- if (k === "seed" || k === "timings" || k === "has_nsfw_concepts" || k === "logs") continue;+ if (SKIP_KEYS.has(k)) continue;visit(v, k);}}@@ -135,3 +208,291 @@ export function extractMedia(response: unknown): { url: string; kind: MediaKind;visit(response, "output");return found;}++// ---------------------------------------------------------------------------+// Structured results: layers, text blocks, transcripts, 3D models, stems.++/** One result file plus what the API said about it (OutputFile.text / OutputFile.meta). */+export interface ExtractedOutput {+ url: string;+ kind: MediaKind;+ contentType?: string;+ /** Text results (transcripts): the content; `url` is a data:text/plain URI of the same text. */+ text?: string;+ meta?: Record<string, unknown>;+}++/** A transcript line: seconds from the start. */+export interface Cue {+ start: number;+ end: number;+ text: string;+ speaker?: string;+}++type Obj = Record<string, unknown>;+const isObj = (v: unknown): v is Obj => v !== null && typeof v === "object" && !Array.isArray(v);+const str = (v: unknown): string | undefined => (typeof v === "string" ? v : undefined);+const num = (v: unknown): number | undefined => (typeof v === "number" && Number.isFinite(v) ? v : undefined);+/** Drop undefined values so stored meta stays small. */+const compact = (o: Obj): Obj => Object.fromEntries(Object.entries(o).filter(([, v]) => v !== undefined));++/** UTF-8 text as a data URI the runner can persist like any other output (works in Node and browsers). */+export function textDataUri(text: string): string {+ const bytes = new TextEncoder().encode(text);+ let bin = "";+ for (let i = 0; i < bytes.length; i += 0x8000) bin += String.fromCharCode(...bytes.subarray(i, i + 0x8000));+ return `data:text/plain;base64,${btoa(bin)}`;+}++/** A file object ({url, content_type}) or a bare URL string. */+function fileOf(v: unknown): { url: string; contentType?: string } | undefined {+ if (typeof v === "string" && /^(https?:|data:)/.test(v)) return { url: v };+ if (isObj(v) && typeof v.url === "string") return { url: v.url, contentType: str(v.content_type) };+ return undefined;+}++/** Replicate wraps everything in `output`; an object output is the result itself. */+function body(response: unknown): unknown {+ return isObj(response) && Object.keys(response).length === 1 && "output" in response && isObj(response.output) ? response.output : response;+}++/** [left, top, right, bottom] → [x, y, w, h]. */+function ltrbToXywh(v: unknown): number[] | undefined {+ if (!Array.isArray(v) || v.length !== 4 || !v.every((n) => typeof n === "number")) return undefined;+ const [l, t, r, b] = v as number[];+ return [l, t, r - l, b - t];+}++const layerName = (z: number) => (z === 0 ? "Background" : `Layer ${z}`);++/**+ * Seedream layerize: layers[] = base image (z 0) + up to 16 RGBA layers, each with z_index, name,+ * description and bounding_box {absolute, normalized} as [l, t, r, b]. Qwen layered (fal images[],+ * Replicate output[]): full-canvas RGBA layers in stacking order, no names or boxes.+ */+function layerOutputs(response: unknown): ExtractedOutput[] {+ const r = body(response);+ if (isObj(r) && Array.isArray(r.layers)) {+ return r.layers.flatMap((l: unknown, i): ExtractedOutput[] => {+ if (!isObj(l)) return [];+ const f = fileOf(l.image) ?? fileOf(l);+ if (!f) return [];+ const z = num(l.z_index) ?? i;+ const box = isObj(l.bounding_box) ? l.bounding_box : undefined;+ return [+ {+ url: f.url,+ kind: "image",+ contentType: f.contentType,+ meta: compact({ name: str(l.name) ?? layerName(z), z, bbox: ltrbToXywh(box?.absolute), bboxNormalized: ltrbToXywh(box?.normalized), description: str(l.description) }),+ },+ ];+ });+ }+ return extractMedia(response)+ .filter((o) => o.kind === "image")+ .map((o, z) => ({ ...o, meta: { name: layerName(z), z } }));+}++/** All `text` strings under a node, in order (Ideogram's container → items → spans). */+function collectText(node: unknown): string[] {+ if (Array.isArray(node)) return node.flatMap(collectText);+ if (!isObj(node)) return [];+ if (typeof node.text === "string") return [node.text];+ return Object.values(node).flatMap(collectText);+}++/**+ * Ideogram layerize-text: image = the background with the text removed, text_containers = the text+ * (container → items → spans with positions and typography), text_html = an overlay that re-renders it,+ * image_layers = extracted picture layers.+ */+function textLayerOutputs(response: unknown): ExtractedOutput[] {+ const r = body(response);+ const bg = isObj(r) ? fileOf(r.image) : undefined;+ if (!isObj(r) || !bg) return layerOutputs(response);+ const blocks = (Array.isArray(r.text_containers) ? r.text_containers : []).map((c) => (isObj(c) && typeof c.text !== "string" ? { ...c, text: collectText(c).join(" ").replace(/\s+/g, " ").trim() } : c));+ const out: ExtractedOutput[] = [+ { url: bg.url, kind: "image", contentType: bg.contentType, meta: compact({ name: "Background", z: 0, textBlocks: blocks, textHtml: str(r.text_html) }) },+ ];+ (Array.isArray(r.image_layers) ? r.image_layers : []).forEach((l, i) => {+ const f = fileOf(isObj(l) ? (l.image ?? l) : l);+ if (!f) return;+ const box = isObj(l) && isObj(l.bounding_box) ? l.bounding_box : undefined;+ out.push({+ url: f.url,+ kind: "image",+ contentType: f.contentType,+ meta: compact({ name: (isObj(l) && str(l.name)) || `Image ${i + 1}`, z: i + 1, bbox: ltrbToXywh(box?.absolute) ?? (isObj(l) && Array.isArray(l.bbox) ? l.bbox : undefined) }),+ });+ });+ return out;+}++interface TimedItem {+ text: string;+ start?: number;+ end?: number;+ speaker?: string;+ type?: string;+}++function timed(v: unknown): TimedItem | null {+ if (!isObj(v)) return null;+ const text = str(v.text) ?? str(v.word);+ if (text === undefined) return null;+ const ts = Array.isArray(v.timestamp) ? v.timestamp : undefined;+ return {+ text,+ start: num(v.start) ?? num(ts?.[0]) ?? num(v.start_time),+ end: num(v.end) ?? num(ts?.[1]) ?? num(v.end_time),+ speaker: str(v.speaker_id) ?? str(v.speaker),+ type: str(v.type),+ };+}++const MAX_CUE_CHARS = 64;++/** Word timings → caption-sized cues: break at sentence ends, pauses, speaker changes and ~64 characters. */+export function wordsToCues(words: TimedItem[]): Cue[] {+ const cues: Cue[] = [];+ const push = (c: Cue | null) => {+ if (c && c.text.trim()) cues.push({ ...c, text: c.text.replace(/\s+/g, " ").trim() });+ };+ const join = (text: string, word: string) => `${text}${text && !/\s$/.test(text) && !/^[,.!?;:]/.test(word) ? " " : ""}${word}`;+ let cur: Cue | null = null;+ for (const w of words) {+ if (w.type === "spacing") {+ if (cur) cur.text += w.text || " ";+ continue;+ }+ if (w.start === undefined) {+ if (cur) cur.text = join(cur.text, w.text);+ continue;+ }+ if (cur && (w.speaker !== cur.speaker || w.start - cur.end > 0.8 || cur.text.length + w.text.length > MAX_CUE_CHARS)) {+ push(cur);+ cur = null;+ }+ cur ??= { start: w.start, end: w.end ?? w.start, text: "", ...(w.speaker ? { speaker: w.speaker } : {}) };+ cur.text = join(cur.text, w.text);+ cur.end = w.end ?? cur.end;+ if (/[.!?…]["')\]]?$/.test(w.text.trim())) {+ push(cur);+ cur = null;+ }+ }+ push(cur);+ return cues;+}++/** Segment-level timings (Whisper chunks, WhisperX segments) are cues already; word-level ones are grouped. */+function cuesFrom(r: Obj): Cue[] {+ const words = Array.isArray(r.words) ? r.words.map(timed).filter((w): w is TimedItem => !!w) : [];+ const segs = (["chunks", "segments", "utterances"] as const).flatMap((k) => (Array.isArray(r[k]) ? (r[k] as unknown[]).map(timed).filter((w): w is TimedItem => !!w) : []));+ const items = segs.length ? segs : words;+ if (!items.length) return [];+ const wordLevel = !segs.length || items.every((s) => s.text.trim().split(/\s+/).length <= 1);+ if (wordLevel) return wordsToCues(items);+ return items+ .filter((s) => s.start !== undefined && s.text.trim())+ .map((s, i, all) => ({ start: s.start!, end: s.end ?? all[i + 1]?.start ?? s.start!, text: s.text.trim(), ...(s.speaker ? { speaker: s.speaker } : {}) }));+}++/** The plain-text result of an API that answers with text (transcripts, OCR), or undefined. */+function textOf(response: unknown): string | undefined {+ const r = body(response);+ if (typeof r === "string" && !/^https?:\/\//.test(r)) return r;+ if (!isObj(r)) return undefined;+ for (const k of ["text", "transcription", "transcript"]) if (typeof r[k] === "string") return r[k] as string;+ // Replicate models that stream text return it as an array of string pieces.+ if (Array.isArray(r.output) && r.output.length && r.output.every((x) => typeof x === "string" && !/^https?:\/\//.test(x))) return r.output.join("");+ if (typeof r.output === "string" && !/^(https?:|data:)/.test(r.output)) return r.output;+ return undefined;+}++function transcriptOutputs(response: unknown): ExtractedOutput[] {+ const r = body(response);+ const o: Obj = isObj(r) ? r : {};+ const cues = cuesFrom(o);+ const text = (textOf(response) ?? cues.map((c) => c.text).join(" ")).trim();+ if (!text && !cues.length) return [];+ const languages = (o.languages ?? o.inferred_languages) as unknown;+ const language = str(o.language_code) ?? str(o.language) ?? (Array.isArray(languages) ? str(languages[0]) : undefined);+ return [{ url: textDataUri(text), kind: "text", contentType: "text/plain", text, meta: compact({ cues, language }) }];+}++/**+ * Image-to-3D: the GLB (Hunyuan model_glb, model_urls.glb; Tripo model_mesh; Meshy model_glb), with the+ * other formats and the preview render in meta.+ */+function model3dOutputs(response: unknown): ExtractedOutput[] {+ const r = body(response);+ if (isObj(r)) {+ const urls = isObj(r.model_urls) ? r.model_urls : {};+ const glb = fileOf(r.model_glb) ?? fileOf(urls.glb) ?? fileOf(r.model_mesh) ?? fileOf(r.mesh) ?? fileOf(r.model_file);+ if (glb) {+ const formats = compact(Object.fromEntries(Object.entries(urls).map(([k, v]) => [k, fileOf(v)?.url])));+ const preview = fileOf(r.thumbnail)?.url ?? fileOf(r.rendered_image)?.url ?? fileOf(r.preview)?.url;+ // A GLB was asked for; generic or missing types are labelled as one so it is stored as .glb.+ const contentType = !glb.contentType || glb.contentType === "application/octet-stream" || MODEL3D_EXT.test(glb.url) ? "model/gltf-binary" : glb.contentType;+ return [{ url: glb.url, kind: "model3d", contentType, meta: compact({ preview, formats: Object.keys(formats).length ? formats : undefined }) }];+ }+ }+ const media = extractMedia(response);+ const mesh = media.find((m) => m.kind === "model3d") ?? media.find((m) => /\.(obj|fbx|ply|usdz)(\?|$)/i.test(m.url)) ?? media[0];+ return mesh ? [{ url: mesh.url, kind: "model3d", contentType: MODEL3D_EXT.test(mesh.url) ? "model/gltf-binary" : mesh.contentType }] : [];+}++const STEM_ORDER = ["vocals", "voice", "speech", "target", "drums", "bass", "guitar", "piano", "other", "residual", "accompaniment", "instrumental"];++/** Demucs: {vocals, drums, bass, other, guitar, piano} files; SAM Audio: {target, residual}. Voice first. */+function stemOutputs(response: unknown): ExtractedOutput[] {+ const r = body(response);+ if (!isObj(r)) return extractMedia(response);+ const stems = Object.entries(r).flatMap(([k, v]) => {+ const f = fileOf(v);+ return f && !LIFECYCLE_KEYS.has(k) ? [{ url: f.url, kind: "audio" as const, contentType: f.contentType, meta: { stem: k } }] : [];+ });+ const rank = (s: string) => (STEM_ORDER.indexOf(s) === -1 ? STEM_ORDER.length : STEM_ORDER.indexOf(s));+ return stems.length ? stems.sort((a, b) => rank(a.meta.stem) - rank(b.meta.stem)) : extractMedia(response);+}++/**+ * Every result of a provider response, with structure the operation defines: layers (name, z, bbox),+ * text blocks, transcripts (text + cues), 3D models and audio stems. A response with no media but+ * text comes back as one text output instead of nothing.+ */+export function extractOutputs(response: unknown, op?: Operation): ExtractedOutput[] {+ let out: ExtractedOutput[];+ switch (op) {+ case "layers":+ out = layerOutputs(response);+ break;+ case "text-layers":+ out = textLayerOutputs(response);+ break;+ case "transcribe":+ out = transcriptOutputs(response);+ break;+ case "image-to-3d":+ out = model3dOutputs(response);+ break;+ case "separate-audio":+ out = stemOutputs(response);+ break;+ default:+ out = extractMedia(response);+ }+ // Captions endpoints also return what they transcribed: keep it with the captioned video.+ if (op === "captions" && out.length) {+ const t = transcriptOutputs(response)[0];+ if (t) out = [{ ...out[0], meta: { ...out[0].meta, transcript: t.text, ...(t.meta?.cues ? { cues: t.meta.cues } : {}) } }, ...out.slice(1)];+ }+ if (!out.length) {+ const text = textOf(response)?.trim();+ if (text) out = transcriptOutputs(response);+ }+ return out;+}diff --git a/src/lib/catalog/types.ts b/src/lib/catalog/types.tsindex e4db470b..a069a8c7 100644--- a/src/lib/catalog/types.ts+++ b/src/lib/catalog/types.ts@@ -57,7 +57,8 @@ export type Operation =| "image-to-3d" // image → a 3D model (.glb), optionally a turntable video| "separate-audio" // audio or video → voice and background stems| "translate" // text → the same text in params.targetLanguage (LLM)- | "write"; // brief → text (LLM: scripts, ad copy, shot lists)+ | "write" // brief → text (LLM: scripts, ad copy, shot lists)+ | "vectorize"; // image → the same picture as an SVGexport const OPERATION_LABELS: Record<Operation, string> = {t2i: "Text to image",@@ -104,6 +105,7 @@ export const OPERATION_LABELS: Record<Operation, string> = {"separate-audio": "Separate voice",translate: "Translate",write: "Write",+ vectorize: "Vectorize",};export const OPERATION_OUTPUT: Record<Operation, MediaKind> = {@@ -151,6 +153,7 @@ export const OPERATION_OUTPUT: Record<Operation, MediaKind> = {"separate-audio": "audio",translate: "text",write: "text",+ vectorize: "image",};/** "llm" runs text steps (translate, write) on the configured LLM (Claude or Ollama) inside the server. */@@ -220,13 +223,20 @@ export interface FieldMap {endTime?: DurationField;/** Frames per second (frame sampling, images to video). */fps?: string;- /** Colour palette: hex strings converted to the API's shape. */- palette?: { key: string; format: "hex" | "rgb-object" | "rgb-array" | "ideogram" };+ /**+ * Colour palette: hex strings converted to the API's shape ("rgb-object" = Recraft on fal [{r,g,b}],+ * "rgb-array" = Recraft on Higgsfield [{rgb:[r,g,b]}], "ideogram" = {members:[{rgb:{r,g,b}, color_weight}]}).+ * `background` names the API's background-colour field: a hex string sent as params.extra[background]+ * is converted to the same shape.+ */+ palette?: { key: string; format: "hex" | "rgb-object" | "rgb-array" | "ideogram"; background?: string };/** A saved style id (Recraft styles, Soul 2.0 moodboards). */styleId?: string;/** How many layers to split an image into. */numLayers?: string;sourceLanguage?: string;+ /** Second image field (inputs.images[1]): a try-on garment, a product shot's scene reference, a style image. */+ image2?: string;/** Static fields always sent (e.g. safety checker switches). */fixed?: Record<string, unknown>;}diff --git a/src/server/providers/fal.ts b/src/server/providers/fal.tsindex 3554c41a..28905729 100644--- a/src/server/providers/fal.ts+++ b/src/server/providers/fal.ts@@ -1,6 +1,6 @@import "server-only";import { createFalClient } from "@fal-ai/client";-import { buildPayload, extractMedia } from "@/lib/catalog/payload";+import { buildPayload, extractOutputs } from "@/lib/catalog/payload";import { getSetting } from "../settings";import { ProviderError, type ProviderAdapter } from "./types";@@ -74,7 +74,7 @@ export const falProvider: ProviderAdapter = {};},- async poll({ meta }) {+ async poll({ meta, route }) {const status = (await falFetch(`${meta.statusUrl as string}?logs=1`)) as {status: string;queue_position?: number;@@ -93,7 +93,8 @@ export const falProvider: ProviderAdapter = {if (status.error) return { state: "failed", error: status.error };try {const result = await falFetch(meta.responseUrl as string);- const outputs = extractMedia(result);+ // Layers, text blocks, transcripts, 3D models and stems keep their structure (OutputFile.meta / text).+ const outputs = extractOutputs(result, route?.op);if (!outputs.length) return { state: "failed", error: `No media in response: ${JSON.stringify(result).slice(0, 300)}` };// Seedance draft mode: keep the id that finishes this take at 1080p.const draftId = /"draft_id"\s*:\s*"([^"]+)"/.exec(JSON.stringify(result))?.[1];diff --git a/src/server/providers/higgsfield.ts b/src/server/providers/higgsfield.tsindex 9b2cf573..440d4675 100644--- a/src/server/providers/higgsfield.ts+++ b/src/server/providers/higgsfield.ts@@ -1,7 +1,8 @@import "server-only";-import { buildPayload, extractMedia } from "@/lib/catalog/payload";+import { buildPayload, extractOutputs } from "@/lib/catalog/payload";+import type { Operation } from "@/lib/catalog/types";import { getSetting } from "../settings";-import { ProviderError, type ProviderAdapter } from "./types";+import { ProviderError, type ProviderAdapter, type RemoteOutput } from "./types";/*** Higgsfield's pay-per-use API (docs.higgsfield.ai, see docs/research/higgsfield-api.md).@@ -69,6 +70,29 @@ export async function hfUpload(data: Buffer, contentType: string): Promise<strinreturn slot.public_url;}+/** RequestStatus fields that hold results (docs/research/higgsfield-api.json → lifecycle.status.outputs). */+const MEDIA_FIELDS = ["images", "image", "video", "videos", "audio", "audios"] as const;+/** Extra artifacts some video/3D jobs attach; kept as links on the first output, not shown as media. */+const ARTIFACT_FIELDS = ["zip", "mov", "jsx", "fbx", "ply"] as const;++/**+ * The outputs of a completed RequestStatus. The body also carries status_url and cancel_url (API links,+ * not results), so only the documented media fields are read.+ */+export function higgsfieldOutputs(status: Record<string, unknown>, op?: Operation): RemoteOutput[] {+ const media = Object.fromEntries(MEDIA_FIELDS.filter((k) => status[k] != null).map((k) => [k, status[k]]));+ const outputs: RemoteOutput[] = extractOutputs(media, op);+ const artifacts = Object.fromEntries(+ ARTIFACT_FIELDS.flatMap((k) => {+ const v = status[k] as { url?: unknown } | string | undefined;+ const url = typeof v === "string" ? v : typeof v?.url === "string" ? v.url : undefined;+ return url ? [[k, url]] : [];+ }),+ );+ if (outputs.length && Object.keys(artifacts).length) outputs[0] = { ...outputs[0], meta: { ...outputs[0].meta, artifacts } };+ return outputs;+}+export const higgsfieldProvider: ProviderAdapter = {id: "higgsfield",label: "Higgsfield",@@ -91,15 +115,15 @@ export const higgsfieldProvider: ProviderAdapter = {};},- async poll({ meta }) {- const s = (await hfFetch(meta.statusUrl as string)) as { status: string; error?: string | null };+ async poll({ meta, route }) {+ const s = (await hfFetch(meta.statusUrl as string)) as { status: string; error?: string | null } & Record<string, unknown>;switch (s.status) {case "queued":return { state: "queued", message: "In queue" };case "in_progress":return { state: "running", message: "Generating" };case "completed": {- const outputs = extractMedia(s);+ const outputs = higgsfieldOutputs(s, route?.op);return outputs.length ? { state: "succeeded", outputs } : { state: "failed", error: `No media in response: ${JSON.stringify(s).slice(0, 300)}` };}case "nsfw":diff --git a/src/server/providers/replicate.ts b/src/server/providers/replicate.tsindex d82dfbf5..2101e1b6 100644--- a/src/server/providers/replicate.ts+++ b/src/server/providers/replicate.ts@@ -1,5 +1,5 @@import "server-only";-import { buildPayload, extractMedia } from "@/lib/catalog/payload";+import { buildPayload, extractOutputs } from "@/lib/catalog/payload";import { getSetting } from "../settings";import { toDataUri } from "../storage";import { ProviderError, type ProviderAdapter } from "./types";@@ -58,7 +58,7 @@ export const replicateProvider: ProviderAdapter = {return { externalId: res.id, payload };},- async poll({ externalId }) {+ async poll({ externalId, route }) {const p = (await rFetch(`${API}/predictions/${externalId}`)) as {status: string;output: unknown;@@ -72,7 +72,8 @@ export const replicateProvider: ProviderAdapter = {return { state: "running", progress: pct ? Number(pct) / 100 : undefined, message: last ?? "Processing" };}if (p.status === "succeeded") {- const outputs = extractMedia({ output: p.output });+ // Object outputs (Scribe's {text, words}) are read as the result itself; URL lists keep their order (layers).+ const outputs = extractOutputs({ output: p.output }, route?.op);if (!outputs.length) return { state: "failed", error: "No media in output" };return { state: "succeeded", outputs };}diff --git a/src/server/providers/types.ts b/src/server/providers/types.tsindex c12efd29..17513b54 100644--- a/src/server/providers/types.ts+++ b/src/server/providers/types.ts@@ -20,6 +20,10 @@ export interface RemoteOutput {draftId?: string;kind?: MediaKind;contentType?: string;+ /** Text results (transcripts): the content; `url` is then a data:text/plain URI holding the same text. */+ text?: string;+ /** Structured extras for OutputFile.meta (layer name/z/bbox, cues, textBlocks, stem, 3D preview…). */+ meta?: Record<string, unknown>;/** Extra headers needed to download (e.g. auth for a private ComfyUI). */headers?: Record<string, string>;}diff --git a/tests/payload.test.ts b/tests/payload.test.tsindex 95194231..fb612449 100644--- a/tests/payload.test.ts+++ b/tests/payload.test.ts@@ -30,6 +30,19 @@ describe("buildPayload", () => {it("renames the prompt field and ignores negative seeds", () => {expect(buildPayload({ prompt: "text", seed: "seed" }, { prompt: "hi", seed: -1 })).toEqual({ text: "hi" });});++ it("never writes params.extra keys onto Object.prototype (S2)", () => {+ const extra = JSON.parse(+ '{"__proto__.polluted": "yes", "constructor.prototype.polluted2": "yes", "a.__proto__.polluted3": "yes", "__proto__": {"polluted4": "yes"}, "prototype": 1, "voice_setting.speed": 1.2}',+ ) as Record<string, unknown>;+ const p = buildPayload({ voice: "voice_setting.voice_id", fixed: { "voice_setting.pitch": 0 } }, { voice: "Wise_Woman", extra });+ const probe = {} as Record<string, unknown>;+ expect([probe.polluted, probe.polluted2, probe.polluted3, probe.polluted4]).toEqual([undefined, undefined, undefined, undefined]);+ expect(Object.getPrototypeOf(p)).toBe(Object.prototype);+ // Dotted keys still nest, and a scalar in the way is replaced by an object instead of throwing.+ expect(p).toEqual({ voice_setting: { voice_id: "Wise_Woman", pitch: 0, speed: 1.2 } });+ expect(buildPayload({}, { extra: { a: "x", "a.b": 1 } })).toEqual({ a: { b: 1 } });+ });});describe("extractMedia", () => {diff --git a/tests/providers.test.ts b/tests/providers.test.tsnew file mode 100644index 00000000..c32ca477--- /dev/null+++ b/tests/providers.test.ts@@ -0,0 +1,84 @@+import fs from "node:fs";+import os from "node:os";+import path from "node:path";+import { afterAll, describe, expect, it } from "vitest";+import type { ModelDef, ModelRoute } from "@/lib/catalog/types";++// Provider adapters read their keys from the settings table: give them a throwaway database.+const dir = fs.mkdtempSync(path.join(os.tmpdir(), "fuobe-providers-"));+process.env.FUOBE_DATA_DIR = dir;++const { higgsfieldProvider } = await import("@/server/providers/higgsfield");+const { falProvider } = await import("@/server/providers/fal");+const { replicateProvider } = await import("@/server/providers/replicate");++afterAll(() => fs.rmSync(dir, { recursive: true, force: true }));++/** Answer fetches from a table of URL → JSON body, in order of registration. */+async function withFetch<T>(answers: Record<string, unknown>, fn: () => Promise<T>): Promise<{ result: T; urls: string[] }> {+ const real = globalThis.fetch;+ const urls: string[] = [];+ globalThis.fetch = (async (url: string) => {+ const u = String(url);+ urls.push(u);+ const hit = Object.keys(answers).find((k) => u.startsWith(k));+ if (!hit) return new Response("not found", { status: 404 });+ return new Response(JSON.stringify(answers[hit]), { status: 200, headers: { "content-type": "application/json" } });+ }) as typeof fetch;+ try {+ return { result: await fn(), urls };+ } finally {+ globalThis.fetch = real;+ }+}++const model = { id: "m", routes: [] } as unknown as ModelDef;+const ctx = (route: Partial<ModelRoute>, meta: Record<string, unknown> = {}) => ({+ externalId: "req-1",+ meta,+ route: { op: "t2i", provider: "fal", endpoint: "x", ...route } as ModelRoute,+ model,+ createdAt: 0,+});++describe("Higgsfield poll (S1)", () => {+ it("returns only the media of a completed RequestStatus, not its status/cancel links", async () => {+ const statusUrl = "https://api.higgsfield.ai/requests/req-1/status";+ // RequestStatus as docs.higgsfield.ai/docs/openapi.json documents it.+ const body = {+ status: "completed",+ request_id: "req-1",+ status_url: statusUrl,+ cancel_url: "https://api.higgsfield.ai/requests/req-1/cancel",+ images: [{ url: "https://cdn.higgsfield.ai/out/a.png" }],+ };+ const { result } = await withFetch({ [statusUrl]: body }, () =>+ higgsfieldProvider.poll(ctx({ provider: "higgsfield", endpoint: "higgsfield-ai/soul/v2/standard" }, { statusUrl })),+ );+ expect(result).toEqual({ state: "succeeded", outputs: [{ url: "https://cdn.higgsfield.ai/out/a.png", kind: "image" }] });+ });++ it("keeps videos first and extra artifacts as links on the output", async () => {+ const statusUrl = "https://api.higgsfield.ai/requests/req-2/status";+ const body = {+ status: "completed",+ request_id: "req-2",+ status_url: statusUrl,+ cancel_url: "https://api.higgsfield.ai/requests/req-2/cancel",+ video: { url: "https://cdn.higgsfield.ai/out/v.mp4" },+ zip: { url: "https://cdn.higgsfield.ai/out/frames.zip" },+ };+ const { result } = await withFetch({ [statusUrl]: body }, () => higgsfieldProvider.poll(ctx({ provider: "higgsfield", op: "t2v" }, { statusUrl })));+ expect(result).toEqual({+ state: "succeeded",+ outputs: [{ url: "https://cdn.higgsfield.ai/out/v.mp4", kind: "video", meta: { artifacts: { zip: "https://cdn.higgsfield.ai/out/frames.zip" } } }],+ });+ });++ it("fails cleanly when a completed status has no media", async () => {+ const statusUrl = "https://api.higgsfield.ai/requests/req-3/status";+ const body = { status: "completed", request_id: "req-3", status_url: statusUrl, cancel_url: "https://api.higgsfield.ai/requests/req-3/cancel" };+ const { result } = await withFetch({ [statusUrl]: body }, () => higgsfieldProvider.poll(ctx({ provider: "higgsfield" }, { statusUrl })));+ expect(result.state).toBe("failed");+ });+});