@nextsparkjs/plugin-ai 0.1.0-beta.1

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.
Files changed (34) hide show
  1. package/.env.example +79 -0
  2. package/README.md +529 -0
  3. package/api/README.md +65 -0
  4. package/api/ai-history/[id]/route.ts +112 -0
  5. package/api/embeddings/route.ts +129 -0
  6. package/api/generate/route.ts +160 -0
  7. package/docs/01-getting-started/01-introduction.md +237 -0
  8. package/docs/01-getting-started/02-installation.md +447 -0
  9. package/docs/01-getting-started/03-configuration.md +416 -0
  10. package/docs/02-features/01-text-generation.md +523 -0
  11. package/docs/02-features/02-embeddings.md +241 -0
  12. package/docs/02-features/03-ai-history.md +549 -0
  13. package/docs/03-advanced-usage/01-core-utilities.md +500 -0
  14. package/docs/04-use-cases/01-content-generation.md +453 -0
  15. package/entities/ai-history/ai-history.config.ts +123 -0
  16. package/entities/ai-history/ai-history.fields.ts +330 -0
  17. package/entities/ai-history/messages/en.json +56 -0
  18. package/entities/ai-history/messages/es.json +56 -0
  19. package/entities/ai-history/migrations/001_ai_history_table.sql +167 -0
  20. package/entities/ai-history/migrations/002_ai_history_metas.sql +103 -0
  21. package/lib/ai-history-meta-service.ts +379 -0
  22. package/lib/ai-history-service.ts +391 -0
  23. package/lib/ai-sdk.ts +7 -0
  24. package/lib/core-utils.ts +217 -0
  25. package/lib/plugin-env.ts +252 -0
  26. package/lib/sanitize.ts +122 -0
  27. package/lib/save-example.ts +237 -0
  28. package/lib/server-env.ts +104 -0
  29. package/package.json +23 -0
  30. package/plugin.config.ts +55 -0
  31. package/public/docs/login-404-error.png +0 -0
  32. package/tsconfig.json +47 -0
  33. package/tsconfig.tsbuildinfo +1 -0
  34. package/types/ai.types.ts +51 -0
