@molecule/api-ai-translation-deepl 1.0.2 → 1.1.0
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/protect.d.ts +56 -0
- package/dist/protect.d.ts.map +1 -0
- package/dist/protect.js +97 -0
- package/dist/provider.d.ts.map +1 -1
- package/dist/provider.js +17 -5
- package/package.json +2 -2
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeps `TranslateParams.protect` substrings out of DeepL's reach.
|
|
3
|
+
*
|
|
4
|
+
* Each protected occurrence is replaced by an indexed `<x>N</x>` tag that DeepL
|
|
5
|
+
* skips (`tag_handling=xml` + `ignore_tags=x`), and restored from the original
|
|
6
|
+
* text afterwards by index — so even whitespace DeepL moves inside the tag, or a
|
|
7
|
+
* reordered sentence, gets the exact original substring back. Because the request
|
|
8
|
+
* is XML, bare `&`/`<`/`>` in plain text are escaped first (unescaped, DeepL
|
|
9
|
+
* rejects the whole batch as "not well-formed") and entities are decoded on the
|
|
10
|
+
* way back out.
|
|
11
|
+
*
|
|
12
|
+
* @module
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Escapes the XML-significant characters of plain text.
|
|
16
|
+
*
|
|
17
|
+
* @param text - Plain text.
|
|
18
|
+
* @returns The text with `&`, `<`, `>` escaped.
|
|
19
|
+
*/
|
|
20
|
+
export declare function escapeXml(text: string): string;
|
|
21
|
+
/**
|
|
22
|
+
* Decodes named and numeric character entities back to plain text.
|
|
23
|
+
*
|
|
24
|
+
* @param text - Text returned by the XML translation.
|
|
25
|
+
* @returns Plain text.
|
|
26
|
+
*/
|
|
27
|
+
export declare function decodeEntities(text: string): string;
|
|
28
|
+
/**
|
|
29
|
+
* A text prepared for DeepL, plus what is needed to restore it.
|
|
30
|
+
*/
|
|
31
|
+
export interface MaskedText {
|
|
32
|
+
/** The XML sent to DeepL. */
|
|
33
|
+
masked: string;
|
|
34
|
+
/** The protected substrings, in order of occurrence. */
|
|
35
|
+
originals: string[];
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Masks one text for an XML-mode request.
|
|
39
|
+
*
|
|
40
|
+
* @param text - The caller's text.
|
|
41
|
+
* @param protect - Literal substrings to protect.
|
|
42
|
+
* @param callerMarkup - True when the caller set `tagHandling` (the text is markup already).
|
|
43
|
+
* @returns The masked text and its protected substrings.
|
|
44
|
+
*/
|
|
45
|
+
export declare function maskText(text: string, protect: string[], callerMarkup: boolean): MaskedText;
|
|
46
|
+
/**
|
|
47
|
+
* Restores a DeepL result: protected substrings back by index, then (for plain
|
|
48
|
+
* text) entities decoded.
|
|
49
|
+
*
|
|
50
|
+
* @param translated - DeepL's output.
|
|
51
|
+
* @param originals - The substrings `maskText` protected.
|
|
52
|
+
* @param callerMarkup - True when the caller set `tagHandling`.
|
|
53
|
+
* @returns The restored text.
|
|
54
|
+
*/
|
|
55
|
+
export declare function unmaskText(translated: string, originals: string[], callerMarkup: boolean): string;
|
|
56
|
+
//# sourceMappingURL=protect.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protect.d.ts","sourceRoot":"","sources":["../src/protect.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAYH;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE9C;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CASnD;AAkBD;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,6BAA6B;IAC7B,MAAM,EAAE,MAAM,CAAA;IACd,wDAAwD;IACxD,SAAS,EAAE,MAAM,EAAE,CAAA;CACpB;AAED;;;;;;;GAOG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,YAAY,EAAE,OAAO,GAAG,UAAU,CAW3F;AAED;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CAAC,UAAU,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE,YAAY,EAAE,OAAO,GAAG,MAAM,CAUjG"}
|
package/dist/protect.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeps `TranslateParams.protect` substrings out of DeepL's reach.
|
|
3
|
+
*
|
|
4
|
+
* Each protected occurrence is replaced by an indexed `<x>N</x>` tag that DeepL
|
|
5
|
+
* skips (`tag_handling=xml` + `ignore_tags=x`), and restored from the original
|
|
6
|
+
* text afterwards by index — so even whitespace DeepL moves inside the tag, or a
|
|
7
|
+
* reordered sentence, gets the exact original substring back. Because the request
|
|
8
|
+
* is XML, bare `&`/`<`/`>` in plain text are escaped first (unescaped, DeepL
|
|
9
|
+
* rejects the whole batch as "not well-formed") and entities are decoded on the
|
|
10
|
+
* way back out.
|
|
11
|
+
*
|
|
12
|
+
* @module
|
|
13
|
+
*/
|
|
14
|
+
/** Named entities DeepL's XML mode can return. */
|
|
15
|
+
const NAMED_ENTITIES = {
|
|
16
|
+
amp: '&',
|
|
17
|
+
lt: '<',
|
|
18
|
+
gt: '>',
|
|
19
|
+
quot: '"',
|
|
20
|
+
apos: "'",
|
|
21
|
+
nbsp: ' ',
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Escapes the XML-significant characters of plain text.
|
|
25
|
+
*
|
|
26
|
+
* @param text - Plain text.
|
|
27
|
+
* @returns The text with `&`, `<`, `>` escaped.
|
|
28
|
+
*/
|
|
29
|
+
export function escapeXml(text) {
|
|
30
|
+
return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Decodes named and numeric character entities back to plain text.
|
|
34
|
+
*
|
|
35
|
+
* @param text - Text returned by the XML translation.
|
|
36
|
+
* @returns Plain text.
|
|
37
|
+
*/
|
|
38
|
+
export function decodeEntities(text) {
|
|
39
|
+
return text.replace(/&(#\d+|#x[0-9a-f]+|amp|lt|gt|quot|apos|nbsp);/gi, (match, entity) => {
|
|
40
|
+
if (entity[0] !== '#')
|
|
41
|
+
return NAMED_ENTITIES[entity.toLowerCase()] ?? match;
|
|
42
|
+
const hex = entity[1] === 'x' || entity[1] === 'X';
|
|
43
|
+
return String.fromCodePoint(parseInt(entity.slice(hex ? 2 : 1), hex ? 16 : 10));
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Builds a regular expression matching any protected substring, longest first so
|
|
48
|
+
* an overlapping shorter token never splits a longer one.
|
|
49
|
+
*
|
|
50
|
+
* @param protect - Literal substrings to protect.
|
|
51
|
+
* @param escaped - Whether to match their XML-escaped form.
|
|
52
|
+
* @returns The pattern, or null when there is nothing to protect.
|
|
53
|
+
*/
|
|
54
|
+
function protectPattern(protect, escaped) {
|
|
55
|
+
const tokens = [...new Set(protect.filter((token) => token.length > 0))]
|
|
56
|
+
.map((token) => (escaped ? escapeXml(token) : token))
|
|
57
|
+
.sort((a, b) => b.length - a.length)
|
|
58
|
+
.map((token) => token.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
|
|
59
|
+
return tokens.length > 0 ? new RegExp(tokens.join('|'), 'g') : null;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Masks one text for an XML-mode request.
|
|
63
|
+
*
|
|
64
|
+
* @param text - The caller's text.
|
|
65
|
+
* @param protect - Literal substrings to protect.
|
|
66
|
+
* @param callerMarkup - True when the caller set `tagHandling` (the text is markup already).
|
|
67
|
+
* @returns The masked text and its protected substrings.
|
|
68
|
+
*/
|
|
69
|
+
export function maskText(text, protect, callerMarkup) {
|
|
70
|
+
const source = callerMarkup ? text : escapeXml(text);
|
|
71
|
+
const pattern = protectPattern(protect, !callerMarkup);
|
|
72
|
+
const originals = [];
|
|
73
|
+
const masked = pattern
|
|
74
|
+
? source.replace(pattern, (match) => {
|
|
75
|
+
originals.push(callerMarkup ? match : decodeEntities(match));
|
|
76
|
+
return `<x>${originals.length - 1}</x>`;
|
|
77
|
+
})
|
|
78
|
+
: source;
|
|
79
|
+
return { masked, originals };
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Restores a DeepL result: protected substrings back by index, then (for plain
|
|
83
|
+
* text) entities decoded.
|
|
84
|
+
*
|
|
85
|
+
* @param translated - DeepL's output.
|
|
86
|
+
* @param originals - The substrings `maskText` protected.
|
|
87
|
+
* @param callerMarkup - True when the caller set `tagHandling`.
|
|
88
|
+
* @returns The restored text.
|
|
89
|
+
*/
|
|
90
|
+
export function unmaskText(translated, originals, callerMarkup) {
|
|
91
|
+
// Decode first so a restored substring that itself contains `<` is never decoded twice.
|
|
92
|
+
const decoded = callerMarkup ? translated : decodeEntities(translated);
|
|
93
|
+
return decoded.replace(/<x>(\s*)(\d+)(\s*)<\/x>/g, (match, before, index, after) => {
|
|
94
|
+
const original = originals[Number(index)];
|
|
95
|
+
return original === undefined ? match : `${before}${original}${after}`;
|
|
96
|
+
});
|
|
97
|
+
}
|
package/dist/provider.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAKH,OAAO,cAAc,CAAA;AAErB,OAAO,KAAK,EACV,qBAAqB,EAKtB,MAAM,8BAA8B,CAAA;
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAKH,OAAO,cAAc,CAAA;AAErB,OAAO,KAAK,EACV,qBAAqB,EAKtB,MAAM,8BAA8B,CAAA;AAGrC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAuU7C;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,qBAAqB,CAE1E;AAID;;GAEG;AACH,eAAO,MAAM,QAAQ,EAAE,qBAUrB,CAAA"}
|
package/dist/provider.js
CHANGED
|
@@ -11,8 +11,9 @@
|
|
|
11
11
|
// runtime registry is populated even when provider.js is imported directly
|
|
12
12
|
// (not through the package barrel).
|
|
13
13
|
import './secrets.js';
|
|
14
|
+
import { maskText, unmaskText } from './protect.js';
|
|
14
15
|
/** HTTP status codes that are retryable. */
|
|
15
|
-
const RETRYABLE_STATUS_CODES = new Set([429, 503, 529]);
|
|
16
|
+
const RETRYABLE_STATUS_CODES = new Set([429, 500, 502, 503, 504, 529]);
|
|
16
17
|
/** Maximum number of retry attempts for retryable errors. */
|
|
17
18
|
const MAX_RETRIES = 3;
|
|
18
19
|
/** Maximum number of texts per translate request (DeepL API limit). */
|
|
@@ -144,8 +145,11 @@ class DeeplTranslationProvider {
|
|
|
144
145
|
* @returns Parsed translation result.
|
|
145
146
|
*/
|
|
146
147
|
async callTranslate(texts, params) {
|
|
148
|
+
const protect = params.protect ?? [];
|
|
149
|
+
const callerMarkup = params.tagHandling !== undefined;
|
|
150
|
+
const masked = protect.length > 0 ? texts.map((text) => maskText(text, protect, callerMarkup)) : null;
|
|
147
151
|
const body = {
|
|
148
|
-
text: texts,
|
|
152
|
+
text: masked ? masked.map((m) => m.masked) : texts,
|
|
149
153
|
target_lang: params.targetLang,
|
|
150
154
|
};
|
|
151
155
|
if (params.sourceLang) {
|
|
@@ -168,6 +172,11 @@ class DeeplTranslationProvider {
|
|
|
168
172
|
if (params.tagHandling) {
|
|
169
173
|
body.tag_handling = params.tagHandling;
|
|
170
174
|
}
|
|
175
|
+
if (masked) {
|
|
176
|
+
// XML even for plain text: it is what makes the <x> placeholders skippable.
|
|
177
|
+
body.tag_handling = params.tagHandling ?? 'xml';
|
|
178
|
+
body.ignore_tags = ['x'];
|
|
179
|
+
}
|
|
171
180
|
if (params.context) {
|
|
172
181
|
body.context = params.context;
|
|
173
182
|
}
|
|
@@ -184,8 +193,8 @@ class DeeplTranslationProvider {
|
|
|
184
193
|
}
|
|
185
194
|
const data = (await response.json());
|
|
186
195
|
return {
|
|
187
|
-
translations: data.translations.map((t) => ({
|
|
188
|
-
text: t.text,
|
|
196
|
+
translations: data.translations.map((t, i) => ({
|
|
197
|
+
text: masked ? unmaskText(t.text, masked[i].originals, callerMarkup) : t.text,
|
|
189
198
|
detectedSourceLang: t.detected_source_language,
|
|
190
199
|
})),
|
|
191
200
|
};
|
|
@@ -245,7 +254,10 @@ class DeeplTranslationProvider {
|
|
|
245
254
|
if (errorBody.length > 0 && errorBody.length < 200)
|
|
246
255
|
detail = errorBody;
|
|
247
256
|
}
|
|
248
|
-
|
|
257
|
+
// `status` lets callers branch without parsing text (456 = quota exhausted).
|
|
258
|
+
return Object.assign(new Error(`DeepL ${endpoint} API error (${response.status}): ${detail}`), {
|
|
259
|
+
status: response.status,
|
|
260
|
+
});
|
|
249
261
|
}
|
|
250
262
|
}
|
|
251
263
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/api-ai-translation-deepl",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "DeepL translation provider for molecule.dev — text translation via DeepL API",
|
|
5
5
|
"homepage": "https://www.molecule.dev/packages/api-ai-translation-deepl",
|
|
6
6
|
"type": "module",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"@molecule/api-secrets": "^1.0.1"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@molecule/api-ai-translation": "1.0
|
|
41
|
+
"@molecule/api-ai-translation": "1.1.0",
|
|
42
42
|
"@types/node": "26.1.2",
|
|
43
43
|
"typescript": "6.0.3",
|
|
44
44
|
"vitest": "4.1.11"
|