@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.
- package/README.md +164 -18
- package/dist/endpoints/bulkGenerateAltTexts.js +8 -4
- package/dist/endpoints/bulkGenerateAltTexts.js.map +1 -1
- package/dist/endpoints/generateAltText.js +3 -1
- package/dist/endpoints/generateAltText.js.map +1 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/plugin.js +19 -4
- package/dist/plugin.js.map +1 -1
- package/dist/resolvers/anthropic.d.ts +65 -0
- package/dist/resolvers/anthropic.js +141 -0
- package/dist/resolvers/anthropic.js.map +1 -0
- package/dist/resolvers/createVisionResolver.d.ts +148 -0
- package/dist/resolvers/createVisionResolver.js +300 -0
- package/dist/resolvers/createVisionResolver.js.map +1 -0
- package/dist/resolvers/mistral.d.ts +14 -1
- package/dist/resolvers/mistral.js +84 -250
- package/dist/resolvers/mistral.js.map +1 -1
- package/dist/resolvers/openAI.d.ts +22 -3
- package/dist/resolvers/openAI.js +57 -138
- package/dist/resolvers/openAI.js.map +1 -1
- package/dist/types/AltTextPluginConfig.d.ts +47 -11
- package/dist/types/AltTextPluginConfig.js.map +1 -1
- package/dist/utilities/altTextHealth.d.ts +3 -1
- package/dist/utilities/altTextHealth.js +74 -8
- package/dist/utilities/altTextHealth.js.map +1 -1
- package/dist/utilities/stableStringify.d.ts +9 -0
- package/dist/utilities/stableStringify.js +19 -0
- package/dist/utilities/stableStringify.js.map +1 -0
- package/package.json +4 -5
package/dist/resolvers/openAI.js
CHANGED
|
@@ -1,34 +1,18 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { makeParseableResponseFormat } from 'openai/lib/parser.mjs';
|
|
3
|
-
import { z } from 'zod';
|
|
1
|
+
import { createVisionResolver, VisionProviderError } from './createVisionResolver.js';
|
|
4
2
|
/** @see https://platform.openai.com/docs/guides/images-vision */ const OPENAI_SUPPORTED_MIME_TYPES = [
|
|
5
3
|
'image/jpeg',
|
|
6
4
|
'image/png',
|
|
7
5
|
'image/gif',
|
|
8
6
|
'image/webp'
|
|
9
7
|
];
|
|
10
|
-
/**
|
|
11
|
-
* Creates a chat completion `JSONSchema` response format object from
|
|
12
|
-
* the given Zod schema.
|
|
13
|
-
*
|
|
14
|
-
* This is a temporary drop in replacement for the zodResponseFormat from openai/helpers/zod.ts
|
|
15
|
-
* because of issue https://github.com/openai/openai-node/issues/1576
|
|
16
|
-
*/ function zodResponseFormat(zodObject, name, props) {
|
|
17
|
-
return makeParseableResponseFormat({
|
|
18
|
-
type: 'json_schema',
|
|
19
|
-
json_schema: {
|
|
20
|
-
...props,
|
|
21
|
-
name,
|
|
22
|
-
schema: z.toJSONSchema(zodObject, {
|
|
23
|
-
target: 'draft-7'
|
|
24
|
-
}),
|
|
25
|
-
strict: true
|
|
26
|
-
}
|
|
27
|
-
}, (content)=>zodObject.parse(JSON.parse(content)));
|
|
28
|
-
}
|
|
29
8
|
/**
|
|
30
9
|
* Creates an OpenAI-based resolver for alt text generation.
|
|
31
10
|
*
|
|
11
|
+
* The thumbnail URL is handed to OpenAI, which fetches it itself — so the URL
|
|
12
|
+
* has to be reachable from the public internet. Behind a private bucket or in
|
|
13
|
+
* local development, reach for a resolver that inlines the bytes instead
|
|
14
|
+
* (`mistralResolver`, `anthropicResolver`).
|
|
15
|
+
*
|
|
32
16
|
* @example
|
|
33
17
|
* ```typescript
|
|
34
18
|
* import { openAIResolver } from '@jhb.software/payload-alt-text-plugin'
|
|
@@ -46,39 +30,15 @@ import { z } from 'zod';
|
|
|
46
30
|
* model: 'Qwen/Qwen2.5-VL-72B-Instruct',
|
|
47
31
|
* })
|
|
48
32
|
* ```
|
|
49
|
-
*/ export const openAIResolver = (
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const getClient = ()=>openai ??= new OpenAI({
|
|
56
|
-
apiKey,
|
|
57
|
-
baseURL: baseUrl
|
|
58
|
-
});
|
|
59
|
-
return {
|
|
60
|
-
key: 'openai',
|
|
61
|
-
resolve: async ({ filename, imageThumbnailUrl, locale })=>{
|
|
62
|
-
try {
|
|
63
|
-
const modelResponseSchema = z.object({
|
|
64
|
-
altText: z.string().describe('A concise, descriptive alt text for the image'),
|
|
65
|
-
keywords: z.array(z.string()).describe('Keywords that describe the content of the image')
|
|
66
|
-
});
|
|
67
|
-
const response = await getClient().chat.completions.parse({
|
|
68
|
-
max_completion_tokens: 150,
|
|
33
|
+
*/ export const openAIResolver = ({ apiKey, baseUrl = 'https://api.openai.com/v1', instructions, model = 'gpt-4.1-nano', supportedMimeTypes = OPENAI_SUPPORTED_MIME_TYPES, timeoutMs = 30_000 })=>createVisionResolver({
|
|
34
|
+
apiKey,
|
|
35
|
+
generate: async ({ filename, imageThumbnailUrl, instructions: resolvedInstructions, maxTokens, responseSchema, signal })=>{
|
|
36
|
+
const response = await fetch(`${baseUrl}/chat/completions`, {
|
|
37
|
+
body: JSON.stringify({
|
|
38
|
+
max_completion_tokens: maxTokens,
|
|
69
39
|
messages: [
|
|
70
40
|
{
|
|
71
|
-
content:
|
|
72
|
-
You are an expert at analyzing images and creating descriptive image alt text.
|
|
73
|
-
|
|
74
|
-
Please analyze the given image and provide the following:
|
|
75
|
-
- 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.
|
|
76
|
-
- A list of keywords that describe the content (e.g., ["Camel", "Palm trees", "Desert"]) as "keywords"
|
|
77
|
-
|
|
78
|
-
If a context is provided, use it to enhance the alt text.
|
|
79
|
-
|
|
80
|
-
Format your response as a JSON object. You must respond in the ${locale} language.
|
|
81
|
-
`,
|
|
41
|
+
content: resolvedInstructions,
|
|
82
42
|
role: 'system'
|
|
83
43
|
},
|
|
84
44
|
{
|
|
@@ -100,95 +60,54 @@ import { z } from 'zod';
|
|
|
100
60
|
}
|
|
101
61
|
],
|
|
102
62
|
model,
|
|
103
|
-
response_format:
|
|
63
|
+
response_format: {
|
|
64
|
+
type: 'json_schema',
|
|
65
|
+
json_schema: {
|
|
66
|
+
name: 'data',
|
|
67
|
+
schema: responseSchema,
|
|
68
|
+
strict: true
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}),
|
|
72
|
+
headers: {
|
|
73
|
+
Authorization: `Bearer ${apiKey}`,
|
|
74
|
+
'Content-Type': 'application/json'
|
|
75
|
+
},
|
|
76
|
+
method: 'POST',
|
|
77
|
+
signal
|
|
78
|
+
});
|
|
79
|
+
if (!response.ok) {
|
|
80
|
+
// Bounded: unbounded provider text would land in the log as-is.
|
|
81
|
+
const body = (await response.text().catch(()=>'')).slice(0, 500);
|
|
82
|
+
throw new VisionProviderError({
|
|
83
|
+
body,
|
|
84
|
+
label: 'OpenAI',
|
|
85
|
+
status: response.status
|
|
104
86
|
});
|
|
105
|
-
const result = response.choices[0]?.message?.parsed;
|
|
106
|
-
if (!result) {
|
|
107
|
-
return {
|
|
108
|
-
error: 'No result from OpenAI',
|
|
109
|
-
success: false
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
return {
|
|
113
|
-
result,
|
|
114
|
-
success: true
|
|
115
|
-
};
|
|
116
|
-
} catch (error) {
|
|
117
|
-
console.error('Error generating alt text:', error);
|
|
118
|
-
return {
|
|
119
|
-
error: error instanceof Error ? error.message : 'Unknown error',
|
|
120
|
-
success: false
|
|
121
|
-
};
|
|
122
87
|
}
|
|
123
|
-
|
|
124
|
-
|
|
88
|
+
const completion = await response.json();
|
|
89
|
+
const choice = completion.choices?.[0];
|
|
90
|
+
// A budget exhausted mid-JSON otherwise reaches JSON.parse and reads as
|
|
91
|
+
// "Unexpected end of JSON input" in the admin panel, which tells an editor
|
|
92
|
+
// nothing about what to change.
|
|
93
|
+
if (choice?.finish_reason === 'length') {
|
|
94
|
+
throw new Error(`OpenAI ran out of tokens before finishing the alt text (max_completion_tokens: ${maxTokens})`);
|
|
95
|
+
}
|
|
96
|
+
const content = choice?.message?.content;
|
|
97
|
+
if (typeof content !== 'string') {
|
|
98
|
+
throw new Error('No result from OpenAI');
|
|
99
|
+
}
|
|
125
100
|
try {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
altText: z.string().describe('A concise, descriptive alt text for the image'),
|
|
130
|
-
keywords: z.array(z.string()).describe('Keywords that describe the content of the image')
|
|
131
|
-
})
|
|
132
|
-
])));
|
|
133
|
-
const response = await getClient().chat.completions.parse({
|
|
134
|
-
max_completion_tokens: 300,
|
|
135
|
-
messages: [
|
|
136
|
-
{
|
|
137
|
-
content: `
|
|
138
|
-
You are an expert at analyzing images and creating descriptive image alt text.
|
|
139
|
-
|
|
140
|
-
Please analyze the given image and provide the following in ${locales.join(', ')}:
|
|
141
|
-
- 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.
|
|
142
|
-
- A localized list of keywords that describe the content (e.g., ["Camel", "Palm trees", "Desert"]) as "keywords"
|
|
143
|
-
|
|
144
|
-
If a context is provided, use it to enhance the alt text.
|
|
145
|
-
|
|
146
|
-
Format your response as a JSON object with ${locales.join(', ')} keys, each containing "altText" and "keywords".
|
|
147
|
-
`,
|
|
148
|
-
role: 'system'
|
|
149
|
-
},
|
|
150
|
-
{
|
|
151
|
-
content: [
|
|
152
|
-
{
|
|
153
|
-
type: 'image_url',
|
|
154
|
-
image_url: {
|
|
155
|
-
url: imageThumbnailUrl
|
|
156
|
-
}
|
|
157
|
-
},
|
|
158
|
-
...filename ? [
|
|
159
|
-
{
|
|
160
|
-
type: 'text',
|
|
161
|
-
text: filename
|
|
162
|
-
}
|
|
163
|
-
] : []
|
|
164
|
-
],
|
|
165
|
-
role: 'user'
|
|
166
|
-
}
|
|
167
|
-
],
|
|
168
|
-
model,
|
|
169
|
-
response_format: zodResponseFormat(modelResponseSchema, 'data')
|
|
170
|
-
});
|
|
171
|
-
const result = response.choices[0]?.message?.parsed;
|
|
172
|
-
if (!result) {
|
|
173
|
-
return {
|
|
174
|
-
error: 'No result from OpenAI',
|
|
175
|
-
success: false
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
return {
|
|
179
|
-
results: result,
|
|
180
|
-
success: true
|
|
181
|
-
};
|
|
182
|
-
} catch (error) {
|
|
183
|
-
console.error('Error generating bulk alt text:', error);
|
|
184
|
-
return {
|
|
185
|
-
error: error instanceof Error ? error.message : 'Unknown error',
|
|
186
|
-
success: false
|
|
187
|
-
};
|
|
101
|
+
return JSON.parse(content);
|
|
102
|
+
} catch {
|
|
103
|
+
throw new Error('OpenAI returned a response that was not valid JSON');
|
|
188
104
|
}
|
|
189
105
|
},
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
106
|
+
instructions,
|
|
107
|
+
key: 'openai',
|
|
108
|
+
label: 'OpenAI',
|
|
109
|
+
supportedMimeTypes,
|
|
110
|
+
timeoutMs
|
|
111
|
+
});
|
|
193
112
|
|
|
194
113
|
//# sourceMappingURL=openAI.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/resolvers/openAI.ts"],"sourcesContent":["import type { AutoParseableResponseFormat } from 'openai/lib/parser.mjs'\nimport type { ChatCompletionContentPartText } from 'openai/resources/chat/completions.mjs'\nimport type { ResponseFormatJSONSchema } from 'openai/resources/shared.mjs'\n\nimport OpenAI from 'openai'\nimport { makeParseableResponseFormat } from 'openai/lib/parser.mjs'\nimport { z } from 'zod'\n\nimport type {\n AltTextBulkResolverArgs,\n AltTextBulkResolverResponse,\n AltTextResolver,\n AltTextResolverArgs,\n AltTextResolverResponse,\n} from './types.js'\n\nexport type OpenAIResolverConfig = {\n /** OpenAI API key for authentication */\n apiKey: string\n /**\n * Base URL for the OpenAI-compatible API.\n * Use this to point at alternative providers (e.g. Azure, Nebius, local inference).\n * @default undefined — the OpenAI SDK defaults to 'https://api.openai.com/v1'\n */\n baseUrl?: string\n /**\n * The OpenAI LLM model to use for alt text generation.\n * @default 'gpt-4.1-nano'\n */\n model?: string\n /**\n * The MIME types the provider accepts for the image URL.\n *\n * Defaults to the formats documented for OpenAI's vision models. Override it\n * when pointing `baseUrl` at another provider whose accepted formats differ —\n * the person choosing the provider is the one who knows.\n *\n * @default ['image/jpeg', 'image/png', 'image/gif', 'image/webp']\n */\n supportedMimeTypes?: string[]\n}\n\n/** @see https://platform.openai.com/docs/guides/images-vision */\nconst OPENAI_SUPPORTED_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']\n\n/**\n * Creates a chat completion `JSONSchema` response format object from\n * the given Zod schema.\n *\n * This is a temporary drop in replacement for the zodResponseFormat from openai/helpers/zod.ts\n * because of issue https://github.com/openai/openai-node/issues/1576\n */\nfunction zodResponseFormat<ZodInput extends z.ZodType>(\n zodObject: ZodInput,\n name: string,\n props?: Omit<ResponseFormatJSONSchema.JSONSchema, 'name' | 'schema' | 'strict'>,\n): AutoParseableResponseFormat<z.infer<ZodInput>> {\n return makeParseableResponseFormat(\n {\n type: 'json_schema',\n json_schema: {\n ...props,\n name,\n schema: z.toJSONSchema(zodObject, { target: 'draft-7' }),\n strict: true,\n },\n },\n (content) => zodObject.parse(JSON.parse(content)),\n )\n}\n\n/**\n * Creates an OpenAI-based resolver for alt text generation.\n *\n * @example\n * ```typescript\n * import { openAIResolver } from '@jhb.software/payload-alt-text-plugin'\n *\n * // OpenAI\n * openAIResolver({\n * apiKey: process.env.OPENAI_API_KEY,\n * model: 'gpt-4.1-mini', // optional, defaults to 'gpt-4.1-nano'\n * })\n *\n * // OpenAI-compatible provider (e.g. Nebius)\n * openAIResolver({\n * apiKey: process.env.NEBIUS_API_KEY,\n * baseUrl: 'https://api.tokenfactory.us-central1.nebius.com/v1',\n * model: 'Qwen/Qwen2.5-VL-72B-Instruct',\n * })\n * ```\n */\nexport const openAIResolver = (config: OpenAIResolverConfig): AltTextResolver => {\n const { apiKey, baseUrl, model = 'gpt-4.1-nano' } = config\n\n // Build the client lazily (once, on first use): the `resolver` argument is\n // evaluated even when the plugin is disabled, so eager construction would\n // throw on a keyless `enabled: !!process.env.OPENAI_API_KEY` setup.\n let openai: OpenAI | undefined\n const getClient = (): OpenAI => (openai ??= new OpenAI({ apiKey, baseURL: baseUrl }))\n\n return {\n key: 'openai',\n resolve: async ({\n filename,\n imageThumbnailUrl,\n locale,\n }: AltTextResolverArgs): Promise<AltTextResolverResponse> => {\n try {\n const modelResponseSchema = 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 const response = await getClient().chat.completions.parse({\n max_completion_tokens: 150,\n messages: [\n {\n content: `\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:\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. You must respond in the ${locale} language.\n `,\n role: 'system',\n },\n {\n content: [\n {\n type: 'image_url',\n image_url: { url: imageThumbnailUrl },\n },\n ...(filename\n ? [\n {\n type: 'text',\n text: filename,\n } satisfies ChatCompletionContentPartText,\n ]\n : []),\n ],\n role: 'user',\n },\n ],\n model,\n response_format: zodResponseFormat(modelResponseSchema, 'data'),\n })\n\n const result = response.choices[0]?.message?.parsed\n\n if (!result) {\n return { error: 'No result from OpenAI', success: false }\n }\n\n return {\n result,\n success: true,\n }\n } catch (error) {\n console.error('Error generating alt text:', error)\n return {\n error: error instanceof Error ? error.message : 'Unknown error',\n success: false,\n }\n }\n },\n resolveBulk: async ({\n filename,\n imageThumbnailUrl,\n locales,\n }: AltTextBulkResolverArgs): Promise<AltTextBulkResolverResponse> => {\n try {\n const modelResponseSchema = z.object(\n Object.fromEntries(\n locales.map((locale) => [\n locale,\n z.object({\n altText: z.string().describe('A concise, descriptive alt text for the image'),\n keywords: z\n .array(z.string())\n .describe('Keywords that describe the content of the image'),\n }),\n ]),\n ),\n )\n\n const response = await getClient().chat.completions.parse({\n max_completion_tokens: 300,\n messages: [\n {\n content: `\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 - 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 - 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.join(', ')} keys, each containing \"altText\" and \"keywords\".\n `,\n role: 'system',\n },\n {\n content: [\n {\n type: 'image_url',\n image_url: { url: imageThumbnailUrl },\n },\n ...(filename\n ? [\n {\n type: 'text',\n text: filename,\n } satisfies ChatCompletionContentPartText,\n ]\n : []),\n ],\n role: 'user',\n },\n ],\n model,\n response_format: zodResponseFormat(modelResponseSchema, 'data'),\n })\n\n const result = response.choices[0]?.message?.parsed\n\n if (!result) {\n return { error: 'No result from OpenAI', success: false }\n }\n\n return {\n results: result,\n success: true,\n }\n } catch (error) {\n console.error('Error generating bulk alt text:', error)\n return {\n error: error instanceof Error ? error.message : 'Unknown error',\n success: false,\n }\n }\n },\n supportedMimeTypes: config.supportedMimeTypes ?? OPENAI_SUPPORTED_MIME_TYPES,\n }\n}\n"],"names":["OpenAI","makeParseableResponseFormat","z","OPENAI_SUPPORTED_MIME_TYPES","zodResponseFormat","zodObject","name","props","type","json_schema","schema","toJSONSchema","target","strict","content","parse","JSON","openAIResolver","config","apiKey","baseUrl","model","openai","getClient","baseURL","key","resolve","filename","imageThumbnailUrl","locale","modelResponseSchema","object","altText","string","describe","keywords","array","response","chat","completions","max_completion_tokens","messages","role","image_url","url","text","response_format","result","choices","message","parsed","error","success","console","Error","resolveBulk","locales","Object","fromEntries","map","join","results","supportedMimeTypes"],"mappings":"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"}
|
|
1
|
+
{"version":3,"sources":["../../src/resolvers/openAI.ts"],"sourcesContent":["import type { VisionInstructions } from './createVisionResolver.js'\nimport type { AltTextResolver } from './types.js'\n\nimport { createVisionResolver, VisionProviderError } from './createVisionResolver.js'\n\nexport type OpenAIResolverConfig = {\n /** OpenAI API key for authentication */\n apiKey: string\n /**\n * Base URL for the OpenAI-compatible API, including the version segment.\n * Use this to point at alternative providers (e.g. Azure, Nebius, local inference).\n * @default 'https://api.openai.com/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 OpenAI LLM model to use for alt text generation.\n * @default 'gpt-4.1-nano'\n */\n model?: string\n /**\n * The MIME types the provider accepts for the image URL.\n *\n * Defaults to the formats documented for OpenAI's vision models. Override it\n * when pointing `baseUrl` at another provider whose accepted formats differ —\n * the person choosing the provider is the one who knows.\n *\n * @default ['image/jpeg', 'image/png', 'image/gif', 'image/webp']\n */\n supportedMimeTypes?: string[]\n /**\n * Abort after this many milliseconds, covering the completion call and the\n * retries the factory makes within it.\n * @default 30000\n */\n timeoutMs?: number\n}\n\n/** @see https://platform.openai.com/docs/guides/images-vision */\nconst OPENAI_SUPPORTED_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']\n\n/**\n * Creates an OpenAI-based resolver for alt text generation.\n *\n * The thumbnail URL is handed to OpenAI, which fetches it itself — so the URL\n * has to be reachable from the public internet. Behind a private bucket or in\n * local development, reach for a resolver that inlines the bytes instead\n * (`mistralResolver`, `anthropicResolver`).\n *\n * @example\n * ```typescript\n * import { openAIResolver } from '@jhb.software/payload-alt-text-plugin'\n *\n * // OpenAI\n * openAIResolver({\n * apiKey: process.env.OPENAI_API_KEY,\n * model: 'gpt-4.1-mini', // optional, defaults to 'gpt-4.1-nano'\n * })\n *\n * // OpenAI-compatible provider (e.g. Nebius)\n * openAIResolver({\n * apiKey: process.env.NEBIUS_API_KEY,\n * baseUrl: 'https://api.tokenfactory.us-central1.nebius.com/v1',\n * model: 'Qwen/Qwen2.5-VL-72B-Instruct',\n * })\n * ```\n */\nexport const openAIResolver = ({\n apiKey,\n baseUrl = 'https://api.openai.com/v1',\n instructions,\n model = 'gpt-4.1-nano',\n supportedMimeTypes = OPENAI_SUPPORTED_MIME_TYPES,\n timeoutMs = 30_000,\n}: OpenAIResolverConfig): AltTextResolver =>\n createVisionResolver({\n apiKey,\n generate: async ({\n filename,\n imageThumbnailUrl,\n instructions: resolvedInstructions,\n maxTokens,\n responseSchema,\n signal,\n }) => {\n const response = await fetch(`${baseUrl}/chat/completions`, {\n body: JSON.stringify({\n max_completion_tokens: maxTokens,\n messages: [\n { content: resolvedInstructions, role: 'system' },\n {\n content: [\n { type: 'image_url', image_url: { url: imageThumbnailUrl } },\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: 'OpenAI', 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 // A budget exhausted mid-JSON otherwise reaches JSON.parse and reads as\n // \"Unexpected end of JSON input\" in the admin panel, which tells an editor\n // nothing about what to change.\n if (choice?.finish_reason === 'length') {\n throw new Error(\n `OpenAI ran out of tokens before finishing the alt text (max_completion_tokens: ${maxTokens})`,\n )\n }\n\n const content = choice?.message?.content\n\n if (typeof content !== 'string') {\n throw new Error('No result from OpenAI')\n }\n\n try {\n return JSON.parse(content)\n } catch {\n throw new Error('OpenAI returned a response that was not valid JSON')\n }\n },\n instructions,\n key: 'openai',\n label: 'OpenAI',\n supportedMimeTypes,\n timeoutMs,\n })\n"],"names":["createVisionResolver","VisionProviderError","OPENAI_SUPPORTED_MIME_TYPES","openAIResolver","apiKey","baseUrl","instructions","model","supportedMimeTypes","timeoutMs","generate","filename","imageThumbnailUrl","resolvedInstructions","maxTokens","responseSchema","signal","response","fetch","body","JSON","stringify","max_completion_tokens","messages","content","role","type","image_url","url","text","response_format","json_schema","name","schema","strict","headers","Authorization","method","ok","catch","slice","label","status","completion","json","choice","choices","finish_reason","Error","message","parse","key"],"mappings":"AAGA,SAASA,oBAAoB,EAAEC,mBAAmB,QAAQ,4BAA2B;AAyCrF,+DAA+D,GAC/D,MAAMC,8BAA8B;IAAC;IAAc;IAAa;IAAa;CAAa;AAE1F;;;;;;;;;;;;;;;;;;;;;;;;;CAyBC,GACD,OAAO,MAAMC,iBAAiB,CAAC,EAC7BC,MAAM,EACNC,UAAU,2BAA2B,EACrCC,YAAY,EACZC,QAAQ,cAAc,EACtBC,qBAAqBN,2BAA2B,EAChDO,YAAY,MAAM,EACG,GACrBT,qBAAqB;QACnBI;QACAM,UAAU,OAAO,EACfC,QAAQ,EACRC,iBAAiB,EACjBN,cAAcO,oBAAoB,EAClCC,SAAS,EACTC,cAAc,EACdC,MAAM,EACP;YACC,MAAMC,WAAW,MAAMC,MAAM,GAAGb,QAAQ,iBAAiB,CAAC,EAAE;gBAC1Dc,MAAMC,KAAKC,SAAS,CAAC;oBACnBC,uBAAuBR;oBACvBS,UAAU;wBACR;4BAAEC,SAASX;4BAAsBY,MAAM;wBAAS;wBAChD;4BACED,SAAS;gCACP;oCAAEE,MAAM;oCAAaC,WAAW;wCAAEC,KAAKhB;oCAAkB;gCAAE;mCACvDD,WAAW;oCAAC;wCAAEe,MAAM;wCAAQG,MAAMlB;oCAAS;iCAAE,GAAG,EAAE;6BACvD;4BACDc,MAAM;wBACR;qBACD;oBACDlB;oBACAuB,iBAAiB;wBACfJ,MAAM;wBACNK,aAAa;4BAAEC,MAAM;4BAAQC,QAAQlB;4BAAgBmB,QAAQ;wBAAK;oBACpE;gBACF;gBACAC,SAAS;oBAAEC,eAAe,CAAC,OAAO,EAAEhC,QAAQ;oBAAE,gBAAgB;gBAAmB;gBACjFiC,QAAQ;gBACRrB;YACF;YAEA,IAAI,CAACC,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;oBAAUC,QAAQzB,SAASyB,MAAM;gBAAC;YACjF;YAEA,MAAMC,aAAc,MAAM1B,SAAS2B,IAAI;YAGvC,MAAMC,SAASF,WAAWG,OAAO,EAAE,CAAC,EAAE;YAEtC,wEAAwE;YACxE,2EAA2E;YAC3E,gCAAgC;YAChC,IAAID,QAAQE,kBAAkB,UAAU;gBACtC,MAAM,IAAIC,MACR,CAAC,+EAA+E,EAAElC,UAAU,CAAC,CAAC;YAElG;YAEA,MAAMU,UAAUqB,QAAQI,SAASzB;YAEjC,IAAI,OAAOA,YAAY,UAAU;gBAC/B,MAAM,IAAIwB,MAAM;YAClB;YAEA,IAAI;gBACF,OAAO5B,KAAK8B,KAAK,CAAC1B;YACpB,EAAE,OAAM;gBACN,MAAM,IAAIwB,MAAM;YAClB;QACF;QACA1C;QACA6C,KAAK;QACLV,OAAO;QACPjC;QACAC;IACF,GAAE"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CollectionSlug, Field, PayloadRequest } from 'payload';
|
|
1
|
+
import type { CollectionSlug, Field, PayloadRequest, Where } from 'payload';
|
|
2
2
|
import type { AltTextResolver } from '../resolvers/types.js';
|
|
3
3
|
import type { AltTextCollectionConfig, IncomingCollectionsConfig, NormalizedAltTextCollectionConfig } from '../utilities/mimeTypes.js';
|
|
4
4
|
export type { AltTextCollectionConfig, NormalizedAltTextCollectionConfig };
|
|
@@ -18,6 +18,47 @@ export type GetImageThumbnail = (doc: Record<string, unknown>, args: {
|
|
|
18
18
|
collection: CollectionSlug;
|
|
19
19
|
req: PayloadRequest;
|
|
20
20
|
}) => Promise<string> | string;
|
|
21
|
+
/**
|
|
22
|
+
* Narrows the health scan of one collection to a subset of its documents — in a
|
|
23
|
+
* multi-tenant CMS, to the tenant the request is for. The returned constraint is
|
|
24
|
+
* ANDed onto the scan's MIME type filter.
|
|
25
|
+
*
|
|
26
|
+
* Called once per configured collection, so collections that do not carry the
|
|
27
|
+
* constraining field (a media library shared across tenants, say) can return `{}`
|
|
28
|
+
* instead of being queried for a field they do not have.
|
|
29
|
+
*
|
|
30
|
+
* Return `{}` to scan the collection whole.
|
|
31
|
+
*
|
|
32
|
+
* @param args.collection The slug of the collection being scanned.
|
|
33
|
+
* @param args.req The request the scan runs for.
|
|
34
|
+
*/
|
|
35
|
+
export type AltTextHealthBaseFilter = (args: {
|
|
36
|
+
collection: CollectionSlug;
|
|
37
|
+
req: PayloadRequest;
|
|
38
|
+
}) => Promise<Where> | Where;
|
|
39
|
+
/** Configuration of the alt text health feature. */
|
|
40
|
+
export type AltTextHealthCheckConfig = {
|
|
41
|
+
/**
|
|
42
|
+
* Access control for the health report — both the REST endpoint and the dashboard
|
|
43
|
+
* widget, which hides itself when this denies.
|
|
44
|
+
*
|
|
45
|
+
* Use it to restrict the collection-wide report more strictly than the
|
|
46
|
+
* per-document generate endpoints (e.g. to admins).
|
|
47
|
+
*
|
|
48
|
+
* @default the plugin's `access`
|
|
49
|
+
*/
|
|
50
|
+
access?: (args: {
|
|
51
|
+
req: PayloadRequest;
|
|
52
|
+
}) => boolean | Promise<boolean>;
|
|
53
|
+
/**
|
|
54
|
+
* Narrows what the report counts. See {@link AltTextHealthBaseFilter}.
|
|
55
|
+
*
|
|
56
|
+
* This is not access control: it scopes the aggregate, it does not decide who
|
|
57
|
+
* may see it. Use `access` for that, and note the report is always filtered to
|
|
58
|
+
* the collections the requesting user can read.
|
|
59
|
+
*/
|
|
60
|
+
baseFilter?: AltTextHealthBaseFilter;
|
|
61
|
+
};
|
|
21
62
|
/** Configuration options for the alt text plugin. */
|
|
22
63
|
export type IncomingAltTextPluginConfig = {
|
|
23
64
|
/**
|
|
@@ -65,19 +106,12 @@ export type IncomingAltTextPluginConfig = {
|
|
|
65
106
|
* Controls the alt text health feature (REST endpoint, cache revalidation hooks, and dashboard widget).
|
|
66
107
|
*
|
|
67
108
|
* - `false` disables the entire feature.
|
|
68
|
-
* - `true` enables it, gated by `access
|
|
69
|
-
* -
|
|
70
|
-
* with that access check — use this to restrict the collection-wide report
|
|
71
|
-
* more strictly than the per-document generate endpoints (e.g. to admins).
|
|
72
|
-
*
|
|
73
|
-
* Regardless of the gate, the report is always filtered to the collections the
|
|
74
|
-
* requesting user can read.
|
|
109
|
+
* - `true` enables it, gated by `access` and covering every document.
|
|
110
|
+
* - An object enables it and configures it. See {@link AltTextHealthCheckConfig}.
|
|
75
111
|
*
|
|
76
112
|
* @default true
|
|
77
113
|
*/
|
|
78
|
-
healthCheck?:
|
|
79
|
-
req: PayloadRequest;
|
|
80
|
-
}) => boolean | Promise<boolean>) | boolean;
|
|
114
|
+
healthCheck?: AltTextHealthCheckConfig | boolean;
|
|
81
115
|
/**
|
|
82
116
|
* The MIME type `getImageThumbnail` delivers, for every configured collection.
|
|
83
117
|
*
|
|
@@ -141,6 +175,8 @@ export type AltTextPluginConfig = {
|
|
|
141
175
|
healthCheckAccess: (args: {
|
|
142
176
|
req: PayloadRequest;
|
|
143
177
|
}) => boolean | Promise<boolean>;
|
|
178
|
+
/** Narrows what the health report counts. See {@link AltTextHealthBaseFilter}. */
|
|
179
|
+
healthCheckBaseFilter?: AltTextHealthBaseFilter;
|
|
144
180
|
/** The locale to generate alt texts in when localization is disabled. */
|
|
145
181
|
locale?: string;
|
|
146
182
|
/** The locales to generate alt texts for. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/types/AltTextPluginConfig.ts"],"sourcesContent":["import type { CollectionSlug, Field, PayloadRequest } from 'payload'\n\nimport type { AltTextResolver } from '../resolvers/types.js'\nimport type {\n AltTextCollectionConfig,\n IncomingCollectionsConfig,\n NormalizedAltTextCollectionConfig,\n} from '../utilities/mimeTypes.js'\n\nexport type { AltTextCollectionConfig, NormalizedAltTextCollectionConfig }\n\n/**\n * Builds the thumbnail URL the resolver fetches. Must be publicly reachable.\n *\n * May be async, so the URL can be signed on demand (S3 presigning, short-lived\n * CDN tokens).\n *\n * @param doc The upload document to build the URL for.\n * @param args.collection The slug of the collection `doc` belongs to — use it to\n * build different URLs per collection (e.g. a Cloudinary transformation for one\n * collection and a plain S3 URL for another).\n * @param args.req The request the generation runs under.\n */\nexport type GetImageThumbnail = (\n doc: Record<string, unknown>,\n args: { collection: CollectionSlug; req: PayloadRequest },\n) => Promise<string> | string\n\n/** Configuration options for the alt text plugin. */\nexport type IncomingAltTextPluginConfig = {\n /**\n * Custom access control for plugin endpoints.\n * Return `true` to allow access, `false` to deny.\n *\n * @default ({ req }) => !!req.user — requires authentication\n */\n access?: (args: { req: PayloadRequest }) => boolean | Promise<boolean>\n\n /**\n * Collections to enable the plugin for.\n *\n * Each entry may be a bare collection slug or an object with a `slug` and an\n * optional `mimeTypes` array restricting which MIME types are tracked,\n * validated, and generated. Bare slugs default to `['image/*']`.\n *\n * @example\n * ```typescript\n * collections: [\n * 'images', // shorthand — defaults to ['image/*']\n * { slug: 'media', mimeTypes: ['image/*', 'application/pdf'] },\n * ]\n * ```\n */\n collections: IncomingCollectionsConfig\n\n /** Whether the plugin is enabled. */\n enabled?: boolean\n\n /** Override the default fields inserted by the plugin via a function that receives the default fields and returns the new fields */\n fieldsOverride?: (args: { defaultFields: Field[] }) => Field[]\n\n /**\n * Builds the image URL sent to the resolver. See {@link GetImageThumbnail}.\n *\n * @remarks\n * - Prefer a thumbnail/preview size over the original (e.g. from the sizes field)\n * - When the URL transcodes, declare the delivered format via\n * `imageThumbnailMimeType` so source formats the resolver does not accept are\n * not rejected\n */\n getImageThumbnail: GetImageThumbnail\n\n /**\n * Controls the alt text health feature (REST endpoint, cache revalidation hooks, and dashboard widget).\n *\n * - `false` disables the entire feature.\n * - `true` enables it, gated by `access
|
|
1
|
+
{"version":3,"sources":["../../src/types/AltTextPluginConfig.ts"],"sourcesContent":["import type { CollectionSlug, Field, PayloadRequest, Where } from 'payload'\n\nimport type { AltTextResolver } from '../resolvers/types.js'\nimport type {\n AltTextCollectionConfig,\n IncomingCollectionsConfig,\n NormalizedAltTextCollectionConfig,\n} from '../utilities/mimeTypes.js'\n\nexport type { AltTextCollectionConfig, NormalizedAltTextCollectionConfig }\n\n/**\n * Builds the thumbnail URL the resolver fetches. Must be publicly reachable.\n *\n * May be async, so the URL can be signed on demand (S3 presigning, short-lived\n * CDN tokens).\n *\n * @param doc The upload document to build the URL for.\n * @param args.collection The slug of the collection `doc` belongs to — use it to\n * build different URLs per collection (e.g. a Cloudinary transformation for one\n * collection and a plain S3 URL for another).\n * @param args.req The request the generation runs under.\n */\nexport type GetImageThumbnail = (\n doc: Record<string, unknown>,\n args: { collection: CollectionSlug; req: PayloadRequest },\n) => Promise<string> | string\n\n/**\n * Narrows the health scan of one collection to a subset of its documents — in a\n * multi-tenant CMS, to the tenant the request is for. The returned constraint is\n * ANDed onto the scan's MIME type filter.\n *\n * Called once per configured collection, so collections that do not carry the\n * constraining field (a media library shared across tenants, say) can return `{}`\n * instead of being queried for a field they do not have.\n *\n * Return `{}` to scan the collection whole.\n *\n * @param args.collection The slug of the collection being scanned.\n * @param args.req The request the scan runs for.\n */\nexport type AltTextHealthBaseFilter = (args: {\n collection: CollectionSlug\n req: PayloadRequest\n}) => Promise<Where> | Where\n\n/** Configuration of the alt text health feature. */\nexport type AltTextHealthCheckConfig = {\n /**\n * Access control for the health report — both the REST endpoint and the dashboard\n * widget, which hides itself when this denies.\n *\n * Use it to restrict the collection-wide report more strictly than the\n * per-document generate endpoints (e.g. to admins).\n *\n * @default the plugin's `access`\n */\n access?: (args: { req: PayloadRequest }) => boolean | Promise<boolean>\n\n /**\n * Narrows what the report counts. See {@link AltTextHealthBaseFilter}.\n *\n * This is not access control: it scopes the aggregate, it does not decide who\n * may see it. Use `access` for that, and note the report is always filtered to\n * the collections the requesting user can read.\n */\n baseFilter?: AltTextHealthBaseFilter\n}\n\n/** Configuration options for the alt text plugin. */\nexport type IncomingAltTextPluginConfig = {\n /**\n * Custom access control for plugin endpoints.\n * Return `true` to allow access, `false` to deny.\n *\n * @default ({ req }) => !!req.user — requires authentication\n */\n access?: (args: { req: PayloadRequest }) => boolean | Promise<boolean>\n\n /**\n * Collections to enable the plugin for.\n *\n * Each entry may be a bare collection slug or an object with a `slug` and an\n * optional `mimeTypes` array restricting which MIME types are tracked,\n * validated, and generated. Bare slugs default to `['image/*']`.\n *\n * @example\n * ```typescript\n * collections: [\n * 'images', // shorthand — defaults to ['image/*']\n * { slug: 'media', mimeTypes: ['image/*', 'application/pdf'] },\n * ]\n * ```\n */\n collections: IncomingCollectionsConfig\n\n /** Whether the plugin is enabled. */\n enabled?: boolean\n\n /** Override the default fields inserted by the plugin via a function that receives the default fields and returns the new fields */\n fieldsOverride?: (args: { defaultFields: Field[] }) => Field[]\n\n /**\n * Builds the image URL sent to the resolver. See {@link GetImageThumbnail}.\n *\n * @remarks\n * - Prefer a thumbnail/preview size over the original (e.g. from the sizes field)\n * - When the URL transcodes, declare the delivered format via\n * `imageThumbnailMimeType` so source formats the resolver does not accept are\n * not rejected\n */\n getImageThumbnail: GetImageThumbnail\n\n /**\n * Controls the alt text health feature (REST endpoint, cache revalidation hooks, and dashboard widget).\n *\n * - `false` disables the entire feature.\n * - `true` enables it, gated by `access` and covering every document.\n * - An object enables it and configures it. See {@link AltTextHealthCheckConfig}.\n *\n * @default true\n */\n healthCheck?: AltTextHealthCheckConfig | boolean\n\n /**\n * The MIME type `getImageThumbnail` delivers, for every configured collection.\n *\n * Declaring it takes a document's stored `mimeType` out of the decision of\n * whether alt text can be generated. Collections may override it or opt out with\n * `null` — see {@link AltTextCollectionConfig.imageThumbnailMimeType} for the\n * rationale and the `f_auto` caveat.\n *\n * @example 'image/webp'\n */\n imageThumbnailMimeType?: string\n\n /**\n * The locale to generate alt texts in when localization is disabled.\n *\n * Required when localization is disabled, ignored when localization is enabled.\n * @example 'en'\n */\n locale?: string\n\n /**\n * Maximum number of concurrent API requests for bulk generate operations.\n *\n * @default 16\n */\n maxBulkGenerateConcurrency?: number\n\n /**\n * Maximum number of image IDs accepted in a single bulk generate request.\n * Requests exceeding this are rejected with `400`. Duplicate IDs are collapsed\n * before the limit is applied, so each image counts once.\n *\n * Raise it for large libraries that need to process more images per request.\n *\n * @default 100\n */\n maxBulkGenerateIds?: number\n\n /** The resolver to use for generating alt text (e.g., openAIResolver) */\n resolver: AltTextResolver\n}\n\n/** Configuration of the alt text plugin after defaults have been applied. */\nexport type AltTextPluginConfig = {\n /** Access control for plugin endpoints. */\n access: (args: { req: PayloadRequest }) => boolean | Promise<boolean>\n\n /**\n * Collections with resolved MIME type filters and resolved delivered thumbnail\n * MIME types. The plugin-level `imageThumbnailMimeType` is folded into these\n * entries during normalization, so this is the only place to read it from.\n */\n collections: NormalizedAltTextCollectionConfig[]\n\n /** Whether the plugin is enabled. */\n enabled: boolean\n\n /** Override the default fields inserted by the plugin via a function that receives the default fields and returns the new fields */\n fieldsOverride?: (args: { defaultFields: Field[] }) => Field[]\n\n /** Function to get the thumbnail URL of an image document. */\n getImageThumbnail: GetImageThumbnail\n\n /** Whether alt text health tracking is enabled. */\n healthCheck: boolean\n\n /** Access control for the health endpoint. Defaults to `access`. */\n healthCheckAccess: (args: { req: PayloadRequest }) => boolean | Promise<boolean>\n\n /** Narrows what the health report counts. See {@link AltTextHealthBaseFilter}. */\n healthCheckBaseFilter?: AltTextHealthBaseFilter\n\n /** The locale to generate alt texts in when localization is disabled. */\n locale?: string\n\n /** The locales to generate alt texts for. */\n locales: string[]\n\n /** Maximum number of concurrent API requests for bulk generate operations. */\n maxBulkGenerateConcurrency: number\n\n /** Maximum number of image IDs accepted per bulk generate request. */\n maxBulkGenerateIds: number\n\n /** The resolver to use for generating alt text */\n resolver: AltTextResolver\n}\n"],"names":[],"mappings":"AAuKA,2EAA2E,GAC3E,WA2CC"}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { PayloadRequest } from 'payload';
|
|
2
|
+
import type { AltTextHealthCacheFactory } from './altTextHealthCache.js';
|
|
2
3
|
export declare const ALT_TEXT_HEALTH_PLUGIN_SLUG = "alt-text";
|
|
3
4
|
export declare const ALT_TEXT_HEALTH_CACHE_TTL = 3600;
|
|
4
5
|
export declare const ALT_TEXT_HEALTH_GLOBAL_TAG = "alt-text-health";
|
|
5
|
-
export type AltTextHealthErrorCode = 'ALT_TEXT_COLLECTION_READ_FAILED' | 'ALT_TEXT_PLUGIN_CONFIG_MISSING';
|
|
6
|
+
export type AltTextHealthErrorCode = 'ALT_TEXT_BASE_FILTER_FAILED' | 'ALT_TEXT_COLLECTION_READ_FAILED' | 'ALT_TEXT_PLUGIN_CONFIG_MISSING';
|
|
6
7
|
export type AltTextHealthError = {
|
|
7
8
|
code: AltTextHealthErrorCode;
|
|
8
9
|
collection?: string;
|
|
@@ -33,6 +34,7 @@ export type AltTextHealthWidgetData = {
|
|
|
33
34
|
totalDocs: number;
|
|
34
35
|
};
|
|
35
36
|
export declare const getAltTextHealthCollectionTag: (collectionSlug: string) => string;
|
|
37
|
+
export declare function getAltTextHealthScan(req: PayloadRequest, cacheFactory?: AltTextHealthCacheFactory<AltTextHealthScan>): Promise<AltTextHealthScan>;
|
|
36
38
|
/**
|
|
37
39
|
* Filters a shared, elevated-access health scan down to the collections the
|
|
38
40
|
* requesting user may read. The scan is computed once with `overrideAccess: true`
|
|
@@ -2,6 +2,7 @@ import { unstable_cache } from 'next/cache.js';
|
|
|
2
2
|
import { createCachedAltTextHealthScan } from './altTextHealthCache.js';
|
|
3
3
|
import { localesFromConfig } from './localesFromConfig.js';
|
|
4
4
|
import { buildMimeTypeWhere } from './mimeTypes.js';
|
|
5
|
+
import { stableStringify } from './stableStringify.js';
|
|
5
6
|
import { summarizeCollection } from './summarizeCollection.js';
|
|
6
7
|
export const ALT_TEXT_HEALTH_PLUGIN_SLUG = 'alt-text';
|
|
7
8
|
export const ALT_TEXT_HEALTH_CACHE_TTL = 3600;
|
|
@@ -22,11 +23,20 @@ const createCollectionReadError = (collection, message)=>({
|
|
|
22
23
|
operation: 'find'
|
|
23
24
|
});
|
|
24
25
|
const PAGE_SIZE = 500;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
const isEmptyWhere = (where)=>!where || Object.keys(where).length === 0;
|
|
27
|
+
async function fetchAllDocs(payload, collection, isLocalized, mimeTypes, baseFilter) {
|
|
28
|
+
const mimeTypeWhere = buildMimeTypeWhere(mimeTypes);
|
|
29
|
+
if (!mimeTypeWhere) {
|
|
28
30
|
return [];
|
|
29
31
|
}
|
|
32
|
+
// The scan runs with `overrideAccess: true`, so a narrowing constraint has to be
|
|
33
|
+
// part of the query itself — this is what keeps the aggregate within one tenant.
|
|
34
|
+
const where = isEmptyWhere(baseFilter) ? mimeTypeWhere : {
|
|
35
|
+
and: [
|
|
36
|
+
mimeTypeWhere,
|
|
37
|
+
baseFilter
|
|
38
|
+
]
|
|
39
|
+
};
|
|
30
40
|
const docs = [];
|
|
31
41
|
let page = 1;
|
|
32
42
|
let hasMore = true;
|
|
@@ -55,10 +65,10 @@ async function fetchAllDocs(payload, collection, isLocalized, mimeTypes) {
|
|
|
55
65
|
}
|
|
56
66
|
return docs;
|
|
57
67
|
}
|
|
58
|
-
async function computeAltTextHealthScan({ collections, isLocalized, localeCodes, payload }) {
|
|
68
|
+
async function computeAltTextHealthScan({ baseFilters, collections, isLocalized, localeCodes, payload }) {
|
|
59
69
|
const collectionSummaries = await Promise.all(collections.map(async ({ slug, mimeTypes })=>{
|
|
60
70
|
try {
|
|
61
|
-
const docs = await fetchAllDocs(payload, slug, isLocalized, mimeTypes);
|
|
71
|
+
const docs = await fetchAllDocs(payload, slug, isLocalized, mimeTypes, baseFilters[slug]);
|
|
62
72
|
return summarizeCollection({
|
|
63
73
|
collection: slug,
|
|
64
74
|
docs,
|
|
@@ -96,7 +106,48 @@ async function computeAltTextHealthScan({ collections, isLocalized, localeCodes,
|
|
|
96
106
|
};
|
|
97
107
|
}
|
|
98
108
|
export const getAltTextHealthCollectionTag = (collectionSlug)=>`${ALT_TEXT_HEALTH_GLOBAL_TAG}:${collectionSlug}`;
|
|
99
|
-
|
|
109
|
+
/**
|
|
110
|
+
* Resolves the configured base filter for every scanned collection.
|
|
111
|
+
*
|
|
112
|
+
* A throwing filter (a tenant cookie pointing at a deleted tenant, say) must not
|
|
113
|
+
* take the dashboard down, and must never fall back to an unfiltered scan — so it
|
|
114
|
+
* ends the scan with an error the widget and the endpoint report. The error carries
|
|
115
|
+
* the slug in its message rather than in `collection`, so it survives the read-access
|
|
116
|
+
* filter that drops errors for collections the caller cannot read.
|
|
117
|
+
*/ async function resolveBaseFilters(req, collections, baseFilter) {
|
|
118
|
+
const baseFilters = {};
|
|
119
|
+
if (!baseFilter) {
|
|
120
|
+
return {
|
|
121
|
+
baseFilters
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
for (const { slug } of collections){
|
|
125
|
+
try {
|
|
126
|
+
baseFilters[slug] = await baseFilter({
|
|
127
|
+
collection: slug,
|
|
128
|
+
req
|
|
129
|
+
});
|
|
130
|
+
} catch (error) {
|
|
131
|
+
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
132
|
+
req.payload.logger.error({
|
|
133
|
+
collection: slug,
|
|
134
|
+
err: error,
|
|
135
|
+
msg: 'Alt text health check failed while resolving the base filter.',
|
|
136
|
+
plugin: ALT_TEXT_HEALTH_PLUGIN_SLUG
|
|
137
|
+
});
|
|
138
|
+
return {
|
|
139
|
+
error: {
|
|
140
|
+
code: 'ALT_TEXT_BASE_FILTER_FAILED',
|
|
141
|
+
message: `Failed to resolve the alt text health base filter for the "${slug}" collection: ${message}`
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
baseFilters
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
export async function getAltTextHealthScan(req, cacheFactory = unstable_cache) {
|
|
100
151
|
const { payload } = req;
|
|
101
152
|
const pluginConfig = payload.config.custom?.altTextPluginConfig;
|
|
102
153
|
const localeCodes = localesFromConfig(payload.config) ?? (pluginConfig?.locale ? [
|
|
@@ -114,6 +165,15 @@ async function getAltTextHealthScan(req) {
|
|
|
114
165
|
});
|
|
115
166
|
}
|
|
116
167
|
const collections = pluginConfig.collections;
|
|
168
|
+
const resolved = await resolveBaseFilters(req, collections, pluginConfig.healthCheckBaseFilter);
|
|
169
|
+
if ('error' in resolved) {
|
|
170
|
+
return createUnknownScan({
|
|
171
|
+
error: resolved.error,
|
|
172
|
+
isLocalized,
|
|
173
|
+
localeCodes
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
const { baseFilters } = resolved;
|
|
117
177
|
const cacheKeyParts = [
|
|
118
178
|
ALT_TEXT_HEALTH_GLOBAL_TAG,
|
|
119
179
|
[
|
|
@@ -121,16 +181,22 @@ async function getAltTextHealthScan(req) {
|
|
|
121
181
|
].map(({ slug, mimeTypes })=>`${slug}:${[
|
|
122
182
|
...mimeTypes
|
|
123
183
|
].sort().join('|')}`).sort().join(','),
|
|
124
|
-
localeCodes.join(',')
|
|
184
|
+
localeCodes.join(','),
|
|
185
|
+
// The scan is shared across requests, so a scoped scan needs a scoped cache
|
|
186
|
+
// entry. Deriving the key from the resolved filters — rather than taking one
|
|
187
|
+
// from the caller — makes it impossible to narrow the scan without also
|
|
188
|
+
// narrowing its cache, which would serve one tenant's counts to another.
|
|
189
|
+
`filter:${stableStringify(baseFilters)}`
|
|
125
190
|
];
|
|
126
191
|
const tags = [
|
|
127
192
|
ALT_TEXT_HEALTH_GLOBAL_TAG,
|
|
128
193
|
...new Set(collections.map(({ slug })=>getAltTextHealthCollectionTag(slug)))
|
|
129
194
|
];
|
|
130
195
|
const getCachedHealthScan = createCachedAltTextHealthScan({
|
|
131
|
-
cacheFactory
|
|
196
|
+
cacheFactory,
|
|
132
197
|
cacheKeyParts,
|
|
133
198
|
compute: async ()=>computeAltTextHealthScan({
|
|
199
|
+
baseFilters,
|
|
134
200
|
collections,
|
|
135
201
|
isLocalized,
|
|
136
202
|
localeCodes,
|