@focus-reactive/payload-plugin-translator 0.3.0 → 0.5.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/README.md +44 -29
- package/dist/index.d.ts +16 -14
- package/dist/index.js +9 -7
- package/dist/plugin.d.ts +15 -4
- package/dist/plugin.js +34 -57
- package/dist/server/features/cancel/handler.d.ts +3 -3
- package/dist/server/features/cancel/handler.js +5 -3
- package/dist/server/features/cancel/route.d.ts +4 -4
- package/dist/server/features/cancel/route.js +4 -4
- package/dist/server/features/cancel-by-collection/handler.d.ts +4 -4
- package/dist/server/features/cancel-by-collection/handler.js +6 -6
- package/dist/server/features/cancel-by-collection/route.d.ts +5 -5
- package/dist/server/features/cancel-by-collection/route.js +4 -4
- package/dist/server/features/createTranslationRoutes.d.ts +23 -0
- package/dist/server/features/createTranslationRoutes.js +27 -0
- package/dist/server/features/enqueue-translation/handler.d.ts +4 -4
- package/dist/server/features/enqueue-translation/handler.js +5 -5
- package/dist/server/features/enqueue-translation/route.d.ts +5 -5
- package/dist/server/features/enqueue-translation/route.js +4 -4
- package/dist/server/features/get-collection-status/handler.d.ts +4 -4
- package/dist/server/features/get-collection-status/handler.js +5 -5
- package/dist/server/features/get-collection-status/route.d.ts +5 -5
- package/dist/server/features/get-collection-status/route.js +4 -4
- package/dist/server/features/get-document-status/handler.d.ts +4 -4
- package/dist/server/features/get-document-status/handler.js +5 -5
- package/dist/server/features/get-document-status/route.d.ts +5 -5
- package/dist/server/features/get-document-status/route.js +4 -4
- package/dist/server/features/index.d.ts +7 -6
- package/dist/server/features/index.js +7 -6
- package/dist/server/features/run-translation/handler.d.ts +3 -3
- package/dist/server/features/run-translation/handler.js +7 -7
- package/dist/server/features/run-translation/route.d.ts +4 -4
- package/dist/server/features/run-translation/route.js +4 -4
- package/dist/server/features/translate-document/handler.js +7 -11
- package/dist/server/modules/task-runner/TaskRunnerProvider.interface.d.ts +19 -1
- package/dist/server/modules/task-runner/index.d.ts +6 -6
- package/dist/server/modules/task-runner/index.js +2 -2
- package/dist/server/modules/task-runner/payload-jobs-runner/PayloadJobsRunnerProvider.d.ts +16 -1
- package/dist/server/modules/task-runner/payload-jobs-runner/PayloadJobsRunnerProvider.js +42 -1
- package/dist/server/modules/task-runner/payload-jobs-runner/PayloadJobsTaskRunner.d.ts +29 -0
- package/dist/server/modules/task-runner/payload-jobs-runner/PayloadJobsTaskRunner.js +101 -6
- package/dist/server/modules/task-runner/payload-jobs-runner/types.d.ts +17 -0
- package/dist/server/modules/task-runner/sync-runner/SyncRunnerProvider.d.ts +18 -10
- package/dist/server/modules/task-runner/sync-runner/SyncRunnerProvider.js +24 -14
- package/dist/server/modules/translation-levels/PluginConfigBuilder.d.ts +46 -0
- package/dist/server/modules/translation-levels/PluginConfigBuilder.js +108 -0
- package/dist/server/modules/translation-levels/collectionLevel.d.ts +15 -0
- package/dist/server/modules/translation-levels/collectionLevel.js +24 -0
- package/dist/server/modules/translation-levels/documentLevel.d.ts +19 -0
- package/dist/server/modules/translation-levels/documentLevel.js +28 -0
- package/dist/server/modules/translation-levels/index.d.ts +3 -0
- package/dist/server/modules/translation-levels/index.js +4 -0
- package/dist/server/modules/translation-levels/types.d.ts +42 -0
- package/dist/server/modules/translation-levels/types.js +10 -0
- package/dist/server/modules/translation-levels/useDocTranslationApi.d.ts +8 -0
- package/dist/server/modules/translation-levels/useDocTranslationApi.js +18 -0
- package/dist/server/modules/translation-pipeline/index.d.ts +5 -3
- package/dist/server/modules/translation-pipeline/index.js +3 -2
- package/dist/server/modules/translation-pipeline/translateContent.d.ts +35 -0
- package/dist/server/modules/translation-pipeline/translateContent.js +30 -0
- package/dist/server/modules/translation-providers/OpenAITranslation.provider.d.ts +4 -3
- package/dist/server/modules/translation-providers/OpenAITranslation.provider.js +15 -15
- package/dist/types/AccessGuard.d.ts +24 -1
- package/dist/types/AccessGuard.js +12 -1
- package/package.json +1 -1
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import OpenAI from
|
|
2
|
-
import { isObject } from
|
|
1
|
+
import OpenAI from "openai";
|
|
2
|
+
import { isObject } from "../../shared";
|
|
3
3
|
/** @deprecated Use `createOpenAIProvider` function instead */ export class OpenAITranslationProvider {
|
|
4
|
-
config;
|
|
5
4
|
openAiClient;
|
|
5
|
+
config;
|
|
6
6
|
constructor(config){
|
|
7
|
-
this.config = config;
|
|
8
7
|
this.openAiClient = new OpenAI({
|
|
9
8
|
apiKey: config.apiKey
|
|
10
9
|
});
|
|
10
|
+
this.config = config;
|
|
11
11
|
}
|
|
12
12
|
async translate(content, souceLng, targetLng) {
|
|
13
13
|
if (this.config.dryRun) {
|
|
14
|
-
console.info(
|
|
14
|
+
console.info("[DRY RUN] Translation simulation:", {
|
|
15
15
|
content,
|
|
16
16
|
sourceLang: souceLng,
|
|
17
17
|
targetLang: targetLng,
|
|
18
|
-
provider:
|
|
18
|
+
provider: "OpenAI"
|
|
19
19
|
});
|
|
20
20
|
const timeout = this.getDryRunTimeout();
|
|
21
21
|
if (timeout > 0) await new Promise((resolve)=>setTimeout(resolve, timeout));
|
|
@@ -26,21 +26,21 @@ import { isObject } from '../../shared';
|
|
|
26
26
|
const chatCompletion = await this.openAiClient.chat.completions.create({
|
|
27
27
|
messages: [
|
|
28
28
|
{
|
|
29
|
-
role:
|
|
29
|
+
role: "system",
|
|
30
30
|
content: systemPrompt
|
|
31
31
|
},
|
|
32
32
|
{
|
|
33
|
-
role:
|
|
33
|
+
role: "user",
|
|
34
34
|
content: JSON.stringify(content)
|
|
35
35
|
}
|
|
36
36
|
],
|
|
37
|
-
model: this.config.model ??
|
|
37
|
+
model: this.config.model ?? "gpt-4o",
|
|
38
38
|
temperature: 0,
|
|
39
39
|
top_p: 1,
|
|
40
40
|
frequency_penalty: 0,
|
|
41
41
|
presence_penalty: 0,
|
|
42
42
|
response_format: {
|
|
43
|
-
type:
|
|
43
|
+
type: "json_object"
|
|
44
44
|
}
|
|
45
45
|
});
|
|
46
46
|
const translatedContent = chatCompletion.choices[0].message.content;
|
|
@@ -54,7 +54,7 @@ import { isObject } from '../../shared';
|
|
|
54
54
|
/**
|
|
55
55
|
* Builds the system prompt for translation.
|
|
56
56
|
*/ buildSystemPrompt(sourceLang, targetLang) {
|
|
57
|
-
const defaultPrompt = `Translate the values from the JSON that the user will send you${sourceLang ? ` from ${sourceLang}` :
|
|
57
|
+
const defaultPrompt = `Translate the values from the JSON that the user will send you${sourceLang ? ` from ${sourceLang}` : ""} into ${targetLang}. Keep all JSON keys exactly as they are, only translate the values.
|
|
58
58
|
The response should be a valid JSON object with the same structure and keys as the input, but with translated values.
|
|
59
59
|
Maintain any special formatting, placeholders, or variables within the values if they exist.`;
|
|
60
60
|
if (this.config.systemPrompt) {
|
|
@@ -71,17 +71,17 @@ Maintain any special formatting, placeholders, or variables within the values if
|
|
|
71
71
|
* If dryRun is an object with transform, returns it.
|
|
72
72
|
* If dryRun is true, returns the default transformer that reverses text.
|
|
73
73
|
*/ getDryRunTransformer() {
|
|
74
|
-
if (typeof this.config.dryRun ===
|
|
74
|
+
if (typeof this.config.dryRun === "object" && this.config.dryRun.transform) {
|
|
75
75
|
return this.config.dryRun.transform;
|
|
76
76
|
}
|
|
77
|
-
return (text)=>text.split(
|
|
77
|
+
return (text)=>text.split("").reverse().join("");
|
|
78
78
|
}
|
|
79
79
|
/**
|
|
80
80
|
* Returns the timeout for dry run mode.
|
|
81
81
|
* If dryRun is an object with timeout, returns it.
|
|
82
82
|
* Otherwise returns 0 (no delay).
|
|
83
83
|
*/ getDryRunTimeout() {
|
|
84
|
-
if (typeof this.config.dryRun ===
|
|
84
|
+
if (typeof this.config.dryRun === "object" && this.config.dryRun.timeout) {
|
|
85
85
|
return this.config.dryRun.timeout;
|
|
86
86
|
}
|
|
87
87
|
return 0;
|
|
@@ -89,7 +89,7 @@ Maintain any special formatting, placeholders, or variables within the values if
|
|
|
89
89
|
async createMockTranslation(content, transformer) {
|
|
90
90
|
try {
|
|
91
91
|
const mockTranslation = await this.transformObjectValues(content, async (value)=>{
|
|
92
|
-
if (typeof value ===
|
|
92
|
+
if (typeof value === "string" && value.trim()) return transformer(value);
|
|
93
93
|
return value;
|
|
94
94
|
});
|
|
95
95
|
return mockTranslation;
|
|
@@ -1,10 +1,33 @@
|
|
|
1
|
-
import type { BasePayload, TypedUser } from
|
|
1
|
+
import type { BasePayload, TypedUser } from "payload";
|
|
2
|
+
/**
|
|
3
|
+
* The request context an {@link AccessGuard} receives when deciding whether to
|
|
4
|
+
* allow a translation API call.
|
|
5
|
+
*/
|
|
2
6
|
export type AccessGuardRequest = {
|
|
7
|
+
/** Incoming request headers (e.g. for token/cookie checks). */
|
|
3
8
|
headers: Headers;
|
|
9
|
+
/** The authenticated user, or `null`/`undefined` if the request is anonymous. */
|
|
4
10
|
user?: TypedUser | null;
|
|
11
|
+
/** The Payload instance, for any DB/permission lookups the guard needs. */
|
|
5
12
|
payload: BasePayload;
|
|
6
13
|
};
|
|
14
|
+
/**
|
|
15
|
+
* Gate for the translation API endpoints. Provide one via `translatorPlugin({ access })`
|
|
16
|
+
* to control who may trigger translations; omit it to leave the endpoints open.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const adminsOnly: AccessGuard = {
|
|
21
|
+
* check: ({ req }) => req.user?.role === 'admin',
|
|
22
|
+
* }
|
|
23
|
+
* translatorPlugin({ collections, translationProvider, runner, access: adminsOnly })
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
7
26
|
export interface AccessGuard {
|
|
27
|
+
/**
|
|
28
|
+
* Return `true` to allow the request, `false` to reject it with `403 Forbidden`.
|
|
29
|
+
* May be async (e.g. to query permissions).
|
|
30
|
+
*/
|
|
8
31
|
check<R extends AccessGuardRequest>({ req }: {
|
|
9
32
|
req: R;
|
|
10
33
|
}): Promise<boolean> | boolean;
|
|
@@ -1,3 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Gate for the translation API endpoints. Provide one via `translatorPlugin({ access })`
|
|
3
|
+
* to control who may trigger translations; omit it to leave the endpoints open.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* const adminsOnly: AccessGuard = {
|
|
8
|
+
* check: ({ req }) => req.user?.role === 'admin',
|
|
9
|
+
* }
|
|
10
|
+
* translatorPlugin({ collections, translationProvider, runner, access: adminsOnly })
|
|
11
|
+
* ```
|
|
12
|
+
*/ export { };
|
|
2
13
|
|
|
3
14
|
//# sourceMappingURL=AccessGuard.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@focus-reactive/payload-plugin-translator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Translation plugin for Payload CMS 3.x. Automatically translate your localized content using any translation provider.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|