@@ -0,0 +1,447 @@
1
+ # Installation and Setup
2
+
3
+ ## Prerequisites
4
+
5
+ - Node.js 18+ and pnpm installed
6
+ - NextSpark project set up
7
+ - Active theme with plugin support
8
+
9
+ ## Step 1: Enable the Plugin
10
+
11
+ ### In Theme Configuration
12
+
13
+ The AI plugin is enabled by default in the default theme. To enable it in your custom theme:
14
+
15
+ ```typescript
16
+ // contents/themes/[your-theme]/theme.config.ts
17
+ export const yourThemeConfig: ThemeConfig = {
18
+ name: 'your-theme',
19
+ // ... other config
20
+ plugins: ['ai'], // Add 'ai' to plugins array
21
+ }
22
+ ```
23
+
24
+ ### Verify Plugin is Active
25
+
26
+ ```bash
27
+ # Check loaded plugins in development
28
+ pnpm dev
29
+
30
+ # Look for plugin load message in console:
31
+ # [AI Plugin] Core utilities loaded - ready for custom endpoints
32
+ ```
33
+
34
+ ## Step 2: Choose Your Provider
35
+
36
+ You can use one or multiple providers simultaneously. Choose based on your needs:
37
+
38
+ ### Option A: Ollama (Local, Free)
39
+
40
+ **Best for:**
41
+ - Development and testing
42
+ - Privacy-sensitive applications
43
+ - Cost-free inference
44
+ - No API key management
45
+
46
+ **Setup:**
47
+
48
+ 1. **Install Ollama** (if not already installed)
49
+ ```bash
50
+ # macOS
51
+ brew install ollama
52
+
53
+ # Linux
54
+ curl -fsSL https://ollama.com/install.sh | sh
55
+
56
+ # Windows
57
+ # Download from https://ollama.com/download
58
+ ```
59
+
60
+ 2. **Start Ollama Server**
61
+ ```bash
62
+ ollama serve
63
+ ```
64
+
65
+ 3. **Pull a Model** (e.g., Llama 3.2 3B)
66
+ ```bash
67
+ ollama pull llama3.2:3b
68
+ ```
69
+
70
+ 4. **Configure Base URL** (optional, defaults to `http://localhost:11434`)
71
+ ```bash
72
+ # contents/plugins/ai/.env
73
+ OLLAMA_BASE_URL=http://localhost:11434
74
+ ```
75
+
76
+ 5. **Test**
77
+ ```bash
78
+ curl http://localhost:11434/api/tags
79
+ # Should return list of installed models
80
+ ```
81
+
82
+ ### Option B: OpenAI (Cloud, Paid)
83
+
84
+ **Best for:**
85
+ - Production deployments
86
+ - Highest quality responses
87
+ - Proven reliability
88
+
89
+ **Setup:**
90
+
91
+ 1. **Get API Key**
92
+ - Visit https://platform.openai.com/api-keys
93
+ - Create new API key
94
+ - Copy the key (starts with `sk-`)
95
+
96
+ 2. **Create Plugin Environment File**
97
+ ```bash
98
+ # Create .env file in plugin directory
99
+ touch contents/plugins/ai/.env
100
+ ```
101
+
102
+ 3. **Add API Key**
103
+ ```bash
104
+ # contents/plugins/ai/.env
105
+ OPENAI_API_KEY=sk-your-openai-api-key-here
106
+ AI_PLUGIN_ENABLED=true
107
+ ```
108
+
109
+ 4. **Set Default Model** (optional)
110
+ ```bash
111
+ # contents/plugins/ai/.env
112
+ DEFAULT_MODEL=gpt-4o-mini
113
+ MAX_TOKENS=2000
114
+ DEFAULT_TEMPERATURE=0.7
115
+ ```
116
+
117
+ 5. **Test API Key**
118
+ ```bash
119
+ curl https://api.openai.com/v1/models \
120
+ -H "Authorization: Bearer sk-your-key-here"
121
+ ```
122
+
123
+ ### Option C: Anthropic (Cloud, Paid)
124
+
125
+ **Best for:**
126
+ - Complex reasoning tasks
127
+ - Long context windows (200K+ tokens)
128
+ - Advanced analysis
129
+
130
+ **Setup:**
131
+
132
+ 1. **Get API Key**
133
+ - Visit https://console.anthropic.com/
134
+ - Create new API key
135
+ - Copy the key (starts with `sk-ant-`)
136
+
137
+ 2. **Add API Key to Plugin Environment**
138
+ ```bash
139
+ # contents/plugins/ai/.env
140
+ ANTHROPIC_API_KEY=sk-ant-your-anthropic-api-key-here
141
+ AI_PLUGIN_ENABLED=true
142
+ ```
143
+
144
+ 3. **Set Default Model** (optional)
145
+ ```bash
146
+ # contents/plugins/ai/.env
147
+ DEFAULT_MODEL=claude-3-5-haiku-20241022
148
+ ```
149
+
150
+ 4. **Test API Key**
151
+ ```bash
152
+ curl https://api.anthropic.com/v1/messages \
153
+ -H "x-api-key: sk-ant-your-key-here" \
154
+ -H "anthropic-version: 2023-06-01" \
155
+ -H "content-type: application/json" \
156
+ -d '{"model":"claude-3-5-haiku-20241022","max_tokens":1024,"messages":[{"role":"user","content":"Hello"}]}'
157
+ ```
158
+
159
+ ## Step 3: Configure Plugin
160
+
161
+ ### Environment Variables Reference
162
+
163
+ Create `contents/plugins/ai/.env` with your configuration:
164
+
165
+ ```bash
166
+ # ==========================================
167
+ # PLUGIN ACTIVATION
168
+ # ==========================================
169
+ AI_PLUGIN_ENABLED=true
170
+
171
+ # ==========================================
172
+ # PROVIDER API KEYS (add one or more)
173
+ # ==========================================
174
+
175
+ # OpenAI (optional)
176
+ OPENAI_API_KEY=sk-your-openai-key
177
+
178
+ # Anthropic (optional)
179
+ ANTHROPIC_API_KEY=sk-ant-your-anthropic-key
180
+
181
+ # Ollama (optional, defaults to localhost)
182
+ OLLAMA_BASE_URL=http://localhost:11434
183
+
184
+ # ==========================================
185
+ # DEFAULT SETTINGS (optional)
186
+ # ==========================================
187
+
188
+ # Default model for generate endpoint
189
+ # Options: llama3.2:3b, gpt-4o-mini, claude-3-5-haiku-20241022
190
+ DEFAULT_MODEL=llama3.2:3b
191
+
192
+ # Default maximum output tokens
193
+ MAX_TOKENS=2000
194
+
195
+ # Default temperature (0-1, creativity)
196
+ DEFAULT_TEMPERATURE=0.7
197
+
198
+ # Ollama default model (if using Ollama)
199
+ OLLAMA_DEFAULT_MODEL=llama3.2:3b
200
+ ```
201
+
202
+ ### Security Best Practices
203
+
204
+ 1. **Never Commit API Keys**
205
+ ```bash
206
+ # Verify .env is in .gitignore
207
+ grep "\.env" .gitignore
208
+
209
+ # Add if missing
210
+ echo "*.env" >> .gitignore
211
+ ```
212
+
213
+ 2. **Use Different Keys for Environments**
214
+ - Development: Use Ollama or low-cost models
215
+ - Staging: Use separate API keys with rate limits
216
+ - Production: Use production keys with monitoring
217
+
218
+ 3. **Restrict API Key Permissions**
219
+ - OpenAI: Limit permissions in dashboard
220
+ - Anthropic: Set usage limits
221
+ - Monitor usage regularly
222
+
223
+ 4. **Rotate Keys Regularly**
224
+ - Set reminders to rotate keys every 90 days
225
+ - Have backup keys ready for emergencies
226
+
227
+ ## Step 4: Rebuild Registry
228
+
229
+ After enabling the plugin, rebuild the registries:
230
+
231
+ ```bash
232
+ # Rebuild all registries (includes plugins)
233
+ pnpm registry:build
234
+
235
+ # Or rebuild individual registries
236
+ pnpm registry:build:plugins
237
+ pnpm registry:build:docs
238
+ ```
239
+
240
+ **Expected Output:**
241
+ ```
242
+ ✓ Plugin registry built successfully
243
+ ✓ AI plugin loaded
244
+ ✓ Docs registry built (includes plugin docs)
245
+ ```
246
+
247
+ ## Step 5: Verify Installation
248
+
249
+ ### Check Plugin Status
250
+
251
+ Visit the generate endpoint info page:
252
+
253
+ ```bash
254
+ # Start development server
255
+ pnpm dev
256
+
257
+ # Open browser or use curl
258
+ curl http://localhost:5173/api/plugin/ai/generate
259
+
260
+ # Should return endpoint documentation with:
261
+ # - Available models
262
+ # - Setup instructions
263
+ # - Usage examples
264
+ ```
265
+
266
+ ### Test Generate Endpoint
267
+
268
+ **Simple Test:**
269
+ ```bash
270
+ curl -X POST http://localhost:5173/api/plugin/ai/generate \
271
+ -H "Content-Type: application/json" \
272
+ -H "Cookie: your-session-cookie" \
273
+ -d '{
274
+ "prompt": "Say hello",
275
+ "model": "llama3.2:3b"
276
+ }'
277
+ ```
278
+
279
+ **Expected Response:**
280
+ ```json
281
+ {
282
+ "success": true,
283
+ "response": "Hello! How can I assist you today?",
284
+ "model": "llama3.2:3b",
285
+ "provider": "ollama",
286
+ "isLocal": true,
287
+ "cost": 0,
288
+ "tokens": {
289
+ "input": 5,
290
+ "output": 12,
291
+ "total": 17
292
+ },
293
+ "userId": "user-id-here"
294
+ }
295
+ ```
296
+
297
+ ### Test Embeddings Endpoint (OpenAI Only)
298
+
299
+ ```bash
300
+ curl -X POST http://localhost:5173/api/plugin/ai/embeddings \
301
+ -H "Content-Type: application/json" \
302
+ -H "Cookie: your-session-cookie" \
303
+ -d '{
304
+ "text": "Hello world"
305
+ }'
306
+ ```
307
+
308
+ **Expected Response:**
309
+ ```json
310
+ {
311
+ "success": true,
312
+ "embedding": [0.123, -0.456, ...], // 1536 numbers
313
+ "model": "text-embedding-3-small",
314
+ "dimensions": 1536,
315
+ "tokens": 2,
316
+ "userId": "user-id-here"
317
+ }
318
+ ```
319
+
320
+ ## Step 6: Database Setup (Optional)
321
+
322
+ The AI History entity requires database tables. These are created automatically via migrations when the plugin is loaded.
323
+
324
+ ### Verify Tables Created
325
+
326
+ ```sql
327
+ -- Check if tables exist
328
+ SELECT table_name
329
+ FROM information_schema.tables
330
+ WHERE table_name IN ('ai_history', 'ai_history_metas');
331
+ ```
332
+
333
+ ### Manual Migration (if needed)
334
+
335
+ ```bash
336
+ # Run migrations manually
337
+ pnpm db:migrate
338
+
339
+ # Or apply specific migration
340
+ psql $DATABASE_URL -f contents/plugins/ai/entities/ai-history/migrations/001_ai_history_table.sql
341
+ psql $DATABASE_URL -f contents/plugins/ai/entities/ai-history/migrations/002_ai_history_metas.sql
342
+ ```
343
+
344
+ ## Troubleshooting
345
+
346
+ ### Plugin Not Loading
347
+
348
+ **Issue:** "AI plugin disabled" error
349
+
350
+ **Solution:**
351
+ ```bash
352
+ # Check AI_PLUGIN_ENABLED in .env
353
+ cat contents/plugins/ai/.env | grep AI_PLUGIN_ENABLED
354
+
355
+ # Should be: AI_PLUGIN_ENABLED=true
356
+
357
+ # Rebuild registry
358
+ pnpm registry:build:plugins
359
+ ```
360
+
361
+ ### Ollama Connection Failed
362
+
363
+ **Issue:** "ECONNREFUSED" error
364
+
365
+ **Solution:**
366
+ ```bash
367
+ # Check if Ollama is running
368
+ curl http://localhost:11434/api/tags
369
+
370
+ # If not running, start it
371
+ ollama serve
372
+
373
+ # Check base URL in .env
374
+ cat contents/plugins/ai/.env | grep OLLAMA_BASE_URL
375
+ ```
376
+
377
+ ### OpenAI Authentication Failed
378
+
379
+ **Issue:** "OpenAI authentication failed"
380
+
381
+ **Solution:**
382
+ ```bash
383
+ # Verify API key format (should start with sk-)
384
+ cat contents/plugins/ai/.env | grep OPENAI_API_KEY
385
+
386
+ # Test key directly
387
+ curl https://api.openai.com/v1/models \
388
+ -H "Authorization: Bearer YOUR-KEY-HERE"
389
+
390
+ # Check for rate limits or invalid key in OpenAI dashboard
391
+ ```
392
+
393
+ ### Anthropic Authentication Failed
394
+
395
+ **Issue:** "Anthropic authentication failed"
396
+
397
+ **Solution:**
398
+ ```bash
399
+ # Verify API key format (should start with sk-ant-)
400
+ cat contents/plugins/ai/.env | grep ANTHROPIC_API_KEY
401
+
402
+ # Test key directly
403
+ curl https://api.anthropic.com/v1/messages \
404
+ -H "x-api-key: YOUR-KEY-HERE" \
405
+ -H "anthropic-version: 2023-06-01" \
406
+ -H "content-type: application/json" \
407
+ -d '{"model":"claude-3-5-haiku-20241022","max_tokens":10,"messages":[{"role":"user","content":"Hi"}]}'
408
+ ```
409
+
410
+ ### Database Tables Not Created
411
+
412
+ **Issue:** "Table 'ai_history' doesn't exist"
413
+
414
+ **Solution:**
415
+ ```bash
416
+ # Run migrations manually
417
+ pnpm db:migrate
418
+
419
+ # Or check migration files exist
420
+ ls contents/plugins/ai/entities/ai-history/migrations/
421
+ ```
422
+
423
+ ### Authentication Required Error
424
+
425
+ **Issue:** "Authentication required" when testing endpoints
426
+
427
+ **Solution:**
428
+ - Endpoints require authentication (session or API key)
429
+ - Test from authenticated dashboard pages
430
+ - Or use API key in Authorization header:
431
+ ```bash
432
+ curl -X POST http://localhost:5173/api/plugin/ai/generate \
433
+ -H "Authorization: Bearer your-api-key-here" \
434
+ -d '{"prompt": "test"}'
435
+ ```
436
+
437
+ ## Next Steps
438
+
439
+ ✅ Plugin installed and configured
440
+ ✅ Provider(s) set up and tested
441
+ ✅ Database tables created
442
+ ✅ Endpoints verified
443
+
444
+ **Continue to:**
445
+ - **[Configuration](./03-configuration.md)** - Detailed configuration options
446
+ - **[Text Generation](../02-features/01-text-generation.md)** - Start using AI
447
+ - **[Core Utilities](../04-advanced-usage/01-core-utilities.md)** - Build custom features