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.
- package/README.md +234 -0
- package/package.json +101 -0
- package/src/config/config-manager.ts +319 -0
- package/src/config/config-types.ts +65 -0
- package/src/config/environment-variables.ts +8 -0
- package/src/config/index.ts +26 -0
- package/src/config/language-models-database.ts +1426 -0
- package/src/config/mcp-server-registry.ts +24 -0
- package/src/config/model-registry.ts +256 -0
- package/src/config/model-tester.ts +211 -0
- package/src/config/model-utils.ts +193 -0
- package/src/config/provider-ui-config.ts +196 -0
- package/src/connectors/composio.json +154 -0
- package/src/index.ts +22 -0
- package/src/memory/ARCHITECTURE.md +302 -0
- package/src/memory/MASTRA_INTEGRATION.md +106 -0
- package/src/memory/README.md +224 -0
- package/src/memory/agent-memory-manager.ts +416 -0
- package/src/memory/example.ts +343 -0
- package/src/memory/index.ts +47 -0
- package/src/memory/mastra-integration.ts +609 -0
- package/src/memory/storage/drizzle-storage.ts +205 -0
- package/src/memory/storage/in-memory-storage.ts +551 -0
- package/src/memory/storage/storage-interface.ts +68 -0
- package/src/memory/types.ts +125 -0
- package/src/models/providers.ts +5 -0
- package/src/models/registry.ts +4 -0
- package/src/models/types.ts +4 -0
- package/src/search.ts +5 -0
- package/src/tools/composio-mastra.ts +305 -0
- package/src/tools/composio-mcp.ts +205 -0
- package/src/tools/index.ts +13 -0
- package/src/tools/qwksearch-api-tools.ts +327 -0
- package/src/tools/search/doc-utils.ts +75 -0
- package/src/tools/search/document.ts +47 -0
- package/src/tools/search/index.ts +13 -0
- package/src/tools/search/link-summarizer.ts +112 -0
- package/src/tools/search/meta-search-types.ts +62 -0
- package/src/tools/search/metaSearchAgent.ts +454 -0
- package/src/tools/search/search-handlers.ts +85 -0
- package/src/tools/search/suggestionGeneratorAgent.ts +54 -0
- package/src/types.d.ts +137 -0
- package/src/utils/README.md +98 -0
- package/src/utils/chat-helpers.ts +18 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/markdown-to-html.ts +53 -0
- package/src/utils/outputParser.ts +73 -0
- package/src/utils/provider-image-cropper.ts +130 -0
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module research/models/providers/index
|
|
3
|
+
* @description Research library module.
|
|
4
|
+
*
|
|
5
|
+
* This file provides metadata about providers without importing them directly,
|
|
6
|
+
* to avoid circular dependency issues with the config system.
|
|
7
|
+
*/
|
|
8
|
+
import { ModelProviderUISection } from "../types";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Gets provider UI configuration without triggering circular dependencies.
|
|
12
|
+
* This function uses static metadata instead of importing provider classes.
|
|
13
|
+
*/
|
|
14
|
+
export const getModelProvidersUIConfigSection =
|
|
15
|
+
(): ModelProviderUISection[] => {
|
|
16
|
+
// Import statically defined metadata instead of loading the providers
|
|
17
|
+
// This breaks the circular dependency
|
|
18
|
+
return [
|
|
19
|
+
{
|
|
20
|
+
key: "openai",
|
|
21
|
+
name: "OpenAI",
|
|
22
|
+
fields: getOpenAIConfigFields(),
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
key: "anthropic",
|
|
26
|
+
name: "Anthropic",
|
|
27
|
+
fields: getAnthropicConfigFields(),
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
key: "gemini",
|
|
31
|
+
name: "Google Gemini",
|
|
32
|
+
fields: getGeminiConfigFields(),
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
key: "groq",
|
|
36
|
+
name: "Groq",
|
|
37
|
+
fields: getGroqConfigFields(),
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
key: "deepseek",
|
|
41
|
+
name: "DeepSeek",
|
|
42
|
+
fields: getDeepSeekConfigFields(),
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
key: "nvidia",
|
|
46
|
+
name: "NVIDIA",
|
|
47
|
+
fields: getNvidiaConfigFields(),
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
key: "openrouter",
|
|
51
|
+
name: "OpenRouter",
|
|
52
|
+
fields: getOpenRouterConfigFields(),
|
|
53
|
+
},
|
|
54
|
+
];
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// Static config field definitions to avoid loading provider classes
|
|
58
|
+
// These match the providerConfigFields in each provider file
|
|
59
|
+
function getOpenAIConfigFields() {
|
|
60
|
+
return [
|
|
61
|
+
{
|
|
62
|
+
type: "password" as const,
|
|
63
|
+
name: "API Key",
|
|
64
|
+
key: "apiKey",
|
|
65
|
+
description: "Your OpenAI API key",
|
|
66
|
+
required: true,
|
|
67
|
+
placeholder: "OpenAI API Key",
|
|
68
|
+
env: "OPENAI_API_KEY",
|
|
69
|
+
scope: "server" as const,
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
type: "string" as const,
|
|
73
|
+
name: "Base URL",
|
|
74
|
+
key: "baseURL",
|
|
75
|
+
description: "The base URL for the OpenAI API",
|
|
76
|
+
required: true,
|
|
77
|
+
placeholder: "OpenAI Base URL",
|
|
78
|
+
default: "https://api.openai.com/v1",
|
|
79
|
+
env: "OPENAI_BASE_URL",
|
|
80
|
+
scope: "server" as const,
|
|
81
|
+
},
|
|
82
|
+
];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
function getAnthropicConfigFields() {
|
|
87
|
+
return [
|
|
88
|
+
{
|
|
89
|
+
type: "password" as const,
|
|
90
|
+
name: "API Key",
|
|
91
|
+
key: "apiKey",
|
|
92
|
+
description: "Your Anthropic API key",
|
|
93
|
+
required: true,
|
|
94
|
+
placeholder: "Anthropic API Key",
|
|
95
|
+
env: "ANTHROPIC_API_KEY",
|
|
96
|
+
scope: "server" as const,
|
|
97
|
+
},
|
|
98
|
+
];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function getGeminiConfigFields() {
|
|
102
|
+
return [
|
|
103
|
+
{
|
|
104
|
+
type: "password" as const,
|
|
105
|
+
name: "API Key",
|
|
106
|
+
key: "apiKey",
|
|
107
|
+
description: "Your Google Gemini API key",
|
|
108
|
+
required: true,
|
|
109
|
+
placeholder: "Google Gemini API Key",
|
|
110
|
+
env: "GEMINI_API_KEY",
|
|
111
|
+
scope: "server" as const,
|
|
112
|
+
},
|
|
113
|
+
];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function getGroqConfigFields() {
|
|
117
|
+
return [
|
|
118
|
+
{
|
|
119
|
+
type: "password" as const,
|
|
120
|
+
name: "API Key",
|
|
121
|
+
key: "apiKey",
|
|
122
|
+
description: "Your Groq API key. Free tier includes fast inference on Llama and Mixtral models - get yours at console.groq.com",
|
|
123
|
+
required: true,
|
|
124
|
+
placeholder: "gsk_...",
|
|
125
|
+
env: "GROQ_API_KEY",
|
|
126
|
+
scope: "server" as const,
|
|
127
|
+
},
|
|
128
|
+
];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function getDeepSeekConfigFields() {
|
|
132
|
+
return [
|
|
133
|
+
{
|
|
134
|
+
type: "password" as const,
|
|
135
|
+
name: "API Key",
|
|
136
|
+
key: "apiKey",
|
|
137
|
+
description: "Your DeepSeek API key",
|
|
138
|
+
required: true,
|
|
139
|
+
placeholder: "DeepSeek API Key",
|
|
140
|
+
env: "DEEPSEEK_API_KEY",
|
|
141
|
+
scope: "server" as const,
|
|
142
|
+
},
|
|
143
|
+
];
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function getNvidiaConfigFields() {
|
|
147
|
+
return [
|
|
148
|
+
{
|
|
149
|
+
type: "password" as const,
|
|
150
|
+
name: "API Key",
|
|
151
|
+
key: "apiKey",
|
|
152
|
+
description: "Your NVIDIA API key. Free tier includes access to Nemotron, Llama, and other models - get yours at build.nvidia.com",
|
|
153
|
+
required: true,
|
|
154
|
+
placeholder: "nvapi-...",
|
|
155
|
+
env: "NVIDIA_API_KEY",
|
|
156
|
+
scope: "server" as const,
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
type: "string" as const,
|
|
160
|
+
name: "Base URL",
|
|
161
|
+
key: "baseURL",
|
|
162
|
+
description: "The base URL for NVIDIA's OpenAI-compatible API",
|
|
163
|
+
required: true,
|
|
164
|
+
placeholder: "https://integrate.api.nvidia.com/v1",
|
|
165
|
+
default: "https://integrate.api.nvidia.com/v1",
|
|
166
|
+
env: "NVIDIA_BASE_URL",
|
|
167
|
+
scope: "server" as const,
|
|
168
|
+
},
|
|
169
|
+
];
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function getOpenRouterConfigFields() {
|
|
173
|
+
return [
|
|
174
|
+
{
|
|
175
|
+
type: "password" as const,
|
|
176
|
+
name: "API Key",
|
|
177
|
+
key: "apiKey",
|
|
178
|
+
description: "Your OpenRouter API key. Get one free at openrouter.ai - includes free access to Llama 3.3 70B, Nemotron, and other models.",
|
|
179
|
+
required: true,
|
|
180
|
+
placeholder: "sk-or-v1-...",
|
|
181
|
+
env: "OPENROUTER_API_KEY",
|
|
182
|
+
scope: "server" as const,
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
type: "string" as const,
|
|
186
|
+
name: "Base URL",
|
|
187
|
+
key: "baseURL",
|
|
188
|
+
description: "The base URL for OpenRouter's OpenAI-compatible API",
|
|
189
|
+
required: true,
|
|
190
|
+
placeholder: "https://openrouter.ai/api/v1",
|
|
191
|
+
default: "https://openrouter.ai/api/v1",
|
|
192
|
+
env: "OPENROUTER_BASE_URL",
|
|
193
|
+
scope: "server" as const,
|
|
194
|
+
},
|
|
195
|
+
];
|
|
196
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
{
|
|
2
|
+
"connectors": [
|
|
3
|
+
{
|
|
4
|
+
"name": "Gmail",
|
|
5
|
+
"composio_id": "gmail",
|
|
6
|
+
"domain": "gmail.com",
|
|
7
|
+
"description": "Google's email service. Search inboxes, send/reply/draft messages, manage labels and threads."
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
"name": "Google Calendar",
|
|
11
|
+
"composio_id": "googlecalendar",
|
|
12
|
+
"domain": "calendar.google.com",
|
|
13
|
+
"description": "Time management and scheduling. Create/update/list events, manage calendars and attendees."
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
"name": "Outlook",
|
|
17
|
+
"composio_id": "outlook",
|
|
18
|
+
"domain": "outlook.com",
|
|
19
|
+
"description": "Microsoft's email and calendaring platform. Manage mail, contacts, calendars and rules."
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
"name": "Notion",
|
|
23
|
+
"composio_id": "notion",
|
|
24
|
+
"domain": "notion.so",
|
|
25
|
+
"description": "Workspace for docs, wikis and databases. Search, read, create and update pages and database items."
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
"name": "Linear",
|
|
29
|
+
"composio_id": "linear",
|
|
30
|
+
"domain": "linear.app",
|
|
31
|
+
"description": "Issue tracking and project management for software teams. Create, query and update issues, projects and cycles."
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
"name": "GitHub",
|
|
35
|
+
"composio_id": "github",
|
|
36
|
+
"domain": "github.com",
|
|
37
|
+
"description": "Code hosting and collaboration. Manage repos, issues, pull requests, branches and reviews."
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"name": "Google Drive",
|
|
41
|
+
"composio_id": "googledrive",
|
|
42
|
+
"domain": "drive.google.com",
|
|
43
|
+
"description": "Cloud file storage. Search, upload, share and manage files, folders and permissions."
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
"name": "SharePoint",
|
|
47
|
+
"composio_id": "share_point",
|
|
48
|
+
"domain": "sharepoint.com",
|
|
49
|
+
"description": "Microsoft document management and intranet platform. Search and manage sites, lists and files."
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
"name": "OneDrive",
|
|
53
|
+
"composio_id": "one_drive",
|
|
54
|
+
"domain": "onedrive.live.com",
|
|
55
|
+
"description": "Microsoft cloud file storage. Upload, share and manage personal/business files and folders."
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
"name": "Slack",
|
|
59
|
+
"composio_id": "slack",
|
|
60
|
+
"domain": "slack.com",
|
|
61
|
+
"description": "Channel-based team messaging. Search history, post messages, manage channels and users."
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
"name": "Box",
|
|
65
|
+
"composio_id": "box",
|
|
66
|
+
"domain": "box.com",
|
|
67
|
+
"description": "Enterprise cloud content management. Manage files, folders, sharing and collaboration."
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
"name": "Dropbox",
|
|
71
|
+
"composio_id": "dropbox",
|
|
72
|
+
"domain": "dropbox.com",
|
|
73
|
+
"description": "Cloud file syncing and sharing. Upload, list, move and share files across devices."
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
"name": "HubSpot",
|
|
77
|
+
"composio_id": "hubspot",
|
|
78
|
+
"domain": "hubspot.com",
|
|
79
|
+
"description": "CRM and marketing platform. Manage contacts, companies, deals, tickets and engagements."
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
"name": "Jira",
|
|
83
|
+
"composio_id": "jira",
|
|
84
|
+
"domain": "atlassian.com",
|
|
85
|
+
"description": "Atlassian issue and project tracking. Create/search/update tickets, manage sprints and boards."
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
"name": "Confluence",
|
|
89
|
+
"composio_id": "confluence",
|
|
90
|
+
"domain": "atlassian.com",
|
|
91
|
+
"description": "Atlassian knowledge base and team wiki. Search, create and edit pages and spaces."
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
"name": "Asana",
|
|
95
|
+
"composio_id": "asana",
|
|
96
|
+
"domain": "asana.com",
|
|
97
|
+
"description": "Task and project management. Create/update tasks, projects, sections and assignments."
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
"name": "Databricks",
|
|
101
|
+
"composio_id": "databricks",
|
|
102
|
+
"domain": "databricks.com",
|
|
103
|
+
"description": "Unified analytics and lakehouse platform. Run SQL/jobs, manage clusters, notebooks and Unity Catalog."
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
"name": "Snowflake",
|
|
107
|
+
"composio_id": "snowflake",
|
|
108
|
+
"domain": "snowflake.com",
|
|
109
|
+
"description": "Cloud data warehouse. Query structured data, manage warehouses, databases and roles."
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
"name": "Salesforce",
|
|
113
|
+
"composio_id": "salesforce",
|
|
114
|
+
"domain": "salesforce.com",
|
|
115
|
+
"description": "Enterprise CRM. Manage leads, accounts, opportunities, cases and custom objects."
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
"name": "ServiceNow",
|
|
119
|
+
"composio_id": "servicenow",
|
|
120
|
+
"domain": "servicenow.com",
|
|
121
|
+
"description": "IT service management platform. Manage incidents, change requests, knowledge base and CMDB."
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
"name": "Microsoft Teams",
|
|
125
|
+
"composio_id": "microsoft_teams",
|
|
126
|
+
"domain": "teams.microsoft.com",
|
|
127
|
+
"description": "Microsoft collaboration platform. Send messages, manage channels, teams and meetings."
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
"name": "Zapier",
|
|
131
|
+
"composio_id": null,
|
|
132
|
+
"domain": null,
|
|
133
|
+
"description": "Not available as a Composio toolkit — Zapier is a direct competitor to Composio's integration platform."
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
"name": "Make.com",
|
|
137
|
+
"composio_id": null,
|
|
138
|
+
"domain": null,
|
|
139
|
+
"description": "Not available as a Composio toolkit — Make (formerly Integromat) is a direct competitor."
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
"name": "Google Workspace",
|
|
143
|
+
"composio_id": null,
|
|
144
|
+
"domain": null,
|
|
145
|
+
"description": "Not a single Composio toolkit. Use the individual Google toolkits instead: gmail, googledrive, googlecalendar, googledocs, googlesheets, etc."
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
"name": "Custom Remote MCPs",
|
|
149
|
+
"composio_id": null,
|
|
150
|
+
"domain": null,
|
|
151
|
+
"description": "Not applicable — Composio is itself an MCP gateway. Custom MCPs are handled via Composio's auth-config/integration system or by registering a custom toolkit."
|
|
152
|
+
}
|
|
153
|
+
]
|
|
154
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview AI Agent Toolkit
|
|
3
|
+
*
|
|
4
|
+
* Multi-provider AI agent toolkit for generating language responses, searching the web,
|
|
5
|
+
* extracting page content, and managing long-term memory across 10+ LLM providers.
|
|
6
|
+
*
|
|
7
|
+
* Built on Vercel AI SDK with prompt templates for research, summarization, citation
|
|
8
|
+
* answering, query resolution, and knowledge-graph extraction.
|
|
9
|
+
*
|
|
10
|
+
* @module ai-research-agent
|
|
11
|
+
* @author vtempest <grokthiscontact@gmail.com>
|
|
12
|
+
* @license AGPL-3.0
|
|
13
|
+
* @see {@link https://github.com/vtempest/ai-research-agent}
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export * from "write-language";
|
|
17
|
+
export * from "./memory";
|
|
18
|
+
export * from "./tools";
|
|
19
|
+
export { configManager, ModelRegistry, getEnv, getModelProvidersUIConfigSection } from "./config";
|
|
20
|
+
export type { Config, ConfigModelProvider, MCPServerConfig, UIConfigSections, Model, ModelWithProvider } from "./config";
|
|
21
|
+
export { cropProvider, cropProviderAsBlob, cropProviderAsDataURL, getProviderImage, getProviderNames } from "./utils/provider-image-cropper";
|
|
22
|
+
export type { Provider } from "./utils/provider-image-cropper";
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
# Memory Module Architecture
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
The memory system has been refactored from a single monolithic file into a modular, well-abstracted architecture that separates concerns and eliminates direct dependencies on Drizzle ORM.
|
|
6
|
+
|
|
7
|
+
## Architecture Diagram
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
11
|
+
│ Application Layer │
|
|
12
|
+
│ (Your app using the memory module) │
|
|
13
|
+
└────────────────────────────┬────────────────────────────────────┘
|
|
14
|
+
│
|
|
15
|
+
│ imports
|
|
16
|
+
↓
|
|
17
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
18
|
+
│ index.ts │
|
|
19
|
+
│ (Public API exports) │
|
|
20
|
+
└────────────────────────────┬────────────────────────────────────┘
|
|
21
|
+
│
|
|
22
|
+
┌───────────────────┼───────────────────┐
|
|
23
|
+
│ │ │
|
|
24
|
+
↓ ↓ ↓
|
|
25
|
+
┌─────────────────┐ ┌─────────────────┐ ┌──────────────────┐
|
|
26
|
+
│ memory-agent.ts│ │ simple-memory.ts│ │ types.ts │
|
|
27
|
+
│ │ │ │ │ │
|
|
28
|
+
│ • Rate limiting │ │ • Message mgmt │ │ • MemoryRecord │
|
|
29
|
+
│ • LLM providers │ │ • Caching │ │ • MEMORY_CONFIG │
|
|
30
|
+
│ • Chat handling │ │ • Summarization │ │ • MEMORY_TYPES │
|
|
31
|
+
│ • Analytics │ │ • Batch ops │ │ • Interfaces │
|
|
32
|
+
└────────┬────────┘ └────────┬────────┘ └──────────────────┘
|
|
33
|
+
│ │
|
|
34
|
+
│ │ uses
|
|
35
|
+
│ ↓
|
|
36
|
+
│ ┌─────────────────────┐
|
|
37
|
+
│ │ storage-interface.ts│
|
|
38
|
+
│ │ │
|
|
39
|
+
│ │ IMemoryStorage │
|
|
40
|
+
│ │ (Abstract Interface)│
|
|
41
|
+
│ └──────────┬──────────┘
|
|
42
|
+
│ │
|
|
43
|
+
│ │ implements
|
|
44
|
+
│ ↓
|
|
45
|
+
│ ┌─────────────────────┐
|
|
46
|
+
└─────────→│ drizzle-storage.ts │
|
|
47
|
+
│ │
|
|
48
|
+
│ DrizzleMemoryStorage│
|
|
49
|
+
│ (Concrete Impl) │
|
|
50
|
+
└──────────┬──────────┘
|
|
51
|
+
│
|
|
52
|
+
│ uses
|
|
53
|
+
↓
|
|
54
|
+
┌─────────────────────┐
|
|
55
|
+
│ Drizzle ORM │
|
|
56
|
+
│ (External Dep) │
|
|
57
|
+
└─────────────────────┘
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Component Responsibilities
|
|
61
|
+
|
|
62
|
+
### 1. **types.ts**
|
|
63
|
+
- **Purpose**: Central type definitions and constants
|
|
64
|
+
- **Exports**:
|
|
65
|
+
- `MemoryRecord`, `Message`, `MemoryType`
|
|
66
|
+
- `MEMORY_CONFIG`, `MEMORY_TYPES`
|
|
67
|
+
- All TypeScript interfaces
|
|
68
|
+
- **Dependencies**: None
|
|
69
|
+
- **Used by**: All other modules
|
|
70
|
+
|
|
71
|
+
### 2. **storage-interface.ts**
|
|
72
|
+
- **Purpose**: Abstract interface for storage operations
|
|
73
|
+
- **Exports**: `IMemoryStorage` interface
|
|
74
|
+
- **Dependencies**: `types.ts`
|
|
75
|
+
- **Used by**: `simple-memory.ts`, `memory-agent.ts`
|
|
76
|
+
- **Key Methods**:
|
|
77
|
+
- `insertMemory()`
|
|
78
|
+
- `findMemories()`
|
|
79
|
+
- `findSimilarMemories()`
|
|
80
|
+
- `updateMemory()`
|
|
81
|
+
- `deleteMemory()`
|
|
82
|
+
- `batchUpdateMemories()`
|
|
83
|
+
|
|
84
|
+
### 3. **drizzle-storage.ts**
|
|
85
|
+
- **Purpose**: Drizzle ORM implementation of `IMemoryStorage`
|
|
86
|
+
- **Exports**: `DrizzleMemoryStorage` class
|
|
87
|
+
- **Dependencies**: `drizzle-orm`, `types.ts`, `storage-interface.ts`
|
|
88
|
+
- **Used by**: Application code (when using Drizzle)
|
|
89
|
+
- **Isolation**: This is the ONLY file that imports `drizzle-orm`
|
|
90
|
+
|
|
91
|
+
### 4. **simple-memory.ts**
|
|
92
|
+
- **Purpose**: Core memory management logic
|
|
93
|
+
- **Exports**: `SimpleMemory` class
|
|
94
|
+
- **Dependencies**: `storage-interface.ts`, `types.ts`, `generateLanguageResponse`
|
|
95
|
+
- **Features**:
|
|
96
|
+
- Message deduplication
|
|
97
|
+
- Auto-summarization
|
|
98
|
+
- Caching (in-memory)
|
|
99
|
+
- Batch processing
|
|
100
|
+
- Memory recall with filtering
|
|
101
|
+
|
|
102
|
+
### 5. **memory-agent.ts**
|
|
103
|
+
- **Purpose**: High-level agent with LLM integration
|
|
104
|
+
- **Exports**: `MemoryAgent` class
|
|
105
|
+
- **Dependencies**: `simple-memory.ts`, `storage-interface.ts`, `types.ts`
|
|
106
|
+
- **Features**:
|
|
107
|
+
- Rate limiting
|
|
108
|
+
- Multi-provider LLM support
|
|
109
|
+
- Chat management
|
|
110
|
+
- Session tracking
|
|
111
|
+
- Analytics
|
|
112
|
+
|
|
113
|
+
### 6. **index.ts**
|
|
114
|
+
- **Purpose**: Public API surface
|
|
115
|
+
- **Exports**: All public classes, interfaces, and types
|
|
116
|
+
- **Role**: Single entry point for consumers
|
|
117
|
+
|
|
118
|
+
### 7. **memory.ts** (Deprecated)
|
|
119
|
+
- **Purpose**: Backward compatibility
|
|
120
|
+
- **Exports**: Re-exports from `index.ts`
|
|
121
|
+
- **Status**: Deprecated, will be removed in future version
|
|
122
|
+
|
|
123
|
+
## Data Flow
|
|
124
|
+
|
|
125
|
+
### Storing a Memory
|
|
126
|
+
```
|
|
127
|
+
Application
|
|
128
|
+
↓ memory.storeFact("content", importance, category)
|
|
129
|
+
SimpleMemory
|
|
130
|
+
↓ storage.insertMemory(userId, type, content, importance)
|
|
131
|
+
IMemoryStorage (interface)
|
|
132
|
+
↓
|
|
133
|
+
DrizzleMemoryStorage
|
|
134
|
+
↓ db.insert().values()
|
|
135
|
+
Database
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Recalling Memories
|
|
139
|
+
```
|
|
140
|
+
Application
|
|
141
|
+
↓ memory.recallRelevantMemories(query, limit)
|
|
142
|
+
SimpleMemory
|
|
143
|
+
↓ Check cache
|
|
144
|
+
↓ storage.findMemories(userId, query, limit, options)
|
|
145
|
+
IMemoryStorage (interface)
|
|
146
|
+
↓
|
|
147
|
+
DrizzleMemoryStorage
|
|
148
|
+
↓ db.select().where().orderBy()
|
|
149
|
+
Database
|
|
150
|
+
↓
|
|
151
|
+
SimpleMemory (apply filters, cache results)
|
|
152
|
+
↓
|
|
153
|
+
Application
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Benefits of New Architecture
|
|
157
|
+
|
|
158
|
+
### 1. **Separation of Concerns**
|
|
159
|
+
- Storage logic is isolated from business logic
|
|
160
|
+
- Each file has a single, clear responsibility
|
|
161
|
+
- Easy to understand and maintain
|
|
162
|
+
|
|
163
|
+
### 2. **Testability**
|
|
164
|
+
```typescript
|
|
165
|
+
// Mock storage for testing
|
|
166
|
+
class MockStorage implements IMemoryStorage {
|
|
167
|
+
private data = new Map();
|
|
168
|
+
|
|
169
|
+
async insertMemory(...) { /* mock */ }
|
|
170
|
+
async findMemories(...) { /* mock */ }
|
|
171
|
+
// ...
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const memory = new SimpleMemory("user", new MockStorage());
|
|
175
|
+
// Test without database!
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### 3. **Flexibility**
|
|
179
|
+
```typescript
|
|
180
|
+
// Can swap implementations easily
|
|
181
|
+
const sqliteStorage = new SQLiteMemoryStorage(db);
|
|
182
|
+
const postgresStorage = new PostgresMemoryStorage(pool);
|
|
183
|
+
const redisStorage = new RedisMemoryStorage(client);
|
|
184
|
+
|
|
185
|
+
// Same interface, different backends
|
|
186
|
+
const memory = new SimpleMemory("user", sqliteStorage);
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### 4. **No Vendor Lock-in**
|
|
190
|
+
- Core logic doesn't depend on Drizzle
|
|
191
|
+
- Can migrate to different ORMs
|
|
192
|
+
- Can use raw SQL if needed
|
|
193
|
+
|
|
194
|
+
### 5. **Better Dependency Management**
|
|
195
|
+
- Drizzle is only imported in one file
|
|
196
|
+
- Easier to upgrade or replace
|
|
197
|
+
- Smaller bundle size if using tree-shaking
|
|
198
|
+
|
|
199
|
+
## Migration from Old Structure
|
|
200
|
+
|
|
201
|
+
### Old (Monolithic)
|
|
202
|
+
```typescript
|
|
203
|
+
import { SimpleMemory } from "./agents/memory/memory";
|
|
204
|
+
|
|
205
|
+
const memory = new SimpleMemory(userId, db, options);
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### New (Modular)
|
|
209
|
+
```typescript
|
|
210
|
+
import { SimpleMemory, DrizzleMemoryStorage } from "./agents/memory";
|
|
211
|
+
|
|
212
|
+
const storage = new DrizzleMemoryStorage(db);
|
|
213
|
+
const memory = new SimpleMemory(userId, storage, options);
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### Key Changes
|
|
217
|
+
1. **Constructor change**: `db` → `storage` (IMemoryStorage)
|
|
218
|
+
2. **Explicit adapter**: Must create storage adapter first
|
|
219
|
+
3. **Better typing**: Full TypeScript support with interfaces
|
|
220
|
+
|
|
221
|
+
## Design Patterns Used
|
|
222
|
+
|
|
223
|
+
### 1. **Dependency Injection**
|
|
224
|
+
- `SimpleMemory` receives `IMemoryStorage` via constructor
|
|
225
|
+
- Enables loose coupling and testability
|
|
226
|
+
|
|
227
|
+
### 2. **Strategy Pattern**
|
|
228
|
+
- `IMemoryStorage` defines the storage strategy interface
|
|
229
|
+
- Different implementations can be swapped at runtime
|
|
230
|
+
|
|
231
|
+
### 3. **Facade Pattern**
|
|
232
|
+
- `MemoryAgent` provides simple interface to complex subsystems
|
|
233
|
+
- Hides complexity of memory management, LLM calls, rate limiting
|
|
234
|
+
|
|
235
|
+
### 4. **Repository Pattern**
|
|
236
|
+
- Storage layer acts as repository for memory entities
|
|
237
|
+
- Abstracts data access logic
|
|
238
|
+
|
|
239
|
+
## Future Extensions
|
|
240
|
+
|
|
241
|
+
### Adding New Storage Backend
|
|
242
|
+
```typescript
|
|
243
|
+
// 1. Implement IMemoryStorage
|
|
244
|
+
export class MongoMemoryStorage implements IMemoryStorage {
|
|
245
|
+
constructor(private collection: Collection) {}
|
|
246
|
+
|
|
247
|
+
async insertMemory(...) {
|
|
248
|
+
return await this.collection.insertOne({...});
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// ... implement other methods
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// 2. Use it
|
|
255
|
+
const storage = new MongoMemoryStorage(collection);
|
|
256
|
+
const memory = new SimpleMemory("user", storage);
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### Adding New Features to SimpleMemory
|
|
260
|
+
- Modify only `simple-memory.ts`
|
|
261
|
+
- No need to touch storage layer
|
|
262
|
+
- Business logic stays separate
|
|
263
|
+
|
|
264
|
+
### Replacing Drizzle
|
|
265
|
+
- Create new storage adapter (e.g., `prisma-storage.ts`)
|
|
266
|
+
- Implement `IMemoryStorage` interface
|
|
267
|
+
- Swap at initialization time
|
|
268
|
+
- Core logic remains unchanged
|
|
269
|
+
|
|
270
|
+
## Performance Considerations
|
|
271
|
+
|
|
272
|
+
### Caching Layer
|
|
273
|
+
- In-memory cache in `SimpleMemory`
|
|
274
|
+
- Reduces database queries
|
|
275
|
+
- TTL-based expiration
|
|
276
|
+
|
|
277
|
+
### Batch Operations
|
|
278
|
+
- `batchUpdateMemories()` for bulk updates
|
|
279
|
+
- Reduces round trips to database
|
|
280
|
+
- Better performance for summarization
|
|
281
|
+
|
|
282
|
+
### Query Optimization
|
|
283
|
+
- Indexed queries in storage layer
|
|
284
|
+
- Limit results to prevent over-fetching
|
|
285
|
+
- Efficient filtering and sorting
|
|
286
|
+
|
|
287
|
+
## Security Considerations
|
|
288
|
+
|
|
289
|
+
### Rate Limiting
|
|
290
|
+
- Prevents abuse of memory storage
|
|
291
|
+
- Configurable limits per user
|
|
292
|
+
- Sliding window implementation
|
|
293
|
+
|
|
294
|
+
### Input Validation
|
|
295
|
+
- Validates all inputs in `SimpleMemory`
|
|
296
|
+
- Prevents injection attacks
|
|
297
|
+
- Sanitizes content before storage
|
|
298
|
+
|
|
299
|
+
### Access Control
|
|
300
|
+
- User ID-based isolation
|
|
301
|
+
- Each user only sees their memories
|
|
302
|
+
- No cross-user data leakage
|