@braze/sanity-plugin-ai-translation 0.2.57 → 0.2.59
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 +57 -4
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1539,7 +1539,12 @@ const globalTransformRegistry = new TransformRegistry(), callGPT5 = async (apiKe
|
|
|
1539
1539
|
cleanedLength: cleanedText.length,
|
|
1540
1540
|
preview: cleanedText.substring(0, 500)
|
|
1541
1541
|
});
|
|
1542
|
-
const parsed = parseReviewFormat(cleanedText, originalDoc)
|
|
1542
|
+
const parsed = parseReviewFormat(cleanedText, originalDoc);
|
|
1543
|
+
devLog.debug("Parsed response structure", {
|
|
1544
|
+
rootNodeCount: parsed.length,
|
|
1545
|
+
rootNodeNames: parsed.map((n) => n.type === "field" && n.name ? n.name : "unknown")
|
|
1546
|
+
});
|
|
1547
|
+
const result = cloneDeep(originalDoc);
|
|
1543
1548
|
return applyTextChanges(result, parsed), result;
|
|
1544
1549
|
}, preprocessResponse = (text) => {
|
|
1545
1550
|
const originalLength = text.length;
|
|
@@ -1559,11 +1564,26 @@ const globalTransformRegistry = new TransformRegistry(), callGPT5 = async (apiKe
|
|
|
1559
1564
|
}, parseReviewFormat = (text, originalDoc) => {
|
|
1560
1565
|
const root = [], stack = [], lines = text.split(`
|
|
1561
1566
|
`);
|
|
1567
|
+
devLog.debug("Parsing response text", {
|
|
1568
|
+
lineCount: lines.length,
|
|
1569
|
+
firstLines: lines.slice(0, 10).map((l, idx) => ({ line: idx, content: l.substring(0, 100) })),
|
|
1570
|
+
hasFieldTags: text.includes("<field"),
|
|
1571
|
+
hasBlockTags: text.includes("<block"),
|
|
1572
|
+
hasSpanTags: text.includes("<span"),
|
|
1573
|
+
textLength: text.length,
|
|
1574
|
+
textPreview: text.substring(0, 500)
|
|
1575
|
+
});
|
|
1576
|
+
let processedLineCount = 0, skippedEmptyLineCount = 0, matchedPatternCount = 0;
|
|
1562
1577
|
for (let i = 0; i < lines.length; i++) {
|
|
1563
1578
|
const trimmed = lines[i].trim();
|
|
1564
|
-
if (!trimmed)
|
|
1579
|
+
if (!trimmed) {
|
|
1580
|
+
skippedEmptyLineCount++;
|
|
1581
|
+
continue;
|
|
1582
|
+
}
|
|
1583
|
+
processedLineCount++;
|
|
1565
1584
|
const compactStringMatch = trimmed.match(/^(\w+):\s*(.*)$/);
|
|
1566
1585
|
if (compactStringMatch && !trimmed.includes("<")) {
|
|
1586
|
+
matchedPatternCount++;
|
|
1567
1587
|
const [, fieldName, text2] = compactStringMatch, node = {
|
|
1568
1588
|
type: "field",
|
|
1569
1589
|
name: fieldName,
|
|
@@ -1580,6 +1600,7 @@ const globalTransformRegistry = new TransformRegistry(), callGPT5 = async (apiKe
|
|
|
1580
1600
|
}
|
|
1581
1601
|
const compactStartMatch = trimmed.match(/^---\s+(\w+)\s+---$/);
|
|
1582
1602
|
if (compactStartMatch) {
|
|
1603
|
+
matchedPatternCount++;
|
|
1583
1604
|
const fieldName = compactStartMatch[1], contentLines = [];
|
|
1584
1605
|
for (i++; i < lines.length; ) {
|
|
1585
1606
|
const currentLine = lines[i];
|
|
@@ -1615,6 +1636,12 @@ const globalTransformRegistry = new TransformRegistry(), callGPT5 = async (apiKe
|
|
|
1615
1636
|
}
|
|
1616
1637
|
const openMatch = trimmed.match(/^<(field|block|span)([^>]*)>(.*)$/);
|
|
1617
1638
|
if (openMatch) {
|
|
1639
|
+
matchedPatternCount++, devLog.debug("Matched opening tag", {
|
|
1640
|
+
line: i,
|
|
1641
|
+
tagName: openMatch[1],
|
|
1642
|
+
attributes: openMatch[2],
|
|
1643
|
+
contentPreview: openMatch[3]?.substring(0, 50)
|
|
1644
|
+
});
|
|
1618
1645
|
const [, tagName, attrsStr, content] = openMatch, attributes = parseAttributes(attrsStr), node = {
|
|
1619
1646
|
type: tagName,
|
|
1620
1647
|
name: attributes.name,
|
|
@@ -1674,6 +1701,7 @@ const globalTransformRegistry = new TransformRegistry(), callGPT5 = async (apiKe
|
|
|
1674
1701
|
continue;
|
|
1675
1702
|
}
|
|
1676
1703
|
if (trimmed.match(/^<\/(field|block|span)>$/) && stack.length > 0) {
|
|
1704
|
+
matchedPatternCount++;
|
|
1677
1705
|
const node = stack.pop();
|
|
1678
1706
|
if (node)
|
|
1679
1707
|
if (stack.length > 0) {
|
|
@@ -1681,9 +1709,24 @@ const globalTransformRegistry = new TransformRegistry(), callGPT5 = async (apiKe
|
|
|
1681
1709
|
parent && parent.children.push(node);
|
|
1682
1710
|
} else
|
|
1683
1711
|
root.push(node);
|
|
1712
|
+
continue;
|
|
1684
1713
|
}
|
|
1714
|
+
processedLineCount <= 20 && devLog.debug("Line did not match any pattern", {
|
|
1715
|
+
line: i,
|
|
1716
|
+
trimmed,
|
|
1717
|
+
startsWithLessThan: trimmed.startsWith("<"),
|
|
1718
|
+
startsWithField: trimmed.startsWith("<field"),
|
|
1719
|
+
startsWithBacktick: trimmed.startsWith("`")
|
|
1720
|
+
});
|
|
1685
1721
|
}
|
|
1686
|
-
return
|
|
1722
|
+
return devLog.debug("Parse complete", {
|
|
1723
|
+
totalLines: lines.length,
|
|
1724
|
+
processedLines: processedLineCount,
|
|
1725
|
+
skippedEmptyLines: skippedEmptyLineCount,
|
|
1726
|
+
rootNodeCount: root.length,
|
|
1727
|
+
stackLength: stack.length,
|
|
1728
|
+
matchedPatterns: matchedPatternCount
|
|
1729
|
+
}), root;
|
|
1687
1730
|
}, parseAttributes = (attrStr) => {
|
|
1688
1731
|
const attrs = {}, attrRegex = /(\w+)="([^"]*)"/g;
|
|
1689
1732
|
let match = attrRegex.exec(attrStr);
|
|
@@ -1691,10 +1734,20 @@ const globalTransformRegistry = new TransformRegistry(), callGPT5 = async (apiKe
|
|
|
1691
1734
|
attrs[match[1]] = unescapeXml(match[2]), match = attrRegex.exec(attrStr);
|
|
1692
1735
|
return attrs;
|
|
1693
1736
|
}, unescapeXml = (str) => str.replace(/'/g, "'").replace(/"/g, '"').replace(/>/g, ">").replace(/</g, "<").replace(/&/g, "&"), applyTextChanges = (doc, parsedNodes) => {
|
|
1737
|
+
devLog.debug("Applying text changes from parsed nodes", {
|
|
1738
|
+
nodeCount: parsedNodes.length,
|
|
1739
|
+
nodeNames: parsedNodes.map((n) => n.type === "field" && n.name ? n.name : "unknown")
|
|
1740
|
+
});
|
|
1694
1741
|
for (const node of parsedNodes)
|
|
1695
1742
|
if (node.type === "field" && node.name) {
|
|
1696
|
-
const path = [node.name];
|
|
1743
|
+
const path = [node.name], beforeValue = get(doc, path);
|
|
1697
1744
|
applyFieldChanges(doc, path, node);
|
|
1745
|
+
const afterValue = get(doc, path);
|
|
1746
|
+
beforeValue !== afterValue && devLog.debug("Field updated during deserialization", {
|
|
1747
|
+
path: path.join("."),
|
|
1748
|
+
before: typeof beforeValue == "string" ? beforeValue.substring(0, 50) : beforeValue,
|
|
1749
|
+
after: typeof afterValue == "string" ? afterValue.substring(0, 50) : afterValue
|
|
1750
|
+
});
|
|
1698
1751
|
}
|
|
1699
1752
|
}, applyFieldChanges = (doc, path, node) => {
|
|
1700
1753
|
const nodeType = node.attributes.type;
|