@braze/sanity-plugin-ai-translation 0.2.52 → 0.2.53
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +33 -20
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1253,7 +1253,9 @@ function restoreTransforms({
|
|
|
1253
1253
|
schemaType,
|
|
1254
1254
|
document: translatedDocument
|
|
1255
1255
|
};
|
|
1256
|
-
devLog.debug("Restoring transform for field", { fieldPath, schemaType })
|
|
1256
|
+
devLog.debug("Restoring transform for field", { fieldPath, schemaType });
|
|
1257
|
+
const restoredValue = transform.fromTranslatable(value, originalValue, context);
|
|
1258
|
+
restoredObj[key] = restoredValue;
|
|
1257
1259
|
} else
|
|
1258
1260
|
restoredObj[key] = restoreTransforms({
|
|
1259
1261
|
translatedDocument: value,
|
|
@@ -1491,9 +1493,9 @@ const globalTransformRegistry = new TransformRegistry(), callGPT5 = async (apiKe
|
|
|
1491
1493
|
}, modelPricing = pricing[model] || pricing["gpt-5"], cachedInputRate = model === "gpt-5.1" ? 0.125 / 1e6 : modelPricing.input;
|
|
1492
1494
|
return Math.max(0, tokens.prompt - tokens.cached) * modelPricing.input + tokens.cached * cachedInputRate + tokens.completion * modelPricing.output + tokens.reasoning * modelPricing.reasoning;
|
|
1493
1495
|
}, deserializeDocument = (originalDoc, reviewedText) => {
|
|
1494
|
-
const parsed = parseReviewFormat(reviewedText), result = cloneDeep(originalDoc);
|
|
1496
|
+
const parsed = parseReviewFormat(reviewedText, originalDoc), result = cloneDeep(originalDoc);
|
|
1495
1497
|
return applyTextChanges(result, parsed), result;
|
|
1496
|
-
}, parseReviewFormat = (text) => {
|
|
1498
|
+
}, parseReviewFormat = (text, originalDoc) => {
|
|
1497
1499
|
const root = [], stack = [], lines = text.split(`
|
|
1498
1500
|
`);
|
|
1499
1501
|
for (let i = 0; i < lines.length; i++) {
|
|
@@ -1526,8 +1528,16 @@ const globalTransformRegistry = new TransformRegistry(), callGPT5 = async (apiKe
|
|
|
1526
1528
|
}
|
|
1527
1529
|
const content = contentLines.join(`
|
|
1528
1530
|
`);
|
|
1529
|
-
let contentType
|
|
1530
|
-
|
|
1531
|
+
let contentType;
|
|
1532
|
+
try {
|
|
1533
|
+
const fieldValue = get(originalDoc, [fieldName]);
|
|
1534
|
+
if (fieldValue && typeof fieldValue == "object" && fieldValue !== null) {
|
|
1535
|
+
const fieldType = fieldValue._type;
|
|
1536
|
+
typeof fieldType == "string" && fieldType.startsWith("translatable.") && (contentType = fieldType);
|
|
1537
|
+
}
|
|
1538
|
+
} catch {
|
|
1539
|
+
}
|
|
1540
|
+
contentType || (content.includes("<block") || content.includes("<span") ? contentType = "translatable.richtext" : contentType = "translatable.vtt.captions");
|
|
1531
1541
|
const node = {
|
|
1532
1542
|
type: "field",
|
|
1533
1543
|
name: fieldName,
|
|
@@ -1564,7 +1574,7 @@ const globalTransformRegistry = new TransformRegistry(), callGPT5 = async (apiKe
|
|
|
1564
1574
|
parent && parent.children.push(node);
|
|
1565
1575
|
} else
|
|
1566
1576
|
root.push(node);
|
|
1567
|
-
else if (attributes.type
|
|
1577
|
+
else if (attributes.type && typeof attributes.type == "string" && attributes.type.startsWith("translatable.")) {
|
|
1568
1578
|
const contentLines = [];
|
|
1569
1579
|
for (i++; i < lines.length; ) {
|
|
1570
1580
|
const currentLine = lines[i];
|
|
@@ -1626,14 +1636,13 @@ const globalTransformRegistry = new TransformRegistry(), callGPT5 = async (apiKe
|
|
|
1626
1636
|
applyFieldChanges(doc, path, node);
|
|
1627
1637
|
}
|
|
1628
1638
|
}, applyFieldChanges = (doc, path, node) => {
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
fieldValue && typeof fieldValue == "object" && fieldValue._type === "translatable.vtt.captions" && node.text && set(doc, [...path, "translationText"], node.text);
|
|
1632
|
-
return;
|
|
1633
|
-
}
|
|
1634
|
-
if (node.attributes.type === "translatable.richtext") {
|
|
1639
|
+
const nodeType = node.attributes.type;
|
|
1640
|
+
if (typeof nodeType == "string" && nodeType.startsWith("translatable.")) {
|
|
1635
1641
|
const fieldValue = get(doc, path);
|
|
1636
|
-
fieldValue && typeof fieldValue == "object" && fieldValue
|
|
1642
|
+
if (fieldValue && typeof fieldValue == "object" && fieldValue !== null) {
|
|
1643
|
+
const fieldType = fieldValue._type;
|
|
1644
|
+
typeof fieldType == "string" && fieldType === nodeType && node.text && set(doc, [...path, "translationText"], node.text);
|
|
1645
|
+
}
|
|
1637
1646
|
return;
|
|
1638
1647
|
}
|
|
1639
1648
|
if (node.attributes.type === "array") {
|
|
@@ -1721,12 +1730,9 @@ const globalTransformRegistry = new TransformRegistry(), callGPT5 = async (apiKe
|
|
|
1721
1730
|
return;
|
|
1722
1731
|
}
|
|
1723
1732
|
if (isPlainObject(value)) {
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
}
|
|
1728
|
-
if (value._type === "translatable.richtext" && typeof value.translationText == "string") {
|
|
1729
|
-
lines.push(`${indentStr}--- ${fieldName} ---`), lines.push(value.translationText), lines.push(`${indentStr}--- /${fieldName} ---`);
|
|
1733
|
+
const valueObj = value, valueType = valueObj._type, translationText = valueObj.translationText;
|
|
1734
|
+
if (typeof valueType == "string" && valueType.startsWith("translatable.") && typeof translationText == "string") {
|
|
1735
|
+
lines.push(`${indentStr}--- ${fieldName} ---`), lines.push(translationText), lines.push(`${indentStr}--- /${fieldName} ---`);
|
|
1730
1736
|
return;
|
|
1731
1737
|
}
|
|
1732
1738
|
lines.push(`${indentStr}<field name="${fieldName}" type="object">`);
|
|
@@ -3984,7 +3990,14 @@ function parseSRTContent(content) {
|
|
|
3984
3990
|
contentPreview: content.substring(0, 100) + (content.length > 100 ? "..." : "")
|
|
3985
3991
|
}), !content || content.trim() === "")
|
|
3986
3992
|
return [];
|
|
3987
|
-
|
|
3993
|
+
let normalizedContent = content;
|
|
3994
|
+
content.includes("\\n") && !content.includes(`
|
|
3995
|
+
`) && (normalizedContent = content.replace(/\\n/g, `
|
|
3996
|
+
`), devLog.debug("Unescaped newlines in SRT content", {
|
|
3997
|
+
originalLength: content.length,
|
|
3998
|
+
normalizedLength: normalizedContent.length
|
|
3999
|
+
}));
|
|
4000
|
+
const cues = [], cueBlocks = normalizedContent.split(/\n\s*\n/);
|
|
3988
4001
|
for (const block of cueBlocks) {
|
|
3989
4002
|
const lines = block.trim().split(`
|
|
3990
4003
|
`);
|