chat-agent-toolkit 1.2.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 (48) hide show
  1. package/README.md +234 -0
  2. package/package.json +101 -0
  3. package/src/config/config-manager.ts +319 -0
  4. package/src/config/config-types.ts +65 -0
  5. package/src/config/environment-variables.ts +8 -0
  6. package/src/config/index.ts +26 -0
  7. package/src/config/language-models-database.ts +1426 -0
  8. package/src/config/mcp-server-registry.ts +24 -0
  9. package/src/config/model-registry.ts +256 -0
  10. package/src/config/model-tester.ts +211 -0
  11. package/src/config/model-utils.ts +193 -0
  12. package/src/config/provider-ui-config.ts +196 -0
  13. package/src/connectors/composio.json +154 -0
  14. package/src/index.ts +22 -0
  15. package/src/memory/ARCHITECTURE.md +302 -0
  16. package/src/memory/MASTRA_INTEGRATION.md +106 -0
  17. package/src/memory/README.md +224 -0
  18. package/src/memory/agent-memory-manager.ts +416 -0
  19. package/src/memory/example.ts +343 -0
  20. package/src/memory/index.ts +47 -0
  21. package/src/memory/mastra-integration.ts +609 -0
  22. package/src/memory/storage/drizzle-storage.ts +205 -0
  23. package/src/memory/storage/in-memory-storage.ts +551 -0
  24. package/src/memory/storage/storage-interface.ts +68 -0
  25. package/src/memory/types.ts +125 -0
  26. package/src/models/providers.ts +5 -0
  27. package/src/models/registry.ts +4 -0
  28. package/src/models/types.ts +4 -0
  29. package/src/search.ts +5 -0
  30. package/src/tools/composio-mastra.ts +305 -0
  31. package/src/tools/composio-mcp.ts +205 -0
  32. package/src/tools/index.ts +13 -0
  33. package/src/tools/qwksearch-api-tools.ts +327 -0
  34. package/src/tools/search/doc-utils.ts +75 -0
  35. package/src/tools/search/document.ts +47 -0
  36. package/src/tools/search/index.ts +13 -0
  37. package/src/tools/search/link-summarizer.ts +112 -0
  38. package/src/tools/search/meta-search-types.ts +62 -0
  39. package/src/tools/search/metaSearchAgent.ts +454 -0
  40. package/src/tools/search/search-handlers.ts +85 -0
  41. package/src/tools/search/suggestionGeneratorAgent.ts +54 -0
  42. package/src/types.d.ts +137 -0
  43. package/src/utils/README.md +98 -0
  44. package/src/utils/chat-helpers.ts +18 -0
  45. package/src/utils/index.ts +2 -0
  46. package/src/utils/markdown-to-html.ts +53 -0
  47. package/src/utils/outputParser.ts +73 -0
  48. package/src/utils/provider-image-cropper.ts +130 -0
