memo-grafter 0.1.0 → 0.2.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/.env.example +5 -3
- package/README.md +187 -182
- package/USER_GUIDE.md +932 -702
- package/dist/MemoGrafter.d.ts +1 -1
- package/dist/MemoGrafter.d.ts.map +1 -1
- package/dist/MemoGrafter.js +16 -5
- package/dist/MemoGrafter.js.map +1 -1
- package/dist/MemoGrafterAgent.d.ts +7 -1
- package/dist/MemoGrafterAgent.d.ts.map +1 -1
- package/dist/MemoGrafterAgent.js +45 -8
- package/dist/MemoGrafterAgent.js.map +1 -1
- package/dist/adapters/AnthropicAdapter.d.ts +9 -0
- package/dist/adapters/AnthropicAdapter.d.ts.map +1 -0
- package/dist/adapters/AnthropicAdapter.js +33 -0
- package/dist/adapters/AnthropicAdapter.js.map +1 -0
- package/dist/adapters/GeminiAdapter.d.ts +15 -0
- package/dist/adapters/GeminiAdapter.d.ts.map +1 -0
- package/dist/adapters/GeminiAdapter.js +48 -0
- package/dist/adapters/GeminiAdapter.js.map +1 -0
- package/dist/fleet/FleetStore.d.ts +1 -1
- package/dist/fleet/FleetStore.d.ts.map +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/pipeline/GrafterPipeline.d.ts +1 -3
- package/dist/pipeline/GrafterPipeline.d.ts.map +1 -1
- package/dist/pipeline/GrafterPipeline.js +6 -19
- package/dist/pipeline/GrafterPipeline.js.map +1 -1
- package/dist/pipeline/IngestPipeline.d.ts +7 -3
- package/dist/pipeline/IngestPipeline.d.ts.map +1 -1
- package/dist/pipeline/IngestPipeline.js +31 -5
- package/dist/pipeline/IngestPipeline.js.map +1 -1
- package/dist/pipeline/RetrieverPipeline.d.ts +11 -0
- package/dist/pipeline/RetrieverPipeline.d.ts.map +1 -0
- package/dist/pipeline/RetrieverPipeline.js +73 -0
- package/dist/pipeline/RetrieverPipeline.js.map +1 -0
- package/dist/pipeline/SegmentProcessor.d.ts +3 -2
- package/dist/pipeline/SegmentProcessor.d.ts.map +1 -1
- package/dist/pipeline/SegmentProcessor.js +52 -40
- package/dist/pipeline/SegmentProcessor.js.map +1 -1
- package/dist/pipeline/TopicDriftDetector.d.ts +21 -3
- package/dist/pipeline/TopicDriftDetector.d.ts.map +1 -1
- package/dist/pipeline/TopicDriftDetector.js +143 -17
- package/dist/pipeline/TopicDriftDetector.js.map +1 -1
- package/dist/prompts/factRetrievalPrompt.d.ts +4 -0
- package/dist/prompts/factRetrievalPrompt.d.ts.map +1 -0
- package/dist/prompts/factRetrievalPrompt.js +25 -0
- package/dist/prompts/factRetrievalPrompt.js.map +1 -0
- package/dist/prompts/historyCompressionPrompt.d.ts +3 -0
- package/dist/prompts/historyCompressionPrompt.d.ts.map +1 -0
- package/dist/prompts/historyCompressionPrompt.js +4 -0
- package/dist/prompts/historyCompressionPrompt.js.map +1 -0
- package/dist/prompts/intentShiftPrompt.d.ts +3 -0
- package/dist/prompts/intentShiftPrompt.d.ts.map +1 -0
- package/dist/prompts/intentShiftPrompt.js +11 -0
- package/dist/prompts/intentShiftPrompt.js.map +1 -0
- package/dist/prompts/memoryInjectionPrompt.d.ts +4 -0
- package/dist/prompts/memoryInjectionPrompt.d.ts.map +1 -0
- package/dist/prompts/memoryInjectionPrompt.js +16 -0
- package/dist/prompts/memoryInjectionPrompt.js.map +1 -0
- package/dist/prompts/segmentExtractionPrompt.d.ts +3 -0
- package/dist/prompts/segmentExtractionPrompt.d.ts.map +1 -0
- package/dist/prompts/segmentExtractionPrompt.js +81 -0
- package/dist/prompts/segmentExtractionPrompt.js.map +1 -0
- package/dist/store/GraphStore.d.ts +16 -19
- package/dist/store/GraphStore.d.ts.map +1 -1
- package/dist/store/GraphStore.js +1 -576
- package/dist/store/GraphStore.js.map +1 -1
- package/dist/store/index.d.ts +3 -0
- package/dist/store/index.d.ts.map +1 -0
- package/dist/store/index.js +2 -0
- package/dist/store/index.js.map +1 -0
- package/dist/store/postgres-pgvector/GraphStore.d.ts +75 -0
- package/dist/store/postgres-pgvector/GraphStore.d.ts.map +1 -0
- package/dist/store/postgres-pgvector/GraphStore.js +782 -0
- package/dist/store/postgres-pgvector/GraphStore.js.map +1 -0
- package/dist/types.d.ts +69 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/drift/driftMarkers.d.ts +4 -0
- package/dist/utils/drift/driftMarkers.d.ts.map +1 -0
- package/dist/utils/drift/driftMarkers.js +31 -0
- package/dist/utils/drift/driftMarkers.js.map +1 -0
- package/dist/utils/drift/driftScore.d.ts +3 -0
- package/dist/utils/drift/driftScore.d.ts.map +1 -0
- package/dist/utils/drift/driftScore.js +11 -0
- package/dist/utils/drift/driftScore.js.map +1 -0
- package/dist/utils/drift/driftThreshold.d.ts +7 -0
- package/dist/utils/drift/driftThreshold.d.ts.map +1 -0
- package/dist/utils/drift/driftThreshold.js +21 -0
- package/dist/utils/drift/driftThreshold.js.map +1 -0
- package/dist/utils/drift/reentryMatch.d.ts +3 -0
- package/dist/utils/drift/reentryMatch.d.ts.map +1 -0
- package/dist/utils/drift/reentryMatch.js +10 -0
- package/dist/utils/drift/reentryMatch.js.map +1 -0
- package/dist/utils/extraction/segmentExtraction.d.ts +5 -0
- package/dist/utils/extraction/segmentExtraction.d.ts.map +1 -0
- package/dist/utils/extraction/segmentExtraction.js +82 -0
- package/dist/utils/extraction/segmentExtraction.js.map +1 -0
- package/dist/utils/reentry/reentryCues.d.ts +3 -0
- package/dist/utils/reentry/reentryCues.d.ts.map +1 -0
- package/dist/utils/reentry/reentryCues.js +14 -0
- package/dist/utils/reentry/reentryCues.js.map +1 -0
- package/dist/utils/reentry/reentryEdges.d.ts +16 -0
- package/dist/utils/reentry/reentryEdges.d.ts.map +1 -0
- package/dist/utils/reentry/reentryEdges.js +80 -0
- package/dist/utils/reentry/reentryEdges.js.map +1 -0
- package/dist/utils/reentry/reentrySimilarity.d.ts +5 -0
- package/dist/utils/reentry/reentrySimilarity.d.ts.map +1 -0
- package/dist/utils/reentry/reentrySimilarity.js +26 -0
- package/dist/utils/reentry/reentrySimilarity.js.map +1 -0
- package/dist/utils/reentry/reentryText.d.ts +6 -0
- package/dist/utils/reentry/reentryText.d.ts.map +1 -0
- package/dist/utils/reentry/reentryText.js +29 -0
- package/dist/utils/reentry/reentryText.js.map +1 -0
- package/dist/utils/reentry/types.d.ts +6 -0
- package/dist/utils/reentry/types.d.ts.map +1 -0
- package/dist/utils/reentry/types.js +2 -0
- package/dist/utils/reentry/types.js.map +1 -0
- package/dist/utils/text/normalizeText.d.ts +2 -0
- package/dist/utils/text/normalizeText.d.ts.map +1 -0
- package/dist/utils/text/normalizeText.js +5 -0
- package/dist/utils/text/normalizeText.js.map +1 -0
- package/dist/utils/text/terms.d.ts +3 -0
- package/dist/utils/text/terms.d.ts.map +1 -0
- package/dist/utils/text/terms.js +51 -0
- package/dist/utils/text/terms.js.map +1 -0
- package/dist/utils/text/tokenCount.d.ts +2 -0
- package/dist/utils/text/tokenCount.d.ts.map +1 -0
- package/dist/utils/text/tokenCount.js +4 -0
- package/dist/utils/text/tokenCount.js.map +1 -0
- package/dist/utils/vector/vectorLiteral.d.ts +3 -0
- package/dist/utils/vector/vectorLiteral.d.ts.map +1 -0
- package/dist/utils/vector/vectorLiteral.js +19 -0
- package/dist/utils/vector/vectorLiteral.js.map +1 -0
- package/package.json +84 -55
package/.env.example
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
DATABASE_URL=postgres://postgres:postgres@localhost:5432/memograffer
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
DATABASE_URL=postgres://postgres:postgres@localhost:5432/memograffer
|
|
2
|
+
ANTHROPIC_API_KEY=
|
|
3
|
+
GEMINI_API_KEY=
|
|
4
|
+
OPENAI_API_KEY=
|
|
5
|
+
REDIS_URL=redis://localhost:6379
|
package/README.md
CHANGED
|
@@ -1,182 +1,187 @@
|
|
|
1
|
-
# Mem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|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
1
|
+
# MemoGrafter
|
|
2
|
+
[](https://www.npmjs.com/package/memo-grafter)
|
|
3
|
+
|
|
4
|
+
Structured memory for TypeScript chatbots.
|
|
5
|
+
|
|
6
|
+
MemoGrafter helps chatbot applications remember conversations without stuffing every old message back into the prompt. It turns conversations into topic-based memory, retrieves the relevant parts later, and can copy useful memory from one chatbot or session into another.
|
|
7
|
+
|
|
8
|
+
It is a memory framework, not an autonomous agent runtime. It does not run tools, schedule tasks, or decide goals for an agent.
|
|
9
|
+
|
|
10
|
+
## What You Can Build
|
|
11
|
+
|
|
12
|
+
- Chatbots that remember user preferences across long conversations.
|
|
13
|
+
- Support or tutoring assistants that recall prior topics without replaying full history.
|
|
14
|
+
- Multi-chatbot demos where one bot can absorb selected memory from another.
|
|
15
|
+
- Prototypes for graph-based conversational memory, retrieval, and memory transfer.
|
|
16
|
+
- Server-side TypeScript apps that need reusable LLM memory primitives.
|
|
17
|
+
|
|
18
|
+
## How It Works
|
|
19
|
+
|
|
20
|
+
At a high level:
|
|
21
|
+
|
|
22
|
+
```text
|
|
23
|
+
chat messages
|
|
24
|
+
-> topic segments
|
|
25
|
+
-> memory nodes
|
|
26
|
+
-> graph links
|
|
27
|
+
-> relevant memory injection
|
|
28
|
+
-> optional memory grafting
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
MemoGrafter stores conversation turns, detects topic shifts, summarizes segments into memory nodes, links related nodes, and injects relevant memory into future LLM calls. Drift detection uses embedding distance, sharp message pivots, short-message dampening, and structural phrases like "by the way" or "going back to" to split conversations into useful topic segments. Memory grafting lets one chatbot or session copy selected memory from another.
|
|
32
|
+
|
|
33
|
+
## Install
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
npm install memo-grafter
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
MemoGrafter runs server-side on Node.js. The built-in storage implementation is `PostgresGraphStore`, which requires PostgreSQL with `pgvector`. Included provider adapters require their matching API keys.
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
DATABASE_URL=postgres://postgres:postgres@localhost:5432/memo_grafter
|
|
43
|
+
ANTHROPIC_API_KEY=sk-ant-...
|
|
44
|
+
GEMINI_API_KEY=...
|
|
45
|
+
OPENAI_API_KEY=sk-...
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Minimal Example
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import "dotenv/config";
|
|
52
|
+
|
|
53
|
+
import {
|
|
54
|
+
MemoGrafterAgent,
|
|
55
|
+
OpenAIEmbedAdapter,
|
|
56
|
+
OpenAILLMAdapter,
|
|
57
|
+
} from "memo-grafter";
|
|
58
|
+
|
|
59
|
+
const agent = new MemoGrafterAgent({
|
|
60
|
+
db: { connectionString: process.env.DATABASE_URL! },
|
|
61
|
+
llm: new OpenAILLMAdapter("gpt-4o"),
|
|
62
|
+
embedder: new OpenAIEmbedAdapter("text-embedding-3-small"),
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
await agent.initialize();
|
|
66
|
+
|
|
67
|
+
console.log(await agent.invoke("I am planning a Japan trip."));
|
|
68
|
+
console.log(await agent.invoke("I like quiet towns, bookstores, and local cafes."));
|
|
69
|
+
console.log(await agent.invoke("What do you remember about my travel preferences?"));
|
|
70
|
+
|
|
71
|
+
const recall = await agent.recall("travel preferences", {
|
|
72
|
+
limit: 5,
|
|
73
|
+
minSimilarity: 0.6,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
console.log(recall.facts);
|
|
77
|
+
console.log(recall.systemPrompt);
|
|
78
|
+
|
|
79
|
+
await agent.close();
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Adapters
|
|
83
|
+
|
|
84
|
+
MemoGrafter includes provider adapters such as `OpenAILLMAdapter`, `OpenAIEmbedAdapter`, `AnthropicLLMAdapter`, `GeminiLLMAdapter`, and `GeminiEmbedAdapter`. You can also bring any provider by implementing the public adapter interfaces:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
import {
|
|
88
|
+
type EmbedAdapter,
|
|
89
|
+
type LLMAdapter,
|
|
90
|
+
type Message,
|
|
91
|
+
} from "memo-grafter";
|
|
92
|
+
|
|
93
|
+
class MyLLMAdapter implements LLMAdapter {
|
|
94
|
+
async complete(messages: Message[], system?: string): Promise<string> {
|
|
95
|
+
// Call your model provider here.
|
|
96
|
+
return "Assistant response";
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
class MyEmbedAdapter implements EmbedAdapter {
|
|
101
|
+
async embed(text: string): Promise<number[]> {
|
|
102
|
+
// Return an embedding vector matching your storage schema.
|
|
103
|
+
return [];
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Storage
|
|
109
|
+
|
|
110
|
+
MemoGrafter uses a public `GraphStore` interface internally. The default implementation is `PostgresGraphStore`, backed by PostgreSQL and `pgvector`, and this is what `MemoGrafter` and `MemoGrafterAgent` construct from the `db.connectionString` config today.
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
import {
|
|
114
|
+
PostgresGraphStore,
|
|
115
|
+
type GraphStore,
|
|
116
|
+
} from "memo-grafter";
|
|
117
|
+
|
|
118
|
+
const store: GraphStore = new PostgresGraphStore(process.env.DATABASE_URL!);
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
The interface boundary keeps the Postgres implementation isolated and gives future storage backends a clear contract to implement.
|
|
122
|
+
|
|
123
|
+
## Targeted Recall
|
|
124
|
+
|
|
125
|
+
Use `agent.recall()` when you want structured memory back without asking the LLM to answer yet. It searches atomic memory nodes by meaning, filters stale memories, groups them with their parent topic summaries, and returns a prompt block you can inspect or pass to your own model call.
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
const result = await agent.recall("deployment config", {
|
|
129
|
+
limit: 8,
|
|
130
|
+
minSimilarity: 0.55,
|
|
131
|
+
tokenBudget: 1000,
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
console.log(result.facts);
|
|
135
|
+
console.log(result.nodes);
|
|
136
|
+
console.log(result.systemPrompt);
|
|
137
|
+
console.log(result.tokenCount);
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
`recall()` is side-effect free. It does not call `invoke()`, does not mutate chat history, and does not inject anything automatically.
|
|
141
|
+
|
|
142
|
+
## Memory Grafting
|
|
143
|
+
|
|
144
|
+
Memory grafting is the core idea behind the name: one chatbot can build memory, and another chatbot can absorb only the useful parts.
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
await writingBot.absorbFromAgent(travelBot, {
|
|
148
|
+
prompt: "Japan travel preferences",
|
|
149
|
+
limit: 3,
|
|
150
|
+
});
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Drift And Reentry
|
|
154
|
+
|
|
155
|
+
Use `driftSensitivity` for developer-friendly topic segmentation:
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
const agent = new MemoGrafterAgent({
|
|
159
|
+
db: { connectionString: process.env.DATABASE_URL! },
|
|
160
|
+
llm,
|
|
161
|
+
embedder,
|
|
162
|
+
drift: {
|
|
163
|
+
mode: "intent",
|
|
164
|
+
driftSensitivity: "medium",
|
|
165
|
+
minSegmentMessages: 3,
|
|
166
|
+
reentryDetection: true,
|
|
167
|
+
},
|
|
168
|
+
});
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Sensitivity presets are `"low"`, `"medium"`, and `"high"`. The older numeric `threshold` option is still accepted for compatibility, but `driftSensitivity` is preferred.
|
|
172
|
+
|
|
173
|
+
When a conversation returns to an earlier topic, MemoGrafter can create a `reentry` edge between the new topic node and the earlier related topic node. For example, a chat can move from database decisions to authentication, then back to database pooling; the later database segment is linked back to the original database topic instead of becoming an isolated duplicate.
|
|
174
|
+
|
|
175
|
+
## Learn More
|
|
176
|
+
|
|
177
|
+
Read [USER_GUIDE.md](https://github.com/mayhemking007/memo-grafter/blob/main/USER_GUIDE.md) for setup, configuration, queue mode, custom adapters, fleet APIs, examples, and troubleshooting.
|
|
178
|
+
|
|
179
|
+
This repository also includes a runnable demo:
|
|
180
|
+
|
|
181
|
+
```text
|
|
182
|
+
examples/chatbot-memory-demo
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## License
|
|
186
|
+
|
|
187
|
+
MIT
|