@braze/sanity-plugin-ai-translation 0.2.56 → 0.2.58
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/README.md +394 -191
- package/dist/index.js +44 -2
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1527,8 +1527,40 @@ const globalTransformRegistry = new TransformRegistry(), callGPT5 = async (apiKe
|
|
|
1527
1527
|
}, modelPricing = pricing[model] || pricing["gpt-5"], cachedInputRate = model === "gpt-5.1" ? 0.125 / 1e6 : modelPricing.input;
|
|
1528
1528
|
return Math.max(0, tokens.prompt - tokens.cached) * modelPricing.input + tokens.cached * cachedInputRate + tokens.completion * modelPricing.output + tokens.reasoning * modelPricing.reasoning;
|
|
1529
1529
|
}, deserializeDocument = (originalDoc, reviewedText) => {
|
|
1530
|
-
|
|
1530
|
+
devLog.debug("Raw GPT-5 response received", {
|
|
1531
|
+
length: reviewedText.length,
|
|
1532
|
+
preview: reviewedText.substring(0, 1e3),
|
|
1533
|
+
hasReasoning: reviewedText.includes("<reasoning>"),
|
|
1534
|
+
hasCodeBlocks: reviewedText.includes("```")
|
|
1535
|
+
});
|
|
1536
|
+
const cleanedText = preprocessResponse(reviewedText);
|
|
1537
|
+
cleanedText.length !== reviewedText.length && devLog.debug("Response after preprocessing", {
|
|
1538
|
+
originalLength: reviewedText.length,
|
|
1539
|
+
cleanedLength: cleanedText.length,
|
|
1540
|
+
preview: cleanedText.substring(0, 500)
|
|
1541
|
+
});
|
|
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);
|
|
1531
1548
|
return applyTextChanges(result, parsed), result;
|
|
1549
|
+
}, preprocessResponse = (text) => {
|
|
1550
|
+
const originalLength = text.length;
|
|
1551
|
+
let cleaned = text;
|
|
1552
|
+
const beforeReasoning = cleaned.length;
|
|
1553
|
+
cleaned = cleaned.replace(/<reasoning>[\s\S]*?<\/reasoning>/gi, "");
|
|
1554
|
+
const reasoningRemoved = beforeReasoning !== cleaned.length, beforeCodeBlocks = cleaned.length;
|
|
1555
|
+
cleaned = cleaned.replace(/^```[a-z]*\s*$/gim, ""), cleaned = cleaned.replace(/^```\s*$/gim, "");
|
|
1556
|
+
const codeBlocksRemoved = beforeCodeBlocks !== cleaned.length, finalLength = cleaned.trim().length;
|
|
1557
|
+
return (reasoningRemoved || codeBlocksRemoved) && devLog.debug("Preprocessed GPT-5 response", {
|
|
1558
|
+
originalLength,
|
|
1559
|
+
finalLength,
|
|
1560
|
+
reasoningRemoved,
|
|
1561
|
+
codeBlocksRemoved,
|
|
1562
|
+
preview: cleaned.trim().substring(0, 200)
|
|
1563
|
+
}), cleaned.trim();
|
|
1532
1564
|
}, parseReviewFormat = (text, originalDoc) => {
|
|
1533
1565
|
const root = [], stack = [], lines = text.split(`
|
|
1534
1566
|
`);
|
|
@@ -1664,10 +1696,20 @@ const globalTransformRegistry = new TransformRegistry(), callGPT5 = async (apiKe
|
|
|
1664
1696
|
attrs[match[1]] = unescapeXml(match[2]), match = attrRegex.exec(attrStr);
|
|
1665
1697
|
return attrs;
|
|
1666
1698
|
}, unescapeXml = (str) => str.replace(/'/g, "'").replace(/"/g, '"').replace(/>/g, ">").replace(/</g, "<").replace(/&/g, "&"), applyTextChanges = (doc, parsedNodes) => {
|
|
1699
|
+
devLog.debug("Applying text changes from parsed nodes", {
|
|
1700
|
+
nodeCount: parsedNodes.length,
|
|
1701
|
+
nodeNames: parsedNodes.map((n) => n.type === "field" && n.name ? n.name : "unknown")
|
|
1702
|
+
});
|
|
1667
1703
|
for (const node of parsedNodes)
|
|
1668
1704
|
if (node.type === "field" && node.name) {
|
|
1669
|
-
const path = [node.name];
|
|
1705
|
+
const path = [node.name], beforeValue = get(doc, path);
|
|
1670
1706
|
applyFieldChanges(doc, path, node);
|
|
1707
|
+
const afterValue = get(doc, path);
|
|
1708
|
+
beforeValue !== afterValue && devLog.debug("Field updated during deserialization", {
|
|
1709
|
+
path: path.join("."),
|
|
1710
|
+
before: typeof beforeValue == "string" ? beforeValue.substring(0, 50) : beforeValue,
|
|
1711
|
+
after: typeof afterValue == "string" ? afterValue.substring(0, 50) : afterValue
|
|
1712
|
+
});
|
|
1671
1713
|
}
|
|
1672
1714
|
}, applyFieldChanges = (doc, path, node) => {
|
|
1673
1715
|
const nodeType = node.attributes.type;
|