Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

feat: price fal.ai clips in Creative Director video so a dollar cap can bound them (#9231)

Adam Eivydcf7463ba4ba
diff --git a/server/services/creativeDirector/videoExecution.js b/server/services/creativeDirector/videoExecution.js
index 1e758090a5..e75603be48 100644
--- a/server/services/creativeDirector/videoExecution.js
+++ b/server/services/creativeDirector/videoExecution.js
@@ -5,6 +5,7 @@ import { ServerError } from '../../lib/errorHandler.js';
import { isMediaAdmissionRefusal } from '../mediaJobQueue/admission.js';
import { creativeDirectorVideoLimitsSchema } from '../../lib/creativeDirectorValidation.js';
import { assertVideoOwner } from '../../lib/creativeDirectorVideoReview.js';
+import { FAL_DEFAULT_IMAGE_VIDEO_MODEL, FAL_DEFAULT_TEXT_VIDEO_MODEL, coerceFalVideoSeconds, estimateFalVideoCostUsd, getFalVideoModel } from '../../lib/falVideoModels.js';
const ACTIVE = new Set(['planning', 'rendering', 'stitching']);
const isMediaAttempt = attempt => ['clip', 'audio'].includes(attempt.kind);
@@ -30,6 +31,34 @@ export function effectiveVideoProject(project) {
...(choices.evaluation.type === 'api' ? { evaluation: choices.evaluation } : {}) } };
}
+/**
+ * Catalog price of one fal clip, billed on the length the catalog coerces the
+ * request to. Null (unknown) for an uncurated model id or any other backend.
+ */
+function falClipCostUsd({ modelId, seconds, params = {} }) {
+ const cost = estimateFalVideoCostUsd({ modelId, seconds, resolution: params.resolution,
+ width: params.width, height: params.height, generateAudio: params.generateAudio === true });
+ return cost === null || cost === undefined ? null : cost;
+}
+
+const falModelFor = (modelId, hasSourceImage) => modelId || (hasSourceImage ? FAL_DEFAULT_IMAGE_VIDEO_MODEL : FAL_DEFAULT_TEXT_VIDEO_MODEL);
+
+/**
+ * Preview total for a fal pin: one estimate per treatment scene, or — before a
+ * treatment exists — the target length cut into clips of the model's default
+ * length. Null when any clip is unpriceable.
+ */
+function estimateFalProjectCostUsd(project, video) {
+ const modelId = falModelFor(video.modelId, Boolean(project.startingImageFile));
+ const model = getFalVideoModel(modelId);
+ if (!model) return null;
+ const scenes = project.treatment?.scenes || [];
+ const seconds = scenes.length ? scenes.map(scene => scene.durationSeconds)
+ : Array.from({ length: Math.max(1, Math.ceil((Number(project.targetDurationSeconds) || 0) / (coerceFalVideoSeconds(model, null) || 1))) }, () => null);
+ const costs = seconds.map(value => falClipCostUsd({ modelId, seconds: value }));
+ return costs.some(cost => cost === null) ? null : Math.round(costs.reduce((sum, cost) => sum + cost, 0) * 1e4) / 1e4;
+}
+
async function resolveChoices(project) {
project = { ...project, videoExecution: null };
const { getSettings } = await import('../settings.js');
@@ -78,7 +107,8 @@ async function resolveChoices(project) {
const evaluation = vision ? { type: 'api', providerId: vision.provider.id, model: vision.model || vision.provider.defaultModel || null } : await agentChoice('evaluate');
const { resolveVideoAudioChoice } = await import('./videoAudio.js');
const audio = await resolveVideoAudioChoice(project);
- return { video, treatment, plan, evaluation, audio, costEstimateUsd: null };
+ return { video, treatment, plan, evaluation, audio,
+ costEstimateUsd: pin.mode === 'fal' ? estimateFalProjectCostUsd(project, video) : null };
}
export async function getVideoExecutionPreview(projectId) {
@@ -111,7 +141,9 @@ export async function getVideoExecutionPreview(projectId) {
configurationRevision: canonicalSnapshotChecksum({ input: videoConfigurationRevision(project), choices }),
limits: creativeDirectorVideoLimitsSchema.parse(project.videoExecution?.limits || {}),
execution,
- costNotice: 'Provider prices and balances are unknown. Clip and agent-call limits are enforced; a dollar cap blocks calls whose price cannot be bounded.' };
+ costNotice: choices?.costEstimateUsd != null
+ ? 'Estimated from the fal.ai price catalog; balances are unknown. A dollar cap refuses any clip that would exceed it. Clip and agent-call limits are also enforced.'
+ : 'Provider prices and balances are unknown. Clip and agent-call limits are enforced; a dollar cap blocks calls whose price cannot be bounded.' };
}
async function ownedMutation(projectId, mutate) {
@@ -228,7 +260,14 @@ export async function reserveVideoAttempt(projectId, details) {
if (!isClip && !isAudio && attempts.filter(attempt => !isMediaAttempt(attempt)).length >= limits.maxAgentCalls) blocker = 'The agent-call limit is exhausted. Review limits before Resume.';
if (isAudio && (attempts.filter(attempt => attempt.kind === 'audio').length >= (limits.maxAudioJobs ?? 1) || sameKey.length > limits.maxRetries)) blocker = 'The audio job limit is exhausted. Review limits before Resume.';
if (details.kind === 'plan' && attempts.filter(attempt => attempt.kind === 'plan').length > limits.maxReplans) blocker = 'The replan limit is exhausted. Review limits before Resume.';
- if (limits.spendCapUsd !== null) blocker = 'The next call has unknown cost and cannot fit an enforceable dollar cap.';
+ // Only media is metered against the dollar cap; agent calls stay bounded by
+ // maxAgentCalls. A clip is charged its catalog estimate, and every earlier
+ // attempt counts as spent (a failed one may still have billed).
+ if (limits.spendCapUsd !== null && (isClip || isAudio)) {
+ const cost = isClip ? details.costUsd : null;
+ if (typeof cost !== 'number') blocker = 'The next call has unknown cost and cannot fit an enforceable dollar cap.';
+ else if (attempts.reduce((sum, attempt) => sum + (attempt.costUsd || 0), 0) + cost > limits.spendCapUsd + 1e-9) blocker = 'The next clip would exceed the dollar cap. Review limits before Resume.';
+ }
if (blocker) return { project: { ...project, status: 'paused', failureReason: blocker, videoExecution: { ...execution, blocker }, updatedAt: now() }, result: null };
const attempt = { ...details, id: randomUUID(), executionId: execution.id, productionRevision: project.videoWorkRevision || 0, status: 'submitting', at: now() };
return { project: { ...project, videoExecution: { ...execution, attempts: [...attempts, attempt] }, updatedAt: now() }, result: attempt };
@@ -292,7 +331,9 @@ export async function enqueueVideoProductionJob(project, { kind = 'video', param
validateVideoShot(project, { sceneId: sceneId || stepId, prompt: String(params.prompt || ''), durationSeconds: seconds }, false);
const { hasConfiguredMediaRoute, enqueueUnattendedMediaJob } = await import('../federatedMedia/defaultRouting.js');
if (await hasConfiguredMediaRoute(kind)) throw new ServerError('A standing peer route would change the reviewed video backend. Change Settings before Resume.', { status: 409, code: 'VIDEO_ROUTE_CHANGED' });
- const attempt = await reserveVideoAttempt(project.id, { kind: 'clip', key: sceneId ? `scene:${sceneId}` : `step:${stepId}`, sceneId, stepId, workRevision, expectedProductionRevision: project.videoWorkRevision || 0 });
+ const costUsd = params.mode === 'fal'
+ ? falClipCostUsd({ modelId: falModelFor(params.modelId, Boolean(params.sourceImagePath)), seconds, params }) : null;
+ const attempt = await reserveVideoAttempt(project.id, { kind: 'clip', ...(costUsd !== null ? { costUsd } : {}), key: sceneId ? `scene:${sceneId}` : `step:${stepId}`, sceneId, stepId, workRevision, expectedProductionRevision: project.videoWorkRevision || 0 });
if (!attempt) return null;
const marker = { projectId: project.id, attemptId: attempt.id, executionId: attempt.executionId };
try {
diff --git a/server/services/creativeDirector/videoExecution.test.js b/server/services/creativeDirector/videoExecution.test.js
index 85146f9185..4da9fb80b4 100644
--- a/server/services/creativeDirector/videoExecution.test.js
+++ b/server/services/creativeDirector/videoExecution.test.js
@@ -226,3 +226,36 @@ it('generates one explicitly selected soundtrack, retains its Track, and reuses
expect(state.project.videoExecution.blocker).toMatch(/audio job limit/);
expect(enqueueJob).toHaveBeenCalledTimes(1);
});
+
+const HAILUO_T2V = 'fal-ai/minimax/hailuo-02/standard/text-to-video';
+const falProject = (modelId = HAILUO_T2V) => { state.project.renderBackend = { video: { mode: 'fal', modelId } }; state.settings = { videoGen: { fal: { apiKey: 'example-key' } } }; };
+const enqueueFal = (params = {}, sceneId = 'opening') => enqueueVideoProductionJob(state.project, { sceneId, workRevision: 0,
+ params: { mode: 'fal', videoMode: 'text', modelId: HAILUO_T2V, prompt: 'Example landscape', duration: 6, ...params } });
+
+it('prices a curated fal pin so a dollar cap can start and bound the run', async () => {
+ falProject();
+ const preview = await getVideoExecutionPreview('example-video');
+ expect(preview.canStart).toBe(true);
+ expect(preview.choices.costEstimateUsd).toBeGreaterThan(0);
+ const clip = preview.choices.costEstimateUsd;
+ await startVideoExecution('example-video', await startInput({ spendCapUsd: clip * 1.5 }));
+ expect(await enqueueFal()).toMatchObject({ jobId: 'example-job-1' });
+ const [attempt] = state.project.videoExecution.attempts;
+ expect(attempt.costUsd).toBeGreaterThan(0);
+ // A second clip would push the reserved total past the cap: refused before enqueue.
+ state.project.treatment.scenes.push({ sceneId: 'second', order: 1, status: 'pending', workRevision: 0 });
+ expect(await enqueueFal({}, 'second')).toBeNull();
+ expect(state.project.videoExecution.blocker).toMatch(/exceed the dollar cap/);
+ expect(enqueueUnattendedMediaJob).toHaveBeenCalledTimes(1);
+});
+
+it('still refuses a dollar cap for an uncurated fal model or an unpriced backend', async () => {
+ falProject('fal-ai/example/uncurated');
+ expect((await getVideoExecutionPreview('example-video')).choices.costEstimateUsd).toBeNull();
+ await expect(startVideoExecution('example-video', await startInput({ spendCapUsd: 5 }))).rejects.toMatchObject({ code: 'VIDEO_COST_UNKNOWN' });
+ // A clip whose model the catalog cannot price is refused at reservation.
+ state.project.renderBackend = { video: { mode: 'fal', modelId: HAILUO_T2V } };
+ await startVideoExecution('example-video', await startInput({ spendCapUsd: 5 }));
+ expect(await enqueueFal({ modelId: 'fal-ai/example/uncurated' })).toBeNull();
+ expect(state.project.videoExecution.blocker).toMatch(/unknown cost/);
+});