mvriu5

payload ai plugin

by mvriu5
AI content generation
v3
^3.84.1

Overview

Summary generated from the README

Adds an AI assistant to the Payload admin dashboard and to document edit views. It reads CMS context, supports mentions, and proposes create, update, delete and global changes that are signed and checked against Payload access control before applying. Providers include OpenAI, Anthropic, Google, Mistral and OpenRouter, and the matching provider SDK must be installed. PAYLOAD_SECRET is required for signing.

Features

  • Signed, short-lived action proposals for create, update, delete and global updates
  • Per-collection read, create, update and delete permissions for the AI
  • Central provider profiles or user-selected provider and API key, with custom base URLs such as Ollama
  • Generate controls on text, textarea, richText and json fields
  • Document translation that fills only empty localized fields without saving
  • Token usage limits per user or site, plus prompt caching and media upload support

Use cases

  • Let editors draft and update content through a chat assistant inside the admin
  • Translate empty localized fields from the default locale into other locales for review
  • Run a self-hosted model through an OpenAI-compatible endpoint with centrally managed credentials

From the author

AI assistant plugin for Payload CMS with provider selection, CMS mentions, and signed action proposals.

Installation

npm i @mvriu5/payload-ai

4

Stars

0

Forks

6

Open Issues

3 weeks ago

Last Updated

288

Weekly Downloads

v1.6.4

npm Version

514.3 KB

Unpacked Size

2

Dependencies

GitHub stars

40 since Sep 6

Show data
GitHub stars by day (UTC)
DateStars
Dec 5, 20264
Dec 4, 20264
Dec 3, 20264
Dec 2, 20264
Dec 1, 20264
Nov 30, 20264
Nov 29, 20264
Nov 28, 20264
Nov 27, 20264
Nov 26, 20264
Nov 25, 20264
Nov 24, 20264
Nov 23, 20264
Nov 22, 20264
Nov 21, 20264
Nov 20, 20264
Nov 19, 20264
Nov 18, 20264
Nov 17, 20264
Nov 16, 20264
Nov 15, 20264
Nov 14, 20264
Nov 13, 20264
Nov 12, 20264
Nov 11, 20264
Nov 10, 20264
Nov 9, 20264
Nov 8, 20264
Nov 7, 20264
Nov 6, 20264
Nov 5, 20264
Nov 4, 20264
Nov 3, 20264
Nov 2, 20264
Nov 1, 20264
Oct 31, 20264
Oct 30, 20264
Oct 29, 20264
Oct 28, 20264
Oct 27, 20264
Oct 26, 20264
Oct 25, 20264
Oct 24, 20264
Oct 23, 20264
Oct 22, 20264
Oct 21, 20264
Oct 20, 20264
Oct 19, 20264
Oct 18, 20264
Oct 17, 20264
Oct 16, 20264
Oct 15, 20264
Oct 14, 20264
Oct 13, 20264
Oct 12, 20264
Oct 11, 20264
Oct 10, 20264
Oct 9, 20264
Oct 8, 20264
Oct 7, 20264
Oct 6, 20264
Oct 5, 20264
Oct 4, 20264
Oct 3, 20264
Oct 2, 20264
Oct 1, 20264
Sep 30, 20264
Sep 29, 20264
Sep 28, 20264
Sep 27, 20264
Sep 26, 20264
Sep 25, 20264
Sep 24, 20264
Sep 23, 20264
Sep 22, 20264
Sep 21, 20264
Sep 20, 20264
Sep 19, 20264
Sep 18, 20264
Sep 17, 20264
Sep 16, 20264
Sep 15, 20264
Sep 14, 20264
Sep 13, 20264
Sep 12, 20264
Sep 11, 20264
Sep 10, 20264
Sep 9, 20264
Sep 8, 20264
Sep 7, 20264
Sep 6, 20264
npm weekly downloads

