Add draft caption ingest support and dev watch workflow

This commit is contained in:
Adolfo Reyna
2026-02-28 21:30:38 -05:00
parent f0afa200b1
commit 8aa1f3addd
3 changed files with 52 additions and 11 deletions

View File

@@ -6,6 +6,7 @@
"scripts": {
"test": "npx mocha test/auth.test.js",
"start": "node index.js",
"dev": "node --watch index.js",
"live-captions:test-sender": "node scripts/liveCaptionsTestSender.js",
"docker": "docker compose up -d",
"docker_restore": "docker-compose exec mongo mongorestore --db EMI_SOCIAL /dump/EMI_SOCIAL/",

View File

@@ -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,
};

View File

@@ -5,7 +5,7 @@ const axios = require("axios");
const baseUrl = (process.env.CAPTION_TEST_BASE_URL || process.env.BASE_URL || "http://localhost:3000").replace(/\/+$/, "");
const ingestUrl = `${baseUrl}/live-captions/ingest`;
const intervalMs = 5000;
const intervalMs = 6000;
const samples = [
{
@@ -37,9 +37,8 @@ const samples = [
let sampleIndex = 0;
let timer = null;
const sendNextSample = async () => {
const payload = samples[sampleIndex];
sampleIndex = (sampleIndex + 1) % samples.length;
const postPayload = async (payload) => {
const kind = payload?.draft ? "draft" : "final";
try {
const response = await axios.post(ingestUrl, payload, {
@@ -47,7 +46,8 @@ const sendNextSample = async () => {
timeout: 10000,
});
const seq = response?.data?.caption?.sequence || response?.data?.latestSequence || "?";
console.log(`[live-captions:test-sender] sent sequence=${seq} original="${payload.original}"`);
const text = payload?.draft || payload?.original || "";
console.log(`[live-captions:test-sender] sent ${kind} sequence=${seq} text="${text}"`);
} catch (error) {
const status = error?.response?.status;
const body = error?.response?.data;
@@ -56,6 +56,21 @@ const sendNextSample = async () => {
}
};
const sendNextSample = async () => {
const payload = samples[sampleIndex];
const draftWords = String(payload?.original || "").split(" ").filter(Boolean);
if (draftWords.length > 2) {
await postPayload({ draft: draftWords.slice(0, 2).join(" ") });
await new Promise((resolve) => setTimeout(resolve, 550));
await postPayload({ draft: draftWords.slice(0, 4).join(" ") });
await new Promise((resolve) => setTimeout(resolve, 550));
}
await postPayload(payload);
sampleIndex = (sampleIndex + 1) % samples.length;
};
const start = async () => {
console.log(`[live-captions:test-sender] posting to ${ingestUrl} every ${intervalMs / 1000}s`);
await sendNextSample();