codex/live-captions #5
+36
-7
@@ -6,6 +6,7 @@ const sessionChecker = require("../middleware/sessionChecker.js");
|
|||||||
const MAX_BUFFER_SIZE = 300;
|
const MAX_BUFFER_SIZE = 300;
|
||||||
const DEFAULT_INITIAL_LIMIT = 40;
|
const DEFAULT_INITIAL_LIMIT = 40;
|
||||||
const MAX_INITIAL_LIMIT = 120;
|
const MAX_INITIAL_LIMIT = 120;
|
||||||
|
const CAPTION_META_KEYS = new Set(["sequence", "createdAt", "original"]);
|
||||||
|
|
||||||
const liveCaptionState = {
|
const liveCaptionState = {
|
||||||
startedAt: Date.now(),
|
startedAt: Date.now(),
|
||||||
@@ -32,12 +33,36 @@ const normalizeTranslations = (translations) => {
|
|||||||
return normalized;
|
return normalized;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const buildTranslationsFromFlatPayload = (payload) => {
|
||||||
|
const ignoredKeys = new Set(["original", "sourceLang", "translations"]);
|
||||||
|
const normalized = {};
|
||||||
|
for (const [key, value] of Object.entries(payload || {})) {
|
||||||
|
if (ignoredKeys.has(key)) continue;
|
||||||
|
const lang = normalizeLang(key);
|
||||||
|
const text = typeof value === "string" ? value.trim() : "";
|
||||||
|
if (!lang || !text) continue;
|
||||||
|
normalized[lang] = text;
|
||||||
|
}
|
||||||
|
return normalized;
|
||||||
|
};
|
||||||
|
|
||||||
|
const inferSourceLangFromTranslations = (original, translations) => {
|
||||||
|
const normalizedOriginal = String(original || "").trim();
|
||||||
|
if (!normalizedOriginal) return "original";
|
||||||
|
for (const [lang, text] of Object.entries(translations || {})) {
|
||||||
|
if (String(text || "").trim() === normalizedOriginal) return normalizeLang(lang);
|
||||||
|
}
|
||||||
|
return "original";
|
||||||
|
};
|
||||||
|
|
||||||
const getAvailableLanguages = () => {
|
const getAvailableLanguages = () => {
|
||||||
const langs = new Set();
|
const langs = new Set();
|
||||||
for (const caption of liveCaptionState.captions) {
|
for (const caption of liveCaptionState.captions) {
|
||||||
if (caption?.sourceLang) langs.add(caption.sourceLang);
|
Object.keys(caption || {}).forEach((key) => {
|
||||||
const translationMap = caption?.translations || {};
|
if (CAPTION_META_KEYS.has(key)) return;
|
||||||
Object.keys(translationMap).forEach((lang) => langs.add(normalizeLang(lang)));
|
const lang = normalizeLang(key);
|
||||||
|
if (lang) langs.add(lang);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
return Array.from(langs).filter(Boolean).sort();
|
return Array.from(langs).filter(Boolean).sort();
|
||||||
};
|
};
|
||||||
@@ -79,20 +104,24 @@ router.post("/ingest", async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
// TODO: Add basic auth/API key validation before production roll-out.
|
// TODO: Add basic auth/API key validation before production roll-out.
|
||||||
const original = typeof req.body?.original === "string" ? req.body.original.trim() : "";
|
const original = typeof req.body?.original === "string" ? req.body.original.trim() : "";
|
||||||
const sourceLang = normalizeLang(req.body?.sourceLang || "original");
|
const mapFromNested = normalizeTranslations(req.body?.translations);
|
||||||
const translations = normalizeTranslations(req.body?.translations);
|
const mapFromFlat = buildTranslationsFromFlatPayload(req.body);
|
||||||
|
const translations = { ...mapFromNested, ...mapFromFlat };
|
||||||
|
const sourceLang = normalizeLang(req.body?.sourceLang || inferSourceLangFromTranslations(original, translations));
|
||||||
|
|
||||||
if (!original) {
|
if (!original) {
|
||||||
return res.status(400).json({ status: "Original text is required" });
|
return res.status(400).json({ status: "Original text is required" });
|
||||||
}
|
}
|
||||||
|
if (sourceLang && sourceLang !== "original" && !translations[sourceLang]) {
|
||||||
|
translations[sourceLang] = original;
|
||||||
|
}
|
||||||
|
|
||||||
const sequence = liveCaptionState.latestSequence + 1;
|
const sequence = liveCaptionState.latestSequence + 1;
|
||||||
const caption = {
|
const caption = {
|
||||||
sequence,
|
sequence,
|
||||||
createdAt: new Date().toISOString(),
|
createdAt: new Date().toISOString(),
|
||||||
sourceLang,
|
|
||||||
original,
|
original,
|
||||||
translations,
|
...translations,
|
||||||
};
|
};
|
||||||
|
|
||||||
liveCaptionState.latestSequence = sequence;
|
liveCaptionState.latestSequence = sequence;
|
||||||
|
|||||||
@@ -10,35 +10,27 @@ const intervalMs = 5000;
|
|||||||
const samples = [
|
const samples = [
|
||||||
{
|
{
|
||||||
original: "Bienvenidos a nuestro servicio de adoracion.",
|
original: "Bienvenidos a nuestro servicio de adoracion.",
|
||||||
sourceLang: "es",
|
es: "Bienvenidos a nuestro servicio de adoracion.",
|
||||||
translations: {
|
en: "Welcome to our worship service.",
|
||||||
en: "Welcome to our worship service.",
|
fr: "Bienvenue a notre service de louange.",
|
||||||
fr: "Bienvenue a notre service de louange.",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
original: "Leamos juntos en el Salmo 23.",
|
original: "Leamos juntos en el Salmo 23.",
|
||||||
sourceLang: "es",
|
es: "Leamos juntos en el Salmo 23.",
|
||||||
translations: {
|
en: "Let us read together in Psalm 23.",
|
||||||
en: "Let us read together in Psalm 23.",
|
fr: "Lisons ensemble le Psaume 23.",
|
||||||
fr: "Lisons ensemble le Psaume 23.",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
original: "Dios es fiel en todo tiempo.",
|
original: "Dios es fiel en todo tiempo.",
|
||||||
sourceLang: "es",
|
es: "Dios es fiel en todo tiempo.",
|
||||||
translations: {
|
en: "God is faithful at all times.",
|
||||||
en: "God is faithful at all times.",
|
fr: "Dieu est fidele en tout temps.",
|
||||||
fr: "Dieu est fidele en tout temps.",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
original: "Tomemos un momento para orar.",
|
original: "Tomemos un momento para orar.",
|
||||||
sourceLang: "es",
|
es: "Tomemos un momento para orar.",
|
||||||
translations: {
|
en: "Let us take a moment to pray.",
|
||||||
en: "Let us take a moment to pray.",
|
fr: "Prenons un moment pour prier.",
|
||||||
fr: "Prenons un moment pour prier.",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user