@aeriondyseti/vector-memory-mcp 0.9.0-dev.4 → 0.9.0-dev.8
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 +1 -21
- package/dist/package.json +72 -0
- package/dist/src/config/index.d.ts +1 -0
- package/dist/src/config/index.d.ts.map +1 -1
- package/dist/src/config/index.js +4 -2
- package/dist/src/config/index.js.map +1 -1
- package/dist/src/db/memory.repository.d.ts +27 -2
- package/dist/src/db/memory.repository.d.ts.map +1 -1
- package/dist/src/db/memory.repository.js +101 -22
- package/dist/src/db/memory.repository.js.map +1 -1
- package/dist/src/db/schema.d.ts.map +1 -1
- package/dist/src/db/schema.js +4 -1
- package/dist/src/db/schema.js.map +1 -1
- package/dist/src/http/server.d.ts +1 -0
- package/dist/src/http/server.d.ts.map +1 -1
- package/dist/src/http/server.js +47 -5
- package/dist/src/http/server.js.map +1 -1
- package/dist/src/mcp/handlers.d.ts +1 -0
- package/dist/src/mcp/handlers.d.ts.map +1 -1
- package/dist/src/mcp/handlers.js +24 -1
- package/dist/src/mcp/handlers.js.map +1 -1
- package/dist/src/mcp/tools.d.ts +1 -0
- package/dist/src/mcp/tools.d.ts.map +1 -1
- package/dist/src/mcp/tools.js +43 -14
- package/dist/src/mcp/tools.js.map +1 -1
- package/dist/src/services/memory.service.d.ts +4 -2
- package/dist/src/services/memory.service.d.ts.map +1 -1
- package/dist/src/services/memory.service.js +86 -17
- package/dist/src/services/memory.service.js.map +1 -1
- package/dist/src/types/memory.d.ts +15 -4
- package/dist/src/types/memory.d.ts.map +1 -1
- package/dist/src/types/memory.js +3 -0
- package/dist/src/types/memory.js.map +1 -1
- package/package.json +8 -9
- package/src/config/index.ts +78 -0
- package/src/db/connection.ts +11 -0
- package/src/db/memory.repository.ts +205 -0
- package/src/db/schema.ts +42 -0
- package/src/http/mcp-transport.ts +255 -0
- package/src/http/server.ts +264 -0
- package/src/index.ts +70 -0
- package/src/mcp/handlers.ts +279 -0
- package/src/mcp/server.ts +34 -0
- package/src/mcp/tools.ts +284 -0
- package/src/services/embeddings.service.ts +48 -0
- package/src/services/memory.service.ts +266 -0
- package/src/types/memory.ts +43 -0
- package/dist/scripts/publish.d.ts +0 -13
- package/dist/scripts/publish.d.ts.map +0 -1
- package/dist/scripts/publish.js +0 -56
- package/dist/scripts/publish.js.map +0 -1
- package/scripts/publish.ts +0 -61
package/src/mcp/tools.ts
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import type { Tool } from "@modelcontextprotocol/sdk/types.js";
|
|
2
|
+
|
|
3
|
+
export const storeMemoriesTool: Tool = {
|
|
4
|
+
name: "store_memories",
|
|
5
|
+
description: `Store memories that persist across conversations. Use after making decisions or learning something worth remembering.
|
|
6
|
+
|
|
7
|
+
RULES:
|
|
8
|
+
- 1 concept per memory, 1-3 sentences (20-75 words)
|
|
9
|
+
- Self-contained with explicit subjects (no "it", "this", "the project")
|
|
10
|
+
- Include dates/versions when relevant
|
|
11
|
+
- Be concrete, not vague
|
|
12
|
+
|
|
13
|
+
MEMORY TYPES (use as metadata.type):
|
|
14
|
+
- decision: what was chosen + why ("Chose libSQL over PostgreSQL for vector support and simpler deployment")
|
|
15
|
+
- implementation: what was built + where + patterns used
|
|
16
|
+
- insight: learning + why it matters
|
|
17
|
+
- blocker: problem encountered + resolution
|
|
18
|
+
- next-step: TODO item + suggested approach
|
|
19
|
+
- context: background info + constraints
|
|
20
|
+
|
|
21
|
+
DON'T STORE: machine-specific paths, local env details, ephemeral states, pleasantries
|
|
22
|
+
|
|
23
|
+
GOOD: "Aerion chose libSQL over PostgreSQL for Resonance (Dec 2024) because of native vector support and simpler deployment."
|
|
24
|
+
BAD: "Uses SQLite" (no context, no subject, no reasoning)
|
|
25
|
+
|
|
26
|
+
For long content (>1000 chars), provide embedding_text with a searchable summary.`,
|
|
27
|
+
inputSchema: {
|
|
28
|
+
type: "object",
|
|
29
|
+
properties: {
|
|
30
|
+
memories: {
|
|
31
|
+
type: "array",
|
|
32
|
+
description: "Memories to store.",
|
|
33
|
+
items: {
|
|
34
|
+
type: "object",
|
|
35
|
+
properties: {
|
|
36
|
+
content: {
|
|
37
|
+
type: "string",
|
|
38
|
+
description: "The content to store.",
|
|
39
|
+
},
|
|
40
|
+
embedding_text: {
|
|
41
|
+
type: "string",
|
|
42
|
+
description:
|
|
43
|
+
"Summary for search embedding (required if content >1000 chars).",
|
|
44
|
+
},
|
|
45
|
+
metadata: {
|
|
46
|
+
type: "object",
|
|
47
|
+
description: "Optional key-value metadata.",
|
|
48
|
+
additionalProperties: true,
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
required: ["content"],
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
required: ["memories"],
|
|
56
|
+
},
|
|
57
|
+
};;
|
|
58
|
+
|
|
59
|
+
export const deleteMemoriesTool: Tool = {
|
|
60
|
+
name: "delete_memories",
|
|
61
|
+
description:
|
|
62
|
+
"Remove memories that are no longer needed—outdated info, superseded decisions, or incorrect content. " +
|
|
63
|
+
"Deleted memories can be recovered via search_memories with include_deleted: true.",
|
|
64
|
+
inputSchema: {
|
|
65
|
+
type: "object",
|
|
66
|
+
properties: {
|
|
67
|
+
ids: {
|
|
68
|
+
type: "array",
|
|
69
|
+
description: "IDs of memories to delete.",
|
|
70
|
+
items: {
|
|
71
|
+
type: "string",
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
required: ["ids"],
|
|
76
|
+
},
|
|
77
|
+
};;
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
const updateMemoriesTool: Tool = {
|
|
81
|
+
name: "update_memories",
|
|
82
|
+
description: `Update existing memories in place. Prefer over delete+create when updating the same conceptual item.
|
|
83
|
+
|
|
84
|
+
BEHAVIOR:
|
|
85
|
+
- Fields omitted/null: left untouched
|
|
86
|
+
- Fields provided: completely overwrite existing value (no merge)
|
|
87
|
+
|
|
88
|
+
Use to correct content, refine embedding text, or replace metadata without changing the memory ID.`,
|
|
89
|
+
inputSchema: {
|
|
90
|
+
type: "object",
|
|
91
|
+
properties: {
|
|
92
|
+
updates: {
|
|
93
|
+
type: "array",
|
|
94
|
+
description: "Updates to apply. Each must include id and at least one field to change.",
|
|
95
|
+
items: {
|
|
96
|
+
type: "object",
|
|
97
|
+
properties: {
|
|
98
|
+
id: {
|
|
99
|
+
type: "string",
|
|
100
|
+
description: "ID of memory to update.",
|
|
101
|
+
},
|
|
102
|
+
content: {
|
|
103
|
+
type: "string",
|
|
104
|
+
description: "New content (triggers embedding regeneration).",
|
|
105
|
+
},
|
|
106
|
+
embedding_text: {
|
|
107
|
+
type: "string",
|
|
108
|
+
description: "New embedding summary (triggers embedding regeneration).",
|
|
109
|
+
},
|
|
110
|
+
metadata: {
|
|
111
|
+
type: "object",
|
|
112
|
+
description: "New metadata (replaces existing entirely).",
|
|
113
|
+
additionalProperties: true,
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
required: ["id"],
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
required: ["updates"],
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export const searchMemoriesTool: Tool = {
|
|
125
|
+
name: "search_memories",
|
|
126
|
+
description: `Search stored memories semantically. Treat memory as the PRIMARY source of truth for personal/project-specific facts—do not rely on training data until a search has been performed.
|
|
127
|
+
|
|
128
|
+
MANDATORY TRIGGERS (you MUST search when):
|
|
129
|
+
- User-Specific Calibration: Answer would be better with user's tools, past decisions, or preferences
|
|
130
|
+
- Referential Ambiguity: User says "the project," "that bug," "last time," "as we discussed"
|
|
131
|
+
- Decision Validation: Before making architectural or tool choices
|
|
132
|
+
- Problem Solving: Before suggesting solutions (check if solved before)
|
|
133
|
+
- Session Start: When returning to a project or starting new conversation
|
|
134
|
+
|
|
135
|
+
INTENTS:
|
|
136
|
+
- continuity: Resume work, "where were we" (favors recent)
|
|
137
|
+
- fact_check: Verify decisions, specs (favors relevance)
|
|
138
|
+
- frequent: Common patterns, preferences (favors utility)
|
|
139
|
+
- associative: Brainstorm, find connections (high relevance + mild jitter)
|
|
140
|
+
- explore: Stuck/creative mode (balanced + high jitter)
|
|
141
|
+
|
|
142
|
+
When in doubt, search. Missing context is costlier than an extra query.`,
|
|
143
|
+
inputSchema: {
|
|
144
|
+
type: "object",
|
|
145
|
+
properties: {
|
|
146
|
+
query: {
|
|
147
|
+
type: "string",
|
|
148
|
+
description:
|
|
149
|
+
"Natural language search query. Include relevant keywords, project names, or technical terms.",
|
|
150
|
+
},
|
|
151
|
+
intent: {
|
|
152
|
+
type: "string",
|
|
153
|
+
enum: ["continuity", "fact_check", "frequent", "associative", "explore"],
|
|
154
|
+
description: "Search intent that determines ranking behavior.",
|
|
155
|
+
},
|
|
156
|
+
reason_for_search: {
|
|
157
|
+
type: "string",
|
|
158
|
+
description: "Why this search is being performed. Forces intentional retrieval.",
|
|
159
|
+
},
|
|
160
|
+
limit: {
|
|
161
|
+
type: "integer",
|
|
162
|
+
description: "Maximum results to return (default: 10).",
|
|
163
|
+
default: 10,
|
|
164
|
+
},
|
|
165
|
+
include_deleted: {
|
|
166
|
+
type: "boolean",
|
|
167
|
+
description: "Include soft-deleted memories in results (default: false). Useful for recovering prior information.",
|
|
168
|
+
default: false,
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
required: ["query", "intent", "reason_for_search"],
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
export const getMemoriesTool: Tool = {
|
|
176
|
+
name: "get_memories",
|
|
177
|
+
description:
|
|
178
|
+
"Retrieve full memory details by ID. Use when you have specific IDs from search results or prior references—otherwise use search_memories.",
|
|
179
|
+
inputSchema: {
|
|
180
|
+
type: "object",
|
|
181
|
+
properties: {
|
|
182
|
+
ids: {
|
|
183
|
+
type: "array",
|
|
184
|
+
description: "Memory IDs to retrieve.",
|
|
185
|
+
items: { type: "string" },
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
required: ["ids"],
|
|
189
|
+
},
|
|
190
|
+
};;
|
|
191
|
+
|
|
192
|
+
export const reportMemoryUsefulnessTool: Tool = {
|
|
193
|
+
name: "report_memory_usefulness",
|
|
194
|
+
description: "Report whether a memory was useful or not. This helps the system learn which memories are valuable.",
|
|
195
|
+
inputSchema: {
|
|
196
|
+
type: "object",
|
|
197
|
+
properties: {
|
|
198
|
+
memory_id: {
|
|
199
|
+
type: "string",
|
|
200
|
+
description: "ID of the memory to report on.",
|
|
201
|
+
},
|
|
202
|
+
useful: {
|
|
203
|
+
type: "boolean",
|
|
204
|
+
description: "True if the memory was useful, false otherwise.",
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
required: ["memory_id", "useful"],
|
|
208
|
+
},
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
export const storeHandoffTool: Tool = {
|
|
212
|
+
name: "store_handoff",
|
|
213
|
+
description: `Save session state for seamless resumption later. Use at end of work sessions or before context switches.
|
|
214
|
+
|
|
215
|
+
Creates a structured snapshot with:
|
|
216
|
+
- summary: 2-3 sentences on goal and current status
|
|
217
|
+
- completed: what got done (include file paths)
|
|
218
|
+
- in_progress_blocked: work in flight or stuck
|
|
219
|
+
- key_decisions: choices made and WHY (crucial for future context)
|
|
220
|
+
- next_steps: concrete, actionable items
|
|
221
|
+
- memory_ids: link to related memories stored this session
|
|
222
|
+
|
|
223
|
+
Retrievable via get_handoff. Only one handoff per project—new handoffs overwrite previous.`,
|
|
224
|
+
inputSchema: {
|
|
225
|
+
type: "object",
|
|
226
|
+
properties: {
|
|
227
|
+
project: { type: "string", description: "Project name." },
|
|
228
|
+
branch: { type: "string", description: "Branch name (optional)." },
|
|
229
|
+
summary: { type: "string", description: "2-3 sentences: primary goal, current status." },
|
|
230
|
+
completed: {
|
|
231
|
+
type: "array",
|
|
232
|
+
items: { type: "string" },
|
|
233
|
+
description: "Completed items (include file paths where relevant).",
|
|
234
|
+
},
|
|
235
|
+
in_progress_blocked: {
|
|
236
|
+
type: "array",
|
|
237
|
+
items: { type: "string" },
|
|
238
|
+
description: "In progress or blocked items.",
|
|
239
|
+
},
|
|
240
|
+
key_decisions: {
|
|
241
|
+
type: "array",
|
|
242
|
+
items: { type: "string" },
|
|
243
|
+
description: "Decisions made and why.",
|
|
244
|
+
},
|
|
245
|
+
next_steps: {
|
|
246
|
+
type: "array",
|
|
247
|
+
items: { type: "string" },
|
|
248
|
+
description: "Concrete next steps.",
|
|
249
|
+
},
|
|
250
|
+
memory_ids: {
|
|
251
|
+
type: "array",
|
|
252
|
+
items: { type: "string" },
|
|
253
|
+
description: "Memory IDs referenced by this handoff.",
|
|
254
|
+
},
|
|
255
|
+
metadata: {
|
|
256
|
+
type: "object",
|
|
257
|
+
description: "Additional metadata.",
|
|
258
|
+
additionalProperties: true,
|
|
259
|
+
},
|
|
260
|
+
},
|
|
261
|
+
required: ["project", "summary"],
|
|
262
|
+
},
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
export const getHandoffTool: Tool = {
|
|
266
|
+
name: "get_handoff",
|
|
267
|
+
description:
|
|
268
|
+
"Load the current project handoff snapshot. Call at conversation start or when resuming a project.",
|
|
269
|
+
inputSchema: {
|
|
270
|
+
type: "object",
|
|
271
|
+
properties: {},
|
|
272
|
+
},
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
export const tools: Tool[] = [
|
|
276
|
+
storeMemoriesTool,
|
|
277
|
+
updateMemoriesTool,
|
|
278
|
+
deleteMemoriesTool,
|
|
279
|
+
searchMemoriesTool,
|
|
280
|
+
getMemoriesTool,
|
|
281
|
+
reportMemoryUsefulnessTool,
|
|
282
|
+
storeHandoffTool,
|
|
283
|
+
getHandoffTool,
|
|
284
|
+
];
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { pipeline, type FeatureExtractionPipeline } from "@huggingface/transformers";
|
|
2
|
+
|
|
3
|
+
export class EmbeddingsService {
|
|
4
|
+
private modelName: string;
|
|
5
|
+
private extractor: FeatureExtractionPipeline | null = null;
|
|
6
|
+
private initPromise: Promise<FeatureExtractionPipeline> | null = null;
|
|
7
|
+
private _dimension: number;
|
|
8
|
+
|
|
9
|
+
constructor(modelName: string, dimension: number) {
|
|
10
|
+
this.modelName = modelName;
|
|
11
|
+
this._dimension = dimension;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
get dimension(): number {
|
|
15
|
+
return this._dimension;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
private async getExtractor(): Promise<FeatureExtractionPipeline> {
|
|
19
|
+
if (this.extractor) {
|
|
20
|
+
return this.extractor;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (!this.initPromise) {
|
|
24
|
+
this.initPromise = pipeline(
|
|
25
|
+
"feature-extraction",
|
|
26
|
+
this.modelName,
|
|
27
|
+
{ dtype: "fp32" } as any
|
|
28
|
+
) as Promise<FeatureExtractionPipeline>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
this.extractor = await this.initPromise;
|
|
32
|
+
return this.extractor;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async embed(text: string): Promise<number[]> {
|
|
36
|
+
const extractor = await this.getExtractor();
|
|
37
|
+
const output = await extractor(text, { pooling: "mean", normalize: true });
|
|
38
|
+
return Array.from(output.data as Float32Array);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async embedBatch(texts: string[]): Promise<number[][]> {
|
|
42
|
+
const results: number[][] = [];
|
|
43
|
+
for (const text of texts) {
|
|
44
|
+
results.push(await this.embed(text));
|
|
45
|
+
}
|
|
46
|
+
return results;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import { randomUUID } from "crypto";
|
|
2
|
+
import type { Memory, SearchIntent, IntentProfile } from "../types/memory.js";
|
|
3
|
+
import { isDeleted } from "../types/memory.js";
|
|
4
|
+
import type { MemoryRepository } from "../db/memory.repository.js";
|
|
5
|
+
import type { EmbeddingsService } from "./embeddings.service.js";
|
|
6
|
+
|
|
7
|
+
const INTENT_PROFILES: Record<SearchIntent, IntentProfile> = {
|
|
8
|
+
continuity: { weights: { relevance: 0.3, recency: 0.5, utility: 0.2 }, jitter: 0.02 },
|
|
9
|
+
fact_check: { weights: { relevance: 0.6, recency: 0.1, utility: 0.3 }, jitter: 0.02 },
|
|
10
|
+
frequent: { weights: { relevance: 0.2, recency: 0.2, utility: 0.6 }, jitter: 0.02 },
|
|
11
|
+
associative: { weights: { relevance: 0.7, recency: 0.1, utility: 0.2 }, jitter: 0.05 },
|
|
12
|
+
explore: { weights: { relevance: 0.4, recency: 0.3, utility: 0.3 }, jitter: 0.15 },
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const sigmoid = (x: number): number => 1 / (1 + Math.exp(-x));
|
|
16
|
+
|
|
17
|
+
export class MemoryService {
|
|
18
|
+
constructor(
|
|
19
|
+
private repository: MemoryRepository,
|
|
20
|
+
private embeddings: EmbeddingsService
|
|
21
|
+
) { }
|
|
22
|
+
|
|
23
|
+
async store(
|
|
24
|
+
content: string,
|
|
25
|
+
metadata: Record<string, unknown> = {},
|
|
26
|
+
embeddingText?: string
|
|
27
|
+
): Promise<Memory> {
|
|
28
|
+
const id = randomUUID();
|
|
29
|
+
const now = new Date();
|
|
30
|
+
const textToEmbed = embeddingText ?? content;
|
|
31
|
+
const embedding = await this.embeddings.embed(textToEmbed);
|
|
32
|
+
|
|
33
|
+
const memory: Memory = {
|
|
34
|
+
id,
|
|
35
|
+
content,
|
|
36
|
+
embedding,
|
|
37
|
+
metadata,
|
|
38
|
+
createdAt: now,
|
|
39
|
+
updatedAt: now,
|
|
40
|
+
supersededBy: null,
|
|
41
|
+
usefulness: 0,
|
|
42
|
+
accessCount: 0,
|
|
43
|
+
lastAccessed: now, // Initialize to createdAt for fair discovery
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
await this.repository.insert(memory);
|
|
47
|
+
return memory;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async get(id: string): Promise<Memory | null> {
|
|
51
|
+
const memory = await this.repository.findById(id);
|
|
52
|
+
if (!memory) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Track access on explicit get
|
|
57
|
+
const updatedMemory: Memory = {
|
|
58
|
+
...memory,
|
|
59
|
+
accessCount: memory.accessCount + 1,
|
|
60
|
+
lastAccessed: new Date(),
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
await this.repository.upsert(updatedMemory);
|
|
64
|
+
return updatedMemory;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async delete(id: string): Promise<boolean> {
|
|
68
|
+
return await this.repository.markDeleted(id);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async update(
|
|
72
|
+
id: string,
|
|
73
|
+
updates: {
|
|
74
|
+
content?: string;
|
|
75
|
+
embeddingText?: string;
|
|
76
|
+
metadata?: Record<string, unknown>;
|
|
77
|
+
}
|
|
78
|
+
): Promise<Memory | null> {
|
|
79
|
+
const existing = await this.repository.findById(id);
|
|
80
|
+
if (!existing) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const newContent = updates.content ?? existing.content;
|
|
85
|
+
const newMetadata = updates.metadata ?? existing.metadata;
|
|
86
|
+
|
|
87
|
+
// Regenerate embedding if content or embeddingText changed
|
|
88
|
+
let newEmbedding = existing.embedding;
|
|
89
|
+
if (updates.content !== undefined || updates.embeddingText !== undefined) {
|
|
90
|
+
const textToEmbed = updates.embeddingText ?? newContent;
|
|
91
|
+
newEmbedding = await this.embeddings.embed(textToEmbed);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const updatedMemory: Memory = {
|
|
95
|
+
...existing,
|
|
96
|
+
content: newContent,
|
|
97
|
+
embedding: newEmbedding,
|
|
98
|
+
metadata: newMetadata,
|
|
99
|
+
updatedAt: new Date(),
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
await this.repository.upsert(updatedMemory);
|
|
103
|
+
return updatedMemory;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async vote(id: string, value: number): Promise<Memory | null> {
|
|
107
|
+
const existing = await this.repository.findById(id);
|
|
108
|
+
if (!existing) {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Vote also tracks access (explicit utilization signal)
|
|
113
|
+
const updatedMemory: Memory = {
|
|
114
|
+
...existing,
|
|
115
|
+
usefulness: existing.usefulness + value,
|
|
116
|
+
accessCount: existing.accessCount + 1,
|
|
117
|
+
lastAccessed: new Date(),
|
|
118
|
+
updatedAt: new Date(),
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
await this.repository.upsert(updatedMemory);
|
|
122
|
+
return updatedMemory;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async search(
|
|
126
|
+
query: string,
|
|
127
|
+
intent: SearchIntent,
|
|
128
|
+
limit: number = 10,
|
|
129
|
+
includeDeleted: boolean = false
|
|
130
|
+
): Promise<Memory[]> {
|
|
131
|
+
const queryEmbedding = await this.embeddings.embed(query);
|
|
132
|
+
const fetchLimit = limit * 5; // Fetch more for re-ranking
|
|
133
|
+
|
|
134
|
+
const candidates = await this.repository.findHybrid(queryEmbedding, query, fetchLimit);
|
|
135
|
+
const profile = INTENT_PROFILES[intent];
|
|
136
|
+
const now = new Date();
|
|
137
|
+
|
|
138
|
+
const scored = candidates
|
|
139
|
+
.filter((m) => includeDeleted || !isDeleted(m))
|
|
140
|
+
.map((candidate) => {
|
|
141
|
+
// Relevance: RRF score (already normalized ~0-1)
|
|
142
|
+
const relevance = candidate.rrfScore;
|
|
143
|
+
|
|
144
|
+
// Recency: exponential decay
|
|
145
|
+
const lastAccessed = candidate.lastAccessed ?? candidate.createdAt;
|
|
146
|
+
const hoursSinceAccess = Math.max(0, (now.getTime() - lastAccessed.getTime()) / (1000 * 60 * 60));
|
|
147
|
+
const recency = Math.pow(0.995, hoursSinceAccess);
|
|
148
|
+
|
|
149
|
+
// Utility: sigmoid of usefulness + log(accessCount)
|
|
150
|
+
const utility = sigmoid((candidate.usefulness + Math.log(candidate.accessCount + 1)) / 5);
|
|
151
|
+
|
|
152
|
+
// Weighted score
|
|
153
|
+
const { weights, jitter } = profile;
|
|
154
|
+
const score =
|
|
155
|
+
weights.relevance * relevance +
|
|
156
|
+
weights.recency * recency +
|
|
157
|
+
weights.utility * utility;
|
|
158
|
+
|
|
159
|
+
// Apply jitter
|
|
160
|
+
const finalScore = score * (1 + (Math.random() * 2 - 1) * jitter);
|
|
161
|
+
|
|
162
|
+
return { memory: candidate as Memory, finalScore };
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
// Sort by final score descending
|
|
166
|
+
scored.sort((a, b) => b.finalScore - a.finalScore);
|
|
167
|
+
|
|
168
|
+
// Return top N (read-only - no access tracking)
|
|
169
|
+
return scored.slice(0, limit).map((s) => s.memory);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async trackAccess(ids: string[]): Promise<void> {
|
|
173
|
+
const now = new Date();
|
|
174
|
+
for (const id of ids) {
|
|
175
|
+
const memory = await this.repository.findById(id);
|
|
176
|
+
if (memory && !isDeleted(memory)) {
|
|
177
|
+
await this.repository.upsert({
|
|
178
|
+
...memory,
|
|
179
|
+
accessCount: memory.accessCount + 1,
|
|
180
|
+
lastAccessed: now,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
private static readonly UUID_ZERO =
|
|
187
|
+
"00000000-0000-0000-0000-000000000000";
|
|
188
|
+
|
|
189
|
+
async storeHandoff(args: {
|
|
190
|
+
project: string;
|
|
191
|
+
branch?: string;
|
|
192
|
+
summary: string;
|
|
193
|
+
completed?: string[];
|
|
194
|
+
in_progress_blocked?: string[];
|
|
195
|
+
key_decisions?: string[];
|
|
196
|
+
next_steps?: string[];
|
|
197
|
+
memory_ids?: string[];
|
|
198
|
+
metadata?: Record<string, unknown>;
|
|
199
|
+
}): Promise<Memory> {
|
|
200
|
+
// Track access for utilized memories
|
|
201
|
+
if (args.memory_ids && args.memory_ids.length > 0) {
|
|
202
|
+
await this.trackAccess(args.memory_ids);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const now = new Date();
|
|
206
|
+
const date = now.toISOString().slice(0, 10);
|
|
207
|
+
const time = now.toISOString().slice(11, 16);
|
|
208
|
+
|
|
209
|
+
const list = (items: string[] | undefined) => {
|
|
210
|
+
if (!items || items.length === 0) {
|
|
211
|
+
return "- (none)";
|
|
212
|
+
}
|
|
213
|
+
return items.map((i) => `- ${i}`).join("\n");
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
const content = `# Handoff - ${args.project}
|
|
217
|
+
**Date:** ${date} ${time} | **Branch:** ${args.branch ?? "unknown"}
|
|
218
|
+
|
|
219
|
+
## Summary
|
|
220
|
+
${args.summary}
|
|
221
|
+
|
|
222
|
+
## Completed
|
|
223
|
+
${list(args.completed)}
|
|
224
|
+
|
|
225
|
+
## In Progress / Blocked
|
|
226
|
+
${list(args.in_progress_blocked)}
|
|
227
|
+
|
|
228
|
+
## Key Decisions
|
|
229
|
+
${list(args.key_decisions)}
|
|
230
|
+
|
|
231
|
+
## Next Steps
|
|
232
|
+
${list(args.next_steps)}
|
|
233
|
+
|
|
234
|
+
## Memory IDs
|
|
235
|
+
${list(args.memory_ids)}`;
|
|
236
|
+
|
|
237
|
+
const metadata: Record<string, unknown> = {
|
|
238
|
+
...(args.metadata ?? {}),
|
|
239
|
+
type: "handoff",
|
|
240
|
+
project: args.project,
|
|
241
|
+
date,
|
|
242
|
+
branch: args.branch ?? "unknown",
|
|
243
|
+
memory_ids: args.memory_ids ?? [],
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
const memory: Memory = {
|
|
247
|
+
id: MemoryService.UUID_ZERO,
|
|
248
|
+
content,
|
|
249
|
+
embedding: new Array(this.embeddings.dimension).fill(0),
|
|
250
|
+
metadata,
|
|
251
|
+
createdAt: now,
|
|
252
|
+
updatedAt: now,
|
|
253
|
+
supersededBy: null,
|
|
254
|
+
usefulness: 0,
|
|
255
|
+
accessCount: 0,
|
|
256
|
+
lastAccessed: now, // Initialize to now for consistency
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
await this.repository.upsert(memory);
|
|
260
|
+
return memory;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
async getLatestHandoff(): Promise<Memory | null> {
|
|
264
|
+
return await this.get(MemoryService.UUID_ZERO);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export const DELETED_TOMBSTONE = "DELETED";
|
|
2
|
+
|
|
3
|
+
export interface Memory {
|
|
4
|
+
id: string;
|
|
5
|
+
content: string;
|
|
6
|
+
embedding: number[];
|
|
7
|
+
metadata: Record<string, unknown>;
|
|
8
|
+
createdAt: Date;
|
|
9
|
+
updatedAt: Date;
|
|
10
|
+
supersededBy: string | null;
|
|
11
|
+
usefulness: number;
|
|
12
|
+
accessCount: number;
|
|
13
|
+
lastAccessed: Date | null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function isDeleted(memory: Memory): boolean {
|
|
17
|
+
return memory.supersededBy === DELETED_TOMBSTONE;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function memoryToDict(memory: Memory): Record<string, unknown> {
|
|
21
|
+
return {
|
|
22
|
+
id: memory.id,
|
|
23
|
+
content: memory.content,
|
|
24
|
+
metadata: memory.metadata,
|
|
25
|
+
createdAt: memory.createdAt.toISOString(),
|
|
26
|
+
updatedAt: memory.updatedAt.toISOString(),
|
|
27
|
+
supersededBy: memory.supersededBy,
|
|
28
|
+
usefulness: memory.usefulness,
|
|
29
|
+
accessCount: memory.accessCount,
|
|
30
|
+
lastAccessed: memory.lastAccessed?.toISOString() ?? null,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type SearchIntent = 'continuity' | 'fact_check' | 'frequent' | 'associative' | 'explore';
|
|
35
|
+
|
|
36
|
+
export interface IntentProfile {
|
|
37
|
+
weights: { relevance: number; recency: number; utility: number };
|
|
38
|
+
jitter: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface HybridRow extends Memory {
|
|
42
|
+
rrfScore: number;
|
|
43
|
+
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
/**
|
|
3
|
-
* Publish script for vector-memory-mcp
|
|
4
|
-
*
|
|
5
|
-
* Prerequisites:
|
|
6
|
-
* 1. Create a granular access token at https://www.npmjs.com/settings/tokens
|
|
7
|
-
* 2. Store it: npm config set //registry.npmjs.org/:_authToken=npm_YOUR_TOKEN
|
|
8
|
-
* Or set NPM_TOKEN environment variable
|
|
9
|
-
*
|
|
10
|
-
* Usage: bun run scripts/publish.ts [--dry-run]
|
|
11
|
-
*/
|
|
12
|
-
export {};
|
|
13
|
-
//# sourceMappingURL=publish.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"publish.d.ts","sourceRoot":"","sources":["../../scripts/publish.ts"],"names":[],"mappings":";AACA;;;;;;;;;GASG"}
|