ai-memory-claw 1.0.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/.gitattributes +28 -0
- package/LICENSE +194 -0
- package/README.md +199 -0
- package/index.ts +167 -0
- package/openclaw.plugin.json +145 -0
- package/package.json +30 -0
- package/src/auto-capture.ts +211 -0
- package/src/auto-recall.ts +180 -0
- package/src/category.ts +149 -0
- package/src/config.ts +74 -0
- package/src/embedding.ts +149 -0
- package/src/forgetter.ts +129 -0
- package/src/integrator.ts +197 -0
- package/src/memory-system.ts +304 -0
- package/src/test.ts +116 -0
- package/src/triggers.ts +126 -0
- package/src/types.ts +191 -0
- package/tsconfig.json +20 -0
package/.gitattributes
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# 文本文件统一使用 LF 换行符(跨平台一致)
|
|
2
|
+
* text=auto eol=lf
|
|
3
|
+
|
|
4
|
+
# 明确指定某些文件类型
|
|
5
|
+
*.ts text
|
|
6
|
+
*.json text
|
|
7
|
+
*.md text
|
|
8
|
+
*.yml text
|
|
9
|
+
*.yaml text
|
|
10
|
+
|
|
11
|
+
# 排除二进制/不适用文本处理的目录
|
|
12
|
+
/node_modules/* -text
|
|
13
|
+
/dist/* -text
|
|
14
|
+
coverage/* -text
|
|
15
|
+
data/* -text
|
|
16
|
+
*.map -text
|
|
17
|
+
*.png binary
|
|
18
|
+
*.jpg binary
|
|
19
|
+
*.jpeg binary
|
|
20
|
+
*.gif binary
|
|
21
|
+
*.ico binary
|
|
22
|
+
*.woff binary
|
|
23
|
+
*.woff2 binary
|
|
24
|
+
|
|
25
|
+
# 确保大量文本文件(如果未来有)被正确识别
|
|
26
|
+
*.txt text
|
|
27
|
+
*.gitignore text
|
|
28
|
+
*.gitattributes text
|
package/LICENSE
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
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 granting the License.
|
|
13
|
+
|
|
14
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
15
|
+
other entities that control, are controlled by, or are under common
|
|
16
|
+
control with that entity. For the purposes of this definition,
|
|
17
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
18
|
+
direction or management of such entity, whether by contract or
|
|
19
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
20
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
21
|
+
|
|
22
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
23
|
+
exercising permissions granted by this License.
|
|
24
|
+
|
|
25
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
26
|
+
including but not limited to software source code, documentation
|
|
27
|
+
source, and configuration files.
|
|
28
|
+
|
|
29
|
+
"Object" form shall mean any form resulting from mechanical
|
|
30
|
+
transformation or translation of a Source form, including but
|
|
31
|
+
not limited to compiled object code, generated documentation,
|
|
32
|
+
and conversions to other media types.
|
|
33
|
+
|
|
34
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
35
|
+
Object form, made available under the License, as indicated by a
|
|
36
|
+
copyright notice that is included in or attached to the work
|
|
37
|
+
(which shall be included in the Source form).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
43
|
+
of this License, Derivative Works shall not include works that remain
|
|
44
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
45
|
+
the Work and Derivative Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean any work of authorship, including
|
|
48
|
+
the original version of the Work and any modifications or additions
|
|
49
|
+
to that Work, that is intentionally submitted to the Licensor for
|
|
50
|
+
inclusion in the Work by the copyright owner or by an individual or
|
|
51
|
+
Legal Entity authorized to submit on behalf of the copyright owner.
|
|
52
|
+
For the purposes of this definition, "submitted" means any form of
|
|
53
|
+
electronic, verbal, or written communication sent to the Licensor or
|
|
54
|
+
its representatives, including but not limited to communication on
|
|
55
|
+
electronic mailing lists, source code control systems, and issue
|
|
56
|
+
tracking systems that are managed by, or on behalf of, the Licensor
|
|
57
|
+
for the purpose of discussing and improving the Work, but excluding
|
|
58
|
+
communication that is conspicuously marked or otherwise designated
|
|
59
|
+
in writing by the copyright owner as "Not a Contribution."
|
|
60
|
+
|
|
61
|
+
"Contributor" shall mean the Licensor and any individual or Legal
|
|
62
|
+
Entity on behalf of whom a Contribution has been received by the
|
|
63
|
+
Licensor and subsequently incorporated within the Work.
|
|
64
|
+
|
|
65
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
66
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
67
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
68
|
+
copyright license to use, reproduce, prepare Derivative Works of,
|
|
69
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
70
|
+
Work and such Derivative Works in Source or Object form.
|
|
71
|
+
|
|
72
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
73
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
74
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
75
|
+
(except as stated in this section) patent license to make, have made,
|
|
76
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
77
|
+
where such license applies only to those patent claims licensable
|
|
78
|
+
by such Contributor that are necessarily infringed by their
|
|
79
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
80
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
81
|
+
institute patent litigation against any entity (including a
|
|
82
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work or
|
|
83
|
+
a Contribution incorporated within the Work constitutes direct or
|
|
84
|
+
contributory patent infringement, then any patent licenses granted
|
|
85
|
+
to You under this License for that Work shall terminate as of the date
|
|
86
|
+
such litigation is filed.
|
|
87
|
+
|
|
88
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
89
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
90
|
+
modifications, and in Source or Object form, provided that You
|
|
91
|
+
meet the following conditions:
|
|
92
|
+
|
|
93
|
+
(a) You must give any other recipients of the Work or
|
|
94
|
+
Derivative Works a copy of this License; and
|
|
95
|
+
|
|
96
|
+
(b) You must cause any modified files to carry prominent notices
|
|
97
|
+
stating that You changed the files; and
|
|
98
|
+
|
|
99
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
100
|
+
that You distribute, all copyright, trademark, patent, and
|
|
101
|
+
attribution notices from the Source form of the Work,
|
|
102
|
+
excluding those notices that do not pertain to any part of
|
|
103
|
+
the Derivative Works; and
|
|
104
|
+
|
|
105
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
106
|
+
distribution, then any Derivative Works that You distribute must
|
|
107
|
+
include a readable copy of the attribution notices contained
|
|
108
|
+
within such NOTICE file, excluding those notices that do not
|
|
109
|
+
pertain to any part of the Derivative Works, in at least one
|
|
110
|
+
of the following places: within a NOTICE text file distributed
|
|
111
|
+
as part of the Derivative Works; within the Source form or
|
|
112
|
+
documentation, if provided along with the Derivative Works; or,
|
|
113
|
+
within a display generated by the Derivative Works, if and
|
|
114
|
+
wherever such third-party notices normally appear. The contents
|
|
115
|
+
of the NOTICE file are for informational purposes only and
|
|
116
|
+
do not modify the License. You may add Your own attribution
|
|
117
|
+
notices within Derivative Works that You distribute, alongside
|
|
118
|
+
or as an addendum to the NOTICE text from the Work,
|
|
119
|
+
provided that such additional attribution notices cannot be
|
|
120
|
+
construed as modifying the License.
|
|
121
|
+
|
|
122
|
+
You may add Your own copyright statement to Your modifications and
|
|
123
|
+
provide for additional or different license terms and conditions
|
|
124
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
125
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
126
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
127
|
+
the conditions stated in this License.
|
|
128
|
+
|
|
129
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
130
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
131
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
132
|
+
this License, without any additional terms or conditions.
|
|
133
|
+
Notwithstanding the above, nothing shall supersede or modify the
|
|
134
|
+
terms of any separate license agreement you may have executed
|
|
135
|
+
with the Licensor regarding such Contributions.
|
|
136
|
+
|
|
137
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
138
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
139
|
+
except as required for reasonable and customary use in describing the
|
|
140
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
141
|
+
|
|
142
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
143
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
144
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
145
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
146
|
+
implied, including, without limitation, any warranties or conditions
|
|
147
|
+
of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
148
|
+
NONINFRINGEMENT. You are solely responsible for determining the
|
|
149
|
+
appropriateness of using or redistributing the Work and assume any
|
|
150
|
+
risks associated with Your exercise of permissions under this License.
|
|
151
|
+
|
|
152
|
+
8. Limitation of Liability. In no event shall any Contributor be
|
|
153
|
+
liable to You for damages, including any direct, indirect,
|
|
154
|
+
special, incidental, or consequential damages of any character
|
|
155
|
+
arising as a result of this License or out of the use or inability
|
|
156
|
+
use the Work (including but dot limited to procurement of substitute
|
|
157
|
+
goods or services; loss of use, data, or profits; or business
|
|
158
|
+
interruption) however caused and on any theory of liability,
|
|
159
|
+
whether in contract, strict liability, or tort (including
|
|
160
|
+
negligence or otherwise) arising in any way out of the use of
|
|
161
|
+
the Work, even if advised of the possibility of such damages.
|
|
162
|
+
|
|
163
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
164
|
+
the Work or Derivative Works, You may choose to offer and charge a
|
|
165
|
+
fee for, warranty, support, indemnity or other liability obligations
|
|
166
|
+
and/or rights consistent with this License. However, in accepting such
|
|
167
|
+
obligations, You may act only on Your own behalf and on Your sole
|
|
168
|
+
responsibility, not on behalf of any other Contributor, and only if
|
|
169
|
+
You agree to indemnify, defend, and hold each Contributor harmless
|
|
170
|
+
for any liability incurred by, that Contributor as a result of your
|
|
171
|
+
accepting any such warranty or additional liability.
|
|
172
|
+
|
|
173
|
+
END OF TERMS AND CONDITIONS
|
|
174
|
+
|
|
175
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
176
|
+
|
|
177
|
+
To apply the Apache License to your work, attach the following
|
|
178
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
179
|
+
replaced with your own identifying information. (Don't include
|
|
180
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
181
|
+
comment syntax for the file format. We also recommend that a
|
|
182
|
+
file or class name and description of purpose be included on the
|
|
183
|
+
same "printed" page as the copyright notice for easier
|
|
184
|
+
identification within third-party archives.
|
|
185
|
+
|
|
186
|
+
Copyright (c) [year] [fullname]
|
|
187
|
+
|
|
188
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
189
|
+
you may not use this file except in compliance with the License.
|
|
190
|
+
You may obtain a copy of the License at
|
|
191
|
+
|
|
192
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
193
|
+
|
|
194
|
+
Unless required by exc
|
package/README.md
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
# AI Memory Claw
|
|
2
|
+
|
|
3
|
+
OpenClaw 插件 - AI 渐进式记忆系统(无感运行版)
|
|
4
|
+
|
|
5
|
+
## 功能特性
|
|
6
|
+
|
|
7
|
+
- **自动召回**:对话前自动搜索并注入相关记忆
|
|
8
|
+
- **自动捕获**:对话后自动生成记忆(100%强制存储)
|
|
9
|
+
- **遗忘机制**:自动删除低价值记忆
|
|
10
|
+
- **整合机制**:自动合并相似记忆
|
|
11
|
+
- **向量搜索**:基于语义相似度的记忆检索
|
|
12
|
+
- **分类分析**:自动识别记忆类别
|
|
13
|
+
|
|
14
|
+
## 安装
|
|
15
|
+
|
|
16
|
+
### 方式一:直接复制(推荐用于本地)
|
|
17
|
+
|
|
18
|
+
1. 克隆或下载本项目
|
|
19
|
+
2. 进入项目目录并安装依赖
|
|
20
|
+
```bash
|
|
21
|
+
npm install
|
|
22
|
+
```
|
|
23
|
+
3. 复制整个项目到 OpenClaw 扩展目录
|
|
24
|
+
```bash
|
|
25
|
+
# 根据你的系统调整路径
|
|
26
|
+
cp -r . ~/.openclaw/extensions/ai-memory-claw
|
|
27
|
+
cd ~/.openclaw/extensions/ai-memory-claw
|
|
28
|
+
npm install # 再次安装确保本地依赖完整
|
|
29
|
+
```
|
|
30
|
+
4. 配置 OpenClaw:编辑 `~/.openclaw/openclaw.json`
|
|
31
|
+
```json
|
|
32
|
+
{
|
|
33
|
+
"plugins": {
|
|
34
|
+
"allow": ["ai-memory-claw"],
|
|
35
|
+
"entries": {
|
|
36
|
+
"ai-memory-claw": {
|
|
37
|
+
"enabled": true,
|
|
38
|
+
"config": {
|
|
39
|
+
"autoRecall": true,
|
|
40
|
+
"autoCapture": true
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
5. 启动 OpenClaw Gateway
|
|
48
|
+
|
|
49
|
+
### 方式二:发布到 npm(供开发者使用)
|
|
50
|
+
|
|
51
|
+
本插件已发布为 npm 包 `ai-memory-claw`。你可以:
|
|
52
|
+
```bash
|
|
53
|
+
npm install ai-memory-claw
|
|
54
|
+
```
|
|
55
|
+
然后在 OpenClaw 配置中指定:
|
|
56
|
+
```json
|
|
57
|
+
{
|
|
58
|
+
"plugins": {
|
|
59
|
+
"entries": {
|
|
60
|
+
"ai-memory-claw": {
|
|
61
|
+
"package": "ai-memory-claw",
|
|
62
|
+
"enabled": true
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## 配置说明
|
|
70
|
+
|
|
71
|
+
在 OpenClaw 配置中通过 `plugins.entries.ai-memory-claw.config` 设置。所有配置项:
|
|
72
|
+
|
|
73
|
+
| 配置项 | 类型 | 默认值 | 说明 |
|
|
74
|
+
|--------|------|--------|------|
|
|
75
|
+
| `dataDir` | `string` | `~/.ai-memory-claw/data` | 记忆数据存储目录 |
|
|
76
|
+
| `autoRecall` | `boolean` | `true` | 对话前自动搜索并注入相关记忆 |
|
|
77
|
+
| `autoCapture` | `boolean` | `true` | 对话后自动生成记忆 |
|
|
78
|
+
| `captureStrategy` | `"always" \| "selective"` | `"always"` | 捕获策略:always=全部存储,selective=仅关键词匹配 |
|
|
79
|
+
| `autoRecallInNewSession` | `boolean` | `true` | 新会话首次对话时自动注入记忆 |
|
|
80
|
+
| `newSessionMemoryLimit` | `number` (1-5) | `1` | 新会话时注入的记忆数量 |
|
|
81
|
+
| `manualTriggerEnabled` | `boolean` | `true` | 用户消息包含关键词时触发手动召回 |
|
|
82
|
+
| `manualTriggerKeywords` | `string[]` | 见下方 | 手动召回触发关键词列表 |
|
|
83
|
+
| `manualRecallLimit` | `number` (1-10) | `3` | 手动触发时注入的记忆数量上限 |
|
|
84
|
+
| `recallThreshold` | `number` (0-1) | `0.6` | 相似度阈值,越高越严格 |
|
|
85
|
+
| `recallLimit` | `number` (1-10) | `2` | 每次召回的记忆数量上限 |
|
|
86
|
+
| `captureMaxChars` | `number` (100-2000) | `500` | 自动捕获的最大字符数 |
|
|
87
|
+
| `enableSummary` | `boolean` | `true` | 启用记忆摘要功能 |
|
|
88
|
+
| `enableForget` | `boolean` | `true` | 启用遗忘机制(定期清理低价值记忆) |
|
|
89
|
+
| `enableIntegration` | `boolean` | `true` | 启用整合机制(合并相似记忆) |
|
|
90
|
+
| `forgetIntervalDays` | `number` (1-30) | `7` | 遗忘检查间隔(天) |
|
|
91
|
+
| `integrationThreshold` | `number` (0.5-1) | `0.8` | 记忆整合相似度阈值 |
|
|
92
|
+
|
|
93
|
+
### 默认触发关键词
|
|
94
|
+
|
|
95
|
+
```json
|
|
96
|
+
[
|
|
97
|
+
"记得", "之前", "上次", "以前", "查一下", "看看之前的记忆",
|
|
98
|
+
"用一下之前的", "参考之前的", "以前是怎么做的", "你还记得吗",
|
|
99
|
+
"记忆里", "历史上", "之前那次"
|
|
100
|
+
]
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## 记忆目录结构
|
|
104
|
+
|
|
105
|
+
```
|
|
106
|
+
~/.ai-memory-claw/data/
|
|
107
|
+
├── 日常事务/
|
|
108
|
+
│ └── 通用/
|
|
109
|
+
│ └── 日常-通用-20260328-2315-xxxxx.json
|
|
110
|
+
├── 系统运维/
|
|
111
|
+
│ └── 通用/
|
|
112
|
+
│ └── 系统-通用-20260328-xxxxxx.json
|
|
113
|
+
└── ...
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## 数据格式
|
|
117
|
+
|
|
118
|
+
每条记忆为 JSON 文件:
|
|
119
|
+
|
|
120
|
+
```json
|
|
121
|
+
{
|
|
122
|
+
"id": "日常-通用-20260328-2315-xxxxx",
|
|
123
|
+
"category": "日常事务",
|
|
124
|
+
"subCategory": "通用",
|
|
125
|
+
"content": {
|
|
126
|
+
"task": "用户询问的问题",
|
|
127
|
+
"process": "处理过程",
|
|
128
|
+
"result": "处理结果"
|
|
129
|
+
},
|
|
130
|
+
"embedding": [0.1, 0.2, ...],
|
|
131
|
+
"importance": "critical",
|
|
132
|
+
"usageCount": 10
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## 开发
|
|
137
|
+
|
|
138
|
+
### 构建
|
|
139
|
+
|
|
140
|
+
TypeScript 源码需编译为 JavaScript:
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
npm run build
|
|
144
|
+
# 或监听模式
|
|
145
|
+
npm run dev
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
输出位于 `dist/` 目录。
|
|
149
|
+
|
|
150
|
+
### 测试
|
|
151
|
+
|
|
152
|
+
本插件使用 Vitest 进行测试:
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
npm test
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### 项目结构
|
|
159
|
+
|
|
160
|
+
```
|
|
161
|
+
ai-memory-claw/
|
|
162
|
+
├── src/
|
|
163
|
+
│ ├── auto-capture.ts # 自动捕获逻辑
|
|
164
|
+
│ ├── auto-recall.ts # 自动召回逻辑
|
|
165
|
+
│ ├── category.ts # 记忆分类分析
|
|
166
|
+
│ ├── config.ts # 配置管理
|
|
167
|
+
│ ├── embedding.ts # 向量化处理
|
|
168
|
+
│ ├── forgetter.ts # 遗忘机制
|
|
169
|
+
│ ├── integrator.ts # 整合机制
|
|
170
|
+
│ ├── memory-system.ts # 核心记忆系统
|
|
171
|
+
│ ├── triggers.ts # 触发器匹配
|
|
172
|
+
│ ├── types.ts # 类型定义
|
|
173
|
+
│ └── test.ts # 测试文件
|
|
174
|
+
├── index.ts # 插件入口
|
|
175
|
+
├── openclaw.plugin.json # OpenClaw 插件元数据
|
|
176
|
+
├── tsconfig.json # TypeScript 配置
|
|
177
|
+
├── package.json # 项目依赖
|
|
178
|
+
└── README.md # 本文档
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## 技术说明
|
|
182
|
+
|
|
183
|
+
- 记忆数据按 `category/subCategory/` 分类存储 JSON 文件
|
|
184
|
+
- 使用简化的本地向量搜索(基于 `chroma-js` 的色彩空间算法,实验性质)
|
|
185
|
+
- 分类基于关键词匹配 + LLM 分析(可选)
|
|
186
|
+
- 支持自动摘要、合并重复记忆、定期遗忘
|
|
187
|
+
|
|
188
|
+
## 许可证
|
|
189
|
+
|
|
190
|
+
Apache License 2.0 - 详见 [LICENSE](LICENSE) 文件。
|
|
191
|
+
|
|
192
|
+
---
|
|
193
|
+
|
|
194
|
+
## 贡献
|
|
195
|
+
|
|
196
|
+
欢迎提交 Issue 和 Pull Request。请确保:
|
|
197
|
+
- 代码通过 `npm run build` 编译
|
|
198
|
+
- 测试通过 `npm test`
|
|
199
|
+
- 遵循项目的代码风格
|
package/index.ts
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AI Memory Claw - OpenClaw 插件入口
|
|
3
|
+
*
|
|
4
|
+
* AI渐进式记忆系统 - 无感运行版
|
|
5
|
+
* 每次对话自动召回、自动捕获记忆
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
|
|
9
|
+
import { MemorySystem } from "./src/memory-system";
|
|
10
|
+
import { AutoRecall } from "./src/auto-recall";
|
|
11
|
+
import { AutoCapture } from "./src/auto-capture";
|
|
12
|
+
|
|
13
|
+
// 默认触发关键词
|
|
14
|
+
const DEFAULT_TRIGGER_KEYWORDS = [
|
|
15
|
+
"记得", "之前", "上次", "以前", "查一下", "看看之前的记忆",
|
|
16
|
+
"用一下之前的", "参考之前的", "以前是怎么做的", "你还记得吗",
|
|
17
|
+
"记忆里", "历史上", "之前那次"
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
// 配置类型
|
|
21
|
+
interface MemoryConfig {
|
|
22
|
+
dataDir: string;
|
|
23
|
+
autoRecall: boolean;
|
|
24
|
+
autoCapture: boolean;
|
|
25
|
+
captureStrategy: "always" | "selective";
|
|
26
|
+
autoRecallInNewSession: boolean;
|
|
27
|
+
newSessionMemoryLimit: number;
|
|
28
|
+
manualTriggerEnabled: boolean;
|
|
29
|
+
manualTriggerKeywords: string[];
|
|
30
|
+
manualRecallLimit: number;
|
|
31
|
+
recallThreshold: number;
|
|
32
|
+
recallLimit: number;
|
|
33
|
+
captureMaxChars: number;
|
|
34
|
+
enableSummary: boolean;
|
|
35
|
+
enableForget: boolean;
|
|
36
|
+
enableIntegration: boolean;
|
|
37
|
+
forgetIntervalDays: number;
|
|
38
|
+
integrationThreshold: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// 默认配置
|
|
42
|
+
const defaultConfig: MemoryConfig = {
|
|
43
|
+
dataDir: "~/.ai-memory-claw/data",
|
|
44
|
+
autoRecall: true,
|
|
45
|
+
autoCapture: true,
|
|
46
|
+
captureStrategy: "always",
|
|
47
|
+
autoRecallInNewSession: true,
|
|
48
|
+
newSessionMemoryLimit: 1,
|
|
49
|
+
manualTriggerEnabled: true,
|
|
50
|
+
manualTriggerKeywords: DEFAULT_TRIGGER_KEYWORDS,
|
|
51
|
+
manualRecallLimit: 3,
|
|
52
|
+
recallThreshold: 0.6,
|
|
53
|
+
recallLimit: 2,
|
|
54
|
+
captureMaxChars: 500,
|
|
55
|
+
enableSummary: true,
|
|
56
|
+
enableForget: true,
|
|
57
|
+
enableIntegration: true,
|
|
58
|
+
forgetIntervalDays: 7,
|
|
59
|
+
integrationThreshold: 0.8
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// 插件配置 schema
|
|
63
|
+
const configSchema = {
|
|
64
|
+
type: "object" as const,
|
|
65
|
+
additionalProperties: false,
|
|
66
|
+
properties: {
|
|
67
|
+
dataDir: { type: "string" as const, default: "~/.ai-memory-claw/data" },
|
|
68
|
+
autoRecall: { type: "boolean" as const, default: true },
|
|
69
|
+
autoCapture: { type: "boolean" as const, default: true },
|
|
70
|
+
captureStrategy: { type: "string" as const, enum: ["always", "selective"], default: "always" },
|
|
71
|
+
autoRecallInNewSession: { type: "boolean" as const, default: true },
|
|
72
|
+
newSessionMemoryLimit: { type: "number" as const, minimum: 1, maximum: 5, default: 1 },
|
|
73
|
+
manualTriggerEnabled: { type: "boolean" as const, default: true },
|
|
74
|
+
manualTriggerKeywords: { type: "array" as const, items: { type: "string" as const }, default: DEFAULT_TRIGGER_KEYWORDS },
|
|
75
|
+
manualRecallLimit: { type: "number" as const, minimum: 1, maximum: 10, default: 3 },
|
|
76
|
+
recallThreshold: { type: "number" as const, minimum: 0, maximum: 1, default: 0.6 },
|
|
77
|
+
recallLimit: { type: "number" as const, minimum: 1, maximum: 10, default: 2 },
|
|
78
|
+
captureMaxChars: { type: "number" as const, minimum: 100, maximum: 2000, default: 500 },
|
|
79
|
+
enableSummary: { type: "boolean" as const, default: true },
|
|
80
|
+
enableForget: { type: "boolean" as const, default: true },
|
|
81
|
+
enableIntegration: { type: "boolean" as const, default: true },
|
|
82
|
+
forgetIntervalDays: { type: "number" as const, minimum: 1, maximum: 30, default: 7 },
|
|
83
|
+
integrationThreshold: { type: "number" as const, minimum: 0.5, maximum: 1, default: 0.8 }
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// 全局实例
|
|
88
|
+
let memorySystem: MemorySystem | null = null;
|
|
89
|
+
let autoRecallInstance: AutoRecall | null = null;
|
|
90
|
+
let config: MemoryConfig = defaultConfig;
|
|
91
|
+
|
|
92
|
+
const plugin = {
|
|
93
|
+
id: "ai-memory-claw",
|
|
94
|
+
name: "AI Memory Claw",
|
|
95
|
+
description: "AI渐进式记忆系统 - 无感运行版",
|
|
96
|
+
configSchema,
|
|
97
|
+
|
|
98
|
+
register(api: OpenClawPluginApi) {
|
|
99
|
+
// 合并配置
|
|
100
|
+
const pluginConfig = api.pluginConfig as Partial<MemoryConfig> || {};
|
|
101
|
+
config = { ...defaultConfig, ...pluginConfig } as MemoryConfig;
|
|
102
|
+
|
|
103
|
+
const dataDir = api.resolvePath(config.dataDir);
|
|
104
|
+
|
|
105
|
+
// 初始化日志欢迎 (方案A)
|
|
106
|
+
api.logger.info("=".repeat(40));
|
|
107
|
+
api.logger.info("✅ AI Memory Claw 插件已加载");
|
|
108
|
+
api.logger.info(`📂 数据目录: ${dataDir}`);
|
|
109
|
+
|
|
110
|
+
// 初始化记忆系统
|
|
111
|
+
memorySystem = new MemorySystem(dataDir, config);
|
|
112
|
+
|
|
113
|
+
// 初始化并显示状态
|
|
114
|
+
memorySystem.initialize().then(() => {
|
|
115
|
+
const stats = memorySystem!.getStats();
|
|
116
|
+
api.logger.info(`📚 当前记忆: ${stats.total} 条`);
|
|
117
|
+
api.logger.info(`🎯 自动召回: ${config.autoRecall ? '已开启' : '已关闭'}`);
|
|
118
|
+
api.logger.info(`📝 自动捕获: ${config.autoCapture ? '已开启' : '已关闭'}`);
|
|
119
|
+
api.logger.info(`🗂️ 存储策略: ${config.captureStrategy}`);
|
|
120
|
+
api.logger.info(`🔄 新会话召回: ${config.autoRecallInNewSession ? '已开启' : '已关闭'}`);
|
|
121
|
+
api.logger.info(`🔍 手动触发: ${config.manualTriggerEnabled ? '已开启' : '已关闭'}`);
|
|
122
|
+
api.logger.info("=".repeat(40));
|
|
123
|
+
}).catch((err) => {
|
|
124
|
+
api.logger.error(`❌ 初始化失败: ${err}`);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// 初始化自动召回
|
|
128
|
+
autoRecallInstance = new AutoRecall(memorySystem, config);
|
|
129
|
+
|
|
130
|
+
// 初始化自动捕获
|
|
131
|
+
const autoCapture = new AutoCapture(memorySystem, config);
|
|
132
|
+
|
|
133
|
+
// 注册 before_agent_start 钩子 (自动召回)
|
|
134
|
+
if (config.autoRecall) {
|
|
135
|
+
api.on("before_agent_start", async (event) => {
|
|
136
|
+
try {
|
|
137
|
+
return await autoRecallInstance!.execute(event);
|
|
138
|
+
} catch (err) {
|
|
139
|
+
api.logger.error(`自动召回失败: ${err}`);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// 注册 agent_end 钩子 (自动捕获)
|
|
145
|
+
if (config.autoCapture) {
|
|
146
|
+
api.on("agent_end", async (event) => {
|
|
147
|
+
try {
|
|
148
|
+
await autoCapture.execute(event);
|
|
149
|
+
} catch (err) {
|
|
150
|
+
api.logger.error(`自动捕获失败: ${err}`);
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// 注册 session_start 钩子(重置召回状态)
|
|
156
|
+
api.on("session_start", () => {
|
|
157
|
+
if (autoRecallInstance) {
|
|
158
|
+
autoRecallInstance.resetSession();
|
|
159
|
+
}
|
|
160
|
+
api.logger.info("[Memory] 新会话开始");
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
api.logger.info("AI Memory Claw 插件初始化完成");
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
export default plugin;
|