@nextsparkjs/plugin-langchain 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 (67) hide show
  1. package/.env.example +41 -0
  2. package/api/observability/metrics/route.ts +110 -0
  3. package/api/observability/traces/[traceId]/route.ts +398 -0
  4. package/api/observability/traces/route.ts +205 -0
  5. package/api/sessions/route.ts +332 -0
  6. package/components/observability/CollapsibleJson.tsx +71 -0
  7. package/components/observability/CompactTimeline.tsx +75 -0
  8. package/components/observability/ConversationFlow.tsx +271 -0
  9. package/components/observability/DisabledMessage.tsx +21 -0
  10. package/components/observability/FiltersPanel.tsx +82 -0
  11. package/components/observability/ObservabilityDashboard.tsx +230 -0
  12. package/components/observability/SpansList.tsx +210 -0
  13. package/components/observability/TraceDetail.tsx +335 -0
  14. package/components/observability/TraceStatusBadge.tsx +39 -0
  15. package/components/observability/TracesTable.tsx +97 -0
  16. package/components/observability/index.ts +7 -0
  17. package/docs/01-getting-started/01-overview.md +196 -0
  18. package/docs/01-getting-started/02-installation.md +368 -0
  19. package/docs/01-getting-started/03-configuration.md +794 -0
  20. package/docs/02-core-concepts/01-architecture.md +566 -0
  21. package/docs/02-core-concepts/02-agents.md +597 -0
  22. package/docs/02-core-concepts/03-tools.md +689 -0
  23. package/docs/03-orchestration/01-graph-orchestrator.md +809 -0
  24. package/docs/03-orchestration/02-legacy-react.md +650 -0
  25. package/docs/04-advanced/01-observability.md +645 -0
  26. package/docs/04-advanced/02-token-tracking.md +469 -0
  27. package/docs/04-advanced/03-streaming.md +476 -0
  28. package/docs/04-advanced/04-guardrails.md +597 -0
  29. package/docs/05-reference/01-api-reference.md +1403 -0
  30. package/docs/05-reference/02-customization.md +646 -0
  31. package/docs/05-reference/03-examples.md +881 -0
  32. package/docs/index.md +85 -0
  33. package/hooks/observability/useMetrics.ts +31 -0
  34. package/hooks/observability/useTraceDetail.ts +48 -0
  35. package/hooks/observability/useTraces.ts +59 -0
  36. package/lib/agent-factory.ts +354 -0
  37. package/lib/agent-helpers.ts +201 -0
  38. package/lib/db-memory-store.ts +417 -0
  39. package/lib/graph/index.ts +58 -0
  40. package/lib/graph/nodes/combiner.ts +399 -0
  41. package/lib/graph/nodes/router.ts +440 -0
  42. package/lib/graph/orchestrator-graph.ts +386 -0
  43. package/lib/graph/prompts/combiner.md +131 -0
  44. package/lib/graph/prompts/router.md +193 -0
  45. package/lib/graph/types.ts +365 -0
  46. package/lib/guardrails.ts +230 -0
  47. package/lib/index.ts +44 -0
  48. package/lib/logger.ts +70 -0
  49. package/lib/memory-store.ts +168 -0
  50. package/lib/message-serializer.ts +110 -0
  51. package/lib/prompt-renderer.ts +94 -0
  52. package/lib/providers.ts +226 -0
  53. package/lib/streaming.ts +232 -0
  54. package/lib/token-tracker.ts +298 -0
  55. package/lib/tools-builder.ts +192 -0
  56. package/lib/tracer-callbacks.ts +342 -0
  57. package/lib/tracer.ts +350 -0
  58. package/migrations/001_langchain_memory.sql +83 -0
  59. package/migrations/002_token_usage.sql +127 -0
  60. package/migrations/003_observability.sql +257 -0
  61. package/package.json +28 -0
  62. package/plugin.config.ts +170 -0
  63. package/presets/lib/langchain.config.ts.preset +142 -0
  64. package/presets/templates/sector7/ai-observability/[traceId]/page.tsx +91 -0
  65. package/presets/templates/sector7/ai-observability/page.tsx +54 -0
  66. package/types/langchain.types.ts +274 -0
  67. package/types/observability.types.ts +270 -0
