@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,416 @@
1
+ # Configuration
2
+
3
+ ## Configuration File Structure
4
+
5
+ The AI plugin uses a dedicated environment file for all configuration:
6
+
7
+ ```
8
+ contents/plugins/ai/
9
+ ├── .env # Your configuration (never commit)
10
+ ├── .env.example # Template (commit this)
11
+ └── lib/
12
+ ├── plugin-env.ts # Client-side config
13
+ └── server-env.ts # Server-side config
14
+ ```
15
+
16
+ ## Environment Variables
17
+
18
+ ### Plugin Activation
19
+
20
+ ```bash
21
+ # Enable/disable the entire plugin
22
+ AI_PLUGIN_ENABLED=true
23
+ ```
24
+
25
+ **Values:**
26
+ - `true` - Plugin active, endpoints available
27
+ - `false` - Plugin disabled, endpoints return 503
28
+
29
+ **Use Case:** Disable plugin temporarily without removing from theme config
30
+
31
+ ### Provider API Keys
32
+
33
+ ```bash
34
+ # OpenAI Configuration
35
+ OPENAI_API_KEY=sk-proj-...
36
+
37
+ # Anthropic Configuration
38
+ ANTHROPIC_API_KEY=sk-ant-...
39
+
40
+ # Ollama Configuration (optional)
41
+ OLLAMA_BASE_URL=http://localhost:11434
42
+ ```
43
+
44
+ **Notes:**
45
+ - You can configure multiple providers simultaneously
46
+ - The plugin will use the appropriate provider based on model selection
47
+ - API keys are **required** for their respective providers
48
+ - Ollama works without API keys (local inference)
49
+
50
+ ### Default Model Settings
51
+
52
+ ```bash
53
+ # Default model for generate endpoint
54
+ DEFAULT_MODEL=llama3.2:3b
55
+
56
+ # Maximum output tokens (affects cost and response length)
57
+ MAX_TOKENS=2000
58
+
59
+ # Temperature for response creativity (0 = deterministic, 1 = creative)
60
+ DEFAULT_TEMPERATURE=0.7
61
+
62
+ # Default Ollama model (used when no model specified for Ollama)
63
+ OLLAMA_DEFAULT_MODEL=llama3.2:3b
64
+ ```
65
+
66
+ ## Provider-Specific Configuration
67
+
68
+ ### OpenAI
69
+
70
+ **Required:**
71
+ ```bash
72
+ OPENAI_API_KEY=sk-proj-your-key-here
73
+ ```
74
+
75
+ **Available Models:**
76
+ - `gpt-4o` - Most capable, multimodal ($0.0025 input / $0.01 output per 1K tokens)
77
+ - `gpt-4o-mini` - Fast and affordable ($0.00015 input / $0.0006 output per 1K tokens)
78
+ - `gpt-3.5-turbo` - Legacy, fast ($0.0005 input / $0.0015 output per 1K tokens)
79
+
80
+ **Best Practices:**
81
+ ```bash
82
+ # Use gpt-4o-mini for development
83
+ DEFAULT_MODEL=gpt-4o-mini
84
+
85
+ # Reserve gpt-4o for production critical features
86
+ # Specify in request: { "model": "gpt-4o", "prompt": "..." }
87
+ ```
88
+
89
+ **Rate Limits:**
90
+ - Tier 1 (New users): 200 requests/day, 40,000 tokens/minute
91
+ - Tier 2-5: Higher limits based on usage
92
+ - Monitor at: https://platform.openai.com/account/limits
93
+
94
+ **Cost Management:**
95
+ ```bash
96
+ # Set conservative token limits
97
+ MAX_TOKENS=1000 # Reduces cost per request
98
+
99
+ # Use lower temperature for deterministic tasks
100
+ DEFAULT_TEMPERATURE=0.3 # More focused responses
101
+ ```
102
+
103
+ ### Anthropic
104
+
105
+ **Required:**
106
+ ```bash
107
+ ANTHROPIC_API_KEY=sk-ant-your-key-here
108
+ ```
109
+
110
+ **Available Models:**
111
+ - `claude-3-5-sonnet-20241022` - Highest quality ($0.003 input / $0.015 output per 1K tokens)
112
+ - `claude-3-5-haiku-20241022` - Fast and affordable ($0.00025 input / $0.00125 output per 1K tokens)
113
+ - `claude-sonnet-4-5-20250929` - Current latest (same pricing as 3.5 Sonnet)
114
+
115
+ **Best Practices:**
116
+ ```bash
117
+ # Use Haiku for most tasks (fast, cheap)
118
+ DEFAULT_MODEL=claude-3-5-haiku-20241022
119
+
120
+ # Use Sonnet for complex reasoning
121
+ # Specify in request: { "model": "claude-3-5-sonnet-20241022", "prompt": "..." }
122
+ ```
123
+
124
+ **Context Windows:**
125
+ - Haiku: 200,000 tokens
126
+ - Sonnet: 200,000 tokens
127
+ - Both support large documents and conversations
128
+
129
+ **Rate Limits:**
130
+ - Tier 1: 50 requests/minute, 40,000 tokens/minute
131
+ - Tier 4: 2,000 requests/minute, 400,000 tokens/minute
132
+ - Monitor at: https://console.anthropic.com/settings/limits
133
+
134
+ ### Ollama (Local)
135
+
136
+ **Optional:**
137
+ ```bash
138
+ OLLAMA_BASE_URL=http://localhost:11434
139
+ ```
140
+
141
+ **Available Models** (install via `ollama pull`):
142
+ - `llama3.2:3b` - Fast, 3B parameters, good for testing
143
+ - `llama3.2` - 11B parameters, better quality
144
+ - `llama3.1` - Previous version, still excellent
145
+ - `qwen2.5` - Chinese company, multilingual
146
+ - `mistral` - European model, strong performance
147
+ - `gemma2` - Google model
148
+ - `codellama` - Specialized for code
149
+
150
+ **Best Practices:**
151
+ ```bash
152
+ # Use smallest model for development
153
+ OLLAMA_DEFAULT_MODEL=llama3.2:3b
154
+
155
+ # Pull multiple models for different tasks
156
+ ollama pull llama3.2:3b # Fast, development
157
+ ollama pull llama3.2 # Better quality
158
+ ollama pull codellama # Code generation
159
+ ```
160
+
161
+ **Hardware Requirements:**
162
+ - 3B models: 4GB RAM minimum
163
+ - 11B models: 8GB RAM minimum
164
+ - 70B+ models: 64GB RAM + GPU recommended
165
+
166
+ **Performance:**
167
+ - Local inference: No API latency
168
+ - GPU acceleration: Significant speedup
169
+ - CPU only: Usable for development
170
+
171
+ **No Cost:**
172
+ - All inference is free
173
+ - Only costs are hardware/electricity
174
+
175
+ ## Configuration Access in Code
176
+
177
+ ### Server-Side Configuration
178
+
179
+ ```typescript
180
+ import { getServerPluginConfig } from '@/contents/plugins/ai/lib/server-env'
181
+
182
+ export async function POST(request: Request) {
183
+ const config = await getServerPluginConfig()
184
+
185
+ console.log(config.defaultModel) // "llama3.2:3b"
186
+ console.log(config.maxTokens) // 2000
187
+ console.log(config.defaultTemperature) // 0.7
188
+ console.log(config.openaiApiKey) // "sk-proj-..."
189
+ console.log(config.anthropicApiKey) // "sk-ant-..."
190
+ console.log(config.ollamaBaseUrl) // "http://localhost:11434"
191
+ }
192
+ ```
193
+
194
+ ### Client-Side Configuration
195
+
196
+ ```typescript
197
+ import { getPluginConfig } from '@/contents/plugins/ai/lib/plugin-env'
198
+
199
+ export function MyComponent() {
200
+ const config = getPluginConfig()
201
+
202
+ // Only non-sensitive config available
203
+ console.log(config.enabled) // true
204
+ console.log(config.defaultModel) // "llama3.2:3b"
205
+
206
+ // API keys NOT available client-side (security)
207
+ }
208
+ ```
209
+
210
+ ## Security Best Practices
211
+
212
+ ### 1. Environment File Security
213
+
214
+ ```bash
215
+ # ✅ ALWAYS in .gitignore
216
+ contents/plugins/ai/.env
217
+
218
+ # ❌ NEVER commit .env files
219
+ # ❌ NEVER share API keys in code
220
+ # ❌ NEVER log API keys
221
+ ```
222
+
223
+ ### 2. API Key Management
224
+
225
+ **Separate Keys by Environment:**
226
+ ```bash
227
+ # Development (.env.local)
228
+ OPENAI_API_KEY=sk-proj-dev-key-here
229
+
230
+ # Production (.env.production)
231
+ OPENAI_API_KEY=sk-proj-prod-key-here
232
+ ```
233
+
234
+ **Use Different Keys for:**
235
+ - Development (rate limited)
236
+ - Staging (separate billing)
237
+ - Production (monitored closely)
238
+
239
+ ### 3. Rate Limiting
240
+
241
+ **Application Level:**
242
+ ```typescript
243
+ // Implement rate limiting in your endpoints
244
+ import { rateLimit } from '@/core/lib/api/rate-limit'
245
+
246
+ export async function POST(request: Request) {
247
+ // Limit to 10 requests per minute per user
248
+ await rateLimit(request, { max: 10, windowMs: 60000 })
249
+
250
+ // Continue with AI request
251
+ }
252
+ ```
253
+
254
+ **Provider Level:**
255
+ - Set usage limits in provider dashboards
256
+ - Monitor usage regularly
257
+ - Set up billing alerts
258
+
259
+ ### 4. Key Rotation
260
+
261
+ **Best Practice:**
262
+ ```bash
263
+ # Rotate keys every 90 days
264
+ # 1. Create new key in provider dashboard
265
+ # 2. Update .env with new key
266
+ # 3. Test thoroughly
267
+ # 4. Revoke old key after 24-48 hours
268
+ ```
269
+
270
+ **Emergency Rotation:**
271
+ ```bash
272
+ # If key compromised:
273
+ # 1. Immediately revoke in provider dashboard
274
+ # 2. Create new key
275
+ # 3. Update production environment
276
+ # 4. Review usage logs for abuse
277
+ ```
278
+
279
+ ### 5. Monitoring and Alerts
280
+
281
+ **Set Up Alerts:**
282
+ - Unusual spending patterns
283
+ - Rate limit warnings
284
+ - Failed authentication attempts
285
+ - High error rates
286
+
287
+ **OpenAI Monitoring:**
288
+ - Dashboard: https://platform.openai.com/usage
289
+ - Set monthly spending limits
290
+ - Enable email notifications
291
+
292
+ **Anthropic Monitoring:**
293
+ - Console: https://console.anthropic.com/settings/usage
294
+ - Track credit usage
295
+ - Set budget alerts
296
+
297
+ ## Configuration Examples
298
+
299
+ ### Development Configuration
300
+
301
+ ```bash
302
+ # contents/plugins/ai/.env (development)
303
+
304
+ # Use local Ollama for free development
305
+ AI_PLUGIN_ENABLED=true
306
+ OLLAMA_BASE_URL=http://localhost:11434
307
+ DEFAULT_MODEL=llama3.2:3b
308
+ MAX_TOKENS=1000
309
+ DEFAULT_TEMPERATURE=0.7
310
+
311
+ # Optional: Add OpenAI for testing production features
312
+ # OPENAI_API_KEY=sk-proj-dev-key
313
+ ```
314
+
315
+ ### Production Configuration
316
+
317
+ ```bash
318
+ # Production environment variables
319
+
320
+ # Use cloud providers for reliability
321
+ AI_PLUGIN_ENABLED=true
322
+
323
+ # Primary: Anthropic (good cost/performance)
324
+ ANTHROPIC_API_KEY=sk-ant-prod-key-here
325
+ DEFAULT_MODEL=claude-3-5-haiku-20241022
326
+
327
+ # Fallback: OpenAI
328
+ OPENAI_API_KEY=sk-proj-prod-key-here
329
+
330
+ # Conservative token limits
331
+ MAX_TOKENS=2000
332
+ DEFAULT_TEMPERATURE=0.7
333
+ ```
334
+
335
+ ### Multi-Provider Configuration
336
+
337
+ ```bash
338
+ # All providers enabled
339
+
340
+ AI_PLUGIN_ENABLED=true
341
+
342
+ # Cloud providers
343
+ OPENAI_API_KEY=sk-proj-key-here
344
+ ANTHROPIC_API_KEY=sk-ant-key-here
345
+
346
+ # Local provider for development
347
+ OLLAMA_BASE_URL=http://localhost:11434
348
+
349
+ # Default to local for development
350
+ DEFAULT_MODEL=llama3.2:3b
351
+
352
+ # Override in production:
353
+ # DEFAULT_MODEL=claude-3-5-haiku-20241022
354
+ ```
355
+
356
+ ## Validation
357
+
358
+ The plugin validates configuration on startup:
359
+
360
+ ```typescript
361
+ import { validateServerPluginEnvironment } from '@/contents/plugins/ai/lib/server-env'
362
+
363
+ const validation = await validateServerPluginEnvironment()
364
+
365
+ if (!validation.valid) {
366
+ console.error('Plugin configuration errors:', validation.errors)
367
+ // Example errors:
368
+ // - "AI_PLUGIN_ENABLED must be 'true' or 'false'"
369
+ // - "No AI provider configured (need OPENAI_API_KEY, ANTHROPIC_API_KEY, or Ollama)"
370
+ }
371
+ ```
372
+
373
+ ## Troubleshooting Configuration
374
+
375
+ ### Issue: "Plugin disabled"
376
+
377
+ **Check:**
378
+ ```bash
379
+ grep AI_PLUGIN_ENABLED contents/plugins/ai/.env
380
+ # Should output: AI_PLUGIN_ENABLED=true
381
+ ```
382
+
383
+ ### Issue: "No provider configured"
384
+
385
+ **Check:**
386
+ ```bash
387
+ # At least one should be set
388
+ grep -E "(OPENAI|ANTHROPIC)_API_KEY" contents/plugins/ai/.env
389
+
390
+ # Or Ollama should be running
391
+ curl http://localhost:11434/api/tags
392
+ ```
393
+
394
+ ### Issue: "Invalid API key format"
395
+
396
+ **OpenAI keys** start with `sk-proj-` (new) or `sk-` (legacy)
397
+ **Anthropic keys** start with `sk-ant-`
398
+
399
+ **Fix:**
400
+ ```bash
401
+ # Copy key exactly as shown in provider dashboard
402
+ # No extra spaces or quotes
403
+ OPENAI_API_KEY=sk-proj-actual-key-here
404
+ ```
405
+
406
+ ## Next Steps
407
+
408
+ ✅ Configuration file created
409
+ ✅ Provider(s) configured
410
+ ✅ Security best practices implemented
411
+ ✅ Validation passing
412
+
413
+ **Continue to:**
414
+ - **[Text Generation](../02-features/01-text-generation.md)** - Use the generate endpoint
415
+ - **[Embeddings](../02-features/02-embeddings.md)** - Generate semantic embeddings
416
+ - **[Core Utilities](../04-advanced-usage/01-core-utilities.md)** - Build custom endpoints