@ai-stack/payloadcms

中文文档
4.0.1发布于 2026/10/14MITTypeScriptES Module构建来源已验证近 30 天下载 1万

Upgrading to 4.0.0? Read the migration guide. Reference images and extracted prompt attachments now require an explicit storage resolver.

原始使用文档
文档目录20 节
  1. 🚀 What is this?
  2. 🎥 See It in Action
  3. ⚠️ Beta Notice
  4. ✨ Features
  5. 📝 Text & RichText Fields
  6. 🎨 Upload Fields
  7. 🔧 Power User Features
  8. 🔜 Coming Soon
  9. 📦 Installation
  10. 🛠️ Quick Setup
  11. Step 1: Configure the Plugin
  12. Step 2: Enable AI in Your Fields
  13. Step 3: Add Your API Keys
  14. ⚙️ Advanced Configuration
  15. Reference images and storage
  16. 📚 Documentation
  17. 🤝 Support This Project
  18. 👥 Contributing
  19. Ways to Contribute
  20. Local Development

Payload AI Plugin

Upgrading to 4.0.0? Read the migration guide. Reference images and extracted prompt attachments now require an explicit storage resolver.

Payload AI Plugin

Transform content creation with intelligent automation — your models, your way

Supported AI Providers


🚀 What is this?

The Payload AI Plugin is your secret weapon for turbocharged content creation. It seamlessly integrates cutting-edge AI capabilities directly into Payload CMS, turning your content workflow from tedious to tremendous.

🎥 See It in Action


⚠️ Beta Notice

This plugin is actively evolving. We're constantly shipping improvements and new features. Tested with Payload v3.88.0.

Quick Start Tip: Try it out with Payload's website template for the smoothest experience.


✨ Features

📝 Text & RichText Fields

Content Generation Magic:

  • ✅ Compose - Generate content from scratch
  • ✅ Proofread - Polish your prose (Beta)
  • ✅ Translate - Break language barriers
  • ✅ Rephrase - Find better ways to say it (Beta)
  • 🔜 Expand - Elaborate on ideas
  • 🔜 Summarize - Distill the essence
  • 🔜 Simplify - Make complex things clear

🎨 Upload Fields

  • 🎙️ Voice Generation - Powered by ElevenLabs & OpenAI
  • 🖼️ Image Generation - Powered by OpenAI (DALL-E & GPT-Image-1)

🔧 Power User Features

  • 🔌 Bring Your Own Model - Not limited to our defaults
  • 🎛️ Field-Level Prompts - Customize AI behavior per field
  • 🔐 Access Control - Lock down who can use AI features
  • 🧠 Prompt Editor - Fine-tune AI instructions
  • 🌍 i18n Support - Works with your multilingual setup
  • 🎨 Custom Components - Extend with your own UI

🔜 Coming Soon

  • 📊 Document Analyzer
  • ✅ Fact Checking
  • 🔄 Automated Workflows
  • 💡 Editor Suggestions
  • 💬 AI Chat Assistant

📦 Installation

pnpm add @ai-stack/payloadcms

That's it! Now let's configure it.


🛠️ Quick Setup

Step 1: Configure the Plugin

Add to src/payload.config.ts:

import { payloadAiPlugin } from '@ai-stack/payloadcms'

export default buildConfig({
  plugins: [
    payloadAiPlugin({
      collections: {
        [Posts.slug]: true,
      },
      debugging: false,
    }),
  ],
  // ... rest of your config
})

Step 2: Enable AI in Your Fields

Add to your RichText fields (e.g., src/collections/Posts/index.ts):

import { PayloadAiPluginLexicalEditorFeature } from '@ai-stack/payloadcms'

fields: [
  {
    name: 'content',
    type: 'richText',
    editor: lexicalEditor({
      features: ({ rootFeatures }) => {
        return [
          HeadingFeature({ enabledHeadingSizes: ['h1', 'h2', 'h3', 'h4'] }),
          // Add this line:
          PayloadAiPluginLexicalEditorFeature(),
        ]
      },
    }),
  },
]

Step 3: Add Your API Keys

Payload AI can read provider credentials in two ways:

  1. Environment variables.
  2. Plugin config, useful when credentials are resolved before Payload starts, such as per-instance multi-tenant setups, external secret managers, or tests.

Option A: Environment Variables

Create a .env file in your project root. Add the keys for the providers you want to use:

# Text Generation - Choose your provider(s)
OPENAI_API_KEY=your-openai-api-key           # OpenAI models (GPT-4, etc.)
ANTHROPIC_API_KEY=your-anthropic-api-key     # Claude models
GOOGLE_GENERATIVE_AI_API_KEY=your-google-key # Gemini models
MINIMAX_API_KEY=your-minimax-api-key         # MiniMax M3 / M2.7 models