@@ -0,0 +1,106 @@
1
+ # Mastra Memory Integration for Cloudflare Workers
2
+
3
+ Complete integration guide for using Mastra's memory system with Cloudflare Workers, D1, and KV storage.
4
+
5
+ ## Features
6
+
7
+ - **Multi-Storage Backend**: D1 (SQL), KV (key-value), or in-memory
8
+ - **Thread-Based Conversations**: Organize conversations by thread ID
9
+ - **Resource Scoping**: Multi-user support with user and resource isolation
10
+ - **Edge-Ready**: Optimized for Cloudflare Workers edge runtime
11
+ - **Compatible**: Works with existing MemoryAgent architecture
12
+
13
+ ## Quick Start
14
+
15
+ ### 1. Initialize Mastra Memory
16
+
17
+ ```typescript
18
+ import { createMastraMemory } from 'chat-agent-toolkit/memory';
19
+
20
+ const memoryManager = createMastraMemory({
21
+ storage: 'd1',
22
+ env: env, // Cloudflare env bindings
23
+ tableName: 'mastra_memories',
24
+ });
25
+ ```
26
+
27
+ ### 2. Create Agent with Memory
28
+
29
+ ```typescript
30
+ import { openai } from '@ai-sdk/openai';
31
+
32
+ const agent = await memoryManager.createAgent({
33
+ id: 'assistant',
34
+ name: 'AI Assistant',
35
+ instructions: 'You are a helpful assistant with memory.',
36
+ model: openai('gpt-4o'),
37
+ userId: 'user-123',
38
+ threadId: 'conversation-1',
39
+ });
40
+ ```
41
+
42
+ ### 3. Chat with Memory Context
43
+
44
+ ```typescript
45
+ // Store and recall messages
46
+ await memoryManager.storeMessage('user-123', 'conversation-1', 'user', 'Hello');
47
+ const history = await memoryManager.recallConversation('user-123', 'conversation-1', 10);
48
+
49
+ // Get relevant context
50
+ const context = await memoryManager.getRelevantContext('user-123', 'previous topic', 5);
51
+
52
+ // Generate response
53
+ const response = await agent.generate('Continue our conversation');
54
+ ```
55
+
56
+ See [examples/mastra-cloudflare-worker.ts](../../examples/mastra-cloudflare-worker.ts) for complete implementation.
57
+
58
+ ## Configuration
59
+
60
+ ### wrangler.toml
61
+
62
+ ```toml
63
+ name = "mastra-memory-app"
64
+ compatibility_date = "2024-03-10"
65
+ compatibility_flags = ["nodejs_compat"]
66
+
67
+ [vars]
68
+ OPENAI_API_KEY = "sk-..."
69
+
70
+ [[d1_databases]]
71
+ binding = "DB"
72
+ database_name = "mastra-memory"
73
+ database_id = "YOUR_D1_ID"
74
+ ```
75
+
76
+ D1 schema auto-initializes on first use.
77
+
78
+ ## API Reference
79
+
80
+ ### MastraMemoryManager
81
+
82
+ - `createAgent(config)` - Create agent with memory
83
+ - `storeMessage(userId, threadId, role, content, metadata?)` - Store message
84
+ - `recallConversation(userId, threadId, limit?)` - Get history
85
+ - `getRelevantContext(userId, query, limit?)` - Get relevant memories
86
+ - `getStorage()` - Access storage for advanced operations
87
+
88
+ ### Storage Backends
89
+
90
+ **D1 Storage** (recommended for production):
91
+ ```typescript
92
+ storage: 'd1',
93
+ env: { DB: env.DB }
94
+ ```
95
+
96
+ **KV Storage** (simpler, less queryable):
97
+ ```typescript
98
+ storage: 'kv',
99
+ env: { KV: env.KV }
100
+ ```
101
+
102
+ ## Links
103
+
104
+ - [Mastra Documentation](https://docs.mastra.ai/)
105
+ - [Cloudflare Workers](https://developers.cloudflare.com/workers/)
106
+ - [Full Example](../../examples/mastra-cloudflare-worker.ts)
@@ -0,0 +1,224 @@
1
+ # Memory Module
2
+
3
+ Intelligent memory management for AI agents with persistent storage, automatic summarization, and vector-based relevance search.
4
+
5
+ ## Architecture
6
+
7
+ The memory system is now split into multiple files with clear separation of concerns:
8
+
9
+ ```
10
+ memory/
11
+ ├── index.ts # Main entry point, exports all public APIs
12
+ ├── types.ts # TypeScript types and constants
13
+ ├── storage-interface.ts # Abstract storage interface
14
+ ├── drizzle-storage.ts # Drizzle ORM implementation
15
+ ├── simple-memory.ts # Core memory management logic
16
+ ├── memory-agent.ts # High-level agent with LLM integration
17
+ ├── memory.ts # Backward compatibility (deprecated)
18
+ └── README.md # This file
19
+ ```
20
+
21
+ ## Key Benefits
22
+
23
+ ### 1. **Abstracted Database Layer**
24
+ The storage layer is now abstracted behind the `IMemoryStorage` interface, making it easy to:
25
+ - Swap database implementations (PostgreSQL, MySQL, SQLite, etc.)
26
+ - Mock storage for testing
27
+ - Add new storage backends without touching core logic
28
+
29
+ ### 2. **Modular Design**
30
+ Each component has a single responsibility:
31
+ - **types.ts** - Type definitions and constants
32
+ - **storage-interface.ts** - Storage contract
33
+ - **drizzle-storage.ts** - Drizzle-specific implementation
34
+ - **simple-memory.ts** - Core memory management
35
+ - **memory-agent.ts** - Agent-level features (rate limiting, LLM integration)
36
+
37
+ ### 3. **No Direct Drizzle Dependency**
38
+ The core `SimpleMemory` class now depends only on the `IMemoryStorage` interface, not on Drizzle directly. This means:
39
+ - Easier testing with mock storage
40
+ - Can use different ORMs or raw SQL
41
+ - Cleaner dependency management
42
+
43
+ ## Usage
44
+
45
+ ### Basic Usage (with Drizzle)
46
+
47
+ ```typescript
48
+ import { DrizzleMemoryStorage, SimpleMemory } from "research-agent";
49
+
50
+ // Create storage adapter
51
+ const storage = new DrizzleMemoryStorage(db);
52
+
53
+ // Create memory instance
54
+ const memory = new SimpleMemory("user-123", storage, {
55
+ maxMemories: 100,
56
+ enableVectorSearch: true,
57
+ enableAutoSummarization: true,
58
+ });
59
+
60
+ // Store a fact
61
+ await memory.storeFact("User prefers dark mode", 8, "preference");
62
+
63
+ // Recall relevant memories
64
+ const memories = await memory.recallRelevantMemories("preferences", 10);
65
+ ```
66
+
67
+ ### Using the Memory Agent
68
+
69
+ ```typescript
70
+ import { DrizzleMemoryStorage, MemoryAgent } from "research-agent";
71
+
72
+ const storage = new DrizzleMemoryStorage(db);
73
+ const agent = new MemoryAgent("user-123", storage, {
74
+ defaultProvider: "groq",
75
+ defaultModel: "mixtral-8x7b-32768",
76
+ });
77
+
78
+ // Chat with memory context
79
+ const response = await agent.chat("What do you remember about my preferences?");
80
+ console.log(response.content);
81
+ console.log(response.memoryContext);
82
+
83
+ // Manually store a fact
84
+ await agent.remember("User likes coffee", 7, "personal");
85
+ ```
86
+
87
+ ### Custom Storage Implementation
88
+
89
+ You can implement your own storage backend:
90
+
91
+ ```typescript
92
+ import { IMemoryStorage } from "research-agent";
93
+
94
+ class MyCustomStorage implements IMemoryStorage {
95
+ async insertMemory(userId, memoryType, content, importance, metadata) {
96
+ // Your implementation
97
+ }
98
+
99
+ async findMemories(userId, query, limit, options) {
100
+ // Your implementation
101
+ }
102
+
103
+ // ... implement other methods
104
+ }
105
+
106
+ const storage = new MyCustomStorage();
107
+ const memory = new SimpleMemory("user-123", storage);
108
+ ```
109
+
110
+ ## Migration Guide
111
+
112
+ If you were using the old monolithic `memory.ts`:
113
+
114
+ ### Before
115
+ ```typescript
116
+ import { SimpleMemory, MemoryAgent } from "research-agent";
117
+
118
+ const memory = new SimpleMemory("user-123", db, options);
119
+ ```
120
+
121
+ ### After
122
+ ```typescript
123
+ import { DrizzleMemoryStorage, SimpleMemory } from "research-agent";
124
+
125
+ const storage = new DrizzleMemoryStorage(db);
126
+ const memory = new SimpleMemory("user-123", storage, options);
127
+ ```
128
+
129
+ The old `memory.ts` file still works for backward compatibility but is deprecated.
130
+
131
+ ## Storage Interface
132
+
133
+ The `IMemoryStorage` interface defines these methods:
134
+
135
+ - `insertMemory()` - Insert a new memory record
136
+ - `findMemories()` - Search memories with filters
137
+ - `findSimilarMemories()` - Find similar content
138
+ - `updateMemory()` - Update a memory record
139
+ - `deleteMemory()` - Delete a memory
140
+ - `getMemoryById()` - Get memory by ID
141
+ - `batchUpdateMemories()` - Batch update multiple memories
142
+
143
+ ## Features
144
+
145
+ ### Core Memory Management
146
+ - **Message deduplication** - Prevents duplicate messages
147
+ - **Auto-summarization** - Extracts facts from conversations
148
+ - **Intelligent caching** - TTL-based cache for performance
149
+ - **Batch processing** - Efficient database operations
150
+ - **Conflict resolution** - Handles similar facts intelligently
151
+
152
+ ### Memory Agent
153
+ - **Rate limiting** - Prevents API abuse
154
+ - **Multi-provider support** - Works with different LLMs
155
+ - **Session management** - Tracks conversation sessions
156
+ - **Analytics** - Monitors performance metrics
157
+ - **Health checks** - System status monitoring
158
+
159
+ ## Configuration
160
+
161
+ ### Memory Options
162
+ ```typescript
163
+ {
164
+ maxMemories: 100, // Maximum memories to store
165
+ summaryThreshold: 10, // Messages before auto-summarization
166
+ cacheExpiry: 300000, // Cache TTL in ms (5 minutes)
167
+ batchSize: 5, // Batch size for processing
168
+ relevanceThreshold: 0.3, // Minimum relevance score
169
+ enableVectorSearch: true, // Enable vector-based search
170
+ enableAutoSummarization: true, // Enable auto-summarization
171
+ }
172
+ ```
173
+
174
+ ### Agent Options
175
+ ```typescript
176
+ {
177
+ memoryOptions: { /* Memory options */ },
178
+ defaultProvider: "groq", // Default LLM provider
179
+ defaultApiKey: "...", // API key
180
+ defaultModel: "...", // Model name
181
+ rateLimit: {
182
+ requests: 10, // Max requests per window
183
+ windowMs: 60000, // Rate limit window (1 minute)
184
+ },
185
+ }
186
+ ```
187
+
188
+ ## Testing
189
+
190
+ With the abstracted storage layer, testing is much easier:
191
+
192
+ ```typescript
193
+ class MockStorage implements IMemoryStorage {
194
+ private memories: Map<string, MemoryRecord> = new Map();
195
+
196
+ async insertMemory(userId, memoryType, content, importance, metadata) {
197
+ const id = Math.random().toString();
198
+ this.memories.set(id, { id, userId, memoryType, content, importance, metadata });
199
+ return id;
200
+ }
201
+
202
+ // ... implement other methods
203
+ }
204
+
205
+ // Use in tests
206
+ const mockStorage = new MockStorage();
207
+ const memory = new SimpleMemory("test-user", mockStorage);
208
+ ```
209
+
210
+ ## Performance
211
+
212
+ - **Caching** - Reduces database queries
213
+ - **Batch operations** - Minimizes round trips
214
+ - **Query optimization** - Efficient database queries
215
+ - **Rate limiting** - Prevents overload
216
+
217
+ ## Future Enhancements
218
+
219
+ Potential improvements:
220
+ - Vector search integration (embeddings API)
221
+ - Memory importance decay over time
222
+ - Cross-user memory sharing
223
+ - Memory export/import
224
+ - Advanced analytics dashboard