import type { GenerateDescription } from '@payloadcms/plugin-seo/types' import { convertLexicalToPlaintext } from '@payloadcms/richtext-lexical/plaintext' // Writes a page's or post's meta description with exe.dev's LLM gateway: // no API key on the VM; calls count against your exe.dev plan. // https://exe.dev/docs/integrations-llm // From https://exe.dev/deploy/payload const GATEWAY = 'https://llm.int.exe.xyz/v1/chat/completions' const MODEL = process.env.LLM_MODEL || 'gpt-5.4-mini' export const generateDescription: GenerateDescription = async ({ doc }) => { const rich = doc?.content ?? doc?.hero?.richText // posts, pages const text = [doc?.title, rich ? convertLexicalToPlaintext({ data: rich }) : ''] .filter(Boolean) .join('\n\n') .slice(0, 12000) if (!text) return '' const res = await fetch(GATEWAY, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ model: MODEL, messages: [ { role: 'system', content: 'Write the meta description for this web page: one or two plain sentences, ' + '100 to 150 characters, no quotes, no hashtags. Reply with the description only.', }, { role: 'user', content: text }, ], }), }) if (!res.ok) throw new Error(`LLM gateway: ${res.status} ${await res.text()}`) const data = await res.json() return String(data.choices?.[0]?.message?.content ?? '').trim() }