@justanarthur/payload-plugin-translator 1.3.21 → 3.0.3
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 +132 -0
- package/dist/client.css +41 -0
- package/dist/client.js +52 -52
- package/dist/index.d.ts +0 -39
- package/dist/index.js +28 -1049
- package/dist/jobs.d.ts +0 -145
- package/dist/jobs.js +8 -804
- package/dist/resolvers/copy.d.ts +0 -2
- package/dist/resolvers/copy.js +3 -13
- package/dist/resolvers/google.d.ts +0 -6
- package/dist/resolvers/google.js +4 -54
- package/dist/resolvers/libreTranslate.d.ts +0 -10
- package/dist/resolvers/libreTranslate.js +4 -56
- package/dist/resolvers/openAI.d.ts +2 -12
- package/dist/resolvers/openAI.js +4 -125
- package/dist/resolvers/types.d.ts +0 -2
- package/dist/shared/chunk-97ktc51q.js +15 -0
- package/dist/shared/chunk-ecnph6hw.js +205 -0
- package/dist/shared/chunk-frnemj69.js +6 -0
- package/dist/shared/chunk-fsveh29m.js +55 -0
- package/dist/shared/chunk-q51q8rd2.js +855 -0
- package/dist/shared/chunk-zhrw0jnz.js +57 -0
- package/dist/types.d.ts +0 -38
- package/package.json +6 -3
- package/dist/styles-jt3fm5fh.scss +0 -5
- package/dist/styles-vs6m7h55.scss +0 -76
package/dist/resolvers/copy.d.ts
CHANGED
package/dist/resolvers/copy.js
CHANGED
|
@@ -1,16 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
key: "copy",
|
|
5
|
-
resolve: (args) => {
|
|
6
|
-
const { texts } = args;
|
|
7
|
-
return {
|
|
8
|
-
success: true,
|
|
9
|
-
translatedTexts: texts
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
};
|
|
13
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
copyResolver
|
|
3
|
+
} from "../shared/chunk-97ktc51q.js";
|
|
14
4
|
|
|
15
5
|
// src/exports/resolvers/copy.ts
|
|
16
6
|
var copy_default = copyResolver;
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { PayloadRequest } from "payload";
|
|
2
2
|
type TranslateResolverArgs = {
|
|
3
|
-
/** Locale to translate from */
|
|
4
3
|
localeFrom: string;
|
|
5
|
-
/** Locale to translate to */
|
|
6
4
|
localeTo: string;
|
|
7
5
|
req: PayloadRequest;
|
|
8
6
|
texts: string[];
|
|
@@ -19,10 +17,6 @@ type TranslateResolver = {
|
|
|
19
17
|
};
|
|
20
18
|
type GoogleResolverConfig = {
|
|
21
19
|
apiKey: string;
|
|
22
|
-
/**
|
|
23
|
-
* How many texts to include into 1 request
|
|
24
|
-
* @default 100
|
|
25
|
-
*/
|
|
26
20
|
chunkLength?: number;
|
|
27
21
|
};
|
|
28
22
|
declare const googleResolver: ({ apiKey, chunkLength }: GoogleResolverConfig) => TranslateResolver;
|
package/dist/resolvers/google.js
CHANGED
|
@@ -1,57 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
// src/resolvers/google.ts
|
|
7
|
-
var localeToCountryCodeMapper = {
|
|
8
|
-
ua: "uk"
|
|
9
|
-
};
|
|
10
|
-
var mapLocale = (incoming) => (incoming in localeToCountryCodeMapper) ? localeToCountryCodeMapper[incoming] : incoming;
|
|
11
|
-
var googleResolver = ({
|
|
12
|
-
apiKey,
|
|
13
|
-
chunkLength = 100
|
|
14
|
-
}) => {
|
|
15
|
-
return {
|
|
16
|
-
key: "google",
|
|
17
|
-
resolve: async (args) => {
|
|
18
|
-
const { localeFrom, localeTo, req, texts } = args;
|
|
19
|
-
const apiUrl = `https://translation.googleapis.com/language/translate/v2?key=${apiKey}`;
|
|
20
|
-
const responses = await Promise.all(chunkArray(texts, chunkLength).map((q) => fetch(apiUrl, {
|
|
21
|
-
body: JSON.stringify({
|
|
22
|
-
q,
|
|
23
|
-
source: mapLocale(localeFrom),
|
|
24
|
-
target: mapLocale(localeTo)
|
|
25
|
-
}),
|
|
26
|
-
headers: {
|
|
27
|
-
"Content-Type": "application/json"
|
|
28
|
-
},
|
|
29
|
-
method: "POST"
|
|
30
|
-
}).then(async (res) => {
|
|
31
|
-
const data = await res.json();
|
|
32
|
-
if (!res.ok)
|
|
33
|
-
req.payload.logger.info({
|
|
34
|
-
googleResponse: data,
|
|
35
|
-
message: "An error occurred when trying to translate the data using Google API"
|
|
36
|
-
});
|
|
37
|
-
return {
|
|
38
|
-
data,
|
|
39
|
-
success: res.ok
|
|
40
|
-
};
|
|
41
|
-
})));
|
|
42
|
-
if (responses.some((res) => !res.success)) {
|
|
43
|
-
return {
|
|
44
|
-
success: false
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
const translatedTexts = responses.flatMap((chunk) => chunk.data.data.translations).map((translation) => translation.translatedText);
|
|
48
|
-
return {
|
|
49
|
-
success: true,
|
|
50
|
-
translatedTexts
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
googleResolver
|
|
3
|
+
} from "../shared/chunk-fsveh29m.js";
|
|
4
|
+
import"../shared/chunk-frnemj69.js";
|
|
55
5
|
|
|
56
6
|
// src/exports/resolvers/google.ts
|
|
57
7
|
var google_default = googleResolver;
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { PayloadRequest } from "payload";
|
|
2
2
|
type TranslateResolverArgs = {
|
|
3
|
-
/** Locale to translate from */
|
|
4
3
|
localeFrom: string;
|
|
5
|
-
/** Locale to translate to */
|
|
6
4
|
localeTo: string;
|
|
7
5
|
req: PayloadRequest;
|
|
8
6
|
texts: string[];
|
|
@@ -19,15 +17,7 @@ type TranslateResolver = {
|
|
|
19
17
|
};
|
|
20
18
|
type LibreResolverConfig = {
|
|
21
19
|
apiKey: string;
|
|
22
|
-
/**
|
|
23
|
-
* How many texts to include into 1 request
|
|
24
|
-
* @default 100
|
|
25
|
-
*/
|
|
26
20
|
chunkLength?: number;
|
|
27
|
-
/**
|
|
28
|
-
* Custom url for the libre translate instance
|
|
29
|
-
* @default "https://libretranslate.com/translate"
|
|
30
|
-
*/
|
|
31
21
|
url?: string;
|
|
32
22
|
};
|
|
33
23
|
declare const libreResolver: ({ apiKey, chunkLength, url }: LibreResolverConfig) => TranslateResolver;
|
|
@@ -1,59 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
// src/resolvers/libreTranslate.ts
|
|
7
|
-
var localeToCountryCodeMapper = {
|
|
8
|
-
ua: "uk"
|
|
9
|
-
};
|
|
10
|
-
var mapLocale = (incoming) => (incoming in localeToCountryCodeMapper) ? localeToCountryCodeMapper[incoming] : incoming;
|
|
11
|
-
var libreResolver = ({
|
|
12
|
-
apiKey,
|
|
13
|
-
chunkLength = 100,
|
|
14
|
-
url = "https://libretranslate.com/translate"
|
|
15
|
-
}) => {
|
|
16
|
-
return {
|
|
17
|
-
key: "libre",
|
|
18
|
-
resolve: async (args) => {
|
|
19
|
-
const { localeFrom, localeTo, req, texts } = args;
|
|
20
|
-
const apiUrl = url;
|
|
21
|
-
const responses = await Promise.all(chunkArray(texts, chunkLength).map((q) => fetch(apiUrl, {
|
|
22
|
-
body: JSON.stringify({
|
|
23
|
-
api_key: apiKey,
|
|
24
|
-
q,
|
|
25
|
-
source: mapLocale(localeFrom),
|
|
26
|
-
target: mapLocale(localeTo)
|
|
27
|
-
}),
|
|
28
|
-
headers: {
|
|
29
|
-
"Content-Type": "application/json"
|
|
30
|
-
},
|
|
31
|
-
method: "POST"
|
|
32
|
-
}).then(async (res) => {
|
|
33
|
-
const data = await res.json();
|
|
34
|
-
if (!res.ok)
|
|
35
|
-
req.payload.logger.info({
|
|
36
|
-
libreResponse: data,
|
|
37
|
-
message: "An error occurred when trying to translate the data using LibreTranslate API"
|
|
38
|
-
});
|
|
39
|
-
return {
|
|
40
|
-
data,
|
|
41
|
-
success: res.ok
|
|
42
|
-
};
|
|
43
|
-
})));
|
|
44
|
-
if (responses.some((res) => !res.success)) {
|
|
45
|
-
return {
|
|
46
|
-
success: false
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
const translatedTexts = responses.flatMap((chunk) => chunk.data.translatedText);
|
|
50
|
-
return {
|
|
51
|
-
success: true,
|
|
52
|
-
translatedTexts
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
libreResolver
|
|
3
|
+
} from "../shared/chunk-zhrw0jnz.js";
|
|
4
|
+
import"../shared/chunk-frnemj69.js";
|
|
57
5
|
|
|
58
6
|
// src/exports/resolvers/libreTranslate.ts
|
|
59
7
|
var libreTranslate_default = libreResolver;
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { PayloadRequest } from "payload";
|
|
2
2
|
type TranslateResolverArgs = {
|
|
3
|
-
/** Locale to translate from */
|
|
4
3
|
localeFrom: string;
|
|
5
|
-
/** Locale to translate to */
|
|
6
4
|
localeTo: string;
|
|
7
5
|
req: PayloadRequest;
|
|
8
6
|
texts: string[];
|
|
@@ -17,25 +15,17 @@ type TranslateResolver = {
|
|
|
17
15
|
key: string;
|
|
18
16
|
resolve: (args: TranslateResolverArgs) => Promise<TranslateResolverResponse> | TranslateResolverResponse;
|
|
19
17
|
};
|
|
20
|
-
type OpenAIMessageExchange = Record<string | number, string>;
|
|
21
18
|
type OpenAIPrompt = (args: {
|
|
22
19
|
localeFrom: string;
|
|
23
20
|
localeTo: string;
|
|
24
|
-
texts:
|
|
21
|
+
texts: string[];
|
|
25
22
|
}) => string;
|
|
26
23
|
type OpenAIResolverConfig = {
|
|
27
24
|
apiKey: string;
|
|
28
25
|
baseUrl?: string;
|
|
29
|
-
/**
|
|
30
|
-
* How many texts to include into 1 request
|
|
31
|
-
* @default 100
|
|
32
|
-
*/
|
|
33
26
|
chunkLength?: number;
|
|
34
|
-
/**
|
|
35
|
-
* @default "gpt-3.5-turbo"
|
|
36
|
-
*/
|
|
37
27
|
model?: string;
|
|
38
28
|
prompt?: OpenAIPrompt;
|
|
39
29
|
};
|
|
40
30
|
declare const openAIResolver: ({ apiKey, baseUrl, chunkLength, model, prompt }: OpenAIResolverConfig) => TranslateResolver;
|
|
41
|
-
export { openAIResolver, openAIResolver as default, OpenAIResolverConfig, OpenAIPrompt
|
|
31
|
+
export { openAIResolver, openAIResolver as default, OpenAIResolverConfig, OpenAIPrompt };
|
package/dist/resolvers/openAI.js
CHANGED
|
@@ -1,128 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
// src/resolvers/openAI.ts
|
|
7
|
-
var defaultPrompt = ({ localeFrom, localeTo, texts }) => {
|
|
8
|
-
return `You are a machine translation service. Your task is to translate values in a strict JSON of key value pairs from ${localeFrom} to ${localeTo}.
|
|
9
|
-
|
|
10
|
-
**INSTRUCTIONS:**
|
|
11
|
-
1. **Translate each value**.
|
|
12
|
-
2. The output **must valid JSON**.
|
|
13
|
-
3. The output array **must have the exact same number of elements** as the input.
|
|
14
|
-
4. Preserve the structure of the JSON array.
|
|
15
|
-
5. Preserve JSON keys without translation.
|
|
16
|
-
6. The **order of the elements must not change**.
|
|
17
|
-
7. **Do not include any text, explanations, or remarks outside of the JSON array.**
|
|
18
|
-
8. **Preserve any special characters, HTML tags, or formatting** present in the original strings.
|
|
19
|
-
9. **Preserve urls, hrefs, and email addresses** without translation.
|
|
20
|
-
10. RETURN ONLY THE RAW JSON, DO NOT RESPOND WITH ANYTHING ELSE, AND NO FORMATTING.
|
|
21
|
-
|
|
22
|
-
**INPUT JSON TO TRANSLATE:**
|
|
23
|
-
${JSON.stringify(texts)}`;
|
|
24
|
-
};
|
|
25
|
-
var openAIResolver = ({
|
|
26
|
-
apiKey,
|
|
27
|
-
baseUrl,
|
|
28
|
-
chunkLength = 100,
|
|
29
|
-
model = "gpt-3.5-turbo",
|
|
30
|
-
prompt = defaultPrompt
|
|
31
|
-
}) => {
|
|
32
|
-
return {
|
|
33
|
-
key: "openai",
|
|
34
|
-
resolve: async ({ localeFrom, localeTo, req, texts }) => {
|
|
35
|
-
const apiUrl = `${baseUrl || "https://api.openai.com"}/v1/chat/completions`;
|
|
36
|
-
try {
|
|
37
|
-
const response = await Promise.all(chunkArray(texts, chunkLength).map((texts2) => {
|
|
38
|
-
const structuredTexts = texts2.reduce((acc, curr, index) => {
|
|
39
|
-
acc[index + 1] = curr;
|
|
40
|
-
return acc;
|
|
41
|
-
}, {});
|
|
42
|
-
return fetch(apiUrl, {
|
|
43
|
-
body: JSON.stringify({
|
|
44
|
-
messages: [
|
|
45
|
-
{
|
|
46
|
-
content: prompt({ localeFrom, localeTo, texts: structuredTexts }),
|
|
47
|
-
role: "user"
|
|
48
|
-
}
|
|
49
|
-
],
|
|
50
|
-
model
|
|
51
|
-
}),
|
|
52
|
-
headers: {
|
|
53
|
-
Authorization: `Bearer ${apiKey}`,
|
|
54
|
-
"Content-Type": "application/json"
|
|
55
|
-
},
|
|
56
|
-
method: "post"
|
|
57
|
-
}).then(async (res) => {
|
|
58
|
-
const data = await res.json();
|
|
59
|
-
if (!res.ok)
|
|
60
|
-
req.payload.logger.info({
|
|
61
|
-
message: "An error occurred when trying to translate the data using OpenAI API",
|
|
62
|
-
openAIresponse: data
|
|
63
|
-
});
|
|
64
|
-
return {
|
|
65
|
-
data,
|
|
66
|
-
success: res.ok
|
|
67
|
-
};
|
|
68
|
-
});
|
|
69
|
-
}));
|
|
70
|
-
const translated = [];
|
|
71
|
-
for (const { data, success } of response) {
|
|
72
|
-
if (!success)
|
|
73
|
-
return {
|
|
74
|
-
success: false
|
|
75
|
-
};
|
|
76
|
-
const content = data?.choices?.[0]?.message?.content;
|
|
77
|
-
if (!content) {
|
|
78
|
-
req.payload.logger.error("An error occurred when trying to translate the data using OpenAI API - missing content in the response");
|
|
79
|
-
return {
|
|
80
|
-
success: false
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
const translatedStructuredTexts = JSON.parse(content);
|
|
84
|
-
const translatedChunk = Object.values(translatedStructuredTexts);
|
|
85
|
-
if (!Array.isArray(translatedChunk)) {
|
|
86
|
-
req.payload.logger.error({
|
|
87
|
-
data: translatedChunk,
|
|
88
|
-
fullContent: content,
|
|
89
|
-
message: "An error occurred when trying to translate the data using OpenAI API - parsed content is not an array"
|
|
90
|
-
});
|
|
91
|
-
return {
|
|
92
|
-
success: false
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
for (const text of translatedChunk) {
|
|
96
|
-
if (text && typeof text !== "string") {
|
|
97
|
-
req.payload.logger.error({
|
|
98
|
-
chunkData: translatedChunk,
|
|
99
|
-
data: text,
|
|
100
|
-
fullContent: content,
|
|
101
|
-
message: "An error occurred when trying to translate the data using OpenAI API - parsed content is not a string"
|
|
102
|
-
});
|
|
103
|
-
return {
|
|
104
|
-
success: false
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
translated.push(text);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
return {
|
|
111
|
-
success: true,
|
|
112
|
-
translatedTexts: translated
|
|
113
|
-
};
|
|
114
|
-
} catch (e) {
|
|
115
|
-
if (e instanceof Error) {
|
|
116
|
-
req.payload.logger.info({
|
|
117
|
-
message: "An error occurred when trying to translate the data using OpenAI API",
|
|
118
|
-
originalErr: e.message
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
return { success: false };
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
openAIResolver
|
|
3
|
+
} from "../shared/chunk-ecnph6hw.js";
|
|
4
|
+
import"../shared/chunk-frnemj69.js";
|
|
126
5
|
|
|
127
6
|
// src/exports/resolvers/openAI.ts
|
|
128
7
|
var openAI_default = openAIResolver;
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import {
|
|
2
|
+
chunkArray
|
|
3
|
+
} from "./chunk-frnemj69.js";
|
|
4
|
+
|
|
5
|
+
// src/resolvers/openAI.ts
|
|
6
|
+
var LOCALE_DISPLAY_NAME = {
|
|
7
|
+
en: "English",
|
|
8
|
+
sk: "Slovak",
|
|
9
|
+
cs: "Czech",
|
|
10
|
+
de: "German",
|
|
11
|
+
uk: "Ukrainian",
|
|
12
|
+
ua: "Ukrainian",
|
|
13
|
+
pl: "Polish",
|
|
14
|
+
hu: "Hungarian",
|
|
15
|
+
fr: "French",
|
|
16
|
+
es: "Spanish",
|
|
17
|
+
it: "Italian",
|
|
18
|
+
pt: "Portuguese",
|
|
19
|
+
nl: "Dutch",
|
|
20
|
+
ro: "Romanian"
|
|
21
|
+
};
|
|
22
|
+
var RETRY_DELAYS_MS = [500, 1000, 2000];
|
|
23
|
+
var defaultPrompt = ({ localeFrom, localeTo, texts }) => {
|
|
24
|
+
const from = LOCALE_DISPLAY_NAME[localeFrom] ?? localeFrom;
|
|
25
|
+
const to = LOCALE_DISPLAY_NAME[localeTo] ?? localeTo;
|
|
26
|
+
return `You are a machine-translation engine. Translate each string in the input JSON array from ${from} (${localeFrom}) to ${to} (${localeTo}).
|
|
27
|
+
|
|
28
|
+
Rules:
|
|
29
|
+
1. The output must be a valid JSON array of strings, with the same length and order as the input.
|
|
30
|
+
2. SLUGS: any input that looks like a URL slug (lowercase, contains only ASCII letters/digits/hyphens/underscores, no spaces, no punctuation) MUST be transliterated into the target language. Output the slug in the same format: lowercase ASCII only, using only letters a-z, digits 0-9, hyphens (-), and underscores (_). Never leave a slug unchanged. Never output accented characters, uppercase letters, spaces, or other symbols in a slug. Examples:
|
|
31
|
+
- "about-us-copy" (en) → "o-nas-kopia" (sk) → "uber-uns-kopie" (de) → "sobre-nos-copia" (pt) → "a-propos-copie" (fr)
|
|
32
|
+
- For Cyrillic source scripts, transliterate to Latin first, then translate.
|
|
33
|
+
3. URLs (containing "://" or "www."), email addresses, hex strings, and other opaque identifiers: keep as-is.
|
|
34
|
+
4. Apply locale-specific formatting for dates, currency, decimal separators in human-readable text.
|
|
35
|
+
5. Return only the JSON array. No markdown fences, no prose, no trailing commentary.
|
|
36
|
+
|
|
37
|
+
INPUT:
|
|
38
|
+
${JSON.stringify(texts)}`;
|
|
39
|
+
};
|
|
40
|
+
var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
41
|
+
var isRetryableStatus = (status) => status === 429 || status >= 500;
|
|
42
|
+
var isGpt5Family = (model) => /^gpt-5/.test(model);
|
|
43
|
+
var isGpt54Plus = (model) => /^gpt-5\.[1-9]/.test(model);
|
|
44
|
+
var usesMaxCompletionTokens = (model) => isGpt5Family(model) || /^o[1-9]/.test(model);
|
|
45
|
+
var deriveMaxTokens = (chunkLength, model) => {
|
|
46
|
+
const base = Math.max(chunkLength * 100, 4000);
|
|
47
|
+
if (model && isGpt54Plus(model))
|
|
48
|
+
return Math.max(base * 4, 16000);
|
|
49
|
+
return base;
|
|
50
|
+
};
|
|
51
|
+
var parseContent = (raw) => {
|
|
52
|
+
const trimmed = raw.trim();
|
|
53
|
+
const m = trimmed.match(/^```(?:json)?\s*\n?([\s\S]*?)\n?\s*```$/);
|
|
54
|
+
const candidate = m ? m[1].trim() : trimmed;
|
|
55
|
+
const fenceStripped = m !== null;
|
|
56
|
+
let parsed;
|
|
57
|
+
try {
|
|
58
|
+
parsed = JSON.parse(candidate);
|
|
59
|
+
} catch (e) {
|
|
60
|
+
return {
|
|
61
|
+
error: e instanceof Error ? e.message : String(e),
|
|
62
|
+
fenceStripped,
|
|
63
|
+
ok: false
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
if (!Array.isArray(parsed)) {
|
|
67
|
+
return { error: "parsed value is not an array", fenceStripped, ok: false };
|
|
68
|
+
}
|
|
69
|
+
if (!parsed.every((v) => typeof v === "string")) {
|
|
70
|
+
return { error: "array contains non-string element", fenceStripped, ok: false };
|
|
71
|
+
}
|
|
72
|
+
return { fenceStripped, ok: true, translated: parsed };
|
|
73
|
+
};
|
|
74
|
+
var openAIResolver = ({
|
|
75
|
+
apiKey,
|
|
76
|
+
baseUrl,
|
|
77
|
+
chunkLength = 100,
|
|
78
|
+
model = "gpt-4o-mini",
|
|
79
|
+
prompt = defaultPrompt
|
|
80
|
+
}) => {
|
|
81
|
+
return {
|
|
82
|
+
key: "openai",
|
|
83
|
+
resolve: async ({ localeFrom, localeTo, req, texts }) => {
|
|
84
|
+
const apiUrl = `${baseUrl || "https://api.openai.com"}/v1/chat/completions`;
|
|
85
|
+
const maxTokens = deriveMaxTokens(chunkLength, model);
|
|
86
|
+
const maxTokensKey = usesMaxCompletionTokens(model) ? "max_completion_tokens" : "max_tokens";
|
|
87
|
+
const supportsCustomTemperature = !isGpt5Family(model);
|
|
88
|
+
const reasoningEffort = isGpt54Plus(model) ? "low" : undefined;
|
|
89
|
+
const logger = req.payload.logger;
|
|
90
|
+
try {
|
|
91
|
+
const response = await Promise.all(chunkArray(texts, chunkLength).map(async (chunk) => {
|
|
92
|
+
for (let attempt = 0;attempt <= RETRY_DELAYS_MS.length; attempt++) {
|
|
93
|
+
let shouldRetry = false;
|
|
94
|
+
let httpStatus = 0;
|
|
95
|
+
try {
|
|
96
|
+
const res = await fetch(apiUrl, {
|
|
97
|
+
body: JSON.stringify({
|
|
98
|
+
messages: [
|
|
99
|
+
{
|
|
100
|
+
content: prompt({ localeFrom, localeTo, texts: chunk }),
|
|
101
|
+
role: "user"
|
|
102
|
+
}
|
|
103
|
+
],
|
|
104
|
+
model,
|
|
105
|
+
...supportsCustomTemperature ? { temperature: 0 } : {},
|
|
106
|
+
...reasoningEffort ? { reasoning_effort: reasoningEffort } : {},
|
|
107
|
+
[maxTokensKey]: maxTokens
|
|
108
|
+
}),
|
|
109
|
+
headers: {
|
|
110
|
+
Authorization: `Bearer ${apiKey}`,
|
|
111
|
+
"Content-Type": "application/json"
|
|
112
|
+
},
|
|
113
|
+
method: "post"
|
|
114
|
+
});
|
|
115
|
+
httpStatus = res.status;
|
|
116
|
+
const data = await res.json();
|
|
117
|
+
if (res.ok) {
|
|
118
|
+
const content = data?.choices?.[0]?.message?.content;
|
|
119
|
+
if (!content) {
|
|
120
|
+
logger.error({
|
|
121
|
+
code: "OPENAI_BAD_JSON",
|
|
122
|
+
message: "OpenAI response missing content",
|
|
123
|
+
openAIResponse: data
|
|
124
|
+
});
|
|
125
|
+
shouldRetry = true;
|
|
126
|
+
} else {
|
|
127
|
+
const result = parseContent(content);
|
|
128
|
+
if (result.ok) {
|
|
129
|
+
if (result.fenceStripped) {
|
|
130
|
+
logger.info({
|
|
131
|
+
code: "OPENAI_FENCE_STRIPPED",
|
|
132
|
+
message: "OpenAI returned fenced JSON despite json_object mode"
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
return { success: true, translated: result.translated };
|
|
136
|
+
}
|
|
137
|
+
logger.error({
|
|
138
|
+
code: "OPENAI_BAD_JSON",
|
|
139
|
+
error: result.error,
|
|
140
|
+
fenceStripped: result.fenceStripped,
|
|
141
|
+
message: "Failed to parse OpenAI response"
|
|
142
|
+
});
|
|
143
|
+
shouldRetry = true;
|
|
144
|
+
}
|
|
145
|
+
} else {
|
|
146
|
+
logger.error({
|
|
147
|
+
code: "OPENAI_HTTP_ERROR",
|
|
148
|
+
message: "OpenAI returned non-2xx status",
|
|
149
|
+
openAIResponse: data,
|
|
150
|
+
status: httpStatus
|
|
151
|
+
});
|
|
152
|
+
if (isRetryableStatus(httpStatus))
|
|
153
|
+
shouldRetry = true;
|
|
154
|
+
}
|
|
155
|
+
} catch (e) {
|
|
156
|
+
logger.error({
|
|
157
|
+
code: "OPENAI_NETWORK_ERROR",
|
|
158
|
+
message: "OpenAI request threw",
|
|
159
|
+
originalErr: e instanceof Error ? e.message : String(e)
|
|
160
|
+
});
|
|
161
|
+
shouldRetry = true;
|
|
162
|
+
}
|
|
163
|
+
if (attempt < RETRY_DELAYS_MS.length && shouldRetry) {
|
|
164
|
+
logger.info({
|
|
165
|
+
attempt: attempt + 1,
|
|
166
|
+
code: "OPENAI_RETRY",
|
|
167
|
+
message: "Retrying OpenAI request after backoff",
|
|
168
|
+
nextBackoffMs: RETRY_DELAYS_MS[attempt],
|
|
169
|
+
status: httpStatus
|
|
170
|
+
});
|
|
171
|
+
await sleep(RETRY_DELAYS_MS[attempt]);
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
logger.error({
|
|
175
|
+
code: "OPENAI_GIVE_UP",
|
|
176
|
+
message: "OpenAI chunk failed after retries",
|
|
177
|
+
status: httpStatus
|
|
178
|
+
});
|
|
179
|
+
return { success: false };
|
|
180
|
+
}
|
|
181
|
+
return { success: false };
|
|
182
|
+
}));
|
|
183
|
+
const translated = [];
|
|
184
|
+
for (const result of response) {
|
|
185
|
+
if (!result.success)
|
|
186
|
+
return { success: false };
|
|
187
|
+
translated.push(...result.translated);
|
|
188
|
+
}
|
|
189
|
+
return {
|
|
190
|
+
success: true,
|
|
191
|
+
translatedTexts: translated
|
|
192
|
+
};
|
|
193
|
+
} catch (e) {
|
|
194
|
+
logger.error({
|
|
195
|
+
code: "OPENAI_UNEXPECTED",
|
|
196
|
+
message: "OpenAI resolve threw an unexpected error",
|
|
197
|
+
originalErr: e instanceof Error ? e.message : String(e)
|
|
198
|
+
});
|
|
199
|
+
return { success: false };
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
export { openAIResolver };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import {
|
|
2
|
+
chunkArray
|
|
3
|
+
} from "./chunk-frnemj69.js";
|
|
4
|
+
|
|
5
|
+
// src/resolvers/google.ts
|
|
6
|
+
var localeToCountryCodeMapper = {
|
|
7
|
+
ua: "uk"
|
|
8
|
+
};
|
|
9
|
+
var mapLocale = (incoming) => (incoming in localeToCountryCodeMapper) ? localeToCountryCodeMapper[incoming] : incoming;
|
|
10
|
+
var googleResolver = ({
|
|
11
|
+
apiKey,
|
|
12
|
+
chunkLength = 100
|
|
13
|
+
}) => {
|
|
14
|
+
return {
|
|
15
|
+
key: "google",
|
|
16
|
+
resolve: async (args) => {
|
|
17
|
+
const { localeFrom, localeTo, req, texts } = args;
|
|
18
|
+
const apiUrl = `https://translation.googleapis.com/language/translate/v2?key=${apiKey}`;
|
|
19
|
+
const responses = await Promise.all(chunkArray(texts, chunkLength).map((q) => fetch(apiUrl, {
|
|
20
|
+
body: JSON.stringify({
|
|
21
|
+
q,
|
|
22
|
+
source: mapLocale(localeFrom),
|
|
23
|
+
target: mapLocale(localeTo)
|
|
24
|
+
}),
|
|
25
|
+
headers: {
|
|
26
|
+
"Content-Type": "application/json"
|
|
27
|
+
},
|
|
28
|
+
method: "POST"
|
|
29
|
+
}).then(async (res) => {
|
|
30
|
+
const data = await res.json();
|
|
31
|
+
if (!res.ok)
|
|
32
|
+
req.payload.logger.info({
|
|
33
|
+
googleResponse: data,
|
|
34
|
+
message: "An error occurred when trying to translate the data using Google API"
|
|
35
|
+
});
|
|
36
|
+
return {
|
|
37
|
+
data,
|
|
38
|
+
success: res.ok
|
|
39
|
+
};
|
|
40
|
+
})));
|
|
41
|
+
if (responses.some((res) => !res.success)) {
|
|
42
|
+
return {
|
|
43
|
+
success: false
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
const translatedTexts = responses.flatMap((chunk) => chunk.data.data.translations).map((translation) => translation.translatedText);
|
|
47
|
+
return {
|
|
48
|
+
success: true,
|
|
49
|
+
translatedTexts
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export { googleResolver };
|