85453da49f
regression / regression-shards (style-16-prod style-9-prod style-17-prod iframe-render-compat variables-prod mp4-h265-sdr, shard-4) (push) Has been cancelled
regression / regression-shards (style-4-prod style-11-prod style-2-prod animejs-adapter typegpu-adapter parallel-capture-regression, shard-5) (push) Has been cancelled
regression / regression-shards (style-7-prod style-8-prod style-10-prod css-spinner-render-compat webm-transparency mp4-h264-sdr webm-vp9, shard-3) (push) Has been cancelled
regression / regression-shards (sub-composition-video style-18-prod raf-ball-render-compat font-variant-numeric sub-comp-t0 sub-comp-id-selector, shard-7) (push) Has been cancelled
Windows render verification / Detect changes (push) Has been cancelled
Windows render verification / Preflight (lint + format) (push) Has been cancelled
Windows render verification / Render on windows-latest (push) Has been cancelled
Windows render verification / Tests on windows-latest (push) Has been cancelled
CI / Detect changes (push) Has been cancelled
CI / Build (push) Has been cancelled
CI / Lint (push) Has been cancelled
CI / Fallow audit (push) Has been cancelled
CI / Format (push) Has been cancelled
CI / Typecheck (push) Has been cancelled
CI / Test (push) Has been cancelled
CI / Producer: integration tests (push) Has been cancelled
CI / Producer: unit tests (push) Has been cancelled
CI / File size check (push) Has been cancelled
CI / Test: skills (push) Has been cancelled
CI / Skills: manifest in sync (push) Has been cancelled
CI / CLI: npx shim (macos-latest) (push) Has been cancelled
CI / CLI: npx shim (ubuntu-latest) (push) Has been cancelled
CI / CLI: npx shim (windows-latest) (push) Has been cancelled
CI / SDK: unit + contract + smoke (push) Has been cancelled
CI / Test: runtime contract (push) Has been cancelled
CI / Studio: load smoke (push) Has been cancelled
CI / Smoke: global install (push) Has been cancelled
CI / CLI smoke (required) (push) Has been cancelled
CI / Semantic PR title (push) Has been cancelled
Player perf / Detect changes (push) Has been cancelled
Player perf / Preflight (lint + format) (push) Has been cancelled
Player perf / player-perf (push) Has been cancelled
Player perf / Perf: drift (push) Has been cancelled
Player perf / Perf: fps (push) Has been cancelled
Player perf / Perf: parity (push) Has been cancelled
Player perf / Perf: scrub (push) Has been cancelled
Player perf / Perf: load (push) Has been cancelled
preview-regression / Detect changes (push) Has been cancelled
preview-regression / Preflight (lint + format) (push) Has been cancelled
preview-regression / Preview parity (push) Has been cancelled
preview-regression / preview-regression (push) Has been cancelled
regression / regression (push) Has been cancelled
regression / Detect changes (push) Has been cancelled
regression / Preflight (lint + format) (push) Has been cancelled
regression / regression-shards (hdr-regression style-5-prod style-3-prod mov-prores, shard-1) (push) Has been cancelled
regression / regression-shards (overlay-montage-prod style-12-prod chat missing-host-comp-id png-sequence portrait-edge-bleed, shard-6) (push) Has been cancelled
regression / regression-shards (style-13-prod style-6-prod vignelli-stacking gsap-letters-render-compat audio-mux-parity, shard-8) (push) Has been cancelled
regression / regression-shards (style-15-prod hdr-hlg-regression style-1-prod many-cuts vfr-screen-recording render-symlinked-assets, shard-2) (push) Has been cancelled
CodeQL / Analyze (actions) (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Docs / Validate docs (push) Has been cancelled
Sync skills to ClawHub / Publish changed skills (push) Has been cancelled
96 lines
3.5 KiB
JavaScript
96 lines
3.5 KiB
JavaScript
import { execFileSync } from "node:child_process";
|
|
import { existsSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { probeSpecs } from "./specs.mjs";
|
|
import { selectModel } from "./local-models.mjs";
|
|
|
|
// Local image generation via mflux (FLUX-on-MLX), the Mac-native runner.
|
|
// Spec-gated: selectModel("imagegen", specs) returns the best FLUX-class model
|
|
// the machine's AVAILABLE RAM can actually run (medium FLUX-schnell --low-ram on
|
|
// ~24GB, up to Qwen-Image on 64GB+). When nothing local fits, this returns null
|
|
// so the registry falls through to the codex image upsell.
|
|
//
|
|
// The official FLUX repos are HF-gated, so the model entries point --path at
|
|
// non-gated community 4-bit re-uploads; the repo is resolved to a local snapshot
|
|
// (hf download, idempotent) because a bare repo id breaks mlx unflatten.
|
|
|
|
// Tokenize the invoke template on whitespace FIRST, then substitute each token,
|
|
// so a {prompt}/{model_path} value with spaces stays a single argv entry.
|
|
function buildArgv(template, vars) {
|
|
return template
|
|
.trim()
|
|
.split(/\s+/)
|
|
.map((tok) => tok.replace(/\{(\w+)\}/g, (_, k) => (k in vars ? String(vars[k]) : `{${k}}`)));
|
|
}
|
|
|
|
// Resolve an HF repo to its local snapshot dir. `hf download` is idempotent and
|
|
// prints the snapshot path as its last line.
|
|
function resolveSnapshot(repo) {
|
|
const out = execFileSync("hf", ["download", repo], {
|
|
encoding: "utf8",
|
|
timeout: 1_800_000,
|
|
stdio: ["ignore", "pipe", "pipe"],
|
|
});
|
|
const path = out.trim().split(/\r?\n/).pop()?.trim();
|
|
return path && existsSync(path) ? path : null;
|
|
}
|
|
|
|
export async function mfluxImageGenerate(intent, ctx) {
|
|
const specs = ctx?.specs || probeSpecs();
|
|
const sel = selectModel("imagegen", specs, { preferTier: ctx?.preferTier });
|
|
if (sel.recommend) return null; // no local model fits -> codex upsell/fallback
|
|
|
|
const { model } = sel;
|
|
const bin = model.invoke.trim().split(/\s+/)[0];
|
|
// Not installed? Surface the exact enable-command FIRST (before the model
|
|
// download) so the agent learns the free local path is available instead of
|
|
// silently taking the codex upsell.
|
|
try {
|
|
execFileSync("which", [bin], { stdio: ["ignore", "ignore", "ignore"] });
|
|
} catch {
|
|
console.error(
|
|
`media-use: local image gen not enabled (\`${bin}\` not on PATH). Install for free on-device FLUX: ${model.install}`,
|
|
);
|
|
return null;
|
|
}
|
|
|
|
const outPath = join(tmpdir(), `media-use-mflux-${process.pid}-${Date.now()}.png`);
|
|
const width = ctx?.width || 512;
|
|
const height = ctx?.height || 512;
|
|
const seed = ctx?.seed ?? 42;
|
|
|
|
const vars = { prompt: intent, w: width, h: height, seed, out: outPath };
|
|
if (model.repo && model.invoke.includes("{model_path}")) {
|
|
const snap = model.repo ? resolveSnapshot(model.repo) : null;
|
|
if (!snap) return null;
|
|
vars.model_path = snap;
|
|
}
|
|
|
|
const argv = buildArgv(model.invoke, vars);
|
|
argv.shift(); // drop the bin (already validated)
|
|
try {
|
|
execFileSync(bin, argv, {
|
|
encoding: "utf8",
|
|
timeout: 1_800_000,
|
|
stdio: ["ignore", "pipe", "pipe"],
|
|
});
|
|
} catch (err) {
|
|
console.error(
|
|
`media-use: local image gen (${model.id}) failed: ${err.stderr?.toString().trim().slice(-200) || err.message}`,
|
|
);
|
|
return null;
|
|
}
|
|
if (!existsSync(outPath)) return null;
|
|
return {
|
|
localPath: outPath,
|
|
ext: ".png",
|
|
source: "generated",
|
|
metadata: {
|
|
description: intent,
|
|
provider: `mflux.${model.id}`,
|
|
provenance: { model: model.id, tier: model.tier, prompt: intent },
|
|
},
|
|
};
|
|
}
|