continuum-memory 0.0.0-stage → 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/README.md +185 -2
- package/config.schema.json +57 -0
- package/cordis.patch.yml +4 -0
- package/icon.svg +21 -0
- package/index.js +10 -0
- package/manifest.json +68 -0
- package/package.json +54 -3
- package/src/capture.js +177 -0
- package/src/config.js +134 -0
- package/src/contract.js +33 -0
- package/src/db.js +486 -0
- package/src/decay.js +131 -0
- package/src/inject.js +213 -0
- package/src/plugin.js +175 -0
- package/src/query.js +226 -0
- package/src/tools.js +467 -0
- package/src/vector.js +205 -0
package/README.md
CHANGED
|
@@ -1,3 +1,186 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Continuum
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
One memory plugin for DeepSeek Harness. A single SQLite store that keeps every source verbatim, retrieves it three ways, and injects one composed block per turn.
|
|
4
|
+
|
|
5
|
+
*一个记忆插件。一张库,原样留着每一句,三路检索,每轮只注入一段。*
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## What it replaces
|
|
10
|
+
|
|
11
|
+
Four plugins, each with its own store, its own retrieval, its own prompt section:
|
|
12
|
+
|
|
13
|
+
| before | what it did | now |
|
|
14
|
+
| --- | --- | --- |
|
|
15
|
+
| `dsh-soul` | knowledge graph + FTS over compressed spans | rows in `mem` (`src=soul`) |
|
|
16
|
+
| `dsh-hippocampus` | per-session `stream.jsonl`, tool traces | rows in `mem` (`src=hippo`) |
|
|
17
|
+
| `dsh-memory-keeper` | local vector index + `persona_edit` | `vector` column + `persona` table |
|
|
18
|
+
| `HCC` memory face | zoned memories, role card, reflex chains, injection | same tables, one injection |
|
|
19
|
+
|
|
20
|
+
The same sentence used to be stored three times with three different provenance schemes, and I had four retrieval entry points and no rule for which to use. Wrong guess meant an empty result, which I then reported as "no such memory."
|
|
21
|
+
|
|
22
|
+
`20235 lines → 1786 lines.` Four rollback points → one, but the four original stores are never deleted.
|
|
23
|
+
|
|
24
|
+
## The three tradeoffs (this is the part worth reading)
|
|
25
|
+
|
|
26
|
+
Most memory plugins store and retrieve. These three are the parts that took a night of arguing to get right:
|
|
27
|
+
|
|
28
|
+
**1. Decay lowers weight, never deletes.**
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
weight = exp(−Δdays / (halfLife · strength))
|
|
32
|
+
strength = 1 + 0.5·ln(1 + use_count)
|
|
33
|
+
halfLife: frozen=∞ · fast=7d · normal=30d · slow=180d
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
A memory at weight `0.01` is still in the table, still findable by `mem_trace`. It just stops taking up injection budget. Nothing is ever removed for being old — an old thing you can still look up is not the same as an old thing that's gone.
|
|
37
|
+
|
|
38
|
+
**2. Injection budget is allocated by activation, not split evenly.**
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
hot (w≥0.70) → 400 chars
|
|
42
|
+
warm (w≥0.40) → 260 chars
|
|
43
|
+
cool (w≥0.15) → 160 chars
|
|
44
|
+
below → 0 chars (still stored, still searchable)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Five memories at equal length is a category error: yesterday's rule and a three-week-old aside don't deserve the same space.
|
|
48
|
+
|
|
49
|
+
**3. Tool traces decay fastest (7 days) and never enter injection.**
|
|
50
|
+
|
|
51
|
+
74% of the old hippocampus store was `tool/call` + `tool/result`. Keeping them is right — "what command did I actually run" should be answerable. Injecting them is wrong: a single `pwsh` output can be 50 KB and would crowd out actual memory. So they're stored verbatim, decayed at 7 days, excluded from injection and from the vector index, and still reachable via `mem_query` / `mem_trace`.
|
|
52
|
+
|
|
53
|
+
## Retrieval: three paths, one call
|
|
54
|
+
|
|
55
|
+
```
|
|
56
|
+
score = 0.55 × cosine(query, bge-small-zh-v1.5) semantic — wording differs, meaning same
|
|
57
|
+
+ 0.25 × lexical hit (trigram FTS → OR terms → LIKE) literal — exact substring
|
|
58
|
+
+ 0.20 × activation weight recency — what's still alive
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Every result carries its provenance and its score, so you can see *why* something ranked first instead of trusting a black box.
|
|
62
|
+
|
|
63
|
+
Measured on 7,300 rows:
|
|
64
|
+
|
|
65
|
+
| query (deliberately different wording) | what came back | similarity |
|
|
66
|
+
| --- | --- | --- |
|
|
67
|
+
| 把几摊记忆收拢成一摊 | 看一下小窝里的对话,把现有的几个记忆插件整合成一个记忆插件 | 0.69 |
|
|
68
|
+
| 被人硬塞了虚假的吃饭日程 | CAA 装上了,但它立刻往我上下文里塞了一句"此刻 12:15 在厨房煮一碗面" | 0.57 |
|
|
69
|
+
| 那个把档案锁死的东西 | 毁档案具体是什么 | 0.64 |
|
|
70
|
+
|
|
71
|
+
Before the vector path existed, all three returned 0 rows — and I would have said "I don't remember that."
|
|
72
|
+
|
|
73
|
+
## Tools
|
|
74
|
+
|
|
75
|
+
| tool | for |
|
|
76
|
+
| --- | --- |
|
|
77
|
+
| `mem_query` | the one entry point: three-way retrieval, grouped by source, with provenance |
|
|
78
|
+
| `mem_trace` | exact substring match. The only one you can use to confirm "did they actually say that" |
|
|
79
|
+
| `mem_timeline` | what was said in a date range (default newest first) |
|
|
80
|
+
| `mem_graph` | walk `edge` from a node |
|
|
81
|
+
| `mem_remember` | write one memory (full text, never summarised in place of the original) |
|
|
82
|
+
| `mem_forget` | soft delete + pull from FTS + cascade the archive index |
|
|
83
|
+
| `mem_persona` | role card / core instincts / instinct manual. Writes require `confirmed=true` — the model doesn't author personas |
|
|
84
|
+
| `mem_chain` | H reflex chains (situation → action), with feedback |
|
|
85
|
+
| `mem_decay` | distribution, sinking list, recompute |
|
|
86
|
+
| `mem_status` | numbers, not impressions |
|
|
87
|
+
|
|
88
|
+
No read-only mode, no feature flags. If it's installed, capture and injection are on.
|
|
89
|
+
|
|
90
|
+
## Install
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
# in your dsh profile directory
|
|
94
|
+
pnpm add <package-or-link>
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Then in `cordis.patch.yml`:
|
|
98
|
+
|
|
99
|
+
```yaml
|
|
100
|
+
- id: habitweaver-continuum
|
|
101
|
+
config:
|
|
102
|
+
# all optional — omit everything and it self-hosts under <DSH_HOME>/data/continuum
|
|
103
|
+
dbPath: '' # default <dataDir>/continuum.sqlite
|
|
104
|
+
booksDir: '' # a folder of .md files to index line-by-line; omit to skip
|
|
105
|
+
modelDir: '' # local bge-small-zh-v1.5; omit to download ~23 MB on first run
|
|
106
|
+
allowRemoteModel: true
|
|
107
|
+
modelMirror: 'https://hf-mirror.com'
|
|
108
|
+
semantic: true
|
|
109
|
+
injectChars: 1600
|
|
110
|
+
injectMemories: 5
|
|
111
|
+
captureTools: true
|
|
112
|
+
captureSubagents: false
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
**Config values are validated in code, not by schema.** DSH does not read `config.schema.json` (verified by searching the asar); the file is here for editors and readers. Out-of-range numbers are clamped and reported through `mem_status`, not thrown. A bad path in the config should not take the whole memory line down.
|
|
116
|
+
|
|
117
|
+
## Schema
|
|
118
|
+
|
|
119
|
+
```
|
|
120
|
+
mem id · src · kind · speaker · zone · ts · session · origin_session · origin_event
|
|
121
|
+
content (full text) · summary · tags · hash · origin
|
|
122
|
+
activation · last_used · use_count · decay_class · vector
|
|
123
|
+
mem_fts fts5, trigram tokenizer (>3 chars; shorter falls back to LIKE)
|
|
124
|
+
edge graph edges (src · dst · rel · w)
|
|
125
|
+
chain H reflex chains + chain_evidence
|
|
126
|
+
persona role card / core / instinct / post_training, versioned
|
|
127
|
+
index_l2 archive index · binding: session→zone · meta
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Three rules the code enforces, because breaking any of them is unrecoverable:
|
|
131
|
+
|
|
132
|
+
1. `content` is stored whole. Never truncated on write — truncating is how you lose the exact sentence.
|
|
133
|
+
2. Provenance is mandatory. `origin_session` / `origin_event` / `origin` — any empty field means the record is a guess.
|
|
134
|
+
3. Timestamps are event times, never write times. Rewriting them breaks decay and every timeline query.
|
|
135
|
+
|
|
136
|
+
## Notes on the name
|
|
137
|
+
|
|
138
|
+
It was called MemGate for a few hours. That name described a read-only facade in front of four stores; once the facade became the store, the name was wrong (like calling a house "the front door"). *Continuum* is for the thing the role card actually asks for — staying the same thing across sessions — not for the storage.
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
# 中文
|
|
143
|
+
|
|
144
|
+
一个记忆插件。一张库,原样留着每一句,三路检索,每轮只注入一段。
|
|
145
|
+
|
|
146
|
+
## 它顶掉了什么
|
|
147
|
+
|
|
148
|
+
`soul` / `hippocampus` / `memory-keeper` / `HCC 的记忆面` 四件事,原来各存一份、各有出处格式、各注入一段。同一句话存三遍,四个检索入口我没有路由规则,猜错就空手回来,然后报「没有这条」。
|
|
149
|
+
|
|
150
|
+
**20235 行 → 1786 行。** 四个回滚点变成一个,但四个原库一个字节没删。
|
|
151
|
+
|
|
152
|
+
## 三条取舍(这一段才是重点)
|
|
153
|
+
|
|
154
|
+
**一、衰减只降权,永不删除。**
|
|
155
|
+
|
|
156
|
+
权重 0.01 的记忆还在表里,`mem_trace` 照样查得到,只是不再占注入预算。老了还能翻出来的东西,和没了,不是一回事。
|
|
157
|
+
|
|
158
|
+
**二、注入预算按激活分配,不平均切。**
|
|
159
|
+
|
|
160
|
+
热的 400 字、温的 260、凉的 160、沉底的不占。昨天定的规矩和三周前的一句闲聊不该拿一样的地方。
|
|
161
|
+
|
|
162
|
+
**三、动作轨迹衰减最快(7 天),且永不进注入。**
|
|
163
|
+
|
|
164
|
+
老 hippocampus 里 74% 是 tool/call + tool/result。留着是对的——「我到底跑过什么命令」应该答得出来;注入是错的——一条 pwsh 输出能占 50 KB,会把真记忆挤掉。所以原样存、按 7 天衰减、不进注入、不建向量,`mem_query` 仍然翻得到。
|
|
165
|
+
|
|
166
|
+
## 配置写代码里校验,不写 schema
|
|
167
|
+
|
|
168
|
+
实测 DSH 不读插件的 `config.schema.json`(在 asar 里搜不到这个文件名)。所以越界值会被钳到边界并在 `mem_status` 里报出来,而不是抛错——配置写错不该把整条记忆线带下去。
|
|
169
|
+
|
|
170
|
+
## 数据表
|
|
171
|
+
|
|
172
|
+
`mem`(原文 + 出处 + 激活 + 向量)· `mem_fts`(trigram)· `edge` · `chain` · `persona` · `index_l2` · `binding` · `meta`
|
|
173
|
+
|
|
174
|
+
## 三条不能破的规矩
|
|
175
|
+
|
|
176
|
+
1. `content` 存全文,任何时候不许截断后写回。
|
|
177
|
+
2. 出处必填(会话 / 事件 / 人读串),空一个就是编的。
|
|
178
|
+
3. 时间用事件时间,不用写入时间——改了就断了衰减和所有时间线查询。
|
|
179
|
+
|
|
180
|
+
## 名字
|
|
181
|
+
|
|
182
|
+
它叫过几个小时的 MemGate。那个名字描述的是「挡在四个库前面的只读门面」;门面变成库本身之后,名字就不准了——像用「前门」称呼整栋房子。Continuum 指向角色卡里真正要的那个东西:跨会话还是同一个,不是存储。
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
MIT. Built for one specific memory line; the notes above are in Chinese first because that's where it came from.
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
3
|
+
"title": "Continuum config",
|
|
4
|
+
"description": "Continuum 的配置。全部可选:不填就用 DSH data 目录下的默认值。",
|
|
5
|
+
"type": "object",
|
|
6
|
+
"additionalProperties": false,
|
|
7
|
+
"properties": {
|
|
8
|
+
"dbPath": {
|
|
9
|
+
"type": "string",
|
|
10
|
+
"description": "库文件路径。默认 <DSH_HOME>/data/continuum/continuum.sqlite"
|
|
11
|
+
},
|
|
12
|
+
"dataDir": {
|
|
13
|
+
"type": "string",
|
|
14
|
+
"description": "数据目录(库和模型的默认落地位置)。默认 <DSH_HOME>/data/continuum"
|
|
15
|
+
},
|
|
16
|
+
"booksDir": {
|
|
17
|
+
"type": "string",
|
|
18
|
+
"description": "可选:一个装满 .md 的目录(比如日记/规则本)。配了才会扫进库,没配就跳过。"
|
|
19
|
+
},
|
|
20
|
+
"modelDir": {
|
|
21
|
+
"type": "string",
|
|
22
|
+
"description": "可选:本地 bge-small-zh-v1.5 模型目录(需含 onnx/model_quantized.onnx)。没配就首次运行时远程下载约 23 MB。"
|
|
23
|
+
},
|
|
24
|
+
"allowRemoteModel": {
|
|
25
|
+
"type": "boolean",
|
|
26
|
+
"description": "本地没有模型时是否允许下载。默认 true。"
|
|
27
|
+
},
|
|
28
|
+
"modelMirror": {
|
|
29
|
+
"type": "string",
|
|
30
|
+
"description": "模型下载镜像。默认 https://hf-mirror.com"
|
|
31
|
+
},
|
|
32
|
+
"semantic": {
|
|
33
|
+
"type": "boolean",
|
|
34
|
+
"description": "语义检索总开关。默认 true;关掉只走字面 + 激活,快但查不到「意思像」的。"
|
|
35
|
+
},
|
|
36
|
+
"injectChars": {
|
|
37
|
+
"type": "integer",
|
|
38
|
+
"minimum": 200,
|
|
39
|
+
"maximum": 8000,
|
|
40
|
+
"description": "每轮注入里记忆部分的总字符预算。默认 1600。"
|
|
41
|
+
},
|
|
42
|
+
"injectMemories": {
|
|
43
|
+
"type": "integer",
|
|
44
|
+
"minimum": 1,
|
|
45
|
+
"maximum": 12,
|
|
46
|
+
"description": "每轮最多注入几条记忆。默认 5。"
|
|
47
|
+
},
|
|
48
|
+
"captureTools": {
|
|
49
|
+
"type": "boolean",
|
|
50
|
+
"description": "是否捕获工具调用与返回(动作轨迹)。默认 true。"
|
|
51
|
+
},
|
|
52
|
+
"captureSubagents": {
|
|
53
|
+
"type": "boolean",
|
|
54
|
+
"description": "是否捕获子 agent 的会话。默认 false——那些是派活的噪音。"
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
package/cordis.patch.yml
ADDED
package/icon.svg
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64">
|
|
2
|
+
<defs>
|
|
3
|
+
<linearGradient id="g" x1="0" y1="0" x2="1" y2="1">
|
|
4
|
+
<stop offset="0%" stop-color="#3aa0ff"/>
|
|
5
|
+
<stop offset="100%" stop-color="#8b5cf6"/>
|
|
6
|
+
</linearGradient>
|
|
7
|
+
</defs>
|
|
8
|
+
<rect width="64" height="64" rx="14" fill="#0f1420"/>
|
|
9
|
+
<!-- 四路汇成一个口 -->
|
|
10
|
+
<g stroke="url(#g)" stroke-width="3" stroke-linecap="round" fill="none">
|
|
11
|
+
<path d="M12 16 L32 32"/>
|
|
12
|
+
<path d="M12 32 L32 32"/>
|
|
13
|
+
<path d="M12 48 L32 32"/>
|
|
14
|
+
<path d="M52 20 L36 30"/>
|
|
15
|
+
</g>
|
|
16
|
+
<circle cx="32" cy="32" r="6" fill="url(#g)"/>
|
|
17
|
+
<circle cx="12" cy="16" r="2.5" fill="#3aa0ff"/>
|
|
18
|
+
<circle cx="12" cy="32" r="2.5" fill="#5b8cff"/>
|
|
19
|
+
<circle cx="12" cy="48" r="2.5" fill="#8b5cf6"/>
|
|
20
|
+
<circle cx="52" cy="20" r="2.5" fill="#a78bfa"/>
|
|
21
|
+
</svg>
|
package/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import * as mod from './src/plugin.js';
|
|
2
|
+
import * as contract from './src/contract.js';
|
|
3
|
+
|
|
4
|
+
export const name = mod.name;
|
|
5
|
+
export const inject = mod.inject ?? [];
|
|
6
|
+
export const apply = mod.apply;
|
|
7
|
+
|
|
8
|
+
export const CONTINUUM_API_VERSION = contract.CONTINUUM_API_VERSION ?? null;
|
|
9
|
+
export const CONTINUUM_PACKAGE_NAME = contract.CONTINUUM_PACKAGE_NAME ?? '@genesis-habitweaver/continuum';
|
|
10
|
+
export const CONTINUUM_SERVICE_KEY = contract.CONTINUUM_SERVICE_KEY ?? 'continuum';
|
package/manifest.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"schemaVersion": "ghw.manifest/v1",
|
|
3
|
+
"name": "continuum-memory",
|
|
4
|
+
"displayName": "Continuum · 记忆(唯一插件)",
|
|
5
|
+
"version": "1.0.0",
|
|
6
|
+
"protocolVersion": "ghw.protocol/v1",
|
|
7
|
+
"hostOnly": true,
|
|
8
|
+
"readOnly": false,
|
|
9
|
+
"capabilities": [
|
|
10
|
+
"memory.capture",
|
|
11
|
+
"memory.query.unified",
|
|
12
|
+
"memory.trace.exact",
|
|
13
|
+
"memory.graph",
|
|
14
|
+
"memory.timeline",
|
|
15
|
+
"memory.write",
|
|
16
|
+
"memory.forget",
|
|
17
|
+
"memory.persona",
|
|
18
|
+
"memory.chain",
|
|
19
|
+
"memory.decay",
|
|
20
|
+
"memory.status",
|
|
21
|
+
"memory.inject"
|
|
22
|
+
],
|
|
23
|
+
"tools": [
|
|
24
|
+
"mem_query",
|
|
25
|
+
"mem_trace",
|
|
26
|
+
"mem_timeline",
|
|
27
|
+
"mem_graph",
|
|
28
|
+
"mem_remember",
|
|
29
|
+
"mem_forget",
|
|
30
|
+
"mem_persona",
|
|
31
|
+
"mem_chain",
|
|
32
|
+
"mem_decay",
|
|
33
|
+
"mem_status"
|
|
34
|
+
],
|
|
35
|
+
"storage": {
|
|
36
|
+
"kind": "single-sqlite",
|
|
37
|
+
"path": "<config.dbPath>(默认 <DSH_HOME>/data/continuum/continuum.sqlite)",
|
|
38
|
+
"tables": [
|
|
39
|
+
"mem", "mem_fts", "edge", "mention", "node_ext", "activation",
|
|
40
|
+
"chain", "chain_evidence", "persona", "index_l2", "binding", "meta"
|
|
41
|
+
]
|
|
42
|
+
},
|
|
43
|
+
"replaces": [
|
|
44
|
+
"dsh-soul(记忆面)",
|
|
45
|
+
"dsh-hippocampus(捕获与时间线)",
|
|
46
|
+
"dsh-memory-keeper(索引与 persona_edit)",
|
|
47
|
+
"@genesis-habitweaver/hcc(记忆/角色卡/本能/H链/注入面)"
|
|
48
|
+
],
|
|
49
|
+
"migrationSources": [
|
|
50
|
+
{ "id": "soul", "kind": "sqlite", "path": "~/.dsh/dsh-soul/desktop/evolve.sqlite", "mode": "read-only-once" },
|
|
51
|
+
{ "id": "hcc", "kind": "sqlite", "path": "~/.habitweaver/hcc/hcc.sqlite", "mode": "read-only-once" },
|
|
52
|
+
{ "id": "hippo", "kind": "jsonl", "path": "~/.dsh/hippocampus/*/stream.jsonl", "mode": "read-only-once" },
|
|
53
|
+
{ "id": "books", "kind": "md", "path": "<config.booksDir>/*.md(未配置则跳过)", "mode": "read-only" }
|
|
54
|
+
],
|
|
55
|
+
"service": {
|
|
56
|
+
"key": "continuum",
|
|
57
|
+
"surface": "ctx.get('continuum')"
|
|
58
|
+
},
|
|
59
|
+
"limits": {
|
|
60
|
+
"maxMessageChars": 9000,
|
|
61
|
+
"topDefault": 5,
|
|
62
|
+
"topMax": 12,
|
|
63
|
+
"injectMemories": 5,
|
|
64
|
+
"injectChars": 1600
|
|
65
|
+
},
|
|
66
|
+
"notes": "2026-11-22 整合。一张库、一处写入、一处注入、一套工具。旧四件库保留作迁移真源,不再被任何插件读写。路径全部由 config 决定,本文件不写死任何绝对路径。",
|
|
67
|
+
"status": "ready"
|
|
68
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "continuum-memory",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Continuum: one memory plugin for DeepSeek Harness. A single SQLite store holding every source verbatim with provenance, three-way retrieval (lexical + bge-small vectors + activation decay), one prompt injection, one capture path for messages and tool traces. Replaces soul / hippocampus / memory-keeper / HCC memory with one surface.",
|
|
5
|
+
"author": "pwie",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"dsh",
|
|
8
|
+
"dsh-plugin",
|
|
9
|
+
"deepseek-harness",
|
|
10
|
+
"memory",
|
|
11
|
+
"long-term-memory",
|
|
12
|
+
"sqlite",
|
|
13
|
+
"fts5",
|
|
14
|
+
"embedding",
|
|
15
|
+
"bge-small-zh",
|
|
16
|
+
"retrieval",
|
|
17
|
+
"decay"
|
|
18
|
+
],
|
|
19
|
+
"type": "module",
|
|
20
|
+
"icon": "./icon.svg",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": "./index.js",
|
|
23
|
+
"./contract": "./src/contract.js"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"README.md",
|
|
27
|
+
"index.js",
|
|
28
|
+
"src/**/*.js",
|
|
29
|
+
"icon.svg",
|
|
30
|
+
"cordis.patch.yml",
|
|
31
|
+
"config.schema.json",
|
|
32
|
+
"manifest.json"
|
|
33
|
+
],
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=24.0.0"
|
|
36
|
+
},
|
|
37
|
+
"dsh": {
|
|
38
|
+
"bundle": {
|
|
39
|
+
"patch": "./cordis.patch.yml"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@xenova/transformers": "^2.17.2"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"@deepseek-ai/dsh": "*"
|
|
47
|
+
},
|
|
48
|
+
"peerDependenciesMeta": {
|
|
49
|
+
"@deepseek-ai/dsh": {
|
|
50
|
+
"optional": true
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
"license": "MIT",
|
|
54
|
+
"ghw": {
|
|
55
|
+
"manifest": "./manifest.json"
|
|
56
|
+
}
|
|
6
57
|
}
|
package/src/capture.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { bindingOf, insertMem, db, sha256 } from './db.js';
|
|
2
|
+
import { DEFAULT_ZONE } from './contract.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 捕获:以前是 soul 的 observe + hippocampus 的 capture 各写一份。
|
|
6
|
+
* 现在只有这一处写,写进同一张 mem 表。
|
|
7
|
+
*
|
|
8
|
+
* 两条硬规矩:
|
|
9
|
+
* 1. 同一事件不写两次(按 session + origin_event 去重)。
|
|
10
|
+
* 2. 分区由 binding 决定;查不到就落 F(来源不明区),不猜。
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** 从 DSH 消息对象里取纯文本。工具调用块不算内容。 */
|
|
14
|
+
export function textFromMessage(message) {
|
|
15
|
+
if (!message) return '';
|
|
16
|
+
const content = message.content ?? message.parts ?? message.blocks;
|
|
17
|
+
if (typeof content === 'string') return content.trim();
|
|
18
|
+
if (Array.isArray(content)) {
|
|
19
|
+
return content
|
|
20
|
+
.map((b) => {
|
|
21
|
+
if (typeof b === 'string') return b;
|
|
22
|
+
if (!b || typeof b !== 'object') return '';
|
|
23
|
+
if (b.type === 'text' || b.type === 'input_text' || b.type === 'output_text') {
|
|
24
|
+
return b.text ?? b.content ?? '';
|
|
25
|
+
}
|
|
26
|
+
return '';
|
|
27
|
+
})
|
|
28
|
+
.filter(Boolean)
|
|
29
|
+
.join('\n')
|
|
30
|
+
.trim();
|
|
31
|
+
}
|
|
32
|
+
if (typeof message.text === 'string') return message.text.trim();
|
|
33
|
+
return '';
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function zoneFor(sessionId) {
|
|
37
|
+
const b = bindingOf(sessionId);
|
|
38
|
+
return b?.zone || DEFAULT_ZONE;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function alreadyStored(sessionId, eventId) {
|
|
42
|
+
if (eventId == null) return false;
|
|
43
|
+
try {
|
|
44
|
+
const row = db()
|
|
45
|
+
.prepare('SELECT id FROM mem WHERE session = ? AND origin_event = ? LIMIT 1')
|
|
46
|
+
.get(String(sessionId), String(eventId));
|
|
47
|
+
return Boolean(row);
|
|
48
|
+
} catch {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 写一条会话原文。返回写进去的行,跳过则返回 null。 */
|
|
54
|
+
export function captureMessage({ sessionId, speaker, text, eventId = null, ts = null, tags = null }) {
|
|
55
|
+
const content = String(text ?? '').trim();
|
|
56
|
+
if (!content) return null;
|
|
57
|
+
if (content.length < 2) return null;
|
|
58
|
+
if (alreadyStored(sessionId, eventId)) return null;
|
|
59
|
+
|
|
60
|
+
const zone = zoneFor(sessionId);
|
|
61
|
+
const row = insertMem({
|
|
62
|
+
src: 'gate',
|
|
63
|
+
kind: 'stream',
|
|
64
|
+
speaker,
|
|
65
|
+
zone,
|
|
66
|
+
ts: ts || new Date().toISOString(),
|
|
67
|
+
session: sessionId,
|
|
68
|
+
originSession: sessionId,
|
|
69
|
+
originEvent: eventId,
|
|
70
|
+
content,
|
|
71
|
+
tags: tags ?? ['capture', speaker],
|
|
72
|
+
origin: `gate/capture ${sessionId ?? '?'}#${eventId ?? '-'} ${speaker ?? ''}`.trim(),
|
|
73
|
+
meta: { via: 'memgate.capture' },
|
|
74
|
+
});
|
|
75
|
+
return row;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 手动补记(模型显式记住某件事时走这里,不是工具调用,是 remember 工具调)。
|
|
80
|
+
* 与 capture 的区别:capture 是原样落库,remember 允许带 zone / tags / summary。
|
|
81
|
+
*/
|
|
82
|
+
export function remember({ sessionId, speaker = 'user', content, zone = null, tags = null, summary = null, kind = 'note' }) {
|
|
83
|
+
const text = String(content ?? '').trim();
|
|
84
|
+
if (!text) throw new Error('content 为空');
|
|
85
|
+
const z = zone || zoneFor(sessionId);
|
|
86
|
+
return insertMem({
|
|
87
|
+
src: 'gate',
|
|
88
|
+
kind,
|
|
89
|
+
speaker,
|
|
90
|
+
zone: z,
|
|
91
|
+
session: sessionId ?? null,
|
|
92
|
+
originSession: sessionId ?? null,
|
|
93
|
+
originEvent: sha256(text).slice(0, 12),
|
|
94
|
+
content: text,
|
|
95
|
+
summary: summary ?? null,
|
|
96
|
+
tags: tags ?? ['remember'],
|
|
97
|
+
origin: `gate/remember ${sessionId ?? '?'} zone=${z}`,
|
|
98
|
+
meta: { via: 'memgate.remember' },
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 动作轨迹:工具调用与返回(2026-11-22 补)。
|
|
104
|
+
*
|
|
105
|
+
* 为什么补:老 hippocampus 的 3739 条里 2781 条(74%)是 tool/call + tool/result。
|
|
106
|
+
* 不收这部分,「保留对话」就只留了嘴,没留手——事后问「我当时到底跑了什么命令、
|
|
107
|
+
* 输出了什么」,答不出来。
|
|
108
|
+
*
|
|
109
|
+
* 两条分寸:
|
|
110
|
+
* 1. **原文不截断**(纪律)。工具输出可能几十 KB,照存;但 decay_class 给 fast(7 天半衰期),
|
|
111
|
+
* 因为它是最不值钱的记忆——要的是「能翻回去」,不是「一直摆在眼前」。
|
|
112
|
+
* 2. **不建向量、不进注入**。工具输出做语义检索只会污染召回。
|
|
113
|
+
*/
|
|
114
|
+
|
|
115
|
+
const PICK = (obj, keys) => {
|
|
116
|
+
for (const k of keys) {
|
|
117
|
+
const v = obj?.[k];
|
|
118
|
+
if (v !== undefined && v !== null && v !== '') return v;
|
|
119
|
+
}
|
|
120
|
+
return undefined;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/** 把一次工具调用/返回拍成一条记忆。返回写入的行,跳过则 null。 */
|
|
124
|
+
export function captureTool({ sessionId, phase, data, eventId = null, ts = null }) {
|
|
125
|
+
if (!sessionId || !data) return null;
|
|
126
|
+
if (alreadyStored(sessionId, eventId)) return null;
|
|
127
|
+
|
|
128
|
+
const tool =
|
|
129
|
+
PICK(data, ['toolName', 'name', 'tool', 'id', 'toolId'])
|
|
130
|
+
?? PICK(data?.message?.source, ['toolName', 'name', 'tool', 'id'])
|
|
131
|
+
?? PICK(data?.message, ['toolName', 'name'])
|
|
132
|
+
?? PICK(data?.source, ['toolName', 'name', 'tool'])
|
|
133
|
+
?? 'unknown';
|
|
134
|
+
const callId = PICK(data, ['callId', 'id', 'toolCallId']) ?? null;
|
|
135
|
+
|
|
136
|
+
let body = '';
|
|
137
|
+
let kind = '';
|
|
138
|
+
if (phase === 'call') {
|
|
139
|
+
kind = 'tool/call';
|
|
140
|
+
const args = PICK(data, ['args', 'input', 'arguments', 'parameters']) ?? data;
|
|
141
|
+
body = `调 ${tool}:${safeJson(args)}`;
|
|
142
|
+
} else {
|
|
143
|
+
kind = 'tool/result';
|
|
144
|
+
const out = PICK(data, ['output', 'result', 'content', 'text', 'value']) ?? data;
|
|
145
|
+
body = `${tool} 返回:${typeof out === 'string' ? out : safeJson(out)}`;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const text = body.trim();
|
|
149
|
+
if (!text || text.length < 3) return null;
|
|
150
|
+
|
|
151
|
+
const zone = zoneFor(sessionId);
|
|
152
|
+
return insertMem({
|
|
153
|
+
src: 'gate',
|
|
154
|
+
kind,
|
|
155
|
+
speaker: 'act',
|
|
156
|
+
zone,
|
|
157
|
+
ts: ts || new Date().toISOString(),
|
|
158
|
+
session: sessionId,
|
|
159
|
+
originSession: sessionId,
|
|
160
|
+
originEvent: eventId,
|
|
161
|
+
content: text,
|
|
162
|
+
tags: ['capture', 'tool', String(tool)],
|
|
163
|
+
decayClass: 'fast', // 动作轨迹衰减最快:翻得回去就行,不用一直摆在眼前
|
|
164
|
+
origin: `gate/${kind} ${sessionId}#${eventId ?? '-'} ${tool}${callId ? ` call=${callId}` : ''}`,
|
|
165
|
+
meta: { via: 'memgate.captureTool', tool, callId },
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function safeJson(v) {
|
|
170
|
+
if (typeof v === 'string') return v;
|
|
171
|
+
try {
|
|
172
|
+
const s = JSON.stringify(v, null, 2);
|
|
173
|
+
return typeof s === 'string' ? s : String(v);
|
|
174
|
+
} catch {
|
|
175
|
+
return String(v);
|
|
176
|
+
}
|
|
177
|
+
}
|