feat: add universal chat room with language-aware translation
This commit is contained in:
97
utils/chatTranslation.js
Normal file
97
utils/chatTranslation.js
Normal file
@@ -0,0 +1,97 @@
|
||||
const axios = require("axios");
|
||||
|
||||
const DEFAULT_MODEL = process.env.OPENAI_TRANSLATION_MODEL || process.env.OPENAI_MODEL || "gpt-4o-mini";
|
||||
|
||||
const normalizeLanguageCode = (rawLanguage) => {
|
||||
if (!rawLanguage || typeof rawLanguage !== "string") return "en";
|
||||
const firstValue = rawLanguage.split(",")[0].trim().toLowerCase();
|
||||
if (!firstValue) return "en";
|
||||
const noQuality = firstValue.split(";")[0].trim();
|
||||
const shortCode = noQuality.split("-")[0].trim();
|
||||
return shortCode || "en";
|
||||
};
|
||||
|
||||
const extractOutputText = (data) => {
|
||||
if (!data) return "";
|
||||
if (typeof data.output_text === "string" && data.output_text.trim()) {
|
||||
return data.output_text.trim();
|
||||
}
|
||||
if (!Array.isArray(data.output)) return "";
|
||||
const chunks = [];
|
||||
data.output.forEach((item) => {
|
||||
if (!Array.isArray(item?.content)) return;
|
||||
item.content.forEach((entry) => {
|
||||
if (entry?.type === "output_text" && typeof entry?.text === "string") {
|
||||
chunks.push(entry.text);
|
||||
}
|
||||
});
|
||||
});
|
||||
return chunks.join("\n").trim();
|
||||
};
|
||||
|
||||
const translateText = async ({ text, sourceLang, targetLang }) => {
|
||||
const normalizedSource = normalizeLanguageCode(sourceLang);
|
||||
const normalizedTarget = normalizeLanguageCode(targetLang);
|
||||
if (!text || !normalizedTarget || normalizedSource === normalizedTarget) {
|
||||
return {
|
||||
translatedText: text,
|
||||
provider: "none",
|
||||
model: "none",
|
||||
};
|
||||
}
|
||||
|
||||
const apiKey = process.env.OPENAI_API_KEY;
|
||||
if (!apiKey) return null;
|
||||
|
||||
try {
|
||||
const response = await axios.post(
|
||||
"https://api.openai.com/v1/responses",
|
||||
{
|
||||
model: DEFAULT_MODEL,
|
||||
input: [
|
||||
{
|
||||
role: "system",
|
||||
content: [
|
||||
{
|
||||
type: "input_text",
|
||||
text: "You translate chat messages. Keep meaning, tone, emojis, names, and references. Return only the translated text.",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
role: "user",
|
||||
content: [
|
||||
{
|
||||
type: "input_text",
|
||||
text: `Translate this message from ${normalizedSource} to ${normalizedTarget}:\n\n${text}`,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
timeout: 15000,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${apiKey}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
const translatedText = extractOutputText(response?.data);
|
||||
if (!translatedText) return null;
|
||||
return {
|
||||
translatedText,
|
||||
provider: "openai",
|
||||
model: DEFAULT_MODEL,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error translating chat message", error?.response?.data || error?.message || error);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
normalizeLanguageCode,
|
||||
translateText,
|
||||
};
|
||||
Reference in New Issue
Block a user