@amaster.ai/pi-memory-mem0 0.1.2-beta.55 → 0.1.2-beta.56
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 +13 -16
- package/dist/index.d.ts +6 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +82 -5
- package/dist/index.js.map +1 -1
- package/dist/prefetch.d.ts +19 -0
- package/dist/prefetch.d.ts.map +1 -0
- package/dist/prefetch.js +58 -0
- package/dist/prefetch.js.map +1 -0
- package/package.json +3 -3
- package/dist/tools.d.ts +0 -15
- package/dist/tools.d.ts.map +0 -1
- package/dist/tools.js +0 -115
- package/dist/tools.js.map +0 -1
package/README.md
CHANGED
|
@@ -2,13 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|

|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Passive semantic memory extension powered by [Mem0](https://mem0.ai), with Platform, embedded, and self-hosted modes.
|
|
6
6
|
|
|
7
7
|
## How It Works
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
After each conversation turn, the user + assistant messages are automatically sent to Mem0 for fact extraction and storage. When you send a prompt, a semantic search is prefetched in the background and relevant memories are recalled before the agent starts — **zero effort required**.
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
Safety boundaries:
|
|
12
|
+
|
|
13
|
+
- **Recall channel**: recalled memories are injected as a custom message delivered on the user channel — never into the system prompt. Each entry is wrapped as `[UNTRUSTED MEMORY DATA]` (JSON-quoted), and entries matching prompt-injection patterns are replaced with `[BLOCKED UNTRUSTED MEMORY: ...]`.
|
|
14
|
+
- **Project namespacing**: memories are scoped to `userId:project:<cwd-hash>` by default, so one project's memories are not visible in another. Set `userIdScope: "exact"` when one identity must be shared across projects.
|
|
15
|
+
- **Credential redaction**: private keys, bearer tokens, and `api_key=…`-style values are redacted before anything is stored.
|
|
16
|
+
- **Platform mode disclosure**: in `platform` mode the captured turns leave your machine and are processed by Mem0 Cloud. Use `embedded` or `self-hosted` mode to keep memory traffic off third-party infrastructure.
|
|
12
17
|
|
|
13
18
|
## Modes
|
|
14
19
|
|
|
@@ -217,14 +222,6 @@ The default Embedded configuration depends on `better-sqlite3` (native addon, tr
|
|
|
217
222
|
|
|
218
223
|
If `better-sqlite3` fails to load (for example, because of a Node ABI mismatch), the default `memory` vector store cannot start. An external vector store can still be used with history disabled or configured to a working provider.
|
|
219
224
|
|
|
220
|
-
## Tools
|
|
221
|
-
|
|
222
|
-
| Tool | Description |
|
|
223
|
-
|------|-------------|
|
|
224
|
-
| `mem0_search` | Semantic search over long-term memories |
|
|
225
|
-
| `mem0_profile` | List all stored memories |
|
|
226
|
-
| `mem0_save` | Store a fact verbatim (bypasses LLM extraction) |
|
|
227
|
-
|
|
228
225
|
## Commands
|
|
229
226
|
|
|
230
227
|
```
|
|
@@ -237,12 +234,12 @@ If `better-sqlite3` fails to load (for example, because of a Node ABI mismatch),
|
|
|
237
234
|
|
|
238
235
|
`pi-memory-mem0` and `pi-memory` run **independently in parallel** as separate extensions:
|
|
239
236
|
|
|
240
|
-
- `pi-memory`: Active memory — agent explicitly manages via tools, local `.md` files, hard char limits
|
|
241
|
-
- `pi-memory-mem0`:
|
|
237
|
+
- `pi-memory`: Active memory — the agent explicitly manages memories via tools, local `.md` files, hard char limits
|
|
238
|
+
- `pi-memory-mem0`: Passive memory — automatic extraction/storage and semantic recall, no capacity limits
|
|
242
239
|
|
|
243
|
-
They do not interfere with each other. `pi-memory-mem0`
|
|
244
|
-
|
|
245
|
-
|
|
240
|
+
They do not interfere with each other. `pi-memory-mem0` injects recalled text as a
|
|
241
|
+
custom user-channel message (never the system prompt); `pi-memory` injects its own
|
|
242
|
+
context separately.
|
|
246
243
|
|
|
247
244
|
## Dedup API
|
|
248
245
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* pi-memory-mem0 —
|
|
2
|
+
* pi-memory-mem0 — Passive semantic memory extension powered by Mem0.
|
|
3
3
|
*
|
|
4
4
|
* Modes:
|
|
5
5
|
* - **platform**: Uses Mem0 Cloud API (needs MEM0_API_KEY)
|
|
6
6
|
* - **embedded**: Runs Mem0 OSS in-process
|
|
7
7
|
* - **self-hosted**: Calls a remote Mem0 OSS REST server
|
|
8
8
|
*
|
|
9
|
+
* After each conversation turn, user + assistant messages are sent to Mem0
|
|
10
|
+
* for fact extraction and storage (credentials are redacted first). Recalled
|
|
11
|
+
* memories are injected as a custom message (delivered to the model on the
|
|
12
|
+
* user channel, never the system prompt) and wrapped as untrusted data.
|
|
13
|
+
*
|
|
9
14
|
* Configuration via settings.json key "pi-memory-mem0".
|
|
10
15
|
* Supports ${ENV_VAR:-fallback} in user and agent settings.
|
|
11
16
|
* Trusted project settings are loaded without environment interpolation.
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAGH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AA4BpE,MAAM,CAAC,OAAO,UAAU,aAAa,CAAC,EAAE,EAAE,YAAY,GAAG,IAAI,CAqL5D"}
|
package/dist/index.js
CHANGED
|
@@ -1,19 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* pi-memory-mem0 —
|
|
2
|
+
* pi-memory-mem0 — Passive semantic memory extension powered by Mem0.
|
|
3
3
|
*
|
|
4
4
|
* Modes:
|
|
5
5
|
* - **platform**: Uses Mem0 Cloud API (needs MEM0_API_KEY)
|
|
6
6
|
* - **embedded**: Runs Mem0 OSS in-process
|
|
7
7
|
* - **self-hosted**: Calls a remote Mem0 OSS REST server
|
|
8
8
|
*
|
|
9
|
+
* After each conversation turn, user + assistant messages are sent to Mem0
|
|
10
|
+
* for fact extraction and storage (credentials are redacted first). Recalled
|
|
11
|
+
* memories are injected as a custom message (delivered to the model on the
|
|
12
|
+
* user channel, never the system prompt) and wrapped as untrusted data.
|
|
13
|
+
*
|
|
9
14
|
* Configuration via settings.json key "pi-memory-mem0".
|
|
10
15
|
* Supports ${ENV_VAR:-fallback} in user and agent settings.
|
|
11
16
|
* Trusted project settings are loaded without environment interpolation.
|
|
12
17
|
*/
|
|
13
18
|
import { isProjectTrusted, loadPiSettings } from '@amaster.ai/pi-shared/settings';
|
|
14
|
-
import {
|
|
19
|
+
import { Prefetch } from './prefetch.js';
|
|
20
|
+
import { formatRecalledMemory, redactMemoryText, scopeMemoryUserId } from './privacy.js';
|
|
15
21
|
import { createMem0Provider, normalizeMem0Mode } from './provider.js';
|
|
16
|
-
import { createMem0Tools } from './tools.js';
|
|
17
22
|
const SETTINGS_KEY = 'pi-memory-mem0';
|
|
18
23
|
const STATUS_KEY = 'mem0';
|
|
19
24
|
function loadConfig(cwd, projectTrusted = false) {
|
|
@@ -40,10 +45,14 @@ function resolveUserId(configUserId, scope = 'project') {
|
|
|
40
45
|
}
|
|
41
46
|
export default function mem0Extension(pi) {
|
|
42
47
|
let provider;
|
|
48
|
+
let prefetch;
|
|
43
49
|
let userId = '';
|
|
44
50
|
let activeMode = '';
|
|
51
|
+
let lastUserText = '';
|
|
52
|
+
let pendingWrite = Promise.resolve();
|
|
45
53
|
pi.on('session_start', async (_event, ctx) => {
|
|
46
54
|
provider = undefined;
|
|
55
|
+
prefetch = undefined;
|
|
47
56
|
const config = loadConfig(ctx.cwd, isProjectTrusted(ctx));
|
|
48
57
|
try {
|
|
49
58
|
const mode = normalizeMem0Mode(config.mode);
|
|
@@ -77,6 +86,7 @@ export default function mem0Extension(pi) {
|
|
|
77
86
|
});
|
|
78
87
|
userId = scopeMemoryUserId(resolvedUserId, ctx.cwd, config.userIdScope);
|
|
79
88
|
activeMode = mode;
|
|
89
|
+
prefetch = new Prefetch(provider, userId, { topK: config.topK ?? 5 });
|
|
80
90
|
}
|
|
81
91
|
catch (err) {
|
|
82
92
|
ctx.ui.setStatus(STATUS_KEY, 'mem0: init failed');
|
|
@@ -84,10 +94,66 @@ export default function mem0Extension(pi) {
|
|
|
84
94
|
return;
|
|
85
95
|
}
|
|
86
96
|
ctx.ui.setStatus(STATUS_KEY, `mem0: ${activeMode}`);
|
|
87
|
-
|
|
88
|
-
|
|
97
|
+
});
|
|
98
|
+
pi.on('input', async (event) => {
|
|
99
|
+
if (!prefetch)
|
|
100
|
+
return;
|
|
101
|
+
const text = event.text ?? '';
|
|
102
|
+
if (text) {
|
|
103
|
+
prefetch.queue(redactMemoryText(text));
|
|
104
|
+
lastUserText = text;
|
|
89
105
|
}
|
|
90
106
|
});
|
|
107
|
+
pi.on('turn_end', async (event) => {
|
|
108
|
+
if (!provider || !lastUserText)
|
|
109
|
+
return;
|
|
110
|
+
const msg = event.message;
|
|
111
|
+
const text = extractText(msg);
|
|
112
|
+
if (!text || msg.role !== 'assistant')
|
|
113
|
+
return;
|
|
114
|
+
const userText = lastUserText;
|
|
115
|
+
lastUserText = '';
|
|
116
|
+
const activeProvider = provider;
|
|
117
|
+
const activeUserId = userId;
|
|
118
|
+
pendingWrite = pendingWrite
|
|
119
|
+
.catch(() => { })
|
|
120
|
+
.then(async () => {
|
|
121
|
+
const result = await activeProvider.add([
|
|
122
|
+
{ role: 'user', content: redactMemoryText(userText) },
|
|
123
|
+
{ role: 'assistant', content: redactMemoryText(text) },
|
|
124
|
+
], { userId: activeUserId });
|
|
125
|
+
// Extraction legitimately finds nothing in many turns, but a totally
|
|
126
|
+
// silent no-op makes a broken pipeline indistinguishable from a quiet
|
|
127
|
+
// one — gate a diagnostic behind DEBUG.
|
|
128
|
+
if (process.env.DEBUG?.includes('pi-memory-mem0') && !result?.results?.length) {
|
|
129
|
+
console.error('[pi-memory-mem0] turn capture stored no memories (extraction empty?)');
|
|
130
|
+
}
|
|
131
|
+
})
|
|
132
|
+
.catch((err) => {
|
|
133
|
+
console.error(`[pi-memory-mem0] failed to store turn: ${err instanceof Error ? err.message : String(err)}`);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
pi.on('before_agent_start', async () => {
|
|
137
|
+
if (!prefetch)
|
|
138
|
+
return;
|
|
139
|
+
const recalled = await prefetch.consume();
|
|
140
|
+
if (!recalled)
|
|
141
|
+
return;
|
|
142
|
+
return {
|
|
143
|
+
message: {
|
|
144
|
+
customType: 'mem0-recall',
|
|
145
|
+
content: recalled,
|
|
146
|
+
display: true,
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
});
|
|
150
|
+
pi.on('session_shutdown', async () => {
|
|
151
|
+
await pendingWrite;
|
|
152
|
+
provider = undefined;
|
|
153
|
+
prefetch = undefined;
|
|
154
|
+
lastUserText = '';
|
|
155
|
+
pendingWrite = Promise.resolve();
|
|
156
|
+
});
|
|
91
157
|
pi.registerCommand('mem0', {
|
|
92
158
|
description: 'Mem0 memory commands. Subcommands: status, search <query>, profile.',
|
|
93
159
|
handler: async (args, ctx) => {
|
|
@@ -142,4 +208,15 @@ export default function mem0Extension(pi) {
|
|
|
142
208
|
},
|
|
143
209
|
});
|
|
144
210
|
}
|
|
211
|
+
function extractText(msg) {
|
|
212
|
+
if (typeof msg.content === 'string')
|
|
213
|
+
return msg.content;
|
|
214
|
+
if (Array.isArray(msg.content)) {
|
|
215
|
+
return msg.content
|
|
216
|
+
.filter((c) => c.type === 'text' && c.text)
|
|
217
|
+
.map((c) => c.text)
|
|
218
|
+
.join('\n');
|
|
219
|
+
}
|
|
220
|
+
return '';
|
|
221
|
+
}
|
|
145
222
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;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I,CAAC;aAC1C,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAK,CAAC;aACnB,IAAI,CAAC,IAAI,CAAC,CAAC;IAChB,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Two-phase semantic prefetch — kicks off a background search on input,
|
|
3
|
+
* consumes the result on before_agent_start with a timeout.
|
|
4
|
+
*/
|
|
5
|
+
import type { Mem0Provider } from './provider.js';
|
|
6
|
+
export declare class Prefetch {
|
|
7
|
+
private readonly provider;
|
|
8
|
+
private readonly userId;
|
|
9
|
+
private readonly topK;
|
|
10
|
+
private pending;
|
|
11
|
+
constructor(provider: Mem0Provider, userId: string, opts: {
|
|
12
|
+
topK: number;
|
|
13
|
+
});
|
|
14
|
+
/** Phase 1: kick off a background search (called on input with user text). */
|
|
15
|
+
queue(query: string): void;
|
|
16
|
+
/** Phase 2: consume the result with a timeout (called on before_agent_start). */
|
|
17
|
+
consume(timeoutMs?: number): Promise<string>;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=prefetch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prefetch.d.ts","sourceRoot":"","sources":["../src/prefetch.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAQlD,qBAAa,QAAQ;IACnB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAe;IACxC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;IAC9B,OAAO,CAAC,OAAO,CAAsC;gBAEzC,QAAQ,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE;IAM1E,8EAA8E;IAC9E,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAQ1B,iFAAiF;IAC3E,OAAO,CAAC,SAAS,SAAO,GAAG,OAAO,CAAC,MAAM,CAAC;CA6BjD"}
|
package/dist/prefetch.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Two-phase semantic prefetch — kicks off a background search on input,
|
|
3
|
+
* consumes the result on before_agent_start with a timeout.
|
|
4
|
+
*/
|
|
5
|
+
import { truncateHead, truncateLine } from '@earendil-works/pi-coding-agent';
|
|
6
|
+
import { formatRecalledMemory } from './privacy.js';
|
|
7
|
+
/** Recalled entries land in per-turn model context — keep the block small. */
|
|
8
|
+
const MAX_ENTRY_CHARS = 1_000;
|
|
9
|
+
const MAX_BLOCK_BYTES = 8 * 1024;
|
|
10
|
+
const MAX_BLOCK_LINES = 50;
|
|
11
|
+
export class Prefetch {
|
|
12
|
+
provider;
|
|
13
|
+
userId;
|
|
14
|
+
topK;
|
|
15
|
+
pending = null;
|
|
16
|
+
constructor(provider, userId, opts) {
|
|
17
|
+
this.provider = provider;
|
|
18
|
+
this.userId = userId;
|
|
19
|
+
this.topK = opts.topK;
|
|
20
|
+
}
|
|
21
|
+
/** Phase 1: kick off a background search (called on input with user text). */
|
|
22
|
+
queue(query) {
|
|
23
|
+
if (!query.trim())
|
|
24
|
+
return;
|
|
25
|
+
const search = this.provider.search(query, { userId: this.userId, topK: this.topK });
|
|
26
|
+
// A replaced or never-consumed search must not surface as an unhandled rejection.
|
|
27
|
+
search.catch(() => { });
|
|
28
|
+
this.pending = search;
|
|
29
|
+
}
|
|
30
|
+
/** Phase 2: consume the result with a timeout (called on before_agent_start). */
|
|
31
|
+
async consume(timeoutMs = 3000) {
|
|
32
|
+
if (!this.pending)
|
|
33
|
+
return '';
|
|
34
|
+
const promise = this.pending;
|
|
35
|
+
this.pending = null;
|
|
36
|
+
try {
|
|
37
|
+
const memories = await Promise.race([
|
|
38
|
+
promise,
|
|
39
|
+
new Promise((resolve) => setTimeout(() => resolve([]), timeoutMs)),
|
|
40
|
+
]);
|
|
41
|
+
if (memories.length === 0)
|
|
42
|
+
return '';
|
|
43
|
+
const lines = memories
|
|
44
|
+
.filter((m) => m.memory?.trim())
|
|
45
|
+
.map((m) => `- ${formatRecalledMemory(truncateLine(m.memory.trim(), MAX_ENTRY_CHARS).text)}`);
|
|
46
|
+
if (lines.length === 0)
|
|
47
|
+
return '';
|
|
48
|
+
return truncateHead(`## Recalled Memories (Mem0)\n${lines.join('\n')}`, {
|
|
49
|
+
maxBytes: MAX_BLOCK_BYTES,
|
|
50
|
+
maxLines: MAX_BLOCK_LINES,
|
|
51
|
+
}).content;
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return '';
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=prefetch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prefetch.js","sourceRoot":"","sources":["../src/prefetch.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AAC7E,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAIpD,8EAA8E;AAC9E,MAAM,eAAe,GAAG,KAAK,CAAC;AAC9B,MAAM,eAAe,GAAG,CAAC,GAAG,IAAI,CAAC;AACjC,MAAM,eAAe,GAAG,EAAE,CAAC;AAE3B,MAAM,OAAO,QAAQ;IACF,QAAQ,CAAe;IACvB,MAAM,CAAS;IACf,IAAI,CAAS;IACtB,OAAO,GAAiC,IAAI,CAAC;IAErD,YAAY,QAAsB,EAAE,MAAc,EAAE,IAAsB;QACxE,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;IACxB,CAAC;IAED,8EAA8E;IAC9E,KAAK,C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AAO,EAAE,CAAC;YAClC,OAAO,YAAY,CAAC,gCAAgC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,EAAE;gBACtE,QAAQ,EAAE,eAAe;gBACzB,QAAQ,EAAE,eAAe;aAC1B,CAAC,CAAC,OAAO,CAAC;QACb,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,EAAE,CAAC;QACZ,CAAC;IACH,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amaster.ai/pi-memory-mem0",
|
|
3
|
-
"version": "0.1.2-beta.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.2-beta.56",
|
|
4
|
+
"description": "Passive Mem0 semantic memory for pi — automatic capture, semantic recall. Platform, embedded, or self-hosted.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|
|
7
7
|
"pi",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
56
|
"mem0ai": "3.0.7",
|
|
57
|
-
"@amaster.ai/pi-shared": "0.1.2-beta.
|
|
57
|
+
"@amaster.ai/pi-shared": "0.1.2-beta.56"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|
|
60
60
|
"@earendil-works/pi-coding-agent": ">=0.79.1",
|
package/dist/tools.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* LLM-callable tools for Mem0: search, profile (get all), and save (verbatim store).
|
|
3
|
-
*/
|
|
4
|
-
import { type AgentToolResult } from '@earendil-works/pi-coding-agent';
|
|
5
|
-
import type { Mem0Provider } from './provider.js';
|
|
6
|
-
export interface ToolDefinition {
|
|
7
|
-
name: string;
|
|
8
|
-
label: string;
|
|
9
|
-
description: string;
|
|
10
|
-
promptSnippet?: string;
|
|
11
|
-
parameters: unknown;
|
|
12
|
-
execute(toolCallId: string, params: Record<string, unknown>, signal?: AbortSignal, onUpdate?: unknown, ctx?: unknown): Promise<AgentToolResult<unknown>>;
|
|
13
|
-
}
|
|
14
|
-
export declare function createMem0Tools(provider: Mem0Provider, userId: string): ToolDefinition[];
|
|
15
|
-
//# sourceMappingURL=tools.d.ts.map
|
package/dist/tools.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../src/tools.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,KAAK,eAAe,EAA8B,MAAM,iCAAiC,CAAC;AAGnG,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAElD,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,MAAM,CAAC;IACpB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,UAAU,EAAE,OAAO,CAAC;IACpB,OAAO,CACL,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/B,MAAM,CAAC,EAAE,WAAW,EACpB,QAAQ,CAAC,EAAE,OAAO,EAClB,GAAG,CAAC,EAAE,OAAO,GACZ,OAAO,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,CAAC;CACtC;AAsBD,wBAAgB,eAAe,CAAC,QAAQ,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,GAAG,cAAc,EAAE,CAgGxF"}
|
package/dist/tools.js
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* LLM-callable tools for Mem0: search, profile (get all), and save (verbatim store).
|
|
3
|
-
*/
|
|
4
|
-
import { truncateHead, truncateLine } from '@earendil-works/pi-coding-agent';
|
|
5
|
-
import { Type } from 'typebox';
|
|
6
|
-
import { formatRecalledMemory, redactMemoryText } from './privacy.js';
|
|
7
|
-
function textResult(text) {
|
|
8
|
-
const result = truncateHead(text, { maxBytes: 47 * 1024, maxLines: 1_900 });
|
|
9
|
-
const output = result.truncated
|
|
10
|
-
? JSON.stringify({
|
|
11
|
-
truncated: true,
|
|
12
|
-
preview: truncateLine(text, 4_000).text,
|
|
13
|
-
})
|
|
14
|
-
: result.content;
|
|
15
|
-
return {
|
|
16
|
-
content: [{ type: 'text', text: output }],
|
|
17
|
-
details: undefined,
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
function rethrowIfCancelled(signal) {
|
|
21
|
-
if (signal?.aborted) {
|
|
22
|
-
throw signal.reason instanceof Error ? signal.reason : new Error('Mem0 request cancelled.');
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
export function createMem0Tools(provider, userId) {
|
|
26
|
-
const searchTool = {
|
|
27
|
-
name: 'mem0_search',
|
|
28
|
-
label: 'Mem0',
|
|
29
|
-
description: 'Search long-term memories by meaning. Returns relevant facts ranked by similarity.',
|
|
30
|
-
promptSnippet: 'Semantic search over long-term user memories.',
|
|
31
|
-
parameters: Type.Object({
|
|
32
|
-
query: Type.String({ description: 'What to search for.' }),
|
|
33
|
-
top_k: Type.Optional(Type.Number({ description: 'Max results (default: 10, max: 50).' })),
|
|
34
|
-
}),
|
|
35
|
-
async execute(_toolCallId, params, signal) {
|
|
36
|
-
const query = String(params.query ?? '');
|
|
37
|
-
const topK = Math.min(Number(params.top_k) || 10, 50);
|
|
38
|
-
if (!query)
|
|
39
|
-
return textResult(JSON.stringify({ error: 'Query cannot be empty.' }));
|
|
40
|
-
try {
|
|
41
|
-
const results = await provider.search(query, {
|
|
42
|
-
userId,
|
|
43
|
-
topK,
|
|
44
|
-
...(signal ? { signal } : {}),
|
|
45
|
-
});
|
|
46
|
-
if (results.length === 0) {
|
|
47
|
-
return textResult(JSON.stringify({ result: 'No relevant memories found.' }));
|
|
48
|
-
}
|
|
49
|
-
return textResult(JSON.stringify({
|
|
50
|
-
results: results.map((r) => ({
|
|
51
|
-
memory: formatRecalledMemory(r.memory),
|
|
52
|
-
score: r.score,
|
|
53
|
-
})),
|
|
54
|
-
count: results.length,
|
|
55
|
-
}));
|
|
56
|
-
}
|
|
57
|
-
catch {
|
|
58
|
-
rethrowIfCancelled(signal);
|
|
59
|
-
throw new Error('Mem0 search failed.');
|
|
60
|
-
}
|
|
61
|
-
},
|
|
62
|
-
};
|
|
63
|
-
const profileTool = {
|
|
64
|
-
name: 'mem0_profile',
|
|
65
|
-
label: 'Mem0',
|
|
66
|
-
description: 'Retrieve all stored long-term memories about the user.',
|
|
67
|
-
promptSnippet: 'Full dump of all stored user memories.',
|
|
68
|
-
parameters: Type.Object({}),
|
|
69
|
-
async execute(_toolCallId, _params, signal) {
|
|
70
|
-
try {
|
|
71
|
-
const memories = await provider.getAll({ userId, ...(signal ? { signal } : {}) });
|
|
72
|
-
if (memories.length === 0) {
|
|
73
|
-
return textResult(JSON.stringify({ result: 'No memories stored yet.' }));
|
|
74
|
-
}
|
|
75
|
-
const lines = memories
|
|
76
|
-
.map((m) => m.memory)
|
|
77
|
-
.filter(Boolean)
|
|
78
|
-
.map(formatRecalledMemory);
|
|
79
|
-
return textResult(JSON.stringify({ result: lines.join('\n'), count: lines.length }));
|
|
80
|
-
}
|
|
81
|
-
catch {
|
|
82
|
-
rethrowIfCancelled(signal);
|
|
83
|
-
throw new Error('Mem0 profile failed.');
|
|
84
|
-
}
|
|
85
|
-
},
|
|
86
|
-
};
|
|
87
|
-
const saveTool = {
|
|
88
|
-
name: 'mem0_save',
|
|
89
|
-
label: 'Mem0',
|
|
90
|
-
description: 'Store a durable fact about the user verbatim (no LLM extraction). Use for explicit preferences or corrections.',
|
|
91
|
-
promptSnippet: 'Save a fact to long-term memory verbatim.',
|
|
92
|
-
parameters: Type.Object({
|
|
93
|
-
fact: Type.String({ description: 'The fact to store.' }),
|
|
94
|
-
}),
|
|
95
|
-
async execute(_toolCallId, params, signal) {
|
|
96
|
-
const fact = String(params.fact ?? '').trim();
|
|
97
|
-
if (!fact)
|
|
98
|
-
return textResult(JSON.stringify({ error: 'Fact cannot be empty.' }));
|
|
99
|
-
try {
|
|
100
|
-
const result = await provider.add([{ role: 'user', content: redactMemoryText(fact) }], {
|
|
101
|
-
userId,
|
|
102
|
-
infer: false,
|
|
103
|
-
...(signal ? { signal } : {}),
|
|
104
|
-
});
|
|
105
|
-
return textResult(JSON.stringify(result ? { result: 'Fact stored.' } : { error: 'Failed to store.' }));
|
|
106
|
-
}
|
|
107
|
-
catch {
|
|
108
|
-
rethrowIfCancelled(signal);
|
|
109
|
-
throw new Error('Mem0 save failed.');
|
|
110
|
-
}
|
|
111
|
-
},
|
|
112
|
-
};
|
|
113
|
-
return [searchTool, profileTool, saveTool];
|
|
114
|
-
}
|
|
115
|
-
//# sourceMappingURL=tools.js.map
|
package/dist/tools.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"tools.js","sourceRoot":"","sources":["../src/tools.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAwB,YAAY,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AACnG,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAC/B,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAkBtE,SAAS,UAAU,CAAC,IAAY;IAC9B,MAAM,MAAM,GAAG,YAAY,CAAC,IAAI,EAAE,EAAE,QAAQ,EAAE,EAAE,GAAG,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC,CAAC;IAC5E,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS;QAC7B,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC;YACb,SAAS,EAAE,IAAI;YACf,OAAO,EAAE,YAAY,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,IAAI;SACxC,CAAC;QACJ,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC;IACnB,OAAO;QACL,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,M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