@jhb.software/payload-alt-text-plugin 0.10.0 → 0.11.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.
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/plugin.ts"],"sourcesContent":["import type { Config, Widget } from 'payload'\n\nimport type {\n AltTextPluginConfig,\n IncomingAltTextPluginConfig,\n} from './types/AltTextPluginConfig.js'\n\nimport { PLUGIN_SLUG } from './constants.js'\nimport { altTextHealthEndpoint } from './endpoints/altTextHealth.js'\nimport { bulkGenerateAltTextsEndpoint } from './endpoints/bulkGenerateAltTexts.js'\nimport { generateAltTextEndpoint } from './endpoints/generateAltText.js'\nimport { altTextField } from './fields/altTextField.js'\nimport { keywordsField } from './fields/keywordsField.js'\nimport {\n createRevalidateAltTextHealthAfterChangeHook,\n createRevalidateAltTextHealthAfterDeleteHook,\n} from './hooks/revalidateAltTextHealth.js'\nimport { translations } from './translations/index.js'\nimport { isValidMimeType, normalizeCollectionsConfig } from './utilities/mimeTypes.js'\nimport { deepMergeSimple } from './utils/deepMergeSimple.js'\n\nconst altTextHealthWidgetDefinition = {\n slug: 'alt-text-health',\n // `Component` was renamed from `ComponentPath` in Payload 3.79.0. Set both for backward compatibility.\n Component: '@jhb.software/payload-alt-text-plugin/server#AltTextHealthWidget',\n ComponentPath: '@jhb.software/payload-alt-text-plugin/server#AltTextHealthWidget',\n label: {\n de: 'Alternativtexte Zustand',\n en: 'Alt text health',\n },\n maxWidth: 'full',\n minWidth: 'medium',\n} satisfies { ComponentPath: string } & Widget\n\nexport const payloadAltTextPlugin =\n (incomingPluginConfig: IncomingAltTextPluginConfig) =>\n (incomingConfig: Config): Config => {\n const config = { ...incomingConfig }\n\n // If the plugin is disabled, return the config without modifying it\n if (incomingPluginConfig.enabled === false) {\n return config\n }\n\n const locales = config.localization\n ? config.localization.locales.map((localeConfig) =>\n typeof localeConfig === 'string' ? localeConfig : localeConfig.code,\n )\n : []\n\n const enableHealthCheck = incomingPluginConfig.healthCheck !== false\n\n const normalizedCollections = normalizeCollectionsConfig(incomingPluginConfig.collections, {\n imageThumbnailMimeType: incomingPluginConfig.imageThumbnailMimeType,\n })\n\n // A declared thumbnail MIME type replaces the per-document source check, so a\n // wrong one fails at boot rather than as a silently missing guard or a 500 per\n // image.\n const supportedMimeTypes = incomingPluginConfig.resolver.supportedMimeTypes\n for (const collection of normalizedCollections) {\n const declared = collection.imageThumbnailMimeType\n if (declared === undefined) {\n continue\n }\n\n if (!isValidMimeType(declared)) {\n throw new Error(\n `The alt-text plugin is configured with imageThumbnailMimeType \"${declared}\" for the \"${collection.slug}\" collection, ` +\n 'but that is not a valid MIME type. Expected something like \"image/webp\".',\n )\n }\n\n if (supportedMimeTypes && !supportedMimeTypes.includes(declared)) {\n throw new Error(\n `The alt-text plugin is configured with imageThumbnailMimeType \"${declared}\" for the \"${collection.slug}\" collection, ` +\n `but the \"${incomingPluginConfig.resolver.key}\" resolver does not support it. ` +\n `Supported types: ${supportedMimeTypes.join(', ')}. ` +\n \"Either change the transformation in getImageThumbnail, or remove the declaration to fall back to checking each document's own mime type.\",\n )\n }\n }\n\n const access = incomingPluginConfig.access ?? (({ req }) => !!req.user)\n\n // A function form of `healthCheck` doubles as the health report's access\n // gate; otherwise it falls back to the shared `access`.\n const healthCheckAccess =\n typeof incomingPluginConfig.healthCheck === 'function'\n ? incomingPluginConfig.healthCheck\n : access\n\n const pluginConfig: AltTextPluginConfig = {\n access,\n collections: normalizedCollections,\n enabled: incomingPluginConfig.enabled ?? true,\n fieldsOverride: incomingPluginConfig.fieldsOverride,\n getImageThumbnail: incomingPluginConfig.getImageThumbnail,\n healthCheck: enableHealthCheck,\n healthCheckAccess,\n locale: incomingPluginConfig.locale,\n locales,\n maxBulkGenerateConcurrency: incomingPluginConfig.maxBulkGenerateConcurrency ?? 16,\n maxBulkGenerateIds: incomingPluginConfig.maxBulkGenerateIds ?? 100,\n resolver: incomingPluginConfig.resolver,\n }\n\n // Validate locale requirement for non-localized mode\n if (locales.length === 0 && !incomingPluginConfig.locale) {\n throw new Error(\n 'The alt-text plugin requires a \"locale\" option when Payload localization is disabled. ' +\n 'Please add { locale: \"en\" } (or your preferred locale) to your plugin configuration.',\n )\n }\n\n const collectionConfigBySlug = new Map<string, (typeof normalizedCollections)[number]>(\n normalizedCollections.map((entry) => [entry.slug, entry]),\n )\n\n // Ensure collections array exists\n config.collections = config.collections || []\n\n // Map over collections and inject AI alt text fields into specified ones\n config.collections = config.collections.map((collectionConfig) => {\n const altTextCollectionConfig = collectionConfigBySlug.get(collectionConfig.slug)\n\n if (altTextCollectionConfig) {\n if (!collectionConfig.upload) {\n console.warn(\n `AI Alt Text Plugin: Collection \"${collectionConfig.slug}\" is not an upload collection. Skipping field injection.`,\n )\n return collectionConfig\n }\n\n const defaultFields = [\n altTextField({\n localized: Boolean(config.localization),\n // When the collection declares what getImageThumbnail delivers, the\n // document's own mime type says nothing about whether generation can\n // succeed — so don't let the admin UI disable the button on it.\n supportedMimeTypes: altTextCollectionConfig.imageThumbnailMimeType\n ? undefined\n : pluginConfig.resolver.supportedMimeTypes,\n trackedMimeTypes: altTextCollectionConfig.mimeTypes,\n validate: altTextCollectionConfig.validate,\n }),\n keywordsField({\n localized: Boolean(config.localization),\n }),\n ]\n\n const fields =\n incomingPluginConfig.fieldsOverride &&\n typeof incomingPluginConfig.fieldsOverride === 'function'\n ? incomingPluginConfig.fieldsOverride({ defaultFields })\n : defaultFields\n\n return {\n ...collectionConfig,\n admin: {\n ...collectionConfig.admin,\n components: {\n ...(collectionConfig.admin?.components ?? {}),\n // TODO: use the beforeBulkAction custom component slot once available: https://github.com/payloadcms/payload/pull/11719\n beforeListTable: [\n ...(collectionConfig.admin?.components?.beforeListTable ?? []),\n {\n path: '@jhb.software/payload-alt-text-plugin/client#BulkGenerateAltTextsButton',\n props: {\n collectionSlug: collectionConfig.slug,\n },\n },\n ],\n },\n // enhance the search by adding the filename, keywords and alt fields (if the user has not provided their own listSearchableFields)\n listSearchableFields: collectionConfig.admin?.listSearchableFields ?? [\n 'filename',\n 'keywords',\n 'alt',\n ],\n },\n fields: [...(collectionConfig.fields ?? []), ...fields],\n hooks: {\n ...collectionConfig.hooks,\n ...(enableHealthCheck && {\n afterChange: [\n ...(collectionConfig.hooks?.afterChange ?? []),\n createRevalidateAltTextHealthAfterChangeHook(collectionConfig.slug),\n ],\n afterDelete: [\n ...(collectionConfig.hooks?.afterDelete ?? []),\n createRevalidateAltTextHealthAfterDeleteHook(collectionConfig.slug),\n ],\n }),\n },\n }\n }\n\n return collectionConfig\n })\n\n const existingWidgets = config.admin?.dashboard?.widgets ?? []\n const widgets =\n !enableHealthCheck || existingWidgets.some((widget) => widget.slug === 'alt-text-health')\n ? existingWidgets\n : [...existingWidgets, altTextHealthWidgetDefinition]\n\n return {\n ...config,\n admin: {\n ...config.admin,\n dashboard: {\n ...config.admin?.dashboard,\n widgets,\n },\n },\n custom: {\n ...config.custom,\n // Make plugin config available in hooks/actions\n altTextPluginConfig: pluginConfig,\n },\n endpoints: [\n ...(config.endpoints ?? []),\n {\n handler: generateAltTextEndpoint(pluginConfig.access),\n method: 'post',\n path: `/${PLUGIN_SLUG}/generate`,\n },\n {\n handler: bulkGenerateAltTextsEndpoint(pluginConfig.access),\n method: 'post',\n path: `/${PLUGIN_SLUG}/generate/bulk`,\n },\n ...(enableHealthCheck\n ? [\n {\n handler: altTextHealthEndpoint(pluginConfig.healthCheckAccess),\n method: 'get' as const,\n path: `/${PLUGIN_SLUG}/health`,\n },\n ]\n : []),\n ],\n i18n: {\n ...config.i18n,\n translations: deepMergeSimple(translations, incomingConfig.i18n?.translations ?? {}),\n },\n }\n }\n"],"names":["PLUGIN_SLUG","altTextHealthEndpoint","bulkGenerateAltTextsEndpoint","generateAltTextEndpoint","altTextField","keywordsField","createRevalidateAltTextHealthAfterChangeHook","createRevalidateAltTextHealthAfterDeleteHook","translations","isValidMimeType","normalizeCollectionsConfig","deepMergeSimple","altTextHealthWidgetDefinition","slug","Component","ComponentPath","label","de","en","maxWidth","minWidth","payloadAltTextPlugin","incomingPluginConfig","incomingConfig","config","enabled","locales","localization","map","localeConfig","code","enableHealthCheck","healthCheck","normalizedCollections","collections","imageThumbnailMimeType","supportedMimeTypes","resolver","collection","declared","undefined","Error","includes","key","join","access","req","user","healthCheckAccess","pluginConfig","fieldsOverride","getImageThumbnail","locale","maxBulkGenerateConcurrency","maxBulkGenerateIds","length","collectionConfigBySlug","Map","entry","collectionConfig","altTextCollectionConfig","get","upload","console","warn","defaultFields","localized","Boolean","trackedMimeTypes","mimeTypes","validate","fields","admin","components","beforeListTable","path","props","collectionSlug","listSearchableFields","hooks","afterChange","afterDelete","existingWidgets","dashboard","widgets","some","widget","custom","altTextPluginConfig","endpoints","handler","method","i18n"],"mappings":"AAOA,SAASA,WAAW,QAAQ,iBAAgB;AAC5C,SAASC,qBAAqB,QAAQ,+BAA8B;AACpE,SAASC,4BAA4B,QAAQ,sCAAqC;AAClF,SAASC,uBAAuB,QAAQ,iCAAgC;AACxE,SAASC,YAAY,QAAQ,2BAA0B;AACvD,SAASC,aAAa,QAAQ,4BAA2B;AACzD,SACEC,4CAA4C,EAC5CC,4CAA4C,QACvC,qCAAoC;AAC3C,SAASC,YAAY,QAAQ,0BAAyB;AACtD,SAASC,eAAe,EAAEC,0BAA0B,QAAQ,2BAA0B;AACtF,SAASC,eAAe,QAAQ,6BAA4B;AAE5D,MAAMC,gCAAgC;IACpCC,MAAM;IACN,uGAAuG;IACvGC,WAAW;IACXC,eAAe;IACfC,OAAO;QACLC,IAAI;QACJC,IAAI;IACN;IACAC,UAAU;IACVC,UAAU;AACZ;AAEA,OAAO,MAAMC,uBACX,CAACC,uBACD,CAACC;QACC,MAAMC,SAAS;YAAE,GAAGD,cAAc;QAAC;QAEnC,oEAAoE;QACpE,IAAID,qBAAqBG,OAAO,KAAK,OAAO;YAC1C,OAAOD;QACT;QAEA,MAAME,UAAUF,OAAOG,YAAY,GAC/BH,OAAOG,YA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AG3D,OAAOgD,KAAK,EAAEW,SAAS;oBAC1BC;gBACF;YACF;YACAG,QAAQ;gBACN,GAAG/D,OAAO+D,MAAM;gBAChB,gDAAgD;gBAChDC,qBAAqBvC;YACvB;YACAwC,WAAW;mBACLjE,OAAOiE,SAAS,IAAI,EAAE;gBAC1B;oBACEC,SAASvF,wBAAwB8C,aAAaJ,MAAM;oBACpD8C,QAAQ;oBACRhB,MAAM,CAAC,CAAC,EAAE3E,YAAY,SAAS,CAAC;gBAClC;gBACA;oBACE0F,SAASxF,6BAA6B+C,aAAaJ,MAAM;oBACzD8C,QAAQ;oBACRhB,MAAM,CAAC,CAAC,EAAE3E,YAAY,cAAc,CAAC;gBACvC;mBACI+B,oBACA;oBACE;wBACE2D,SAASzF,sBAAsBgD,aAAaD,iBAAiB;wBAC7D2C,QAAQ;wBACRhB,MAAM,CAAC,CAAC,EAAE3E,YAAY,OAAO,CAAC;oBAChC;iBACD,GACD,EAAE;aACP;YACD4F,MAAM;gBACJ,GAAGpE,OAAOoE,IAAI;gBACdpF,cAAcG,gBAAgBH,cAAce,eAAeqE,IAAI,EAAEpF,gBAAgB,CAAC;YACpF;QACF;IACF,EAAC"}
1
+ {"version":3,"sources":["../src/plugin.ts"],"sourcesContent":["import type { Config, Widget } from 'payload'\n\nimport type {\n AltTextPluginConfig,\n IncomingAltTextPluginConfig,\n} from './types/AltTextPluginConfig.js'\n\nimport { PLUGIN_SLUG } from './constants.js'\nimport { altTextHealthEndpoint } from './endpoints/altTextHealth.js'\nimport { bulkGenerateAltTextsEndpoint } from './endpoints/bulkGenerateAltTexts.js'\nimport { generateAltTextEndpoint } from './endpoints/generateAltText.js'\nimport { altTextField } from './fields/altTextField.js'\nimport { keywordsField } from './fields/keywordsField.js'\nimport {\n createRevalidateAltTextHealthAfterChangeHook,\n createRevalidateAltTextHealthAfterDeleteHook,\n} from './hooks/revalidateAltTextHealth.js'\nimport { translations } from './translations/index.js'\nimport { isValidMimeType, normalizeCollectionsConfig } from './utilities/mimeTypes.js'\nimport { deepMergeSimple } from './utils/deepMergeSimple.js'\n\nconst altTextHealthWidgetDefinition = {\n slug: 'alt-text-health',\n // `Component` was renamed from `ComponentPath` in Payload 3.79.0. Set both for backward compatibility.\n Component: '@jhb.software/payload-alt-text-plugin/server#AltTextHealthWidget',\n ComponentPath: '@jhb.software/payload-alt-text-plugin/server#AltTextHealthWidget',\n label: {\n de: 'Alternativtexte Zustand',\n en: 'Alt text health',\n },\n maxWidth: 'full',\n minWidth: 'medium',\n} satisfies { ComponentPath: string } & Widget\n\nexport const payloadAltTextPlugin =\n (incomingPluginConfig: IncomingAltTextPluginConfig) =>\n (incomingConfig: Config): Config => {\n const config = { ...incomingConfig }\n\n // If the plugin is disabled, return the config without modifying it\n if (incomingPluginConfig.enabled === false) {\n return config\n }\n\n const locales = config.localization\n ? config.localization.locales.map((localeConfig) =>\n typeof localeConfig === 'string' ? localeConfig : localeConfig.code,\n )\n : []\n\n const enableHealthCheck = incomingPluginConfig.healthCheck !== false\n\n const normalizedCollections = normalizeCollectionsConfig(incomingPluginConfig.collections, {\n imageThumbnailMimeType: incomingPluginConfig.imageThumbnailMimeType,\n })\n\n // A declared thumbnail MIME type replaces the per-document source check, so a\n // wrong one fails at boot rather than as a silently missing guard or a 500 per\n // image.\n const supportedMimeTypes = incomingPluginConfig.resolver.supportedMimeTypes\n for (const collection of normalizedCollections) {\n const declared = collection.imageThumbnailMimeType\n if (declared === undefined) {\n continue\n }\n\n if (!isValidMimeType(declared)) {\n throw new Error(\n `The alt-text plugin is configured with imageThumbnailMimeType \"${declared}\" for the \"${collection.slug}\" collection, ` +\n 'but that is not a valid MIME type. Expected something like \"image/webp\".',\n )\n }\n\n if (supportedMimeTypes && !supportedMimeTypes.includes(declared)) {\n throw new Error(\n `The alt-text plugin is configured with imageThumbnailMimeType \"${declared}\" for the \"${collection.slug}\" collection, ` +\n `but the \"${incomingPluginConfig.resolver.key}\" resolver does not support it. ` +\n `Supported types: ${supportedMimeTypes.join(', ')}. ` +\n \"Either change the transformation in getImageThumbnail, or remove the declaration to fall back to checking each document's own mime type.\",\n )\n }\n }\n\n const access = incomingPluginConfig.access ?? (({ req }) => !!req.user)\n\n // The former function form was the health report's access gate. Accepting it\n // silently would widen that gate to the plugin's `access`, so it fails at boot.\n if (typeof incomingPluginConfig.healthCheck === 'function') {\n throw new Error(\n 'The alt-text plugin no longer accepts a function for `healthCheck`. ' +\n 'Move the access check to `healthCheck: { access: ({ req }) => ... }`.',\n )\n }\n\n const healthCheckConfig =\n typeof incomingPluginConfig.healthCheck === 'object' ? incomingPluginConfig.healthCheck : {}\n\n // The health report's own gate, falling back to the shared `access`.\n const healthCheckAccess = healthCheckConfig.access ?? access\n\n const pluginConfig: AltTextPluginConfig = {\n access,\n collections: normalizedCollections,\n enabled: incomingPluginConfig.enabled ?? true,\n fieldsOverride: incomingPluginConfig.fieldsOverride,\n getImageThumbnail: incomingPluginConfig.getImageThumbnail,\n healthCheck: enableHealthCheck,\n healthCheckAccess,\n healthCheckBaseFilter: healthCheckConfig.baseFilter,\n locale: incomingPluginConfig.locale,\n locales,\n maxBulkGenerateConcurrency: incomingPluginConfig.maxBulkGenerateConcurrency ?? 16,\n maxBulkGenerateIds: incomingPluginConfig.maxBulkGenerateIds ?? 100,\n resolver: incomingPluginConfig.resolver,\n }\n\n // Validate locale requirement for non-localized mode\n if (locales.length === 0 && !incomingPluginConfig.locale) {\n throw new Error(\n 'The alt-text plugin requires a \"locale\" option when Payload localization is disabled. ' +\n 'Please add { locale: \"en\" } (or your preferred locale) to your plugin configuration.',\n )\n }\n\n const collectionConfigBySlug = new Map<string, (typeof normalizedCollections)[number]>(\n normalizedCollections.map((entry) => [entry.slug, entry]),\n )\n\n // Collected while collections are mapped and flushed in `onInit`: no Payload instance —\n // and therefore no logger — exists while the config is still being built.\n const configWarnings: string[] = []\n\n // Ensure collections array exists\n config.collections = config.collections || []\n\n // Map over collections and inject AI alt text fields into specified ones\n config.collections = config.collections.map((collectionConfig) => {\n const altTextCollectionConfig = collectionConfigBySlug.get(collectionConfig.slug)\n\n if (altTextCollectionConfig) {\n if (!collectionConfig.upload) {\n configWarnings.push(\n `AI Alt Text Plugin: Collection \"${collectionConfig.slug}\" is not an upload collection. Skipping field injection.`,\n )\n return collectionConfig\n }\n\n const defaultFields = [\n altTextField({\n localized: Boolean(config.localization),\n // When the collection declares what getImageThumbnail delivers, the\n // document's own mime type says nothing about whether generation can\n // succeed — so don't let the admin UI disable the button on it.\n supportedMimeTypes: altTextCollectionConfig.imageThumbnailMimeType\n ? undefined\n : pluginConfig.resolver.supportedMimeTypes,\n trackedMimeTypes: altTextCollectionConfig.mimeTypes,\n validate: altTextCollectionConfig.validate,\n }),\n keywordsField({\n localized: Boolean(config.localization),\n }),\n ]\n\n const fields =\n incomingPluginConfig.fieldsOverride &&\n typeof incomingPluginConfig.fieldsOverride === 'function'\n ? incomingPluginConfig.fieldsOverride({ defaultFields })\n : defaultFields\n\n return {\n ...collectionConfig,\n admin: {\n ...collectionConfig.admin,\n components: {\n ...(collectionConfig.admin?.components ?? {}),\n // TODO: use the beforeBulkAction custom component slot once available: https://github.com/payloadcms/payload/pull/11719\n beforeListTable: [\n ...(collectionConfig.admin?.components?.beforeListTable ?? []),\n {\n path: '@jhb.software/payload-alt-text-plugin/client#BulkGenerateAltTextsButton',\n props: {\n collectionSlug: collectionConfig.slug,\n },\n },\n ],\n },\n // enhance the search by adding the filename, keywords and alt fields (if the user has not provided their own listSearchableFields)\n listSearchableFields: collectionConfig.admin?.listSearchableFields ?? [\n 'filename',\n 'keywords',\n 'alt',\n ],\n },\n fields: [...(collectionConfig.fields ?? []), ...fields],\n hooks: {\n ...collectionConfig.hooks,\n ...(enableHealthCheck && {\n afterChange: [\n ...(collectionConfig.hooks?.afterChange ?? []),\n createRevalidateAltTextHealthAfterChangeHook(collectionConfig.slug),\n ],\n afterDelete: [\n ...(collectionConfig.hooks?.afterDelete ?? []),\n createRevalidateAltTextHealthAfterDeleteHook(collectionConfig.slug),\n ],\n }),\n },\n }\n }\n\n return collectionConfig\n })\n\n const existingWidgets = config.admin?.dashboard?.widgets ?? []\n const widgets =\n !enableHealthCheck || existingWidgets.some((widget) => widget.slug === 'alt-text-health')\n ? existingWidgets\n : [...existingWidgets, altTextHealthWidgetDefinition]\n\n return {\n ...config,\n admin: {\n ...config.admin,\n dashboard: {\n ...config.admin?.dashboard,\n widgets,\n },\n },\n custom: {\n ...config.custom,\n // Make plugin config available in hooks/actions\n altTextPluginConfig: pluginConfig,\n },\n endpoints: [\n ...(config.endpoints ?? []),\n {\n handler: generateAltTextEndpoint(pluginConfig.access),\n method: 'post',\n path: `/${PLUGIN_SLUG}/generate`,\n },\n {\n handler: bulkGenerateAltTextsEndpoint(pluginConfig.access),\n method: 'post',\n path: `/${PLUGIN_SLUG}/generate/bulk`,\n },\n ...(enableHealthCheck\n ? [\n {\n handler: altTextHealthEndpoint(pluginConfig.healthCheckAccess),\n method: 'get' as const,\n path: `/${PLUGIN_SLUG}/health`,\n },\n ]\n : []),\n ],\n i18n: {\n ...config.i18n,\n translations: deepMergeSimple(translations, incomingConfig.i18n?.translations ?? {}),\n },\n onInit: async (payload) => {\n for (const warning of configWarnings) {\n payload.logger.warn(warning)\n }\n\n await config.onInit?.(payload)\n },\n }\n }\n"],"names":["PLUGIN_SLUG","altTextHealthEndpoint","bulkGenerateAltTextsEndpoint","generateAltTextEndpoint","altTextField","keywordsField","createRevalidateAltTextHealthAfterChangeHook","createRevalidateAltTextHealthAfterDeleteHook","translations","isValidMimeType","normalizeCollectionsConfig","deepMergeSimple","altTextHealthWidgetDefinition","slug","Component","ComponentPath","label","de","en","maxWidth","minWidth","payloadAltTextPlugin","incomingPluginConfig","incomingConfig","config","enabled","locales","localization","map","localeConfig","code","enableHealthCheck","healthCheck","normalizedCollections","collections","imageThumbnailMimeType","supportedMimeTypes","resolver","collection","declared","undefined","Error","includes","key","join","access","req","user","healthCheckConfig","healthCheckAccess","pluginConfig","fieldsOverride","getImageThumbnail","healthCheckBaseFilter","baseFilter","locale","maxBulkGenerateConcurrency","maxBulkGenerateIds","length","collectionConfigBySlug","Map","entry","configWarnings","collectionConfig","altTextCollectionConfig","get","upload","push","defaultFields","localized","Boolean","trackedMimeTypes","mimeTypes","validate","fields","admin","components","beforeListTable","path","props","collectionSlug","listSearchableFields","hooks","afterChange","afterDelete","existingWidgets","dashboard","widgets","some","widget","custom","altTextPluginConfig","endpoints","handler","method","i18n","onInit","payload","warning","logger","warn"],"mappings":"AAOA,SAASA,WAAW,QAAQ,iBAAgB;AAC5C,SAASC,qBAAqB,QAAQ,+BAA8B;AACpE,SAASC,4BAA4B,QAAQ,sCAAqC;AAClF,SAASC,uBAAuB,QAAQ,iCAAgC;AACxE,SAASC,YAAY,QAAQ,2BAA0B;AACvD,SAASC,aAAa,QAAQ,4BAA2B;AACzD,SACEC,4CAA4C,EAC5CC,4CAA4C,QACvC,qCAAoC;AAC3C,SAASC,YAAY,QAAQ,0BAAyB;AACtD,SAASC,eAAe,EAAEC,0BAA0B,QAAQ,2BAA0B;AACtF,SAASC,eAAe,QAAQ,6BAA4B;AAE5D,MAAMC,gCAAgC;IACpCC,MAAM;IACN,uGAAuG;IACvGC,WAAW;IACXC,eAAe;IACfC,OAAO;QACLC,IAAI;QACJC,IAAI;IACN;IACAC,UAAU;IACVC,UAAU;AACZ;AAEA,OAAO,MAAMC,uBACX,CAACC,uBACD,CAACC;QACC,MAAMC,SAAS;YAAE,GAAGD,cAAc;QAAC;QAEnC,oEAAoE;QACpE,IAAID,qBAAqBG,OAAO,KAAK,OAAO;YAC1C,OAAOD;QACT;QAEA,MAAME,UAAUF,OAAOG,YAAY,GAC/BH,OAAOG,YAAY,CAACD,OAAO,CAACE,GAAG,CAAC,CAACC,eAC/B,OAAOA,iBAAiB,WAAWA,eAAeA,aAAaC,IAAI,IAErE,EAAE;QAEN,MAAMC,oBAAoBT,qBAAqBU,WAAW,KAAK;QAE/D,MAAMC,wBAAwBvB,2BAA2BY,qBAAqB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;gBACL,GAAGnD,OAAOmD,KAAK;gBACfW,WAAW;oBACT,GAAG9D,OAAOmD,KAAK,EAAEW,SAAS;oBAC1BC;gBACF;YACF;YACAG,QAAQ;gBACN,GAAGlE,OAAOkE,MAAM;gBAChB,gDAAgD;gBAChDC,qBAAqBzC;YACvB;YACA0C,WAAW;mBACLpE,OAAOoE,SAAS,IAAI,EAAE;gBAC1B;oBACEC,SAAS1F,wBAAwB+C,aAAaL,MAAM;oBACpDiD,QAAQ;oBACRhB,MAAM,CAAC,CAAC,EAAE9E,YAAY,SAAS,CAAC;gBAClC;gBACA;oBACE6F,SAAS3F,6BAA6BgD,aAAaL,MAAM;oBACzDiD,QAAQ;oBACRhB,MAAM,CAAC,CAAC,EAAE9E,YAAY,cAAc,CAAC;gBACvC;mBACI+B,oBACA;oBACE;wBACE8D,SAAS5F,sBAAsBiD,aAAaD,iBAAiB;wBAC7D6C,QAAQ;wBACRhB,MAAM,CAAC,CAAC,EAAE9E,YAAY,OAAO,CAAC;oBAChC;iBACD,GACD,EAAE;aACP;YACD+F,MAAM;gBACJ,GAAGvE,OAAOuE,IAAI;gBACdvF,cAAcG,gBAAgBH,cAAce,eAAewE,IAAI,EAAEvF,gBAAgB,CAAC;YACpF;YACAwF,QAAQ,OAAOC;gBACb,KAAK,MAAMC,WAAWpC,eAAgB;oBACpCmC,QAAQE,MAAM,CAACC,IAAI,CAACF;gBACtB;gBAEA,MAAM1E,OAAOwE,MAAM,GAAGC;YACxB;QACF;IACF,EAAC"}
@@ -0,0 +1,65 @@
1
+ import type { VisionInstructions } from './createVisionResolver.js';
2
+ import type { AltTextResolver } from './types.js';
3
+ export type AnthropicResolverConfig = {
4
+ /** Anthropic API key for authentication */
5
+ apiKey: string;
6
+ /**
7
+ * Base URL of the Anthropic API.
8
+ * @default 'https://api.anthropic.com'
9
+ */
10
+ baseUrl?: string;
11
+ /**
12
+ * Caps how long Claude thinks before answering. Lower effort still thinks on a
13
+ * difficult image, just less than a higher setting would.
14
+ *
15
+ * Describing an image is not a reasoning-heavy task, so `'low'` keeps the
16
+ * spend down on the models that accept it. Omitted, the field is not sent and
17
+ * Claude uses its default (`'high'`) — which also keeps models without effort
18
+ * support, such as `claude-haiku-4-5`, usable.
19
+ */
20
+ effort?: 'high' | 'low' | 'max' | 'medium' | 'xhigh';
21
+ /**
22
+ * Builds the instructions from the default ones, e.g. to append a house style
23
+ * rule. Sent as the system prompt, separately from the image.
24
+ *
25
+ * @default ({ defaultInstructions }) => defaultInstructions
26
+ */
27
+ instructions?: VisionInstructions;
28
+ /**
29
+ * The Claude model to use for alt text generation.
30
+ *
31
+ * Must be able to read images. `claude-sonnet-5` is the cheaper choice for a
32
+ * large media library; `claude-haiku-4-5` works too, but only without
33
+ * `effort`.
34
+ *
35
+ * @default 'claude-opus-5'
36
+ */
37
+ model?: string;
38
+ /**
39
+ * Abort after this many milliseconds. Covers downloading the image and the
40
+ * message call together.
41
+ * @default 30000
42
+ */
43
+ timeoutMs?: number;
44
+ };
45
+ /**
46
+ * Creates a Claude-based resolver for alt text generation.
47
+ *
48
+ * The image is downloaded and sent as bytes. Claude can fetch an image URL
49
+ * itself, but that path is not dependable for a CMS: it requires the file to be
50
+ * reachable from the public internet — never true in local development, and not
51
+ * true for private buckets. Sending the bytes removes that whole class of
52
+ * failure for the price of one extra download, and supplies the `media_type`
53
+ * that a base64 image block requires and a URL cannot carry.
54
+ *
55
+ * @example
56
+ * ```typescript
57
+ * import { anthropicResolver } from '@jhb.software/payload-alt-text-plugin'
58
+ *
59
+ * anthropicResolver({
60
+ * apiKey: process.env.ANTHROPIC_API_KEY,
61
+ * model: 'claude-opus-5', // optional, this is the default
62
+ * })
63
+ * ```
64
+ */
65
+ export declare const anthropicResolver: ({ apiKey, baseUrl, effort, instructions, model, timeoutMs, }: AnthropicResolverConfig) => AltTextResolver;
@@ -0,0 +1,141 @@
1
+ import { createVisionResolver, VisionProviderError } from './createVisionResolver.js';
2
+ /**
3
+ * Image formats the Messages API accepts.
4
+ *
5
+ * Narrower than what an upload collection may hold — SVG and AVIF are missing,
6
+ * so the endpoint rejects those documents and their generate button stays
7
+ * disabled instead of failing at the provider.
8
+ *
9
+ * @see https://platform.claude.com/docs/en/build-with-claude/vision
10
+ */ const SUPPORTED_MIME_TYPES = [
11
+ 'image/jpeg',
12
+ 'image/png',
13
+ 'image/gif',
14
+ 'image/webp'
15
+ ];
16
+ /**
17
+ * Claude's 10 MB ceiling is measured on the base64 payload, which inflates the
18
+ * raw bytes by roughly 4/3 — so the guard has to sit at ~7.5 MB of raw image to
19
+ * mean 10 MB on the wire. Checking the raw length against 10 MB would wave
20
+ * through a 9 MB photo that arrives as ~12 MB and is rejected by the provider,
21
+ * costing the download and replacing a readable message with a raw 400.
22
+ */ const MAX_IMAGE_BYTES = Math.floor(10 * 1024 * 1024 * 3 / 4);
23
+ /**
24
+ * Room for one alt text and its keywords per locale, plus the thinking tokens
25
+ * Claude spends before answering. A budget sized for the answer alone would be
26
+ * exhausted while reasoning, and the response would be cut off mid-JSON.
27
+ */ const MAX_TOKENS_PER_LOCALE = 2000;
28
+ /**
29
+ * Creates a Claude-based resolver for alt text generation.
30
+ *
31
+ * The image is downloaded and sent as bytes. Claude can fetch an image URL
32
+ * itself, but that path is not dependable for a CMS: it requires the file to be
33
+ * reachable from the public internet — never true in local development, and not
34
+ * true for private buckets. Sending the bytes removes that whole class of
35
+ * failure for the price of one extra download, and supplies the `media_type`
36
+ * that a base64 image block requires and a URL cannot carry.
37
+ *
38
+ * @example
39
+ * ```typescript
40
+ * import { anthropicResolver } from '@jhb.software/payload-alt-text-plugin'
41
+ *
42
+ * anthropicResolver({
43
+ * apiKey: process.env.ANTHROPIC_API_KEY,
44
+ * model: 'claude-opus-5', // optional, this is the default
45
+ * })
46
+ * ```
47
+ */ export const anthropicResolver = ({ apiKey, baseUrl = 'https://api.anthropic.com', effort, instructions, model = 'claude-opus-5', timeoutMs = 30_000 })=>createVisionResolver({
48
+ apiKey,
49
+ generate: async ({ filename, image, instructions: resolvedInstructions, maxTokens, responseSchema, signal })=>{
50
+ if (!image) {
51
+ throw new Error('The image was not downloaded');
52
+ }
53
+ const response = await fetch(`${baseUrl}/v1/messages`, {
54
+ body: JSON.stringify({
55
+ max_tokens: maxTokens,
56
+ messages: [
57
+ {
58
+ content: [
59
+ // Claude works best when the image comes before the text.
60
+ {
61
+ type: 'image',
62
+ source: {
63
+ type: 'base64',
64
+ data: image.base64,
65
+ media_type: image.mediaType
66
+ }
67
+ },
68
+ ...filename ? [
69
+ {
70
+ type: 'text',
71
+ text: filename
72
+ }
73
+ ] : []
74
+ ],
75
+ role: 'user'
76
+ }
77
+ ],
78
+ model,
79
+ // `format` constrains the response to the schema the plugin needs;
80
+ // `effort` caps how long Claude thinks before producing it. Only sent
81
+ // when configured: some models reject the field outright.
82
+ output_config: {
83
+ ...effort ? {
84
+ effort
85
+ } : {},
86
+ format: {
87
+ type: 'json_schema',
88
+ schema: responseSchema
89
+ }
90
+ },
91
+ // The instructions are an operator instruction, not a turn in the
92
+ // conversation, so they travel as the top-level system prompt.
93
+ system: resolvedInstructions
94
+ }),
95
+ headers: {
96
+ 'anthropic-version': '2023-06-01',
97
+ 'content-type': 'application/json',
98
+ 'x-api-key': apiKey
99
+ },
100
+ method: 'POST',
101
+ signal
102
+ });
103
+ if (!response.ok) {
104
+ // Bounded: unbounded provider text would land in the log as-is.
105
+ const body = (await response.text().catch(()=>'')).slice(0, 500);
106
+ throw new VisionProviderError({
107
+ body,
108
+ label: 'Anthropic',
109
+ status: response.status
110
+ });
111
+ }
112
+ const message = await response.json();
113
+ // A refusal and a truncated answer both arrive as a 200 with unusable
114
+ // content, so they are named rather than surfacing as a JSON parse error.
115
+ if (message.stop_reason === 'refusal') {
116
+ throw new Error('Claude declined to describe this image');
117
+ }
118
+ if (message.stop_reason === 'max_tokens') {
119
+ throw new Error(`Claude ran out of tokens before finishing the alt text (max_tokens: ${maxTokens})`);
120
+ }
121
+ const text = message.content?.find((block)=>block.type === 'text')?.text;
122
+ if (typeof text !== 'string') {
123
+ throw new Error('No result from Anthropic');
124
+ }
125
+ try {
126
+ return JSON.parse(text);
127
+ } catch {
128
+ throw new Error('Claude returned a response that was not valid JSON');
129
+ }
130
+ },
131
+ inlineImage: true,
132
+ instructions,
133
+ key: 'anthropic',
134
+ label: 'Anthropic',
135
+ maxImageBytes: MAX_IMAGE_BYTES,
136
+ maxTokensPerLocale: MAX_TOKENS_PER_LOCALE,
137
+ supportedMimeTypes: SUPPORTED_MIME_TYPES,
138
+ timeoutMs
139
+ });
140
+
141
+ //# sourceMappingURL=anthropic.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/resolvers/anthropic.ts"],"sourcesContent":["import type { VisionInstructions } from './createVisionResolver.js'\nimport type { AltTextResolver } from './types.js'\n\nimport { createVisionResolver, VisionProviderError } from './createVisionResolver.js'\n\nexport type AnthropicResolverConfig = {\n /** Anthropic API key for authentication */\n apiKey: string\n /**\n * Base URL of the Anthropic API.\n * @default 'https://api.anthropic.com'\n */\n baseUrl?: string\n /**\n * Caps how long Claude thinks before answering. Lower effort still thinks on a\n * difficult image, just less than a higher setting would.\n *\n * Describing an image is not a reasoning-heavy task, so `'low'` keeps the\n * spend down on the models that accept it. Omitted, the field is not sent and\n * Claude uses its default (`'high'`) — which also keeps models without effort\n * support, such as `claude-haiku-4-5`, usable.\n */\n effort?: 'high' | 'low' | 'max' | 'medium' | 'xhigh'\n /**\n * Builds the instructions from the default ones, e.g. to append a house style\n * rule. Sent as the system prompt, separately from the image.\n *\n * @default ({ defaultInstructions }) => defaultInstructions\n */\n instructions?: VisionInstructions\n /**\n * The Claude model to use for alt text generation.\n *\n * Must be able to read images. `claude-sonnet-5` is the cheaper choice for a\n * large media library; `claude-haiku-4-5` works too, but only without\n * `effort`.\n *\n * @default 'claude-opus-5'\n */\n model?: string\n /**\n * Abort after this many milliseconds. Covers downloading the image and the\n * message call together.\n * @default 30000\n */\n timeoutMs?: number\n}\n\n/**\n * Image formats the Messages API accepts.\n *\n * Narrower than what an upload collection may hold — SVG and AVIF are missing,\n * so the endpoint rejects those documents and their generate button stays\n * disabled instead of failing at the provider.\n *\n * @see https://platform.claude.com/docs/en/build-with-claude/vision\n */\nconst SUPPORTED_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']\n\n/**\n * Claude's 10 MB ceiling is measured on the base64 payload, which inflates the\n * raw bytes by roughly 4/3 — so the guard has to sit at ~7.5 MB of raw image to\n * mean 10 MB on the wire. Checking the raw length against 10 MB would wave\n * through a 9 MB photo that arrives as ~12 MB and is rejected by the provider,\n * costing the download and replacing a readable message with a raw 400.\n */\nconst MAX_IMAGE_BYTES = Math.floor((10 * 1024 * 1024 * 3) / 4)\n\n/**\n * Room for one alt text and its keywords per locale, plus the thinking tokens\n * Claude spends before answering. A budget sized for the answer alone would be\n * exhausted while reasoning, and the response would be cut off mid-JSON.\n */\nconst MAX_TOKENS_PER_LOCALE = 2000\n\ntype AnthropicMessage = {\n content?: { text?: string; type: string }[]\n stop_reason?: string\n}\n\n/**\n * Creates a Claude-based resolver for alt text generation.\n *\n * The image is downloaded and sent as bytes. Claude can fetch an image URL\n * itself, but that path is not dependable for a CMS: it requires the file to be\n * reachable from the public internet — never true in local development, and not\n * true for private buckets. Sending the bytes removes that whole class of\n * failure for the price of one extra download, and supplies the `media_type`\n * that a base64 image block requires and a URL cannot carry.\n *\n * @example\n * ```typescript\n * import { anthropicResolver } from '@jhb.software/payload-alt-text-plugin'\n *\n * anthropicResolver({\n * apiKey: process.env.ANTHROPIC_API_KEY,\n * model: 'claude-opus-5', // optional, this is the default\n * })\n * ```\n */\nexport const anthropicResolver = ({\n apiKey,\n baseUrl = 'https://api.anthropic.com',\n effort,\n instructions,\n model = 'claude-opus-5',\n timeoutMs = 30_000,\n}: AnthropicResolverConfig): AltTextResolver =>\n createVisionResolver({\n apiKey,\n generate: async ({\n filename,\n image,\n instructions: resolvedInstructions,\n maxTokens,\n responseSchema,\n signal,\n }) => {\n if (!image) {\n throw new Error('The image was not downloaded')\n }\n\n const response = await fetch(`${baseUrl}/v1/messages`, {\n body: JSON.stringify({\n max_tokens: maxTokens,\n messages: [\n {\n content: [\n // Claude works best when the image comes before the text.\n {\n type: 'image',\n source: { type: 'base64', data: image.base64, media_type: image.mediaType },\n },\n ...(filename ? [{ type: 'text', text: filename }] : []),\n ],\n role: 'user',\n },\n ],\n model,\n // `format` constrains the response to the schema the plugin needs;\n // `effort` caps how long Claude thinks before producing it. Only sent\n // when configured: some models reject the field outright.\n output_config: {\n ...(effort ? { effort } : {}),\n format: { type: 'json_schema', schema: responseSchema },\n },\n // The instructions are an operator instruction, not a turn in the\n // conversation, so they travel as the top-level system prompt.\n system: resolvedInstructions,\n }),\n headers: {\n 'anthropic-version': '2023-06-01',\n 'content-type': 'application/json',\n 'x-api-key': apiKey,\n },\n method: 'POST',\n signal,\n })\n\n if (!response.ok) {\n // Bounded: unbounded provider text would land in the log as-is.\n const body = (await response.text().catch(() => '')).slice(0, 500)\n\n throw new VisionProviderError({ body, label: 'Anthropic', status: response.status })\n }\n\n const message = (await response.json()) as AnthropicMessage\n\n // A refusal and a truncated answer both arrive as a 200 with unusable\n // content, so they are named rather than surfacing as a JSON parse error.\n if (message.stop_reason === 'refusal') {\n throw new Error('Claude declined to describe this image')\n }\n\n if (message.stop_reason === 'max_tokens') {\n throw new Error(\n `Claude ran out of tokens before finishing the alt text (max_tokens: ${maxTokens})`,\n )\n }\n\n const text = message.content?.find((block) => block.type === 'text')?.text\n\n if (typeof text !== 'string') {\n throw new Error('No result from Anthropic')\n }\n\n try {\n return JSON.parse(text)\n } catch {\n throw new Error('Claude returned a response that was not valid JSON')\n }\n },\n inlineImage: true,\n instructions,\n key: 'anthropic',\n label: 'Anthropic',\n maxImageBytes: MAX_IMAGE_BYTES,\n maxTokensPerLocale: MAX_TOKENS_PER_LOCALE,\n supportedMimeTypes: SUPPORTED_MIME_TYPES,\n timeoutMs,\n })\n"],"names":["createVisionResolver","VisionProviderError","SUPPORTED_MIME_TYPES","MAX_IMAGE_BYTES","Math","floor","MAX_TOKENS_PER_LOCALE","anthropicResolver","apiKey","baseUrl","effort","instructions","model","timeoutMs","generate","filename","image","resolvedInstructions","maxTokens","responseSchema","signal","Error","response","fetch","body","JSON","stringify","max_tokens","messages","content","type","source","data","base64","media_type","mediaType","text","role","output_config","format","schema","system","headers","method","ok","catch","slice","label","status","message","json","stop_reason","find","block","parse","inlineImage","key","maxImageBytes","maxTokensPerLocale","supportedMimeTypes"],"mappings":"AAGA,SAASA,oBAAoB,EAAEC,mBAAmB,QAAQ,4BAA2B;AA6CrF;;;;;;;;CAQC,GACD,MAAMC,uBAAuB;IAAC;IAAc;IAAa;IAAa;CAAa;AAEnF;;;;;;CAMC,GACD,MAAMC,kBAAkBC,KAAKC,KAAK,CAAC,AAAC,KAAK,OAAO,OAAO,IAAK;AAE5D;;;;CAIC,GACD,MAAMC,wBAAwB;AAO9B;;;;;;;;;;;;;;;;;;;CAmBC,GACD,OAAO,MAAMC,oBAAoB,CAAC,EAChCC,MAAM,EACNC,UAAU,2BAA2B,EACrCC,MAAM,EACNC,YAAY,EACZC,QAAQ,eAAe,EACvBC,YAAY,MAAM,EACM,GACxBb,qBAAqB;QACnBQ;QACAM,UAAU,OAAO,EACfC,QAAQ,EACRC,KAAK,EACLL,cAAcM,oBAAoB,EAClCC,SAAS,EACTC,cAAc,EACdC,MAAM,EACP;YACC,IAAI,CAACJ,OAAO;gBACV,MAAM,IAAIK,MAAM;YAClB;YAEA,MAAMC,WAAW,MAAMC,MAAM,GAAGd,QAAQ,YAAY,CAAC,EAAE;gBACrDe,MAAMC,KAAKC,SAAS,CAAC;oBACnBC,YAAYT;oBACZU,UAAU;wBACR;4BACEC,SAAS;gCACP,0DAA0D;gCAC1D;oCACEC,MAAM;oCACNC,QAAQ;wCAAED,MAAM;wCAAUE,MAAMhB,MAAMiB,MAAM;wCAAEC,YAAYlB,MAAMmB,SAAS;oCAAC;gCAC5E;mCACIpB,WAA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ACb5C;QACA6C,KAAK;QACLT,OAAO;QACPU,eAAetD;QACfuD,oBAAoBpD;QACpBqD,oBAAoBzD;QACpBW;IACF,GAAE"}
@@ -0,0 +1,148 @@
1
+ import type { PayloadRequest } from 'payload';
2
+ import { z } from 'zod';
3
+ import type { AltTextResolver } from './types.js';
4
+ export type VisionInstructionsArgs = {
5
+ /** The instructions the resolver would send on its own, stating the rules the plugin depends on */
6
+ defaultInstructions: string;
7
+ /** The uploaded file's name, when the endpoint could supply one */
8
+ filename?: string;
9
+ /** The locales the response must cover, as configured in Payload */
10
+ locales: string[];
11
+ };
12
+ export type VisionInstructions = (args: VisionInstructionsArgs) => Promise<string> | string;
13
+ /** The thumbnail's bytes, handed to providers that declared `inlineImage`. */
14
+ export type VisionImage = {
15
+ /** Base64-encoded bytes, without a data URI prefix */
16
+ base64: string;
17
+ /** `data:<mediaType>;base64,<base64>`, for providers that take a data URI */
18
+ dataUri: string;
19
+ /** The format actually served at the thumbnail URL, not the document's stored one */
20
+ mediaType: string;
21
+ };
22
+ export type VisionGenerateArgs = {
23
+ /** The uploaded file's name, when the endpoint could supply one */
24
+ filename?: string;
25
+ /** The downloaded thumbnail — present only when the resolver declared `inlineImage` */
26
+ image?: VisionImage;
27
+ /**
28
+ * The format the collection declares `getImageThumbnail` delivers, or
29
+ * undefined when nothing was declared. Resolvers that inline the bytes should
30
+ * use `image.mediaType`, which is what the URL actually served, with this
31
+ * declaration already standing in when the host named no usable type.
32
+ */
33
+ imageThumbnailMimeType?: string;
34
+ /** URL of the image thumbnail, for providers that fetch it themselves */
35
+ imageThumbnailUrl: string;
36
+ /** The instructions to send, e.g. as the system prompt */
37
+ instructions: string;
38
+ /** The locales the response must cover */
39
+ locales: string[];
40
+ /** Token budget for the response, scaled by the number of requested locales */
41
+ maxTokens: number;
42
+ req: PayloadRequest;
43
+ /** Draft-7 JSON Schema of the object the provider must return */
44
+ responseSchema: Record<string, unknown>;
45
+ /**
46
+ * Aborts once `timeoutMs` has elapsed, already covering the image download.
47
+ * Undefined when the resolver declares no `timeoutMs`, leaving the deadline to
48
+ * the provider client.
49
+ */
50
+ signal?: AbortSignal;
51
+ };
52
+ /**
53
+ * A non-ok HTTP response from a provider.
54
+ *
55
+ * Carries the status so the factory can tell a rate limit or an outage — worth
56
+ * another attempt — from a malformed request, which would fail identically every
57
+ * time.
58
+ *
59
+ * The response body is kept off `message` deliberately. That message is shown in
60
+ * the admin panel to anyone allowed to generate an alt text, while the body is
61
+ * text the provider chose: OpenAI echoes a masked form of the rejected API key
62
+ * into a 401, and providers routinely name organization ids, project ids and
63
+ * internal endpoints. It goes to the server log, where the person debugging the
64
+ * configuration is, and not to an editor's screen.
65
+ */
66
+ export declare class VisionProviderError extends Error {
67
+ /** The provider's response body, for the log only — never for `message`. */
68
+ readonly body?: string;
69
+ readonly status: number;
70
+ constructor({ body, label, status }: {
71
+ body?: string;
72
+ label: string;
73
+ status: number;
74
+ });
75
+ /** Rate limits and server-side failures are transient; a 4xx is not. */
76
+ get isTransient(): boolean;
77
+ }
78
+ export type VisionResolverConfig = {
79
+ /**
80
+ * Checked before any work happens, so a plugin wired as
81
+ * `enabled: !!process.env.X_API_KEY` fails with a readable message instead of
82
+ * a provider error — or, worse, a paid-for image download.
83
+ */
84
+ apiKey: string;
85
+ /**
86
+ * Sends one request to the provider and resolves with its parsed JSON
87
+ * response. Rejecting fails the generation, so provider errors need no
88
+ * special handling beyond throwing a readable message.
89
+ */
90
+ generate: (args: VisionGenerateArgs) => Promise<unknown>;
91
+ /**
92
+ * Download the thumbnail and hand `generate` the bytes rather than the URL.
93
+ *
94
+ * Needed by every provider whose own fetcher requires a publicly reachable
95
+ * file — never true in local development, not true for private buckets.
96
+ */
97
+ inlineImage?: boolean;
98
+ /**
99
+ * Builds the instructions from the default ones, e.g. to append a house style
100
+ * rule. Called once per generation. The image and the required response shape
101
+ * are not part of the instructions and cannot be altered here.
102
+ *
103
+ * @default ({ defaultInstructions }) => defaultInstructions
104
+ */
105
+ instructions?: VisionInstructions;
106
+ /** Identifies the resolver, e.g. in log entries */
107
+ key: string;
108
+ /** Provider name used in error messages shown in the admin UI */
109
+ label: string;
110
+ /**
111
+ * Rejects an inlined image above this size before it is sent.
112
+ * @default 20971520 (20 MB)
113
+ */
114
+ maxImageBytes?: number;
115
+ /**
116
+ * Token budget granted per requested locale. A ceiling, not a reservation, so
117
+ * headroom is free; the default keeps the pre-factory bulk budget of 300 for
118
+ * every locale count rather than only for two or more.
119
+ * @default 300
120
+ */
121
+ maxTokensPerLocale?: number;
122
+ /** @see AltTextResolver.supportedMimeTypes */
123
+ supportedMimeTypes?: string[];
124
+ /**
125
+ * Abort after this many milliseconds, covering the image download and the
126
+ * provider call together. Omit it to impose no deadline of the factory's own —
127
+ * appropriate when the provider's own client already has one.
128
+ */
129
+ timeoutMs?: number;
130
+ };
131
+ /** One schema entry per requested locale, so the model must answer for all of them. */
132
+ export declare const schemaForLocales: (locales: string[]) => z.ZodObject<{
133
+ [x: string]: z.ZodObject<{
134
+ altText: z.ZodString;
135
+ keywords: z.ZodArray<z.ZodString>;
136
+ }, z.core.$strip>;
137
+ }, z.core.$strip>;
138
+ /**
139
+ * Creates a resolver for a vision (LLM) provider, leaving only the provider call
140
+ * to `generate`: the prompt, the required response schema, the optional image
141
+ * download and the strict reading of the response are handled here.
142
+ *
143
+ * All locales go into a single call rather than one call each: the image is
144
+ * uploaded and analyzed once — the expensive part — and every language ends up
145
+ * describing the same reading of it. `resolve` is that same call with one
146
+ * locale.
147
+ */
148
+ export declare const createVisionResolver: ({ apiKey, generate, inlineImage, instructions, key, label, maxImageBytes, maxTokensPerLocale, supportedMimeTypes, timeoutMs, }: VisionResolverConfig) => AltTextResolver;