251+141 since Sep 6

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 5, 2026251
Dec 4, 2026198
Dec 3, 2026154
Dec 2, 202675
Dec 1, 2026144
Nov 30, 2026153
Nov 29, 2026147
Nov 28, 2026142
Nov 27, 2026140
Nov 26, 2026144
Nov 25, 2026167
Nov 24, 2026125
Nov 23, 2026168
Nov 22, 2026167
Nov 21, 2026201
Nov 20, 2026200
Nov 19, 2026201
Nov 18, 2026197
Nov 17, 2026176
Nov 16, 2026136
Nov 15, 2026189
Nov 14, 2026169
Nov 13, 2026162
Nov 12, 2026156
Nov 11, 2026134
Nov 10, 2026154
Nov 9, 2026137
Nov 8, 202695
Nov 7, 202692
Nov 6, 2026105
Nov 5, 2026112
Nov 4, 2026113
Nov 3, 202694
Nov 2, 2026104
Nov 1, 202695
Oct 31, 202692
Oct 30, 202688
Oct 29, 202674
Oct 28, 202665
Oct 27, 202664
Oct 26, 202661
Oct 25, 202662
Oct 24, 202662
Oct 23, 202650
Oct 22, 202671
Oct 21, 202688
Oct 20, 202685
Oct 19, 202693
Oct 18, 2026127
Oct 17, 2026113
Oct 16, 2026128
Oct 15, 2026137
Oct 14, 2026147
Oct 13, 2026183
Oct 12, 2026163
Oct 11, 2026144
Oct 10, 2026184
Oct 9, 2026187
Oct 8, 2026163
Oct 7, 2026140
Oct 6, 2026112
Oct 5, 2026134
Oct 4, 2026123
Oct 3, 202690
Oct 2, 202688
Oct 1, 202689
Sep 30, 202693
Sep 29, 202692
Sep 28, 202683
Sep 27, 202676
Sep 26, 202696
Sep 25, 2026102
Sep 24, 2026109
Sep 23, 2026116
Sep 22, 2026117
Sep 21, 2026135
Sep 20, 2026145
Sep 19, 2026134
Sep 18, 2026138
Sep 17, 2026139
Sep 16, 2026136
Sep 15, 2026137
Sep 14, 2026119
Sep 13, 2026118
Sep 12, 2026143
Sep 11, 2026126
Sep 10, 2026122
Sep 9, 2026125
Sep 8, 2026120
Sep 7, 2026115
Sep 6, 2026110

Each point is npm's rolling 7-day download count.

Health Score

