@mvriu5/payload-ai 1.4.0 → 1.6.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 +60 -0
- package/dist/ai/promptCaching.d.ts +23 -0
- package/dist/ai/promptCaching.js +120 -0
- package/dist/ai/proposalRepair.d.ts +30 -0
- package/dist/ai/proposalRepair.js +76 -0
- package/dist/ai/sensitiveData.d.ts +1 -0
- package/dist/ai/sensitiveData.js +3 -1
- package/dist/components/ai-input/AIInput.js +35 -5
- package/dist/components/ai-input/AIInput.module.css +49 -0
- package/dist/components/generate-field/GenerateField.d.ts +11 -0
- package/dist/components/generate-field/GenerateField.js +109 -0
- package/dist/components/generate-field/GenerateField.module.css +39 -0
- package/dist/components/generate-field/richText.d.ts +29 -0
- package/dist/components/generate-field/richText.js +30 -0
- package/dist/components/translate-document/TranslateDocumentButton.d.ts +2 -0
- package/dist/components/translate-document/TranslateDocumentButton.js +111 -0
- package/dist/components/translate-document/TranslateDocumentButton.module.css +11 -0
- package/dist/exports/client.d.ts +4 -0
- package/dist/exports/client.js +4 -0
- package/dist/handlers/chatHandler.d.ts +1 -0
- package/dist/handlers/chatHandler.js +464 -138
- package/dist/handlers/generateFieldHandler.d.ts +14 -0
- package/dist/handlers/generateFieldHandler.js +144 -0
- package/dist/handlers/translateDocumentHandler.d.ts +14 -0
- package/dist/handlers/translateDocumentHandler.js +186 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +112 -4
- package/dist/payload/documentTranslation.d.ts +15 -0
- package/dist/payload/documentTranslation.js +120 -0
- package/dist/payload/textFieldGeneration.d.ts +26 -0
- package/dist/payload/textFieldGeneration.js +99 -0
- package/dist/payload/toolFieldSelection.d.ts +25 -0
- package/dist/payload/toolFieldSelection.js +118 -0
- package/dist/payload/toolSchemas.d.ts +7 -0
- package/dist/payload/toolSchemas.js +117 -0
- package/package.json +3 -3
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export const createGeneratedRichTextValue = (value)=>{
|
|
2
|
+
const paragraphs = value.split(/\n\s*\n/).map((paragraph)=>paragraph.replace(/\s*\n\s*/g, " ").trim()).filter(Boolean);
|
|
3
|
+
return {
|
|
4
|
+
root: {
|
|
5
|
+
children: paragraphs.map((text)=>({
|
|
6
|
+
children: [
|
|
7
|
+
{
|
|
8
|
+
detail: 0,
|
|
9
|
+
format: 0,
|
|
10
|
+
mode: "normal",
|
|
11
|
+
style: "",
|
|
12
|
+
text,
|
|
13
|
+
type: "text",
|
|
14
|
+
version: 1
|
|
15
|
+
}
|
|
16
|
+
],
|
|
17
|
+
direction: "ltr",
|
|
18
|
+
format: "",
|
|
19
|
+
indent: 0,
|
|
20
|
+
type: "paragraph",
|
|
21
|
+
version: 1
|
|
22
|
+
})),
|
|
23
|
+
direction: "ltr",
|
|
24
|
+
format: "",
|
|
25
|
+
indent: 0,
|
|
26
|
+
type: "root",
|
|
27
|
+
version: 1
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { Button, useConfig, useDocumentForm, useDocumentInfo, useLocale } from "@payloadcms/ui";
|
|
4
|
+
import { formatAdminURL } from "payload/shared";
|
|
5
|
+
import { useEffect, useState } from "react";
|
|
6
|
+
import styles from "./TranslateDocumentButton.module.css";
|
|
7
|
+
const TranslateDocumentButton = ()=>{
|
|
8
|
+
const configContext = useConfig();
|
|
9
|
+
const documentForm = useDocumentForm();
|
|
10
|
+
const documentInfo = useDocumentInfo();
|
|
11
|
+
const locale = useLocale();
|
|
12
|
+
const [available, setAvailable] = useState(false);
|
|
13
|
+
const [error, setError] = useState("");
|
|
14
|
+
const [isChecking, setIsChecking] = useState(true);
|
|
15
|
+
const [isTranslating, setIsTranslating] = useState(false);
|
|
16
|
+
const scope = documentInfo.collectionSlug ? {
|
|
17
|
+
slug: documentInfo.collectionSlug,
|
|
18
|
+
type: "collection"
|
|
19
|
+
} : documentInfo.globalSlug ? {
|
|
20
|
+
slug: documentInfo.globalSlug,
|
|
21
|
+
type: "global"
|
|
22
|
+
} : null;
|
|
23
|
+
const apiRoute = configContext?.config?.routes?.api;
|
|
24
|
+
const request = async (action)=>{
|
|
25
|
+
if (!apiRoute || !scope) return null;
|
|
26
|
+
const response = await fetch(formatAdminURL({
|
|
27
|
+
apiRoute,
|
|
28
|
+
path: "/ai-translate-document"
|
|
29
|
+
}), {
|
|
30
|
+
body: JSON.stringify({
|
|
31
|
+
action,
|
|
32
|
+
id: documentInfo.id,
|
|
33
|
+
locale: locale.code,
|
|
34
|
+
scope
|
|
35
|
+
}),
|
|
36
|
+
headers: {
|
|
37
|
+
"Content-Type": "application/json"
|
|
38
|
+
},
|
|
39
|
+
method: "POST"
|
|
40
|
+
});
|
|
41
|
+
const result = await response.json().catch(()=>null);
|
|
42
|
+
if (!response.ok || !result) throw new Error(result?.error || "Could not translate this locale.");
|
|
43
|
+
return result;
|
|
44
|
+
};
|
|
45
|
+
useEffect(()=>{
|
|
46
|
+
let active = true;
|
|
47
|
+
setAvailable(false);
|
|
48
|
+
setError("");
|
|
49
|
+
setIsChecking(true);
|
|
50
|
+
void request("status").then((result)=>{
|
|
51
|
+
if (active) setAvailable(Boolean(result?.available));
|
|
52
|
+
}).catch(()=>{
|
|
53
|
+
if (active) setAvailable(false);
|
|
54
|
+
}).finally(()=>{
|
|
55
|
+
if (active) setIsChecking(false);
|
|
56
|
+
});
|
|
57
|
+
return ()=>{
|
|
58
|
+
active = false;
|
|
59
|
+
};
|
|
60
|
+
}, [
|
|
61
|
+
apiRoute,
|
|
62
|
+
documentInfo.id,
|
|
63
|
+
locale.code,
|
|
64
|
+
scope?.slug,
|
|
65
|
+
scope?.type
|
|
66
|
+
]);
|
|
67
|
+
const translate = async ()=>{
|
|
68
|
+
if (isTranslating) return;
|
|
69
|
+
setError("");
|
|
70
|
+
setIsTranslating(true);
|
|
71
|
+
try {
|
|
72
|
+
const result = await request("translate");
|
|
73
|
+
for (const entry of result?.values || []){
|
|
74
|
+
documentForm.dispatchFields({
|
|
75
|
+
...entry.fieldType === "richText" ? {
|
|
76
|
+
initialValue: entry.value
|
|
77
|
+
} : {},
|
|
78
|
+
path: entry.path,
|
|
79
|
+
type: "UPDATE",
|
|
80
|
+
value: entry.value
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
documentForm.setModified(true);
|
|
84
|
+
setAvailable(false);
|
|
85
|
+
} catch (translationError) {
|
|
86
|
+
setError(translationError instanceof Error ? translationError.message : "Could not translate this locale.");
|
|
87
|
+
} finally{
|
|
88
|
+
setIsTranslating(false);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
if (isChecking || !available) return null;
|
|
92
|
+
return /*#__PURE__*/ _jsxs("div", {
|
|
93
|
+
className: styles.wrapper,
|
|
94
|
+
children: [
|
|
95
|
+
error && /*#__PURE__*/ _jsx("span", {
|
|
96
|
+
className: styles.error,
|
|
97
|
+
role: "alert",
|
|
98
|
+
children: error
|
|
99
|
+
}),
|
|
100
|
+
/*#__PURE__*/ _jsx(Button, {
|
|
101
|
+
buttonStyle: "subtle",
|
|
102
|
+
disabled: isTranslating,
|
|
103
|
+
margin: false,
|
|
104
|
+
onClick: ()=>void translate(),
|
|
105
|
+
size: "medium",
|
|
106
|
+
children: isTranslating ? "Translating..." : "Translate"
|
|
107
|
+
})
|
|
108
|
+
]
|
|
109
|
+
});
|
|
110
|
+
};
|
|
111
|
+
export default TranslateDocumentButton;
|
package/dist/exports/client.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import Dashboard from "../components/dashboard/Dashboard.js";
|
|
2
2
|
import APIKeyField from "../components/APIKeyField.js";
|
|
3
3
|
import AIInput from "../components/ai-input/AIInput.js";
|
|
4
|
+
import GenerateField from "../components/generate-field/GenerateField.js";
|
|
5
|
+
import TranslateDocumentButton from "../components/translate-document/TranslateDocumentButton.js";
|
|
4
6
|
export { Dashboard };
|
|
5
7
|
export { APIKeyField as AIApiKeyField };
|
|
6
8
|
export { AIInput };
|
|
9
|
+
export { GenerateField };
|
|
10
|
+
export { TranslateDocumentButton };
|
package/dist/exports/client.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import Dashboard from "../components/dashboard/Dashboard.js";
|
|
2
2
|
import APIKeyField from "../components/APIKeyField.js";
|
|
3
3
|
import AIInput from "../components/ai-input/AIInput.js";
|
|
4
|
+
import GenerateField from "../components/generate-field/GenerateField.js";
|
|
5
|
+
import TranslateDocumentButton from "../components/translate-document/TranslateDocumentButton.js";
|
|
4
6
|
export { Dashboard };
|
|
5
7
|
export { APIKeyField as AIApiKeyField };
|
|
6
8
|
export { AIInput };
|
|
9
|
+
export { GenerateField };
|
|
10
|
+
export { TranslateDocumentButton };
|
|
@@ -39,6 +39,7 @@ type ChatOptions = {
|
|
|
39
39
|
maxOutputTokens?: number;
|
|
40
40
|
maxTokenUsage?: ResolvedMaxTokenUsageOptions;
|
|
41
41
|
models?: AIModelConfig;
|
|
42
|
+
promptCaching?: boolean;
|
|
42
43
|
providers?: ResolvedAIProviderConfig[];
|
|
43
44
|
};
|
|
44
45
|
export declare const createChatHandler: (options?: ChatOptions) => PayloadHandler;
|