Open-Generative-AI/lib/csvParser.js
Anuragp22 24e4bb40e5 feat: background worker, prompt template, section switcher, home hub
Bundles three changes that were originally landed as stacked PRs into
peer branches and never reached main. Combined here as a single PR
targeting main.

worker/index.js (new)
---------------------
Long-lived Node process that:
- Polls Postgres every 2s for batches in status='running'.
- Claims queued jobs with SELECT ... FOR UPDATE SKIP LOCKED (single
  worker today, ready for N-worker scale tomorrow with no further
  code change).
- Re-uploads local-only trainer images (/api/uploads/...) to MuAPI's
  /upload_file once and caches the resulting CDN URL on the trainer
  row so future jobs reuse it.
- Submits POST /api/v1/<batch.model> with prompt, images_list,
  aspect_ratio, duration, quality.
- Polls /api/v1/predictions/:id/result, extracts videoUrl from
  outputs[0]/url/output.url/video_url on success.
- Backoff: min(10 * 3^retries, 300)s. After retries >= 3, status=
  'failed', waiting for manual Retry from the UI.
- Recovery on boot: re-queues anything left in 'submitting' from a
  crashed run; 'polling' jobs keep their muapiRequestId.

Configuration: MUAPI_API_KEY (required to do work), MUAPI_BASE_URL
(default https://api.muapi.ai), WORKER_TICK_MS (default 2000),
UPLOAD_DIR (default /data/uploads).

worker/package.json: { "type": "module" } so plain Node 20 runs the
ESM imports.

Dockerfile: copy /app/worker into the runner stage.

docker-compose.override.yml: replace placeholder with the real
'npx prisma generate && node worker/index.js'.

lib/promptTemplate.js (new) + worker integration
------------------------------------------------
renderPrompt({trainer, studio, job}) wraps each row's raw practice
description in a fixed narrative template that pins identity,
biomechanics, environment, lighting, clothing, expression, video
style, and a list of forbidden behaviours (no camera movement, no
cuts, no limb warping, no pose distortion, etc).

duration, aspect_ratio, and quality remain SEPARATE fields in the
MuAPI request body; they are never injected into the prompt text.

lib/csvParser.js: stops appending 'Start position: ... Camera: ...'
into the stored prompt. Just stores the raw practice description;
startPosition + cameraAngle remain available as their own columns
for the renderer to consume.

components/SectionSwitcher.jsx (new)
------------------------------------
Two-pill switcher (Studio · Batch) rendered in the top-left of every
shell. Active pill is yellow on black. Click writes the choice to
localStorage so the home hub can highlight 'Last used'.

StandaloneShell, BatchShell, BatchDetail headers all updated to host
the switcher next to the logo. The redundant '← Studio' link in
BatchShell's right-side actions is removed since the switcher
replaces it.

app/page.js + components/HomeHub.jsx
------------------------------------
/ used to redirect to /studio. Now serves a hub with two cards:
Batch (CSV-driven automation) and Studio (one-off generations).
The card the user last opened gets a yellow accent + 'Last used'
tag from localStorage.

Stub-package copy
-----------------
packages/ai-agent and packages/workflow-ui description strings and
runtime 'feature unavailable' notices: removed organization-specific
phrasing in favour of generic 'this fork' wording.
2026-04-23 09:41:55 +05:30

86 lines
2.7 KiB
JavaScript

import Papa from 'papaparse';
// Maps the Rasika-style somatic-practice CSV into our Job shape.
// Expected columns (case-insensitive, trimmed):
// "Video Generation Model", "Character", "Practice Name",
// "Practice Description", "Start Position", "Time (Duration)",
// "Studio", "Camera Angle", "Video Quality", "Status"
//
// Only Character, Practice Name, and Practice Description are required.
const REQUIRED_COLUMNS = ['Character', 'Practice Name', 'Practice Description'];
export function parseBatchCsv(text) {
const result = Papa.parse(text, {
header: true,
skipEmptyLines: true,
transformHeader: (h) => h.trim(),
});
if (result.errors && result.errors.length > 0) {
const first = result.errors[0];
throw new Error(`CSV parse error at row ${first.row}: ${first.message}`);
}
const headers = result.meta.fields || [];
const missing = REQUIRED_COLUMNS.filter(
(col) => !headers.some((h) => h.toLowerCase() === col.toLowerCase()),
);
if (missing.length > 0) {
throw new Error(`Missing required column(s): ${missing.join(', ')}`);
}
const rows = result.data.map((raw, idx) => normaliseRow(raw, idx));
return { rows, headers };
}
function normaliseRow(raw, idx) {
const get = (name) => {
const key = Object.keys(raw).find((k) => k.toLowerCase() === name.toLowerCase());
return key ? (raw[key] ?? '').toString().trim() : '';
};
const character = get('Character');
const practiceName = get('Practice Name');
const description = get('Practice Description');
const startPosition = get('Start Position');
const cameraAngle = get('Camera Angle');
const studio = get('Studio');
const timeStr = get('Time (Duration)');
const qualityStr = get('Video Quality');
return {
rowIndex: idx,
practiceName,
characterLabel: character,
studioLabel: studio || null,
// Worker renders the full template (lib/promptTemplate.js) at submit
// time using trainer, studio, and the structured fields below. We just
// store the raw practice description here so the prompt-builder has
// clean inputs to work with.
prompt: description,
rawDescription: description,
startPosition: startPosition || null,
cameraAngle: cameraAngle || null,
duration: parseDuration(timeStr),
quality: parseQuality(qualityStr),
};
}
function parseDuration(s) {
if (!s) return 15;
const m = s.match(/(\d+)/);
if (!m) return 15;
const n = parseInt(m[1], 10);
// Seedance 2.0 i2v supports 5, 10, 15. Snap to nearest valid.
if (n <= 5) return 5;
if (n <= 10) return 10;
return 15;
}
function parseQuality(s) {
if (!s) return 'basic';
const lower = s.toLowerCase();
if (lower.includes('1080') || lower === 'high') return 'high';
return 'basic';
}