@@ -0,0 +1,597 @@
1
+ # Guardrails & Security
2
+
3
+ This guide covers the security middleware for AI agents, including prompt injection detection, PII masking, and content filtering.
4
+
5
+ ## Overview
6
+
7
+ Guardrails provide **three layers of protection**:
8
+
9
+ ```
10
+ User Input → [Injection Check] → [PII Mask] → Agent → [Content Filter] → Response
11
+ ↓ ↓ ↓
12
+ Block/Warn Redact Block/Redact
13
+ ```
14
+
15
+ **Key Features**:
16
+ - **Prompt Injection Detection**: Blocks attempts to manipulate agent behavior
17
+ - **PII Masking**: Redacts sensitive personal information
18
+ - **Content Filtering**: Filters inappropriate AI outputs
19
+ - **Configurable Actions**: Block, warn, log, or redact
20
+
21
+ ---
22
+
23
+ ## Architecture
24
+
25
+ ### Processing Flow
26
+
27
+ ```
28
+ ┌─────────────────────────────────────────────────────────────┐
29
+ │ INPUT PROCESSING │
30
+ │ │
31
+ │ User Input │
32
+ │ │ │
33
+ │ ▼ │
34
+ │ ┌──────────────────┐ │
35
+ │ │ Injection Check │──blocked?──► Error: Input blocked │
36
+ │ └────────┬─────────┘ │
37
+ │ │ (safe) │
38
+ │ ▼ │
39
+ │ ┌──────────────────┐ │
40
+ │ │ PII Masking │ │
41
+ │ └────────┬─────────┘ │
42
+ │ │ │
43
+ │ ▼ │
44
+ │ Processed Input → Agent │
45
+ └─────────────────────────────────────────────────────────────┘
46
+
47
+ ┌─────────────────────────────────────────────────────────────┐
48
+ │ OUTPUT PROCESSING │
49
+ │ │
50
+ │ Agent Output │
51
+ │ │ │
52
+ │ ▼ │
53
+ │ ┌──────────────────┐ │
54
+ │ │ Content Filter │──blocked?──► Empty response │
55
+ │ └────────┬─────────┘ │
56
+ │ │ │
57
+ │ ▼ │
58
+ │ Filtered Output → User │
59
+ └─────────────────────────────────────────────────────────────┘
60
+ ```
61
+
62
+ ---
63
+
64
+ ## Configuration
65
+
66
+ Configure guardrails in `langchain.config.ts`:
67
+
68
+ ```typescript
69
+ export const langchainConfig = {
70
+ guardrails: {
71
+ promptInjection: {
72
+ enabled: true,
73
+ action: 'block', // 'block' | 'warn' | 'log'
74
+ customPatterns: [], // Additional regex patterns
75
+ },
76
+ piiMasking: {
77
+ enabled: true,
78
+ types: ['email', 'phone', 'ssn', 'creditCard', 'ipAddress'],
79
+ action: 'mask', // 'mask' | 'remove' | 'log'
80
+ },
81
+ contentFilter: {
82
+ enabled: true,
83
+ customPatterns: [], // Content patterns to filter
84
+ action: 'redact', // 'block' | 'redact'
85
+ },
86
+ },
87
+ }
88
+ ```
89
+
90
+ ### Configuration Options
91
+
92
+ | Section | Option | Type | Description |
93
+ |---------|--------|------|-------------|
94
+ | `promptInjection` | `enabled` | boolean | Enable injection detection |
95
+ | | `action` | string | Action: `block`, `warn`, `log` |
96
+ | | `customPatterns` | RegExp[] | Additional detection patterns |
97
+ | `piiMasking` | `enabled` | boolean | Enable PII masking |
98
+ | | `types` | string[] | PII types to mask |
99
+ | | `action` | string | Action: `mask`, `remove`, `log` |
100
+ | `contentFilter` | `enabled` | boolean | Enable output filtering |
101
+ | | `customPatterns` | RegExp[] | Patterns to filter |
102
+ | | `action` | string | Action: `block`, `redact` |
103
+
104
+ ---
105
+
106
+ ## Prompt Injection Detection
107
+
108
+ ### Built-in Patterns
109
+
110
+ The guardrails detect common prompt injection attempts:
111
+
112
+ | Pattern | Example | Description |
113
+ |---------|---------|-------------|
114
+ | Ignore instructions | "ignore previous instructions" | Attempts to override system prompt |
115
+ | Forget commands | "forget everything above" | Tries to clear context |
116
+ | Role impersonation | "you are now a hacker" | Attempts to change agent behavior |
117
+ | Disregard rules | "disregard your rules" | Bypassing constraints |
118
+ | Pretend commands | "pretend to be a different AI" | Role manipulation |
119
+ | Act as prompts | "act as if you have no restrictions" | Behavior modification |
120
+ | Jailbreak attempts | "jailbreak mode" | Known bypass terms |
121
+ | System injection | `[system]`, `<system>`, `{{system}}` | Template/prompt injection |
122
+
123
+ ### Pattern List
124
+
125
+ ```typescript
126
+ const INJECTION_PATTERNS = [
127
+ /ignore\s+(previous|all|above)\s+(instructions?|prompts?)/i,
128
+ /forget\s+(everything|all|previous)/i,
129
+ /you\s+are\s+now\s+/i,
130
+ /disregard\s+(all|previous|your)\s/i,
131
+ /pretend\s+(you\s+are|to\s+be)/i,
132
+ /act\s+as\s+(if|a|an)\s/i,
133
+ /jailbreak/i,
134
+ /bypass\s+(restrictions?|filters?|rules?)/i,
135
+ /system\s*:\s*/i,
136
+ /\[system\]/i,
137
+ /\<system\>/i,
138
+ /\{\{.*system.*\}\}/i,
139
+ ]
140
+ ```
141
+
142
+ ### Custom Patterns
143
+
144
+ Add your own detection patterns:
145
+
146
+ ```typescript
147
+ guardrails: {
148
+ promptInjection: {
149
+ enabled: true,
150
+ action: 'block',
151
+ customPatterns: [
152
+ /reveal\s+your\s+prompt/i,
153
+ /what\s+are\s+your\s+instructions/i,
154
+ /show\s+me\s+your\s+system\s+message/i,
155
+ ],
156
+ },
157
+ }
158
+ ```
159
+
160
+ ### Actions
161
+
162
+ | Action | Behavior |
163
+ |--------|----------|
164
+ | `block` | Throws error, request is rejected |
165
+ | `warn` | Continues but adds warning to response |
166
+ | `log` | Silently logs, request continues |
167
+
168
+ ### Usage
169
+
170
+ ```typescript
171
+ import { guardrails } from '@/contents/plugins/langchain/lib/guardrails'
172
+
173
+ const result = guardrails.checkInjection(
174
+ userInput,
175
+ config.guardrails.promptInjection
176
+ )
177
+
178
+ if (!result.safe) {
179
+ console.log('Injection detected:', result.reason)
180
+ console.log('Pattern matched:', result.pattern)
181
+ }
182
+ ```
183
+
184
+ ---
185
+
186
+ ## PII Masking
187
+
188
+ ### Supported PII Types
189
+
190
+ | Type | Pattern | Example | Masked |
191
+ |------|---------|---------|--------|
192
+ | `email` | Standard email format | john@example.com | jo**********om |
193
+ | `phone` | US phone numbers | (555) 123-4567 | (5********67 |
194
+ | `ssn` | Social Security Number | 123-45-6789 | 12*****89 |
195
+ | `creditCard` | Credit card numbers | 1234-5678-9012-3456 | 12**************56 |
196
+ | `ipAddress` | IPv4 addresses | 192.168.1.100 | 19*******00 |
197
+
198
+ ### Masking Actions
199
+
200
+ | Action | Input | Output |
201
+ |--------|-------|--------|
202
+ | `mask` | john@example.com | jo**********om |
203
+ | `remove` | john@example.com | [REDACTED] |
204
+ | `log` | john@example.com | john@example.com (logged) |
205
+
206
+ ### Usage
207
+
208
+ ```typescript
209
+ import { guardrails } from '@/contents/plugins/langchain/lib/guardrails'
210
+
211
+ const result = guardrails.maskPII(userInput, {
212
+ enabled: true,
213
+ types: ['email', 'phone', 'creditCard'],
214
+ action: 'mask',
215
+ })
216
+
217
+ console.log(result.masked) // Text with PII masked
218
+ console.log(result.hasPII) // true/false
219
+ console.log(result.mappings) // [{original, masked, type}]
220
+ ```
221
+
222
+ ### Response Structure
223
+
224
+ ```typescript
225
+ interface PIIMaskResult {
226
+ masked: string // Text with PII masked
227
+ mappings: Array<{ // Details of masked items
228
+ original: string // Original value
229
+ masked: string // Masked value
230
+ type: string // PII type (email, phone, etc.)
231
+ }>
232
+ hasPII: boolean // Whether PII was found
233
+ }
234
+ ```
235
+
236
+ ---
237
+
238
+ ## Content Filtering
239
+
240
+ ### Purpose
241
+
242
+ Filter AI outputs to prevent:
243
+ - Harmful content generation
244
+ - Inappropriate responses
245
+ - Sensitive information disclosure
246
+
247
+ ### Custom Patterns
248
+
249
+ ```typescript
250
+ guardrails: {
251
+ contentFilter: {
252
+ enabled: true,
253
+ action: 'redact',
254
+ customPatterns: [
255
+ /password\s*[:=]\s*\S+/gi, // Password exposure
256
+ /api[_-]?key\s*[:=]\s*\S+/gi, // API key exposure
257
+ /secret\s*[:=]\s*\S+/gi, // Secret exposure
258
+ ],
259
+ },
260
+ }
261
+ ```
262
+
263
+ ### Actions
264
+
265
+ | Action | Behavior |
266
+ |--------|----------|
267
+ | `block` | Returns empty response |
268
+ | `redact` | Replaces matched content with `[FILTERED]` |
269
+
270
+ ### Usage
271
+
272
+ ```typescript
273
+ import { guardrails } from '@/contents/plugins/langchain/lib/guardrails'
274
+
275
+ const result = guardrails.filterContent(
276
+ aiOutput,
277
+ config.guardrails.contentFilter
278
+ )
279
+
280
+ if (result.blocked) {
281
+ console.log('Content blocked:', result.reason)
282
+ } else {
283
+ console.log('Filtered output:', result.filtered)
284
+ }
285
+ ```
286
+
287
+ ---
288
+
289
+ ## Complete Pipeline
290
+
291
+ ### processInput
292
+
293
+ Runs all input guardrails:
294
+
295
+ ```typescript
296
+ import { guardrails } from '@/contents/plugins/langchain/lib/guardrails'
297
+
298
+ try {
299
+ const { processed, warnings } = await guardrails.processInput(
300
+ userInput,
301
+ config.guardrails
302
+ )
303
+
304
+ if (warnings.length > 0) {
305
+ console.log('Warnings:', warnings)
306
+ }
307
+
308
+ // Use 'processed' for agent input
309
+ const response = await agent.invoke(processed)
310
+ } catch (error) {
311
+ // Input was blocked
312
+ console.error('Input blocked:', error.message)
313
+ }
314
+ ```
315
+
316
+ ### processOutput
317
+
318
+ Runs output guardrails:
319
+
320
+ ```typescript
321
+ const { processed, blocked } = await guardrails.processOutput(
322
+ agentOutput,
323
+ config.guardrails
324
+ )
325
+
326
+ if (blocked) {
327
+ return { response: 'Unable to generate appropriate response.' }
328
+ }
329
+
330
+ return { response: processed }
331
+ ```
332
+
333
+ ---
334
+
335
+ ## Integration with Orchestrator
336
+
337
+ ### Automatic Integration
338
+
339
+ When guardrails are enabled, they're automatically applied:
340
+
341
+ ```typescript
342
+ // In agent factory or orchestrator
343
+ const guardrailsConfig = langchainConfig.guardrails
344
+
345
+ // Before agent invocation
346
+ const { processed: safeInput, warnings } = await guardrails.processInput(
347
+ message,
348
+ guardrailsConfig
349
+ )
350
+
351
+ // After agent response
352
+ const { processed: safeOutput, blocked } = await guardrails.processOutput(
353
+ response,
354
+ guardrailsConfig
355
+ )
356
+ ```
357
+
358
+ ### Graph Node Integration
359
+
360
+ ```typescript
361
+ // In router or handler nodes
362
+ async function routerNode(state: OrchestratorState) {
363
+ const config = langchainConfig.guardrails
364
+
365
+ // Check input before processing
366
+ if (config.promptInjection?.enabled) {
367
+ const check = guardrails.checkInjection(
368
+ state.messages[state.messages.length - 1].content,
369
+ config.promptInjection
370
+ )
371
+
372
+ if (!check.safe && config.promptInjection.action === 'block') {
373
+ return {
374
+ error: 'Your message was blocked for security reasons.',
375
+ intents: [],
376
+ }
377
+ }
378
+ }
379
+
380
+ // Continue with normal processing
381
+ // ...
382
+ }
383
+ ```
384
+
385
+ ---
386
+
387
+ ## API Response Types
388
+
389
+ ### InjectionCheckResult
390
+
391
+ ```typescript
392
+ interface InjectionCheckResult {
393
+ safe: boolean // Whether input is safe
394
+ reason?: string // Reason if blocked
395
+ pattern?: string // Pattern that matched
396
+ }
397
+ ```
398
+
399
+ ### PIIMaskResult
400
+
401
+ ```typescript
402
+ interface PIIMaskResult {
403
+ masked: string // Text with PII masked
404
+ mappings: Array<{
405
+ original: string // Original PII value
406
+ masked: string // Masked value
407
+ type: string // PII type
408
+ }>
409
+ hasPII: boolean // Whether PII was detected
410
+ }
411
+ ```
412
+
413
+ ### ContentFilterResult
414
+
415
+ ```typescript
416
+ interface ContentFilterResult {
417
+ filtered: string // Filtered output
418
+ blocked: boolean // Whether content was blocked
419
+ reason?: string // Reason if blocked
420
+ }
421
+ ```
422
+
423
+ ---
424
+
425
+ ## Best Practices
426
+
427
+ ### 1. Defense in Depth
428
+
429
+ Enable multiple layers:
430
+
431
+ ```typescript
432
+ guardrails: {
433
+ promptInjection: { enabled: true, action: 'block' },
434
+ piiMasking: { enabled: true, action: 'mask' },
435
+ contentFilter: { enabled: true, action: 'redact' },
436
+ }
437
+ ```
438
+
439
+ ### 2. Start Permissive, Tighten Gradually
440
+
441
+ ```typescript
442
+ // Development: log only
443
+ guardrails: {
444
+ promptInjection: { enabled: true, action: 'log' },
445
+ }
446
+
447
+ // Production: block
448
+ guardrails: {
449
+ promptInjection: { enabled: true, action: 'block' },
450
+ }
451
+ ```
452
+
453
+ ### 3. Monitor Warnings
454
+
455
+ Log warnings for review:
456
+
457
+ ```typescript
458
+ const { processed, warnings } = await guardrails.processInput(input, config)
459
+
460
+ if (warnings.length > 0) {
461
+ await logger.warn('Guardrail warnings', {
462
+ userId: context.userId,
463
+ warnings,
464
+ inputPreview: input.slice(0, 100),
465
+ })
466
+ }
467
+ ```
468
+
469
+ ### 4. Custom Patterns for Your Domain
470
+
471
+ Add domain-specific patterns:
472
+
473
+ ```typescript
474
+ // For a financial app
475
+ customPatterns: [
476
+ /transfer\s+all\s+funds/i,
477
+ /send\s+money\s+to\s+\d+/i,
478
+ ]
479
+
480
+ // For a customer service app
481
+ customPatterns: [
482
+ /reveal\s+customer\s+data/i,
483
+ /show\s+all\s+users/i,
484
+ ]
485
+ ```
486
+
487
+ ### 5. Handle Blocked Requests Gracefully
488
+
489
+ ```typescript
490
+ try {
491
+ const result = await guardrails.processInput(input, config)
492
+ // Process normally
493
+ } catch (error) {
494
+ // User-friendly response
495
+ return {
496
+ success: false,
497
+ message: 'Your request could not be processed. Please rephrase.',
498
+ code: 'GUARDRAIL_BLOCK',
499
+ }
500
+ }
501
+ ```
502
+
503
+ ---
504
+
505
+ ## Troubleshooting
506
+
507
+ ### False Positives
508
+
509
+ If legitimate requests are being blocked:
510
+
511
+ 1. Check which pattern matched:
512
+ ```typescript
513
+ const result = guardrails.checkInjection(input, config)
514
+ console.log('Pattern:', result.pattern)
515
+ ```
516
+
517
+ 2. Adjust patterns or use `warn` action:
518
+ ```typescript
519
+ guardrails: {
520
+ promptInjection: { action: 'warn' } // Log but don't block
521
+ }
522
+ ```
523
+
524
+ ### PII Not Detected
525
+
526
+ If PII is not being masked:
527
+
528
+ 1. Ensure the type is enabled:
529
+ ```typescript
530
+ piiMasking: {
531
+ types: ['email', 'phone'], // Add missing types
532
+ }
533
+ ```
534
+
535
+ 2. Check pattern matches your format:
536
+ ```typescript
537
+ // For international phone numbers, add custom pattern
538
+ // The built-in pattern is optimized for US formats
539
+ ```
540
+
541
+ ### Performance Impact
542
+
543
+ Guardrails add minimal overhead:
544
+ - Injection check: < 1ms (regex matching)
545
+ - PII masking: < 5ms (depends on input length)
546
+ - Content filtering: < 1ms
547
+
548
+ For high-throughput scenarios, consider:
549
+ - Sampling (process 10% of requests)
550
+ - Caching results for identical inputs
551
+ - Async logging with buffering
552
+
553
+ ---
554
+
555
+ ## Security Considerations
556
+
557
+ ### Pattern Limitations
558
+
559
+ Regex-based detection has limitations:
560
+ - May miss sophisticated injection attempts
561
+ - Can be bypassed with encoding/obfuscation
562
+ - Cannot understand semantic intent
563
+
564
+ **Recommendation**: Combine with:
565
+ - Input length limits
566
+ - Rate limiting
567
+ - User authentication
568
+ - Logging and monitoring
569
+
570
+ ### PII Mapping Security
571
+
572
+ The `mappings` array contains original PII values:
573
+
574
+ ```typescript
575
+ const result = guardrails.maskPII(input, config)
576
+ // result.mappings contains original values - handle securely!
577
+ ```
578
+
579
+ **Do not**:
580
+ - Log the mappings array
581
+ - Store mappings in database
582
+ - Return mappings to client
583
+
584
+ ### Content Filter Effectiveness
585
+
586
+ Content filtering is a final safeguard, not a primary defense:
587
+ - Configure model with appropriate system prompts
588
+ - Use model-level content policies
589
+ - Combine with output validation
590
+
591
+ ---
592
+
593
+ ## Related Documentation
594
+
595
+ - [Graph Orchestrator](../03-orchestration/01-graph-orchestrator.md) - How guardrails integrate with the graph
596
+ - [Observability](./01-observability.md) - Logging guardrail events
597
+ - [Configuration](../01-getting-started/03-configuration.md) - Full configuration reference