dual-mem 0.1.0__tar.gz

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.
Files changed (109) hide show
  1. dual_mem-0.1.0/LICENSE +201 -0
  2. dual_mem-0.1.0/PKG-INFO +280 -0
  3. dual_mem-0.1.0/README.md +252 -0
  4. dual_mem-0.1.0/dual_mem/__init__.py +46 -0
  5. dual_mem-0.1.0/dual_mem/agent/__init__.py +6 -0
  6. dual_mem-0.1.0/dual_mem/agent/basic_profile.py +132 -0
  7. dual_mem-0.1.0/dual_mem/agent/extractor.py +75 -0
  8. dual_mem-0.1.0/dual_mem/agent/gate.py +301 -0
  9. dual_mem-0.1.0/dual_mem/agent/lightweight_extractor.py +427 -0
  10. dual_mem-0.1.0/dual_mem/agent/mem_agent.py +527 -0
  11. dual_mem-0.1.0/dual_mem/agent/prompts.py +851 -0
  12. dual_mem-0.1.0/dual_mem/agent/reconciler.py +313 -0
  13. dual_mem-0.1.0/dual_mem/agent/summarizer.py +32 -0
  14. dual_mem-0.1.0/dual_mem/api/__init__.py +8 -0
  15. dual_mem-0.1.0/dual_mem/api/app.py +242 -0
  16. dual_mem-0.1.0/dual_mem/api/schemas.py +109 -0
  17. dual_mem-0.1.0/dual_mem/cli/__init__.py +8 -0
  18. dual_mem-0.1.0/dual_mem/cli/main.py +127 -0
  19. dual_mem-0.1.0/dual_mem/client.py +364 -0
  20. dual_mem-0.1.0/dual_mem/config.py +119 -0
  21. dual_mem-0.1.0/dual_mem/isolation.py +40 -0
  22. dual_mem-0.1.0/dual_mem/mcp/__init__.py +8 -0
  23. dual_mem-0.1.0/dual_mem/mcp/server.py +139 -0
  24. dual_mem-0.1.0/dual_mem/providers/__init__.py +0 -0
  25. dual_mem-0.1.0/dual_mem/providers/embedding.py +136 -0
  26. dual_mem-0.1.0/dual_mem/providers/llm.py +155 -0
  27. dual_mem-0.1.0/dual_mem/registry.py +96 -0
  28. dual_mem-0.1.0/dual_mem/retrieval/__init__.py +6 -0
  29. dual_mem-0.1.0/dual_mem/retrieval/anchor_search.py +341 -0
  30. dual_mem-0.1.0/dual_mem/retrieval/bm25.py +74 -0
  31. dual_mem-0.1.0/dual_mem/retrieval/evolution.py +124 -0
  32. dual_mem-0.1.0/dual_mem/retrieval/formatter.py +38 -0
  33. dual_mem-0.1.0/dual_mem/retrieval/fusion_scorer.py +175 -0
  34. dual_mem-0.1.0/dual_mem/retrieval/graph_expander.py +164 -0
  35. dual_mem-0.1.0/dual_mem/retrieval/intent.py +146 -0
  36. dual_mem-0.1.0/dual_mem/retrieval/query_understanding.py +54 -0
  37. dual_mem-0.1.0/dual_mem/retrieval/reader.py +703 -0
  38. dual_mem-0.1.0/dual_mem/retrieval/reconsolidation.py +121 -0
  39. dual_mem-0.1.0/dual_mem/retrieval/rrf.py +48 -0
  40. dual_mem-0.1.0/dual_mem/sdk_models.py +244 -0
  41. dual_mem-0.1.0/dual_mem/storage/__init__.py +0 -0
  42. dual_mem-0.1.0/dual_mem/storage/cache_store.py +269 -0
  43. dual_mem-0.1.0/dual_mem/storage/graph_store.py +232 -0
  44. dual_mem-0.1.0/dual_mem/storage/history_store.py +70 -0
  45. dual_mem-0.1.0/dual_mem/storage/vector_store.py +138 -0
  46. dual_mem-0.1.0/dual_mem/system2/__init__.py +6 -0
  47. dual_mem-0.1.0/dual_mem/system2/clustering.py +123 -0
  48. dual_mem-0.1.0/dual_mem/system2/cross_domain_sweeper.py +233 -0
  49. dual_mem-0.1.0/dual_mem/system2/reconciler_worker.py +214 -0
  50. dual_mem-0.1.0/dual_mem/system2/s2_tools.py +406 -0
  51. dual_mem-0.1.0/dual_mem/system2/system2_agent.py +271 -0
  52. dual_mem-0.1.0/dual_mem/system2/system2_writer.py +248 -0
  53. dual_mem-0.1.0/dual_mem/types.py +153 -0
  54. dual_mem-0.1.0/dual_mem/writer/__init__.py +6 -0
  55. dual_mem-0.1.0/dual_mem/writer/memory_writer.py +178 -0
  56. dual_mem-0.1.0/dual_mem.egg-info/PKG-INFO +280 -0
  57. dual_mem-0.1.0/dual_mem.egg-info/SOURCES.txt +107 -0
  58. dual_mem-0.1.0/dual_mem.egg-info/dependency_links.txt +1 -0
  59. dual_mem-0.1.0/dual_mem.egg-info/entry_points.txt +3 -0
  60. dual_mem-0.1.0/dual_mem.egg-info/requires.txt +20 -0
  61. dual_mem-0.1.0/dual_mem.egg-info/top_level.txt +1 -0
  62. dual_mem-0.1.0/pyproject.toml +47 -0
  63. dual_mem-0.1.0/setup.cfg +4 -0
  64. dual_mem-0.1.0/tests/test_anchor_search.py +168 -0
  65. dual_mem-0.1.0/tests/test_api.py +150 -0
  66. dual_mem-0.1.0/tests/test_basic_profile.py +79 -0
  67. dual_mem-0.1.0/tests/test_cli.py +39 -0
  68. dual_mem-0.1.0/tests/test_client_lite.py +69 -0
  69. dual_mem-0.1.0/tests/test_clustering.py +75 -0
  70. dual_mem-0.1.0/tests/test_concurrent_writes.py +104 -0
  71. dual_mem-0.1.0/tests/test_config.py +75 -0
  72. dual_mem-0.1.0/tests/test_embedding.py +46 -0
  73. dual_mem-0.1.0/tests/test_evolution.py +74 -0
  74. dual_mem-0.1.0/tests/test_extractor.py +65 -0
  75. dual_mem-0.1.0/tests/test_formatter.py +42 -0
  76. dual_mem-0.1.0/tests/test_fusion_scorer.py +143 -0
  77. dual_mem-0.1.0/tests/test_graph_expander.py +94 -0
  78. dual_mem-0.1.0/tests/test_graph_store.py +71 -0
  79. dual_mem-0.1.0/tests/test_intent.py +57 -0
  80. dual_mem-0.1.0/tests/test_isolation.py +51 -0
  81. dual_mem-0.1.0/tests/test_lightweight_extractor.py +117 -0
  82. dual_mem-0.1.0/tests/test_llm.py +93 -0
  83. dual_mem-0.1.0/tests/test_mcp.py +47 -0
  84. dual_mem-0.1.0/tests/test_mem_agent.py +88 -0
  85. dual_mem-0.1.0/tests/test_messages_input.py +106 -0
  86. dual_mem-0.1.0/tests/test_pipeline_trace.py +65 -0
  87. dual_mem-0.1.0/tests/test_prompts.py +29 -0
  88. dual_mem-0.1.0/tests/test_read_result_debug.py +46 -0
  89. dual_mem-0.1.0/tests/test_reader_full.py +117 -0
  90. dual_mem-0.1.0/tests/test_reader_hybrid.py +84 -0
  91. dual_mem-0.1.0/tests/test_reader_normal.py +36 -0
  92. dual_mem-0.1.0/tests/test_reconciler.py +119 -0
  93. dual_mem-0.1.0/tests/test_reconsolidation_drain.py +123 -0
  94. dual_mem-0.1.0/tests/test_reconsolidation_enqueue.py +131 -0
  95. dual_mem-0.1.0/tests/test_reflector.py +76 -0
  96. dual_mem-0.1.0/tests/test_reflector_apply.py +124 -0
  97. dual_mem-0.1.0/tests/test_registry.py +33 -0
  98. dual_mem-0.1.0/tests/test_rerank.py +38 -0
  99. dual_mem-0.1.0/tests/test_sqlite_stores.py +55 -0
  100. dual_mem-0.1.0/tests/test_summarizer.py +27 -0
  101. dual_mem-0.1.0/tests/test_sweeper.py +92 -0
  102. dual_mem-0.1.0/tests/test_system2_agent.py +201 -0
  103. dual_mem-0.1.0/tests/test_system2_writer.py +45 -0
  104. dual_mem-0.1.0/tests/test_types.py +130 -0
  105. dual_mem-0.1.0/tests/test_ultra_e2e.py +79 -0
  106. dual_mem-0.1.0/tests/test_vector_store.py +66 -0
  107. dual_mem-0.1.0/tests/test_writer_lite.py +35 -0
  108. dual_mem-0.1.0/tests/test_writer_lite_with_extractor.py +112 -0
  109. dual_mem-0.1.0/tests/test_writer_pro.py +80 -0
