@ai-stack/payloadcms 3.76.0-beta.1 → 3.76.0-beta.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/dist/ai/core/generateObject.js +1 -4
- package/dist/ai/core/generateObject.js.map +1 -1
- package/dist/ai/core/generateText.js +1 -5
- package/dist/ai/core/generateText.js.map +1 -1
- package/dist/ai/core/media/generateMedia.js +1 -4
- package/dist/ai/core/media/generateMedia.js.map +1 -1
- package/dist/ai/core/media/image/generateImage.js +4 -14
- package/dist/ai/core/media/image/generateImage.js.map +1 -1
- package/dist/ai/core/media/image/handlers/multimodal.js +7 -24
- package/dist/ai/core/media/image/handlers/multimodal.js.map +1 -1
- package/dist/ai/core/media/image/handlers/standard.js +7 -2
- package/dist/ai/core/media/image/handlers/standard.js.map +1 -1
- package/dist/ai/core/media/speech/generateSpeech.js +2 -3
- package/dist/ai/core/media/speech/generateSpeech.js.map +1 -1
- package/dist/ai/core/media/types.d.ts +2 -3
- package/dist/ai/core/media/types.js.map +1 -1
- package/dist/ai/core/streamObject.js +0 -3
- package/dist/ai/core/streamObject.js.map +1 -1
- package/dist/ai/core/streamText.js +1 -4
- package/dist/ai/core/streamText.js.map +1 -1
- package/dist/ai/core/types.d.ts +2 -2
- package/dist/ai/core/types.js.map +1 -1
- package/dist/ai/providers/blocks/anthropic.js +2 -44
- package/dist/ai/providers/blocks/anthropic.js.map +1 -1
- package/dist/ai/providers/blocks/elevenlabs.js +4 -109
- package/dist/ai/providers/blocks/elevenlabs.js.map +1 -1
- package/dist/ai/providers/blocks/fal.js +2 -120
- package/dist/ai/providers/blocks/fal.js.map +1 -1
- package/dist/ai/providers/blocks/google.js +6 -240
- package/dist/ai/providers/blocks/google.js.map +1 -1
- package/dist/ai/providers/blocks/openai-compatible.js +2 -146
- package/dist/ai/providers/blocks/openai-compatible.js.map +1 -1
- package/dist/ai/providers/blocks/openai.js +2 -202
- package/dist/ai/providers/blocks/openai.js.map +1 -1
- package/dist/ai/providers/blocks/xai.js +2 -55
- package/dist/ai/providers/blocks/xai.js.map +1 -1
- package/dist/ai/providers/index.d.ts +1 -1
- package/dist/ai/providers/index.js +0 -2
- package/dist/ai/providers/index.js.map +1 -1
- package/dist/ai/providers/registry.d.ts +24 -28
- package/dist/ai/providers/registry.js +184 -138
- package/dist/ai/providers/registry.js.map +1 -1
- package/dist/ai/providers/types.d.ts +12 -33
- package/dist/ai/providers/types.js +0 -1
- package/dist/ai/providers/types.js.map +1 -1
- package/dist/ai/schemas/lexicalJsonSchema.js +1 -1
- package/dist/ai/schemas/lexicalJsonSchema.js.map +1 -1
- package/dist/ai/utilities/filterEditorSchemaByNodes.js.map +1 -0
- package/dist/ai/utilities/generateFileNameByPrompt.js.map +1 -0
- package/dist/ai/utilities/isObjectSchema.js.map +1 -0
- package/dist/ai/{utils → utilities}/nodeToSchemaMap.js +6 -6
- package/dist/ai/utilities/nodeToSchemaMap.js.map +1 -0
- package/dist/ai/{prompts.d.ts → utilities/prompts.d.ts} +1 -1
- package/dist/ai/utilities/prompts.js.map +1 -0
- package/dist/ai/utilities/systemGenerate.js.map +1 -0
- package/dist/collections/AIJobs.js +3 -12
- package/dist/collections/AIJobs.js.map +1 -1
- package/dist/collections/AIProviders.js +56 -29
- package/dist/collections/AIProviders.js.map +1 -1
- package/dist/collections/Instructions.js +91 -59
- package/dist/collections/Instructions.js.map +1 -1
- package/dist/collections/shared.d.ts +30 -0
- package/dist/collections/shared.js +15 -0
- package/dist/collections/shared.js.map +1 -0
- package/dist/endpoints/fetchFields.js +14 -6
- package/dist/endpoints/fetchFields.js.map +1 -1
- package/dist/endpoints/fetchVoices.js +1 -1
- package/dist/endpoints/fetchVoices.js.map +1 -1
- package/dist/endpoints/generate.d.ts +7 -0
- package/dist/endpoints/generate.js +268 -0
- package/dist/endpoints/generate.js.map +1 -0
- package/dist/endpoints/index.js +9 -639
- package/dist/endpoints/index.js.map +1 -1
- package/dist/endpoints/promptMentions.d.ts +2 -0
- package/dist/endpoints/promptMentions.js +166 -0
- package/dist/endpoints/promptMentions.js.map +1 -0
- package/dist/endpoints/upload.d.ts +7 -0
- package/dist/endpoints/upload.js +294 -0
- package/dist/endpoints/upload.js.map +1 -0
- package/dist/endpoints/videogenWebhook.d.ts +7 -0
- package/dist/endpoints/videogenWebhook.js +132 -0
- package/dist/endpoints/videogenWebhook.js.map +1 -0
- package/dist/exports/client.d.ts +2 -1
- package/dist/exports/client.js +2 -1
- package/dist/exports/client.js.map +1 -1
- package/dist/exports/fields.d.ts +2 -1
- package/dist/exports/fields.js +2 -1
- package/dist/exports/fields.js.map +1 -1
- package/dist/fields/ArrayComposeField/ArrayComposeField.js +1 -1
- package/dist/fields/ArrayComposeField/ArrayComposeField.js.map +1 -1
- package/dist/fields/ArrayComposeField/ArrayComposeField.jsx +1 -1
- package/dist/fields/ComposeField/ComposeField.d.ts +1 -0
- package/dist/fields/ComposeField/ComposeField.js +18 -8
- package/dist/fields/ComposeField/ComposeField.js.map +1 -1
- package/dist/fields/ComposeField/ComposeField.jsx +12 -7
- package/dist/fields/LexicalEditor/feature.server.js +1 -1
- package/dist/fields/LexicalEditor/feature.server.js.map +1 -1
- package/dist/fields/PromptEditorField/feature.client.d.ts +1 -0
- package/dist/fields/PromptEditorField/feature.client.js +173 -0
- package/dist/fields/PromptEditorField/feature.client.js.map +1 -0
- package/dist/fields/PromptEditorField/feature.client.jsx +148 -0
- package/dist/fields/PromptEditorField/feature.server.d.ts +1 -0
- package/dist/fields/PromptEditorField/feature.server.js +30 -0
- package/dist/fields/PromptEditorField/feature.server.js.map +1 -0
- package/dist/fields/PromptField.d.ts +4 -0
- package/dist/fields/PromptField.js +18 -0
- package/dist/fields/PromptField.js.map +1 -0
- package/dist/fields/SelectField/SelectField.js +0 -1
- package/dist/fields/SelectField/SelectField.js.map +1 -1
- package/dist/fields/SelectField/SelectField.jsx +0 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/libraries/handlebars/helpers.js +2 -2
- package/dist/libraries/handlebars/helpers.js.map +1 -1
- package/dist/libraries/handlebars/replacePlaceholders.js +5 -1
- package/dist/libraries/handlebars/replacePlaceholders.js.map +1 -1
- package/dist/payload-ai.d.ts +5 -19
- package/dist/plugin.js +18 -21
- package/dist/plugin.js.map +1 -1
- package/dist/providers/FieldProvider/FieldProvider.js +11 -20
- package/dist/providers/FieldProvider/FieldProvider.js.map +1 -1
- package/dist/providers/FieldProvider/FieldProvider.jsx +8 -18
- package/dist/providers/InstructionsProvider/InstructionsProvider.js +5 -2
- package/dist/providers/InstructionsProvider/InstructionsProvider.js.map +1 -1
- package/dist/providers/InstructionsProvider/InstructionsProvider.jsx +5 -2
- package/dist/translations/de.json +47 -0
- package/dist/translations/en.json +45 -2
- package/dist/translations/es.json +45 -2
- package/dist/translations/fa.json +45 -2
- package/dist/translations/fr.json +46 -3
- package/dist/translations/hi.json +47 -0
- package/dist/translations/index.d.ts +88 -26
- package/dist/translations/index.js +18 -32
- package/dist/translations/index.js.map +1 -1
- package/dist/translations/ja.json +47 -0
- package/dist/translations/nb.json +47 -0
- package/dist/translations/nl.json +47 -0
- package/dist/translations/pl.json +45 -2
- package/dist/translations/pt.json +47 -0
- package/dist/translations/ru.json +45 -2
- package/dist/translations/th.json +47 -0
- package/dist/translations/translation-schema.json +184 -11
- package/dist/translations/uk.json +45 -2
- package/dist/translations/zh.json +47 -0
- package/dist/types.d.ts +64 -28
- package/dist/types.js.map +1 -1
- package/dist/ui/Compose/Compose.js +42 -79
- package/dist/ui/Compose/Compose.js.map +1 -1
- package/dist/ui/Compose/Compose.jsx +32 -86
- package/dist/ui/Compose/ComposePlaceholder.js +1 -1
- package/dist/ui/Compose/ComposePlaceholder.js.map +1 -1
- package/dist/ui/Compose/ComposePlaceholder.jsx +1 -1
- package/dist/ui/Compose/{compose.module.css → compose.module.scss} +3 -5
- package/dist/ui/Compose/hooks/menu/Item.d.ts +1 -1
- package/dist/ui/Compose/hooks/menu/Item.js +7 -3
- package/dist/ui/Compose/hooks/menu/Item.js.map +1 -1
- package/dist/ui/Compose/hooks/menu/Item.jsx +11 -5
- package/dist/ui/Compose/hooks/menu/TranslateMenu.js +15 -5
- package/dist/ui/Compose/hooks/menu/TranslateMenu.js.map +1 -1
- package/dist/ui/Compose/hooks/menu/TranslateMenu.jsx +11 -5
- package/dist/ui/Compose/hooks/menu/items.d.ts +8 -8
- package/dist/ui/Compose/hooks/menu/itemsMap.d.ts +2 -1
- package/dist/ui/Compose/hooks/menu/itemsMap.js.map +1 -1
- package/dist/ui/Compose/hooks/menu/types.d.ts +21 -0
- package/dist/ui/Compose/hooks/menu/types.js +3 -0
- package/dist/ui/Compose/hooks/menu/types.js.map +1 -0
- package/dist/ui/Compose/hooks/menu/useMenu.d.ts +2 -2
- package/dist/ui/Compose/hooks/menu/useMenu.js +45 -23
- package/dist/ui/Compose/hooks/menu/useMenu.js.map +1 -1
- package/dist/ui/Compose/hooks/menu/useMenu.jsx +43 -23
- package/dist/ui/Compose/hooks/mergeGeneratedValue.d.ts +14 -0
- package/dist/ui/Compose/hooks/mergeGeneratedValue.js +38 -0
- package/dist/ui/Compose/hooks/mergeGeneratedValue.js.map +1 -0
- package/dist/ui/Compose/hooks/useGenerate.js +37 -12
- package/dist/ui/Compose/hooks/useGenerate.js.map +1 -1
- package/dist/ui/Compose/hooks/useGenerateUpload.js +66 -24
- package/dist/ui/Compose/hooks/useGenerateUpload.js.map +1 -1
- package/dist/ui/Compose/hooks/useHistory.js +1 -1
- package/dist/ui/Compose/hooks/useHistory.js.map +1 -1
- package/dist/ui/Compose/hooks/useStreamingUpdate.js +4 -4
- package/dist/ui/Compose/hooks/useStreamingUpdate.js.map +1 -1
- package/dist/ui/ConfigDashboard/configDashboard.module.css +94 -0
- package/dist/ui/ConfigDashboard/index.js +27 -92
- package/dist/ui/ConfigDashboard/index.js.map +1 -1
- package/dist/ui/ConfigDashboard/index.jsx +24 -77
- package/dist/ui/DynamicModelSelect/index.js +6 -27
- package/dist/ui/DynamicModelSelect/index.js.map +1 -1
- package/dist/ui/DynamicModelSelect/index.jsx +6 -29
- package/dist/ui/DynamicProviderSelect/index.js +6 -27
- package/dist/ui/DynamicProviderSelect/index.js.map +1 -1
- package/dist/ui/DynamicProviderSelect/index.jsx +6 -29
- package/dist/ui/DynamicVoiceSelect/index.js +34 -83
- package/dist/ui/DynamicVoiceSelect/index.js.map +1 -1
- package/dist/ui/DynamicVoiceSelect/index.jsx +16 -53
- package/dist/ui/GlobalProviderOptions/index.d.ts +2 -0
- package/dist/ui/GlobalProviderOptions/index.js +118 -0
- package/dist/ui/GlobalProviderOptions/index.js.map +1 -0
- package/dist/ui/GlobalProviderOptions/index.jsx +60 -0
- package/dist/ui/Icons/Icons.js +1 -1
- package/dist/ui/Icons/Icons.js.map +1 -1
- package/dist/ui/Icons/Icons.jsx +1 -1
- package/dist/ui/Icons/LottieAnimation.js +1 -1
- package/dist/ui/Icons/LottieAnimation.js.map +1 -1
- package/dist/ui/Icons/LottieAnimation.jsx +1 -1
- package/dist/ui/InstructionProviderOptions/ProviderOptionsTree.d.ts +12 -0
- package/dist/ui/InstructionProviderOptions/ProviderOptionsTree.js +166 -0
- package/dist/ui/InstructionProviderOptions/ProviderOptionsTree.js.map +1 -0
- package/dist/ui/InstructionProviderOptions/ProviderOptionsTree.jsx +83 -0
- package/dist/ui/InstructionProviderOptions/index.d.ts +2 -0
- package/dist/ui/InstructionProviderOptions/index.js +157 -0
- package/dist/ui/InstructionProviderOptions/index.js.map +1 -0
- package/dist/ui/InstructionProviderOptions/index.jsx +92 -0
- package/dist/ui/VoicesFetcher/index.js.map +1 -1
- package/dist/ui/hooks/useAISettings.d.ts +26 -0
- package/dist/ui/hooks/useAISettings.js +73 -0
- package/dist/ui/hooks/useAISettings.js.map +1 -0
- package/dist/ui/providerOptions/updateProviderOptionsValue.d.ts +6 -0
- package/dist/ui/providerOptions/updateProviderOptionsValue.js +50 -0
- package/dist/ui/providerOptions/updateProviderOptionsValue.js.map +1 -0
- package/dist/ui/shared/handleSelectChange.d.ts +5 -0
- package/dist/ui/shared/handleSelectChange.js +12 -0
- package/dist/ui/shared/handleSelectChange.js.map +1 -0
- package/dist/ui/shared/types.d.ts +11 -0
- package/dist/ui/shared/types.js +5 -0
- package/dist/ui/shared/types.js.map +1 -0
- package/dist/utilities/ai/resolveEffectiveInstructionSettings.d.ts +15 -0
- package/dist/utilities/ai/resolveEffectiveInstructionSettings.js +136 -0
- package/dist/utilities/ai/resolveEffectiveInstructionSettings.js.map +1 -0
- package/dist/{endpoints → utilities}/buildPromptUtils.js +14 -5
- package/dist/utilities/buildPromptUtils.js.map +1 -0
- package/dist/utilities/buildSmartPrompt.js +3 -3
- package/dist/utilities/buildSmartPrompt.js.map +1 -1
- package/dist/utilities/fields/fieldToJsonSchema.js.map +1 -0
- package/dist/utilities/fields/getFieldBySchemaPath.js.map +1 -0
- package/dist/utilities/fields/getFieldInfo.js.map +1 -0
- package/dist/utilities/{updateFieldsConfig.js → fields/updateFieldsConfig.js} +8 -3
- package/dist/utilities/fields/updateFieldsConfig.js.map +1 -0
- package/dist/utilities/images/extractImageData.js.map +1 -0
- package/dist/utilities/images/extractPromptAttachments.js.map +1 -0
- package/dist/utilities/{fetchImages.d.ts → images/fetchImages.d.ts} +1 -1
- package/dist/utilities/images/fetchImages.js +49 -0
- package/dist/utilities/images/fetchImages.js.map +1 -0
- package/dist/utilities/images/resolveImageReferences.js +183 -0
- package/dist/utilities/images/resolveImageReferences.js.map +1 -0
- package/dist/utilities/init/autoSetupProviders.d.ts +3 -0
- package/dist/utilities/init/autoSetupProviders.js +216 -0
- package/dist/utilities/init/autoSetupProviders.js.map +1 -0
- package/dist/utilities/lexical/editorSchemaValidator.js.map +1 -0
- package/dist/utilities/lexical/lexicalToHTML.js.map +1 -0
- package/dist/utilities/lexical/lexicalToPromptTemplate.d.ts +2 -0
- package/dist/utilities/lexical/lexicalToPromptTemplate.js +50 -0
- package/dist/utilities/lexical/lexicalToPromptTemplate.js.map +1 -0
- package/dist/utilities/lexical/setSafeLexicalState.js.map +1 -0
- package/dist/utilities/lexical/stringToLexicalJSON.d.ts +2 -0
- package/dist/utilities/lexical/stringToLexicalJSON.js +39 -0
- package/dist/utilities/lexical/stringToLexicalJSON.js.map +1 -0
- package/dist/utilities/sanitizeLog.d.ts +1 -0
- package/dist/utilities/sanitizeLog.js +39 -0
- package/dist/utilities/sanitizeLog.js.map +1 -0
- package/dist/utilities/seedProperties.js +54 -22
- package/dist/utilities/seedProperties.js.map +1 -1
- package/package.json +36 -2
- package/dist/ai/analyse.d.ts +0 -1
- package/dist/ai/analyse.js +0 -3
- package/dist/ai/analyse.js.map +0 -1
- package/dist/ai/index.d.ts +0 -11
- package/dist/ai/index.js +0 -25
- package/dist/ai/index.js.map +0 -1
- package/dist/ai/prompts.js.map +0 -1
- package/dist/ai/utils/filterEditorSchemaByNodes.js.map +0 -1
- package/dist/ai/utils/generateFileNameByPrompt.js.map +0 -1
- package/dist/ai/utils/isObjectSchema.js.map +0 -1
- package/dist/ai/utils/nodeToSchemaMap.js.map +0 -1
- package/dist/ai/utils/systemGenerate.js.map +0 -1
- package/dist/endpoints/buildPromptUtils.js.map +0 -1
- package/dist/endpoints/chat.d.ts +0 -4
- package/dist/fields/PromptEditorField/PromptEditorField.d.ts +0 -3
- package/dist/fields/PromptEditorField/PromptEditorField.js +0 -217
- package/dist/fields/PromptEditorField/PromptEditorField.js.map +0 -1
- package/dist/fields/PromptEditorField/PromptEditorField.jsx +0 -160
- package/dist/ui/AIConfigDashboard/index.d.ts +0 -2
- package/dist/ui/AIConfigDashboard/index.js +0 -224
- package/dist/ui/AIConfigDashboard/index.js.map +0 -1
- package/dist/ui/AIConfigDashboard/index.jsx +0 -175
- package/dist/ui/ProviderOptionsEditor/index.d.ts +0 -7
- package/dist/ui/ProviderOptionsEditor/index.js +0 -291
- package/dist/ui/ProviderOptionsEditor/index.js.map +0 -1
- package/dist/ui/ProviderOptionsEditor/index.jsx +0 -210
- package/dist/utilities/editorSchemaValidator.js.map +0 -1
- package/dist/utilities/extractImageData.js.map +0 -1
- package/dist/utilities/extractPromptAttachments.js.map +0 -1
- package/dist/utilities/fetchImages.js +0 -38
- package/dist/utilities/fetchImages.js.map +0 -1
- package/dist/utilities/fieldToJsonSchema.js.map +0 -1
- package/dist/utilities/getFieldBySchemaPath.js.map +0 -1
- package/dist/utilities/getFieldInfo.js.map +0 -1
- package/dist/utilities/getProviderOptionsFields.d.ts +0 -16
- package/dist/utilities/getProviderOptionsFields.js +0 -80
- package/dist/utilities/getProviderOptionsFields.js.map +0 -1
- package/dist/utilities/isPluginActivated.d.ts +0 -2
- package/dist/utilities/isPluginActivated.js +0 -5
- package/dist/utilities/isPluginActivated.js.map +0 -1
- package/dist/utilities/lexicalToHTML.js.map +0 -1
- package/dist/utilities/resolveImageReferences.js +0 -167
- package/dist/utilities/resolveImageReferences.js.map +0 -1
- package/dist/utilities/schemaConverter.d.ts +0 -3
- package/dist/utilities/schemaConverter.js +0 -93
- package/dist/utilities/schemaConverter.js.map +0 -1
- package/dist/utilities/setSafeLexicalState.js.map +0 -1
- package/dist/utilities/updateFieldsConfig.js.map +0 -1
- /package/dist/ai/{utils → utilities}/filterEditorSchemaByNodes.d.ts +0 -0
- /package/dist/ai/{utils → utilities}/filterEditorSchemaByNodes.js +0 -0
- /package/dist/ai/{utils → utilities}/generateFileNameByPrompt.d.ts +0 -0
- /package/dist/ai/{utils → utilities}/generateFileNameByPrompt.js +0 -0
- /package/dist/ai/{utils → utilities}/isObjectSchema.d.ts +0 -0
- /package/dist/ai/{utils → utilities}/isObjectSchema.js +0 -0
- /package/dist/ai/{utils → utilities}/nodeToSchemaMap.d.ts +0 -0
- /package/dist/ai/{prompts.js → utilities/prompts.js} +0 -0
- /package/dist/ai/{utils → utilities}/systemGenerate.d.ts +0 -0
- /package/dist/ai/{utils → utilities}/systemGenerate.js +0 -0
- /package/dist/ui/Icons/{icons.module.css → icons.module.scss} +0 -0
- /package/dist/{endpoints → utilities}/buildPromptUtils.d.ts +0 -0
- /package/dist/utilities/{fieldToJsonSchema.d.ts → fields/fieldToJsonSchema.d.ts} +0 -0
- /package/dist/utilities/{fieldToJsonSchema.js → fields/fieldToJsonSchema.js} +0 -0
- /package/dist/utilities/{getFieldBySchemaPath.d.ts → fields/getFieldBySchemaPath.d.ts} +0 -0
- /package/dist/utilities/{getFieldBySchemaPath.js → fields/getFieldBySchemaPath.js} +0 -0
- /package/dist/utilities/{getFieldInfo.d.ts → fields/getFieldInfo.d.ts} +0 -0
- /package/dist/utilities/{getFieldInfo.js → fields/getFieldInfo.js} +0 -0
- /package/dist/utilities/{updateFieldsConfig.d.ts → fields/updateFieldsConfig.d.ts} +0 -0
- /package/dist/utilities/{extractImageData.d.ts → images/extractImageData.d.ts} +0 -0
- /package/dist/utilities/{extractImageData.js → images/extractImageData.js} +0 -0
- /package/dist/utilities/{extractPromptAttachments.d.ts → images/extractPromptAttachments.d.ts} +0 -0
- /package/dist/utilities/{extractPromptAttachments.js → images/extractPromptAttachments.js} +0 -0
- /package/dist/utilities/{resolveImageReferences.d.ts → images/resolveImageReferences.d.ts} +0 -0
- /package/dist/utilities/{editorSchemaValidator.d.ts → lexical/editorSchemaValidator.d.ts} +0 -0
- /package/dist/utilities/{editorSchemaValidator.js → lexical/editorSchemaValidator.js} +0 -0
- /package/dist/utilities/{lexicalToHTML.d.ts → lexical/lexicalToHTML.d.ts} +0 -0
- /package/dist/utilities/{lexicalToHTML.js → lexical/lexicalToHTML.js} +0 -0
- /package/dist/utilities/{setSafeLexicalState.d.ts → lexical/setSafeLexicalState.d.ts} +0 -0
- /package/dist/utilities/{setSafeLexicalState.js → lexical/setSafeLexicalState.js} +0 -0
package/dist/endpoints/index.js
CHANGED
|
@@ -1,652 +1,22 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { replacePlaceholders } from '../libraries/handlebars/replacePlaceholders.js';
|
|
7
|
-
import { buildSmartPrompt, isGenericPrompt } from '../utilities/buildSmartPrompt.js';
|
|
8
|
-
import { extractImageData } from '../utilities/extractImageData.js';
|
|
9
|
-
import { fetchImages } from '../utilities/fetchImages.js';
|
|
10
|
-
import { fieldToJsonSchema } from '../utilities/fieldToJsonSchema.js';
|
|
11
|
-
import { getFieldBySchemaPath } from '../utilities/getFieldBySchemaPath.js';
|
|
12
|
-
import { resolveImageReferences } from '../utilities/resolveImageReferences.js';
|
|
13
|
-
import { assignPrompt, extendContextWithPromptFields } from './buildPromptUtils.js';
|
|
1
|
+
import { PLUGIN_API_ENDPOINT_GENERATE, PLUGIN_API_ENDPOINT_GENERATE_UPLOAD, PLUGIN_API_ENDPOINT_VIDEOGEN_WEBHOOK } from '../defaults.js';
|
|
2
|
+
import { generateHandler } from './generate.js';
|
|
3
|
+
import { promptMentionsEndpoint } from './promptMentions.js';
|
|
4
|
+
import { uploadHandler } from './upload.js';
|
|
5
|
+
import { videogenWebhookHandler } from './videogenWebhook.js';
|
|
14
6
|
export const endpoints = (pluginConfig)=>({
|
|
7
|
+
promptMentions: promptMentionsEndpoint,
|
|
15
8
|
textarea: {
|
|
16
|
-
|
|
17
|
-
handler: async (req)=>{
|
|
18
|
-
try {
|
|
19
|
-
// Check authentication and authorization first
|
|
20
|
-
await checkAccess(req, pluginConfig);
|
|
21
|
-
const data = await req.json?.();
|
|
22
|
-
const { allowedEditorNodes = [], locale = 'en', options } = data;
|
|
23
|
-
const { action, actionParams, instructionId } = options;
|
|
24
|
-
const contextData = data.doc;
|
|
25
|
-
if (!instructionId) {
|
|
26
|
-
throw new Error(`Instruction ID is required for "${PLUGIN_NAME}" to work, please check your configuration, or try again`);
|
|
27
|
-
}
|
|
28
|
-
// Verify user has access to the specific instruction
|
|
29
|
-
const instructions = await req.payload.findByID({
|
|
30
|
-
id: instructionId,
|
|
31
|
-
collection: PLUGIN_INSTRUCTIONS_TABLE,
|
|
32
|
-
req
|
|
33
|
-
});
|
|
34
|
-
const { collections } = req.payload.config;
|
|
35
|
-
const collection = collections.find((collection)=>collection.slug === PLUGIN_INSTRUCTIONS_TABLE);
|
|
36
|
-
if (!collection) {
|
|
37
|
-
throw new Error('Collection not found');
|
|
38
|
-
}
|
|
39
|
-
const { custom: { [PLUGIN_NAME]: { editorConfig = {} } = {} } = {} } = collection.admin;
|
|
40
|
-
const { schema: editorSchema = {} } = editorConfig;
|
|
41
|
-
let { prompt: promptTemplate = '' } = instructions;
|
|
42
|
-
// Smart fallback: if prompt is generic, build a contextual prompt from field metadata
|
|
43
|
-
if (isGenericPrompt(promptTemplate)) {
|
|
44
|
-
const schemaPath = String(instructions['schema-path']);
|
|
45
|
-
promptTemplate = buildSmartPrompt({
|
|
46
|
-
documentData: contextData,
|
|
47
|
-
payload: req.payload,
|
|
48
|
-
schemaPath
|
|
49
|
-
});
|
|
50
|
-
if (pluginConfig.debugging) {
|
|
51
|
-
req.payload.logger.info({
|
|
52
|
-
smartPrompt: promptTemplate
|
|
53
|
-
}, `— AI Plugin: Using smart fallback prompt for ${schemaPath}`);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
let allowedEditorSchema = editorSchema;
|
|
57
|
-
if (allowedEditorNodes.length) {
|
|
58
|
-
allowedEditorSchema = filterEditorSchemaByNodes(editorSchema, allowedEditorNodes);
|
|
59
|
-
// Debug: Log what nodes were received and what definitions remain
|
|
60
|
-
if (pluginConfig.debugging) {
|
|
61
|
-
req.payload.logger.info({
|
|
62
|
-
receivedNodes: allowedEditorNodes,
|
|
63
|
-
remainingDefinitions: Object.keys(allowedEditorSchema.definitions || {})
|
|
64
|
-
}, '— AI Plugin: Schema filtering debug');
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
const schemaPath = String(instructions['schema-path']);
|
|
68
|
-
const parts = (schemaPath || '').split('.') || [];
|
|
69
|
-
const collectionName = parts[0];
|
|
70
|
-
const fieldName = parts.length > 1 ? parts[parts.length - 1] : '';
|
|
71
|
-
registerEditorHelper(req.payload, schemaPath);
|
|
72
|
-
const { defaultLocale, locales = [] } = req.payload.config.localization || {};
|
|
73
|
-
const localeData = locales.find((l)=>{
|
|
74
|
-
return l.code === locale;
|
|
75
|
-
});
|
|
76
|
-
let localeInfo = locale;
|
|
77
|
-
if (localeData && defaultLocale && localeData.label && typeof localeData.label === 'object' && defaultLocale in localeData.label) {
|
|
78
|
-
localeInfo = localeData.label[defaultLocale];
|
|
79
|
-
}
|
|
80
|
-
const prompts = await assignPrompt(action, {
|
|
81
|
-
type: String(instructions['field-type']),
|
|
82
|
-
actionParams,
|
|
83
|
-
collection: collectionName,
|
|
84
|
-
context: contextData,
|
|
85
|
-
field: fieldName || '',
|
|
86
|
-
layout: instructions.layout,
|
|
87
|
-
locale: localeInfo,
|
|
88
|
-
pluginConfig,
|
|
89
|
-
systemPrompt: instructions.system,
|
|
90
|
-
template: String(promptTemplate)
|
|
91
|
-
});
|
|
92
|
-
if (pluginConfig.debugging) {
|
|
93
|
-
req.payload.logger.info({
|
|
94
|
-
prompts
|
|
95
|
-
}, `— AI Plugin: Executing text prompt on ${schemaPath}`);
|
|
96
|
-
}
|
|
97
|
-
// Build per-field JSON schema for structured generation when applicable
|
|
98
|
-
let jsonSchema = allowedEditorSchema;
|
|
99
|
-
let targetField;
|
|
100
|
-
try {
|
|
101
|
-
const targetCollection = req.payload.config.collections.find((c)=>c.slug === collectionName);
|
|
102
|
-
if (targetCollection && fieldName) {
|
|
103
|
-
targetField = getFieldBySchemaPath(targetCollection, schemaPath);
|
|
104
|
-
const supported = [
|
|
105
|
-
'array',
|
|
106
|
-
'text',
|
|
107
|
-
'textarea',
|
|
108
|
-
'select',
|
|
109
|
-
'number',
|
|
110
|
-
'date',
|
|
111
|
-
'code',
|
|
112
|
-
'email',
|
|
113
|
-
'json'
|
|
114
|
-
];
|
|
115
|
-
const t = String(targetField?.type || '');
|
|
116
|
-
if (targetField && supported.includes(t)) {
|
|
117
|
-
// For array fields, use count from array-settings if available
|
|
118
|
-
if (t === 'array') {
|
|
119
|
-
const arraySettings = instructions['array-settings'] || {};
|
|
120
|
-
const count = arraySettings.count || 3;
|
|
121
|
-
// Override the field's maxRows with the requested count
|
|
122
|
-
const modifiedField = {
|
|
123
|
-
...targetField,
|
|
124
|
-
maxRows: count,
|
|
125
|
-
minRows: count
|
|
126
|
-
};
|
|
127
|
-
jsonSchema = fieldToJsonSchema(modifiedField, {
|
|
128
|
-
nameOverride: fieldName
|
|
129
|
-
});
|
|
130
|
-
} else {
|
|
131
|
-
jsonSchema = fieldToJsonSchema(targetField, {
|
|
132
|
-
nameOverride: fieldName
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
} catch (e) {
|
|
138
|
-
req.payload.logger.error(e, '— AI Plugin: Error building field JSON schema');
|
|
139
|
-
}
|
|
140
|
-
// Get model settings from instruction
|
|
141
|
-
const settingsName = instructions['model-id'] === 'richtext' ? 'richtext-settings' : instructions['model-id'] === 'text' ? 'text-settings' : instructions['model-id'] === 'array' ? 'array-settings' : undefined;
|
|
142
|
-
if (!settingsName) {
|
|
143
|
-
throw new Error(`Unsupported model-id: ${instructions['model-id']}`);
|
|
144
|
-
}
|
|
145
|
-
const modelSettings = instructions[settingsName] || {};
|
|
146
|
-
// Resolve @field:filename references from the prompt
|
|
147
|
-
const { images: resolvedImages, processedPrompt } = await resolveImageReferences(prompts.prompt, contextData, req, collectionName);
|
|
148
|
-
// Extract hardcoded URLs from the processed prompt
|
|
149
|
-
const hardcodedImages = extractImageData(processedPrompt);
|
|
150
|
-
// Combine images
|
|
151
|
-
const allImages = [
|
|
152
|
-
...hardcodedImages,
|
|
153
|
-
...resolvedImages
|
|
154
|
-
];
|
|
155
|
-
let images;
|
|
156
|
-
if (allImages.length > 0) {
|
|
157
|
-
const imageParts = await fetchImages(req, allImages);
|
|
158
|
-
if (imageParts.length > 0) {
|
|
159
|
-
images = imageParts;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
let promptToUse = processedPrompt;
|
|
163
|
-
let systemToUse = prompts.system;
|
|
164
|
-
// let messagesToUse: any = undefined
|
|
165
|
-
// Execute beforeGenerate hooks
|
|
166
|
-
if (targetField && targetField.custom?.ai?.beforeGenerate) {
|
|
167
|
-
const beforeHooks = targetField.custom.ai.beforeGenerate;
|
|
168
|
-
for (const hook of beforeHooks){
|
|
169
|
-
const result = await hook({
|
|
170
|
-
doc: contextData,
|
|
171
|
-
field: targetField,
|
|
172
|
-
headers: req.headers,
|
|
173
|
-
instructions,
|
|
174
|
-
payload: req.payload,
|
|
175
|
-
prompt: promptToUse,
|
|
176
|
-
req,
|
|
177
|
-
system: systemToUse
|
|
178
|
-
});
|
|
179
|
-
if (result) {
|
|
180
|
-
if (result.prompt) promptToUse = result.prompt;
|
|
181
|
-
if (result.system) systemToUse = result.system;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
const streamResult = await req.payload.ai.streamObject({
|
|
186
|
-
// extractAttachments: modelSettings.extractAttachments as boolean | undefined,
|
|
187
|
-
images,
|
|
188
|
-
maxTokens: modelSettings.maxTokens,
|
|
189
|
-
// messages: messagesToUse,
|
|
190
|
-
model: modelSettings.model,
|
|
191
|
-
onFinish: async ({ object })=>{
|
|
192
|
-
if (targetField && targetField.custom?.ai?.afterGenerate) {
|
|
193
|
-
const afterHooks = targetField.custom.ai.afterGenerate;
|
|
194
|
-
for (const hook of afterHooks){
|
|
195
|
-
await hook({
|
|
196
|
-
doc: contextData,
|
|
197
|
-
field: targetField,
|
|
198
|
-
headers: req.headers,
|
|
199
|
-
instructions,
|
|
200
|
-
payload: req.payload,
|
|
201
|
-
req,
|
|
202
|
-
result: object
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
},
|
|
207
|
-
prompt: processedPrompt,
|
|
208
|
-
provider: modelSettings.provider,
|
|
209
|
-
providerOptions: {
|
|
210
|
-
openai: {
|
|
211
|
-
strictJsonSchema: true
|
|
212
|
-
}
|
|
213
|
-
},
|
|
214
|
-
schema: jsonSchema,
|
|
215
|
-
system: systemToUse,
|
|
216
|
-
temperature: modelSettings.temperature
|
|
217
|
-
});
|
|
218
|
-
return streamResult;
|
|
219
|
-
} catch (error) {
|
|
220
|
-
req.payload.logger.error(error, 'Error generating content: ');
|
|
221
|
-
const message = error && typeof error === 'object' && 'message' in error ? error.message : String(error);
|
|
222
|
-
return new Response(JSON.stringify({
|
|
223
|
-
error: message
|
|
224
|
-
}), {
|
|
225
|
-
headers: {
|
|
226
|
-
'Content-Type': 'application/json'
|
|
227
|
-
},
|
|
228
|
-
status: message.includes('Authentication required') || message.includes('Insufficient permissions') ? 401 : 500
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
},
|
|
9
|
+
handler: generateHandler(pluginConfig),
|
|
232
10
|
method: 'post',
|
|
233
11
|
path: PLUGIN_API_ENDPOINT_GENERATE
|
|
234
12
|
},
|
|
235
13
|
upload: {
|
|
236
|
-
|
|
237
|
-
handler: async (req)=>{
|
|
238
|
-
try {
|
|
239
|
-
// Check authentication and authorization first
|
|
240
|
-
await checkAccess(req, pluginConfig);
|
|
241
|
-
const data = await req.json?.();
|
|
242
|
-
const { collectionSlug, documentId, options } = data;
|
|
243
|
-
const { instructionId } = options;
|
|
244
|
-
let docData = {};
|
|
245
|
-
if (documentId) {
|
|
246
|
-
try {
|
|
247
|
-
docData = await req.payload.findByID({
|
|
248
|
-
id: documentId,
|
|
249
|
-
collection: collectionSlug,
|
|
250
|
-
draft: true,
|
|
251
|
-
req
|
|
252
|
-
});
|
|
253
|
-
} catch (e) {
|
|
254
|
-
req.payload.logger.error(e, '— AI Plugin: Error fetching document, you should try again after enabling drafts for this collection');
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
const contextData = {
|
|
258
|
-
...data.doc,
|
|
259
|
-
...docData
|
|
260
|
-
};
|
|
261
|
-
let instructions = {
|
|
262
|
-
images: [],
|
|
263
|
-
'model-id': '',
|
|
264
|
-
prompt: ''
|
|
265
|
-
};
|
|
266
|
-
if (instructionId) {
|
|
267
|
-
// Verify user has access to the specific instruction
|
|
268
|
-
instructions = await req.payload.findByID({
|
|
269
|
-
id: instructionId,
|
|
270
|
-
collection: PLUGIN_INSTRUCTIONS_TABLE,
|
|
271
|
-
req
|
|
272
|
-
});
|
|
273
|
-
}
|
|
274
|
-
let { prompt: promptTemplate = '' } = instructions;
|
|
275
|
-
const { images: sampleImages = [] } = instructions;
|
|
276
|
-
const schemaPath = String(instructions['schema-path']);
|
|
277
|
-
registerEditorHelper(req.payload, schemaPath);
|
|
278
|
-
// Smart fallback: if prompt is generic, build a contextual prompt from field metadata
|
|
279
|
-
if (isGenericPrompt(promptTemplate)) {
|
|
280
|
-
promptTemplate = buildSmartPrompt({
|
|
281
|
-
documentData: contextData,
|
|
282
|
-
payload: req.payload,
|
|
283
|
-
schemaPath
|
|
284
|
-
});
|
|
285
|
-
if (pluginConfig.debugging) {
|
|
286
|
-
req.payload.logger.info({
|
|
287
|
-
smartPrompt: promptTemplate
|
|
288
|
-
}, `— AI Plugin: Using smart fallback prompt for ${schemaPath}`);
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
const extendedContext = extendContextWithPromptFields(contextData, {
|
|
292
|
-
type: String(instructions['field-type']),
|
|
293
|
-
collection: collectionSlug
|
|
294
|
-
}, pluginConfig);
|
|
295
|
-
const text = await replacePlaceholders(promptTemplate, extendedContext);
|
|
296
|
-
const uploadCollectionSlug = instructions['relation-to'];
|
|
297
|
-
// Resolve @field:filename references from the prompt
|
|
298
|
-
const { images: resolvedImages, processedPrompt } = await resolveImageReferences(text, contextData, req, collectionSlug);
|
|
299
|
-
// Extract hardcoded URLs from the processed prompt and merge with resolved images and sample images
|
|
300
|
-
const images = [
|
|
301
|
-
...extractImageData(processedPrompt),
|
|
302
|
-
...resolvedImages,
|
|
303
|
-
...sampleImages
|
|
304
|
-
];
|
|
305
|
-
// Process images - convert to ImagePart format using helper
|
|
306
|
-
const editImages = await fetchImages(req, images);
|
|
307
|
-
let promptToUse = text;
|
|
308
|
-
let targetField;
|
|
309
|
-
try {
|
|
310
|
-
const targetCollection = req.payload.config.collections.find((c)=>c.slug === collectionSlug);
|
|
311
|
-
if (targetCollection && schemaPath) {
|
|
312
|
-
targetField = getFieldBySchemaPath(targetCollection, schemaPath);
|
|
313
|
-
}
|
|
314
|
-
} catch (e) {
|
|
315
|
-
req.payload.logger.error(e, '— AI Plugin: Error finding field for hooks');
|
|
316
|
-
}
|
|
317
|
-
if (targetField && targetField.custom?.ai?.beforeGenerate) {
|
|
318
|
-
const beforeHooks = targetField.custom.ai.beforeGenerate;
|
|
319
|
-
for (const hook of beforeHooks){
|
|
320
|
-
const result = await hook({
|
|
321
|
-
doc: contextData,
|
|
322
|
-
field: targetField,
|
|
323
|
-
headers: req.headers,
|
|
324
|
-
instructions,
|
|
325
|
-
payload: req.payload,
|
|
326
|
-
prompt: promptToUse,
|
|
327
|
-
req
|
|
328
|
-
});
|
|
329
|
-
if (result) {
|
|
330
|
-
if (result.prompt) {
|
|
331
|
-
promptToUse = result.prompt;
|
|
332
|
-
}
|
|
333
|
-
if (result.instructions) {
|
|
334
|
-
instructions = {
|
|
335
|
-
...instructions,
|
|
336
|
-
...result.instructions
|
|
337
|
-
};
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
if (pluginConfig.debugging) {
|
|
343
|
-
req.payload.logger.info({
|
|
344
|
-
text: promptToUse
|
|
345
|
-
}, `— AI Plugin: Executing media generation`);
|
|
346
|
-
}
|
|
347
|
-
// Prepare callback URL for async jobs
|
|
348
|
-
const serverURL = req.payload.config?.serverURL || process.env.SERVER_URL || process.env.NEXT_PUBLIC_SERVER_URL;
|
|
349
|
-
const callbackUrl = serverURL ? `${serverURL.replace(/\/$/, '')}/api${PLUGIN_API_ENDPOINT_VIDEOGEN_WEBHOOK}?instructionId=${instructionId}` : undefined;
|
|
350
|
-
// Get model settings
|
|
351
|
-
// Re-evaluate settings name and settings in case instructions changed
|
|
352
|
-
const modelId = instructions['model-id'];
|
|
353
|
-
const settingsName = modelId === 'image' ? 'image-settings' : modelId === 'video' ? 'video-settings' : modelId === 'tts' ? 'tts-settings' : undefined;
|
|
354
|
-
if (!settingsName) {
|
|
355
|
-
throw new Error(`Unsupported model-id: ${modelId}`);
|
|
356
|
-
}
|
|
357
|
-
// Get model settings from instruction
|
|
358
|
-
const instructionSettings = instructions[settingsName] || {};
|
|
359
|
-
// Fallback to AISettings global defaults if instruction-level settings are missing
|
|
360
|
-
let globalDefaults = {};
|
|
361
|
-
if (!instructionSettings.provider || !instructionSettings.model) {
|
|
362
|
-
try {
|
|
363
|
-
const aiSettings = await req.payload.findGlobal({
|
|
364
|
-
slug: 'ai-providers',
|
|
365
|
-
context: {
|
|
366
|
-
unsafe: true
|
|
367
|
-
}
|
|
368
|
-
});
|
|
369
|
-
// Map modelId to the corresponding default settings key
|
|
370
|
-
const defaultsKey = modelId === 'image' ? 'image' : modelId === 'video' ? 'video' : modelId === 'tts' ? 'tts' : undefined;
|
|
371
|
-
if (defaultsKey && aiSettings?.defaults?.[defaultsKey]) {
|
|
372
|
-
globalDefaults = aiSettings.defaults[defaultsKey];
|
|
373
|
-
if (pluginConfig.debugging) {
|
|
374
|
-
req.payload.logger.info({
|
|
375
|
-
globalDefaults
|
|
376
|
-
}, `— AI Plugin: Using AISettings defaults for ${modelId}`);
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
} catch (e) {
|
|
380
|
-
req.payload.logger.error(e, '— AI Plugin: Error fetching AISettings defaults');
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
// Merge: instruction settings take priority over global defaults
|
|
384
|
-
// Filter out null/undefined values so they don't overwrite valid defaults
|
|
385
|
-
const filteredInstructionSettings = Object.fromEntries(Object.entries(instructionSettings).filter(([_, v])=>v != null));
|
|
386
|
-
const modelSettings = {
|
|
387
|
-
...globalDefaults,
|
|
388
|
-
...filteredInstructionSettings
|
|
389
|
-
};
|
|
390
|
-
// Use payload.ai.generateMedia directly! 🎉
|
|
391
|
-
const result = await req.payload.ai.generateMedia({
|
|
392
|
-
callbackUrl,
|
|
393
|
-
images: editImages,
|
|
394
|
-
instructionId,
|
|
395
|
-
model: modelSettings.model,
|
|
396
|
-
prompt: promptToUse,
|
|
397
|
-
provider: modelSettings.provider,
|
|
398
|
-
...modelSettings
|
|
399
|
-
});
|
|
400
|
-
if (targetField && targetField.custom?.ai?.afterGenerate) {
|
|
401
|
-
const afterHooks = targetField.custom.ai.afterGenerate;
|
|
402
|
-
for (const hook of afterHooks){
|
|
403
|
-
await hook({
|
|
404
|
-
doc: contextData,
|
|
405
|
-
field: targetField,
|
|
406
|
-
headers: req.headers,
|
|
407
|
-
instructions,
|
|
408
|
-
payload: req.payload,
|
|
409
|
-
req,
|
|
410
|
-
result
|
|
411
|
-
});
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
// If model returned files immediately, proceed with upload
|
|
415
|
-
if (result && 'files' in result && Array.isArray(result.files) && result.files.length > 0) {
|
|
416
|
-
const uploadedDocs = [];
|
|
417
|
-
for (const file of result.files){
|
|
418
|
-
let assetData;
|
|
419
|
-
// Create a synthetic result for the single file to pass to mediaUpload
|
|
420
|
-
const singleFileResult = {
|
|
421
|
-
files: [
|
|
422
|
-
file
|
|
423
|
-
]
|
|
424
|
-
};
|
|
425
|
-
if (typeof pluginConfig.mediaUpload === 'function') {
|
|
426
|
-
const uploadResult = await pluginConfig.mediaUpload(singleFileResult, {
|
|
427
|
-
collection: uploadCollectionSlug,
|
|
428
|
-
request: req
|
|
429
|
-
});
|
|
430
|
-
assetData = {
|
|
431
|
-
id: uploadResult.id,
|
|
432
|
-
alt: uploadResult.alt
|
|
433
|
-
};
|
|
434
|
-
} else {
|
|
435
|
-
const created = await req.payload.create({
|
|
436
|
-
collection: uploadCollectionSlug,
|
|
437
|
-
data: {
|
|
438
|
-
alt: text
|
|
439
|
-
},
|
|
440
|
-
file,
|
|
441
|
-
req
|
|
442
|
-
});
|
|
443
|
-
assetData = {
|
|
444
|
-
id: created.id,
|
|
445
|
-
alt: created.alt
|
|
446
|
-
};
|
|
447
|
-
}
|
|
448
|
-
if (assetData.id) {
|
|
449
|
-
uploadedDocs.push(assetData);
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
if (uploadedDocs.length === 0) {
|
|
453
|
-
req.payload.logger.error('Error uploading generated media, is your media upload function correct?');
|
|
454
|
-
throw new Error('Error uploading generated media!');
|
|
455
|
-
}
|
|
456
|
-
// Check if target field supports multiple values
|
|
457
|
-
let hasMany = false;
|
|
458
|
-
if (targetField) {
|
|
459
|
-
if (targetField.type === 'relationship' || targetField.type === 'upload' || targetField.type === 'select') {
|
|
460
|
-
hasMany = targetField.hasMany === true;
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
if (hasMany) {
|
|
464
|
-
return new Response(JSON.stringify({
|
|
465
|
-
result: uploadedDocs.map((d)=>({
|
|
466
|
-
id: d.id,
|
|
467
|
-
alt: d.alt
|
|
468
|
-
}))
|
|
469
|
-
}));
|
|
470
|
-
}
|
|
471
|
-
return new Response(JSON.stringify({
|
|
472
|
-
result: {
|
|
473
|
-
id: uploadedDocs[0].id,
|
|
474
|
-
alt: uploadedDocs[0].alt
|
|
475
|
-
}
|
|
476
|
-
}));
|
|
477
|
-
}
|
|
478
|
-
// Otherwise, assume async job launch
|
|
479
|
-
if (result && ('jobId' in result || 'taskId' in result)) {
|
|
480
|
-
const externalTaskId = result.jobId || result.taskId;
|
|
481
|
-
const status = result.status || 'queued';
|
|
482
|
-
const progress = result.progress ?? 0;
|
|
483
|
-
// Create AI Job doc and return only its id
|
|
484
|
-
const createdJob = await req.payload.create({
|
|
485
|
-
collection: PLUGIN_AI_JOBS_TABLE,
|
|
486
|
-
data: {
|
|
487
|
-
instructionId,
|
|
488
|
-
progress,
|
|
489
|
-
status,
|
|
490
|
-
task_id: externalTaskId
|
|
491
|
-
},
|
|
492
|
-
overrideAccess: true,
|
|
493
|
-
req
|
|
494
|
-
});
|
|
495
|
-
return new Response(JSON.stringify({
|
|
496
|
-
job: {
|
|
497
|
-
id: createdJob.id
|
|
498
|
-
}
|
|
499
|
-
}), {
|
|
500
|
-
headers: {
|
|
501
|
-
'Content-Type': 'application/json'
|
|
502
|
-
}
|
|
503
|
-
});
|
|
504
|
-
}
|
|
505
|
-
throw new Error('Unexpected model response.');
|
|
506
|
-
} catch (error) {
|
|
507
|
-
req.payload.logger.error(// @ts-expect-error
|
|
508
|
-
error?.type || error.message, 'Error generating upload: ');
|
|
509
|
-
const message = error && typeof error === 'object' && 'message' in error ? error.message : String(error);
|
|
510
|
-
return new Response(JSON.stringify({
|
|
511
|
-
error: message
|
|
512
|
-
}), {
|
|
513
|
-
headers: {
|
|
514
|
-
'Content-Type': 'application/json'
|
|
515
|
-
},
|
|
516
|
-
status: message.includes('Authentication required') || message.includes('Insufficient permissions') ? 401 : 500
|
|
517
|
-
});
|
|
518
|
-
}
|
|
519
|
-
},
|
|
14
|
+
handler: uploadHandler(pluginConfig),
|
|
520
15
|
method: 'post',
|
|
521
16
|
path: PLUGIN_API_ENDPOINT_GENERATE_UPLOAD
|
|
522
17
|
},
|
|
523
18
|
videogenWebhook: {
|
|
524
|
-
handler:
|
|
525
|
-
console.log('videogenWebhook --> ', req);
|
|
526
|
-
try {
|
|
527
|
-
const urlAll = new URL(req.url || '');
|
|
528
|
-
const qpSecret = urlAll.searchParams.get('secret') || '';
|
|
529
|
-
const headerSecret = req.headers.get('x-webhook-secret') || '';
|
|
530
|
-
const falSecret = process.env.FAL_WEBHOOK_SECRET;
|
|
531
|
-
const legacySecret = process.env.VIDEOGEN_WEBHOOK_SECRET;
|
|
532
|
-
const provided = qpSecret || headerSecret;
|
|
533
|
-
// TODO: fal is failing because of auth but webhook seem to work
|
|
534
|
-
if (!provided || (falSecret ? provided !== falSecret : provided !== legacySecret)) {
|
|
535
|
-
return new Response('Unauthorized', {
|
|
536
|
-
status: 401
|
|
537
|
-
});
|
|
538
|
-
}
|
|
539
|
-
const instructionId = urlAll.searchParams.get('instructionId');
|
|
540
|
-
if (!instructionId) {
|
|
541
|
-
throw new Error('instructionId missing');
|
|
542
|
-
}
|
|
543
|
-
const body = await req.json?.();
|
|
544
|
-
// Normalize fal webhook payload
|
|
545
|
-
const status = body && (body.status || body.data?.status || body.response?.status) || undefined;
|
|
546
|
-
const progress = (body && (body.progress ?? body.data?.progress ?? body.response?.progress)) ?? undefined;
|
|
547
|
-
const requestId = body && (body.taskId || body.request_id || body.gateway_request_id || body.request?.request_id) || undefined;
|
|
548
|
-
const error = body?.error || body?.data?.error || body?.response?.error;
|
|
549
|
-
// Update AI Job row by task_id (and instructionId)
|
|
550
|
-
const jobSearch = await req.payload.find({
|
|
551
|
-
collection: PLUGIN_AI_JOBS_TABLE,
|
|
552
|
-
depth: 0,
|
|
553
|
-
limit: 1,
|
|
554
|
-
where: {
|
|
555
|
-
and: [
|
|
556
|
-
{
|
|
557
|
-
task_id: {
|
|
558
|
-
equals: requestId
|
|
559
|
-
}
|
|
560
|
-
},
|
|
561
|
-
{
|
|
562
|
-
instructionId: {
|
|
563
|
-
equals: instructionId
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
]
|
|
567
|
-
}
|
|
568
|
-
});
|
|
569
|
-
const jobDoc = jobSearch.docs?.[0];
|
|
570
|
-
if (jobDoc) {
|
|
571
|
-
await req.payload.update({
|
|
572
|
-
id: jobDoc.id,
|
|
573
|
-
collection: PLUGIN_AI_JOBS_TABLE,
|
|
574
|
-
data: {
|
|
575
|
-
progress,
|
|
576
|
-
status,
|
|
577
|
-
task_id: requestId
|
|
578
|
-
},
|
|
579
|
-
overrideAccess: true,
|
|
580
|
-
req
|
|
581
|
-
});
|
|
582
|
-
}
|
|
583
|
-
console.log('fal webhook body: ', body);
|
|
584
|
-
const videoUrl = body?.outputs?.[0]?.url || body?.data?.outputs?.[0]?.url || body?.video?.url || body?.data?.video?.url || body?.response?.video?.url || body?.videos?.[0]?.url || body?.data?.videos?.[0]?.url;
|
|
585
|
-
if (status === 'completed' && videoUrl) {
|
|
586
|
-
// Fetch the related instruction to get upload collection
|
|
587
|
-
const instructions = await req.payload.findByID({
|
|
588
|
-
id: instructionId,
|
|
589
|
-
collection: PLUGIN_INSTRUCTIONS_TABLE,
|
|
590
|
-
req
|
|
591
|
-
});
|
|
592
|
-
const uploadCollectionSlug = instructions['relation-to'];
|
|
593
|
-
const videoResp = await fetch(videoUrl);
|
|
594
|
-
if (!videoResp.ok) {
|
|
595
|
-
throw new Error(`Failed to fetch output: ${videoResp.status}`);
|
|
596
|
-
}
|
|
597
|
-
const buffer = Buffer.from(await videoResp.arrayBuffer());
|
|
598
|
-
const created = await req.payload.create({
|
|
599
|
-
collection: uploadCollectionSlug,
|
|
600
|
-
data: {
|
|
601
|
-
alt: 'video generation'
|
|
602
|
-
},
|
|
603
|
-
file: {
|
|
604
|
-
name: 'video_generation.mp4',
|
|
605
|
-
data: buffer,
|
|
606
|
-
mimetype: 'video/mp4',
|
|
607
|
-
size: buffer.byteLength
|
|
608
|
-
},
|
|
609
|
-
overrideAccess: true,
|
|
610
|
-
req
|
|
611
|
-
});
|
|
612
|
-
// Persist the result on the AI Job record
|
|
613
|
-
if (jobDoc) {
|
|
614
|
-
await req.payload.update({
|
|
615
|
-
id: jobDoc.id,
|
|
616
|
-
collection: PLUGIN_AI_JOBS_TABLE,
|
|
617
|
-
data: {
|
|
618
|
-
progress: 100,
|
|
619
|
-
result_id: created?.id,
|
|
620
|
-
status: 'completed'
|
|
621
|
-
},
|
|
622
|
-
overrideAccess: true,
|
|
623
|
-
req
|
|
624
|
-
});
|
|
625
|
-
}
|
|
626
|
-
}
|
|
627
|
-
if (status === 'failed' && error) {
|
|
628
|
-
req.payload.logger.error(error, 'Video generation failed: ');
|
|
629
|
-
}
|
|
630
|
-
return new Response(JSON.stringify({
|
|
631
|
-
ok: true
|
|
632
|
-
}), {
|
|
633
|
-
headers: {
|
|
634
|
-
'Content-Type': 'application/json'
|
|
635
|
-
}
|
|
636
|
-
});
|
|
637
|
-
} catch (error) {
|
|
638
|
-
req.payload.logger.error(error, 'Error in videogen webhook: ');
|
|
639
|
-
const message = error && typeof error === 'object' && 'message' in error ? error.message : String(error);
|
|
640
|
-
return new Response(JSON.stringify({
|
|
641
|
-
error: message
|
|
642
|
-
}), {
|
|
643
|
-
headers: {
|
|
644
|
-
'Content-Type': 'application/json'
|
|
645
|
-
},
|
|
646
|
-
status: 500
|
|
647
|
-
});
|
|
648
|
-
}
|
|
649
|
-
},
|
|
19
|
+
handler: videogenWebhookHandler,
|
|
650
20
|
method: 'post',
|
|
651
21
|
path: PLUGIN_API_ENDPOINT_VIDEOGEN_WEBHOOK
|
|
652
22
|
}
|