# Image Generation - Choose your provider(s)
OPENAI_API_KEY=your-openai-api-key           # DALL-E (uses same key as above)
# OPENAI_ORG_ID=your-org-id                  # Required only for GPT-Image-1 model
GOOGLE_GENERATIVE_AI_API_KEY=your-google-key # Imagen (uses same key as above)

# Audio/Voice Generation - Choose your provider(s)
ELEVENLABS_API_KEY=your-elevenlabs-api-key   # ElevenLabs voices
OPENAI_API_KEY=your-openai-api-key           # OpenAI TTS (uses same key as above)

# Optional: Use custom OpenAI-compatible endpoint
# OPENAI_BASE_URL=https://api.openai.com/v1

You only need the keys for the providers you plan to use. Mix and match based on your preferences!

Option B: Plugin Config

If you prefer to resolve secrets yourself at startup, pass them through providers. Config values take priority over environment variables, and omitted values still fall back to .env.

payloadAiPlugin({
  collections: {
    [Posts.slug]: true,
  },
  providers: {
    openai: {
      apiKey: process.env.PAYLOAD_AI_OPENAI_API_KEY,
      baseURL: process.env.PAYLOAD_AI_OPENAI_BASE_URL,
      orgId: process.env.PAYLOAD_AI_OPENAI_ORG_ID,
    },
  },
})

Supported provider keys: openai, anthropic, google, elevenLabs, and minimax.

Important: Restart your server after updating .env or plugin settings!

You may also need to regenerate the import map:

payload generate:importmap

⚙️ Advanced Configuration

🔐 Access Control & Multi-Tenant Setup

Generation requires authentication and permission from access.generate. Source documents, instructions, and built-in media creation also enforce the caller's collection, field, and per-document permissions. Configure overrideInstructions.access to restrict who can read or edit instructions; access.settings controls the settings UI and does not replace these collection rules. Custom mediaUpload callbacks must enforce their own access checks, as shown below. Trusted initialization and instruction seeding run separately from user requests.

Reference images and storage

Reference images now require a server-side resolveReferenceImage callback. This is a breaking change for image editing with sample images or image URLs in prompts, and for text generation with extractAttachments enabled. Text-to-image generation without references is unchanged. The plugin does not fetch reference URLs, forward login credentials, or require Node DNS/HTTP APIs for reference resolution.

For selected sample images, the plugin reloads the media document from uploadCollectionSlug (default: media) with the caller's Payload read permissions and depth: 0 before invoking the resolver. Use that document's ID to retrieve bytes from a fixed storage bucket or other application-controlled store. Do not use a document's URL or filename as an unchecked network destination or filesystem path. Fields hidden by access control will not be available to the resolver.

The resolver receives { source, request, signal, maxBytes } and must return a PNG, JPEG, or WebP Blob. source.kind is media (with collection and the authorized document) or url (with an untrusted URL extracted from a prompt). Reject URL sources unless your application can map them to an explicitly approved storage object and authorize access. Merely checking that a URL uses HTTPS is insufficient. Prefer storage SDKs with a fixed bucket/account over arbitrary URL fetching; do not forward request cookies or tokens.

For example, connect your application's storage reader:

resolveReferenceImage: async ({ source, signal, maxBytes }) => {
  if (source.kind !== 'media') {
    throw new Error('Only selected media can be used as reference images.')
  }
  // Application-owned helper: map this authorized ID to your fixed bucket/object key.
  // Enforce maxBytes during loading and honor signal to cancel outstanding work.
  return readReferenceBlobFromStorage({
    collection: source.collection,
    id: source.document.id,
    signal,
    maxBytes,
  })
},

readReferenceBlobFromStorage above is an application helper, not a Payload or plugin API. Its implementation depends on your storage adapter. The plugin validates the returned Blob, rejects empty files or files over 20 MiB, and stops waiting after 30 seconds. The resolver must enforce the limit while loading to avoid buffering oversized objects; cancellation is cooperative and requires honoring signal. MIME checks do not verify image file contents. Built-in text models resolve extracted attachments through the same callback and pass bytes to the AI SDK. Automatic SDK attachment downloads are disabled. URLs remain ordinary text when attachment extraction is disabled. Custom generation models remain responsible for any network requests they make themselves.

import { payloadAiPlugin } from '@ai-stack/payloadcms'

