@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,646 @@
1
+ # Advanced Customization
2
+
3
+ This guide covers advanced customization techniques for building sophisticated AI agent systems tailored to different product types and complexity levels.
4
+
5
+ ## Customization Philosophy
6
+
7
+ The LangChain plugin is designed to be **flexible and extensible**. While presets provide a starting point, real-world applications often require custom behavior based on:
8
+
9
+ - **Product Type**: CRM, CMS, productivity app, e-commerce
10
+ - **Complexity Level**: Simple assistant vs. enterprise multi-agent
11
+ - **User Needs**: Technical users vs. non-technical users
12
+ - **Domain Requirements**: Industry-specific terminology and workflows
13
+
14
+ ## Orchestration Complexity Levels
15
+
16
+ ### Level 1: Single Agent (Simplest)
17
+
18
+ **Best for:** MVPs, prototypes, simple tools
19
+
20
+ ```
21
+ User → Single Agent → Response
22
+ ```
23
+
24
+ **Implementation:**
25
+
26
+ ```typescript
27
+ // One agent with all tools
28
+ export const AGENTS = {
29
+ 'assistant': {
30
+ provider: 'ollama',
31
+ temperature: 0.3,
32
+ systemPrompt: 'unified-assistant',
33
+ createTools: (ctx) => [
34
+ ...createTaskTools(ctx),
35
+ ...createCustomerTools(ctx),
36
+ ...createPageTools(ctx),
37
+ ],
38
+ },
39
+ }
40
+ ```
41
+
42
+ **Prompt Pattern:**
43
+
44
+ ```markdown
45
+ You are a helpful assistant with access to tasks, customers, and pages.
46
+
47
+ ## Available Tools
48
+ - Task tools: list_tasks, create_task, update_task, ...
49
+ - Customer tools: list_customers, search_customers, ...
50
+ - Page tools: list_pages, create_page, ...
51
+
52
+ ## Guidelines
53
+ - Use the appropriate tool based on user request
54
+ - Always use tools to access data
55
+ - Format responses clearly
56
+ ```
57
+
58
+ **Pros:** Simple, fast, low latency
59
+ **Cons:** All tools in one context, potential confusion with many tools
60
+
61
+ ---
62
+
63
+ ### Level 2: Keyword-Based Router
64
+
65
+ **Best for:** Small teams, clear domain separation
66
+
67
+ ```
68
+ User → Router (keywords) → Specialized Agent → Response
69
+ ```
70
+
71
+ **Implementation:**
72
+
73
+ ```typescript
74
+ export const AGENTS = {
75
+ 'orchestrator': {
76
+ provider: 'ollama',
77
+ temperature: 0.1,
78
+ systemPrompt: 'keyword-router',
79
+ createTools: () => createRoutingTools(),
80
+ },
81
+ 'task-assistant': { ... },
82
+ 'customer-assistant': { ... },
83
+ }
84
+ ```
85
+
86
+ **Prompt Pattern:**
87
+
88
+ ```markdown
89
+ You route requests based on keywords.
90
+
91
+ ## Routing Rules
92
+
93
+ | Keywords | Route To |
94
+ |----------|----------|
95
+ | task, tarea, todo, pendiente | route_to_task |
96
+ | customer, cliente, client, account | route_to_customer |
97
+ | page, página, content, block | route_to_page |
98
+
99
+ ## Rules
100
+ - Match the FIRST keyword found
101
+ - If no keywords match, ask for clarification
102
+ - ONLY route or greet, never perform actions directly
103
+ ```
104
+
105
+ **Pros:** Predictable, fast routing
106
+ **Cons:** Can miss intent with paraphrased requests
107
+
108
+ ---
109
+
110
+ ### Level 3: Intent-Based Router
111
+
112
+ **Best for:** Medium complexity, varied user input
113
+
114
+ ```
115
+ User → Router (intent analysis) → Specialized Agent → Response
116
+ ```
117
+
118
+ **Implementation:**
119
+
120
+ ```typescript
121
+ export const AGENTS = {
122
+ 'orchestrator': {
123
+ provider: 'openai', // Better model for intent analysis
124
+ model: 'gpt-4o-mini',
125
+ temperature: 0.1,
126
+ systemPrompt: 'intent-router',
127
+ createTools: () => createRoutingTools(),
128
+ },
129
+ // ... specialized agents
130
+ }
131
+ ```
132
+
133
+ **Prompt Pattern:**
134
+
135
+ ```markdown
136
+ You analyze user intent and route to the appropriate agent.
137
+
138
+ ## Intent Classification
139
+
140
+ ### Task-Related Intents:
141
+ - TASK_LIST: User wants to see their tasks
142
+ - TASK_CREATE: User wants to create a new task
143
+ - TASK_UPDATE: User wants to modify an existing task
144
+ - TASK_SEARCH: User is looking for a specific task
145
+
146
+ ### Customer-Related Intents:
147
+ - CUSTOMER_INFO: User wants customer details
148
+ - CUSTOMER_SEARCH: User is looking for customers
149
+ - CUSTOMER_UPDATE: User wants to modify customer data
150
+
151
+ ### Page-Related Intents:
152
+ - PAGE_CREATE: User wants to create content
153
+ - PAGE_EDIT: User wants to modify pages/blocks
154
+
155
+ ## Context Awareness
156
+
157
+ When the user uses pronouns or references:
158
+ - "it", "this", "that" → Check conversation history
159
+ - "the same", "that one" → Reference previous entity
160
+ - "modify it", "update it" → Route to same agent as last operation
161
+
162
+ ## Ambiguity Handling
163
+
164
+ If intent is unclear between two domains:
165
+ 1. Check conversation context for clues
166
+ 2. If still unclear, use ask_clarification tool
167
+ ```
168
+
169
+ **Pros:** Handles varied input, context-aware
170
+ **Cons:** Higher latency, requires better model
171
+
172
+ ---
173
+
174
+ ### Level 4: Hierarchical Multi-Agent
175
+
176
+ **Best for:** Enterprise applications, complex domains
177
+
178
+ ```
179
+ User → Domain Router → Domain Orchestrator → Specialized Agent → Response
180
+ ```
181
+
182
+ **Implementation:**
183
+
184
+ ```typescript
185
+ export const AGENTS = {
186
+ // Top-level router
187
+ 'main-orchestrator': {
188
+ provider: 'openai',
189
+ model: 'gpt-4o-mini',
190
+ temperature: 0.1,
191
+ systemPrompt: 'main-orchestrator',
192
+ createTools: () => createDomainRoutingTools(),
193
+ },
194
+
195
+ // Domain orchestrators
196
+ 'crm-orchestrator': {
197
+ provider: 'ollama',
198
+ temperature: 0.1,
199
+ systemPrompt: 'crm-orchestrator',
200
+ createTools: () => createCrmRoutingTools(),
201
+ },
202
+ 'content-orchestrator': {
203
+ provider: 'ollama',
204
+ temperature: 0.1,
205
+ systemPrompt: 'content-orchestrator',
206
+ createTools: () => createContentRoutingTools(),
207
+ },
208
+
209
+ // Specialized agents
210
+ 'lead-specialist': { ... },
211
+ 'deal-specialist': { ... },
212
+ 'page-specialist': { ... },
213
+ 'blog-specialist': { ... },
214
+ }
215
+ ```
216
+
217
+ **Pros:** Handles complex domains, scales well
218
+ **Cons:** Higher latency, more complex maintenance
219
+
220
+ ---
221
+
222
+ ### Level 5: Adaptive Agent System
223
+
224
+ **Best for:** AI-first products, dynamic workflows
225
+
226
+ ```
227
+ User → Meta-Orchestrator → [Create/Select/Combine Agents] → Response
228
+ ```
229
+
230
+ **Implementation:**
231
+
232
+ ```typescript
233
+ // Dynamic agent creation based on user needs
234
+ async function createAdaptiveAgent(userProfile, context) {
235
+ const capabilities = analyzeRequiredCapabilities(userProfile)
236
+
237
+ return await createAgent({
238
+ sessionId: context.sessionId,
239
+ systemPrompt: generateDynamicPrompt(capabilities),
240
+ tools: selectRelevantTools(capabilities, context),
241
+ modelConfig: selectOptimalModel(capabilities),
242
+ context,
243
+ })
244
+ }
245
+ ```
246
+
247
+ **Pros:** Highly personalized, efficient
248
+ **Cons:** Complex implementation, requires careful testing
249
+
250
+ ## Product-Specific Patterns
251
+
252
+ ### CRM Application
253
+
254
+ **Entities:** Leads, Contacts, Deals, Activities
255
+
256
+ ```typescript
257
+ export const AGENTS = {
258
+ 'sales-assistant': {
259
+ provider: 'openai',
260
+ temperature: 0.3,
261
+ systemPrompt: 'sales-assistant',
262
+ createTools: (ctx) => [
263
+ ...createLeadTools(ctx),
264
+ ...createContactTools(ctx),
265
+ ...createDealTools(ctx),
266
+ ...createActivityTools(ctx),
267
+ ],
268
+ },
269
+ }
270
+ ```
271
+
272
+ **Prompt Features:**
273
+
274
+ ```markdown
275
+ ## Sales Context Awareness
276
+
277
+ When user mentions:
278
+ - "prospect", "lead" → Lead entity
279
+ - "contact", "person" → Contact entity
280
+ - "deal", "opportunity" → Deal entity
281
+ - "call", "meeting", "email" → Activity entity
282
+
283
+ ## Sales Workflow Understanding
284
+
285
+ - Leads can be converted to Contacts
286
+ - Contacts are associated with Deals
287
+ - Activities are logged against Deals or Contacts
288
+
289
+ ## Proactive Suggestions
290
+
291
+ After showing a lead, suggest:
292
+ - "Would you like to convert this lead?"
293
+ - "Should I log a follow-up activity?"
294
+ ```
295
+
296
+ ---
297
+
298
+ ### Content Management System
299
+
300
+ **Entities:** Pages, Posts, Blocks, Media
301
+
302
+ ```typescript
303
+ export const AGENTS = {
304
+ 'content-orchestrator': {
305
+ temperature: 0.1,
306
+ createTools: () => createContentRoutingTools(),
307
+ },
308
+ 'page-editor': {
309
+ temperature: 0.5, // Higher for creative content
310
+ createTools: (ctx) => createPageTools(ctx),
311
+ },
312
+ 'blog-writer': {
313
+ temperature: 0.7, // Even higher for blog content
314
+ createTools: (ctx) => createBlogTools(ctx),
315
+ },
316
+ }
317
+ ```
318
+
319
+ **Prompt Features:**
320
+
321
+ ```markdown
322
+ ## Content Types
323
+
324
+ - **Pages**: Landing pages, static content
325
+ - **Posts**: Blog articles, news
326
+ - **Blocks**: Reusable content components
327
+
328
+ ## Creative Assistance
329
+
330
+ When asked to create content:
331
+ 1. Ask about tone (formal, casual, persuasive)
332
+ 2. Ask about target audience
333
+ 3. Generate multiple options when appropriate
334
+
335
+ ## SEO Awareness
336
+
337
+ Always suggest:
338
+ - Meta titles and descriptions
339
+ - Keyword placement
340
+ - Alt text for images
341
+ ```
342
+
343
+ ---
344
+
345
+ ### E-Commerce Platform
346
+
347
+ **Entities:** Products, Orders, Customers, Inventory
348
+
349
+ ```typescript
350
+ export const AGENTS = {
351
+ 'commerce-orchestrator': {
352
+ temperature: 0.1,
353
+ createTools: () => createCommerceRoutingTools(),
354
+ },
355
+ 'product-manager': {
356
+ temperature: 0.3,
357
+ createTools: (ctx) => [
358
+ ...createProductTools(ctx),
359
+ ...createInventoryTools(ctx),
360
+ ],
361
+ },
362
+ 'order-specialist': {
363
+ temperature: 0.2, // Lower for accuracy
364
+ createTools: (ctx) => [
365
+ ...createOrderTools(ctx),
366
+ ...createShippingTools(ctx),
367
+ ],
368
+ },
369
+ }
370
+ ```
371
+
372
+ **Prompt Features:**
373
+
374
+ ```markdown
375
+ ## Commerce Context
376
+
377
+ - Products have variants (size, color)
378
+ - Inventory is tracked per variant
379
+ - Orders can have multiple items
380
+
381
+ ## Critical Operations
382
+
383
+ Before any order modification:
384
+ 1. Verify current order status
385
+ 2. Check inventory availability
386
+ 3. Confirm with user if total changes
387
+
388
+ ## Customer Service Mode
389
+
390
+ When user mentions complaint or issue:
391
+ - Show empathy
392
+ - Offer resolution options
393
+ - Log the interaction
394
+ ```
395
+
396
+ ---
397
+
398
+ ### Project Management Tool
399
+
400
+ **Entities:** Projects, Tasks, Sprints, Team Members
401
+
402
+ ```typescript
403
+ export const AGENTS = {
404
+ 'pm-assistant': {
405
+ provider: 'openai',
406
+ temperature: 0.3,
407
+ createTools: (ctx) => [
408
+ ...createProjectTools(ctx),
409
+ ...createTaskTools(ctx),
410
+ ...createSprintTools(ctx),
411
+ ...createTeamTools(ctx),
412
+ ],
413
+ },
414
+ }
415
+ ```
416
+
417
+ **Prompt Features:**
418
+
419
+ ```markdown
420
+ ## Project Structure
421
+
422
+ - Projects contain Sprints
423
+ - Sprints contain Tasks
424
+ - Tasks are assigned to Team Members
425
+
426
+ ## Agile Awareness
427
+
428
+ Understand:
429
+ - Sprint planning and capacity
430
+ - Story points and velocity
431
+ - Blockers and dependencies
432
+
433
+ ## Reporting Capabilities
434
+
435
+ Can generate:
436
+ - Sprint burndown status
437
+ - Task completion reports
438
+ - Team workload distribution
439
+ ```
440
+
441
+ ## Advanced Prompt Techniques
442
+
443
+ ### Dynamic Context Injection
444
+
445
+ ```typescript
446
+ function buildDynamicPrompt(basePrompt: string, context: any): string {
447
+ let prompt = basePrompt
448
+
449
+ // Inject user preferences
450
+ if (context.userPreferences) {
451
+ prompt += `\n\n## User Preferences\n${context.userPreferences}`
452
+ }
453
+
454
+ // Inject recent activity
455
+ if (context.recentActivity) {
456
+ prompt += `\n\n## Recent Activity\n${context.recentActivity}`
457
+ }
458
+
459
+ return prompt
460
+ }
461
+ ```
462
+
463
+ ### Conditional Tool Loading
464
+
465
+ ```typescript
466
+ createTools: (context) => {
467
+ const tools = [...coreTools(context)]
468
+
469
+ // Add admin tools for admins
470
+ if (context.userRole === 'admin') {
471
+ tools.push(...adminTools(context))
472
+ }
473
+
474
+ // Add premium tools for paid users
475
+ if (context.isPremium) {
476
+ tools.push(...premiumTools(context))
477
+ }
478
+
479
+ return tools
480
+ }
481
+ ```
482
+
483
+ ### Personalized Responses
484
+
485
+ ```markdown
486
+ ## Response Personalization
487
+
488
+ Based on user profile:
489
+ - **New users**: Provide more explanation, offer tips
490
+ - **Power users**: Be concise, skip basics
491
+ - **Technical users**: Include IDs, technical details
492
+ - **Non-technical users**: Use simple language, avoid jargon
493
+ ```
494
+
495
+ ## Model Selection Strategy
496
+
497
+ ### By Task Type
498
+
499
+ | Task Type | Recommended Model | Reasoning |
500
+ |-----------|-------------------|-----------|
501
+ | Routing | gpt-4o-mini / qwen2.5:7b | Fast, accurate for classification |
502
+ | Data CRUD | qwen2.5:7b / llama3.2:3b | Simple operations, low latency |
503
+ | Creative | gpt-4o / claude-3-sonnet | Better quality for content |
504
+ | Analysis | gpt-4o | Complex reasoning |
505
+
506
+ ### By Importance
507
+
508
+ ```typescript
509
+ export const AGENTS = {
510
+ // Critical path - use best model
511
+ 'checkout-assistant': {
512
+ provider: 'openai',
513
+ model: 'gpt-4o',
514
+ temperature: 0.1,
515
+ },
516
+
517
+ // Standard operations - use fast model
518
+ 'inventory-checker': {
519
+ provider: 'ollama',
520
+ temperature: 0.2,
521
+ },
522
+
523
+ // Background tasks - use cheapest
524
+ 'report-generator': {
525
+ provider: 'ollama',
526
+ temperature: 0.3,
527
+ },
528
+ }
529
+ ```
530
+
531
+ ## Error Handling Strategies
532
+
533
+ ### Graceful Degradation
534
+
535
+ ```typescript
536
+ async function processWithFallback(message, context) {
537
+ try {
538
+ // Try orchestrated flow
539
+ return await processWithOrchestrator(message, context)
540
+ } catch (orchestratorError) {
541
+ console.warn('Orchestrator failed, falling back to single agent')
542
+
543
+ try {
544
+ // Fall back to single agent
545
+ return await processSingleAgent(message, context)
546
+ } catch (singleError) {
547
+ // Last resort - provide helpful error
548
+ return {
549
+ content: 'Lo siento, estoy teniendo problemas. Por favor intenta de nuevo.',
550
+ error: true,
551
+ }
552
+ }
553
+ }
554
+ }
555
+ ```
556
+
557
+ ### Retry with Different Model
558
+
559
+ ```typescript
560
+ async function chatWithRetry(agent, message, maxRetries = 2) {
561
+ for (let i = 0; i <= maxRetries; i++) {
562
+ try {
563
+ return await agent.chat(message)
564
+ } catch (error) {
565
+ if (i === maxRetries) throw error
566
+
567
+ // Try with different model on retry
568
+ agent = await createAgentWithFallbackModel(agent.config)
569
+ }
570
+ }
571
+ }
572
+ ```
573
+
574
+ ## Performance Optimization
575
+
576
+ ### Caching Strategies
577
+
578
+ ```typescript
579
+ const promptCache = new Map<string, string>()
580
+ const toolCache = new Map<string, any[]>()
581
+
582
+ function getTools(agentName: string, context: ToolContext) {
583
+ const cacheKey = `${agentName}-${context.teamId}`
584
+
585
+ if (!toolCache.has(cacheKey)) {
586
+ toolCache.set(cacheKey, createToolsForAgent(agentName, context))
587
+ }
588
+
589
+ return toolCache.get(cacheKey)
590
+ }
591
+ ```
592
+
593
+ ### Parallel Tool Loading
594
+
595
+ ```typescript
596
+ async function initializeAgents(context) {
597
+ // Load all agents in parallel
598
+ const [taskAgent, customerAgent, pageAgent] = await Promise.all([
599
+ createAgent({ ...taskConfig, context }),
600
+ createAgent({ ...customerConfig, context }),
601
+ createAgent({ ...pageConfig, context }),
602
+ ])
603
+
604
+ return { taskAgent, customerAgent, pageAgent }
605
+ }
606
+ ```
607
+
608
+ ## Testing Customizations
609
+
610
+ ### Unit Testing Prompts
611
+
612
+ ```typescript
613
+ describe('Orchestrator Routing', () => {
614
+ it('routes task requests correctly', async () => {
615
+ const result = await orchestrator.chat('Show me my tasks')
616
+ expect(result.agentUsed).toBe('task')
617
+ })
618
+
619
+ it('handles context references', async () => {
620
+ await orchestrator.chat('Show customer StartupXYZ')
621
+ const result = await orchestrator.chat('Update their phone')
622
+ expect(result.agentUsed).toBe('customer')
623
+ })
624
+ })
625
+ ```
626
+
627
+ ### Integration Testing
628
+
629
+ ```typescript
630
+ describe('Full Workflow', () => {
631
+ it('completes task creation flow', async () => {
632
+ const response = await processWithOrchestrator(
633
+ 'Create a task to review the proposal',
634
+ testContext
635
+ )
636
+
637
+ expect(response.content).toContain('created')
638
+ expect(response.content).toMatch(/\/dashboard\/tasks\//)
639
+ })
640
+ })
641
+ ```
642
+
643
+ ## Next Steps
644
+
645
+ - [API Reference](./01-api-reference.md) - Complete API documentation
646
+ - [Examples](./03-examples.md) - Real-world implementation examples