@jhb.software/payload-alt-text-plugin 0.10.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/README.md +225 -36
  2. package/dist/components/BulkGenerateAltTextsButton.js +3 -16
  3. package/dist/components/BulkGenerateAltTextsButton.js.map +1 -1
  4. package/dist/components/summarizeBulkGenerate.d.ts +26 -0
  5. package/dist/components/summarizeBulkGenerate.js +56 -0
  6. package/dist/components/summarizeBulkGenerate.js.map +1 -0
  7. package/dist/endpoints/bulkGenerateAltTexts.d.ts +18 -1
  8. package/dist/endpoints/bulkGenerateAltTexts.js +39 -16
  9. package/dist/endpoints/bulkGenerateAltTexts.js.map +1 -1
  10. package/dist/endpoints/generateAltText.js +17 -9
  11. package/dist/endpoints/generateAltText.js.map +1 -1
  12. package/dist/index.d.ts +6 -1
  13. package/dist/index.js +2 -0
  14. package/dist/index.js.map +1 -1
  15. package/dist/plugin.js +20 -4
  16. package/dist/plugin.js.map +1 -1
  17. package/dist/resolvers/anthropic.d.ts +64 -0
  18. package/dist/resolvers/anthropic.js +140 -0
  19. package/dist/resolvers/anthropic.js.map +1 -0
  20. package/dist/resolvers/createVisionResolver.d.ts +148 -0
  21. package/dist/resolvers/createVisionResolver.js +300 -0
  22. package/dist/resolvers/createVisionResolver.js.map +1 -0
  23. package/dist/resolvers/mistral.d.ts +15 -1
  24. package/dist/resolvers/mistral.js +85 -250
  25. package/dist/resolvers/mistral.js.map +1 -1
  26. package/dist/resolvers/openAI.d.ts +22 -3
  27. package/dist/resolvers/openAI.js +57 -138
  28. package/dist/resolvers/openAI.js.map +1 -1
  29. package/dist/translations/de.js +12 -4
  30. package/dist/translations/de.js.map +1 -1
  31. package/dist/translations/en.js +12 -4
  32. package/dist/translations/en.js.map +1 -1
  33. package/dist/translations/translation-schema.json +24 -8
  34. package/dist/types/AltTextPluginConfig.d.ts +71 -12
  35. package/dist/types/AltTextPluginConfig.js.map +1 -1
  36. package/dist/utilities/altTextHealth.d.ts +3 -1
  37. package/dist/utilities/altTextHealth.js +103 -12
  38. package/dist/utilities/altTextHealth.js.map +1 -1
  39. package/dist/utilities/resolveLocales.d.ts +15 -0
  40. package/dist/utilities/resolveLocales.js +38 -0
  41. package/dist/utilities/resolveLocales.js.map +1 -0
  42. package/dist/utilities/stableStringify.d.ts +9 -0
  43. package/dist/utilities/stableStringify.js +19 -0
  44. package/dist/utilities/stableStringify.js.map +1 -0
  45. package/package.json +4 -5
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/resolvers/createVisionResolver.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\n\nimport { z } from 'zod'\n\nimport type {\n AltTextBulkResolverArgs,\n AltTextBulkResolverResponse,\n AltTextResolver,\n AltTextResolverArgs,\n AltTextResolverResponse,\n AltTextResult,\n} from './types.js'\n\nexport type VisionInstructionsArgs = {\n /** The instructions the resolver would send on its own, stating the rules the plugin depends on */\n defaultInstructions: string\n /** The uploaded file's name, when the endpoint could supply one */\n filename?: string\n /** The locales the response must cover, as configured in Payload */\n locales: string[]\n}\n\nexport type VisionInstructions = (args: VisionInstructionsArgs) => Promise<string> | string\n\n/** The thumbnail's bytes, handed to providers that declared `inlineImage`. */\nexport type VisionImage = {\n /** Base64-encoded bytes, without a data URI prefix */\n base64: string\n /** `data:<mediaType>;base64,<base64>`, for providers that take a data URI */\n dataUri: string\n /** The format actually served at the thumbnail URL, not the document's stored one */\n mediaType: string\n}\n\nexport type VisionGenerateArgs = {\n /** The uploaded file's name, when the endpoint could supply one */\n filename?: string\n /** The downloaded thumbnail — present only when the resolver declared `inlineImage` */\n image?: VisionImage\n /**\n * The format the collection declares `getImageThumbnail` delivers, or\n * undefined when nothing was declared. Resolvers that inline the bytes should\n * use `image.mediaType`, which is what the URL actually served, with this\n * declaration already standing in when the host named no usable type.\n */\n imageThumbnailMimeType?: string\n /** URL of the image thumbnail, for providers that fetch it themselves */\n imageThumbnailUrl: string\n /** The instructions to send, e.g. as the system prompt */\n instructions: string\n /** The locales the response must cover */\n locales: string[]\n /** Token budget for the response, scaled by the number of requested locales */\n maxTokens: number\n req: PayloadRequest\n /** Draft-7 JSON Schema of the object the provider must return */\n responseSchema: Record<string, unknown>\n /**\n * Aborts once `timeoutMs` has elapsed, already covering the image download.\n * Undefined when the resolver declares no `timeoutMs`, leaving the deadline to\n * the provider client.\n */\n signal?: AbortSignal\n}\n\n/**\n * A non-ok HTTP response from a provider.\n *\n * Carries the status so the factory can tell a rate limit or an outage — worth\n * another attempt — from a malformed request, which would fail identically every\n * time.\n *\n * The response body is kept off `message` deliberately. That message is shown in\n * the admin panel to anyone allowed to generate an alt text, while the body is\n * text the provider chose: OpenAI echoes a masked form of the rejected API key\n * into a 401, and providers routinely name organization ids, project ids and\n * internal endpoints. It goes to the server log, where the person debugging the\n * configuration is, and not to an editor's screen.\n */\nexport class VisionProviderError extends Error {\n /** The provider's response body, for the log only — never for `message`. */\n readonly body?: string\n readonly status: number\n\n constructor({ body, label, status }: { body?: string; label: string; status: number }) {\n super(`${label} responded with status ${status}`)\n this.body = body\n this.name = 'VisionProviderError'\n this.status = status\n }\n\n /** Rate limits and server-side failures are transient; a 4xx is not. */\n get isTransient(): boolean {\n return this.status === 429 || this.status >= 500\n }\n}\n\n/** Attempts after the first, for a provider error that may pass on a retry. */\nconst MAX_RETRIES = 2\n\n/** Backs off between attempts, bounded by the resolver's own deadline. */\nconst retryDelayMs = (attempt: number) => 250 * 2 ** (attempt - 1)\n\nexport type VisionResolverConfig = {\n /**\n * Checked before any work happens, so a plugin wired as\n * `enabled: !!process.env.X_API_KEY` fails with a readable message instead of\n * a provider error — or, worse, a paid-for image download.\n */\n apiKey: string\n /**\n * Sends one request to the provider and resolves with its parsed JSON\n * response. Rejecting fails the generation, so provider errors need no\n * special handling beyond throwing a readable message.\n */\n generate: (args: VisionGenerateArgs) => Promise<unknown>\n /**\n * Download the thumbnail and hand `generate` the bytes rather than the URL.\n *\n * Needed by every provider whose own fetcher requires a publicly reachable\n * file.\n */\n inlineImage?: boolean\n /**\n * Builds the instructions from the default ones, e.g. to append a house style\n * rule. Called once per generation. The image and the required response shape\n * are not part of the instructions and cannot be altered here.\n *\n * @default ({ defaultInstructions }) => defaultInstructions\n */\n instructions?: VisionInstructions\n /** Identifies the resolver, e.g. in log entries */\n key: string\n /** Provider name used in error messages shown in the admin UI */\n label: string\n /**\n * Rejects an inlined image above this size before it is sent.\n * @default 20971520 (20 MB)\n */\n maxImageBytes?: number\n /**\n * Token budget granted per requested locale. A ceiling, not a reservation, so\n * headroom is free; the default keeps the pre-factory bulk budget of 300 for\n * every locale count rather than only for two or more.\n * @default 300\n */\n maxTokensPerLocale?: number\n /** @see AltTextResolver.supportedMimeTypes */\n supportedMimeTypes?: string[]\n /**\n * Abort after this many milliseconds, covering the image download and the\n * provider call together. Omit it to impose no deadline of the factory's own —\n * appropriate when the provider's own client already has one.\n */\n timeoutMs?: number\n}\n\nconst altTextSchema = z.object({\n altText: z.string().describe('A concise, descriptive alt text for the image'),\n keywords: z.array(z.string()).describe('Keywords that describe the content of the image'),\n})\n\n/** One schema entry per requested locale, so the model must answer for all of them. */\nexport const schemaForLocales = (locales: string[]) =>\n z.object(Object.fromEntries(locales.map((locale) => [locale, altTextSchema])))\n\n/**\n * Rules dictated by the plugin rather than by the provider: one entry per\n * configured locale, describing what is visible rather than guessing at it.\n */\nconst buildDefaultInstructions = ({ locales }: { locales: string[] }): string =>\n [\n `You are an expert at analyzing images and creating descriptive image alt text.`,\n\n `Please analyze the given image and provide the following in ${locales.join(', ')}:`,\n\n `- A concise, localized descriptive alt text (1-2 sentences) as \"altText\". Focus on the subject, action, and setting. Avoid phrases like 'Image of', 'A picture of', or 'Photo showing'. Be specific and include relevant details like location or context if visible. Make no assumptions.`,\n\n `- A localized list of keywords that describe the content (e.g., [\"Camel\", \"Palm trees\", \"Desert\"]) as \"keywords\"`,\n\n `If a context is provided, use it to enhance the alt text.`,\n\n `Format your response as a JSON object with ${locales.map((locale) => `\"${locale}\"`).join(', ')} keys, each containing \"altText\" and \"keywords\".`,\n ].join('\\n\\n')\n\n/**\n * Downloads the image and returns its bytes.\n *\n * The document's mime type is checked by the endpoint before the resolver runs,\n * but `getImageThumbnail` may point at a derivative in a different format, so\n * what was actually served is what counts. Only when the host names no usable\n * type at all — no header, or a generic `application/octet-stream` as private\n * buckets and signed URLs often send — does the collection's declared\n * `imageThumbnailMimeType` stand in for it.\n */\nasync function fetchImage({\n declaredMediaType,\n label,\n maxImageBytes,\n signal,\n supportedMimeTypes,\n url,\n}: {\n declaredMediaType?: string\n label: string\n maxImageBytes: number\n signal?: AbortSignal\n supportedMimeTypes?: string[]\n url: string\n}): Promise<{ error: string } | { image: VisionImage }> {\n let response: Response\n\n try {\n response = await fetch(url, { signal })\n } catch (error) {\n return {\n error: `Could not download the image from ${url}: ${error instanceof Error ? error.message : 'unknown error'}`,\n }\n }\n\n if (!response.ok) {\n return { error: `Could not download the image from ${url}: status ${response.status}` }\n }\n\n const served = response.headers.get('content-type')?.split(';')[0]?.trim().toLowerCase()\n const mediaType =\n served && served !== 'application/octet-stream' ? served : declaredMediaType?.toLowerCase()\n\n if (!mediaType) {\n return {\n error: `The image at ${url} was served as ${served ? `\"${served}\"` : 'no content type at all'}, which does not name an image format. Declare imageThumbnailMimeType for the collection so ${label} knows what it is reading.`,\n }\n }\n\n if (supportedMimeTypes && !supportedMimeTypes.includes(mediaType)) {\n return {\n error: `The image at ${url} was served as \"${mediaType}\", which ${label} cannot read. Supported types: ${supportedMimeTypes.join(', ')}.`,\n }\n }\n\n const tooLarge = (byteLength: number) =>\n `The image at ${url} is ${Math.round(byteLength / 1024 / 1024)} MB, above ${label}'s ${Math.round(maxImageBytes / 1024 / 1024)} MB limit. Point getImageThumbnail at a smaller image size.`\n\n // Measuring by reading is work the header already answers, and the file is on\n // its way to being rejected: `getImageThumbnail` may point at the original\n // upload, which can be far above the provider's limit.\n const declaredLength = Number(response.headers.get('content-length'))\n\n if (Number.isInteger(declaredLength) && declaredLength > maxImageBytes) {\n return { error: tooLarge(declaredLength) }\n }\n\n const bytes = Buffer.from(await response.arrayBuffer())\n\n if (bytes.byteLength === 0) {\n return { error: `The image at ${url} was empty.` }\n }\n\n if (bytes.byteLength > maxImageBytes) {\n return { error: tooLarge(bytes.byteLength) }\n }\n\n const base64 = bytes.toString('base64')\n\n return { image: { base64, dataUri: `data:${mediaType};base64,${base64}`, mediaType } }\n}\n\n/**\n * Runs the provider call, retrying a transient failure.\n *\n * Every bundled resolver reaches its provider over `fetch`, which retries\n * nothing by itself. Without this, a bulk generation that trips a rate limit\n * gives up on the first 429 and leaves those images without an alt text. The\n * resolver's `timeoutMs` covers the attempts together, so a deadline still\n * bounds the whole call.\n */\nasync function generateWithRetry({\n args,\n generate,\n}: {\n args: VisionGenerateArgs\n generate: (args: VisionGenerateArgs) => Promise<unknown>\n}): Promise<unknown> {\n for (let attempt = 0; ; attempt++) {\n try {\n return await generate(args)\n } catch (error) {\n const isRetryable = error instanceof VisionProviderError && error.isTransient\n\n if (!isRetryable || attempt >= MAX_RETRIES || args.signal?.aborted) {\n throw error\n }\n\n await new Promise((resolve) => setTimeout(resolve, retryDelayMs(attempt + 1)))\n }\n }\n}\n\n/**\n * Reads the model's response.\n *\n * Deliberately strict about a blank `altText`: the field is required on the\n * collection, so an empty string would satisfy that requirement while telling a\n * screen reader nothing — and nobody looks at an alt text again once it is set.\n */\nfunction parseResults(content: unknown, locales: string[]): null | Record<string, AltTextResult> {\n const parsed = schemaForLocales(locales).safeParse(content)\n\n if (!parsed.success) {\n return null\n }\n\n const results: Record<string, AltTextResult> = {}\n\n for (const locale of locales) {\n const entry = parsed.data[locale]\n\n if (entry.altText.trim().length === 0) {\n return null\n }\n\n results[locale] = { altText: entry.altText.trim(), keywords: entry.keywords }\n }\n\n return results\n}\n\n/**\n * Creates a resolver for a vision (LLM) provider, leaving only the provider call\n * to `generate`: the prompt, the required response schema, the optional image\n * download and the strict reading of the response are handled here.\n *\n * All locales go into a single call rather than one call each: the image is\n * uploaded and analyzed once — the expensive part — and every language ends up\n * describing the same reading of it. `resolve` is that same call with one\n * locale.\n */\nexport const createVisionResolver = ({\n apiKey,\n generate,\n inlineImage = false,\n instructions = ({ defaultInstructions }) => defaultInstructions,\n key,\n label,\n maxImageBytes = 20 * 1024 * 1024,\n maxTokensPerLocale = 300,\n supportedMimeTypes,\n timeoutMs,\n}: VisionResolverConfig): AltTextResolver => {\n const run = async ({\n filename,\n imageThumbnailMimeType,\n imageThumbnailUrl,\n locales,\n req,\n }: {\n filename?: string\n imageThumbnailMimeType?: string\n imageThumbnailUrl: string\n locales: string[]\n req: PayloadRequest\n }): Promise<\n { error: string; success: false } | { results: Record<string, AltTextResult>; success: true }\n > => {\n if (!apiKey) {\n return { error: `No ${label} API key configured`, success: false }\n }\n\n if (locales.length === 0) {\n return { error: 'No locale requested', success: false }\n }\n\n const signal = timeoutMs === undefined ? undefined : AbortSignal.timeout(timeoutMs)\n\n let image: undefined | VisionImage\n\n if (inlineImage) {\n const downloaded = await fetchImage({\n declaredMediaType: imageThumbnailMimeType,\n label,\n maxImageBytes,\n signal,\n supportedMimeTypes,\n url: imageThumbnailUrl,\n })\n\n if ('error' in downloaded) {\n return { error: downloaded.error, success: false }\n }\n\n image = downloaded.image\n }\n\n try {\n const defaultInstructions = buildDefaultInstructions({ locales })\n\n const content = await generateWithRetry({\n args: {\n filename,\n image,\n imageThumbnailMimeType,\n imageThumbnailUrl,\n instructions: await instructions({ defaultInstructions, filename, locales }),\n locales,\n maxTokens: maxTokensPerLocale * locales.length,\n req,\n responseSchema: z.toJSONSchema(schemaForLocales(locales), { target: 'draft-7' }),\n signal,\n },\n generate,\n })\n\n const results = parseResults(content, locales)\n\n if (!results) {\n return {\n error: `${label} did not return a usable alt text for every requested locale (${locales.join(', ')})`,\n success: false,\n }\n }\n\n return { results, success: true }\n } catch (error) {\n req.payload.logger.error({\n err: error,\n msg: 'Error generating alt text',\n // Logged separately: it is deliberately absent from the error message\n // the admin panel shows, and is what a misconfiguration is diagnosed from.\n providerResponse: error instanceof VisionProviderError ? error.body : undefined,\n resolver: key,\n })\n\n return { error: error instanceof Error ? error.message : 'Unknown error', success: false }\n }\n }\n\n return {\n key,\n resolve: async ({\n filename,\n imageThumbnailMimeType,\n imageThumbnailUrl,\n locale,\n req,\n }: AltTextResolverArgs): Promise<AltTextResolverResponse> => {\n const result = await run({\n filename,\n imageThumbnailMimeType,\n imageThumbnailUrl,\n locales: [locale],\n req,\n })\n\n if (!result.success) {\n return { error: result.error, success: false }\n }\n\n return { result: result.results[locale], success: true }\n },\n resolveBulk: async ({\n filename,\n imageThumbnailMimeType,\n imageThumbnailUrl,\n locales,\n req,\n }: AltTextBulkResolverArgs): Promise<AltTextBulkResolverResponse> => {\n const result = await run({\n filename,\n imageThumbnailMimeType,\n imageThumbnailUrl,\n locales,\n req,\n })\n\n if (!result.success) {\n return { error: result.error, success: false }\n }\n\n return { results: result.results, success: true }\n },\n supportedMimeTypes,\n }\n}\n"],"names":["z","VisionProviderError","Error","body","status","label","name","isTransient","MAX_RETRIES","retryDelayMs","attempt","altTextSchema","object","altText","string","describe","keywords","array","schemaForLocales","locales","Object","fromEntries","map","locale","buildDefaultInstructions","join","fetchImage","declaredMediaType","maxImageBytes","signal","supportedMimeTypes","url","response","fetch","error","message","ok","served","headers","get","split","trim","toLowerCase","mediaType","includes","tooLarge","byteLength","Math","round","declaredLength","Number","isInteger","bytes","Buffer","from","arrayBuffer","base64","toString","image","dataUri","generateWithRetry","args","generate","isRetryable","aborted","Promise","resolve","setTimeout","parseResults","content","parsed","safeParse","success","results","entry","data","length","createVisionResolver","apiKey","inlineImage","instructions","defaultInstructions","key","maxTokensPerLocale","timeoutMs","run","filename","imageThumbnailMimeType","imageThumbnailUrl","req","undefined","AbortSignal","timeout","downloaded","maxTokens","responseSchema","toJSONSchema","target","payload","logger","err","msg","providerResponse","resolver","result","resolveBulk"],"mappings":"AAEA,SAASA,CAAC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C;gBACArE,SAAS;oBAACI;iBAAO;gBACjBkE;YACF;YAEA,IAAI,CAACe,OAAOhC,OAAO,EAAE;gBACnB,OAAO;oBAAEtC,OAAOsE,OAAOtE,KAAK;oBAAEsC,SAAS;gBAAM;YAC/C;YAEA,OAAO;gBAAEgC,QAAQA,OAAO/B,OAAO,CAAClD,OAAO;gBAAEiD,SAAS;YAAK;QACzD;QACAiC,aAAa,OAAO,EAClBnB,QAAQ,EACRC,sBAAsB,EACtBC,iBAAiB,EACjBrE,OAAO,EACPsE,GAAG,EACqB;YACxB,MAAMe,SAAS,MAAMnB,IAAI;gBACvBC;gBACAC;gBACAC;gBACArE;gBACAsE;YACF;YAEA,IAAI,CAACe,OAAOhC,OAAO,EAAE;gBACnB,OAAO;oBAAEtC,OAAOsE,OAAOtE,KAAK;oBAAEsC,SAAS;gBAAM;YAC/C;YAEA,OAAO;gBAAEC,SAAS+B,OAAO/B,OAAO;gBAAED,SAAS;YAAK;QAClD;QACA1C;IACF;AACF,EAAC"}
@@ -1,3 +1,4 @@
1
+ import type { VisionInstructions } from './createVisionResolver.js';
1
2
  import type { AltTextResolver } from './types.js';
