@karixi/payload-ai 0.1.1 → 0.1.2
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 +157 -73
- package/package.json +9 -1
package/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# @karixi/payload-ai
|
|
2
2
|
|
|
3
|
-
AI-powered data population and admin features for Payload CMS 3.
|
|
3
|
+
AI-powered data population and admin features for [Payload CMS 3](https://payloadcms.com).
|
|
4
|
+
|
|
5
|
+
Generate realistic content for any collection using Anthropic Claude or OpenAI — via MCP tools, the admin panel, or automatic hooks.
|
|
4
6
|
|
|
5
7
|
## Installation
|
|
6
8
|
|
|
@@ -8,120 +10,219 @@ AI-powered data population and admin features for Payload CMS 3.
|
|
|
8
10
|
bun add @karixi/payload-ai
|
|
9
11
|
```
|
|
10
12
|
|
|
11
|
-
|
|
13
|
+
Peer dependencies:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
bun add payload @payloadcms/plugin-mcp
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Setup
|
|
20
|
+
|
|
21
|
+
This plugin has two parts:
|
|
12
22
|
|
|
13
|
-
|
|
23
|
+
1. **`aiPlugin`** — injects hooks into your Payload config (smart defaults, auto alt text)
|
|
24
|
+
2. **`getAITools` / `getAIPrompts` / `getAIResources`** — registers MCP tools inside `@payloadcms/plugin-mcp`
|
|
25
|
+
|
|
26
|
+
### Anthropic (Claude)
|
|
27
|
+
|
|
28
|
+
```ts
|
|
14
29
|
// payload.config.ts
|
|
15
30
|
import { buildConfig } from 'payload'
|
|
16
|
-
import { mongooseAdapter } from '@payloadcms/db-mongodb'
|
|
17
31
|
import { mcpPlugin } from '@payloadcms/plugin-mcp'
|
|
18
32
|
import { aiPlugin, getAITools, getAIPrompts, getAIResources } from '@karixi/payload-ai'
|
|
19
33
|
|
|
20
34
|
export default buildConfig({
|
|
21
35
|
plugins: [
|
|
36
|
+
// 1. AI plugin — hooks and admin features
|
|
22
37
|
aiPlugin({
|
|
23
38
|
provider: 'anthropic',
|
|
24
|
-
apiKeyEnvVar: 'ANTHROPIC_API_KEY',
|
|
39
|
+
apiKeyEnvVar: 'ANTHROPIC_API_KEY', // reads process.env.ANTHROPIC_API_KEY
|
|
25
40
|
features: {
|
|
26
41
|
adminUI: true,
|
|
27
42
|
devTools: false,
|
|
28
43
|
},
|
|
29
44
|
rollbackOnError: true,
|
|
30
45
|
}),
|
|
46
|
+
|
|
47
|
+
// 2. MCP plugin — exposes AI tools to Claude Code / Claude Desktop
|
|
31
48
|
mcpPlugin({
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
49
|
+
collections: {
|
|
50
|
+
posts: { enabled: true },
|
|
51
|
+
categories: { enabled: true },
|
|
52
|
+
media: { enabled: { find: true, create: false, update: false, delete: false } },
|
|
53
|
+
},
|
|
54
|
+
mcp: {
|
|
55
|
+
tools: getAITools({
|
|
56
|
+
provider: 'anthropic',
|
|
57
|
+
apiKeyEnvVar: 'ANTHROPIC_API_KEY',
|
|
58
|
+
}),
|
|
59
|
+
prompts: getAIPrompts(),
|
|
60
|
+
resources: getAIResources(),
|
|
61
|
+
},
|
|
35
62
|
}),
|
|
36
63
|
],
|
|
37
|
-
|
|
38
|
-
collections: [/* your collections */],
|
|
64
|
+
// ...
|
|
39
65
|
})
|
|
40
66
|
```
|
|
41
67
|
|
|
42
|
-
|
|
68
|
+
### OpenAI
|
|
43
69
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
70
|
+
```ts
|
|
71
|
+
aiPlugin({
|
|
72
|
+
provider: 'openai',
|
|
73
|
+
apiKeyEnvVar: 'OPENAI_API_KEY',
|
|
74
|
+
features: { adminUI: true },
|
|
75
|
+
}),
|
|
76
|
+
|
|
77
|
+
mcpPlugin({
|
|
78
|
+
collections: { posts: { enabled: true } },
|
|
79
|
+
mcp: {
|
|
80
|
+
tools: getAITools({
|
|
81
|
+
provider: 'openai',
|
|
82
|
+
apiKeyEnvVar: 'OPENAI_API_KEY',
|
|
83
|
+
}),
|
|
84
|
+
prompts: getAIPrompts(),
|
|
85
|
+
resources: getAIResources(),
|
|
86
|
+
},
|
|
87
|
+
}),
|
|
88
|
+
```
|
|
50
89
|
|
|
51
|
-
|
|
90
|
+
> **Note:** `getAITools` requires the same `provider` and `apiKeyEnvVar` as `aiPlugin` so it can create the AI provider when tools are invoked. `getAIPrompts()` and `getAIResources()` take no arguments.
|
|
91
|
+
|
|
92
|
+
## Connecting Claude Code
|
|
93
|
+
|
|
94
|
+
After starting your Payload dev server:
|
|
52
95
|
|
|
53
96
|
```bash
|
|
97
|
+
# 1. Create an API key in the Payload admin panel (MCP > API Keys)
|
|
98
|
+
# Enable the AI tools under the key's permissions
|
|
99
|
+
|
|
100
|
+
# 2. Register the MCP server with Claude Code
|
|
54
101
|
claude mcp add --transport http Payload http://localhost:3000/api/mcp \
|
|
55
|
-
--header "Authorization: Bearer
|
|
102
|
+
--header "Authorization: Bearer YOUR_MCP_API_KEY"
|
|
56
103
|
```
|
|
57
104
|
|
|
58
|
-
Then use natural language
|
|
105
|
+
Then use natural language:
|
|
59
106
|
|
|
60
107
|
```
|
|
61
|
-
Generate 10 blog posts
|
|
62
|
-
Populate all collections with realistic sample data
|
|
108
|
+
> Generate 10 blog posts about sustainable fashion
|
|
109
|
+
> Populate all collections with realistic sample data for an ecommerce demo
|
|
110
|
+
> Show me the schema for the products collection
|
|
63
111
|
```
|
|
64
112
|
|
|
113
|
+
### Claude Desktop
|
|
114
|
+
|
|
115
|
+
Add to `~/Library/Application Support/Claude/claude_desktop_config.json`:
|
|
116
|
+
|
|
117
|
+
```json
|
|
118
|
+
{
|
|
119
|
+
"mcpServers": {
|
|
120
|
+
"Payload": {
|
|
121
|
+
"command": "npx",
|
|
122
|
+
"args": [
|
|
123
|
+
"mcp-remote",
|
|
124
|
+
"http://localhost:3000/api/mcp",
|
|
125
|
+
"--header",
|
|
126
|
+
"Authorization: Bearer YOUR_MCP_API_KEY"
|
|
127
|
+
]
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## MCP Tools
|
|
134
|
+
|
|
135
|
+
These tools are registered inside the MCP plugin and callable by any MCP client:
|
|
136
|
+
|
|
137
|
+
| Tool | Parameters | Description |
|
|
138
|
+
|------|------------|-------------|
|
|
139
|
+
| `populateCollection` | `collection`, `count`, `theme?` | Generate and insert N documents into a single collection |
|
|
140
|
+
| `bulkPopulate` | `theme`, `counts` | Populate multiple collections at once in dependency order |
|
|
141
|
+
| `getCollectionSchema` | `collection` | Return the analyzed field schema as JSON |
|
|
142
|
+
| `listCollections` | — | List all collections with field counts and populatable status |
|
|
143
|
+
|
|
144
|
+
### MCP Prompts
|
|
145
|
+
|
|
146
|
+
| Prompt | Args | Description |
|
|
147
|
+
|--------|------|-------------|
|
|
148
|
+
| `generateContentBrief` | `collection`, `theme`, `count` | Structured brief to guide content generation |
|
|
149
|
+
| `reviewGeneratedContent` | `collection`, `documentId` | Quality review of a generated document |
|
|
150
|
+
|
|
151
|
+
### MCP Resources
|
|
152
|
+
|
|
153
|
+
| Resource | URI | Description |
|
|
154
|
+
|----------|-----|-------------|
|
|
155
|
+
| `schemaOverview` | `schema://collections` | High-level overview of all collections |
|
|
156
|
+
|
|
65
157
|
## Features
|
|
66
158
|
|
|
67
159
|
### Admin UI (`features.adminUI: true`)
|
|
68
160
|
|
|
69
|
-
- **AI Fill** button on individual
|
|
70
|
-
- **Bulk Generate** panel — populate any collection with N documents
|
|
71
|
-
- **Smart defaults** —
|
|
72
|
-
- **Image alt text** —
|
|
161
|
+
- **AI Fill** button on individual fields — generates a value with one click
|
|
162
|
+
- **Bulk Generate** panel — populate any collection with N documents
|
|
163
|
+
- **Smart defaults** — auto-fills empty fields on document creation via `beforeChange` hook
|
|
164
|
+
- **Image alt text** — generates alt text for uploaded media via `afterChange` hook
|
|
73
165
|
|
|
74
166
|
### Dev Tools (`features.devTools: true`)
|
|
75
167
|
|
|
76
|
-
Requires `@anthropic-ai/stagehand` (optional peer dependency)
|
|
168
|
+
Requires `@anthropic-ai/stagehand` (optional peer dependency):
|
|
169
|
+
|
|
170
|
+
```bash
|
|
171
|
+
bun add -d @anthropic-ai/stagehand
|
|
172
|
+
```
|
|
77
173
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
174
|
+
Adds four additional MCP tools:
|
|
175
|
+
|
|
176
|
+
| Tool | Description |
|
|
177
|
+
|------|-------------|
|
|
178
|
+
| `screenshot` | Capture page screenshots with viewport presets |
|
|
179
|
+
| `visual_diff` | Compare before/after screenshots for regressions |
|
|
180
|
+
| `test_form` | Fill and submit Payload admin forms programmatically |
|
|
181
|
+
| `edit_test_fix` | AI-driven screenshot → analyze → suggest fix loop |
|
|
82
182
|
|
|
83
183
|
## Plugin Configuration
|
|
84
184
|
|
|
85
|
-
```
|
|
185
|
+
```ts
|
|
86
186
|
aiPlugin({
|
|
87
|
-
|
|
88
|
-
|
|
187
|
+
// Required
|
|
188
|
+
provider: 'anthropic' | 'openai',
|
|
189
|
+
apiKeyEnvVar: string, // env var name (not the key itself)
|
|
190
|
+
|
|
191
|
+
// Optional
|
|
89
192
|
features: {
|
|
90
|
-
adminUI: boolean,
|
|
91
|
-
devTools: boolean,
|
|
193
|
+
adminUI: boolean, // default: false
|
|
194
|
+
devTools: boolean, // default: false
|
|
92
195
|
},
|
|
93
196
|
collections: {
|
|
94
197
|
posts: {
|
|
95
198
|
fields: {
|
|
96
199
|
title: { enabled: true, prompt: 'A compelling blog post title' },
|
|
97
|
-
status: { enabled: false },
|
|
200
|
+
status: { enabled: false }, // skip this field
|
|
98
201
|
},
|
|
99
202
|
},
|
|
100
203
|
},
|
|
101
204
|
rateLimit: {
|
|
102
205
|
maxTokensPerBatch: 100000,
|
|
103
|
-
delayBetweenRequests: 200,
|
|
206
|
+
delayBetweenRequests: 200, // ms
|
|
104
207
|
maxConcurrentRequests: 3,
|
|
105
208
|
},
|
|
106
|
-
rollbackOnError: true,
|
|
107
|
-
maxConcurrentBrowserInstances: 2,
|
|
209
|
+
rollbackOnError: true, // delete created docs if bulk generation fails
|
|
210
|
+
maxConcurrentBrowserInstances: 2,
|
|
108
211
|
})
|
|
109
212
|
```
|
|
110
213
|
|
|
111
|
-
##
|
|
112
|
-
|
|
113
|
-
### Plugin entry points
|
|
214
|
+
## How It Works
|
|
114
215
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
216
|
+
1. **Schema analysis** — reads your Payload collection configs and normalizes fields into a structured schema
|
|
217
|
+
2. **Dependency resolution** — topologically sorts collections so referenced collections are populated first
|
|
218
|
+
3. **Prompt building** — generates per-field AI instructions (respects select options, relationship IDs, richText handling)
|
|
219
|
+
4. **AI generation** — calls the configured provider, validates responses, retries with error context (up to 3 attempts)
|
|
220
|
+
5. **Post-processing** — converts plain text to Lexical JSON for richText fields, links relationships, adds content variation
|
|
221
|
+
6. **Rollback** — if `rollbackOnError` is enabled, deletes all created documents on failure
|
|
121
222
|
|
|
122
|
-
|
|
223
|
+
## Types
|
|
123
224
|
|
|
124
|
-
```
|
|
225
|
+
```ts
|
|
125
226
|
import type {
|
|
126
227
|
AIPluginConfig,
|
|
127
228
|
AIProvider,
|
|
@@ -134,30 +235,13 @@ import type {
|
|
|
134
235
|
} from '@karixi/payload-ai'
|
|
135
236
|
```
|
|
136
237
|
|
|
137
|
-
### Core utilities (advanced)
|
|
138
|
-
|
|
139
|
-
```typescript
|
|
140
|
-
// Analyze collection fields into a structured schema
|
|
141
|
-
import { analyzeFields } from '@karixi/payload-ai/core/field-analyzer'
|
|
142
|
-
|
|
143
|
-
// Resolve creation order for collections with relationships
|
|
144
|
-
import { resolveCreationOrder, getDependencies } from '@karixi/payload-ai/core/dependency-resolver'
|
|
145
|
-
|
|
146
|
-
// Convert plain text or structured content to Lexical rich text format
|
|
147
|
-
import { textToLexical, contentToLexical } from '@karixi/payload-ai/generate/richtext-generator'
|
|
148
|
-
```
|
|
149
|
-
|
|
150
238
|
## Requirements
|
|
151
239
|
|
|
152
|
-
-
|
|
153
|
-
- `@payloadcms/plugin-mcp` ^3.
|
|
240
|
+
- Payload CMS ^3.81.0
|
|
241
|
+
- `@payloadcms/plugin-mcp` ^3.81.0
|
|
154
242
|
- Node.js 20+ or Bun 1.1+
|
|
155
|
-
-
|
|
243
|
+
- An Anthropic or OpenAI API key
|
|
156
244
|
|
|
157
|
-
##
|
|
245
|
+
## License
|
|
158
246
|
|
|
159
|
-
|
|
160
|
-
bun run build # compile TypeScript
|
|
161
|
-
bun run typecheck # type-check without emitting
|
|
162
|
-
bun run test # run unit tests
|
|
163
|
-
```
|
|
247
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@karixi/payload-ai",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.mjs",
|
|
6
6
|
"types": "dist/index.d.mts",
|
|
@@ -15,6 +15,14 @@
|
|
|
15
15
|
"files": [
|
|
16
16
|
"dist"
|
|
17
17
|
],
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/leecois/karixi-payload-ai.git"
|
|
21
|
+
},
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/leecois/karixi-payload-ai/issues"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/leecois/karixi-payload-ai#readme",
|
|
18
26
|
"peerDependencies": {
|
|
19
27
|
"payload": "^3.81.0",
|
|
20
28
|
"@payloadcms/plugin-mcp": "^3.81.0"
|