54/100Good

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/1255332434-root.svg)](https://payloaddirectory.dev/plugins/1255332434-root)
HTML
<a href="https://payloaddirectory.dev/plugins/1255332434-root"><img src="https://payloaddirectory.dev/badge/1255332434-root.svg" alt="Payload Directory"></a>
Updated Nov 9, 2026Created May 31, 2026Published Jul 30, 2026

README

payload-ai-plugin

AI assistant plugin for Payload CMS. It adds an admin dashboard assistant that can read CMS context, use mentions, and create signed action proposals for create, update, delete, and global updates.

Install

npm add @mvriu5/payload-ai @ai-sdk/openai

Install only the provider SDKs you actually use:

npm add @mvriu5/payload-ai @ai-sdk/anthropic
npm add @mvriu5/payload-ai @ai-sdk/google
npm add @mvriu5/payload-ai @ai-sdk/mistral

OpenRouter uses the community OpenRouter provider:

npm add @mvriu5/payload-ai @openrouter/ai-sdk-provider

Usage

import { buildConfig } from "payload"
import { payloadAiPlugin } from "@mvriu5/payload-ai"

export default buildConfig({
    plugins: [
        payloadAiPlugin({
            collections: {
                posts: true,
            },
        }),
    ],
})

Without centrally configured providers, the plugin adds two fields to the configured Payload admin user collection:

  • aiProvider
  • aiApiKey

Users can select their provider and optionally store their own API key in account settings. If no account-level key is set, the chat endpoint uses provider environment variables.

When providers is configured, provider selection and API keys are managed centrally. The plugin does not add either field to the user collection. All configured provider models are grouped by provider in the AI input.

Options

import type { PayloadAIPluginOptions } from "@mvriu5/payload-ai"

const options: PayloadAIPluginOptions = {
    aiInput: true,
    allowUserApiKeys: false,
    authCollections: {
        aiInput: false,
        generateFields: false,
    },
    collections: {
        media: {
            read: true,
            update: true,
        },
        posts: {
            read: true,
            create: true,
            update: true,
            delete: false,
        },
        users: true,
    },
    generateFields: true,
    media: {
        enabled: true,
        collectionSlug: "media",
        acceptedMimeTypes: ["image/*"],
        maxFileSize: 10 * 1024 * 1024,
    },
    providers: [
        {
            id: "company-openai",
            label: "Company OpenAI",
            provider: "openai",
            apiKey: process.env.COMPANY_OPENAI_API_KEY,
            models: [
                { label: "GPT-4.1 Mini", value: "gpt-4.1-mini" },
                { label: "GPT-4.1", value: "gpt-4.1" },
            ],
            defaultModel: "gpt-4.1-mini",
        },
        {
            id: "ollama",
            label: "Local Ollama",
            provider: "openai",
            baseURL: "http://localhost:11434/v1",
            apiKey: "ollama",
            models: [
                { label: "Llama 3.3", value: "llama3.3" },
                { label: "Qwen 3", value: "qwen3" },
            ],
        },
    ],
    maxOutputTokens: 1200,
    promptCaching: true,
    translate: true,
    uploadCollections: {
        aiInput: true,
        generateFields: false,
    },
    maxTokenUsage: {
        type: "user",
        perDay: 50_000,
        perWeek: 250_000,
    },
}

models configures model choices for the user-selected provider mode. Use providers instead when provider selection, credentials, and endpoints should be managed centrally.

aiInput

Controls whether the collapsible AI Assistant input is added to collection and global edit views. It defaults to true.

payloadAiPlugin({
    aiInput: false,
})

Disabling this option does not remove the AI dashboard or per-field Generate controls.

generateFields

Controls whether supported fields receive a Generate control. It defaults to true.

payloadAiPlugin({
    generateFields: false,
})

Generate controls are added to text, textarea, richText, and json fields in collection and global edit views. When disabled, the plugin also omits the /ai-generate-field endpoint. The embedded AI Assistant and dashboard remain available.

authCollections and uploadCollections

Auth and upload collections do not receive the embedded AI Assistant or Generate controls by default. Enable either feature explicitly for the respective collection type:

payloadAiPlugin({
    authCollections: {
        aiInput: true,
        generateFields: true,
    },
    uploadCollections: {
        aiInput: true,
        generateFields: true,
    },
})

The global aiInput and generateFields options still take precedence when set to false.

translate

Controls document translation for localized collections and globals. It defaults to true.

payloadAiPlugin({
    translate: false,
})

When enabled, the plugin adds a Translate action to the document header. It is shown only when:

  • a non-default locale is selected
  • at least one localized field is empty in that locale
  • the corresponding field in the default locale contains source content

Translation fills only empty localized fields in the current Payload form and marks it as modified. Existing translated values are never overwritten. It does not save the document or global, so the generated values can be reviewed and validated before saving.

When disabled, the plugin does not register the header control or the /ai-translate-document endpoint.

collections

Restricts AI read and write proposals to enabled collection slugs. If omitted, all non-internal Payload collections are available.

Use true to enable all AI actions for a collection:

payloadAiPlugin({
    collections: {
        posts: true,
    },
})

Use granular permissions to control each action:

payloadAiPlugin({
    collections: {
        posts: {
            read: true,
            create: true,
            update: true,
            delete: false,
        },
    },
})

You can mix both forms in the same object:

payloadAiPlugin({
    collections: {
        posts: true,
        pages: true,
        users: {
            read: true,
            update: true,
        },
    },
})

read controls schema/context access, document search, and mentions. create, update, and delete control AI action proposals and server-side apply permissions.

media

Enables media uploads from the AI assistant. Uploaded files are created through the configured Payload upload collection and then passed to the chat endpoint as media attachments.

payloadAiPlugin({
    collections: {
        media: {
            read: true,
            update: true,
        },
        posts: true,
    },
    media: {
        enabled: true,
        collectionSlug: "media",
        acceptedMimeTypes: ["image/*"],
        maxFileSize: 10 * 1024 * 1024,
    },
})

collectionSlug defaults to "media". The target collection must be configured with Payload upload support.

acceptedMimeTypes accepts exact MIME types such as "image/png" and wildcard groups such as "image/*". If omitted, all file types accepted by the upload collection can be sent to the endpoint.

maxFileSize is checked before creating the upload document. The value is in bytes.

If you want the AI to use uploaded files in other documents, enable read on the media collection so the chat endpoint can validate and inspect the uploaded media document. If the media collection has editable fields such as alt, caption, or credit and you want the AI to fill them, also enable update for that collection.

Upload references in AI proposals are restricted to the uploaded attachments for the current request. This prevents the model from inventing arbitrary media IDs for upload fields.

models

Overrides the model list shown in the admin UI and the default model per provider.

Built-in providers are openai, openrouter, claude, mistral, and google.

The built-in model selection was checked on October 6, 2026 against the official OpenAI, Anthropic, Google, Mistral, and OpenRouter catalogs. It includes a broad selection of current and older text models for the streaming tool loop. Preview and deprecated direct-provider models are labeled explicitly and are not defaults.

The defaults are gpt-6-luna, claude-haiku-4-5, gemini-3.8-flash, mistral-small-2603, and openrouter/auto. Each provider's first model is used unless you configure a different default.

The built-in options cover these model families:

  • OpenAI: GPT-6, GPT-5.6, GPT-5.5, GPT-5.4, GPT-5.3 Codex, GPT-5.2, GPT-5.1, GPT-5, GPT-4.1, GPT-4o, o3, o3 Mini, o4 Mini, and o1.
  • Claude: Haiku 4.5, Sonnet 4.5–5.5, Opus 4.5–5.5, and Fable 5/5.1.
  • Google: Gemini 2.5, Gemini 3 Flash, Gemini 3.1 Pro/Flash Lite, and Gemini 3.5–3.8 Flash variants.
  • Mistral: Small, Medium, Large, Ministral, Codestral, and older Pixtral, Nemo, Magistral, Devstral, and Small Creative models.
  • OpenRouter: openrouter/auto, current and older OpenAI/Claude/Gemini/Mistral models, GPT-OSS, DeepSeek, Qwen, Grok, Llama, GLM, Kimi, and MiniMax.

The exact model IDs are defined in src/features/providers/options.ts. This is a static selection, not an automatically synchronized catalog. Models known to be retired on the direct APIs are excluded; OpenRouter is checked separately because it may still serve older models through other providers. Its listed models advertise support for both tools and tool_choice.

Deprecated models are retained as optional choices for existing integrations and may be removed by their provider. Availability also depends on your account: for example, Gemini 2.5 is restricted to existing users. Live inference is not verified by the model list.

providers

Enables centrally managed provider profiles. Each profile supports:

  • id: unique provider profile identifier
  • label: provider group label shown in the model select
  • provider: SDK adapter (openai, openrouter, claude, mistral, or google)
  • models: allowed model list
  • defaultModel: optional default; otherwise the first configured model is used
  • baseURL: optional custom provider URL
  • apiKey: optional server-side API key

When at least one profile is configured:

  • aiProvider and aiApiKey are not added to the admin user collection.
  • Models from every configured profile are displayed in provider groups.
  • The chat endpoint validates both provider IDs and model IDs against this configuration.
  • User-level provider and API key values are ignored.

apiKey and baseURL remain server-side and are not included in Payload's public admin configuration. Custom endpoints must implement the protocol expected by their selected SDK adapter. For Ollama, vLLM, and similar APIs, use the openai adapter with an OpenAI-compatible /v1 endpoint.

maxOutputTokens

Controls the maximum number of output tokens the chat endpoint may generate per request. If omitted, the plugin uses 700.

payloadAiPlugin({
    maxOutputTokens: 1200,
})

maxTokenUsage

Limits total AI tokens across rolling 24-hour and 7-day windows.

payloadAiPlugin({
    maxTokenUsage: {
        type: "user",
        perDay: 50_000,
        perWeek: 250_000,
    },
})

Use type: "user" to enforce separate budgets per authenticated user, or type: "site" to share one budget across the entire Payload installation. perDay and perWeek are optional individually, but at least one must be configured.

Completed model usage is stored in the hidden payload-ai-usage collection. Requests made after a limit is reached return HTTP 429. Because providers report token usage after completion, the request that crosses a limit is allowed to finish and subsequent requests are blocked.

promptCaching

Enables provider prompt-caching hints and defaults to true. The chat endpoint keeps static instructions and Payload schemas in a stable prompt prefix, adds Anthropic cache-control breakpoints, and supplies a stable OpenAI prompt cache key. Gemini uses the same stable prefix with its implicit caching.

payloadAiPlugin({
    promptCaching: false,
})

Disable it when explicit provider-side caching is not desired. Dynamic document data and user prompts are never placed in the cacheable schema section. Managed providers with a custom baseURL receive the stable prefix but no provider-specific cache parameters, preserving compatibility with Ollama, vLLM, and other OpenAI-compatible endpoints.

allowUserApiKeys

Controls whether the plugin adds an aiApiKey field to the admin user collection.

payloadAiPlugin({
    allowUserApiKeys: false,
})

When disabled, users can still select an AI provider, but API keys must come from environment variables.

This option only applies when providers is not configured. Managed provider mode never adds user-level AI settings.

disabled

Disables endpoint and UI registration while keeping the plugin call in your config.

Provider Environment Variables

The package lazy-loads provider SDKs at runtime. If a user selects claude, google, mistral, openai, or openrouter, the matching provider package must be installed in the host app. openrouter uses @openrouter/ai-sdk-provider.

API key priority is:

  1. managed provider apiKey, when providers is configured
  2. account-level API key, unless allowUserApiKeys: false
  3. provider environment variables
  • OPENAI_API_KEY, OPENAI_MODEL
  • OPENROUTER_API_KEY, OPENROUTER_MODEL
  • ANTHROPIC_API_KEY, ANTHROPIC_MODEL
  • GOOGLE_GENERATIVE_AI_API_KEY, GOOGLE_GENERATIVE_AI_MODEL
  • MISTRAL_API_KEY, MISTRAL_MODEL

PAYLOAD_SECRET is required for signing AI action proposals.

Security

AI write operations are proposal-based. The chat endpoint signs every proposal with an HMAC signature and a short TTL. The apply endpoint verifies the signature, validates the target collection/global, enforces Payload access control with overrideAccess: false, and rejects proposals containing sensitive API-key-like fields.

The apply endpoint returns only minimal status/doc references and does not return normalized data, proposal payloads, API keys, or raw error details to the client.

Exports

import { payloadAiPlugin } from "@mvriu5/payload-ai"
import type { PayloadAiPluginOptions } from "@mvriu5/payload-ai"

Client components are exported through:

import { AIInput, AIApiKeyField } from "@mvriu5/payload-ai/client"

Similar plugins