@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.
Files changed (36) hide show
  1. package/README.md +60 -0
  2. package/dist/ai/promptCaching.d.ts +23 -0
  3. package/dist/ai/promptCaching.js +120 -0
  4. package/dist/ai/proposalRepair.d.ts +30 -0
  5. package/dist/ai/proposalRepair.js +76 -0
  6. package/dist/ai/sensitiveData.d.ts +1 -0
  7. package/dist/ai/sensitiveData.js +3 -1
  8. package/dist/components/ai-input/AIInput.js +35 -5
  9. package/dist/components/ai-input/AIInput.module.css +49 -0
  10. package/dist/components/generate-field/GenerateField.d.ts +11 -0
  11. package/dist/components/generate-field/GenerateField.js +109 -0
  12. package/dist/components/generate-field/GenerateField.module.css +39 -0
  13. package/dist/components/generate-field/richText.d.ts +29 -0
  14. package/dist/components/generate-field/richText.js +30 -0
  15. package/dist/components/translate-document/TranslateDocumentButton.d.ts +2 -0
  16. package/dist/components/translate-document/TranslateDocumentButton.js +111 -0
  17. package/dist/components/translate-document/TranslateDocumentButton.module.css +11 -0
  18. package/dist/exports/client.d.ts +4 -0
  19. package/dist/exports/client.js +4 -0
  20. package/dist/handlers/chatHandler.d.ts +1 -0
  21. package/dist/handlers/chatHandler.js +464 -138
  22. package/dist/handlers/generateFieldHandler.d.ts +14 -0
  23. package/dist/handlers/generateFieldHandler.js +144 -0
  24. package/dist/handlers/translateDocumentHandler.d.ts +14 -0
  25. package/dist/handlers/translateDocumentHandler.js +186 -0
  26. package/dist/index.d.ts +4 -0
  27. package/dist/index.js +112 -4
  28. package/dist/payload/documentTranslation.d.ts +15 -0
  29. package/dist/payload/documentTranslation.js +120 -0
  30. package/dist/payload/textFieldGeneration.d.ts +26 -0
  31. package/dist/payload/textFieldGeneration.js +99 -0
  32. package/dist/payload/toolFieldSelection.d.ts +25 -0
  33. package/dist/payload/toolFieldSelection.js +118 -0
  34. package/dist/payload/toolSchemas.d.ts +7 -0
  35. package/dist/payload/toolSchemas.js +117 -0
  36. 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,2 @@
1
+ declare const TranslateDocumentButton: () => import("react").JSX.Element | null;
2
+ export default TranslateDocumentButton;
@@ -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;
@@ -0,0 +1,11 @@
1
+ .wrapper {
2
+ align-items: center;
3
+ display: flex;
4
+ gap: 8px;
5
+ }
6
+
7
+ .error {
8
+ color: var(--theme-error-500);
9
+ font-size: 12px;
10
+ max-width: 240px;
11
+ }
@@ -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 };
@@ -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;