memo-grafter 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/.env.example +3 -0
- package/LICENSE +21 -0
- package/README.md +182 -0
- package/USER_GUIDE.md +702 -0
- package/dist/MemoGrafter.d.ts +29 -0
- package/dist/MemoGrafter.d.ts.map +1 -0
- package/dist/MemoGrafter.js +106 -0
- package/dist/MemoGrafter.js.map +1 -0
- package/dist/MemoGrafterAgent.d.ts +18 -0
- package/dist/MemoGrafterAgent.d.ts.map +1 -0
- package/dist/MemoGrafterAgent.js +55 -0
- package/dist/MemoGrafterAgent.js.map +1 -0
- package/dist/adapters/OpenAIAdapter.d.ts +14 -0
- package/dist/adapters/OpenAIAdapter.d.ts.map +1 -0
- package/dist/adapters/OpenAIAdapter.js +37 -0
- package/dist/adapters/OpenAIAdapter.js.map +1 -0
- package/dist/adapters/types.d.ts +2 -0
- package/dist/adapters/types.d.ts.map +1 -0
- package/dist/adapters/types.js +2 -0
- package/dist/adapters/types.js.map +1 -0
- package/dist/fleet/ConductorAgent.d.ts +15 -0
- package/dist/fleet/ConductorAgent.d.ts.map +1 -0
- package/dist/fleet/ConductorAgent.js +41 -0
- package/dist/fleet/ConductorAgent.js.map +1 -0
- package/dist/fleet/FleetStore.d.ts +12 -0
- package/dist/fleet/FleetStore.d.ts.map +1 -0
- package/dist/fleet/FleetStore.js +30 -0
- package/dist/fleet/FleetStore.js.map +1 -0
- package/dist/fleet/MemoGrafterFleet.d.ts +20 -0
- package/dist/fleet/MemoGrafterFleet.d.ts.map +1 -0
- package/dist/fleet/MemoGrafterFleet.js +49 -0
- package/dist/fleet/MemoGrafterFleet.js.map +1 -0
- package/dist/fleet/WorkerAgent.d.ts +30 -0
- package/dist/fleet/WorkerAgent.d.ts.map +1 -0
- package/dist/fleet/WorkerAgent.js +95 -0
- package/dist/fleet/WorkerAgent.js.map +1 -0
- package/dist/fleet/types.d.ts +38 -0
- package/dist/fleet/types.d.ts.map +1 -0
- package/dist/fleet/types.js +2 -0
- package/dist/fleet/types.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/pipeline/GrafterPipeline.d.ts +16 -0
- package/dist/pipeline/GrafterPipeline.d.ts.map +1 -0
- package/dist/pipeline/GrafterPipeline.js +58 -0
- package/dist/pipeline/GrafterPipeline.js.map +1 -0
- package/dist/pipeline/IngestPipeline.d.ts +19 -0
- package/dist/pipeline/IngestPipeline.d.ts.map +1 -0
- package/dist/pipeline/IngestPipeline.js +44 -0
- package/dist/pipeline/IngestPipeline.js.map +1 -0
- package/dist/pipeline/SegmentProcessor.d.ts +19 -0
- package/dist/pipeline/SegmentProcessor.d.ts.map +1 -0
- package/dist/pipeline/SegmentProcessor.js +92 -0
- package/dist/pipeline/SegmentProcessor.js.map +1 -0
- package/dist/pipeline/TopicDriftDetector.d.ts +22 -0
- package/dist/pipeline/TopicDriftDetector.d.ts.map +1 -0
- package/dist/pipeline/TopicDriftDetector.js +96 -0
- package/dist/pipeline/TopicDriftDetector.js.map +1 -0
- package/dist/queue/IngestQueue.d.ts +16 -0
- package/dist/queue/IngestQueue.d.ts.map +1 -0
- package/dist/queue/IngestQueue.js +103 -0
- package/dist/queue/IngestQueue.js.map +1 -0
- package/dist/store/GraphStore.d.ts +71 -0
- package/dist/store/GraphStore.d.ts.map +1 -0
- package/dist/store/GraphStore.js +577 -0
- package/dist/store/GraphStore.js.map +1 -0
- package/dist/types.d.ts +84 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/drift/cosineSimilarity.d.ts +2 -0
- package/dist/utils/drift/cosineSimilarity.d.ts.map +1 -0
- package/dist/utils/drift/cosineSimilarity.js +12 -0
- package/dist/utils/drift/cosineSimilarity.js.map +1 -0
- package/dist/utils/drift/vectorAvg.d.ts +2 -0
- package/dist/utils/drift/vectorAvg.d.ts.map +1 -0
- package/dist/utils/drift/vectorAvg.js +11 -0
- package/dist/utils/drift/vectorAvg.js.map +1 -0
- package/dist/utils/normalizeText.d.ts +2 -0
- package/dist/utils/normalizeText.d.ts.map +1 -0
- package/dist/utils/normalizeText.js +14 -0
- package/dist/utils/normalizeText.js.map +1 -0
- package/dist/utils/vectorLiteral.d.ts +3 -0
- package/dist/utils/vectorLiteral.d.ts.map +1 -0
- package/dist/utils/vectorLiteral.js +18 -0
- package/dist/utils/vectorLiteral.js.map +1 -0
- package/package.json +55 -0
package/.env.example
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 MemoGrafter contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# MemoGrafter
|
|
2
|
+
|
|
3
|
+
Experimental structured memory for TypeScript chatbots.
|
|
4
|
+
|
|
5
|
+
MemoGrafter turns chatbot conversations into topic segments, topic nodes, and graph edges, then injects relevant memory into future turns. Its core idea is memory grafting: copying useful conversational memory from one chatbot or session into another.
|
|
6
|
+
|
|
7
|
+
MemoGrafter is a chatbot memory framework. It is not an autonomous agent runtime.
|
|
8
|
+
|
|
9
|
+
## Why MemoGrafter?
|
|
10
|
+
|
|
11
|
+
Most chatbots either forget old context or keep stuffing long chat history back into the prompt. MemoGrafter explores a different shape:
|
|
12
|
+
|
|
13
|
+
- store conversations as structured memory
|
|
14
|
+
- detect topic shifts
|
|
15
|
+
- summarize topic segments into memory nodes
|
|
16
|
+
- connect related nodes in a graph
|
|
17
|
+
- inject only relevant memory into later calls
|
|
18
|
+
- graft selected memory into another chatbot/session
|
|
19
|
+
|
|
20
|
+
The project is early-stage and experimental, but it is useful for demos, prototypes, and exploring memory workflows beyond plain chat history.
|
|
21
|
+
|
|
22
|
+
## How It Works
|
|
23
|
+
|
|
24
|
+
```text
|
|
25
|
+
chat messages
|
|
26
|
+
-> topic segments
|
|
27
|
+
-> topic nodes
|
|
28
|
+
-> graph edges
|
|
29
|
+
-> memory injection
|
|
30
|
+
-> selective grafting into another chatbot
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
On each chatbot call, MemoGrafter can retrieve existing topic memory and pass it to the LLM as a system prompt. After the response, it ingests the updated conversation so future turns have better memory.
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npm install memo-grafter
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
For local development from this repository:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
git clone <repo-url> project-memoGrafter
|
|
45
|
+
cd project-memoGrafter
|
|
46
|
+
npm install
|
|
47
|
+
npm run build
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Then install it from a local app:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
npm install D:/cohort/projects/project-memoGrafter
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Environment
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
DATABASE_URL=postgres://postgres:postgres@localhost:5432/memo_grafter
|
|
60
|
+
OPENAI_API_KEY=sk-...
|
|
61
|
+
REDIS_URL=redis://localhost:6379
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
`DATABASE_URL` is required. PostgreSQL must have `pgvector` enabled.
|
|
65
|
+
|
|
66
|
+
`OPENAI_API_KEY` is only needed when using the included OpenAI adapters.
|
|
67
|
+
|
|
68
|
+
`REDIS_URL` is optional and only needed for queue mode.
|
|
69
|
+
|
|
70
|
+
## Minimal Usage
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
import "dotenv/config";
|
|
74
|
+
|
|
75
|
+
import {
|
|
76
|
+
MemoGrafterAgent,
|
|
77
|
+
OpenAIEmbedAdapter,
|
|
78
|
+
OpenAILLMAdapter,
|
|
79
|
+
} from "memo-grafter";
|
|
80
|
+
|
|
81
|
+
const agent = new MemoGrafterAgent({
|
|
82
|
+
db: {
|
|
83
|
+
connectionString: process.env.DATABASE_URL!,
|
|
84
|
+
},
|
|
85
|
+
llm: new OpenAILLMAdapter("gpt-4o"),
|
|
86
|
+
embedder: new OpenAIEmbedAdapter("text-embedding-3-small"),
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
await agent.initialize();
|
|
90
|
+
|
|
91
|
+
console.log(await agent.invoke("I am planning a Japan trip."));
|
|
92
|
+
console.log(await agent.invoke("I like quiet towns, bookstores, and local cafes."));
|
|
93
|
+
|
|
94
|
+
const nodes = await agent.getActiveNodes();
|
|
95
|
+
console.log(nodes.map((node) => ({ label: node.label, summary: node.summary })));
|
|
96
|
+
|
|
97
|
+
await agent.close();
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Run with:
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
npx tsx --env-file=.env src/index.ts
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Memory Grafting
|
|
107
|
+
|
|
108
|
+
Memory grafting copies selected memory from one chatbot/session into another.
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
const travelBot = new MemoGrafterAgent(config);
|
|
112
|
+
const writingBot = new MemoGrafterAgent(config);
|
|
113
|
+
|
|
114
|
+
await travelBot.initialize();
|
|
115
|
+
await writingBot.initialize();
|
|
116
|
+
|
|
117
|
+
await travelBot.invoke("I am planning a Japan trip.");
|
|
118
|
+
await travelBot.invoke("I like quiet towns, bookstores, and local cafes.");
|
|
119
|
+
await travelBot.invoke("My budget is around 2500 dollars.");
|
|
120
|
+
|
|
121
|
+
await writingBot.absorbFromAgent(travelBot, {
|
|
122
|
+
prompt: "Japan travel preferences",
|
|
123
|
+
minSimilarity: 0.6,
|
|
124
|
+
limit: 3,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
const intro = await writingBot.invoke(
|
|
128
|
+
"Suggest a reflective blog intro for my Japan trip."
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
console.log(intro);
|
|
132
|
+
|
|
133
|
+
await travelBot.close();
|
|
134
|
+
await writingBot.close();
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
You can also preview a graft before copying it:
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
const graft = await travelBot.graft();
|
|
141
|
+
|
|
142
|
+
console.log(graft.systemPrompt);
|
|
143
|
+
console.log(graft.nodes);
|
|
144
|
+
console.log(graft.tokenCount);
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Key Features
|
|
148
|
+
|
|
149
|
+
- Structured chatbot memory over PostgreSQL and pgvector.
|
|
150
|
+
- Topic drift detection for splitting conversations into segments.
|
|
151
|
+
- Topic nodes with summaries, embeddings, message ranges, and graph edges.
|
|
152
|
+
- Automatic memory injection during chatbot calls.
|
|
153
|
+
- Selective memory grafting by topic ID or semantic prompt.
|
|
154
|
+
- Optional BullMQ/Redis queue mode for background ingestion.
|
|
155
|
+
- OpenAI adapters included.
|
|
156
|
+
- Custom LLM and embedding adapters supported.
|
|
157
|
+
- Minimal fleet API for color-scoped worker chatbots and conductor grafting.
|
|
158
|
+
|
|
159
|
+
## Requirements
|
|
160
|
+
|
|
161
|
+
- Node.js 18 or newer.
|
|
162
|
+
- Server-side Node.js runtime.
|
|
163
|
+
- PostgreSQL with `pgvector`.
|
|
164
|
+
- An LLM adapter and embedding adapter.
|
|
165
|
+
- OpenAI API key if using `OpenAILLMAdapter` or `OpenAIEmbedAdapter`.
|
|
166
|
+
- Redis only if using queue mode.
|
|
167
|
+
|
|
168
|
+
MemoGrafter does not run in browser code.
|
|
169
|
+
|
|
170
|
+
## Learn More
|
|
171
|
+
|
|
172
|
+
Read the full [USER_GUIDE.md](./USER_GUIDE.md) for setup, concepts, configuration, queue mode, custom adapters, fleet usage, examples, and troubleshooting.
|
|
173
|
+
|
|
174
|
+
This repository also includes a runnable demo:
|
|
175
|
+
|
|
176
|
+
```text
|
|
177
|
+
examples/chatbot-memory-demo
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## License
|
|
181
|
+
|
|
182
|
+
MIT
|