dual_mem-0.1.0/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.
@@ -0,0 +1,280 @@
1
+ Metadata-Version: 2.4
2
+ Name: dual-mem
3
+ Version: 0.1.0
4
+ Summary: Dual-system layered memory SDK (Hy-Memory reproduction)
5
+ Requires-Python: >=3.11
6
+ Description-Content-Type: text/markdown
7
+ License-File: LICENSE
8
+ Requires-Dist: pydantic>=2.6
9
+ Requires-Dist: pydantic-settings>=2.2
10
+ Requires-Dist: pyyaml>=6.0
11
+ Requires-Dist: openai>=1.30
12
+ Requires-Dist: chromadb>=0.5
13
+ Requires-Dist: kuzu>=0.4
14
+ Requires-Dist: scikit-learn>=1.4
15
+ Requires-Dist: numpy>=1.26
16
+ Requires-Dist: rank-bm25>=0.2.2
17
+ Requires-Dist: jieba>=0.42
18
+ Requires-Dist: fastapi>=0.110
19
+ Requires-Dist: uvicorn>=0.29
20
+ Requires-Dist: typer>=0.12
21
+ Requires-Dist: mcp>=1.2
22
+ Requires-Dist: httpx>=0.27
23
+ Provides-Extra: dev
24
+ Requires-Dist: pytest>=8; extra == "dev"
25
+ Requires-Dist: pytest-asyncio>=0.23; extra == "dev"
26
+ Requires-Dist: respx>=0.21; extra == "dev"
27
+ Dynamic: license-file
28
+
29
+ # dual-mem
30
+
31
+ > 分层记忆内核 —— 从零复现 [Hy-Memory](https://github.com/) 的演化链 / System1-System2 / 8 层记忆框架,封装成一套干净、可测、契约对齐的 Python 记忆 SDK。
32
+
33
+ `dual_mem` 是一个面向 LLM 应用 / Agent 的**长期记忆**组件。它把零散的对话沉淀成结构化、可演化、可检索的记忆,并通过 **SDK / REST / MCP / CLI / Skill** 五种方式对外提供能力。
34
+
35
+ 核心能力:
36
+
37
+ - **演化链**:偏好/事实发生变化时(如「最爱 Java」→「转向 Python」、「住上海」→「搬北京」),写侧显式建立 `supersedes`/`superseded_by` 双向指针;旧版本软删保留,检索命中链头时自动展开整条历史(最新→最旧)。
38
+ - **System1(写侧同步认知)**:一次写入内集中所有 LLM 调用 —— 抽取 identity/facts、更新 L0 结构化画像、生成摘要、协调演化链。
39
+ - **System2(ultra 异步沉淀)**:把同领域事实聚类,升维为高阶认知结构(L6 行为 Schema / L7 意图),并支持跨域升维出「核心 Schema」。
40
+ - **三路召回(零 LLM)**:profile(画像/身份/模式)/ proactive(推断意图)/ normal(普通事实知识),叠加演化链展开、BM25+RRF 重排、中文时间词解析、tag 桥接。
41
+
42
+ ---
43
+
44
+ ## 安装
45
+
46
+ 需要 Python ≥ 3.11。
47
+
48
+ ```bash
49
+ git clone <repo-url> dual-mem
50
+ cd dual-mem
51
+ pip install -e . # 运行时依赖
52
+ pip install -e ".[dev]" # 含测试依赖(pytest / pytest-asyncio / respx)
53
+ ```
54
+
55
+ 主要依赖:`pydantic` / `pydantic-settings`(配置),`openai`(LLM/Embed 兼容协议),`chromadb`(向量库),`kuzu`(图库),`scikit-learn`+`numpy`(System2 聚类),`rank-bm25`+`jieba`(中文重排),`fastapi`+`uvicorn`(REST),`typer`(CLI),`mcp`(MCP server)。
56
+
57
+ ---
58
+
59
+ ## 快速开始(SDK)
60
+
61
+ `MemoryClient` 是全异步门面。最小例子:
62
+
63
+ ```python
64
+ import asyncio
65
+ from dual_mem import MemoryClient
66
+
67
+
68
+ async def main():
69
+ client = MemoryClient(mode="pro", storage_dir="./.dual_mem_data")
70
+
71
+ await client.add(
72
+ content="我最爱的编程语言是 Java,已经用了5年。",
73
+ app_id="my_app",
74
+ user_id="alice",
75
+ )
76
+
77
+ res = await client.search(
78
+ query="用户的编程语言偏好",
79
+ app_ids=["my_app"],
80
+ user_id="alice",
81
+ )
82
+ # res["memories"] = {"profile": [...], "proactive": [...], "normal": [...]}
83
+ for m in res["memories"]["profile"]:
84
+ print(m["content"], m.get("evolution_chain"))
85
+
86
+
87
+ asyncio.run(main())
88
+ ```
89
+
90
+ ### 配置(YAML)
91
+
92
+ 配置主源是 YAML 文件,默认读 `~/.dual_mem/config.yaml`(可用环境变量 `DUAL_MEM_CONFIG_FILE` 指定其它路径)。复制仓库根的 `config.example.yaml` 即可:
93
+
94
+ ```bash
95
+ mkdir -p ~/.dual_mem
96
+ cp config.example.yaml ~/.dual_mem/config.yaml
97
+ ```
98
+
99
+ ```yaml
100
+ mode: pro # lite | pro | ultra
101
+ storage_dir: ./.dual_mem_data
102
+ llm_base_url: https://api.openai.com/v1
103
+ llm_api_key: sk-your-llm-key
104
+ llm_model: gpt-4o-mini
105
+ embed_base_url: https://api.openai.com/v1
106
+ embed_api_key: sk-your-embed-key
107
+ embed_model: text-embedding-3-small
108
+ embed_dim: 1536
109
+ auth_disabled: true # REST 本地鉴权开关
110
+ app_whitelist: # REST app_id 白名单
111
+ - default
112
+ system2_trigger_mode: per_write # per_write | manual | scheduled
113
+ ```
114
+
115
+ 优先级:显式传参 `MemoryClient(mode=...)` > `DUAL_MEM_` 前缀环境变量(临时覆盖)> YAML 文件 > 默认值。
116
+
117
+ LLM/Embed 走 OpenAI 兼容协议,`base_url` 可指向任意兼容服务(OpenAI / Hunyuan / 本地推理)。
118
+
119
+ ---
120
+
121
+ ## 三档模式
122
+
123
+ `mode` 自动派生 `agent_mode` 与 `enable_graph`,决定写入深度、LLM 开销与是否启用图库:
124
+
125
+ | 维度 | lite | pro | ultra |
126
+ |---|---|---|---|
127
+ | 一次 `add` 的 LLM 次数 | **0**(仅 embed) | 2~3 | pro + 异步 1~N |
128
+ | 写入层 | 仅 L1_RAW | L0 / L1(SHADOW) / L2 / L3 / L4 | + L5 / L6 / L7(图库) |
129
+ | System2 异步沉淀 | 否 | 否 | **是**(队列 + 跨域 sweeper) |
130
+ | 图库(Kuzu) | 关 | 关 | **开** |
131
+ | Writer | MemoryWriter | MemoryWriter | System2Writer(内部仍跑 System1) |
132
+ | 适用 | 纯检索 / 离线写入 | 个性化助手 | 深度画像 / 认知沉淀 |
133
+
134
+ > lite 与 pro 共用 `MemoryWriter`,差别仅在 `agent_mode`;ultra 用 `System2Writer`,写完 System1 后 fire-and-forget 入 System2 队列,由 `client.digest()` 或调度触发加工。
135
+
136
+ ---
137
+
138
+ ## 五种用法
139
+
140
+ ### 1. SDK
141
+
142
+ 见上「快速开始」。`MemoryClient` 提供 `add / search / get / list / update / delete / delete_bulk / digest`,全异步。
143
+
144
+ ### 2. REST(`dual-mem serve`)
145
+
146
+ ```bash
147
+ dual-mem serve --host 0.0.0.0 --port 8000
148
+ ```
149
+
150
+ 契约对齐 hy-api:`POST /v1/memories/`、`POST /v1/memories/search`、`GET /v1/memories/`、`GET|DELETE /v1/memories/{id}`、`DELETE /v1/memories/`(需 `confirm=true`),外加 `/health` `/ping` `/info`。开启鉴权时需 `Authorization: Bearer <appkey>` 且 `app_id` 在白名单内。
151
+
152
+ ```bash
153
+ curl -X POST http://localhost:8000/v1/memories/ \
154
+ -H "Content-Type: application/json" \
155
+ -d '{"content": "用户喜欢喝咖啡", "app_id": "default", "user_id": "u1"}'
156
+ ```
157
+
158
+ ### 3. MCP(`dual-mem-mcp`)
159
+
160
+ ```bash
161
+ dual-mem-mcp # stdio(供 Cursor/Claude Desktop 经 uvx 拉起)
162
+ dual-mem-mcp --transport streamable-http --port 8765 # HTTP,暴露 /mcp 端点
163
+ ```
164
+
165
+ 暴露工具:`memory_add / memory_search / memory_get / memory_list / memory_delete`,可直接接入支持 MCP 的 Agent 客户端。`memory_search` 返回结果按 profile/proactive/normal 三路分组,演化过的记忆带 `evolution_chain`。
166
+
167
+ Cursor `mcp.json` 最小配置:
168
+
169
+ ```json
170
+ { "mcpServers": { "dual-mem": { "command": "uvx", "args": ["dual-mem-mcp"] } } }
171
+ ```
172
+
173
+ 更多传输模式、uvx、Cursor/Claude Desktop 配置见 [`docs/mcp_integration.md`](docs/mcp_integration.md);整体分层见 [`docs/architecture.md`](docs/architecture.md)。
174
+
175
+ ### 4. CLI
176
+
177
+ ```bash
178
+ dual-mem add --content "用户喜欢喝咖啡" --app-id default --user-id u1
179
+ dual-mem search "用户的饮品偏好" --app-id default --user-id u1
180
+ dual-mem list --app-id default --user-id u1
181
+ dual-mem get <memory_id>
182
+ dual-mem delete <memory_id>
183
+ dual-mem digest # 触发 System2 沉淀(ultra)
184
+ ```
185
+
186
+ ### 5. Skill
187
+
188
+ `skills/dual-mem/SKILL.md` 指导 Agent 何时、如何通过 CLI 或 MCP 读写记忆(调用范式、三路分组用法、演化链解读),可直接装入支持 Skill 的 Agent。
189
+
190
+ ---
191
+
192
+ ## 架构
193
+
194
+ ### 8 层记忆框架(L0–L7)
195
+
196
+ | 层 | 名称 | 含义 |
197
+ |---|---|---|
198
+ | L0 | BASIC_INFO | 结构化画像(name/age/location/occupation/employer),diff-only 演化链 |
199
+ | L1 | RAW | 原始对话(pro/ultra 写后转 SHADOW,避免与 L2/L4 双重命中) |
200
+ | L2 | FACT | 客观事件、经历、计划 |
201
+ | L3 | SUMMARY | 长内容(≥500 字)摘要 |
202
+ | L4 | IDENTITY | 偏好、态度、价值观、人格特质 |
203
+ | L5 | KNOWLEDGE | 领域知识 |
204
+ | L6 | SCHEMA | System2 提炼的行为模式(场景+模式+洞察) |
205
+ | L7 | INTENTION | System2 推断的具体未来意图 |
206
+
207
+ ### 写侧 System1(集中 LLM)
208
+
209
+ ```
210
+ add → 写 L1_RAW → Extractor(identity/facts JSON + L0 tool)
211
+ → Summarizer(≥500 字才出 L3)
212
+ → Reconciler(search_query 改写 → 双路检索 → 判 ADD/SUPERSEDE/DELETE)
213
+ → L1 转 SHADOW
214
+ ```
215
+
216
+ ### 异步 System2(仅 ultra)
217
+
218
+ ```
219
+ digest() → 取 fresh facts → 两阶段 DBSCAN 聚类
220
+ → System2Agent 真 ReAct 循环(OpenAI function-calling 8 工具,tool_choice=auto,
221
+ 上限 system2_max_iters=10 轮,无 tool_calls 即终止)
222
+ 读:search_vdb / search_graph / get_node / expand_node
223
+ 写:create_schema(L6) / create_intention(L7) / add_evidence / add_edge
224
+ → CrossDomainSweeper:基础 Schema ≥5 → 升维出核心 Schema(CROSS_ABSTRACTS_TO)
225
+ ```
226
+
227
+ ### 演化链
228
+
229
+ Reconciler 在写侧显式建 `supersedes`/`superseded_by` 双向指针;`DELETE` 不真删(`is_latest=False` + `SHADOW`),信息无损。读侧命中链中任一节点 → 回溯到链头(is_latest)→ 附 `evolution_chain`(最新→最旧)。**不依赖相似度聚类。**
230
+
231
+ ### 三路召回(零 LLM)
232
+
233
+ | 路 | 召回层 | 说明 |
234
+ |---|---|---|
235
+ | profile | L0 / L4 / L6 | 稳定画像;identity 40% / schema 40% / 自由 20% 配额,`profile_limit=-1` 全量 |
236
+ | proactive | L7 | 推断意图;`intention_limit=0` 时恒空(默认关) |
237
+ | normal | L2 / L5 / L3 / L1 | 普通事实知识;受 `limit` 与 `min_score` 约束,BM25+RRF 重排 |
238
+
239
+ > 返回的 profile/proactive/normal 是**路由分组**,不是层;真实层看每条记忆的 `category` 字段。
240
+
241
+ ---
242
+
243
+ ## 与 Hy-Memory 的差异
244
+
245
+ 本项目复现的是 Hy-Memory 的**能力与行为**,不是源码拷贝。主要差异:
246
+
247
+ 1. **8 层命名对齐代码而非官网**:官网文档 L1–L6 命名相对源码有 ±1 位移,本项目弃用官网命名,采用源码语义的 `L0_BASIC_INFO`…`L7_INTENTION`。
248
+ 2. **REST 契约对齐 hy-api 文档**:源码 REST 是无鉴权的 stdlib `HTTPServer` + 自定义 `/api/v1/add` 内部协议;本项目用 FastAPI 重写为文档约定的 `/v1/memories/` 契约 + Bearer/appkey 鉴权 + 统一错误码。
249
+ 3. **新增 MCP / CLI / Skill / 鉴权 / 测试**:源码这五块完全没有,本项目全新构建(含 128 单测 + e2e smoke)。
250
+ 4. **时间查询解析增强**:源码默认 legacy reader 不解析时间词(横评点名短板,「昨天聊了啥」类几乎全错);本项目默认给 reader 接入中文相对时间解析(`昨天/上周/X月X日` → `created_after` 区间)。
251
+ 5. **System2 工具集对齐**:源码 System2 是 8 工具 ReAct 多轮循环;本项目同样实现为 `chat_with_tools` + `tool_choice=auto` 的真 ReAct 循环(`system2_max_iters=10` 轮上限,无 tool_calls 即终止),早期文档里的「单次 LLM 输出 ops JSON 数组」已废弃。
252
+ 6. **配置收敛**:源码几十个分散的 `MEMORY_*` 环境变量收敛为 `DUAL_MEM_` 前缀的 pydantic-settings,`mode` 自动派生其余开关。
253
+ 7. **砍掉死代码**:源码 4 套 reader(legacy/hybrid/hybrid_tag/exhaustive)、`rdb.py` 桩、`scorer(V1)`、`emotion_analyzer`、`intention_detector` 等只留一套增强 reader。
254
+
255
+ ---
256
+
257
+ ## 已知短板 / 限制
258
+
259
+ - **supersedes 不跨层纠错**:演化链是**层内**链路(L4↔L4、L2↔L2)。L0 若被误抽(如一句话误判用户名),不会被 L2/L4 纠正 —— 如实保留源码行为。
260
+ - **tag_index 图桥接为简化版**:跨域升维(CrossDomainSweeper)用「基础 Schema 数 ≥5 触发 + 一次 LLM 归纳」,而非源码的行为升维 embed + 矩阵碰撞 + Union-Find。
261
+ - **L0 抽取依赖 LLM 质量**:结构化画像的准确性取决于 extract 阶段 LLM 的判断,弱模型可能误抽/漏抽。
262
+ - **Kuzu 无原生向量索引**:L6/L7 的向量召回在 Python 侧做 cosine 全量比对,数据量大时性能受限。
263
+ - **摘要触发阈值**:L3 仅在 content ≥ 500 字才生成;单条短文本 add 几乎不产摘要(messages 形态更易触发)。
264
+
265
+ ---
266
+
267
+ ## 测试与开发
268
+
269
+ ```bash
270
+ # 全量单测(已全程 mock LLM/Embed,不真连网络)
271
+ python -m pytest tests/ -q
272
+
273
+ # 静态检查(__init__ 参数 / 方法名 / 模块路径 / 导出列表一致性)
274
+ python ~/.agents/rules/check_ast.py .
275
+
276
+ # 端到端 smoke(三档 + REST + MCP + CLI,无需 API key)
277
+ python tmp/m7_e2e_demo.py
278
+ ```
279
+
280
+ 测试遵循 TDD,`tests/conftest.py` 注入确定性 `FakeEmbed` + 脚本化 `FakeLLMClient`,绝不真连 LLM/Embed。