@mzhub/mem-ts 0.1.0
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 +335 -0
- package/dist/BaseAdapter-BoRh1T7O.d.mts +75 -0
- package/dist/BaseAdapter-CQVX-gcA.d.ts +75 -0
- package/dist/BaseProvider-CEoiLGj5.d.ts +34 -0
- package/dist/BaseProvider-edMh_R9t.d.mts +34 -0
- package/dist/adapters/index.d.mts +259 -0
- package/dist/adapters/index.d.ts +259 -0
- package/dist/adapters/index.js +1570 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/index.mjs +1542 -0
- package/dist/adapters/index.mjs.map +1 -0
- package/dist/index-Ci5Q9G9H.d.mts +289 -0
- package/dist/index-Dl-Q2au9.d.ts +289 -0
- package/dist/index.d.mts +1206 -0
- package/dist/index.d.ts +1206 -0
- package/dist/index.js +5126 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +5058 -0
- package/dist/index.mjs.map +1 -0
- package/dist/middleware/index.d.mts +4 -0
- package/dist/middleware/index.d.ts +4 -0
- package/dist/middleware/index.js +63 -0
- package/dist/middleware/index.js.map +1 -0
- package/dist/middleware/index.mjs +59 -0
- package/dist/middleware/index.mjs.map +1 -0
- package/dist/providers/index.d.mts +96 -0
- package/dist/providers/index.d.ts +96 -0
- package/dist/providers/index.js +379 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/index.mjs +370 -0
- package/dist/providers/index.mjs.map +1 -0
- package/dist/types-G9qmfSeZ.d.mts +260 -0
- package/dist/types-G9qmfSeZ.d.ts +260 -0
- package/logo.png +0 -0
- package/package.json +114 -0
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core type definitions for mem-ts Memory OS
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* A single memory fact representing a relationship between entities
|
|
6
|
+
*/
|
|
7
|
+
interface MemoryFact {
|
|
8
|
+
/** Unique identifier (UUID) */
|
|
9
|
+
id: string;
|
|
10
|
+
/** The entity (e.g., "User", "Project:WebApp") */
|
|
11
|
+
subject: string;
|
|
12
|
+
/** The relationship (e.g., "HAS_ALLERGY", "PREFERS", "WORKS_AT") */
|
|
13
|
+
predicate: string;
|
|
14
|
+
/** The value (e.g., "Peanuts", "Dark Mode", "Acme Corp") */
|
|
15
|
+
object: string;
|
|
16
|
+
/** Confidence score 0-1 */
|
|
17
|
+
confidence: number;
|
|
18
|
+
/**
|
|
19
|
+
* Importance score 1-10 (Amygdala pattern)
|
|
20
|
+
* 1-3: Trivia (preferences, minor details)
|
|
21
|
+
* 4-6: Standard (work, location, relationships)
|
|
22
|
+
* 7-8: Important (strong preferences, constraints)
|
|
23
|
+
* 9-10: Critical (allergies, safety, medical, boundaries)
|
|
24
|
+
*/
|
|
25
|
+
importance: number;
|
|
26
|
+
/** Conversation ID that created/updated this fact */
|
|
27
|
+
source: string;
|
|
28
|
+
/** Specific conversation exchange ID for episodic linking */
|
|
29
|
+
sourceConversationId?: string;
|
|
30
|
+
/** When this fact was first created */
|
|
31
|
+
createdAt: Date;
|
|
32
|
+
/** When this fact was last updated */
|
|
33
|
+
updatedAt: Date;
|
|
34
|
+
/** When this fact was superseded (null if still valid) */
|
|
35
|
+
invalidatedAt: Date | null;
|
|
36
|
+
/** Number of times this fact has been accessed (Hebbian learning) */
|
|
37
|
+
accessCount?: number;
|
|
38
|
+
/** Last time this fact was accessed */
|
|
39
|
+
lastAccessedAt?: Date;
|
|
40
|
+
/**
|
|
41
|
+
* Emotional context when fact was learned (Emotional Coloring)
|
|
42
|
+
* Tracks whether the fact was learned in a positive or negative context
|
|
43
|
+
*/
|
|
44
|
+
sentiment?: "positive" | "negative" | "neutral";
|
|
45
|
+
/** Description of the emotional context */
|
|
46
|
+
emotionalContext?: string;
|
|
47
|
+
/**
|
|
48
|
+
* Memory consolidation stage (Memory Consolidation Levels)
|
|
49
|
+
* - short-term: Just learned, may not persist
|
|
50
|
+
* - working: Being actively used, intermediate storage
|
|
51
|
+
* - long-term: Consolidated through reinforcement
|
|
52
|
+
*/
|
|
53
|
+
memoryStage?: "short-term" | "working" | "long-term";
|
|
54
|
+
/**
|
|
55
|
+
* IDs of related facts (Associative Linking / Knowledge Graph)
|
|
56
|
+
* Creates connections between facts for richer context
|
|
57
|
+
*/
|
|
58
|
+
relatedFactIds?: string[];
|
|
59
|
+
/**
|
|
60
|
+
* Vector embedding for semantic search (Attention Filtering)
|
|
61
|
+
* Stored as array of numbers for similarity matching
|
|
62
|
+
*/
|
|
63
|
+
embedding?: number[];
|
|
64
|
+
/**
|
|
65
|
+
* Memory level in the hierarchy pyramid:
|
|
66
|
+
* - raw_log: Raw conversation data (ephemeral, auto-deleted)
|
|
67
|
+
* - fact: Specific discrete facts (your normal facts)
|
|
68
|
+
* - pattern: Synthesized patterns/traits from multiple facts
|
|
69
|
+
* - core_belief: Unchangeable truths, always loaded (allergies, identity)
|
|
70
|
+
*/
|
|
71
|
+
memoryLevel?: "raw_log" | "fact" | "pattern" | "core_belief";
|
|
72
|
+
/**
|
|
73
|
+
* IDs of lower-level facts that this pattern/belief was derived from.
|
|
74
|
+
* Creates provenance chain for "why do I believe this?"
|
|
75
|
+
*/
|
|
76
|
+
childrenIds?: string[];
|
|
77
|
+
/** Additional metadata */
|
|
78
|
+
metadata?: Record<string, unknown>;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* A memory operation for updating the fact graph
|
|
82
|
+
*/
|
|
83
|
+
interface MemoryOperation {
|
|
84
|
+
/** Operation type */
|
|
85
|
+
op: "INSERT" | "UPDATE" | "DELETE";
|
|
86
|
+
/** The entity */
|
|
87
|
+
subject: string;
|
|
88
|
+
/** The relationship */
|
|
89
|
+
predicate: string;
|
|
90
|
+
/** The value */
|
|
91
|
+
object: string;
|
|
92
|
+
/** Reason for this operation (especially for DELETEs) */
|
|
93
|
+
reason?: string;
|
|
94
|
+
/** Confidence score 0-1 */
|
|
95
|
+
confidence?: number;
|
|
96
|
+
/** Importance score 1-10 (for safety-critical facts) */
|
|
97
|
+
importance?: number;
|
|
98
|
+
/** Sentiment context when fact was learned */
|
|
99
|
+
sentiment?: "positive" | "negative" | "neutral";
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Result from the fact extraction LLM
|
|
103
|
+
*/
|
|
104
|
+
interface ExtractionResult {
|
|
105
|
+
/** List of operations to apply */
|
|
106
|
+
operations: MemoryOperation[];
|
|
107
|
+
/** Reasoning for the extractions */
|
|
108
|
+
reasoning?: string;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* A single message in a conversation
|
|
112
|
+
*/
|
|
113
|
+
interface Message {
|
|
114
|
+
role: "user" | "assistant" | "system";
|
|
115
|
+
content: string;
|
|
116
|
+
timestamp: Date;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* A conversation exchange (user message + assistant response)
|
|
120
|
+
*/
|
|
121
|
+
interface ConversationExchange {
|
|
122
|
+
id: string;
|
|
123
|
+
userId: string;
|
|
124
|
+
sessionId: string;
|
|
125
|
+
userMessage: string;
|
|
126
|
+
assistantResponse: string;
|
|
127
|
+
timestamp: Date;
|
|
128
|
+
metadata?: Record<string, unknown>;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Session information
|
|
132
|
+
*/
|
|
133
|
+
interface Session {
|
|
134
|
+
id: string;
|
|
135
|
+
userId: string;
|
|
136
|
+
startedAt: Date;
|
|
137
|
+
endedAt: Date | null;
|
|
138
|
+
messageCount: number;
|
|
139
|
+
summary?: string;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Result from hydrating context before an LLM call
|
|
143
|
+
*/
|
|
144
|
+
interface HydratedContext {
|
|
145
|
+
/** Compiled prompt ready for injection into system message */
|
|
146
|
+
compiledPrompt: string;
|
|
147
|
+
/** Raw facts that were retrieved */
|
|
148
|
+
facts: MemoryFact[];
|
|
149
|
+
/** Recent conversation history */
|
|
150
|
+
recentHistory: ConversationExchange[];
|
|
151
|
+
/** Token estimate for the compiled context */
|
|
152
|
+
estimatedTokens: number;
|
|
153
|
+
/** Whether this was served from cache */
|
|
154
|
+
fromCache: boolean;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Options for hydration
|
|
158
|
+
*/
|
|
159
|
+
interface HydrateOptions {
|
|
160
|
+
/** Maximum number of facts to include */
|
|
161
|
+
maxFacts?: number;
|
|
162
|
+
/** Maximum number of recent messages to include */
|
|
163
|
+
maxHistory?: number;
|
|
164
|
+
/** Specific predicates to filter for */
|
|
165
|
+
predicates?: string[];
|
|
166
|
+
/** Whether to include invalidated facts */
|
|
167
|
+
includeInvalidated?: boolean;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Options for LLM completion
|
|
171
|
+
*/
|
|
172
|
+
interface CompletionOptions {
|
|
173
|
+
/** System prompt */
|
|
174
|
+
systemPrompt: string;
|
|
175
|
+
/** User prompt */
|
|
176
|
+
userPrompt: string;
|
|
177
|
+
/** Maximum tokens in response */
|
|
178
|
+
maxTokens?: number;
|
|
179
|
+
/** Temperature (0-1, lower = more deterministic) */
|
|
180
|
+
temperature?: number;
|
|
181
|
+
/** Force JSON output mode */
|
|
182
|
+
jsonMode?: boolean;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Result from LLM completion
|
|
186
|
+
*/
|
|
187
|
+
interface CompletionResult {
|
|
188
|
+
/** The generated content */
|
|
189
|
+
content: string;
|
|
190
|
+
/** Token usage */
|
|
191
|
+
usage: {
|
|
192
|
+
inputTokens: number;
|
|
193
|
+
outputTokens: number;
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Supported LLM providers
|
|
198
|
+
*/
|
|
199
|
+
type ProviderName = "openai" | "anthropic" | "gemini" | "groq" | "cerebras";
|
|
200
|
+
/**
|
|
201
|
+
* LLM provider configuration
|
|
202
|
+
*/
|
|
203
|
+
interface ProviderConfig {
|
|
204
|
+
/** Provider name */
|
|
205
|
+
provider: ProviderName;
|
|
206
|
+
/** API key */
|
|
207
|
+
apiKey: string;
|
|
208
|
+
/** Model to use (each provider has its own default) */
|
|
209
|
+
model?: string;
|
|
210
|
+
/** Base URL override (for proxies or self-hosted) */
|
|
211
|
+
baseUrl?: string;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Filter options for querying facts
|
|
215
|
+
*/
|
|
216
|
+
interface FactFilter {
|
|
217
|
+
/** Filter by subject */
|
|
218
|
+
subject?: string;
|
|
219
|
+
/** Filter by predicate */
|
|
220
|
+
predicate?: string;
|
|
221
|
+
/** Filter by predicates (OR) */
|
|
222
|
+
predicates?: string[];
|
|
223
|
+
/** Only valid (non-invalidated) facts */
|
|
224
|
+
validOnly?: boolean;
|
|
225
|
+
/** Limit number of results */
|
|
226
|
+
limit?: number;
|
|
227
|
+
/** Order by field */
|
|
228
|
+
orderBy?: "createdAt" | "updatedAt" | "confidence";
|
|
229
|
+
/** Order direction */
|
|
230
|
+
orderDir?: "asc" | "desc";
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Options for MemoryOS behavior
|
|
234
|
+
*/
|
|
235
|
+
interface MemoryOSOptions {
|
|
236
|
+
/** Auto-summarize conversations after this many messages */
|
|
237
|
+
autoSummarizeAfter?: number;
|
|
238
|
+
/** Conflict resolution strategy */
|
|
239
|
+
conflictStrategy?: "latest" | "merge" | "keep_both";
|
|
240
|
+
/** Enable semantic caching */
|
|
241
|
+
enableCache?: boolean;
|
|
242
|
+
/** Cache TTL in seconds */
|
|
243
|
+
cacheTtl?: number;
|
|
244
|
+
/** Debug mode */
|
|
245
|
+
debug?: boolean;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Events emitted by MemoryOS
|
|
249
|
+
*/
|
|
250
|
+
interface MemoryOSEvents {
|
|
251
|
+
"fact:created": (fact: MemoryFact) => void;
|
|
252
|
+
"fact:updated": (fact: MemoryFact, oldFact: MemoryFact) => void;
|
|
253
|
+
"fact:deleted": (fact: MemoryFact, reason: string) => void;
|
|
254
|
+
"session:start": (session: Session) => void;
|
|
255
|
+
"session:end": (session: Session) => void;
|
|
256
|
+
"extraction:complete": (result: ExtractionResult) => void;
|
|
257
|
+
error: (error: Error) => void;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export type { ConversationExchange as C, ExtractionResult as E, FactFilter as F, HydrateOptions as H, MemoryFact as M, ProviderConfig as P, Session as S, CompletionOptions as a, CompletionResult as b, MemoryOSOptions as c, HydratedContext as d, ProviderName as e, MemoryOperation as f, Message as g, MemoryOSEvents as h };
|
package/logo.png
ADDED
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mzhub/mem-ts",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Persistent memory for AI agents - A tiered memory system with fact extraction and conflict resolution",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./adapters": {
|
|
15
|
+
"types": "./dist/adapters/index.d.ts",
|
|
16
|
+
"import": "./dist/adapters/index.mjs",
|
|
17
|
+
"require": "./dist/adapters/index.js"
|
|
18
|
+
},
|
|
19
|
+
"./providers": {
|
|
20
|
+
"types": "./dist/providers/index.d.ts",
|
|
21
|
+
"import": "./dist/providers/index.mjs",
|
|
22
|
+
"require": "./dist/providers/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./middleware": {
|
|
25
|
+
"types": "./dist/middleware/index.d.ts",
|
|
26
|
+
"import": "./dist/middleware/index.mjs",
|
|
27
|
+
"require": "./dist/middleware/index.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist",
|
|
32
|
+
"README.md",
|
|
33
|
+
"logo.png"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsup",
|
|
37
|
+
"dev": "tsup --watch",
|
|
38
|
+
"test": "vitest",
|
|
39
|
+
"test:run": "vitest run",
|
|
40
|
+
"lint": "eslint src",
|
|
41
|
+
"typecheck": "tsc --noEmit",
|
|
42
|
+
"prepublishOnly": "npm run build"
|
|
43
|
+
},
|
|
44
|
+
"keywords": [
|
|
45
|
+
"ai",
|
|
46
|
+
"memory",
|
|
47
|
+
"llm",
|
|
48
|
+
"agents",
|
|
49
|
+
"knowledge-graph",
|
|
50
|
+
"context",
|
|
51
|
+
"openai",
|
|
52
|
+
"anthropic",
|
|
53
|
+
"gemini",
|
|
54
|
+
"groq",
|
|
55
|
+
"cerebras",
|
|
56
|
+
"ai-memory",
|
|
57
|
+
"chatbot-memory"
|
|
58
|
+
],
|
|
59
|
+
"author": "MZ Hub",
|
|
60
|
+
"license": "MIT",
|
|
61
|
+
"repository": {
|
|
62
|
+
"type": "git",
|
|
63
|
+
"url": "https://github.com/MZ-Hub/mem-ts.git"
|
|
64
|
+
},
|
|
65
|
+
"homepage": "https://github.com/MZ-Hub/mem-ts#readme",
|
|
66
|
+
"bugs": {
|
|
67
|
+
"url": "https://github.com/MZ-Hub/mem-ts/issues"
|
|
68
|
+
},
|
|
69
|
+
"devDependencies": {
|
|
70
|
+
"@types/node": "^22.10.2",
|
|
71
|
+
"@types/uuid": "^10.0.0",
|
|
72
|
+
"tsup": "^8.5.1",
|
|
73
|
+
"typescript": "^5.7.2",
|
|
74
|
+
"vitest": "^2.1.9"
|
|
75
|
+
},
|
|
76
|
+
"dependencies": {
|
|
77
|
+
"uuid": "^11.0.3"
|
|
78
|
+
},
|
|
79
|
+
"peerDependencies": {
|
|
80
|
+
"@anthropic-ai/sdk": "^0.30.0",
|
|
81
|
+
"@cerebras/cerebras_cloud_sdk": "^1.0.0",
|
|
82
|
+
"@google/generative-ai": ">=0.21.0",
|
|
83
|
+
"groq-sdk": "^0.8.0",
|
|
84
|
+
"openai": "^4.0.0",
|
|
85
|
+
"mongodb": "^6.0.0",
|
|
86
|
+
"pg": "^8.0.0"
|
|
87
|
+
},
|
|
88
|
+
"peerDependenciesMeta": {
|
|
89
|
+
"openai": {
|
|
90
|
+
"optional": true
|
|
91
|
+
},
|
|
92
|
+
"@anthropic-ai/sdk": {
|
|
93
|
+
"optional": true
|
|
94
|
+
},
|
|
95
|
+
"@google/generative-ai": {
|
|
96
|
+
"optional": true
|
|
97
|
+
},
|
|
98
|
+
"groq-sdk": {
|
|
99
|
+
"optional": true
|
|
100
|
+
},
|
|
101
|
+
"@cerebras/cerebras_cloud_sdk": {
|
|
102
|
+
"optional": true
|
|
103
|
+
},
|
|
104
|
+
"mongodb": {
|
|
105
|
+
"optional": true
|
|
106
|
+
},
|
|
107
|
+
"pg": {
|
|
108
|
+
"optional": true
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
"engines": {
|
|
112
|
+
"node": ">=18.0.0"
|
|
113
|
+
}
|
|
114
|
+
}
|