@molecule/api-ai-translation-deepl 1.0.2 → 1.1.1
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/LICENSE +111 -24
- package/README.md +11 -11
- 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 +4 -4
package/LICENSE
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
|
|
1
2
|
Apache License
|
|
2
3
|
Version 2.0, January 2004
|
|
3
4
|
http://www.apache.org/licenses/
|
|
@@ -34,21 +35,33 @@
|
|
|
34
35
|
|
|
35
36
|
"Work" shall mean the work of authorship, whether in Source or
|
|
36
37
|
Object form, made available under the License, as indicated by a
|
|
37
|
-
copyright notice that is included in or attached to the work
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
38
40
|
|
|
39
41
|
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
42
|
form, that is based on (or derived from) the Work and for which the
|
|
41
43
|
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
-
represent, as a whole, an original work of authorship.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
49
62
|
|
|
50
63
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
-
on behalf of whom a Contribution has been received by
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
52
65
|
subsequently incorporated within the Work.
|
|
53
66
|
|
|
54
67
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
@@ -61,8 +74,18 @@
|
|
|
61
74
|
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
75
|
this License, each Contributor hereby grants to You a perpetual,
|
|
63
76
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
-
|
|
65
|
-
and otherwise transfer the Work
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
66
89
|
|
|
67
90
|
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
91
|
Work or Derivative Works thereof in any medium, with or without
|
|
@@ -84,23 +107,87 @@
|
|
|
84
107
|
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
108
|
distribution, then any Derivative Works that You distribute must
|
|
86
109
|
include a readable copy of the attribution notices contained
|
|
87
|
-
within such NOTICE file
|
|
88
|
-
|
|
89
|
-
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
90
138
|
|
|
91
139
|
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
-
names, trademarks, service marks, or product names of the Licensor
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
93
143
|
|
|
94
144
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
-
agreed to in writing, Licensor provides the Work
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
104
191
|
|
|
105
192
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
193
|
you may not use this file except in compliance with the License.
|
package/README.md
CHANGED
|
@@ -3,21 +3,21 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
|
3
3
|
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
4
|
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
5
|
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
-
Generated: 2026-
|
|
6
|
+
Generated: 2026-10-16T23:28:41.928Z
|
|
7
7
|
-->
|
|
8
8
|
|
|
9
9
|
# @molecule/api-ai-translation-deepl
|
|
10
10
|
|
|
11
|
-
> **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
|
|
12
|
-
> It is written to be read by coding agents as much as by people, and is generated from this
|
|
13
|
-
> package's source — edit `src/index.ts` JSDoc, not this file.
|
|
14
|
-
|
|
15
11
|
DeepL translation provider for molecule.dev.
|
|
16
12
|
|
|
17
13
|
Implements the full `@molecule/api-ai-translation` contract (`translate`,
|
|
18
14
|
`getSupportedLanguages`, `getUsage`) over the DeepL REST API, auto-batching
|
|
19
15
|
large inputs (50 texts per request, DeepL's limit).
|
|
20
16
|
|
|
17
|
+
> **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
|
|
18
|
+
> It is written to be read by coding agents as much as by people, and is generated from this
|
|
19
|
+
> package's source — edit `src/index.ts` JSDoc, not this file.
|
|
20
|
+
|
|
21
21
|
## Quick Start
|
|
22
22
|
|
|
23
23
|
```typescript
|
|
@@ -83,20 +83,20 @@ function createProvider(config?: DeeplConfig): AITranslationProvider
|
|
|
83
83
|
|
|
84
84
|
### Constants
|
|
85
85
|
|
|
86
|
-
#### `
|
|
86
|
+
#### `provider`
|
|
87
87
|
|
|
88
|
-
|
|
88
|
+
The provider implementation.
|
|
89
89
|
|
|
90
90
|
```typescript
|
|
91
|
-
const
|
|
91
|
+
const provider: AITranslationProvider
|
|
92
92
|
```
|
|
93
93
|
|
|
94
|
-
#### `
|
|
94
|
+
#### `aiTranslationDeeplSecretDefinitions`
|
|
95
95
|
|
|
96
|
-
|
|
96
|
+
Secret definitions required by the DeepL translation bond.
|
|
97
97
|
|
|
98
98
|
```typescript
|
|
99
|
-
const
|
|
99
|
+
const aiTranslationDeeplSecretDefinitions: SecretDefinition[]
|
|
100
100
|
```
|
|
101
101
|
|
|
102
102
|
## Core Interface
|
|
@@ -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.
|
|
3
|
+
"version": "1.1.1",
|
|
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,9 +38,9 @@
|
|
|
38
38
|
"@molecule/api-secrets": "^1.0.1"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@molecule/api-ai-translation": "1.
|
|
42
|
-
"@types/node": "26.
|
|
41
|
+
"@molecule/api-ai-translation": "1.1.1",
|
|
42
|
+
"@types/node": "26.6.5",
|
|
43
43
|
"typescript": "6.0.3",
|
|
44
|
-
"vitest": "4.1.
|
|
44
|
+
"vitest": "4.1.12"
|
|
45
45
|
}
|
|
46
46
|
}
|