Add draft caption ingest support and dev watch workflow
This commit is contained in:
@@ -6,7 +6,7 @@ const sessionChecker = require("../middleware/sessionChecker.js");
|
||||
const MAX_BUFFER_SIZE = 300;
|
||||
const DEFAULT_INITIAL_LIMIT = 40;
|
||||
const MAX_INITIAL_LIMIT = 120;
|
||||
const CAPTION_META_KEYS = new Set(["sequence", "createdAt", "original"]);
|
||||
const CAPTION_META_KEYS = new Set(["sequence", "createdAt", "original", "draft", "sourceLang", "lang", "isDraft", "status", "translations"]);
|
||||
|
||||
const liveCaptionState = {
|
||||
startedAt: Date.now(),
|
||||
@@ -33,8 +33,23 @@ const normalizeTranslations = (translations) => {
|
||||
return normalized;
|
||||
};
|
||||
|
||||
const readText = (value) => {
|
||||
if (typeof value === "string") return value.trim();
|
||||
return "";
|
||||
};
|
||||
|
||||
const extractDraftText = (body = {}) => {
|
||||
const directDraft = readText(body?.draft);
|
||||
if (directDraft) return directDraft;
|
||||
const nestedDraft = readText(body?.draft?.text);
|
||||
if (nestedDraft) return nestedDraft;
|
||||
const fallbackText = readText(body?.text);
|
||||
if (fallbackText) return fallbackText;
|
||||
return "";
|
||||
};
|
||||
|
||||
const buildTranslationsFromFlatPayload = (payload) => {
|
||||
const ignoredKeys = new Set(["original", "sourceLang", "translations"]);
|
||||
const ignoredKeys = new Set(["original", "draft", "sourceLang", "lang", "isDraft", "status", "translations"]);
|
||||
const normalized = {};
|
||||
for (const [key, value] of Object.entries(payload || {})) {
|
||||
if (ignoredKeys.has(key)) continue;
|
||||
@@ -103,16 +118,22 @@ router.get("/stream", sessionChecker, async (req, res) => {
|
||||
router.post("/ingest", async (req, res) => {
|
||||
try {
|
||||
// TODO: Add basic auth/API key validation before production roll-out.
|
||||
const original = typeof req.body?.original === "string" ? req.body.original.trim() : "";
|
||||
const draft = extractDraftText(req.body || {});
|
||||
const originalFromPayload = readText(req.body?.original);
|
||||
const original = originalFromPayload || draft;
|
||||
const requestedLang = normalizeLang(req.body?.lang);
|
||||
const sourceLangFromRequest = normalizeLang(req.body?.sourceLang || (requestedLang && requestedLang !== "draft" ? requestedLang : ""));
|
||||
const isDraft = !!draft || requestedLang === "draft" || sourceLangFromRequest === "draft" || req.body?.isDraft === true || req.body?.status === "draft";
|
||||
const mapFromNested = normalizeTranslations(req.body?.translations);
|
||||
const mapFromFlat = buildTranslationsFromFlatPayload(req.body);
|
||||
const translations = { ...mapFromNested, ...mapFromFlat };
|
||||
const sourceLang = normalizeLang(req.body?.sourceLang || inferSourceLangFromTranslations(original, translations));
|
||||
const translations = isDraft ? {} : { ...mapFromNested, ...mapFromFlat };
|
||||
const inferredSource = inferSourceLangFromTranslations(original, translations);
|
||||
const sourceLang = isDraft ? "" : (sourceLangFromRequest || inferredSource);
|
||||
|
||||
if (!original) {
|
||||
return res.status(400).json({ status: "Original text is required" });
|
||||
}
|
||||
if (sourceLang && sourceLang !== "original" && !translations[sourceLang]) {
|
||||
if (sourceLang && sourceLang !== "original" && sourceLang !== "draft" && !translations[sourceLang]) {
|
||||
translations[sourceLang] = original;
|
||||
}
|
||||
|
||||
@@ -121,6 +142,10 @@ router.post("/ingest", async (req, res) => {
|
||||
sequence,
|
||||
createdAt: new Date().toISOString(),
|
||||
original,
|
||||
sourceLang: sourceLang || undefined,
|
||||
lang: isDraft ? "draft" : (sourceLang || undefined),
|
||||
isDraft,
|
||||
status: isDraft ? "draft" : "final",
|
||||
...translations,
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user