export default buildConfig({
  plugins: [
    payloadAiPlugin({
      collections: {
        [Posts.slug]: true,
      },

      // Enable AI for globals too
      globals: {
        [Home.slug]: true,
      },

      // Development helpers
      debugging: false,
      disableSponsorMessage: false,
      generatePromptOnInit: process.env.NODE_ENV !== 'production',

      // Specify media collection for GPT-Image-1
      uploadCollectionSlug: 'media',

      // Lock down AI features
      access: {
        generate: ({ req }) => req.user?.role === 'admin',
        settings: ({ req }) => req.user?.role === 'admin',
      },

      // Customize language options
      options: {
        enabledLanguages: ['en-US', 'zh-SG', 'zh-CN', 'en'],
      },

      // Reference additional fields in prompts
      promptFields: [
        {
          name: 'url',
          collections: ['images'],
        },
        {
          name: 'markdown',
          async getter(doc, { collection }) {
            return docToMarkdown(collection, doc)
          },
        },
      ],

      // Control initial prompt generation
      seedPrompts: ({ path }) => {
        if (path.endsWith('.meta.description')) {
          return {
            data: {
              prompt: 'Generate SEO-friendly meta description: {{markdown}}',
            },
          }
        }
        if (path.endsWith('.slug')) return false // Disable for slugs
        return undefined // Use defaults
      },

      // Custom media upload (useful for multi-tenant)
      mediaUpload: async (result, { request, collection }) => {
        return request.payload.create({
          collection,
          data: result.data,
          file: result.file,
          overrideAccess: false,
          req: request,
        })
      },
    }),
  ],
})
🎨 Custom Components & Fields

Custom fields don't automatically inherit AI capabilities. If your AI-enabled fields don't show Compose settings, manually add this component path:

@ai-stack/payloadcms/fields#ComposeField

Debug Tip: Enable debugging: true in your plugin config to see which fields have AI enabled.


📚 Documentation

Need more details? Check out the Complete Setup Guide for:

  • Custom model configuration
  • Advanced prompt engineering
  • Field-specific customization
  • Troubleshooting tips

🤝 Support This Project

Built with ❤️ in my free time. If this plugin saves you hours of work, consider fueling future development!

Buy Me A Coffee

Every coffee keeps the AI models running and new features shipping. Thank you! 🙏


👥 Contributing

We love contributors! Whether you're fixing typos, suggesting features, or building new capabilities, all contributions are welcome.

Ways to Contribute

  • 🐛 Report bugs
  • 💡 Suggest features
  • 📖 Improve documentation
  • 🔧 Submit pull requests

Join the conversation on Payload's Discord and let's build something amazing together!

Local Development

Want to hack on the plugin? Here's how:

Prerequisites

  • Node.js (version in .nvmrc)
  • pnpm
  • Database connection (Postgres or MongoDB)
  • Optional: AI provider API keys

Setup

# 1. Install dependencies
pnpm install

# 2. Set up environment
cp dev/.env.example dev/.env
# Edit dev/.env with your DATABASE_URI, PAYLOAD_SECRET, and API keys

# 3. Start development server
pnpm dev
# Admin UI available at http://localhost:3000

# 4. Generate types/importmap if needed
pnpm generate:importmap
pnpm generate:types

Development Workflow

  • Plugin source: src/
  • Test app: dev/
  • Edit files in src/ and refresh to see changes

Testing & Quality

pnpm test                    # Run all tests
pnpm lint                    # ESLint
pnpm prettier --write .      # Format code
pnpm build                   # Build plugin

Test in Another Project

pnpm pack
# In your other project:
pnpm add /path/to/ai-plugin-*.tgz

Project Structure

├── src/              # Plugin source code
├── dev/              # Test Payload app
│   ├── int.spec.ts   # Integration tests
│   └── e2e.spec.ts   # E2E tests
└── README.md         # You are here!

Made with ❤️ and ☕ by the community

Star on GitHub • Join Discord • Read the Guide

版本与发布记录

@ai-stack/payloadcms 共发布 65 个版本,首次发布于 2024/08/26;过去 12 个月发布了 28 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.0.1latest2026/10/1423 天前
4.0.02026/09/271 个月前
3.2.322026/09/101 个月前
3.2.312026/09/032 个月前
3.2.302026/08/112 个月前
3.2.292026/07/243 个月前

其他发布通道:beta → 3.76.0-beta.8。

关于 @ai-stack/payloadcms 的常见问题

@ai-stack/payloadcms 是什么?

Upgrading to 4.0.0? Read the migration guide. Reference images and extracted prompt attachments now require an explicit storage resolver.

如何安装 @ai-stack/payloadcms?

在项目目录运行 npm install @ai-stack/payloadcms,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:lexical(^0.50.0)、payload(^3.90.2)、@lexical/html(^0.50.0)、@payloadcms/ui(^3.90.2)、@payloadcms/translations(^3.90.2)、@payloadcms/richtext-lexical(^3.90.2)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@ai-stack/payloadcms 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ai-stack/payloadcms 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@ai-stack/payloadcms 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^18.20.2 || >=20.9.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ai-stack/payloadcms 可以商用吗?

@ai-stack/payloadcms 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ai-stack/payloadcms 的下载量有多少?

根据 npm 官方下载统计,@ai-stack/payloadcms 在 2026/10/05 – 2026/11/03 这 30 天内被下载 10,127 次,日均约 338 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ai-stack/payloadcms 还在维护吗?

最新版本 4.0.1 发布于 2026/10/14(23 天前);至今共发布 65 个版本,其中过去 12 个月发布了 28 个;首次发布于 2024/08/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。