@amaster.ai/pi-memory-mem0 0.1.2-beta.23
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/LICENSE +201 -0
- package/README.md +171 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +165 -0
- package/dist/index.js.map +1 -0
- package/dist/prefetch.d.ts +19 -0
- package/dist/prefetch.d.ts.map +1 -0
- package/dist/prefetch.js +42 -0
- package/dist/prefetch.js.map +1 -0
- package/dist/provider.d.ts +33 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +195 -0
- package/dist/provider.js.map +1 -0
- package/dist/sync.d.ts +16 -0
- package/dist/sync.d.ts.map +1 -0
- package/dist/sync.js +40 -0
- package/dist/sync.js.map +1 -0
- package/dist/tools.d.ts +15 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +90 -0
- package/dist/tools.js.map +1 -0
- package/dist/types.d.ts +53 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/package.json +75 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# @amaster.ai/pi-memory-mem0
|
|
2
|
+
|
|
3
|
+
Mem0 被动语义记忆扩展 — 支持 Platform (云端) 和 Open-Source (本地 SQLite) 双模式。
|
|
4
|
+
|
|
5
|
+
## 工作原理
|
|
6
|
+
|
|
7
|
+
每轮对话结束后,自动将 user + assistant 消息对发送给 Mem0 进行事实提取和向量化存储。下一轮开始前,自动用语义搜索召回相关记忆并注入 system prompt。
|
|
8
|
+
|
|
9
|
+
**你不需要做任何事** — 记忆的存储和召回完全自动。
|
|
10
|
+
|
|
11
|
+
## 两种模式
|
|
12
|
+
|
|
13
|
+
| 模式 | 存储位置 | 依赖 | 适用场景 |
|
|
14
|
+
|------|---------|------|---------|
|
|
15
|
+
| `platform` | Mem0 Cloud | MEM0_API_KEY | 快速上手,多设备同步 |
|
|
16
|
+
| `open-source` | 本地 SQLite (`~/.mem0/vector_store.db`) | LLM + Embedding API | 数据私有,零外部服务 |
|
|
17
|
+
|
|
18
|
+
## 快速开始
|
|
19
|
+
|
|
20
|
+
### Platform 模式
|
|
21
|
+
|
|
22
|
+
```json
|
|
23
|
+
{
|
|
24
|
+
"pi-memory-mem0": {
|
|
25
|
+
"mode": "platform",
|
|
26
|
+
"apiKey": "${MEM0_API_KEY}",
|
|
27
|
+
"userId": "${USER}"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Open-Source 模式 (推荐)
|
|
33
|
+
|
|
34
|
+
默认复用 pi 已配置的 model provider API key,**不需要额外设置环境变量**。
|
|
35
|
+
|
|
36
|
+
```json
|
|
37
|
+
{
|
|
38
|
+
"pi-memory-mem0": {
|
|
39
|
+
"mode": "open-source",
|
|
40
|
+
"userId": "${USER}"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
默认使用 OpenAI `text-embedding-3-small` (embedding) + `gpt-4.1-nano` (提取)。API key 自动从 pi model registry 获取。
|
|
46
|
+
|
|
47
|
+
### 自定义 LLM / Embedding
|
|
48
|
+
|
|
49
|
+
```json
|
|
50
|
+
{
|
|
51
|
+
"pi-memory-mem0": {
|
|
52
|
+
"mode": "open-source",
|
|
53
|
+
"userId": "${USER}",
|
|
54
|
+
"oss": {
|
|
55
|
+
"llm": {
|
|
56
|
+
"provider": "deepseek",
|
|
57
|
+
"config": { "model": "deepseek-chat" }
|
|
58
|
+
},
|
|
59
|
+
"embedder": {
|
|
60
|
+
"provider": "openai",
|
|
61
|
+
"config": { "model": "text-embedding-3-small" }
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### 纯本地 (Ollama)
|
|
69
|
+
|
|
70
|
+
```json
|
|
71
|
+
{
|
|
72
|
+
"pi-memory-mem0": {
|
|
73
|
+
"mode": "open-source",
|
|
74
|
+
"userId": "${USER}",
|
|
75
|
+
"oss": {
|
|
76
|
+
"llm": {
|
|
77
|
+
"provider": "ollama",
|
|
78
|
+
"config": { "model": "llama3", "url": "http://localhost:11434" }
|
|
79
|
+
},
|
|
80
|
+
"embedder": {
|
|
81
|
+
"provider": "ollama",
|
|
82
|
+
"config": { "model": "nomic-embed-text", "url": "http://localhost:11434" }
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
"useRegistryKeys": false
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## 配置参考
|
|
91
|
+
|
|
92
|
+
| 字段 | 类型 | 默认值 | 说明 |
|
|
93
|
+
|------|------|--------|------|
|
|
94
|
+
| `mode` | `"platform"` \| `"open-source"` | `"platform"` | 运行模式 |
|
|
95
|
+
| `apiKey` | string | — | Platform 模式必须。支持 `${MEM0_API_KEY}` |
|
|
96
|
+
| `baseUrl` | string | `https://api.mem0.ai` | Platform 自定义端点 |
|
|
97
|
+
| `userId` | string | `$USER` 或 `"default-user"` | 记忆作用域 |
|
|
98
|
+
| `topK` | number | `5` | 每轮最多召回条数 |
|
|
99
|
+
| `useRegistryKeys` | boolean | `true` | OSS 模式是否从 pi registry 取 API key |
|
|
100
|
+
| `oss.llm` | object | OpenAI gpt-4.1-nano | OSS 提取模型 |
|
|
101
|
+
| `oss.embedder` | object | OpenAI text-embedding-3-small | OSS embedding 模型 |
|
|
102
|
+
| `oss.vectorStore` | object | SQLite (默认) | 自定义向量存储 |
|
|
103
|
+
| `oss.disableHistory` | boolean | `false` | 禁用操作历史记录 |
|
|
104
|
+
|
|
105
|
+
## 数据存储位置
|
|
106
|
+
|
|
107
|
+
| 模式 | 位置 |
|
|
108
|
+
|------|------|
|
|
109
|
+
| Platform | Mem0 Cloud (api.mem0.ai) |
|
|
110
|
+
| Open-Source | `~/.pi/agent/memories/mem0.db` (SQLite,和 pi-memory 的 MEMORY.md/USER.md 同目录) |
|
|
111
|
+
|
|
112
|
+
可通过 `oss.vectorStore.config.dbPath` 自定义路径。`PI_AGENT_HOME` 环境变量会改变默认目录。
|
|
113
|
+
|
|
114
|
+
## API Key 解析顺序 (OSS 模式)
|
|
115
|
+
|
|
116
|
+
LLM 和 Embedder 的 API key **各自独立解析**,按以下顺序:
|
|
117
|
+
|
|
118
|
+
1. settings.json 的 `oss.llm.config.apiKey` / `oss.embedder.config.apiKey` (显式配置)
|
|
119
|
+
2. pi model registry 的对应 provider key (`useRegistryKeys: true` 时)
|
|
120
|
+
3. 环境变量 (`OPENAI_API_KEY`, `DEEPSEEK_API_KEY` 等,mem0 SDK 内部读取)
|
|
121
|
+
|
|
122
|
+
例如配置 `"llm": { "provider": "deepseek" }` + `"embedder": { "provider": "openai" }`,会分别从 pi registry 拿 deepseek 的 key 给 LLM,拿 openai 的 key 给 embedder。
|
|
123
|
+
|
|
124
|
+
### 通过代理 API 调用 (如 amaster)
|
|
125
|
+
|
|
126
|
+
如果你的 embedding/LLM 走统一代理(如 amaster credits),可以配 `baseUrl` 转发:
|
|
127
|
+
|
|
128
|
+
```json
|
|
129
|
+
{
|
|
130
|
+
"pi-memory-mem0": {
|
|
131
|
+
"mode": "open-source",
|
|
132
|
+
"oss": {
|
|
133
|
+
"llm": {
|
|
134
|
+
"provider": "openai",
|
|
135
|
+
"config": { "model": "deepseek-v4", "baseUrl": "https://credits.amaster.ai/v1" }
|
|
136
|
+
},
|
|
137
|
+
"embedder": {
|
|
138
|
+
"provider": "openai",
|
|
139
|
+
"config": { "model": "text-embedding-3-small", "baseUrl": "https://credits.amaster.ai/v1" }
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
这样 key 从 pi registry 的 `openai` provider 解析,但实际请求发到 amaster 的端点。
|
|
147
|
+
|
|
148
|
+
## 提供的工具
|
|
149
|
+
|
|
150
|
+
| 工具 | 说明 |
|
|
151
|
+
|------|------|
|
|
152
|
+
| `mem0_search` | 语义搜索长期记忆 |
|
|
153
|
+
| `mem0_profile` | 列出用户所有记忆 |
|
|
154
|
+
| `mem0_save` | 手动存储一条事实(不经过 LLM 提取) |
|
|
155
|
+
|
|
156
|
+
## 命令
|
|
157
|
+
|
|
158
|
+
```
|
|
159
|
+
/mem0 status # 查看状态
|
|
160
|
+
/mem0 search <query> # 语义搜索
|
|
161
|
+
/mem0 profile # 列出所有记忆
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## 与 pi-memory 的关系
|
|
165
|
+
|
|
166
|
+
`pi-memory-mem0` 和 `pi-memory` 是**独立并行运行**的两个扩展:
|
|
167
|
+
|
|
168
|
+
- `pi-memory`:主动记忆,agent 通过工具显式管理,本地 `.md` 文件,有 char limit
|
|
169
|
+
- `pi-memory-mem0`:被动记忆,自动提取存储,语义检索,无容量限制
|
|
170
|
+
|
|
171
|
+
两者互不干扰,各自注入 system prompt。
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pi-memory-mem0 — Passive semantic memory extension powered by Mem0.
|
|
3
|
+
*
|
|
4
|
+
* Two modes:
|
|
5
|
+
* - **platform**: Uses Mem0 Cloud API (needs MEM0_API_KEY)
|
|
6
|
+
* - **open-source**: Runs locally with SQLite vector store (needs OPENAI_API_KEY or Ollama)
|
|
7
|
+
*
|
|
8
|
+
* Configuration via settings.json key "pi-memory-mem0".
|
|
9
|
+
* Supports ${ENV_VAR:-fallback} in all string values.
|
|
10
|
+
*/
|
|
11
|
+
import type { ExtensionAPI } from '@earendil-works/pi-coding-agent';
|
|
12
|
+
export default function mem0Extension(pi: ExtensionAPI): void;
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AA4BpE,MAAM,CAAC,OAAO,UAAU,aAAa,CAAC,EAAE,EAAE,YAAY,GAAG,IAAI,CAkI5D"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pi-memory-mem0 — Passive semantic memory extension powered by Mem0.
|
|
3
|
+
*
|
|
4
|
+
* Two modes:
|
|
5
|
+
* - **platform**: Uses Mem0 Cloud API (needs MEM0_API_KEY)
|
|
6
|
+
* - **open-source**: Runs locally with SQLite vector store (needs OPENAI_API_KEY or Ollama)
|
|
7
|
+
*
|
|
8
|
+
* Configuration via settings.json key "pi-memory-mem0".
|
|
9
|
+
* Supports ${ENV_VAR:-fallback} in all string values.
|
|
10
|
+
*/
|
|
11
|
+
import { loadPiSettings, resolveAgentDir } from '@amaster.ai/pi-shared/settings';
|
|
12
|
+
import { Prefetch } from './prefetch.js';
|
|
13
|
+
import { createMem0Provider } from './provider.js';
|
|
14
|
+
import { TurnSync } from './sync.js';
|
|
15
|
+
import { createMem0Tools } from './tools.js';
|
|
16
|
+
const SETTINGS_KEY = 'pi-memory-mem0';
|
|
17
|
+
const STATUS_KEY = 'mem0';
|
|
18
|
+
function loadConfig(cwd) {
|
|
19
|
+
try {
|
|
20
|
+
return loadPiSettings(SETTINGS_KEY, {
|
|
21
|
+
cwd,
|
|
22
|
+
agentDir: resolveAgentDir(),
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return {};
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function resolveUserId(configUserId) {
|
|
30
|
+
if (configUserId?.trim())
|
|
31
|
+
return configUserId.trim();
|
|
32
|
+
if (process.env.USER)
|
|
33
|
+
return process.env.USER;
|
|
34
|
+
if (process.env.USERNAME)
|
|
35
|
+
return process.env.USERNAME;
|
|
36
|
+
return 'default-user';
|
|
37
|
+
}
|
|
38
|
+
export default function mem0Extension(pi) {
|
|
39
|
+
let provider;
|
|
40
|
+
let sync;
|
|
41
|
+
let prefetch;
|
|
42
|
+
pi.on('session_start', async (_event, ctx) => {
|
|
43
|
+
const config = loadConfig(ctx.cwd);
|
|
44
|
+
const mode = config.mode ?? 'platform';
|
|
45
|
+
// Platform mode requires apiKey; OSS mode requires an LLM provider (env key)
|
|
46
|
+
if (mode === 'platform' && !config.apiKey?.trim()) {
|
|
47
|
+
ctx.ui.setStatus(STATUS_KEY, 'mem0: disabled (no API key)');
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
provider = await createMem0Provider({
|
|
52
|
+
config,
|
|
53
|
+
resolveKey: async (providerName) => {
|
|
54
|
+
const registry = ctx.modelRegistry;
|
|
55
|
+
if (registry.getApiKeyForProvider) {
|
|
56
|
+
return registry.getApiKeyForProvider(providerName);
|
|
57
|
+
}
|
|
58
|
+
return undefined;
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
ctx.ui.setStatus(STATUS_KEY, 'mem0: init failed');
|
|
64
|
+
ctx.ui.notify(`Mem0 init failed: ${err instanceof Error ? err.message : String(err)}`, 'error');
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const userId = resolveUserId(config.userId);
|
|
68
|
+
sync = new TurnSync(provider, userId);
|
|
69
|
+
prefetch = new Prefetch(provider, userId, {
|
|
70
|
+
topK: config.topK ?? 5,
|
|
71
|
+
});
|
|
72
|
+
ctx.ui.setStatus(STATUS_KEY, `mem0: ${mode}`);
|
|
73
|
+
for (const tool of createMem0Tools(provider, userId)) {
|
|
74
|
+
pi.registerTool(tool);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
pi.on('turn_end', async (event) => {
|
|
78
|
+
if (!sync || !prefetch)
|
|
79
|
+
return;
|
|
80
|
+
const msg = event.message;
|
|
81
|
+
const text = extractText(msg);
|
|
82
|
+
if (!text)
|
|
83
|
+
return;
|
|
84
|
+
if (msg.role === 'user') {
|
|
85
|
+
prefetch.queue(text);
|
|
86
|
+
}
|
|
87
|
+
// Buffer messages for paired sync
|
|
88
|
+
sync.onMessage(msg.role ?? '', text);
|
|
89
|
+
});
|
|
90
|
+
pi.on('before_agent_start', async (event) => {
|
|
91
|
+
if (!prefetch)
|
|
92
|
+
return;
|
|
93
|
+
const recalled = await prefetch.consume();
|
|
94
|
+
if (!recalled)
|
|
95
|
+
return;
|
|
96
|
+
return {
|
|
97
|
+
systemPrompt: event.systemPrompt ? `${event.systemPrompt}\n\n${recalled}` : recalled,
|
|
98
|
+
};
|
|
99
|
+
});
|
|
100
|
+
pi.on('session_shutdown', async () => {
|
|
101
|
+
provider = undefined;
|
|
102
|
+
sync = undefined;
|
|
103
|
+
prefetch = undefined;
|
|
104
|
+
});
|
|
105
|
+
pi.registerCommand('mem0', {
|
|
106
|
+
description: 'Mem0 memory commands. Subcommands: status, search <query>, profile.',
|
|
107
|
+
handler: async (args, ctx) => {
|
|
108
|
+
if (!provider) {
|
|
109
|
+
ctx.ui.notify('Mem0 is not active.', 'warning');
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const config = loadConfig(ctx.cwd);
|
|
113
|
+
const userId = resolveUserId(config.userId);
|
|
114
|
+
const parts = args.trim().split(/\s+/).filter(Boolean);
|
|
115
|
+
const subcommand = parts[0]?.toLowerCase() ?? 'status';
|
|
116
|
+
const rest = parts.slice(1).join(' ').trim();
|
|
117
|
+
switch (subcommand) {
|
|
118
|
+
case 'status': {
|
|
119
|
+
ctx.ui.notify(`Mem0: active (mode: ${config.mode ?? 'platform'})`, 'info');
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
case 'search': {
|
|
123
|
+
if (!rest) {
|
|
124
|
+
ctx.ui.notify('Usage: /mem0 search <query>', 'warning');
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
const results = await provider.search(rest, { userId, topK: 10 });
|
|
128
|
+
if (results.length === 0) {
|
|
129
|
+
ctx.ui.notify('No relevant memories found.', 'info');
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
const lines = results.map((r, i) => `${i + 1}. ${r.memory}`);
|
|
133
|
+
ctx.ui.notify(`Mem0 search results:\n${lines.join('\n')}`, 'info');
|
|
134
|
+
}
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
case 'profile': {
|
|
138
|
+
const all = await provider.getAll({ userId });
|
|
139
|
+
if (all.length === 0) {
|
|
140
|
+
ctx.ui.notify('No memories stored yet.', 'info');
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
const lines = all.map((m, i) => `${i + 1}. ${m.memory}`);
|
|
144
|
+
ctx.ui.notify(`Mem0 memories (${all.length}):\n${lines.join('\n')}`, 'info');
|
|
145
|
+
}
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
default:
|
|
149
|
+
ctx.ui.notify('Unknown subcommand. Available: status, search, profile.', 'warning');
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
function extractText(msg) {
|
|
155
|
+
if (typeof msg.content === 'string')
|
|
156
|
+
return msg.content;
|
|
157
|
+
if (Array.isArray(msg.content)) {
|
|
158
|
+
return msg.content
|
|
159
|
+
.filter((c) => c.type === 'text' && c.text)
|
|
160
|
+
.map((c) => c.text)
|
|
161
|
+
.join('\n');
|
|
162
|
+
}
|
|
163
|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CAAC,EAAE,CAAC,MAAM,CAAC,yDAAyD,EAAE,SAAS,CAAC,CAAC;YACxF,CAAC;QACH,CAAC;KACF,CAAC,CAAC;AACL,CAAC;AAED,SAAS,WAAW,CAAC,GAA0B;IAC7C,IAAI,OAAO,GAAG,CAAC,OAAO,KAAK,QAAQ;QAAE,OAAO,GAAG,CAAC,OAAO,CAAC;IACxD,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC;QAC/B,OAAQ,GAAG,CAAC,OAAkD;aAC3D,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,MAAM,IAAI,CAAC,CAAC,IAAI,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 turn_end,
|
|
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 turn_end with user message). */
|
|
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;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAGlD,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,oFAAoF;IACpF,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAK1B,iFAAiF;IAC3E,OAAO,CAAC,SAAS,SAAO,GAAG,OAAO,CAAC,MAAM,CAAC;CAqBjD"}
|
package/dist/prefetch.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Two-phase semantic prefetch — kicks off a background search on turn_end,
|
|
3
|
+
* consumes the result on before_agent_start with a timeout.
|
|
4
|
+
*/
|
|
5
|
+
export class Prefetch {
|
|
6
|
+
provider;
|
|
7
|
+
userId;
|
|
8
|
+
topK;
|
|
9
|
+
pending = null;
|
|
10
|
+
constructor(provider, userId, opts) {
|
|
11
|
+
this.provider = provider;
|
|
12
|
+
this.userId = userId;
|
|
13
|
+
this.topK = opts.topK;
|
|
14
|
+
}
|
|
15
|
+
/** Phase 1: kick off a background search (called on turn_end with user message). */
|
|
16
|
+
queue(query) {
|
|
17
|
+
if (!query.trim())
|
|
18
|
+
return;
|
|
19
|
+
this.pending = this.provider.search(query, { userId: this.userId, topK: this.topK });
|
|
20
|
+
}
|
|
21
|
+
/** Phase 2: consume the result with a timeout (called on before_agent_start). */
|
|
22
|
+
async consume(timeoutMs = 3000) {
|
|
23
|
+
if (!this.pending)
|
|
24
|
+
return '';
|
|
25
|
+
const promise = this.pending;
|
|
26
|
+
this.pending = null;
|
|
27
|
+
try {
|
|
28
|
+
const memories = await Promise.race([
|
|
29
|
+
promise,
|
|
30
|
+
new Promise((resolve) => setTimeout(() => resolve([]), timeoutMs)),
|
|
31
|
+
]);
|
|
32
|
+
if (memories.length === 0)
|
|
33
|
+
return '';
|
|
34
|
+
const lines = memories.filter((m) => m.memory?.trim()).map((m) => `- ${m.memory.trim()}`);
|
|
35
|
+
return lines.length > 0 ? `## Recalled Memories (Mem0)\n${lines.join('\n')}` : '';
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return '';
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=prefetch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prefetch.js","sourceRoot":"","sources":["../src/prefetch.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,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,oFAAoF;IACpF,KAAK,CAAC,KAAa;QACjB,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE;YAAE,OAAO;QAC1B,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;IACvF,CAAC;IAED,iFAAiF;IACjF,KAAK,CAAC,OAAO,CAAC,SAAS,GAAG,IAAI;QAC5B,IAAI,CAAC,IAAI,CAAC,OAAO;YAAE,OAAO,EAAE,CAAC;QAE7B,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC;QAC7B,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QAEpB,IAAI,CAAC;YACH,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC;gBAClC,OAAO;gBACP,IAAI,OAAO,CAAe,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE,SAAS,CAAC,CAAC;aACjF,CAAC,CAAC;YAEH,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC;gBAAE,OAAO,EAAE,CAAC;YAErC,MAAM,KAAK,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;YAE1F,OAAO,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,gCAAgC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACpF,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,EAAE,CAAC;QACZ,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mem0 provider abstraction — supports both Platform (cloud) and OSS (local SQLite) modes.
|
|
3
|
+
*
|
|
4
|
+
* Uses the `mem0ai` npm SDK which handles:
|
|
5
|
+
* - Platform mode: REST API calls to api.mem0.ai
|
|
6
|
+
* - OSS mode: local SQLite vector store + LLM extraction via configured provider
|
|
7
|
+
*/
|
|
8
|
+
import type { AddResult, Mem0ExtensionConfig, MemoryItem } from './types.js';
|
|
9
|
+
export interface Mem0Provider {
|
|
10
|
+
add(messages: Array<{
|
|
11
|
+
role: string;
|
|
12
|
+
content: string;
|
|
13
|
+
}>, opts: {
|
|
14
|
+
userId: string;
|
|
15
|
+
infer?: boolean;
|
|
16
|
+
}): Promise<AddResult | null>;
|
|
17
|
+
search(query: string, opts: {
|
|
18
|
+
userId: string;
|
|
19
|
+
topK?: number;
|
|
20
|
+
}): Promise<MemoryItem[]>;
|
|
21
|
+
getAll(opts: {
|
|
22
|
+
userId: string;
|
|
23
|
+
}): Promise<MemoryItem[]>;
|
|
24
|
+
}
|
|
25
|
+
/** Optional key resolver — pulls API keys from pi's model registry. */
|
|
26
|
+
export type KeyResolver = (provider: string) => Promise<string | undefined>;
|
|
27
|
+
export interface CreateProviderOptions {
|
|
28
|
+
config: Mem0ExtensionConfig;
|
|
29
|
+
/** Resolve API key from pi model registry by provider name. */
|
|
30
|
+
resolveKey?: KeyResolver;
|
|
31
|
+
}
|
|
32
|
+
export declare function createMem0Provider(opts: CreateProviderOptions): Promise<Mem0Provider>;
|
|
33
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,KAAK,EAAE,SAAS,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAM7E,MAAM,WAAW,YAAY;IAC3B,GAAG,CACD,QAAQ,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC,EAClD,IAAI,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE,GACxC,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC,CAAC;IAE7B,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC;IAEtF,MAAM,CAAC,IAAI,EAAE;QAAE,MAAM,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC;CACzD;AA8FD,uEAAuE;AACvE,MAAM,MAAM,WAAW,GAAG,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;AA8G5E,MAAM,WAAW,qBAAqB;IACpC,MAAM,EAAE,mBAAmB,CAAC;IAC5B,+DAA+D;IAC/D,UAAU,CAAC,EAAE,WAAW,CAAC;CAC1B;AAED,wBAAsB,kBAAkB,CAAC,IAAI,EAAE,qBAAqB,GAAG,OAAO,CAAC,YAAY,CAAC,CAe3F"}
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mem0 provider abstraction — supports both Platform (cloud) and OSS (local SQLite) modes.
|
|
3
|
+
*
|
|
4
|
+
* Uses the `mem0ai` npm SDK which handles:
|
|
5
|
+
* - Platform mode: REST API calls to api.mem0.ai
|
|
6
|
+
* - OSS mode: local SQLite vector store + LLM extraction via configured provider
|
|
7
|
+
*/
|
|
8
|
+
import { join } from 'node:path';
|
|
9
|
+
import { resolveAgentDir } from '@amaster.ai/pi-shared/settings';
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
// Normalizers
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
function normalizeMemoryItem(raw) {
|
|
14
|
+
return {
|
|
15
|
+
id: String(raw.id ?? raw.memory_id ?? ''),
|
|
16
|
+
memory: String(raw.memory ?? raw.text ?? raw.content ?? ''),
|
|
17
|
+
score: typeof raw.score === 'number' ? raw.score : undefined,
|
|
18
|
+
user_id: raw.user_id,
|
|
19
|
+
created_at: (raw.created_at ?? raw.createdAt),
|
|
20
|
+
updated_at: (raw.updated_at ?? raw.updatedAt),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function normalizeResults(raw) {
|
|
24
|
+
if (Array.isArray(raw))
|
|
25
|
+
return raw.map((item) => normalizeMemoryItem(item));
|
|
26
|
+
if (raw &&
|
|
27
|
+
typeof raw === 'object' &&
|
|
28
|
+
'results' in raw &&
|
|
29
|
+
Array.isArray(raw.results)) {
|
|
30
|
+
return raw.results.map((item) => normalizeMemoryItem(item));
|
|
31
|
+
}
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
// Platform Provider
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
class PlatformProvider {
|
|
38
|
+
apiKey;
|
|
39
|
+
baseUrl;
|
|
40
|
+
client;
|
|
41
|
+
initPromise = null;
|
|
42
|
+
constructor(apiKey, baseUrl) {
|
|
43
|
+
this.apiKey = apiKey;
|
|
44
|
+
this.baseUrl = baseUrl;
|
|
45
|
+
}
|
|
46
|
+
async ensureClient() {
|
|
47
|
+
if (this.client)
|
|
48
|
+
return;
|
|
49
|
+
if (this.initPromise)
|
|
50
|
+
return this.initPromise;
|
|
51
|
+
this.initPromise = this._init();
|
|
52
|
+
return this.initPromise;
|
|
53
|
+
}
|
|
54
|
+
async _init() {
|
|
55
|
+
const { MemoryClient } = await import('mem0ai');
|
|
56
|
+
const opts = { apiKey: this.apiKey };
|
|
57
|
+
if (this.baseUrl)
|
|
58
|
+
opts.host = this.baseUrl;
|
|
59
|
+
this.client = new MemoryClient(opts);
|
|
60
|
+
}
|
|
61
|
+
async add(messages, opts) {
|
|
62
|
+
await this.ensureClient();
|
|
63
|
+
const addOpts = { userId: opts.userId };
|
|
64
|
+
if (opts.infer === false)
|
|
65
|
+
addOpts.infer = false;
|
|
66
|
+
const result = await this.client.add(messages, addOpts);
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
async search(query, opts) {
|
|
70
|
+
await this.ensureClient();
|
|
71
|
+
const searchOpts = {
|
|
72
|
+
filters: { user_id: opts.userId },
|
|
73
|
+
};
|
|
74
|
+
if (opts.topK)
|
|
75
|
+
searchOpts.topK = opts.topK;
|
|
76
|
+
const results = await this.client.search(query, searchOpts);
|
|
77
|
+
return normalizeResults(results);
|
|
78
|
+
}
|
|
79
|
+
async getAll(opts) {
|
|
80
|
+
await this.ensureClient();
|
|
81
|
+
const results = await this.client.getAll({
|
|
82
|
+
filters: { user_id: opts.userId },
|
|
83
|
+
});
|
|
84
|
+
return normalizeResults(results);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
class OSSProvider {
|
|
88
|
+
ossConfig;
|
|
89
|
+
resolveKey;
|
|
90
|
+
memory;
|
|
91
|
+
initPromise = null;
|
|
92
|
+
constructor(ossConfig, resolveKey) {
|
|
93
|
+
this.ossConfig = ossConfig;
|
|
94
|
+
this.resolveKey = resolveKey;
|
|
95
|
+
}
|
|
96
|
+
async ensureMemory() {
|
|
97
|
+
if (this.memory)
|
|
98
|
+
return;
|
|
99
|
+
if (this.initPromise)
|
|
100
|
+
return this.initPromise;
|
|
101
|
+
this.initPromise = this._init();
|
|
102
|
+
return this.initPromise;
|
|
103
|
+
}
|
|
104
|
+
async _buildConfig() {
|
|
105
|
+
const config = {};
|
|
106
|
+
const defaultEmbedder = { provider: 'openai', config: { model: 'text-embedding-3-small' } };
|
|
107
|
+
const defaultLlm = { provider: 'openai', config: { model: 'gpt-4.1-nano' } };
|
|
108
|
+
const embedderProvider = this.ossConfig?.embedder?.provider || defaultEmbedder.provider;
|
|
109
|
+
const embedderCfg = {
|
|
110
|
+
...defaultEmbedder.config,
|
|
111
|
+
...(this.ossConfig?.embedder?.config ?? {}),
|
|
112
|
+
};
|
|
113
|
+
const llmProvider = this.ossConfig?.llm?.provider || defaultLlm.provider;
|
|
114
|
+
const llmCfg = {
|
|
115
|
+
...defaultLlm.config,
|
|
116
|
+
...(this.ossConfig?.llm?.config ?? {}),
|
|
117
|
+
};
|
|
118
|
+
// Resolve API keys from pi model registry if not explicitly set
|
|
119
|
+
if (this.resolveKey) {
|
|
120
|
+
if (!embedderCfg.apiKey && !embedderCfg.api_key) {
|
|
121
|
+
const key = await this.resolveKey(embedderProvider);
|
|
122
|
+
if (key)
|
|
123
|
+
embedderCfg.apiKey = key;
|
|
124
|
+
}
|
|
125
|
+
if (!llmCfg.apiKey && !llmCfg.api_key) {
|
|
126
|
+
const key = await this.resolveKey(llmProvider);
|
|
127
|
+
if (key)
|
|
128
|
+
llmCfg.apiKey = key;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
config.embedder = { provider: embedderProvider, config: embedderCfg };
|
|
132
|
+
config.llm = { provider: llmProvider, config: llmCfg };
|
|
133
|
+
if (this.ossConfig?.vectorStore) {
|
|
134
|
+
config.vectorStore = this.ossConfig.vectorStore;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
// Default: SQLite in <PI_AGENT_HOME>/memories/mem0.db (alongside pi-memory's files)
|
|
138
|
+
config.vectorStore = {
|
|
139
|
+
provider: 'sqlite',
|
|
140
|
+
config: { dbPath: join(resolveAgentDir(), 'memories', 'mem0.db') },
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
if (this.ossConfig?.disableHistory) {
|
|
144
|
+
config.disableHistory = true;
|
|
145
|
+
}
|
|
146
|
+
return config;
|
|
147
|
+
}
|
|
148
|
+
async _init() {
|
|
149
|
+
const mod = await import('mem0ai/oss');
|
|
150
|
+
const Memory = mod.Memory ?? mod.default;
|
|
151
|
+
const builtConfig = await this._buildConfig();
|
|
152
|
+
this.memory = new Memory(builtConfig);
|
|
153
|
+
await this.memory.getAll({ filters: { user_id: '__warmup__' } });
|
|
154
|
+
}
|
|
155
|
+
async add(messages, opts) {
|
|
156
|
+
await this.ensureMemory();
|
|
157
|
+
const addOpts = { userId: opts.userId };
|
|
158
|
+
if (opts.infer === false)
|
|
159
|
+
addOpts.infer = false;
|
|
160
|
+
const result = await this.memory.add(messages, addOpts);
|
|
161
|
+
return result;
|
|
162
|
+
}
|
|
163
|
+
async search(query, opts) {
|
|
164
|
+
await this.ensureMemory();
|
|
165
|
+
const searchOpts = {
|
|
166
|
+
filters: { user_id: opts.userId },
|
|
167
|
+
};
|
|
168
|
+
if (opts.topK)
|
|
169
|
+
searchOpts.topK = opts.topK;
|
|
170
|
+
const results = await this.memory.search(query, searchOpts);
|
|
171
|
+
return normalizeResults(results);
|
|
172
|
+
}
|
|
173
|
+
async getAll(opts) {
|
|
174
|
+
await this.ensureMemory();
|
|
175
|
+
const results = await this.memory.getAll({
|
|
176
|
+
filters: { user_id: opts.userId },
|
|
177
|
+
});
|
|
178
|
+
return normalizeResults(results);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
export async function createMem0Provider(opts) {
|
|
182
|
+
const { config, resolveKey } = opts;
|
|
183
|
+
const mode = config.mode ?? 'platform';
|
|
184
|
+
if (mode === 'open-source') {
|
|
185
|
+
const useRegistry = config.useRegistryKeys !== false;
|
|
186
|
+
const provider = new OSSProvider(config.oss, useRegistry ? resolveKey : undefined);
|
|
187
|
+
await provider.ensureMemory();
|
|
188
|
+
return provider;
|
|
189
|
+
}
|
|
190
|
+
if (!config.apiKey?.trim()) {
|
|
191
|
+
throw new Error('Platform mode requires apiKey.');
|
|
192
|
+
}
|
|
193
|
+
return new PlatformProvider(config.apiKey.trim(), config.baseUrl?.trim());
|
|
194
|
+
}
|
|
195
|
+
//# sourceMappingURL=provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AAkBjE,8EAA8E;AAC9E,cAAc;AACd,8EAA8E;AAE9E,SAAS,mBAAmB,CAAC,GAA4B;IACvD,OAAO;QACL,EAAE,EAAE,MA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AAK,CAAC,gCAAgC,CAAC,CAAC;IACpD,CAAC;IACD,OAAO,IAAI,gBAAgB,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,MAAM,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;AAC5E,CAAC"}
|
package/dist/sync.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Passive turn sync — sends conversation pairs to Mem0 for server-side extraction.
|
|
3
|
+
* Buffers user and assistant messages, syncs as pairs.
|
|
4
|
+
*/
|
|
5
|
+
import type { Mem0Provider } from './provider.js';
|
|
6
|
+
export declare class TurnSync {
|
|
7
|
+
private readonly provider;
|
|
8
|
+
private readonly userId;
|
|
9
|
+
private inflight;
|
|
10
|
+
private lastUserText;
|
|
11
|
+
constructor(provider: Mem0Provider, userId: string);
|
|
12
|
+
/** Buffer a message. When we have a user+assistant pair, sync it. */
|
|
13
|
+
onMessage(role: string, text: string): void;
|
|
14
|
+
private sync;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=sync.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sync.d.ts","sourceRoot":"","sources":["../src/sync.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAElD,qBAAa,QAAQ;IACnB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAe;IACxC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,YAAY,CAAM;gBAEd,QAAQ,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM;IAKlD,qEAAqE;IACrE,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI;IAY3C,OAAO,CAAC,IAAI;CAiBb"}
|
package/dist/sync.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Passive turn sync — sends conversation pairs to Mem0 for server-side extraction.
|
|
3
|
+
* Buffers user and assistant messages, syncs as pairs.
|
|
4
|
+
*/
|
|
5
|
+
export class TurnSync {
|
|
6
|
+
provider;
|
|
7
|
+
userId;
|
|
8
|
+
inflight = false;
|
|
9
|
+
lastUserText = '';
|
|
10
|
+
constructor(provider, userId) {
|
|
11
|
+
this.provider = provider;
|
|
12
|
+
this.userId = userId;
|
|
13
|
+
}
|
|
14
|
+
/** Buffer a message. When we have a user+assistant pair, sync it. */
|
|
15
|
+
onMessage(role, text) {
|
|
16
|
+
if (role === 'user') {
|
|
17
|
+
this.lastUserText = text;
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
if (role === 'assistant' && text && this.lastUserText) {
|
|
21
|
+
this.sync(this.lastUserText, text);
|
|
22
|
+
this.lastUserText = '';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
sync(userContent, assistantContent) {
|
|
26
|
+
if (this.inflight)
|
|
27
|
+
return;
|
|
28
|
+
this.inflight = true;
|
|
29
|
+
this.provider
|
|
30
|
+
.add([
|
|
31
|
+
{ role: 'user', content: userContent },
|
|
32
|
+
{ role: 'assistant', content: assistantContent },
|
|
33
|
+
], { userId: this.userId })
|
|
34
|
+
.catch(() => { })
|
|
35
|
+
.finally(() => {
|
|
36
|
+
this.inflight = false;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=sync.js.map
|
package/dist/sync.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sync.js","sourceRoot":"","sources":["../src/sync.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,MAAM,OAAO,QAAQ;IACF,QAAQ,CAAe;IACvB,MAAM,CAAS;IACxB,QAAQ,GAAG,KAAK,CAAC;IACjB,YAAY,GAAG,EAAE,CAAC;IAE1B,YAAY,QAAsB,EAAE,MAAc;QAChD,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED,qEAAqE;IACrE,SAAS,CAAC,IAAY,EAAE,IAAY;QAClC,IAAI,IAAI,KAAK,MAAM,EAAE,CAAC;YACpB,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC;YACzB,OAAO;QACT,CAAC;QAED,IAAI,IAAI,KAAK,WAAW,IAAI,IAAI,IAAI,IAAI,CAAC,YAAY,EAAE,CAAC;YACtD,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE,IAAI,CAAC,CAAC;YACnC,IAAI,CAAC,YAAY,GAAG,EAAE,CAAC;QACzB,CAAC;IACH,CAAC;IAEO,IAAI,CAAC,WAAmB,EAAE,gBAAwB;QACxD,IAAI,IAAI,CAAC,QAAQ;YAAE,OAAO;QAE1B,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;QACrB,IAAI,CAAC,QAAQ;aACV,GAAG,CACF;YACE,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE;YACtC,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,gBAAgB,EAAE;SACjD,EACD,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,CACxB;aACA,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC;aACf,OAAO,CAAC,GAAG,EAAE;YACZ,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC;QACxB,CAAC,CAAC,CAAC;IACP,CAAC;CACF"}
|
package/dist/tools.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../src/tools.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iCAAiC,CAAC;AAEvE,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;AAMD,wBAAgB,eAAe,CAAC,QAAQ,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,GAAG,cAAc,EAAE,CA8FxF"}
|
package/dist/tools.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM-callable tools for Mem0: search, profile (get all), and save (verbatim store).
|
|
3
|
+
*/
|
|
4
|
+
import { Type } from 'typebox';
|
|
5
|
+
function textResult(text) {
|
|
6
|
+
return { content: [{ type: 'text', text }], details: undefined };
|
|
7
|
+
}
|
|
8
|
+
export function createMem0Tools(provider, userId) {
|
|
9
|
+
const searchTool = {
|
|
10
|
+
name: 'mem0_search',
|
|
11
|
+
label: 'Mem0',
|
|
12
|
+
description: 'Search long-term memories by meaning. Returns relevant facts ranked by similarity.',
|
|
13
|
+
promptSnippet: 'Semantic search over long-term user memories.',
|
|
14
|
+
parameters: Type.Object({
|
|
15
|
+
query: Type.String({ description: 'What to search for.' }),
|
|
16
|
+
top_k: Type.Optional(Type.Number({ description: 'Max results (default: 10, max: 50).' })),
|
|
17
|
+
}),
|
|
18
|
+
async execute(_toolCallId, params) {
|
|
19
|
+
const query = String(params.query ?? '');
|
|
20
|
+
const topK = Math.min(Number(params.top_k) || 10, 50);
|
|
21
|
+
if (!query)
|
|
22
|
+
return textResult(JSON.stringify({ error: 'Query cannot be empty.' }));
|
|
23
|
+
try {
|
|
24
|
+
const results = await provider.search(query, { userId, topK });
|
|
25
|
+
if (results.length === 0) {
|
|
26
|
+
return textResult(JSON.stringify({ result: 'No relevant memories found.' }));
|
|
27
|
+
}
|
|
28
|
+
return textResult(JSON.stringify({
|
|
29
|
+
results: results.map((r) => ({ memory: r.memory, score: r.score })),
|
|
30
|
+
count: results.length,
|
|
31
|
+
}));
|
|
32
|
+
}
|
|
33
|
+
catch (err) {
|
|
34
|
+
return textResult(JSON.stringify({
|
|
35
|
+
error: `Search failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
36
|
+
}));
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
const profileTool = {
|
|
41
|
+
name: 'mem0_profile',
|
|
42
|
+
label: 'Mem0',
|
|
43
|
+
description: 'Retrieve all stored long-term memories about the user.',
|
|
44
|
+
promptSnippet: 'Full dump of all stored user memories.',
|
|
45
|
+
parameters: Type.Object({}),
|
|
46
|
+
async execute() {
|
|
47
|
+
try {
|
|
48
|
+
const memories = await provider.getAll({ userId });
|
|
49
|
+
if (memories.length === 0) {
|
|
50
|
+
return textResult(JSON.stringify({ result: 'No memories stored yet.' }));
|
|
51
|
+
}
|
|
52
|
+
const lines = memories.map((m) => m.memory).filter(Boolean);
|
|
53
|
+
return textResult(JSON.stringify({ result: lines.join('\n'), count: lines.length }));
|
|
54
|
+
}
|
|
55
|
+
catch (err) {
|
|
56
|
+
return textResult(JSON.stringify({
|
|
57
|
+
error: `Profile failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
58
|
+
}));
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
const saveTool = {
|
|
63
|
+
name: 'mem0_save',
|
|
64
|
+
label: 'Mem0',
|
|
65
|
+
description: 'Store a durable fact about the user verbatim (no LLM extraction). Use for explicit preferences or corrections.',
|
|
66
|
+
promptSnippet: 'Save a fact to long-term memory verbatim.',
|
|
67
|
+
parameters: Type.Object({
|
|
68
|
+
fact: Type.String({ description: 'The fact to store.' }),
|
|
69
|
+
}),
|
|
70
|
+
async execute(_toolCallId, params) {
|
|
71
|
+
const fact = String(params.fact ?? '').trim();
|
|
72
|
+
if (!fact)
|
|
73
|
+
return textResult(JSON.stringify({ error: 'Fact cannot be empty.' }));
|
|
74
|
+
try {
|
|
75
|
+
const result = await provider.add([{ role: 'user', content: fact }], {
|
|
76
|
+
userId,
|
|
77
|
+
infer: false,
|
|
78
|
+
});
|
|
79
|
+
return textResult(JSON.stringify(result ? { result: 'Fact stored.' } : { error: 'Failed to store.' }));
|
|
80
|
+
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
return textResult(JSON.stringify({
|
|
83
|
+
error: `Save failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
84
|
+
}));
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
return [searchTool, profileTool, saveTool];
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=tools.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.js","sourceRoot":"","sources":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;gBACH,OAAO,UAAU,CACf,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,cAAc,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,kBAAkB,EAAE,CAAC,CACpF,CAAC;YACJ,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,OAAO,UAAU,CACf,IAAI,CAAC,SAAS,CAAC;oBACb,KAAK,EAAE,gBAAgB,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE;iBAC1E,CAAC,CACH,CAAC;YACJ,CAAC;QACH,CAAC;KACF,CAAC;IAEF,OAAO,CAAC,UAAU,EAAE,WAAW,EAAE,QAAQ,CAAC,CAAC;AAC7C,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration types for pi-memory-mem0.
|
|
3
|
+
*/
|
|
4
|
+
export type Mem0Mode = 'platform' | 'open-source';
|
|
5
|
+
export interface Mem0ExtensionConfig {
|
|
6
|
+
/** "platform" (Mem0 Cloud) or "open-source" (local SQLite). Default: "platform". */
|
|
7
|
+
mode?: Mem0Mode;
|
|
8
|
+
/** Mem0 Platform API key. Supports ${MEM0_API_KEY}. */
|
|
9
|
+
apiKey?: string;
|
|
10
|
+
/** Custom API base URL. Default: https://api.mem0.ai */
|
|
11
|
+
baseUrl?: string;
|
|
12
|
+
oss?: {
|
|
13
|
+
embedder?: {
|
|
14
|
+
provider: string;
|
|
15
|
+
config?: Record<string, unknown>;
|
|
16
|
+
};
|
|
17
|
+
llm?: {
|
|
18
|
+
provider: string;
|
|
19
|
+
config?: Record<string, unknown>;
|
|
20
|
+
};
|
|
21
|
+
vectorStore?: {
|
|
22
|
+
provider: string;
|
|
23
|
+
config?: Record<string, unknown>;
|
|
24
|
+
};
|
|
25
|
+
disableHistory?: boolean;
|
|
26
|
+
};
|
|
27
|
+
/** User identifier for memory scoping. Supports ${USER}. */
|
|
28
|
+
userId?: string;
|
|
29
|
+
/** Max recalled memories per turn. Default: 5 */
|
|
30
|
+
topK?: number;
|
|
31
|
+
/**
|
|
32
|
+
* When true (default), OSS mode will attempt to resolve API keys from
|
|
33
|
+
* pi's model registry instead of requiring separate env vars.
|
|
34
|
+
* Falls back to OPENAI_API_KEY / DEEPSEEK_API_KEY env vars if registry lookup fails.
|
|
35
|
+
*/
|
|
36
|
+
useRegistryKeys?: boolean;
|
|
37
|
+
}
|
|
38
|
+
export interface MemoryItem {
|
|
39
|
+
id: string;
|
|
40
|
+
memory: string;
|
|
41
|
+
score?: number | undefined;
|
|
42
|
+
user_id?: string | undefined;
|
|
43
|
+
created_at?: string | undefined;
|
|
44
|
+
updated_at?: string | undefined;
|
|
45
|
+
}
|
|
46
|
+
export interface AddResult {
|
|
47
|
+
results?: Array<{
|
|
48
|
+
id: string;
|
|
49
|
+
memory: string;
|
|
50
|
+
event: string;
|
|
51
|
+
}>;
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,MAAM,QAAQ,GAAG,UAAU,GAAG,aAAa,CAAC;AAElD,MAAM,WAAW,mBAAmB;IAClC,oFAAoF;IACpF,IAAI,CAAC,EAAE,QAAQ,CAAC;IAGhB,uDAAuD;IACvD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,wDAAwD;IACxD,OAAO,CAAC,EAAE,MAAM,CAAC;IAGjB,GAAG,CAAC,EAAE;QACJ,QAAQ,CAAC,EAAE;YAAE,QAAQ,EAAE,MAAM,CAAC;YAAC,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;SAAE,CAAC;QAClE,GAAG,CAAC,EAAE;YAAE,QAAQ,EAAE,MAAM,CAAC;YAAC,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;SAAE,CAAC;QAC7D,WAAW,CAAC,EAAE;YAAE,QAAQ,EAAE,MAAM,CAAC;YAAC,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;SAAE,CAAC;QACrE,cAAc,CAAC,EAAE,OAAO,CAAC;KAC1B,CAAC;IAGF,4DAA4D;IAC5D,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,iDAAiD;IACjD,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;;;OAIG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;CAC3B;AAED,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC3B,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAChC,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACjC;AAED,MAAM,WAAW,SAAS;IACxB,OAAO,CAAC,EAAE,KAAK,CAAC;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CAChE"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|
package/package.json
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@amaster.ai/pi-memory-mem0",
|
|
3
|
+
"version": "0.1.2-beta.23",
|
|
4
|
+
"description": "Mem0 passive memory extension for pi — dual-mode: Platform (cloud) or Open-Source (local SQLite).",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"pi-package",
|
|
7
|
+
"pi",
|
|
8
|
+
"extension",
|
|
9
|
+
"memory",
|
|
10
|
+
"mem0",
|
|
11
|
+
"semantic-search",
|
|
12
|
+
"sqlite"
|
|
13
|
+
],
|
|
14
|
+
"license": "Apache-2.0",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"main": "./dist/index.js",
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./provider": {
|
|
25
|
+
"types": "./dist/provider.d.ts",
|
|
26
|
+
"default": "./dist/provider.js"
|
|
27
|
+
},
|
|
28
|
+
"./package.json": {
|
|
29
|
+
"default": "./package.json"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"pi": {
|
|
33
|
+
"extensions": [
|
|
34
|
+
"./dist/index.js"
|
|
35
|
+
]
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"dist",
|
|
39
|
+
"README.md"
|
|
40
|
+
],
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "https://github.com/TGYD-helige/pi.git",
|
|
47
|
+
"directory": "packages/pi-memory-mem0"
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"mem0ai": "^3.0.6",
|
|
51
|
+
"@amaster.ai/pi-shared": "0.1.2-beta.23"
|
|
52
|
+
},
|
|
53
|
+
"peerDependencies": {
|
|
54
|
+
"@earendil-works/pi-coding-agent": ">=0.74.0",
|
|
55
|
+
"typebox": "*"
|
|
56
|
+
},
|
|
57
|
+
"peerDependenciesMeta": {
|
|
58
|
+
"@earendil-works/pi-coding-agent": {
|
|
59
|
+
"optional": true
|
|
60
|
+
},
|
|
61
|
+
"typebox": {
|
|
62
|
+
"optional": true
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
"devDependencies": {
|
|
66
|
+
"@earendil-works/pi-coding-agent": "0.74.0",
|
|
67
|
+
"typebox": "*",
|
|
68
|
+
"vitest": "^4.0.0"
|
|
69
|
+
},
|
|
70
|
+
"scripts": {
|
|
71
|
+
"build": "tsc -b",
|
|
72
|
+
"typecheck": "tsc -b --pretty false",
|
|
73
|
+
"test": "vitest run src"
|
|
74
|
+
}
|
|
75
|
+
}
|