2
3
  export type MistralResolverConfig = {
3
4
  /** Mistral API key for authentication */
@@ -7,6 +8,13 @@ export type MistralResolverConfig = {
7
8
  * @default 'https://api.mistral.ai/v1'
8
9
  */
9
10
  baseUrl?: string;
11
+ /**
12
+ * Builds the instructions from the default ones, e.g. to append a house style
13
+ * rule. Sent as the system message, separately from the image.
14
+ *
15
+ * @default ({ defaultInstructions }) => defaultInstructions
16
+ */
17
+ instructions?: VisionInstructions;
10
18
  /**
11
19
  * The vision-capable Mistral model to use for alt text generation.
12
20
  *
@@ -27,6 +35,12 @@ export type MistralResolverConfig = {
27
35
  /**
28
36
  * Creates a Mistral-based resolver for alt text generation.
29
37
  *
38
+ * The image is downloaded and sent as bytes rather than handed to Mistral as a
39
+ * URL. Mistral's own fetcher requires a publicly reachable file, and even that is
40
+ * not sufficient: hosts serving the image fine to everyone else were observed
41
+ * refusing it with `File could not be fetched from url` (error 3310). See #184
42
+ * for the reproduction.
43
+ *
30
44
  * @example
31
45
  * ```typescript
32
46
  * import { mistralResolver } from '@jhb.software/payload-alt-text-plugin'
@@ -37,4 +51,4 @@ export type MistralResolverConfig = {
37
51
  * })
38
52
  * ```
39
53
  */
40
- export declare const mistralResolver: (config: MistralResolverConfig) => AltTextResolver;
54
+ export declare const mistralResolver: ({ apiKey, baseUrl, instructions, model, timeoutMs, }: MistralResolverConfig) => AltTextResolver;
@@ -1,220 +1,27 @@
1
- import { z } from 'zod';
1
+ import { createVisionResolver, VisionProviderError } from './createVisionResolver.js';
2
2
  /**
3
3
  * Image formats the Mistral API accepts.
4
4
  *
5
5
  * Narrower than what an upload collection may hold — SVG and AVIF are missing,
6
6
  * so the endpoint rejects those documents and their generate button stays
7
7
  * disabled instead of failing at the provider.
8
+ *
9
+ * @see https://docs.mistral.ai/capabilities/vision/
8
10
  */ const SUPPORTED_MIME_TYPES = [
9
11
  'image/jpeg',
10
12
  'image/png',
11
13
  'image/gif',
12
14
  'image/webp'
13
15
  ];
14
- /** Mistral rejects images above 20 MB. */ const MAX_IMAGE_BYTES = 20 * 1024 * 1024;
15
- const altTextSchema = z.object({
16
- altText: z.string().describe('A concise, descriptive alt text for the image'),
17
- keywords: z.array(z.string()).describe('Keywords that describe the content of the image')
18
- });
19
- /** One schema entry per requested locale, so the model must answer for all of them. */ const schemaForLocales = (locales)=>z.object(Object.fromEntries(locales.map((locale)=>[
20
- locale,
21
- altTextSchema
22
- ])));
23
- /**
24
- * Downloads the image and returns it as a data URI.
25
- *
26
- * Mistral can fetch an image URL itself, but that path is not dependable for a
27
- * CMS. It requires the file to be reachable from the public internet — never
28
- * true in local development, and not true for private buckets — and some hosts
29
- * refuse Mistral's fetcher outright, which surfaces as `File could not be
30
- * fetched from url` (error 3310). Sending the bytes removes that whole class of
31
- * failure for the price of one extra download.
32
- */ async function fetchImageAsDataUri(url, signal) {
33
- let response;
34
- try {
35
- response = await fetch(url, {
36
- signal
37
- });
38
- } catch (error) {
39
- return {
40
- error: `Could not download the image from ${url}: ${error instanceof Error ? error.message : 'unknown error'}`
41
- };
42
- }
43
- if (!response.ok) {
44
- return {
45
- error: `Could not download the image from ${url}: status ${response.status}`
46
- };
47
- }
48
- // The document's mime type is checked by the endpoint before the resolver
49
- // runs, but `getImageThumbnail` may point at a derivative in a different
50
- // format, so trust what was actually served.
51
- const contentType = response.headers.get('content-type')?.split(';')[0]?.trim().toLowerCase();
52
- if (!contentType || !SUPPORTED_MIME_TYPES.includes(contentType)) {
53
- return {
54
- error: `The image at ${url} was served as "${contentType ?? 'an unknown type'}", which Mistral cannot read. Supported types: ${SUPPORTED_MIME_TYPES.join(', ')}.`
55
- };
56
- }
57
- const bytes = Buffer.from(await response.arrayBuffer());
58
- if (bytes.byteLength === 0) {
59
- return {
60
- error: `The image at ${url} was empty.`
61
- };
62
- }
63
- if (bytes.byteLength > MAX_IMAGE_BYTES) {
64
- return {
65
- error: `The image at ${url} is ${Math.round(bytes.byteLength / 1024 / 1024)} MB, above Mistral's 20 MB limit. Point getImageThumbnail at a smaller image size.`
66
- };
67
- }
68
- return {
69
- dataUri: `data:${contentType};base64,${bytes.toString('base64')}`
70
- };
71
- }
72
- function buildPrompt(locales) {
73
- const languages = locales.join(', ');
74
- return `
75
- You are an expert at analyzing images and creating descriptive image alt text.
76
-
77
- Please analyze the given image and provide the following in ${languages}:
78
- - A concise, descriptive alt text (1-2 sentences) as "altText". Focus on the subject, action, and setting. Avoid phrases like 'Image of', 'A picture of', or 'Photo showing'. Be specific and include relevant details like location or context if visible. Make no assumptions.
79
- - A list of keywords that describe the content (e.g., ["Camel", "Palm trees", "Desert"]) as "keywords"
80
-
81
- If a context is provided, use it to enhance the alt text.
82
-
83
- Format your response as a JSON object with ${locales.map((locale)=>`"${locale}"`).join(', ')} keys, each containing "altText" and "keywords".
84
- `;
85
- }
86
- /**
87
- * Reads the model's response.
88
- *
89
- * Deliberately strict about a blank `altText`: the field is required on the
90
- * collection, so an empty string would satisfy that requirement while telling a
91
- * screen reader nothing — and nobody looks at an alt text again once it is set.
92
- */ function parseResults(content, locales) {
93
- const parsed = schemaForLocales(locales).safeParse(content);
94
- if (!parsed.success) {
95
- return null;
96
- }
97
- const results = {};
98
- for (const locale of locales){
99
- const entry = parsed.data[locale];
100
- if (entry.altText.trim().length === 0) {
101
- return null;
102
- }
103
- results[locale] = {
104
- altText: entry.altText.trim(),
105
- keywords: entry.keywords
106
- };
107
- }
108
- return results;
109
- }
110
- /**
111
- * One request, one or more locales.
112
- *
113
- * All locales go into a single call rather than one call each: the image is
114
- * uploaded and analyzed once — the expensive part — and every language ends up
115
- * describing the same reading of it.
116
- */ async function generate({ apiKey, baseUrl, filename, imageThumbnailUrl, locales, model, timeoutMs }) {
117
- if (!apiKey) {
118
- return {
119
- error: 'No Mistral API key configured',
120
- success: false
121
- };
122
- }
123
- if (locales.length === 0) {
124
- return {
125
- error: 'No locale requested',
126
- success: false
127
- };
128
- }
129
- const signal = AbortSignal.timeout(timeoutMs);
130
- const image = await fetchImageAsDataUri(imageThumbnailUrl, signal);
131
- if ('error' in image) {
132
- return {
133
- error: image.error,
134
- success: false
135
- };
136
- }
137
- try {
138
- const response = await fetch(`${baseUrl}/chat/completions`, {
139
- body: JSON.stringify({
140
- max_tokens: 150 * locales.length,
141
- messages: [
142
- {
143
- content: buildPrompt(locales),
144
- role: 'system'
145
- },
146
- {
147
- content: [
148
- {
149
- type: 'image_url',
150
- image_url: image.dataUri
151
- },
152
- ...filename ? [
153
- {
154
- type: 'text',
155
- text: filename
156
- }
157
- ] : []
158
- ],
159
- role: 'user'
160
- }
161
- ],
162
- model,
163
- response_format: {
164
- type: 'json_schema',
165
- json_schema: {
166
- name: 'data',
167
- schema: z.toJSONSchema(schemaForLocales(locales), {
168
- target: 'draft-7'
169
- }),
170
- strict: true
171
- }
172
- }
173
- }),
174
- headers: {
175
- Authorization: `Bearer ${apiKey}`,
176
- 'Content-Type': 'application/json'
177
- },
178
- method: 'POST',
179
- signal
180
- });
181
- if (!response.ok) {
182
- const body = await response.text().catch(()=>'');
183
- return {
184
- error: `Mistral responded with status ${response.status}${body ? `: ${body}` : ''}`,
185
- success: false
186
- };
187
- }
188
- const completion = await response.json();
189
- const content = completion.choices?.[0]?.message?.content;
190
- if (typeof content !== 'string') {
191
- return {
192
- error: 'No result from Mistral',
193
- success: false
194
- };
195
- }
196
- const results = parseResults(JSON.parse(content), locales);
197
- if (!results) {
198
- return {
199
- error: `Mistral did not return a usable alt text for every requested locale (${locales.join(', ')})`,
200
- success: false
201
- };
202
- }
203
- return {
204
- results,
205
- success: true
206
- };
207
- } catch (error) {
208
- console.error('Error generating alt text:', error);
209
- return {
210
- error: error instanceof Error ? error.message : 'Unknown error',
211
- success: false
212
- };
213
- }
214
- }
215
16
  /**
216
17
  * Creates a Mistral-based resolver for alt text generation.
217
18
  *
19
+ * The image is downloaded and sent as bytes rather than handed to Mistral as a
20
+ * URL. Mistral's own fetcher requires a publicly reachable file, and even that is
21
+ * not sufficient: hosts serving the image fine to everyone else were observed
22
+ * refusing it with `File could not be fetched from url` (error 3310). See #184
23
+ * for the reproduction.
24
+ *
218
25
  * @example
219
26
  * ```typescript
220
27
  * import { mistralResolver } from '@jhb.software/payload-alt-text-plugin'
@@ -224,57 +31,85 @@ function buildPrompt(locales) {
224
31
  * model: 'mistral-medium-latest', // optional, this is the default
225
32
  * })
226
33
  * ```
227
- */ export const mistralResolver = (config)=>{
228
- const { apiKey, baseUrl = 'https://api.mistral.ai/v1', model = 'mistral-medium-latest', timeoutMs = 30_000 } = config;
229
- return {
230
- key: 'mistral',
231
- resolve: async ({ filename, imageThumbnailUrl, locale })=>{
232
- const result = await generate({
233
- apiKey,
234
- baseUrl,
235
- filename,
236
- imageThumbnailUrl,
237
- locales: [
238
- locale
239
- ],
240
- model,
241
- timeoutMs
242
- });
243
- if (!result.success) {
244
- return {
245
- error: result.error,
246
- success: false
247
- };
34
+ */ export const mistralResolver = ({ apiKey, baseUrl = 'https://api.mistral.ai/v1', instructions, model = 'mistral-medium-latest', timeoutMs = 30_000 })=>createVisionResolver({
35
+ apiKey,
36
+ generate: async ({ filename, image, instructions: resolvedInstructions, maxTokens, responseSchema, signal })=>{
37
+ if (!image) {
38
+ throw new Error('The image was not downloaded');
248
39
  }
249
- return {
250
- result: result.results[locale],
251
- success: true
252
- };
253
- },
254
- resolveBulk: async ({ filename, imageThumbnailUrl, locales })=>{
255
- const result = await generate({
256
- apiKey,
257
- baseUrl,
258
- filename,
259
- imageThumbnailUrl,
260
- locales,
261
- model,
262
- timeoutMs
40
+ const response = await fetch(`${baseUrl}/chat/completions`, {
41
+ body: JSON.stringify({
42
+ max_tokens: maxTokens,
43
+ messages: [
44
+ {
45
+ content: resolvedInstructions,
46
+ role: 'system'
47
+ },
48
+ {
49
+ content: [
50
+ {
51
+ type: 'image_url',
52
+ image_url: image.dataUri
53
+ },
54
+ ...filename ? [
55
+ {
56
+ type: 'text',
57
+ text: filename
58
+ }
59
+ ] : []
60
+ ],
61
+ role: 'user'
62
+ }
63
+ ],
64
+ model,
65
+ response_format: {
66
+ type: 'json_schema',
67
+ json_schema: {
68
+ name: 'data',
69
+ schema: responseSchema,
70
+ strict: true
71
+ }
72
+ }
73
+ }),
74
+ headers: {
75
+ Authorization: `Bearer ${apiKey}`,
76
+ 'Content-Type': 'application/json'
77
+ },
78
+ method: 'POST',
79
+ signal
263
80
  });
264
- if (!result.success) {
265
- return {
266
- error: result.error,
267
- success: false
268
- };
81
+ if (!response.ok) {
82
+ // Bounded: unbounded provider text would land in the log as-is.
83
+ const body = (await response.text().catch(()=>'')).slice(0, 500);
84
+ throw new VisionProviderError({
85
+ body,
86
+ label: 'Mistral',
87
+ status: response.status
88
+ });
89
+ }
90
+ const completion = await response.json();
91
+ const choice = completion.choices?.[0];
92
+ if (choice?.finish_reason === 'length') {
93
+ throw new Error(`Mistral ran out of tokens before finishing the alt text (max_tokens: ${maxTokens})`);
94
+ }
95
+ const content = choice?.message?.content;
96
+ if (typeof content !== 'string') {
97
+ throw new Error('No result from Mistral');
98
+ }
99
+ try {
100
+ return JSON.parse(content);
101
+ } catch {
102
+ throw new Error('Mistral returned a response that was not valid JSON');
269
103
  }
270
- return {
271
- results: result.results,
272
- success: true
273
- };
274
104
  },
275
- // https://docs.mistral.ai/capabilities/vision/
276
- supportedMimeTypes: SUPPORTED_MIME_TYPES
277
- };
278
- };
105
+ inlineImage: true,
106
+ instructions,
107
+ key: 'mistral',
108
+ label: 'Mistral',
109
+ // Mistral rejects images above 20 MB.
110
+ maxImageBytes: 20 * 1024 * 1024,
111
+ supportedMimeTypes: SUPPORTED_MIME_TYPES,
112
+ timeoutMs
113
+ });
279
114
 
280
115
  //# sourceMappingURL=mistral.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/resolvers/mistral.ts"],"sourcesContent":["import { z } from 'zod'\n\nimport type {\n AltTextBulkResolverArgs,\n AltTextBulkResolverResponse,\n AltTextResolver,\n AltTextResolverArgs,\n AltTextResolverResponse,\n AltTextResult,\n} from './types.js'\n\nexport type MistralResolverConfig = {\n /** Mistral API key for authentication */\n apiKey: string\n /**\n * Base URL of the Mistral API.\n * @default 'https://api.mistral.ai/v1'\n */\n baseUrl?: string\n /**\n * The vision-capable Mistral model to use for alt text generation.\n *\n * Must be able to read images — `mistral-medium-latest`,\n * `mistral-large-latest`, `mistral-small-latest` and the `ministral-*` models\n * all are.\n *\n * @default 'mistral-medium-latest'\n */\n model?: string\n /**\n * Abort after this many milliseconds. Covers downloading the image and the\n * completion call together.\n * @default 30000\n */\n timeoutMs?: number\n}\n\n/**\n * Image formats the Mistral 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 */\nconst SUPPORTED_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']\n\n/** Mistral rejects images above 20 MB. */\nconst MAX_IMAGE_BYTES = 20 * 1024 * 1024\n\nconst altTextSchema = z.object({\n altText: z.string().describe('A concise, descriptive alt text for the image'),\n keywords: z.array(z.string()).describe('Keywords that describe the content of the image'),\n})\n\n/** One schema entry per requested locale, so the model must answer for all of them. */\nconst schemaForLocales = (locales: string[]) =>\n z.object(Object.fromEntries(locales.map((locale) => [locale, altTextSchema])))\n\n/**\n * Downloads the image and returns it as a data URI.\n *\n * Mistral can fetch an image URL itself, but that path is not dependable for a\n * CMS. It requires the file to be reachable from the public internet — never\n * true in local development, and not true for private buckets — and some hosts\n * refuse Mistral's fetcher outright, which surfaces as `File could not be\n * fetched from url` (error 3310). Sending the bytes removes that whole class of\n * failure for the price of one extra download.\n */\nasync function fetchImageAsDataUri(\n url: string,\n signal: AbortSignal,\n): Promise<{ dataUri: string } | { error: string }> {\n let response: Response\n\n try {\n response = await fetch(url, { signal })\n } catch (error) {\n return {\n error: `Could not download the image from ${url}: ${error instanceof Error ? error.message : 'unknown error'}`,\n }\n }\n\n if (!response.ok) {\n return { error: `Could not download the image from ${url}: status ${response.status}` }\n }\n\n // The document's mime type is checked by the endpoint before the resolver\n // runs, but `getImageThumbnail` may point at a derivative in a different\n // format, so trust what was actually served.\n const contentType = response.headers.get('content-type')?.split(';')[0]?.trim().toLowerCase()\n\n if (!contentType || !SUPPORTED_MIME_TYPES.includes(contentType)) {\n return {\n error: `The image at ${url} was served as \"${contentType ?? 'an unknown type'}\", which Mistral cannot read. Supported types: ${SUPPORTED_MIME_TYPES.join(', ')}.`,\n }\n }\n\n const bytes = Buffer.from(await response.arrayBuffer())\n\n if (bytes.byteLength === 0) {\n return { error: `The image at ${url} was empty.` }\n }\n\n if (bytes.byteLength > MAX_IMAGE_BYTES) {\n return {\n error: `The image at ${url} is ${Math.round(bytes.byteLength / 1024 / 1024)} MB, above Mistral's 20 MB limit. Point getImageThumbnail at a smaller image size.`,\n }\n }\n\n return { dataUri: `data:${contentType};base64,${bytes.toString('base64')}` }\n}\n\nfunction buildPrompt(locales: string[]): string {\n const languages = locales.join(', ')\n\n return `\n You are an expert at analyzing images and creating descriptive image alt text.\n\n Please analyze the given image and provide the following in ${languages}:\n - A concise, descriptive alt text (1-2 sentences) as \"altText\". Focus on the subject, action, and setting. Avoid phrases like 'Image of', 'A picture of', or 'Photo showing'. Be specific and include relevant details like location or context if visible. Make no assumptions.\n - A list of keywords that describe the content (e.g., [\"Camel\", \"Palm trees\", \"Desert\"]) as \"keywords\"\n\n If a context is provided, use it to enhance the alt text.\n\n Format your response as a JSON object with ${locales.map((locale) => `\"${locale}\"`).join(', ')} keys, each containing \"altText\" and \"keywords\".\n `\n}\n\n/**\n * Reads the model's response.\n *\n * Deliberately strict about a blank `altText`: the field is required on the\n * collection, so an empty string would satisfy that requirement while telling a\n * screen reader nothing — and nobody looks at an alt text again once it is set.\n */\nfunction parseResults(content: unknown, locales: string[]): null | Record<string, AltTextResult> {\n const parsed = schemaForLocales(locales).safeParse(content)\n\n if (!parsed.success) {\n return null\n }\n\n const results: Record<string, AltTextResult> = {}\n\n for (const locale of locales) {\n const entry = parsed.data[locale]\n\n if (entry.altText.trim().length === 0) {\n return null\n }\n\n results[locale] = { altText: entry.altText.trim(), keywords: entry.keywords }\n }\n\n return results\n}\n\n/**\n * One request, one or more locales.\n *\n * All locales go into a single call rather than one call each: the image is\n * uploaded and analyzed once — the expensive part — and every language ends up\n * describing the same reading of it.\n */\nasync function generate({\n apiKey,\n baseUrl,\n filename,\n imageThumbnailUrl,\n locales,\n model,\n timeoutMs,\n}: {\n apiKey: string\n baseUrl: string\n filename?: string\n imageThumbnailUrl: string\n locales: string[]\n model: string\n timeoutMs: number\n}): Promise<\n { error: string; success: false } | { results: Record<string, AltTextResult>; success: true }\n> {\n if (!apiKey) {\n return { error: 'No Mistral API key configured', success: false }\n }\n\n if (locales.length === 0) {\n return { error: 'No locale requested', success: false }\n }\n\n const signal = AbortSignal.timeout(timeoutMs)\n const image = await fetchImageAsDataUri(imageThumbnailUrl, signal)\n\n if ('error' in image) {\n return { error: image.error, success: false }\n }\n\n try {\n const response = await fetch(`${baseUrl}/chat/completions`, {\n body: JSON.stringify({\n max_tokens: 150 * locales.length,\n messages: [\n { content: buildPrompt(locales), role: 'system' },\n {\n content: [\n { type: 'image_url', image_url: image.dataUri },\n ...(filename ? [{ type: 'text', text: filename }] : []),\n ],\n role: 'user',\n },\n ],\n model,\n response_format: {\n type: 'json_schema',\n json_schema: {\n name: 'data',\n schema: z.toJSONSchema(schemaForLocales(locales), { target: 'draft-7' }),\n strict: true,\n },\n },\n }),\n headers: { Authorization: `Bearer ${apiKey}`, 'Content-Type': 'application/json' },\n method: 'POST',\n signal,\n })\n\n if (!response.ok) {\n const body = await response.text().catch(() => '')\n\n return {\n error: `Mistral responded with status ${response.status}${body ? `: ${body}` : ''}`,\n success: false,\n }\n }\n\n const completion = (await response.json()) as {\n choices?: { message?: { content?: unknown } }[]\n }\n const content = completion.choices?.[0]?.message?.content\n\n if (typeof content !== 'string') {\n return { error: 'No result from Mistral', success: false }\n }\n\n const results = parseResults(JSON.parse(content), locales)\n\n if (!results) {\n return {\n error: `Mistral did not return a usable alt text for every requested locale (${locales.join(', ')})`,\n success: false,\n }\n }\n\n return { results, success: true }\n } catch (error) {\n console.error('Error generating alt text:', error)\n\n return { error: error instanceof Error ? error.message : 'Unknown error', success: false }\n }\n}\n\n/**\n * Creates a Mistral-based resolver for alt text generation.\n *\n * @example\n * ```typescript\n * import { mistralResolver } from '@jhb.software/payload-alt-text-plugin'\n *\n * mistralResolver({\n * apiKey: process.env.MISTRAL_API_KEY,\n * model: 'mistral-medium-latest', // optional, this is the default\n * })\n * ```\n */\nexport const mistralResolver = (config: MistralResolverConfig): AltTextResolver => {\n const {\n apiKey,\n baseUrl = 'https://api.mistral.ai/v1',\n model = 'mistral-medium-latest',\n timeoutMs = 30_000,\n } = config\n\n return {\n key: 'mistral',\n resolve: async ({\n filename,\n imageThumbnailUrl,\n locale,\n }: AltTextResolverArgs): Promise<AltTextResolverResponse> => {\n const result = await generate({\n apiKey,\n baseUrl,\n filename,\n imageThumbnailUrl,\n locales: [locale],\n model,\n timeoutMs,\n })\n\n if (!result.success) {\n return { error: result.error, success: false }\n }\n\n return { result: result.results[locale], success: true }\n },\n resolveBulk: async ({\n filename,\n imageThumbnailUrl,\n locales,\n }: AltTextBulkResolverArgs): Promise<AltTextBulkResolverResponse> => {\n const result = await generate({\n apiKey,\n baseUrl,\n filename,\n imageThumbnailUrl,\n locales,\n model,\n timeoutMs,\n })\n\n if (!result.success) {\n return { error: result.error, success: false }\n }\n\n return { results: result.results, success: true }\n },\n // https://docs.mistral.ai/capabilities/vision/\n supportedMimeTypes: SUPPORTED_MIME_TYPES,\n }\n}\n"],"names":["z","SUPPORTED_MIME_TYPES","MAX_IMAGE_BYTES","altTextSchema","object","altText","string","describe","keywords","array","schemaForLocales","locales","Object","fromEntries","map","locale","fetchImageAsDataUri","url","signal","response","fetch","error","Error","message","ok","status","contentType","headers","get","split","trim","toLowerCase","includes","join","bytes","Buffer","from","arrayBuffer","byteLength","Math","round","dataUri","toString","buildPrompt","languages","parseResults","content","parsed","safeParse","success","results","entry","data","length","generate","apiKey","baseUrl","filename","imageThumbnailUrl","model","timeoutMs","AbortSignal","timeout","image","body","JSON","stringify","max_tokens","messages","role","type","image_url","text","response_format","json_schema","name","schema","toJSONSchema","target","strict","Authorization","method","catch","completion","json","choices","parse","console","mistralResolver","config","key","resolve","result","resolveBulk","supportedMimeTypes"],"mappings":"AAAA,SAASA,CAAC,QAAQ,MAAK;AAqCvB;;;;;;CAMC,GACD,MAAMC,uBAAuB;IAAC;IAAc;IAAa;IAAa;CAAa;AAEnF,wCAAwC,GACxC,MAAMC,kBAAkB,KAAK,OAAO;AAEpC,MAAMC,gBAAgBH,EAAEI,MAAM,CAAC;IAC7BC,SAASL,EAAEM,MAAM,GAAGC,QAAQ,CAAC;IAC7BC,UAAUR,EAAES,KAAK,CAACT,EAAEM,MAAM,IAAIC,QAAQ,CAAC;AACzC;AAEA,qFAAq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;gBACnB,OAAO;oBAAE5B,OAAOuE,OAAOvE,KAAK;oBAAE4B,SAAS;gBAAM;YAC/C;YAEA,OAAO;gBAAEC,SAAS0C,OAAO1C,OAAO;gBAAED,SAAS;YAAK;QAClD;QACA,+CAA+C;QAC/C6C,oBAAoB7F;IACtB;AACF,EAAC"}
1
+ {"version":3,"sources":["../../src/resolvers/mistral.ts"],"sourcesContent":["import type { VisionInstructions } from './createVisionResolver.js'\nimport type { AltTextResolver } from './types.js'\n\nimport { createVisionResolver, VisionProviderError } from './createVisionResolver.js'\n\nexport type MistralResolverConfig = {\n /** Mistral API key for authentication */\n apiKey: string\n /**\n * Base URL of the Mistral API.\n * @default 'https://api.mistral.ai/v1'\n */\n baseUrl?: string\n /**\n * Builds the instructions from the default ones, e.g. to append a house style\n * rule. Sent as the system message, separately from the image.\n *\n * @default ({ defaultInstructions }) => defaultInstructions\n */\n instructions?: VisionInstructions\n /**\n * The vision-capable Mistral model to use for alt text generation.\n *\n * Must be able to read images — `mistral-medium-latest`,\n * `mistral-large-latest`, `mistral-small-latest` and the `ministral-*` models\n * all are.\n *\n * @default 'mistral-medium-latest'\n */\n model?: string\n /**\n * Abort after this many milliseconds. Covers downloading the image and the\n * completion call together.\n * @default 30000\n */\n timeoutMs?: number\n}\n\n/**\n * Image formats the Mistral 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://docs.mistral.ai/capabilities/vision/\n */\nconst SUPPORTED_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']\n\n/**\n * Creates a Mistral-based resolver for alt text generation.\n *\n * The image is downloaded and sent as bytes rather than handed to Mistral as a\n * URL. Mistral's own fetcher requires a publicly reachable file, and even that is\n * not sufficient: hosts serving the image fine to everyone else were observed\n * refusing it with `File could not be fetched from url` (error 3310). See #184\n * for the reproduction.\n *\n * @example\n * ```typescript\n * import { mistralResolver } from '@jhb.software/payload-alt-text-plugin'\n *\n * mistralResolver({\n * apiKey: process.env.MISTRAL_API_KEY,\n * model: 'mistral-medium-latest', // optional, this is the default\n * })\n * ```\n */\nexport const mistralResolver = ({\n apiKey,\n baseUrl = 'https://api.mistral.ai/v1',\n instructions,\n model = 'mistral-medium-latest',\n timeoutMs = 30_000,\n}: MistralResolverConfig): 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}/chat/completions`, {\n body: JSON.stringify({\n max_tokens: maxTokens,\n messages: [\n { content: resolvedInstructions, role: 'system' },\n {\n content: [\n { type: 'image_url', image_url: image.dataUri },\n ...(filename ? [{ type: 'text', text: filename }] : []),\n ],\n role: 'user',\n },\n ],\n model,\n response_format: {\n type: 'json_schema',\n json_schema: { name: 'data', schema: responseSchema, strict: true },\n },\n }),\n headers: { Authorization: `Bearer ${apiKey}`, 'Content-Type': 'application/json' },\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: 'Mistral', status: response.status })\n }\n\n const completion = (await response.json()) as {\n choices?: { finish_reason?: string; message?: { content?: unknown } }[]\n }\n const choice = completion.choices?.[0]\n\n if (choice?.finish_reason === 'length') {\n throw new Error(\n `Mistral ran out of tokens before finishing the alt text (max_tokens: ${maxTokens})`,\n )\n }\n\n const content = choice?.message?.content\n\n if (typeof content !== 'string') {\n throw new Error('No result from Mistral')\n }\n\n try {\n return JSON.parse(content)\n } catch {\n throw new Error('Mistral returned a response that was not valid JSON')\n }\n },\n inlineImage: true,\n instructions,\n key: 'mistral',\n label: 'Mistral',\n // Mistral rejects images above 20 MB.\n maxImageBytes: 20 * 1024 * 1024,\n supportedMimeTypes: SUPPORTED_MIME_TYPES,\n timeoutMs,\n })\n"],"names":["createVisionResolver","VisionProviderError","SUPPORTED_MIME_TYPES","mistralResolver","apiKey","baseUrl","instructions","model","timeoutMs","generate","filename","image","resolvedInstructions","maxTokens","responseSchema","signal","Error","response","fetch","body","JSON","stringify","max_tokens","messages","content","role","type","image_url","dataUri","text","response_format","json_schema","name","schema","strict","headers","Authorization","method","ok","catch","slice","label","status","completion","json","choice","choices","finish_reason","message","parse","inlineImage","key","maxImageBytes","supportedMimeTypes"],"mappings":"AAGA,SAASA,oBAAoB,EAAEC,mBAAmB,QAAQ,4BAA2B;AAmCrF;;;;;;;;CAQC,GACD,MAAMC,uBAAuB;IAAC;IAAc;IAAa;IAAa;CAAa;AAEnF;;;;;;;;;;;;;;;;;;CAkBC,GACD,OAAO,MAAMC,kBAAkB,CAAC,EAC9BC,MAAM,EACNC,UAAU,2BAA2B,EACrCC,YAAY,EACZC,QAAQ,uBAAuB,EAC/BC,YAAY,MAAM,EACI,GACtBR,qBAAqB;QACnBI;QACAK,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,GAAGb,QAAQ,iBAAiB,CAAC,EAAE;gBAC1Dc,MAAMC,KAAKC,SAAS,CAAC;oBACnBC,YAAYT;oBACZU,UAAU;wBACR;4BAAEC,SAASZ;4BAAsBa,MAAM;wBAAS;wBAChD;4BACED,SAAS;gCACP;oCAAEE,MAAM;oCAAaC,WAAWhB,MAAMiB,OAAO;gCAAC;mCAC1ClB,WAAW;oCAAC;wCAAEgB,MAAM;wCAAQG,MAAMnB;oCAAS;iCAAE,GAAG,EAAE;6BACvD;4BACDe,MAAM;wBACR;qBACD;oBACDlB;oBACAuB,iBAAiB;wBACfJ,MAAM;wBACNK,aAAa;4BAAEC,MAAM;4BAAQC,QAAQnB;4BAAgBoB,QAAQ;wBAAK;oBACpE;gBACF;gBACAC,SAAS;oBAAEC,eAAe,CAAC,OAAO,EAAEhC,QAAQ;oBAAE,gBAAgB;gBAAmB;gBACjFiC,QAAQ;gBACRtB;YACF;YAEA,IAAI,CAACE,SAASqB,EAAE,EAAE;gBAChB,gEAAgE;gBAChE,MAAMnB,OAAO,AAAC,CAAA,MAAMF,SAASY,IAAI,GAAGU,KAAK,CAAC,IAAM,GAAE,EAAGC,KAAK,CAAC,GAAG;gBAE9D,MAAM,IAAIvC,oBAAoB;oBAAEkB;oBAAMsB,OAAO;oBAAWC,QAAQzB,SAASyB,MAAM;gBAAC;YAClF;YAEA,MAAMC,aAAc,MAAM1B,SAAS2B,IAAI;YAGvC,MAAMC,SAASF,WAAWG,OAAO,EAAE,CAAC,EAAE;YAEtC,IAAID,QAAQE,kBAAkB,UAAU;gBACtC,MAAM,IAAI/B,MACR,CAAC,qEAAqE,EAAEH,UAAU,CAAC,CAAC;YAExF;YAEA,MAAMW,UAAUqB,QAAQG,SAASxB;YAEjC,IAAI,OAAOA,YAAY,UAAU;gBAC/B,MAAM,IAAIR,MAAM;YAClB;YAEA,IAAI;gBACF,OAAOI,KAAK6B,KAAK,CAACzB;YACpB,EAAE,OAAM;gBACN,MAAM,IAAIR,MAAM;YAClB;QACF;QACAkC,aAAa;QACb5C;QACA6C,KAAK;QACLV,OAAO;QACP,sCAAsC;QACtCW,eAAe,KAAK,OAAO;QAC3BC,oBAAoBnD;QACpBM;IACF,GAAE"}
@@ -1,13 +1,21 @@
1
+ import type { VisionInstructions } from './createVisionResolver.js';
1
2
  import type { AltTextResolver } from './types.js';
2
3
  export type OpenAIResolverConfig = {
3
4
  /** OpenAI API key for authentication */
4
5
  apiKey: string;
5
6
  /**
6
- * Base URL for the OpenAI-compatible API.
7
+ * Base URL for the OpenAI-compatible API, including the version segment.
7
8
  * Use this to point at alternative providers (e.g. Azure, Nebius, local inference).
8
- * @default undefined — the OpenAI SDK defaults to 'https://api.openai.com/v1'
9
+ * @default 'https://api.openai.com/v1'
9
10
  */
10
11
  baseUrl?: string;
12
+ /**
13
+ * Builds the instructions from the default ones, e.g. to append a house style
14
+ * rule. Sent as the system message, separately from the image.
15
+ *
16
+ * @default ({ defaultInstructions }) => defaultInstructions
17
+ */
18
+ instructions?: VisionInstructions;
11
19
  /**
12
20
  * The OpenAI LLM model to use for alt text generation.
13
21
  * @default 'gpt-4.1-nano'
@@ -23,10 +31,21 @@ export type OpenAIResolverConfig = {
23
31
  * @default ['image/jpeg', 'image/png', 'image/gif', 'image/webp']
24
32
  */
25
33
  supportedMimeTypes?: string[];
34
+ /**
35
+ * Abort after this many milliseconds, covering the completion call and the
36
+ * retries the factory makes within it.
37
+ * @default 30000
38
+ */
39
+ timeoutMs?: number;
26
40
  };
27
41
  /**
28
42
  * Creates an OpenAI-based resolver for alt text generation.
29
43
  *
44
+ * The thumbnail URL is handed to OpenAI, which fetches it itself — so the URL
45
+ * has to be reachable from the public internet. When it is not, reach for a
46
+ * resolver that inlines the bytes instead (`mistralResolver`,
47
+ * `anthropicResolver`).
48
+ *
30
49
  * @example
31
50
  * ```typescript
32
51
  * import { openAIResolver } from '@jhb.software/payload-alt-text-plugin'
@@ -45,4 +64,4 @@ export type OpenAIResolverConfig = {
45
64
  * })
46
65
  * ```
47
66
  */
48
- export declare const openAIResolver: (config: OpenAIResolverConfig) => AltTextResolver;
67
+ export declare const openAIResolver: ({ apiKey, baseUrl, instructions, model, supportedMimeTypes, timeoutMs, }: OpenAIResolverConfig) => AltTextResolver;